Skip to content

Commit 2b8113b

Browse files
committed
[SYCL] Temporary store sycl::group functions result in temporary private alloca
It's a workaround for a community change, that makes sret arguments of CallInst to be in target's alloca address space, which for us resulted in a regression in a corner case, when an invalid AS cast from local to private AS was inserted when a function was writing to GV in local AS. Signed-off-by: Sidorov, Dmitry <[email protected]>
1 parent b23d69e commit 2b8113b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

llvm/lib/SYCLLowerIR/LowerWGScope.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ STATISTIC(LocalMemUsed, "amount of additional local memory used for sharing");
103103
static constexpr char WG_SCOPE_MD[] = "work_group_scope";
104104
static constexpr char WI_SCOPE_MD[] = "work_item_scope";
105105
static constexpr char PFWI_MD[] = "parallel_for_work_item";
106+
static constexpr char GET_GROUP_PREFIX[] = "_ZNK4sycl3_V15group";
106107

107108
static cl::opt<int> Debug("sycl-lower-wg-debug", llvm::cl::Optional,
108109
llvm::cl::Hidden,
@@ -818,6 +819,30 @@ PreservedAnalyses SYCLLowerWGScopePass::run(Function &F,
818819
Allocas.insert(AllocaI);
819820
}
820821
for (; I && (I != BB.getTerminator()); I = I->getNextNode()) {
822+
if (CallInst *CI = dyn_cast<CallInst>(I)) {
823+
if (CI->getCalledFunction()->getName().
824+
starts_with(GET_GROUP_PREFIX) &&
825+
CI->hasStructRetAttr()) {
826+
if (auto *ASCast = dyn_cast<AddrSpaceCastOperator>(CI->getOperand(0))) {
827+
unsigned SrcAS = ASCast->getSrcAddressSpace();
828+
unsigned DstAS = ASCast->getDestAddressSpace();
829+
if (SrcAS == static_cast<unsigned>(spirv::AddrSpace::Local) &&
830+
DstAS == static_cast<unsigned>(spirv::AddrSpace::Private)) {
831+
IRBuilder<> Builder(CI->getContext());
832+
llvm::BasicBlock &FirstBB = F.getEntryBlock();
833+
Builder.SetInsertPoint(&FirstBB, FirstBB.begin());
834+
Type *ResTy = CI->getParamStructRetType(0);
835+
auto *TMPAlloca = Builder.CreateAlloca(
836+
ResTy, nullptr, "lower_wg.local_copy");
837+
Builder.SetInsertPoint(CI->getNextNode());
838+
auto *LI = Builder.CreateLoad(ResTy, TMPAlloca, "lower_wg.private_load");
839+
Builder.CreateStore(LI, ASCast->getPointerOperand());
840+
ASCast->replaceAllUsesWith(TMPAlloca);
841+
ASCast->dropAllReferences();
842+
}
843+
}
844+
}
845+
}
821846
if (isWIScopeInst(I)) {
822847
if (isPFWICall(I))
823848
PFWICalls.insert(dyn_cast<CallInst>(I));

0 commit comments

Comments
 (0)