Skip to content

Commit bc074f1

Browse files
committed
PERF EXPERIMENT
1 parent f452f6d commit bc074f1

File tree

5 files changed

+32
-67
lines changed

5 files changed

+32
-67
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,11 +1056,10 @@ pub(super) fn transmute_scalar<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
10561056
to_scalar: abi::Scalar,
10571057
) -> Bx::Value {
10581058
assert_eq!(from_scalar.size(bx.cx()), to_scalar.size(bx.cx()));
1059-
let imm_ty = bx.cx().val_ty(imm);
1060-
assert_ne!(
1061-
bx.cx().type_kind(imm_ty),
1059+
debug_assert_ne!(
1060+
bx.cx().type_kind(bx.cx().val_ty(imm)),
10621061
TypeKind::Vector,
1063-
"Vector type {imm_ty:?} not allowed in transmute_scalar {from_scalar:?} -> {to_scalar:?}"
1062+
"Vector type {imm:?} not allowed in transmute_scalar {from_scalar:?} -> {to_scalar:?}"
10641063
);
10651064

10661065
// While optimizations will remove no-op transmutes, they might still be
@@ -1086,7 +1085,9 @@ pub(super) fn transmute_scalar<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
10861085
// That said, last time we tried removing this, it didn't actually help
10871086
// the rustc-perf results, so might as well keep doing it
10881087
// <https://github.com/rust-lang/rust/pull/135610#issuecomment-2599275182>
1089-
assume_scalar_range(bx, imm, from_scalar, from_backend_ty);
1088+
//
1089+
// EXPERIMENT: What if we skip it for perf?
1090+
// assume_scalar_range(bx, imm, from_scalar, from_backend_ty);
10901091

10911092
imm = match (from_scalar.primitive(), to_scalar.primitive()) {
10921093
(Int(..) | Float(_), Int(..) | Float(_)) => bx.bitcast(imm, to_backend_ty),
@@ -1109,12 +1110,15 @@ pub(super) fn transmute_scalar<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
11091110

11101111
debug_assert_eq!(bx.cx().val_ty(imm), to_backend_ty);
11111112

1112-
// This `assume` remains important for cases like (a conceptual)
1113-
// transmute::<u32, NonZeroU32>(x) == 0
1114-
// since it's never passed to something with parameter metadata (especially
1115-
// after MIR inlining) so the only way to tell the backend about the
1116-
// constraint that the `transmute` introduced is to `assume` it.
1117-
assume_scalar_range(bx, imm, to_scalar, to_backend_ty);
1113+
// For bool we don't need the assert since `to_immediate_scalar` covers it.
1114+
if !to_scalar.is_bool() {
1115+
// This `assume` remains important for cases like (a conceptual)
1116+
// transmute::<u32, NonZeroU32>(x) == 0
1117+
// since it's never passed to something with parameter metadata (especially
1118+
// after MIR inlining) so the only way to tell the backend about the
1119+
// constraint that the `transmute` introduced is to `assume` it.
1120+
assume_scalar_range(bx, imm, to_scalar, to_backend_ty);
1121+
}
11181122

11191123
imm = bx.to_immediate_scalar(imm, to_scalar);
11201124
imm

tests/codegen/array-cmp.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ pub fn array_of_tuple_le(a: &[(i16, u16); 2], b: &[(i16, u16); 2]) -> bool {
3939
// CHECK: %[[EQ00:.+]] = icmp eq i16 %[[A00]], %[[B00]]
4040
// CHECK-NEXT: br i1 %[[EQ00]], label %[[L01:.+]], label %[[EXIT_S:.+]]
4141

42-
// CHECK: [[EXIT_S]]:
43-
// CHECK: %[[RET_S:.+]] = icmp sle i16
44-
// CHECK: br label
45-
4642
// CHECK: [[L01]]:
4743
// CHECK: %[[PA01:.+]] = getelementptr{{.+}}i8, ptr %a, {{i32|i64}} 2
4844
// CHECK: %[[PB01:.+]] = getelementptr{{.+}}i8, ptr %b, {{i32|i64}} 2
@@ -70,12 +66,8 @@ pub fn array_of_tuple_le(a: &[(i16, u16); 2], b: &[(i16, u16); 2]) -> bool {
7066
// CHECK: %[[EQ11:.+]] = icmp eq i16 %[[A11]], %[[B11]]
7167
// CHECK-NEXT: br i1 %[[EQ11]], label %[[DONE:.+]], label %[[EXIT_U]]
7268

73-
// CHECK: [[EXIT_U]]:
74-
// CHECK: %[[RET_U:.+]] = icmp ule i16
75-
// CHECK: br label
76-
7769
// CHECK: [[DONE]]:
78-
// CHECK: %[[RET:.+]] = phi i1 [ true, %[[L11]] ], [ %[[RET_S]], %[[EXIT_S]] ], [ %[[RET_U]], %[[EXIT_U]] ]
70+
// CHECK: %[[RET:.+]] = phi i1 [ %{{.+}}, %[[EXIT_S]] ], [ %{{.+}}, %[[EXIT_U]] ], [ true, %[[L11]] ]
7971
// CHECK: ret i1 %[[RET]]
8072

8173
a <= b

tests/codegen/enum/enum-extract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn use_option_ordering(x: Option<Ordering>) -> Ordering {
6767

6868
// CHECK: [[BLOCK]]:
6969
// OPT: %[[SHIFTED:.+]] = sub i8 %x, -1
70-
// OPT: %[[IN_WIDTH:.+]] = icmp ule i8 %[[SHIFTED]], 3
70+
// OPT: %[[IN_WIDTH:.+]] = icmp ule i8 %[[SHIFTED]], 2
7171
// OPT: call void @llvm.assume(i1 %[[IN_WIDTH]])
7272
// DBG-NOT: assume
7373
// CHECK: ret i8 %x

tests/codegen/enum/enum-match.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ pub enum Enum0 {
1818
// CHECK-LABEL: define{{( dso_local)?}} noundef{{( range\(i8 [0-9]+, [0-9]+\))?}} i8 @match0(i8{{.+}}%e)
1919
// CHECK-NEXT: start:
2020
// CHECK-NEXT: %[[IS_B:.+]] = icmp eq i8 %e, 2
21-
// CHECK-NEXT: %[[R:.+]] = select i1 %[[IS_B]], i8 13, i8 %e
21+
// CHECK-NEXT: %[[TRUNC:.+]] = and i8 %e, 1
22+
// CHECK-NEXT: %[[R:.+]] = select i1 %[[IS_B]], i8 13, i8 %[[TRUNC]]
2223
// CHECK-NEXT: ret i8 %[[R]]
2324
#[no_mangle]
2425
pub fn match0(e: Enum0) -> u8 {

tests/codegen/intrinsics/transmute-niched.rs

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ pub unsafe fn check_to_enum(x: i8) -> SmallEnum {
3333
// CHECK-LABEL: @check_from_enum(
3434
#[no_mangle]
3535
pub unsafe fn check_from_enum(x: SmallEnum) -> i8 {
36-
// CHECK-NOT: icmp
37-
// CHECK-NOT: assume
38-
// OPT: %0 = sub i8 %x, 10
39-
// OPT: %1 = icmp ule i8 %0, 2
40-
// OPT: call void @llvm.assume(i1 %1)
4136
// CHECK-NOT: icmp
4237
// CHECK-NOT: assume
4338
// CHECK: ret i8 %x
@@ -63,11 +58,6 @@ pub unsafe fn check_to_ordering(x: u8) -> std::cmp::Ordering {
6358
// CHECK-LABEL: @check_from_ordering(
6459
#[no_mangle]
6560
pub unsafe fn check_from_ordering(x: std::cmp::Ordering) -> u8 {
66-
// CHECK-NOT: icmp
67-
// CHECK-NOT: assume
68-
// OPT: %0 = sub i8 %x, -1
69-
// OPT: %1 = icmp ule i8 %0, 2
70-
// OPT: call void @llvm.assume(i1 %1)
7161
// CHECK-NOT: icmp
7262
// CHECK-NOT: assume
7363
// CHECK: ret i8 %x
@@ -105,11 +95,9 @@ pub enum Minus100ToPlus100 {
10595
pub unsafe fn check_enum_from_char(x: char) -> Minus100ToPlus100 {
10696
// CHECK-NOT: icmp
10797
// CHECK-NOT: assume
108-
// OPT: %0 = icmp ule i32 %x, 1114111
109-
// OPT: call void @llvm.assume(i1 %0)
110-
// OPT: %1 = sub i32 %x, -100
111-
// OPT: %2 = icmp ule i32 %1, 200
112-
// OPT: call void @llvm.assume(i1 %2)
98+
// OPT: %0 = sub i32 %x, -100
99+
// OPT: %1 = icmp ule i32 %0, 200
100+
// OPT: call void @llvm.assume(i1 %1)
113101
// CHECK-NOT: icmp
114102
// CHECK-NOT: assume
115103
// CHECK: ret i32 %x
@@ -122,11 +110,8 @@ pub unsafe fn check_enum_from_char(x: char) -> Minus100ToPlus100 {
122110
pub unsafe fn check_enum_to_char(x: Minus100ToPlus100) -> char {
123111
// CHECK-NOT: icmp
124112
// CHECK-NOT: assume
125-
// OPT: %0 = sub i32 %x, -100
126-
// OPT: %1 = icmp ule i32 %0, 200
127-
// OPT: call void @llvm.assume(i1 %1)
128-
// OPT: %2 = icmp ule i32 %x, 1114111
129-
// OPT: call void @llvm.assume(i1 %2)
113+
// OPT: %0 = icmp ule i32 %x, 1114111
114+
// OPT: call void @llvm.assume(i1 %0)
130115
// CHECK-NOT: icmp
131116
// CHECK-NOT: assume
132117
// CHECK: ret i32 %x
@@ -139,16 +124,11 @@ pub unsafe fn check_enum_to_char(x: Minus100ToPlus100) -> char {
139124
pub unsafe fn check_swap_pair(x: (char, NonZero<u32>)) -> (NonZero<u32>, char) {
140125
// CHECK-NOT: icmp
141126
// CHECK-NOT: assume
142-
// OPT: %0 = icmp ule i32 %x.0, 1114111
143-
// OPT: call void @llvm.assume(i1 %0)
144-
// OPT: %1 = sub i32 %x.0, 1
145-
// OPT: %2 = icmp ule i32 %1, -2
127+
// OPT: %0 = sub i32 %x.0, 1
128+
// OPT: %1 = icmp ule i32 %0, -2
129+
// OPT: call void @llvm.assume(i1 %1)
130+
// OPT: %2 = icmp ule i32 %x.1, 1114111
146131
// OPT: call void @llvm.assume(i1 %2)
147-
// OPT: %3 = sub i32 %x.1, 1
148-
// OPT: %4 = icmp ule i32 %3, -2
149-
// OPT: call void @llvm.assume(i1 %4)
150-
// OPT: %5 = icmp ule i32 %x.1, 1114111
151-
// OPT: call void @llvm.assume(i1 %5)
152132
// CHECK-NOT: icmp
153133
// CHECK-NOT: assume
154134
// CHECK: %[[P1:.+]] = insertvalue { i32, i32 } poison, i32 %x.0, 0
@@ -161,13 +141,6 @@ pub unsafe fn check_swap_pair(x: (char, NonZero<u32>)) -> (NonZero<u32>, char) {
161141
// CHECK-LABEL: @check_bool_from_ordering(
162142
#[no_mangle]
163143
pub unsafe fn check_bool_from_ordering(x: std::cmp::Ordering) -> bool {
164-
// CHECK-NOT: icmp
165-
// CHECK-NOT: assume
166-
// OPT: %0 = sub i8 %x, -1
167-
// OPT: %1 = icmp ule i8 %0, 2
168-
// OPT: call void @llvm.assume(i1 %1)
169-
// OPT: %2 = icmp ule i8 %x, 1
170-
// OPT: call void @llvm.assume(i1 %2)
171144
// CHECK-NOT: icmp
172145
// CHECK-NOT: assume
173146
// CHECK: %[[R:.+]] = trunc{{( nuw)?}} i8 %x to i1
@@ -182,11 +155,9 @@ pub unsafe fn check_bool_to_ordering(x: bool) -> std::cmp::Ordering {
182155
// CHECK: %_0 = zext i1 %x to i8
183156
// CHECK-NOT: icmp
184157
// CHECK-NOT: assume
185-
// OPT: %0 = icmp ule i8 %_0, 1
186-
// OPT: call void @llvm.assume(i1 %0)
187-
// OPT: %1 = sub i8 %_0, -1
188-
// OPT: %2 = icmp ule i8 %1, 2
189-
// OPT: call void @llvm.assume(i1 %2)
158+
// OPT: %0 = sub i8 %_0, -1
159+
// OPT: %1 = icmp ule i8 %0, 2
160+
// OPT: call void @llvm.assume(i1 %1)
190161
// CHECK-NOT: icmp
191162
// CHECK-NOT: assume
192163
// CHECK: ret i8 %_0
@@ -197,10 +168,7 @@ pub unsafe fn check_bool_to_ordering(x: bool) -> std::cmp::Ordering {
197168
// CHECK-LABEL: @check_nonnull_to_ptr(
198169
#[no_mangle]
199170
pub unsafe fn check_nonnull_to_ptr(x: NonNull<u8>) -> *const u8 {
200-
// CHECK-NOT: icmp
201-
// CHECK-NOT: assume
202-
// OPT: %0 = icmp ne ptr %x, null
203-
// OPT: call void @llvm.assume(i1 %0)
171+
// CHECK: start
204172
// CHECK-NOT: icmp
205173
// CHECK-NOT: assume
206174
// CHECK: ret ptr %x

0 commit comments

Comments
 (0)