10b57cec5SDimitry Andric //===-- AMDGPUMachineFunctionInfo.cpp ---------------------------------------=//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "AMDGPUMachineFunction.h"
1081ad6265SDimitry Andric #include "AMDGPU.h"
110b57cec5SDimitry Andric #include "AMDGPUPerfHintAnalysis.h"
12e8d8bef9SDimitry Andric #include "AMDGPUSubtarget.h"
13fe013be4SDimitry Andric #include "Utils/AMDGPUBaseInfo.h"
140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
15fe013be4SDimitry Andric #include "llvm/IR/ConstantRange.h"
16fcaf7f86SDimitry Andric #include "llvm/IR/Constants.h"
17fe013be4SDimitry Andric #include "llvm/IR/Metadata.h"
18e8d8bef9SDimitry Andric #include "llvm/Target/TargetMachine.h"
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric
22*cdc20ff6SDimitry Andric static const GlobalVariable *
getKernelDynLDSGlobalFromFunction(const Function & F)23*cdc20ff6SDimitry Andric getKernelDynLDSGlobalFromFunction(const Function &F) {
24*cdc20ff6SDimitry Andric const Module *M = F.getParent();
25*cdc20ff6SDimitry Andric SmallString<64> KernelDynLDSName("llvm.amdgcn.");
26*cdc20ff6SDimitry Andric KernelDynLDSName += F.getName();
27*cdc20ff6SDimitry Andric KernelDynLDSName += ".dynlds";
28*cdc20ff6SDimitry Andric return M->getNamedGlobal(KernelDynLDSName);
29*cdc20ff6SDimitry Andric }
30*cdc20ff6SDimitry Andric
hasLDSKernelArgument(const Function & F)31*cdc20ff6SDimitry Andric static bool hasLDSKernelArgument(const Function &F) {
32*cdc20ff6SDimitry Andric for (const Argument &Arg : F.args()) {
33*cdc20ff6SDimitry Andric Type *ArgTy = Arg.getType();
34*cdc20ff6SDimitry Andric if (auto PtrTy = dyn_cast<PointerType>(ArgTy)) {
35*cdc20ff6SDimitry Andric if (PtrTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS)
36*cdc20ff6SDimitry Andric return true;
37*cdc20ff6SDimitry Andric }
38*cdc20ff6SDimitry Andric }
39*cdc20ff6SDimitry Andric return false;
40*cdc20ff6SDimitry Andric }
41*cdc20ff6SDimitry Andric
AMDGPUMachineFunction(const Function & F,const AMDGPUSubtarget & ST)42bdd1243dSDimitry Andric AMDGPUMachineFunction::AMDGPUMachineFunction(const Function &F,
43bdd1243dSDimitry Andric const AMDGPUSubtarget &ST)
44bdd1243dSDimitry Andric : IsEntryFunction(AMDGPU::isEntryFunctionCC(F.getCallingConv())),
45e8d8bef9SDimitry Andric IsModuleEntryFunction(
46bdd1243dSDimitry Andric AMDGPU::isModuleEntryFunctionCC(F.getCallingConv())),
47c9157d92SDimitry Andric IsChainFunction(AMDGPU::isChainCC(F.getCallingConv())),
48bdd1243dSDimitry Andric NoSignedZerosFPMath(false) {
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric // FIXME: Should initialize KernArgSize based on ExplicitKernelArgOffset,
510b57cec5SDimitry Andric // except reserved size is not correctly aligned.
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric Attribute MemBoundAttr = F.getFnAttribute("amdgpu-memory-bound");
54fe6060f1SDimitry Andric MemoryBound = MemBoundAttr.getValueAsBool();
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric Attribute WaveLimitAttr = F.getFnAttribute("amdgpu-wave-limiter");
57fe6060f1SDimitry Andric WaveLimiter = WaveLimitAttr.getValueAsBool();
580b57cec5SDimitry Andric
5981ad6265SDimitry Andric // FIXME: How is this attribute supposed to interact with statically known
6081ad6265SDimitry Andric // global sizes?
6181ad6265SDimitry Andric StringRef S = F.getFnAttribute("amdgpu-gds-size").getValueAsString();
6281ad6265SDimitry Andric if (!S.empty())
6381ad6265SDimitry Andric S.consumeInteger(0, GDSSize);
6481ad6265SDimitry Andric
6581ad6265SDimitry Andric // Assume the attribute allocates before any known GDS globals.
6681ad6265SDimitry Andric StaticGDSSize = GDSSize;
6781ad6265SDimitry Andric
68fe013be4SDimitry Andric // Second value, if present, is the maximum value that can be assigned.
69fe013be4SDimitry Andric // Useful in PromoteAlloca or for LDS spills. Could be used for diagnostics
70fe013be4SDimitry Andric // during codegen.
71fe013be4SDimitry Andric std::pair<unsigned, unsigned> LDSSizeRange = AMDGPU::getIntegerPairAttribute(
72fe013be4SDimitry Andric F, "amdgpu-lds-size", {0, UINT32_MAX}, true);
73fe013be4SDimitry Andric
74fe013be4SDimitry Andric // The two separate variables are only profitable when the LDS module lowering
75fe013be4SDimitry Andric // pass is disabled. If graphics does not use dynamic LDS, this is never
76fe013be4SDimitry Andric // profitable. Leaving cleanup for a later change.
77fe013be4SDimitry Andric LDSSize = LDSSizeRange.first;
78fe013be4SDimitry Andric StaticLDSSize = LDSSize;
79fe013be4SDimitry Andric
800b57cec5SDimitry Andric CallingConv::ID CC = F.getCallingConv();
810b57cec5SDimitry Andric if (CC == CallingConv::AMDGPU_KERNEL || CC == CallingConv::SPIR_KERNEL)
820b57cec5SDimitry Andric ExplicitKernArgSize = ST.getExplicitKernArgSize(F, MaxKernArgAlign);
83bdd1243dSDimitry Andric
84bdd1243dSDimitry Andric // FIXME: Shouldn't be target specific
85bdd1243dSDimitry Andric Attribute NSZAttr = F.getFnAttribute("no-signed-zeros-fp-math");
86bdd1243dSDimitry Andric NoSignedZerosFPMath =
87bdd1243dSDimitry Andric NSZAttr.isStringAttribute() && NSZAttr.getValueAsString() == "true";
88*cdc20ff6SDimitry Andric
89*cdc20ff6SDimitry Andric const GlobalVariable *DynLdsGlobal = getKernelDynLDSGlobalFromFunction(F);
90*cdc20ff6SDimitry Andric if (DynLdsGlobal || hasLDSKernelArgument(F))
91*cdc20ff6SDimitry Andric UsesDynamicLDS = true;
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric
allocateLDSGlobal(const DataLayout & DL,const GlobalVariable & GV,Align Trailing)940b57cec5SDimitry Andric unsigned AMDGPUMachineFunction::allocateLDSGlobal(const DataLayout &DL,
95bdd1243dSDimitry Andric const GlobalVariable &GV,
96bdd1243dSDimitry Andric Align Trailing) {
97bdd1243dSDimitry Andric auto Entry = LocalMemoryObjects.insert(std::pair(&GV, 0));
980b57cec5SDimitry Andric if (!Entry.second)
990b57cec5SDimitry Andric return Entry.first->second;
1000b57cec5SDimitry Andric
1015ffd83dbSDimitry Andric Align Alignment =
1025ffd83dbSDimitry Andric DL.getValueOrABITypeAlignment(GV.getAlign(), GV.getValueType());
1030b57cec5SDimitry Andric
10481ad6265SDimitry Andric unsigned Offset;
10581ad6265SDimitry Andric if (GV.getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
106fe013be4SDimitry Andric
107fe013be4SDimitry Andric std::optional<uint32_t> MaybeAbs = getLDSAbsoluteAddress(GV);
108fe013be4SDimitry Andric if (MaybeAbs) {
109fe013be4SDimitry Andric // Absolute address LDS variables that exist prior to the LDS lowering
110fe013be4SDimitry Andric // pass raise a fatal error in that pass. These failure modes are only
111fe013be4SDimitry Andric // reachable if that lowering pass is disabled or broken. If/when adding
112fe013be4SDimitry Andric // support for absolute addresses on user specified variables, the
113fe013be4SDimitry Andric // alignment check moves to the lowering pass and the frame calculation
114fe013be4SDimitry Andric // needs to take the user variables into consideration.
115fe013be4SDimitry Andric
116fe013be4SDimitry Andric uint32_t ObjectStart = *MaybeAbs;
117fe013be4SDimitry Andric
118fe013be4SDimitry Andric if (ObjectStart != alignTo(ObjectStart, Alignment)) {
119fe013be4SDimitry Andric report_fatal_error("Absolute address LDS variable inconsistent with "
120fe013be4SDimitry Andric "variable alignment");
121fe013be4SDimitry Andric }
122fe013be4SDimitry Andric
123fe013be4SDimitry Andric if (isModuleEntryFunction()) {
124fe013be4SDimitry Andric // If this is a module entry function, we can also sanity check against
125fe013be4SDimitry Andric // the static frame. Strictly it would be better to check against the
126fe013be4SDimitry Andric // attribute, i.e. that the variable is within the always-allocated
127fe013be4SDimitry Andric // section, and not within some other non-absolute-address object
128fe013be4SDimitry Andric // allocated here, but the extra error detection is minimal and we would
129fe013be4SDimitry Andric // have to pass the Function around or cache the attribute value.
130fe013be4SDimitry Andric uint32_t ObjectEnd =
131fe013be4SDimitry Andric ObjectStart + DL.getTypeAllocSize(GV.getValueType());
132fe013be4SDimitry Andric if (ObjectEnd > StaticLDSSize) {
133fe013be4SDimitry Andric report_fatal_error(
134fe013be4SDimitry Andric "Absolute address LDS variable outside of static frame");
135fe013be4SDimitry Andric }
136fe013be4SDimitry Andric }
137fe013be4SDimitry Andric
138fe013be4SDimitry Andric Entry.first->second = ObjectStart;
139fe013be4SDimitry Andric return ObjectStart;
140fe013be4SDimitry Andric }
141fe013be4SDimitry Andric
1420b57cec5SDimitry Andric /// TODO: We should sort these to minimize wasted space due to alignment
1430b57cec5SDimitry Andric /// padding. Currently the padding is decided by the first encountered use
1440b57cec5SDimitry Andric /// during lowering.
14581ad6265SDimitry Andric Offset = StaticLDSSize = alignTo(StaticLDSSize, Alignment);
1460b57cec5SDimitry Andric
147e8d8bef9SDimitry Andric StaticLDSSize += DL.getTypeAllocSize(GV.getValueType());
148e8d8bef9SDimitry Andric
149bdd1243dSDimitry Andric // Align LDS size to trailing, e.g. for aligning dynamic shared memory
150bdd1243dSDimitry Andric LDSSize = alignTo(StaticLDSSize, Trailing);
15181ad6265SDimitry Andric } else {
15281ad6265SDimitry Andric assert(GV.getAddressSpace() == AMDGPUAS::REGION_ADDRESS &&
15381ad6265SDimitry Andric "expected region address space");
1540b57cec5SDimitry Andric
15581ad6265SDimitry Andric Offset = StaticGDSSize = alignTo(StaticGDSSize, Alignment);
15681ad6265SDimitry Andric StaticGDSSize += DL.getTypeAllocSize(GV.getValueType());
15781ad6265SDimitry Andric
15881ad6265SDimitry Andric // FIXME: Apply alignment of dynamic GDS
15981ad6265SDimitry Andric GDSSize = StaticGDSSize;
16081ad6265SDimitry Andric }
16181ad6265SDimitry Andric
16281ad6265SDimitry Andric Entry.first->second = Offset;
1630b57cec5SDimitry Andric return Offset;
1640b57cec5SDimitry Andric }
165e8d8bef9SDimitry Andric
166bdd1243dSDimitry Andric std::optional<uint32_t>
getLDSKernelIdMetadata(const Function & F)167fcaf7f86SDimitry Andric AMDGPUMachineFunction::getLDSKernelIdMetadata(const Function &F) {
168fe013be4SDimitry Andric // TODO: Would be more consistent with the abs symbols to use a range
169fe013be4SDimitry Andric MDNode *MD = F.getMetadata("llvm.amdgcn.lds.kernel.id");
170fcaf7f86SDimitry Andric if (MD && MD->getNumOperands() == 1) {
171fe013be4SDimitry Andric if (ConstantInt *KnownSize =
172fe013be4SDimitry Andric mdconst::extract<ConstantInt>(MD->getOperand(0))) {
173fe013be4SDimitry Andric uint64_t ZExt = KnownSize->getZExtValue();
174fe013be4SDimitry Andric if (ZExt <= UINT32_MAX) {
175fe013be4SDimitry Andric return ZExt;
176fcaf7f86SDimitry Andric }
177fcaf7f86SDimitry Andric }
178fcaf7f86SDimitry Andric }
179fcaf7f86SDimitry Andric return {};
180fcaf7f86SDimitry Andric }
181fcaf7f86SDimitry Andric
182fe013be4SDimitry Andric std::optional<uint32_t>
getLDSAbsoluteAddress(const GlobalValue & GV)183fe013be4SDimitry Andric AMDGPUMachineFunction::getLDSAbsoluteAddress(const GlobalValue &GV) {
184fe013be4SDimitry Andric if (GV.getAddressSpace() != AMDGPUAS::LOCAL_ADDRESS)
185fe013be4SDimitry Andric return {};
186fe013be4SDimitry Andric
187fe013be4SDimitry Andric std::optional<ConstantRange> AbsSymRange = GV.getAbsoluteSymbolRange();
188fe013be4SDimitry Andric if (!AbsSymRange)
189fe013be4SDimitry Andric return {};
190fe013be4SDimitry Andric
191fe013be4SDimitry Andric if (const APInt *V = AbsSymRange->getSingleElement()) {
192fe013be4SDimitry Andric std::optional<uint64_t> ZExt = V->tryZExtValue();
193fe013be4SDimitry Andric if (ZExt && (*ZExt <= UINT32_MAX)) {
194fe013be4SDimitry Andric return *ZExt;
195fe013be4SDimitry Andric }
196fe013be4SDimitry Andric }
197fe013be4SDimitry Andric
198fe013be4SDimitry Andric return {};
199fe013be4SDimitry Andric }
200fe013be4SDimitry Andric
setDynLDSAlign(const Function & F,const GlobalVariable & GV)201fe013be4SDimitry Andric void AMDGPUMachineFunction::setDynLDSAlign(const Function &F,
202e8d8bef9SDimitry Andric const GlobalVariable &GV) {
203fe013be4SDimitry Andric const Module *M = F.getParent();
204fe013be4SDimitry Andric const DataLayout &DL = M->getDataLayout();
205e8d8bef9SDimitry Andric assert(DL.getTypeAllocSize(GV.getValueType()).isZero());
206e8d8bef9SDimitry Andric
207e8d8bef9SDimitry Andric Align Alignment =
208e8d8bef9SDimitry Andric DL.getValueOrABITypeAlignment(GV.getAlign(), GV.getValueType());
209e8d8bef9SDimitry Andric if (Alignment <= DynLDSAlign)
210e8d8bef9SDimitry Andric return;
211e8d8bef9SDimitry Andric
212e8d8bef9SDimitry Andric LDSSize = alignTo(StaticLDSSize, Alignment);
213e8d8bef9SDimitry Andric DynLDSAlign = Alignment;
214fe013be4SDimitry Andric
215fe013be4SDimitry Andric // If there is a dynamic LDS variable associated with this function F, every
216fe013be4SDimitry Andric // further dynamic LDS instance (allocated by calling setDynLDSAlign) must
217fe013be4SDimitry Andric // map to the same address. This holds because no LDS is allocated after the
218fe013be4SDimitry Andric // lowering pass if there are dynamic LDS variables present.
219fe013be4SDimitry Andric const GlobalVariable *Dyn = getKernelDynLDSGlobalFromFunction(F);
220fe013be4SDimitry Andric if (Dyn) {
221fe013be4SDimitry Andric unsigned Offset = LDSSize; // return this?
222fe013be4SDimitry Andric std::optional<uint32_t> Expect = getLDSAbsoluteAddress(*Dyn);
223fe013be4SDimitry Andric if (!Expect || (Offset != *Expect)) {
224fe013be4SDimitry Andric report_fatal_error("Inconsistent metadata on dynamic LDS variable");
225fe013be4SDimitry Andric }
226fe013be4SDimitry Andric }
227e8d8bef9SDimitry Andric }
228*cdc20ff6SDimitry Andric
setUsesDynamicLDS(bool DynLDS)229*cdc20ff6SDimitry Andric void AMDGPUMachineFunction::setUsesDynamicLDS(bool DynLDS) {
230*cdc20ff6SDimitry Andric UsesDynamicLDS = DynLDS;
231*cdc20ff6SDimitry Andric }
232*cdc20ff6SDimitry Andric
isDynamicLDSUsed() const233*cdc20ff6SDimitry Andric bool AMDGPUMachineFunction::isDynamicLDSUsed() const { return UsesDynamicLDS; }
234