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