MasonProtter / Bumper.jl

Bring Your Own Stack
MIT License
152 stars 6 forks source link

Add package extension for StaticCompiler.jl #26

Closed MasonProtter closed 9 months ago

MasonProtter commented 9 months ago

I'm not adding any tests for this because I don't want the instability of StaticCompiler.jl messing with the test suite, but it's useful to provide this anyways.

With this, on julia versions 1.9+ (I didn't bother with Requires.jl), you can do

using Bumper, StaticTools
function times_table(argc::Int, argv::Ptr{Ptr{UInt8}})
    argc == 3 || return printf(c"Incorrect number of command-line arguments\n")
    rows = argparse(Int64, argv, 2)            # First command-line argument
    cols = argparse(Int64, argv, 3)            # Second command-line argument

    buf = AllocBuffer(MallocVector)
    @no_escape buf begin
        M = @alloc(Int, rows, cols)
        for i=1:rows
            for j=1:cols
                M[i,j] = i*j
            end
        end
        printf(M)
    end
    free(buf)
end

using StaticCompiler
filepath = compile_executable(times_table, (Int64, Ptr{Ptr{UInt8}}), "./")

and then

shell> ./times_table 12, 7
1   2   3   4   5   6   7
2   4   6   8   10  12  14
3   6   9   12  15  18  21
4   8   12  16  20  24  28
5   10  15  20  25  30  35
6   12  18  24  30  36  42
7   14  21  28  35  42  49
8   16  24  32  40  48  56
9   18  27  36  45  54  63
10  20  30  40  50  60  70
11  22  33  44  55  66  77
12  24  36  48  60  72  84

cc @brenhinkeller

MasonProtter commented 9 months ago

Coverage decrease is expected.

brenhinkeller commented 9 months ago

That's awesome!! Nice work Mason!

We could add integration tests on the StaticCompiler or StaticTools side instead where the instability is expected -- could be useful on that end