Skip to content

Commit 80d5400

Browse files
committed
[mlir][spirv] Account for type conversion failures in scf-to-spirv
Fixes: #59136 Reviewed By: antiagainst Differential Revision: https://reviews.llvm.org/D141292
1 parent a5098e5 commit 80d5400

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
1818
#include "mlir/IR/BuiltinOps.h"
1919
#include "mlir/Transforms/DialectConversion.h"
20+
#include "llvm/Support/FormatVariadic.h"
2021

2122
using namespace mlir;
2223

@@ -286,6 +287,10 @@ IfOpConversion::matchAndRewrite(scf::IfOp ifOp, OpAdaptor adaptor,
286287
SmallVector<Type, 8> returnTypes;
287288
for (auto result : ifOp.getResults()) {
288289
auto convertedType = typeConverter.convertType(result.getType());
290+
if (!convertedType)
291+
return rewriter.notifyMatchFailure(
292+
loc, llvm::formatv("failed to convert type '{0}'", result.getType()));
293+
289294
returnTypes.push_back(convertedType);
290295
}
291296
replaceSCFOutputValue(ifOp, selectionOp, rewriter, scfToSPIRVContext,

mlir/test/Conversion/SCFToSPIRV/if.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,18 @@ func.func @simple_if_yield_type_change(%arg2 : memref<10xf32, #spirv.storage_cla
153153
return
154154
}
155155

156+
// Memrefs without a spirv storage class are not supported. The conversion
157+
// should preserve the `scf.if` and not crash.
158+
func.func @unsupported_yield_type(%arg0 : memref<8xi32>, %arg1 : memref<8xi32>, %c : i1) {
159+
// CHECK-LABEL: @unsupported_yield_type
160+
// CHECK-NEXT: scf.if
161+
// CHECK: spirv.Return
162+
%r = scf.if %c -> (memref<8xi32>) {
163+
scf.yield %arg0 : memref<8xi32>
164+
} else {
165+
scf.yield %arg1 : memref<8xi32>
166+
}
167+
return
168+
}
169+
156170
} // end module

0 commit comments

Comments
 (0)