Skip to content

Commit 96d57de

Browse files
[mlir][cf] Do not access erased operation in cf.cond_br lowering (#148358)
Do not access the erased `cf.cond_br` operation in the lowering pattern. That won't work anymore in a One-Shot Dialect Conversion and triggers a use-after-free sanitizer error. After the One-Shot Dialect Conversion refactoring, a `ConversionPatternRewriter` will behave more like a normal `PatternRewriter`. --------- Co-authored-by: Markus Böck <markus.boeck02@gmail.com>
1 parent 7f7cf27 commit 96d57de

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,12 @@ struct BranchOpLowering : public ConvertOpToLLVMPattern<cf::BranchOp> {
138138
TypeRange(adaptor.getOperands()));
139139
if (failed(convertedBlock))
140140
return failure();
141+
DictionaryAttr attrs = op->getAttrDictionary();
141142
Operation *newOp = rewriter.replaceOpWithNewOp<LLVM::BrOp>(
142143
op, adaptor.getOperands(), *convertedBlock);
143144
// TODO: We should not just forward all attributes like that. But there are
144145
// existing Flang tests that depend on this behavior.
145-
newOp->setAttrs(op->getAttrDictionary());
146+
newOp->setAttrs(attrs);
146147
return success();
147148
}
148149
};
@@ -166,18 +167,14 @@ struct CondBranchOpLowering : public ConvertOpToLLVMPattern<cf::CondBranchOp> {
166167
TypeRange(adaptor.getFalseDestOperands()));
167168
if (failed(convertedFalseBlock))
168169
return failure();
170+
DictionaryAttr attrs = op->getAttrDictionary();
169171
auto newOp = rewriter.replaceOpWithNewOp<LLVM::CondBrOp>(
170-
op, adaptor.getCondition(), *convertedTrueBlock,
171-
adaptor.getTrueDestOperands(), *convertedFalseBlock,
172-
adaptor.getFalseDestOperands());
173-
ArrayRef<int32_t> weights = op.getWeights();
174-
if (!weights.empty()) {
175-
newOp.setWeights(weights);
176-
op.removeBranchWeightsAttr();
177-
}
172+
op, adaptor.getCondition(), adaptor.getTrueDestOperands(),
173+
adaptor.getFalseDestOperands(), op.getBranchWeightsAttr(),
174+
*convertedTrueBlock, *convertedFalseBlock);
178175
// TODO: We should not just forward all attributes like that. But there are
179176
// existing Flang tests that depend on this behavior.
180-
newOp->setAttrs(op->getAttrDictionary());
177+
newOp->setAttrs(attrs);
181178
return success();
182179
}
183180
};

0 commit comments

Comments
 (0)