Skip to content
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

Make i128 aligned to 16 bytes on all 64bit plats #38871

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/librustc_llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ extern "C" {
-> ValueRef;
pub fn LLVMSetFunctionCallConv(Fn: ValueRef, CC: c_uint);
pub fn LLVMRustAddDereferenceableAttr(Fn: ValueRef, index: c_uint, bytes: u64);
pub fn LLVMRustAddAlignAttr(Fn: ValueRef, index: c_uint, bytes: u64);
pub fn LLVMRustAddFunctionAttribute(Fn: ValueRef, index: c_uint, attr: Attribute);
pub fn LLVMRustAddFunctionAttrStringValue(Fn: ValueRef,
index: c_uint,
Expand Down Expand Up @@ -721,6 +722,7 @@ extern "C" {
pub fn LLVMSetInstructionCallConv(Instr: ValueRef, CC: c_uint);
pub fn LLVMRustAddCallSiteAttribute(Instr: ValueRef, index: c_uint, attr: Attribute);
pub fn LLVMRustAddDereferenceableCallSiteAttr(Instr: ValueRef, index: c_uint, bytes: u64);
pub fn LLVMRustAddAlignCallSiteAttr(Instr: ValueRef, index: c_uint, bytes: u64);

// Operations on load/store instructions (only)
pub fn LLVMSetVolatile(MemoryAccessInst: ValueRef, volatile: Bool);
Expand Down
12 changes: 12 additions & 0 deletions src/librustc_trans/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl ArgAttribute {
pub struct ArgAttributes {
regular: ArgAttribute,
dereferenceable_bytes: u64,
align: u64,
}

impl ArgAttributes {
Expand All @@ -109,6 +110,11 @@ impl ArgAttributes {
self
}

pub fn set_align(&mut self, align: u64) -> &mut Self {
self.align = align;
self
}

pub fn apply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
unsafe {
self.regular.for_each_kind(|attr| attr.apply_llfn(idx, llfn));
Expand All @@ -117,6 +123,9 @@ impl ArgAttributes {
idx.as_uint(),
self.dereferenceable_bytes);
}
if self.align != 0 {
llvm::LLVMRustAddAlignAttr(llfn, idx.as_uint(), self.align);
}
}
}

Expand All @@ -128,6 +137,9 @@ impl ArgAttributes {
idx.as_uint(),
self.dereferenceable_bytes);
}
if self.align != 0 {
llvm::LLVMRustAddAlignCallSiteAttr(callsite, idx.as_uint(), self.align);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_trans/cabi_asmjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![allow(non_upper_case_globals)]

use llvm::{Struct, Array};
use abi::{FnType, ArgType, ArgAttribute};
use abi::{FnType, ArgType, ArgAttribute, ty_align};
use context::CrateContext;

// Data layout: e-p:32:32-i64:64-v128:32:128-n32-S128
Expand Down Expand Up @@ -40,6 +40,7 @@ fn classify_arg_ty(ccx: &CrateContext, arg: &mut ArgType) {
if arg.ty.is_aggregate() {
arg.make_indirect(ccx);
arg.attrs.set(ArgAttribute::ByVal);
arg.attrs.set_align(ty_align(arg.ty, 0) as u64);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustc_trans/cabi_x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use llvm::*;
use abi::{ArgAttribute, FnType};
use abi::{ArgAttribute, FnType, ty_align};
use type_::Type;
use super::common::*;
use super::machine::*;
Expand Down Expand Up @@ -53,6 +53,7 @@ pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType, flavor: Flavor) {
if arg.ty.kind() == Struct {
arg.make_indirect(ccx);
arg.attrs.set(ArgAttribute::ByVal);
arg.attrs.set_align(ty_align(arg.ty, 0) as u64);
} else {
arg.extend_integer_width_to(32);
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_trans/cabi_x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use self::RegClass::*;

use llvm::{Integer, Pointer, Float, Double};
use llvm::{Struct, Array, Vector};
use abi::{self, ArgType, ArgAttribute, FnType};
use abi::{self, ArgType, ArgAttribute, FnType, ty_align};
use context::CrateContext;
use type_::Type;

Expand Down Expand Up @@ -343,6 +343,7 @@ pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType) {
arg.make_indirect(ccx);
if let Some(attr) = ind_attr {
arg.attrs.set(attr);
arg.attrs.set_align(ty_align(arg.ty, 0) as u64);
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this a noop? (same for all the other cabi_* changes)

Copy link
Member Author

Choose a reason for hiding this comment

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

See #38870 for this.

}
} else {
arg.cast = Some(llreg_ty(ccx, &cls));
Expand Down
13 changes: 10 additions & 3 deletions src/rt/rust_test_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,17 @@ LARGE_INTEGER increment_all_parts(LARGE_INTEGER li) {
return li;
}

#define DO_INT128_TEST !(defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && \
defined(__amd64__)
#if !(defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && defined(__amd64__)

#if DO_INT128_TEST
struct Foo {
__int128 a;
int8_t b;
uint16_t c;
};

struct Foo adt_id(struct Foo foo) {
return foo;
}

unsigned __int128 identity(unsigned __int128 a) {
return a;
Expand Down
19 changes: 19 additions & 0 deletions src/rustllvm/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
AttributeSet::get(Call->getContext(), Index, B)));
}

extern "C" void LLVMRustAddAlignCallSiteAttr(LLVMValueRef Instr,
unsigned Index,
uint64_t Bytes) {
CallSite Call = CallSite(unwrap<Instruction>(Instr));
AttrBuilder B;
B.addAlignmentAttr(Bytes);
Call.setAttributes(Call.getAttributes().addAttributes(
Call->getContext(), Index,
AttributeSet::get(Call->getContext(), Index, B)));
}

extern "C" void LLVMRustAddFunctionAttribute(LLVMValueRef Fn, unsigned Index,
LLVMRustAttribute RustAttr) {
Function *A = unwrap<Function>(Fn);
Expand All @@ -190,6 +201,14 @@ extern "C" void LLVMRustAddDereferenceableAttr(LLVMValueRef Fn, unsigned Index,
A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
}

extern "C" void LLVMRustAddAlignAttr(LLVMValueRef Fn, unsigned Index,
uint64_t Bytes) {
Function *A = unwrap<Function>(Fn);
AttrBuilder B;
B.addAlignmentAttr(Bytes);
A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
}

extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn,
unsigned Index,
const char *Name,
Expand Down
12 changes: 12 additions & 0 deletions src/test/run-pass/i128-ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@

#![feature(i128_type)]

#[repr(C)]
#[derive(Copy, Clone, PartialEq, Debug)]
struct Foo {
a: i128,
b: i8,
c: u16,
}

#[link(name = "rust_test_helpers", kind = "static")]
extern "C" {
fn identity(f: u128) -> u128;
fn square(f: i128) -> i128;
fn sub(f: i128, f: i128) -> i128;
fn adt_id(f: Foo) -> Foo;
}

fn main() {
Expand All @@ -41,5 +50,8 @@ fn main() {
let k_d = 0x2468_ACF1_3579_BDFF_DB97_530E_CA86_420;
let k_out = sub(k_d, k);
assert_eq!(k, k_out);
let a = Foo { a: 1, b: 2, c: 3 };
let b = adt_id(a);
assert_eq!(a, b);
}
}