Skip to content

Commit 1b7547d

Browse files
committed
[mlir][Affine] Handle null parent op in getAffineParallelInductionVarOwner
The issue occurs during a downstream pass which does dialect conversion, where both [`FuncOpConversion`](https://github.com/llvm/llvm-project/blob/cde67b6663f994fcb4ded28fd79b23a13d347c4a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp#L480) and [`SubviewFolder`](https://github.com/llvm/llvm-project/blob/cde67b6663f994fcb4ded28fd79b23a13d347c4a/mlir/lib/Dialect/MemRef/Transforms/ExpandStridedMetadata.cpp#L187) are run together. The original starting IR is: ```mlir module { func.func @foo(%arg0: memref<100x100xf32>, %arg1: index, %arg2: index, %arg3: index, %arg4: index) -> memref<?x?xf32, strided<[100, 1], offset: ?>> { %subview = memref.subview %arg0[%arg1, %arg2] [%arg3, %arg4] [1, 1] : memref<100x100xf32> to memref<?x?xf32, strided<[100, 1], offset: ?>> return %subview : memref<?x?xf32, strided<[100, 1], offset: ?>> } } ``` After `FuncOpConversion` runs, the IR looks like: ```mlir "builtin.module"() ({ "llvm.func"() <{CConv = #llvm.cconv<ccc>, function_type = !llvm.func<struct<(ptr, ptr, i64, array<2 x i64>, array<2 x i64>)> (ptr, ptr, i64, i64, i64, i64, i64, i64, i64, i64, i64)>, linkage = #llvm.linkage<external>, sym_name = "foo", visibility_ = 0 : i64}> ({ ^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i64, %arg3: i64, %arg4: i64, %arg5: i64, %arg6: i64, %arg7: i64, %arg8: i64, %arg9: i64, %arg10: i64): %0 = "memref.subview"(<<UNKNOWN SSA VALUE>>, <<UNKNOWN SSA VALUE>>, <<UNKNOWN SSA VALUE>>, <<UNKNOWN SSA VALUE>>, <<UNKNOWN SSA VALUE>>) <{operandSegmentSizes = array<i32: 1, 2, 2, 0>, static_offsets = array<i64: -9223372036854775808, -9223372036854775808>, static_sizes = array<i64: -9223372036854775808, -9223372036854775808>, static_strides = array<i64: 1, 1>}> : (memref<100x100xf32>, index, index, index, index) -> memref<?x?xf32, strided<[100, 1], offset: ?>> "func.return"(%0) : (memref<?x?xf32, strided<[100, 1], offset: ?>>) -> () }) : () -> () "func.func"() <{function_type = (memref<100x100xf32>, index, index, index, index) -> memref<?x?xf32, strided<[100, 1], offset: ?>>, sym_name = "foo"}> ({ }) : () -> () }) {llvm.data_layout = "", llvm.target_triple = ""} : () -> () ``` The `<<UNKNOWN SSA VALUE>>`'s here are block arguments of a separate unlinked block, which is disconnected from the rest of the IR (so not only is the IR verifier-invalid, it can't even be parsed). This IR is created by signature conversion in the dialect conversion infra. Now `SubviewFolder` is applied, and the utility function here is called on one of these disconnected block arguments, causing a crash. It is hard to construct the reproducer in upstream because upstream does not have such setup. Adding a pass for such case is over-indexing on testing. Since it is a safer check and the method returns null to let callers handle the failure, making one-line change without tests is reasonable. Co-authored-by: Rahul Kayaith <[email protected]> Signed-off-by: hanhanW <[email protected]>
1 parent 1485df6 commit 1b7547d

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ AffineParallelOp mlir::affine::getAffineParallelInductionVarOwner(Value val) {
26672667
if (!ivArg || !ivArg.getOwner())
26682668
return nullptr;
26692669
Operation *containingOp = ivArg.getOwner()->getParentOp();
2670-
auto parallelOp = dyn_cast<AffineParallelOp>(containingOp);
2670+
auto parallelOp = dyn_cast_if_present<AffineParallelOp>(containingOp);
26712671
if (parallelOp && llvm::is_contained(parallelOp.getIVs(), val))
26722672
return parallelOp;
26732673
return nullptr;

0 commit comments

Comments
 (0)