Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nvcc's --threads flag #1571

Merged
merged 5 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/compiler/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub struct ParsedArguments {
pub common_args: Vec<OsString>,
/// Commandline arguments for the compiler that specify the architecture given
pub arch_args: Vec<OsString>,
/// Commandline arguments for the preprocessor or the compiler that don't affect the computed hash.
pub unhashed_args: Vec<OsString>,
Copy link
Collaborator

@drahnr drahnr Jan 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have another statement, that none of these arguments listed may in any circumstance change the resulting artifact.

/// Extra files that need to have their contents hashed.
pub extra_hash_files: Vec<PathBuf>,
/// Whether or not the `-showIncludes` argument is passed on MSVC
Expand Down Expand Up @@ -400,6 +402,7 @@ where
compiler.plusplus(),
)
};

// A compiler binary may be a symlink to another and so has the same digest, but that means
// the toolchain will not contain the correct path to invoke the compiler! Add the compiler
// executable path to try and prevent this
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/diab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ where
preprocessor_args,
common_args,
arch_args: vec![],
unhashed_args: vec![],
extra_hash_files: vec![],
msvc_show_includes: false,
profile_generate: false,
Expand Down Expand Up @@ -349,6 +350,7 @@ pub fn generate_compile_commands(
out_file.into(),
];
arguments.extend(parsed_args.preprocessor_args.clone());
arguments.extend(parsed_args.unhashed_args.clone());
arguments.extend(parsed_args.common_args.clone());
let command = CompileCommand {
executable: executable.to_owned(),
Expand Down Expand Up @@ -755,6 +757,7 @@ mod test {
preprocessor_args: vec![],
common_args: vec![],
arch_args: vec![],
unhashed_args: vec![],
extra_hash_files: vec![],
msvc_show_includes: false,
profile_generate: false,
Expand Down
11 changes: 10 additions & 1 deletion src/compiler/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ ArgData! { pub
PreprocessorArgumentFlag,
PreprocessorArgument(OsString),
PreprocessorArgumentPath(PathBuf),
// Used for arguments that shouldn't affect the computed hash
Unhashed(OsString),
DoCompilation,
Output(PathBuf),
NeedDepTarget,
Expand Down Expand Up @@ -248,6 +250,7 @@ where
let mut dep_flag = OsString::from("-MT");
let mut common_args = vec![];
let mut arch_args = vec![];
let mut unhashed_args = vec![];
let mut preprocessor_args = vec![];
let mut dependency_args = vec![];
let mut extra_hash_files = vec![];
Expand Down Expand Up @@ -348,7 +351,8 @@ where
| Some(PreprocessorArgument(_))
| Some(PreprocessorArgumentPath(_))
| Some(PassThrough(_))
| Some(PassThroughPath(_)) => {}
| Some(PassThroughPath(_))
| Some(Unhashed(_)) => {}
Some(Language(lang)) => {
language = match lang.to_string_lossy().as_ref() {
"c" => Some(Language::C),
Expand Down Expand Up @@ -387,6 +391,7 @@ where
| Some(PassThroughFlag)
| Some(PassThrough(_))
| Some(PassThroughPath(_)) => &mut common_args,
Some(Unhashed(_)) => &mut unhashed_args,
Some(Arch(_)) => &mut arch_args,
Some(ExtraHashFile(path)) => {
extra_hash_files.push(cwd.join(path));
Expand Down Expand Up @@ -454,6 +459,7 @@ where
| Some(PassThrough(_))
| Some(PassThroughFlag)
| Some(PassThroughPath(_)) => &mut common_args,
Some(Unhashed(_)) => &mut unhashed_args,
Some(ExtraHashFile(path)) => {
extra_hash_files.push(cwd.join(path));
&mut common_args
Expand Down Expand Up @@ -571,6 +577,7 @@ where
preprocessor_args,
common_args,
arch_args,
unhashed_args,
extra_hash_files,
msvc_show_includes: false,
profile_generate,
Expand Down Expand Up @@ -725,6 +732,7 @@ pub fn generate_compile_commands(
out_file.into(),
];
arguments.extend(parsed_args.preprocessor_args.clone());
arguments.extend(parsed_args.unhashed_args.clone());
arguments.extend(parsed_args.common_args.clone());
arguments.extend(parsed_args.arch_args.clone());
let command = CompileCommand {
Expand Down Expand Up @@ -1787,6 +1795,7 @@ mod test {
preprocessor_args: vec![],
common_args: vec![],
arch_args: vec![],
unhashed_args: vec![],
extra_hash_files: vec![],
msvc_show_includes: false,
profile_generate: false,
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/msvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ pub fn parse_arguments(
let mut output_arg = None;
let mut input_arg = None;
let mut common_args = vec![];
let mut unhashed_args = vec![];
let mut preprocessor_args = vec![];
let mut dependency_args = vec![];
let mut extra_hash_files = vec![];
Expand Down Expand Up @@ -706,6 +707,7 @@ pub fn parse_arguments(
| Some(PassThroughPath(_))
| Some(PedanticFlag)
| Some(Standard(_)) => &mut common_args,
Some(Unhashed(_)) => &mut unhashed_args,

Some(ProfileGenerate) => {
profile_generate = true;
Expand Down Expand Up @@ -822,6 +824,7 @@ pub fn parse_arguments(
preprocessor_args,
common_args,
arch_args: vec![],
unhashed_args,
extra_hash_files,
msvc_show_includes: show_includes,
profile_generate,
Expand Down Expand Up @@ -1019,6 +1022,7 @@ fn generate_compile_commands(
];
arguments.extend(parsed_args.preprocessor_args.clone());
arguments.extend(parsed_args.dependency_args.clone());
arguments.extend(parsed_args.unhashed_args.clone());
arguments.extend(parsed_args.common_args.clone());

let command = CompileCommand {
Expand Down Expand Up @@ -1624,6 +1628,7 @@ mod test {
preprocessor_args: vec![],
common_args: vec![],
arch_args: vec![],
unhashed_args: vec![],
extra_hash_files: vec![],
msvc_show_includes: false,
profile_generate: false,
Expand Down Expand Up @@ -1684,6 +1689,7 @@ mod test {
preprocessor_args: vec![],
common_args: vec![],
arch_args: vec![],
unhashed_args: vec![],
extra_hash_files: vec![],
msvc_show_includes: false,
profile_generate: false,
Expand Down
34 changes: 34 additions & 0 deletions src/compiler/nvcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ counted_array!(pub static ARGS: [ArgInfo<gcc::ArgData>; _] = [
take_arg!("--ptxas-options", OsString, CanBeSeparated('='), PassThrough),
take_arg!("--relocatable-device-code", OsString, CanBeSeparated('='), PreprocessorArgument),
take_arg!("--system-include", PathBuf, CanBeSeparated('='), PreprocessorArgumentPath),
take_arg!("--threads", OsString, CanBeSeparated('='), Unhashed),

take_arg!("-Xarchive", OsString, CanBeSeparated('='), PassThrough),
take_arg!("-Xcompiler", OsString, CanBeSeparated('='), PreprocessorArgument),
Expand All @@ -216,6 +217,7 @@ counted_array!(pub static ARGS: [ArgInfo<gcc::ArgData>; _] = [
flag!("-nohdinitlist", PreprocessorArgumentFlag),
flag!("-ptx", DoCompilation),
take_arg!("-rdc", OsString, CanBeSeparated('='), PreprocessorArgument),
take_arg!("-t", OsString, CanBeSeparated('='), Unhashed),
take_arg!("-x", OsString, CanBeSeparated('='), Language),
]);

Expand Down Expand Up @@ -281,6 +283,38 @@ mod test {
assert!(a.common_args.is_empty());
}

#[test]
fn test_parse_threads_argument_simple_cu() {
let a = parses!(
"-t=1",
"-t",
"2",
"--threads=1",
"--threads=2",
"-c",
"foo.cu",
"-o",
"foo.o"
);
assert_eq!(Some("foo.cu"), a.input.to_str());
assert_eq!(Language::Cuda, a.language);
assert_map_contains!(
a.outputs,
(
"obj",
ArtifactDescriptor {
path: "foo.o".into(),
optional: false
}
)
);
assert!(a.preprocessor_args.is_empty());
assert_eq!(
ovec!["-t=1", "-t=2", "--threads", "1", "--threads", "2"],
a.unhashed_args
);
}

#[test]
fn test_parse_arguments_simple_c_as_cu() {
let a = parses!("-x", "cu", "-c", "foo.c", "-o", "foo.o");
Expand Down
54 changes: 54 additions & 0 deletions src/compiler/tasking_vx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ where
preprocessor_args,
common_args,
arch_args: vec![],
unhashed_args: vec![],
extra_hash_files: vec![],
msvc_show_includes: false,
profile_generate: false,
Expand Down Expand Up @@ -364,6 +365,7 @@ fn generate_compile_commands(
out_file.path.as_os_str().into(),
];
arguments.extend(parsed_args.preprocessor_args.clone());
arguments.extend(parsed_args.unhashed_args.clone());
arguments.extend(parsed_args.common_args.clone());
let command = CompileCommand {
executable: executable.to_owned(),
Expand Down Expand Up @@ -695,6 +697,7 @@ mod test {
preprocessor_args: vec![],
common_args: vec![],
arch_args: vec![],
unhashed_args: vec![],
extra_hash_files: vec![],
msvc_show_includes: false,
profile_generate: false,
Expand All @@ -718,4 +721,55 @@ mod test {
// Ensure that we ran all processes.
assert_eq!(0, creator.lock().unwrap().children.len());
}

#[test]
fn test_cuda_threads_included_in_compile_command() {
let creator = new_creator();
let f = TestFixture::new();
let parsed_args = ParsedArguments {
input: "foo.cu".into(),
language: Language::Cuda,
compilation_flag: "-c".into(),
depfile: None,
outputs: vec![(
"obj",
ArtifactDescriptor {
path: "foo.o".into(),
optional: false,
},
)]
.into_iter()
.collect(),
dependency_args: vec![],
preprocessor_args: vec![],
common_args: vec![],
arch_args: vec![],
unhashed_args: ovec!["--threads", "2"],
extra_hash_files: vec![],
msvc_show_includes: false,
profile_generate: false,
color_mode: ColorMode::Auto,
suppress_rewrite_includes_only: false,
};
let compiler = &f.bins[0];
// Compiler invocation.
next_command(&creator, Ok(MockChild::new(exit_status(0), "", "")));
let mut path_transformer = dist::PathTransformer::default();
let (command, _, cacheable) = generate_compile_commands(
&mut path_transformer,
compiler,
&parsed_args,
f.tempdir.path(),
&[],
)
.unwrap();
assert_eq!(
ovec!["-c", "foo.cu", "-o", "foo.o", "--threads", "2"],
command.arguments
);
let _ = command.execute(&creator).wait();
assert_eq!(Cacheable::Yes, cacheable);
// Ensure that we ran all processes.
assert_eq!(0, creator.lock().unwrap().children.len());
}
}