[clang] [CIR][CUDA] Translate offloading entry attributes to LLVM IR (PR #241233)

Arseniy Obolenskiy via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 03:23:41 PST 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/241233

Let -fgpu-rdc reuse llvm::offloading::emitOffloadingEntry instead of rebuilding the entry layout in CIR

>From 896200aa30cde5b84a6d4e3e458f05b25a9c0eae Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 11 Nov 2026 12:13:31 +0100
Subject: [PATCH] [CIR][CUDA] Translate offloading entry attributes to LLVM IR

Let -fgpu-rdc reuse llvm::offloading::emitOffloadingEntry instead of rebuilding the entry layout in CIR
---
 .../clang/CIR/Dialect/IR/CIRCUDAAttrs.td      | 23 ++++++++++
 .../clang/CIR/Dialect/IR/CIRDialect.td        |  2 +
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       | 13 ++++++
 .../CIR/Lowering/DirectToLLVM/CMakeLists.txt  |  1 +
 .../Lowering/DirectToLLVM/LowerToLLVMIR.cpp   | 44 +++++++++++++++++++
 .../CIR/IR/invalid-cuda-offload-entries.cir   | 14 ++++++
 .../CIR/Lowering/cuda-offload-entries.cir     | 35 +++++++++++++++
 7 files changed, 132 insertions(+)
 create mode 100644 clang/test/CIR/IR/invalid-cuda-offload-entries.cir
 create mode 100644 clang/test/CIR/Lowering/cuda-offload-entries.cir

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRCUDAAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRCUDAAttrs.td
index ff58a0bd8eab1e..a03578c7ae91a1 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRCUDAAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRCUDAAttrs.td
@@ -79,4 +79,27 @@ def CIR_CUDAVarRegistrationInfoAttr : CIR_Attr<"CUDAVarRegistrationInfo", "cu.va
   let hasCustomAssemblyFormat = 1;
 }
 
+//===----------------------------------------------------------------------===//
+// CUDAOffloadEntryAttr
+//===----------------------------------------------------------------------===//
+
+def CIR_CUDAOffloadEntryAttr : CIR_Attr<"CUDAOffloadEntry", "cu.offload_entry"> {
+  let summary = "Offloading entry for a kernel or device variable.";
+  let description = [{
+    The arguments of `llvm::offloading::emitOffloadingEntry`, which emits the
+    entry during translation to LLVM IR. Used for -fgpu-rdc.
+  }];
+  let parameters = (ins
+    "uint16_t":$kind,
+    "mlir::FlatSymbolRefAttr":$addr,
+    "mlir::StringAttr":$name,
+    "uint64_t":$size,
+    "uint32_t":$flags,
+    "uint64_t":$data,
+    OptionalParameter<"mlir::FlatSymbolRefAttr">:$aux_addr
+  );
+  let assemblyFormat = "`<` struct(params) `>`";
+  let canHaveIllegalCXXABIType = 0;
+}
+
 #endif // CLANG_CIR_DIALECT_IR_CIRCUDAATTRS_TD
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
index 2177b80dcdab93..afd5004864df87 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
@@ -118,6 +118,8 @@ def CIR_Dialect : Dialect {
     // Raw bytes of the device-side fat binary, read by CIRGen so LoweringPrepare
     // can build the runtime-registration globals without doing file I/O.
     static llvm::StringRef getCUDADeviceBinaryAttrName() { return "cir.cu.device_binary"; }
+    // #cir.cu.offload_entry attributes, emitted after the globals they refer to.
+    static llvm::StringRef getCUDAOffloadEntriesAttrName() { return "cir.cu.offload_entries"; }
     // Mangled symbol name of the C++20 named-module initializer function,
     // precomputed by CIRGen so later passes don't need a live ASTContext.
     static llvm::StringRef getCXXModuleInitFnNameAttrName() { return "cir.cxx_module_init_fn_name"; }
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index de9f5461b97aea..cf3c6fbe81e9fb 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -330,6 +330,19 @@ cir::CIRDialect::verifyOperationAttribute(mlir::Operation *op,
                                        /*trailingZerosNum=*/0);
   }
 
