Skip to content

Conversation

@CoTinker
Copy link
Contributor

This PR adds a FunctionOpInterface check in OpToFuncCallLowering to resolve a crash when ops not in function. Fixes #113334.

@llvmbot
Copy link
Member

llvmbot commented Oct 23, 2024

@llvm/pr-subscribers-mlir-gpu

Author: Longsheng Mou (CoTinker)

Changes

This PR adds a FunctionOpInterface check in OpToFuncCallLowering to resolve a crash when ops not in function. Fixes #113334.


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

2 Files Affected:

  • (modified) mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h (+5)
  • (modified) mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir (+14)
diff --git a/mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h b/mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h
index 1cf8a1acb31935..3b94abd88f9ed2 100644
--- a/mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h
+++ b/mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h
@@ -61,6 +61,11 @@ struct OpToFuncCallLowering : public ConvertOpToLLVMPattern<SourceOp> {
                                   SourceOp>::value,
                   "expected op with same operand and result types");
 
+    if (!op->template getParentOfType<FunctionOpInterface>()) {
+      return rewriter.notifyMatchFailure(
+          op, "expected op to be within a function region");
+    }
+
     SmallVector<Value, 1> castedOperands;
     for (Value operand : adaptor.getOperands())
       castedOperands.push_back(maybeCast(operand, rewriter));
diff --git a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
index ddd96bf797e6e7..c1b2407a66d915 100644
--- a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
+++ b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
@@ -481,3 +481,17 @@ module @test_module {
     func.return %resultf16, %resultf32, %resultf64, %resultbf16 : f16, f32, f64, bf16
   }
 }
+
+// -----
+
+// Math operation not inside function
+// Ensure it not crash
+
+module {
+  "test.some_op_with_region"() ({
+  ^bb0(%arg0: f64):
+    // CHECK: math.atan
+    %0 = math.atan %arg0 : f64
+    "test.possible_terminator"() : () -> ()
+  }) : () -> ()
+}

@llvmbot
Copy link
Member

llvmbot commented Oct 23, 2024

@llvm/pr-subscribers-mlir

Author: Longsheng Mou (CoTinker)

Changes

This PR adds a FunctionOpInterface check in OpToFuncCallLowering to resolve a crash when ops not in function. Fixes #113334.


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

2 Files Affected:

  • (modified) mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h (+5)
  • (modified) mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir (+14)
diff --git a/mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h b/mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h
index 1cf8a1acb31935..3b94abd88f9ed2 100644
--- a/mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h
+++ b/mlir/lib/Conversion/GPUCommon/OpToFuncCallLowering.h
@@ -61,6 +61,11 @@ struct OpToFuncCallLowering : public ConvertOpToLLVMPattern<SourceOp> {
                                   SourceOp>::value,
                   "expected op with same operand and result types");
 
+    if (!op->template getParentOfType<FunctionOpInterface>()) {
+      return rewriter.notifyMatchFailure(
+          op, "expected op to be within a function region");
+    }
+
     SmallVector<Value, 1> castedOperands;
     for (Value operand : adaptor.getOperands())
       castedOperands.push_back(maybeCast(operand, rewriter));
diff --git a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
index ddd96bf797e6e7..c1b2407a66d915 100644
--- a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
+++ b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
@@ -481,3 +481,17 @@ module @test_module {
     func.return %resultf16, %resultf32, %resultf64, %resultbf16 : f16, f32, f64, bf16
   }
 }
+
+// -----
+
+// Math operation not inside function
+// Ensure it not crash
+
+module {
+  "test.some_op_with_region"() ({
+  ^bb0(%arg0: f64):
+    // CHECK: math.atan
+    %0 = math.atan %arg0 : f64
+    "test.possible_terminator"() : () -> ()
+  }) : () -> ()
+}

Copy link
Contributor

@nirvedhmeshram nirvedhmeshram left a comment

Choose a reason for hiding this comment

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

LGTM, also can we add -allow-unregistered-dialect to the test. In certain build set ups it can give an error about operation being parsed with an unregistered dialect

This PR adds a `FunctionOpInterface` check in `OpToFuncCallLowering`
to resolve a crash when ops not in function.
@CoTinker
Copy link
Contributor Author

LGTM, also can we add -allow-unregistered-dialect to the test. In certain build set ups it can give an error about operation being parsed with an unregistered dialect

Done

@CoTinker CoTinker merged commit 8f9fc6c into llvm:main Oct 26, 2024
8 checks passed
@CoTinker CoTinker deleted the func branch October 26, 2024 03:22
winner245 pushed a commit to winner245/llvm-project that referenced this pull request Oct 26, 2024
…lvm#113449)

This PR adds a `FunctionOpInterface` check in `OpToFuncCallLowering` to
resolve a crash when ops not in function. Fixes llvm#113334.
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…lvm#113449)

This PR adds a `FunctionOpInterface` check in `OpToFuncCallLowering` to
resolve a crash when ops not in function. Fixes llvm#113334.
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] -convert-math-to-rocdl crashes

3 participants