Skip to content

Commit 3fc506b

Browse files
committed
Simplify the GEP instruction for index
1 parent 4790a43 commit 3fc506b

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
422422
layout.size
423423
};
424424

425-
let llval = bx.inbounds_gep(bx.cx().backend_type(self.layout), self.val.llval, &[
426-
bx.cx().const_usize(0),
427-
llindex,
428-
]);
425+
let llval = bx.inbounds_gep(bx.cx().backend_type(layout), self.val.llval, &[llindex]);
429426
let align = self.val.align.restrict_for_offset(offset);
430427
PlaceValue::new_sized(llval, align).with_type(layout)
431428
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Issue: <https://github.com/rust-lang/rust/issues/133979>
2+
//! Check that bounds checking are eliminated.
3+
4+
//@ compile-flags: -Copt-level=2
5+
6+
#![crate_type = "lib"]
7+
8+
// CHECK-LABEL: @test(
9+
#[no_mangle]
10+
fn test(a: &[&[u8]]) -> u32 {
11+
// CHECK-NOT: panic_bounds_check
12+
a.iter()
13+
.enumerate()
14+
.map(|(y, b)| {
15+
b.iter()
16+
.enumerate()
17+
.filter(|(_, c)| **c == b'A')
18+
.map(|(x, _)| a[y][x] as u32)
19+
.sum::<u32>()
20+
})
21+
.sum()
22+
}

tests/codegen/gep-index.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! Check that index and offset use the same getelementptr format.
2+
3+
//@ revisions: NO-OPT OPT
4+
//@[NO-OPT] compile-flags: -Copt-level=0
5+
//@[OPT] compile-flags: -Copt-level=1
6+
7+
#![crate_type = "lib"]
8+
9+
struct Foo(i32, i32);
10+
11+
// CHECK-LABEL: @index_on_struct(
12+
#[no_mangle]
13+
fn index_on_struct(a: &[Foo], index: usize) -> &Foo {
14+
// CHECK: getelementptr inbounds %Foo, ptr %a.0, {{i64|i32}} %index
15+
&a[index]
16+
}
17+
18+
// CHECK-LABEL: @offset_on_struct(
19+
#[no_mangle]
20+
fn offset_on_struct(a: *const Foo, index: usize) -> *const Foo {
21+
// CHECK: getelementptr inbounds %Foo, ptr %a, {{i64|i32}} %index
22+
unsafe { a.add(index) }
23+
}
24+
25+
// CHECK-LABEL: @index_on_i32(
26+
#[no_mangle]
27+
fn index_on_i32(a: &[i32], index: usize) -> &i32 {
28+
// CHECK: getelementptr inbounds i32, ptr %a.0, {{i64|i32}} %index
29+
&a[index]
30+
}
31+
32+
// CHECK-LABEL: @offset_on_i32(
33+
#[no_mangle]
34+
fn offset_on_i32(a: *const i32, index: usize) -> *const i32 {
35+
// CHECK: getelementptr inbounds i32, ptr %a, {{i64|i32}} %index
36+
unsafe { a.add(index) }
37+
}

0 commit comments

Comments
 (0)