Skip to content

Commit 8c9e53a

Browse files
nnethercoteLegNeato
authored andcommitted
Fix compiletest's --target-arch handling.
compiletests lets you specify one or more target architectures. E.g. on CI we run this: ``` cargo run -p compiletests --release --no-default-features -- \ --target-arch compute_61,compute_70,compute_90 ``` While trying out various different target architectures, including invalid ones, I get strange results. First, if the target arch has fewer than 8 chars (e.g. `--target-arch blah`), then nvvm panics: ``` thread 'rustc' panicked at crates/nvvm/src/lib.rs:246:38: byte index 8 is out of bounds of `blah` ``` Second, if the target arch has 8 or more chars but is invalid, it is just ignored. This means invalid flags like `--target-arch compute_999` run, but just use the default target architecture. This commit fixes the problems. - Fix the slicing into the `-arch=` option to avoid the bounds panic. - In `CodegenArgs::parse`, check for the `Err` case and panic instead of swallowing it. A panic isn't ideal, but it's much better than silently accepting and ignoring invalid input. - Use a `String` instead of `&'static str` for the `Err` case, so the error message is more informative, i.e. it includes the bad flag value. Also, the commit improves the `NvvmOption::from_str` test. - Move inputs next to expected outputs, which makes it easier to read and update. - Add the missing compute capabilities. - Add some checks for invalid inputs.
1 parent a459452 commit 8c9e53a

File tree

2 files changed

+70
-65
lines changed

2 files changed

+70
-65
lines changed

crates/nvvm/src/lib.rs

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl Display for NvvmOption {
179179
}
180180

