Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested ifelse not giving computed gotos #10320

Closed
simonbyrne opened this issue Feb 25, 2015 · 6 comments
Closed

nested ifelse not giving computed gotos #10320

simonbyrne opened this issue Feb 25, 2015 · 6 comments
Labels
compiler:codegen Generation of LLVM IR and native code performance Must go faster

Comments

@simonbyrne
Copy link
Contributor

From #5410: LLVM should be able optimise certain chains of ifelse statements into computed gotos. However the examples mentioned in that issue no longer work, e.g.

function foo(x)
    if x == 1
        x * 7
    elseif x == 2
        x * 8
    elseif x == 3
        x * 9
    elseif x == 4
        x * 10
    elseif x == 5
        x * 11
    elseif x == 6
        x * 12
    elseif x == 7
        x * 13
    elseif x == 8
        x * 14
    elseif x == 9
        x * 15
    elseif x == 10
        x * 16
    elseif x == 11
        x * 17
    else
        x * 0
    end
end
code_native(foo, (Int,))

gives a sequence of cmp/jne instructions.

@ihnorton ihnorton added the performance Must go faster label Mar 1, 2015
@ihnorton ihnorton added the compiler:codegen Generation of LLVM IR and native code label Mar 11, 2015
@yuyichao
Copy link
Contributor

yuyichao commented Jul 22, 2017

It's a fixed LLVM issue. LLVM is smart enough to generate switches (i.e. jump tables) for a few cases I've tested (even triggering a bug that needs to be workarounded). For the code shown above, llvm is smart enough to optimize it as

if x - 1 < 11
    return statically_computed_results[x]
else
    return 0
end

@ihnorton
Copy link
Member

I think @simonbyrne's code was mostly for demonstration purposes...

@yuyichao
Copy link
Contributor

That's why I first addressed the general case.

@simonbyrne
Copy link
Contributor Author

Which version of LLVM is it fixed in?

@yuyichao
Copy link
Contributor

Not sure, I'm using 4.0.

@StefanKarpinski
Copy link
Member

Julia 0.6 and 0.7-DEV both produce native code using LLVM 3.9.1:

	.section	__TEXT,__text,regular,pure_instructions
Filename: REPL[111]
	pushq	%rbp
	movq	%rsp, %rbp
Source line: 2
	leaq	-1(%rdi), %rax
	cmpq	$10, %rax
	ja	L31
	movabsq	$4745995008, %rax       ## imm = 0x11AE22300
	movq	-8(%rax,%rdi,8), %rax
	popq	%rbp
	retq
Source line: 3
L31:
	xorl	%eax, %eax
	popq	%rbp
	retq
	nopw	%cs:(%rax,%rax)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:codegen Generation of LLVM IR and native code performance Must go faster
Projects
None yet
Development

No branches or pull requests

4 participants