Skip to content

Commit f210433

Browse files
committed
Pare back to analysis only
1 parent cbe278b commit f210433

File tree

4 files changed

+58
-95
lines changed

4 files changed

+58
-95
lines changed

llvm/lib/Analysis/DXILResource.cpp

Lines changed: 25 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "llvm/ADT/SmallString.h"
1212
#include "llvm/ADT/StringRef.h"
1313
#include "llvm/IR/Constants.h"
14-
#include "llvm/IR/DebugLoc.h"
1514
#include "llvm/IR/DerivedTypes.h"
1615
#include "llvm/IR/DiagnosticInfo.h"
1716
#include "llvm/IR/Instructions.h"
@@ -22,7 +21,6 @@
2221
#include "llvm/InitializePasses.h"
2322
#include "llvm/Support/FormatVariadic.h"
2423
#include <algorithm>
25-
#include <iterator>
2624

2725
#define DEBUG_TYPE "dxil-resource"
2826

@@ -832,11 +830,6 @@ static bool isUpdateCounterIntrinsic(Function &F) {
832830
}
833831

834832
void DXILResourceCounterDirectionMap::populate(Module &M, DXILBindingMap &DBM) {
835-
SmallVector<
836-
std::tuple<const dxil::ResourceBindingInfo *, ResourceCounterDirection,
837-
const Function *, const CallInst *>>
838-
DiagCounterDirs;
839-
840833
for (Function &F : M.functions()) {
841834
if (!isUpdateCounterIntrinsic(F))
842835
continue;
@@ -863,82 +856,42 @@ void DXILResourceCounterDirectionMap::populate(Module &M, DXILBindingMap &DBM) {
863856
SmallVector<const dxil::ResourceBindingInfo *> RBInfos =
864857
DBM.findByUse(HandleArg);
865858
for (const dxil::ResourceBindingInfo *RBInfo : RBInfos)
866-
DiagCounterDirs.emplace_back(RBInfo, Direction, &F, CI);
859+
CounterDirections.emplace_back(RBInfo, Direction);
867860
}
868861
}
869862

870863
// Sort by the Binding and Direction for fast lookup
871-
std::sort(DiagCounterDirs.begin(), DiagCounterDirs.end(),
872-
[](const auto &LHS, const auto &RHS) {
873-
const auto L =
874-
std::pair{std::get<const dxil::ResourceBindingInfo *>(LHS),
875-
std::get<ResourceCounterDirection>(LHS)};
876-
const auto R =
877-
std::pair{std::get<const dxil::ResourceBindingInfo *>(RHS),
878-
std::get<ResourceCounterDirection>(RHS)};
879-
return L < R;
880-
});
864+
std::sort(CounterDirections.begin(), CounterDirections.end());
881865

882-
{
883-
auto *SpanStart = DiagCounterDirs.begin();
884-
auto *SpanEnd = SpanStart;
885-
auto *ItEnd = DiagCounterDirs.end();
886-
887-
while (SpanStart < ItEnd) {
888-
while (SpanEnd < ItEnd &&
889-
std::get<const dxil::ResourceBindingInfo *>(*SpanStart) ==
890-
std::get<const dxil::ResourceBindingInfo *>(*SpanEnd))
891-
SpanEnd++;
892-
893-
// SpanStart : SpanEnd-1 are the same binding. Its an error if they aren't
894-
// all the same direction
895-
ResourceCounterDirection ResourceDir =
896-
std::get<ResourceCounterDirection>(*SpanStart);
897-
bool ValidUse = true;
898-
for (auto *CheckIt = SpanStart; CheckIt < SpanEnd; ++CheckIt) {
899-
if (ResourceDir != std::get<ResourceCounterDirection>(*CheckIt)) {
900-
ValidUse = false;
901-
break;
902-
}
903-
}
866+
// Remove the duplicate entries. Since direction is considered for equality
867+
// a unique resource with more than one direction will not be deduped.
868+
auto UniqueEnd =
869+
std::unique(CounterDirections.begin(), CounterDirections.end());
904870

905-
// Update the direction and raise a diag when an invalid use is detected
906-
for (auto *RaiseIt = SpanStart; !ValidUse && RaiseIt < SpanEnd;
907-
++RaiseIt) {
908-
std::get<ResourceCounterDirection>(*RaiseIt) =
871+
// If any duplicate entries still exist at this point then it must be a
872+
// resource that was both incremented and decremented which is not allowed.
873+
// Mark all those entries as invalid.
874+
{
875+
auto DupFirst = CounterDirections.begin();
876+
auto DupNext = DupFirst + 1;
877+
auto DupLast = UniqueEnd;
878+
for (; DupFirst < DupLast && DupNext < DupLast; ++DupFirst, ++DupNext) {
879+
if (std::get<const dxil::ResourceBindingInfo *>(*DupFirst) ==
880+
std::get<const dxil::ResourceBindingInfo *>(*DupNext)) {
881+
std::get<ResourceCounterDirection>(*DupFirst) =
882+
ResourceCounterDirection::Invalid;
883+
std::get<ResourceCounterDirection>(*DupNext) =
909884
ResourceCounterDirection::Invalid;
910-
911-
StringRef Message =
912-
"RWStructuredBuffers may increment or decrement their "
913-
"counters, but not both.";
914-
const Function *F = std::get<const Function *>(*RaiseIt);
915-
const CallInst *CI = std::get<const CallInst *>(*RaiseIt);
916-
M.getContext().diagnose(
917-
DiagnosticInfoGenericWithLoc(Message, *F, CI->getDebugLoc()));
918885
}
919-
920-
SpanStart = SpanEnd;
921886
}
922887
}
923888

924-
// Remove the duplicate entries. All entries with the same resource have the
925-
// same direction so it can be ignored.
926-
auto *const UniqueEnd =
927-
std::unique(DiagCounterDirs.begin(), DiagCounterDirs.end(),
928-
[](const auto &LHS, const auto &RHS) {
929-
return std::get<const dxil::ResourceBindingInfo *>(LHS) ==
930-
std::get<const dxil::ResourceBindingInfo *>(RHS);
931-
});
932-
933-
// Copy the results into the final location
934-
CounterDirections.clear();
935-
CounterDirections.reserve(UniqueEnd - DiagCounterDirs.begin());
936-
std::transform(DiagCounterDirs.begin(), UniqueEnd,
937-
std::back_inserter(CounterDirections), [](const auto &Item) {
938-
return std::pair{
939-
std::get<const dxil::ResourceBindingInfo *>(Item),
940-
std::get<ResourceCounterDirection>(Item)};
941-
});
889+
// Remove the duplicate entries again now that each duplicate resource has the
890+
// same direction for each entry leaving one entry per RBI
891+
UniqueEnd = std::unique(CounterDirections.begin(), UniqueEnd);
892+
893+
// Actually erase the invalidated items
894+
CounterDirections.erase(UniqueEnd, CounterDirections.end());
942895
}
943896

944897
ResourceCounterDirection DXILResourceCounterDirectionMap::operator[](

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -859,14 +859,6 @@ PreservedAnalyses DXILOpLowering::run(Module &M, ModuleAnalysisManager &MAM) {
859859
DXILBindingMap &DBM = MAM.getResult<DXILResourceBindingAnalysis>(M);
860860
DXILResourceTypeMap &DRTM = MAM.getResult<DXILResourceTypeAnalysis>(M);
861861

862-
// TODO: This needs to be called even though its not currently being used in
863-
// order for tests to pass. It will eventually need to be used as part of
864-
// https://github.com/llvm/llvm-project/issues/125126 which will have the same
865-
// effect. Until then discard the result.
866-
DXILResourceCounterDirectionMap &DRCDM =
867-
MAM.getResult<DXILResourceCounterDirectionAnalysis>(M);
868-
(void)DRCDM;
869-
870862
bool MadeChanges = OpLowerer(M, DBM, DRTM).lowerIntrinsics();
871863
if (!MadeChanges)
872864
return PreservedAnalyses::all();

llvm/test/CodeGen/DirectX/resource_counter_error.ll

Lines changed: 0 additions & 13 deletions
This file was deleted.

llvm/unittests/Target/DirectX/UniqueResourceFromUseTests.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ define void @main() {
371371
}
372372
}
373373

374-
// Test that multiple different resources with unique incs/decs don't
375-
// raise a Diagnostic and can be individually checked for direction
374+
// Test that multiple different resources with unique incs/decs aren't
375+
// marked invalid
376376
TEST_F(UniqueResourceFromUseTest, TestResourceCounterMultiple) {
377377
StringRef Assembly = R"(
378378
define void @main() {
@@ -409,4 +409,35 @@ define void @main() {
409409
}
410410
}
411411

412+
// Test that single different resources with unique incs/decs is marked invalid
413+
TEST_F(UniqueResourceFromUseTest, TestResourceCounterInvalid) {
414+
StringRef Assembly = R"(
415+
define void @main() {
416+
entry:
417+
%handle = call target("dx.RawBuffer", float, 1, 0) @llvm.dx.resource.handlefrombinding(i32 1, i32 2, i32 3, i32 4, i1 false)
418+
call i32 @llvm.dx.resource.updatecounter(target("dx.RawBuffer", float, 1, 0) %handle, i8 -1)
419+
call i32 @llvm.dx.resource.updatecounter(target("dx.RawBuffer", float, 1, 0) %handle, i8 1)
420+
ret void
421+
}
422+
)";
423+
424+
auto M = parseAsm(Assembly);
425+
426+
const DXILBindingMap &DBM = MAM->getResult<DXILResourceBindingAnalysis>(*M);
427+
const DXILResourceCounterDirectionMap &DCDM =
428+
MAM->getResult<DXILResourceCounterDirectionAnalysis>(*M);
429+
430+
for (const Function &F : M->functions()) {
431+
if (F.getIntrinsicID() != Intrinsic::dx_resource_handlefrombinding) {
432+
continue;
433+
}
434+
435+
for (const User *U : F.users()) {
436+
const CallInst *CI = cast<CallInst>(U);
437+
const auto *const Binding = DBM.find(CI);
438+
ASSERT_EQ(DCDM[*Binding], ResourceCounterDirection::Invalid);
439+
}
440+
}
441+
}
442+
412443
} // namespace

0 commit comments

Comments
 (0)