17 | module Compiler.Enzyme.MLIR.Dialect.Ops
20 | import Compiler.Array
21 | import Compiler.MLIR.IR.Builders
22 | import Compiler.MLIR.IR.Location
23 | import Compiler.MLIR.IR.MLIRContext
24 | import Compiler.MLIR.IR.Operation
25 | import Compiler.MLIR.IR.OpDefinition
26 | import Compiler.MLIR.IR.Region
27 | import Compiler.MLIR.IR.TypeRange
28 | import Compiler.MLIR.IR.ValueRange
30 | ffi : String -> String
31 | ffi = libxla "c/Enzyme/MLIR/Dialect/Ops.h"
33 | %foreign (ffi "AutoDiffRegionOp_delete")
34 | prim__deleteAutoDiffRegionOp : AnyPtr -> PrimIO ()
36 | %foreign (ffi "AutoDiffRegionOp_create")
37 | prim__autoDiffRegionOpCreate :
38 | GCAnyPtr -> AnyPtr -> GCAnyPtr -> GCAnyPtr -> GCAnyPtr -> Int -> Int -> PrimIO AnyPtr
41 | data AutoDiffRegionOp = MkAutoDiffRegionOp GCAnyPtr
49 | | EnzymeActivenoneed
52 | Cast Activity Int where
57 | EnzymeDupnoneed => 3
58 | EnzymeActivenoneed => 4
59 | EnzymeConstnoneed => 5
61 | namespace AutoDiffRegionOp
74 | (MkOpBuilder builder)
76 | (MkLocation location)
77 | (MkTypeRange outputs)
78 | (MkValueRange inputs)
81 | op <- primIO $
prim__autoDiffRegionOpCreate
82 | builder ctx location outputs inputs (cast activity) (cast retActivity)
83 | op <- onCollectAny' op (primIO . prim__deleteAutoDiffRegionOp)
84 | pure (MkAutoDiffRegionOp op)
86 | %foreign (ffi "AutoDiffRegionOp_getBody")
87 | prim__autoDiffRegionOpGetBody : GCAnyPtr -> AnyPtr
90 | getBody : AutoDiffRegionOp -> Region
91 | getBody (MkAutoDiffRegionOp op) = MkRegion $
prim__autoDiffRegionOpGetBody op
93 | %foreign (ffi "AutoDiffRegionOp_getOperation")
94 | prim__autoDiffRegionOpGetOperation : GCAnyPtr -> PrimIO AnyPtr
97 | Op AutoDiffRegionOp where
98 | getOperation (MkAutoDiffRegionOp op) =
99 | MkOperation <$> (primIO $
prim__autoDiffRegionOpGetOperation op)
101 | %foreign (ffi "YieldOp_delete")
102 | prim__deleteYieldOp : AnyPtr -> PrimIO ()
104 | %foreign (ffi "YieldOp_create")
105 | prim__yieldOpCreate : GCAnyPtr -> GCAnyPtr -> GCAnyPtr -> PrimIO AnyPtr
108 | data YieldOp = MkYieldOp GCAnyPtr
112 | create : HasIO io => OpBuilder -> Location -> ValueRange -> io YieldOp
113 | create (MkOpBuilder builder) (MkLocation location) (MkValueRange operands) = do
114 | op <- primIO $
prim__yieldOpCreate builder location operands
115 | op <- onCollectAny' op (primIO . prim__deleteYieldOp)
116 | pure (MkYieldOp op)
118 | %foreign (ffi "BatchOp_delete")
119 | prim__deleteBatchOp : AnyPtr -> PrimIO ()
121 | %foreign (ffi "BatchOp_create")
122 | prim__batchOpCreate :
134 | data BatchOp = MkBatchOp GCAnyPtr
149 | (MkOpBuilder builder)
150 | (MkMLIRContext ctx)
151 | (MkLocation location)
152 | (MkTypeRange outputs)
154 | (MkValueRange inputs)
156 | MkArray batchShape batchShapeLen <- fromList $
cast {to = Int64} <$> batchShape
157 | op <- primIO $
prim__batchOpCreate
158 | builder ctx location outputs fn inputs batchShape batchShapeLen
159 | op <- onCollectAny' op (primIO . prim__deleteBatchOp)
160 | pure (MkBatchOp op)
162 | %foreign (ffi "BatchOp_getOperation")
163 | prim__batchOpGetOperation : GCAnyPtr -> PrimIO AnyPtr
167 | getOperation (MkBatchOp op) = MkOperation <$> (primIO $
prim__batchOpGetOperation op)