[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 21:10:54 PST 2026
================
@@ -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() ||
----------------
RiverDave wrote:
I talked with @koparasy about this. When I first wrote the tool downstream, CIR modules didn’t record whether they came from a host or a device compilation, so I reused clang-offload-bundler’s target parsing, since that’s the tool I modeled this on. As you noticed, the bundler was never meant for CUDA, so pulling it in as a dependency just to validate targets didn’t make much sense.
Until not ver long ago, CIRGen now records that on every module in `cir.lowering_options` (`cuda`, `cuda_is_device`), next to `cir.triple`. So the tool no longer takes -targets: it reads each input’s host/device role from the module itself and rejects anything that isn’t a CUDA or HIP compilation. That removes the bundler dependency entirely. (I'm pretty much happy with the result. I think the code's a lot easier to read :D and the tool much less cumbersome to use)
https://github.com/llvm/llvm-project/pull/240322
More information about the cfe-commits
mailing list