Skip to content

Commit adf7a0a

Browse files
yurydelendikdschuff
authored andcommitted
[WebAssembly] Use TargetIndex operands in DbgValue to track WebAssembly operands locations
Extends DWARF expression language to express locals/globals locations. (via target-index operands atm) (possible variants are: non-virtual registers or address spaces) The WebAssemblyExplicitLocals can replace virtual registers to targertindex operand type at the time when WebAssembly backend introduces {get,set,tee}_local instead of corresponding virtual registers. Reviewed By: aprantl, dschuff Tags: #debug-info, #llvm Differential Revision: https://reviews.llvm.org/D52634
1 parent 538b485 commit adf7a0a

16 files changed

+244
-4
lines changed

llvm/include/llvm/BinaryFormat/Dwarf.def

+2
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ HANDLE_DW_OP(0xa9, reinterpret, 5, DWARF)
648648
// Vendor extensions:
649649
// Extensions for GNU-style thread-local storage.
650650
HANDLE_DW_OP(0xe0, GNU_push_tls_address, 0, GNU)
651+
// Extensions for WebAssembly.
652+
HANDLE_DW_OP(0xed, WASM_location, 0, WASM)
651653
// The GNU entry value extension.
652654
HANDLE_DW_OP(0xf3, GNU_entry_value, 0, GNU)
653655
// Extensions for Fission proposal.

llvm/include/llvm/BinaryFormat/Dwarf.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ enum LLVMConstants : uint32_t {
6363
DWARF_VENDOR_GNU = 3,
6464
DWARF_VENDOR_GOOGLE = 4,
6565
DWARF_VENDOR_LLVM = 5,
66-
DWARF_VENDOR_MIPS = 6
66+
DWARF_VENDOR_MIPS = 6,
67+
DWARF_VENDOR_WASM = 7
6768
};
6869

