https://github.com/razvanlupusoru updated 
https://github.com/llvm/llvm-project/pull/244313

>From 25e6de80553ab4a849d2df075579ea7d784c2899 Mon Sep 17 00:00:00 2001
From: Razvan Lupusoru <[email protected]>
Date: Thu, 19 Nov 2026 09:49:58 -0800
Subject: [PATCH 1/2] [CIR][OpenACC] Attach acc global variable interfaces

Attaches the OpenACC dialect operation interfaces to
`cir.get_global` and `cir.global` which are used for implicit
`acc declare` determination.

Along with it, the OpenACC passes are registered so they
can run through `cir-opt` and a new test folder to hold
the IR tests for OpenACC dialect Transforms.
---
 .../Dialect/OpenACC/CIROpenACCOpsInterfaces.h |  40 ++++
 .../OpenACC/CIROpenACCOpsInterfaces.cpp       |  91 ++++++++
 clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt  |   1 +
 .../OpenACC/RegisterOpenACCExtensions.cpp     |   4 +
 .../test/CIR/OpenACC/acc-implicit-declare.cir | 199 ++++++++++++++++++
 clang/tools/cir-opt/CMakeLists.txt            |   1 +
 clang/tools/cir-opt/cir-opt.cpp               |   2 +
 7 files changed, 338 insertions(+)
 create mode 100644 
clang/include/clang/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.h
 create mode 100644 clang/lib/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.cpp
 create mode 100644 clang/test/CIR/OpenACC/acc-implicit-declare.cir

