Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 48b312f

Browse files
committed
Don't take TyCtxt as argument for compile_global_asm
This allows it to be executed on a background thread.
1 parent 066f844 commit 48b312f

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
lines changed

src/driver/aot.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
22
//! standalone executable.
33
4+
use std::sync::Arc;
5+
46
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
57
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
68
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -14,6 +16,7 @@ use rustc_session::Session;
1416
use cranelift_codegen::isa::TargetIsa;
1517
use cranelift_object::{ObjectBuilder, ObjectModule};
1618

19+
use crate::global_asm::GlobalAsmConfig;
1720
use crate::{prelude::*, BackendConfig};
1821

1922
struct ModuleCodegenResult(CompiledModule, Option<(WorkProductId, WorkProduct)>);
@@ -141,7 +144,11 @@ fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCo
141144

142145
fn module_codegen(
143146
tcx: TyCtxt<'_>,
144-
(backend_config, cgu_name): (BackendConfig, rustc_span::Symbol),
147+
(backend_config, global_asm_config, cgu_name): (
148+
BackendConfig,
149+
Arc<GlobalAsmConfig>,
150+
rustc_span::Symbol,
151+
),
145152
) -> ModuleCodegenResult {
146153
let cgu = tcx.codegen_unit(cgu_name);
147154
let mono_items = cgu.items_in_deterministic_order(tcx);
@@ -198,9 +205,13 @@ fn module_codegen(
198205
)
199206
});
200207

201-
match crate::global_asm::compile_global_asm(tcx, cgu.name().as_str(), &cx.global_asm) {
208+
match crate::global_asm::compile_global_asm(
209+
&global_asm_config,
210+
cgu.name().as_str(),
211+
&cx.global_asm,
212+
) {
202213
Ok(()) => {}
203-
Err(err) => tcx.sess.fatal(&err.to_string()),
214+
Err(err) => tcx.sess.fatal(&err),
204215
}
205216

206217
codegen_result
@@ -226,6 +237,8 @@ pub(crate) fn run_aot(
226237
}
227238
}
228239

