• Stars
    star
    496
  • Rank 88,637 (Top 2 %)
  • Language
    Julia
  • License
    Other
  • Created almost 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compiles Julia code to a standalone library (experimental)

StaticCompiler

CI CI (Integration) CI (Julia nightly) CI (Integration nightly) Coverage

This is an experimental package to compile Julia code to standalone libraries. A system image is not needed.

Installation and Usage

Installation is the same as any other registered Julia package

using Pkg
Pkg.add("StaticCompiler")

There are two main ways to use this package:

Linked compilation

The first option is via the compile function, which can be used when you want to compile a Julia function for later use from within Julia:

julia> using StaticCompiler

julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2)
fib (generic function with 1 method)

julia> fib_compiled, path = compile(fib, Tuple{Int}, "fib")
(f = fib(::Int64) :: Int64, path = "fib")

julia> fib_compiled(10)
55

Now we can quit this session and load a new one where fib is not defined:

julia> using StaticCompiler

julia> fib
ERROR: UndefVarError: fib not defined

julia> fib_compiled = load_function("fib")
fib(::Int64) :: Int64

julia> fib_compiled(10)
55

See the file tests/runtests.jl for some examples of functions that work with compile (and some that don't, marked with @test_skip).

Standalone compilation

The second way to use this package is via the compile_executable and compile_shlib functions, for when you want to compile a Julia function to a native executable or shared library for use from outside of Julia:

julia> using StaticCompiler, StaticTools

julia> hello() = println(c"Hello, world!")
hello (generic function with 1 method)

julia> compile_executable(hello, (), "./")
"/Users/user/hello"

shell> ls -alh hello
-rwxrwxr-x. 1 user user 8.4K Oct 20 20:36 hello

shell> ./hello
Hello, world!

This latter approach comes with substantially more limitations, as you cannot rely on libjulia (see, e.g., StaticTools.jl for some ways to work around these limitations).

The low-level function StaticCompiler.generate_obj (not exported) generates object files. This can be used for more control of compilation. This can be used to cross-compile to other targets.

Mixtape

This feature allows one to change functionality when statically compiling. This uses code and API from Mixtape to transform lowered code much like Cassette.

To use the Mixtape feature, define a CompilationContext struct and pass this to any of the compilation functions with the mixtape keyword. Define transform and allow functions for this CompilationContext to define the transformation to be done.

See here for an example.

Approach

This package uses the GPUCompiler package to generate code.

Limitations

  • GC-tracked allocations and global variables do work with compile, but the way they are implemented is brittle and can be dangerous. Allocate with care.
  • GC-tracked allocations and global variables do not work with compile_executable or compile_shlib. This has some interesting consequences, including that all functions within the function you want to compile must either be inlined or return only native types (otherwise Julia would have to allocate a place to put the results, which will fail).
  • Since error handling relies on libjulia, you can only throw errors from standalone-compiled (compile_executable / compile_shlib) code if an explicit overload has been defined for that particular error with @device_override (see quirks.jl).
  • Type instability. Type unstable code cannot currently be statically compiled via this package.
  • Doesn't work on Windows (but works in WSL on Windows 10+). PRs welcome.

More Repositories

1

Eyeball.jl

Object and type viewer for Julia
Julia
140
star
2

FunctionalModels.jl

Equation-based modeling and simulations in Julia
Julia
112
star
3

OpenDSS

Electrical power system simulation tool primarily for utility power distribution systems
Pascal
88
star
4

WebAssemblyCompiler.jl

Create WebAssembly with Julia
Julia
76
star
5

openmodelica-javascript

Code to compile OpenModelica models to JavaScript
C
61
star
6

mdpad

Live, interactive pages using Markdown
24
star
7

WebAssemblyInterfaces.jl

Setup code to interface between JavaScript and Julia compiled to WebAssembly
JavaScript
24
star
8

ExportWebAssembly.jl

[WIP] Export Julia functions to WebAssembly and JavaScript
Assembly
23
star
9

CodeGen.jl

[Experimental] Code generation for Julia in Julia
Julia
22
star
10

markdown-folder

Atom package that folds and unfolds markdown headings
CoffeeScript
21
star
11

Lorenz-WebAssembly-Model.jl

Lorenz attraction app in Julia compiled to WebAssembly
CSS
21
star
12

Maker.jl

A make-like system for data analysis
Julia
20
star
13

JuliaMarkdown

Julia Markdown for easy web interfaces.
15
star
14

Calc.jl

An RPN calculator for the Julia REPL
Julia
10
star
15

REPLinModule.jl

REPL mode to evaluate in a module
Julia
8
star
16

opendss-vscode

OpenDSS script support in VS Code
6
star
17

ReactiveBasics.jl

Basic Reactive Functional Programming (RFP)
Julia
5
star
18

mdpad-js

One-file web apps in Markdown
JavaScript
5
star
19

language-opendss

CoffeeScript
4
star
20

jl2js-dock

[WIP] Docker files for converting from Julia to JavaScript / WebAssembly
C++
3
star
21

MMM-KitchenTimer

Kitchen timer module for Magic Mirror
JavaScript
3
star
22

COMTRADE.jl

Access COMTRADE files in Julia
Julia
2
star
23

PooledElements.jl

Pooled strings, elements, and arrays for Julia
Julia
2
star
24

SundialsKLUBuilder

Julia
1
star
25

BitcodeRunner.jl

Load LLVM bitcode files directly into a Julia module
Julia
1
star
26

BlisBuilder

BinaryBuilder for BLIS
Julia
1
star
27

tshort.github.com

website
1
star