Skip to content

add support for building outlined aarch64 intrinsics #407

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod c {

use std::collections::BTreeMap;
use std::env;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

struct Sources {
// SYMBOL -> PATH TO SOURCE
Expand Down Expand Up @@ -487,7 +487,20 @@ mod c {
// use of that macro in lib/builtins/int_util.h in compiler-rt.
cfg.flag_if_supported(&format!("-ffile-prefix-map={}=.", root.display()));

// Include out-of-line atomics for aarch64, which are all generated by supplying different
// sets of flags to the same source file.
let src_dir = root.join("lib/builtins");
if target_arch == "aarch64" {
let atomics_libs = build_aarch64_out_of_line_atomics_libraries(&src_dir, cfg);
if !atomics_libs.is_empty() {
for library in atomics_libs {
cfg.object(library);
}
// Some run-time CPU feature detection is necessary, as well.
sources.extend(&[("__aarch64_have_lse_atomics", "cpu_model.c")]);
}
}

for (sym, src) in sources.map.iter() {
let src = src_dir.join(src);
cfg.file(&src);
Expand All @@ -497,4 +510,60 @@ mod c {

cfg.compile("libcompiler-rt.a");
}

fn build_aarch64_out_of_line_atomics_libraries(
builtins_dir: &Path,
cfg: &cc::Build,
) -> Vec<PathBuf> {
// NOTE: because we're recompiling the same source file in N different ways, building
// serially is necessary. If we want to lift this restriction, we can either:
// - create symlinks to lse.S and build those_(though we'd still need to pass special
// #define-like flags to each of these), or
// - synthesizing tiny .S files in out/ with the proper #defines, which ultimately #include
// lse.S.
// That said, it's unclear how useful this added complexity will be, so just do the simple
// thing for now.
let outlined_atomics_file = builtins_dir.join("aarch64/lse.S");

// A stable release hasn't been made with lse.S yet. Until we pick that up, do nothing.
if !outlined_atomics_file.exists() {
return vec![];
}

println!("cargo:rerun-if-changed={}", outlined_atomics_file.display());
let out_dir: PathBuf = env::var("OUT_DIR").unwrap().into();

// Ideally, this would be a Vec of object files, but cc doesn't make it *entirely*
// trivial to build an individual object.
let mut atomics_libraries = Vec::new();
for instruction_type in &["cas", "cwp", "ldadd", "ldclr", "ldeor", "ldset"] {
Copy link
Member

Choose a reason for hiding this comment

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

Should this be swp instead of cwp?

for size in &[1, 2, 4, 8, 16] {
if *size == 16 && *instruction_type != "cas" {
continue;
}

for (model_number, model_name) in
&[(1, "relax"), (2, "acq"), (3, "rel"), (4, "acq_rel")]
{
let library_name = format!(
"liboutline_atomic_helper_{}_{}_{}.a",
instruction_type, size, model_name
);
let sym = format!("__aarch64_{}{}_{}", instruction_type, size, model_name);
let mut cfg = cfg.clone();

cfg.include(&builtins_dir)
.define(&format!("L_{}", instruction_type), None)
.define("SIZE", size.to_string().as_str())
.define("MODEL", model_number.to_string().as_str())
.file(&outlined_atomics_file);
cfg.compile(&library_name);

atomics_libraries.push(out_dir.join(library_name));
println!("cargo:rustc-cfg={}=\"optimized-c\"", sym);
}
}
}
atomics_libraries
}
}