[mlir][SCFToEmitC] Fix crash when scf.while carries a memref loop variable#183944
Merged
Conversation
…iable When a scf.while op has a loop-carried value whose type converts to emitc::ArrayType (e.g. memref<1xf64>), the WhileLowering pattern unconditionally called emitc::LValueType::get(arrayType), which triggered an assertion because LValueType cannot wrap an array type. Fix by returning a match failure in createVariablesForResults and createVariablesForLoopCarriedValues when the converted type is an emitc::ArrayType. This converts the crash into a proper legalization failure. Fixes llvm#182649
Member
|
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-emitc Author: Mehdi Amini (joker-eph) ChangesWhen a scf.while op has a loop-carried value whose type converts to emitc::ArrayType (e.g. memref<1xf64>), the WhileLowering pattern unconditionally called emitc::LValueType::get(arrayType), which triggered an assertion because LValueType cannot wrap an array type. Fix by returning a match failure in createVariablesForResults and createVariablesForLoopCarriedValues when the converted type is an emitc::ArrayType. This converts the crash into a proper legalization failure. Fixes #182649 Full diff: https://github.com/llvm/llvm-project/pull/183944.diff 2 Files Affected:
diff --git a/mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp b/mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp
index 71e3f88a63f34..07feba7e565d9 100644
--- a/mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp
+++ b/mlir/lib/Conversion/SCFToEmitC/SCFToEmitC.cpp
@@ -89,6 +89,9 @@ createVariablesForResults(T op, const TypeConverter *typeConverter,
Type resultType = typeConverter->convertType(result.getType());
if (!resultType)
return rewriter.notifyMatchFailure(op, "result type conversion failed");
+ if (isa<emitc::ArrayType>(resultType))
+ return rewriter.notifyMatchFailure(
+ op, "cannot create variable for result of array type");
Type varType = emitc::LValueType::get(resultType);
emitc::OpaqueAttr noInit = emitc::OpaqueAttr::get(context, "");
emitc::VariableOp var =
@@ -393,6 +396,10 @@ struct WhileLowering : public OpConversionPattern<WhileOp> {
Type convertedType = getTypeConverter()->convertType(init.getType());
if (!convertedType)
return rewriter.notifyMatchFailure(whileOp, "type conversion failed");
+ if (isa<emitc::ArrayType>(convertedType))
+ return rewriter.notifyMatchFailure(
+ whileOp,
+ "cannot create variable for loop-carried value of array type");
auto var = emitc::VariableOp::create(
rewriter, loc, emitc::LValueType::get(convertedType), noInit);
diff --git a/mlir/test/Conversion/SCFToEmitC/scf-to-emitc-failed.mlir b/mlir/test/Conversion/SCFToEmitC/scf-to-emitc-failed.mlir
index 02c27deaa7a13..7f226c51323d8 100644
--- a/mlir/test/Conversion/SCFToEmitC/scf-to-emitc-failed.mlir
+++ b/mlir/test/Conversion/SCFToEmitC/scf-to-emitc-failed.mlir
@@ -8,3 +8,27 @@ func.func @unsupported_type_vector(%arg0 : index, %arg1 : index, %arg2 : index)
}
return %r : vector<3xindex>
}
+
+// -----
+
+// Regression test for https://github.com/llvm/llvm-project/issues/182649
+// scf.while with a memref loop-carried value (which converts to emitc.array)
+// must fail gracefully rather than crashing with an assertion.
+
+func.func @while_with_memref_carried(%n: index) {
+ %c0 = arith.constant 0 : index
+ %zero_f64 = arith.constant 0.0 : f64
+ %buf = memref.alloca() : memref<1xf64>
+ memref.store %zero_f64, %buf[%c0] : memref<1xf64>
+ // expected-error@+1 {{failed to legalize operation 'scf.while'}}
+ scf.while (%i = %c0, %acc = %buf) : (index, memref<1xf64>) -> (index, memref<1xf64>) {
+ %cond = arith.cmpi slt, %i, %n : index
+ scf.condition(%cond) %i, %acc : index, memref<1xf64>
+ } do {
+ ^bb0(%i: index, %acc: memref<1xf64>):
+ %c1 = arith.constant 1 : index
+ %next_i = arith.addi %i, %c1 : index
+ scf.yield %next_i, %acc : index, memref<1xf64>
+ }
+ return
+}
|
aniragil
approved these changes
Mar 4, 2026
Contributor
aniragil
left a comment
There was a problem hiding this comment.
LGTM, thanks @joker-eph !
sahas3
pushed a commit
to sahas3/llvm-project
that referenced
this pull request
Mar 4, 2026
…iable (llvm#183944) When a scf.while op has a loop-carried value whose type converts to emitc::ArrayType (e.g. memref<1xf64>), the WhileLowering pattern unconditionally called emitc::LValueType::get(arrayType), which triggered an assertion because LValueType cannot wrap an array type. Fix by returning a match failure in createVariablesForResults and createVariablesForLoopCarriedValues when the converted type is an emitc::ArrayType. This converts the crash into a proper legalization failure. Fixes llvm#182649
sujianIBM
pushed a commit
to sujianIBM/llvm-project
that referenced
this pull request
Mar 5, 2026
…iable (llvm#183944) When a scf.while op has a loop-carried value whose type converts to emitc::ArrayType (e.g. memref<1xf64>), the WhileLowering pattern unconditionally called emitc::LValueType::get(arrayType), which triggered an assertion because LValueType cannot wrap an array type. Fix by returning a match failure in createVariablesForResults and createVariablesForLoopCarriedValues when the converted type is an emitc::ArrayType. This converts the crash into a proper legalization failure. Fixes llvm#182649
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a scf.while op has a loop-carried value whose type converts to emitc::ArrayType (e.g. memref<1xf64>), the WhileLowering pattern unconditionally called emitc::LValueType::get(arrayType), which triggered an assertion because LValueType cannot wrap an array type.
Fix by returning a match failure in createVariablesForResults and createVariablesForLoopCarriedValues when the converted type is an emitc::ArrayType. This converts the crash into a proper legalization failure.
Fixes #182649