Skip to content

Commit 35afd41

Browse files
committed
[WebAssembly] Add DBG_VALUE with local operands location in WebAssemblyExplicitLocals pass
Extends DWARF expression larguage to express locals/globals locations. (via target-index operands) The WebAssemblyExplicitLocals can replace virtual registers to target-index() operand type at the time when WebAssembly backend introduces local.{get,set,tee} instead of corresponding virtual registers. Reviewers: aprantl, dschuff Differential Revision: https://reviews.llvm.org/D52634
1 parent 56b8425 commit 35afd41

File tree

15 files changed

+171
-4
lines changed

15 files changed

+171
-4
lines changed

llvm/include/llvm/BinaryFormat/Dwarf.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ HANDLE_DW_OP(0xa9, reinterpret, 5, DWARF)
633633
// Vendor extensions:
634634
// Extensions for GNU-style thread-local storage.
635635
HANDLE_DW_OP(0xe0, GNU_push_tls_address, 0, GNU)
636+
// Extensions for WebAssembly.
637+
HANDLE_DW_OP(0xed, WASM_location, 0, WASM)
636638
// The GNU entry value extension.
637639
HANDLE_DW_OP(0xf3, GNU_entry_value, 0, GNU)
638640
// Extensions for Fission proposal.

llvm/include/llvm/BinaryFormat/Dwarf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ enum LLVMConstants : uint32_t {
5858
DWARF_VENDOR_GNU = 3,
5959
DWARF_VENDOR_GOOGLE = 4,
6060
DWARF_VENDOR_LLVM = 5,
61-
DWARF_VENDOR_MIPS = 6
61+
DWARF_VENDOR_MIPS = 6,
62+
DWARF_VENDOR_WASM = 7
6263
};
6364

