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

Add zeroext/signext attributes to C ABI functions where appropriate #31725

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/librustc_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ impl AttrBuilder {
}
}

pub fn merge(&mut self, other: AttrBuilder) {
for attr in other.attrs {
self.attrs.push(attr);
}
}

pub fn arg<'a, T: AttrHelper + 'static>(&'a mut self, idx: usize, a: T) -> &'a mut AttrBuilder {
self.attrs.push((idx, box a as Box<AttrHelper+'static>));
self
Expand Down
21 changes: 19 additions & 2 deletions src/librustc_trans/trans/cabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub enum ArgKind {
/// Pass the argument directly using the normal converted
/// LLVM type or by coercing to another specified type
Direct,
/// Extend the argument for ABI requirements. The signedness
/// of the argument is required to select the appropriate
/// extension method
Extend,
/// Pass the argument indirectly via a hidden pointer
Indirect,
/// Ignore the argument (useful for empty struct)
Expand Down Expand Up @@ -85,13 +89,27 @@ impl ArgType {
}
}

pub fn extend(ty: Type) -> ArgType {
ArgType {
kind: Extend,
ty: ty,
cast: None,
pad: None,
attr: None,
}
}

pub fn is_indirect(&self) -> bool {
return self.kind == Indirect;
}

pub fn is_ignore(&self) -> bool {
return self.kind == Ignore;
}

pub fn is_extend(&self) -> bool {
return self.kind == Extend;
}
}

/// Metadata describing how the arguments to a native function
Expand Down Expand Up @@ -131,7 +149,6 @@ pub fn compute_abi_info(ccx: &CrateContext,
"powerpc" => cabi_powerpc::compute_abi_info(ccx, atys, rty, ret_def),
"powerpc64" => cabi_powerpc64::compute_abi_info(ccx, atys, rty, ret_def),
"asmjs" => cabi_asmjs::compute_abi_info(ccx, atys, rty, ret_def),
a => ccx.sess().fatal(&format!("unrecognized arch \"{}\" in target specification", a)
),
a => ccx.sess().fatal(&format!("unrecognized arch \"{}\" in target specification", a)),
}
}
14 changes: 10 additions & 4 deletions src/librustc_trans/trans/cabi_aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ fn is_homogenous_aggregate_ty(ty: Type) -> Option<(Type, u64)> {

fn classify_ret_ty(ccx: &CrateContext, ty: Type) -> ArgType {
if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
return ArgType::direct(ty, None, None, attr);
if ty.kind() == Integer && ty.int_width() < 32 {
return ArgType::extend(ty);
} else {
return ArgType::direct(ty, None, None, None);
}
}
if let Some((base_ty, members)) = is_homogenous_aggregate_ty(ty) {
let llty = Type::array(&base_ty, members);
Expand All @@ -190,8 +193,11 @@ fn classify_ret_ty(ccx: &CrateContext, ty: Type) -> ArgType {

fn classify_arg_ty(ccx: &CrateContext, ty: Type) -> ArgType {
if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
return ArgType::direct(ty, None, None, attr);
if ty.kind() == Integer && ty.int_width() < 32 {
return ArgType::extend(ty);
} else {
return ArgType::direct(ty, None, None, None);
}
}
if let Some((base_ty, members)) = is_homogenous_aggregate_ty(ty) {
let llty = Type::array(&base_ty, members);
Expand Down
14 changes: 10 additions & 4 deletions src/librustc_trans/trans/cabi_arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ fn ty_size(ty: Type, align_fn: TyAlignFn) -> usize {

fn classify_ret_ty(ccx: &CrateContext, ty: Type, align_fn: TyAlignFn) -> ArgType {
if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
return ArgType::direct(ty, None, None, attr);
if ty.kind() == Integer && ty.int_width() < 32 {
return ArgType::extend(ty);
} else {
return ArgType::direct(ty, None, None, None);
}
}
let size = ty_size(ty, align_fn);
if size <= 4 {
Expand All @@ -150,8 +153,11 @@ fn classify_ret_ty(ccx: &CrateContext, ty: Type, align_fn: TyAlignFn) -> ArgType

fn classify_arg_ty(ccx: &CrateContext, ty: Type, align_fn: TyAlignFn) -> ArgType {
if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
return ArgType::direct(ty, None, None, attr);
if ty.kind() == Integer && ty.int_width() < 32 {
return ArgType::extend(ty);
} else {
return ArgType::direct(ty, None, None, None);
}
}
let align = align_fn(ty);
let size = ty_size(ty, align_fn);
Expand Down
16 changes: 11 additions & 5 deletions src/librustc_trans/trans/cabi_mips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ fn ty_size(ty: Type) -> usize {
}
}

fn classify_ret_ty(ccx: &CrateContext, ty: Type) -> ArgType {
fn classify_ret_ty(_ccx: &CrateContext, ty: Type) -> ArgType {
if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
ArgType::direct(ty, None, None, attr)
if ty.kind() == Integer && ty.int_width() < 32 {
ArgType::extend(ty)
} else {
ArgType::direct(ty, None, None, None)
}
} else {
ArgType::indirect(ty, Some(Attribute::StructRet))
}
Expand All @@ -105,8 +108,11 @@ fn classify_arg_ty(ccx: &CrateContext, ty: Type, offset: &mut usize) -> ArgType
*offset += align_up_to(size, align * 8) / 8;

if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
ArgType::direct(ty, None, None, attr)
if ty.kind() == Integer {
ArgType::extend(ty)
} else {
ArgType::direct(ty, None, None, None)
}
} else {
ArgType::direct(
ty,
Expand Down
16 changes: 11 additions & 5 deletions src/librustc_trans/trans/cabi_powerpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ fn ty_size(ty: Type) -> usize {
}
}

fn classify_ret_ty(ccx: &CrateContext, ty: Type) -> ArgType {
fn classify_ret_ty(_ccx: &CrateContext, ty: Type) -> ArgType {
if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
ArgType::direct(ty, None, None, attr)
if ty.kind() == Integer && ty.int_width() < 32 {
ArgType::extend(ty)
} else {
ArgType::direct(ty, None, None, None)
}
} else {
ArgType::indirect(ty, Some(Attribute::StructRet))
}
Expand All @@ -101,8 +104,11 @@ fn classify_arg_ty(ccx: &CrateContext, ty: Type, offset: &mut usize) -> ArgType
*offset += align_up_to(size, align * 8) / 8;

if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
ArgType::direct(ty, None, None, attr)
if ty.kind() == Integer && ty.int_width() < 32 {
ArgType::extend(ty)
} else {
ArgType::direct(ty, None, None, None)
}
} else {
ArgType::direct(
ty,
Expand Down
14 changes: 10 additions & 4 deletions src/librustc_trans/trans/cabi_powerpc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ fn is_homogenous_aggregate_ty(ty: Type) -> Option<(Type, u64)> {

fn classify_ret_ty(ccx: &CrateContext, ty: Type) -> ArgType {
if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
return ArgType::direct(ty, None, None, attr);
if ty.kind() == Integer && ty.int_width() < 32 {
return ArgType::extend(ty);
} else {
return ArgType::direct(ty, None, None, None);
}
}

// The PowerPC64 big endian ABI doesn't return aggregates in registers
Expand Down Expand Up @@ -187,8 +190,11 @@ fn classify_ret_ty(ccx: &CrateContext, ty: Type) -> ArgType {

fn classify_arg_ty(ccx: &CrateContext, ty: Type) -> ArgType {
if is_reg_ty(ty) {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
return ArgType::direct(ty, None, None, attr);
if ty.kind() == Integer && ty.int_width() < 32 {
return ArgType::extend(ty);
} else {
return ArgType::direct(ty, None, None, None);
}
}
if let Some((base_ty, members)) = is_homogenous_aggregate_ty(ty) {
let llty = Type::array(&base_ty, members);
Expand Down
14 changes: 10 additions & 4 deletions src/librustc_trans/trans/cabi_x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ pub fn compute_abi_info(ccx: &CrateContext,
}
}
} else {
let attr = if rty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
ret_ty = ArgType::direct(rty, None, None, attr);
ret_ty = if rty.kind() == Integer && rty.int_width() < 32 {
ArgType::extend(rty)
} else {
ArgType::direct(rty, None, None, None)
};
}

for &t in atys {
Expand All @@ -71,8 +74,11 @@ pub fn compute_abi_info(ccx: &CrateContext,
}
}
_ => {
let attr = if t == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
ArgType::direct(t, None, None, attr)
if t.kind() == Integer && t.int_width() < 32 {
ArgType::extend(t)
} else {
ArgType::direct(t, None, None, None)
}
}
};
arg_tys.push(ty);
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_trans/trans/cabi_x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,12 @@ pub fn compute_abi_info(ccx: &CrateContext,
None)
}
} else {
let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
ArgType::direct(ty, None, None, attr)
// Extend integer types that are < 32 bits wide
if ty.kind() == Integer && ty.int_width() < 32 {
ArgType::extend(ty)
} else {
ArgType::direct(ty, None, None, None)
}
}
}

Expand Down
Loading