6970
/// Constants that define the DWARF format as 32 or 64 bit.

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,10 @@ static bool emitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) {
879879
OS << MI->getOperand(0).getImm();
880880
} else if (MI->getOperand(0).isCImm()) {
881881
MI->getOperand(0).getCImm()->getValue().print(OS, false /*isSigned*/);
882+
} else if (MI->getOperand(0).isTargetIndex()) {
883+
auto Op = MI->getOperand(0);
884+
OS << "!target-index(" << Op.getIndex() << "," << Op.getOffset() << ")";
885+
return true;
882886
} else {
883887
unsigned Reg;
884888
if (MI->getOperand(0).isReg()) {

llvm/lib/CodeGen/AsmPrinter/DebugLocEntry.h

+35-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,33 @@
2020
namespace llvm {
2121
class AsmPrinter;
2222

23+
/// This struct describes target specific location.
24+
struct TargetIndexLocation {
25+
int Index;
26+
int Offset;
27+
28+
TargetIndexLocation() = default;
29+
TargetIndexLocation(unsigned Idx, int64_t Offset)
30+
: Index(Idx), Offset(Offset) {}
31+
32+
bool operator==(const TargetIndexLocation &Other) const {
33+
return Index == Other.Index && Offset == Other.Offset;
34+
}
35+
};
36+
2337
/// A single location or constant.
2438
class DbgValueLoc {
2539
/// Any complex address location expression for this DbgValueLoc.
2640
const DIExpression *Expression;
2741

2842
/// Type of entry that this represents.
29-
enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
43+
enum EntryType {
44+
E_Location,
45+
E_Integer,
46+
E_ConstantFP,
47+
E_ConstantInt,
48+
E_TargetIndexLocation
49+
};
3050
enum EntryType EntryKind;
3151

3252
/// Either a constant,
@@ -36,8 +56,12 @@ class DbgValueLoc {
3656
const ConstantInt *CIP;
3757
} Constant;
3858

39-
/// Or a location in the machine frame.
40-
MachineLocation Loc;
59+
union {
60+
/// Or a location in the machine frame.
61+
MachineLocation Loc;
62+
/// Or a location from target specific location.
63+
TargetIndexLocation TIL;
64+
};
4165

4266
public:
4367
DbgValueLoc(const DIExpression *Expr, int64_t i)
@@ -56,15 +80,21 @@ class DbgValueLoc {
5680
: Expression(Expr), EntryKind(E_Location), Loc(Loc) {
5781
assert(cast<DIExpression>(Expr)->isValid());
5882
}
83+
DbgValueLoc(const DIExpression *Expr, TargetIndexLocation Loc)
84+
: Expression(Expr), EntryKind(E_TargetIndexLocation), TIL(Loc) {}
5985

6086
bool isLocation() const { return EntryKind == E_Location; }
87+
bool isTargetIndexLocation() const {
88+
return EntryKind == E_TargetIndexLocation;
89+
}
6190
bool isInt() const { return EntryKind == E_Integer; }
6291
bool isConstantFP() const { return EntryKind == E_ConstantFP; }
6392
bool isConstantInt() const { return EntryKind == E_ConstantInt; }
6493
int64_t getInt() const { return Constant.Int; }
6594
const ConstantFP *getConstantFP() const { return Constant.CFP; }
6695
const ConstantInt *getConstantInt() const { return Constant.CIP; }
6796
MachineLocation getLoc() const { return Loc; }
97+
TargetIndexLocation getTargetIndexLocation() const { return TIL; }
6898
bool isFragment() const { return getExpression()->isFragment(); }
6999
bool isEntryVal() const { return getExpression()->isEntryValue(); }
70100
const DIExpression *getExpression() const { return Expression; }
@@ -162,6 +192,8 @@ inline bool operator==(const DbgValueLoc &A,
162192
switch (A.EntryKind) {
163193
case DbgValueLoc::E_Location:
164194
return A.Loc == B.Loc;
195+
case DbgValueLoc::E_TargetIndexLocation:
196+
return A.TIL == B.TIL;
165197
case DbgValueLoc::E_Integer:
166198
return A.Constant.Int == B.Constant.Int;
167199
case DbgValueLoc::E_ConstantFP:

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ static DbgValueLoc getDebugLocValue(const MachineInstr *MI) {
241241
MachineLocation MLoc(RegOp.getReg(), Op1.isImm());
242242
return DbgValueLoc(Expr, MLoc);
243243
}
244+
if (MI->getOperand(0).isTargetIndex()) {
245+
auto Op = MI->getOperand(0);
246+
return DbgValueLoc(Expr,
247+
TargetIndexLocation(Op.getIndex(), Op.getOffset()));
248+
}
244249
if (MI->getOperand(0).isImm())
245250
return DbgValueLoc(Expr, MI->getOperand(0).getImm());
246251
if (MI->getOperand(0).isFPImm())
@@ -2241,6 +2246,11 @@ void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
22412246
if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
22422247
return;
22432248
return DwarfExpr.addExpression(std::move(Cursor));
2249+
} else if (Value.isTargetIndexLocation()) {
2250+
TargetIndexLocation Loc = Value.getTargetIndexLocation();
2251+
// TODO TargetIndexLocation is a target-independent. Currently only the WebAssembly-specific
2252+
// encoding is supported.
2253+
DwarfExpr.addWasmLocation(Loc.Index, Loc.Offset);
22442254
} else if (Value.isConstantFP()) {
22452255
APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
22462256
DwarfExpr.addUnsignedConstant(RawBytes);

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,11 @@ void DwarfExpression::emitLegacyZExt(unsigned FromBits) {
572572
emitUnsigned((1ULL << FromBits) - 1);
573573
emitOp(dwarf::DW_OP_and);
574574
}
575+
576+
void DwarfExpression::addWasmLocation(unsigned Index, int64_t Offset) {
577+
assert(LocationKind == Implicit || LocationKind == Unknown);
578+
LocationKind = Implicit;
579+
emitOp(dwarf::DW_OP_WASM_location);
580+
emitUnsigned(Index);
581+
emitSigned(Offset);
582+
}

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h

+4
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ class DwarfExpression {
337337

338338
void emitLegacySExt(unsigned FromBits);
339339
void emitLegacyZExt(unsigned FromBits);
340+
341+
/// Emit location information expressed via WebAssembly location + offset
342+
/// The Index is an identifier for locals, globals or operand stack.
343+
void addWasmLocation(unsigned Index, int64_t Offset);
340344
};
341345

342346
/// DwarfExpression implementation for .debug_loc entries.

llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ static DescVector getDescriptions() {
9393
Descriptions[DW_OP_implicit_value] =
9494
Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeBlock);
9595
Descriptions[DW_OP_stack_value] = Desc(Op::Dwarf3);
96+
Descriptions[DW_OP_WASM_location] =
97+
Desc(Op::Dwarf4, Op::SizeLEB, Op::SignedSizeLEB);
9698
Descriptions[DW_OP_GNU_push_tls_address] = Desc(Op::Dwarf3);
9799
Descriptions[DW_OP_addrx] = Desc(Op::Dwarf4, Op::SizeLEB);
98100
Descriptions[DW_OP_GNU_addr_index] = Desc(Op::Dwarf4, Op::SizeLEB);

llvm/lib/Target/WebAssembly/WebAssembly.h

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ void initializeWebAssemblyRegNumberingPass(PassRegistry &);
7979
void initializeWebAssemblyPeepholePass(PassRegistry &);
8080
void initializeWebAssemblyCallIndirectFixupPass(PassRegistry &);
8181

82+
namespace WebAssembly {
83+
enum TargetIndex { TI_LOCAL_START, TI_GLOBAL_START, TI_OPERAND_STACK_START };
84+
} // end namespace WebAssembly
85+
8286
} // end namespace llvm
8387

8488
#endif

llvm/lib/Target/WebAssembly/WebAssemblyDebugValueManager.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "WebAssemblyDebugValueManager.h"
15+
#include "WebAssembly.h"
1516
#include "WebAssemblyMachineFunctionInfo.h"
1617
#include "llvm/CodeGen/MachineInstr.h"
1718

@@ -43,3 +44,10 @@ void WebAssemblyDebugValueManager::clone(MachineInstr *Insert,
4344
MBB->insert(Insert, Clone);
4445
}
4546
}
47+
48+
void WebAssemblyDebugValueManager::replaceWithLocal(unsigned LocalId) {
49+
for (auto *DBI : DbgValues) {
50+
MachineOperand &Op = DBI->getOperand(0);
51+
Op.ChangeToTargetIndex(llvm::WebAssembly::TI_LOCAL_START, LocalId);
52+
}
53+
}

llvm/lib/Target/WebAssembly/WebAssemblyDebugValueManager.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class WebAssemblyDebugValueManager {
3030
void move(MachineInstr *Insert);
3131
void updateReg(unsigned Reg);
3232
void clone(MachineInstr *Insert, unsigned NewReg);
33+
void replaceWithLocal(unsigned LocalId);
3334
};
3435

3536
} // end namespace llvm

llvm/lib/Target/WebAssembly/WebAssemblyExplicitLocals.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
1919
#include "WebAssembly.h"
20+
#include "WebAssemblyDebugValueManager.h"
2021
#include "WebAssemblyMachineFunctionInfo.h"
2122
#include "WebAssemblySubtarget.h"
2223
#include "WebAssemblyUtilities.h"
@@ -261,6 +262,8 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
261262
.addImm(LocalId)
262263
.addReg(MI.getOperand(2).getReg());
263264

265+
WebAssemblyDebugValueManager(&MI).replaceWithLocal(LocalId);
266+
264267
MI.eraseFromParent();
265268
Changed = true;
266269
continue;
@@ -290,6 +293,9 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
290293
} else {
291294
unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
292295
unsigned Opc = getLocalSetOpcode(RC);
296+
297+
WebAssemblyDebugValueManager(&MI).replaceWithLocal(LocalId);
298+
293299
BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc))
294300
.addImm(LocalId)
295301
.addReg(NewReg);

llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "WebAssemblyInstrInfo.h"
1616
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17+
#include "WebAssembly.h"
1718
#include "WebAssemblyMachineFunctionInfo.h"
1819
#include "WebAssemblySubtarget.h"
1920
#include "llvm/CodeGen/MachineFrameInfo.h"
@@ -230,3 +231,12 @@ bool WebAssemblyInstrInfo::reverseBranchCondition(
230231
Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
231232
return false;
232233
}
234+
235+
ArrayRef<std::pair<int, const char *>>
236+
WebAssemblyInstrInfo::getSerializableTargetIndices() const {
237+
static const std::pair<int, const char *> TargetIndices[] = {
238+
{WebAssembly::TI_LOCAL_START, "wasm-local-start"},
239+
{WebAssembly::TI_GLOBAL_START, "wasm-global-start"},
240+
{WebAssembly::TI_OPERAND_STACK_START, "wasm-operator-stack-start"}};
241+
return makeArrayRef(TargetIndices);
242+
}

llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.h

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYINSTRINFO_H
1717

1818
#include "WebAssemblyRegisterInfo.h"
19+
#include "llvm/ADT/ArrayRef.h"
1920
#include "llvm/CodeGen/TargetInstrInfo.h"
2021

2122
#define GET_INSTRINFO_HEADER
@@ -64,6 +65,9 @@ class WebAssemblyInstrInfo final : public WebAssemblyGenInstrInfo {
6465
int *BytesAdded = nullptr) const override;
6566
bool
6667
reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
68+
69+
ArrayRef<std::pair<int, const char *>>
70+
getSerializableTargetIndices() const override;
6771
};
6872

6973
} // end namespace llvm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; RUN: llc < %s -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s
2+
3+
; Verify if dwarfdump contains DBG_VALUE associated with locals.
4+
; See also dgb-value-ti.ll test.
5+
6+
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
7+
target triple = "wasm32-unknown-unknown-wasm"
8+
9+
define hidden i32 @fib(i32 %n) local_unnamed_addr #0 !dbg !7 {
10+
11+
entry:
12+
; CHECK: DW_TAG_subprogram
13+
; CHECK: DW_TAG_variable
14+
15+
call void @llvm.dbg.value(metadata i32 1, metadata !16, metadata !DIExpression()), !dbg !19
16+
%cmp8 = icmp sgt i32 %n, 0, !dbg !21
17+
br i1 %cmp8, label %for.body, label %for.end, !dbg !24
18+
19+
for.body: ; preds = %entry, %for.body
20+
%b.011 = phi i32 [ %add, %for.body ], [ 1, %entry ]
21+
%a.010 = phi i32 [ %b.011, %for.body ], [ 0, %entry ]
22+
%i.09 = phi i32 [ %inc, %for.body ], [ 0, %entry ]
23+
24+
; CHECK: DW_OP_WASM_location 0x0 +[[LOCAL_1:[0-9]+]]
25+
call void @llvm.dbg.value(metadata i32 %b.011, metadata !16, metadata !DIExpression()), !dbg !19
26+
27+
; CHECK-NOT: DW_OP_WASM_location 0x0 +[[LOCAL_1]]
28+
; CHECK: DW_OP_WASM_location 0x0 +[[LOCAL_2:[0-9]+]]
29+
%add = add nsw i32 %b.011, %a.010, !dbg !26
30+
%inc = add nuw nsw i32 %i.09, 1, !dbg !28
31+
call void @llvm.dbg.value(metadata i32 %add, metadata !16, metadata !DIExpression()), !dbg !19
32+
%exitcond = icmp eq i32 %inc, %n, !dbg !21
33+
br i1 %exitcond, label %for.end, label %for.body, !dbg !24, !llvm.loop !29
34+
35+
for.end: ; preds = %for.body, %entry
36+
%b.0.lcssa = phi i32 [ 1, %entry ], [ %add, %for.body ], !dbg !31
37+
call void @llvm.dbg.value(metadata i32 %b.0.lcssa, metadata !16, metadata !DIExpression()), !dbg !19
38+
ret i32 %b.0.lcssa, !dbg !32
39+
}
40+
41+
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
42+
43+
!llvm.dbg.cu = !{!0}
44+
!llvm.module.flags = !{!4}
45+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 8.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
46+
!1 = !DIFile(filename: "<unknown>", directory: "")
47+
!2 = !{}
48+
!4 = !{i32 2, !"Debug Info Version", i32 3}
49+
!7 = distinct !DISubprogram(name: "fib", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
50+
!8 = !DISubroutineType(types: !9)
51+
!9 = !{!10, !10}
52+
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
53+
!11 = !{!16}
54+
!16 = !DILocalVariable(name: "b", scope: !7, file: !1, line: 2, type: !10)
55+
!17 = !DILocation(line: 1, column: 13, scope: !7)
56+
!18 = !DILocation(line: 2, column: 13, scope: !7)
57+
!19 = !DILocation(line: 2, column: 20, scope: !7)
58+
!20 = !DILocation(line: 2, column: 7, scope: !7)
59+
!21 = !DILocation(line: 3, column: 17, scope: !22)
60+
!22 = distinct !DILexicalBlock(scope: !23, file: !1, line: 3, column: 3)
61+
!23 = distinct !DILexicalBlock(scope: !7, file: !1, line: 3, column: 3)
62+
!24 = !DILocation(line: 3, column: 3, scope: !23)
63+
!25 = !DILocation(line: 2, column: 10, scope: !7)
64+
!26 = !DILocation(line: 6, column: 7, scope: !27)
65+
!27 = distinct !DILexicalBlock(scope: !22, file: !1, line: 3, column: 27)
66+
!28 = !DILocation(line: 3, column: 23, scope: !22)
67+
!29 = distinct !{!29, !24, !30}
68+
!30 = !DILocation(line: 7, column: 3, scope: !23)
69+
!31 = !DILocation(line: 0, scope: !7)
70+
!32 = !DILocation(line: 8, column: 3, scope: !7)

0 commit comments

Comments
 (0)