clang 24.0.0git
CIRPasses.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 implements machinery for any CIR <-> CIR passes used by clang.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/IR/BuiltinOps.h"
14#include "mlir/Pass/PassManager.h"
19#include "llvm/Support/TimeProfiler.h"
20#include "llvm/TargetParser/Triple.h"
21
22namespace cir {
23
24/// Map a target triple to the ABI target that drives CallConvLowering.
25/// Returns None for targets whose calling convention is not yet implemented.
26static CallConvTarget
27getCallConvTarget(const llvm::Triple &triple,
28 cir::LoweringOptionsAttr loweringLangOpts) {
29 // Windows is not supported. UEFI shares its convention.
30 if (triple.getArch() == llvm::Triple::x86_64 && !triple.isOSWindowsOrUEFI())
32 if (triple.isAMDGPU())
34 if (triple.isAArch64() && !triple.isOSWindows() &&
35 triple.getArch() != llvm::Triple::aarch64_32)
37 bool isHLSL = loweringLangOpts && loweringLangOpts.getHlsl();
38 bool isOpenMPDevice =
39 loweringLangOpts && loweringLangOpts.getOpenmpIsTargetDevice();
40 // AMDGCN-flavored SPIR-V follows the AMDGPU ABI instead. HLSL's integer
41 // promotion rules and resource types are not modeled yet.
42 // FIXME: Lower OpenMP device code once MLIR can translate it afterwards.
43 if (triple.isSPIRV() && triple.getVendor() != llvm::Triple::AMD && !isHLSL &&
44 !isOpenMPDevice)
47}
48
49/// The AVX level the classifier uses to size a native vector, read from the
50/// target ABI name.
51static llvm::abi::X86AVXABILevel getX86AVXABILevel(llvm::StringRef abi) {
52 if (abi == "avx512")
53 return llvm::abi::X86AVXABILevel::AVX512;
54 if (abi == "avx")
55 return llvm::abi::X86AVXABILevel::AVX;
56 return llvm::abi::X86AVXABILevel::None;
57}
58
59/// Whether `__attribute__((target(...)))` on a function may raise its AVX ABI
60/// level above the command line's. A target that opts out, and any ABI older
61/// than the rule, stay at the module level.
62static bool allowsX86TargetAttrAvx(const llvm::Triple &triple,
64 return !triple.isPS() && compat > clang::LangOptions::ClangABI::Ver23;
65}
66
67/// The x86_64 ABI-compatibility flags, derived from the target and the
68/// requested compatibility version. Every flag defaults to true in the ABI
69/// library, which is not what any target computes: Clang11Compat is false for a
70/// modern Linux target, so leaving it at the default classifies a union larger
71/// than an eightbyte as though every member spanned its size.
72static llvm::abi::X86ABICompatInfo
73getX86ABICompatInfo(const llvm::Triple &triple,
75 llvm::abi::X86ABICompatInfo abiCompat;
76 abiCompat.HonorsRevision98 = !triple.isOSDarwin();
77 abiCompat.ClassifyIntegerMMXAsSSE =
78 compat > clang::LangOptions::ClangABI::Ver3_8 && !triple.isOSDarwin() &&
79 !triple.isPS() && !triple.isOSFreeBSD();
80 abiCompat.PassInt128VectorsInMem =
81 compat > clang::LangOptions::ClangABI::Ver9 &&
82 (triple.isOSLinux() || triple.isOSNetBSD());
83 abiCompat.ReturnCXXRecordGreaterThan128InMem =
84 compat > clang::LangOptions::ClangABI::Ver20 && !triple.isPS();
85 abiCompat.Clang11Compat =
86 compat <= clang::LangOptions::ClangABI::Ver11 || triple.isPS();
87 // Match the classic CodeGen default of -msmall-int-abi-compat.
89 abiCompat.SmallIntABICompat = triple.isOSDarwin() || triple.isPS()
90 ? llvm::abi::SmallIntABICompatKind::Clang23
91 : llvm::abi::SmallIntABICompatKind::Both;
92 return abiCompat;
93}
94
95mlir::LogicalResult
96runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirContext,
97 bool enableVerifier, bool enableIdiomRecognizer,
98 bool enableCIRSimplify, bool enableLibOpt,
99 llvm::StringRef libOptOptions, bool enableCallConvLowering) {
100
101 llvm::TimeTraceScope scope("CIR To CIR Passes");
102
103 std::optional<llvm::Triple> triple = cir::getTripleFromModule(theModule);
104 if (!triple)
105 return theModule.emitError()
106 << "missing '" << cir::CIRDialect::getTripleAttrName()
107 << "' attribute";
108
109 auto abiAttr = theModule->getAttrOfType<mlir::StringAttr>(
110 cir::CIRDialect::getTargetABIAttrName());
111 if (!abiAttr)
112 return theModule.emitError()
113 << "missing '" << cir::CIRDialect::getTargetABIAttrName()
114 << "' attribute";
115
116 mlir::PassManager pm(&mlirContext);
118
119 if (enableCIRSimplify)
120 pm.addPass(mlir::createCIRSimplifyPass());
121
122 if (enableIdiomRecognizer)
124
125 if (enableLibOpt) {
126 auto libOptPass = mlir::createLibOptPass();
127 auto errorHandler = [](const llvm::Twine &) -> mlir::LogicalResult {
128 return mlir::LogicalResult::failure();
129 };
130
131 if (libOptPass->initializeOptions(libOptOptions, errorHandler).failed())
132 return mlir::failure();
133
134 pm.addPass(std::move(libOptPass));
135 }
136
137 pm.addPass(mlir::createTargetLoweringPass());
138 pm.addPass(mlir::createCXXABILoweringPass());
139
140 // LoweringPrepare synthesizes calls to runtime helpers such as __divsc3, and
141 // outlines dynamic global initializers into functions. It must run before
142 // CallConvLowering so the classifier sees them, otherwise their signatures
143 // go unclassified and caller and callee disagree on the ABI.
145
146 if (enableCallConvLowering) {
147 // CallConvLowering rewrites signatures and call sites using the classifier,
148 // so it must run after CXXABILowering has lowered C++ ABI types to plain
149 // records the classifier can handle.
150 auto loweringLangOpts = theModule->getAttrOfType<cir::LoweringOptionsAttr>(
151 cir::CIRDialect::getLoweringOptionsAttrName());
152 CallConvTarget target = getCallConvTarget(*triple, loweringLangOpts);
153 if (target != CallConvTarget::None) {
154 // Source the ABI-compatibility version from the module's serialized
155 // #cir.lowering_options so a reloaded .cir classifies the same way
156 // it was compiled, without a live clang::LangOptions. CIRGen sets this
157 // attribute at module construction; if it is absent fall back to the
158 // LangOptions default, matching how LowerModule reads the same attribute.
159 auto compat = clang::LangOptions::ClangABI::Latest;
160 if (loweringLangOpts)
161 compat = static_cast<clang::LangOptions::ClangABI>(
162 loweringLangOpts.getClangAbiCompat());
164 target, getX86AVXABILevel(abiAttr.getValue()),
165 allowsX86TargetAttrAvx(*triple, compat),
166 getX86ABICompatInfo(*triple, compat)));
167 }
168 }
169
170 pm.enableVerifier(enableVerifier);
171 (void)mlir::applyPassManagerCLOptions(pm);
172 return pm.run(theModule);
173}
174
175} // namespace cir
176
177namespace mlir {
178
179void populateCIRPreLoweringPasses(OpPassManager &pm) {
180 pm.addPass(createHoistAllocasPass());
181 pm.addPass(createCIRFlattenCFGPass());
182 pm.addPass(createCIREHABILoweringPass());
183 pm.addPass(createGotoSolverPass());
184 pm.addPass(createCIRFoldEmptyBranchesPass());
185 pm.addNestedPass<cir::FuncOp>(createCIRForwardReturnSlotPass());
186}
187
188} // namespace mlir
Defines the clang::LangOptions interface.
ClangABI
Clang versions with different platform ABI conformance.
static bool allowsX86TargetAttrAvx(const llvm::Triple &triple, clang::LangOptions::ClangABI compat)
Whether __attribute__((target(...))) on a function may raise its AVX ABI level above the command line...
Definition CIRPasses.cpp:62
static CallConvTarget getCallConvTarget(const llvm::Triple &triple, cir::LoweringOptionsAttr loweringLangOpts)
Map a target triple to the ABI target that drives CallConvLowering.
Definition CIRPasses.cpp:27
mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirCtx, bool enableVerifier, bool enableIdiomRecognizer, bool enableCIRSimplify, bool enableLibOpt, llvm::StringRef libOptOptions, bool enableCallConvLowering)
Definition CIRPasses.cpp:96
static llvm::abi::X86AVXABILevel getX86AVXABILevel(llvm::StringRef abi)
The AVX level the classifier uses to size a native vector, read from the target ABI name.
Definition CIRPasses.cpp:51
static llvm::abi::X86ABICompatInfo getX86ABICompatInfo(const llvm::Triple &triple, clang::LangOptions::ClangABI compat)
The x86_64 ABI-compatibility flags, derived from the target and the requested compatibility version.
Definition CIRPasses.cpp:73
std::optional< llvm::Triple > getTripleFromModule(mlir::ModuleOp mod)
Attempts to construct an llvm::Triple from a module's cir.triple attribute.
CallConvTarget
The ABI target whose calling-convention rules drive CallConvLowering.
Definition Passes.h:23
std::unique_ptr< Pass > createCallConvLoweringPass()
std::unique_ptr< Pass > createCIREHABILoweringPass()
std::unique_ptr< Pass > createCIRCanonicalizePass()
std::unique_ptr< Pass > createCIRFlattenCFGPass()
std::unique_ptr< Pass > createLibOptPass()
Definition LibOpt.cpp:65
std::unique_ptr< Pass > createIdiomRecognizerPass()
void populateCIRPreLoweringPasses(mlir::OpPassManager &pm)
std::unique_ptr< Pass > createTargetLoweringPass()
std::unique_ptr< Pass > createGotoSolverPass()
std::unique_ptr< Pass > createLoweringPreparePass()
std::unique_ptr< Pass > createCIRForwardReturnSlotPass()
std::unique_ptr< Pass > createCIRSimplifyPass()
std::unique_ptr< Pass > createCXXABILoweringPass()
std::unique_ptr< Pass > createHoistAllocasPass()
std::unique_ptr< Pass > createCIRFoldEmptyBranchesPass()
static bool smallIntABICompatOption()