[llvm-branch-commits] [mlir] [mlir][acc] Translate #acc.offload_packager to LLVM IR (PR #242808)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 15 03:56:04 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-openacc

Author: Ivan R. Ivanov (ivanradanov)

<details>
<summary>Changes</summary>

Add a translation library for the OpenACC dialect that implements the offloading translation interface of #acc.offload_packager. Each object of a gpu.binary is wrapped in an offloading binary of kind OpenACC, carrying the target triple and architecture, and the images are placed in the .llvm.offloading section of the host module for the offloading linker wrapper to extract.

Assisted-by: Claude Sonnet 5.5

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


10 Files Affected:

- (modified) mlir/include/mlir/Target/LLVMIR/Dialect/All.h (+2) 
- (added) mlir/include/mlir/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.h (+29) 
- (modified) mlir/lib/Target/LLVMIR/CMakeLists.txt (+1) 
- (modified) mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt (+1) 
- (added) mlir/lib/Target/LLVMIR/Dialect/OpenACC/CMakeLists.txt (+20) 
- (added) mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp (+173) 
- (modified) mlir/test/CMakeLists.txt (+1) 
- (added) mlir/test/Target/LLVMIR/openacc-offload-objdump.mlir (+23) 
- (added) mlir/test/Target/LLVMIR/openacc-offload.mlir (+43) 
- (modified) mlir/test/lit.cfg.py (+1) 


``````````diff
diff --git a/mlir/include/mlir/Target/LLVMIR/Dialect/All.h b/mlir/include/mlir/Target/LLVMIR/Dialect/All.h
index 95e90789e9580b..05b66ace902ad3 100644
--- a/mlir/include/mlir/Target/LLVMIR/Dialect/All.h
+++ b/mlir/include/mlir/Target/LLVMIR/Dialect/All.h
@@ -23,6 +23,7 @@
 #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
 #include "mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h"
 #include "mlir/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.h"
+#include "mlir/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.h"
 #include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
 #include "mlir/Target/LLVMIR/Dialect/Ptr/PtrToLLVMIRTranslation.h"
 #include "mlir/Target/LLVMIR/Dialect/ROCDL/ROCDLToLLVMIRTranslation.h"