181181
impl FromStr for NvvmOption {
182-
type Err = &'static str;
182+
type Err = String;
183183

184184
fn from_str(s: &str) -> Result<Self, Self::Err> {
185185
let s = s.trim();
@@ -192,9 +192,9 @@ impl FromStr for NvvmOption {
192192
Self::NoOpts
193193
} else if slice == "3" {
194194
// implied
195-
return Err("-opt=3 is default");
195+
return Err("-opt=3 is the default".to_string());
196196
} else {
197-
return Err("unknown optimization level");
197+
return Err(format!("unknown -opt value: {slice}"));
198198
}
199199
}
200200
_ if s.starts_with("-ftz=") => {
@@ -203,9 +203,9 @@ impl FromStr for NvvmOption {
203203
Self::Ftz
204204
} else if slice == "0" {
205205
// implied
206-
return Err("-ftz=0 is default");
206+
return Err("-ftz=0 is the default".to_string());
207207
} else {
208-
return Err("unknown ftz option");
208+
return Err(format!("unknown -ftz value: {slice}"));
209209
}
210210
}
211211
_ if s.starts_with("-prec-sqrt=") => {
@@ -214,9 +214,9 @@ impl FromStr for NvvmOption {
214214
Self::FastSqrt
215215
} else if slice == "1" {
216216
// implied
217-
return Err("-prec-sqrt=1 is default");
217+
return Err("-prec-sqrt=1 is the default".to_string());
218218
} else {
219-
return Err("unknown prec-sqrt option");
219+
return Err(format!("unknown -prec-sqrt value: {slice}"));
220220
}
221221
}
222222
_ if s.starts_with("-prec-div=") => {
@@ -225,9 +225,9 @@ impl FromStr for NvvmOption {
225225
Self::FastDiv
226226
} else if slice == "1" {
227227
// implied
228-
return Err("-prec-div=1 is default");
228+
return Err("-prec-div=1 is the default".to_string());
229229
} else {
230-
return Err("unknown prec-div option");
230+
return Err(format!("unknown -prec-div value: {slice}"));
231231
}
232232
}
233233
_ if s.starts_with("-fma=") => {
@@ -236,13 +236,16 @@ impl FromStr for NvvmOption {
236236
Self::NoFmaContraction
237237
} else if slice == "1" {
238238
// implied
239-
return Err("-fma=1 is default");
239+
return Err("-fma=1 is the default".to_string());
240240
} else {
241-
return Err("unknown fma option");
241+
return Err(format!("unknown -fma value: {slice}"));
242242
}
243243
}
244244
_ if s.starts_with("-arch=") => {
245245
let slice = &s[6..];
246+
if !slice.starts_with("compute_") {
247+
return Err(format!("unknown -arch value: {slice}"));
248+
}
246249
let arch_num = &slice[8..];
247250
let arch = match arch_num {
248251
"35" => NvvmArch::Compute35,
@@ -277,11 +280,11 @@ impl FromStr for NvvmOption {
277280
"121" => NvvmArch::Compute121,
278281
"121f" => NvvmArch::Compute121f,
279282
"121a" => NvvmArch::Compute121a,
280-
_ => return Err("unknown arch"),
283+
_ => return Err(format!("unknown -arch=compute_NN value: {arch_num}")),
281284
};
282285
Self::Arch(arch)
283286
}
284-
_ => return Err("umknown option"),
287+
_ => return Err(format!("unknown option: {s}")),
285288
})
286289
}
287290
}
@@ -1013,55 +1016,53 @@ mod tests {
10131016
use crate::NvvmArch::*;
10141017
use crate::NvvmOption::{self, *};
10151018

1016-
let opts = vec![
1017-
"-g",
1018-
"-generate-line-info",
1019-
"-opt=0",
1020-
"-arch=compute_35",
1021-
"-arch=compute_37",
1022-
"-arch=compute_50",
1023-
"-arch=compute_52",
1024-
"-arch=compute_53",
1025-
"-arch=compute_60",
1026-
"-arch=compute_61",
1027-
"-arch=compute_62",
1028-
"-arch=compute_70",
1029-
"-arch=compute_72",
1030-
"-arch=compute_75",
1031-
"-arch=compute_80",
1032-
"-ftz=1",
1033-
"-prec-sqrt=0",
1034-
"-prec-div=0",
1035-
"-fma=0",
1036-
];
1037-
let expected = vec![
1038-
GenDebugInfo,
1039-
GenLineInfo,
1040-
NoOpts,
1041-
Arch(Compute35),
1042-
Arch(Compute37),
1043-
Arch(Compute50),
1044-
Arch(Compute52),
1045-
Arch(Compute53),
1046-
Arch(Compute60),
1047-
Arch(Compute61),
1048-
Arch(Compute62),
1049-
Arch(Compute70),
1050-
Arch(Compute72),
1051-
Arch(Compute75),
1052-
Arch(Compute80),
1053-
Ftz,
1054-
FastSqrt,
1055-
FastDiv,
1056-
NoFmaContraction,
1057-
];
1058-
1059-
let found = opts
1060-
.into_iter()
1061-
.map(|x| NvvmOption::from_str(x).unwrap())
1062-
.collect::<Vec<_>>();
1063-
1064-
assert_eq!(found, expected);
1019+
let ok = |opt, val| assert_eq!(NvvmOption::from_str(opt), Ok(val));
1020+
let err = |opt, s: &str| assert_eq!(NvvmOption::from_str(opt), Err(s.to_string()));
1021+
1022+
ok("-arch=compute_35", Arch(Compute35));
1023+
ok("-arch=compute_37", Arch(Compute37));
1024+
ok("-arch=compute_50", Arch(Compute50));
1025+
ok("-arch=compute_52", Arch(Compute52));
1026+
ok("-arch=compute_53", Arch(Compute53));
1027+
ok("-arch=compute_60", Arch(Compute60));
1028+
ok("-arch=compute_61", Arch(Compute61));
1029+
ok("-arch=compute_62", Arch(Compute62));
1030+
ok("-arch=compute_70", Arch(Compute70));
1031+
ok("-arch=compute_72", Arch(Compute72));
1032+
ok("-arch=compute_75", Arch(Compute75));
1033+
ok("-arch=compute_80", Arch(Compute80));
1034+
ok("-arch=compute_86", Arch(Compute86));
1035+
ok("-arch=compute_87", Arch(Compute87));
1036+
ok("-arch=compute_89", Arch(Compute89));
1037+
ok("-arch=compute_90", Arch(Compute90));
1038+
ok("-arch=compute_90a", Arch(Compute90a));
1039+
ok("-arch=compute_100", Arch(Compute100));
1040+
ok("-arch=compute_100f", Arch(Compute100f));
1041+
ok("-arch=compute_100a", Arch(Compute100a));
1042+
ok("-arch=compute_101", Arch(Compute101));
1043+
ok("-arch=compute_101f", Arch(Compute101f));
1044+
ok("-arch=compute_101a", Arch(Compute101a));
1045+
ok("-arch=compute_120", Arch(Compute120));
1046+
ok("-arch=compute_120f", Arch(Compute120f));
1047+
ok("-arch=compute_120a", Arch(Compute120a));
1048+
ok("-arch=compute_121", Arch(Compute121));
1049+
ok("-arch=compute_121f", Arch(Compute121f));
1050+
ok("-arch=compute_121a", Arch(Compute121a));
1051+
ok("-fma=0", NoFmaContraction);
1052+
ok("-ftz=1", Ftz);
1053+
ok("-g", GenDebugInfo);
1054+
ok("-generate-line-info", GenLineInfo);
1055+
ok("-opt=0", NoOpts);
1056+
ok("-prec-div=0", FastDiv);
1057+
ok("-prec-sqrt=0", FastSqrt);
1058+
1059+
err("blah", "unknown option: blah");
1060+
err("-aardvark", "unknown option: -aardvark");
1061+
err("-arch=compute75", "unknown -arch value: compute75");
1062+
err("-arch=compute_10", "unknown -arch=compute_NN value: 10");
1063+
err("-arch=compute_100x", "unknown -arch=compute_NN value: 100x");
1064+
err("-opt=3", "-opt=3 is the default");
1065+
err("-opt=99", "unknown -opt value: 99");
10651066
}
10661067

10671068
#[test]

crates/rustc_codegen_nvvm/src/context.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,7 @@ impl CodegenArgs {
673673
continue;
674674
}
675675

676-
if let Ok(flag) = NvvmOption::from_str(arg) {
677-
cg_args.nvvm_options.push(flag);
678-
} else if arg == "--override-libm" {
676+
if arg == "--override-libm" {
679677
cg_args.override_libm = true;
680678
} else if arg == "--use-constant-memory-space" {
681679
cg_args.use_constant_memory_space = true;
@@ -714,6 +712,12 @@ impl CodegenArgs {
714712
skip_next = true;
715713
} else if let Some(entry) = arg.strip_prefix("--disassemble-entry=") {
716714
cg_args.disassemble = Some(DisassembleMode::Entry(entry.to_string()));
715+
} else {
716+
// Do this only after all the other flags above have been tried.
717+
match NvvmOption::from_str(arg) {
718+
Ok(flag) => cg_args.nvvm_options.push(flag),
719+
Err(err) => panic!("{}", err),
720+
}
717721
}
718722
}
719723

0 commit comments

Comments
 (0)