[MLIR] Fix OpenACC parser crash with opaque pointers#183521
Merged
Merged
Conversation
Member
|
@llvm/pr-subscribers-openacc @llvm/pr-subscribers-mlir Author: Mehdi Amini (joker-eph) ChangesFixes #181453 Full diff: https://github.com/llvm/llvm-project/pull/183521.diff 2 Files Affected:
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)
|
razvanlupusoru
approved these changes
Feb 26, 2026
Contributor
razvanlupusoru
left a comment
There was a problem hiding this comment.
Thank you for taking initiative to fix this issue! I appreciate that :)
sujianIBM
pushed a commit
to sujianIBM/llvm-project
that referenced
this pull request
Mar 5, 2026
This was referenced Apr 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #181453
Fixes #181589