+  if (attrName == getCUDAOffloadEntriesAttrName()) {
+    if (!mlir::isa<mlir::ModuleOp>(op))
+      return op->emitOpError() << "expects '" << getCUDAOffloadEntriesAttrName()
+                               << "' attribute to be attached to '"
+                               << mlir::ModuleOp::getOperationName() << "'";
+    auto entries = mlir::dyn_cast<mlir::ArrayAttr>(attr.getValue());
+    if (!entries ||
+        !llvm::all_of(entries, llvm::IsaPred<cir::CUDAOffloadEntryAttr>))
+      return op->emitOpError() << "expects '" << getCUDAOffloadEntriesAttrName()
+                               << "' to be an array of #cir.cu.offload_entry";
+    return success();
+  }
+
   if (attrName == getTripleAttrName()) {
     if (!mlir::isa<mlir::ModuleOp>(op))
       return op->emitOpError()
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
index 915456b90ecb5c..e0ca23072911ef 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
@@ -1,5 +1,6 @@
 set(LLVM_LINK_COMPONENTS
   Core
+  FrontendOffloading
   Support
   TransformUtils
   )
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp
index 2b80606fea6138..3f193ea248abd7 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp
@@ -17,6 +17,7 @@
 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
 #include "clang/CIR/Dialect/IR/CIRDialect.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/Frontend/Offloading/Utility.h"
 #include "llvm/IR/Constant.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/GlobalVariable.h"
@@ -29,6 +30,42 @@ using namespace llvm;
 namespace cir {
 namespace direct {
 
+namespace {
+/// Emits the -fgpu-rdc offloading entries once the globals they refer to have
+/// been translated.
+class CUDAOffloadEntries : public mlir::LLVM::ModuleTranslation::Extension {
+public:
+  MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CUDAOffloadEntries)
+
+  CUDAOffloadEntries(mlir::ModuleOp mod, mlir::ArrayAttr entries,
+                     llvm::Module &llvmModule)
+      : mod(mod), entries(entries), llvmModule(llvmModule) {}
+
+  mlir::LogicalResult finalize() override {
+    for (auto entry : entries.getAsRange<cir::CUDAOffloadEntryAttr>()) {
+      mlir::FlatSymbolRefAttr aux = entry.getAuxAddr();
+      llvm::GlobalValue *addr =
+          llvmModule.getNamedValue(entry.getAddr().getValue());
+      llvm::GlobalValue *auxAddr =
+          aux ? llvmModule.getNamedValue(aux.getValue()) : nullptr;
+      if (!addr || (aux && !auxAddr))
+        return mod.emitError("offloading entry refers to undefined symbol ")
+               << (addr ? aux : entry.getAddr());
+      llvm::offloading::emitOffloadingEntry(
+          llvmModule, static_cast<llvm::object::OffloadKind>(entry.getKind()),
+          addr, entry.getName(), entry.getSize(), entry.getFlags(),
+          entry.getData(), auxAddr);
+    }
+    return mlir::success();
+  }
+
+private:
+  mlir::ModuleOp mod;
+  mlir::ArrayAttr entries;
+  llvm::Module &llvmModule;
+};
+} // namespace
+
 /// Implementation of the dialect interface that converts CIR attributes to LLVM
 /// IR metadata.
 class CIRDialectLLVMIRTranslationInterface
@@ -211,6 +248,13 @@ class CIRDialectLLVMIRTranslationInterface
       }
     }
 
