1//===- AMDGPUMCResourceInfo.h ----- MC Resource Info --------------*- C++ -*-=//
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/// \file
10/// \brief MC infrastructure to propagate the function level resource usage
11/// info.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUMCRESOURCEINFO_H
16#define LLVM_LIB_TARGET_AMDGPU_AMDGPUMCRESOURCEINFO_H
17
18#include "AMDGPUResourceUsageAnalysis.h"
19#include "MCTargetDesc/AMDGPUMCExpr.h"
20
21namespace llvm {
22
23class MCContext;
24class MCSymbol;
25class StringRef;
26class MachineFunction;
27
28class MCResourceInfo {
29public:
30 enum ResourceInfoKind {
31 RIK_NumVGPR,
32 RIK_NumAGPR,
33 RIK_NumSGPR,
34 RIK_NumNamedBarrier,
35 RIK_PrivateSegSize,
36 RIK_UsesVCC,
37 RIK_UsesFlatScratch,
38 RIK_HasDynSizedStack,
39 RIK_HasRecursion,
40 RIK_HasIndirectCall
41 };
42
43private:
44 int32_t MaxVGPR = 0;
45 int32_t MaxAGPR = 0;
46 int32_t MaxSGPR = 0;
47 int32_t MaxNamedBarrier = 0;
48
49 // Whether the MCResourceInfo has been finalized through finalize(MCContext
50 // &). Should only be called once, at the end of AsmPrinting to assign MaxXGPR
51 // symbols to their final value.
52 bool Finalized = false;
53
54 void assignResourceInfoExpr(int64_t localValue, ResourceInfoKind RIK,
55 AMDGPUMCExpr::VariantKind Kind,
56 const MachineFunction &MF,
57 const SmallVectorImpl<const Function *> &Callees,
58 MCContext &OutContext);
59
60 // Assigns expression for Max S/V/A-GPRs to the referenced symbols.
61 void assignMaxRegs(MCContext &OutContext);
62
63 // Take flattened max of cyclic function calls' knowns. For example, for
64 // a cycle A->B->C->D->A, take max(A, B, C, D) for A and have B, C, D have the
65 // propgated value from A.
66 const MCExpr *flattenedCycleMax(MCSymbol *RecSym, ResourceInfoKind RIK,
67 MCContext &OutContext);
68
69public:
70 MCResourceInfo() = default;
71 void addMaxVGPRCandidate(int32_t candidate) {
72 MaxVGPR = std::max(a: MaxVGPR, b: candidate);
73 }
74 void addMaxAGPRCandidate(int32_t candidate) {
75 MaxAGPR = std::max(a: MaxAGPR, b: candidate);
76 }
77 void addMaxSGPRCandidate(int32_t candidate) {
78 MaxSGPR = std::max(a: MaxSGPR, b: candidate);
79 }
80 void addMaxNamedBarrierCandidate(int32_t candidate) {
81 MaxNamedBarrier = std::max(a: MaxNamedBarrier, b: candidate);
82 }
83
84 MCSymbol *getSymbol(StringRef FuncName, ResourceInfoKind RIK,
85 MCContext &OutContext, bool IsLocal);
86 const MCExpr *getSymRefExpr(StringRef FuncName, ResourceInfoKind RIK,
87 MCContext &Ctx, bool IsLocal);
88
89 void reset();
90
91 // Resolves the final symbols that requires the inter-function resource info
92 // to be resolved.
93 void finalize(MCContext &OutContext);
94
95 MCSymbol *getMaxVGPRSymbol(MCContext &OutContext);
96 MCSymbol *getMaxAGPRSymbol(MCContext &OutContext);
97 MCSymbol *getMaxSGPRSymbol(MCContext &OutContext);
98 MCSymbol *getMaxNamedBarrierSymbol(MCContext &OutContext);
99
100 /// AMDGPUResourceUsageAnalysis gathers resource usage on a per-function
101 /// granularity. However, some resource info has to be assigned the call
102 /// transitive maximum or accumulative. For example, if A calls B and B's VGPR
103 /// usage exceeds A's, A should be assigned B's VGPR usage. Furthermore,
104 /// functions with indirect calls should be assigned the module level maximum.
105 void gatherResourceInfo(
106 const MachineFunction &MF,
107 const AMDGPUResourceUsageAnalysisWrapperPass::FunctionResourceInfo &FRI,
108 MCContext &OutContext);
109
110 const MCExpr *createTotalNumVGPRs(const MachineFunction &MF, MCContext &Ctx);
111 const MCExpr *createTotalNumSGPRs(const MachineFunction &MF, bool hasXnack,
112 MCContext &Ctx);
113};
114} // namespace llvm
115
116#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUMCRESOURCEINFO_H
117