Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5f3b84a

Browse files
committedMar 16, 2025
Auto merge of rust-lang#137278 - heiseish:101210-extra-codegen-tests, r=scottmcm
added some new test to check for result and options opt Apologies for the delay. Finally have some time to get back into contributing. ## Context - Added some tests to show optimization on result and options for 64 and 128 bits - Relevant issue rust-lang#101210 ## Some newb questions from me - [x] My local llvm IR has `nuw` in `result_nop_match_128` etc whereas [godbolt version](https://rust.godbolt.org/z/Td9zoT5zn) doesn't have. So I put optional there, but not sure if it's desirable (maybe I'm not using the compiled llvm in the repo). I ran the test with ```bash ./x test tests/codegen/try_question_mark_nop.rs ``` - [x] Unless I'm reading it wrongly, but `option_nop_match_128` and `option_nop_traits_128` look to be **not** optimized away? Update: Here's the test for future reference ```rust // CHECK-LABEL: `@option_nop_match_128` #[no_mangle] pub fn option_nop_match_128(x: Option<i128>) -> Option<i128> { // CHECK: start: // CHECK-NEXT: %trunc = trunc nuw i128 %0 to i1 // CHECK-NEXT: br i1 %trunc, label %bb3, label %bb4 // CHECK: bb3: // CHECK-NEXT: %2 = getelementptr inbounds {{(nuw )?}}i8, ptr %_0, i64 16 // CHECK-NEXT: store i128 %1, ptr %2, align 16 // CHECK: bb4: // CHECK-NEXT: %storemerge = phi i128 [ 1, %bb3 ], [ 0, %start ] // CHECK-NEXT: store i128 %storemerge, ptr %_0, align 16 // CHECK-NEXT: ret void match x { Some(x) => Some(x), None => None, } } ``` r? `@scottmcm`
2 parents 66678e6 + 0fba203 commit 5f3b84a

File tree

1 file changed

+121
-20
lines changed

1 file changed

+121
-20
lines changed
 

‎tests/codegen/try_question_mark_nop.rs

+121-20
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,57 @@ pub fn result_nop_traits_32(x: Result<i32, u32>) -> Result<i32, u32> {
6363
try { x? }
6464
}
6565

