Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 41dcb52

Browse files
committedOct 29, 2023
Merge commit 'dde58803fd6cbb270c7a437f36a8a3a29fbef679' into sync_cg_clif-2023-10-29
1 parent da1ed4d commit 41dcb52

File tree

9 files changed

+100
-334
lines changed

9 files changed

+100
-334
lines changed
 

‎Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ smallvec = "1.8.1"
3535

3636
[features]
3737
# Enable features not ready to be enabled when compiling as part of rustc
38-
unstable-features = ["jit", "inline_asm"]
38+
unstable-features = ["jit", "inline_asm_sym"]
3939
jit = ["cranelift-jit", "libloading"]
40-
inline_asm = []
40+
inline_asm_sym = []
4141

4242
[package.metadata.rust-analyzer]
4343
rustc_private = true

‎patches/stdlib-lock.toml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ version = "0.2.15"
4040
source = "registry+https://github.com/rust-lang/crates.io-index"
4141
checksum = "56fc6cf8dc8c4158eed8649f9b8b0ea1518eb62b544fe9490d66fa0b349eafe9"
4242

43-
[[package]]
44-
name = "auxv"
45-
version = "0.3.3"
46-
source = "registry+https://github.com/rust-lang/crates.io-index"
47-
checksum = "e50430f9beb8effb02399fa81c76eeaa26b05e4f03b09285cad8d079c1af5a3d"
48-
dependencies = [
49-
"byteorder",
50-
"gcc",
51-
]
52-
53-
[[package]]
54-
name = "byteorder"
55-
version = "1.4.3"
56-
source = "registry+https://github.com/rust-lang/crates.io-index"
57-
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
58-
5943
[[package]]
6044
name = "cc"
6145
version = "1.0.79"
@@ -388,7 +372,6 @@ dependencies = [
388372
name = "std_detect"
389373
version = "0.1.5"
390374
dependencies = [
391-
"auxv",
392375
"cfg-if",
393376
"compiler_builtins",
394377
"cupid",

‎rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2023-10-21"
2+
channel = "nightly-2023-10-29"
33
components = ["rust-src", "rustc-dev", "llvm-tools"]

‎scripts/setup_rust_fork.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ set -e
44
# Compiletest expects all standard library paths to start with /rustc/FAKE_PREFIX.
55
# CG_CLIF_STDLIB_REMAP_PATH_PREFIX will cause cg_clif's build system to pass
66
# --remap-path-prefix to handle this.
7-
CG_CLIF_STDLIB_REMAP_PATH_PREFIX=/rustc/FAKE_PREFIX ./y.sh build
7+
# CG_CLIF_FORCE_GNU_AS will force usage of as instead of the LLVM backend of rustc as we
8+
# the LLVM backend isn't compiled in here.
9+
CG_CLIF_FORCE_GNU_AS=1 CG_CLIF_STDLIB_REMAP_PATH_PREFIX=/rustc/FAKE_PREFIX ./y.sh build
810

911
echo "[SETUP] Rust fork"
1012
git clone https://github.com/rust-lang/rust.git || true

‎scripts/test_bootstrap.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ rm -r compiler/rustc_codegen_cranelift/{Cargo.*,src}
1111
cp ../Cargo.* compiler/rustc_codegen_cranelift/
1212
cp -r ../src compiler/rustc_codegen_cranelift/src
1313

14-
./x.py build --stage 1 library/std
14+
# CG_CLIF_FORCE_GNU_AS will force usage of as instead of the LLVM backend of rustc as we
15+
# the LLVM backend isn't compiled in here.
16+
CG_CLIF_FORCE_GNU_AS=1 ./x.py build --stage 1 library/std
1517
popd

‎src/global_asm.rs

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
4646
global_asm.push_str(&string);
4747
}
4848
InlineAsmOperand::SymFn { anon_const } => {
49+
if cfg!(not(feature = "inline_asm_sym")) {
50+
tcx.sess.span_err(
51+
item.span,
52+
"asm! and global_asm! sym operands are not yet supported",
53+
);
54+
}
55+
4956
let ty = tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
5057
let instance = match ty.kind() {
5158
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
@@ -57,6 +64,13 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
5764
global_asm.push_str(symbol.name);
5865
}
5966
InlineAsmOperand::SymStatic { path: _, def_id } => {
67+
if cfg!(not(feature = "inline_asm_sym")) {
68+
tcx.sess.span_err(
69+
item.span,
70+
"asm! and global_asm! sym operands are not yet supported",
71+
);
72+
}
73+
6074
let instance = Instance::mono(tcx, def_id).polymorphize(tcx);
6175
let symbol = tcx.symbol_name(instance);
6276
global_asm.push_str(symbol.name);
@@ -81,22 +95,23 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
8195
}
8296
}
8397

84-
pub(crate) fn asm_supported(tcx: TyCtxt<'_>) -> bool {
85-
cfg!(feature = "inline_asm") && !tcx.sess.target.is_like_windows
86-
}
87-
8898
#[derive(Debug)]
8999
pub(crate) struct GlobalAsmConfig {
90-
asm_enabled: bool,
91100
assembler: PathBuf,
101+
target: String,
92102
pub(crate) output_filenames: Arc<OutputFilenames>,
93103
}
94104

95105
impl GlobalAsmConfig {
96106
pub(crate) fn new(tcx: TyCtxt<'_>) -> Self {
97107
GlobalAsmConfig {
98-
asm_enabled: asm_supported(tcx),
99108
assembler: crate::toolchain::get_toolchain_binary(tcx.sess, "as"),
109+
target: match &tcx.sess.opts.target_triple {
110+
rustc_target::spec::TargetTriple::TargetTriple(triple) => triple.clone(),
111+
rustc_target::spec::TargetTriple::TargetJson { path_for_rustdoc, .. } => {
112+
path_for_rustdoc.to_str().unwrap().to_owned()
113+
}
114+
},
100115
output_filenames: tcx.output_filenames(()).clone(),
101116
}
102117
}
@@ -111,21 +126,6 @@ pub(crate) fn compile_global_asm(
111126
return Ok(None);
112127
}
113128

114-
if !config.asm_enabled {
115-
if global_asm.contains("__rust_probestack") {
116-
return Ok(None);
117-
}
118-
119-
if cfg!(not(feature = "inline_asm")) {
120-
return Err(
121-
"asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift"
122-
.to_owned(),
123-
);
124-
} else {
125-
return Err("asm! and global_asm! are not yet supported on Windows".to_owned());
126-
}
127-
}
128-
129129
// Remove all LLVM style comments
130130
let mut global_asm = global_asm
131131
.lines()
@@ -134,20 +134,67 @@ pub(crate) fn compile_global_asm(
134134
.join("\n");
135135
global_asm.push('\n');
136136

137-
let output_object_file = config.output_filenames.temp_path(OutputType::Object, Some(cgu_name));
137+
let global_asm_object_file = add_file_stem_postfix(
138+
config.output_filenames.temp_path(OutputType::Object, Some(cgu_name)),
139+
".asm",
140+
);
138141

139142
// Assemble `global_asm`
140-
let global_asm_object_file = add_file_stem_postfix(output_object_file, ".asm");
141-
let mut child = Command::new(&config.assembler)
142-
.arg("-o")
143-
.arg(&global_asm_object_file)
144-
.stdin(Stdio::piped())
145-
.spawn()
146-
.expect("Failed to spawn `as`.");
147-
child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
148-
let status = child.wait().expect("Failed to wait for `as`.");
149-
if !status.success() {
150-
return Err(format!("Failed to assemble `{}`", global_asm));
143+
if option_env!("CG_CLIF_FORCE_GNU_AS").is_some() {
144+
let mut child = Command::new(&config.assembler)
145+
.arg("-o")
146+
.arg(&global_asm_object_file)
147+
.stdin(Stdio::piped())
148+
.spawn()
149+
.expect("Failed to spawn `as`.");
150+
child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
151+
let status = child.wait().expect("Failed to wait for `as`.");
152+
if !status.success() {
153+
return Err(format!("Failed to assemble `{}`", global_asm));
154+
}
155+
} else {
156+
let mut child = Command::new(std::env::current_exe().unwrap())
157+
.arg("--target")
158+
.arg(&config.target)
159+
.arg("--crate-type")
160+
.arg("staticlib")
161+
.arg("--emit")
162+
.arg("obj")
163+
.arg("-o")
164+
.arg(&global_asm_object_file)
165+
.arg("-")
166+
.arg("-Abad_asm_style")
167+
.arg("-Zcodegen-backend=llvm")
168+
.stdin(Stdio::piped())
169+
.spawn()
170+
.expect("Failed to spawn `as`.");
171+
let mut stdin = child.stdin.take().unwrap();
172+
stdin
173+
.write_all(
174+
br####"
175+
#![feature(decl_macro, no_core, rustc_attrs)]
176+
#![allow(internal_features)]
177+
#![no_core]
178+
#[rustc_builtin_macro]
179+
#[rustc_macro_transparency = "semitransparent"]
180+
macro global_asm() { /* compiler built-in */ }
181+
global_asm!(r###"
182+
"####,
183+
)
184+
.unwrap();
185+
stdin.write_all(global_asm.as_bytes()).unwrap();
186+
stdin
187+
.write_all(
188+
br####"
189+
"###);
190+
"####,
191+
)
192+
.unwrap();
193+
std::mem::drop(stdin);
194+
let status = child.wait().expect("Failed to wait for `as`.");
195+
if !status.success() {
196+
return Err(format!("Failed to assemble `{}`", global_asm));
197+
}
151198
}
152199

153200
Ok(Some(global_asm_object_file))

‎src/inline_asm.rs

Lines changed: 11 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_span::sym;
88
use rustc_target::asm::*;
99
use target_lexicon::BinaryFormat;
1010

11-
use crate::global_asm::asm_supported;
1211
use crate::prelude::*;
1312

1413
enum CInlineAsmOperand<'tcx> {
@@ -45,208 +44,11 @@ pub(crate) fn codegen_inline_asm<'tcx>(
4544
) {
4645
// FIXME add .eh_frame unwind info directives
4746

48-
if !asm_supported(fx.tcx) {
49-
if template.is_empty() {
50-
let destination_block = fx.get_block(destination.unwrap());
51-
fx.bcx.ins().jump(destination_block, &[]);
52-
return;
53-
}
54-
55-
// Used by panic_abort
56-
if template[0] == InlineAsmTemplatePiece::String("int $$0x29".to_string()) {
57-
fx.bcx.ins().trap(TrapCode::User(1));
58-
return;
59-
}
60-
61-
// Used by stdarch
62-
if template[0] == InlineAsmTemplatePiece::String("mov ".to_string())
63-
&& matches!(
64-
template[1],
65-
InlineAsmTemplatePiece::Placeholder {
66-
operand_idx: 0,
67-
modifier: Some('r'),
68-
span: _
69-
}
70-
)
71-
&& template[2] == InlineAsmTemplatePiece::String(", rbx".to_string())
72-
&& template[3] == InlineAsmTemplatePiece::String("\n".to_string())
73-
&& template[4] == InlineAsmTemplatePiece::String("cpuid".to_string())
74-
&& template[5] == InlineAsmTemplatePiece::String("\n".to_string())
75-
&& template[6] == InlineAsmTemplatePiece::String("xchg ".to_string())
76-
&& matches!(
77-
template[7],
78-
InlineAsmTemplatePiece::Placeholder {
79-
operand_idx: 0,
80-
modifier: Some('r'),
81-
span: _
82-
}
83-
)
84-
&& template[8] == InlineAsmTemplatePiece::String(", rbx".to_string())
85-
{
86-
assert_eq!(operands.len(), 4);
87-
let (leaf, eax_place) = match operands[1] {
88-
InlineAsmOperand::InOut {
89-
reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::ax)),
90-
late: _,
91-
ref in_value,
92-
out_place: Some(out_place),
93-
} => (
94-
crate::base::codegen_operand(fx, in_value).load_scalar(fx),
95-
crate::base::codegen_place(fx, out_place),
96-
),
97-
_ => unreachable!(),
98-
};
99-
let ebx_place = match operands[0] {
100-
InlineAsmOperand::Out {
101-
reg:
102-
InlineAsmRegOrRegClass::RegClass(InlineAsmRegClass::X86(
103-
X86InlineAsmRegClass::reg,
104-
)),
105-
late: _,
106-
place: Some(place),
107-
} => crate::base::codegen_place(fx, place),
108-
_ => unreachable!(),
109-
};
110-
let (sub_leaf, ecx_place) = match operands[2] {
111-
InlineAsmOperand::InOut {
112-
reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::cx)),
113-
late: _,
114-
ref in_value,
115-
out_place: Some(out_place),
116-
} => (
117-
crate::base::codegen_operand(fx, in_value).load_scalar(fx),
118-
crate::base::codegen_place(fx, out_place),
119-
),
120-
_ => unreachable!(),
121-
};
122-
let edx_place = match operands[3] {
123-
InlineAsmOperand::Out {
124-
reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::dx)),
125-
late: _,
126-
place: Some(place),
127-
} => crate::base::codegen_place(fx, place),
128-
_ => unreachable!(),
129-
};
130-
131-
let (eax, ebx, ecx, edx) = crate::intrinsics::codegen_cpuid_call(fx, leaf, sub_leaf);
132-
133-
eax_place.write_cvalue(fx, CValue::by_val(eax, fx.layout_of(fx.tcx.types.u32)));
134-
ebx_place.write_cvalue(fx, CValue::by_val(ebx, fx.layout_of(fx.tcx.types.u32)));
135-
ecx_place.write_cvalue(fx, CValue::by_val(ecx, fx.layout_of(fx.tcx.types.u32)));
136-
edx_place.write_cvalue(fx, CValue::by_val(edx, fx.layout_of(fx.tcx.types.u32)));
137-
let destination_block = fx.get_block(destination.unwrap());
138-
fx.bcx.ins().jump(destination_block, &[]);
139-
return;
140-
}
141-
142-
// Used by compiler-builtins
143-
if fx.tcx.symbol_name(fx.instance).name.starts_with("___chkstk") {
144-
// ___chkstk, ___chkstk_ms and __alloca are only used on Windows
145-
crate::trap::trap_unimplemented(fx, "Stack probes are not supported");
146-
return;
147-
} else if fx.tcx.symbol_name(fx.instance).name == "__alloca" {
148-
crate::trap::trap_unimplemented(fx, "Alloca is not supported");
149-
return;
150-
}
151-
152-
// Used by core::hint::spin_loop()
153-
if template[0]
154-
== InlineAsmTemplatePiece::String(".insn i 0x0F, 0, x0, x0, 0x010".to_string())
155-
&& template.len() == 1
156-
{
157-
let destination_block = fx.get_block(destination.unwrap());
158-
fx.bcx.ins().jump(destination_block, &[]);
159-
return;
160-
}
161-
162-
// Used by measureme
163-
if template[0] == InlineAsmTemplatePiece::String("xor %eax, %eax".to_string())
164-
&& template[1] == InlineAsmTemplatePiece::String("\n".to_string())
165-
&& template[2] == InlineAsmTemplatePiece::String("mov %rbx, ".to_string())
166-
&& matches!(
167-
template[3],
168-
InlineAsmTemplatePiece::Placeholder {
169-
operand_idx: 0,
170-
modifier: Some('r'),
171-
span: _
172-
}
173-
)
174-
&& template[4] == InlineAsmTemplatePiece::String("\n".to_string())
175-
&& template[5] == InlineAsmTemplatePiece::String("cpuid".to_string())
176-
&& template[6] == InlineAsmTemplatePiece::String("\n".to_string())
177-
&& template[7] == InlineAsmTemplatePiece::String("mov ".to_string())
178-
&& matches!(
179-
template[8],
180-
InlineAsmTemplatePiece::Placeholder {
181-
operand_idx: 0,
182-
modifier: Some('r'),
183-
span: _
184-
}
185-
)
186-
&& template[9] == InlineAsmTemplatePiece::String(", %rbx".to_string())
187-
{
188-
let destination_block = fx.get_block(destination.unwrap());
189-
fx.bcx.ins().jump(destination_block, &[]);
190-
return;
191-
} else if template[0] == InlineAsmTemplatePiece::String("rdpmc".to_string()) {
192-
// Return zero dummy values for all performance counters
193-
match operands[0] {
194-
InlineAsmOperand::In {
195-
reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::cx)),
196-
value: _,
197-
} => {}
198-
_ => unreachable!(),
199-
};
200-
let lo = match operands[1] {
201-
InlineAsmOperand::Out {
202-
reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::ax)),
203-
late: true,
204-
place: Some(place),
205-
} => crate::base::codegen_place(fx, place),
206-
_ => unreachable!(),
207-
};
208-
let hi = match operands[2] {
209-
InlineAsmOperand::Out {
210-
reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::dx)),
211-
late: true,
212-
place: Some(place),
213-
} => crate::base::codegen_place(fx, place),
214-
_ => unreachable!(),
215-
};
216-
217-
let u32_layout = fx.layout_of(fx.tcx.types.u32);
218-
let zero = fx.bcx.ins().iconst(types::I32, 0);
219-
lo.write_cvalue(fx, CValue::by_val(zero, u32_layout));
220-
hi.write_cvalue(fx, CValue::by_val(zero, u32_layout));
221-
222-
let destination_block = fx.get_block(destination.unwrap());
223-
fx.bcx.ins().jump(destination_block, &[]);
224-
return;
225-
} else if template[0] == InlineAsmTemplatePiece::String("lock xadd ".to_string())
226-
&& matches!(
227-
template[1],
228-
InlineAsmTemplatePiece::Placeholder { operand_idx: 1, modifier: None, span: _ }
229-
)
230-
&& template[2] == InlineAsmTemplatePiece::String(", (".to_string())
231-
&& matches!(
232-
template[3],
233-
InlineAsmTemplatePiece::Placeholder { operand_idx: 0, modifier: None, span: _ }
234-
)
235-
&& template[4] == InlineAsmTemplatePiece::String(")".to_string())
236-
{
237-
let destination_block = fx.get_block(destination.unwrap());
238-
fx.bcx.ins().jump(destination_block, &[]);
239-
return;
240-
}
241-
242-
if cfg!(not(feature = "inline_asm")) {
243-
fx.tcx.sess.span_err(
244-
span,
245-
"asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift",
246-
);
247-
} else {
248-
fx.tcx.sess.span_err(span, "asm! and global_asm! are not yet supported on Windows");
249-
}
47+
// Used by panic_abort on Windows, but uses a syntax which only happens to work with
48+
// asm!() by accident and breaks with the GNU assembler as well as global_asm!() for
49+
// the LLVM backend.
50+
if template[0] == InlineAsmTemplatePiece::String("int $$0x29".to_string()) {
51+
fx.bcx.ins().trap(TrapCode::User(1));
25052
return;
25153
}
25254

