Skip to content

Commit 234781a

Browse files
committed
Auto merge of #82285 - nhwn:nonzero-debug, r=nagisa
Use u32 over Option<u32> in DebugLoc ~~Changes `Option<u32>` fields in `DebugLoc` to `Option<NonZeroU32>`. Since the respective fields (`line` and `col`) are guaranteed to be 1-based, this layout optimization is a freebie.~~ EDIT: Changes `Option<u32>` fields in `DebugLoc` to `u32`. As `@bugadani` pointed out, an `Option<NonZeroU32>` is probably an unnecessary layer of abstraction since the `None` variant is always used as `UNKNOWN_LINE_NUMBER` (which is just `0`). Also, `SourceInfo` in `metadata.rs` already uses a `u32` instead of an `Option<u32>` to encode the same information, so I think this change is warranted. Since `@jyn514` raised some concerns over measuring performance in a similar PR (#82255), does this need a perf run?
2 parents 66ec64c + 408d402 commit 234781a

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::metadata::{file_metadata, UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER};
1+
use super::metadata::file_metadata;
22
use super::utils::DIB;
33
use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext};
44
use rustc_codegen_ssa::traits::*;
@@ -102,8 +102,8 @@ fn make_mir_scope(
102102
DIB(cx),
103103
parent_scope.dbg_scope.unwrap(),
104104
file_metadata,
105-
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
106-
loc.col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
105+
loc.line,
106+
loc.col,
107107
)
108108
},
109109
};

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1842,10 +1842,7 @@ impl<'tcx> VariantInfo<'_, 'tcx> {
18421842
.span;
18431843
if !span.is_dummy() {
18441844
let loc = cx.lookup_debug_loc(span.lo());
1845-
return Some(SourceInfo {
1846-
file: file_metadata(cx, &loc.file),
1847-
line: loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
1848-
});
1845+
return Some(SourceInfo { file: file_metadata(cx, &loc.file), line: loc.line });
18491846
}
18501847
}
18511848
_ => {}
@@ -2484,7 +2481,7 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global
24842481
let loc = cx.lookup_debug_loc(span.lo());
24852482
(file_metadata(cx, &loc.file), loc.line)
24862483
} else {
2487-
(unknown_file_metadata(cx), None)
2484+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
24882485
};
24892486

24902487
let is_local_to_unit = is_node_local_to_unit(cx, def_id);
@@ -2507,7 +2504,7 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global
25072504
linkage_name.as_ptr().cast(),
25082505
linkage_name.len(),
25092506
file_metadata,
2510-
line_number.unwrap_or(UNKNOWN_LINE_NUMBER),
2507+
line_number,
25112508
type_metadata,
25122509
is_local_to_unit,
25132510
global,

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ pub struct DebugLoc {
224224
/// Information about the original source file.
225225
pub file: Lrc<SourceFile>,
226226
/// The (1-based) line number.
227-
pub line: Option<u32>,
227+
pub line: u32,
228228
/// The (1-based) column number.
229-
pub col: Option<u32>,
229+
pub col: u32,
230230
}
231231

232232
impl CodegenCx<'ll, '_> {
@@ -243,16 +243,16 @@ impl CodegenCx<'ll, '_> {
243243
let line = (line + 1) as u32;
244244
let col = (pos - line_pos).to_u32() + 1;
245245

246-
(file, Some(line), Some(col))
246+
(file, line, col)
247247
}
248-
Err(file) => (file, None, None),
248+
Err(file) => (file, UNKNOWN_LINE_NUMBER, UNKNOWN_COLUMN_NUMBER),
249249
};
250250

251251
// For MSVC, omit the column number.
252252
// Otherwise, emit it. This mimics clang behaviour.
253253
// See discussion in https://github.com/rust-lang/rust/issues/42921
254254
if self.sess().target.is_like_msvc {
255-
DebugLoc { file, line, col: None }
255+
DebugLoc { file, line, col: UNKNOWN_COLUMN_NUMBER }
256256
} else {
257257
DebugLoc { file, line, col }
258258
}
@@ -358,9 +358,9 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
358358
linkage_name.as_ptr().cast(),
359359
linkage_name.len(),
360360
file_metadata,
361-
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
361+
loc.line,
362362
function_type_metadata,
363-
scope_line.unwrap_or(UNKNOWN_LINE_NUMBER),
363+
scope_line,
364364
flags,
365365
spflags,
366366
maybe_definition_llfn,
@@ -550,14 +550,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
550550
) -> &'ll DILocation {
551551
let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo());
552552

553-
unsafe {
554-
llvm::LLVMRustDIBuilderCreateDebugLocation(
555-
line.unwrap_or(UNKNOWN_LINE_NUMBER),
556-
col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
557-
scope,
558-
inlined_at,
559-
)
560-
}
553+
unsafe { llvm::LLVMRustDIBuilderCreateDebugLocation(line, col, scope, inlined_at) }
561554
}
562555

563556
fn create_vtable_metadata(&self, ty: Ty<'tcx>, vtable: Self::Value) {
@@ -606,7 +599,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
606599
name.as_ptr().cast(),
607600
name.len(),
608601
file_metadata,
609-
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
602+
loc.line,
610603
type_metadata,
611604
true,
612605
DIFlags::FlagZero,

0 commit comments

Comments
 (0)