clang 24.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 return createMicrosoftCXXABI(lm);
41 }
42
43 llvm_unreachable("invalid C++ ABI kind");
44}
45
47 clang::CodeGenOptions codeGenOpts,
48 mlir::ModuleOp &module,
49 std::unique_ptr<clang::TargetInfo> target)
50 : module(module), langOpts(std::move(langOpts)), target(std::move(target)) {
51 if (this->target)
52 abi = createCXXABI(*this);
53}
54
56 if (!targetLoweringInfo)
57 targetLoweringInfo = createTargetLoweringInfo(getTarget().getTriple());
58 return *targetLoweringInfo;
59}
60
61// TODO: not to create it every time
62std::unique_ptr<LowerModule> createLowerModule(mlir::ModuleOp module) {
63 // If the triple is not present, e.g. CIR modules parsed from text, or clang
64 // has no TargetInfo support for it yet, LowerModule currently gets created
65 // with null target and abi. If triple becomes a requirement, we should not
66 // allow LowerModules to be created with null target and abi.
68
69 // Fetch target information.
70 std::unique_ptr<clang::TargetInfo> targetInfo;
71 if (std::optional<llvm::Triple> triple = cir::getTripleFromModule(module)) {
72 clang::TargetOptions targetOptions;
73 targetOptions.Triple = triple->str();
74 targetInfo = clang::targets::AllocateTarget(*triple, targetOptions);
75 }
76
77 // Populate the lowering-relevant LangOptions from the module's
78 // #cir.lowering_options attribute so a reloaded .cir lowers the same
79 // way it was compiled, without a live clang::LangOptions. When the attribute
80 // is absent (e.g. hand-written CIR) the defaults are kept;
81 clang::LangOptions langOpts;
82 if (auto loweringLangOpts =
83 mlir::dyn_cast_if_present<cir::LoweringOptionsAttr>(
84 module->getAttr(cir::CIRDialect::getLoweringOptionsAttrName()))) {
85 langOpts.Exceptions = loweringLangOpts.getExceptions();
86 langOpts.ThreadsafeStatics = loweringLangOpts.getThreadsafeStatics();
87 langOpts.CUDA = loweringLangOpts.getCuda();
88 langOpts.CUDAIsDevice = loweringLangOpts.getCudaIsDevice();
89 langOpts.HIP = loweringLangOpts.getHip();
90 langOpts.GPURelocatableDeviceCode = loweringLangOpts.getGpuRdc();
91 langOpts.OpenMP = loweringLangOpts.getOpenmp();
92 langOpts.OpenMPIsTargetDevice = loweringLangOpts.getOpenmpIsTargetDevice();
93 langOpts.SYCLIsDevice = loweringLangOpts.getSyclIsDevice();
94 langOpts.setClangABICompat(static_cast<clang::LangOptions::ClangABI>(
95 loweringLangOpts.getClangAbiCompat()));
96 langOpts.LongDoubleSize = loweringLangOpts.getLongDoubleSize();
97 langOpts.RelativeCXXABIVTables =
98 loweringLangOpts.getRelativeCxxAbiVtables();
99 }
100 // Some langopts are still not lowered.
102
103 if (auto cxxABI = module->getAttrOfType<mlir::StringAttr>(
104 cir::CIRDialect::getCXXABIAttrName()))
105 langOpts.CXXABI = clang::TargetCXXABI::getKind(cxxABI.getValue());
106
107 // FIXME(cir): This just uses the default code generation options. We need to
108 // account for custom options.
110 clang::CodeGenOptions codeGenOpts;
111
112 if (auto optInfo = mlir::cast_if_present<cir::OptInfoAttr>(
113 module->getAttr(cir::CIRDialect::getOptInfoAttrName()))) {
114 codeGenOpts.OptimizationLevel = optInfo.getLevel();
115 codeGenOpts.OptimizeSize = optInfo.getSize();
116 }
117
118 if (targetInfo) {
122 targetInfo->adjust(diags, langOpts, /*Aux=*/nullptr);
123 }
124
125 return std::make_unique<LowerModule>(std::move(langOpts),
126 std::move(codeGenOpts), module,
127 std::move(targetInfo));
128}
129
130} // namespace cir
Defines the clang::LangOptions interface.
static StringRef getTriple(const Command &Job)
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:49
const TargetLoweringInfo & getTargetLoweringInfo()
const clang::TargetInfo & getTarget() const
Definition LowerModule.h:70
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
static llvm::IntrusiveRefCntPtr< DiagnosticIDs > create()
Options for controlling the compiler diagnostics engine.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:240
A diagnostic client that ignores all diagnostics.
ClangABI
Clang versions with different platform ABI conformance.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
std::optional< TargetCXXABI::Kind > CXXABI
C++ ABI to compile with, if specified by the frontend through -fc++-abi=.
Kind getKind() const
Options for controlling the target.
std::string Triple
The name of the target triple to compile for.
Defines the clang::TargetInfo interface.
std::unique_ptr< CIRCXXABI > createItaniumCXXABI(LowerModule &lm)
Creates an Itanium-family ABI.
std::unique_ptr< TargetLoweringInfo > createTargetLoweringInfo(const llvm::Triple &triple)
Select target lowering rules without constructing language or ABI state.
static std::unique_ptr< CIRCXXABI > createCXXABI(LowerModule &lm)
std::unique_ptr< CIRCXXABI > createMicrosoftCXXABI(LowerModule &lm)
Creates a Microsoft-family ABI.
std::unique_ptr< LowerModule > createLowerModule(mlir::ModuleOp module)
std::optional< llvm::Triple > getTripleFromModule(mlir::ModuleOp mod)
Attempts to construct an llvm::Triple from a module's cir.triple attribute.
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()