Skip to content

Commit d19187f

Browse files
authored
[AMDGPU] Move into SIProgramInfo and cache getFunctionCodeSize. NFCI. (#127111)
This moves function as is, improvements to the estimate go into a subseqent patch.
1 parent 51c9109 commit d19187f

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
748748
RI.getSymbol(CurrentFnSym->getName(), RIK::RIK_PrivateSegSize,
749749
OutContext, IsLocal)
750750
->getVariableValue(),
751-
getFunctionCodeSize(MF), MFI);
751+
CurrentProgramInfo.getFunctionCodeSize(MF), MFI);
752752
return false;
753753
}
754754

@@ -757,7 +757,8 @@ bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
757757
CurrentProgramInfo.NumArchVGPR,
758758
STM.hasMAIInsts() ? CurrentProgramInfo.NumAccVGPR : nullptr,
759759
CurrentProgramInfo.NumVGPR, CurrentProgramInfo.NumSGPR,
760-
CurrentProgramInfo.ScratchSize, getFunctionCodeSize(MF), MFI);
760+
CurrentProgramInfo.ScratchSize,
761+
CurrentProgramInfo.getFunctionCodeSize(MF), MFI);
761762

762763
OutStreamer->emitRawComment(
763764
" FloatMode: " + Twine(CurrentProgramInfo.FloatMode), false);
@@ -893,27 +894,6 @@ void AMDGPUAsmPrinter::initializeTargetID(const Module &M) {
893894
}
894895
}
895896

896-
uint64_t AMDGPUAsmPrinter::getFunctionCodeSize(const MachineFunction &MF) const {
897-
const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
898-
const SIInstrInfo *TII = STM.getInstrInfo();
899-
900-
uint64_t CodeSize = 0;
901-
902-
for (const MachineBasicBlock &MBB : MF) {
903-
for (const MachineInstr &MI : MBB) {
904-
// TODO: CodeSize should account for multiple functions.
905-
906-
// TODO: Should we count size of debug info?
907-
if (MI.isDebugInstr())
908-
continue;
909-
910-
CodeSize += TII->getInstSizeInBytes(MI);
911-
}
912-
}
913-
914-
return CodeSize;
915-
}
916-
917897
// AccumOffset computed for the MCExpr equivalent of:
918898
// alignTo(std::max(1, NumVGPR), 4) / 4 - 1;
919899
static const MCExpr *computeAccumOffset(const MCExpr *NumVGPR, MCContext &Ctx) {

llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ class AMDGPUAsmPrinter final : public AsmPrinter {
5050

5151
MCCodeEmitter *DumpCodeInstEmitter = nullptr;
5252

53-
uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
54-
5553
void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
5654
void getAmdKernelCode(AMDGPU::AMDGPUMCKernelCodeT &Out,
5755
const SIProgramInfo &KernelInfo,

llvm/lib/Target/AMDGPU/SIProgramInfo.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void SIProgramInfo::reset(const MachineFunction &MF) {
2727

2828
const MCExpr *ZeroExpr = MCConstantExpr::create(0, Ctx);
2929

30+
CodeSizeInBytes.reset();
31+
3032
VGPRBlocks = ZeroExpr;
3133
SGPRBlocks = ZeroExpr;
3234
Priority = 0;
@@ -199,3 +201,28 @@ const MCExpr *SIProgramInfo::getPGMRSrc2(CallingConv::ID CC,
199201

200202
return MCConstantExpr::create(0, Ctx);
201203
}
204+
205+
uint64_t SIProgramInfo::getFunctionCodeSize(const MachineFunction &MF) {
206+
if (CodeSizeInBytes.has_value())
207+
return *CodeSizeInBytes;
208+
209+
const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
210+
const SIInstrInfo *TII = STM.getInstrInfo();
211+
212+
uint64_t CodeSize = 0;
213+
214+
for (const MachineBasicBlock &MBB : MF) {
215+
for (const MachineInstr &MI : MBB) {
216+
// TODO: CodeSize should account for multiple functions.
217+
218+
// TODO: Should we count size of debug info?
219+
if (MI.isDebugInstr())
220+
continue;
221+
222+
CodeSize += TII->getInstSizeInBytes(MI);
223+
}
224+
}
225+
226+
CodeSizeInBytes = CodeSize;
227+
return CodeSize;
228+
}

llvm/lib/Target/AMDGPU/SIProgramInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/IR/CallingConv.h"
2020
#include "llvm/Support/Compiler.h"
2121
#include <cstdint>
22+
#include <optional>
2223

2324
namespace llvm {
2425

@@ -29,6 +30,8 @@ class MachineFunction;
2930

3031
/// Track resource usage for kernels / entry functions.
3132
struct LLVM_EXTERNAL_VISIBILITY SIProgramInfo {
33+
std::optional<uint64_t> CodeSizeInBytes;
34+
3235
// Fields set in PGM_RSRC1 pm4 packet.
3336
const MCExpr *VGPRBlocks = nullptr;
3437
const MCExpr *SGPRBlocks = nullptr;
@@ -97,6 +100,9 @@ struct LLVM_EXTERNAL_VISIBILITY SIProgramInfo {
97100
// non-MCExpr members.
98101
void reset(const MachineFunction &MF);
99102

103+
// Get function code size and cache the value.
104+
uint64_t getFunctionCodeSize(const MachineFunction &MF);
105+
100106
/// Compute the value of the ComputePGMRsrc1 register.
101107
const MCExpr *getComputePGMRSrc1(const GCNSubtarget &ST,
102108
MCContext &Ctx) const;

0 commit comments

Comments
 (0)