Skip to content

Commit 17cfab3

Browse files
stefan-iligcbot
authored andcommitted
Avoid unecessary looping in GenericAddressDynamicResolution
Avoid looping through all instructions on each visit.
1 parent 18183da commit 17cfab3

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

IGC/Compiler/Optimizer/OpenCLPasses/GenericAddressResolution/GenericAddressDynamicResolution.cpp

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ SPDX-License-Identifier: MIT
1414
#include "Compiler/CISACodeGen/OpenCLKernelCodeGen.hpp"
1515
#include "Compiler/IGCPassSupport.h"
1616
#include "Compiler/MetaDataUtilsWrapper.h"
17+
1718
#include "common/LLVMWarningsPush.hpp"
1819
#include "llvmWrapper/Support/Alignment.h"
1920
#include "llvmWrapper/IR/DerivedTypes.h"
21+
#include <llvm/ADT/SmallVector.h>
2022
#include <llvm/IR/Module.h>
2123
#include <llvm/IR/Instructions.h>
2224
#include <llvm/IR/DataLayout.h>
@@ -32,34 +34,33 @@ namespace {
3234
class GenericAddressDynamicResolution : public FunctionPass {
3335
public:
3436
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-
virtual StringRef getPassName() const override
44+
StringRef getPassName() const override
4545
{
4646
return "GenericAddressDynamicResolution";
4747
}
4848

49-
virtual void getAnalysisUsage(AnalysisUsage& AU) const override
49+
void getAnalysisUsage(AnalysisUsage& AU) const override
5050
{
5151
AU.addRequired<MetaDataUtilsWrapper>();
5252
AU.addRequired<CodeGenContextWrapper>();
5353
AU.addRequired<CastToGASAnalysis>();
5454
}
5555

56-
virtual bool runOnFunction(Function& F) override;
57-
56+
bool runOnFunction(Function& F) override;
57+
private:
5858
bool visitLoadStoreInst(Instruction& I);
5959
bool visitIntrinsicCall(CallInst& I);
60-
Module* getModule() { return m_module; }
6160

62-
private:
61+
Module* m_module = nullptr;
62+
CodeGenContext* m_ctx = nullptr;
63+
llvm::SmallVector<Instruction*, 32> generatedLoadStores;
6364
bool m_needPrivateBranches = false;
6465
bool m_needLocalBranches = false;
6566

@@ -96,43 +97,37 @@ bool GenericAddressDynamicResolution::runOnFunction(Function& F)
9697
m_needLocalBranches = !GI.isNoLocalToGenericOptionEnabled() && GI.canGenericPointToLocal(F);
9798

9899
bool modified = false;
99-
bool changed = false;
100100

101-
// iterate for all the intrinisics used by to_local, to_global, and to_private
102-
do {
103-
changed = false;
104-
105-
for (inst_iterator i = inst_begin(F); i != inst_end(F); ++i) {
106-
Instruction& instruction = (*i);
101+
llvm::SmallVector<Instruction*, 32> callInstructions;
102+
llvm::SmallVector<Instruction*, 32> loadStoreInstructions;
107103

108-
if (CallInst * intrinsic = dyn_cast<CallInst>(&instruction)) {
109-
changed = visitIntrinsicCall(*intrinsic);
110-
}
104+
for (auto& instruction: llvm::instructions(F)) {
111105

112-
if (changed) {
113-
modified = true;
114-
break;
115-
}
106+
if (isa<CallInst>(&instruction)) {
107+
callInstructions.push_back(&instruction);
108+
}
109+
if (isa<LoadInst, StoreInst>(instruction)) {
110+
loadStoreInstructions.push_back(&instruction);
116111
}
117-
} while (changed);
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+
}
118117

119118
// iterate over all loads/stores with generic address space pointers
120-
do {
121-
changed = false;
122-
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-
}
119+
for (auto* loadStoreInst : loadStoreInstructions) {
120+
modified |= visitLoadStoreInst(*loadStoreInst);
121+
}
129122

130-
if (changed) {
131-
modified = true;
132-
break;
133-
}
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);
134129
}
135-
} while (changed);
130+
}
136131

