Skip to content

Sema: remove incorrect safety check for saturating left shift #24411

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

Merged
merged 1 commit into from
Jul 30, 2025

Conversation

Justus2308
Copy link
Contributor

@Justus2308 Justus2308 commented Jul 11, 2025

Closes #23033

Added a check for .shl_sat before emitting an overflow safety check and reenabled some tests.

pub const panic = @import("std").debug.no_panic;
var lhs: u3 = 1;
var rhs: u1 = 0;
export fn shlSat() void {
    lhs <<|= rhs;
}
zig build-obj repro.zig --verbose-air
old
thread 172206856 panic: reached unreachable code
# Begin Function AIR: repro.shlSat:
# Total AIR+Liveness bytes: 469B
# AIR Instructions:         21 (189B)
# AIR Extra Data:           28 (112B)
# Liveness tomb_bits:       16B
# Liveness Extra Data:      6 (24B)
# Liveness special table:   2 (16B)
  %13 = alloc(*[32]usize)
  %14 = alloc(*builtin.StackTrace)
  %15 = struct_field_ptr_index_1(*[]usize, %14)
  %16 = array_to_slice([]usize, %13!)
  %17!= store(%15!, %16!)
  %18 = struct_field_ptr_index_0(*usize, %14)
  %19!= store(%18!, @.zero_usize)
  %20!= set_err_return_trace(%14!)
  %0!= save_err_return_trace_index()
  %1!= dbg_stmt(2:5)
  %2 = load(u3, <*u3, &repro.lhs>)
  %3 = load(u1, <*u1, &repro.rhs>)
  %4 = cmp_lt(%3, <u1, 3>)
  %7!= block(void, {
    %8!= cond_br(%4!, likely {
      %9!= br(%7, @.void_value)
    }, cold {
      %2! %3!
      %5!= call(<fn () noreturn, (function 'shiftRhsTooBig')>, [])
      %6!= unreach()
    })
  } %4!)
  %10 = shl_sat(%2!, %3!)
  %11!= store_safe(<*u3, &repro.lhs>, %10!)
  %12!= ret_safe(@.void_value)
# End Function AIR: repro.shlSat

[stack trace...]
new
# Begin Function AIR: repro.shlSat:
# Total AIR+Liveness bytes: 219B
# AIR Instructions:         7 (63B)
# AIR Extra Data:           9 (36B)
# Liveness tomb_bits:       8B
# Liveness Extra Data:      0 (0B)
# Liveness special table:   0 (0B)
  %0!= save_err_return_trace_index()
  %1!= dbg_stmt(2:5)
  %2 = load(u3, <*u3, &repro.lhs>)
  %3 = load(u1, <*u1, &repro.rhs>)
  %4 = shl_sat(%2!, %3!)
  %5!= store_safe(<*u3, &repro.lhs>, %4!)
  %6!= ret_safe(@.void_value)
# End Function AIR: repro.shlSat

@alexrp alexrp requested a review from mlugg July 13, 2025 02:22
Copy link
Member

@mlugg mlugg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mlugg mlugg merged commit 6ec275e into ziglang:master Jul 30, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sema: saturating left shift produces an incorrect safety check and invalid Air
2 participants