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
46static std::unique_ptr<TargetLoweringInfo>
48 const llvm::Triple &triple = lm.getTarget().getTriple();
49
50 switch (triple.getArch()) {
51 case llvm::Triple::amdgpu:
53 case llvm::Triple::nvptx:
54 case llvm::Triple::nvptx64:
56 case llvm::Triple::spir:
57 case llvm::Triple::spir64:
59 case llvm::Triple::spirv:
60 case llvm::Triple::spirv32:
61 case llvm::Triple::spirv64:
63 default:
65 return std::make_unique<TargetLoweringInfo>();
66 }
67}
68
70 clang::CodeGenOptions codeGenOpts,
71 mlir::ModuleOp &module,
72 std::unique_ptr<clang::TargetInfo> target)
73 : module(module), langOpts(std::move(langOpts)), target(std::move(target)) {
74 if (this->target)
75 abi = createCXXABI(*this);
76}
77
79 if (!targetLoweringInfo)
80 targetLoweringInfo = createTargetLoweringInfo(*this);
81 return *targetLoweringInfo;
82}
83
84// TODO: not to create it every time
85std::unique_ptr<LowerModule> createLowerModule(mlir::ModuleOp module) {
86 // If the triple is not present, e.g. CIR modules parsed from text, or clang
87 // has no TargetInfo support for it yet, LowerModule currently gets created
88 // with null target and abi. If triple becomes a requirement, we should not
89 // allow LowerModules to be created with null target and abi.
91
92 // Fetch target information.
93 std::unique_ptr<clang::TargetInfo> targetInfo;
94 if (std::optional<llvm::Triple> triple = cir::getTripleFromModule(module)) {
95 clang::TargetOptions targetOptions;
96 targetOptions.Triple = triple->str();
97 targetInfo = clang::targets::AllocateTarget(*triple, targetOptions);
98 }
99
100 // Populate the lowering-relevant LangOptions from the module's
101 // #cir.lowering_options attribute so a reloaded .cir lowers the same
102 // way it was compiled, without a live clang::LangOptions. When the attribute
103 // is absent (e.g. hand-written CIR) the defaults are kept;
104 clang::LangOptions langOpts;
105 if (auto loweringLangOpts =
106 mlir::dyn_cast_if_present<cir::LoweringOptionsAttr>(
107 module->getAttr(cir::CIRDialect::getLoweringOptionsAttrName()))) {
108 langOpts.Exceptions = loweringLangOpts.getExceptions();
109 langOpts.ThreadsafeStatics = loweringLangOpts.getThreadsafeStatics();
110 langOpts.CUDA = loweringLangOpts.getCuda();
111 langOpts.CUDAIsDevice = loweringLangOpts.getCudaIsDevice();
112 langOpts.HIP = loweringLangOpts.getHip();
113 langOpts.GPURelocatableDeviceCode = loweringLangOpts.getGpuRdc();
114 langOpts.OpenMP = loweringLangOpts.getOpenmp();
115 langOpts.OpenMPIsTargetDevice = loweringLangOpts.getOpenmpIsTargetDevice();
116 langOpts.setClangABICompat(static_cast<clang::LangOptions::ClangABI>(
117 loweringLangOpts.getClangAbiCompat()));
118 langOpts.LongDoubleSize = loweringLangOpts.getLongDoubleSize();
119 }
120 // Some langopts are still not lowered.
122
123 if (auto cxxABI = module->getAttrOfType<mlir::StringAttr>(
124 cir::CIRDialect::getCXXABIAttrName()))
125 langOpts.CXXABI = clang::TargetCXXABI::getKind(cxxABI.getValue());
126
127 // FIXME(cir): This just uses the default code generation options. We need to
128 // account for custom options.
130 clang::CodeGenOptions codeGenOpts;
131
132 if (auto optInfo = mlir::cast_if_present<cir::OptInfoAttr>(
133 module->getAttr(cir::CIRDialect::getOptInfoAttrName()))) {
134 codeGenOpts.OptimizationLevel = optInfo.getLevel();
135 codeGenOpts.OptimizeSize = optInfo.getSize();
136 }
137
138 if (targetInfo) {
142 targetInfo->adjust(diags, langOpts, /*Aux=*/nullptr);
143 }
144
145 return std::make_unique<LowerModule>(std::move(langOpts),
146 std::move(codeGenOpts), module,
147 std::move(targetInfo));
148}
149
150} // 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: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:241
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
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
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< TargetLoweringInfo > createCommonSPIRTargetLoweringInfo()
Definition SPIRV.cpp:53
std::unique_ptr< TargetLoweringInfo > createSPIRVTargetLoweringInfo()
Definition SPIRV.cpp:57
std::unique_ptr< CIRCXXABI > createItaniumCXXABI(LowerModule &lm)
Creates an Itanium-family ABI.
static std::unique_ptr< CIRCXXABI > createCXXABI(LowerModule &lm)
std::unique_ptr< TargetLoweringInfo > createNVPTXTargetLoweringInfo()
Definition NVPTX.cpp:44
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< TargetLoweringInfo > createAMDGPUTargetLoweringInfo()
Definition AMDGPU.cpp:50
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()