Non-optimal optimization of for loop

173 views
Skip to first unread message

Scott Jones

unread,
Mar 2, 2016, 5:36:28 AM3/2/16
to julia-dev
I was helping someone on Gitter (https://gitter.im/JuliaLang/julia) with some benchmarks against C++, and ran into this:

julia> function jl_foo()

                         x = 0


                         for i in 1:1000_000_000

                             x += 1

                         end


                         return x

                     end

jl_foo (generic function with 1 method)


julia> @code_native jl_foo()

.section __TEXT,__text,regular,pure_instructions

pushq %rbp

movq %rsp, %rbp

xorl %eax, %eax

movabsq $5868546160, %rcx       ## imm = 0x15DCAE870

movq (%rcx), %rcx

cmpq $0, %rcx

je L28

movq %rcx, %rax

L28:

popq %rbp

retq


I would have expected the native code to be simply:

movabsq $1000000000, %rax

retq


Does anybody have an explanation for why it is loading the constant value from memory, and needlessly checking to see if it is 0 (where it leaves the value of 0 in %rax) before copying it into %rax?

Thanks, Scott

Kristoffer Carlsson

unread,
Mar 2, 2016, 8:56:38 AM3/2/16
to julia-dev
On 0.4.3 it just returns the value.

Scott Jones

unread,
Mar 2, 2016, 8:59:38 AM3/2/16
to julia-dev
Ah, good to know!  So it is a regression then, and hopefully somebody can file an issue about it.  Thanks for checking v0.4.3 (I should have thought to do that, duh!)
Reply all
Reply to author
Forward
0 new messages