Skip to content

Commit c34dc9a

Browse files
authored
[mlir][SCFToEmitC] Don't convert unsupported types in EmitC (#131786)
This PR adds check for unsupported types in emitc, which fixes a crash. Fixes #131442.
1 parent efc31ec commit c34dc9a

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitCPass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ struct ConvertMemRefToEmitCPass
3333

3434
// Fallback for other types.
3535
converter.addConversion([](Type type) -> std::optional<Type> {
36-
if (emitc::isSupportedEmitCType(type))
37-
return type;
38-
return {};
36+
if (!emitc::isSupportedEmitCType(type))
37+
return {};
38+
return type;
3939
});
4040

4141
populateMemRefToEmitCTypeConversion(converter);

mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,12 @@ void mlir::populateSCFToEmitCConversionPatterns(RewritePatternSet &patterns,
319319
void SCFToEmitCPass::runOnOperation() {
320320
RewritePatternSet patterns(&getContext());
321321
TypeConverter typeConverter;
322-
// Fallback converter
323-
// See note https://mlir.llvm.org/docs/DialectConversion/#type-converter
324-
// Type converters are called most to least recently inserted
325-
typeConverter.addConversion([](Type t) { return t; });
322+
// Fallback for other types.
323+
typeConverter.addConversion([](Type type) -> std::optional<Type> {
324+
if (!emitc::isSupportedEmitCType(type))
325+
return {};
326+
return type;
327+
});
326328
populateEmitCSizeTTypeConversions(typeConverter);
327329
populateSCFToEmitCConversionPatterns(patterns, typeConverter);
328330

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: mlir-opt -convert-scf-to-emitc %s -split-input-file -verify-diagnostics
2+
3+
func.func @unsupported_type_vector(%arg0 : index, %arg1 : index, %arg2 : index) -> vector<3xindex> {
4+
%zero = arith.constant dense<0> : vector<3xindex>
5+
// expected-error@+1 {{failed to legalize operation 'scf.for'}}
6+
%r = scf.for %i0 = %arg0 to %arg1 step %arg2 iter_args(%acc = %zero) -> vector<3xindex> {
7+
scf.yield %acc : vector<3xindex>
8+
}
9+
return %r : vector<3xindex>
10+
}

0 commit comments

Comments
 (0)