Skip to content

Commit 1e9b4e6

Browse files
author
Amirhossein Pashaeehir
committed
[TEMP] Store FrameStreamer state in stack instead of trying to guess the previous state
1 parent 4d934c0 commit 1e9b4e6

File tree

2 files changed

+39
-56
lines changed

2 files changed

+39
-56
lines changed

llvm/include/llvm/DWARFCFIChecker/DWARFCFIFunctionFrameStreamer.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class CFIFunctionFrameStreamer : public MCStreamer {
3333
public:
3434
CFIFunctionFrameStreamer(MCContext &Context,
3535
std::unique_ptr<CFIFunctionFrameReceiver> Receiver)
36-
: MCStreamer(Context), LastInstruction(std::nullopt),
37-
Receiver(std::move(Receiver)), LastDirectiveIndex(0) {
36+
: MCStreamer(Context), Receiver(std::move(Receiver)) {
3837
assert(this->Receiver && "Receiver should not be null");
3938
}
4039

@@ -61,14 +60,13 @@ class CFIFunctionFrameStreamer : public MCStreamer {
6160
void emitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame) override;
6261

6362
private:
64-
std::pair<unsigned, unsigned> updateDirectivesRange();
65-
void updateReceiver();
63+
void updateReceiver(const std::optional<MCInst> &NewInst);
6664

6765
private:
66+
std::vector<std::optional<MCInst>> FrameLastInstructions; //! FIXME
67+
std::vector<unsigned> FrameLastDirectiveIndices; //! FIXME
6868
std::vector<unsigned> FrameIndices;
69-
std::optional<MCInst> LastInstruction;
7069
std::unique_ptr<CFIFunctionFrameReceiver> Receiver;
71-
unsigned LastDirectiveIndex;
7270
};
7371

7472
} // namespace llvm

llvm/lib/DWARFCFIChecker/DWARFCFIFunctionFrameStreamer.cpp

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,88 +10,73 @@
1010
#include "llvm/ADT/ArrayRef.h"
1111
#include "llvm/MC/MCContext.h"
1212
#include "llvm/MC/MCDwarf.h"
13+
#include "llvm/MC/MCInst.h"
1314
#include "llvm/MC/MCInstrInfo.h"
1415
#include "llvm/MC/MCStreamer.h"
1516
#include <optional>
1617

1718
using namespace llvm;
1819

