[llvm-branch-commits] [mlir] [mlir][acc] Translate the acc.offload_entry attribute to an offloadin… (PR #242809)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Nov 15 03:56:47 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-openacc
Author: Ivan R. Ivanov (ivanradanov)
<details>
<summary>Changes</summary>
…g entry
Add the acc.offload_entry unit attribute, which marks a global host symbol (either a function or a global variable) that the offload runtime must be able to associate with its device counterpart. The translation emits entries that are registered with the OpenACC runtime libacctarget.
Assisted-by: Claude Sonnet 5.5
---
Full diff: https://github.com/llvm/llvm-project/pull/242809.diff
4 Files Affected:
- (modified) mlir/include/mlir/Dialect/OpenACC/OpenACC.h (+7)
- (modified) mlir/lib/Target/LLVMIR/Dialect/OpenACC/CMakeLists.txt (+1)
- (modified) mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp (+47)
- (modified) mlir/test/Target/LLVMIR/openacc-offload.mlir (+24-2)
``````````diff
diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACC.h b/mlir/include/mlir/Dialect/OpenACC/OpenACC.h
index f770299b17fa0..1cd19756017cc 100644
--- a/mlir/include/mlir/Dialect/OpenACC/OpenACC.h
+++ b/mlir/include/mlir/Dialect/OpenACC/OpenACC.h
@@ -216,6 +216,13 @@ inline bool isSpecializedAccRoutine(mlir::Operation *op) {
op->hasDiscardableAttr(mlir::acc::getSpecializedRoutineAttrName());
}
+/// Attribute on global symbols that are associated with device-side symbols of
+/// the same name. The LLVM IR translation emits entries to be registered with
+/// the runtime.
+static constexpr StringLiteral getOffloadEntryAttrName() {
+ return StringLiteral("acc.offload_entry");
+}
+
static constexpr StringLiteral getFromDefaultClauseAttrName() {
return StringLiteral("acc.from_default");
}
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenACC/CMakeLists.txt b/mlir/lib/Target/LLVMIR/Dialect/OpenACC/CMakeLists.txt
index b7878da6e1f65..ce5f73fe49829 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenACC/CMakeLists.txt
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenACC/CMakeLists.txt
@@ -4,6 +4,7 @@ add_mlir_translation_library(MLIROpenACCToLLVMIRTranslation
LINK_COMPONENTS
BinaryFormat
Core
+ FrontendOffloading
Object
TransformUtils
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp
index b0af31a58a6d8..639c1489ea667 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp
@@ -21,9 +21,11 @@
#include "mlir/Dialect/OpenACC/OpenACC.h"
#include "mlir/IR/Operation.h"
#include "mlir/Support/LLVM.h"
+#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "llvm/ADT/TypeSwitch.h"
+#include "llvm/Frontend/Offloading/Utility.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
@@ -159,9 +161,54 @@ OffloadPackagerAttrImpl::launchKernel(Attribute, Operation *launchFuncOperation,
return launchFuncOperation->emitError("unexpected kernel launch");
}
+/// Registers the host function or variable `op` as an offloading entry of kind
+/// OpenACC.
+static LogicalResult
+convertOffloadEntry(Operation *op, LLVM::ModuleTranslation &moduleTranslation) {
+ llvm::Module *m = moduleTranslation.getLLVMModule();
+ llvm::GlobalValue *global = nullptr;
+ uint64_t size = 0;
+ if (auto func = dyn_cast<LLVM::LLVMFuncOp>(op)) {
+ global = moduleTranslation.lookupFunction(func.getName());
+ assert(global && "could not find the translated host symbol");
+ } else if (auto variable = dyn_cast<LLVM::GlobalOp>(op)) {
+ global = moduleTranslation.lookupGlobal(variable);
+ assert(global && "could not find the translated host symbol");
+ auto *llvmVariable = cast<llvm::GlobalVariable>(global);
+ size = m->getDataLayout().getTypeStoreSize(llvmVariable->getValueType());
+ } else {
+ return op->emitError("unsupported operation for '")
+ << acc::getOffloadEntryAttrName() << "'";
+ }
+
+ StringRef symbolName = global->getName();
+ llvm::offloading::emitOffloadingEntry(*m, llvm::object::OFK_OpenACC, global,
+ symbolName, size,
+ /*Flags=*/0, /*Data=*/0);
+ return success();
+}
+
+namespace {
+class OpenACCDialectLLVMIRTranslationInterface
+ : public LLVMTranslationDialectInterface {
+public:
+ using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
+
+ LogicalResult
+ amendOperation(Operation *op, ArrayRef<llvm::Instruction *> instructions,
+ NamedAttribute attribute,
+ LLVM::ModuleTranslation &moduleTranslation) const override {
+ if (attribute.getName() == acc::getOffloadEntryAttrName())
+ return convertOffloadEntry(op, moduleTranslation);
+ return success();
+ }
+};
+} // namespace
+
void mlir::registerOpenACCDialectTranslation(DialectRegistry ®istry) {
registry.insert<acc::OpenACCDialect>();
registry.addExtension(+[](MLIRContext *ctx, acc::OpenACCDialect *dialect) {
+ dialect->addInterfaces<OpenACCDialectLLVMIRTranslationInterface>();
acc::OffloadPackagerAttr::attachInterface<OffloadPackagerAttrImpl>(*ctx);
});
}
diff --git a/mlir/test/Target/LLVMIR/openacc-offload.mlir b/mlir/test/Target/LLVMIR/openacc-offload.mlir
index 1db3cade46b1a..8e822378ae7b4 100644
--- a/mlir/test/Target/LLVMIR/openacc-offload.mlir
+++ b/mlir/test/Target/LLVMIR/openacc-offload.mlir
@@ -1,10 +1,14 @@
// RUN: mlir-translate -mlir-to-llvmir -split-input-file -verify-diagnostics %s | FileCheck %s
-// CHECK: @device_module_binary = internal unnamed_addr constant [{{[0-9]+}} x i8] c"\10\FF\10\AD
-// CHECK-SAME: section ".llvm.offloading"
+// CHECK: @device_module_binary = internal unnamed_addr constant [{{[0-9]+}} x i8] c"{{.*}} section ".llvm.offloading"
// CHECK: @llvm.used = appending global {{.*}} @device_module_binary
+// CHECK: @.offloading.entry_name = internal unnamed_addr constant [9 x i8] c"kernel_0\00", section ".llvm.rodata.offloading"
+// CHECK: @.offloading.entry.kernel_0 = weak constant %struct.__tgt_offload_entry { i64 0, i16 1, i16 16, i32 0, ptr @kernel_0, ptr @.offloading.entry_name, i64 0, i64 0, ptr null }, section "llvm_offload_entries"
module attributes {gpu.container_module} {
+ llvm.func @kernel_0() attributes {acc.offload_entry} {
+ llvm.return
+ }
gpu.binary @device_module <#acc.offload_packager> [
#gpu.object<#nvvm.target<chip = "sm_80">, offload = "BC">]
}
@@ -41,3 +45,21 @@ module attributes {gpu.container_module} {
gpu.binary @fatbin <#acc.offload_packager> [
#gpu.object<#nvvm.target<chip = "sm_80">, fatbin = "">]
}
+
+// -----
+
+// CHECK: @.offloading.entry.var = weak constant %struct.__tgt_offload_entry { i64 0, i16 1, i16 16, i32 0, ptr @var, ptr @.offloading.entry_name, i64 4, i64 0, ptr null }, section "llvm_offload_entries"
+
+module {
+ llvm.mlir.global external @var(0 : i32) {acc.offload_entry} : i32
+}
+
+// -----
+
+module {
+ llvm.func @f() {
+ // expected-error @below {{unsupported operation for 'acc.offload_entry'}}
+ %c = llvm.mlir.constant(1 : i32) {acc.offload_entry} : i32
+ llvm.return
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/242809
More information about the llvm-branch-commits
mailing list