Skip to content

Commit 5d2b78b

Browse files
cjgillotcuviper
authored andcommitted
Do not produce fragment for ZST.
(cherry picked from commit 930b2e7)
1 parent b6e160e commit 5d2b78b

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,17 +574,22 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
574574
}
575575

576576
let place = fragment.contents;
577+
let fragment = if fragment_layout.size == Size::ZERO {
578+
// Fragment is a ZST, so does not represent anything.
579+
continue;
580+
} else if fragment_layout.size == var_layout.size {
581+
// Fragment covers entire variable, so as far as
582+
// DWARF is concerned, it's not really a fragment.
583+
None
584+
} else {
585+
Some(fragment_start..fragment_start + fragment_layout.size)
586+
};
587+
577588
per_local[place.local].push(PerLocalVarDebugInfo {
578589
name: var.name,
579590
source_info: var.source_info,
580591
dbg_var,
581-
fragment: if fragment_layout.size == var_layout.size {
582-
// Fragment covers entire variable, so as far as
583-
// DWARF is concerned, it's not really a fragment.
584-
None
585-
} else {
586-
Some(fragment_start..fragment_start + fragment_layout.size)
587-
},
592+
fragment,
588593
projection: place.projection,
589594
});
590595
}

tests/codegen/sroa-fragment-debuginfo.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ pub struct ZstSlice<'input> {
2929

3030
#[no_mangle]
3131
pub fn zst(s: &[u8]) {
32+
// The field `extra` is a ZST. The fragment for the field `slice` encompasses the whole
33+
// variable, so is not a fragment. In that case, the variable must have no fragment.
34+
3235
// CHECK: void @zst(
33-
// CHECK: %slice.dbg.spill1 = alloca { ptr, i64 },
34-
// CHECK: %slice.dbg.spill = alloca %Zst,
35-
// CHECK: %s.dbg.spill = alloca { ptr, i64 },
36-
// CHECK: call void @llvm.dbg.declare(metadata ptr %s.dbg.spill, metadata ![[S_ZST:.*]], metadata !DIExpression()),
37-
// CHECK: call void @llvm.dbg.declare(metadata ptr %slice.dbg.spill, metadata ![[SLICE_ZST:.*]], metadata !DIExpression(DW_OP_LLVM_fragment, 0, 0)),
38-
// CHECK: call void @llvm.dbg.declare(metadata ptr %slice.dbg.spill1, metadata ![[SLICE_ZST]], metadata !DIExpression()),
36+
// CHECK-NOT: call void @llvm.dbg.declare(metadata ptr %slice.dbg.spill, metadata !{}, metadata !DIExpression(DW_OP_LLVM_fragment,
37+
// CHECK: call void @llvm.dbg.declare(metadata ptr %{{.*}}, metadata ![[SLICE_ZST:.*]], metadata !DIExpression()),
38+
// CHECK-NOT: call void @llvm.dbg.declare(metadata ptr %{{.*}}, metadata ![[SLICE_ZST]],
3939
let slice = ZstSlice { slice: s, extra: Zst };
4040
}
4141

4242
// CHECK: ![[S_EXTRA]] = !DILocalVariable(name: "s",
4343
// CHECK: ![[SLICE_EXTRA]] = !DILocalVariable(name: "slice",
44-
// CHECK: ![[S_ZST]] = !DILocalVariable(name: "s",
45-
// CHECK: ![[SLICE_ZST]] = !DILocalVariable(name: "slice",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Verify that we do not trigger a LLVM assertion by creating zero-sized DWARF fragments.
2+
//
3+
// build-pass
4+
// compile-flags: -g -Zmir-opt-level=0 -Zmir-enable-passes=+ScalarReplacementOfAggregates
5+
// compile-flags: -Cno-prepopulate-passes
6+
7+
#![crate_type = "lib"]
8+
9+
pub struct ExtraSlice<'input> {
10+
slice: &'input [u8],
11+
extra: u32,
12+
}
13+
14+
#[no_mangle]
15+
pub fn extra(s: &[u8]) {
16+
let slice = ExtraSlice { slice: s, extra: s.len() as u32 };
17+
}
18+
19+
struct Zst;
20+
21+
pub struct ZstSlice<'input> {
22+
slice: &'input [u8],
23+
extra: Zst,
24+
}
25+
26+
#[no_mangle]
27+
pub fn zst(s: &[u8]) {
28+
// The field `extra` is a ZST. The fragment for the field `slice` encompasses the whole
29+
// variable, so is not a fragment. In that case, the variable must have no fragment.
30+
let slice = ZstSlice { slice: s, extra: Zst };
31+
}

0 commit comments

Comments
 (0)