Skip to content

Commit

Permalink
[MLIR] Add primitive builders for scf.if
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D144886
  • Loading branch information
frgossen committed Feb 27, 2023
1 parent 0a0b6fa commit e7b52c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
Expand Up @@ -773,6 +773,8 @@ def IfOp : SCF_Op<"if", [DeclareOpInterfaceMethods<RegionBranchOpInterface, [
let skipDefaultBuilders = 1;
let builders = [
OpBuilder<(ins "TypeRange":$resultTypes, "Value":$cond)>,
OpBuilder<(ins "TypeRange":$resultTypes, "Value":$cond,
"bool":$addThenBlock, "bool":$addElseBlock)>,
OpBuilder<(ins "Value":$cond, "bool":$withElseRegion)>,
OpBuilder<(ins "TypeRange":$resultTypes, "Value":$cond,
"bool":$withElseRegion)>,
Expand Down
19 changes: 16 additions & 3 deletions mlir/lib/Dialect/SCF/IR/SCF.cpp
Expand Up @@ -1593,13 +1593,26 @@ IfOp::inferReturnTypes(MLIRContext *ctx, std::optional<Location> loc,

void IfOp::build(OpBuilder &builder, OperationState &result,
TypeRange resultTypes, Value cond) {
return build(builder, result, resultTypes, cond, /*addThenBlock=*/false,
/*addElseBlock=*/false);
}

void IfOp::build(OpBuilder &builder, OperationState &result,
TypeRange resultTypes, Value cond, bool addThenBlock,
bool addElseBlock) {
assert((!addElseBlock || addThenBlock) &&
"must not create else block w/o then block");
result.addTypes(resultTypes);
result.addOperands(cond);

// Build regions.
// Add regions and blocks.
OpBuilder::InsertionGuard guard(builder);
result.addRegion();
result.addRegion();
Region *thenRegion = result.addRegion();
if (addThenBlock)
builder.createBlock(thenRegion);
Region *elseRegion = result.addRegion();
if (addElseBlock)
builder.createBlock(elseRegion);
}

void IfOp::build(OpBuilder &builder, OperationState &result, Value cond,
Expand Down

0 comments on commit e7b52c4

Please sign in to comment.