xref: /llvm-project/llvm/lib/Target/AMDGPU/AMDGPUMachineFunction.cpp (revision 7030654296a0416bd9402a0278dbd42f1bf268b2)
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 "AMDGPU.h"
11 #include "AMDGPUPerfHintAnalysis.h"
12 #include "AMDGPUSubtarget.h"
13 #include "llvm/CodeGen/MachineModuleInfo.h"
14 #include "llvm/Target/TargetMachine.h"
15 
16 using namespace llvm;
17 
18 AMDGPUMachineFunction::AMDGPUMachineFunction(const MachineFunction &MF)
19     : Mode(MF.getFunction()), IsEntryFunction(AMDGPU::isEntryFunctionCC(
20                                   MF.getFunction().getCallingConv())),
21       IsModuleEntryFunction(
22           AMDGPU::isModuleEntryFunctionCC(MF.getFunction().getCallingConv())),
23       NoSignedZerosFPMath(MF.getTarget().Options.NoSignedZerosFPMath) {
24   const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(MF);
25 
26   // FIXME: Should initialize KernArgSize based on ExplicitKernelArgOffset,
27   // except reserved size is not correctly aligned.
28   const Function &F = MF.getFunction();
29 
30   Attribute MemBoundAttr = F.getFnAttribute("amdgpu-memory-bound");
31   MemoryBound = MemBoundAttr.getValueAsBool();
32 
33   Attribute WaveLimitAttr = F.getFnAttribute("amdgpu-wave-limiter");
34   WaveLimiter = WaveLimitAttr.getValueAsBool();
35 
36   // FIXME: How is this attribute supposed to interact with statically known
37   // global sizes?
38   StringRef S = F.getFnAttribute("amdgpu-gds-size").getValueAsString();
39   if (!S.empty())
40     S.consumeInteger(0, GDSSize);
41 
42   // Assume the attribute allocates before any known GDS globals.
43   StaticGDSSize = GDSSize;
44 
45   CallingConv::ID CC = F.getCallingConv();
46   if (CC == CallingConv::AMDGPU_KERNEL || CC == CallingConv::SPIR_KERNEL)
47     ExplicitKernArgSize = ST.getExplicitKernArgSize(F, MaxKernArgAlign);
48 }
49 
50 unsigned AMDGPUMachineFunction::allocateLDSGlobal(const DataLayout &DL,
51                                                   const GlobalVariable &GV) {
52   auto Entry = LocalMemoryObjects.insert(std::make_pair(&GV, 0));
53   if (!Entry.second)
54     return Entry.first->second;
55 
56   Align Alignment =
57       DL.getValueOrABITypeAlignment(GV.getAlign(), GV.getValueType());
58 
59   unsigned Offset;
60   if (GV.getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
61     /// TODO: We should sort these to minimize wasted space due to alignment
62     /// padding. Currently the padding is decided by the first encountered use
63     /// during lowering.
64     Offset = StaticLDSSize = alignTo(StaticLDSSize, Alignment);
65 
66     StaticLDSSize += DL.getTypeAllocSize(GV.getValueType());
67 
68     // Update the LDS size considering the padding to align the dynamic shared
69     // memory.
70     LDSSize = alignTo(StaticLDSSize, DynLDSAlign);
71   } else {
72     assert(GV.getAddressSpace() == AMDGPUAS::REGION_ADDRESS &&
73            "expected region address space");
74 
75     Offset = StaticGDSSize = alignTo(StaticGDSSize, Alignment);
76     StaticGDSSize += DL.getTypeAllocSize(GV.getValueType());
77 
78     // FIXME: Apply alignment of dynamic GDS
79     GDSSize = StaticGDSSize;
80   }
81 
82   Entry.first->second = Offset;
83   return Offset;
84 }
85 
86 void AMDGPUMachineFunction::allocateModuleLDSGlobal(const Module *M) {
87   if (isModuleEntryFunction()) {
88     const GlobalVariable *GV = M->getNamedGlobal("llvm.amdgcn.module.lds");
89     if (GV) {
90       unsigned Offset = allocateLDSGlobal(M->getDataLayout(), *GV);
91       (void)Offset;
92       assert(Offset == 0 &&
93              "Module LDS expected to be allocated before other LDS");
94     }
95   }
96 }
97 
98 void AMDGPUMachineFunction::setDynLDSAlign(const DataLayout &DL,
99                                            const GlobalVariable &GV) {
100   assert(DL.getTypeAllocSize(GV.getValueType()).isZero());
101 
102   Align Alignment =
103       DL.getValueOrABITypeAlignment(GV.getAlign(), GV.getValueType());
104   if (Alignment <= DynLDSAlign)
105     return;
106 
107   LDSSize = alignTo(StaticLDSSize, Alignment);
108   DynLDSAlign = Alignment;
109 }
110