Skip to content

Commit dfd6306

Browse files
committed
Auto merge of #88326 - eddyb:inline-ty-layout-methods, r=oli-obk
`#[inline]` non-generic `pub fn`s in `rustc_target::abi` and `ty::layout`. Mostly doing this as a perf curiosity, having spotted that `#[inline]` usage is a bit spotty.
2 parents 720a1b2 + efb4148 commit dfd6306

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

compiler/rustc_middle/src/ty/layout.rs

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub trait IntegerExt {
4242
}
4343

4444
impl IntegerExt for Integer {
45+
#[inline]
4546
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>, signed: bool) -> Ty<'tcx> {
4647
match (*self, signed) {
4748
(I8, false) => tcx.types.u8,
@@ -149,6 +150,7 @@ pub trait PrimitiveExt {
149150
}
150151

151152
impl PrimitiveExt for Primitive {
153+
#[inline]
152154
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
153155
match *self {
154156
Int(i, signed) => i.to_ty(tcx, signed),
@@ -160,6 +162,7 @@ impl PrimitiveExt for Primitive {
160162

161163
/// Return an *integer* type matching this primitive.
162164
/// Useful in particular when dealing with enum discriminants.
165+
#[inline]
163166
fn to_int_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
164167
match *self {
165168
Int(i, signed) => i.to_ty(tcx, signed),
@@ -2018,12 +2021,14 @@ pub trait HasParamEnv<'tcx> {
20182021
}
20192022

20202023
impl<'tcx> HasDataLayout for TyCtxt<'tcx> {
2024+
#[inline]
20212025
fn data_layout(&self) -> &TargetDataLayout {
20222026
&self.data_layout
20232027
}
20242028
}
20252029

20262030
impl<'tcx> HasTyCtxt<'tcx> for TyCtxt<'tcx> {
2031+
#[inline]
20272032
fn tcx(&self) -> TyCtxt<'tcx> {
20282033
*self
20292034
}
@@ -2055,6 +2060,7 @@ impl<'tcx> LayoutOf for LayoutCx<'tcx, TyCtxt<'tcx>> {
20552060

20562061
/// Computes the layout of a type. Note that this implicitly
20572062
/// executes in "reveal all" mode, and will normalize the input type.
2063+
#[inline]
20582064
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
20592065
self.tcx.layout_of(self.param_env.and(ty))
20602066
}
@@ -2066,6 +2072,7 @@ impl LayoutOf for LayoutCx<'tcx, ty::query::TyCtxtAt<'tcx>> {
20662072

20672073
/// Computes the layout of a type. Note that this implicitly
20682074
/// executes in "reveal all" mode, and will normalize the input type.
2075+
#[inline]
20692076
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
20702077
self.tcx.layout_of(self.param_env.and(ty))
20712078
}
@@ -2416,6 +2423,7 @@ where
24162423
}
24172424

24182425
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for LayoutError<'tcx> {
2426+
#[inline]
24192427
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
24202428
use crate::ty::layout::LayoutError::*;
24212429
mem::discriminant(self).hash_stable(hcx, hasher);
@@ -2606,6 +2614,7 @@ where
26062614
/// compiled with `-Cpanic=unwind` and referenced from another crate compiled
26072615
/// with `-Cpanic=abort` will look like they can't unwind when in fact they
26082616
/// might (from a foreign exception or similar).
2617+
#[inline]
26092618
pub fn fn_can_unwind(
26102619
tcx: TyCtxt<'tcx>,
26112620
codegen_fn_attr_flags: CodegenFnAttrFlags,
@@ -2681,6 +2690,7 @@ pub fn fn_can_unwind(
26812690
}
26822691
}
26832692

2693+
#[inline]
26842694
pub fn conv_from_spec_abi(tcx: TyCtxt<'_>, abi: SpecAbi) -> Conv {
26852695
use rustc_target::spec::abi::Abi::*;
26862696
match tcx.sess.target.adjust_abi(abi) {

compiler/rustc_target/src/abi/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ impl TargetDataLayout {
194194
/// to represent object size in bits. It would need to be 1 << 61 to account for this, but is
195195
/// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
196196
/// address space on 64-bit ARMv8 and x86_64.
197+
#[inline]
197198
pub fn obj_size_bound(&self) -> u64 {
198199
match self.pointer_size.bits() {
199200
16 => 1 << 15,
@@ -203,6 +204,7 @@ impl TargetDataLayout {
203204
}
204205
}
205206

207+
#[inline]
206208
pub fn ptr_sized_integer(&self) -> Integer {
207209
match self.pointer_size.bits() {
208210
16 => I16,
@@ -212,6 +214,7 @@ impl TargetDataLayout {
212214
}
213215
}
214216

217+
#[inline]
215218
pub fn vector_align(&self, vec_size: Size) -> AbiAndPrefAlign {
216219
for &(size, align) in &self.vector_align {
217220
if size == vec_size {
@@ -562,14 +565,17 @@ pub struct AbiAndPrefAlign {
562565
}
563566

564567
impl AbiAndPrefAlign {
568+
#[inline]
565569
pub fn new(align: Align) -> AbiAndPrefAlign {
566570
AbiAndPrefAlign { abi: align, pref: align }
567571
}
568572

573+
#[inline]
569574
pub fn min(self, other: AbiAndPrefAlign) -> AbiAndPrefAlign {
570575
AbiAndPrefAlign { abi: self.abi.min(other.abi), pref: self.pref.min(other.pref) }
571576
}
572577

578+
#[inline]
573579
pub fn max(self, other: AbiAndPrefAlign) -> AbiAndPrefAlign {
574580
AbiAndPrefAlign { abi: self.abi.max(other.abi), pref: self.pref.max(other.pref) }
575581
}
@@ -586,6 +592,7 @@ pub enum Integer {
586592
}
587593

588594
impl Integer {
595+
#[inline]
589596
pub fn size(self) -> Size {
590597
match self {
591598
I8 => Size::from_bytes(1),
@@ -609,6 +616,7 @@ impl Integer {
609616
}
610617

611618
/// Finds the smallest Integer type which can represent the signed value.
619+
#[inline]
612620
pub fn fit_signed(x: i128) -> Integer {
613621
match x {
614622
-0x0000_0000_0000_0080..=0x0000_0000_0000_007f => I8,
@@ -620,6 +628,7 @@ impl Integer {
620628
}
621629

622630
/// Finds the smallest Integer type which can represent the unsigned value.
631+
#[inline]
623632
pub fn fit_unsigned(x: u128) -> Integer {
624633
match x {
625634
0..=0x0000_0000_0000_00ff => I8,
@@ -655,6 +664,9 @@ impl Integer {
655664
I8
656665
}
657666

667+
// FIXME(eddyb) consolidate this and other methods that find the appropriate
668+
// `Integer` given some requirements.
669+
#[inline]
658670
fn from_size(size: Size) -> Result<Self, String> {
659671
match size.bits() {
660672
8 => Ok(Integer::I8),
@@ -706,10 +718,14 @@ impl Primitive {
706718
}
707719
}
708720

721+
// FIXME(eddyb) remove, it's trivial thanks to `matches!`.
722+
#[inline]
709723
pub fn is_float(self) -> bool {
710724
matches!(self, F32 | F64)
711725
}
712726

727+
// FIXME(eddyb) remove, it's completely unused.
728+
#[inline]
713729
pub fn is_int(self) -> bool {
714730
matches!(self, Int(..))
715731
}
@@ -786,6 +802,7 @@ pub struct Scalar {
786802
}
787803

788804
impl Scalar {
805+
#[inline]
789806
pub fn is_bool(&self) -> bool {
790807
matches!(self.value, Int(I8, false))
791808
&& matches!(self.valid_range, WrappingRange { start: 0, end: 1 })
@@ -852,6 +869,7 @@ pub enum FieldsShape {
852869
}
853870

854871
impl FieldsShape {
872+
#[inline]
855873
pub fn count(&self) -> usize {
856874
match *self {
857875
FieldsShape::Primitive => 0,
@@ -861,6 +879,7 @@ impl FieldsShape {
861879
}
862880
}
863881

882+
#[inline]
864883
pub fn offset(&self, i: usize) -> Size {
865884
match *self {
866885
FieldsShape::Primitive => {
@@ -884,6 +903,7 @@ impl FieldsShape {
884903
}
885904
}
886905

906+
#[inline]
887907
pub fn memory_index(&self, i: usize) -> usize {
888908
match *self {
889909
FieldsShape::Primitive => {
@@ -967,6 +987,7 @@ impl Abi {
967987
}
968988

969989
/// Returns `true` if this is a single signed integer scalar
990+
#[inline]
970991
pub fn is_signed(&self) -> bool {
971992
match *self {
972993
Abi::Scalar(ref scal) => match scal.value {

0 commit comments

Comments
 (0)