Author: David Rivera Date: 2026-11-20T19:12:20-05:00 New Revision: 4b78a4221e0b1d3b0c9a85445e50b6cf75a77434
URL: https://github.com/llvm/llvm-project/commit/4b78a4221e0b1d3b0c9a85445e50b6cf75a77434 DIFF: https://github.com/llvm/llvm-project/commit/4b78a4221e0b1d3b0c9a85445e50b6cf75a77434.diff LOG: [CIR] Introduce `cir-offload-merge` tool with a combine mode (#240322) 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 host.cir gfx90a-xnack-on.cir gfx90a-xnack-off.cir -o combined.cir ``` IR: ```mlir module attributes {cir.offload.container} { module @host attributes { cir.lowering_options = #cir.lowering_options<..., cuda_is_device = false, hip = true, ...>, cir.offload.kind = #cir.offload<host>, ...} {...} module @device_0 attributes { cir.amdgpu_xnack = 1 : i32, cir.lowering_options = #cir.lowering_options<..., cuda_is_device = true, hip = true, ...>, cir.offload.kind = #cir.offload<device>, cir.triple = "amdgpu9.0a-amd-amdhsa", ...} {...} module @device_1 attributes { cir.amdgpu_xnack = 0 : i32, cir.lowering_options = #cir.lowering_options<..., cuda_is_device = true, hip = true, ...>, cir.offload.kind = #cir.offload<device>, cir.triple = "amdgpu9.0a-amd-amdhsa", ...} {...} } ``` For this patch the tool: * reads each input's role from `cir.lowering_options`, which CIRGen emits on every module: `cuda_is_device` marks a device module (HIP sets `cuda` as well). Inputs that are not CUDA or HIP compilations, or that lack the attribute, are rejected. Exactly one host and at least one device module are required; * tags each nested module with `cir.offload.kind` and names it `@host` or `@device_<N>`, numbered in input order; * puts the host module first, as the container verifier requires, and verifies the container before writing it. Inputs are validated through the attributes CIRGen already records on each module. For example, `cir.lowering_options` tells whether a module comes from a host or a device compilation. As of now, the tool assumes all inputs come from the same driver invocation, so it does not yet check that the modules are compatible with each other (types, globals, kernel boundaries). That is left for a follow-up. Not in this patch: the split mode and the cross-boundary (host-device) passes that will 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 Added: clang/test/CIR/Tools/cir-offload-merge.cir clang/tools/cir-offload-merge/CMakeLists.txt clang/tools/cir-offload-merge/cir-offload-merge.cpp Modified: clang/test/CMakeLists.txt clang/test/lit.cfg.py clang/tools/CMakeLists.txt Removed: ################################################################################ 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..75679b8f7f4247 --- /dev/null +++ b/clang/test/CIR/Tools/cir-offload-merge.cir @@ -0,0 +1,106 @@ +// RUN: rm -rf %t && split-file %s %t + +// RUN: cir-offload-merge -combine \ +// 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 + +// COMBINE: module attributes {cir.offload.container} { +// COMBINE-NEXT: module @host attributes {cir.lowering_options = #cir.lowering_options<{{.*}}cuda_is_device = false{{.*}}>, 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.lowering_options = #cir.lowering_options<{{.*}}cuda_is_device = true{{.*}}>, 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: %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} { +// REORDER-NEXT: module @host +// 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: cir.func{{.*}} @device_fn + +// RUN: cir-offload-merge -combine \ +// RUN: %t/host-hip.cir \ +// RUN: %t/device-gfx90a.cir | FileCheck %s --check-prefix=HIP + +// HIP: module @host attributes {cir.lowering_options = #cir.lowering_options<{{.*}}cuda_is_device = false, hip = true{{.*}}>, cir.offload.kind = #cir.offload<host> +// HIP: module @device_0 attributes {{{.*}}cir.lowering_options = #cir.lowering_options<{{.*}}cuda_is_device = true, hip = true{{.*}}>, cir.offload.kind = #cir.offload<device> + +// 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 -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 %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.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 %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 %t/host.cir %t/device-gfx90a.cir -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-MIXED +// ERR-MIXED: device-gfx90a.cir{{.*}}: error: expected all inputs to be CUDA or all to be HIP + +// 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: no-lowering-options.cir{{.*}}: error: expected 'cir.lowering_options' attribute + +// 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: not-offload.cir{{.*}}: error: expected a CUDA or HIP compilation + +// RUN: not cir-offload-merge -combine %t/host.cir %t/device-sycl.cir -o %t/out.cir 2>&1 | FileCheck %s --check-prefix=ERR-SYCL +// ERR-SYCL: device-sycl.cir{{.*}}: error: SYCL compilations are not supported yet + +// 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 '{{.*}}invalid.cir' + +//--- host.cir +module attributes {cir.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = true, cuda_is_device = false, hip = false, hlsl = false, sycl_is_device = 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.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = true, cuda_is_device = true, hip = false, hlsl = false, sycl_is_device = 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 attributes {cir.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = true, cuda_is_device = true, hip = false, hlsl = false, sycl_is_device = 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, sycl_is_device = 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, sycl_is_device = 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, sycl_is_device = 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-sycl.cir +module attributes {cir.lowering_options = #cir.lowering_options<exceptions = false, threadsafe_statics = true, cuda = false, cuda_is_device = false, hip = false, hlsl = false, sycl_is_device = true, 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/test/CMakeLists.txt b/clang/test/CMakeLists.txt index 466e559272b15c..7fff733d8ee7b2 100644 --- a/clang/test/CMakeLists.txt +++ b/clang/test/CMakeLists.txt @@ -117,6 +117,7 @@ list(APPEND CLANG_TEST_DEPS if(CLANG_ENABLE_CIR) list(APPEND CLANG_TEST_DEPS + cir-offload-merge cir-opt cir-translate llvm-strings @@ -277,6 +278,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 aaef462872c374..9359e902cc8696 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..3c78b25b6b9c59 --- /dev/null +++ b/clang/tools/cir-offload-merge/CMakeLists.txt @@ -0,0 +1,15 @@ +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 +) + +target_link_libraries(cir-offload-merge + PRIVATE + CIRRegisterAllDialects + MLIRCIR + MLIRIR + 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..cde2cd28345fe9 --- /dev/null +++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp @@ -0,0 +1,197 @@ +//===-- 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/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 "llvm/ADT/SmallVector.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/ToolOutputFile.h" + +#include <optional> +#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(llvm::cl::Positional, llvm::cl::desc("<input files>"), + 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)); + +// The host/device role and language of a module come from the LangOptions +// that CIRGen records in cir.lowering_options. +static mlir::FailureOr<cir::LoweringOptionsAttr> +getLoweringOptions(mlir::ModuleOp moduleOp) { + auto loweringOpts = moduleOp->getAttrOfType<cir::LoweringOptionsAttr>( + cir::CIRDialect::getLoweringOptionsAttrName()); + if (!loweringOpts) { + mlir::emitError(moduleOp.getLoc()) + << "expected '" << cir::CIRDialect::getLoweringOptionsAttrName() + << "' attribute"; + return mlir::failure(); + } + if (loweringOpts.getSyclIsDevice()) { + mlir::emitError(moduleOp.getLoc(), + "SYCL compilations are not supported yet"); + return mlir::failure(); + } + if (!loweringOpts.getCuda()) { + mlir::emitError(moduleOp.getLoc(), "expected a CUDA or HIP compilation"); + return mlir::failure(); + } + return loweringOpts; +} + +static void setOffloadAttrs(mlir::ModuleOp moduleOp, llvm::StringRef name, + cir::OffloadKind offloadKind) { + moduleOp.setSymName(name); + moduleOp->setAttr( + cir::CIRDialect::getOffloadKindAttrName(), + cir::OffloadKindAttr::get(moduleOp.getContext(), offloadKind)); +} + +static mlir::OwningOpRef<mlir::ModuleOp> +combineInputs(mlir::MLIRContext &context) { + mlir::Location unknownLoc = mlir::UnknownLoc::get(&context); + mlir::OwningOpRef<mlir::ModuleOp> hostModule; + llvm::SmallVector<mlir::OwningOpRef<mlir::ModuleOp>> deviceModules; + std::optional<bool> isHIP; + + for (const std::string &fileName : InputFileNames) { + mlir::OwningOpRef<mlir::ModuleOp> moduleOp = + mlir::parseSourceFile<mlir::ModuleOp>(fileName, + mlir::ParserConfig(&context)); + if (!moduleOp) { + mlir::emitError(unknownLoc) + << "failed to parse input file '" << fileName << "'"; + return {}; + } + + mlir::FailureOr<cir::LoweringOptionsAttr> loweringOpts = + getLoweringOptions(*moduleOp); + if (mlir::failed(loweringOpts)) + return {}; + + // A CUDA host cannot be combined with HIP devices, or the other way round. + if (!isHIP) + isHIP = loweringOpts->getHip(); + if (loweringOpts->getHip() != *isHIP) { + mlir::emitError(moduleOp->getLoc(), + "expected all inputs to be CUDA or all to be HIP"); + return {}; + } + + if (!loweringOpts->getCudaIsDevice()) { + if (hostModule) { + mlir::emitError(moduleOp->getLoc(), "expected exactly one host module"); + return {}; + } + setOffloadAttrs(*moduleOp, "host", cir::OffloadKind::Host); + hostModule = std::move(moduleOp); + continue; + } + + std::string deviceName = + ("device_" + llvm::Twine(deviceModules.size())).str(); + setOffloadAttrs(*moduleOp, deviceName, cir::OffloadKind::Device); + deviceModules.push_back(std::move(moduleOp)); + } + + if (!hostModule) { + mlir::emitError(unknownLoc, "expected exactly one host module"); + return {}; + } + if (deviceModules.empty()) { + mlir::emitError(unknownLoc, "expected at least one device module"); + return {}; + } + + // The container verifier expects the host module first, followed by the + // device modules. + mlir::ModuleOp container = mlir::ModuleOp::create(unknownLoc); + 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; +} + +static int writeModuleToOutput(mlir::ModuleOp moduleOp, + llvm::StringRef outputFileName) { + // Type aliases are only printed for an operation without a parent. + assert(!moduleOp->getParentOp() && "expected a top-level module"); + std::string errorMessage; + std::unique_ptr<llvm::ToolOutputFile> outputFile = + mlir::openOutputFile(outputFileName, &errorMessage); + if (!outputFile) { + mlir::emitError(moduleOp.getLoc(), errorMessage); + return 1; + } + + // TODO: Support bytecode output. + moduleOp->print(outputFile->os()); + outputFile->os() << '\n'; + outputFile->keep(); + return 0; +} + +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"); + + mlir::DialectRegistry registry; + cir::registerAllDialects(registry); + mlir::MLIRContext context(registry); + context.loadDialect<cir::CIRDialect>(); + mlir::Location unknownLoc = mlir::UnknownLoc::get(&context); + + if (!Combine) { + mlir::emitError(unknownLoc, "expected -combine"); + return 1; + } + if (InputFileNames.empty()) { + mlir::emitError(unknownLoc, "no input files"); + return 1; + } + + mlir::OwningOpRef<mlir::ModuleOp> container = combineInputs(context); + if (!container) + return 1; + if (mlir::failed(mlir::verify(*container))) { + mlir::emitError(container->getLoc(), "failed to verify combined module"); + return 1; + } + + return writeModuleToOutput(*container, OutputFileName); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
