@@ -164,13 +164,16 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
164
164
InterfaceMethod<[{
165
165
Return the mutable operand range of values that are yielded to the next
166
166
iteration by the loop terminator.
167
+
168
+ For loop operations that dont yield a value, this should return
169
+ std::nullopt.
167
170
}],
168
- /*retTy=*/":: llvm::MutableArrayRef<::mlir::OpOperand>",
171
+ /*retTy=*/"std::optional<:: llvm::MutableArrayRef<::mlir::OpOperand> >",
169
172
/*methodName=*/"getYieldedValuesMutable",
170
173
/*args=*/(ins),
171
174
/*methodBody=*/"",
172
175
/*defaultImplementation=*/[{
173
- return {} ;
176
+ return std::nullopt ;
174
177
}]
175
178
>,
176
179
InterfaceMethod<[{
@@ -257,16 +260,17 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
257
260
});
258
261
}
259
262
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 `{}`.
261
265
::mlir::ValueRange getYieldedValues() {
262
266
auto mutableValues = $_op.getYieldedValuesMutable();
263
- if (mutableValues. empty())
267
+ if (! mutableValues || mutableValues-> empty())
264
268
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();
267
271
return OperandRange(
268
272
yieldOp->operand_begin() + firstOperandIndex,
269
- yieldOp->operand_begin() + firstOperandIndex + mutableValues. size());
273
+ yieldOp->operand_begin() + firstOperandIndex + mutableValues-> size());
270
274
}
271
275
272
276
/// Return the "init" operands that are used as initialization values for
@@ -331,14 +335,17 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
331
335
332
336
/// Return the yielded value that corresponds to the given region iter_arg.
333
337
/// 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` .
335
339
OpOperand *getTiedLoopYieldedValue(BlockArgument bbArg) {
336
340
auto iterArgs = $_op.getRegionIterArgs();
337
341
auto it = llvm::find(iterArgs, bbArg);
338
342
if (it == iterArgs.end())
339
343
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)];
342
349
}
343
350
344
351
/// Return the loop result that corresponds to the given init operand.
0 commit comments