Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid bounds check when slicing from [0..] #1839

Open
andrewrk opened this issue Dec 17, 2018 · 1 comment
Open

avoid bounds check when slicing from [0..] #1839

andrewrk opened this issue Dec 17, 2018 · 1 comment
Milestone

Comments

@andrewrk
Copy link
Member

export fn entry() void {
    var z = []u32{1234};
    var y: []const u32 = z[0..];
    var x = @sliceToBytes(y);
}
define void @entry() #2 !dbg !41 {
Entry:
  %0 = alloca %"[]u32", align 8
  %1 = alloca %"[]u8", align 8
  %z = alloca [1 x i32], align 4
  %y = alloca %"[]u32", align 8
  %x = alloca %"[]u8", align 8
  %2 = bitcast [1 x i32]* %z to i8*, !dbg !60
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %2, i8* align 4 bitcast ([1 x i32]* @0 to i8*), i64 4, i1 false), !dbg !60
  call void @llvm.dbg.declare(metadata [1 x i32]* %z, metadata !45, metadata !DIExpression()), !dbg !60
  br i1 true, label %BoundsCheckOk, label %BoundsCheckFail, !dbg !61

BoundsCheckFail:                                  ; preds = %Entry
  tail call fastcc void @panic(%"[]u8"* @3, %StackTrace* null), !dbg !61
  unreachable, !dbg !61

BoundsCheckOk:                                    ; preds = %Entry
  %3 = getelementptr inbounds %"[]u32", %"[]u32"* %0, i32 0, i32 0, !dbg !61
  %4 = getelementptr inbounds [1 x i32], [1 x i32]* %z, i64 0, i64 0, !dbg !61
...

The bounds check can always be elided, even in debug mode, when slicing from 0 with inferred len.

@andrewrk andrewrk added optimization stage1 The process of building from source via WebAssembly and the C backend. labels Dec 17, 2018
@andrewrk andrewrk added this to the 1.1.0 milestone Dec 17, 2018
andrewrk added a commit that referenced this issue Dec 18, 2018
```zig
export fn entry() void {
    var z = []u32{1234};
    var y: []const u32 = z[0..];
    var x = @sliceToBytes(y);
}
```

don't worry about the bounds check, that's #1839

```llvm
define void @entry() #2 !dbg !41 {
Entry:
  %z = alloca [1 x i32], align 4
  %y = alloca %"[]u32", align 8
  %x = alloca %"[]u8", align 8
  %0 = bitcast [1 x i32]* %z to i8*, !dbg !60
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %0, i8* align 4 bitcast ([1 x i32]* @0 to i8*), i64 4, i1 false), !dbg !60
  call void @llvm.dbg.declare(metadata [1 x i32]* %z, metadata !45, metadata !DIExpression()), !dbg !60
  br i1 true, label %BoundsCheckOk, label %BoundsCheckFail, !dbg !61

BoundsCheckFail:                                  ; preds = %Entry
  tail call fastcc void @Panic(%"[]u8"* @3, %StackTrace* null), !dbg !61
  unreachable, !dbg !61

BoundsCheckOk:                                    ; preds = %Entry
  %1 = getelementptr inbounds %"[]u32", %"[]u32"* %y, i32 0, i32 0, !dbg !61
  %2 = getelementptr inbounds [1 x i32], [1 x i32]* %z, i64 0, i64 0, !dbg !61
  store i32* %2, i32** %1, align 8, !dbg !61
  %3 = getelementptr inbounds %"[]u32", %"[]u32"* %y, i32 0, i32 1, !dbg !61
  store i64 1, i64* %3, align 8, !dbg !61
  call void @llvm.dbg.declare(metadata %"[]u32"* %y, metadata !51, metadata !DIExpression()), !dbg !62
  %4 = bitcast %"[]u8"* %x to %"[]u32"*, !dbg !63
  %5 = bitcast %"[]u32"* %y to i8*, !dbg !64
  %6 = bitcast %"[]u32"* %4 to i8*, !dbg !64
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %6, i8* align 8 %5, i64 16, i1 false), !dbg !64
  call void @llvm.dbg.declare(metadata %"[]u8"* %x, metadata !58, metadata !DIExpression()), !dbg !65
  ret void, !dbg !66
}
```
@mikdusan
Copy link
Member

In the following use case (all factors must apply) no bounds checking is performed:

  1. must be slicing an array
  2. array must not be comptime known
  3. start-index must be comptime known
  4. end-index must be inferred
export fn entry() void {
    var a = [_]u32{ 1,2,3,4 };
    const slice = a[99..];
}

@Vexu Vexu removed the stage1 The process of building from source via WebAssembly and the C backend. label Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants