37#include "mlir/ABI/ABIRewriteContext.h"
38#include "mlir/Dialect/DLTI/DLTI.h"
39#include "mlir/IR/Builders.h"
40#include "mlir/IR/BuiltinOps.h"
41#include "mlir/IR/SymbolTable.h"
42#include "mlir/Interfaces/DataLayoutInterfaces.h"
43#include "mlir/Pass/Pass.h"
46#include "llvm/ABI/TargetInfo.h"
47#include "llvm/ADT/ScopeExit.h"
52using namespace mlir::abi;
56#define GEN_PASS_DEF_CALLCONVLOWERING
57#include "clang/CIR/Dialect/Passes.h.inc"
64 if (
auto recTy = dyn_cast<cir::RecordType>(ty))
65 return !recTy.isComplete() ||
67 if (
auto arrTy = dyn_cast<cir::ArrayType>(ty))
76 if (fc.returnInfo.kind == ArgKind::Indirect)
78 return llvm::any_of(fc.argInfos, [](
const ArgClassification &ac) {
79 return ac.kind == ArgKind::Indirect || ac.kind == ArgKind::IndirectAliased;
87 cir::MemoryEffectsAttr effects = op.getMemoryEffectsAttr();
90 op.setMemoryEffectsAttr(cir::MemoryEffectsAttr::get(
91 effects.getContext(), effects.getOther(), cir::ModRefInfo::ModRef,
92 effects.getInaccessibleMem(), effects.getErrnoMem(),
93 effects.getTargetMem0(), effects.getTargetMem1()));
104 const FunctionClassification &callFc) {
105 if (callFc.argInfos.size() < calleeFc.argInfos.size())
107 return calleeFc.returnInfo == callFc.returnInfo &&
108 std::equal(calleeFc.argInfos.begin(), calleeFc.argInfos.end(),
109 callFc.argInfos.begin());
115struct CallConvLoweringPass
116 :
public impl::CallConvLoweringBase<CallConvLoweringPass> {
117 using CallConvLoweringBase::CallConvLoweringBase;
119 CallConvLoweringPass(
const CallConvLoweringOptions &options,
120 const llvm::abi::X86ABICompatInfo &x86AbiCompat)
121 : CallConvLoweringBase(options), x86AbiCompat(x86AbiCompat) {}
123 void runOnOperation()
override;
129 llvm::abi::X86ABICompatInfo x86AbiCompat;
137static std::unique_ptr<CIRABIClassifier>
139 StringRef classificationAttrName,
const DataLayout &dl,
141 if (!classificationAttrName.empty())
154 llvm_unreachable(
"an unset driver mode is rejected before this point");
164cir::FuncOp lookupCallee(Operation *callOp, SymbolTable &symbolTable) {
165 FlatSymbolRefAttr callee;
166 if (
auto call = dyn_cast<cir::CallOp>(callOp))
167 callee = call.getCalleeAttr();
168 else if (
auto tryCall = dyn_cast<cir::TryCallOp>(callOp))
169 callee = tryCall.getCalleeAttr();
174 return symbolTable.lookup<cir::FuncOp>(callee.getValue());
177void CallConvLoweringPass::runOnOperation() {
178 ModuleOp moduleOp = getOperation();
179 MLIRContext *ctx = &getContext();
182 bool haveAttr = !classificationAttr.empty();
183 if (haveTarget == haveAttr) {
184 moduleOp.emitOpError() <<
"CallConvLowering requires exactly one of "
185 "'target' or 'classification-attr' pass options";
190 if (!moduleOp->hasAttr(DLTIDialect::kDataLayoutAttrName)) {
191 moduleOp.emitOpError()
192 <<
"CallConvLowering requires a DataLayout (dlti.dl_spec attribute "
200 auto loweringOpts = moduleOp->getAttrOfType<cir::LoweringOptionsAttr>(
201 cir::CIRDialect::getLoweringOptionsAttrName());
202 bool enableNoundefAttrs =
203 !loweringOpts || loweringOpts.getEnableNoundefAttrs();
205 DataLayout dl(moduleOp);
206 CIRABIRewriteContext rewriteCtx(moduleOp, dl, enableNoundefAttrs);
211 llvm::scope_exit drainParamSlots(
212 [&] { rewriteCtx.finalizeParameterSlots(); });
213 SymbolTable symbolTable(moduleOp);
215 cir::ABIClassifierOptions classifierOptions;
220 target, classificationAttr, dl, moduleOp, classifierOptions);
225 llvm::MapVector<cir::FuncOp, FunctionClassification> classifications;
226 bool anyFailed =
false;
227 moduleOp.walk([&](cir::FuncOp f) {
231 if (!f.getRequiresCallConvLowering())
237 cir::FuncType fnTy = f.getFunctionType();
238 if (f.isDeclaration() &&
242 std::optional<FunctionClassification> fc = classifier->classifyFunction(f);
247 classifications.insert({f, std::move(*fc)});
263 llvm::DenseMap<cir::FuncOp, SmallVector<Operation *>> callers;
267 llvm::DenseMap<Operation *, FunctionClassification> variadicCallSites;
268 moduleOp.walk([&](Operation *op) {
269 auto call = dyn_cast<cir::CIRCallOpInterface>(op);
272 cir::FuncOp callee = lookupCallee(op, symbolTable);
273 if (!callee || !callee.getRequiresCallConvLowering())
275 callers[callee].push_back(op);
277 cir::FuncType calleeTy = callee.getFunctionType();
278 if (call.getNumArgOperands() <= calleeTy.getNumInputs())
284 if (!calleeTy.isVarArg()) {
285 op->emitOpError() <<
"extra arguments to a callee without a prototype "
286 "not yet implemented in CallConvLowering";
290 std::optional<FunctionClassification> fc =
291 classifier->classifyVariadicCall(call, callee);
296 variadicCallSites.insert({op, std::move(*fc)});
309 llvm::DenseMap<cir::FuncOp, SmallVector<cir::GetGlobalOp>> addressTakers;
310 moduleOp.walk([&](cir::GetGlobalOp getGlobal) {
311 auto ptrTy = cast<cir::PointerType>(getGlobal.getAddr().getType());
312 if (!isa<cir::FuncType>(ptrTy.getPointee()))
317 auto callee = cast<cir::FuncOp>(symbolTable.lookup(getGlobal.getName()));
318 addressTakers[callee].push_back(getGlobal);
326 for (
auto &kv : classifications) {
327 if (failed(rewriteCtx.prepareNonByvalParameters(kv.first, kv.second))) {
337 for (
auto &kv : classifications) {
338 cir::FuncOp func = kv.first;
354 OpBuilder builder(ctx);
355 for (
auto &kv : classifications) {
356 cir::FuncOp func = kv.first;
357 const FunctionClassification &fc = kv.second;
358 if (failed(rewriteCtx.rewriteFunctionDefinition(func, fc, builder))) {
362 for (Operation *callOp : callers.lookup(func)) {
363 const FunctionClassification *callFc = &fc;
364 if (
auto it = variadicCallSites.find(callOp);
365 it != variadicCallSites.end()) {
366 callFc = &it->second;
368 "a call site's declared parameters must be classified the same "
369 "way as the callee's");
373 if (failed(rewriteCtx.rewriteCallSite(callOp, *callFc, builder))) {
378 for (cir::GetGlobalOp addrOp : addressTakers.lookup(func))
379 rewriteCtx.rewriteFunctionAddress(addrOp, func, builder);
388 SmallVector<cir::CIRCallOpInterface> indirectCalls;
391 auto inFunctionToLower = [](Operation *op) {
392 auto func = op->getParentOfType<cir::FuncOp>();
393 return func && func.getRequiresCallConvLowering();
395 moduleOp.walk([&](cir::CIRCallOpInterface c) {
396 if (c.isIndirect() && inFunctionToLower(c))
397 indirectCalls.push_back(c);
399 for (cir::CIRCallOpInterface c : indirectCalls) {
400 std::optional<FunctionClassification> fc =
401 classifier->classifyIndirectCall(c);
408 if (failed(rewriteCtx.rewriteCallSite(c.getOperation(), *fc, builder))) {
416 SmallVector<cir::VAArgOp> vaArgs;
417 moduleOp.walk([&](cir::VAArgOp v) {
418 if (inFunctionToLower(v))
421 for (cir::VAArgOp v : vaArgs) {
422 std::optional<ArgClassification> ac = classifier->classifyVAArg(v);
427 if (failed(rewriteCtx.rewriteVAArg(v.getOperation(), *ac, builder))) {
434 for (
auto &kv : classifications)
435 kv.first.setRequiresCallConvLowering(
false);
441 return std::make_unique<CallConvLoweringPass>();
447 const llvm::abi::X86ABICompatInfo &x86AbiCompat) {
448 CallConvLoweringOptions options;
449 options.target = target;
450 options.x86AvxAbiLevel = x86AvxAbiLevel;
452 return std::make_unique<CallConvLoweringPass>(options, x86AbiCompat);
static bool hasIndirectSlot(const FunctionClassification &fc)
Whether fc gives the callee access to memory through a pointer the ABI introduced: an sret slot for a...
static bool classifiesSamePrefix(const FunctionClassification &calleeFc, const FunctionClassification &callFc)
Whether callFc classifies a call's leading arguments and its return exactly as calleeFc classifies th...
static bool hasIncompleteRecordByValue(mlir::Type ty)
Whether ty, or an aggregate member/element reached by value (never through a pointer),...
static void widenForArgMemory(OpTy op)
Record on op that the memory its pointer arguments address may be read and written.
static std::unique_ptr< CIRABIClassifier > createABIClassifier(cir::CallConvTarget target, StringRef classificationAttrName, const DataLayout &dl, ModuleOp modOp, const ABIClassifierOptions &options)
Build the classifier the pass options select.
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...
std::unique_ptr< CIRABIClassifier > createAMDGPUABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp)
The AMDGPU classifier.
std::unique_ptr< CIRABIClassifier > createX86_64ABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp, const ABIClassifierOptions &options)
The x86_64 System V classifier.
std::unique_ptr< CIRABIClassifier > createInjectedABIClassifier(const mlir::DataLayout &dl, llvm::StringRef attrName)
A classifier that reads each function's classification from the DictionaryAttr named attrName.
std::unique_ptr< CIRABIClassifier > createTestTargetABIClassifier(const mlir::DataLayout &dl)
The MLIR test ABI target's classifier, whose predictable rules approximate x86_64 SysV.
CallConvTarget
The ABI target whose calling-convention rules drive CallConvLowering.
std::unique_ptr< Pass > createCallConvLoweringPass()
The target-specific settings a classifier needs beyond what it can read from the module.
bool allowsX86TargetAttrAvx
Whether a function's own recorded target features may raise its x86_64 AVX level above x86AvxAbiLevel...
llvm::abi::X86ABICompatInfo x86AbiCompat
The x86_64 flags whose value depends on the target and the requested ABI compatibility version.
llvm::abi::X86AVXABILevel x86AvxAbiLevel
The AVX level the x86_64 classifier sizes a native vector at.