@@ -14,11 +14,9 @@ SPDX-License-Identifier: MIT
1414#include " Compiler/CISACodeGen/OpenCLKernelCodeGen.hpp"
1515#include " Compiler/IGCPassSupport.h"
1616#include " Compiler/MetaDataUtilsWrapper.h"
17-
1817#include " common/LLVMWarningsPush.hpp"
1918#include " llvmWrapper/Support/Alignment.h"
2019#include " llvmWrapper/IR/DerivedTypes.h"
21- #include < llvm/ADT/SmallVector.h>
2220#include < llvm/IR/Module.h>
2321#include < llvm/IR/Instructions.h>
2422#include < llvm/IR/DataLayout.h>
@@ -34,33 +32,34 @@ namespace {
3432 class GenericAddressDynamicResolution : public FunctionPass {
3533 public:
3634 static char ID;
35+ Module* m_module = nullptr ;
36+ CodeGenContext* m_ctx = nullptr ;
3737
3838 GenericAddressDynamicResolution ()
3939 : FunctionPass(ID)
4040 {
4141 }
4242 ~GenericAddressDynamicResolution () = default ;
4343
44- StringRef getPassName () const override
44+ virtual StringRef getPassName () const override
4545 {
4646 return " GenericAddressDynamicResolution" ;
4747 }
4848
49- void getAnalysisUsage (AnalysisUsage& AU) const override
49+ virtual void getAnalysisUsage (AnalysisUsage& AU) const override
5050 {
5151 AU.addRequired <MetaDataUtilsWrapper>();
5252 AU.addRequired <CodeGenContextWrapper>();
5353 AU.addRequired <CastToGASAnalysis>();
5454 }
5555
56- bool runOnFunction (Function& F) override ;
57- private:
56+ virtual bool runOnFunction (Function& F) override ;
57+
5858 bool visitLoadStoreInst (Instruction& I);
5959 bool visitIntrinsicCall (CallInst& I);
60+ Module* getModule () { return m_module; }
6061
61- Module* m_module = nullptr ;
62- CodeGenContext* m_ctx = nullptr ;
63- llvm::SmallVector<Instruction*, 32 > generatedLoadStores;
62+ private:
6463 bool m_needPrivateBranches = false ;
6564 bool m_needLocalBranches = false ;
6665
@@ -97,37 +96,43 @@ bool GenericAddressDynamicResolution::runOnFunction(Function& F)
9796 m_needLocalBranches = !GI.isNoLocalToGenericOptionEnabled () && GI.canGenericPointToLocal (F);
9897
9998 bool modified = false ;
99+ bool changed = false ;
100100
101- llvm::SmallVector<Instruction*, 32 > callInstructions;
102- llvm::SmallVector<Instruction*, 32 > loadStoreInstructions;
101+ // iterate for all the intrinisics used by to_local, to_global, and to_private
102+ do {
103+ changed = false ;
103104
104- for (auto & instruction: llvm::instructions (F)) {
105+ for (inst_iterator i = inst_begin (F); i != inst_end (F); ++i) {
106+ Instruction& instruction = (*i);
105107
106- if (isa<CallInst>(&instruction)) {
107- callInstructions.push_back (&instruction);
108- }
109- if (isa<LoadInst, StoreInst>(instruction)) {
110- loadStoreInstructions.push_back (&instruction);
108+ if (CallInst * intrinsic = dyn_cast<CallInst>(&instruction)) {
109+ changed = visitIntrinsicCall (*intrinsic);
110+ }
111+
112+ if (changed) {
113+ modified = true ;
114+ break ;
115+ }
111116 }
112- }
113- // iterate for all the intrinisics used by to_local, to_global, and to_private
114- for (auto * callInst : callInstructions) {
115- modified |= visitIntrinsicCall (cast<CallInst>(*callInst));
116- }
117+ } while (changed);
117118
118119 // iterate over all loads/stores with generic address space pointers
119- for (auto * loadStoreInst : loadStoreInstructions) {
120- modified |= visitLoadStoreInst (*loadStoreInst);
121- }
120+ do {
121+ changed = false ;
122122
123- // iterate over all newly generated load/stores
124- while (!generatedLoadStores.empty ()) {
125- llvm::SmallVector<Instruction*, 32 > newInstructions = generatedLoadStores;
126- generatedLoadStores.clear ();
127- for (auto * loadStoreInst : newInstructions) {
128- modified |= visitLoadStoreInst (*loadStoreInst);
123+ for (inst_iterator i = inst_begin (F); i != inst_end (F); ++i) {
124+ Instruction& instruction = (*i);
125+
126+ if (isa<LoadInst>(instruction) || isa<StoreInst>(instruction)) {
127+ changed = visitLoadStoreInst (instruction);
128+ }
129+
130+ if (changed) {
131+ modified = true ;
132+ break ;
133+ }
129134 }
130- }
135+ } while (changed);
131136
132137 if (m_numAdditionalControlFlows)
133138 {
@@ -150,7 +155,8 @@ bool GenericAddressDynamicResolution::runOnFunction(Function& F)
150155
151156Type* GenericAddressDynamicResolution::getPointerAsIntType (LLVMContext& ctx, const unsigned AS)
152157{
153- DataLayout dataLayout = m_module->getDataLayout ();
158+ Module* pModule = getModule ();
159+ DataLayout dataLayout = pModule->getDataLayout ();
154160 unsigned ptrBits (dataLayout.getPointerSizeInBits (AS));
155161 return IntegerType::get (ctx, ptrBits);
156162}
@@ -162,11 +168,11 @@ bool GenericAddressDynamicResolution::visitLoadStoreInst(Instruction& I)
162168 Value* pointerOperand = nullptr ;
163169 unsigned int pointerAddressSpace = ADDRESS_SPACE_NUM_ADDRESSES;
164170
165- if (auto * load = dyn_cast<LoadInst>(&I)) {
171+ if (LoadInst * load = dyn_cast<LoadInst>(&I)) {
166172 pointerOperand = load->getPointerOperand ();
167173 pointerAddressSpace = load->getPointerAddressSpace ();
168174 }
169- else if (auto * store = dyn_cast<StoreInst>(&I)) {
175+ else if (StoreInst * store = dyn_cast<StoreInst>(&I)) {
170176 pointerOperand = store->getPointerOperand ();
171177 pointerAddressSpace = store->getPointerAddressSpace ();
172178 }
@@ -235,7 +241,7 @@ void GenericAddressDynamicResolution::resolveGAS(Instruction& I, Value* pointerO
235241 // with the corresponding address space.
236242
237243 IGCLLVM::IRBuilder<> builder (&I);
238- auto * pointerType = dyn_cast<PointerType>(pointerOperand->getType ());
244+ PointerType * pointerType = dyn_cast<PointerType>(pointerOperand->getType ());
239245 IGC_ASSERT ( pointerType != nullptr );
240246 ConstantInt* privateTag = builder.getInt64 (1 ); // tag 001
241247 ConstantInt* localTag = builder.getInt64 (2 ); // tag 010
@@ -269,18 +275,15 @@ void GenericAddressDynamicResolution::resolveGAS(Instruction& I, Value* pointerO
269275 builder.SetInsertPoint (BB);
270276 PointerType* ptrType = IGCLLVM::getWithSamePointeeType (pointerType, addressSpace);
271277 Value* ptr = builder.CreateAddrSpaceCast (pointerOperand, ptrType);
272- Instruction* generatedLoadStore = nullptr ;
273278
274- if (auto * LI = dyn_cast<LoadInst>(&I))
279+ if (LoadInst * LI = dyn_cast<LoadInst>(&I))
275280 {
276281 load = builder.CreateAlignedLoad (LI->getType (), ptr, getAlign (*LI), LI->isVolatile (), LoadName);
277- generatedLoadStore = cast<Instruction>(load);
278282 }
279- else if (auto * SI = dyn_cast<StoreInst>(&I))
283+ else if (StoreInst * SI = dyn_cast<StoreInst>(&I))
280284 {
281- generatedLoadStore = builder.CreateAlignedStore (I.getOperand (0 ), ptr, getAlign (*SI), SI->isVolatile ());
285+ builder.CreateAlignedStore (I.getOperand (0 ), ptr, getAlign (*SI), SI->isVolatile ());
282286 }
283- generatedLoadStores.push_back (generatedLoadStore);
284287
285288 builder.CreateBr (convergeBlock);
286289 return BB;
@@ -342,25 +345,22 @@ void GenericAddressDynamicResolution::resolveGAS(Instruction& I, Value* pointerO
342345void GenericAddressDynamicResolution::resolveGASWithoutBranches (Instruction& I, Value* pointerOperand)
343346{
344347 IGCLLVM::IRBuilder<> builder (&I);
345- auto * pointerType = dyn_cast<PointerType>(pointerOperand->getType ());
348+ PointerType * pointerType = dyn_cast<PointerType>(pointerOperand->getType ());
346349 IGC_ASSERT ( pointerType != nullptr );
347350
348351 Value* nonLocalLoad = nullptr ;
349- Instruction* generatedLoadStore = nullptr ;
350352
351353 PointerType* ptrType = IGCLLVM::getWithSamePointeeType (pointerType, ADDRESS_SPACE_GLOBAL);
352354 Value* globalPtr = builder.CreateAddrSpaceCast (pointerOperand, ptrType);
353355
354- if (auto * LI = dyn_cast<LoadInst>(&I))
356+ if (LoadInst * LI = dyn_cast<LoadInst>(&I))
355357 {
356358 nonLocalLoad = builder.CreateAlignedLoad (LI->getType (), globalPtr, getAlign (*LI), LI->isVolatile (), " globalOrPrivateLoad" );
357- generatedLoadStore = cast<Instruction>(nonLocalLoad);
358359 }
359- else if (auto * SI = dyn_cast<StoreInst>(&I))
360+ else if (StoreInst * SI = dyn_cast<StoreInst>(&I))
360361 {
361- generatedLoadStore = builder.CreateAlignedStore (I.getOperand (0 ), globalPtr, getAlign (*SI), SI->isVolatile ());
362+ builder.CreateAlignedStore (I.getOperand (0 ), globalPtr, getAlign (*SI), SI->isVolatile ());
362363 }
363- generatedLoadStores.push_back (generatedLoadStore);
364364
365365 if (nonLocalLoad != nullptr )
366366 {
@@ -386,12 +386,12 @@ bool GenericAddressDynamicResolution::visitIntrinsicCall(CallInst& I)
386386 {
387387 IGC_ASSERT (IGCLLVM::getNumArgOperands (&I) == 1 );
388388 Value* arg = I.getArgOperand (0 );
389- auto * dstType = dyn_cast<PointerType>(I.getType ());
389+ PointerType * dstType = dyn_cast<PointerType>(I.getType ());
390390 IGC_ASSERT ( dstType != nullptr );
391391 const unsigned targetAS = cast<PointerType>(I.getType ())->getAddressSpace ();
392392
393393 IGCLLVM::IRBuilder<> builder (&I);
394- auto * pointerType = dyn_cast<PointerType>(arg->getType ());
394+ PointerType * pointerType = dyn_cast<PointerType>(arg->getType ());
395395 IGC_ASSERT ( pointerType != nullptr );
396396 ConstantInt* globalTag = builder.getInt64 (0 ); // tag 000/111
397397 ConstantInt* privateTag = builder.getInt64 (1 ); // tag 001
@@ -411,7 +411,7 @@ bool GenericAddressDynamicResolution::visitIntrinsicCall(CallInst& I)
411411 // Force distinguishing private and global pointers if a kernel uses explicit casts.
412412 // For more details please refer to section "Generic Address Space Explicit Casts" in
413413 // documentation directory under igc/generic-pointers/generic-pointers.md
414- auto * ClContext = static_cast <OpenCLProgramContext*>(m_ctx);
414+ auto ClContext = static_cast <OpenCLProgramContext*>(m_ctx);
415415 ClContext->setDistinguishBetweenPrivateAndGlobalPtr (true );
416416 }
417417
0 commit comments