[Mlir-commits] [mlir] [MLIR] Fix OpenACC parser crash with opaque pointers (PR #183521)

Mehdi Amini llvmlistbot at llvm.org
Thu Feb 26 05:00:23 PST 2026


https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/183521

Fixes #181453
Fixes #181589

>From 166ef3aa07bbe67ca5187bee8f52b01862538f57 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Thu, 26 Feb 2026 04:55:22 -0800
Subject: [PATCH] [MLIR] Fix OpenACC parser crash with opaque pointers

Fixes #181453
Fixes #181589
---
 mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp | 15 +++++++++++----
 mlir/test/Dialect/OpenACC/ops.mlir      | 18 ++++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)

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)



More information about the Mlir-commits mailing list