Skip to content

[mlir][SCFToEmitC] Don't convert unsupported types in EmitC #131786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025

Conversation

CoTinker
Copy link
Contributor

This PR adds check for unsupported types in emitc, which fixes a crash. Fixes #131442.

@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

@llvm/pr-subscribers-mlir-emitc

@llvm/pr-subscribers-mlir

Author: Longsheng Mou (CoTinker)

Changes

This PR adds check for unsupported types in emitc, which fixes a crash. Fixes #131442.


Full diff: https://github.com/llvm/llvm-project/pull/131786.diff

2 Files Affected:

  • (modified) mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp (+5-1)
  • (added) mlir/test/Conversion/SCFToEmitC/scf-to-emitc-failed.mlir (+10)
diff --git a/mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp b/mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp
index 92523ca4f12b2..6f57703bf17b3 100644
--- a/mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp
+++ b/mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp
@@ -322,7 +322,11 @@ void SCFToEmitCPass::runOnOperation() {
   // Fallback converter
   // See note https://mlir.llvm.org/docs/DialectConversion/#type-converter
   // Type converters are called most to least recently inserted
-  typeConverter.addConversion([](Type t) { return t; });
+  typeConverter.addConversion([](Type type) -> std::optional<Type> {
+    if (emitc::isSupportedEmitCType(type))
+      return type;
+    return {};
+  });
   populateEmitCSizeTTypeConversions(typeConverter);
   populateSCFToEmitCConversionPatterns(patterns, typeConverter);
 
diff --git a/mlir/test/Conversion/SCFToEmitC/scf-to-emitc-failed.mlir b/mlir/test/Conversion/SCFToEmitC/scf-to-emitc-failed.mlir
new file mode 100644
index 0000000000000..02c27deaa7a13
--- /dev/null
+++ b/mlir/test/Conversion/SCFToEmitC/scf-to-emitc-failed.mlir
@@ -0,0 +1,10 @@
+// RUN: mlir-opt -convert-scf-to-emitc %s -split-input-file -verify-diagnostics
+
+func.func @unsupported_type_vector(%arg0 : index, %arg1 : index, %arg2 : index) -> vector<3xindex> {
+  %zero = arith.constant dense<0> : vector<3xindex>
+  // expected-error@+1 {{failed to legalize operation 'scf.for'}}
+  %r = scf.for %i0 = %arg0 to %arg1 step %arg2 iter_args(%acc = %zero) -> vector<3xindex> {
+    scf.yield %acc : vector<3xindex>
+  }
+  return %r : vector<3xindex>
+}

Copy link
Member

@marbre marbre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up! Just some minor remarks.

Copy link
Contributor

@josel-amd josel-amd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

This PR adds check for unsupported types in emitc, which fixes a
crash.
@CoTinker CoTinker requested a review from marbre March 19, 2025 01:43
@CoTinker CoTinker merged commit c34dc9a into llvm:main Mar 19, 2025
11 checks passed
@CoTinker CoTinker deleted the scf_to_emitc branch March 19, 2025 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MLIR] crashed on -convert-scf-to-emitc with error: !emitc.lvalue must wrap supported emitc type, but got 'vector<4xf32>'
4 participants