Skip to content

Commit c75310a

Browse files
Auto merge of #150605 - RalfJung:fallback-intrinsic-skip, r=<try>
skip codegen for intrinsics with big fallback bodies if backend does not need them
2 parents 5497a36 + 57e44f5 commit c75310a

6 files changed

Lines changed: 25 additions & 9 deletions

File tree

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use rustc_middle::ty::TyCtxt;
4242
use rustc_middle::util::Providers;
4343
use rustc_session::Session;
4444
use rustc_session::config::{OptLevel, OutputFilenames, PrintKind, PrintRequest};
45-
use rustc_span::Symbol;
45+
use rustc_span::{Symbol, sym};
4646
use rustc_target::spec::{RelocModel, TlsModel};
4747

4848
use crate::llvm::ToLlvmBool;
@@ -333,6 +333,10 @@ impl CodegenBackend for LlvmCodegenBackend {
333333
target_config(sess)
334334
}
335335

336+
fn replaced_intrinsics(&self) -> Vec<Symbol> {
337+
vec![sym::unchecked_funnel_shl, sym::unchecked_funnel_shr, sym::carrying_mul_add]
338+
}
339+
336340
fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any> {
337341
Box::new(rustc_codegen_ssa::base::codegen_crate(
338342
LlvmCodegenBackend(()),

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ pub trait CodegenBackend {
7878

7979
fn print_version(&self) {}
8080

81+
/// Returns a list of all intrinsics that this backend definitely
82+
/// replaces, which means their fallback bodies do not need to be monomorphized.
83+
fn replaced_intrinsics(&self) -> Vec<Symbol> {
84+
vec![]
85+
}
86+
8187
/// Value printed by `--print=backend-has-zstd`.
8288
///
8389
/// Used by compiletest to determine whether tests involving zstd compression

compiler/rustc_interface/src/interface.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
496496
);
497497

498498
codegen_backend.init(&sess);
499+
sess.replaced_intrinsics = FxHashSet::from_iter(codegen_backend.replaced_intrinsics());
499500

500501
let cfg = parse_cfg(sess.dcx(), config.crate_cfg);
501502
let mut cfg = config::build_configuration(&sess, cfg);

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,11 +1001,12 @@ fn visit_instance_use<'tcx>(
10011001
if tcx.should_codegen_locally(panic_instance) {
10021002
output.push(create_fn_mono_item(tcx, panic_instance, source));
10031003
}
1004-
} else if !intrinsic.must_be_overridden {
1004+
} else if !intrinsic.must_be_overridden
1005+
&& !tcx.sess.replaced_intrinsics.contains(&intrinsic.name)
1006+
{
10051007
// Codegen the fallback body of intrinsics with fallback bodies.
1006-
// We explicitly skip this otherwise to ensure we get a linker error
1007-
// if anyone tries to call this intrinsic and the codegen backend did not
1008-
// override the implementation.
1008+
// We have to skip this otherwise as there's no body to codegen.
1009+
// We also skip intrinsics the backend handles, to reduce monomorphizations.
10091010
let instance = ty::Instance::new_raw(instance.def_id(), instance.args);
10101011
if tcx.should_codegen_locally(instance) {
10111012
output.push(create_fn_mono_item(tcx, instance, source));

compiler/rustc_session/src/session.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{env, io};
88
use rand::{RngCore, rng};
99
use rustc_data_structures::base_n::{CASE_INSENSITIVE, ToBaseN};
1010
use rustc_data_structures::flock;
11-
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
11+
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
1212
use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
1313
use rustc_data_structures::sync::{DynSend, DynSync, Lock, MappedReadGuard, ReadGuard, RwLock};
1414
use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
@@ -154,6 +154,10 @@ pub struct Session {
154154
/// preserved with a flag like `-C save-temps`, since these files may be
155155
/// hard linked.
156156
pub invocation_temp: Option<String>,
157+
158+
/// The names of intrinsics that the current codegen backend replaces
159+
/// with its own implementations.
160+
pub replaced_intrinsics: FxHashSet<Symbol>,
157161
}
158162

159163
#[derive(Clone, Copy)]
@@ -1091,6 +1095,7 @@ pub fn build_session(
10911095
target_filesearch,
10921096
host_filesearch,
10931097
invocation_temp,
1098+
replaced_intrinsics: FxHashSet::default(), // filled by `run_compiler`
10941099
};
10951100

10961101
validate_commandline_args_with_session_available(&sess);

tests/codegen-llvm/intrinsics/carrying_mul_add.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111

1212
use std::intrinsics::{carrying_mul_add, fallback};
1313

14-
// The fallbacks are emitted even when they're never used, but optimize out.
14+
// The fallbacks should not be emitted.
1515

16-
// RAW: wide_mul_u128
17-
// OPT-NOT: wide_mul_u128
16+
// NOT: wide_mul_u128
1817

1918
// CHECK-LABEL: @cma_u8
2019
#[no_mangle]

0 commit comments

Comments
 (0)