@@ -24,12 +24,119 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
2525======================= end_copyright_notice ==================================*/
2626#pragma once
27+
28+ #include " Compiler/CISACodeGen/GenCodeGenModule.h"
29+
2730#include " common/LLVMWarningsPush.hpp"
2831#include < llvm/Pass.h>
2932#include " common/LLVMWarningsPop.hpp"
3033
34+ using namespace llvm ;
35+
3136namespace IGC
3237{
3338 // / Lower down alloca to private memory
34- llvm::ModulePass* CreatePrivateMemoryResolution ();
39+ ModulePass* CreatePrivateMemoryResolution ();
40+
41+ // / \brief Analyze alloca instructions and determine the size and offset of
42+ // / each alloca and the total amount of private memory needed by each kernel.
43+ class ModuleAllocaInfo {
44+ public:
45+ ModuleAllocaInfo (Module* M, const DataLayout* DL,
46+ GenXFunctionGroupAnalysis* FGA = nullptr )
47+ : M(M), DL(DL), FGA(FGA) {
48+ analyze ();
49+ }
50+
51+ ~ModuleAllocaInfo () {
52+ for (auto I = InfoMap.begin (), E = InfoMap.end (); I != E; ++I)
53+ delete I->second ;
54+ }
55+
56+ ModuleAllocaInfo (const ModuleAllocaInfo&) = delete ;
57+ ModuleAllocaInfo& operator =(const ModuleAllocaInfo&) = delete ;
58+
59+ // / \brief Return the offset of alloca instruction in private memory buffer.
60+ // This function should not be called when AI is variable length alloca
61+ unsigned getConstBufferOffset (AllocaInst* AI) const {
62+ IGC_ASSERT (isa<ConstantInt>(AI->getArraySize ()));
63+ Function* F = AI->getParent ()->getParent ();
64+ return getFuncAllocaInfo (F)->AllocaDesc [AI].first ;
65+ }
66+
67+ // / \brief Return the size of alloca instruction in private memory buffer.
68+ // This function should not be called when AI is variable length alloca
69+ unsigned getConstBufferSize (AllocaInst* AI) const {
70+ IGC_ASSERT (isa<ConstantInt>(AI->getArraySize ()));
71+ Function* F = AI->getParent ()->getParent ();
72+ return getFuncAllocaInfo (F)->AllocaDesc [AI].second ;
73+ }
74+
75+ // / \brief Return all alloca instructions of a given function.
76+ SmallVector<AllocaInst*, 8 > & getAllocaInsts (Function * F) const {
77+ return getFuncAllocaInfo (F)->Allocas ;
78+ }
79+
80+ // / \brief Return the total private memory size per WI of a given function.
81+ unsigned getTotalPrivateMemPerWI (Function* F) const {
82+ auto FI = getFuncAllocaInfo (F);
83+ return FI ? FI->TotalSize : 0 ;
84+ }
85+
86+ private:
87+ // / \brief The module being analyzed.
88+ Module* const M;
89+
90+ // / \brief The DataLayout object.
91+ const DataLayout* const DL;
92+
93+ // / \brief The optional function group analysis.
94+ GenXFunctionGroupAnalysis* const FGA;
95+
96+ struct FunctionAllocaInfo {
97+ void setAllocaDesc (AllocaInst* AI, unsigned Offset, unsigned Size) {
98+ AllocaDesc[AI] = std::make_pair (Offset, Size);
99+ }
100+
101+ // / \brief Total amount of private memory size per kernel. All functions in
102+ // / a kernel will have the same size.
103+ unsigned TotalSize = 0 ;
104+
105+ // / \brief Alloca instructions for a function.
106+ SmallVector<AllocaInst*, 8 > Allocas;
107+
108+ // / \brief Alloca instruction, its offset and size in buffer.
109+ DenseMap<AllocaInst*, std::pair<unsigned , unsigned >> AllocaDesc;
110+ };
111+
112+ FunctionAllocaInfo* getFuncAllocaInfo (Function* F) const {
113+ auto Iter = InfoMap.find (F);
114+ if (Iter != InfoMap.end ())
115+ return Iter->second ;
116+ return nullptr ;
117+ }
118+
119+ FunctionAllocaInfo* getOrCreateFuncAllocaInfo (Function* F) {
120+ auto Iter = InfoMap.find (F);
121+ if (Iter != InfoMap.end ())
122+ return Iter->second ;
123+
124+ auto AllocaInfo = new FunctionAllocaInfo;
125+ InfoMap[F] = AllocaInfo;
126+ return AllocaInfo;
127+ }
128+
129+ // / \brief Analyze the module and fill function alloca info map.
130+ void analyze ();
131+
132+ // / \brief When function group analysis is available, visit group by group.
133+ void analyze (FunctionGroup* FG);
134+
135+ // / \brief Analyze individual functions.
136+ void analyze (Function* F, unsigned & gOffset , unsigned & gAlignment );
137+
138+ // / \brief Each function has an entry that describes its private memory
139+ // / usage information.
140+ DenseMap<Function*, FunctionAllocaInfo*> InfoMap;
141+ };
35142}
0 commit comments