Skip to content

Commit ccddb6f

Browse files
committed
Revert "[DWARF] Emit a worst-case prologue_end flag for pathological inputs (#107849)"
This reverts commit bf483dd. See PR, there's a test testing for this behaviour (possibly adaptable), and a duplicate line entry too
1 parent 20c4e95 commit ccddb6f

9 files changed

+33
-405
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 24 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,16 +2062,6 @@ void DwarfDebug::beginInstruction(const MachineInstr *MI) {
20622062
unsigned LastAsmLine =
20632063
Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
20642064

2065-
if (!DL && MI == PrologEndLoc) {
2066-
// In rare situations, we might want to place the end of the prologue
2067-
// somewhere that doesn't have a source location already. It should be in
2068-
// the entry block.
2069-
assert(MI->getParent() == &*MI->getMF()->begin());
2070-
recordSourceLine(SP->getScopeLine(), 0, SP,
2071-
DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT);
2072-
return;
2073-
}
2074-
20752065
bool PrevInstInSameSection =
20762066
(!PrevInstBB ||
20772067
PrevInstBB->getSectionID() == MI->getParent()->getSectionID());
@@ -2148,109 +2138,32 @@ static std::pair<const MachineInstr *, bool>
21482138
findPrologueEndLoc(const MachineFunction *MF) {
21492139
// First known non-DBG_VALUE and non-frame setup location marks
21502140
// the beginning of the function body.
2151-
const auto &TII = *MF->getSubtarget().getInstrInfo();
2152-
const MachineInstr *NonTrivialInst = nullptr;
2141+
const MachineInstr *LineZeroLoc = nullptr;
21532142
const Function &F = MF->getFunction();
21542143

21552144
// Some instructions may be inserted into prologue after this function. Must
21562145
// keep prologue for these cases.
21572146
bool IsEmptyPrologue =
21582147
!(F.hasPrologueData() || F.getMetadata(LLVMContext::MD_func_sanitize));
2159-
2160-
// Helper lambda to examine each instruction and potentially return it
2161-
// as the prologue_end point.
2162-
auto ExamineInst = [&](const MachineInstr &MI)
2163-
-> std::optional<std::pair<const MachineInstr *, bool>> {
2164-
// Is this instruction trivial data shuffling or frame-setup?
2165-
bool isCopy = (TII.isCopyInstr(MI) ? true : false);
2166-
bool isTrivRemat = TII.isTriviallyReMaterializable(MI);
2167-
bool isFrameSetup = MI.getFlag(MachineInstr::FrameSetup);
2168-
2169-
if (!isFrameSetup && MI.getDebugLoc()) {
2170-
// Scan forward to try to find a non-zero line number. The
2171-
// prologue_end marks the first breakpoint in the function after the
2172-
// frame setup, and a compiler-generated line 0 location is not a
2173-
// meaningful breakpoint. If none is found, return the first
2174-
// location after the frame setup.
2175-
if (MI.getDebugLoc().getLine())
2176-
return std::make_pair(&MI, IsEmptyPrologue);
2177-
}
2178-
2179-
// Keep track of the first "non-trivial" instruction seen, i.e. anything
2180-
// that doesn't involve shuffling data around or is a frame-setup.
2181-
if (!isCopy && !isTrivRemat && !isFrameSetup && !NonTrivialInst)
2182-
NonTrivialInst = &MI;
2183-
2184-
IsEmptyPrologue = false;
2185-
return std::nullopt;
2186-
};
2187-
2188-
// Examine all the instructions at the start of the function. This doesn't
2189-
// necessarily mean just the entry block: unoptimised code can fall-through
2190-
// into an initial loop, and it makes sense to put the initial breakpoint on
2191-
// the first instruction of such a loop. However, if we pass branches, we're
2192-
// better off synthesising an early prologue_end.
2193-
auto CurBlock = MF->begin();
2194-
auto CurInst = CurBlock->begin();
2195-
while (true) {
2196-
// Skip empty blocks, in rare cases the entry can be empty.
2197-
if (CurInst == CurBlock->end()) {
2198-
++CurBlock;
2199-
CurInst = CurBlock->begin();
2200-
continue;
2201-
}
2202-
2203-
// Check whether this non-meta instruction a good position for prologue_end.
2204-
if (!CurInst->isMetaInstruction()) {
2205-
auto FoundInst = ExamineInst(*CurInst);
2206-
if (FoundInst)
2207-
return *FoundInst;
2208-
}
2209-
2210-
// Try to continue searching, but use a backup-location if substantive
2211-
// computation is happening.
2212-
auto NextInst = std::next(CurInst);
2213-
if (NextInst != CurInst->getParent()->end()) {
2214-
// Continue examining the current block.
2215-
CurInst = NextInst;
2216-
continue;
2217-
}
2218-
2219-
// We've reached the end of the block. Did we just look at a terminator?
2220-
if (CurInst->isTerminator()) {
2221-
// Some kind of "real" control flow is occurring. At the very least
2222-
// we would have to start exploring the CFG, a good signal that the
2223-
// prologue is over.
2224-
break;
2148+
for (const auto &MBB : *MF) {
2149+
for (const auto &MI : MBB) {
2150+
if (!MI.isMetaInstruction()) {
2151+
if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
2152+
// Scan forward to try to find a non-zero line number. The
2153+
// prologue_end marks the first breakpoint in the function after the
2154+
// frame setup, and a compiler-generated line 0 location is not a
2155+
// meaningful breakpoint. If none is found, return the first
2156+
// location after the frame setup.
2157+
if (MI.getDebugLoc().getLine())
2158+
return std::make_pair(&MI, IsEmptyPrologue);
2159+
2160+
LineZeroLoc = &MI;
2161+
}
2162+
IsEmptyPrologue = false;
2163+
}
22252164
}
2226-
2227-
// If we've already fallen through into a loop, don't fall through
2228-
// further, use a backup-location.
2229-
if (CurBlock->pred_size() > 1)
2230-
break;
2231-
2232-
// Fall-through from entry to the next block. This is common at -O0 when
2233-
// there's no initialisation in the function. Bail if we're also at the
2234-
// end of the function.
2235-
if (++CurBlock == MF->end())
2236-
break;
2237-
CurInst = CurBlock->begin();
2238-
}
2239-
2240-
// We couldn't find any source-location, suggesting all meaningful information
2241-
// got optimised away. Set the prologue_end to be the first non-trivial
2242-
// instruction, which will get the scope line number. This is better than
2243-
// nothing.
2244-
// Only do this in the entry block, as we'll be giving it the scope line for
2245-
// the function. Return IsEmptyPrologue==true if we've picked the first
2246-
// instruction.
2247-
if (NonTrivialInst && NonTrivialInst->getParent() == &*MF->begin()) {
2248-
IsEmptyPrologue = NonTrivialInst == &*MF->begin()->begin();
2249-
return std::make_pair(NonTrivialInst, IsEmptyPrologue);
22502165
}
2251-
2252-
// If the entry path is empty, just don't have a prologue_end at all.
2253-
return std::make_pair(nullptr, IsEmptyPrologue);
2166+
return std::make_pair(LineZeroLoc, IsEmptyPrologue);
22542167
}
22552168

22562169
/// Register a source line with debug info. Returns the unique label that was
@@ -2282,21 +2195,12 @@ DwarfDebug::emitInitialLocDirective(const MachineFunction &MF, unsigned CUID) {
22822195
bool IsEmptyPrologue = PrologEnd.second;
22832196

22842197
// If the prolog is empty, no need to generate scope line for the proc.
2285-
if (IsEmptyPrologue) {
2286-
// If there's nowhere to put a prologue_end flag, emit a scope line in case
2287-
// there are simply no source locations anywhere in the function.
2288-
if (PrologEndLoc) {
2289-
// Avoid trying to assign prologue_end to a line-zero location.
2290-
// Instructions with no DebugLoc at all are fine, they'll be given the
2291-
// scope line nuumber.
2292-
const DebugLoc &DL = PrologEndLoc->getDebugLoc();
2293-
if (!DL || DL->getLine() != 0)
2294-
return PrologEndLoc;
2295-
2296-
// Later, don't place the prologue_end flag on this line-zero location.
2297-
PrologEndLoc = nullptr;
2298-
}
2299-
}
2198+
if (IsEmptyPrologue)
2199+
// In degenerate cases, we can have functions with no source locations
2200+
// at all. These want a scope line, to avoid a totally empty function.
2201+
// Thus, only skip scope line if there's location to place prologue_end.
2202+
if (PrologEndLoc)
2203+
return PrologEndLoc;
23002204

23012205
// Ensure the compile unit is created if the function is called before
23022206
// beginFunction().

llvm/test/CodeGen/X86/no-non-zero-debug-loc-prologue.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
; RUN: llc -filetype=asm -mtriple=x86_64-apple-macosx12.0.0 -O0 %s -o - | FileCheck %s --implicit-check-not=prologue_end
1+
; RUN: llc -filetype=asm -mtriple=x86_64-apple-macosx12.0.0 -O0 %s -o - | FileCheck %s
22
; CHECK: Lfunc_begin0:
33
; CHECK-NEXT: .file{{.+}}
44
; CHECK-NEXT: .loc 1 1 0 ## test-small.c:1:0{{$}}
55
; CHECK-NEXT: .cfi_startproc
66
; CHECK-NEXT: ## %bb.{{[0-9]+}}:
7-
; CHECK-NEXT: .loc 1 0 1
7+
; CHECK-NEXT: .loc 1 0 1 prologue_end{{.*}}
88
define void @test() #0 !dbg !9 {
99
ret void, !dbg !12
1010
}
@@ -19,4 +19,4 @@ define void @test() #0 !dbg !9 {
1919
!9 = distinct !DISubprogram(name: "test", scope: !10, file: !10, line: 1, type: !11, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !7)
2020
!10 = !DIFile(filename: "test-small.c", directory: "/Users/shubham/Development/test")
2121
!11 = !DISubroutineType(types: !7)
22-
!12 = !DILocation(line: 0, column: 1, scope: !9)
22+
!12 = !DILocation(line: 0, column: 1, scope: !9)

llvm/test/CodeGen/X86/pseudo_cmov_lower2.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ declare void @llvm.dbg.value(metadata, metadata, metadata)
200200
; minus the DEBUG_VALUE line and changes in labels..
201201
define double @foo1_g(float %p1, double %p2, double %p3) nounwind !dbg !4 {
202202
; CHECK-LABEL: foo1_g:
203+
; CHECK: .file 1 "." "test.c"
204+
; CHECK-NEXT: .loc 1 3 0
203205
; CHECK: # %bb.0: # %entry
204-
; CHECK-NEXT: .file 1 "." "test.c"
205-
; CHECK-NEXT: .loc 1 3 0 prologue_end
206206
; CHECK-NEXT: xorps %xmm3, %xmm3
207207
; CHECK-NEXT: ucomiss %xmm3, %xmm0
208208
; CHECK-NEXT: movsd {{.*#+}} xmm0 = [1.25E+0,0.0E+0]

llvm/test/DebugInfo/MIR/X86/dbg-prologue-backup-loc2.mir

Lines changed: 0 additions & 134 deletions
This file was deleted.

llvm/test/DebugInfo/MIR/X86/empty-inline.mir

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
#
1414
# CHECK: Address Line Column File ISA Discriminator OpIndex Flags
1515
# CHECK-NEXT: ---
16-
# CHECK-NEXT: 25 0 1 0 0 0 is_stmt prologue_end
17-
# CHECK-NEXT: 29 28 1 0 0 0 is_stmt
16+
# CHECK-NEXT: 25 0 1 0 0 0 is_stmt
17+
# CHECK-NEXT: 29 28 1 0 0 0 is_stmt prologue_end
1818
# CHECK-NEXT: 29 28 1 0 0 0 is_stmt end_sequence
19-
2019
--- |
2120
source_filename = "t.ll"
2221
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"

0 commit comments

Comments
 (0)