Skip to content

Commit a126152

Browse files
Do not GC DW_AT_declaration DW_TAG_subprograms (bytecodealliance#9578)
* Add a test * Log section-relative offsets of DIEs For easier correlation to llvm-dwarfdump's output. * Do not GC function declarations Declarations must be consistent across compilation units.
1 parent 185f7a8 commit a126152

File tree

8 files changed

+102
-14
lines changed

8 files changed

+102
-14
lines changed

crates/cranelift/src/debug/gc.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ fn build_unit_dependencies(
108108
Ok(())
109109
}
110110

111-
fn has_die_back_edge(die: &read::DebuggingInformationEntry<Reader<'_>>) -> bool {
112-
match die.tag() {
111+
fn has_die_back_edge(die: &read::DebuggingInformationEntry<Reader<'_>>) -> read::Result<bool> {
112+
let result = match die.tag() {
113113
constants::DW_TAG_variable
114114
| constants::DW_TAG_constant
115115
| constants::DW_TAG_inlined_subroutine
@@ -124,8 +124,10 @@ fn has_die_back_edge(die: &read::DebuggingInformationEntry<Reader<'_>>) -> bool
124124
| constants::DW_TAG_variant_part
125125
| constants::DW_TAG_variant
126126
| constants::DW_TAG_formal_parameter => true,
127+
constants::DW_TAG_subprogram => die.attr(constants::DW_AT_declaration)?.is_some(),
127128
_ => false,
128-
}
129+
};
130+
Ok(result)
129131
}
130132

131133
fn has_valid_code_range(
@@ -223,7 +225,7 @@ fn build_die_dependencies(
223225
let child_entry = child.entry();
224226
let child_offset = child_entry.offset().to_unit_section_offset(unit);
225227
deps.add_edge(child_offset, offset);
226-
if has_die_back_edge(child_entry) {
228+
if has_die_back_edge(child_entry)? {
227229
deps.add_edge(offset, child_offset);
228230
}
229231
if has_valid_code_range(child_entry, dwarf, unit, at)? {

crates/cranelift/src/debug/transform/debug_transform_logging.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::debug::Reader;
22
use core::fmt;
3-
use gimli::{write, AttributeValue, DebuggingInformationEntry, Dwarf, LittleEndian, Unit};
3+
use gimli::{
4+
write, AttributeValue, DebuggingInformationEntry, Dwarf, LittleEndian, Unit, UnitSectionOffset,
5+
};
46

57
macro_rules! dbi_log {
68
($($tt:tt)*) => {
@@ -18,7 +20,7 @@ pub struct CompileUnitSummary<'a> {
1820
impl<'a> fmt::Debug for CompileUnitSummary<'a> {
1921
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2022
let unit = self.unit;
21-
let offs: usize = unit.header.offset().as_debug_info_offset().unwrap().0;
23+
let offs = get_offset_value(unit.header.offset());
2224
write!(f, "0x{offs:08x} [")?;
2325
let comp_dir = match unit.comp_dir {
2426
Some(dir) => &dir.to_string_lossy(),
@@ -51,7 +53,7 @@ pub fn log_begin_input_die(
5153
) {
5254
dbi_log!(
5355
"=== Begin DIE at 0x{:08x} (depth = {}):\n{:?}",
54-
die.offset().0,
56+
get_offset_value(die.offset().to_unit_section_offset(unit)),
5557
depth,
5658
DieDetailedSummary { dwarf, unit, die }
5759
);
@@ -210,14 +212,15 @@ impl<'a> fmt::Debug for OutDieDetailedSummary<'a> {
210212

211213
pub fn log_end_output_die(
212214
input_die: &DebuggingInformationEntry<Reader<'_>>,
215+
input_unit: &Unit<Reader<'_>, usize>,
213216
die_id: write::UnitEntryId,
214217
unit: &write::Unit,
215218
strings: &write::StringTable,
216219
depth: isize,
217220
) {
218221
dbi_log!(
219222
"=== End DIE at 0x{:08x} (depth = {}):\n{:?}",
220-
input_die.offset().0,
223+
get_offset_value(input_die.offset().to_unit_section_offset(input_unit)),
221224
depth,
222225
OutDieDetailedSummary {
223226
die_id,
@@ -229,13 +232,21 @@ pub fn log_end_output_die(
229232

230233
pub fn log_end_output_die_skipped(
231234
input_die: &DebuggingInformationEntry<Reader<'_>>,
235+
input_unit: &Unit<Reader<'_>, usize>,
232236
reason: &str,
233237
depth: isize,
234238
) {
235239
dbi_log!(
236240
"=== End DIE at 0x{:08x} (depth = {}):\n Skipped as {}\n",
237-
input_die.offset().0,
241+
get_offset_value(input_die.offset().to_unit_section_offset(input_unit)),
238242
depth,
239243
reason
240244
);
241245
}
246+
247+
fn get_offset_value(offset: UnitSectionOffset) -> usize {
248+
match offset {
249+
UnitSectionOffset::DebugInfoOffset(offs) => offs.0,
250+
UnitSectionOffset::DebugTypesOffset(offs) => offs.0,
251+
}
252+
}

crates/cranelift/src/debug/transform/unit.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ pub(crate) fn clone_unit(
336336
let (wp_die_id, vmctx_die_id) =
337337
add_internal_types(out_unit, out_root_id, out_strings, memory_offset);
338338

339-
log_end_output_die(entry, out_root_id, out_unit, out_strings, 0);
339+
log_end_output_die(entry, unit, out_root_id, out_unit, out_strings, 0);
340340
stack.push(out_root_id);
341341
(
342342
out_unit,
@@ -376,7 +376,7 @@ pub(crate) fn clone_unit(
376376
// if D is below B continue to skip
377377
if new_depth > 0 {
378378
skip_at_depth = Some((new_depth, cached));
379-
log_end_output_die_skipped(entry, "unreachable", current_depth);
379+
log_end_output_die_skipped(entry, unit, "unreachable", current_depth);
380380
continue;
381381
}
382382
// otherwise process D with `depth_delta` being the difference from A to D
@@ -395,7 +395,7 @@ pub(crate) fn clone_unit(
395395
// Here B = C so `depth` is 0. A is the previous node so `cached` =
396396
// `depth_delta`.
397397
skip_at_depth = Some((0, depth_delta));
398-
log_end_output_die_skipped(entry, "unreachable", current_depth);
398+
log_end_output_die_skipped(entry, unit, "unreachable", current_depth);
399399
continue;
400400
}
401401

@@ -467,7 +467,7 @@ pub(crate) fn clone_unit(
467467
stack.push(die_id);
468468
assert_eq!(stack.len(), new_stack_len);
469469
die_ref_map.insert(entry.offset(), die_id);
470-
log_end_output_die(entry, die_id, out_unit, out_strings, current_depth);
470+
log_end_output_die(entry, unit, die_id, out_unit, out_strings, current_depth);
471471
continue;
472472
}
473473

@@ -524,7 +524,14 @@ pub(crate) fn clone_unit(
524524
)?;
525525
}
526526

527-
log_end_output_die(entry, out_die_id, out_unit, out_strings, current_depth);
527+
log_end_output_die(
528+
entry,
529+
unit,
530+
out_die_id,
531+
out_unit,
532+
out_strings,
533+
current_depth,
534+
);
528535
}
529536
die_ref_map.patch(pending_die_refs, out_unit);
530537
Ok(Some((out_unit_id, die_ref_map, pending_di_refs)))

tests/all/debug/lldb.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,40 @@ check: exited with status
172172
Ok(())
173173
}
174174

175+
#[test]
176+
#[ignore]
177+
pub fn test_debug_dwarf_generic_lldb() -> Result<()> {
178+
let output = lldb_with_script(
179+
&[
180+
"-Ccache=n",
181+
"-Ddebug-info",
182+
"tests/all/debug/testsuite/generic.wasm",
183+
],
184+
r#"b MainDefinedFunction
185+
r
186+
p __vmctx->set()
187+
n
188+
p (x + x)
189+
b SatelliteFunction
190+
c
191+
n
192+
p (x + x)
193+
c"#,
194+
)?;
195+
196+
check_lldb_output(
197+
&output,
198+
r#"
199+
check: stop reason = breakpoint 1.1
200+
check: 2
201+
check: stop reason = breakpoint 2.1
202+
check: 4
203+
check: exited with status = 0
204+
"#,
205+
)?;
206+
Ok(())
207+
}
208+
175209
#[test]
176210
#[ignore]
177211
pub fn test_debug_dwarf_ref() -> Result<()> {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "generic.h"
2+
3+
int SomeClass::SatelliteFunction(int x) {
4+
x *= 2;
5+
return x;
6+
}

tests/all/debug/testsuite/generic.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// clang generic.cpp generic-satellite.cpp -o generic.wasm -g -target
2+
// wasm32-unknown-wasip1
3+
//
4+
#include "generic.h"
5+
6+
int SomeClass::MainDefinedFunction() {
7+
int x = HIDE_FROM_CHECKER(1);
8+
int y = SatelliteFunction(x);
9+
return x + y;
10+
}
11+
12+
int TestClassDefinitionSpreadAcrossCompileUnits() {
13+
int result = SomeClass::MainDefinedFunction();
14+
return result != 3 ? 1 : 0;
15+
}
16+
17+
int main() {
18+
int exitCode = 0;
19+
exitCode += TestClassDefinitionSpreadAcrossCompileUnits();
20+
return exitCode;
21+
}

tests/all/debug/testsuite/generic.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define HIDE_FROM_CHECKER(x) x
2+
3+
class SomeClass {
4+
public:
5+
static int MainDefinedFunction();
6+
static int SatelliteFunction(int x);
7+
};
21.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)