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

File tree

16 files changed

+244
-4
lines changed

16 files changed

+244
-4
lines changed

llvm/include/llvm/BinaryFormat/Dwarf.def

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 35 additions & 3 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)