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"
17#include "mlir/IR/PatternMatch.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/FileSystem.h"
24
25namespace cir {
26
27static std::unique_ptr<CIRCXXABI> createCXXABI(LowerModule &lm) {
28 switch (lm.getCXXABIKind()) {
29 case clang::TargetCXXABI::AppleARM64:
30 case clang::TargetCXXABI::Fuchsia:
31 case clang::TargetCXXABI::GenericAArch64:
32 case clang::TargetCXXABI::GenericARM:
33 case clang::TargetCXXABI::iOS:
34 case clang::TargetCXXABI::WatchOS:
35 case clang::TargetCXXABI::GenericMIPS:
36 case clang::TargetCXXABI::GenericItanium:
37 case clang::TargetCXXABI::WebAssembly:
38 case clang::TargetCXXABI::XL:
39 return createItaniumCXXABI(lm);
40 case clang::TargetCXXABI::Microsoft:
41 llvm_unreachable("Windows ABI NYI");
42 }
43
44 llvm_unreachable("invalid C++ ABI kind");
45}
46
47static std::unique_ptr<TargetLoweringInfo>
50 return std::make_unique<TargetLoweringInfo>();
51}
52
54 clang::CodeGenOptions codeGenOpts,
55 mlir::ModuleOp &module,
56 std::unique_ptr<clang::TargetInfo> target,
57 mlir::PatternRewriter &rewriter)
58 : module(module), target(std::move(target)), abi(createCXXABI(*this)),
59 rewriter(rewriter) {}
60
62 if (!targetLoweringInfo)
63 targetLoweringInfo = createTargetLoweringInfo(*this);
64 return *targetLoweringInfo;
65}
66
67// TODO: not to create it every time
68std::unique_ptr<LowerModule>
69createLowerModule(mlir::ModuleOp module, mlir::PatternRewriter &rewriter) {
70 // Fetch target information.
71 llvm::Triple triple(mlir::cast<mlir::StringAttr>(
72 module->getAttr(cir::CIRDialect::getTripleAttrName()))
73 .getValue());
74 clang::TargetOptions targetOptions;
75 targetOptions.Triple = triple.str();
76 auto targetInfo = clang::targets::AllocateTarget(triple, targetOptions);
77
78 // FIXME(cir): This just uses the default language options. We need to account
79 // for custom options.
80 // Create context.
82 clang::LangOptions langOpts;
83
84 // FIXME(cir): This just uses the default code generation options. We need to
85 // account for custom options.
87 clang::CodeGenOptions codeGenOpts;
88
89 if (auto optInfo = mlir::cast_if_present<cir::OptInfoAttr>(
90 module->getAttr(cir::CIRDialect::getOptInfoAttrName()))) {
91 codeGenOpts.OptimizationLevel = optInfo.getLevel();
92 codeGenOpts.OptimizeSize = optInfo.getSize();
93 }
94
95 return std::make_unique<LowerModule>(std::move(langOpts),
96 std::move(codeGenOpts), module,
97 std::move(targetInfo), rewriter);
98}
99
100} // namespace cir
Defines the clang::LangOptions interface.
Defines the clang::TargetOptions class.
clang::TargetCXXABI::Kind getCXXABIKind() const
Definition LowerModule.h:42
LowerModule(clang::LangOptions langOpts, clang::CodeGenOptions codeGenOpts, mlir::ModuleOp &module, std::unique_ptr< clang::TargetInfo > target, mlir::PatternRewriter &rewriter)
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.
std::unique_ptr< LowerModule > createLowerModule(mlir::ModuleOp module, mlir::PatternRewriter &rewriter)
static std::unique_ptr< CIRCXXABI > createCXXABI(LowerModule &lm)
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 targetLoweringInfo()