Skip to content

Commit

Permalink
inlining: optimize and_int(x, true) and or_int(x, false)
Browse files Browse the repository at this point in the history
We need an additional inlining pass to optimize cases like
`and_int(x, true)` and `or_int(x, false)` to `x`.
  • Loading branch information
aviatesk committed Jun 8, 2024
1 parent 75951ec commit 4494e75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 18 additions & 2 deletions base/compiler/ssair/inlining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,7 @@ function early_inline_special_case(ir::IRCode, stmt::Expr, flag::UInt32,
@nospecialize(type), sig::Signature, state::InliningState)
OptimizationParams(state.interp).inlining || return nothing
(; f, ft, argtypes) = sig

= partialorder(optimizer_lattice(state.interp))
if isa(type, Const) # || isconstType(type)
val = type.val
is_inlineable_constant(val) || return nothing
Expand Down Expand Up @@ -1714,9 +1714,25 @@ function early_inline_special_case(ir::IRCode, stmt::Expr, flag::UInt32,
elseif cond.val === false
return SomeCase(stmt.args[4])
end
elseif (optimizer_lattice(state.interp), cond, Bool) && stmt.args[3] === stmt.args[4]
elseif cond Bool && stmt.args[3] === stmt.args[4]
return SomeCase(stmt.args[3])
end
elseif f === and_int && length(argtypes) == 3
argtype2, argtype3 = argtypes[2], argtypes[3]
if argtype2 isa Const && argtype2.val === true && argtype3 Bool
return SomeCase(stmt.args[3])
end
if argtype3 isa Const && argtype3.val === true && argtype2 Bool
return SomeCase(stmt.args[2])
end
elseif f === or_int && length(argtypes) == 3
argtype2, argtype3 = argtypes[2], argtypes[3]
if argtype2 isa Const && argtype2.val === false && argtype3 Bool
return SomeCase(stmt.args[3])
end
if argtype3 isa Const && argtype3.val === false && argtype2 Bool
return SomeCase(stmt.args[2])
end
end
return nothing
end
Expand Down
6 changes: 6 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2214,3 +2214,9 @@ let ir = Base.code_ircode((Issue52644,); optimize_until="Inlining") do t
@test irfunc(Issue52644(Tuple{})) === :DataType
@test_throws MethodError irfunc(Issue52644(Tuple{<:Integer}))
end

# inlining optimization for `and_int` and `or_int`
@test fully_eliminated(x->x&true, (Bool,); retval=Argument(2))
@test fully_eliminated(x->true&x, (Bool,); retval=Argument(2))
@test fully_eliminated(x->x|false, (Bool,); retval=Argument(2))
@test fully_eliminated(x->false|x, (Bool,); retval=Argument(2))

0 comments on commit 4494e75

Please sign in to comment.