clang 23.0.0git
LowerModule.cpp
Go to the documentation of this file.
1//===--- LowerModule.cpp - Lower CIR Module to a Target -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file partially mimics clang/lib/CodeGen/CodeGenModule.cpp. The queries
10// are adapted to operate on the CIR dialect, however.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LowerModule.h"
15#include "CIRCXXABI.h"
16#include "mlir/IR/BuiltinAttributes.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/FileSystem.h"
23
24namespace cir {
25
26static std::unique_ptr<CIRCXXABI> createCXXABI(LowerModule &lm) {
27 switch (lm.getCXXABIKind()) {
28 case clang::TargetCXXABI::AppleARM64:
29 case clang::TargetCXXABI::Fuchsia:
30 case clang::TargetCXXABI::GenericAArch64:
31 case clang::TargetCXXABI::GenericARM:
32 case clang::TargetCXXABI::iOS:
33 case clang::TargetCXXABI::WatchOS:
34 case clang::TargetCXXABI::GenericMIPS:
35 case clang::TargetCXXABI::GenericItanium:
36 case clang::TargetCXXABI::WebAssembly:
37 case clang::TargetCXXABI::XL:
38 return createItaniumCXXABI(lm);
39 case clang::TargetCXXABI::Microsoft:
40 llvm_unreachable("Windows ABI NYI");
41 }
42
43 llvm_unreachable("invalid C++ ABI kind");
44}
45
46static std::unique_ptr<TargetLoweringInfo>
49 return std::make_unique<TargetLoweringInfo>();
50}
51
53 clang::CodeGenOptions codeGenOpts,
54 mlir::ModuleOp &module,
55 std::unique_ptr<clang::TargetInfo> target)
56 : module(module), target(std::move(target)), abi(createCXXABI(*this)) {}
57
59 if (!targetLoweringInfo)
60 targetLoweringInfo = createTargetLoweringInfo(*this);
61 return *targetLoweringInfo;
62}
63
64// TODO: not to create it every time
65std::unique_ptr<LowerModule> createLowerModule(mlir::ModuleOp module) {
66 // If the triple is not present, e.g. CIR modules parsed from text, we
67 // cannot init LowerModule properly.
69 if (!module->hasAttr(cir::CIRDialect::getTripleAttrName()))
70 return nullptr;
71
72 // Fetch target information.
73 llvm::Triple triple(mlir::cast<mlir::StringAttr>(
74 module->getAttr(cir::CIRDialect::getTripleAttrName()))
75 .getValue());
76 clang::TargetOptions targetOptions;
77 targetOptions.Triple = triple.str();
78 auto targetInfo = clang::targets::AllocateTarget(triple, targetOptions);
79
80 // FIXME(cir): This just uses the default language options. We need to account
81 // for custom options.
82 // Create context.
84 clang::LangOptions langOpts;
85
86 // FIXME(cir): This just uses the default code generation options. We need to
87 // account for custom options.
89 clang::CodeGenOptions codeGenOpts;
90
91 if (auto optInfo = mlir::cast_if_present<cir::OptInfoAttr>(
92 module->getAttr(cir::CIRDialect::getOptInfoAttrName()))) {
93 codeGenOpts.OptimizationLevel = optInfo.getLevel();
94 codeGenOpts.OptimizeSize = optInfo.getSize();
95 }
96
97 return std::make_unique<LowerModule>(std::move(langOpts),
98 std::move(codeGenOpts), module,
99 std::move(targetInfo));
100}
101
102} // namespace cir
Defines the clang::LangOptions interface.
Defines the clang::TargetOptions class.
LowerModule(clang::LangOptions langOpts, clang::CodeGenOptions codeGenOpts, mlir::ModuleOp &module, std::unique_ptr< clang::TargetInfo > target)
clang::TargetCXXABI::Kind getCXXABIKind() const
Definition LowerModule.h:41
const TargetLoweringInfo & getTargetLoweringInfo()
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Options for controlling the target.
std::string Triple
The name of the target triple to compile for.
Defines the clang::TargetInfo interface.
static std::unique_ptr< TargetLoweringInfo > createTargetLoweringInfo(LowerModule &lm)
std::unique_ptr< CIRCXXABI > createItaniumCXXABI(LowerModule &lm)
Creates an Itanium-family ABI.
static std::unique_ptr< CIRCXXABI > createCXXABI(LowerModule &lm)
std::unique_ptr< LowerModule > createLowerModule(mlir::ModuleOp module)
std::unique_ptr< clang::TargetInfo > AllocateTarget(const llvm::Triple &Triple, const clang::TargetOptions &Opts)
Definition Targets.cpp:111
static bool lowerModuleLangOpts()
static bool lowerModuleCodeGenOpts()
static bool makeTripleAlwaysPresent()
static bool targetLoweringInfo()