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 // This kernel calls no functions that require the module lds struct 87 static bool canElideModuleLDS(const Function &F) { 88 return F.hasFnAttribute("amdgpu-elide-module-lds"); 89 } 90 91 void AMDGPUMachineFunction::allocateModuleLDSGlobal(const Function &F) { 92 const Module *M = F.getParent(); 93 if (isModuleEntryFunction()) { 94 const GlobalVariable *GV = M->getNamedGlobal("llvm.amdgcn.module.lds"); 95 if (GV && !canElideModuleLDS(F)) { 96 unsigned Offset = allocateLDSGlobal(M->getDataLayout(), *GV); 97 (void)Offset; 98 assert(Offset == 0 && 99 "Module LDS expected to be allocated before other LDS"); 100 } 101 } 102 } 103 104 void AMDGPUMachineFunction::setDynLDSAlign(const DataLayout &DL, 105 const GlobalVariable &GV) { 106 assert(DL.getTypeAllocSize(GV.getValueType()).isZero()); 107 108 Align Alignment = 109 DL.getValueOrABITypeAlignment(GV.getAlign(), GV.getValueType()); 110 if (Alignment <= DynLDSAlign) 111 return; 112 113 LDSSize = alignTo(StaticLDSSize, Alignment); 114 DynLDSAlign = Alignment; 115 } 116