diff --git a/clang/include/clang/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.h 
b/clang/include/clang/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.h
new file mode 100644
index 00000000000000..80ca9726e49aa2
--- /dev/null
+++ b/clang/include/clang/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains external OpenACC operation interfaces for CIR.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_CIR_DIALECT_OPENACC_CIROPENACCOPSINTERFACES_H
+#define CLANG_CIR_DIALECT_OPENACC_CIROPENACCOPSINTERFACES_H
+
+#include "mlir/Dialect/OpenACC/OpenACC.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+
+namespace cir::acc {
+
+struct AddressOfGlobalModel
+    : public mlir::acc::AddressOfGlobalOpInterface::ExternalModel<
+          AddressOfGlobalModel, cir::GetGlobalOp> {
+  mlir::SymbolRefAttr getSymbol(mlir::Operation *op) const;
+};
+
+struct GlobalVariableModel
+    : public mlir::acc::GlobalVariableOpInterface::ExternalModel<
+          GlobalVariableModel, cir::GlobalOp> {
+  bool isConstant(mlir::Operation *op) const;
+  bool hasInitializer(mlir::Operation *op) const;
+  mlir::Region *getInitRegion(mlir::Operation *op) const;
+  bool isDeviceAccessible(mlir::Operation *op) const;
+  bool isInDeviceMemory(mlir::Operation *op) const;
+  bool isCompilerGenerated(mlir::Operation *op) const;
+};
+
+} // namespace cir::acc
+
+#endif // CLANG_CIR_DIALECT_OPENACC_CIROPENACCOPSINTERFACES_H
diff --git a/clang/lib/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.cpp 
b/clang/lib/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.cpp
new file mode 100644
index 00000000000000..1a36ba787d4d6a
--- /dev/null
+++ b/clang/lib/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.cpp
@@ -0,0 +1,91 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Implementation of external OpenACC operation interfaces for CIR.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.h"
+
+#include "clang/CIR/Dialect/IR/CIRTypes.h"
+#include "llvm/Support/ErrorHandling.h"
+
+using namespace mlir;
+using namespace cir;
+
+static std::optional<LangAddressSpace>
+getGlobalLangAddressSpace(Operation *op) {
+  auto langAS = dyn_cast_if_present<LangAddressSpaceAttr>(
+      cast<GlobalOp>(op).getAddrSpaceAttr());
+  if (!langAS)
+    return std::nullopt;
+  return langAS.getValue();
+}
+
+// Address spaces OpenACC treats as reachable from the device. This includes
+// memory that resides on the device and host memory the device can access
+// directly (`offload_global_host`).
+static bool isDeviceAccessibleAddressSpace(LangAddressSpace as) {
+  switch (as) {
+  case LangAddressSpace::OffloadPrivate:
+  case LangAddressSpace::OffloadLocal:
+  case LangAddressSpace::OffloadGlobal:
+  case LangAddressSpace::OffloadConstant:
+  case LangAddressSpace::OffloadGlobalDevice:
+  case LangAddressSpace::OffloadGlobalHost:
+    return true;
+  case LangAddressSpace::Default:
+  case LangAddressSpace::OffloadGeneric:
+    return false;
+  }
+  llvm_unreachable("unhandled CIR language address space");
+}
+
+// Address spaces whose storage OpenACC treats as residing on the device.
+static bool isDeviceResidentAddressSpace(LangAddressSpace as) {
+  if (as == LangAddressSpace::OffloadGlobalHost)
+    return false;
+  return isDeviceAccessibleAddressSpace(as);
+}
+
+SymbolRefAttr cir::acc::AddressOfGlobalModel::getSymbol(Operation *op) const {
+  return cast<GetGlobalOp>(op).getNameAttr();
+}
+
+bool cir::acc::GlobalVariableModel::isConstant(Operation *op) const {
+  return cast<GlobalOp>(op).getConstant();
+}
+
+bool cir::acc::GlobalVariableModel::hasInitializer(Operation *op) const {
+  return cast<GlobalOp>(op).hasInitializer();
+}
+
+Region *cir::acc::GlobalVariableModel::getInitRegion(Operation *op) const {
+  Region &ctorRegion = cast<GlobalOp>(op).getCtorRegion();
+  return ctorRegion.empty() ? nullptr : &ctorRegion;
+}
+
+bool cir::acc::GlobalVariableModel::isDeviceAccessible(Operation *op) const {
+  std::optional<LangAddressSpace> as = getGlobalLangAddressSpace(op);
+  return as && isDeviceAccessibleAddressSpace(*as);
+}
+
+bool cir::acc::GlobalVariableModel::isInDeviceMemory(Operation *op) const {
+  std::optional<LangAddressSpace> as = getGlobalLangAddressSpace(op);
+  return as && isDeviceResidentAddressSpace(*as);
+}
+
+// A leading '.' marks a symbol that is not a source identifier. The
+// `__const.` prefix marks a materialized constant aggregate. Ideally this
+// should be a property of the global itself, but having a property puts
+// burden on every creation site to decide it and this is a reasonable
+// approximation for now.
+bool cir::acc::GlobalVariableModel::isCompilerGenerated(Operation *op) const {
+  StringRef name = cast<GlobalOp>(op).getSymName();
+  return name.starts_with(".") || name.starts_with("__const.");
+}
\ No newline at end of file
diff --git a/clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt 
b/clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt
index de40a8138af474..3450a125b46803 100644
--- a/clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/OpenACC/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_clang_library(CIROpenACCSupport
+  CIROpenACCOpsInterfaces.cpp
   CIROpenACCTypeInterfaces.cpp
   RegisterOpenACCExtensions.cpp
 
diff --git a/clang/lib/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.cpp 
b/clang/lib/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.cpp
index 3dfe7aeb6b1d61..8226a9f3a4bc9e 100644
--- a/clang/lib/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.cpp
+++ b/clang/lib/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.cpp
@@ -13,6 +13,7 @@
 #include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h"
 #include "clang/CIR/Dialect/IR/CIRDialect.h"
 #include "clang/CIR/Dialect/IR/CIRTypes.h"
+#include "clang/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.h"
 #include "clang/CIR/Dialect/OpenACC/CIROpenACCTypeInterfaces.h"
 
 namespace cir::acc {
@@ -21,6 +22,9 @@ void registerOpenACCExtensions(mlir::DialectRegistry 
&registry) {
   registry.addExtension(+[](mlir::MLIRContext *ctx, cir::CIRDialect *dialect) {
     cir::PointerType::attachInterface<
         OpenACCPointerLikeModel<cir::PointerType>>(*ctx);
+
+    cir::GetGlobalOp::attachInterface<AddressOfGlobalModel>(*ctx);
+    cir::GlobalOp::attachInterface<GlobalVariableModel>(*ctx);
   });
 }
 
diff --git a/clang/test/CIR/OpenACC/acc-implicit-declare.cir 
b/clang/test/CIR/OpenACC/acc-implicit-declare.cir
new file mode 100644
index 00000000000000..d0550be9e953ab
--- /dev/null
+++ b/clang/test/CIR/OpenACC/acc-implicit-declare.cir
@@ -0,0 +1,199 @@
+// RUN: cir-opt %s --pass-pipeline="builtin.module(acc-implicit-declare)" 
-split-input-file 2>&1 | FileCheck %s
+
+// -----
+
+// A non-constant global in a compute region is hoisted so implicit mapping
+// can handle it. It is not marked with acc declare.
+
+!s32i = !cir.int<s, 32>
+
+cir.global external @gscalar = #cir.int<0> : !s32i
+
+cir.func @test_scalar_in_serial() {
+  acc.serial {
+    %addr = cir.get_global @gscalar : !cir.ptr<!s32i>
+    %val = cir.load %addr : !cir.ptr<!s32i>, !s32i
+    acc.yield
+  }
+  cir.return
+}
+
+// CHECK: cir.global external @gscalar =
+// CHECK-NOT: acc.declare
+// CHECK-LABEL: cir.func @test_scalar_in_serial
+// CHECK: %[[ADDR:.*]] = cir.get_global @gscalar
+// CHECK: acc.serial
+// CHECK: cir.load %[[ADDR]]
+
+// -----
+
+// An initialized constant stays in the device region and is declared copyin.
+
+!s32i = !cir.int<s, 32>
+
+cir.global constant external @gscalarconst = #cir.int<1> : !s32i
+
+cir.func @test_constant_in_serial() {
+  acc.serial {
+    %addr = cir.get_global @gscalarconst : !cir.ptr<!s32i>
+    %val = cir.load %addr : !cir.ptr<!s32i>, !s32i
+    acc.yield
+  }
+  cir.return
+}
+
+// CHECK: cir.global constant external @gscalarconst {{.*}} {acc.declare = 
#acc.declare<dataClause = acc_copyin>}
+// CHECK-LABEL: cir.func @test_constant_in_serial
+// CHECK: acc.serial
+// CHECK: cir.get_global @gscalarconst
+
+// -----
+
+// An external constant has no initializer in this translation unit, so its
+// address is hoisted for implicit mapping instead of being declared.
+// A declaration cannot have public visibility.
+
+!s32i = !cir.int<s, 32>
+
+cir.global "private" constant external @gexternalconstant : !s32i
+
+cir.func @test_external_constant_in_serial() {
+  acc.serial {
+    %addr = cir.get_global @gexternalconstant : !cir.ptr<!s32i>
+    %val = cir.load %addr : !cir.ptr<!s32i>, !s32i
+    acc.yield
+  }
+  cir.return
+}
+
+// CHECK: cir.global "private" constant external @gexternalconstant : !s32i
+// CHECK-NOT: acc.declare
+// CHECK-LABEL: cir.func @test_external_constant_in_serial
+// CHECK: cir.get_global @gexternalconstant
+// CHECK: acc.serial
+
+// -----
+
+// A global used from an acc routine cannot be mapped, so it is declared.
+
+!s32i = !cir.int<s, 32>
+
+cir.global external @gscalar_routine = #cir.int<0> : !s32i
+
+acc.routine @acc_routine_0 func(@test_scalar_in_accroutine)
+cir.func @test_scalar_in_accroutine() attributes {acc.routine_info = 
#acc.routine_info<[@acc_routine_0]>} {
+  %addr = cir.get_global @gscalar_routine : !cir.ptr<!s32i>
+  %val = cir.load %addr : !cir.ptr<!s32i>, !s32i
+  cir.return
+}
+
+// CHECK: cir.global external @gscalar_routine {{.*}} {acc.declare = 
#acc.declare<dataClause = acc_copyin>}
+
+// -----
+
+// A constant used from an acc routine is declared and stays in the function.
+
+!s32i = !cir.int<s, 32>
+
+cir.global constant external @gscalarconst_routine = #cir.int<1> : !s32i
+
+acc.routine @acc_routine_1 func(@test_constant_in_accroutine)
+cir.func @test_constant_in_accroutine() attributes {acc.routine_info = 
#acc.routine_info<[@acc_routine_1]>} {
+  %addr = cir.get_global @gscalarconst_routine : !cir.ptr<!s32i>
+  %val = cir.load %addr : !cir.ptr<!s32i>, !s32i
+  cir.return
+}
+
+// CHECK: cir.global constant external @gscalarconst_routine {{.*}} 
{acc.declare = #acc.declare<dataClause = acc_copyin>}
+// CHECK-LABEL: cir.func @test_constant_in_accroutine
+// CHECK: cir.get_global @gscalarconst_routine
+
+// -----
+
+// Device-resident storage is already valid on device, so it is neither
+// hoisted nor declared.
+
+!s32i = !cir.int<s, 32>
+
+cir.global external lang_address_space(offload_global) @gdevice = #cir.int<0> 
: !s32i
+
+cir.func @test_device_global() {
+  acc.serial {
+    %addr = cir.get_global @gdevice : !cir.ptr<!s32i, 
lang_address_space(offload_global)>
+    %val = cir.load %addr : !cir.ptr<!s32i, 
lang_address_space(offload_global)>, !s32i
+    acc.yield
+  }
+  cir.return
+}
+
+// CHECK: cir.global external lang_address_space(offload_global) @gdevice =
+// CHECK-NOT: acc.declare
+// CHECK-LABEL: cir.func @test_device_global
+// CHECK: acc.serial
+// CHECK: cir.get_global @gdevice
+
+// -----
+
+// Storage with offload_local resides on the device, so the reference
+// stays in the compute region.
+
+!s32i = !cir.int<s, 32>
+
+cir.global external lang_address_space(offload_local) @gshared = #cir.int<0> : 
!s32i
+
+cir.func @test_shared_global() {
+  acc.serial {
+    %addr = cir.get_global @gshared : !cir.ptr<!s32i, 
lang_address_space(offload_local)>
+    %val = cir.load %addr : !cir.ptr<!s32i, 
lang_address_space(offload_local)>, !s32i
+    acc.yield
+  }
+  cir.return
+}
+
+// CHECK: cir.global external lang_address_space(offload_local) @gshared =
+// CHECK-NOT: acc.declare
+// CHECK-LABEL: cir.func @test_shared_global
+// CHECK: acc.serial
+// CHECK: cir.get_global @gshared
+
+// -----
+
+// Host storage that the device may access directly (offload_global_host) is
+// device-accessible, so implicit declare leaves the reference in place.
+
+!s32i = !cir.int<s, 32>
+
+cir.global external lang_address_space(offload_global_host) @ghost = 
#cir.int<0> : !s32i
+
+cir.func @test_host_visible_global() {
+  acc.serial {
+    %addr = cir.get_global @ghost : !cir.ptr<!s32i, 
lang_address_space(offload_global_host)>
+    %val = cir.load %addr : !cir.ptr<!s32i, 
lang_address_space(offload_global_host)>, !s32i
+    acc.yield
+  }
+  cir.return
+}
+
+// CHECK: cir.global external lang_address_space(offload_global_host) @ghost =
+// CHECK-NOT: acc.declare
+// CHECK-LABEL: cir.func @test_host_visible_global
+// CHECK: acc.serial
+// CHECK: cir.get_global @ghost
+
+// -----
+
+// The constructor region of a global that is already declared is an
+// initialization region. Globals referenced from it are declared too.
+
+!s32i = !cir.int<s, 32>
+
+cir.global external @g_from_ctor = #cir.int<1> : !s32i
+
+cir.global external @g_declared = #cir.int<0> : !s32i ctor {
+  %0 = cir.get_global @g_from_ctor : !cir.ptr<!s32i>
+} {acc.declare = #acc.declare<dataClause = acc_copyin>}
+
+// CHECK: cir.global external @g_from_ctor {{.*}} {acc.declare = 
#acc.declare<dataClause = acc_copyin>}
+// CHECK: cir.global external @g_declared {{.*}} ctor
+// CHECK: cir.get_global @g_from_ctor
+// CHECK: {acc.declare = #acc.declare<dataClause = acc_copyin>}
diff --git a/clang/tools/cir-opt/CMakeLists.txt 
b/clang/tools/cir-opt/CMakeLists.txt
index 0203721cf5a1aa..02f21dbaea8669 100644
--- a/clang/tools/cir-opt/CMakeLists.txt
+++ b/clang/tools/cir-opt/CMakeLists.txt
@@ -26,6 +26,7 @@ target_link_libraries(cir-opt
   MLIRIR
   MLIRLLVMDialect
   MLIRMemRefDialect
+  MLIROpenACCTransforms
   MLIROpenMPTransforms
   MLIROptLib
   MLIRParser
diff --git a/clang/tools/cir-opt/cir-opt.cpp b/clang/tools/cir-opt/cir-opt.cpp
index 18a2343b2588ab..5b15f94d130bf9 100644
--- a/clang/tools/cir-opt/cir-opt.cpp
+++ b/clang/tools/cir-opt/cir-opt.cpp
@@ -15,6 +15,7 @@
 #include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h"
 #include "mlir/Dialect/MemRef/IR/MemRef.h"
+#include "mlir/Dialect/OpenACC/Transforms/Passes.h"
 #include "mlir/Dialect/OpenMP/Transforms/Passes.h"
 #include "mlir/Pass/PassManager.h"
 #include "mlir/Pass/PassOptions.h"
@@ -105,6 +106,7 @@ int main(int argc, char **argv) {
     return mlir::createCallConvLoweringPass();
   });
 
+  mlir::acc::registerOpenACCPasses();
   mlir::omp::registerOpenMPPasses();
   mlir::registerTransformsPasses();
 

>From 15b4e9b96ea5cb12d61ea141b8016bd8595f3330 Mon Sep 17 00:00:00 2001
From: Razvan Lupusoru <[email protected]>
Date: Thu, 19 Nov 2026 09:53:31 -0800
Subject: [PATCH 2/2] Add eof line

---
 clang/lib/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.cpp 
b/clang/lib/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.cpp
index 1a36ba787d4d6a..40b167f8295047 100644
--- a/clang/lib/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.cpp
+++ b/clang/lib/CIR/Dialect/OpenACC/CIROpenACCOpsInterfaces.cpp
@@ -88,4 +88,4 @@ bool 
cir::acc::GlobalVariableModel::isInDeviceMemory(Operation *op) const {
 bool cir::acc::GlobalVariableModel::isCompilerGenerated(Operation *op) const {
   StringRef name = cast<GlobalOp>(op).getSymName();
   return name.starts_with(".") || name.starts_with("__const.");
-}
\ No newline at end of file
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [clang] [... Razvan Lupusoru via cfe-commits
    • [cla... via cfe-commits
    • [cla... Razvan Lupusoru via cfe-commits
    • [cla... Razvan Lupusoru via cfe-commits
    • [cla... Valentin Clement バレンタイン クレメン via cfe-commits

Reply via email to