1 //===-- AMDGPUMachineFunctionInfo.cpp ---------------------------------------=//
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 "AMDGPUMachineFunction.h"
10 #include "AMDGPUSubtarget.h"
11 #include "AMDGPUPerfHintAnalysis.h"
12 #include "llvm/CodeGen/MachineModuleInfo.h"
13 
14 using namespace llvm;
15 
AMDGPUMachineFunction(const MachineFunction & MF)16 AMDGPUMachineFunction::AMDGPUMachineFunction(const MachineFunction &MF) :
17   MachineFunctionInfo(),
18   Mode(MF.getFunction()),
19   IsEntryFunction(AMDGPU::isEntryFunctionCC(MF.getFunction().getCallingConv())),
20   NoSignedZerosFPMath(MF.getTarget().Options.NoSignedZerosFPMath) {
21   const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(MF);
22 
23   // FIXME: Should initialize KernArgSize based on ExplicitKernelArgOffset,
24   // except reserved size is not correctly aligned.
25   const Function &F = MF.getFunction();
26 
27   Attribute MemBoundAttr = F.getFnAttribute("amdgpu-memory-bound");
28   MemoryBound = MemBoundAttr.isStringAttribute() &&
29                 MemBoundAttr.getValueAsString() == "true";
30 
31   Attribute WaveLimitAttr = F.getFnAttribute("amdgpu-wave-limiter");
32   WaveLimiter = WaveLimitAttr.isStringAttribute() &&
33                 WaveLimitAttr.getValueAsString() == "true";
34 
35   CallingConv::ID CC = F.getCallingConv();
36   if (CC == CallingConv::AMDGPU_KERNEL || CC == CallingConv::SPIR_KERNEL)
37     ExplicitKernArgSize = ST.getExplicitKernArgSize(F, MaxKernArgAlign);
38 }
39 
allocateLDSGlobal(const DataLayout & DL,const GlobalVariable & GV)40 unsigned AMDGPUMachineFunction::allocateLDSGlobal(const DataLayout &DL,
41                                                   const GlobalVariable &GV) {
42   auto Entry = LocalMemoryObjects.insert(std::make_pair(&GV, 0));
43   if (!Entry.second)
44     return Entry.first->second;
45 
46   Align Alignment =
47       DL.getValueOrABITypeAlignment(GV.getAlign(), GV.getValueType());
48 
49   /// TODO: We should sort these to minimize wasted space due to alignment
50   /// padding. Currently the padding is decided by the first encountered use
51   /// during lowering.
52   unsigned Offset = StaticLDSSize = alignTo(StaticLDSSize, Alignment);
53 
54   Entry.first->second = Offset;
55   StaticLDSSize += DL.getTypeAllocSize(GV.getValueType());
56 
57   // Update the LDS size considering the padding to align the dynamic shared
58   // memory.
59   LDSSize = alignTo(StaticLDSSize, DynLDSAlign);
60 
61   return Offset;
62 }
63 
setDynLDSAlign(const DataLayout & DL,const GlobalVariable & GV)64 void AMDGPUMachineFunction::setDynLDSAlign(const DataLayout &DL,
65                                            const GlobalVariable &GV) {
66   assert(DL.getTypeAllocSize(GV.getValueType()).isZero());
67 
68   Align Alignment =
69       DL.getValueOrABITypeAlignment(GV.getAlign(), GV.getValueType());
70   if (Alignment <= DynLDSAlign)
71     return;
72 
73   LDSSize = alignTo(StaticLDSSize, Alignment);
74   DynLDSAlign = Alignment;
75 }
76