Skip to content

Commit 930b2e7

Browse files
committed
Do not produce fragment for ZST.
1 parent f49494e commit 930b2e7

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

Diff for: compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -584,17 +584,22 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
584584
}
585585

586586
let place = fragment.contents;
587+
let fragment = if fragment_layout.size == Size::ZERO {
588+
// Fragment is a ZST, so does not represent anything.
589+
continue;
590+
} else if fragment_layout.size == var_layout.size {
591+
// Fragment covers entire variable, so as far as
592+
// DWARF is concerned, it's not really a fragment.
593+
None
594+
} else {
595+
Some(fragment_start..fragment_start + fragment_layout.size)
596+
};
597+
587598
per_local[place.local].push(PerLocalVarDebugInfo {
588599
name: var.name,
589600
source_info: var.source_info,
590601
dbg_var,
591-
fragment: if fragment_layout.size == var_layout.size {
592-
// Fragment covers entire variable, so as far as
593-
// DWARF is concerned, it's not really a fragment.
594-
None
595-
} else {
596-
Some(fragment_start..fragment_start + fragment_layout.size)
597-
},
602+
fragment,
598603
projection: place.projection,
599604
});
600605
}

Diff for: tests/codegen/sroa-fragment-debuginfo.rs

+6-8
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",

Diff for: tests/ui/debuginfo/sroa-fragment-debuginfo.rs

+31
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)