@@ -819,23 +819,33 @@ PreservedAnalyses SYCLLowerWGScopePass::run(Function &F,
819819 Allocas.insert (AllocaI);
820820 }
821821 for (; I && (I != BB.getTerminator ()); I = I->getNextNode ()) {
822+ // sycl::group functions returning a structure would return it via sret
823+ // CallInst operand, which will be placed in target's alloca address
824+ // space (which is private for SPIR). As such sycl::group function
825+ // might write to a Global with Local address space - frontend will
826+ // insert address space cast from Local to Private, which is illegal.
827+ // So here we create a temporary alloca, that will be used as sret
828+ // operand, and copy from it to the Global.
822829 if (CallInst *CI = dyn_cast<CallInst>(I)) {
823- if (CI->getCalledFunction ()->getName ().
824- starts_with (GET_GROUP_PREFIX) &&
830+ if (CI->getCalledFunction ()->getName ().starts_with (GET_GROUP_PREFIX) &&
825831 CI->hasStructRetAttr ()) {
826- if (auto *ASCast = dyn_cast<AddrSpaceCastOperator>(CI->getOperand (0 ))) {
832+ if (auto *ASCast =
833+ dyn_cast<AddrSpaceCastOperator>(CI->getOperand (0 ))) {
827834 unsigned SrcAS = ASCast->getSrcAddressSpace ();
828835 unsigned DstAS = ASCast->getDestAddressSpace ();
829836 if (SrcAS == static_cast <unsigned >(spirv::AddrSpace::Local) &&
830837 DstAS == static_cast <unsigned >(spirv::AddrSpace::Private)) {
838+ LLVM_DEBUG (llvm::dbgs () << " +++ Illegal AS cast found in a call: "
839+ << *CI << " \n " );
831840 IRBuilder<> Builder (CI->getContext ());
832841 llvm::BasicBlock &FirstBB = F.getEntryBlock ();
833842 Builder.SetInsertPoint (&FirstBB, FirstBB.begin ());
834843 Type *ResTy = CI->getParamStructRetType (0 );
835- auto *TMPAlloca = Builder. CreateAlloca (
836- ResTy, nullptr , " lower_wg.local_copy" );
844+ auto *TMPAlloca =
845+ Builder. CreateAlloca ( ResTy, nullptr , " lower_wg.local_copy" );
837846 Builder.SetInsertPoint (CI->getNextNode ());
838- auto *LI = Builder.CreateLoad (ResTy, TMPAlloca, " lower_wg.private_load" );
847+ auto *LI =
848+ Builder.CreateLoad (ResTy, TMPAlloca, " lower_wg.private_load" );
839849 Builder.CreateStore (LI, ASCast->getPointerOperand ());
840850 ASCast->replaceAllUsesWith (TMPAlloca);
841851 ASCast->dropAllReferences ();
0 commit comments