@@ -945,55 +945,10 @@ impl Step for Rustc {
945945 "build" ,
946946 ) ;
947947
948- rustc_cargo ( builder, & mut cargo, target, compiler. stage ) ;
948+ rustc_cargo ( builder, & mut cargo, target, & compiler) ;
949949
950- if builder. config . rust_profile_use . is_some ( )
951- && builder. config . rust_profile_generate . is_some ( )
952- {
953- panic ! ( "Cannot use and generate PGO profiles at the same time" ) ;
954- }
955-
956- // With LLD, we can use ICF (identical code folding) to reduce the executable size
957- // of librustc_driver/rustc and to improve i-cache utilization.
958- //
959- // -Wl,[link options] doesn't work on MSVC. However, /OPT:ICF (technically /OPT:REF,ICF)
960- // is already on by default in MSVC optimized builds, which is interpreted as --icf=all:
961- // https://github.com/llvm/llvm-project/blob/3329cec2f79185bafd678f310fafadba2a8c76d2/lld/COFF/Driver.cpp#L1746
962- // https://github.com/rust-lang/rust/blob/f22819bcce4abaff7d1246a56eec493418f9f4ee/compiler/rustc_codegen_ssa/src/back/linker.rs#L827
963- if builder. config . lld_mode . is_used ( ) && !compiler. host . is_msvc ( ) {
964- cargo. rustflag ( "-Clink-args=-Wl,--icf=all" ) ;
965- }
966-
967- let is_collecting = if let Some ( path) = & builder. config . rust_profile_generate {
968- if compiler. stage == 1 {
969- cargo. rustflag ( & format ! ( "-Cprofile-generate={path}" ) ) ;
970- // Apparently necessary to avoid overflowing the counters during
971- // a Cargo build profile
972- cargo. rustflag ( "-Cllvm-args=-vp-counters-per-site=4" ) ;
973- true
974- } else {
975- false
976- }
977- } else if let Some ( path) = & builder. config . rust_profile_use {
978- if compiler. stage == 1 {
979- cargo. rustflag ( & format ! ( "-Cprofile-use={path}" ) ) ;
980- if builder. is_verbose ( ) {
981- cargo. rustflag ( "-Cllvm-args=-pgo-warn-missing-function" ) ;
982- }
983- true
984- } else {
985- false
986- }
987- } else {
988- false
989- } ;
990- if is_collecting {
991- // Ensure paths to Rust sources are relative, not absolute.
992- cargo. rustflag ( & format ! (
993- "-Cllvm-args=-static-func-strip-dirname-prefix={}" ,
994- builder. config. src. components( ) . count( )
995- ) ) ;
996- }
950+ // NB: all RUSTFLAGS should be added to `rustc_cargo()` so they will be
951+ // consistently applied by check/doc/test modes too.
997952
998953 for krate in & * self . crates {
999954 cargo. arg ( "-p" ) . arg ( krate) ;
@@ -1044,7 +999,12 @@ impl Step for Rustc {
1044999 }
10451000}
10461001
1047- pub fn rustc_cargo ( builder : & Builder < ' _ > , cargo : & mut Cargo , target : TargetSelection , stage : u32 ) {
1002+ pub fn rustc_cargo (
1003+ builder : & Builder < ' _ > ,
1004+ cargo : & mut Cargo ,
1005+ target : TargetSelection ,
1006+ compiler : & Compiler ,
1007+ ) {
10481008 cargo
10491009 . arg ( "--features" )
10501010 . arg ( builder. rustc_features ( builder. kind , target) )
@@ -1055,7 +1015,7 @@ pub fn rustc_cargo(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelec
10551015
10561016 // We currently don't support cross-crate LTO in stage0. This also isn't hugely necessary
10571017 // and may just be a time sink.
1058- if stage != 0 {
1018+ if compiler . stage != 0 {
10591019 match builder. config . rust_lto {
10601020 RustcLto :: Thin | RustcLto :: Fat => {
10611021 // Since using LTO for optimizing dylibs is currently experimental,
@@ -1081,7 +1041,52 @@ pub fn rustc_cargo(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelec
10811041 cargo. rustflag ( "-Clto=off" ) ;
10821042 }
10831043
1084- rustc_cargo_env ( builder, cargo, target, stage) ;
1044+ // With LLD, we can use ICF (identical code folding) to reduce the executable size
1045+ // of librustc_driver/rustc and to improve i-cache utilization.
1046+ //
1047+ // -Wl,[link options] doesn't work on MSVC. However, /OPT:ICF (technically /OPT:REF,ICF)
1048+ // is already on by default in MSVC optimized builds, which is interpreted as --icf=all:
1049+ // https://github.com/llvm/llvm-project/blob/3329cec2f79185bafd678f310fafadba2a8c76d2/lld/COFF/Driver.cpp#L1746
1050+ // https://github.com/rust-lang/rust/blob/f22819bcce4abaff7d1246a56eec493418f9f4ee/compiler/rustc_codegen_ssa/src/back/linker.rs#L827
1051+ if builder. config . lld_mode . is_used ( ) && !compiler. host . is_msvc ( ) {
1052+ cargo. rustflag ( "-Clink-args=-Wl,--icf=all" ) ;
1053+ }
1054+
1055+ if builder. config . rust_profile_use . is_some ( ) && builder. config . rust_profile_generate . is_some ( ) {
1056+ panic ! ( "Cannot use and generate PGO profiles at the same time" ) ;
1057+ }
1058+ let is_collecting = if let Some ( path) = & builder. config . rust_profile_generate {
1059+ if compiler. stage == 1 {
1060+ cargo. rustflag ( & format ! ( "-Cprofile-generate={path}" ) ) ;
1061+ // Apparently necessary to avoid overflowing the counters during
1062+ // a Cargo build profile
1063+ cargo. rustflag ( "-Cllvm-args=-vp-counters-per-site=4" ) ;
1064+ true
1065+ } else {
1066+ false
1067+ }
1068+ } else if let Some ( path) = & builder. config . rust_profile_use {
1069+ if compiler. stage == 1 {
1070+ cargo. rustflag ( & format ! ( "-Cprofile-use={path}" ) ) ;
1071+ if builder. is_verbose ( ) {
1072+ cargo. rustflag ( "-Cllvm-args=-pgo-warn-missing-function" ) ;
1073+ }
1074+ true
1075+ } else {
1076+ false
1077+ }
1078+ } else {
1079+ false
1080+ } ;
1081+ if is_collecting {
1082+ // Ensure paths to Rust sources are relative, not absolute.
1083+ cargo. rustflag ( & format ! (
1084+ "-Cllvm-args=-static-func-strip-dirname-prefix={}" ,
1085+ builder. config. src. components( ) . count( )
1086+ ) ) ;
1087+ }
1088+
1089+ rustc_cargo_env ( builder, cargo, target, compiler. stage ) ;
10851090}
10861091
10871092pub fn rustc_cargo_env (
0 commit comments