| 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" |
| 27 | #include "clang/CIR/Dialect/Analysis/CIRAliasAnalysis.h" |
| 28 | #include "clang/CIR/Dialect/IR/CIRDataLayout.h" |
| 29 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 30 | #include "llvm/ADT/SmallVector.h" |
| 31 | #include <cassert> |
| 32 | #include <utility> |
| 33 | |
| 34 | namespace cir { |
| 35 | |
| 36 | /// CIR-specific implementation of mlir::abi::ABIRewriteContext. |
| 37 | /// |
| 38 | /// The driver pass (CallConvLoweringPass) computes a FunctionClassification |
| 39 | /// for each cir.func / cir.call and dispatches to this class to perform the |
| 40 | /// actual IR rewriting using cir dialect operations. |
| 41 | /// |
| 42 | /// Holds a reference to the module's DataLayout for coercion alignment |
| 43 | /// queries. The DataLayout must outlive the rewrite context. |
| 44 | class CIRABIRewriteContext : public mlir::abi::ABIRewriteContext { |
| 45 | public: |
| 46 | CIRABIRewriteContext(mlir::ModuleOp module, const mlir::DataLayout &dl, |
| 47 | bool enableNoundefAttrs) |
| 48 | : module(module), dl(dl), enableNoundefAttrs(enableNoundefAttrs), |
| 49 | cirDataLayout(module), aliasAnalysis(module) { |
| 50 | cir::registerCIRAliasAnalyses(aa&: aliasAnalysis, op: module); |
| 51 | } |
| 52 | |
| 53 | ~CIRABIRewriteContext() { |
| 54 | assert(pendingParamSlots.empty() && |
| 55 | "finalizeParameterSlots must run before the rewrite context dies" ); |
| 56 | } |
| 57 | |
| 58 | mlir::LogicalResult |
| 59 | rewriteFunctionDefinition(mlir::FunctionOpInterface funcOp, |
| 60 | const mlir::abi::FunctionClassification &fc, |
| 61 | mlir::OpBuilder &builder) override; |
| 62 | |
| 63 | mlir::LogicalResult |
| 64 | rewriteCallSite(mlir::Operation *callOp, |
| 65 | const mlir::abi::FunctionClassification &fc, |
| 66 | mlir::OpBuilder &builder) override; |
| 67 | |
| 68 | /// Expand a `cir.va_arg` into the x86-64 SysV register-save-area / |
| 69 | /// overflow-area sequence. |
| 70 | mlir::LogicalResult rewriteVAArg(mlir::Operation *vaArgOp, |
| 71 | const mlir::abi::ArgClassification &ac, |
| 72 | mlir::OpBuilder &builder) override; |
| 73 | |
| 74 | /// Retype \p addrOp, which holds the address of \p funcOp, to the signature |
| 75 | /// funcOp was rewritten to, and cast it back so existing uses keep the type |
| 76 | /// they were built for. A no-op when the ABI left funcOp's type alone. |
| 77 | /// Call after funcOp has been rewritten. Not an override, since taking a |
| 78 | /// function's address has no counterpart in the generic contract. |
| 79 | void rewriteFunctionAddress(cir::GetGlobalOp addrOp, cir::FuncOp funcOp, |
| 80 | mlir::OpBuilder &builder); |
| 81 | |
| 82 | /// Bring each non-byval indirect parameter of \p funcOp into the shape the |
| 83 | /// rest of the rewrite assumes: the parameter's only use, if it has one, is |
| 84 | /// a single store into an alloca of the incoming pointer's type, directly or |
| 85 | /// through an address_space cast of it. No other non-byval indirect |
| 86 | /// parameter spills to that alloca, and it states the alignment the ABI |
| 87 | /// promises rather than the one CIRGen picked for a local copy. A use of |
| 88 | /// the parameter as a call argument is routed through a load of that slot, |
| 89 | /// so that it names the storage an argument has to name. |
| 90 | /// |
| 91 | /// Call for every function before any definition or call site is rewritten. |
| 92 | /// findParamSpill asserts this shape while the enclosing definition is |
| 93 | /// rewritten, and a call is rewritten with its callee rather than with its |
| 94 | /// enclosing function and so may be reached first. |
| 95 | /// |
| 96 | /// A parameter read with no spill to name is given one, since it becomes |
| 97 | /// the incoming pointer directly. A parameter spilled twice, spilled to a |
| 98 | /// slot another such parameter also spills to, consumed other than as a |
| 99 | /// call argument, spilled where the incoming pointer cannot replace the |
| 100 | /// storage, or read where the spill does not dominate it gets a diagnostic |
| 101 | /// on the operation at fault and failure. |
| 102 | /// |
| 103 | /// Not an override, since this has no counterpart in the generic contract. |
| 104 | mlir::LogicalResult |
| 105 | prepareNonByvalParameters(cir::FuncOp funcOp, |
| 106 | const mlir::abi::FunctionClassification &fc); |
| 107 | |
| 108 | /// Replace each non-byval indirect parameter's spill slot with the |
| 109 | /// incoming pointer, so the body operates on the caller's storage in place. |
| 110 | /// Call once, after every function and call site has been rewritten: a call |
| 111 | /// forwarding such a parameter reads the slot to recognize it. Not an |
| 112 | /// override, since deferring this has no counterpart in the generic |
| 113 | /// contract. |
| 114 | void finalizeParameterSlots(); |
| 115 | |
| 116 | mlir::StringRef getDialectNamespace() const override { return "cir" ; } |
| 117 | |
| 118 | private: |
| 119 | mlir::ModuleOp module; |
| 120 | const mlir::DataLayout &dl; |
| 121 | |
| 122 | /// Whether extended and indirect arguments are marked llvm.noundef. Set |
| 123 | /// from the enable_noundef_attrs field of the module's |
| 124 | /// #cir.lowering_options. |
| 125 | bool enableNoundefAttrs; |
| 126 | |
| 127 | /// The CIR view of the module's data layout, which provides the store and |
| 128 | /// alloc sizes that \c dl does not. |
| 129 | cir::CIRDataLayout cirDataLayout; |
| 130 | |
| 131 | /// Alias analysis over the module with the CIR implementations registered. |
| 132 | /// rewriteCallSite asks it whether a byval operand's storage is written |
| 133 | /// between its load and the call. |
| 134 | mlir::AliasAnalysis aliasAnalysis; |
| 135 | |
| 136 | /// Param-slot allocas that non-byval indirect parameters will |
| 137 | /// replace, paired with the incoming pointer that replaces them. The |
| 138 | /// rewrite retypes the block argument but leaves the slot standing, because |
| 139 | /// a call site recognizes a forwardable parameter by the slot its operand |
| 140 | /// was loaded from. finalizeParameterSlots does the replacement once every |
| 141 | /// call site has been rewritten. |
| 142 | llvm::SmallVector<std::pair<cir::AllocaOp, mlir::BlockArgument>> |
| 143 | pendingParamSlots; |
| 144 | }; |
| 145 | |
| 146 | /// The signature the indirect call \p call reaches its callee through. |
| 147 | cir::FuncType getIndirectCalleeType(cir::CIRCallOpInterface call); |
| 148 | |
| 149 | } // namespace cir |
| 150 | |
| 151 | #endif // CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_CIRABIREWRITECONTEXT_H |
| 152 | |