6465
/// 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
@@ -866,6 +866,10 @@ static bool emitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) {
866866
OS << MI->getOperand(0).getImm();
867867
} else if (MI->getOperand(0).isCImm()) {
868868
MI->getOperand(0).getCImm()->getValue().print(OS, false /*isSigned*/);
869+
} else if (MI->getOperand(0).isTargetIndex()) {
870+
auto Op = MI->getOperand(0);
871+
OS << "!target-index(" << Op.getIndex() << "," << Op.getOffset() << ")";
872+
return true;
869873
} else {
870874
unsigned Reg;
871875
if (MI->getOperand(0).isReg()) {

llvm/lib/CodeGen/AsmPrinter/DebugLocEntry.h

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,32 @@
2020
namespace llvm {
2121
class AsmPrinter;
2222

23+
struct TargetIndexLocation {
24+
int Index;
25+
int Offset;
26+
27+
TargetIndexLocation() = default;
28+
TargetIndexLocation(unsigned Idx, int64_t Offset)
29+
: Index(Idx), Offset(Offset) {}
30+
31+
bool operator==(const TargetIndexLocation &Other) const {
32+
return Index == Other.Index && Offset == Other.Offset;
33+
}
34+
};
35+
2336
/// A single location or constant.
2437
class DbgValueLoc {
2538
/// Any complex address location expression for this DbgValueLoc.
2639
const DIExpression *Expression;
2740

2841
/// Type of entry that this represents.
29-
enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
42+
enum EntryType {
43+
E_Location,
44+
E_Integer,
45+
E_ConstantFP,
46+
E_ConstantInt,
47+
E_TargetIndexLocation
48+
};
3049
enum EntryType EntryKind;
3150

3251
/// Either a constant,
@@ -36,8 +55,12 @@ class DbgValueLoc {
3655
const ConstantInt *CIP;
3756
} Constant;
3857

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

4265
public:
4366
DbgValueLoc(const DIExpression *Expr, int64_t i)
@@ -56,15 +79,21 @@ class DbgValueLoc {
5679
: Expression(Expr), EntryKind(E_Location), Loc(Loc) {
5780
assert(cast<DIExpression>(Expr)->isValid());
5881
}
82+
DbgValueLoc(const DIExpression *Expr, TargetIndexLocation Loc)
83+
: Expression(Expr), EntryKind(E_TargetIndexLocation), TIL(Loc) {}
5984

6085
bool isLocation() const { return EntryKind == E_Location; }
86+
bool isTargetIndexLocation() const {
87+
return EntryKind == E_TargetIndexLocation;
88+
}
6189
bool isInt() const { return EntryKind == E_Integer; }
6290
bool isConstantFP() const { return EntryKind == E_ConstantFP; }
6391
bool isConstantInt() const { return EntryKind == E_ConstantInt; }
6492
int64_t getInt() const { return Constant.Int; }
6593
const ConstantFP *getConstantFP() const { return Constant.CFP; }
6694
const ConstantInt *getConstantInt() const { return Constant.CIP; }
6795
MachineLocation getLoc() const { return Loc; }
96+
TargetIndexLocation getTargetIndexLocation() const { return TIL; }
6897
bool isFragment() const { return getExpression()->isFragment(); }
6998
bool isEntryVal() const { return getExpression()->isEntryValue(); }
7099
const DIExpression *getExpression() const { return Expression; }
@@ -162,6 +191,8 @@ inline bool operator==(const DbgValueLoc &A,
162191
switch (A.EntryKind) {
163192
case DbgValueLoc::E_Location:
164193
return A.Loc == B.Loc;
194+
case DbgValueLoc::E_TargetIndexLocation:
195+
return A.TIL == B.TIL;
165196
case DbgValueLoc::E_Integer:
166197
return A.Constant.Int == B.Constant.Int;
167198
case DbgValueLoc::E_ConstantFP:

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ static DbgValueLoc getDebugLocValue(const MachineInstr *MI) {
257257
MachineLocation MLoc(RegOp.getReg(), Op1.isImm());
258258
return DbgValueLoc(Expr, MLoc);
259259
}
260+
if (MI->getOperand(0).isTargetIndex()) {
261+
auto Op = MI->getOperand(0);
262+
return DbgValueLoc(Expr,
263+
TargetIndexLocation(Op.getIndex(), Op.getOffset()));
264+
}
260265
if (MI->getOperand(0).isImm())
261266
return DbgValueLoc(Expr, MI->getOperand(0).getImm());
262267
if (MI->getOperand(0).isFPImm())
@@ -2027,6 +2032,9 @@ void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
20272032
if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
20282033
return;
20292034
return DwarfExpr.addExpression(std::move(Cursor));
2035+
} else if (Value.isTargetIndexLocation()) {
2036+
TargetIndexLocation Loc = Value.getTargetIndexLocation();
2037+
DwarfExpr.addTargetIndexLocation(Loc.Index, Loc.Offset);
20302038
} else if (Value.isConstantFP()) {
20312039
APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
20322040
DwarfExpr.addUnsignedConstant(RawBytes);

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,11 @@ void DwarfExpression::emitLegacyZExt(unsigned FromBits) {
521521
emitUnsigned((1ULL << FromBits) - 1);
522522
emitOp(dwarf::DW_OP_and);
523523
}
524+
525+
void DwarfExpression::addTargetIndexLocation(unsigned Index, int64_t Offset) {
526+
assert(LocationKind == Implicit || LocationKind == Unknown);
527+
LocationKind = Implicit;
528+
emitOp(dwarf::DW_OP_WASM_location);
529+
emitUnsigned(Index);
530+
emitSigned(Offset);
531+
}

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ class DwarfExpression {
295295

296296
void emitLegacySExt(unsigned FromBits);
297297
void emitLegacyZExt(unsigned FromBits);
298+
299+
/// Emit location information expressed via target's index + offset
300+
/// It is an extension for WebAssembly locals, globals and operand stack.
301+
void addTargetIndexLocation(unsigned Index, int64_t Offset);
298302
};
299303

300304
/// 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)