Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 73 additions & 27 deletions lib/Transforms/Scalar/ScalarReplAggregatesHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6498,8 +6498,56 @@ ModulePass *llvm::createSROA_Parameter_HLSL() {
//===----------------------------------------------------------------------===//

namespace {

struct GVDebugInfoPatchCache {
std::unordered_map<Function *, SetVector<DISubprogram *>>
SubprogramsForFunction;
std::unordered_set<DILocation *> Seen;
DITypeIdentifierMap EmptyMap;

void CollectSubprograms(DILocation *Loc, SetVector<DISubprogram *> &Set) {
while (Loc) {
// This is potentially very expensive. Avoid repeatedly looking for
// DISubprogram's
if (Seen.count(Loc))
return;
Seen.insert(Loc);
auto *Scope = dyn_cast<DIScope>(Loc->getScope());
while (Scope) {
if (auto SubP = dyn_cast<DISubprogram>(Scope)) {
Set.insert(SubP);
break;
}
Scope = Scope->getScope().resolve(EmptyMap);
}
Loc = Loc->getInlinedAt();
}
}

SetVector<DISubprogram *> &
GetSubprogramsForFunction(Function *F, DebugInfoFinder &DbgFinder) {
auto It = SubprogramsForFunction.find(F);
if (It != SubprogramsForFunction.end())
return It->second;

SetVector<DISubprogram *> &Ret = SubprogramsForFunction[F];
for (DISubprogram *SP : DbgFinder.subprograms()) {
if (SP->getFunction() == F) {
Ret.insert(SP);
break;
}
}

for (BasicBlock &BB : *F)
for (Instruction &I : BB)
CollectSubprograms(I.getDebugLoc(), Ret);
return Ret;
}
};

class LowerStaticGlobalIntoAlloca : public ModulePass {
DebugInfoFinder m_DbgFinder;
GVDebugInfoPatchCache m_GVDebugInfoCache;

public:
static char ID; // Pass identification, replacement for typeid
Expand Down Expand Up @@ -6687,28 +6735,15 @@ FindGlobalVariableFragment(const DebugInfoFinder &DbgFinder,
// Create a fake local variable for the GlobalVariable GV that has just been
// lowered to local Alloca.
//
static void PatchDebugInfo(DebugInfoFinder &DbgFinder, Function *F,
static void PatchDebugInfo(GVDebugInfoPatchCache &Cache,
DebugInfoFinder &DbgFinder, Function *F,
GlobalVariable *GV, AllocaInst *AI) {
if (!DbgFinder.compile_unit_count())
return;

// Find the subprogram for function
DISubprogram *Subprogram = nullptr;
for (DISubprogram *SP : DbgFinder.subprograms()) {
if (SP->getFunction() == F) {
Subprogram = SP;
break;
}
}

DIGlobalVariable *DGV = dxilutil::FindGlobalVariableDebugInfo(GV, DbgFinder);
if (!DGV)
return;

DITypeIdentifierMap EmptyMap;
DIBuilder DIB(*GV->getParent());
DIScope *Scope = Subprogram;
DebugLoc Loc = DebugLoc::get(DGV->getLine(), 0, Scope);
if (!DbgFinder.compile_unit_count())
return;

// If the variable is a member of another variable, find the offset and size
bool IsFragment = false;
Expand All @@ -6725,20 +6760,31 @@ static void PatchDebugInfo(DebugInfoFinder &DbgFinder, Function *F,
// Subprogram as its scope, so we don't have to make one up for it.
llvm::dwarf::Tag Tag = llvm::dwarf::Tag::DW_TAG_arg_variable;

DITypeIdentifierMap EmptyMap;
DIType *Ty = DGV->getType().resolve(EmptyMap);
DXASSERT(Ty->getTag() != dwarf::DW_TAG_member,
"Member type is not allowed for variables.");
DILocalVariable *ConvertedLocalVar = DIB.createLocalVariable(
Tag, Scope, Name, DGV->getFile(), DGV->getLine(), Ty);

DIExpression *Expr = nullptr;
if (IsFragment) {
Expr = DIB.createBitPieceExpression(OffsetInBits, SizeInBits);
} else {
Expr = DIB.createExpression(ArrayRef<int64_t>());
}
DIBuilder DIB(*GV->getParent());

SetVector<DISubprogram *> &Subprograms =
Cache.GetSubprogramsForFunction(F, DbgFinder);
for (DISubprogram *Subprogram : Subprograms) {
DIScope *Scope = Subprogram;
DebugLoc Loc = DebugLoc::get(DGV->getLine(), 0, Scope);

DILocalVariable *ConvertedLocalVar = DIB.createLocalVariable(
Tag, Scope, Name, DGV->getFile(), DGV->getLine(), Ty);

DIB.insertDeclare(AI, ConvertedLocalVar, Expr, Loc, AI->getNextNode());
DIExpression *Expr = nullptr;
if (IsFragment) {
Expr = DIB.createBitPieceExpression(OffsetInBits, SizeInBits);
} else {
Expr = DIB.createExpression(ArrayRef<int64_t>());
}

DIB.insertDeclare(AI, ConvertedLocalVar, Expr, Loc, AI->getNextNode());
}
}

// Collect instructions using GV and the value used by the instruction.
Expand Down Expand Up @@ -6816,7 +6862,7 @@ bool LowerStaticGlobalIntoAlloca::lowerStaticGlobalIntoAlloca(
if (AI->user_empty())
AI->eraseFromParent();
else
PatchDebugInfo(m_DbgFinder, F, GV, AI);
PatchDebugInfo(m_GVDebugInfoCache, m_DbgFinder, F, GV, AI);
}

GV->removeDeadConstantUsers();
Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if this were an IR to IR test rather than HLSL to IR running the full pipeline.

DXC's hacked up SROA is quite messy and massively under tested. The ExtractIRForPassTest.py script should be able to turn this source into an IR test pretty easily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing. Can I also keep this HLSL test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an IR test. I want to keep the HLSL test too just to verify end-to-end.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %dxc -E main -T ps_6_0 -Od -Zi %s | FileCheck %s

// Check that the debug info for a global variable is present for every inlined scope

// CHECK: @main

// CHECK-DAG: ![[main_scope:[0-9]+]] = !DISubprogram(name: "main"
// CHECK-DAG: ![[main_var:[0-9]+]] = !DILocalVariable(tag: DW_TAG_arg_variable, name: "global.g_cond", arg: {{[0-9]+}}, scope: ![[main_scope]],
// CHECK-DAG: call void @llvm.dbg.value(metadata i32 %{{.+}}, metadata ![[main_var]],

// CHECK-DAG: ![[foo_scope:[0-9]+]] = !DISubprogram(name: "foo"
// CHECK-DAG: ![[foo_var:[0-9]+]] = !DILocalVariable(tag: DW_TAG_arg_variable, name: "global.g_cond", arg: {{[0-9]+}}, scope: ![[foo_scope]],
// CHECK-DAG: call void @llvm.dbg.value(metadata i32 %{{.+}}, metadata ![[foo_var]],

// CHECK-DAG: ![[bar_scope:[0-9]+]] = !DISubprogram(name: "bar"
// CHECK-DAG: ![[bar_var:[0-9]+]] = !DILocalVariable(tag: DW_TAG_arg_variable, name: "global.g_cond", arg: {{[0-9]+}}, scope: ![[bar_scope]],
// CHECK-DAG: call void @llvm.dbg.value(metadata i32 %{{.+}}, metadata ![[bar_var]],

static bool g_cond;

Texture2D tex0 : register(t0);
Texture2D tex1 : register(t1);

float4 bar() {
if (g_cond)
return tex0.Load(1);
return 0;
}

float4 foo() {
float4 ret = g_cond ? tex0.Load(0) : tex1.Load(0);
ret += bar();
return ret;
}

[RootSignature("DescriptorTable(SRV(t0, numDescriptors=2))")]
float4 main(uint a : A) : sv_target {
g_cond = a != 0;
return foo();
};