Skip to content

Commit 967a228

Browse files
committed
Replace ZExt and SExt flags with ArgExtension enum
Both flags are mutually exclusive
1 parent 539402c commit 967a228

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl ArgAttributeExt for ArgAttribute {
3636
where
3737
F: FnMut(llvm::Attribute),
3838
{
39-
for_each_kind!(self, f, NoAlias, NoCapture, NonNull, ReadOnly, SExt, StructRet, ZExt, InReg)
39+
for_each_kind!(self, f, NoAlias, NoCapture, NonNull, ReadOnly, StructRet, InReg)
4040
}
4141
}
4242

@@ -65,6 +65,15 @@ impl ArgAttributesExt for ArgAttributes {
6565
llvm::LLVMRustAddByValAttr(llfn, idx.as_uint(), ty.unwrap());
6666
}
6767
regular.for_each_kind(|attr| attr.apply_llfn(idx, llfn));
68+
match self.arg_ext {
69+
ArgExtension::None => {}
70+
ArgExtension::Zext => {
71+
llvm::Attribute::ZExt.apply_llfn(idx, llfn);
72+
}
73+
ArgExtension::Sext => {
74+
llvm::Attribute::SExt.apply_llfn(idx, llfn);
75+
}
76+
}
6877
}
6978
}
7079

@@ -95,6 +104,15 @@ impl ArgAttributesExt for ArgAttributes {
95104
llvm::LLVMRustAddByValCallSiteAttr(callsite, idx.as_uint(), ty.unwrap());
96105
}
97106
regular.for_each_kind(|attr| attr.apply_callsite(idx, callsite));
107+
match self.arg_ext {
108+
ArgExtension::None => {}
109+
ArgExtension::Zext => {
110+
llvm::Attribute::ZExt.apply_callsite(idx, callsite);
111+
}
112+
ArgExtension::Sext => {
113+
llvm::Attribute::SExt.apply_callsite(idx, callsite);
114+
}
115+
}
98116
}
99117
}
100118
}

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2619,7 +2619,7 @@ where
26192619
is_return: bool| {
26202620
// Booleans are always an i1 that needs to be zero-extended.
26212621
if scalar.is_bool() {
2622-
attrs.set(ArgAttribute::ZExt);
2622+
attrs.zext();
26232623
return;
26242624
}
26252625

compiler/rustc_target/src/abi/call/mips64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::abi::call::{ArgAbi, ArgAttribute, CastTarget, FnAbi, PassMode, Reg, RegKind, Uniform};
1+
use crate::abi::call::{ArgAbi, CastTarget, FnAbi, PassMode, Reg, RegKind, Uniform};
22
use crate::abi::{self, HasDataLayout, LayoutOf, Size, TyAndLayout, TyAndLayoutMethods};
33

44
fn extend_integer_width_mips<Ty>(arg: &mut ArgAbi<'_, Ty>, bits: u64) {
@@ -7,7 +7,7 @@ fn extend_integer_width_mips<Ty>(arg: &mut ArgAbi<'_, Ty>, bits: u64) {
77
if let abi::Int(i, signed) = scalar.value {
88
if !signed && i.size().bits() == 32 {
99
if let PassMode::Direct(ref mut attrs) = arg.mode {
10-
attrs.set(ArgAttribute::SExt);
10+
attrs.sext();
1111
return;
1212
}
1313
}

compiler/rustc_target/src/abi/call/mod.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,28 @@ mod attr_impl {
5757
const NoCapture = 1 << 2;
5858
const NonNull = 1 << 3;
5959
const ReadOnly = 1 << 4;
60-
const SExt = 1 << 5;
6160
const StructRet = 1 << 6;
62-
const ZExt = 1 << 7;
6361
const InReg = 1 << 8;
6462
}
6563
}
6664
}
6765

66+
/// Sometimes an ABI requires small integers to be extended to a full or partial register. This enum
67+
/// defines if this extension should be zero-extension or sign-extension when necssary. When it is
68+
/// not necesary to extend the argument, this enum is ignored.
69+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
70+
pub enum ArgExtension {
71+
None,
72+
Zext,
73+
Sext,
74+
}
75+
6876
/// A compact representation of LLVM attributes (at least those relevant for this module)
6977
/// that can be manipulated without interacting with LLVM's Attribute machinery.
7078
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
7179
pub struct ArgAttributes {
7280
pub regular: ArgAttribute,
81+
pub arg_ext: ArgExtension,
7382
/// The minimum size of the pointee, guaranteed to be valid for the duration of the whole call
7483
/// (corresponding to LLVM's dereferenceable and dereferenceable_or_null attributes).
7584
pub pointee_size: Size,
@@ -80,11 +89,24 @@ impl ArgAttributes {
8089
pub fn new() -> Self {
8190
ArgAttributes {
8291
regular: ArgAttribute::default(),
92+
arg_ext: ArgExtension::None,
8393
pointee_size: Size::ZERO,
8494
pointee_align: None,
8595
}
8696
}
8797

98+
pub fn zext(&mut self) -> &mut Self {
99+
assert_ne!(self.arg_ext, ArgExtension::Sext);
100+
self.arg_ext = ArgExtension::Zext;
101+
self
102+
}
103+
104+
pub fn sext(&mut self) -> &mut Self {
105+
assert_ne!(self.arg_ext, ArgExtension::Zext);
106+
self.arg_ext = ArgExtension::Sext;
107+
self
108+
}
109+
88110
pub fn set(&mut self, attr: ArgAttribute) -> &mut Self {
89111
self.regular |= attr;
90112
self
@@ -457,7 +479,11 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
457479
if let abi::Int(i, signed) = scalar.value {
458480
if i.size().bits() < bits {
459481
if let PassMode::Direct(ref mut attrs) = self.mode {
460-
attrs.set(if signed { ArgAttribute::SExt } else { ArgAttribute::ZExt });
482+
if signed {
483+
attrs.sext()
484+
} else {
485+
attrs.zext()
486+
};
461487
}
462488
}
463489
}

compiler/rustc_target/src/abi/call/riscv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Reference: Clang RISC-V ELF psABI lowering code
55
// https://github.com/llvm/llvm-project/blob/8e780252a7284be45cf1ba224cabd884847e8e92/clang/lib/CodeGen/TargetInfo.cpp#L9311-L9773
66

7-
use crate::abi::call::{ArgAbi, ArgAttribute, CastTarget, FnAbi, PassMode, Reg, RegKind, Uniform};
7+
use crate::abi::call::{ArgAbi, CastTarget, FnAbi, PassMode, Reg, RegKind, Uniform};
88
use crate::abi::{
99
self, Abi, FieldsShape, HasDataLayout, LayoutOf, Size, TyAndLayout, TyAndLayoutMethods,
1010
};
@@ -308,7 +308,7 @@ fn extend_integer_width<'a, Ty>(arg: &mut ArgAbi<'a, Ty>, xlen: u64) {
308308
// 32-bit integers are always sign-extended
309309
if i.size().bits() == 32 && xlen > 32 {
310310
if let PassMode::Direct(ref mut attrs) = arg.mode {
311-
attrs.set(ArgAttribute::SExt);
311+
attrs.sext();
312312
return;
313313
}
314314
}

0 commit comments

Comments
 (0)