Skip to content

Commit 6340164

Browse files
committed
add -Zoffload=Enable flag behind -Zunstable-options, to enable gpu (host) code generation
1 parent 42d6b0d commit 6340164

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,12 @@ pub(crate) fn run_pass_manager(
668668
write::llvm_optimize(cgcx, dcx, module, None, config, opt_level, opt_stage, stage)?;
669669
}
670670

671+
if enable_gpu && !thin {
672+
let cx =
673+
SimpleCx::new(module.module_llvm.llmod(), &module.module_llvm.llcx, cgcx.pointer_size);
674+
crate::builder::gpu_offload::handle_gpu_code(cgcx, &cx);
675+
}
676+
671677
if cfg!(llvm_enzyme) && enable_ad && !thin {
672678
let cx =
673679
SimpleCx::new(module.module_llvm.llmod(), &module.module_llvm.llcx, cgcx.pointer_size);

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ pub struct ModuleConfig {
120120
pub emit_lifetime_markers: bool,
121121
pub llvm_plugins: Vec<String>,
122122
pub autodiff: Vec<config::AutoDiff>,
123+
pub offload: Vec<config::Offload>,
123124
}
124125

125126
impl ModuleConfig {
@@ -268,6 +269,7 @@ impl ModuleConfig {
268269
emit_lifetime_markers: sess.emit_lifetime_markers(),
269270
llvm_plugins: if_regular!(sess.opts.unstable_opts.llvm_plugins.clone(), vec![]),
270271
autodiff: if_regular!(sess.opts.unstable_opts.autodiff.clone(), vec![]),
272+
offload: if_regular!(sess.opts.unstable_opts.offload.clone(), vec![]),
271273
}
272274
}
273275

compiler/rustc_interface/src/tests.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use rustc_session::config::{
1313
CoverageOptions, DebugInfo, DumpMonoStatsFormat, ErrorOutputType, ExternEntry, ExternLocation,
1414
Externs, FmtDebug, FunctionReturn, InliningThreshold, Input, InstrumentCoverage,
1515
InstrumentXRay, LinkSelfContained, LinkerPluginLto, LocationDetail, LtoCli, MirIncludeSpans,
16-
NextSolverConfig, OomStrategy, Options, OutFileName, OutputType, OutputTypes, PAuthKey, PacRet,
17-
Passes, PatchableFunctionEntry, Polonius, ProcMacroExecutionStrategy, Strip, SwitchWithOptPath,
18-
SymbolManglingVersion, WasiExecModel, build_configuration, build_session_options,
19-
rustc_optgroups,
16+
NextSolverConfig, Offload, OomStrategy, Options, OutFileName, OutputType, OutputTypes,
17+
PAuthKey, PacRet, Passes, PatchableFunctionEntry, Polonius, ProcMacroExecutionStrategy, Strip,
18+
SwitchWithOptPath, SymbolManglingVersion, WasiExecModel, build_configuration,
19+
build_session_options, rustc_optgroups,
2020
};
2121
use rustc_session::lint::Level;
2222
use rustc_session::search_paths::SearchPath;
@@ -833,6 +833,7 @@ fn test_unstable_options_tracking_hash() {
833833
tracked!(no_profiler_runtime, true);
834834
tracked!(no_trait_vptr, true);
835835
tracked!(no_unique_section_names, true);
836+
tracked!(offload, vec![Offload::Enable]);
836837
tracked!(on_broken_pipe, OnBrokenPipe::Kill);
837838
tracked!(oom, OomStrategy::Panic);
838839
tracked!(osx_rpath_install_name, true);

compiler/rustc_session/src/config.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ pub enum CoverageLevel {
226226
Mcdc,
227227
}
228228

229+
// The different settings that the `-Z offload` flag can have.
230+
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
231+
pub enum Offload {
232+
/// Enable the llvm offload pipeline
233+
Enable,
234+
}
235+
229236
/// The different settings that the `-Z autodiff` flag can have.
230237
#[derive(Clone, PartialEq, Hash, Debug)]
231238
pub enum AutoDiff {
@@ -2706,6 +2713,15 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27062713
)
27072714
}
27082715

2716+
if !nightly_options::is_unstable_enabled(matches)
2717+
&& unstable_opts.offload.contains(&Offload::Enable)
2718+
{
2719+
early_dcx.early_fatal(
2720+
"`-Zoffload=Enable` also requires `-Zunstable-options` \
2721+
and a nightly compiler",
2722+
)
2723+
}
2724+
27092725
let target_triple = parse_target_triple(early_dcx, matches);
27102726

27112727
// Ensure `-Z unstable-options` is required when using the unstable `-C link-self-contained` and
@@ -3178,7 +3194,7 @@ pub(crate) mod dep_tracking {
31783194
AutoDiff, BranchProtection, CFGuard, CFProtection, CollapseMacroDebuginfo, CoverageOptions,
31793195
CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug, FunctionReturn,
31803196
InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
3181-
LtoCli, MirStripDebugInfo, NextSolverConfig, OomStrategy, OptLevel, OutFileName,
3197+
LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OomStrategy, OptLevel, OutFileName,
31823198
OutputType, OutputTypes, PatchableFunctionEntry, Polonius, RemapPathScopeComponents,
31833199
ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath,
31843200
SymbolManglingVersion, WasiExecModel,
@@ -3225,6 +3241,7 @@ pub(crate) mod dep_tracking {
32253241
impl_dep_tracking_hash_via_hash!(
32263242
(),
32273243
AutoDiff,
3244+
Offload,
32283245
bool,
32293246
usize,
32303247
NonZero<usize>,

compiler/rustc_session/src/options.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ mod desc {
726726
pub(crate) const parse_list_with_polarity: &str =
727727
"a comma-separated list of strings, with elements beginning with + or -";
728728
pub(crate) const parse_autodiff: &str = "a comma separated list of settings: `Enable`, `PrintSteps`, `PrintTA`, `PrintTAFn`, `PrintAA`, `PrintPerf`, `PrintModBefore`, `PrintModAfter`, `PrintModFinal`, `PrintPasses`, `NoPostopt`, `LooseTypes`, `Inline`";
729+
pub(crate) const parse_offload: &str = "a comma separated list of settings: `Enable`";
729730
pub(crate) const parse_comma_list: &str = "a comma-separated list of strings";
730731
pub(crate) const parse_opt_comma_list: &str = parse_comma_list;
731732
pub(crate) const parse_number: &str = "a number";
@@ -1357,6 +1358,27 @@ pub mod parse {
13571358
}
13581359
}
13591360

1361+
pub(crate) fn parse_offload(slot: &mut Vec<Offload>, v: Option<&str>) -> bool {
1362+
let Some(v) = v else {
1363+
*slot = vec![];
1364+
return true;
1365+
};
1366+
let mut v: Vec<&str> = v.split(",").collect();
1367+
v.sort_unstable();
1368+
for &val in v.iter() {
1369+
let variant = match val {
1370+
"Enable" => Offload::Enable,
1371+
_ => {
1372+
// FIXME(ZuseZ4): print an error saying which value is not recognized
1373+
return false;
1374+
}
1375+
};
1376+
slot.push(variant);
1377+
}
1378+
1379+
true
1380+
}
1381+
13601382
pub(crate) fn parse_autodiff(slot: &mut Vec<AutoDiff>, v: Option<&str>) -> bool {
13611383
let Some(v) = v else {
13621384
*slot = vec![];
@@ -2401,6 +2423,11 @@ options! {
24012423
"do not use unique names for text and data sections when -Z function-sections is used"),
24022424
normalize_docs: bool = (false, parse_bool, [TRACKED],
24032425
"normalize associated items in rustdoc when generating documentation"),
2426+
offload: Vec<crate::config::Offload> = (Vec::new(), parse_offload, [TRACKED],
2427+
"a list of offload flags to enable
2428+
Mandatory setting:
2429+
`=Enable`
2430+
Currently the only option available"),
24042431
on_broken_pipe: OnBrokenPipe = (OnBrokenPipe::Default, parse_on_broken_pipe, [TRACKED],
24052432
"behavior of std::io::ErrorKind::BrokenPipe (SIGPIPE)"),
24062433
oom: OomStrategy = (OomStrategy::Abort, parse_oom_strategy, [TRACKED],

0 commit comments

Comments
 (0)