Skip to content

Commit 0b91c98

Browse files
Make getYieldedValuesMutable return an std::optional<llvm::MutableArrayRef>.
1 parent 391d9a7 commit 0b91c98

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

mlir/include/mlir/Interfaces/LoopLikeInterface.td

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,16 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
164164
InterfaceMethod<[{
165165
Return the mutable operand range of values that are yielded to the next
166166
iteration by the loop terminator.
167+
168+
For loop operations that dont yield a value, this should return
169+
std::nullopt.
167170
}],
168-
/*retTy=*/"::llvm::MutableArrayRef<::mlir::OpOperand>",
171+
/*retTy=*/"std::optional<::llvm::MutableArrayRef<::mlir::OpOperand>>",
169172
/*methodName=*/"getYieldedValuesMutable",
170173
/*args=*/(ins),
171174
/*methodBody=*/"",
172175
/*defaultImplementation=*/[{
173-
return {};
176+
return std::nullopt;
174177
}]
175178
>,
176179
InterfaceMethod<[{
@@ -257,16 +260,17 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
257260
});
258261
}
259262

260-
/// Return the values that are yielded to the next iteration.
263+
/// Return the values that are yielded to the next iteration. If
264+
/// the loop doesnt yield any values return `{}`.
261265
::mlir::ValueRange getYieldedValues() {
262266
auto mutableValues = $_op.getYieldedValuesMutable();
263-
if (mutableValues.empty())
267+
if (!mutableValues || mutableValues->empty())
264268
return {};
265-
Operation *yieldOp = mutableValues.begin()->getOwner();
266-
unsigned firstOperandIndex = mutableValues.begin()->getOperandNumber();
269+
Operation *yieldOp = mutableValues->begin()->getOwner();
270+
unsigned firstOperandIndex = mutableValues->begin()->getOperandNumber();
267271
return OperandRange(
268272
yieldOp->operand_begin() + firstOperandIndex,
269-
yieldOp->operand_begin() + firstOperandIndex + mutableValues.size());
273+
yieldOp->operand_begin() + firstOperandIndex + mutableValues->size());
270274
}
271275

272276
/// Return the "init" operands that are used as initialization values for
@@ -331,14 +335,17 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
331335

332336
/// Return the yielded value that corresponds to the given region iter_arg.
333337
/// Return "nullptr" if the given block argument is not a region iter_arg
334-
/// of this loop op.
338+
/// of this loop op or if there is no yield corresponding to this `bbArg`.
335339
OpOperand *getTiedLoopYieldedValue(BlockArgument bbArg) {
336340
auto iterArgs = $_op.getRegionIterArgs();
337341
auto it = llvm::find(iterArgs, bbArg);
338342
if (it == iterArgs.end())
339343
return {};
340-
return
341-
&$_op.getYieldedValuesMutable()[std::distance(iterArgs.begin(), it)];
344+
std::optional<llvm::MutableArrayRef<::mlir::OpOperand>> yieldValues =
345+
$_op.getYieldedValuesMutable();
346+
if (!yieldValues)
347+
return {};
348+
return &yieldValues.value()[std::distance(iterArgs.begin(), it)];
342349
}
343350

344351
/// Return the loop result that corresponds to the given init operand.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2127,7 +2127,7 @@ unsigned AffineForOp::getNumIterOperands() {
21272127
return getNumOperands() - lbMap.getNumInputs() - ubMap.getNumInputs();
21282128
}
21292129

2130-
MutableArrayRef<OpOperand> AffineForOp::getYieldedValuesMutable() {
2130+
std::optional<MutableArrayRef<OpOperand>> AffineForOp::getYieldedValuesMutable() {
21312131
return cast<AffineYieldOp>(getBody()->getTerminator()).getOperandsMutable();
21322132
}
21332133

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ std::optional<APInt> ForOp::getConstantStep() {
12121212
return {};
12131213
}
12141214

1215-
MutableArrayRef<OpOperand> ForOp::getYieldedValuesMutable() {
1215+
std::optional<MutableArrayRef<OpOperand>> ForOp::getYieldedValuesMutable() {
12161216
return cast<scf::YieldOp>(getBody()->getTerminator()).getResultsMutable();
12171217
}
12181218

@@ -3222,7 +3222,7 @@ YieldOp WhileOp::getYieldOp() {
32223222
return cast<YieldOp>(getAfterBody()->getTerminator());
32233223
}
32243224

3225-
MutableArrayRef<OpOperand> WhileOp::getYieldedValuesMutable() {
3225+
std::optional<MutableArrayRef<OpOperand>> WhileOp::getYieldedValuesMutable() {
32263226
return getYieldOp().getResultsMutable();
32273227
}
32283228

0 commit comments

Comments
 (0)