@@ -43,6 +44,7 @@ static inline void registerAllToLLVMIRTranslations(DialectRegistry &registry) {
   registerGPUDialectTranslation(registry);
   registerLLVMDialectTranslation(registry);
   registerNVVMDialectTranslation(registry);
+  registerOpenACCDialectTranslation(registry);
   registerOpenMPDialectTranslation(registry);
   registerPtrDialectTranslation(registry);
   registerROCDLDialectTranslation(registry);
diff --git a/mlir/include/mlir/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.h b/mlir/include/mlir/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.h
new file mode 100644
index 00000000000000..423a72651f4dca
--- /dev/null
+++ b/mlir/include/mlir/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.h
@@ -0,0 +1,29 @@
+//===- OpenACCToLLVMIRTranslation.h - OpenACC Dialect to LLVM IR -*- C++ -*-==//
+//
+// 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 provides registration calls for OpenACC dialect to LLVM IR translation.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TARGET_LLVMIR_DIALECT_OPENACC_OPENACCTOLLVMIRTRANSLATION_H
+#define MLIR_TARGET_LLVMIR_DIALECT_OPENACC_OPENACCTOLLVMIRTRANSLATION_H
+
+namespace mlir {
+class DialectRegistry;
+class MLIRContext;
+
+/// Register the OpenACC dialect and the translation from it to the LLVM IR in
+/// the given registry.
+void registerOpenACCDialectTranslation(DialectRegistry &registry);
+
+/// Register the OpenACC dialect and the translation from it in the registry
+/// associated with the given context.
+void registerOpenACCDialectTranslation(MLIRContext &context);
+} // namespace mlir
+
+#endif // MLIR_TARGET_LLVMIR_DIALECT_OPENACC_OPENACCTOLLVMIRTRANSLATION_H
diff --git a/mlir/lib/Target/LLVMIR/CMakeLists.txt b/mlir/lib/Target/LLVMIR/CMakeLists.txt
index 8f64d302c39fbb..585a70ebf9c7ef 100644
--- a/mlir/lib/Target/LLVMIR/CMakeLists.txt
+++ b/mlir/lib/Target/LLVMIR/CMakeLists.txt
@@ -56,6 +56,7 @@ add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
   MLIRGPUToLLVMIRTranslation
   MLIRLLVMToLLVMIRTranslation
   MLIRNVVMToLLVMIRTranslation
+  MLIROpenACCToLLVMIRTranslation
   MLIROpenMPToLLVMIRTranslation
   MLIRROCDLToLLVMIRTranslation
   MLIRPtrToLLVMIRTranslation
diff --git a/mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt b/mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt
index d590b236cfc915..a102c4323075b7 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt
+++ b/mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt
@@ -5,6 +5,7 @@ add_subdirectory(Builtin)
 add_subdirectory(GPU)
 add_subdirectory(LLVMIR)
 add_subdirectory(NVVM)
+add_subdirectory(OpenACC)
 add_subdirectory(OpenMP)
 add_subdirectory(ROCDL)
 add_subdirectory(Ptr)
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenACC/CMakeLists.txt b/mlir/lib/Target/LLVMIR/Dialect/OpenACC/CMakeLists.txt
new file mode 100644
index 00000000000000..b7878da6e1f650
--- /dev/null
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenACC/CMakeLists.txt
@@ -0,0 +1,20 @@
+add_mlir_translation_library(MLIROpenACCToLLVMIRTranslation
+  OpenACCToLLVMIRTranslation.cpp
+
+  LINK_COMPONENTS
+  BinaryFormat
+  Core
+  Object
+  TransformUtils
+
+  LINK_LIBS PUBLIC
+  MLIRGPUDialect
+  MLIRIR
+  MLIRLLVMDialect
+  MLIRNVVMDialect
+  MLIROpenACCDialect
+  MLIRROCDLDialect
+  MLIRSupport
+  MLIRTargetLLVMIRExport
+  MLIRXeVMDialect
+  )
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp
new file mode 100644
index 00000000000000..b0af31a58a6d86
--- /dev/null
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp
@@ -0,0 +1,173 @@
+//===- OpenACCToLLVMIRTranslation.cpp - Translate OpenACC to LLVM IR ------===//
+//
+// 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 implements the OpenACC to LLVM IR translation.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.h"
+
+#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
+#include "mlir/Dialect/GPU/IR/GPUDialect.h"
+#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/Dialect/LLVMIR/NVVMDialect.h"
+#include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
+#include "mlir/Dialect/LLVMIR/XeVMDialect.h"
+#include "mlir/Dialect/OpenACC/OpenACC.h"
+#include "mlir/IR/Operation.h"
+#include "mlir/Support/LLVM.h"
+#include "mlir/Target/LLVMIR/ModuleTranslation.h"
+
+#include "llvm/ADT/TypeSwitch.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/GlobalVariable.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Object/OffloadBinary.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
+
+using namespace mlir;
+
+namespace {
+/// The extracted generic information for a target specific offload attribute.
+struct OffloadTargetInfo {
+  StringRef triple;
+  StringRef arch;
+};
+
+/// Returns the offloading information of a GPU target attribute, or
+/// `std::nullopt` if the target is not supported.
+static std::optional<OffloadTargetInfo> getOffloadTargetInfo(Attribute target) {
+  return llvm::TypeSwitch<Attribute, std::optional<OffloadTargetInfo>>(target)
+      .Case([](NVVM::NVVMTargetAttr nvvm) {
+        return OffloadTargetInfo{nvvm.getTriple(), nvvm.getChip()};
+      })
+      .Case([](ROCDL::ROCDLTargetAttr rocdl) {
+        return OffloadTargetInfo{rocdl.getTriple(), rocdl.getChip()};
+      })
+      .Case([](xevm::XeVMTargetAttr xevm) {
+        return OffloadTargetInfo{xevm.getTriple(), xevm.getChip()};
+      })
+      .Default([](Attribute) { return std::nullopt; });
+}
+
+/// Implementation of the `OffloadingLLVMTranslationAttrInterface` model for
+/// `#acc.offload_packager`.
+class OffloadPackagerAttrImpl
+    : public gpu::OffloadingLLVMTranslationAttrInterface::FallbackModel<
+          OffloadPackagerAttrImpl> {
+public:
+  /// Embeds the objects of a `gpu.binary` in the host module as offloading
+  /// binaries placed in the `.llvm.offloading` section.
+  LogicalResult embedBinary(Attribute attribute, Operation *operation,
+                            llvm::IRBuilderBase &builder,
+                            LLVM::ModuleTranslation &moduleTranslation) const;
+
+  LogicalResult launchKernel(Attribute attribute,
+                             Operation *launchFuncOperation,
+                             Operation *binaryOperation,
+                             llvm::IRBuilderBase &builder,
+                             LLVM::ModuleTranslation &moduleTranslation) const;
+};
+} // namespace
+
+LogicalResult OffloadPackagerAttrImpl::embedBinary(
+    Attribute attribute, Operation *operation, llvm::IRBuilderBase &builder,
+    LLVM::ModuleTranslation &moduleTranslation) const {
+  assert(operation && "The binary operation must be non null.");
+  auto op = mlir::dyn_cast<gpu::BinaryOp>(operation);
+  if (!op) {
+    operation->emitError("operation must be a GPU binary");
+    return failure();
+  }
+
+  using namespace llvm::object;
+
+  SmallVector<char> binaryData;
+  llvm::raw_svector_ostream os(binaryData);
+  for (Attribute attr : op.getObjectsAttr().getValue()) {
+    auto object = dyn_cast<gpu::ObjectAttr>(attr);
+    if (!object)
+      return op.emitError("expected a gpu.object");
+
+    // The section is chosen by the offloading binary, so a requested one would
+    // be ignored.
+    if (DictionaryAttr properties = object.getProperties())
+      if (properties.get(gpu::elfSectionName))
+        return op.emitError("the offload packager does not support custom "
+                            "sections");
+
+    std::optional<OffloadTargetInfo> target =
+        getOffloadTargetInfo(object.getTarget());
+    if (!target)
+      return op.emitError("the offload packager does not support the target ")
+             << object.getTarget();
+
+    OffloadBinary::OffloadingImage image;
+    bool requiresNullTerminator = false;
+    switch (object.getFormat()) {
+    case gpu::CompilationTarget::Offload:
+      image.TheImageKind = IMG_Bitcode;
+      requiresNullTerminator = true;
+      break;
+    case gpu::CompilationTarget::Binary:
+      image.TheImageKind = IMG_Object;
+      requiresNullTerminator = false;
+      break;
+    case gpu::CompilationTarget::Assembly:
+    case gpu::CompilationTarget::Fatbin:
+      return op.emitError("the offloading linker wrapper cannot link the "
+                          "assembly or fatbin formats");
+    }
+
+    StringRef serialized = object.getObject().getValue();
+    image.Image = llvm::MemoryBuffer::getMemBuffer(
+        serialized, /*BufferName=*/"", requiresNullTerminator);
+    image.TheOffloadKind = OFK_OpenACC;
+    image.StringData["arch"] = target->arch;
+    image.StringData["triple"] = target->triple;
+
+    llvm::SmallString<0> buffer = OffloadBinary::write(image);
+    if (buffer.size() % OffloadBinary::getAlignment() != 0)
+      return op.emitError("offload binary has invalid size alignment");
+    os << buffer;
+  }
+
+  llvm::Module &m = *moduleTranslation.getLLVMModule();
+  llvm::Constant *serializedCst = llvm::ConstantDataArray::getString(
+      m.getContext(), os.str(), /*AddNull=*/false);
+  auto *serializedObj =
+      new llvm::GlobalVariable(m, serializedCst->getType(), /*isConstant=*/true,
+                               llvm::GlobalValue::InternalLinkage,
+                               serializedCst, op.getName() + "_binary");
+  serializedObj->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
+  serializedObj->setSection(".llvm.offloading");
+  serializedObj->setAlignment(llvm::Align(OffloadBinary::getAlignment()));
+  llvm::appendToUsed(m, {serializedObj});
+  return success();
+}
+
+LogicalResult
+OffloadPackagerAttrImpl::launchKernel(Attribute, Operation *launchFuncOperation,
+                                      Operation *, llvm::IRBuilderBase &,
+                                      LLVM::ModuleTranslation &) const {
+  return launchFuncOperation->emitError("unexpected kernel launch");
+}
+
+void mlir::registerOpenACCDialectTranslation(DialectRegistry &registry) {
+  registry.insert<acc::OpenACCDialect>();
+  registry.addExtension(+[](MLIRContext *ctx, acc::OpenACCDialect *dialect) {
+    acc::OffloadPackagerAttr::attachInterface<OffloadPackagerAttrImpl>(*ctx);
+  });
+}
+
+void mlir::registerOpenACCDialectTranslation(MLIRContext &context) {
+  DialectRegistry registry;
+  registerOpenACCDialectTranslation(registry);
+  context.appendDialectRegistry(registry);
+}
diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt
index 0329b46b52a476..05704008c46126 100644
--- a/mlir/test/CMakeLists.txt
+++ b/mlir/test/CMakeLists.txt
@@ -149,6 +149,7 @@ if(NOT MLIR_STANDALONE_BUILD)
   list(APPEND MLIR_TEST_DEPENDS
     FileCheck
     count
+    llvm-objdump
     llvm-strings
     not
     split-file
diff --git a/mlir/test/Target/LLVMIR/openacc-offload-objdump.mlir b/mlir/test/Target/LLVMIR/openacc-offload-objdump.mlir
new file mode 100644
index 00000000000000..32833646e50117
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/openacc-offload-objdump.mlir
@@ -0,0 +1,23 @@
+// REQUIRES: has_llc_binary
+// RUN: mlir-translate -mlir-to-llvmir %s | llc -filetype=obj -o %t.o
+// RUN: llvm-objdump --offloading %t.o | FileCheck %s
+
+// Check that the images embedded by the offload packager are read back as
+// offloading binaries from the object file of the host module.
+
+// CHECK: OFFLOADING IMAGE [0]:
+// CHECK-NEXT: kind            llvm ir
+// CHECK-NEXT: arch            sm_80
+// CHECK-NEXT: triple          nvptx64-nvidia-cuda
+// CHECK-NEXT: producer        openacc
+// CHECK: OFFLOADING IMAGE [1]:
+// CHECK-NEXT: kind            elf
+// CHECK-NEXT: arch            gfx90a
+// CHECK-NEXT: triple          amdgcn-amd-amdhsa
+// CHECK-NEXT: producer        openacc
+
+module attributes {gpu.container_module} {
+  gpu.binary @device_module <#acc.offload_packager> [
+    #gpu.object<#nvvm.target<chip = "sm_80">, offload = "BC">,
+    #gpu.object<#rocdl.target<chip = "gfx90a">, bin =  "\177ELF">]
+}
diff --git a/mlir/test/Target/LLVMIR/openacc-offload.mlir b/mlir/test/Target/LLVMIR/openacc-offload.mlir
new file mode 100644
index 00000000000000..1db3cade46b1a4
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/openacc-offload.mlir
@@ -0,0 +1,43 @@
+// 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: @llvm.used = appending global {{.*}} @device_module_binary
+
+module attributes {gpu.container_module} {
+  gpu.binary @device_module <#acc.offload_packager> [
+    #gpu.object<#nvvm.target<chip = "sm_80">, offload = "BC">]
+}
+
+// -----
+
+// CHECK: @nvidia_object_module_binary = internal unnamed_addr constant
+// CHECK: @amd_module_binary = internal unnamed_addr constant {{.*}}amdgcn-amd-amdhsa{{.*}}gfx90a
+// CHECK: @intel_module_binary = internal unnamed_addr constant {{.*}}spirv64-unknown-unknown{{.*}}bmg
+
+module attributes {gpu.container_module} {
+  gpu.binary @nvidia_object_module <#acc.offload_packager> [
+    #gpu.object<#nvvm.target<chip = "sm_80">, bin = "\177ELF">]
+  gpu.binary @amd_module <#acc.offload_packager> [
+    #gpu.object<#rocdl.target<chip = "gfx90a">, offload = "BC">]
+  gpu.binary @intel_module <#acc.offload_packager> [
+    #gpu.object<#xevm.target, offload = "BC">]
+}
+
+// -----
+
+module attributes {gpu.container_module} {
+  // expected-error @below {{cannot link the assembly or fatbin formats}}
+  // expected-error @below {{LLVM Translation failed for operation: gpu.binary}}
+  gpu.binary @ptx <#acc.offload_packager> [
+    #gpu.object<#nvvm.target<chip = "sm_80">, assembly = ".version 8.0">]
+}
+
+// -----
+
+module attributes {gpu.container_module} {
+  // expected-error @below {{cannot link the assembly or fatbin formats}}
+  // expected-error @below {{LLVM Translation failed for operation: gpu.binary}}
+  gpu.binary @fatbin <#acc.offload_packager> [
+    #gpu.object<#nvvm.target<chip = "sm_80">, fatbin = "">]
+}
diff --git a/mlir/test/lit.cfg.py b/mlir/test/lit.cfg.py
index ca070a37ebda50..998286b6380d61 100644
--- a/mlir/test/lit.cfg.py
+++ b/mlir/test/lit.cfg.py
@@ -178,6 +178,7 @@ def find_real_python_interpreter():
     llvm_config.with_environment("PATH", dirs, append_path=True)
 
 tools = [
+    "llvm-objdump",
     "llvm-strings",
     "mlir-tblgen",
     "mlir-translate",

``````````

</details>


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


More information about the llvm-branch-commits mailing list