Skip to content

Commit 0bf4812

Browse files
yurydelendikalexcrichton
authored andcommitted
[WIP] Record WebAssembly specific frame pointer.
1 parent 275b8cc commit 0bf4812

8 files changed

+73
-5
lines changed

llvm/include/llvm/CodeGen/TargetRegisterInfo.h

+20
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ struct RegClassWeight {
210210
unsigned WeightLimit;
211211
};
212212

213+
struct FrameBaseLocation {
214+
enum LocationKind { Register, CFA, TargetIndex } Kind;
215+
struct TargetIndexInfo {
216+
unsigned Index;
217+
signed Offset;
218+
};
219+
union {
220+
unsigned Reg;
221+
TargetIndexInfo TI;
222+
};
223+
};
224+
213225
/// TargetRegisterInfo base class - We assume that the target defines a static
214226
/// array of TargetRegisterDesc objects that represent all of the machine
215227
/// registers that the target has. As such, we simply have to track a pointer
@@ -992,6 +1004,14 @@ class TargetRegisterInfo : public MCRegisterInfo {
9921004
/// for values allocated in the current stack frame.
9931005
virtual Register getFrameRegister(const MachineFunction &MF) const = 0;
9941006

1007+
virtual FrameBaseLocation
1008+
getFrameBaseLocation(const MachineFunction &MF) const {
1009+
FrameBaseLocation Loc;
1010+
Loc.Kind = FrameBaseLocation::Register;
1011+
Loc.Reg = getFrameRegister(MF);
1012+
return Loc;
1013+
}
1014+
9951015
/// Mark a register and all its aliases as reserved in the given set.
9961016
void markSuperRegs(BitVector &RegisterSet, unsigned Reg) const;
9971017

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,24 @@ DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) {
392392

393393
// Only include DW_AT_frame_base in full debug info
394394
if (!includeMinimalInlineScopes()) {
395-
if (Asm->MF->getTarget().getTargetTriple().isNVPTX()) {
395+
const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo();
396+
auto FBL = RI->getFrameBaseLocation(*Asm->MF);
397+
if (FBL.Kind == FrameBaseLocation::CFA) {
396398
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
397399
addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_call_frame_cfa);
398400
addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc);
401+
} else if (FBL.Kind == FrameBaseLocation::TargetIndex) {
402+
if (FBL.TI.Offset >= 0) {
403+
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
404+
DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
405+
DIExpressionCursor Cursor({});
406+
DwarfExpr.addTargetIndexLocation(FBL.TI.Index, FBL.TI.Offset);
407+
DwarfExpr.addExpression(std::move(Cursor));
408+
addBlock(*SPDie, dwarf::DW_AT_frame_base, DwarfExpr.finalize());
409+
}
399410
} else {
400-
const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo();
401-
MachineLocation Location(RI->getFrameRegister(*Asm->MF));
411+
assert(FBL.Kind == FrameBaseLocation::Register);
412+
MachineLocation Location(FBL.Reg);
402413
if (RI->isPhysicalRegister(Location.getReg()))
403414
addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
404415
}

llvm/lib/Target/NVPTX/NVPTXRegisterInfo.h

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class NVPTXRegisterInfo : public NVPTXGenRegisterInfo {
4444

4545
Register getFrameRegister(const MachineFunction &MF) const override;
4646

47+
FrameBaseLocation
48+
getFrameBaseLocation(const MachineFunction &MF) const override {
49+
FrameBaseLocation Loc;
50+
Loc.Kind = FrameBaseLocation::CFA;
51+
return Loc;
52+
}
53+
4754
ManagedStringPool *getStrPool() const {
4855
return const_cast<ManagedStringPool *>(&ManagedStrPool);
4956
}

llvm/lib/Target/WebAssembly/WebAssemblyExplicitLocals.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
385385
Changed = true;
386386
}
387387

388+
{
389+
auto RL = Reg2Local.find(MFI.SPVReg);
390+
if (RL != Reg2Local.end()) {
391+
MFI.SPLocal = RL->second;
392+
}
393+
}
394+
388395
#ifndef NDEBUG
389396
// Assert that all registers have been stackified at this point.
390397
for (const MachineBasicBlock &MBB : MF) {

llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
6060
bool CFGStackified = false;
6161

6262
public:
63-
explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF) {}
63+
explicit WebAssemblyFunctionInfo(MachineFunction &MF)
64+
: MF(MF), SPVReg(WebAssembly::NoRegister) {}
6465
~WebAssemblyFunctionInfo() override;
6566
void initializeBaseYamlFields(const yaml::WebAssemblyFunctionInfo &YamlMFI);
6667

@@ -129,6 +130,9 @@ class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
129130

130131
bool isCFGStackified() const { return CFGStackified; }
131132
void setCFGStackified(bool Value = true) { CFGStackified = Value; }
133+
134+
unsigned SPVReg;
135+
unsigned SPLocal;
132136
};
133137

134138
void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty,

llvm/lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ WebAssemblyRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
140140
return Regs[TFI->hasFP(MF)][TT.isArch64Bit()];
141141
}
142142

143+
FrameBaseLocation
144+
WebAssemblyRegisterInfo::getFrameBaseLocation(const MachineFunction &MF) const {
145+
const WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
146+
FrameBaseLocation Loc;
147+
Loc.Kind = FrameBaseLocation::TargetIndex;
148+
signed Local = MFI.SPVReg != WebAssembly::NoRegister ? MFI.SPLocal : -1;
149+
Loc.TI = {0, Local};
150+
return Loc;
151+
}
152+
143153
const TargetRegisterClass *
144154
WebAssemblyRegisterInfo::getPointerRegClass(const MachineFunction &MF,
145155
unsigned Kind) const {

llvm/lib/Target/WebAssembly/WebAssemblyRegisterInfo.h

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class WebAssemblyRegisterInfo final : public WebAssemblyGenRegisterInfo {
4141
// Debug information queries.
4242
Register getFrameRegister(const MachineFunction &MF) const override;
4343

44+
FrameBaseLocation
45+
getFrameBaseLocation(const MachineFunction &MF) const override;
46+
4447
const TargetRegisterClass *
4548
getPointerRegClass(const MachineFunction &MF,
4649
unsigned Kind = 0) const override;

llvm/lib/Target/WebAssembly/WebAssemblyReplacePhysRegs.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,14 @@ bool WebAssemblyReplacePhysRegs::runOnMachineFunction(MachineFunction &MF) {
8888
for (auto I = MRI.reg_begin(PReg), E = MRI.reg_end(); I != E;) {
8989
MachineOperand &MO = *I++;
9090
if (!MO.isImplicit()) {
91-
if (VReg == WebAssembly::NoRegister)
91+
if (VReg == WebAssembly::NoRegister) {
9292
VReg = MRI.createVirtualRegister(RC);
93+
if (PReg == WebAssembly::SP32) {
94+
WebAssemblyFunctionInfo &MFI =
95+
*MF.getInfo<WebAssemblyFunctionInfo>();
96+
MFI.SPVReg = VReg;
97+
}
98+
}
9399
MO.setReg(VReg);
94100
if (MO.getParent()->isDebugValue())
95101
MO.setIsDebug();

0 commit comments

Comments
 (0)