# Does Julia bitcode system image file generated with --compile=all --output-bc flags actually use llvm IR representation?

**URL:** <https://discourse.julialang.org/t/does-julia-bitcode-system-image-file-generated-with-compile-all-output-bc-flags-actually-use-llvm-ir-representation/3219>\
**Category:** Internals & Design\
**Created:** [April 14, 2017, 4:49pm UTC](https://discourse.julialang.org/t/does-julia-bitcode-system-image-file-generated-with-compile-all-output-bc-flags-actually-use-llvm-ir-representation/3219 "2017-04-14T16:49:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![psrivas2](https://avatars.discourse-cdn.com/v4/letter/p/8e7dd6/32.png) [@psrivas2](https://discourse.julialang.org/u/psrivas2)\
**Post date:** [April 14, 2017, 4:49pm UTC](https://discourse.julialang.org/t/does-julia-bitcode-system-image-file-generated-with-compile-all-output-bc-flags-actually-use-llvm-ir-representation/3219/1 "2017-04-14T16:49:14Z")

</div>

I built a system image file with a new function `printHello`

```julia
function printHello()
  println("Hello World!")
end

```

I used the `--compile=all --output-bc` flags in the `build_sysimg.jl` to generate the system image in llvm bitcode format `sys-hello.bc`. I use this bitcode system image file to start julia and set compile to “no” `julia -J sys-hello.bc --compile=no` (so that I am informed if any expression needs to be compiled from the REPL) and then run the `printHello()` function in REPL. This works and I am under the impression that for running the `printHello` function julia did not have to compile anything and used the bitcode of that function `julia_printHello_<some numbers>` to get the native IR and run it.

However, even if I go ahead and remove everything from this function, I still get the correct output.  
Is Julia still dynamically compiling the printHello function from the AST? Is the LLVM bitcode generated in `sys-hello.bc` of any use, or only the binary blob containing the AST matters?

PS: This is in relation to my effort to get LLVM IR of my function in Julia and being able to make changes to it, which are eventually reflected in the output. A related topic is [here](https://discourse.julialang.org/t/getting-reusable-llvm-ir-representation-of-a-module-or-a-script-modifying-llvm-ir-and-replugging-it-in-julia/2935)

**Update: April 14 2017 12:10pm CST** : I see now that the command `julia -J sys-hello.bc --compile=no` does not even look for the file sys-hello.bc. Instead it looks for sys-hello.so in its place and uses it. I am not sure how is this helpful. The application should be more transparent about this. Just display the error that this is not a sys image file if we cannot use it, rather than having the user believe that he/she is using a file which he/she is not.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [April 14, 2017, 5:50pm UTC](https://discourse.julialang.org/t/does-julia-bitcode-system-image-file-generated-with-compile-all-output-bc-flags-actually-use-llvm-ir-representation/3219/2 "2017-04-14T17:50:47Z")

</div>

> Instead it looks for sys-hello.so in its place and uses it. I am not sure how is this helpful

I agree. We seem to have been looking at the same thing yesterday: [make jl\_init more friendly to embedded targets by vtjnash · Pull Request #21365 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/21365) 🙂

To address the question in the title, yes that’s the complete IR (intermediate representation). To actually use this, you need to finish compiling and linking it (to a shared library representation). I only mentioned this very briefly in a footnote of my on the rest of the process: [https://juliacomputing.com/blog/2016/02/09/static-julia.html#supplemental-tools](https://juliacomputing.com/blog/2016/02/09/static-julia.html#supplemental-tools)  
In particular, you’ll want to pass the `.bc` file to `llc` or `clang` to turn into a `.so` file that the dynamic linker can load.

---

<div class="post-metadata">

**Author:** ![psrivas2](https://avatars.discourse-cdn.com/v4/letter/p/8e7dd6/32.png) [@psrivas2](https://discourse.julialang.org/u/psrivas2)\
**Post date:** [April 14, 2017, 6:25pm UTC](https://discourse.julialang.org/t/does-julia-bitcode-system-image-file-generated-with-compile-all-output-bc-flags-actually-use-llvm-ir-representation/3219/3 "2017-04-14T18:25:54Z")

</div>

Thanks. Yes that looks like the the way forward. However, when using clang 3.7 to build a shared library it gives the error `fatal error: error in backend: Cannot generate unaligned atomic store`

This is generated by `load/store atomic` statements on `i128` types with byte alignment statements such as the one in the following code

```llvm
define internal void @"julia_setindex!_53289"(%jl_value_t*, i128) #4 {
top:
  call void @llvm.dbg.value(metadata %jl_value_t* null, i64 0, metadata !272997, metadata !846405), !dbg !918393
  call void @llvm.dbg.value(metadata %jl_value_t* %0, i64 0, metadata !272997, metadata !846405), !dbg !918393
  call void @llvm.dbg.value(metadata i128 %1, i64 0, metadata !272998, metadata !846354), !dbg !918393
  %2 = bitcast %jl_value_t* %0 to i128*, !dbg !918393
  store atomic i128 %1, i128* %2 release, align 8, !dbg !918393
  ret void, !dbg !918393
}

```

Should I start this as a separate topic?

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [April 14, 2017, 11:37pm UTC](https://discourse.julialang.org/t/does-julia-bitcode-system-image-file-generated-with-compile-all-output-bc-flags-actually-use-llvm-ir-representation/3219/4 "2017-04-14T23:37:14Z")

</div>

Are you on 0.5? This should be fixed by [https://github.com/JuliaLang/julia/pull/19482](https://github.com/JuliaLang/julia/pull/19482) on 0.6. Also make sure that you use the same version of `llc` as Julia does.

---

<div class="post-metadata">

**Author:** ![psrivas2](https://avatars.discourse-cdn.com/v4/letter/p/8e7dd6/32.png) [@psrivas2](https://discourse.julialang.org/u/psrivas2)\
**Post date:** [April 15, 2017, 2:36pm UTC](https://discourse.julialang.org/t/does-julia-bitcode-system-image-file-generated-with-compile-all-output-bc-flags-actually-use-llvm-ir-representation/3219/5 "2017-04-15T14:36:36Z")

</div>

Yes, I am using the release-0.5 branch, and after fixing the alignment issues I am able to compile the bitcode to .so. But when I try to load it as a Julia system image file, it says `System Image file not found`.  
Here is the command

```bash
clang -fPIC -shared -o sys-hello.so sys-hello.bc -ljulia -L ~/Utilities/julia-src/install/release-0.5/lib

```

I am using llvm and clang 3.7

---

<div class="post-metadata">

**Author:** ![psrivas2](https://avatars.discourse-cdn.com/v4/letter/p/8e7dd6/32.png) [@psrivas2](https://discourse.julialang.org/u/psrivas2)\
**Post date:** [April 16, 2017, 4:40pm UTC](https://discourse.julialang.org/t/does-julia-bitcode-system-image-file-generated-with-compile-all-output-bc-flags-actually-use-llvm-ir-representation/3219/6 "2017-04-16T16:40:26Z")

</div>

I can confirm that the same is true if I use the latest commit on master (eee75d1) and clang 3.9. If I try to compile the generated bitcode file of system image to .so with the above command, julia instance does not recognize it as a system image.
