Skip to content

Commit 4fdeaca

Browse files
michalpaszkowskiigcbot
authored andcommitted
[IGC] Replace calls to kernels when looping through a copied collection of users
Collect pointers to users (callsites) of the original kernel function and loop through the collection. Otherwise when looping through F->users(), calling call->setCalledFunction(NewF) modifies the F->users() by removing the very first element (second element becomes first) and the loop skips every second element.
1 parent 9fa74ab commit 4fdeaca

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

IGC/Compiler/Optimizer/OpenCLPasses/KernelFunctionCloning.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,20 @@ bool KernelFunctionCloning::runOnModule(Module& M) {
130130
NewF->setLinkage(GlobalValue::InternalLinkage);
131131
if (!F->getParent()->getFunction(NewF->getName()))
132132
F->getParent()->getFunctionList().push_back(NewF);
133+
134+
// Collect pointers to users (callsites) of the original kernel
135+
// function and loop through the collection. Otherwise when looping
136+
// through F->users(), calling call->setCalledFunction(NewF) modifies
137+
// the F->users() by removing the very first element (second element
138+
// becomes first) and the loop skips every second element.
139+
SmallVector<User*, 8> originalKernelFunctionUsers;
133140
for (auto* U : F->users()) {
141+
originalKernelFunctionUsers.push_back(U);
142+
}
143+
144+
// Replace the original calls to kernel function with calls to user
145+
// function clone at the callsites.
146+
for (auto& U : originalKernelFunctionUsers) {
134147
IGCLLVM::CallSite* call = nullptr;
135148
#if LLVM_VERSION_MAJOR < 11
136149
IGCLLVM::CallSite callSite(U);
@@ -142,6 +155,7 @@ bool KernelFunctionCloning::runOnModule(Module& M) {
142155
continue;
143156
call->setCalledFunction(NewF);
144157
}
158+
145159
Changed = true;
146160
}
147161

0 commit comments

Comments
 (0)