-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Set target-abi module flag for RISC-V targets #123612
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1801,12 +1801,21 @@ extern "C" { | |
/// | ||
/// In order for Rust-C LTO to work, module flags must be compatible with Clang. What | ||
/// "compatible" means depends on the merge behaviors involved. | ||
pub fn LLVMRustAddModuleFlag( | ||
pub fn LLVMRustAddModuleFlagU32( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can keep its name so that the change can be smaller; consider only riscv needs to set a string flag. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it doesn't hurt because the current naming scheme is more consistent(method name with a type suffix. And we don't have to care about patch size. |
||
M: &Module, | ||
merge_behavior: LLVMModFlagBehavior, | ||
name: *const c_char, | ||
value: u32, | ||
); | ||
|
||
pub fn LLVMRustAddModuleFlagString( | ||
M: &Module, | ||
merge_behavior: LLVMModFlagBehavior, | ||
name: *const c_char, | ||
value: *const c_char, | ||
value_len: size_t, | ||
); | ||
|
||
pub fn LLVMRustHasModuleFlag(M: &Module, name: *const c_char, len: size_t) -> bool; | ||
|
||
pub fn LLVMRustDIBuilderCreate(M: &Module) -> &mut DIBuilder<'_>; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//@ revisions:riscv64gc riscv32gc riscv32imac | ||
|
||
//@[riscv64gc] compile-flags: --target=riscv64gc-unknown-linux-gnu | ||
//@[riscv64gc] needs-llvm-components: riscv | ||
// riscv64gc: !{i32 1, !"target-abi", !"lp64d"} | ||
|
||
//@[riscv32gc] compile-flags: --target=riscv32gc-unknown-linux-musl | ||
//@[riscv32gc] needs-llvm-components: riscv | ||
// riscv32gc: !{i32 1, !"target-abi", !"ilp32d"} | ||
|
||
//@[riscv32imac] compile-flags: --target=riscv32imac-unknown-none-elf | ||
//@[riscv32imac] needs-llvm-components: riscv | ||
// riscv32imac-NOT: !"target-abi" | ||
|
||
#![feature(no_core, lang_items)] | ||
#![crate_type = "lib"] | ||
#![no_core] | ||
|
||
#[lang = "sized"] | ||
trait Sized {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extern void hello(); | ||
|
||
void _start() { | ||
hello(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#![allow(internal_features)] | ||
#![feature(no_core, lang_items)] | ||
#![no_core] | ||
|
||
#[lang = "sized"] | ||
trait Sized {} | ||
|
||
#[no_mangle] | ||
pub fn hello() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//! Make sure that cross-language LTO works on riscv targets, | ||
//! which requires extra abi metadata to be emitted. | ||
//@ needs-matching-clang | ||
//@ needs-llvm-components riscv | ||
extern crate run_make_support; | ||
|
||
use run_make_support::{bin_name, rustc, tmp_dir}; | ||
use std::{ | ||
env, | ||
path::PathBuf, | ||
process::{Command, Output}, | ||
str, | ||
}; | ||
|
||
fn handle_failed_output(output: Output) { | ||
eprintln!("output status: `{}`", output.status); | ||
eprintln!("=== STDOUT ===\n{}\n\n", String::from_utf8(output.stdout).unwrap()); | ||
eprintln!("=== STDERR ===\n{}\n\n", String::from_utf8(output.stderr).unwrap()); | ||
std::process::exit(1) | ||
} | ||
|
||
fn check_target(target: &str, clang_target: &str, carch: &str, is_double_float: bool) { | ||
eprintln!("Checking target {target}"); | ||
// Rust part | ||
rustc() | ||
.input("riscv-xlto.rs") | ||
.crate_type("rlib") | ||
.target(target) | ||
.panic("abort") | ||
.linker_plugin_lto("on") | ||
.run(); | ||
// C part | ||
let clang = env::var("CLANG").unwrap(); | ||
jieyouxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let mut cmd = Command::new(clang); | ||
let executable = tmp_dir().join("riscv-xlto"); | ||
cmd.arg("-target") | ||
.arg(clang_target) | ||
.arg(format!("-march={carch}")) | ||
.arg(format!("-flto=thin")) | ||
.arg(format!("-fuse-ld=lld")) | ||
.arg("-nostdlib") | ||
.arg("-o") | ||
.arg(&executable) | ||
.arg("cstart.c") | ||
.arg(tmp_dir().join("libriscv_xlto.rlib")); | ||
eprintln!("{cmd:?}"); | ||
let output = cmd.output().unwrap(); | ||
if !output.status.success() { | ||
handle_failed_output(output); | ||
} | ||
// Check that the built binary has correct float abi | ||
let llvm_readobj = | ||
PathBuf::from(env::var("LLVM_BIN_DIR").unwrap()).join(bin_name("llvm-readobj")); | ||
jieyouxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let mut cmd = Command::new(llvm_readobj); | ||
cmd.arg("--file-header").arg(executable); | ||
eprintln!("{cmd:?}"); | ||
let output = cmd.output().unwrap(); | ||
if output.status.success() { | ||
assert!( | ||
!(is_double_float | ||
^ dbg!(str::from_utf8(&output.stdout).unwrap()) | ||
.contains("EF_RISCV_FLOAT_ABI_DOUBLE")) | ||
) | ||
} else { | ||
handle_failed_output(output); | ||
} | ||
} | ||
|
||
fn main() { | ||
check_target("riscv64gc-unknown-linux-gnu", "riscv64-linux-gnu", "rv64gc", true); | ||
check_target("riscv64imac-unknown-none-elf", "riscv64-unknown-elf", "rv64imac", false); | ||
check_target("riscv32imac-unknown-none-elf", "riscv32-unknown-elf", "rv32imac", false); | ||
check_target("riscv32gc-unknown-linux-gnu", "riscv32-linux-gnu", "rv32gc", true); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.