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

Sema: missed simplifications of OPVs #22419

Open
jacobly0 opened this issue Jan 5, 2025 · 0 comments
Open

Sema: missed simplifications of OPVs #22419

jacobly0 opened this issue Jan 5, 2025 · 0 comments
Labels
frontend Tokenization, parsing, AstGen, Sema, and Liveness. optimization

Comments

@jacobly0
Copy link
Member

jacobly0 commented Jan 5, 2025

const std = @import("std");
pub const Panic = struct {
    pub fn call(_: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
        @trap();
    }
    pub fn sentinelMismatch(expected: anytype, _: @TypeOf(expected)) noreturn {
        @trap();
    }
    pub fn unwrapError(_: ?*std.builtin.StackTrace, _: anyerror) noreturn {
        @trap();
    }
    pub fn outOfBounds(_: usize, _: usize) noreturn {
        @trap();
    }
    pub fn startGreaterThanEnd(_: usize, _: usize) noreturn {
        @trap();
    }
    pub fn inactiveUnionField(active: anytype, _: @TypeOf(active)) noreturn {
        @trap();
    }
    pub const messages = std.debug.FormattedPanic.messages;
};
fn ptrAddr(ptr: [*]const void, index: usize) *const void {
    return &ptr[index];
}
fn ptrAccess(ptr: [*]const void, index: usize) void {
    return ptr[index];
}
fn sliceAddr(slice: []const void, index: usize) *const void {
    return &slice[index];
}
fn sliceAccess(slice: []const void, index: usize) void {
    return slice[index];
}
fn unionInit(val: u8) union(enum) { val: u8 } {
    return .{ .val = val };
}
fn unionAccess(val: union(enum(u1)) { only }) void {
    return val.only;
}
comptime {
    _ = .{ &ptrAddr, &ptrAccess, &sliceAddr, &sliceAccess, &unionInit, &unionAccess };
}
$ zig build-obj repro.zig --verbose-air -fstrip
# Begin Function AIR: repro.ptrAddr:
# Total AIR+Liveness bytes: 232B
# AIR Instructions:         8 (72B)
# AIR Extra Data:           12 (48B)
# Liveness tomb_bits:       8B
# Liveness Extra Data:      0 (0B)
# Liveness special table:   0 (0B)
  %0 = arg([*]const void)
  %1 = arg(usize)
  %2 = alloc(*[*]const void)
  %3!= store_safe(%2, %0!)
  %4 = bitcast(*const [*]const void, %2!)
  %5 = load([*]const void, %4!)
  %6 = ptr_elem_ptr(*const void, %5!, %1!)
  %7!= ret_safe(%6!)
# End Function AIR: repro.ptrAddr

# Begin Function AIR: repro.ptrAccess:
# Total AIR+Liveness bytes: 172B
# AIR Instructions:         4 (36B)
# AIR Extra Data:           6 (24B)
# Liveness tomb_bits:       8B
# Liveness Extra Data:      0 (0B)
# Liveness special table:   0 (0B)
  %0 = arg([*]const void)
  %1 = arg(usize)
  %2 = ptr_elem_val(%0!, %1!)
  %3!= ret_safe(%2!)
# End Function AIR: repro.ptrAccess

# Begin Function AIR: repro.sliceAddr:
# Total AIR+Liveness bytes: 395B
# AIR Instructions:         15 (135B)
# AIR Extra Data:           26 (104B)
# Liveness tomb_bits:       8B
# Liveness Extra Data:      7 (28B)
# Liveness special table:   2 (16B)
  %0 = arg([]const void)
  %1 = arg(usize)
  %2 = alloc(*[]const void)
  %3!= store_safe(%2, %0!)
  %4 = bitcast(*const []const void, %2!)
  %5 = load([]const void, %4!)
  %6 = slice_len(usize, %5)
  %7 = cmp_lt(%1, %6)
  %10!= block(void, {
    %11!= cond_br(%7!, likely {
      %6!
      %12!= br(%10, @Air.Inst.Ref.void_value)
    }, cold {
      %5!
      %8!= call(<fn (usize, usize) noreturn, (function 'outOfBounds')>, [%1!, %6!])
      %9!= unreach()
    })
  } %7! %6!)
  %13 = slice_elem_ptr(*const void, %5!, %1!)
  %14!= ret_safe(%13!)
# End Function AIR: repro.sliceAddr

# Begin Function AIR: repro.sliceAccess:
# Total AIR+Liveness bytes: 335B
# AIR Instructions:         11 (99B)
# AIR Extra Data:           20 (80B)
# Liveness tomb_bits:       8B
# Liveness Extra Data:      7 (28B)
# Liveness special table:   2 (16B)
  %0 = arg([]const void)
  %1 = arg(usize)
  %2 = slice_len(usize, %0)
  %3 = cmp_lt(%1, %2)
  %6!= block(void, {
    %7!= cond_br(%3!, likely {
      %2!
      %8!= br(%6, @Air.Inst.Ref.void_value)
    }, cold {
      %0!
      %4!= call(<fn (usize, usize) noreturn, (function 'outOfBounds')>, [%1!, %2!])
      %5!= unreach()
    })
  } %2! %3!)
  %9 = slice_elem_val(%0!, %1!)
  %10!= ret_safe(%9!)
# End Function AIR: repro.sliceAccess

# Begin Function AIR: repro.unionInit:
# Total AIR+Liveness bytes: 198B
# AIR Instructions:         6 (54B)
# AIR Extra Data:           8 (32B)
# Liveness tomb_bits:       8B
# Liveness Extra Data:      0 (0B)
# Liveness special table:   0 (0B)
  %0 = arg(u8)
  %1 = ret_ptr(*repro.unionInit__union_175)
  %2 = struct_field_ptr_index_0(*u8, %1)
  %3!= store_safe(%2!, %0!)
  %4!= set_union_tag(%1, <@typeInfo(repro.unionInit__union_175).@"union".tag_type.?, .val>)
  %5!= ret_load(%1!)
# End Function AIR: repro.unionInit

# Begin Function AIR: repro.unionAccess:
# Total AIR+Liveness bytes: 167B
# AIR Instructions:         3 (27B)
# AIR Extra Data:           7 (28B)
# Liveness tomb_bits:       8B
# Liveness Extra Data:      0 (0B)
# Liveness special table:   0 (0B)
  %0 = arg(repro.unionAccess__union_181)
  %1 = struct_field_val(%0!, 0)
  %2!= ret_safe(%1!)
# End Function AIR: repro.unionAccess
@jacobly0 jacobly0 added optimization frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend Tokenization, parsing, AstGen, Sema, and Liveness. optimization
Projects
None yet
Development

No branches or pull requests

1 participant