Skip to content

[mlir] fix crash when scf utils work on llvm.func #120688

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
Dec 20, 2024

Conversation

cxy-1993
Copy link
Contributor

fixed #119378

@llvmbot
Copy link
Member

llvmbot commented Dec 20, 2024

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-scf

Author: donald chen (cxy-1993)

Changes

fixed #119378


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

3 Files Affected:

  • (modified) mlir/lib/Dialect/SCF/Utils/Utils.cpp (+1-1)
  • (modified) mlir/test/Transforms/scf-if-utils.mlir (+12)
  • (modified) mlir/test/lib/Dialect/SCF/TestSCFUtils.cpp (+4)
diff --git a/mlir/lib/Dialect/SCF/Utils/Utils.cpp b/mlir/lib/Dialect/SCF/Utils/Utils.cpp
index e341c3744f1d8f..41410a0a56aa98 100644
--- a/mlir/lib/Dialect/SCF/Utils/Utils.cpp
+++ b/mlir/lib/Dialect/SCF/Utils/Utils.cpp
@@ -130,7 +130,7 @@ FailureOr<func::FuncOp> mlir::outlineSingleBlockRegion(RewriterBase &rewriter,
 
   // Outline before current function.
   OpBuilder::InsertionGuard g(rewriter);
-  rewriter.setInsertionPoint(region.getParentOfType<func::FuncOp>());
+  rewriter.setInsertionPoint(region.getParentOfType<FunctionOpInterface>());
 
   SetVector<Value> captures;
   getUsedValuesDefinedAbove(region, captures);
diff --git a/mlir/test/Transforms/scf-if-utils.mlir b/mlir/test/Transforms/scf-if-utils.mlir
index 449be18483741c..2825ff19bfd018 100644
--- a/mlir/test/Transforms/scf-if-utils.mlir
+++ b/mlir/test/Transforms/scf-if-utils.mlir
@@ -73,3 +73,15 @@ func.func @outline_empty_if_else(%cond: i1, %a: index, %b: memref<?xf32>, %c: i8
   }
   return
 }
+
+// -----
+
+// This test checks scf utils can work on llvm func.
+// CHECK-LABEL: @llvm_func
+llvm.func @llvm_func(%cond: i1, %a: i32) {
+  scf.if %cond {
+  } else {
+    "some_op"(%cond, %a) : (i1, i32) -> ()
+  }
+  llvm.return
+}
diff --git a/mlir/test/lib/Dialect/SCF/TestSCFUtils.cpp b/mlir/test/lib/Dialect/SCF/TestSCFUtils.cpp
index 3ff7f9966e93da..a3be1f94fa28a3 100644
--- a/mlir/test/lib/Dialect/SCF/TestSCFUtils.cpp
+++ b/mlir/test/lib/Dialect/SCF/TestSCFUtils.cpp
@@ -79,6 +79,10 @@ struct TestSCFIfUtilsPass
   StringRef getDescription() const final { return "test scf.if utils"; }
   explicit TestSCFIfUtilsPass() = default;
 
+  void getDependentDialects(DialectRegistry &registry) const override {
+    registry.insert<func::FuncDialect>();
+  }
+
   void runOnOperation() override {
     int count = 0;
     getOperation().walk([&](scf::IfOp ifOp) {

@ftynse ftynse changed the title [mlir] [nfc] fix crash when scf utils work on llvm.func [mlir] fix crash when scf utils work on llvm.func Dec 20, 2024
@ftynse
Copy link
Member

ftynse commented Dec 20, 2024

I edited the title of the PR, because a bug fix is certainly NOT a non-functional change (nfc).

Copy link
Member

@ftynse ftynse left a comment

Choose a reason for hiding this comment

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

It's feels odd to me to create func.func when outlining from llvm.func, but I suppose we can live with that. The better solution would be to find a way to create a function, and a call to it, of the same kind as the current function, but it is likely impossible without modifications to the function interface. So we can live with what's proposed here + additional lowering.

Please make sure the test actually tests the change.

@cxy-1993
Copy link
Contributor Author

It's feels odd to me to create func.func when outlining from llvm.func, but I suppose we can live with that. The better solution would be to find a way to create a function, and a call to it, of the same kind as the current function, but it is likely impossible without modifications to the function interface. So we can live with what's proposed here + additional lowering.

Please make sure the test actually tests the change.

Thanks for the comments on this patch. I agree it would be better if we could make a matching function op, but it's hard to do right now.

Regarding your comment on test, it was primarily designed to prevent crashes under llvm func. However, following your suggestion, I've also included checks for outlining.

@cxy-1993 cxy-1993 merged commit 701f240 into llvm:main Dec 20, 2024
8 checks passed
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] --test-scf-if-utils crashes
3 participants