66+
// CHECK-LABEL: @control_flow_nop_match_32
67+
#[no_mangle]
68+
pub fn control_flow_nop_match_32(x: ControlFlow<i32, u32>) -> ControlFlow<i32, u32> {
69+
// CHECK: start:
70+
// CHECK-NEXT: insertvalue { i32, i32 }
71+
// CHECK-NEXT: insertvalue { i32, i32 }
72+
// CHECK-NEXT: ret { i32, i32 }
73+
match x {
74+
Continue(x) => Continue(x),
75+
Break(x) => Break(x),
76+
}
77+
}
78+
79+
// CHECK-LABEL: @control_flow_nop_traits_32
80+
#[no_mangle]
81+
pub fn control_flow_nop_traits_32(x: ControlFlow<i32, u32>) -> ControlFlow<i32, u32> {
82+
// CHECK: start:
83+
// CHECK-NEXT: insertvalue { i32, i32 }
84+
// CHECK-NEXT: insertvalue { i32, i32 }
85+
// CHECK-NEXT: ret { i32, i32 }
86+
try { x? }
87+
}
88+
89+
// CHECK-LABEL: @option_nop_match_64
90+
#[no_mangle]
91+
pub fn option_nop_match_64(x: Option<u64>) -> Option<u64> {
92+
// CHECK: start:
93+
// TWENTY-NEXT: %[[TRUNC:[0-9]+]] = trunc nuw i64 %0 to i1
94+
// TWENTY-NEXT: %[[SEL:\.[0-9]+]] = select i1 %[[TRUNC]], i64 %1, i64 undef
95+
// CHECK-NEXT: [[REG1:%[0-9a-zA-Z_.]+]] = insertvalue { i64, i64 } poison, i64 %0, 0
96+
// NINETEEN-NEXT: [[REG2:%[0-9a-zA-Z_.]+]] = insertvalue { i64, i64 } [[REG1]], i64 %1, 1
97+
// TWENTY-NEXT: [[REG2:%[0-9a-zA-Z_.]+]] = insertvalue { i64, i64 } [[REG1]], i64 %[[SEL]], 1
98+
// CHECK-NEXT: ret { i64, i64 } [[REG2]]
99+
match x {
100+
Some(x) => Some(x),
101+
None => None,
102+
}
103+
}
104+
105+
// CHECK-LABEL: @option_nop_traits_64
106+
#[no_mangle]
107+
pub fn option_nop_traits_64(x: Option<u64>) -> Option<u64> {
108+
// CHECK: start:
109+
// TWENTY-NEXT: %[[TRUNC:[0-9]+]] = trunc nuw i64 %0 to i1
110+
// TWENTY-NEXT: %[[SEL:\.[0-9]+]] = select i1 %[[TRUNC]], i64 %1, i64 undef
111+
// CHECK-NEXT: insertvalue { i64, i64 }
112+
// CHECK-NEXT: insertvalue { i64, i64 }
113+
// CHECK-NEXT: ret { i64, i64 }
114+
try { x? }
115+
}
116+
66117
// CHECK-LABEL: @result_nop_match_64
67118
#[no_mangle]
68119
pub fn result_nop_match_64(x: Result<i64, u64>) -> Result<i64, u64> {
@@ -86,48 +137,98 @@ pub fn result_nop_traits_64(x: Result<i64, u64>) -> Result<i64, u64> {
86137
try { x? }
87138
}
88139

89-
// CHECK-LABEL: @result_nop_match_ptr
140+
// CHECK-LABEL: @control_flow_nop_match_64
90141
#[no_mangle]
91-
pub fn result_nop_match_ptr(x: Result<usize, Box<()>>) -> Result<usize, Box<()>> {
142+
pub fn control_flow_nop_match_64(x: ControlFlow<i64, u64>) -> ControlFlow<i64, u64> {
92143
// CHECK: start:
93-
// CHECK-NEXT: insertvalue { i{{[0-9]+}}, ptr }
94-
// CHECK-NEXT: insertvalue { i{{[0-9]+}}, ptr }
95-
// CHECK-NEXT: ret
144+
// CHECK-NEXT: insertvalue { i64, i64 }
145+
// CHECK-NEXT: insertvalue { i64, i64 }
146+
// CHECK-NEXT: ret { i64, i64 }
147+
match x {
148+
Continue(x) => Continue(x),
149+
Break(x) => Break(x),
150+
}
151+
}
152+
153+
// CHECK-LABEL: @control_flow_nop_traits_64
154+
#[no_mangle]
155+
pub fn control_flow_nop_traits_64(x: ControlFlow<i64, u64>) -> ControlFlow<i64, u64> {
156+
// CHECK: start:
157+
// CHECK-NEXT: insertvalue { i64, i64 }
158+
// CHECK-NEXT: insertvalue { i64, i64 }
159+
// CHECK-NEXT: ret { i64, i64 }
160+
try { x? }
161+
}
162+
163+
// CHECK-LABEL: @result_nop_match_128
164+
#[no_mangle]
165+
pub fn result_nop_match_128(x: Result<i128, u128>) -> Result<i128, u128> {
166+
// CHECK: start:
167+
// CHECK-NEXT: getelementptr inbounds {{(nuw )?}}i8
168+
// CHECK-NEXT: store i128
169+
// CHECK-NEXT: store i128
170+
// CHECK-NEXT: ret void
96171
match x {
97172
Ok(x) => Ok(x),
98173
Err(x) => Err(x),
99174
}
100175
}
101176

102-
// CHECK-LABEL: @result_nop_traits_ptr
177+
// CHECK-LABEL: @result_nop_traits_128
103178
#[no_mangle]
104-
pub fn result_nop_traits_ptr(x: Result<u64, NonNull<()>>) -> Result<u64, NonNull<()>> {
179+
pub fn result_nop_traits_128(x: Result<i128, u128>) -> Result<i128, u128> {
105180
// CHECK: start:
106-
// CHECK-NEXT: insertvalue { i{{[0-9]+}}, ptr }
107-
// CHECK-NEXT: insertvalue { i{{[0-9]+}}, ptr }
108-
// CHECK-NEXT: ret
181+
// CHECK-NEXT: getelementptr inbounds {{(nuw )?}}i8
182+
// CHECK-NEXT: store i128
183+
// CHECK-NEXT: store i128
184+
// CHECK-NEXT: ret void
109185
try { x? }
110186
}
111187

112-
// CHECK-LABEL: @control_flow_nop_match_32
188+
// CHECK-LABEL: @control_flow_nop_match_128
113189
#[no_mangle]
114-
pub fn control_flow_nop_match_32(x: ControlFlow<i32, u32>) -> ControlFlow<i32, u32> {
190+
pub fn control_flow_nop_match_128(x: ControlFlow<i128, u128>) -> ControlFlow<i128, u128> {
115191
// CHECK: start:
116-
// CHECK-NEXT: insertvalue { i32, i32 }
117-
// CHECK-NEXT: insertvalue { i32, i32 }
118-
// CHECK-NEXT: ret { i32, i32 }
192+
// CHECK-NEXT: getelementptr inbounds {{(nuw )?}}i8
193+
// CHECK-NEXT: store i128
194+
// CHECK-NEXT: store i128
195+
// CHECK-NEXT: ret void
119196
match x {
120197
Continue(x) => Continue(x),
121198
Break(x) => Break(x),
122199
}
123200
}
124201

125-
// CHECK-LABEL: @control_flow_nop_traits_32
202+
// CHECK-LABEL: @control_flow_nop_traits_128
126203
#[no_mangle]
127-
pub fn control_flow_nop_traits_32(x: ControlFlow<i32, u32>) -> ControlFlow<i32, u32> {
204+
pub fn control_flow_nop_traits_128(x: ControlFlow<i128, u128>) -> ControlFlow<i128, u128> {
128205
// CHECK: start:
129-
// CHECK-NEXT: insertvalue { i32, i32 }
130-
// CHECK-NEXT: insertvalue { i32, i32 }
131-
// CHECK-NEXT: ret { i32, i32 }
206+
// CHECK-NEXT: getelementptr inbounds {{(nuw )?}}i8
207+
// CHECK-NEXT: store i128
208+
// CHECK-NEXT: store i128
209+
// CHECK-NEXT: ret void
210+
try { x? }
211+
}
212+
213+
// CHECK-LABEL: @result_nop_match_ptr
214+
#[no_mangle]
215+
pub fn result_nop_match_ptr(x: Result<usize, Box<()>>) -> Result<usize, Box<()>> {
216+
// CHECK: start:
217+
// CHECK-NEXT: insertvalue { i{{[0-9]+}}, ptr }
218+
// CHECK-NEXT: insertvalue { i{{[0-9]+}}, ptr }
219+
// CHECK-NEXT: ret
220+
match x {
221+
Ok(x) => Ok(x),
222+
Err(x) => Err(x),
223+
}
224+
}
225+
226+
// CHECK-LABEL: @result_nop_traits_ptr
227+
#[no_mangle]
228+
pub fn result_nop_traits_ptr(x: Result<u64, NonNull<()>>) -> Result<u64, NonNull<()>> {
229+
// CHECK: start:
230+
// CHECK-NEXT: insertvalue { i{{[0-9]+}}, ptr }
231+
// CHECK-NEXT: insertvalue { i{{[0-9]+}}, ptr }
232+
// CHECK-NEXT: ret
132233
try { x? }
133234
}

0 commit comments

Comments
 (0)
Failed to load comments.