240+
let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
241+
229242
let modules = super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
230243
cgus.iter()
231244
.map(|cgu| {
@@ -243,7 +256,7 @@ pub(crate) fn run_aot(
243256
.with_task(
244257
dep_node,
245258
tcx,
246-
(backend_config.clone(), cgu.name()),
259+
(backend_config.clone(), global_asm_config.clone(), cgu.name()),
247260
module_codegen,
248261
Some(rustc_middle::dep_graph::hash_result),
249262
)

src/global_asm.rs

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
22
//! standalone executable.
33
4-
use std::io::{self, Write};
4+
use std::io::Write;
55
use std::path::PathBuf;
66
use std::process::{Command, Stdio};
7+
use std::sync::Arc;
78

89
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
910
use rustc_hir::ItemId;
10-
use rustc_session::config::OutputType;
11+
use rustc_session::config::{OutputFilenames, OutputType};
1112

1213
use crate::prelude::*;
1314

@@ -31,52 +32,68 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
3132
}
3233
}
3334

35+
#[derive(Debug)]
36+
pub(crate) struct GlobalAsmConfig {
37+
asm_enabled: bool,
38+
assembler: PathBuf,
39+
linker: PathBuf,
40+
output_filenames: Arc<OutputFilenames>,
41+
}
42+
43+
impl GlobalAsmConfig {
44+
pub(crate) fn new(tcx: TyCtxt<'_>) -> Self {
45+
let asm_enabled = cfg!(feature = "inline_asm")
46+
&& !tcx.sess.target.is_like_osx
47+
&& !tcx.sess.target.is_like_windows;
48+
49+
GlobalAsmConfig {
50+
asm_enabled,
51+
assembler: crate::toolchain::get_toolchain_binary(tcx.sess, "as"),
52+
linker: crate::toolchain::get_toolchain_binary(tcx.sess, "ld"),
53+
output_filenames: tcx.output_filenames(()).clone(),
54+
}
55+
}
56+
}
57+
3458
pub(crate) fn compile_global_asm(
35-
tcx: TyCtxt<'_>,
59+
config: &GlobalAsmConfig,
3660
cgu_name: &str,
3761
global_asm: &str,
38-
) -> io::Result<()> {
62+
) -> Result<(), String> {
3963
if global_asm.is_empty() {
4064
return Ok(());
4165
}
4266

43-
if cfg!(not(feature = "inline_asm"))
44-
|| tcx.sess.target.is_like_osx
45-
|| tcx.sess.target.is_like_windows
46-
{
67+
if !config.asm_enabled {
4768
if global_asm.contains("__rust_probestack") {
4869
return Ok(());
4970
}
5071

5172
// FIXME fix linker error on macOS
5273
if cfg!(not(feature = "inline_asm")) {
53-
return Err(io::Error::new(
54-
io::ErrorKind::Unsupported,
55-
"asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift",
56-
));
74+
return Err(
75+
"asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift"
76+
.to_owned(),
77+
);
5778
} else {
58-
return Err(io::Error::new(
59-
io::ErrorKind::Unsupported,
60-
"asm! and global_asm! are not yet supported on macOS and Windows",
61-
));
79+
return Err(
80+
"asm! and global_asm! are not yet supported on macOS and Windows".to_owned()
81+
);
6282
}
6383
}
6484

65-
let assembler = crate::toolchain::get_toolchain_binary(tcx.sess, "as");
66-
let linker = crate::toolchain::get_toolchain_binary(tcx.sess, "ld");
67-
6885
// Remove all LLVM style comments
6986
let global_asm = global_asm
7087
.lines()
7188
.map(|line| if let Some(index) = line.find("//") { &line[0..index] } else { line })
7289
.collect::<Vec<_>>()
7390
.join("\n");
7491

75-
let output_object_file = tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu_name));
92+
let output_object_file = config.output_filenames.temp_path(OutputType::Object, Some(cgu_name));
7693

7794
// Assemble `global_asm`
7895
let global_asm_object_file = add_file_stem_postfix(output_object_file.clone(), ".asm");
79-
let mut child = Command::new(assembler)
96+
let mut child = Command::new(&config.assembler)
8097
.arg("-o")
8198
.arg(&global_asm_object_file)
8299
.stdin(Stdio::piped())
@@ -85,16 +102,13 @@ pub(crate) fn compile_global_asm(
85102
child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
86103
let status = child.wait().expect("Failed to wait for `as`.");
87104
if !status.success() {
88-
return Err(io::Error::new(
89-
io::ErrorKind::Other,
90-
format!("Failed to assemble `{}`", global_asm),
91-
));
105+
return Err(format!("Failed to assemble `{}`", global_asm));
92106
}
93107

94108
// Link the global asm and main object file together
95109
let main_object_file = add_file_stem_postfix(output_object_file.clone(), ".main");
96110
std::fs::rename(&output_object_file, &main_object_file).unwrap();
97-
let status = Command::new(linker)
111+
let status = Command::new(&config.linker)
98112
.arg("-r") // Create a new object file
99113
.arg("-o")
100114
.arg(output_object_file)
@@ -103,13 +117,10 @@ pub(crate) fn compile_global_asm(
103117
.status()
104118
.unwrap();
105119
if !status.success() {
106-
return Err(io::Error::new(
107-
io::ErrorKind::Other,
108-
format!(
109-
"Failed to link `{}` and `{}` together",
110-
main_object_file.display(),
111-
global_asm_object_file.display(),
112-
),
120+
return Err(format!(
121+
"Failed to link `{}` and `{}` together",
122+
main_object_file.display(),
123+
global_asm_object_file.display(),
113124
));
114125
}
115126

0 commit comments

Comments
 (0)