[clang] [CIR] Add cir-translate and cir-lsp-server tools (PR #131181)
Morris Hafner via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 13 11:19:44 PDT 2025
================
@@ -0,0 +1,166 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Converts CIR directly to LLVM IR, similar to mlir-translate or LLVM llc.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/DLTI/DLTI.h"
+#include "mlir/Dialect/Func/IR/FuncOps.h"
+#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/MLIRContext.h"
+#include "mlir/InitAllTranslations.h"
+#include "mlir/Support/LogicalResult.h"
+#include "mlir/Target/LLVMIR/Dialect/All.h"
+#include "mlir/Target/LLVMIR/Import.h"
+#include "mlir/Tools/mlir-translate/MlirTranslateMain.h"
+#include "mlir/Tools/mlir-translate/Translation.h"
+
+#include "llvm/IR/Module.h"
+#include "llvm/TargetParser/Host.h"
+
+#include "clang/Basic/TargetInfo.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "clang/CIR/Dialect/Passes.h"
+#include "clang/CIR/LowerToLLVM.h"
+#include "clang/CIR/MissingFeatures.h"
+
+namespace cir {
+namespace direct {
+extern void registerCIRDialectTranslation(mlir::DialectRegistry ®istry);
+} // namespace direct
+
+namespace {
+
+/// The goal of this option is to ensure that the triple and data layout specs
+/// are always available in the ClangIR module. With this requirement met, the
+/// behavior of this option is designed to be as intuitive as possible, as shown
+/// in the table below:
+///
+/// +--------+--------+-------------+-----------------+-----------------------+
+/// | Option | Triple | Data Layout | Behavior Triple | Behavior Data Layout |
+/// +========+========+=============+=================+=======================+
+/// | T | T | T | Overwrite | Derive from triple |
+/// | T | T | F | Overwrite | Derive from triple |
+/// | T | F | T | Overwrite | Derive from triple |
+/// | T | F | F | Overwrite | Derive from triple |
+/// | F | T | T | | |
+/// | F | T | F | | Derive from triple |
+/// | F | F | T | Set default | Derive from triple |
+/// | F | F | F | Set default | Derive from triple |
+/// +--------+--------+-------------+-----------------+-----------------------+
+llvm::cl::opt<std::string>
+ targetTripleOption("target",
+ llvm::cl::desc("Specify a default target triple when "
+ "it's not available in the module"),
+ llvm::cl::init(""));
+
+std::string prepareCIRModuleTriple(mlir::ModuleOp mod) {
+ std::string triple = targetTripleOption;
+
+ // Treat "" as the default target machine.
+ if (triple.empty()) {
+ triple = llvm::sys::getDefaultTargetTriple();
----------------
mmha wrote:
This differs from the incubator which defaults to `"x86_64-unknown-linux-gnu"`.
https://github.com/llvm/llvm-project/pull/131181
More information about the cfe-commits
mailing list