Skip to content

Commit 2f01db9

Browse files
authored
Eliminate build.rs-generated Aarch64 atomic macros (#951)
Replace `build.rs` Rust generation with macros, using the unstable `${concat(...)}`. Fixes: #947
1 parent add44a7 commit 2f01db9

File tree

4 files changed

+75
-65
lines changed

4 files changed

+75
-65
lines changed

builtins-test/tests/lse.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(decl_macro)] // so we can use pub(super)
2+
#![feature(macro_metavar_expr_concat)]
23
#![cfg(all(target_arch = "aarch64", target_os = "linux", not(feature = "no-asm")))]
34

45
/// Translate a byte size to a Rust type.
@@ -87,7 +88,7 @@ test_op!(add, |left, right| left.wrapping_add(right));
8788
test_op!(clr, |left, right| left & !right);
8889
test_op!(xor, std::ops::BitXor::bitxor);
8990
test_op!(or, std::ops::BitOr::bitor);
90-
91+
use compiler_builtins::{foreach_bytes, foreach_ordering};
9192
compiler_builtins::foreach_cas!(cas::test);
9293
compiler_builtins::foreach_cas16!(test_cas16);
9394
compiler_builtins::foreach_swp!(swap::test);

compiler-builtins/build.rs

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
mod configure;
22

3-
use std::collections::BTreeMap;
43
use std::env;
5-
use std::path::PathBuf;
6-
use std::sync::atomic::Ordering;
74

85
use configure::{Target, configure_aliases, configure_f16_f128};
96

@@ -85,10 +82,6 @@ fn main() {
8582
{
8683
println!("cargo:rustc-cfg=kernel_user_helpers")
8784
}
88-
89-
if llvm_target[0].starts_with("aarch64") {
90-
generate_aarch64_outlined_atomics();
91-
}
9285
}
9386

9487
/// Run configuration for `libm` since it is included directly.
@@ -131,61 +124,6 @@ fn configure_libm(target: &Target) {
131124
println!("cargo:rustc-cfg=feature=\"unstable-intrinsics\"");
132125
}
133126

134-
fn aarch64_symbol(ordering: Ordering) -> &'static str {
135-
match ordering {
136-
Ordering::Relaxed => "relax",
137-
Ordering::Acquire => "acq",
138-
Ordering::Release => "rel",
139-
Ordering::AcqRel => "acq_rel",
140-
_ => panic!("unknown symbol for {ordering:?}"),
141-
}
142-
}
143-
144-
/// The `concat_idents` macro is extremely annoying and doesn't allow us to define new items.
145-
/// Define them from the build script instead.
146-
/// Note that the majority of the code is still defined in `aarch64.rs` through inline macros.
147-
fn generate_aarch64_outlined_atomics() {
148-
use std::fmt::Write;
149-
// #[macro_export] so that we can use this in tests
150-
let gen_macro =
151-
|name| format!("#[macro_export] macro_rules! foreach_{name} {{ ($macro:path) => {{\n");
152-
153-
// Generate different macros for add/clr/eor/set so that we can test them separately.
154-
let sym_names = ["cas", "ldadd", "ldclr", "ldeor", "ldset", "swp"];
155-
let mut macros = BTreeMap::new();
156-
for sym in sym_names {
157-
macros.insert(sym, gen_macro(sym));
158-
}
159-
160-
// Only CAS supports 16 bytes, and it has a different implementation that uses a different macro.
161-
let mut cas16 = gen_macro("cas16");
162-
163-
for ordering in [
164-
Ordering::Relaxed,
165-
Ordering::Acquire,
166-
Ordering::Release,
167-
Ordering::AcqRel,
168-
] {
169-
let sym_ordering = aarch64_symbol(ordering);
170-
for size in [1, 2, 4, 8] {
171-
for (sym, macro_) in &mut macros {
172-
let name = format!("__aarch64_{sym}{size}_{sym_ordering}");
173-
writeln!(macro_, "$macro!( {ordering:?}, {size}, {name} );").unwrap();
174-
}
175-
}
176-
let name = format!("__aarch64_cas16_{sym_ordering}");
177-
writeln!(cas16, "$macro!( {ordering:?}, {name} );").unwrap();
178-
}
179-
180-
let mut buf = String::new();
181-
for macro_def in macros.values().chain(std::iter::once(&cas16)) {
182-
buf += macro_def;
183-
buf += "}; }\n";
184-
}
185-
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
186-
std::fs::write(out_dir.join("outlined_atomics.rs"), buf).unwrap();
187-
}
188-
189127
/// Emit directives for features we expect to support that aren't in `Cargo.toml`.
190128
///
191129
/// These are mostly cfg elements emitted by this `build.rs`.