137132
if (m_numAdditionalControlFlows)
138133
{
@@ -155,8 +150,7 @@ bool GenericAddressDynamicResolution::runOnFunction(Function& F)
155150

156151
Type* GenericAddressDynamicResolution::getPointerAsIntType(LLVMContext& ctx, const unsigned AS)
157152
{
158-
Module* pModule = getModule();
159-
DataLayout dataLayout = pModule->getDataLayout();
153+
DataLayout dataLayout = m_module->getDataLayout();
160154
unsigned ptrBits(dataLayout.getPointerSizeInBits(AS));
161155
return IntegerType::get(ctx, ptrBits);
162156
}
@@ -168,11 +162,11 @@ bool GenericAddressDynamicResolution::visitLoadStoreInst(Instruction& I)
168162
Value* pointerOperand = nullptr;
169163
unsigned int pointerAddressSpace = ADDRESS_SPACE_NUM_ADDRESSES;
170164

171-
if (LoadInst * load = dyn_cast<LoadInst>(&I)) {
165+
if (auto* load = dyn_cast<LoadInst>(&I)) {
172166
pointerOperand = load->getPointerOperand();
173167
pointerAddressSpace = load->getPointerAddressSpace();
174168
}
175-
else if (StoreInst * store = dyn_cast<StoreInst>(&I)) {
169+
else if (auto* store = dyn_cast<StoreInst>(&I)) {
176170
pointerOperand = store->getPointerOperand();
177171
pointerAddressSpace = store->getPointerAddressSpace();
178172
}
@@ -241,7 +235,7 @@ void GenericAddressDynamicResolution::resolveGAS(Instruction& I, Value* pointerO
241235
// with the corresponding address space.
242236

243237
IGCLLVM::IRBuilder<> builder(&I);
244-
PointerType* pointerType = dyn_cast<PointerType>(pointerOperand->getType());
238+
auto* pointerType = dyn_cast<PointerType>(pointerOperand->getType());
245239
IGC_ASSERT( pointerType != nullptr );
246240
ConstantInt* privateTag = builder.getInt64(1); // tag 001
247241
ConstantInt* localTag = builder.getInt64(2); // tag 010
@@ -275,15 +269,18 @@ void GenericAddressDynamicResolution::resolveGAS(Instruction& I, Value* pointerO
275269
builder.SetInsertPoint(BB);
276270
PointerType* ptrType = IGCLLVM::getWithSamePointeeType(pointerType, addressSpace);
277271
Value* ptr = builder.CreateAddrSpaceCast(pointerOperand, ptrType);
272+
Instruction* generatedLoadStore = nullptr;
278273

279-
if (LoadInst* LI = dyn_cast<LoadInst>(&I))
274+
if (auto* LI = dyn_cast<LoadInst>(&I))
280275
{
281276
load = builder.CreateAlignedLoad(LI->getType(), ptr, getAlign(*LI), LI->isVolatile(), LoadName);
277+
generatedLoadStore = cast<Instruction>(load);
282278
}
283-
else if (StoreInst* SI = dyn_cast<StoreInst>(&I))
279+
else if (auto* SI = dyn_cast<StoreInst>(&I))
284280
{
285-
builder.CreateAlignedStore(I.getOperand(0), ptr, getAlign(*SI), SI->isVolatile());
281+
generatedLoadStore = builder.CreateAlignedStore(I.getOperand(0), ptr, getAlign(*SI), SI->isVolatile());
286282
}
283+
generatedLoadStores.push_back(generatedLoadStore);
287284

288285
builder.CreateBr(convergeBlock);
289286
return BB;
@@ -345,22 +342,25 @@ void GenericAddressDynamicResolution::resolveGAS(Instruction& I, Value* pointerO
345342
void GenericAddressDynamicResolution::resolveGASWithoutBranches(Instruction& I, Value* pointerOperand)
346343
{
347344
IGCLLVM::IRBuilder<> builder(&I);
348-
PointerType* pointerType = dyn_cast<PointerType>(pointerOperand->getType());
345+
auto* pointerType = dyn_cast<PointerType>(pointerOperand->getType());
349346
IGC_ASSERT( pointerType != nullptr );
350347

351348
Value* nonLocalLoad = nullptr;
349+
Instruction* generatedLoadStore = nullptr;
352350

353351
PointerType* ptrType = IGCLLVM::getWithSamePointeeType(pointerType, ADDRESS_SPACE_GLOBAL);
354352
Value* globalPtr = builder.CreateAddrSpaceCast(pointerOperand, ptrType);
355353

356-
if (LoadInst* LI = dyn_cast<LoadInst>(&I))
354+
if (auto* LI = dyn_cast<LoadInst>(&I))
357355
{
358356
nonLocalLoad = builder.CreateAlignedLoad(LI->getType(), globalPtr, getAlign(*LI), LI->isVolatile(), "globalOrPrivateLoad");
357+
generatedLoadStore = cast<Instruction>(nonLocalLoad);
359358
}
360-
else if (StoreInst* SI = dyn_cast<StoreInst>(&I))
359+
else if (auto* SI = dyn_cast<StoreInst>(&I))
361360
{
362-
builder.CreateAlignedStore(I.getOperand(0), globalPtr, getAlign(*SI), SI->isVolatile());
361+
generatedLoadStore = builder.CreateAlignedStore(I.getOperand(0), globalPtr, getAlign(*SI), SI->isVolatile());
363362
}
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-
PointerType* dstType = dyn_cast<PointerType>(I.getType());
389+
auto* 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-
PointerType* pointerType = dyn_cast<PointerType>(arg->getType());
394+
auto* 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

Comments
 (0)