| @@ -309,12 +309,46 @@ fn on_path(program: &str) -> Option<PathBuf> { |
| 309 | 309 | .find(|p| p.is_file()) |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | +/// Fixed code layout (issue #193): functions and loops aligned to 32 |
| 313 | +/// bytes, so the speed of identical C does not depend on where the |
| 314 | +/// linker (ThinLTO, mostly) placed it. Each flag is probed, under |
| 315 | +/// -Werror so an unrecognised-flag warning fails: `-falign-loops` where |
| 316 | +/// it is understood (clang 16+, GCC), else clang's older `-mllvm` |
| 317 | +/// spelling. `no_align` is `LOUISE_NO_ALIGN`, for bisecting a layout |
| 318 | +/// effect. |
| 319 | +fn align_flags(cc: &str, no_align: bool) -> Vec<String> { |
| 320 | + const SOURCE: &str = "int f(void) { return 0; }\n"; |
| 321 | + if no_align { |
| 322 | + return Vec::new(); |
| 323 | + } |
| 324 | + let probed = |args: &[&str]| { |
| 325 | + probe( |
| 326 | + cc, |
| 327 | + &[&["-Werror"][..], args, &["-c", "-o", "/dev/null"][..]].concat(), |
| 328 | + SOURCE, |
| 329 | + ) |
| 330 | + }; |
| 331 | + let mut flags = Vec::new(); |
| 332 | + if probed(&["-falign-loops=32"]) { |
| 333 | + flags.push("-falign-loops=32".to_owned()); |
| 334 | + } else if probed(&["-mllvm", "-align-loops=32"]) { |
| 335 | + flags.extend(["-mllvm".to_owned(), "-align-loops=32".to_owned()]); |
| 336 | + } |
| 337 | + if probed(&["-falign-functions=32"]) { |
| 338 | + flags.push("-falign-functions=32".to_owned()); |
| 339 | + } |
| 340 | + flags |
| 341 | +} |
| 342 | + |
| 312 | 343 | /// The compiler and linker a build uses, and the flags it gives them. |
| 313 | 344 | #[derive(Clone, Debug, PartialEq)] |
| 314 | 345 | struct Toolchain { |
| 315 | 346 | cc: String, |
| 316 | 347 | /// The `-fuse-ld=` linker; none for the compiler's default. |
| 317 | 348 | ld: Option<String>, |
| 349 | + /// The fixed-layout flags ([`align_flags`]), ahead of `flags` so the |
| 350 | + /// user's can override them. Kept with the cached choice. |
| 351 | + align: Vec<String>, |
| 318 | 352 | flags: Vec<String>, |
| 319 | 353 | /// The newest LLVM (major version) whose bitcode this toolchain links |
| 320 | 354 | /// with ThinLTO: `ld.lld`'s, when the compiler is a clang no newer |
| @@ -362,10 +396,11 @@ impl Toolchain { |
| 362 | 396 | /// newly installed compiler or linker changes it. |
| 363 | 397 | fn key(cc: Option<&str>) -> String { |
| 364 | 398 | let mut h = std::collections::hash_map::DefaultHasher::new(); |
| 365 | | − // The file's format: version 2 has `lto`. |
| 366 | | − 2u32.hash(&mut h); |
| 399 | + // The file's format: version 3 has `align` (version 2 had `lto`). |
| 400 | + 3u32.hash(&mut h); |
| 367 | 401 | cc.hash(&mut h); |
| 368 | 402 | std::env::var("LOUISE_LD").ok().hash(&mut h); |
| 403 | + std::env::var_os("LOUISE_NO_ALIGN").is_some().hash(&mut h); |
| 369 | 404 | std::env::var_os("PATH").hash(&mut h); |
| 370 | 405 | let candidates = cc |
| 371 | 406 | .map_or(COMPILERS.to_vec(), |cc| vec![cc]) |
| @@ -386,12 +421,14 @@ impl Toolchain { |
| 386 | 421 | fn write(&self, file: &Path, key: &str) { |
| 387 | 422 | let ld = self.ld.as_deref().unwrap_or(""); |
| 388 | 423 | let lto = self.lto.map_or(String::new(), |v| v.to_string()); |
| 389 | | − let _ = std::fs::write(file, format!("{key}\n{}\n{ld}\n{lto}\n", self.cc)); |
| 424 | + let align = self.align.join(" "); |
| 425 | + let _ = std::fs::write(file, format!("{key}\n{}\n{ld}\n{lto}\n{align}\n", self.cc)); |
| 390 | 426 | } |
| 391 | 427 | |
| 392 | | − /// The cached choice in `file` (compiler, linker, `lto`), if it was |
| 393 | | − /// made with `key`. |
| 394 | | − fn read(file: &Path, key: &str) -> Option<(String, Option<String>, Option<u32>)> { |
| 428 | + /// The cached choice in `file` (compiler, linker, layout flags, |
| 429 | + /// `lto`), if it was made with `key`. `flags` is empty: the user's |
| 430 | + /// flags are not cached. |
| 431 | + fn read(file: &Path, key: &str) -> Option<Toolchain> { |
| 395 | 432 | let text = std::fs::read_to_string(file).ok()?; |
| 396 | 433 | let mut lines = text.lines(); |
| 397 | 434 | if lines.next()? != key { |
| @@ -401,7 +438,17 @@ impl Toolchain { |
| 401 | 438 | let ld = lines.next()?; |
| 402 | 439 | let ld = (!ld.is_empty()).then(|| ld.to_owned()); |
| 403 | 440 | let lto = lines.next()?.parse().ok(); |
| 404 | | − Some((cc, ld, lto)) |
| 441 | + let align = lines |
| 442 | + .next() |
| 443 | + .map(|line| line.split_whitespace().map(str::to_owned).collect()) |
| 444 | + .unwrap_or_default(); |
| 445 | + Some(Toolchain { |
| 446 | + cc, |
| 447 | + ld, |
| 448 | + align, |
| 449 | + flags: Vec::new(), |
| 450 | + lto, |
| 451 | + }) |
| 405 | 452 | } |
| 406 | 453 | } |
| 407 | 454 | |
| @@ -746,8 +793,10 @@ fn toolchain(opts: &CcOptions, build: &Path) -> Result<Toolchain, BuildError> { |
| 746 | 793 | let given = opts.cc.clone().or_else(|| std::env::var("LOUISE_CC").ok()); |
| 747 | 794 | let file = build.join("cache").join("toolchain"); |
| 748 | 795 | let key = Toolchain::key(given.as_deref()); |
| 749 | | − if let Some((cc, ld, lto)) = Toolchain::read(&file, &key) { |
| 750 | | − return Ok(Toolchain { cc, ld, flags, lto }); |
| 796 | + if let Some(kept) = Toolchain::read(&file, &key) { |
| 797 | + let mut all = kept.align.clone(); |
| 798 | + all.extend(flags); |
| 799 | + return Ok(Toolchain { flags: all, ..kept }); |
| 751 | 800 | } |
| 752 | 801 | let cc = match given { |
| 753 | 802 | Some(cc) => cc, |
| @@ -758,11 +807,15 @@ fn toolchain(opts: &CcOptions, build: &Path) -> Result<Toolchain, BuildError> { |
| 758 | 807 | ) |
| 759 | 808 | })?, |
| 760 | 809 | }; |
| 810 | + let align = align_flags(&cc, std::env::var_os("LOUISE_NO_ALIGN").is_some()); |
| 811 | + let mut all = align.clone(); |
| 812 | + all.extend(flags); |
| 761 | 813 | let found = Toolchain { |
| 762 | 814 | lto: find_lto(&cc), |
| 763 | 815 | cc, |
| 764 | 816 | ld: find_ld(), |
| 765 | | − flags, |
| 817 | + align, |
| 818 | + flags: all, |
| 766 | 819 | }; |
| 767 | 820 | found.write(&file, &key); |
| 768 | 821 | Ok(found) |
| @@ -1085,12 +1138,17 @@ mod tests { |
| 1085 | 1138 | // A kept choice is used as it is: only its key is checked. |
| 1086 | 1139 | let given = std::env::var("LOUISE_CC").ok(); |
| 1087 | 1140 | let key = Toolchain::key(given.as_deref()); |
| 1088 | | − std::fs::write(&file, format!("{key}\nkept-cc\nkept-ld\n21\n")).unwrap(); |
| 1141 | + std::fs::write( |
| 1142 | + &file, |
| 1143 | + format!("{key}\nkept-cc\nkept-ld\n21\n-falign-loops=32 -falign-functions=32\n"), |
| 1144 | + ) |
| 1145 | + .unwrap(); |
| 1089 | 1146 | let kept = toolchain(&opts, &dir).unwrap(); |
| 1090 | 1147 | assert_eq!( |
| 1091 | 1148 | (kept.cc.as_str(), kept.ld.as_deref(), kept.lto), |
| 1092 | 1149 | ("kept-cc", Some("kept-ld"), Some(21)) |
| 1093 | 1150 | ); |
| 1151 | + assert_eq!(kept.align, ["-falign-loops=32", "-falign-functions=32"]); |
| 1094 | 1152 | // Another compiler changes the key: the choice is made again. |
| 1095 | 1153 | let other = CcOptions { |
| 1096 | 1154 | cc: Some(found.cc.clone()), |
| @@ -1103,6 +1161,52 @@ mod tests { |
| 1103 | 1161 | } |
| 1104 | 1162 | |
| 1105 | 1163 | #[test] |
| 1164 | + fn release_builds_get_a_fixed_code_layout() { |
| 1165 | + // Issue #193: functions and loops aligned to 32 bytes, so the |
| 1166 | + // speed of identical C does not depend on where ThinLTO placed |
| 1167 | + // it. Probed: `-falign-loops`, or clang's older `-mllvm` spelling. |
| 1168 | + let cc = find_cc().expect("a C compiler"); |
| 1169 | + let flags = align_flags(&cc, false); |
| 1170 | + assert!( |
| 1171 | + flags.iter().any(|f| f == "-falign-loops=32") |
| 1172 | + || flags.windows(2).any(|w| w == ["-mllvm", "-align-loops=32"]), |
| 1173 | + "loops: {flags:?}" |
| 1174 | + ); |
| 1175 | + assert!( |
| 1176 | + flags.iter().any(|f| f == "-falign-functions=32"), |
| 1177 | + "functions: {flags:?}" |
| 1178 | + ); |
| 1179 | + assert!(align_flags(&cc, true).is_empty(), "LOUISE_NO_ALIGN"); |
| 1180 | + } |
| 1181 | + |
| 1182 | + #[test] |
| 1183 | + fn the_layout_flags_come_first() { |
| 1184 | + // They precede the user's, which override them (the compiler |
| 1185 | + // takes the last of a repeated flag). |
| 1186 | + let dir = std::env::temp_dir().join(format!("louise-align-test-{}", std::process::id())); |
| 1187 | + std::fs::create_dir_all(dir.join("cache")).unwrap(); |
| 1188 | + let opts = CcOptions { |
| 1189 | + flags: vec!["-falign-functions=64".into()], |
| 1190 | + ..CcOptions::default() |
| 1191 | + }; |
| 1192 | + let tools = toolchain(&opts, &dir).unwrap(); |
| 1193 | + let user = tools |
| 1194 | + .flags |
| 1195 | + .iter() |
| 1196 | + .rposition(|f| f == "-falign-functions=64") |
| 1197 | + .expect("the user's flag"); |
| 1198 | + assert!( |
| 1199 | + user >= 2 |
| 1200 | + && tools.flags[..user] |
| 1201 | + .iter() |
| 1202 | + .all(|f| f == "-mllvm" || (f.contains("align") && f.ends_with("=32"))), |
| 1203 | + "the layout flags, then the user's: {:?}", |
| 1204 | + tools.flags |
| 1205 | + ); |
| 1206 | + let _ = std::fs::remove_dir_all(&dir); |
| 1207 | + } |
| 1208 | + |
| 1209 | + #[test] |
| 1106 | 1210 | fn a_link_that_fails_with_the_found_linker_uses_the_default() { |
| 1107 | 1211 | if chosen_ld().is_some() { |
| 1108 | 1212 | eprintln!("skipping: LOUISE_LD is set"); |
| @@ -1124,6 +1228,7 @@ mod tests { |
| 1124 | 1228 | let tools = Toolchain { |
| 1125 | 1229 | cc, |
| 1126 | 1230 | ld: Some("no-such-linker".into()), |
| 1231 | + align: Vec::new(), |
| 1127 | 1232 | flags: Vec::new(), |
| 1128 | 1233 | lto: None, |
| 1129 | 1234 | }; |
| @@ -1200,6 +1305,7 @@ mod tests { |
| 1200 | 1305 | let tools = |lto| Toolchain { |
| 1201 | 1306 | cc: "clang".into(), |
| 1202 | 1307 | ld: Some("lld".into()), |
| 1308 | + align: Vec::new(), |
| 1203 | 1309 | flags: Vec::new(), |
| 1204 | 1310 | lto, |
| 1205 | 1311 | }; |