Skip to content

Commit 6640a01

Browse files
committed
[HLSL] Anaylze updateCounter usage
1 parent b07fc0f commit 6640a01

File tree

4 files changed

+240
-50
lines changed

4 files changed

+240
-50
lines changed

llvm/include/llvm/Analysis/DXILResource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ class DXILResourceMap {
463463
/// ambiguous so multiple creation instructions may be returned. The resulting
464464
/// ResourceInfo can be used to depuplicate unique handles that
465465
/// reference the same resource
466-
SmallVector<dxil::ResourceInfo> findByUse(const Value *Key) const;
466+
SmallVector<dxil::ResourceInfo *> findByUse(const Value *Key);
467467

468468
const_iterator find(const CallInst *Key) const {
469469
auto Pos = CallMap.find(Key);

llvm/lib/Analysis/DXILResource.cpp

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ 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

701704
void DXILResourceMap::populate(Module &M, DXILResourceTypeMap &DRTM) {
702705
SmallVector<std::tuple<CallInst *, ResourceInfo, ResourceTypeInfo>> CIToInfos;
@@ -775,6 +778,42 @@ void DXILResourceMap::populate(Module &M, DXILResourceTypeMap &DRTM) {
775778
// Adjust the resource binding to use the next ID.
776779
RI.setBindingID(NextID++);
777780
}
781+
782+
for (Function &F : M.functions()) {
783+
if (!isUpdateCounterIntrinsic(F))
784+
continue;
785+
786+
LLVM_DEBUG(dbgs() << "Update Counter Function: " << F.getName() << "\n");
787+
788+
for (const User *U : F.users()) {
789+
const CallInst *CI = dyn_cast<CallInst>(U);
790+
assert(CI && "Users of dx_resource_updateCounter must be call instrs");
791+
792+
// Determine if the use is an increment or decrement
793+
Value *CountArg = CI->getArgOperand(1);
794+
ConstantInt *CountValue = cast<ConstantInt>(CountArg);
795+
int64_t CountLiteral = CountValue->getSExtValue();
796+
797+
// 0 is an unknown direction and shouldn't result in an insert
798+
if (CountLiteral == 0)
799+
continue;
800+
801+
ResourceCounterDirection Direction = ResourceCounterDirection::Decrement;
802+
if (CountLiteral > 0)
803+
Direction = ResourceCounterDirection::Increment;
804+
805+
// Collect all potential creation points for the handle arg
806+
Value *HandleArg = CI->getArgOperand(0);
807+
SmallVector<ResourceInfo *> RBInfos = findByUse(HandleArg);
808+
for (ResourceInfo *RBInfo : RBInfos) {
809+
if (RBInfo->CounterDirection == ResourceCounterDirection::Unknown ||
810+
RBInfo->CounterDirection == Direction)
811+
RBInfo->CounterDirection = Direction;
812+
else
813+
RBInfo->CounterDirection = ResourceCounterDirection::Invalid;
814+
}
815+
}
816+
}
778817
}
779818

780819
void DXILResourceMap::print(raw_ostream &OS, DXILResourceTypeMap &DRTM,
@@ -793,10 +832,9 @@ void DXILResourceMap::print(raw_ostream &OS, DXILResourceTypeMap &DRTM,
793832
}
794833
}
795834

796-
SmallVector<dxil::ResourceInfo>
797-
DXILResourceMap::findByUse(const Value *Key) const {
835+
SmallVector<dxil::ResourceInfo *> DXILResourceMap::findByUse(const Value *Key) {
798836
if (const PHINode *Phi = dyn_cast<PHINode>(Key)) {
799-
SmallVector<dxil::ResourceInfo> Children;
837+
SmallVector<dxil::ResourceInfo *> Children;
800838
for (const Value *V : Phi->operands()) {
801839
Children.append(findByUse(V));
802840
}
@@ -810,9 +848,10 @@ DXILResourceMap::findByUse(const Value *Key) const {
810848
switch (CI->getIntrinsicID()) {
811849
// Found the create, return the binding
812850
case Intrinsic::dx_resource_handlefrombinding: {
813-
const auto *It = find(CI);
851+
auto Pos = CallMap.find(CI);
852+
ResourceInfo *It = &Infos[Pos->second];
814853
assert(It != Infos.end() && "HandleFromBinding must be in resource map");
815-
return {*It};
854+
return {It};
816855
}
817856
default:
818857
break;
@@ -821,7 +860,7 @@ DXILResourceMap::findByUse(const Value *Key) const {
821860
// Check if any of the parameters are the resource we are following. If so
822861
// keep searching. If none of them are return an empty list
823862
const Type *UseType = CI->getType();
824-
SmallVector<dxil::ResourceInfo> Children;
863+
SmallVector<dxil::ResourceInfo *> Children;
825864
for (const Value *V : CI->args()) {
826865
if (V->getType() != UseType)
827866
continue;

llvm/test/Analysis/DXILResource/buffer-frombinding.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ define void @test_typedbuffer() {
6969
%uav1 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
7070
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0(
7171
i32 3, i32 5, i32 1, i32 0, i1 false)
72+
call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav1, i8 -1)
7273
; CHECK: Resource [[UAV1:[0-9]+]]:
7374
; CHECK: Binding:
7475
; CHECK: Record ID: 1
7576
; CHECK: Space: 3
7677
; CHECK: Lower Bound: 5
7778
; CHECK: Size: 1
7879
; CHECK: Globally Coherent: 0
79-
; CHECK: Counter Direction: Unknown
80+
; CHECK: Counter Direction: Decrement
8081
; CHECK: Class: UAV
8182
; CHECK: Kind: TypedBuffer
8283
; CHECK: IsROV: 0
@@ -92,14 +93,15 @@ define void @test_typedbuffer() {
9293
%uav2_2 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
9394
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0(
9495
i32 4, i32 0, i32 10, i32 5, i1 false)
96+
call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav2_2, i8 1)
9597
; CHECK: Resource [[UAV2:[0-9]+]]:
9698
; CHECK: Binding:
9799
; CHECK: Record ID: 2
98100
; CHECK: Space: 4
99101
; CHECK: Lower Bound: 0
100102
; CHECK: Size: 10
101103
; CHECK: Globally Coherent: 0
102-
; CHECK: Counter Direction: Unknown
104+
; CHECK: Counter Direction: Increment
103105
; CHECK: Class: UAV
104106
; CHECK: Kind: TypedBuffer
105107
; CHECK: IsROV: 0

0 commit comments

Comments
 (0)