================ @@ -0,0 +1,178 @@ +//===-- 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 "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(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)); + +static int reportError(const llvm::Twine &message) { + llvm::errs() << "error: " << message << '\n'; + return 1; +} + +// 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 moduleOp, llvm::StringRef fileName) { + auto loweringOpts = moduleOp->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 moduleOp, llvm::StringRef name, + cir::OffloadKind offloadKind) { + moduleOp.setSymName(name); ---------------- RiverDave wrote:
One limitation worth considering: Codegen names each module after its source file, e.g. compiling k.hip gives `module @"/path/to/k.hip"`, and that's the same for the host and every device compilation of that file. Once they're nested, the container is a symbol table for its nested modules, so those names collide (you get a diagnostic like "redefinition of symbol"). We originally renamed each module mostly for readability and for differentiating each TU (But I guess that is not really valid anymore, as we have an offload.kind attribute). Anyways, a solution that came to mind is that perhaps we could preserve the original filename under an attribute like `cir.offload.source_name` when doing combine; when we split, that's the symbol name we'd give back to the module and the attribute is discarded afterwards. I hope I explained that well... Thoughts? https://github.com/llvm/llvm-project/pull/240322 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
