Skip to content

Commit 8aed93d

Browse files
committed
Auto merge of #113116 - nnethercote:codegen-opts, r=oli-obk
A mish-mash of micro-optimizations These were aimed at speeding up LLVM codegen, but ended up affecting other places as well. r? `@bjorn3`
2 parents 3307274 + 7e786e8 commit 8aed93d

File tree

12 files changed

+32
-117
lines changed

12 files changed

+32
-117
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl DebugContext {
8181

8282
match tcx.sess.source_map().lookup_line(span.lo()) {
8383
Ok(SourceFileAndLine { sf: file, line }) => {
84-
let line_pos = file.line_begin_pos(span.lo());
84+
let line_pos = file.lines(|lines| lines[line]);
8585

8686
(
8787
file,

Diff for: compiler/rustc_codegen_llvm/src/attributes.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Set and unset common attributes on LLVM values.
22
33
use rustc_codegen_ssa::traits::*;
4-
use rustc_data_structures::small_str::SmallStr;
54
use rustc_hir::def_id::DefId;
65
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
76
use rustc_middle::ty::{self, TyCtxt};
@@ -481,8 +480,8 @@ pub fn from_fn_attrs<'ll, 'tcx>(
481480

482481
let global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str());
483482
let function_features = function_features.iter().map(|s| s.as_str());
484-
let target_features =
485-
global_features.chain(function_features).intersperse(",").collect::<SmallStr<1024>>();
483+
let target_features: String =
484+
global_features.chain(function_features).intersperse(",").collect();
486485
if !target_features.is_empty() {
487486
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features));
488487
}

Diff for: compiler/rustc_codegen_llvm/src/builder.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_span::Span;
2323
use rustc_symbol_mangling::typeid::{kcfi_typeid_for_fnabi, typeid_for_fnabi, TypeIdOptions};
2424
use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2525
use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target};
26+
use smallvec::SmallVec;
2627
use std::borrow::Cow;
2728
use std::iter;
2829
use std::ops::Deref;
@@ -225,17 +226,21 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
225226
let args = self.check_call("invoke", llty, llfn, args);
226227
let funclet_bundle = funclet.map(|funclet| funclet.bundle());
227228
let funclet_bundle = funclet_bundle.as_ref().map(|b| &*b.raw);
228-
let mut bundles = vec![funclet_bundle];
229+
let mut bundles: SmallVec<[_; 2]> = SmallVec::new();
230+
if let Some(funclet_bundle) = funclet_bundle {
231+
bundles.push(funclet_bundle);
232+
}
229233

230234
// Emit CFI pointer type membership test
231235
self.cfi_type_test(fn_attrs, fn_abi, llfn);
232236

233237
// Emit KCFI operand bundle
234238
let kcfi_bundle = self.kcfi_operand_bundle(fn_attrs, fn_abi, llfn);
235239
let kcfi_bundle = kcfi_bundle.as_ref().map(|b| &*b.raw);
236-
bundles.push(kcfi_bundle);
240+
if let Some(kcfi_bundle) = kcfi_bundle {
241+
bundles.push(kcfi_bundle);
242+
}
237243

238-
bundles.retain(|bundle| bundle.is_some());
239244
let invoke = unsafe {
240245
llvm::LLVMRustBuildInvoke(
241246
self.llbuilder,
@@ -1181,17 +1186,21 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11811186
let args = self.check_call("call", llty, llfn, args);
11821187
let funclet_bundle = funclet.map(|funclet| funclet.bundle());
11831188
let funclet_bundle = funclet_bundle.as_ref().map(|b| &*b.raw);
1184-
let mut bundles = vec![funclet_bundle];
1189+
let mut bundles: SmallVec<[_; 2]> = SmallVec::new();
1190+
if let Some(funclet_bundle) = funclet_bundle {
1191+
bundles.push(funclet_bundle);
1192+
}
11851193

11861194
// Emit CFI pointer type membership test
11871195
self.cfi_type_test(fn_attrs, fn_abi, llfn);
11881196

11891197
// Emit KCFI operand bundle
11901198
let kcfi_bundle = self.kcfi_operand_bundle(fn_attrs, fn_abi, llfn);
11911199
let kcfi_bundle = kcfi_bundle.as_ref().map(|b| &*b.raw);
1192-
bundles.push(kcfi_bundle);
1200+
if let Some(kcfi_bundle) = kcfi_bundle {
1201+
bundles.push(kcfi_bundle);
1202+
}
11931203

1194-
bundles.retain(|bundle| bundle.is_some());
11951204
let call = unsafe {
11961205
llvm::LLVMRustBuildCall(
11971206
self.llbuilder,

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ fn make_mir_scope<'ll, 'tcx>(
6565
debug_context.scopes[parent]
6666
} else {
6767
// The root is the function itself.
68-
let loc = cx.lookup_debug_loc(mir.span.lo());
68+
let file = cx.sess().source_map().lookup_source_file(mir.span.lo());
6969
debug_context.scopes[scope] = DebugScope {
70-
file_start_pos: loc.file.start_pos,
71-
file_end_pos: loc.file.end_pos,
70+
file_start_pos: file.start_pos,
71+
file_end_pos: file.end_pos,
7272
..debug_context.scopes[scope]
7373
};
7474
instantiated.insert(scope);

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl CodegenCx<'_, '_> {
262262
pub fn lookup_debug_loc(&self, pos: BytePos) -> DebugLoc {
263263
let (file, line, col) = match self.sess().source_map().lookup_line(pos) {
264264
Ok(SourceFileAndLine { sf: file, line }) => {
265-
let line_pos = file.line_begin_pos(pos);
265+
let line_pos = file.lines(|lines| lines[line]);
266266

267267
// Use 1-based indexing.
268268
let line = (line + 1) as u32;
@@ -331,7 +331,7 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
331331
llvm::LLVMRustDIBuilderCreateSubroutineType(DIB(self), fn_signature)
332332
};
333333

334-
let mut name = String::new();
334+
let mut name = String::with_capacity(64);
335335
type_names::push_item_name(tcx, def_id, false, &mut name);
336336

337337
// Find the enclosing function, in case this is a closure.

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/namespace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn item_namespace<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DISco
2828
.map(|parent| item_namespace(cx, DefId { krate: def_id.krate, index: parent }));
2929

3030
let namespace_name_string = {
31-
let mut output = String::new();
31+
let mut output = String::with_capacity(64);
3232
type_names::push_item_name(cx.tcx, def_id, false, &mut output);
3333
output
3434
};

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ extern "C" {
13011301
NumArgs: c_uint,
13021302
Then: &'a BasicBlock,
13031303
Catch: &'a BasicBlock,
1304-
OpBundles: *const Option<&OperandBundleDef<'a>>,
1304+
OpBundles: *const &OperandBundleDef<'a>,
13051305
NumOpBundles: c_uint,
13061306
Name: *const c_char,
13071307
) -> &'a Value;
@@ -1673,7 +1673,7 @@ extern "C" {
16731673
Fn: &'a Value,
16741674
Args: *const &'a Value,
16751675
NumArgs: c_uint,
1676-
OpBundles: *const Option<&OperandBundleDef<'a>>,
1676+
OpBundles: *const &OperandBundleDef<'a>,
16771677
NumOpBundles: c_uint,
16781678
) -> &'a Value;
16791679
pub fn LLVMRustBuildMemCpy<'a>(

Diff for: compiler/rustc_data_structures/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub mod macros;
6868
pub mod obligation_forest;
6969
pub mod sip128;
7070
pub mod small_c_str;
71-
pub mod small_str;
7271
pub mod snapshot_map;
7372
pub mod svh;
7473
pub use ena::snapshot_vec;

Diff for: compiler/rustc_data_structures/src/small_str.rs

-68
This file was deleted.

Diff for: compiler/rustc_data_structures/src/small_str/tests.rs

-20
This file was deleted.

Diff for: compiler/rustc_mir_transform/src/deref_separator.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use rustc_middle::ty::TyCtxt;
88

99
pub struct Derefer;
1010

11-
pub struct DerefChecker<'tcx> {
11+
pub struct DerefChecker<'a, 'tcx> {
1212
tcx: TyCtxt<'tcx>,
1313
patcher: MirPatch<'tcx>,
14-
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
14+
local_decls: &'a IndexVec<Local, LocalDecl<'tcx>>,
1515
}
1616

17-
impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
17+
impl<'a, 'tcx> MutVisitor<'tcx> for DerefChecker<'a, 'tcx> {
1818
fn tcx(&self) -> TyCtxt<'tcx> {
1919
self.tcx
2020
}
@@ -36,7 +36,7 @@ impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
3636

3737
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
3838
if !p_ref.projection.is_empty() && p_elem == ProjectionElem::Deref {
39-
let ty = p_ref.ty(&self.local_decls, self.tcx).ty;
39+
let ty = p_ref.ty(self.local_decls, self.tcx).ty;
4040
let temp = self.patcher.new_internal_with_info(
4141
ty,
4242
self.local_decls[p_ref.local].source_info.span,
@@ -70,7 +70,7 @@ impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
7070

7171
pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
7272
let patch = MirPatch::new(body);
73-
let mut checker = DerefChecker { tcx, patcher: patch, local_decls: body.local_decls.clone() };
73+
let mut checker = DerefChecker { tcx, patcher: patch, local_decls: &body.local_decls };
7474

7575
for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
7676
checker.visit_basic_block_data(bb, data);

Diff for: compiler/rustc_span/src/source_map.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1072,11 +1072,7 @@ impl SourceMap {
10721072
/// This index is guaranteed to be valid for the lifetime of this `SourceMap`,
10731073
/// since `source_files` is a `MonotonicVec`
10741074
pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
1075-
self.files
1076-
.borrow()
1077-
.source_files
1078-
.binary_search_by_key(&pos, |key| key.start_pos)
1079-
.unwrap_or_else(|p| p - 1)
1075+
self.files.borrow().source_files.partition_point(|x| x.start_pos <= pos) - 1
10801076
}
10811077

10821078
pub fn count_lines(&self) -> usize {

0 commit comments

Comments
 (0)