19-
std::pair<unsigned, unsigned>
20-
CFIFunctionFrameStreamer::updateDirectivesRange() {
21-
auto Frames = getDwarfFrameInfos();
22-
unsigned CurrentCFIDirectiveIndex = 0;
23-
if (hasUnfinishedDwarfFrameInfo()) {
24-
assert(!FrameIndices.empty() && "FunctionUnitStreamer frame indices should "
25-
"be synced with MCStreamer's");
26-
assert(FrameIndices.back() < Frames.size());
27-
CurrentCFIDirectiveIndex = Frames[FrameIndices.back()].Instructions.size();
28-
}
29-
30-
assert(CurrentCFIDirectiveIndex >= LastDirectiveIndex);
31-
std::pair<unsigned, unsigned> CFIDirectivesRange(LastDirectiveIndex,
32-
CurrentCFIDirectiveIndex);
33-
LastDirectiveIndex = CurrentCFIDirectiveIndex;
34-
return CFIDirectivesRange;
35-
}
36-
37-
void CFIFunctionFrameStreamer::updateReceiver() {
38-
if (FrameIndices.empty()) {
39-
auto CFIDirectivesRange = updateDirectivesRange();
40-
assert(CFIDirectivesRange.first == CFIDirectivesRange.second &&
41-
"CFI directives should be in some frame");
42-
return;
43-
}
20+
void CFIFunctionFrameStreamer::updateReceiver(
21+
const std::optional<MCInst> &NewInst) {
22+
assert(!FrameIndices.empty() && hasUnfinishedDwarfFrameInfo() &&
23+
"FunctionUnitStreamer frame indices should be synced with "
24+
"MCStreamer's"); //! FIXME split this assertions and also check another
25+
//! vectors
26+
//! Add tests for nested frames
4427

45-
const MCDwarfFrameInfo *LastFrame =
46-
&getDwarfFrameInfos()[FrameIndices.back()];
28+
auto Frames = getDwarfFrameInfos();
29+
assert(FrameIndices.back() < Frames.size());
30+
unsigned LastDirectiveIndex = FrameLastDirectiveIndices.back();
31+
unsigned CurrentDirectiveIndex =
32+
Frames[FrameIndices.back()].Instructions.size();
33+
assert(CurrentDirectiveIndex >= LastDirectiveIndex);
4734

48-
auto DirectivesRange = updateDirectivesRange();
35+
const MCDwarfFrameInfo *LastFrame = &Frames[FrameIndices.back()];
4936
ArrayRef<MCCFIInstruction> Directives;
50-
if (DirectivesRange.first < DirectivesRange.second) {
37+
if (LastDirectiveIndex < CurrentDirectiveIndex) {
5138
Directives = ArrayRef<MCCFIInstruction>(LastFrame->Instructions);
5239
Directives =
53-
Directives.drop_front(DirectivesRange.first)
54-
.drop_back(LastFrame->Instructions.size() - DirectivesRange.second);
40+
Directives.drop_front(LastDirectiveIndex)
41+
.drop_back(LastFrame->Instructions.size() - CurrentDirectiveIndex);
5542
}
5643

57-
if (LastInstruction) {
58-
Receiver->emitInstructionAndDirectives(LastInstruction.value(), Directives);
59-
} else {
44+
auto MaybeLastInstruction = FrameLastInstructions.back();
45+
if (MaybeLastInstruction)
46+
Receiver->emitInstructionAndDirectives(*MaybeLastInstruction, Directives);
47+
else
6048
Receiver->startFunctionFrame(false /* TODO: should put isEH here */,
6149
Directives);
62-
}
50+
51+
FrameLastInstructions.back() = NewInst;
52+
FrameLastDirectiveIndices.back() = CurrentDirectiveIndex;
6353
}
6454

6555
void CFIFunctionFrameStreamer::emitInstruction(const MCInst &Inst,
6656
const MCSubtargetInfo &STI) {
67-
updateReceiver();
68-
LastInstruction = Inst;
57+
if (hasUnfinishedDwarfFrameInfo()) {
58+
updateReceiver(Inst);
59+
}
6960
}
7061

7162
void CFIFunctionFrameStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
72-
updateReceiver();
63+
FrameLastInstructions.push_back(std::nullopt);
64+
FrameLastDirectiveIndices.push_back(0);
7365
FrameIndices.push_back(getNumFrameInfos());
74-
LastInstruction = std::nullopt;
75-
LastDirectiveIndex = 0;
66+
7667
MCStreamer::emitCFIStartProcImpl(Frame);
7768
}
7869

7970
void CFIFunctionFrameStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame) {
80-
updateReceiver();
71+
updateReceiver(std::nullopt);
8172

8273
assert(!FrameIndices.empty() && "There should be at least one frame to pop");
74+
FrameLastDirectiveIndices.pop_back();
75+
FrameLastInstructions.pop_back();
8376
FrameIndices.pop_back();
8477

78+
dbgs() << "finishing frame\n";
8579
Receiver->finishFunctionFrame();
8680

87-
LastInstruction = std::nullopt;
88-
LastDirectiveIndex = 0;
89-
if (!FrameIndices.empty()) {
90-
auto DwarfFrameInfos = getDwarfFrameInfos();
91-
assert(FrameIndices.back() < DwarfFrameInfos.size());
92-
93-
LastDirectiveIndex =
94-
DwarfFrameInfos[FrameIndices.back()].Instructions.size();
95-
}
9681
MCStreamer::emitCFIEndProcImpl(CurFrame);
9782
}

0 commit comments

Comments
 (0)