16#include <mlir/CAPI/IR.h>
17#include <mlir/CAPI/Support.h>
18#include <mlir/CAPI/Wrap.h>
19#include <mlir/IR/Builders.h>
29 auto *block = point.getBlock();
31 return {.block = {.ptr =
nullptr}, .point = {.ptr =
nullptr}};
34 if (point.getPoint() == block->end()) {
35 return {.block = wrap(block), .point = {.ptr =
nullptr}};
38 return {.block = wrap(block), .point = wrap(&*point.getPoint())};
43class ListenerT :
public OpBuilder::Listener {
46 : opInsertedCb(op), blockInsertedCb(block), userData(data) {}
50 void notifyOperationInserted(Operation *op, OpBuilder::InsertPoint previous)
final {
51 opInsertedCb(wrap(op), wrapInsertPoint(previous), userData);
56 void notifyBlockInserted(Block *block, Region *previous, Region::iterator previousIt)
final {
58 wrap(block), wrap(previous),
59 (!previous || previous->end() == previousIt) ? MlirBlock {.ptr = nullptr}
68 void *userData =
nullptr;
79 return MlirOpBuilder {.ptr =
new OpBuilderT(unwrap(ctx))};
85 auto *l =
reinterpret_cast<ListenerT *
>(listener.ptr);
86 return MlirOpBuilder {.ptr =
new OpBuilderT(unwrap(ctx), l)};
91 delete reinterpret_cast<OpBuilderT *
>(builder.ptr);
96 return wrap(unwrap(builder)->getContext());
102 unwrap(builder)->setInsertionPointToStart(unwrap(block));
107 unwrap(builder)->setInsertionPointToEnd(unwrap(block));
112 unwrap(builder)->setInsertionPoint(unwrap(operation));
117 unwrap(builder)->setInsertionPointAfter(unwrap(operation));
122 unwrap(builder)->setInsertionPointAfterValue(unwrap(value));
127 return wrapInsertPoint(unwrap(builder)->saveInsertionPoint());
132 auto *block = unwrap(rawIp.
block);
134 OpBuilderT::InsertPoint ip;
135 unwrap(builder)->restoreInsertionPoint(ip);
137 Block::iterator it = rawIp.
point.ptr ? Block::iterator(unwrap(rawIp.
point)) : block->end();
138 OpBuilderT::InsertPoint ip(block, it);
139 unwrap(builder)->restoreInsertionPoint(ip);
145 unwrap(builder)->clearInsertionPoint();
151 auto it = unwrap(builder)->getInsertionPoint();
152 auto *blk = unwrap(builder)->getInsertionBlock();
154 return MlirOperation {
nullptr};
157 return wrap(it != blk->end() ? &*it :
nullptr);
163 return wrap(unwrap(builder)->getInsertionBlock());
168 return wrap(unwrap(builder)->insert(unwrap(op)));
181 return MlirOpBuilderListener {.ptr =
new ListenerT(opCb, blockCb, userData)};
186 delete reinterpret_cast<ListenerT *
>(listener.ptr);
void mlirOpBuilderRestoreInsertionPoint(MlirOpBuilder builder, MlirOpBuilderInsertPoint rawIp)
Restore the insert point to a previously saved point.
void mlirOpBuilderDestroy(MlirOpBuilder builder)
Destroys builder and frees the underlying OpBuilder object.
void mlirOpBuilderClearInsertionPoint(MlirOpBuilder builder)
Reset the insertion point to no location.
MlirOpBuilder mlirOpBuilderCreate(MlirContext ctx)
Creates a new OpBuilder for the given MLIR context.
MlirOpBuilder mlirOpBuilderCreateWithListener(MlirContext ctx, MlirOpBuilderListener listener)
Creates a new OpBuilder for the given MLIR context and attaches listener so that operation and block ...
void mlirOpBuilderSetInsertionPointToStart(MlirOpBuilder builder, MlirBlock block)
Sets the insertion point of builder to the beginning of block so that subsequent insertions prepend t...
MlirOpBuilderInsertPoint mlirOpBuilderSaveInsertionPoint(MlirOpBuilder builder)
Return a saved insertion point.
void mlirOpBuilderSetInsertionPointAfter(MlirOpBuilder builder, MlirOperation operation)
Sets the insertion point right after the given operation.
MlirOperation mlirOpBuilderGetInsertionPoint(MlirOpBuilder builder)
Returns the operation before which new operations will be inserted, or a null MlirOperation if there ...
void mlirOpBuilderSetInsertionPointToEnd(MlirOpBuilder builder, MlirBlock block)
Sets the insertion point to the end of the given block.
void mlirOpBuilderListenerDestroy(MlirOpBuilderListener listener)
Destroys listener and frees the underlying ListenerT object.
MlirBlock mlirOpBuilderGetInsertionBlock(MlirOpBuilder builder)
Returns the block that builder is currently inserting into, or a null MlirBlock if no insertion point...
void mlirOpBuilderSetInsertionPoint(MlirOpBuilder builder, MlirOperation operation)
Sets the insertion point right before the given operation.
MlirOpBuilderListener mlirOpBuilderListenerCreate(MlirNotifyOperationInserted opCb, MlirNotifyBlockInserted blockCb, void *userData)
Creates a new OpBuilder::Listener that calls opCb whenever an operation is inserted and blockCb whene...
MlirContext mlirOpBuilderGetContext(MlirOpBuilder builder)
Returns the MLIR context associated with builder.
MlirOperation mlirOpBuilderInsert(MlirOpBuilder builder, MlirOperation op)
Inserts op at the current insertion point of builder and returns it.
void mlirOpBuilderSetInsertionPointAfterValue(MlirOpBuilder builder, MlirValue value)
Sets the insertion point right after the given value is defined.
void(* MlirNotifyOperationInserted)(MlirOperation, MlirOpBuilderInsertPoint, void *)
Callback type for listening to operation insertions in an mlir::OpBuilder.
void(* MlirNotifyBlockInserted)(MlirBlock, MlirRegion, MlirBlock, void *)
Callback type for listening to block insertions in an mlir::OpBuilder.
Current insertion point of an mlir::OpBuilder instance represented as a block and an operation within...
MlirOperation point
The operation that the builder is inserting before.
MlirBlock block
The block that the builder is inserting into.