Skip to content

Commit

Permalink
Fix ABI compliance when passing structs around
Browse files Browse the repository at this point in the history
When accepting or returning structs, the data must be copied using
memcpy into a temporary slot, this slot must then be loaded into the
final slot. Without this, LLVM appears to generate the wrong code on
platforms such as ARM64.

In addition, for ARM64 we now generate the correct layouts for
homogeneous float structs (e.g. `{ f64, f64, f64, f64 }`).

This fixes #792.
  • Loading branch information
yorickpeterse committed Jan 12, 2025
1 parent 71a9d23 commit 3356191
Show file tree
Hide file tree
Showing 11 changed files with 606 additions and 97 deletions.
25 changes: 18 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ jobs:
key: amd64-linux-gnu-${{ hashFiles('Cargo.lock', 'rust-toolchain.toml') }}
- name: Run compiler tests
run: cargo test
# We run tests with and without optimizations, such that we can catch any
# potential miscompilations introduced by optimizations. We only do this
# for this particular target as our optimizations aren't target specific.
- name: Run stdlib tests with optimizations
- name: Run stdlib tests with default optimizations
run: 'cd std && cargo run -- test'
- name: Run stdlib tests without optimizations
run: 'cd std && cargo run -- test --opt=none'
- name: Run stdlib tests with aggressive optimizations
run: 'cd std && cargo run -- test --opt=aggressive'

amd64-linux-musl:
timeout-minutes: 15
Expand All @@ -97,8 +96,12 @@ jobs:
key: amd64-linux-musl-${{ hashFiles('Cargo.lock', 'rust-toolchain.toml') }}
- name: Run compiler tests
run: cargo test
- name: Run stdlib tests
- name: Run stdlib tests with default optimizations
run: 'cd std && cargo run -- test'
- name: Run stdlib tests without optimizations
run: 'cd std && cargo run -- test --opt=none'
- name: Run stdlib tests with aggressive optimizations
run: 'cd std && cargo run -- test --opt=aggressive'

amd64-mac-native:
timeout-minutes: 15
Expand All @@ -117,8 +120,12 @@ jobs:
run: ./ci/mac.sh
- name: Run compiler tests
run: cargo test
- name: Run stdlib tests
- name: Run stdlib tests with default optimizations
run: 'cd std && cargo run -- test'
- name: Run stdlib tests without optimizations
run: 'cd std && cargo run -- test --opt=none'
- name: Run stdlib tests with aggressive optimizations
run: 'cd std && cargo run -- test --opt=aggressive'

arm64-mac-native:
timeout-minutes: 15
Expand All @@ -137,8 +144,12 @@ jobs:
run: ./ci/mac.sh
- name: Run compiler tests
run: cargo test
- name: Run stdlib tests
- name: Run stdlib tests with default optimizations
run: 'cd std && cargo run -- test'
- name: Run stdlib tests without optimizations
run: 'cd std && cargo run -- test --opt=none'
- name: Run stdlib tests with aggressive optimizations
run: 'cd std && cargo run -- test --opt=aggressive'

amd64-freebsd-native:
timeout-minutes: 15
Expand Down
1 change: 1 addition & 0 deletions compiler/src/llvm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod abi;
pub(crate) mod builder;
pub(crate) mod constants;
pub(crate) mod context;
Expand Down
42 changes: 42 additions & 0 deletions compiler/src/llvm/abi/amd64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::llvm::context::{size_in_bits, Context};
use crate::llvm::layouts::{ArgumentType, ReturnType};
use inkwell::targets::TargetData;
use inkwell::types::{BasicType, StructType};

pub(crate) fn struct_argument<'ctx>(
ctx: &'ctx Context,
tdata: &TargetData,
typ: StructType<'ctx>,
) -> ArgumentType<'ctx> {
let bytes = tdata.get_abi_size(&typ) as u32;

if bytes <= 8 {
let bits = ctx.custom_int(size_in_bits(bytes));

ArgumentType::Regular(bits.as_basic_type_enum())
} else if bytes <= 16 {
ArgumentType::Regular(
ctx.binary_struct(tdata, typ).as_basic_type_enum(),
)
} else {
ArgumentType::StructValue(typ)
}
}

pub(crate) fn struct_return<'ctx>(
ctx: &'ctx Context,
tdata: &TargetData,
typ: StructType<'ctx>,
) -> ReturnType<'ctx> {
let bytes = tdata.get_abi_size(&typ) as u32;

if bytes <= 8 {
let bits = ctx.custom_int(size_in_bits(bytes));

ReturnType::Regular(bits.as_basic_type_enum())
} else if bytes <= 16 {
ReturnType::Regular(ctx.binary_struct(tdata, typ).as_basic_type_enum())
} else {
ReturnType::Struct(typ)
}
}
211 changes: 211 additions & 0 deletions compiler/src/llvm/abi/arm64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
use crate::llvm::abi::generic::classify;
use crate::llvm::context::{size_in_bits, Context};
use crate::llvm::layouts::{ArgumentType, ReturnType};
use inkwell::targets::TargetData;
use inkwell::types::{BasicType, StructType};

pub(crate) fn struct_argument<'ctx>(
ctx: &'ctx Context,
tdata: &TargetData,
typ: StructType<'ctx>,
) -> ArgumentType<'ctx> {
if let Some(h) = homogeneous_struct(ctx, tdata, typ) {
return ArgumentType::Regular(h.as_basic_type_enum());
}

let bytes = tdata.get_abi_size(&typ) as u32;

if bytes <= 8 {
return ArgumentType::Regular(ctx.i64_type().as_basic_type_enum());
}

if bytes <= 16 {
ArgumentType::Regular(ctx.two_words().as_basic_type_enum())
} else {
// clang and Rust don't use "byval" for ARM64 when the struct is too
// large, so neither do we.
ArgumentType::Pointer
}
}

