@@ -435,13 +435,24 @@ struct AAAMDAttributesFunction : public AAAMDAttributes {
435435 return ;
436436 }
437437
438+ SmallPtrSet<const Constant *, 8 > VisitedConsts;
439+
438440 for (Instruction &I : instructions (F)) {
439441 if (isa<AddrSpaceCastInst>(I) &&
440- cast<AddrSpaceCastInst & >(I).getSrcAddressSpace () ==
442+ cast<AddrSpaceCastInst>(I).getSrcAddressSpace () ==
441443 AMDGPUAS::PRIVATE_ADDRESS) {
442444 removeAssumedBits (FLAT_SCRATCH_INIT);
443445 return ;
444446 }
447+ // check for addrSpaceCast in constant expressions
448+ for (const Use &U : I.operands ()) {
449+ if (const auto *C = dyn_cast<Constant>(U)) {
450+ if (constHasASCast (C, VisitedConsts)) {
451+ removeAssumedBits (FLAT_SCRATCH_INIT);
452+ return ;
453+ }
454+ }
455+ }
445456 }
446457 }
447458
@@ -709,9 +720,9 @@ struct AAAMDAttributesFunction : public AAAMDAttributes {
709720 // already removed in updateImpl() and execution won't reach here.
710721 if (!Callee)
711722 return true ;
712- else
713- return Callee->getIntrinsicID () !=
714- Intrinsic::amdgcn_addrspacecast_nonnull;
723+
724+ return Callee->getIntrinsicID () !=
725+ Intrinsic::amdgcn_addrspacecast_nonnull;
715726 };
716727
717728 bool UsedAssumedInformation = false ;
@@ -721,6 +732,28 @@ struct AAAMDAttributesFunction : public AAAMDAttributes {
721732 return !A.checkForAllCallLikeInstructions (CheckForNoFlatScratchInit, *this ,
722733 UsedAssumedInformation);
723734 }
735+
736+ bool constHasASCast (const Constant *C,
737+ SmallPtrSetImpl<const Constant *> &Visited) {
738+ if (!Visited.insert (C).second )
739+ return false ;
740+
741+ if (const auto *CE = dyn_cast<ConstantExpr>(C))
742+ if (CE->getOpcode () == Instruction::AddrSpaceCast &&
743+ CE->getOperand (0 )->getType ()->getPointerAddressSpace () ==
744+ AMDGPUAS::PRIVATE_ADDRESS)
745+ return true ;
746+
747+ for (const Use &U : C->operands ()) {
748+ const auto *OpC = dyn_cast<Constant>(U);
749+ if (!OpC || !Visited.insert (OpC).second )
750+ continue ;
751+
752+ if (constHasASCast (OpC, Visited))
753+ return true ;
754+ }
755+ return false ;
756+ }
724757};
725758
726759AAAMDAttributes &AAAMDAttributes::createForPosition (const IRPosition &IRP,
0 commit comments