Skip to content

Commit

Permalink
add tfuncs for [and|or]_int intrinsics (#51266)
Browse files Browse the repository at this point in the history
So that they can be constant folded when either of argument is known to
be `Core([false|true])`. It may help inference accuracy.
  • Loading branch information
aviatesk authored Sep 12, 2023
1 parent ad27e67 commit 5d82d80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
26 changes: 24 additions & 2 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,33 @@ add_tfunc(div_float_fast, 2, 2, math_tfunc, 2)
# bitwise operators
# -----------------

@nospecs and_int_tfunc(𝕃::AbstractLattice, x, y) = and_int_tfunc(widenlattice(𝕃), x, y)
@nospecs function and_int_tfunc(𝕃::ConstsLattice, x, y)
if isa(x, Const) && x.val === false && widenconst(y) === Bool
return Const(false)
elseif isa(y, Const) && y.val === false && widenconst(x) === Bool
return Const(false)
end
return and_int_tfunc(widenlattice(𝕃), x, y)
end
@nospecs and_int_tfunc(::JLTypeLattice, x, y) = widenconst(x)

@nospecs or_int_tfunc(𝕃::AbstractLattice, x, y) = or_int_tfunc(widenlattice(𝕃), x, y)
@nospecs function or_int_tfunc(𝕃::ConstsLattice, x, y)
if isa(x, Const) && x.val === true && widenconst(y) === Bool
return Const(true)
elseif isa(y, Const) && y.val === true && widenconst(x) === Bool
return Const(true)
end
return or_int_tfunc(widenlattice(𝕃), x, y)
end
@nospecs or_int_tfunc(::JLTypeLattice, x, y) = widenconst(x)

@nospecs shift_tfunc(𝕃::AbstractLattice, x, y) = shift_tfunc(widenlattice(𝕃), x, y)
@nospecs shift_tfunc(::JLTypeLattice, x, y) = widenconst(x)

add_tfunc(and_int, 2, 2, math_tfunc, 1)
add_tfunc(or_int, 2, 2, math_tfunc, 1)
add_tfunc(and_int, 2, 2, and_int_tfunc, 1)
add_tfunc(or_int, 2, 2, or_int_tfunc, 1)
add_tfunc(xor_int, 2, 2, math_tfunc, 1)
add_tfunc(not_int, 1, 1, math_tfunc, 0) # usually used as not_int(::Bool) to negate a condition
add_tfunc(shl_int, 2, 2, shift_tfunc, 1)
Expand Down
8 changes: 8 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5190,3 +5190,11 @@ foo51090(b) = return bar51090(b)
@test Base.return_types() do
Base.or_int(true, 1)
end |> only === Union{}

# [add|or]_int tfuncs
@test Base.return_types((Bool,)) do b
Val(Core.Intrinsics.and_int(b, false))
end |> only == Val{false}
@test Base.return_types((Bool,)) do b
Val(Core.Intrinsics.or_int(true, b))
end |> only == Val{true}

2 comments on commit 5d82d80

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

The package evaluation job you requested has completed - possible new issues were detected.
The full report is available.

Please sign in to comment.