[Mlir-commits] [mlir] [mlir][openacc] Support already existing global in GPU module (PR #210806)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jul 20 13:50:07 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-openacc

Author: Valentin Clement (バレンタイン クレメン) (clementval)

<details>
<summary>Changes</summary>



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


2 Files Affected:

- (modified) mlir/lib/Dialect/OpenACC/Transforms/ACCDeclareGPUModuleInsertion.cpp (+11-10) 
- (modified) mlir/test/Dialect/OpenACC/acc-declare-gpu-module-insertion.mlir (+18-1) 


``````````diff
diff --git a/mlir/lib/Dialect/OpenACC/Transforms/ACCDeclareGPUModuleInsertion.cpp b/mlir/lib/Dialect/OpenACC/Transforms/ACCDeclareGPUModuleInsertion.cpp
index f815245882630..19460fd9daa31 100644
--- a/mlir/lib/Dialect/OpenACC/Transforms/ACCDeclareGPUModuleInsertion.cpp
+++ b/mlir/lib/Dialect/OpenACC/Transforms/ACCDeclareGPUModuleInsertion.cpp
@@ -54,7 +54,6 @@
 #include "mlir/Dialect/OpenACC/Transforms/Passes.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/Operation.h"
-#include "mlir/IR/OperationSupport.h"
 #include "mlir/IR/SymbolTable.h"
 
 namespace mlir {
@@ -99,20 +98,22 @@ class ACCDeclareGPUModuleInsertion
       StringAttr name = symOp.getNameAttr();
 
       if (Operation *existing = gpuSymTable.lookup(name.getValue())) {
-        // Reuse only when the existing GPU symbol is structurally equivalent to
-        // the global we would insert. Otherwise treat as a conflict (different
-        // op type or different definition).
-        if (existing->getName() != globalOp.getName() ||
-            !OperationEquivalence::isEquivalentTo(
-                existing, &globalOp,
-                OperationEquivalence::ignoreValueEquivalence,
-                /*markEquivalent=*/nullptr,
-                OperationEquivalence::IgnoreLocations)) {
+        // A same-named symbol may already exist from an earlier pass (e.g.
+        // CUDA Fortran can clone device globals before ACCImplicitDeclare
+        // marks the host copy with acc.declare). Reuse it when the op type
+        // matches; only a different op type is a real conflict.
+        if (existing->getName() != globalOp.getName()) {
           accSupport.emitNYI(globalOp.getLoc(),
                              llvm::Twine("duplicate global symbol '") +
                                  name.getValue() + "' in gpu module");
           return failure();
         }
+        // Propagate acc.declare onto the GPU copy if it was cloned before the
+        // host global was marked.
+        if (!existing->getAttr(acc::getDeclareAttrName()))
+          if (Attribute declareAttr =
+                  globalOp.getAttr(acc::getDeclareAttrName()))
+            existing->setAttr(acc::getDeclareAttrName(), declareAttr);
         continue;
       }
 
diff --git a/mlir/test/Dialect/OpenACC/acc-declare-gpu-module-insertion.mlir b/mlir/test/Dialect/OpenACC/acc-declare-gpu-module-insertion.mlir
index 8d206673ac02d..2e5910d155641 100644
--- a/mlir/test/Dialect/OpenACC/acc-declare-gpu-module-insertion.mlir
+++ b/mlir/test/Dialect/OpenACC/acc-declare-gpu-module-insertion.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -acc-declare-gpu-module-insertion | FileCheck %s
+// RUN: mlir-opt %s -split-input-file -acc-declare-gpu-module-insertion | FileCheck %s
 
 // Test that globals with acc.declare are copied into the GPU module.
 // The host global stays in the module; a copy is inserted into the GPU module.
@@ -12,3 +12,20 @@
 module {
   memref.global @arr : memref<7xf32> = dense<0.0> {acc.declare = #acc.declare<dataClause = acc_create>}
 }
+
+// -----
+
+// If the GPU module already has the global (e.g. from CUDA Fortran pass before
+// ACCImplicitDeclare marked the host), reuse it and propagate acc.declare.
+// CHECK-LABEL: module attributes {gpu.container_module}
+// CHECK: memref.global @precloned {{.*}} {acc.declare = #acc.declare<dataClause = acc_copyin>}
+// CHECK: gpu.module @acc_gpu_module {
+// CHECK-NEXT: memref.global @precloned {{.*}} {acc.declare = #acc.declare<dataClause = acc_copyin>}
+// CHECK-NEXT: }
+
+module attributes {gpu.container_module} {
+  memref.global @precloned : memref<4xf32> = dense<0.0> {acc.declare = #acc.declare<dataClause = acc_copyin>}
+  gpu.module @acc_gpu_module {
+    memref.global @precloned : memref<4xf32> = dense<0.0>
+  }
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/210806


More information about the Mlir-commits mailing list