Skip to content

Commit df5b081

Browse files
authored
Special purpose inliner for Core.ifelse (#47096)
This function isn't used as much anymore now that Base.ifelse is an actual generic function, but it's still used in a few places across the ecosystem, and there currently isn't anything that would fold it for constant conditions, so add a special case inliner for it. This probably doesn't have a huge impact, but I happened to run into a case where it was causing annoying suboptimialities and it's a quick fix.
1 parent 4c0f8de commit df5b081

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

base/compiler/ssair/inlining.jl

+9
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,15 @@ function early_inline_special_case(
17451745
setting === :const || setting === :conditional || setting === :type || return nothing
17461746
# barriered successfully already, eliminate it
17471747
return SomeCase(stmt.args[3])
1748+
elseif f === Core.ifelse && length(argtypes) == 4
1749+
cond = argtypes[2]
1750+
if isa(cond, Const)
1751+
if cond.val === true
1752+
return SomeCase(stmt.args[3])
1753+
elseif cond.val === false
1754+
return SomeCase(stmt.args[4])
1755+
end
1756+
end
17481757
end
17491758
return nothing
17501759
end

test/compiler/inline.jl

+9
Original file line numberDiff line numberDiff line change
@@ -1761,3 +1761,12 @@ let interp = Core.Compiler.NativeInterpreter()
17611761
@test count(isinvoke(:*), ir.stmts.inst) == 0
17621762
@test count(iscall((ir, Core.Intrinsics.mul_int)), ir.stmts.inst) == 1
17631763
end
1764+
1765+
# Test special purpose inliner for Core.ifelse
1766+
f_ifelse_1(a, b) = Core.ifelse(true, a, b)
1767+
f_ifelse_2(a, b) = Core.ifelse(false, a, b)
1768+
f_ifelse_3(a, b) = Core.ifelse(a, true, b)
1769+
1770+
@test fully_eliminated(f_ifelse_1, Tuple{Any, Any}; retval=Core.Argument(2))
1771+
@test fully_eliminated(f_ifelse_2, Tuple{Any, Any}; retval=Core.Argument(3))
1772+
@test !fully_eliminated(f_ifelse_3, Tuple{Any, Any})

0 commit comments

Comments
 (0)