Skip to content

Lack of constant prop in nextpow(2, x) #58906

Open
@tecosaur

Description

@tecosaur

When calling nextpow(2, x), it seems that it's expected that Base._nextpow2 is inlined.

julia/base/intfuncs.jl

Lines 575 to 578 in 4846c3d

# Special case fast path for x::Integer, a == 2.
# This is a very common case. Constant prop will make sure that a call site
# specified as `nextpow(2, x)` will get this special case inlined.
a == 2 && isa(x, Integer) && return _nextpow2(x)

However, with 1.11 through to nightly, it seems like this may not be the case. I see those lines were written in 2018, and needless to say a lot has changed since then.

julia> npow2constproptest(x) = nextpow(2, x)
npow2constproptest (generic function with 1 method)

julia> @code_native npow2constproptest(1234)
	.file	"npow2constproptest"
	.section	.ltext,"axl",@progbits
	.globl	julia_npow2constproptest_4029   # -- Begin function julia_npow2constproptest_4029
	.p2align	4
	.type	julia_npow2constproptest_4029,@function
julia_npow2constproptest_4029:          # @julia_npow2constproptest_4029
; Function Signature: npow2constproptest(Int64)
; ┌ @ REPL[4]:1 within `npow2constproptest`
# %bb.0:                                # %top
	#DEBUG_VALUE: npow2constproptest:x <- $rdi
	push	rbp
	mov	rbp, rsp
	mov	rsi, rdi
	movabs	rax, offset j_nextpow_4031
	mov	edi, 2
	call	rax
	pop	rbp
	ret
.Lfunc_end0:
	.size	julia_npow2constproptest_4029, .Lfunc_end0-julia_npow2constproptest_4029
; └
                                        # -- End function

Is a Base.@constprop :aggressive annotation needed here? From a quick check, it seems to help. There does seem to be some extra overhead compared to _nextpow2 though. Perhaps I'm being overly optimistic, but I hoped that npow2constproptest would compile to the same instructions as _nextpow2, and even with aggressive constant prop that doesn't seem to be the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler:optimizerOptimization passes (mostly in base/compiler/ssair/)mathsMathematical functionsperformanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions