[Mlir-commits] [mlir] [mlir][openacc] Support already existing global in GPU module (PR #210806)
Valentin Clement バレンタイン クレメン
llvmlistbot at llvm.org
Mon Jul 20 13:49:28 PDT 2026
https://github.com/clementval created https://github.com/llvm/llvm-project/pull/210806
None
>From 18909c11e514588853789b0859a6f130785e0f03 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Mon, 20 Jul 2026 13:45:03 -0700
Subject: [PATCH] [mlir][openacc] Support already existing global in GPU module
---
.../ACCDeclareGPUModuleInsertion.cpp | 21 ++++++++++---------
.../acc-declare-gpu-module-insertion.mlir | 19 ++++++++++++++++-
2 files changed, 29 insertions(+), 11 deletions(-)
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>
+ }
+}
More information about the Mlir-commits
mailing list