Skip to content

[mlir][spirv] Verify matching of entry block arguments and function signature #133167

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
Apr 1, 2025

Conversation

hiraditya
Copy link
Collaborator

Fixes: #132894

@hiraditya hiraditya requested review from jeanPerier and gysit March 26, 2025 21:39
@llvmbot llvmbot added the mlir label Mar 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2025

@llvm/pr-subscribers-mlir-spirv

@llvm/pr-subscribers-mlir

Author: AdityaK (hiraditya)

Changes

Fixes: #132894


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

2 Files Affected:

  • (modified) mlir/lib/Interfaces/CallInterfaces.cpp (+3-2)
  • (added) mlir/test/mlir-opt/print-region.mlir (+17)
diff --git a/mlir/lib/Interfaces/CallInterfaces.cpp b/mlir/lib/Interfaces/CallInterfaces.cpp
index e8ed4b339a0cb..815b906d7d0f2 100644
--- a/mlir/lib/Interfaces/CallInterfaces.cpp
+++ b/mlir/lib/Interfaces/CallInterfaces.cpp
@@ -116,9 +116,10 @@ void call_interface_impl::printFunctionSignature(
 
     if (!isExternal) {
       ArrayRef<NamedAttribute> attrs;
-      if (argAttrs)
+      if (argAttrs) {
         attrs = llvm::cast<DictionaryAttr>(argAttrs[i]).getValue();
-      p.printRegionArgument(body->getArgument(i), attrs);
+        p.printRegionArgument(body->getArgument(i), attrs);
+      }
     } else {
       p.printType(argTypes[i]);
       if (argAttrs)
diff --git a/mlir/test/mlir-opt/print-region.mlir b/mlir/test/mlir-opt/print-region.mlir
new file mode 100644
index 0000000000000..678457defff57
--- /dev/null
+++ b/mlir/test/mlir-opt/print-region.mlir
@@ -0,0 +1,17 @@
+// Bug: https://github.com/llvm/llvm-project/issues/132894
+
+// RUN: mlir-opt %s | FileCheck
+
+// CHECK: module {
+// CHECK:   spirv.func @f(f32) "None" {
+// CHECK:     %c0 = arith.constant 0 : index
+// CHECK:     spirv.Return
+// CHECK:   }
+// CHECK: }
+
+module {
+  spirv.func @f(f32) "None" {
+    %c0 = arith.constant 0 : index
+    spirv.Return
+  }
+}

@hiraditya hiraditya force-pushed the mlir_bug branch 2 times, most recently from cc3d634 to 30187f5 Compare March 26, 2025 23:46
@hiraditya hiraditya changed the title Print region argument only when argAttrs are present Print region argument only when sufficient number of args are present in body Mar 27, 2025
@hiraditya hiraditya marked this pull request as draft March 28, 2025 04:18
@hiraditya hiraditya changed the title Print region argument only when sufficient number of args are present in body [mlir] Add test for invalid entry block Mar 28, 2025
@hiraditya hiraditya marked this pull request as ready for review March 28, 2025 18:54
@CoTinker
Copy link
Contributor

The FunctionOpInterface implement the verifyBody and check the arguments number.

"::llvm::LogicalResult", "verifyBody", (ins),
/*methodBody=*/[{}], /*defaultImplementation=*/[{
if ($_op.isExternal())
return success();
ArrayRef<Type> fnInputTypes = $_op.getArgumentTypes();
// NOTE: This should just be $_op.front() but access generically
// because the interface methods defined here may be shadowed in
// arbitrary ways. https://github.com/llvm/llvm-project/issues/54807
Block &entryBlock = $_op->getRegion(0).front();
unsigned numArguments = fnInputTypes.size();
if (entryBlock.getNumArguments() != numArguments)
return $_op.emitOpError("entry block must have ")
<< numArguments << " arguments to match function signature";

The spirv.func op override the verifyBody funciton, but not check the arguments, causing this crash.

LogicalResult spirv::FuncOp::verifyBody() {

Copy link

github-actions bot commented Mar 31, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@joker-eph joker-eph changed the title [mlir] Add test for invalid entry block [mlir][spirv] Verify matching of entry block arguments and function signature Mar 31, 2025
Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

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

LGTM, @kuhar any more comments here?

Copy link
Member

@kuhar kuhar 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 fixing this

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

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

LGTM

@hiraditya hiraditya merged commit 96d60c0 into llvm:main Apr 1, 2025
11 checks passed
@hiraditya hiraditya deleted the mlir_bug branch April 1, 2025 21:10
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request Apr 2, 2025
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] Spirv func op missing verifier (Assertion 'Index < this->size() && "Invalid index!"' failed in array)
5 participants