tshort / StaticCompiler.jl

Compiles Julia code to a standalone library (experimental)
Other
490 stars 31 forks source link

Bad code generation with step-ranges #20

Closed MasonProtter closed 3 years ago

MasonProtter commented 4 years ago

Curious issue here:

using StaticCompiler, LLVM
foo() = 1:1:5
m = irgen(foo, Tuple{})

LLVM.verify(m) #totally kosher
using StaticCompiler, LLVM
bar() = 1:0.5:5
m = irgen(bar, Tuple{})

LLVM.verify(m) #barfs a big error
julia>  versioninfo()
Julia Version 1.4.0-rc1.0
 Commit b0c33b0cf5 (2020-01-23 17:23 UTC)
 Platform Info:
   OS: macOS (x86_64-apple-darwin19.2.0)
   CPU: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
   WORD_SIZE: 64
   LIBM: libopenlibm
   LLVM: libLLVM-8.0.1 (ORCJIT, skylake)
 Environment:
   JULIA_NUM_THREADS = 4
aminya commented 4 years ago

This code different but macOS has some problems with StaticCompiler. Is the error the same as the one in this issue? : https://github.com/tshort/StaticCompiler.jl/issues/13

chriselrod commented 4 years ago

The error is:

julia> LLVM.verify(m) #barfs a big error
ERROR: LLVM error: Global is referenced in a different module!

Out of curiosity, if the goal is to write this to a file (like in the README), is code_llvm (with dump_module=true) insufficient? Is the problem that code_llvm produces more references to Julia, like @jl_gc_pool_alloc?

MasonProtter commented 4 years ago

For context, this problem was raised because I was trying to statically compile a function which included a StepRange in an intermediate computation, and I was able to trim it down to just the pure case of returning a step-range.

tshort commented 4 years ago

I can reproduce. It also fails with irgen(bar, Tuple{}, overdub = false). I'll try to dig into this over the weekend.

@chriselrod, code_llvm is insufficient for the reason you state, and for me, I need to cross compile, and code_llvm doesn't allow you to set the target.

tshort commented 4 years ago

This is a tough type of bug. I can only reproduce it on 1 out of 4 of my systems. bar() compiles and verifies fine on all but one.

Processor OS Julia binary Fails (Y/N)
SkyLake Windows 10 1809 Windows N
SkyLake Ubuntu 19.04 under Hyper-V Linux N
Haswell Windows 10 1809 Windows Y
Haswell Windows subsystem for Linux Linux N

The one failure is with verify with the same error message that @chriselrod reported. All these are with Julia v1.3.1 with the latest master of StaticCompiler.

I don't have access to macOS.