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.BuiltinOps
18 |
19 | import Compiler.LLVM.Support.RawOStream
20 | import Compiler.MLIR.IR.Location
21 | import Compiler.MLIR.IR.MLIRContext
22 | import Compiler.MLIR.IR.OpDefinition
23 | import Compiler.MLIR.IR.Operation
24 | import Compiler.FFI
25 |
26 | ffi : String -> String
27 | ffi = libxla "c/mlir/IR/BuiltinOps.h"
28 |
29 | public export
30 | data ModuleOp = MkModuleOp GCAnyPtr
31 |
32 | export
33 | %foreign (ffi "ModuleOp_delete")
34 | prim__delete : AnyPtr -> PrimIO ()
35 |
36 | %foreign (ffi "ModuleOp_create")
37 | prim__moduleOpCreate : GCAnyPtr -> String -> PrimIO AnyPtr
38 |
39 | namespace ModuleOp
40 |   export
41 |   create : HasIO io => Location -> String -> io ModuleOp
42 |   create (MkLocation loc) name = do
43 |     op <- primIO $ prim__moduleOpCreate loc name
44 |     op <- onCollectAny' op (primIO . prim__delete)
45 |     pure (MkModuleOp op)
46 |
47 | %foreign (ffi "ModuleOp_dump")
48 | prim__moduleOpDump : GCAnyPtr -> PrimIO ()
49 |
50 | export
51 | dump : HasIO io => ModuleOp -> io ()
52 | dump (MkModuleOp op) = primIO $ prim__moduleOpDump op
53 |
54 | export
55 | %foreign (ffi "ModuleOp_getOperation")
56 | prim__moduleOpGetOperation : GCAnyPtr -> PrimIO AnyPtr
57 |
58 | export
59 | Op ModuleOp where
60 |   getOperation (MkModuleOp moduleOp) =
61 |     MkOperation <$> (primIO $ prim__moduleOpGetOperation moduleOp)
62 |
63 | export
64 | %foreign (ffi "ModuleOp_push_back")
65 | prim__moduleOpPushBack : GCAnyPtr -> AnyPtr -> PrimIO ()
66 |
67 | export
68 | pushBack : HasIO io => ModuleOp -> Operation -> io ()
69 | pushBack (MkModuleOp mOp) (MkOperation op) = primIO $ prim__moduleOpPushBack mOp op
70 |