38#include "mlir/ABI/ABIRewriteContext.h"
39#include "mlir/Dialect/DLTI/DLTI.h"
40#include "mlir/IR/Builders.h"
41#include "mlir/IR/BuiltinOps.h"
42#include "mlir/IR/SymbolTable.h"
43#include "mlir/Interfaces/DataLayoutInterfaces.h"
44#include "mlir/Pass/Pass.h"
47#include "llvm/ABI/TargetInfo.h"
48#include "llvm/ADT/ScopeExit.h"
53using namespace mlir::abi;
57#define GEN_PASS_DEF_CALLCONVLOWERING
58#include "clang/CIR/Dialect/Passes.h.inc"
65 if (
auto recTy = dyn_cast<cir::RecordType>(ty))
66 return !recTy.isComplete() ||
68 if (
auto arrTy = dyn_cast<cir::ArrayType>(ty))
77 if (fc.returnInfo.kind == ArgKind::Indirect)
79 return llvm::any_of(fc.argInfos, [](
const ArgClassification &ac) {
80 return ac.kind == ArgKind::Indirect || ac.kind == ArgKind::IndirectAliased;
88 cir::MemoryEffectsAttr effects = op.getMemoryEffectsAttr();
91 op.setMemoryEffectsAttr(cir::MemoryEffectsAttr::get(
92 effects.getContext(), effects.getOther(), cir::ModRefInfo::ModRef,
93 effects.getInaccessibleMem(), effects.getErrnoMem(),
94 effects.getTargetMem0(), effects.getTargetMem1()));
105 const FunctionClassification &callFc) {
106 if (callFc.argInfos.size() < calleeFc.argInfos.size())
108 return calleeFc.returnInfo == callFc.returnInfo &&
109 std::equal(calleeFc.argInfos.begin(), calleeFc.argInfos.end(),
110 callFc.argInfos.begin());
116struct CallConvLoweringPass
117 :
public impl::CallConvLoweringBase<CallConvLoweringPass> {
118 using CallConvLoweringBase::CallConvLoweringBase;
120 CallConvLoweringPass(
const CallConvLoweringOptions &options,
121 const llvm::abi::X86ABICompatInfo &x86AbiCompat)
122 : CallConvLoweringBase(options), x86AbiCompat(x86AbiCompat) {}
124 void runOnOperation()
override;
130 llvm::abi::X86ABICompatInfo x86AbiCompat;
138static std::unique_ptr<CIRABIClassifier>
140 StringRef classificationAttrName,
const DataLayout &dl,
142 if (!classificationAttrName.empty())
159 llvm_unreachable(
"an unset driver mode is rejected before this point");
169cir::FuncOp lookupCallee(Operation *callOp, SymbolTable &symbolTable) {
170 FlatSymbolRefAttr callee;
171 if (
auto call = dyn_cast<cir::CallOp>(callOp))
172 callee = call.getCalleeAttr();
173 else if (
auto tryCall = dyn_cast<cir::TryCallOp>(callOp))
174 callee = tryCall.getCalleeAttr();
179 return symbolTable.lookup<cir::FuncOp>(callee.getValue());
182void CallConvLoweringPass::runOnOperation() {
183 ModuleOp moduleOp = getOperation();
184 MLIRContext *ctx = &getContext();
187 bool haveAttr = !classificationAttr.empty();
188 if (haveTarget == haveAttr) {
189 moduleOp.emitOpError() <<
"CallConvLowering requires exactly one of "
190 "'target' or 'classification-attr' pass options";
195 if (!moduleOp->hasAttr(DLTIDialect::kDataLayoutAttrName)) {
196 moduleOp.emitOpError()
197 <<
"CallConvLowering requires a DataLayout (dlti.dl_spec attribute "
205 auto loweringOpts = moduleOp->getAttrOfType<cir::LoweringOptionsAttr>(
206 cir::CIRDialect::getLoweringOptionsAttrName());
207 bool enableNoundefAttrs =
208 !loweringOpts || loweringOpts.getEnableNoundefAttrs();
215 loweringOpts.getOpenmpIsTargetDevice()) {
216 moduleOp.emitOpError() <<
"OpenMP device code with SPIR-V target "
217 "is not yet supported";
222 DataLayout dl(moduleOp);
223 CIRABIRewriteContext rewriteCtx(moduleOp, dl, target, enableNoundefAttrs);
228 llvm::scope_exit drainParamSlots(
229 [&] { rewriteCtx.finalizeParameterSlots(); });
230 SymbolTable symbolTable(moduleOp);
232 cir::ABIClassifierOptions classifierOptions;
237 target, classificationAttr, dl, moduleOp, classifierOptions);
242 llvm::MapVector<cir::FuncOp, FunctionClassification> classifications;
243 bool anyFailed =
false;
244 moduleOp.walk([&](cir::FuncOp f) {
248 if (!f.getRequiresCallConvLowering())
254 cir::FuncType fnTy = f.getFunctionType();
255 if (f.isDeclaration() &&
259 mlir::FailureOr<FunctionClassification> fc =
260 classifier->classifyFunction(f);
265 classifications.insert({f, std::move(*fc)});
281 llvm::DenseMap<cir::FuncOp, SmallVector<Operation *>> callers;
285 llvm::DenseMap<Operation *, FunctionClassification> variadicCallSites;
286 moduleOp.walk([&](Operation *op) {
287 auto call = dyn_cast<cir::CIRCallOpInterface>(op);
290 cir::FuncOp callee = lookupCallee(op, symbolTable);
291 if (!callee || !callee.getRequiresCallConvLowering())
293 callers[callee].push_back(op);
295 cir::FuncType calleeTy = callee.getFunctionType();
296 if (call.getNumArgOperands() <= calleeTy.getNumInputs())
302 if (!calleeTy.isVarArg()) {
303 op->emitOpError() <<
"extra arguments to a callee without a prototype "
304 "not yet implemented in CallConvLowering";
308 mlir::FailureOr<FunctionClassification> fc =
309 classifier->classifyVariadicCall(call, callee);
314 variadicCallSites.insert({op, std::move(*fc)});
327 llvm::DenseMap<cir::FuncOp, SmallVector<cir::GetGlobalOp>> addressTakers;
328 moduleOp.walk([&](cir::GetGlobalOp getGlobal) {
329 auto ptrTy = cast<cir::PointerType>(getGlobal.getAddr().getType());
330 if (!isa<cir::FuncType>(ptrTy.getPointee()))
335 auto callee = cast<cir::FuncOp>(symbolTable.lookup(getGlobal.getName()));
336 addressTakers[callee].push_back(getGlobal);
344 for (
auto &kv : classifications) {
345 if (failed(rewriteCtx.prepareNonByvalParameters(kv.first, kv.second))) {
355 for (
auto &kv : classifications) {
356 cir::FuncOp func = kv.first;
372 OpBuilder builder(ctx);
373 for (
auto &kv : classifications) {
374 cir::FuncOp func = kv.first;
375 const FunctionClassification &fc = kv.second;
376 if (failed(rewriteCtx.rewriteFunctionDefinition(func, fc, builder))) {
380 for (Operation *callOp : callers.lookup(func)) {
381 const FunctionClassification *callFc = &fc;
382 if (
auto it = variadicCallSites.find(callOp);
383 it != variadicCallSites.end()) {
384 callFc = &it->second;
386 "a call site's declared parameters must be classified the same "
387 "way as the callee's");
391 if (failed(rewriteCtx.rewriteCallSite(callOp, *callFc, builder))) {
396 for (cir::GetGlobalOp addrOp : addressTakers.lookup(func))
397 rewriteCtx.rewriteFunctionAddress(addrOp, func, builder);
406 SmallVector<cir::CIRCallOpInterface> indirectCalls;
409 auto inFunctionToLower = [](Operation *op) {
410 auto func = op->getParentOfType<cir::FuncOp>();
411 return func && func.getRequiresCallConvLowering();
413 moduleOp.walk([&](cir::CIRCallOpInterface c) {
414 if (c.isIndirect() && inFunctionToLower(c))
415 indirectCalls.push_back(c);
417 for (cir::CIRCallOpInterface c : indirectCalls) {
418 mlir::FailureOr<FunctionClassification> fc =
419 classifier->classifyIndirectCall(c);
426 if (failed(rewriteCtx.rewriteCallSite(c.getOperation(), *fc, builder))) {
434 SmallVector<cir::VAArgOp> vaArgs;
435 moduleOp.walk([&](cir::VAArgOp v) {
436 if (inFunctionToLower(v))
439 for (cir::VAArgOp v : vaArgs) {
440 mlir::FailureOr<ArgClassification> ac = classifier->classifyVAArg(v);
445 if (failed(rewriteCtx.rewriteVAArg(v.getOperation(), *ac, builder))) {
452 for (
auto &kv : classifications)
453 kv.first.setRequiresCallConvLowering(
false);
459 return std::make_unique<CallConvLoweringPass>();
465 const llvm::abi::X86ABICompatInfo &x86AbiCompat) {
466 CallConvLoweringOptions options;
467 options.target = target;
468 options.x86AvxAbiLevel = x86AvxAbiLevel;
470 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 > createAArch64ABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp)
The AArch64 classifier, covering AAPCS64 and DarwinPCS.
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 > createSPIRVABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp)
The SPIR-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.