LLVM 19.0.0git
AMDGPUMCExpr.cpp
Go to the documentation of this file.
1//===- AMDGPUMCExpr.cpp - AMDGPU specific MC expression classes -----------===//
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#include "AMDGPUMCExpr.h"
10#include "GCNSubtarget.h"
12#include "llvm/IR/Function.h"
13#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCStreamer.h"
15#include "llvm/MC/MCSymbol.h"
16#include "llvm/MC/MCValue.h"
19#include <optional>
20
21using namespace llvm;
22using namespace llvm::AMDGPU;
23
24AMDGPUMCExpr::AMDGPUMCExpr(VariantKind Kind, ArrayRef<const MCExpr *> Args,
25 MCContext &Ctx)
26 : Kind(Kind), Ctx(Ctx) {
27 assert(Args.size() >= 1 && "Needs a minimum of one expression.");
28 assert(Kind != AGVK_None && "Cannot construct AMDGPUMCExpr of kind none.");
29
30 // Allocating the variadic arguments through the same allocation mechanism
31 // that the object itself is allocated with so they end up in the same memory.
32 //
33 // Will result in an asan failure if allocated on the heap through standard
34 // allocation (e.g., through SmallVector's grow).
35 RawArgs = static_cast<const MCExpr **>(
36 Ctx.allocate(sizeof(const MCExpr *) * Args.size()));
37 std::uninitialized_copy(Args.begin(), Args.end(), RawArgs);
38 this->Args = ArrayRef<const MCExpr *>(RawArgs, Args.size());
39}
40
41AMDGPUMCExpr::~AMDGPUMCExpr() { Ctx.deallocate(RawArgs); }
42
45 MCContext &Ctx) {
46 return new (Ctx) AMDGPUMCExpr(Kind, Args, Ctx);
47}
48
50 assert(Index < Args.size() && "Indexing out of bounds AMDGPUMCExpr sub-expr");
51 return Args[Index];
52}
53
55 switch (Kind) {
56 default:
57 llvm_unreachable("Unknown AMDGPUMCExpr kind.");
58 case AGVK_Or:
59 OS << "or(";
60 break;
61 case AGVK_Max:
62 OS << "max(";
63 break;
64 case AGVK_ExtraSGPRs:
65 OS << "extrasgprs(";
66 break;
68 OS << "totalnumvgprs(";
69 break;
70 case AGVK_AlignTo:
71 OS << "alignto(";
72 break;
73 case AGVK_Occupancy:
74 OS << "occupancy(";
75 break;
76 }
77 for (auto It = Args.begin(); It != Args.end(); ++It) {
78 (*It)->print(OS, MAI, /*InParens=*/false);
79 if ((It + 1) != Args.end())
80 OS << ", ";
81 }
82 OS << ')';
83}
84
85static int64_t op(AMDGPUMCExpr::VariantKind Kind, int64_t Arg1, int64_t Arg2) {
86 switch (Kind) {
87 default:
88 llvm_unreachable("Unknown AMDGPUMCExpr kind.");
90 return std::max(Arg1, Arg2);
92 return Arg1 | Arg2;
93 }
94}
95
96bool AMDGPUMCExpr::evaluateExtraSGPRs(MCValue &Res, const MCAsmLayout *Layout,
97 const MCFixup *Fixup) const {
98 auto TryGetMCExprValue = [&](const MCExpr *Arg, uint64_t &ConstantValue) {
99 MCValue MCVal;
100 if (!Arg->evaluateAsRelocatable(MCVal, Layout, Fixup) ||
101 !MCVal.isAbsolute())
102 return false;
103
104 ConstantValue = MCVal.getConstant();
105 return true;
106 };
107
108 assert(Args.size() == 3 &&
109 "AMDGPUMCExpr Argument count incorrect for ExtraSGPRs");
110 const MCSubtargetInfo *STI = Ctx.getSubtargetInfo();
111 uint64_t VCCUsed = 0, FlatScrUsed = 0, XNACKUsed = 0;
112
113 bool Success = TryGetMCExprValue(Args[2], XNACKUsed);
114
115 assert(Success && "Arguments 3 for ExtraSGPRs should be a known constant");
116 if (!Success || !TryGetMCExprValue(Args[0], VCCUsed) ||
117 !TryGetMCExprValue(Args[1], FlatScrUsed))
118 return false;
119
121 STI, (bool)VCCUsed, (bool)FlatScrUsed, (bool)XNACKUsed);
122 Res = MCValue::get(ExtraSGPRs);
123 return true;
124}
125
126bool AMDGPUMCExpr::evaluateTotalNumVGPR(MCValue &Res, const MCAsmLayout *Layout,
127 const MCFixup *Fixup) const {
128 auto TryGetMCExprValue = [&](const MCExpr *Arg, uint64_t &ConstantValue) {
129 MCValue MCVal;
130 if (!Arg->evaluateAsRelocatable(MCVal, Layout, Fixup) ||
131 !MCVal.isAbsolute())
132 return false;
133
134 ConstantValue = MCVal.getConstant();
135 return true;
136 };
137 assert(Args.size() == 2 &&
138 "AMDGPUMCExpr Argument count incorrect for TotalNumVGPRs");
139 const MCSubtargetInfo *STI = Ctx.getSubtargetInfo();
140 uint64_t NumAGPR = 0, NumVGPR = 0;
141
142 bool Has90AInsts = AMDGPU::isGFX90A(*STI);
143
144 if (!TryGetMCExprValue(Args[0], NumAGPR) ||
145 !TryGetMCExprValue(Args[1], NumVGPR))
146 return false;
147
148 uint64_t TotalNum = Has90AInsts && NumAGPR ? alignTo(NumVGPR, 4) + NumAGPR
149 : std::max(NumVGPR, NumAGPR);
150 Res = MCValue::get(TotalNum);
151 return true;
152}
153
154bool AMDGPUMCExpr::evaluateAlignTo(MCValue &Res, const MCAsmLayout *Layout,
155 const MCFixup *Fixup) const {
156 auto TryGetMCExprValue = [&](const MCExpr *Arg, uint64_t &ConstantValue) {
157 MCValue MCVal;
158 if (!Arg->evaluateAsRelocatable(MCVal, Layout, Fixup) ||
159 !MCVal.isAbsolute())
160 return false;
161
162 ConstantValue = MCVal.getConstant();
163 return true;
164 };
165
166 assert(Args.size() == 2 &&
167 "AMDGPUMCExpr Argument count incorrect for AlignTo");
168 uint64_t Value = 0, Align = 0;
169 if (!TryGetMCExprValue(Args[0], Value) || !TryGetMCExprValue(Args[1], Align))
170 return false;
171
173 return true;
174}
175
176bool AMDGPUMCExpr::evaluateOccupancy(MCValue &Res, const MCAsmLayout *Layout,
177 const MCFixup *Fixup) const {
178 auto TryGetMCExprValue = [&](const MCExpr *Arg, uint64_t &ConstantValue) {
179 MCValue MCVal;
180 if (!Arg->evaluateAsRelocatable(MCVal, Layout, Fixup) ||
181 !MCVal.isAbsolute())
182 return false;
183
184 ConstantValue = MCVal.getConstant();
185 return true;
186 };
187 assert(Args.size() == 7 &&
188 "AMDGPUMCExpr Argument count incorrect for Occupancy");
189 uint64_t InitOccupancy, MaxWaves, Granule, TargetTotalNumVGPRs, Generation,
191
192 bool Success = true;
193 Success &= TryGetMCExprValue(Args[0], MaxWaves);
194 Success &= TryGetMCExprValue(Args[1], Granule);
195 Success &= TryGetMCExprValue(Args[2], TargetTotalNumVGPRs);
196 Success &= TryGetMCExprValue(Args[3], Generation);
197 Success &= TryGetMCExprValue(Args[4], InitOccupancy);
198
199 assert(Success && "Arguments 1 to 5 for Occupancy should be known constants");
200
201 if (!Success || !TryGetMCExprValue(Args[5], NumSGPRs) ||
202 !TryGetMCExprValue(Args[6], NumVGPRs))
203 return false;
204
205 unsigned Occupancy = InitOccupancy;
206 if (NumSGPRs)
207 Occupancy = std::min(
209 NumSGPRs, MaxWaves,
210 static_cast<AMDGPUSubtarget::Generation>(Generation)));
211 if (NumVGPRs)
212 Occupancy = std::min(Occupancy,
214 NumVGPRs, Granule, MaxWaves, TargetTotalNumVGPRs));
215
216 Res = MCValue::get(Occupancy);
217 return true;
218}
219
221 const MCAsmLayout *Layout,
222 const MCFixup *Fixup) const {
223 std::optional<int64_t> Total;
224
225 switch (Kind) {
226 default:
227 break;
228 case AGVK_ExtraSGPRs:
229 return evaluateExtraSGPRs(Res, Layout, Fixup);
230 case AGVK_AlignTo:
231 return evaluateAlignTo(Res, Layout, Fixup);
233 return evaluateTotalNumVGPR(Res, Layout, Fixup);
234 case AGVK_Occupancy:
235 return evaluateOccupancy(Res, Layout, Fixup);
236 }
237
238 for (const MCExpr *Arg : Args) {
239 MCValue ArgRes;
240 if (!Arg->evaluateAsRelocatable(ArgRes, Layout, Fixup) ||
241 !ArgRes.isAbsolute())
242 return false;
243
244 if (!Total.has_value())
245 Total = ArgRes.getConstant();
246 Total = op(Kind, *Total, ArgRes.getConstant());
247 }
248
249 Res = MCValue::get(*Total);
250 return true;
251}
252
254 for (const MCExpr *Arg : Args)
255 Streamer.visitUsedExpr(*Arg);
256}
257
259 for (const MCExpr *Arg : Args) {
260 if (Arg->findAssociatedFragment())
261 return Arg->findAssociatedFragment();
262 }
263 return nullptr;
264}
265
266/// Allow delayed MCExpr resolve of ExtraSGPRs (in case VCCUsed or FlatScrUsed
267/// are unresolvable but needed for further MCExprs). Derived from
268/// implementation of IsaInfo::getNumExtraSGPRs in AMDGPUBaseInfo.cpp.
269///
271 const MCExpr *FlatScrUsed,
272 bool XNACKUsed,
273 MCContext &Ctx) {
274
275 return create(AGVK_ExtraSGPRs,
276 {VCCUsed, FlatScrUsed, MCConstantExpr::create(XNACKUsed, Ctx)},
277 Ctx);
278}
279
281 const MCExpr *NumVGPR,
282 MCContext &Ctx) {
283 return create(AGVK_TotalNumVGPRs, {NumAGPR, NumVGPR}, Ctx);
284}
285
286/// Mimics GCNSubtarget::computeOccupancy for MCExpr.
287///
288/// Remove dependency on GCNSubtarget and depend only only the necessary values
289/// for said occupancy computation. Should match computeOccupancy implementation
290/// without passing \p STM on.
292 const MCExpr *NumSGPRs,
293 const MCExpr *NumVGPRs,
294 const GCNSubtarget &STM,
295 MCContext &Ctx) {
296 unsigned MaxWaves = IsaInfo::getMaxWavesPerEU(&STM);
297 unsigned Granule = IsaInfo::getVGPRAllocGranule(&STM);
298 unsigned TargetTotalNumVGPRs = IsaInfo::getTotalNumVGPRs(&STM);
299 unsigned Generation = STM.getGeneration();
300
301 auto CreateExpr = [&Ctx](unsigned Value) {
302 return MCConstantExpr::create(Value, Ctx);
303 };
304
305 return create(AGVK_Occupancy,
306 {CreateExpr(MaxWaves), CreateExpr(Granule),
307 CreateExpr(TargetTotalNumVGPRs), CreateExpr(Generation),
308 CreateExpr(InitOcc), NumSGPRs, NumVGPRs},
309 Ctx);
310}
#define Success
This file defines the BumpPtrAllocator interface.
AMD GCN specific subclass of TargetSubtarget.
#define op(i)
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
AMDGPU target specific MCExpr operations.
Definition: AMDGPUMCExpr.h:30
MCFragment * findAssociatedFragment() const override
void visitUsedExpr(MCStreamer &Streamer) const override
static const AMDGPUMCExpr * createOccupancy(unsigned InitOcc, const MCExpr *NumSGPRs, const MCExpr *NumVGPRs, const GCNSubtarget &STM, MCContext &Ctx)
Mimics GCNSubtarget::computeOccupancy for MCExpr.
static const AMDGPUMCExpr * createTotalNumVGPR(const MCExpr *NumAGPR, const MCExpr *NumVGPR, MCContext &Ctx)
static const AMDGPUMCExpr * create(VariantKind Kind, ArrayRef< const MCExpr * > Args, MCContext &Ctx)
static const AMDGPUMCExpr * createExtraSGPRs(const MCExpr *VCCUsed, const MCExpr *FlatScrUsed, bool XNACKUsed, MCContext &Ctx)
Allow delayed MCExpr resolve of ExtraSGPRs (in case VCCUsed or FlatScrUsed are unresolvable but neede...
const MCExpr * getSubExpr(size_t Index) const
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Generation getGeneration() const
Definition: GCNSubtarget.h:313
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:83
void * allocate(unsigned Size, unsigned Align=8)
Definition: MCContext.h:812
void deallocate(void *Ptr)
Definition: MCContext.h:816
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCContext.h:418
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:798
MCFragment * findAssociatedFragment() const
Find the "associated section" for this expression, which is currently defined as the absolute section...
Definition: MCExpr.cpp:1042
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Streaming machine code generation interface.
Definition: MCStreamer.h:213
void visitUsedExpr(const MCExpr &Expr)
Generic base class for all target subtargets.
This represents an "assembler immediate".
Definition: MCValue.h:36
int64_t getConstant() const
Definition: MCValue.h:43
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:59
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:49
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char NumVGPRs[]
Key for Kernel::CodeProps::Metadata::mNumVGPRs.
constexpr char NumSGPRs[]
Key for Kernel::CodeProps::Metadata::mNumSGPRs.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI)
unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI)
unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed, bool FlatScrUsed, bool XNACKUsed)
unsigned getNumWavesPerEUWithNumVGPRs(const MCSubtargetInfo *STI, unsigned NumVGPRs)
unsigned getOccupancyWithNumSGPRs(unsigned SGPRs, unsigned MaxWaves, AMDGPUSubtarget::Generation Gen)
unsigned getVGPRAllocGranule(const MCSubtargetInfo *STI, std::optional< bool > EnableWavefrontSize32)
bool isGFX90A(const MCSubtargetInfo &STI)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39