Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LLVM wizardry
julia> code_llvm(g, (Int,))
define i64 @julia_g_20191(i64) {
top:
%1 = icmp sgt i64 %0, 0, !dbg !8
br i1 %1, label %L.preheader, label %L3, !dbg !8
L.preheader: ; preds = %top
%2 = add i64 %0, -1, !dbg !8
%3 = zext i64 %2 to i65
%4 = add i64 %0, -2, !dbg !8
%5 = zext i64 %4 to i65
%6 = mul i65 %3, %5, !dbg !8
%7 = add i64 %0, -3, !dbg !8
%8 = zext i64 %7 to i65
%9 = mul i65 %6, %8, !dbg !8
%10 = lshr i65 %9, 1, !dbg !8
%11 = trunc i65 %10 to i64
%12 = mul i64 %11, 6148914691236517206, !dbg !8
%13 = mul i64 %0, 6, !dbg !8
%14 = add i64 %12, %13, !dbg !8
%15 = lshr i65 %6, 1, !dbg !8
%16 = trunc i65 %15 to i64
%17 = mul i64 %16, 6, !dbg !8
%18 = add i64 %14, %17, !dbg !8
%19 = add i64 %18, -4, !dbg !8
br label %L3
L3: ; preds = %L.preheader, %top
%tot.1 = phi i64 [ 0, %top ], [ %19, %L.preheader ]
ret i64 %tot.1, !dbg !9
}
julia> code_native(g, (Int,))
.text
Filename: none
Source line: 0
push rbp
mov rbp, rsp
xor eax, eax
Source line: 3
test rdi, rdi
jle 72
lea rax, qword ptr [rdi - 1]
lea rcx, qword ptr [rdi - 2]
mul rcx
mov r8, rax
mov rsi, rdx
lea rdx, qword ptr [rdi - 3]
mov ecx, edx
imul ecx, esi
mul rdx
add edx, ecx
shld rdx, rax, 63
movabs rax, 6148914691236517206
imul rax, rdx
lea rcx, qword ptr [rdi + 2*rdi]
lea rax, qword ptr [rax + 2*rcx]
shld rsi, r8, 63
lea rcx, qword ptr [rsi + 2*rsi]
lea rax, qword ptr [rax + 2*rcx - 4]
Source line: 6
pop rbp
ret
f(n) = n*n + n
function g(n)
tot = zero(n)
for i in 1:n
tot += f(i)
end
tot
end
@time g(10000000) # wtf?
code_native(g, (Int,)) # How does it do this?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment