Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 8d79e00

Browse files
authored
Printing out line num and debug variable and expression when disassembling. (microsoft#2738)
1 parent 73e5b38 commit 8d79e00

File tree

7 files changed

+90
-7
lines changed

7 files changed

+90
-7
lines changed

include/llvm/IR/Metadata.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,10 @@ class MDNode : public Metadata {
966966
static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
967967
static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
968968
static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B);
969+
970+
/// \brief Methods to print body of node, ie. without the '<addr> = ' prefix
971+
void printAsBody(raw_ostream &OS, const Module *M = nullptr) const; // HLSL Change
972+
void printAsBody(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr) const; // HLSL Change
969973
};
970974

971975
/// \brief Tuple of metadata.

lib/IR/AsmWriter.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3352,6 +3352,19 @@ void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
33523352
printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
33533353
}
33543354

3355+
// HLSL Change Begin
3356+
void MDNode::printAsBody(raw_ostream &OS, const Module *M) const {
3357+
ModuleSlotTracker MST(M, true);
3358+
printAsBody(OS, MST, M);
3359+
}
3360+
void MDNode::printAsBody(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M) const {
3361+
TypePrinting TypePrinter;
3362+
if (M)
3363+
TypePrinter.incorporateTypes(*M);
3364+
WriteMDNodeBodyInternal(OS, this, &TypePrinter, MST.getMachine(), M);
3365+
}
3366+
// HLSL Change end
3367+
33553368
// Value::dump - allow easy printing of Values from the debugger.
33563369
LLVM_DUMP_METHOD
33573370
void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %dxc -E main -T ps_6_0 %s -Zi -Od | FileCheck %s
2+
3+
[RootSignature("")]
4+
float main(float a : A) : SV_Target {
5+
float2 xy = float2(0,0);
6+
xy.x = sin(a);
7+
// CHECK: call float @dx.op.unary.f32(i32 13,
8+
// CHECK-SAME: line:6
9+
10+
// CHECK: call void @llvm.dbg.value(
11+
// CHECK-SAME: var:"xy"
12+
// CHECK-SAME: !DIExpression(DW_OP_bit_piece, 0, 32)
13+
14+
xy.y = cos(xy.x);
15+
// CHECK: call float @dx.op.unary.f32(i32 12,
16+
// CHECK-SAME: line:14
17+
18+
// CHECK: call void @llvm.dbg.value(
19+
// CHECK-SAME: var:"xy"
20+
// CHECK-SAME: !DIExpression(DW_OP_bit_piece, 32, 32)
21+
22+
float z = abs(xy.y);
23+
// CHECK: call float @dx.op.unary.f32(i32 6,
24+
// CHECK-SAME: line:22
25+
26+
// CHECK: call void @llvm.dbg.value(
27+
// CHECK-SAME: var:"z"
28+
// CHECK-SAME: !DIExpression()
29+
30+
float w = tan(z);
31+
// CHECK: call float @dx.op.unary.f32(i32 14,
32+
// CHECK-SAME: line:30
33+
34+
// CHECK: call void @llvm.dbg.value(
35+
// CHECK-SAME: var:"w"
36+
// CHECK-SAME: !DIExpression()
37+
38+
return w;
39+
}
40+

tools/clang/test/HLSLFileCheck/dxil/debug/locals/scalarized_vector.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
// CHECK: %[[x:.*]] = add i32
66
// CHECK: %[[y:.*]] = add i32
7-
// CHECK-DAG: call void @llvm.dbg.value(metadata i32 %[[x]], i64 0, metadata ![[vec:.*]], metadata ![[xexp:.*]])
8-
// CHECK-DAG: call void @llvm.dbg.value(metadata i32 %[[y]], i64 0, metadata ![[vec]], metadata ![[yexp:.*]])
7+
// CHECK-DAG: call void @llvm.dbg.value(metadata i32 %[[x]], i64 0, metadata ![[vec:.*]], metadata ![[xexp:.*]]), !dbg
8+
// CHECK-DAG: call void @llvm.dbg.value(metadata i32 %[[y]], i64 0, metadata ![[vec]], metadata ![[yexp:.*]]), !dbg
99

1010
// Exclude quoted source file (see readme)
1111
// CHECK-LABEL: {{!"[^"]*\\0A[^"]*"}}
@@ -18,4 +18,4 @@ int2 main(int2 a : A, int2 b : B) : OUT
1818
{
1919
int2 vec = a + b;
2020
return vec;
21-
}
21+
}

tools/clang/test/HLSLFileCheck/dxil/debug/types/double.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// CHECK: %[[bufret:.*]] = call %dx.types.CBufRet.f64 @dx.op.cbufferLoadLegacy.f64
66
// CHECK: %[[d:.*]] = extractvalue %dx.types.CBufRet.f64 %[[bufret]], 0
7-
// CHECK: call void @llvm.dbg.value(metadata double %[[d]], i64 0, metadata ![[divar:.*]], metadata ![[diexpr:.*]])
7+
// CHECK: call void @llvm.dbg.value(metadata double %[[d]], i64 0, metadata ![[divar:.*]], metadata ![[diexpr:.*]]), !dbg
88

99
// CHECK: !DIBasicType(name: "double", size: 64, align: 64, encoding: DW_ATE_float)
1010

@@ -19,4 +19,4 @@ float main() : OUT
1919
{
2020
double d = cb_d;
2121
return (float)d;
22-
}
22+
}

tools/clang/test/HLSLFileCheck/dxil/debug/types/half.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// CHECK: %[[bufret:.*]] = call %dx.types.CBufRet.f16.8 @dx.op.cbufferLoadLegacy.f16
66
// CHECK: %[[h:.*]] = extractvalue %dx.types.CBufRet.f16.8 %[[bufret]], 0
7-
// CHECK: call void @llvm.dbg.value(metadata half %[[h]], i64 0, metadata ![[divar:.*]], metadata ![[diexpr:.*]])
7+
// CHECK: call void @llvm.dbg.value(metadata half %[[h]], i64 0, metadata ![[divar:.*]], metadata ![[diexpr:.*]]), !dbg
88

99
// CHECK: !DIBasicType(name: "half", size: 16, align: 16, encoding: DW_ATE_float)
1010

@@ -19,4 +19,4 @@ float main() : OUT
1919
{
2020
half h = cb_h;
2121
return (float)h;
22-
}
22+
}

tools/clang/tools/dxcompiler/dxcdisassembler.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#include "dxc/DXIL/DxilOperations.h"
2424
#include "llvm/IR/DiagnosticInfo.h"
2525
#include "llvm/IR/DiagnosticPrinter.h"
26+
#include "llvm/IR/IntrinsicInst.h"
2627
#include "llvm/IR/AssemblyAnnotationWriter.h"
28+
#include "llvm/IR/DebugInfoMetadata.h"
2729
#include "llvm/Support/FormattedStream.h"
2830
#include "llvm/Support/Format.h"
2931
#include "dxc/DxilContainer/DxilPipelineStateValidation.h"
@@ -1287,6 +1289,30 @@ class DxcAssemblyAnnotationWriter : public llvm::AssemblyAnnotationWriter {
12871289
public:
12881290
~DxcAssemblyAnnotationWriter() {}
12891291
void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
1292+
if (const Instruction *I = dyn_cast<Instruction>(&V)) {
1293+
if (isa<DbgInfoIntrinsic>(I)) {
1294+
DILocalVariable *Var = nullptr;
1295+
DIExpression *Expr = nullptr;
1296+
if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
1297+
Var = DI->getVariable();
1298+
Expr = DI->getExpression();
1299+
}
1300+
else if (const DbgValueInst *DI = dyn_cast<DbgValueInst>(I)) {
1301+
Var = DI->getVariable();
1302+
Expr = DI->getExpression();
1303+
}
1304+
1305+
if (Var && Expr) {
1306+
OS << " ; var:\"" << Var->getName() << "\"" << " ";
1307+
Expr->printAsBody(OS);
1308+
}
1309+
}
1310+
else {
1311+
DebugLoc Loc = I->getDebugLoc();
1312+
if (Loc && Loc.getLine() != 0)
1313+
OS << " ; line:" << Loc.getLine() << " col:" << Loc.getCol();
1314+
}
1315+
}
12901316
const CallInst *CI = dyn_cast<const CallInst>(&V);
12911317
if (!CI) {
12921318
return;

0 commit comments

Comments
 (0)