@@ -280,6 +82,12 @@ pub(crate) fn codegen_inline_asm<'tcx>(
28082
CInlineAsmOperand::Const { value }
28183
}
28284
InlineAsmOperand::SymFn { ref value } => {
85+
if cfg!(not(feature = "inline_asm_sym")) {
86+
fx.tcx
87+
.sess
88+
.span_err(span, "asm! and global_asm! sym operands are not yet supported");
89+
}
90+
28391
let const_ = fx.monomorphize(value.const_);
28492
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
28593
let instance = ty::Instance::resolve_for_fn_ptr(

‎src/intrinsics/cpuid.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.

‎src/intrinsics/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ macro_rules! intrinsic_args {
1212
}
1313
}
1414

15-
mod cpuid;
1615
mod llvm;
1716
mod llvm_aarch64;
1817
mod llvm_x86;
@@ -25,7 +24,6 @@ use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
2524
use rustc_middle::ty::GenericArgsRef;
2625
use rustc_span::symbol::{kw, sym, Symbol};
2726

28-
pub(crate) use self::cpuid::codegen_cpuid_call;
2927
pub(crate) use self::llvm::codegen_llvm_intrinsic_call;
3028
use crate::prelude::*;
3129

0 commit comments

Comments
 (0)
Please sign in to comment.