clang 24.0.0git
CIRABIRewriteContext.h
Go to the documentation of this file.
1//===- CIRABIRewriteContext.h - CIR ABI rewrite context ---------*- 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// Defines CIRABIRewriteContext, the CIR dialect's implementation of the
10// generic mlir::abi::ABIRewriteContext. Given a FunctionClassification it
11// rewrites a cir.func signature, the function body, and call sites to match
12// the ABI-lowered shape.
13//
14// This file handles Direct (pass-through and coerce-in-registers), Extend,
15// Ignore, Indirect (sret return, byval and non-byval arguments), and Expand
16// (struct flattening into scalar fields).
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_CIRABIREWRITECONTEXT_H
21#define CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_CIRABIREWRITECONTEXT_H
22
23#include "mlir/ABI/ABIRewriteContext.h"
24#include "mlir/Analysis/AliasAnalysis.h"
25#include "mlir/IR/BuiltinOps.h"
26#include "mlir/Interfaces/DataLayoutInterfaces.h"
31#include "llvm/ADT/SmallVector.h"
32#include <cassert>
33#include <utility>
34
35namespace cir {
36
38
39/// CIR-specific implementation of mlir::abi::ABIRewriteContext.
40///
41/// The driver pass (CallConvLoweringPass) computes a FunctionClassification
42/// for each cir.func / cir.call and dispatches to this class to perform the
43/// actual IR rewriting using cir dialect operations.
44///
45/// Holds a reference to the module's DataLayout for coercion alignment
46/// queries. The DataLayout must outlive the rewrite context.
48public:
49 CIRABIRewriteContext(mlir::ModuleOp module, const mlir::DataLayout &dl,
50 cir::CallConvTarget target, bool enableNoundefAttrs)
51 : module(module), dl(dl), target(target),
52 enableNoundefAttrs(enableNoundefAttrs), cirDataLayout(module),
53 aliasAnalysis(module) {
54 cir::registerCIRAliasAnalyses(aliasAnalysis, module);
55 }
56
58 assert(pendingParamSlots.empty() &&
59 "finalizeParameterSlots must run before the rewrite context dies");
60 }
61
62 mlir::LogicalResult
63 rewriteFunctionDefinition(mlir::FunctionOpInterface funcOp,
64 const mlir::abi::FunctionClassification &fc,
65 mlir::OpBuilder &builder) override;
66
67 mlir::LogicalResult
68 rewriteCallSite(mlir::Operation *callOp,
69 const mlir::abi::FunctionClassification &fc,
70 mlir::OpBuilder &builder) override;
71
72 /// Expand a `cir.va_arg` with the va_arg rules of the target this context
73 /// lowers for.
74 mlir::LogicalResult rewriteVAArg(mlir::Operation *vaArgOp,
75 const mlir::abi::ArgClassification &ac,
76 mlir::OpBuilder &builder) override;
77
78 /// Retype \p addrOp, which holds the address of \p funcOp, to the signature
79 /// funcOp was rewritten to, and cast it back so existing uses keep the type
80 /// they were built for. A no-op when the ABI left funcOp's type alone.
81 /// Call after funcOp has been rewritten. Not an override, since taking a
82 /// function's address has no counterpart in the generic contract.
83 void rewriteFunctionAddress(cir::GetGlobalOp addrOp, cir::FuncOp funcOp,
84 mlir::OpBuilder &builder);
85
86 /// Bring each non-byval indirect parameter of \p funcOp into the shape the
87 /// rest of the rewrite assumes: the parameter's only use, if it has one, is
88 /// a single store into an alloca of the incoming pointer's type, directly or
89 /// through an address_space cast of it. No other non-byval indirect
90 /// parameter spills to that alloca, and it states the alignment the ABI
91 /// promises rather than the one CIRGen picked for a local copy. A use of
92 /// the parameter as a call argument is routed through a load of that slot,
93 /// so that it names the storage an argument has to name.
94 ///
95 /// Call for every function before any definition or call site is rewritten.
96 /// findParamSpill asserts this shape while the enclosing definition is
97 /// rewritten, and a call is rewritten with its callee rather than with its
98 /// enclosing function and so may be reached first.
99 ///
100 /// A parameter read with no spill to name is given one, since it becomes
101 /// the incoming pointer directly. A parameter spilled twice, spilled to a
102 /// slot another such parameter also spills to, consumed other than as a
103 /// call argument, spilled where the incoming pointer cannot replace the
104 /// storage, or read where the spill does not dominate it gets a diagnostic
105 /// on the operation at fault and failure.
106 ///
107 /// Not an override, since this has no counterpart in the generic contract.
108 mlir::LogicalResult
109 prepareNonByvalParameters(cir::FuncOp funcOp,
110 const mlir::abi::FunctionClassification &fc);
111
112 /// Replace each non-byval indirect parameter's spill slot with the
113 /// incoming pointer, so the body operates on the caller's storage in place.
114 /// Call once, after every function and call site has been rewritten: a call
115 /// forwarding such a parameter reads the slot to recognize it. Not an
116 /// override, since deferring this has no counterpart in the generic
117 /// contract.
119
120 mlir::StringRef getDialectNamespace() const override { return "cir"; }
121
122private:
123 /// Expand \p op into the x86-64 SysV register-save-area / overflow-area
124 /// sequence.
125 mlir::LogicalResult rewriteVAArgX86_64(cir::VAArgOp op,
126 const mlir::abi::ArgClassification &ac,
127 CIRBaseBuilderTy &builder);
128
129 /// Expand \p op into a read from the 4-byte slots its pointer va_list walks.
130 mlir::LogicalResult rewriteVAArgAMDGPU(cir::VAArgOp op,
131 const mlir::abi::ArgClassification &ac,
132 CIRBaseBuilderTy &builder);
133
134 mlir::ModuleOp module;
135 const mlir::DataLayout &dl;
136
137 /// The target whose rules drive the target-specific rewrites, such as
138 /// va_arg expansion.
139 cir::CallConvTarget target;
140
141 /// Whether extended and indirect arguments are marked llvm.noundef. Set
142 /// from the enable_noundef_attrs field of the module's
143 /// #cir.lowering_options.
144 bool enableNoundefAttrs;
145
146 /// The CIR view of the module's data layout, which provides the store and
147 /// alloc sizes that \c dl does not.
148 cir::CIRDataLayout cirDataLayout;
149
150 /// Alias analysis over the module with the CIR implementations registered.
151 /// rewriteCallSite asks it whether a byval operand's storage is written
152 /// between its load and the call.
153 mlir::AliasAnalysis aliasAnalysis;
154
155 /// Param-slot allocas that non-byval indirect parameters will
156 /// replace, paired with the incoming pointer that replaces them. The
157 /// rewrite retypes the block argument but leaves the slot standing, because
158 /// a call site recognizes a forwardable parameter by the slot its operand
159 /// was loaded from. finalizeParameterSlots does the replacement once every
160 /// call site has been rewritten.
162 pendingParamSlots;
163};
164
165/// The signature the indirect call \p call reaches its callee through.
166cir::FuncType getIndirectCalleeType(cir::CIRCallOpInterface call);
167
168} // namespace cir
169
170#endif // CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_CIRABIREWRITECONTEXT_H
void rewriteFunctionAddress(cir::GetGlobalOp addrOp, cir::FuncOp funcOp, mlir::OpBuilder &builder)
Retype addrOp, which holds the address of funcOp, to the signature funcOp was rewritten to,...
mlir::LogicalResult rewriteFunctionDefinition(mlir::FunctionOpInterface funcOp, const mlir::abi::FunctionClassification &fc, mlir::OpBuilder &builder) override
void finalizeParameterSlots()
Replace each non-byval indirect parameter's spill slot with the incoming pointer, so the body operate...
mlir::StringRef getDialectNamespace() const override
CIRABIRewriteContext(mlir::ModuleOp module, const mlir::DataLayout &dl, cir::CallConvTarget target, bool enableNoundefAttrs)
mlir::LogicalResult prepareNonByvalParameters(cir::FuncOp funcOp, const mlir::abi::FunctionClassification &fc)
Bring each non-byval indirect parameter of funcOp into the shape the rest of the rewrite assumes: the...
mlir::LogicalResult rewriteVAArg(mlir::Operation *vaArgOp, const mlir::abi::ArgClassification &ac, mlir::OpBuilder &builder) override
Expand a cir.va_arg with the va_arg rules of the target this context lowers for.
mlir::LogicalResult rewriteCallSite(mlir::Operation *callOp, const mlir::abi::FunctionClassification &fc, mlir::OpBuilder &builder) override
void registerCIRAliasAnalyses(mlir::AliasAnalysis &aa, mlir::Operation *op)
Register all CIR alias analysis implementations with aa, which answers queries about values within op...
cir::FuncType getIndirectCalleeType(cir::CIRCallOpInterface call)
The signature the indirect call call reaches its callee through.
CallConvTarget
The ABI target whose calling-convention rules drive CallConvLowering.
Definition Passes.h:23