1//===- CIRABIClassifier.h - ABI classification for CIR ----------*- C++ -*-===//
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 defines CIRABIClassifier, the interface CallConvLowering uses to
10// obtain a mlir::abi::FunctionClassification for a CIR signature.
11//
12// CIRABIClassifier is the producer half of CIR's calling-convention lowering.
13// The consumer half is CIRABIRewriteContext, which turns a classification into
14// IR and needs to know nothing about where it came from.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef CLANG_LIB_CIR_DIALECT_TRANSFORMS_CIRABICLASSIFIER_H
19#define CLANG_LIB_CIR_DIALECT_TRANSFORMS_CIRABICLASSIFIER_H
20
21#include "mlir/ABI/ABIRewriteContext.h"
22#include "mlir/ABI/ABITypeMapper.h"
23#include "mlir/IR/BuiltinOps.h"
24#include "mlir/IR/Diagnostics.h"
25#include "mlir/Interfaces/DataLayoutInterfaces.h"
26#include "mlir/Support/LogicalResult.h"
27#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
28#include "clang/CIR/Dialect/IR/CIRDialect.h"
29#include "clang/CIR/Dialect/Passes.h"
30#include "llvm/ABI/FunctionInfo.h"
31#include "llvm/ABI/TargetInfo.h"
32#include "llvm/ADT/STLFunctionalExtras.h"
33#include "llvm/ADT/StringRef.h"
34#include "llvm/IR/CallingConv.h"
35
36#include <memory>
37
38namespace cir {
39
40/// The target-specific settings a classifier needs beyond what it can read
41/// from the module. Every field names the target it belongs to, because a
42/// classifier for one target ignores the others' settings.
43struct ABIClassifierOptions {
44 // TODO: Make this class target-independent.
45
46 /// The AVX level the x86_64 classifier sizes a native vector at.
47 llvm::abi::X86AVXABILevel x86AvxAbiLevel = llvm::abi::X86AVXABILevel::None;
48
49 /// Whether a function's own recorded target features may raise its x86_64
50 /// AVX level above x86AvxAbiLevel.
51 bool allowsX86TargetAttrAvx = true;
52
53 /// The x86_64 flags whose value depends on the target and the requested ABI
54 /// compatibility version.
55 llvm::abi::X86ABICompatInfo x86AbiCompat;
56};
57
58/// Produces the classification CallConvLowering rewrites a signature from.
59///
60/// A subclass supplies one target's rules, or one of the two test drivers that
61/// stand in for a target. The pass holds a single instance for the whole
62/// module. Each method returns failure after emitting an error on the
63/// operation it was given, including for an operation the subclass does not
64/// support yet.
65class CIRABIClassifier {
66public:
67 virtual ~CIRABIClassifier() = default;
68
69 /// Classify \p func's declared signature.
70 virtual mlir::FailureOr<mlir::abi::FunctionClassification>
71 classifyFunction(cir::FuncOp func) = 0;
72
73 /// Classify a direct call to the variadic function \p callee that passes
74 /// arguments through the ellipsis. The classification covers every operand
75 /// of \p call, and classifies the declared parameters the same way as
76 /// classifyFunction(callee).
77 virtual mlir::FailureOr<mlir::abi::FunctionClassification>
78 classifyVariadicCall(cir::CIRCallOpInterface call, cir::FuncOp callee) = 0;
79
80 /// Classify \p call, whose callee is resolved at run time, from the
81 /// signature its callee pointer points to.
82 virtual mlir::FailureOr<mlir::abi::FunctionClassification>
83 classifyIndirectCall(cir::CIRCallOpInterface call) = 0;
84
85 /// Classify the type fetched by \p vaArg as an unnamed argument with a full
86 /// register budget.
87 virtual mlir::FailureOr<mlir::abi::ArgClassification>
88 classifyVAArg(cir::VAArgOp vaArg) = 0;
89};
90
91/// Classifies through the LLVM ABI Lowering Library. The signature's CIR types
92/// are mapped to llvm::abi types, the target's TargetInfo classifies them, and
93/// each llvm::abi::ArgInfo is converted to an ArgClassification.
94///
95/// A subclass supplies the TargetInfo and classifies `cir.va_arg`, whose
96/// expansion is specific to each target.
97class CIRABIClassifierCommon : public CIRABIClassifier {
98public:
99 mlir::FailureOr<mlir::abi::FunctionClassification>
100 classifyFunction(cir::FuncOp func) override;
101
102 /// The call is classified at the callee's features, not the caller's,
103 /// because the pass rewrites the definition and its call sites from one
104 /// classification. Classic instead arranges every call site from the
105 /// caller and reports a caller whose features disagree with its callee in
106 /// checkFunctionCallABI, which has no equivalent here yet.
107 mlir::FailureOr<mlir::abi::FunctionClassification>
108 classifyVariadicCall(cir::CIRCallOpInterface call,
109 cir::FuncOp callee) override;
110
111 /// A callee resolved at run time carries no features of its own, so the
112 /// call is classified at the features of the function containing it, which
113 /// is the declaration classic arranges every call site from. A call that
114 /// passes arguments through an ellipsis is classified from its own
115 /// operands, as a direct one is.
116 mlir::FailureOr<mlir::abi::FunctionClassification>
117 classifyIndirectCall(cir::CIRCallOpInterface call) override;
118
119protected:
120 CIRABIClassifierCommon(const mlir::DataLayout &dl, mlir::ModuleOp modOp)
121 : dl(dl), modOp(modOp), cirDataLayout(modOp), typeMapper(dl) {}
122
123 /// The library classifier for a function whose recorded target features
124 /// may refine the ABI rules.
125 virtual const llvm::abi::TargetInfo &
126 getTargetInfo(cir::FuncOp featureSource) = 0;
127
128 /// Classify \p ty, the type fetched by a `cir.va_arg`, as an unnamed
129 /// argument with every argument register free.
130 mlir::FailureOr<mlir::abi::ArgClassification>
131 classifyVarArgType(mlir::Type ty, const llvm::abi::TargetInfo &targetInfo,
132 llvm::function_ref<mlir::InFlightDiagnostic()> emitError);
133
134 const mlir::DataLayout &dl;
135 mlir::ModuleOp modOp;
136 cir::CIRDataLayout cirDataLayout;
137 mlir::abi::ABITypeMapper typeMapper;
138
139private:
140 /// Classify a signature returning \p retCIR and taking \p inputs, whose
141 /// first \p required entries are declared parameters, under \p callConv.
142 mlir::FailureOr<mlir::abi::FunctionClassification>
143 classifySignature(mlir::Type retCIR, mlir::TypeRange inputs,
144 llvm::abi::RequiredArgs required,
145 const llvm::abi::TargetInfo &targetInfo,
146 llvm::CallingConv::ID callConv,
147 llvm::function_ref<mlir::InFlightDiagnostic()> emitError);
148
149 /// Classify \p call, which passes more operands than \p calleeTy declares,
150 /// from the call's operands.
151 mlir::FailureOr<mlir::abi::FunctionClassification>
152 classifyVariadicCall(cir::CIRCallOpInterface call, cir::FuncType calleeTy,
153 const llvm::abi::TargetInfo &targetInfo,
154 llvm::CallingConv::ID callConv);
155};
156
157/// The MLIR test ABI target's classifier, whose predictable rules approximate
158/// x86_64 SysV.
159std::unique_ptr<CIRABIClassifier>
160createTestTargetABIClassifier(const mlir::DataLayout &dl);
161
162/// A classifier that reads each function's classification from the
163/// DictionaryAttr named \p attrName.
164std::unique_ptr<CIRABIClassifier>
165createInjectedABIClassifier(const mlir::DataLayout &dl,
166 llvm::StringRef attrName);
167
168/// The x86_64 System V classifier.
169std::unique_ptr<CIRABIClassifier>
170createX86_64ABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp,
171 const ABIClassifierOptions &options);
172
173/// The AMDGPU classifier.
174std::unique_ptr<CIRABIClassifier>
175createAMDGPUABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp);
176
177/// The SPIR-V classifier.
178std::unique_ptr<CIRABIClassifier>
179createSPIRVABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp);
180
181/// Map a CIR calling convention to the llvm::CallingConv the ABI classifier
182/// uses. Conventions the classifier does not distinguish fall back to C.
183llvm::CallingConv::ID convertCallingConv(cir::CallingConv cc);
184
185} // namespace cir
186
187#endif // CLANG_LIB_CIR_DIALECT_TRANSFORMS_CIRABICLASSIFIER_H
188