pub(crate) fn struct_return<'ctx>(
ctx: &'ctx Context,
tdata: &TargetData,
typ: StructType<'ctx>,
) -> ReturnType<'ctx> {
let bytes = tdata.get_abi_size(&typ) as u32;

if let Some(h) = homogeneous_struct(ctx, tdata, typ) {
return ReturnType::Regular(h.as_basic_type_enum());
}

if bytes <= 8 {
let bits = ctx.custom_int(size_in_bits(bytes));

return ReturnType::Regular(bits.as_basic_type_enum());
}

if bytes <= 16 {
ReturnType::Regular(ctx.two_words().as_basic_type_enum())
} else {
ReturnType::Struct(typ)
}
}

pub(crate) fn homogeneous_struct<'ctx>(
context: &'ctx Context,
tdata: &TargetData,
typ: StructType<'ctx>,
) -> Option<StructType<'ctx>> {
let mut classes = Vec::new();

classify(tdata, typ.as_basic_type_enum(), &mut classes);

if classes.is_empty() || classes.len() > 4 {
return None;
}

let first = classes[0];

if classes.iter().all(|&c| c.is_float() && c == first) {
let fields: Vec<_> =
classes.into_iter().map(|c| c.to_llvm_type(context)).collect();

Some(context.struct_type(&fields))
} else {
None
}
}

#[cfg(test)]
mod tests {
use super::*;
use inkwell::targets::{
CodeModel, InitializationConfig, RelocMode, Target, TargetMachine,
TargetTriple,
};
use inkwell::types::BasicTypeEnum;
use inkwell::OptimizationLevel;

fn setup() -> TargetMachine {
Target::initialize_aarch64(&InitializationConfig::default());

let triple = TargetTriple::create("aarch64-unknown-linux-gnu");

Target::from_triple(&triple)
.unwrap()
.create_target_machine(
&triple,
"",
"",
OptimizationLevel::None,
RelocMode::PIC,
CodeModel::Default,
)
.unwrap()
}

#[test]
fn test_struct_argument_with_homogeneous() {
let machine = setup();
let tdata = machine.get_target_data();
let ctx = Context::new();
let tests = [
(vec![ctx.f32_type().into()], vec![ctx.f32_type().into()]),
(vec![ctx.f32_type().into(); 2], vec![ctx.f32_type().into(); 2]),
(vec![ctx.f32_type().into(); 3], vec![ctx.f32_type().into(); 3]),
(vec![ctx.f32_type().into(); 4], vec![ctx.f32_type().into(); 4]),
(vec![ctx.f64_type().into()], vec![ctx.f64_type().into()]),
(vec![ctx.f64_type().into(); 2], vec![ctx.f64_type().into(); 2]),
(vec![ctx.f64_type().into(); 3], vec![ctx.f64_type().into(); 3]),
(vec![ctx.f64_type().into(); 4], vec![ctx.f64_type().into(); 4]),
];

for (in_fields, out_fields) in tests {
let inp = ctx.struct_type(&in_fields.as_slice());
let ArgumentType::Regular(BasicTypeEnum::StructType(out)) =
struct_argument(&ctx, &tdata, inp)
else {
panic!("expected a struct")
};

assert_eq!(out.get_field_types(), out_fields);
}
}

#[test]
fn test_struct_argument_with_scalar() {
let machine = setup();
let tdata = machine.get_target_data();
let ctx = Context::new();
let int64 = ctx.i64_type().as_basic_type_enum();
let tests = [
(vec![ctx.i8_type().into()], ArgumentType::Regular(int64)),
(vec![ctx.i16_type().into()], ArgumentType::Regular(int64)),
(vec![ctx.i32_type().into()], ArgumentType::Regular(int64)),
(vec![ctx.i64_type().into()], ArgumentType::Regular(int64)),
(
vec![ctx.i32_type().into(), ctx.i32_type().into()],
ArgumentType::Regular(int64),
),
];

for (in_fields, exp) in tests {
let inp = ctx.struct_type(&in_fields.as_slice());

assert_eq!(struct_argument(&ctx, &tdata, inp), exp);
}
}

#[test]
fn test_struct_argument_sixteen_bytes() {
let machine = setup();
let tdata = machine.get_target_data();
let ctx = Context::new();
let inp =
ctx.struct_type(&[ctx.i64_type().into(), ctx.i32_type().into()]);
let ArgumentType::Regular(BasicTypeEnum::StructType(out)) =
struct_argument(&ctx, &tdata, inp)
else {
panic!("expected a struct")
};

assert_eq!(
out.get_field_types(),
vec![ctx.i64_type().into(), ctx.i64_type().into()]
);
}

#[test]
fn test_struct_argument_large() {
let machine = setup();
let tdata = machine.get_target_data();
let ctx = Context::new();
let inp = ctx.struct_type(&[
ctx.i64_type().into(),
ctx.i64_type().into(),
ctx.i64_type().into(),
]);

assert_eq!(struct_argument(&ctx, &tdata, inp), ArgumentType::Pointer);
}

#[test]
fn test_struct_argument_mixed_floats() {
let machine = setup();
let tdata = machine.get_target_data();
let ctx = Context::new();
let inp =
ctx.struct_type(&[ctx.f64_type().into(), ctx.f32_type().into()]);
let ArgumentType::Regular(BasicTypeEnum::StructType(out)) =
struct_argument(&ctx, &tdata, inp)
else {
panic!("expected a struct")
};

assert_eq!(
out.get_field_types(),
vec![ctx.i64_type().into(), ctx.i64_type().into()]
);
}
}
Loading

0 comments on commit 3356191

Please sign in to comment.