Skip to content

[MLIR] Fix OpenACC parser crash with opaque pointers#183521

Merged
joker-eph merged 1 commit into
llvm:mainfrom
joker-eph:openacc_parser_crash
Feb 27, 2026
Merged

[MLIR] Fix OpenACC parser crash with opaque pointers#183521
joker-eph merged 1 commit into
llvm:mainfrom
joker-eph:openacc_parser_crash

Conversation

@joker-eph
Copy link
Copy Markdown
Contributor

Fixes #181453
Fixes #181589

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Feb 26, 2026

@llvm/pr-subscribers-openacc
@llvm/pr-subscribers-mlir-openacc

@llvm/pr-subscribers-mlir

Author: Mehdi Amini (joker-eph)

Changes

Fixes #181453
Fixes #181589


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

2 Files Affected:

  • (modified) mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp (+11-4)
  • (modified) mlir/test/Dialect/OpenACC/ops.mlir (+18)
diff --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
index ce024648b160c..5ec164a892d67 100644
--- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
+++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
@@ -780,11 +780,14 @@ static ParseResult parseVarPtrType(mlir::OpAsmParser &parser,
       return failure();
   } else {
     // Set `varType` from the element type of the type of `varPtr`.
-    if (mlir::isa<mlir::acc::PointerLikeType>(varPtrType))
-      varTypeAttr = mlir::TypeAttr::get(
-          mlir::cast<mlir::acc::PointerLikeType>(varPtrType).getElementType());
-    else
+    if (auto ptrTy = dyn_cast<acc::PointerLikeType>(varPtrType)) {
+      Type elementType = ptrTy.getElementType();
+      // Opaque pointers (e.g. !llvm.ptr) have no element type; fall back to
+      // using varPtrType itself so that the attribute is always valid.
+      varTypeAttr = mlir::TypeAttr::get(elementType ? elementType : varPtrType);
+    } else {
       varTypeAttr = mlir::TypeAttr::get(varPtrType);
+    }
   }
 
   return success();
@@ -802,6 +805,10 @@ static void printVarPtrType(mlir::OpAsmPrinter &p, mlir::Operation *op,
       mlir::isa<mlir::acc::PointerLikeType>(varPtrType)
           ? mlir::cast<mlir::acc::PointerLikeType>(varPtrType).getElementType()
           : varPtrType;
+  // Opaque pointers (e.g. !llvm.ptr) have no element type; use varPtrType as
+  // the baseline so that the inferred varType is not redundantly printed.
+  if (!typeToCheckAgainst)
+    typeToCheckAgainst = varPtrType;
   if (typeToCheckAgainst != varType) {
     p << " varType(";
     p.printType(varType);
diff --git a/mlir/test/Dialect/OpenACC/ops.mlir b/mlir/test/Dialect/OpenACC/ops.mlir
index 15698ca457ca0..069c4ee104f1e 100644
--- a/mlir/test/Dialect/OpenACC/ops.mlir
+++ b/mlir/test/Dialect/OpenACC/ops.mlir
@@ -2504,3 +2504,21 @@ func.func @test_acc_reduction_combine(%arg0 : memref<i32>, %arg1 : memref<i32>)
 
 // CHECK-LABEL: func @test_acc_reduction_combine
 // CHECK:       acc.reduction_combine %arg0 into %arg1 <add> : memref<i32>
+
+// -----
+
+// Test that acc.getdeviceptr with an opaque pointer (!llvm.ptr, which has no
+// element type) can be parsed and printed without an explicit varType clause.
+// This is a regression test for a crash where getElementType() returned null
+// for opaque pointers and was passed to TypeAttr::get() without a null check.
+
+func.func @test_getdeviceptr_opaque_ptr(%a: !llvm.ptr) -> () {
+  %0 = acc.getdeviceptr varPtr(%a : !llvm.ptr) -> !llvm.ptr
+  acc.declare_enter dataOperands(%0 : !llvm.ptr)
+  return
+}
+
+// CHECK-LABEL: func @test_getdeviceptr_opaque_ptr(
+// CHECK-SAME:    %[[A:.*]]: !llvm.ptr)
+// CHECK:         %[[DEVPTR:.*]] = acc.getdeviceptr varPtr(%[[A]] : !llvm.ptr) -> !llvm.ptr
+// CHECK:         acc.declare_enter dataOperands(%[[DEVPTR]] : !llvm.ptr)

Copy link
Copy Markdown
Contributor

@razvanlupusoru razvanlupusoru left a comment

Choose a reason for hiding this comment

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

Thank you for taking initiative to fix this issue! I appreciate that :)

Copy link
Copy Markdown
Contributor

@CoTinker CoTinker left a comment

Choose a reason for hiding this comment

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

LGTM

@joker-eph joker-eph merged commit 9c2a3ca into llvm:main Feb 27, 2026
14 checks passed
sujianIBM pushed a commit to sujianIBM/llvm-project that referenced this pull request Mar 5, 2026
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-opt crash: Null pointer dereference in acc.getdeviceptr custom parser mlir-opt crash: Null pointer dereference in acc.getdeviceptr custom parser

4 participants