Skip to content

Commit a3c8ffb

Browse files
committed
Revert "[mlir] Use OpBuilder::createBlock in op builders and patterns (llvm#82770)"
This reverts commit 65d0767.
1 parent aec5ee9 commit a3c8ffb

File tree

20 files changed

+88
-71
lines changed

20 files changed

+88
-71
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
14391439
let extraClassDeclaration = [{
14401440
// Add an entry block to an empty function, and set up the block arguments
14411441
// to match the signature of the function.
1442-
Block *addEntryBlock(OpBuilder &builder);
1442+
Block *addEntryBlock();
14431443

14441444
bool isVarArg() { return getFunctionType().isVarArg(); }
14451445

mlir/include/mlir/Dialect/SPIRV/IR/SPIRVControlFlowOps.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def SPIRV_LoopOp : SPIRV_Op<"mlir.loop", [InFunctionScope]> {
285285

286286
// Adds an empty entry block and loop merge block containing one
287287
// spirv.mlir.merge op.
288-
void addEntryAndMergeBlock(OpBuilder &builder);
288+
void addEntryAndMergeBlock();
289289
}];
290290

291291
let hasOpcode = 0;
@@ -427,7 +427,7 @@ def SPIRV_SelectionOp : SPIRV_Op<"mlir.selection", [InFunctionScope]> {
427427
Block *getMergeBlock();
428428

429429
/// Adds a selection merge block containing one spirv.mlir.merge op.
430-
void addMergeBlock(OpBuilder &builder);
430+
void addMergeBlock();
431431

432432
/// Creates a spirv.mlir.selection op for `if (<condition>) then { <thenBody> }`
433433
/// with `builder`. `builder`'s insertion point will remain at after the

mlir/include/mlir/Interfaces/FunctionInterfaces.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface", [
131131
static void buildWithEntryBlock(
132132
OpBuilder &builder, OperationState &state, StringRef name, Type type,
133133
ArrayRef<NamedAttribute> attrs, TypeRange inputTypes) {
134-
OpBuilder::InsertionGuard g(builder);
135134
state.addAttribute(SymbolTable::getSymbolAttrName(),
136135
builder.getStringAttr(name));
137136
state.addAttribute(ConcreteOp::getFunctionTypeAttrName(state.name),
@@ -140,7 +139,8 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface", [
140139

141140
// Add the function body.
142141
Region *bodyRegion = state.addRegion();
143-
Block *body = builder.createBlock(bodyRegion);
142+
Block *body = new Block();
143+
bodyRegion->push_back(body);
144144
for (Type input : inputTypes)
145145
body->addArgument(input, state.location);
146146
}

mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static void addResumeFunction(ModuleOp module) {
259259
kResume, LLVM::LLVMFunctionType::get(voidTy, {ptrType}));
260260
resumeOp.setPrivate();
261261

262-
auto *block = resumeOp.addEntryBlock(moduleBuilder);
262+
auto *block = resumeOp.addEntryBlock();
263263
auto blockBuilder = ImplicitLocOpBuilder::atBlockEnd(loc, block);
264264

265265
blockBuilder.create<LLVM::CoroResumeOp>(resumeOp.getArgument(0));

mlir/lib/Conversion/ControlFlowToSCF/ControlFlowToSCF.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ ControlFlowToSCFTransformation::createStructuredDoWhileLoopOp(
9898
loc, builder.create<arith::TruncIOp>(loc, builder.getI1Type(), condition),
9999
loopVariablesNextIter);
100100

101-
Block *afterBlock = builder.createBlock(&whileOp.getAfter());
101+
auto *afterBlock = new Block;
102+
whileOp.getAfter().push_back(afterBlock);
102103
afterBlock->addArguments(
103104
loopVariablesInit.getTypes(),
104105
SmallVector<Location>(loopVariablesInit.size(), loc));
106+
builder.setInsertionPointToEnd(afterBlock);
105107
builder.create<scf::YieldOp>(loc, afterBlock->getArguments());
106108

107109
return whileOp.getOperation();

mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static void wrapForExternalCallers(OpBuilder &rewriter, Location loc,
135135
propagateArgResAttrs(rewriter, !!resultStructType, funcOp, wrapperFuncOp);
136136

137137
OpBuilder::InsertionGuard guard(rewriter);
138-
rewriter.setInsertionPointToStart(wrapperFuncOp.addEntryBlock(rewriter));
138+
rewriter.setInsertionPointToStart(wrapperFuncOp.addEntryBlock());
139139

140140
SmallVector<Value, 8> args;
141141
size_t argOffset = resultStructType ? 1 : 0;
@@ -203,7 +203,7 @@ static void wrapExternalFunction(OpBuilder &builder, Location loc,
203203

204204
// The wrapper that we synthetize here should only be visible in this module.
205205
newFuncOp.setLinkage(LLVM::Linkage::Private);
206-
builder.setInsertionPointToStart(newFuncOp.addEntryBlock(builder));
206+
builder.setInsertionPointToStart(newFuncOp.addEntryBlock());
207207

208208
// Get a ValueRange containing arguments.
209209
FunctionType type = cast<FunctionType>(funcOp.getFunctionType());

mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,9 @@ struct GlobalMemrefOpLowering
520520
global, arrayTy, global.getConstant(), linkage, global.getSymName(),
521521
initialValue, alignment, *addressSpace);
522522
if (!global.isExternal() && global.isUninitialized()) {
523-
rewriter.createBlock(&newGlobal.getInitializerRegion());
523+
Block *blk = new Block();
524+
newGlobal.getInitializerRegion().push_back(blk);
525+
rewriter.setInsertionPointToStart(blk);
524526
Value undef[] = {
525527
rewriter.create<LLVM::UndefOp>(global.getLoc(), arrayTy)};
526528
rewriter.create<LLVM::ReturnOp>(global.getLoc(), undef);

mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ struct ForOpConversion final : SCFToSPIRVPattern<scf::ForOp> {
138138
// from header to merge.
139139
auto loc = forOp.getLoc();
140140
auto loopOp = rewriter.create<spirv::LoopOp>(loc, spirv::LoopControl::None);
141-
loopOp.addEntryAndMergeBlock(rewriter);
141+
loopOp.addEntryAndMergeBlock();
142142

143143
OpBuilder::InsertionGuard guard(rewriter);
144144
// Create the block for the header.
145-
Block *header = rewriter.createBlock(&loopOp.getBody(),
146-
getBlockIt(loopOp.getBody(), 1));
147-
rewriter.setInsertionPointAfter(loopOp);
145+
auto *header = new Block();
146+
// Insert the header.
147+
loopOp.getBody().getBlocks().insert(getBlockIt(loopOp.getBody(), 1),
148+
header);
148149

149150
// Create the new induction variable to use.
150151
Value adapLowerBound = adaptor.getLowerBound();
@@ -341,7 +342,7 @@ struct WhileOpConversion final : SCFToSPIRVPattern<scf::WhileOp> {
341342
ConversionPatternRewriter &rewriter) const override {
342343
auto loc = whileOp.getLoc();
343344
auto loopOp = rewriter.create<spirv::LoopOp>(loc, spirv::LoopControl::None);
344-
loopOp.addEntryAndMergeBlock(rewriter);
345+
loopOp.addEntryAndMergeBlock();
345346

346347
Region &beforeRegion = whileOp.getBefore();
347348
Region &afterRegion = whileOp.getAfter();

mlir/lib/Dialect/Affine/IR/AffineOps.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,8 +1809,6 @@ void AffineForOp::build(OpBuilder &builder, OperationState &result,
18091809
"upper bound operand count does not match the affine map");
18101810
assert(step > 0 && "step has to be a positive integer constant");
18111811

1812-
OpBuilder::InsertionGuard guard(builder);
1813-
18141812
// Set variadic segment sizes.
18151813
result.addAttribute(
18161814
getOperandSegmentSizeAttr(),
@@ -1839,11 +1837,12 @@ void AffineForOp::build(OpBuilder &builder, OperationState &result,
18391837
// Create a region and a block for the body. The argument of the region is
18401838
// the loop induction variable.
18411839
Region *bodyRegion = result.addRegion();
1842-
Block *bodyBlock = builder.createBlock(bodyRegion);
1840+
bodyRegion->push_back(new Block);
1841+
Block &bodyBlock = bodyRegion->front();
18431842
Value inductionVar =
1844-
bodyBlock->addArgument(builder.getIndexType(), result.location);
1843+
bodyBlock.addArgument(builder.getIndexType(), result.location);
18451844
for (Value val : iterArgs)
1846-
bodyBlock->addArgument(val.getType(), val.getLoc());
1845+
bodyBlock.addArgument(val.getType(), val.getLoc());
18471846

18481847
// Create the default terminator if the builder is not provided and if the
18491848
// iteration arguments are not provided. Otherwise, leave this to the caller
@@ -1852,9 +1851,9 @@ void AffineForOp::build(OpBuilder &builder, OperationState &result,
18521851
ensureTerminator(*bodyRegion, builder, result.location);
18531852
} else if (bodyBuilder) {
18541853
OpBuilder::InsertionGuard guard(builder);
1855-
builder.setInsertionPointToStart(bodyBlock);
1854+
builder.setInsertionPointToStart(&bodyBlock);
18561855
bodyBuilder(builder, result.location, inductionVar,
1857-
bodyBlock->getArguments().drop_front());
1856+
bodyBlock.getArguments().drop_front());
18581857
}
18591858
}
18601859

@@ -2891,20 +2890,18 @@ void AffineIfOp::build(OpBuilder &builder, OperationState &result,
28912890
TypeRange resultTypes, IntegerSet set, ValueRange args,
28922891
bool withElseRegion) {
28932892
assert(resultTypes.empty() || withElseRegion);
2894-
OpBuilder::InsertionGuard guard(builder);
2895-
28962893
result.addTypes(resultTypes);
28972894
result.addOperands(args);
28982895
result.addAttribute(getConditionAttrStrName(), IntegerSetAttr::get(set));
28992896

29002897
Region *thenRegion = result.addRegion();
2901-
builder.createBlock(thenRegion);
2898+
thenRegion->push_back(new Block());
29022899
if (resultTypes.empty())
29032900
AffineIfOp::ensureTerminator(*thenRegion, builder, result.location);
29042901

29052902
Region *elseRegion = result.addRegion();
29062903
if (withElseRegion) {
2907-
builder.createBlock(elseRegion);
2904+
elseRegion->push_back(new Block());
29082905
if (resultTypes.empty())
29092906
AffineIfOp::ensureTerminator(*elseRegion, builder, result.location);
29102907
}
@@ -3691,7 +3688,6 @@ void AffineParallelOp::build(OpBuilder &builder, OperationState &result,
36913688
"expected upper bound maps to have as many inputs as upper bound "
36923689
"operands");
36933690

3694-
OpBuilder::InsertionGuard guard(builder);
36953691
result.addTypes(resultTypes);
36963692

36973693
// Convert the reductions to integer attributes.
@@ -3737,11 +3733,11 @@ void AffineParallelOp::build(OpBuilder &builder, OperationState &result,
37373733

37383734
// Create a region and a block for the body.
37393735
auto *bodyRegion = result.addRegion();
3740-
Block *body = builder.createBlock(bodyRegion);
3741-
3736+
auto *body = new Block();
37423737
// Add all the block arguments.
37433738
for (unsigned i = 0, e = steps.size(); i < e; ++i)
37443739
body->addArgument(IndexType::get(builder.getContext()), result.location);
3740+
bodyRegion->push_back(body);
37453741
if (resultTypes.empty())
37463742
ensureTerminator(*bodyRegion, builder, result.location);
37473743
}

mlir/lib/Dialect/Async/IR/Async.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void ExecuteOp::getSuccessorRegions(RegionBranchPoint point,
6868
void ExecuteOp::build(OpBuilder &builder, OperationState &result,
6969
TypeRange resultTypes, ValueRange dependencies,
7070
ValueRange operands, BodyBuilderFn bodyBuilder) {
71-
OpBuilder::InsertionGuard guard(builder);
71+
7272
result.addOperands(dependencies);
7373
result.addOperands(operands);
7474

@@ -87,21 +87,26 @@ void ExecuteOp::build(OpBuilder &builder, OperationState &result,
8787

8888
// Add a body region with block arguments as unwrapped async value operands.
8989
Region *bodyRegion = result.addRegion();
90-
Block *bodyBlock = builder.createBlock(bodyRegion);
90+
bodyRegion->push_back(new Block);
91+
Block &bodyBlock = bodyRegion->front();
9192
for (Value operand : operands) {
9293
auto valueType = llvm::dyn_cast<ValueType>(operand.getType());
93-
bodyBlock->addArgument(valueType ? valueType.getValueType()
94-
: operand.getType(),
95-
operand.getLoc());
94+
bodyBlock.addArgument(valueType ? valueType.getValueType()
95+
: operand.getType(),
96+
operand.getLoc());
9697
}
9798

9899
// Create the default terminator if the builder is not provided and if the
99100
// expected result is empty. Otherwise, leave this to the caller
100101
// because we don't know which values to return from the execute op.
101102
if (resultTypes.empty() && !bodyBuilder) {
103+
OpBuilder::InsertionGuard guard(builder);
104+
builder.setInsertionPointToStart(&bodyBlock);
102105
builder.create<async::YieldOp>(result.location, ValueRange());
103106
} else if (bodyBuilder) {
104-
bodyBuilder(builder, result.location, bodyBlock->getArguments());
107+
OpBuilder::InsertionGuard guard(builder);
108+
builder.setInsertionPointToStart(&bodyBlock);
109+
bodyBuilder(builder, result.location, bodyBlock.getArguments());
105110
}
106111
}
107112

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,20 @@ LogicalResult ExpressionOp::verify() {
270270

271271
void ForOp::build(OpBuilder &builder, OperationState &result, Value lb,
272272
Value ub, Value step, BodyBuilderFn bodyBuilder) {
273-
OpBuilder::InsertionGuard g(builder);
274273
result.addOperands({lb, ub, step});
275274
Type t = lb.getType();
276275
Region *bodyRegion = result.addRegion();
277-
Block *bodyBlock = builder.createBlock(bodyRegion);
278-
bodyBlock->addArgument(t, result.location);
276+
bodyRegion->push_back(new Block);
277+
Block &bodyBlock = bodyRegion->front();
278+
bodyBlock.addArgument(t, result.location);
279279

280280
// Create the default terminator if the builder is not provided.
281281
if (!bodyBuilder) {
282282
ForOp::ensureTerminator(*bodyRegion, builder, result.location);
283283
} else {
284284
OpBuilder::InsertionGuard guard(builder);
285-
builder.setInsertionPointToStart(bodyBlock);
286-
bodyBuilder(builder, result.location, bodyBlock->getArgument(0));
285+
builder.setInsertionPointToStart(&bodyBlock);
286+
bodyBuilder(builder, result.location, bodyBlock.getArgument(0));
287287
}
288288
}
289289

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,10 +2166,11 @@ LogicalResult ShuffleVectorOp::verify() {
21662166
//===----------------------------------------------------------------------===//
21672167

21682168
// Add the entry block to the function.
2169-
Block *LLVMFuncOp::addEntryBlock(OpBuilder &builder) {
2169+
Block *LLVMFuncOp::addEntryBlock() {
21702170
assert(empty() && "function already has an entry block");
2171-
OpBuilder::InsertionGuard g(builder);
2172-
Block *entry = builder.createBlock(&getBody());
2171+
2172+
auto *entry = new Block;
2173+
push_back(entry);
21732174

21742175
// FIXME: Allow passing in proper locations for the entry arguments.
21752176
LLVMFunctionType type = getFunctionType();

mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ struct MoveInitOperandsToInput : public OpRewritePattern<GenericOp> {
132132
newIndexingMaps, genericOp.getIteratorTypesArray(),
133133
/*bodyBuild=*/nullptr, linalg::getPrunedAttributeList(genericOp));
134134

135-
OpBuilder::InsertionGuard guard(rewriter);
136135
Region &region = newOp.getRegion();
137-
Block *block = rewriter.createBlock(&region);
136+
Block *block = new Block();
137+
region.push_back(block);
138138
IRMapping mapper;
139+
OpBuilder::InsertionGuard guard(rewriter);
140+
rewriter.setInsertionPointToStart(block);
139141
for (auto bbarg : genericOp.getRegionInputArgs())
140142
mapper.map(bbarg, block->addArgument(bbarg.getType(), loc));
141143

mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,11 @@ static void generateFusedElementwiseOpRegion(
178178
// Build the region of the fused op.
179179
Block &producerBlock = producer->getRegion(0).front();
180180
Block &consumerBlock = consumer->getRegion(0).front();
181-
OpBuilder::InsertionGuard guard(rewriter);
182-
Block *fusedBlock = rewriter.createBlock(&fusedOp.getRegion());
181+
Block *fusedBlock = new Block();
182+
fusedOp.getRegion().push_back(fusedBlock);
183183
IRMapping mapper;
184+
OpBuilder::InsertionGuard guard(rewriter);
185+
rewriter.setInsertionPointToStart(fusedBlock);
184186

185187
// 2. Add an index operation for every fused loop dimension and use the
186188
// `consumerToProducerLoopsMap` to map the producer indices.

mlir/lib/Dialect/Linalg/Utils/Utils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,14 @@ GenericOp makeTransposeOp(OpBuilder &b, Location loc, Value inputTensor,
275275
auto transposeOp =
276276
b.create<GenericOp>(loc, resultTensorType, inputTensor, outputTensor,
277277
indexingMaps, iteratorTypes);
278+
Region &body = transposeOp.getRegion();
279+
body.push_back(new Block());
280+
body.front().addArguments({elementType, elementType}, {loc, loc});
278281

279282
// Create the body of the transpose operation.
280283
OpBuilder::InsertionGuard g(b);
281-
Region &body = transposeOp.getRegion();
282-
Block *bodyBlock = b.createBlock(&body, /*insertPt=*/{},
283-
{elementType, elementType}, {loc, loc});
284-
b.create<YieldOp>(loc, bodyBlock->getArgument(0));
284+
b.setInsertionPointToEnd(&body.front());
285+
b.create<YieldOp>(loc, transposeOp.getRegion().front().getArgument(0));
285286
return transposeOp;
286287
}
287288

mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,6 @@ OpFoldResult ExtractStridedMetadataOp::getConstifiedMixedOffset() {
14201420

14211421
void GenericAtomicRMWOp::build(OpBuilder &builder, OperationState &result,
14221422
Value memref, ValueRange ivs) {
1423-
OpBuilder::InsertionGuard g(builder);
14241423
result.addOperands(memref);
14251424
result.addOperands(ivs);
14261425

@@ -1429,7 +1428,7 @@ void GenericAtomicRMWOp::build(OpBuilder &builder, OperationState &result,
14291428
result.addTypes(elementType);
14301429

14311430
Region *bodyRegion = result.addRegion();
1432-
builder.createBlock(bodyRegion);
1431+
bodyRegion->push_back(new Block());
14331432
bodyRegion->addArgument(elementType, memref.getLoc());
14341433
}
14351434
}

mlir/lib/Dialect/SPIRV/IR/ControlFlowOps.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,12 @@ Block *LoopOp::getMergeBlock() {
365365
return &getBody().back();
366366
}
367367

368-
void LoopOp::addEntryAndMergeBlock(OpBuilder &builder) {
368+
void LoopOp::addEntryAndMergeBlock() {
369369
assert(getBody().empty() && "entry and merge block already exist");
370-
OpBuilder::InsertionGuard g(builder);
371-
builder.createBlock(&getBody());
372-
builder.createBlock(&getBody());
370+
getBody().push_back(new Block());
371+
auto *mergeBlock = new Block();
372+
getBody().push_back(mergeBlock);
373+
OpBuilder builder = OpBuilder::atBlockEnd(mergeBlock);
373374

374375
// Add a spirv.mlir.merge op into the merge block.
375376
builder.create<spirv::MergeOp>(getLoc());
@@ -524,10 +525,11 @@ Block *SelectionOp::getMergeBlock() {
524525
return &getBody().back();
525526
}
526527

527-
void SelectionOp::addMergeBlock(OpBuilder &builder) {
528+
void SelectionOp::addMergeBlock() {
528529
assert(getBody().empty() && "entry and merge block already exist");
529-
OpBuilder::InsertionGuard guard(builder);
530-
builder.createBlock(&getBody());
530+
auto *mergeBlock = new Block();
531+
getBody().push_back(mergeBlock);
532+
OpBuilder builder = OpBuilder::atBlockEnd(mergeBlock);
531533

532534
// Add a spirv.mlir.merge op into the merge block.
533535
builder.create<spirv::MergeOp>(getLoc());
@@ -540,7 +542,7 @@ SelectionOp::createIfThen(Location loc, Value condition,
540542
auto selectionOp =
541543
builder.create<spirv::SelectionOp>(loc, spirv::SelectionControl::None);
542544

543-
selectionOp.addMergeBlock(builder);
545+
selectionOp.addMergeBlock();
544546
Block *mergeBlock = selectionOp.getMergeBlock();
545547
Block *thenBlock = nullptr;
546548

0 commit comments

Comments
 (0)