Skip to content

Commit be9f0eb

Browse files
eternastudentoigcbot
authored andcommitted
Move ModuleAllocaInfo to the header for reuse.
1 parent 1df496c commit be9f0eb

File tree

2 files changed

+108
-104
lines changed

2 files changed

+108
-104
lines changed

IGC/Compiler/Optimizer/OpenCLPasses/PrivateMemory/PrivateMemoryResolution.cpp

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -108,109 +108,6 @@ namespace IGC {
108108
{
109109
return new PrivateMemoryResolution();
110110
}
111-
112-
/// \brief Analyze alloca instructions and determine the size and offset of
113-
/// each alloca and the total amount of private memory needed by each kernel.
114-
class ModuleAllocaInfo {
115-
public:
116-
ModuleAllocaInfo(Module* M, const DataLayout* DL,
117-
GenXFunctionGroupAnalysis* FGA = nullptr)
118-
: M(M), DL(DL), FGA(FGA) {
119-
analyze();
120-
}
121-
122-
~ModuleAllocaInfo() {
123-
for (auto I = InfoMap.begin(), E = InfoMap.end(); I != E; ++I)
124-
delete I->second;
125-
}
126-
127-
ModuleAllocaInfo(const ModuleAllocaInfo&) = delete;
128-
ModuleAllocaInfo& operator=(const ModuleAllocaInfo&) = delete;
129-
130-
/// \brief Return the offset of alloca instruction in private memory buffer.
131-
// This function should not be called when AI is variable length alloca
132-
unsigned getConstBufferOffset(AllocaInst* AI) const {
133-
IGC_ASSERT(isa<ConstantInt>(AI->getArraySize()));
134-
Function* F = AI->getParent()->getParent();
135-
return getFuncAllocaInfo(F)->AllocaDesc[AI].first;
136-
}
137-
138-
/// \brief Return the size of alloca instruction in private memory buffer.
139-
// This function should not be called when AI is variable length alloca
140-
unsigned getConstBufferSize(AllocaInst* AI) const {
141-
IGC_ASSERT(isa<ConstantInt>(AI->getArraySize()));
142-
Function* F = AI->getParent()->getParent();
143-
return getFuncAllocaInfo(F)->AllocaDesc[AI].second;
144-
}
145-
146-
/// \brief Return all alloca instructions of a given function.
147-
SmallVector<AllocaInst*, 8> & getAllocaInsts(Function * F) const {
148-
return getFuncAllocaInfo(F)->Allocas;
149-
}
150-
151-
/// \brief Return the total private memory size per WI of a given function.
152-
unsigned getTotalPrivateMemPerWI(Function* F) const {
153-
auto FI = getFuncAllocaInfo(F);
154-
return FI ? FI->TotalSize : 0;
155-
}
156-
157-
private:
158-
/// \brief The module being analyzed.
159-
Module* const M;
160-
161-
/// \brief The DataLayout object.
162-
const DataLayout* const DL;
163-
164-
/// \brief The optional function group analysis.
165-
GenXFunctionGroupAnalysis* const FGA;
166-
167-
struct FunctionAllocaInfo {
168-
void setAllocaDesc(AllocaInst* AI, unsigned Offset, unsigned Size) {
169-
AllocaDesc[AI] = std::make_pair(Offset, Size);
170-
}
171-
172-
/// \brief Total amount of private memory size per kernel. All functions in
173-
/// a kernel will have the same size.
174-
unsigned TotalSize = 0;
175-
176-
/// \brief Alloca instructions for a function.
177-
SmallVector<AllocaInst*, 8> Allocas;
178-
179-
/// \brief Alloca instruction, its offset and size in buffer.
180-
DenseMap<AllocaInst*, std::pair<unsigned, unsigned>> AllocaDesc;
181-
};
182-
183-
FunctionAllocaInfo* getFuncAllocaInfo(Function* F) const {
184-
auto Iter = InfoMap.find(F);
185-
if (Iter != InfoMap.end())
186-
return Iter->second;
187-
return nullptr;
188-
}
189-
190-
FunctionAllocaInfo* getOrCreateFuncAllocaInfo(Function* F) {
191-
auto Iter = InfoMap.find(F);
192-
if (Iter != InfoMap.end())
193-
return Iter->second;
194-
195-
auto AllocaInfo = new FunctionAllocaInfo;
196-
InfoMap[F] = AllocaInfo;
197-
return AllocaInfo;
198-
}
199-
200-
/// \brief Analyze the module and fill function alloca info map.
201-
void analyze();
202-
203-
/// \brief When function group analysis is available, visit group by group.
204-
void analyze(FunctionGroup* FG);
205-
206-
/// \brief Analyze individual functions.
207-
void analyze(Function* F, unsigned& gOffset, unsigned& gAlignment);
208-
209-
/// \brief Each function has an entry that describes its private memory
210-
/// usage information.
211-
DenseMap<Function*, FunctionAllocaInfo*> InfoMap;
212-
};
213-
214111
} // namespace IGC
215112

216113
void ModuleAllocaInfo::analyze() {

IGC/Compiler/Optimizer/OpenCLPasses/PrivateMemory/PrivateMemoryResolution.hpp

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
3136
namespace 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

Comments
 (0)