+    if (attribute.getName() ==
+        cir::CIRDialect::getCUDAOffloadEntriesAttrName()) {
+      if (auto entries = mlir::dyn_cast<mlir::ArrayAttr>(attribute.getValue()))
+        moduleTranslation.getOrCreateExtension<CUDAOffloadEntries>(mod, entries,
+                                                                   *llvmModule);
+    }
+
     if (attribute.getName() == "cir.amdgpu_sramecc") {
       if (auto intAttr =
               mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
diff --git a/clang/test/CIR/IR/invalid-cuda-offload-entries.cir b/clang/test/CIR/IR/invalid-cuda-offload-entries.cir
new file mode 100644
index 00000000000000..1eaf9ef6c6ec21
--- /dev/null
+++ b/clang/test/CIR/IR/invalid-cuda-offload-entries.cir
@@ -0,0 +1,14 @@
+// RUN: cir-opt %s -verify-diagnostics -split-input-file
+
+// expected-error at +1 {{expects 'cir.cu.offload_entries' to be an array of #cir.cu.offload_entry}}
+module attributes {cir.cu.offload_entries = [1 : i32]} {
+}
+
+// -----
+
+module {
+  // expected-error at +1 {{expects 'cir.cu.offload_entries' attribute to be attached to 'builtin.module'}}
+  cir.func @f() attributes {cir.cu.offload_entries = []} {
+    cir.return
+  }
+}
diff --git a/clang/test/CIR/Lowering/cuda-offload-entries.cir b/clang/test/CIR/Lowering/cuda-offload-entries.cir
new file mode 100644
index 00000000000000..a98b4ebb33905e
--- /dev/null
+++ b/clang/test/CIR/Lowering/cuda-offload-entries.cir
@@ -0,0 +1,35 @@
+// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering --split-input-file \
+// RUN:   -verify-diagnostics -o - | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+module attributes {cir.triple = "x86_64-unknown-linux-gnu", cir.cu.offload_entries = [
+    #cir.cu.offload_entry<kind = 4, addr = @_Z1kv, name = "_Z1kv", size = 0, flags = 0, data = 0>,
+    #cir.cu.offload_entry<kind = 4, addr = @mv.managed, name = "mv", size = 4, flags = 1, data = 4, aux_addr = @mv>]} {
+  cir.global external @mv.managed = #cir.int<1> : !s32i align(4)
+  cir.global external @mv = #cir.ptr<null> : !cir.ptr<!s32i>
+  cir.global constant external @_Z1kv = #cir.global_view<@_Z16__device_stub__kv> : !cir.ptr<!cir.func<()>> align(8)
+  cir.func @_Z16__device_stub__kv() {
+    cir.return
+  }
+}
+
+// CHECK: @.offloading.entry_name = internal unnamed_addr constant [6 x i8] c"_Z1kv\00", section ".llvm.rodata.offloading", align 1
+// CHECK: @.offloading.entry._Z1kv = weak constant %struct.__tgt_offload_entry { i64 0, i16 1, i16 4, i32 0, ptr @_Z1kv, ptr @.offloading.entry_name, i64 0, i64 0, ptr null }, section "llvm_offload_entries", align 8
+// CHECK: @.offloading.entry_name.1 = internal unnamed_addr constant [3 x i8] c"mv\00", section ".llvm.rodata.offloading", align 1
+// CHECK: @.offloading.entry.mv = weak constant %struct.__tgt_offload_entry { i64 0, i16 1, i16 4, i32 1, ptr @mv.managed, ptr @.offloading.entry_name.1, i64 4, i64 4, ptr @mv }, section "llvm_offload_entries", align 8
+
+// -----
+
+// expected-error @below {{offloading entry refers to undefined symbol @missing}}
+module attributes {cir.triple = "x86_64-unknown-linux-gnu", cir.cu.offload_entries = [
+    #cir.cu.offload_entry<kind = 4, addr = @missing, name = "missing", size = 0, flags = 0, data = 0>]} {
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+// expected-error @below {{offloading entry refers to undefined symbol @missing}}
+module attributes {cir.triple = "x86_64-unknown-linux-gnu", cir.cu.offload_entries = [
+    #cir.cu.offload_entry<kind = 4, addr = @mv.managed, name = "mv", size = 4, flags = 1, data = 4, aux_addr = @missing>]} {
+  cir.global external @mv.managed = #cir.int<1> : !s32i align(4)
+}



More information about the cfe-commits mailing list