@@ -697,8 +697,12 @@ bool DXILResourceTypeMap::invalidate(Module &M, const PreservedAnalyses &PA,
697697}
698698
699699// ===----------------------------------------------------------------------===//
700+ static bool isUpdateCounterIntrinsic (Function &F) {
701+ return F.getIntrinsicID () == Intrinsic::dx_resource_updatecounter;
702+ }
700703
701- void DXILResourceMap::populate (Module &M, DXILResourceTypeMap &DRTM) {
704+ void DXILResourceMap::populateResourceInfos (Module &M,
705+ DXILResourceTypeMap &DRTM) {
702706 SmallVector<std::tuple<CallInst *, ResourceInfo, ResourceTypeInfo>> CIToInfos;
703707
704708 for (Function &F : M.functions ()) {
@@ -777,6 +781,48 @@ void DXILResourceMap::populate(Module &M, DXILResourceTypeMap &DRTM) {
777781 }
778782}
779783
784+ void DXILResourceMap::populateCounterDirections (Module &M) {
785+ for (Function &F : M.functions ()) {
786+ if (!isUpdateCounterIntrinsic (F))
787+ continue ;
788+
789+ LLVM_DEBUG (dbgs () << " Update Counter Function: " << F.getName () << " \n " );
790+
791+ for (const User *U : F.users ()) {
792+ const CallInst *CI = dyn_cast<CallInst>(U);
793+ assert (CI && " Users of dx_resource_updateCounter must be call instrs" );
794+
795+ // Determine if the use is an increment or decrement
796+ Value *CountArg = CI->getArgOperand (1 );
797+ ConstantInt *CountValue = cast<ConstantInt>(CountArg);
798+ int64_t CountLiteral = CountValue->getSExtValue ();
799+
800+ // 0 is an unknown direction and shouldn't result in an insert
801+ if (CountLiteral == 0 )
802+ continue ;
803+
804+ ResourceCounterDirection Direction = ResourceCounterDirection::Decrement;
805+ if (CountLiteral > 0 )
806+ Direction = ResourceCounterDirection::Increment;
807+
808+ // Collect all potential creation points for the handle arg
809+ Value *HandleArg = CI->getArgOperand (0 );
810+ SmallVector<ResourceInfo *> RBInfos = findByUse (HandleArg);
811+ for (ResourceInfo *RBInfo : RBInfos) {
812+ if (RBInfo->CounterDirection == ResourceCounterDirection::Unknown)
813+ RBInfo->CounterDirection = Direction;
814+ else if (RBInfo->CounterDirection != Direction)
815+ RBInfo->CounterDirection = ResourceCounterDirection::Invalid;
816+ }
817+ }
818+ }
819+ }
820+
821+ void DXILResourceMap::populate (Module &M, DXILResourceTypeMap &DRTM) {
822+ populateResourceInfos (M, DRTM);
823+ populateCounterDirections (M);
824+ }
825+
780826void DXILResourceMap::print (raw_ostream &OS, DXILResourceTypeMap &DRTM,
781827 const DataLayout &DL) const {
782828 for (unsigned I = 0 , E = Infos.size (); I != E; ++I) {
@@ -793,10 +839,9 @@ void DXILResourceMap::print(raw_ostream &OS, DXILResourceTypeMap &DRTM,
793839 }
794840}
795841
796- SmallVector<dxil::ResourceInfo>
797- DXILResourceMap::findByUse (const Value *Key) const {
842+ SmallVector<dxil::ResourceInfo *> DXILResourceMap::findByUse (const Value *Key) {
798843 if (const PHINode *Phi = dyn_cast<PHINode>(Key)) {
799- SmallVector<dxil::ResourceInfo> Children;
844+ SmallVector<dxil::ResourceInfo * > Children;
800845 for (const Value *V : Phi->operands ()) {
801846 Children.append (findByUse (V));
802847 }
@@ -810,9 +855,9 @@ DXILResourceMap::findByUse(const Value *Key) const {
810855 switch (CI->getIntrinsicID ()) {
811856 // Found the create, return the binding
812857 case Intrinsic::dx_resource_handlefrombinding: {
813- const auto *It = find (CI);
814- assert (It != Infos .end () && " HandleFromBinding must be in resource map" );
815- return {*It };
858+ auto Pos = CallMap. find (CI);
859+ assert (Pos != CallMap .end () && " HandleFromBinding must be in resource map" );
860+ return {&Infos[Pos-> second ] };
816861 }
817862 default :
818863 break ;
@@ -821,7 +866,7 @@ DXILResourceMap::findByUse(const Value *Key) const {
821866 // Check if any of the parameters are the resource we are following. If so
822867 // keep searching. If none of them are return an empty list
823868 const Type *UseType = CI->getType ();
824- SmallVector<dxil::ResourceInfo> Children;
869+ SmallVector<dxil::ResourceInfo * > Children;
825870 for (const Value *V : CI->args ()) {
826871 if (V->getType () != UseType)
827872 continue ;
0 commit comments