0 | {--
 1 | Copyright (C) 2025  Joel Berkeley
 2 |
 3 | This program is free software: you can redistribute it and/or modify
 4 | it under the terms of the GNU Affero General Public License as published
 5 | by the Free Software Foundation, either version 3 of the License, or
 6 | (at your option) any later version.
 7 |
 8 | This program is distributed in the hope that it will be useful,
 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 | GNU Affero General Public License for more details.
12 |
13 | You should have received a copy of the GNU Affero General Public License
14 | along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 | --}
16 | ||| For internal spidr use only.
17 | module Compiler.MLIR.IR.BuiltinAttributes
18 |
19 | import Compiler.LLVM.ADT.APFloat
20 | import Compiler.LLVM.ADT.APInt
21 | import Compiler.MLIR.IR.BuiltinTypeInterfaces
22 | import Compiler.FFI
23 |
24 | ffi : String -> String
25 | ffi = libxla "c/mlir/IR/BuiltinAttributes.h"
26 |
27 | public export
28 | data DenseElementsAttr = MkDenseElementsAttr GCAnyPtr
29 |
30 | export
31 | %foreign (ffi "DenseElementsAttr_delete")
32 | prim__deleteDenseElementsAttr : AnyPtr -> PrimIO ()
33 |
34 | namespace DenseElementsAttr
35 |   namespace APInt
36 |     %foreign (ffi "DenseElementsAttr_get_APInt")
37 |     prim__denseElementsAttrGet : GCAnyPtr -> GCAnyPtr -> PrimIO AnyPtr
38 |
39 |     export
40 |     get : HasIO io => ShapedType -> APInt -> io DenseElementsAttr
41 |     get (MkShapedType st) (MkAPInt value) = do
42 |       attr <- primIO $ prim__denseElementsAttrGet st value
43 |       attr <- onCollectAny' attr (primIO . prim__deleteDenseElementsAttr)
44 |       pure (MkDenseElementsAttr attr)
45 |
46 |   namespace APFloat
47 |     %foreign (ffi "DenseElementsAttr_get_APFloat")
48 |     prim__denseElementsAttrGet : GCAnyPtr -> GCAnyPtr -> PrimIO AnyPtr
49 |
50 |     export
51 |     get : HasIO io => ShapedType -> APFloat -> io DenseElementsAttr
52 |     get (MkShapedType st) (MkAPFloat value) = do
53 |       attr <- primIO $ prim__denseElementsAttrGet st value
54 |       attr <- onCollectAny' attr (primIO . prim__deleteDenseElementsAttr)
55 |       pure (MkDenseElementsAttr attr)
56 |