compiler-builtins/src/aarch64_linux.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,78 @@ macro_rules! or {
262262
};
263263
}
264264

265-
// See `generate_aarch64_outlined_atomics` in build.rs.
266-
include!(concat!(env!("OUT_DIR"), "/outlined_atomics.rs"));
265+
#[macro_export]
266+
macro_rules! foreach_ordering {
267+
($macro:path, $bytes:tt, $name:ident) => {
268+
$macro!( Relaxed, $bytes, ${concat($name, _relax)} );
269+
$macro!( Acquire, $bytes, ${concat($name, _acq)} );
270+
$macro!( Release, $bytes, ${concat($name, _rel)} );
271+
$macro!( AcqRel, $bytes, ${concat($name, _acq_rel)} );
272+
};
273+
($macro:path, $name:ident) => {
274+
$macro!( Relaxed, ${concat($name, _relax)} );
275+
$macro!( Acquire, ${concat($name, _acq)} );
276+
$macro!( Release, ${concat($name, _rel)} );
277+
$macro!( AcqRel, ${concat($name, _acq_rel)} );
278+
};
279+
}
280+
281+
#[macro_export]
282+
macro_rules! foreach_bytes {
283+
($macro:path, $name:ident) => {
284+
foreach_ordering!( $macro, 1, ${concat(__aarch64_, $name, "1")} );
285+
foreach_ordering!( $macro, 2, ${concat(__aarch64_, $name, "2")} );
286+
foreach_ordering!( $macro, 4, ${concat(__aarch64_, $name, "4")} );
287+
foreach_ordering!( $macro, 8, ${concat(__aarch64_, $name, "8")} );
288+
};
289+
}
290+
291+
/// Generate different macros for cas/swp/add/clr/eor/set so that we can test them separately.
292+
#[macro_export]
293+
macro_rules! foreach_cas {
294+
($macro:path) => {
295+
foreach_bytes!($macro, cas);
296+
};
297+
}
298+
299+
/// Only CAS supports 16 bytes, and it has a different implementation that uses a different macro.
300+
#[macro_export]
301+
macro_rules! foreach_cas16 {
302+
($macro:path) => {
303+
foreach_ordering!($macro, __aarch64_cas16);
304+
};
305+
}
306+
#[macro_export]
307+
macro_rules! foreach_swp {
308+
($macro:path) => {
309+
foreach_bytes!($macro, swp);
310+
};
311+
}
312+
#[macro_export]
313+
macro_rules! foreach_ldadd {
314+
($macro:path) => {
315+
foreach_bytes!($macro, ldadd);
316+
};
317+
}
318+
#[macro_export]
319+
macro_rules! foreach_ldclr {
320+
($macro:path) => {
321+
foreach_bytes!($macro, ldclr);
322+
};
323+
}
324+
#[macro_export]
325+
macro_rules! foreach_ldeor {
326+
($macro:path) => {
327+
foreach_bytes!($macro, ldeor);
328+
};
329+
}
330+
#[macro_export]
331+
macro_rules! foreach_ldset {
332+
($macro:path) => {
333+
foreach_bytes!($macro, ldset);
334+
};
335+
}
336+
267337
foreach_cas!(compare_and_swap);
268338
foreach_cas16!(compare_and_swap_i128);
269339
foreach_swp!(swap);

compiler-builtins/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(linkage)]
99
#![feature(naked_functions)]
1010
#![feature(repr_simd)]
11+
#![feature(macro_metavar_expr_concat)]
1112
#![cfg_attr(f16_enabled, feature(f16))]
1213
#![cfg_attr(f128_enabled, feature(f128))]
1314
#![no_builtins]

0 commit comments

Comments
 (0)