Skip to content

Commit 492e8ba

Browse files
[mlir] Fix memory leaks after #81759 (#82762)
This commit fixes memory leaks that were introduced by #81759. The way ops and blocks are erased changed slightly. The leaks were caused by an incorrect implementation of op builders: blocks must be created with the supplied builder object. Otherwise, they are not properly tracked by the dialect conversion and can leak during rollback.
1 parent ad49fe3 commit 492e8ba

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

mlir/lib/Dialect/GPU/IR/GPUDialect.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ void LaunchOp::build(OpBuilder &builder, OperationState &result,
648648
TypeRange workgroupAttributions,
649649
TypeRange privateAttributions, Value clusterSizeX,
650650
Value clusterSizeY, Value clusterSizeZ) {
651+
OpBuilder::InsertionGuard g(builder);
652+
651653
// Add a WorkGroup attribution attribute. This attribute is required to
652654
// identify private attributions in the list of block argguments.
653655
result.addAttribute(getNumWorkgroupAttributionsAttrName(),
@@ -674,7 +676,7 @@ void LaunchOp::build(OpBuilder &builder, OperationState &result,
674676
// attributions, where the first kNumConfigRegionAttributes arguments have
675677
// `index` type and the rest have the same types as the data operands.
676678
Region *kernelRegion = result.addRegion();
677-
Block *body = new Block();
679+
Block *body = builder.createBlock(kernelRegion);
678680
// TODO: Allow passing in proper locations here.
679681
for (unsigned i = 0; i < kNumConfigRegionAttributes; ++i)
680682
body->addArgument(builder.getIndexType(), result.location);
@@ -683,7 +685,6 @@ void LaunchOp::build(OpBuilder &builder, OperationState &result,
683685
body->addArgument(argTy, result.location);
684686
for (Type argTy : privateAttributions)
685687
body->addArgument(argTy, result.location);
686-
kernelRegion->push_back(body);
687688
// Fill OperandSegmentSize Attribute.
688689
SmallVector<int32_t, 11> segmentSizes(11, 1);
689690
segmentSizes.front() = asyncDependencies.size();
@@ -1325,6 +1326,8 @@ void GPUFuncOp::build(OpBuilder &builder, OperationState &result,
13251326
TypeRange workgroupAttributions,
13261327
TypeRange privateAttributions,
13271328
ArrayRef<NamedAttribute> attrs) {
1329+
OpBuilder::InsertionGuard g(builder);
1330+
13281331
result.addAttribute(SymbolTable::getSymbolAttrName(),
13291332
builder.getStringAttr(name));
13301333
result.addAttribute(getFunctionTypeAttrName(result.name),
@@ -1333,7 +1336,7 @@ void GPUFuncOp::build(OpBuilder &builder, OperationState &result,
13331336
builder.getI64IntegerAttr(workgroupAttributions.size()));
13341337
result.addAttributes(attrs);
13351338
Region *body = result.addRegion();
1336-
Block *entryBlock = new Block;
1339+
Block *entryBlock = builder.createBlock(body);
13371340

13381341
// TODO: Allow passing in proper locations here.
13391342
for (Type argTy : type.getInputs())
@@ -1342,8 +1345,6 @@ void GPUFuncOp::build(OpBuilder &builder, OperationState &result,
13421345
entryBlock->addArgument(argTy, result.location);
13431346
for (Type argTy : privateAttributions)
13441347
entryBlock->addArgument(argTy, result.location);
1345-
1346-
body->getBlocks().push_back(entryBlock);
13471348
}
13481349

13491350
/// Parses a GPU function memory attribution.

mlir/lib/Dialect/SCF/IR/SCF.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,18 @@ void ConditionOp::getSuccessorRegions(
306306
void ForOp::build(OpBuilder &builder, OperationState &result, Value lb,
307307
Value ub, Value step, ValueRange iterArgs,
308308
BodyBuilderFn bodyBuilder) {
309+
OpBuilder::InsertionGuard guard(builder);
310+
309311
result.addOperands({lb, ub, step});
310312
result.addOperands(iterArgs);
311313
for (Value v : iterArgs)
312314
result.addTypes(v.getType());
313315
Type t = lb.getType();
314316
Region *bodyRegion = result.addRegion();
315-
bodyRegion->push_back(new Block);
316-
Block &bodyBlock = bodyRegion->front();
317-
bodyBlock.addArgument(t, result.location);
317+
Block *bodyBlock = builder.createBlock(bodyRegion);
318+
bodyBlock->addArgument(t, result.location);
318319
for (Value v : iterArgs)
319-
bodyBlock.addArgument(v.getType(), v.getLoc());
320+
bodyBlock->addArgument(v.getType(), v.getLoc());
320321

321322
// Create the default terminator if the builder is not provided and if the
322323
// iteration arguments are not provided. Otherwise, leave this to the caller
@@ -325,9 +326,9 @@ void ForOp::build(OpBuilder &builder, OperationState &result, Value lb,
325326
ForOp::ensureTerminator(*bodyRegion, builder, result.location);
326327
} else if (bodyBuilder) {
327328
OpBuilder::InsertionGuard guard(builder);
328-
builder.setInsertionPointToStart(&bodyBlock);
329-
bodyBuilder(builder, result.location, bodyBlock.getArgument(0),
330-
bodyBlock.getArguments().drop_front());
329+
builder.setInsertionPointToStart(bodyBlock);
330+
bodyBuilder(builder, result.location, bodyBlock->getArgument(0),
331+
bodyBlock->getArguments().drop_front());
331332
}
332333
}
333334

0 commit comments

Comments
 (0)