llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clangir

Author: David Rivera (RiverDave)

<details>
<summary>Changes</summary>

Add `cir-offload-merge`, a standalone tool that nests a host CIR module and its 
device CIR modules in an offload container (the `cir.offload.container` module 
added in #<!-- -->206576). This is the first piece of the Host-Device CIR 
Combine Pipeline: passes that need both sides of an offload compilation in one 
module will run on this container, and a split mode will write the modules back 
out per target.

invocation:
```bash
cir-offload-merge -combine \
  -input=host.cir -input=gfx90a-xnack-on.cir -input=gfx90a-xnack-off.cir \
  
-targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a:xnack+,hip-amdgcn-amd-amdhsa--gfx90a:xnack-
 \
  -output=combined.cir
```
IR:
```mlir
module attributes {cir.offload.container} {
  module @<!-- -->host attributes {
      cir.offload.bundle_id = "host-x86_64-unknown-linux-gnu",
      cir.offload.kind = #cir.offload&lt;host&gt;, ...} {...}
  module @<!-- -->device_0 attributes {
      cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack+",
      cir.offload.kind = #cir.offload&lt;device&gt;, ...} {...}
  module @<!-- -->device_1 attributes {
      cir.offload.bundle_id = "hip-amdgcn-amd-amdhsa--gfx90a:xnack-",
      cir.offload.kind = #cir.offload&lt;device&gt;, ...} {...}
}
```
For this patch The tool concretely does:

* pairs each input with an offload bundle ID, `&lt;offload kind&gt;-&lt;target 
triple&gt;[-&lt;target ID&gt;]`, checked with `clang-offload-bundler`'s parser. 
Exactly one host and at least one device target are required, and duplicate 
targets are rejected;
* tags each nested module with `cir.offload.kind` and `cir.offload.bundle_id`, 
and names it `@<!-- -->host` or `@<!-- -->device_&lt;N&gt;`, numbered in 
-targets order;
* puts the host module first, as the container verifier requires, and verifies 
the container before writing it;
* runs no passes.

To record which compilation each nested module came from, this patch also adds 
`cir.offload.bundle_id`, a string attribute holding the module's bundle ID. 
`cir.offload.kind` only says host or device, and the target triple is not 
enough either: a multi-arch HIP build produces device modules that share a 
triple and only differ in their target ID, as above. Something that will be 
coming in a later PR in the split mode will use the bundle ID to route each 
module to its output. The verifier checks that the attribute is a non-empty 
string on a `builtin.module` and that bundle IDs are unique within a container. 
It is optional, so existing containers stay valid.

Not in this patch: the split mode and the passes that run on the container.

This is part of the Host-Device CIR Combine Pipeline work discussed on 
Discourse: 
https://discourse.llvm.org/t/gsoc-2026-host-device-cir-combine-pipeline-project-idea-discussion/89623

---

Patch is 24.46 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/240322.diff


11 Files Affected:

- (modified) clang/include/clang/CIR/Dialect/IR/CIRAttrs.td (+27-3) 
- (modified) clang/include/clang/CIR/Dialect/IR/CIRDialect.td (+1) 
- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+23) 
- (modified) clang/test/CIR/IR/invalid-offload-container.cir (+41) 
- (modified) clang/test/CIR/IR/offload-container.cir (+16) 
- (added) clang/test/CIR/Tools/cir-offload-merge.cir (+101) 
- (modified) clang/test/CMakeLists.txt (+2) 
- (modified) clang/test/lit.cfg.py (+1) 
- (modified) clang/tools/CMakeLists.txt (+1) 
- (added) clang/tools/cir-offload-merge/CMakeLists.txt (+23) 
- (added) clang/tools/cir-offload-merge/cir-offload-merge.cpp (+220) 


``````````diff
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@+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@+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..4c025932e91cca
--- /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_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
+// 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_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 \
+// 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_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
+
+// 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..942606fb5cf611
--- /dev/null
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -0,0 +1,220 @@
+//===-- 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/StringSet.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include <string>
+
+static llvm::cl::OptionCategory
+    CIROffloadMergeCategory("cir-offload-merge options");
+
+static llvm::cl::opt<bool> Combine("combine",
+                                   llvm::cl::desc("Combine CIR inputs"),
+                                   llvm::cl::cat(CIROffloadMergeCategory));
+
+static llvm::cl::list<std::string>
+    InputFileNames("input",
+                   llvm::cl::desc("Input CIR file. Can be specified multiple "
+                                  "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;
+  std::string Target;
+  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) {
+  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...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/240322
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to