[clang] [CIR] Introduce `cir-offload-merge` tool with a combine mode (PR #240322)
David Rivera via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 20:53:25 PST 2026
https://github.com/RiverDave updated https://github.com/llvm/llvm-project/pull/240322
>From e041ce98d0c5487cbdc40a47586142ce4eb4316e Mon Sep 17 00:00:00 2001
From: David Rivera <davidriverg at gmail.com>
Date: Wed, 4 Nov 2026 05:25:01 -0500
Subject: [PATCH 1/7] [CIR] Add cir-offload-merge tool with a combine mode
Nest a host CIR module and its device modules in an offload container.
Add cir.offload.bundle_id to record each module's offload bundle ID, since
modules can share a triple and differ only in target ID.
---
.../include/clang/CIR/Dialect/IR/CIRAttrs.td | 30 ++-
.../clang/CIR/Dialect/IR/CIRDialect.td | 1 +
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 23 ++
.../test/CIR/IR/invalid-offload-container.cir | 41 +++
clang/test/CIR/IR/offload-container.cir | 16 ++
clang/test/CIR/Tools/cir-offload-merge.cir | 101 ++++++++
clang/test/CMakeLists.txt | 2 +
clang/test/lit.cfg.py | 1 +
clang/tools/CMakeLists.txt | 1 +
clang/tools/cir-offload-merge/CMakeLists.txt | 23 ++
.../cir-offload-merge/cir-offload-merge.cpp | 234 ++++++++++++++++++
11 files changed, 470 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CIR/Tools/cir-offload-merge.cir
create mode 100644 clang/tools/cir-offload-merge/CMakeLists.txt
create mode 100644 clang/tools/cir-offload-merge/cir-offload-merge.cpp
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index 2fb7102b0ccf2c..997cfcb75bbea7 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -153,9 +153,33 @@ def CIR_OffloadKindAttr : CIR_EnumAttr<CIR_OffloadKind, "offload"> {
unchanged; the layout above is enforced by the CIR dialect attribute
verifier, following the `gpu.container_module` precedent.
- The attribute names `cir.offload.container` and `cir.offload.kind` are
- defined by the `getOffloadContainerAttrName` and `getOffloadKindAttrName`
- methods in the CIRDialect class.
+ A nested module may also carry `cir.offload.bundle_id`, a string holding
+ the offload bundle ID (`<offload kind>-<target triple>[-<target ID>]`, as
+ used by clang-offload-bundler) of the compilation the module came from,
+ e.g. `"hip-amdgcn-amd-amdhsa--gfx90a:xnack+"`. It identifies a module
+ when splitting the container back into per-target modules; the target
+ triple alone does not, since several device modules may share a triple
+ and only differ in their target ID. Bundle IDs are unique within a
+ container.
+
+ ```mlir
+ module attributes {cir.offload.container} {
+ module @host attributes {
+ cir.offload.kind = #cir.offload<host>,
+ cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu"} {}
+ module @device1 attributes {
+ cir.offload.kind = #cir.offload<device>,
+ cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+"} {}
+ module @device2 attributes {
+ cir.offload.kind = #cir.offload<device>,
+ cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-"} {}
+ }
+ ```
+
+ The attribute names `cir.offload.container`, `cir.offload.kind` and
+ `cir.offload.bundle_id` are defined by the `getOffloadContainerAttrName`,
+ `getOffloadKindAttrName` and `getOffloadBundleIDAttrName` methods in the
+ CIRDialect class.
}];
}
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
index 31283c32f0e44e..4987788ce3d5b4 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
@@ -75,6 +75,7 @@ def CIR_Dialect : Dialect {
static llvm::StringRef getGlobalDtorsAttrName() { return "cir.global_dtors"; }
static llvm::StringRef getOffloadContainerAttrName() { return "cir.offload.container"; }
static llvm::StringRef getOffloadKindAttrName() { return "cir.offload.kind"; }
+ static llvm::StringRef getOffloadBundleIDAttrName() { return "cir.offload.bundle_id"; }
static llvm::StringRef getOperandSegmentSizesAttrName() { return "operandSegmentSizes"; }
static llvm::StringRef getNoCallerSavedRegsAttrName() { return "no_caller_saved_registers"; }
static llvm::StringRef getNoCallbackAttrName() { return "nocallback"; }
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 31f8c142acb3c3..c1f65e889faeb9 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -33,6 +33,7 @@
#include "clang/CIR/MissingFeatures.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/LogicalResult.h"
@@ -254,6 +255,16 @@ static LogicalResult verifyOffloadContainer(mlir::Operation *op) {
if (failed(verifyOffloadKind(module, cir::OffloadKind::Device)))
return failure();
}
+
+ llvm::StringSet<> bundleIDs;
+ for (auto module : container.getOps<mlir::ModuleOp>()) {
+ auto bundleID = module->getAttrOfType<mlir::StringAttr>(
+ cir::CIRDialect::getOffloadBundleIDAttrName());
+ if (bundleID && !bundleIDs.insert(bundleID.getValue()).second)
+ return module.emitOpError()
+ << "duplicate '" << cir::CIRDialect::getOffloadBundleIDAttrName()
+ << "' value '" << bundleID.getValue() << "'";
+ }
return success();
}
@@ -304,6 +315,18 @@ cir::CIRDialect::verifyOperationAttribute(mlir::Operation *op,
<< "' attribute to be attached to '"
<< mlir::ModuleOp::getOperationName() << "'";
+ if (attrName == getOffloadBundleIDAttrName()) {
+ if (!mlir::isa<mlir::ModuleOp>(op))
+ return op->emitOpError() << "expects '" << getOffloadBundleIDAttrName()
+ << "' attribute to be attached to '"
+ << mlir::ModuleOp::getOperationName() << "'";
+ auto bundleID = mlir::dyn_cast<mlir::StringAttr>(attr.getValue());
+ if (!bundleID || bundleID.getValue().empty())
+ return op->emitOpError() << "expects '" << getOffloadBundleIDAttrName()
+ << "' to be a non-empty string";
+ return success();
+ }
+
if (attrName == getModuleFramePointerAttrName()) {
if (!mlir::isa<mlir::ModuleOp>(op))
return op->emitOpError() << "expects '" << getModuleFramePointerAttrName()
diff --git a/clang/test/CIR/IR/invalid-offload-container.cir b/clang/test/CIR/IR/invalid-offload-container.cir
index 95a8c77363efad..3cc8d446633ca4 100644
--- a/clang/test/CIR/IR/invalid-offload-container.cir
+++ b/clang/test/CIR/IR/invalid-offload-container.cir
@@ -78,3 +78,44 @@ module attributes {cir.offload.container = 0 : i32} {
module @device_0 attributes {cir.offload.kind = #cir.offload<device>} {
}
}
+
+// -----
+
+module attributes {cir.offload.container} {
+ module @host attributes {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu", cir.offload.kind = #cir.offload<host>} {
+ }
+ module @device_0 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80", cir.offload.kind = #cir.offload<device>} {
+ }
+ module @device_1 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80", cir.offload.kind = #cir.offload<device>} { // expected-error {{duplicate 'cir.offload.bundle_id' value 'cuda-nvptx64-nvidia-cuda--sm_80'}}
+ }
+}
+
+// -----
+
+module {
+ cir.func @f() {
+ cir.return {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu"} // expected-error {{expects 'cir.offload.bundle_id' attribute to be attached to 'builtin.module'}}
+ }
+}
+
+// -----
+
+module {
+ cir.func @f() {
+ cir.return
+ }
+ // expected-error at +1 {{expects 'cir.offload.bundle_id' to be a non-empty string}}
+ module @m attributes {cir.offload.bundle_id = 0 : i32} {
+ }
+}
+
+// -----
+
+module {
+ cir.func @f() {
+ cir.return
+ }
+ // expected-error at +1 {{expects 'cir.offload.bundle_id' to be a non-empty string}}
+ module @m attributes {cir.offload.bundle_id = ""} {
+ }
+}
diff --git a/clang/test/CIR/IR/offload-container.cir b/clang/test/CIR/IR/offload-container.cir
index 20a5b24cf7465a..ca61f3ad903967 100644
--- a/clang/test/CIR/IR/offload-container.cir
+++ b/clang/test/CIR/IR/offload-container.cir
@@ -26,3 +26,19 @@ module attributes {cir.offload.container} {
// CHECK-LABEL: module @host attributes {cir.offload.kind = #cir.offload<host>} {
// CHECK-LABEL: module @device_0 attributes {cir.offload.kind = #cir.offload<device>} {
// CHECK-LABEL: module @device_1 attributes {cir.offload.kind = #cir.offload<device>} {
+
+// -----
+
+module attributes {cir.offload.container} {
+ module @host attributes {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu", cir.offload.kind = #cir.offload<host>} {
+ }
+ module @device_0 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+", cir.offload.kind = #cir.offload<device>} {
+ }
+ module @device_1 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-", cir.offload.kind = #cir.offload<device>} {
+ }
+}
+
+// CHECK-LABEL: module attributes {cir.offload.container} {
+// CHECK-LABEL: module @host attributes {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu", cir.offload.kind = #cir.offload<host>} {
+// CHECK-LABEL: module @device_0 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+", cir.offload.kind = #cir.offload<device>} {
+// CHECK-LABEL: module @device_1 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-", cir.offload.kind = #cir.offload<device>} {
diff --git a/clang/test/CIR/Tools/cir-offload-merge.cir b/clang/test/CIR/Tools/cir-offload-merge.cir
new file mode 100644
index 00000000000000..4ccd8c34059ad4
--- /dev/null
+++ b/clang/test/CIR/Tools/cir-offload-merge.cir
@@ -0,0 +1,101 @@
+// RUN: rm -rf %t && split-file %s %t
+
+// RUN: cir-offload-merge -combine \
+// RUN: -input=%t/host.cir \
+// RUN: -input=%t/device-sm80.cir \
+// RUN: -targets=host-x86_64-unknown-linux-gnu,cuda-nvptx64-nvidia-cuda--sm_80 \
+// RUN: -output=%t/combined.cir
+// RUN: FileCheck %s --check-prefix=COMBINE --input-file=%t/combined.cir
+// RUN: cir-opt %t/combined.cir --verify-roundtrip -o /dev/null
+
+// COMBINE: module attributes {cir.offload.container} {
+// COMBINE-NEXT: module @host attributes {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu", cir.offload.kind = #cir.offload<host>, cir.triple = "x86_64-unknown-linux-gnu"} {
+// COMBINE-NEXT: cir.func{{.*}} @host_fn
+// COMBINE: module @device_cuda_nvptx64_nvidia_cuda__sm_80 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80", cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
+// COMBINE-NEXT: cir.func{{.*}} @device_fn
+
+// The host module goes first whatever the input order; device modules keep
+// their relative order.
+// RUN: cir-offload-merge -combine \
+// RUN: -input=%t/device-sm90.cir \
+// RUN: -input=%t/host.cir \
+// RUN: -input=%t/device-sm80.cir \
+// RUN: -targets=cuda-nvptx64-nvidia-cuda--sm_90,host-x86_64-unknown-linux-gnu,cuda-nvptx64-nvidia-cuda--sm_80 \
+// RUN: -output=%t/reordered.cir
+// RUN: FileCheck %s --check-prefix=REORDER --input-file=%t/reordered.cir
+
+// REORDER: module attributes {cir.offload.container} {
+// REORDER-NEXT: module @host
+// REORDER: module @device_cuda_nvptx64_nvidia_cuda__sm_90
+// REORDER: module @device_cuda_nvptx64_nvidia_cuda__sm_80
+
+// Device modules sharing a triple are told apart by their target ID.
+// RUN: cir-offload-merge -combine \
+// RUN: -input=%t/host.cir \
+// RUN: -input=%t/device-gfx90a.cir \
+// RUN: -input=%t/device-gfx90a.cir \
+// RUN: -targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a:xnack+,hip-amdgcn-amd-amdhsa--gfx90a:xnack- \
+// RUN: -output=%t/multi-arch.cir
+// RUN: FileCheck %s --check-prefix=MULTI-ARCH --input-file=%t/multi-arch.cir
+
+// MULTI-ARCH: module @device_hip_amdgcn_amd_amdhsa__gfx90a_xnack_ attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+"
+// MULTI-ARCH: module @device_hip_amdgcn_amd_amdhsa__gfx90a_xnack__1 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-"
+
+// RUN: not cir-offload-merge -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-MODE
+// ERR-NO-MODE: error: expected -combine
+
+// RUN: not cir-offload-merge -combine -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-INPUT
+// ERR-NO-INPUT: error: missing required -input
+
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-TARGETS
+// ERR-NO-TARGETS: error: missing required -targets
+
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu 2>&1 | FileCheck %s --check-prefix=ERR-NO-OUTPUT
+// ERR-NO-OUTPUT: error: missing required -output
+
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/device-sm80.cir -targets=host-x86_64-unknown-linux-gnu,cuda-nvptx64-nvidia-cuda--sm_80 -output=%t/a.cir -output=%t/b.cir 2>&1 | FileCheck %s --check-prefix=ERR-MULTI-OUTPUT
+// ERR-MULTI-OUTPUT: error: combine mode expects exactly one output
+
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/device-sm80.cir -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-COUNT
+// ERR-COUNT: error: number of input files and targets should match in combine mode
+
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -targets=not-a-target -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-BAD-TARGET
+// ERR-BAD-TARGET: error: invalid target 'not-a-target'
+
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/device-sm80.cir -targets=cuda-nvptx64-nvidia-cuda--sm_80,cuda-nvptx64-nvidia-cuda--sm_80 -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-DUP-TARGET
+// ERR-DUP-TARGET: error: duplicate target 'cuda-nvptx64-nvidia-cuda--sm_80'
+
+// RUN: not cir-offload-merge -combine -input=%t/device-sm80.cir -targets=cuda-nvptx64-nvidia-cuda--sm_80 -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-HOST
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu,host-aarch64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-HOST
+// ERR-NO-HOST: error: expected exactly one host target
+
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-DEVICE
+// ERR-NO-DEVICE: error: expected at least one device target
+
+// RUN: not cir-offload-merge -combine -input=%t/invalid.cir -input=%t/device-sm80.cir -targets=host-x86_64-unknown-linux-gnu,cuda-nvptx64-nvidia-cuda--sm_80 -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-PARSE
+// ERR-PARSE: error: failed to parse input file
+
+//--- host.cir
+module attributes {cir.triple = "x86_64-unknown-linux-gnu"} {
+ cir.func @host_fn() {
+ cir.return
+ }
+}
+
+//--- device-sm80.cir
+module attributes {cir.triple = "nvptx64-nvidia-cuda"} {
+ cir.func @device_fn() {
+ cir.return
+ }
+}
+
+//--- device-sm90.cir
+module {
+}
+
+//--- device-gfx90a.cir
+module {
+}
+
+//--- invalid.cir
+module {
diff --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt
index 3cbc8354900fa6..5859a264cf1fd7 100644
--- a/clang/test/CMakeLists.txt
+++ b/clang/test/CMakeLists.txt
@@ -115,6 +115,7 @@ list(APPEND CLANG_TEST_DEPS
if(CLANG_ENABLE_CIR)
list(APPEND CLANG_TEST_DEPS
+ cir-offload-merge
cir-opt
cir-translate
llvm-strings
@@ -257,6 +258,7 @@ if(CLANG_ENABLE_CIR)
# directly by tests under clang/test/CIR.
set(CLANG_CIR_TEST_DEPS
clang
+ cir-offload-merge
cir-opt
cir-translate
)
diff --git a/clang/test/lit.cfg.py b/clang/test/lit.cfg.py
index bd1dac8044f73b..73b6264ab3d3a8 100644
--- a/clang/test/lit.cfg.py
+++ b/clang/test/lit.cfg.py
@@ -138,6 +138,7 @@
]
if config.clang_enable_cir:
+ tools.append("cir-offload-merge")
tools.append("cir-opt")
if config.clang_examples:
diff --git a/clang/tools/CMakeLists.txt b/clang/tools/CMakeLists.txt
index 855af780d4e02a..81a1e5ced7cf4d 100644
--- a/clang/tools/CMakeLists.txt
+++ b/clang/tools/CMakeLists.txt
@@ -3,6 +3,7 @@ create_subdirectory_options(CLANG TOOL)
add_clang_subdirectory(diagtool)
add_clang_subdirectory(driver)
if(CLANG_ENABLE_CIR)
+ add_clang_subdirectory(cir-offload-merge)
add_clang_subdirectory(cir-opt)
add_clang_subdirectory(cir-translate)
add_clang_subdirectory(cir-lsp-server)
diff --git a/clang/tools/cir-offload-merge/CMakeLists.txt b/clang/tools/cir-offload-merge/CMakeLists.txt
new file mode 100644
index 00000000000000..8a7af9e4e51550
--- /dev/null
+++ b/clang/tools/cir-offload-merge/CMakeLists.txt
@@ -0,0 +1,23 @@
+include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
+include_directories(${LLVM_BINARY_DIR}/tools/mlir/include)
+
+add_clang_tool(cir-offload-merge
+ cir-offload-merge.cpp
+)
+
+clang_target_link_libraries(cir-offload-merge
+ PRIVATE
+ clangBasic
+ clangDriver
+)
+
+target_link_libraries(cir-offload-merge
+ PRIVATE
+ CIRRegisterAllDialects
+ MLIRCIR
+ MLIRIR
+ MLIRLLVMDialect
+ MLIRMemRefDialect
+ MLIRParser
+ MLIRSupport
+)
diff --git a/clang/tools/cir-offload-merge/cir-offload-merge.cpp b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
new file mode 100644
index 00000000000000..86411316de6592
--- /dev/null
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -0,0 +1,234 @@
+//===-- cir-offload-merge/cir-offload-merge.cpp ---------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// \file
+// This tool combines a host CIR module and its device CIR modules into a
+// single module marked as an offload container (carrying the
+// `cir.offload.container` unit attribute).
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/Dialect/MemRef/IR/MemRef.h"
+#include "mlir/IR/BuiltinAttributes.h"
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/MLIRContext.h"
+#include "mlir/IR/OwningOpRef.h"
+#include "mlir/IR/Verifier.h"
+#include "mlir/Parser/Parser.h"
+#include "mlir/Support/FileUtilities.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "clang/CIR/InitAllDialects.h"
+#include "clang/Driver/OffloadBundler.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringSet.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include <cctype>
+#include <string>
+
+namespace {
+
+llvm::cl::OptionCategory CIROffloadMergeCategory("cir-offload-merge options");
+
+llvm::cl::opt<bool> Combine("combine", llvm::cl::desc("Combine CIR inputs"),
+ llvm::cl::cat(CIROffloadMergeCategory));
+
+llvm::cl::list<std::string>
+ InputFileNames("input",
+ llvm::cl::desc("Input CIR file. Can be specified multiple "
+ "times."),
+ llvm::cl::cat(CIROffloadMergeCategory));
+
+llvm::cl::list<std::string>
+ TargetNames("targets", llvm::cl::CommaSeparated,
+ llvm::cl::desc("[<offload kind>-<target triple>,...]"),
+ llvm::cl::cat(CIROffloadMergeCategory));
+
+llvm::cl::list<std::string>
+ OutputFileNames("output", llvm::cl::desc("Output CIR file."),
+ llvm::cl::cat(CIROffloadMergeCategory));
+
+struct InputTarget {
+ std::string Input;
+ std::string Target;
+ bool IsHost = false;
+};
+
+int reportError(const llvm::Twine &message) {
+ llvm::errs() << "error: " << message << '\n';
+ return 1;
+}
+
+std::string sanitizeModuleName(llvm::StringRef target) {
+ std::string name = "device_";
+ for (char c : target) {
+ if (std::isalnum(static_cast<unsigned char>(c)))
+ name.push_back(c);
+ else
+ name.push_back('_');
+ }
+ return name;
+}
+
+std::string makeUnique(llvm::StringRef name,
+ llvm::StringMap<unsigned> &seenNames) {
+ unsigned &count = seenNames[name];
+ if (count++ == 0)
+ return name.str();
+ return (name + "_" + llvm::Twine(count - 1)).str();
+}
+
+bool isValidTarget(llvm::StringRef target,
+ const clang::OffloadBundlerConfig &bundlerConfig) {
+ if (!clang::checkOffloadBundleID(target))
+ return false;
+
+ clang::OffloadTargetInfo offloadInfo(target, bundlerConfig);
+ // The bundler has no "cuda" offload kind, but CUDA modules are combined here.
+ return (offloadInfo.isOffloadKindValid() ||
+ offloadInfo.OffloadKind == "cuda") &&
+ offloadInfo.isTripleValid();
+}
+
+int validateCombineCommandLine(
+ llvm::SmallVectorImpl<InputTarget> &inputTargets) {
+ if (InputFileNames.empty())
+ return reportError("missing required -input");
+ if (TargetNames.empty())
+ return reportError("missing required -targets");
+ if (OutputFileNames.empty())
+ return reportError("missing required -output");
+ if (OutputFileNames.size() != 1)
+ return reportError("combine mode expects exactly one output");
+ if (InputFileNames.size() != TargetNames.size())
+ return reportError("number of input files and targets should match in "
+ "combine mode");
+
+ clang::OffloadBundlerConfig bundlerConfig;
+ llvm::StringSet<> seenTargets;
+ unsigned numHostTargets = 0;
+
+ for (auto [input, target] : llvm::zip_equal(InputFileNames, TargetNames)) {
+ if (!seenTargets.insert(target).second)
+ return reportError("duplicate target '" + target + "'");
+ if (!isValidTarget(target, bundlerConfig))
+ return reportError("invalid target '" + target + "'");
+
+ bool isHost = clang::OffloadTargetInfo(target, bundlerConfig).hasHostKind();
+ numHostTargets += isHost;
+ inputTargets.push_back({input, target, isHost});
+ }
+
+ if (numHostTargets != 1)
+ return reportError("expected exactly one host target");
+ if (inputTargets.size() == 1)
+ return reportError("expected at least one device target");
+ return 0;
+}
+
+void setOffloadAttrs(mlir::ModuleOp module, llvm::StringRef name,
+ cir::OffloadKind offloadKind, llvm::StringRef bundleID) {
+ module.setSymName(name);
+ module->setAttr(cir::CIRDialect::getOffloadKindAttrName(),
+ cir::OffloadKindAttr::get(module.getContext(), offloadKind));
+ module->setAttr(cir::CIRDialect::getOffloadBundleIDAttrName(),
+ mlir::StringAttr::get(module.getContext(), bundleID));
+}
+
+mlir::OwningOpRef<mlir::ModuleOp>
+combineInputs(llvm::ArrayRef<InputTarget> inputTargets,
+ mlir::MLIRContext &context) {
+ mlir::OwningOpRef<mlir::ModuleOp> hostModule;
+ llvm::SmallVector<mlir::OwningOpRef<mlir::ModuleOp>> deviceModules;
+ llvm::StringMap<unsigned> deviceNames;
+
+ for (const InputTarget &inputTarget : inputTargets) {
+ mlir::OwningOpRef<mlir::ModuleOp> module =
+ mlir::parseSourceFile<mlir::ModuleOp>(inputTarget.Input,
+ mlir::ParserConfig(&context));
+ if (!module)
+ return {};
+
+ if (inputTarget.IsHost) {
+ setOffloadAttrs(*module, "host", cir::OffloadKind::Host,
+ inputTarget.Target);
+ hostModule = std::move(module);
+ continue;
+ }
+
+ std::string deviceName =
+ makeUnique(sanitizeModuleName(inputTarget.Target), deviceNames);
+ setOffloadAttrs(*module, deviceName, cir::OffloadKind::Device,
+ inputTarget.Target);
+ deviceModules.push_back(std::move(module));
+ }
+
+ // The container verifier expects the host module first, followed by the
+ // device modules.
+ mlir::ModuleOp container =
+ mlir::ModuleOp::create(mlir::UnknownLoc::get(&context));
+ container->setAttr(cir::CIRDialect::getOffloadContainerAttrName(),
+ mlir::UnitAttr::get(&context));
+ container.getBody()->push_back(hostModule.release());
+ for (mlir::OwningOpRef<mlir::ModuleOp> &deviceModule : deviceModules)
+ container.getBody()->push_back(deviceModule.release());
+ return container;
+}
+
+int writeModuleToOutput(mlir::ModuleOp module, llvm::StringRef outputFileName) {
+ // Type aliases are only printed for an operation without a parent.
+ assert(!module->getParentOp() && "expected a top-level module");
+ std::string errorMessage;
+ std::unique_ptr<llvm::ToolOutputFile> outputFile =
+ mlir::openOutputFile(outputFileName, &errorMessage);
+ if (!outputFile)
+ return reportError(errorMessage);
+
+ // TODO: Support bytecode output.
+ module->print(outputFile->os());
+ outputFile->os() << '\n';
+ outputFile->keep();
+ return 0;
+}
+
+} // namespace
+
+int main(int argc, char **argv) {
+ llvm::InitLLVM y(argc, argv);
+ llvm::cl::HideUnrelatedOptions(CIROffloadMergeCategory);
+ llvm::cl::ParseCommandLineOptions(argc, argv,
+ "CIR host-device offload merge\n");
+
+ if (!Combine)
+ return reportError("expected -combine");
+
+ mlir::DialectRegistry registry;
+ cir::registerAllDialects(registry);
+ registry.insert<mlir::memref::MemRefDialect, mlir::LLVM::LLVMDialect>();
+ mlir::MLIRContext context(registry);
+ context.loadDialect<cir::CIRDialect>();
+
+ llvm::SmallVector<InputTarget> inputTargets;
+ if (int errorCode = validateCombineCommandLine(inputTargets))
+ return errorCode;
+
+ mlir::OwningOpRef<mlir::ModuleOp> container =
+ combineInputs(inputTargets, context);
+ if (!container)
+ return reportError("failed to parse input file");
+ if (mlir::failed(mlir::verify(*container)))
+ return reportError("failed to verify combined module");
+
+ return writeModuleToOutput(*container, OutputFileNames.front());
+}
>From 0c19254766ada182557aaa0ceb074f7dc78415db Mon Sep 17 00:00:00 2001
From: David Rivera <davidriverg at gmail.com>
Date: Mon, 9 Nov 2026 04:32:45 -0500
Subject: [PATCH 2/7] [CIR] Use static functions in cir-offload-merge
Keep the anonymous namespace for the struct only, and comment why the
bundler is used to check targets.
---
.../cir-offload-merge/cir-offload-merge.cpp | 54 +++++++++++--------
1 file changed, 31 insertions(+), 23 deletions(-)
diff --git a/clang/tools/cir-offload-merge/cir-offload-merge.cpp b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
index 86411316de6592..664be79c5346cf 100644
--- a/clang/tools/cir-offload-merge/cir-offload-merge.cpp
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -37,40 +37,42 @@
#include <cctype>
#include <string>
-namespace {
-
-llvm::cl::OptionCategory CIROffloadMergeCategory("cir-offload-merge options");
+static llvm::cl::OptionCategory
+ CIROffloadMergeCategory("cir-offload-merge options");
-llvm::cl::opt<bool> Combine("combine", llvm::cl::desc("Combine CIR inputs"),
- llvm::cl::cat(CIROffloadMergeCategory));
+static llvm::cl::opt<bool> Combine("combine",
+ llvm::cl::desc("Combine CIR inputs"),
+ llvm::cl::cat(CIROffloadMergeCategory));
-llvm::cl::list<std::string>
+static llvm::cl::list<std::string>
InputFileNames("input",
llvm::cl::desc("Input CIR file. Can be specified multiple "
"times."),
llvm::cl::cat(CIROffloadMergeCategory));
-llvm::cl::list<std::string>
+static llvm::cl::list<std::string>
TargetNames("targets", llvm::cl::CommaSeparated,
llvm::cl::desc("[<offload kind>-<target triple>,...]"),
llvm::cl::cat(CIROffloadMergeCategory));
-llvm::cl::list<std::string>
+static llvm::cl::list<std::string>
OutputFileNames("output", llvm::cl::desc("Output CIR file."),
llvm::cl::cat(CIROffloadMergeCategory));
+namespace {
struct InputTarget {
std::string Input;
std::string Target;
bool IsHost = false;
};
+} // namespace
-int reportError(const llvm::Twine &message) {
+static int reportError(const llvm::Twine &message) {
llvm::errs() << "error: " << message << '\n';
return 1;
}
-std::string sanitizeModuleName(llvm::StringRef target) {
+static std::string sanitizeModuleName(llvm::StringRef target) {
std::string name = "device_";
for (char c : target) {
if (std::isalnum(static_cast<unsigned char>(c)))
@@ -81,16 +83,20 @@ std::string sanitizeModuleName(llvm::StringRef target) {
return name;
}
-std::string makeUnique(llvm::StringRef name,
- llvm::StringMap<unsigned> &seenNames) {
+static std::string makeUnique(llvm::StringRef name,
+ llvm::StringMap<unsigned> &seenNames) {
unsigned &count = seenNames[name];
if (count++ == 0)
return name.str();
return (name + "_" + llvm::Twine(count - 1)).str();
}
-bool isValidTarget(llvm::StringRef target,
- const clang::OffloadBundlerConfig &bundlerConfig) {
+// Targets are offload bundle IDs, so they are checked with
+// clang-offload-bundler's parser (thus, legalized). checkOffloadBundleID must
+// run first, since OffloadTargetInfo asserts on a malformed ID.
+static bool
+isValidCombineTarget(llvm::StringRef target,
+ const clang::OffloadBundlerConfig &bundlerConfig) {
if (!clang::checkOffloadBundleID(target))
return false;
@@ -101,8 +107,8 @@ bool isValidTarget(llvm::StringRef target,
offloadInfo.isTripleValid();
}
-int validateCombineCommandLine(
- llvm::SmallVectorImpl<InputTarget> &inputTargets) {
+static int
+validateCombineCommandLine(llvm::SmallVectorImpl<InputTarget> &inputTargets) {
if (InputFileNames.empty())
return reportError("missing required -input");
if (TargetNames.empty())
@@ -115,6 +121,8 @@ int validateCombineCommandLine(
return reportError("number of input files and targets should match in "
"combine mode");
+ // OffloadTargetInfo requires a bundler config; the defaults are enough to
+ // parse a target.
clang::OffloadBundlerConfig bundlerConfig;
llvm::StringSet<> seenTargets;
unsigned numHostTargets = 0;
@@ -122,7 +130,7 @@ int validateCombineCommandLine(
for (auto [input, target] : llvm::zip_equal(InputFileNames, TargetNames)) {
if (!seenTargets.insert(target).second)
return reportError("duplicate target '" + target + "'");
- if (!isValidTarget(target, bundlerConfig))
+ if (!isValidCombineTarget(target, bundlerConfig))
return reportError("invalid target '" + target + "'");
bool isHost = clang::OffloadTargetInfo(target, bundlerConfig).hasHostKind();
@@ -137,8 +145,9 @@ int validateCombineCommandLine(
return 0;
}
-void setOffloadAttrs(mlir::ModuleOp module, llvm::StringRef name,
- cir::OffloadKind offloadKind, llvm::StringRef bundleID) {
+static void setOffloadAttrs(mlir::ModuleOp module, llvm::StringRef name,
+ cir::OffloadKind offloadKind,
+ llvm::StringRef bundleID) {
module.setSymName(name);
module->setAttr(cir::CIRDialect::getOffloadKindAttrName(),
cir::OffloadKindAttr::get(module.getContext(), offloadKind));
@@ -146,7 +155,7 @@ void setOffloadAttrs(mlir::ModuleOp module, llvm::StringRef name,
mlir::StringAttr::get(module.getContext(), bundleID));
}
-mlir::OwningOpRef<mlir::ModuleOp>
+static mlir::OwningOpRef<mlir::ModuleOp>
combineInputs(llvm::ArrayRef<InputTarget> inputTargets,
mlir::MLIRContext &context) {
mlir::OwningOpRef<mlir::ModuleOp> hostModule;
@@ -186,7 +195,8 @@ combineInputs(llvm::ArrayRef<InputTarget> inputTargets,
return container;
}
-int writeModuleToOutput(mlir::ModuleOp module, llvm::StringRef outputFileName) {
+static int writeModuleToOutput(mlir::ModuleOp module,
+ llvm::StringRef outputFileName) {
// Type aliases are only printed for an operation without a parent.
assert(!module->getParentOp() && "expected a top-level module");
std::string errorMessage;
@@ -202,8 +212,6 @@ int writeModuleToOutput(mlir::ModuleOp module, llvm::StringRef outputFileName) {
return 0;
}
-} // namespace
-
int main(int argc, char **argv) {
llvm::InitLLVM y(argc, argv);
llvm::cl::HideUnrelatedOptions(CIROffloadMergeCategory);
>From 08e17a00fe832788df7aee2ed62342ebfc9ca42f Mon Sep 17 00:00:00 2001
From: David Rivera <davidriverg at gmail.com>
Date: Mon, 9 Nov 2026 05:22:06 -0500
Subject: [PATCH 3/7] [CIR] Name cir-offload-merge device modules by position
Name device modules @device_0, @device_1, ... in -targets order. The bundle
ID attribute already identifies each module.
---
clang/test/CIR/Tools/cir-offload-merge.cir | 10 ++++----
.../cir-offload-merge/cir-offload-merge.cpp | 24 +------------------
2 files changed, 6 insertions(+), 28 deletions(-)
diff --git a/clang/test/CIR/Tools/cir-offload-merge.cir b/clang/test/CIR/Tools/cir-offload-merge.cir
index 4ccd8c34059ad4..4c025932e91cca 100644
--- a/clang/test/CIR/Tools/cir-offload-merge.cir
+++ b/clang/test/CIR/Tools/cir-offload-merge.cir
@@ -11,7 +11,7 @@
// COMBINE: module attributes {cir.offload.container} {
// COMBINE-NEXT: module @host attributes {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu", cir.offload.kind = #cir.offload<host>, cir.triple = "x86_64-unknown-linux-gnu"} {
// COMBINE-NEXT: cir.func{{.*}} @host_fn
-// COMBINE: module @device_cuda_nvptx64_nvidia_cuda__sm_80 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80", cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
+// COMBINE: module @device_0 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80", cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
// COMBINE-NEXT: cir.func{{.*}} @device_fn
// The host module goes first whatever the input order; device modules keep
@@ -26,8 +26,8 @@
// REORDER: module attributes {cir.offload.container} {
// REORDER-NEXT: module @host
-// REORDER: module @device_cuda_nvptx64_nvidia_cuda__sm_90
-// REORDER: module @device_cuda_nvptx64_nvidia_cuda__sm_80
+// REORDER: module @device_0 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_90"
+// REORDER: module @device_1 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80"
// Device modules sharing a triple are told apart by their target ID.
// RUN: cir-offload-merge -combine \
@@ -38,8 +38,8 @@
// RUN: -output=%t/multi-arch.cir
// RUN: FileCheck %s --check-prefix=MULTI-ARCH --input-file=%t/multi-arch.cir
-// MULTI-ARCH: module @device_hip_amdgcn_amd_amdhsa__gfx90a_xnack_ attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+"
-// MULTI-ARCH: module @device_hip_amdgcn_amd_amdhsa__gfx90a_xnack__1 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-"
+// MULTI-ARCH: module @device_0 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+"
+// MULTI-ARCH: module @device_1 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-"
// RUN: not cir-offload-merge -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-MODE
// ERR-NO-MODE: error: expected -combine
diff --git a/clang/tools/cir-offload-merge/cir-offload-merge.cpp b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
index 664be79c5346cf..942606fb5cf611 100644
--- a/clang/tools/cir-offload-merge/cir-offload-merge.cpp
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -27,14 +27,12 @@
#include "clang/Driver/OffloadBundler.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/raw_ostream.h"
-#include <cctype>
#include <string>
static llvm::cl::OptionCategory
@@ -72,25 +70,6 @@ static int reportError(const llvm::Twine &message) {
return 1;
}
-static std::string sanitizeModuleName(llvm::StringRef target) {
- std::string name = "device_";
- for (char c : target) {
- if (std::isalnum(static_cast<unsigned char>(c)))
- name.push_back(c);
- else
- name.push_back('_');
- }
- return name;
-}
-
-static std::string makeUnique(llvm::StringRef name,
- llvm::StringMap<unsigned> &seenNames) {
- unsigned &count = seenNames[name];
- if (count++ == 0)
- return name.str();
- return (name + "_" + llvm::Twine(count - 1)).str();
-}
-
// Targets are offload bundle IDs, so they are checked with
// clang-offload-bundler's parser (thus, legalized). checkOffloadBundleID must
// run first, since OffloadTargetInfo asserts on a malformed ID.
@@ -160,7 +139,6 @@ combineInputs(llvm::ArrayRef<InputTarget> inputTargets,
mlir::MLIRContext &context) {
mlir::OwningOpRef<mlir::ModuleOp> hostModule;
llvm::SmallVector<mlir::OwningOpRef<mlir::ModuleOp>> deviceModules;
- llvm::StringMap<unsigned> deviceNames;
for (const InputTarget &inputTarget : inputTargets) {
mlir::OwningOpRef<mlir::ModuleOp> module =
@@ -177,7 +155,7 @@ combineInputs(llvm::ArrayRef<InputTarget> inputTargets,
}
std::string deviceName =
- makeUnique(sanitizeModuleName(inputTarget.Target), deviceNames);
+ ("device_" + llvm::Twine(deviceModules.size())).str();
setOffloadAttrs(*module, deviceName, cir::OffloadKind::Device,
inputTarget.Target);
deviceModules.push_back(std::move(module));
>From facc70f105f6c42e0ca05b4ff402a9e6e3f6c377 Mon Sep 17 00:00:00 2001
From: David Rivera <davidriverg at gmail.com>
Date: Tue, 10 Nov 2026 23:19:55 -0500
Subject: [PATCH 4/7] [CIR] Drop cir.offload.bundle_id
---
.../include/clang/CIR/Dialect/IR/CIRAttrs.td | 30 ++------------
.../clang/CIR/Dialect/IR/CIRDialect.td | 1 -
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 23 -----------
.../test/CIR/IR/invalid-offload-container.cir | 41 -------------------
clang/test/CIR/IR/offload-container.cir | 16 --------
clang/test/CIR/Tools/cir-offload-merge.cir | 15 +++----
.../cir-offload-merge/cir-offload-merge.cpp | 14 ++-----
7 files changed, 15 insertions(+), 125 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index 997cfcb75bbea7..2fb7102b0ccf2c 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -153,33 +153,9 @@ def CIR_OffloadKindAttr : CIR_EnumAttr<CIR_OffloadKind, "offload"> {
unchanged; the layout above is enforced by the CIR dialect attribute
verifier, following the `gpu.container_module` precedent.
- A nested module may also carry `cir.offload.bundle_id`, a string holding
- the offload bundle ID (`<offload kind>-<target triple>[-<target ID>]`, as
- used by clang-offload-bundler) of the compilation the module came from,
- e.g. `"hip-amdgcn-amd-amdhsa--gfx90a:xnack+"`. It identifies a module
- when splitting the container back into per-target modules; the target
- triple alone does not, since several device modules may share a triple
- and only differ in their target ID. Bundle IDs are unique within a
- container.
-
- ```mlir
- module attributes {cir.offload.container} {
- module @host attributes {
- cir.offload.kind = #cir.offload<host>,
- cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu"} {}
- module @device1 attributes {
- cir.offload.kind = #cir.offload<device>,
- cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+"} {}
- module @device2 attributes {
- cir.offload.kind = #cir.offload<device>,
- cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-"} {}
- }
- ```
-
- The attribute names `cir.offload.container`, `cir.offload.kind` and
- `cir.offload.bundle_id` are defined by the `getOffloadContainerAttrName`,
- `getOffloadKindAttrName` and `getOffloadBundleIDAttrName` methods in the
- CIRDialect class.
+ The attribute names `cir.offload.container` and `cir.offload.kind` are
+ defined by the `getOffloadContainerAttrName` and `getOffloadKindAttrName`
+ methods in the CIRDialect class.
}];
}
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
index 4987788ce3d5b4..31283c32f0e44e 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
@@ -75,7 +75,6 @@ def CIR_Dialect : Dialect {
static llvm::StringRef getGlobalDtorsAttrName() { return "cir.global_dtors"; }
static llvm::StringRef getOffloadContainerAttrName() { return "cir.offload.container"; }
static llvm::StringRef getOffloadKindAttrName() { return "cir.offload.kind"; }
- static llvm::StringRef getOffloadBundleIDAttrName() { return "cir.offload.bundle_id"; }
static llvm::StringRef getOperandSegmentSizesAttrName() { return "operandSegmentSizes"; }
static llvm::StringRef getNoCallerSavedRegsAttrName() { return "no_caller_saved_registers"; }
static llvm::StringRef getNoCallbackAttrName() { return "nocallback"; }
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index c1f65e889faeb9..31f8c142acb3c3 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -33,7 +33,6 @@
#include "clang/CIR/MissingFeatures.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SmallSet.h"
-#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/LogicalResult.h"
@@ -255,16 +254,6 @@ static LogicalResult verifyOffloadContainer(mlir::Operation *op) {
if (failed(verifyOffloadKind(module, cir::OffloadKind::Device)))
return failure();
}
-
- llvm::StringSet<> bundleIDs;
- for (auto module : container.getOps<mlir::ModuleOp>()) {
- auto bundleID = module->getAttrOfType<mlir::StringAttr>(
- cir::CIRDialect::getOffloadBundleIDAttrName());
- if (bundleID && !bundleIDs.insert(bundleID.getValue()).second)
- return module.emitOpError()
- << "duplicate '" << cir::CIRDialect::getOffloadBundleIDAttrName()
- << "' value '" << bundleID.getValue() << "'";
- }
return success();
}
@@ -315,18 +304,6 @@ cir::CIRDialect::verifyOperationAttribute(mlir::Operation *op,
<< "' attribute to be attached to '"
<< mlir::ModuleOp::getOperationName() << "'";
- if (attrName == getOffloadBundleIDAttrName()) {
- if (!mlir::isa<mlir::ModuleOp>(op))
- return op->emitOpError() << "expects '" << getOffloadBundleIDAttrName()
- << "' attribute to be attached to '"
- << mlir::ModuleOp::getOperationName() << "'";
- auto bundleID = mlir::dyn_cast<mlir::StringAttr>(attr.getValue());
- if (!bundleID || bundleID.getValue().empty())
- return op->emitOpError() << "expects '" << getOffloadBundleIDAttrName()
- << "' to be a non-empty string";
- return success();
- }
-
if (attrName == getModuleFramePointerAttrName()) {
if (!mlir::isa<mlir::ModuleOp>(op))
return op->emitOpError() << "expects '" << getModuleFramePointerAttrName()
diff --git a/clang/test/CIR/IR/invalid-offload-container.cir b/clang/test/CIR/IR/invalid-offload-container.cir
index 3cc8d446633ca4..95a8c77363efad 100644
--- a/clang/test/CIR/IR/invalid-offload-container.cir
+++ b/clang/test/CIR/IR/invalid-offload-container.cir
@@ -78,44 +78,3 @@ module attributes {cir.offload.container = 0 : i32} {
module @device_0 attributes {cir.offload.kind = #cir.offload<device>} {
}
}
-
-// -----
-
-module attributes {cir.offload.container} {
- module @host attributes {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu", cir.offload.kind = #cir.offload<host>} {
- }
- module @device_0 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80", cir.offload.kind = #cir.offload<device>} {
- }
- module @device_1 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80", cir.offload.kind = #cir.offload<device>} { // expected-error {{duplicate 'cir.offload.bundle_id' value 'cuda-nvptx64-nvidia-cuda--sm_80'}}
- }
-}
-
-// -----
-
-module {
- cir.func @f() {
- cir.return {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu"} // expected-error {{expects 'cir.offload.bundle_id' attribute to be attached to 'builtin.module'}}
- }
-}
-
-// -----
-
-module {
- cir.func @f() {
- cir.return
- }
- // expected-error at +1 {{expects 'cir.offload.bundle_id' to be a non-empty string}}
- module @m attributes {cir.offload.bundle_id = 0 : i32} {
- }
-}
-
-// -----
-
-module {
- cir.func @f() {
- cir.return
- }
- // expected-error at +1 {{expects 'cir.offload.bundle_id' to be a non-empty string}}
- module @m attributes {cir.offload.bundle_id = ""} {
- }
-}
diff --git a/clang/test/CIR/IR/offload-container.cir b/clang/test/CIR/IR/offload-container.cir
index ca61f3ad903967..20a5b24cf7465a 100644
--- a/clang/test/CIR/IR/offload-container.cir
+++ b/clang/test/CIR/IR/offload-container.cir
@@ -26,19 +26,3 @@ module attributes {cir.offload.container} {
// CHECK-LABEL: module @host attributes {cir.offload.kind = #cir.offload<host>} {
// CHECK-LABEL: module @device_0 attributes {cir.offload.kind = #cir.offload<device>} {
// CHECK-LABEL: module @device_1 attributes {cir.offload.kind = #cir.offload<device>} {
-
-// -----
-
-module attributes {cir.offload.container} {
- module @host attributes {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu", cir.offload.kind = #cir.offload<host>} {
- }
- module @device_0 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+", cir.offload.kind = #cir.offload<device>} {
- }
- module @device_1 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-", cir.offload.kind = #cir.offload<device>} {
- }
-}
-
-// CHECK-LABEL: module attributes {cir.offload.container} {
-// CHECK-LABEL: module @host attributes {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu", cir.offload.kind = #cir.offload<host>} {
-// CHECK-LABEL: module @device_0 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+", cir.offload.kind = #cir.offload<device>} {
-// CHECK-LABEL: module @device_1 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-", cir.offload.kind = #cir.offload<device>} {
diff --git a/clang/test/CIR/Tools/cir-offload-merge.cir b/clang/test/CIR/Tools/cir-offload-merge.cir
index 4c025932e91cca..e5ef9bdb7cf2c4 100644
--- a/clang/test/CIR/Tools/cir-offload-merge.cir
+++ b/clang/test/CIR/Tools/cir-offload-merge.cir
@@ -9,9 +9,9 @@
// RUN: cir-opt %t/combined.cir --verify-roundtrip -o /dev/null
// COMBINE: module attributes {cir.offload.container} {
-// COMBINE-NEXT: module @host attributes {cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu", cir.offload.kind = #cir.offload<host>, cir.triple = "x86_64-unknown-linux-gnu"} {
+// COMBINE-NEXT: module @host attributes {cir.offload.kind = #cir.offload<host>, cir.triple = "x86_64-unknown-linux-gnu"} {
// COMBINE-NEXT: cir.func{{.*}} @host_fn
-// COMBINE: module @device_0 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80", cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
+// COMBINE: module @device_0 attributes {cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
// COMBINE-NEXT: cir.func{{.*}} @device_fn
// The host module goes first whatever the input order; device modules keep
@@ -26,10 +26,11 @@
// REORDER: module attributes {cir.offload.container} {
// REORDER-NEXT: module @host
-// REORDER: module @device_0 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_90"
-// REORDER: module @device_1 attributes {cir.offload.bundle_id = "cuda-nvptx64-nvidia-cuda--sm_80"
+// REORDER: module @device_0 attributes {cir.offload.kind = #cir.offload<device>} {
+// REORDER-NEXT: }
+// REORDER-NEXT: module @device_1 attributes {cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
-// Device modules sharing a triple are told apart by their target ID.
+// Device targets may share a triple and only differ in their target ID.
// RUN: cir-offload-merge -combine \
// RUN: -input=%t/host.cir \
// RUN: -input=%t/device-gfx90a.cir \
@@ -38,8 +39,8 @@
// RUN: -output=%t/multi-arch.cir
// RUN: FileCheck %s --check-prefix=MULTI-ARCH --input-file=%t/multi-arch.cir
-// MULTI-ARCH: module @device_0 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+"
-// MULTI-ARCH: module @device_1 attributes {cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-"
+// MULTI-ARCH: module @device_0 attributes {cir.offload.kind = #cir.offload<device>}
+// MULTI-ARCH: module @device_1 attributes {cir.offload.kind = #cir.offload<device>}
// RUN: not cir-offload-merge -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-MODE
// ERR-NO-MODE: error: expected -combine
diff --git a/clang/tools/cir-offload-merge/cir-offload-merge.cpp b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
index 942606fb5cf611..ba35a6499ca809 100644
--- a/clang/tools/cir-offload-merge/cir-offload-merge.cpp
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -60,7 +60,6 @@ static llvm::cl::list<std::string>
namespace {
struct InputTarget {
std::string Input;
- std::string Target;
bool IsHost = false;
};
} // namespace
@@ -114,7 +113,7 @@ validateCombineCommandLine(llvm::SmallVectorImpl<InputTarget> &inputTargets) {
bool isHost = clang::OffloadTargetInfo(target, bundlerConfig).hasHostKind();
numHostTargets += isHost;
- inputTargets.push_back({input, target, isHost});
+ inputTargets.push_back({input, isHost});
}
if (numHostTargets != 1)
@@ -125,13 +124,10 @@ validateCombineCommandLine(llvm::SmallVectorImpl<InputTarget> &inputTargets) {
}
static void setOffloadAttrs(mlir::ModuleOp module, llvm::StringRef name,
- cir::OffloadKind offloadKind,
- llvm::StringRef bundleID) {
+ cir::OffloadKind offloadKind) {
module.setSymName(name);
module->setAttr(cir::CIRDialect::getOffloadKindAttrName(),
cir::OffloadKindAttr::get(module.getContext(), offloadKind));
- module->setAttr(cir::CIRDialect::getOffloadBundleIDAttrName(),
- mlir::StringAttr::get(module.getContext(), bundleID));
}
static mlir::OwningOpRef<mlir::ModuleOp>
@@ -148,16 +144,14 @@ combineInputs(llvm::ArrayRef<InputTarget> inputTargets,
return {};
if (inputTarget.IsHost) {
- setOffloadAttrs(*module, "host", cir::OffloadKind::Host,
- inputTarget.Target);
+ setOffloadAttrs(*module, "host", cir::OffloadKind::Host);
hostModule = std::move(module);
continue;
}
std::string deviceName =
("device_" + llvm::Twine(deviceModules.size())).str();
- setOffloadAttrs(*module, deviceName, cir::OffloadKind::Device,
- inputTarget.Target);
+ setOffloadAttrs(*module, deviceName, cir::OffloadKind::Device);
deviceModules.push_back(std::move(module));
}
>From af9cba1c13b12a15742d4c1a5516af15c8b53cc0 Mon Sep 17 00:00:00 2001
From: David Rivera <davidriverg at gmail.com>
Date: Tue, 10 Nov 2026 23:46:12 -0500
Subject: [PATCH 5/7] [CIR] Infer cir-offload-merge roles from lowering options
---
clang/test/CIR/Tools/cir-offload-merge.cir | 76 +++++------
clang/tools/cir-offload-merge/CMakeLists.txt | 6 -
.../cir-offload-merge/cir-offload-merge.cpp | 118 +++++++-----------
3 files changed, 87 insertions(+), 113 deletions(-)
diff --git a/clang/test/CIR/Tools/cir-offload-merge.cir b/clang/test/CIR/Tools/cir-offload-merge.cir
index e5ef9bdb7cf2c4..3ed14a696bb3b1 100644
--- a/clang/test/CIR/Tools/cir-offload-merge.cir
+++ b/clang/test/CIR/Tools/cir-offload-merge.cir
@@ -3,15 +3,14 @@
// RUN: cir-offload-merge -combine \
// RUN: -input=%t/host.cir \
// RUN: -input=%t/device-sm80.cir \
-// RUN: -targets=host-x86_64-unknown-linux-gnu,cuda-nvptx64-nvidia-cuda--sm_80 \
// RUN: -output=%t/combined.cir
// RUN: FileCheck %s --check-prefix=COMBINE --input-file=%t/combined.cir
// RUN: cir-opt %t/combined.cir --verify-roundtrip -o /dev/null
// COMBINE: module attributes {cir.offload.container} {
-// COMBINE-NEXT: module @host attributes {cir.offload.kind = #cir.offload<host>, cir.triple = "x86_64-unknown-linux-gnu"} {
+// COMBINE-NEXT: module @host attributes {{{.*}}cir.offload.kind = #cir.offload<host>, cir.triple = "x86_64-unknown-linux-gnu"} {
// COMBINE-NEXT: cir.func{{.*}} @host_fn
-// COMBINE: module @device_0 attributes {cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
+// COMBINE: module @device_0 attributes {{{.*}}cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
// COMBINE-NEXT: cir.func{{.*}} @device_fn
// The host module goes first whatever the input order; device modules keep
@@ -20,83 +19,86 @@
// RUN: -input=%t/device-sm90.cir \
// RUN: -input=%t/host.cir \
// RUN: -input=%t/device-sm80.cir \
-// RUN: -targets=cuda-nvptx64-nvidia-cuda--sm_90,host-x86_64-unknown-linux-gnu,cuda-nvptx64-nvidia-cuda--sm_80 \
// RUN: -output=%t/reordered.cir
// RUN: FileCheck %s --check-prefix=REORDER --input-file=%t/reordered.cir
// REORDER: module attributes {cir.offload.container} {
// REORDER-NEXT: module @host
-// REORDER: module @device_0 attributes {cir.offload.kind = #cir.offload<device>} {
+// REORDER: module @device_0 attributes {{{.*}}cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
// REORDER-NEXT: }
-// REORDER-NEXT: module @device_1 attributes {cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
+// REORDER-NEXT: module @device_1 attributes {{{.*}}cir.offload.kind = #cir.offload<device>, cir.triple = "nvptx64-nvidia-cuda"} {
+// REORDER-NEXT: cir.func{{.*}} @device_fn
-// Device targets may share a triple and only differ in their target ID.
// RUN: cir-offload-merge -combine \
-// RUN: -input=%t/host.cir \
-// RUN: -input=%t/device-gfx90a.cir \
+// RUN: -input=%t/host-hip.cir \
// RUN: -input=%t/device-gfx90a.cir \
-// RUN: -targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a:xnack+,hip-amdgcn-amd-amdhsa--gfx90a:xnack- \
-// RUN: -output=%t/multi-arch.cir
-// RUN: FileCheck %s --check-prefix=MULTI-ARCH --input-file=%t/multi-arch.cir
+// RUN: -output=%t/hip.cir
+// RUN: FileCheck %s --check-prefix=HIP --input-file=%t/hip.cir
-// MULTI-ARCH: module @device_0 attributes {cir.offload.kind = #cir.offload<device>}
-// MULTI-ARCH: module @device_1 attributes {cir.offload.kind = #cir.offload<device>}
+// HIP: module @host attributes {{{.*}}cir.offload.kind = #cir.offload<host>
+// HIP: module @device_0 attributes {{{.*}}cir.offload.kind = #cir.offload<device>
-// RUN: not cir-offload-merge -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-MODE
+// RUN: not cir-offload-merge -input=%t/host.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-MODE
// ERR-NO-MODE: error: expected -combine
-// RUN: not cir-offload-merge -combine -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-INPUT
+// RUN: not cir-offload-merge -combine -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-INPUT
// ERR-NO-INPUT: error: missing required -input
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-TARGETS
-// ERR-NO-TARGETS: error: missing required -targets
-
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu 2>&1 | FileCheck %s --check-prefix=ERR-NO-OUTPUT
+// RUN: not cir-offload-merge -combine -input=%t/host.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-OUTPUT
// ERR-NO-OUTPUT: error: missing required -output
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/device-sm80.cir -targets=host-x86_64-unknown-linux-gnu,cuda-nvptx64-nvidia-cuda--sm_80 -output=%t/a.cir -output=%t/b.cir 2>&1 | FileCheck %s --check-prefix=ERR-MULTI-OUTPUT
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/device-sm80.cir -output=%t/a.cir -output=%t/b.cir 2>&1 | FileCheck %s --check-prefix=ERR-MULTI-OUTPUT
// ERR-MULTI-OUTPUT: error: combine mode expects exactly one output
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/device-sm80.cir -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-COUNT
-// ERR-COUNT: error: number of input files and targets should match in combine mode
+// RUN: not cir-offload-merge -combine -input=%t/device-sm80.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-HOSTS
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/host-hip.cir -input=%t/device-sm80.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-HOSTS
+// ERR-HOSTS: error: expected exactly one host module
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -targets=not-a-target -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-BAD-TARGET
-// ERR-BAD-TARGET: error: invalid target 'not-a-target'
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-DEVICE
+// ERR-NO-DEVICE: error: expected at least one device module
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/device-sm80.cir -targets=cuda-nvptx64-nvidia-cuda--sm_80,cuda-nvptx64-nvidia-cuda--sm_80 -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-DUP-TARGET
-// ERR-DUP-TARGET: error: duplicate target 'cuda-nvptx64-nvidia-cuda--sm_80'
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/no-lowering-options.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-OPTS
+// ERR-NO-OPTS: error: '{{.*}}no-lowering-options.cir' has no 'cir.lowering_options' attribute
-// RUN: not cir-offload-merge -combine -input=%t/device-sm80.cir -targets=cuda-nvptx64-nvidia-cuda--sm_80 -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-HOST
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu,host-aarch64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-HOST
-// ERR-NO-HOST: error: expected exactly one host target
+// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/not-offload.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NOT-OFFLOAD
+// ERR-NOT-OFFLOAD: error: '{{.*}}not-offload.cir' is not a CUDA or HIP compilation
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -targets=host-x86_64-unknown-linux-gnu -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-DEVICE
-// ERR-NO-DEVICE: error: expected at least one device target
-
-// RUN: not cir-offload-merge -combine -input=%t/invalid.cir -input=%t/device-sm80.cir -targets=host-x86_64-unknown-linux-gnu,cuda-nvptx64-nvidia-cuda--sm_80 -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-PARSE
+// RUN: not cir-offload-merge -combine -input=%t/invalid.cir -input=%t/device-sm80.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-PARSE
// ERR-PARSE: error: failed to parse input file
//--- host.cir
-module attributes {cir.triple = "x86_64-unknown-linux-gnu"} {
+module attributes {cir.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = true, cuda_is_device = false, hip = false, hlsl = false, gpu_rdc = false, openmp = false, openmp_is_target_device = false, clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = false, no_trapping_math = true, ssp_buffer_size = 8>, cir.triple = "x86_64-unknown-linux-gnu"} {
cir.func @host_fn() {
cir.return
}
}
//--- device-sm80.cir
-module attributes {cir.triple = "nvptx64-nvidia-cuda"} {
+module attributes {cir.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = true, cuda_is_device = true, hip = false, hlsl = false, gpu_rdc = false, openmp = false, openmp_is_target_device = false, clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = false, no_trapping_math = true, ssp_buffer_size = 8>, cir.triple = "nvptx64-nvidia-cuda"} {
cir.func @device_fn() {
cir.return
}
}
//--- device-sm90.cir
-module {
+module attributes {cir.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = true, cuda_is_device = true, hip = false, hlsl = false, gpu_rdc = false, openmp = false, openmp_is_target_device = false, clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = false, no_trapping_math = true, ssp_buffer_size = 8>, cir.triple = "nvptx64-nvidia-cuda"} {
+}
+
+//--- host-hip.cir
+module attributes {cir.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = true, cuda_is_device = false, hip = true, hlsl = false, gpu_rdc = false, openmp = false, openmp_is_target_device = false, clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = false, no_trapping_math = true, ssp_buffer_size = 8>} {
}
//--- device-gfx90a.cir
+module attributes {cir.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = true, cuda_is_device = true, hip = true, hlsl = false, gpu_rdc = false, openmp = false, openmp_is_target_device = false, clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = false, no_trapping_math = true, ssp_buffer_size = 8>} {
+}
+
+//--- no-lowering-options.cir
module {
}
+//--- not-offload.cir
+module attributes {cir.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = false, cuda_is_device = false, hip = false, hlsl = false, gpu_rdc = false, openmp = false, openmp_is_target_device = false, clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = false, no_trapping_math = true, ssp_buffer_size = 8>} {
+}
+
//--- invalid.cir
module {
diff --git a/clang/tools/cir-offload-merge/CMakeLists.txt b/clang/tools/cir-offload-merge/CMakeLists.txt
index 8a7af9e4e51550..e338fb2456e0a0 100644
--- a/clang/tools/cir-offload-merge/CMakeLists.txt
+++ b/clang/tools/cir-offload-merge/CMakeLists.txt
@@ -5,12 +5,6 @@ add_clang_tool(cir-offload-merge
cir-offload-merge.cpp
)
-clang_target_link_libraries(cir-offload-merge
- PRIVATE
- clangBasic
- clangDriver
-)
-
target_link_libraries(cir-offload-merge
PRIVATE
CIRRegisterAllDialects
diff --git a/clang/tools/cir-offload-merge/cir-offload-merge.cpp b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
index ba35a6499ca809..769708785358da 100644
--- a/clang/tools/cir-offload-merge/cir-offload-merge.cpp
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -24,10 +24,8 @@
#include "mlir/Support/FileUtilities.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/InitAllDialects.h"
-#include "clang/Driver/OffloadBundler.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/StringSet.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/ToolOutputFile.h"
@@ -48,81 +46,44 @@ static llvm::cl::list<std::string>
"times."),
llvm::cl::cat(CIROffloadMergeCategory));
-static llvm::cl::list<std::string>
- TargetNames("targets", llvm::cl::CommaSeparated,
- llvm::cl::desc("[<offload kind>-<target triple>,...]"),
- llvm::cl::cat(CIROffloadMergeCategory));
-
static llvm::cl::list<std::string>
OutputFileNames("output", llvm::cl::desc("Output CIR file."),
llvm::cl::cat(CIROffloadMergeCategory));
-namespace {
-struct InputTarget {
- std::string Input;
- bool IsHost = false;
-};
-} // namespace
-
static int reportError(const llvm::Twine &message) {
llvm::errs() << "error: " << message << '\n';
return 1;
}
-// Targets are offload bundle IDs, so they are checked with
-// clang-offload-bundler's parser (thus, legalized). checkOffloadBundleID must
-// run first, since OffloadTargetInfo asserts on a malformed ID.
-static bool
-isValidCombineTarget(llvm::StringRef target,
- const clang::OffloadBundlerConfig &bundlerConfig) {
- if (!clang::checkOffloadBundleID(target))
- return false;
-
- clang::OffloadTargetInfo offloadInfo(target, bundlerConfig);
- // The bundler has no "cuda" offload kind, but CUDA modules are combined here.
- return (offloadInfo.isOffloadKindValid() ||
- offloadInfo.OffloadKind == "cuda") &&
- offloadInfo.isTripleValid();
-}
-
-static int
-validateCombineCommandLine(llvm::SmallVectorImpl<InputTarget> &inputTargets) {
+static int validateCombineCommandLine() {
if (InputFileNames.empty())
return reportError("missing required -input");
- if (TargetNames.empty())
- return reportError("missing required -targets");
if (OutputFileNames.empty())
return reportError("missing required -output");
if (OutputFileNames.size() != 1)
return reportError("combine mode expects exactly one output");
- if (InputFileNames.size() != TargetNames.size())
- return reportError("number of input files and targets should match in "
- "combine mode");
-
- // OffloadTargetInfo requires a bundler config; the defaults are enough to
- // parse a target.
- clang::OffloadBundlerConfig bundlerConfig;
- llvm::StringSet<> seenTargets;
- unsigned numHostTargets = 0;
-
- for (auto [input, target] : llvm::zip_equal(InputFileNames, TargetNames)) {
- if (!seenTargets.insert(target).second)
- return reportError("duplicate target '" + target + "'");
- if (!isValidCombineTarget(target, bundlerConfig))
- return reportError("invalid target '" + target + "'");
-
- bool isHost = clang::OffloadTargetInfo(target, bundlerConfig).hasHostKind();
- numHostTargets += isHost;
- inputTargets.push_back({input, isHost});
- }
-
- if (numHostTargets != 1)
- return reportError("expected exactly one host target");
- if (inputTargets.size() == 1)
- return reportError("expected at least one device target");
return 0;
}
+// The host/device role of a module comes from the LangOptions that CIRGen
+// records in cir.lowering_options.
+static mlir::FailureOr<cir::OffloadKind>
+getOffloadKind(mlir::ModuleOp module, llvm::StringRef fileName) {
+ auto loweringOpts = module->getAttrOfType<cir::LoweringOptionsAttr>(
+ cir::CIRDialect::getLoweringOptionsAttrName());
+ if (!loweringOpts) {
+ reportError("'" + fileName + "' has no '" +
+ cir::CIRDialect::getLoweringOptionsAttrName() + "' attribute");
+ return mlir::failure();
+ }
+ if (!loweringOpts.getCuda()) {
+ reportError("'" + fileName + "' is not a CUDA or HIP compilation");
+ return mlir::failure();
+ }
+ return loweringOpts.getCudaIsDevice() ? cir::OffloadKind::Device
+ : cir::OffloadKind::Host;
+}
+
static void setOffloadAttrs(mlir::ModuleOp module, llvm::StringRef name,
cir::OffloadKind offloadKind) {
module.setSymName(name);
@@ -131,19 +92,29 @@ static void setOffloadAttrs(mlir::ModuleOp module, llvm::StringRef name,
}
static mlir::OwningOpRef<mlir::ModuleOp>
-combineInputs(llvm::ArrayRef<InputTarget> inputTargets,
- mlir::MLIRContext &context) {
+combineInputs(mlir::MLIRContext &context) {
mlir::OwningOpRef<mlir::ModuleOp> hostModule;
llvm::SmallVector<mlir::OwningOpRef<mlir::ModuleOp>> deviceModules;
- for (const InputTarget &inputTarget : inputTargets) {
+ for (const std::string &fileName : InputFileNames) {
mlir::OwningOpRef<mlir::ModuleOp> module =
- mlir::parseSourceFile<mlir::ModuleOp>(inputTarget.Input,
+ mlir::parseSourceFile<mlir::ModuleOp>(fileName,
mlir::ParserConfig(&context));
- if (!module)
+ if (!module) {
+ reportError("failed to parse input file");
+ return {};
+ }
+
+ mlir::FailureOr<cir::OffloadKind> offloadKind =
+ getOffloadKind(*module, fileName);
+ if (mlir::failed(offloadKind))
return {};
- if (inputTarget.IsHost) {
+ if (*offloadKind == cir::OffloadKind::Host) {
+ if (hostModule) {
+ reportError("expected exactly one host module");
+ return {};
+ }
setOffloadAttrs(*module, "host", cir::OffloadKind::Host);
hostModule = std::move(module);
continue;
@@ -155,6 +126,15 @@ combineInputs(llvm::ArrayRef<InputTarget> inputTargets,
deviceModules.push_back(std::move(module));
}
+ if (!hostModule) {
+ reportError("expected exactly one host module");
+ return {};
+ }
+ if (deviceModules.empty()) {
+ reportError("expected at least one device module");
+ return {};
+ }
+
// The container verifier expects the host module first, followed by the
// device modules.
mlir::ModuleOp container =
@@ -199,14 +179,12 @@ int main(int argc, char **argv) {
mlir::MLIRContext context(registry);
context.loadDialect<cir::CIRDialect>();
- llvm::SmallVector<InputTarget> inputTargets;
- if (int errorCode = validateCombineCommandLine(inputTargets))
+ if (int errorCode = validateCombineCommandLine())
return errorCode;
- mlir::OwningOpRef<mlir::ModuleOp> container =
- combineInputs(inputTargets, context);
+ mlir::OwningOpRef<mlir::ModuleOp> container = combineInputs(context);
if (!container)
- return reportError("failed to parse input file");
+ return 1;
if (mlir::failed(mlir::verify(*container)))
return reportError("failed to verify combined module");
>From 7b32f2ef5bb306f6bdb221d63f60749ed70b860e Mon Sep 17 00:00:00 2001
From: David Rivera <davidriverg at gmail.com>
Date: Tue, 10 Nov 2026 23:49:17 -0500
Subject: [PATCH 6/7] [CIR] Address cir-offload-merge review nits
---
clang/test/CIR/Tools/cir-offload-merge.cir | 44 ++++++--------
.../cir-offload-merge/cir-offload-merge.cpp | 60 ++++++++-----------
2 files changed, 43 insertions(+), 61 deletions(-)
diff --git a/clang/test/CIR/Tools/cir-offload-merge.cir b/clang/test/CIR/Tools/cir-offload-merge.cir
index 3ed14a696bb3b1..5ea3df469ee230 100644
--- a/clang/test/CIR/Tools/cir-offload-merge.cir
+++ b/clang/test/CIR/Tools/cir-offload-merge.cir
@@ -1,9 +1,9 @@
// RUN: rm -rf %t && split-file %s %t
// RUN: cir-offload-merge -combine \
-// RUN: -input=%t/host.cir \
-// RUN: -input=%t/device-sm80.cir \
-// RUN: -output=%t/combined.cir
+// RUN: %t/host.cir \
+// RUN: %t/device-sm80.cir \
+// RUN: -o %t/combined.cir
// RUN: FileCheck %s --check-prefix=COMBINE --input-file=%t/combined.cir
// RUN: cir-opt %t/combined.cir --verify-roundtrip -o /dev/null
@@ -16,10 +16,10 @@
// The host module goes first whatever the input order; device modules keep
// their relative order.
// RUN: cir-offload-merge -combine \
-// RUN: -input=%t/device-sm90.cir \
-// RUN: -input=%t/host.cir \
-// RUN: -input=%t/device-sm80.cir \
-// RUN: -output=%t/reordered.cir
+// RUN: %t/device-sm90.cir \
+// RUN: %t/host.cir \
+// RUN: %t/device-sm80.cir \
+// RUN: -o %t/reordered.cir
// RUN: FileCheck %s --check-prefix=REORDER --input-file=%t/reordered.cir
// REORDER: module attributes {cir.offload.container} {
@@ -30,40 +30,32 @@
// REORDER-NEXT: cir.func{{.*}} @device_fn
// RUN: cir-offload-merge -combine \
-// RUN: -input=%t/host-hip.cir \
-// RUN: -input=%t/device-gfx90a.cir \
-// RUN: -output=%t/hip.cir
-// RUN: FileCheck %s --check-prefix=HIP --input-file=%t/hip.cir
+// RUN: %t/host-hip.cir \
+// RUN: %t/device-gfx90a.cir | FileCheck %s --check-prefix=HIP
// HIP: module @host attributes {{{.*}}cir.offload.kind = #cir.offload<host>
// HIP: module @device_0 attributes {{{.*}}cir.offload.kind = #cir.offload<device>
-// RUN: not cir-offload-merge -input=%t/host.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-MODE
+// RUN: not cir-offload-merge %t/host.cir -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-MODE
// ERR-NO-MODE: error: expected -combine
-// RUN: not cir-offload-merge -combine -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-INPUT
-// ERR-NO-INPUT: error: missing required -input
+// RUN: not cir-offload-merge -combine -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-INPUT
+// ERR-NO-INPUT: error: no input files
-// RUN: not cir-offload-merge -combine -input=%t/host.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-OUTPUT
-// ERR-NO-OUTPUT: error: missing required -output
-
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/device-sm80.cir -output=%t/a.cir -output=%t/b.cir 2>&1 | FileCheck %s --check-prefix=ERR-MULTI-OUTPUT
-// ERR-MULTI-OUTPUT: error: combine mode expects exactly one output
-
-// RUN: not cir-offload-merge -combine -input=%t/device-sm80.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-HOSTS
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/host-hip.cir -input=%t/device-sm80.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-HOSTS
+// RUN: not cir-offload-merge -combine %t/device-sm80.cir -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-HOSTS
+// RUN: not cir-offload-merge -combine %t/host.cir %t/host-hip.cir %t/device-sm80.cir -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-HOSTS
// ERR-HOSTS: error: expected exactly one host module
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-DEVICE
+// RUN: not cir-offload-merge -combine %t/host.cir -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-DEVICE
// ERR-NO-DEVICE: error: expected at least one device module
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/no-lowering-options.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-OPTS
+// RUN: not cir-offload-merge -combine %t/host.cir %t/no-lowering-options.cir -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NO-OPTS
// ERR-NO-OPTS: error: '{{.*}}no-lowering-options.cir' has no 'cir.lowering_options' attribute
-// RUN: not cir-offload-merge -combine -input=%t/host.cir -input=%t/not-offload.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NOT-OFFLOAD
+// RUN: not cir-offload-merge -combine %t/host.cir %t/not-offload.cir -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-NOT-OFFLOAD
// ERR-NOT-OFFLOAD: error: '{{.*}}not-offload.cir' is not a CUDA or HIP compilation
-// RUN: not cir-offload-merge -combine -input=%t/invalid.cir -input=%t/device-sm80.cir -output=%t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-PARSE
+// RUN: not cir-offload-merge -combine %t/invalid.cir %t/device-sm80.cir -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-PARSE
// ERR-PARSE: error: failed to parse input file
//--- host.cir
diff --git a/clang/tools/cir-offload-merge/cir-offload-merge.cpp b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
index 769708785358da..1e55b6ae56dca5 100644
--- a/clang/tools/cir-offload-merge/cir-offload-merge.cpp
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -41,35 +41,24 @@ static llvm::cl::opt<bool> Combine("combine",
llvm::cl::cat(CIROffloadMergeCategory));
static llvm::cl::list<std::string>
- InputFileNames("input",
- llvm::cl::desc("Input CIR file. Can be specified multiple "
- "times."),
+ InputFileNames(llvm::cl::Positional, llvm::cl::desc("<input files>"),
llvm::cl::cat(CIROffloadMergeCategory));
-static llvm::cl::list<std::string>
- OutputFileNames("output", llvm::cl::desc("Output CIR file."),
- llvm::cl::cat(CIROffloadMergeCategory));
+static llvm::cl::opt<std::string>
+ OutputFileName("o", llvm::cl::desc("Output CIR file"),
+ llvm::cl::value_desc("filename"), llvm::cl::init("-"),
+ llvm::cl::cat(CIROffloadMergeCategory));
static int reportError(const llvm::Twine &message) {
llvm::errs() << "error: " << message << '\n';
return 1;
}
-static int validateCombineCommandLine() {
- if (InputFileNames.empty())
- return reportError("missing required -input");
- if (OutputFileNames.empty())
- return reportError("missing required -output");
- if (OutputFileNames.size() != 1)
- return reportError("combine mode expects exactly one output");
- return 0;
-}
-
// The host/device role of a module comes from the LangOptions that CIRGen
// records in cir.lowering_options.
static mlir::FailureOr<cir::OffloadKind>
-getOffloadKind(mlir::ModuleOp module, llvm::StringRef fileName) {
- auto loweringOpts = module->getAttrOfType<cir::LoweringOptionsAttr>(
+getOffloadKind(mlir::ModuleOp moduleOp, llvm::StringRef fileName) {
+ auto loweringOpts = moduleOp->getAttrOfType<cir::LoweringOptionsAttr>(
cir::CIRDialect::getLoweringOptionsAttrName());
if (!loweringOpts) {
reportError("'" + fileName + "' has no '" +
@@ -84,11 +73,12 @@ getOffloadKind(mlir::ModuleOp module, llvm::StringRef fileName) {
: cir::OffloadKind::Host;
}
-static void setOffloadAttrs(mlir::ModuleOp module, llvm::StringRef name,
+static void setOffloadAttrs(mlir::ModuleOp moduleOp, llvm::StringRef name,
cir::OffloadKind offloadKind) {
- module.setSymName(name);
- module->setAttr(cir::CIRDialect::getOffloadKindAttrName(),
- cir::OffloadKindAttr::get(module.getContext(), offloadKind));
+ moduleOp.setSymName(name);
+ moduleOp->setAttr(
+ cir::CIRDialect::getOffloadKindAttrName(),
+ cir::OffloadKindAttr::get(moduleOp.getContext(), offloadKind));
}
static mlir::OwningOpRef<mlir::ModuleOp>
@@ -97,16 +87,16 @@ combineInputs(mlir::MLIRContext &context) {
llvm::SmallVector<mlir::OwningOpRef<mlir::ModuleOp>> deviceModules;
for (const std::string &fileName : InputFileNames) {
- mlir::OwningOpRef<mlir::ModuleOp> module =
+ mlir::OwningOpRef<mlir::ModuleOp> moduleOp =
mlir::parseSourceFile<mlir::ModuleOp>(fileName,
mlir::ParserConfig(&context));
- if (!module) {
+ if (!moduleOp) {
reportError("failed to parse input file");
return {};
}
mlir::FailureOr<cir::OffloadKind> offloadKind =
- getOffloadKind(*module, fileName);
+ getOffloadKind(*moduleOp, fileName);
if (mlir::failed(offloadKind))
return {};
@@ -115,15 +105,15 @@ combineInputs(mlir::MLIRContext &context) {
reportError("expected exactly one host module");
return {};
}
- setOffloadAttrs(*module, "host", cir::OffloadKind::Host);
- hostModule = std::move(module);
+ setOffloadAttrs(*moduleOp, "host", cir::OffloadKind::Host);
+ hostModule = std::move(moduleOp);
continue;
}
std::string deviceName =
("device_" + llvm::Twine(deviceModules.size())).str();
- setOffloadAttrs(*module, deviceName, cir::OffloadKind::Device);
- deviceModules.push_back(std::move(module));
+ setOffloadAttrs(*moduleOp, deviceName, cir::OffloadKind::Device);
+ deviceModules.push_back(std::move(moduleOp));
}
if (!hostModule) {
@@ -147,10 +137,10 @@ combineInputs(mlir::MLIRContext &context) {
return container;
}
-static int writeModuleToOutput(mlir::ModuleOp module,
+static int writeModuleToOutput(mlir::ModuleOp moduleOp,
llvm::StringRef outputFileName) {
// Type aliases are only printed for an operation without a parent.
- assert(!module->getParentOp() && "expected a top-level module");
+ assert(!moduleOp->getParentOp() && "expected a top-level module");
std::string errorMessage;
std::unique_ptr<llvm::ToolOutputFile> outputFile =
mlir::openOutputFile(outputFileName, &errorMessage);
@@ -158,7 +148,7 @@ static int writeModuleToOutput(mlir::ModuleOp module,
return reportError(errorMessage);
// TODO: Support bytecode output.
- module->print(outputFile->os());
+ moduleOp->print(outputFile->os());
outputFile->os() << '\n';
outputFile->keep();
return 0;
@@ -179,8 +169,8 @@ int main(int argc, char **argv) {
mlir::MLIRContext context(registry);
context.loadDialect<cir::CIRDialect>();
- if (int errorCode = validateCombineCommandLine())
- return errorCode;
+ if (InputFileNames.empty())
+ return reportError("no input files");
mlir::OwningOpRef<mlir::ModuleOp> container = combineInputs(context);
if (!container)
@@ -188,5 +178,5 @@ int main(int argc, char **argv) {
if (mlir::failed(mlir::verify(*container)))
return reportError("failed to verify combined module");
- return writeModuleToOutput(*container, OutputFileNames.front());
+ return writeModuleToOutput(*container, OutputFileName);
}
>From a6d61fafb560da9344dfc8f57be2717adf43f776 Mon Sep 17 00:00:00 2001
From: David Rivera <davidriverg at gmail.com>
Date: Tue, 10 Nov 2026 23:52:27 -0500
Subject: [PATCH 7/7] [CIR] Remove unused include in cir-offload-merge
---
clang/tools/cir-offload-merge/cir-offload-merge.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/tools/cir-offload-merge/cir-offload-merge.cpp b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
index 1e55b6ae56dca5..390dc70e2b2952 100644
--- a/clang/tools/cir-offload-merge/cir-offload-merge.cpp
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -24,7 +24,6 @@
#include "mlir/Support/FileUtilities.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/InitAllDialects.h"
-#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
More information about the cfe-commits
mailing list