Giter Site home page Giter Site logo

mixtape.jl's Introduction

Mixtape.jl

Note: Usage of this compiler package requires Julia > 1.6.

Function

Mixtape.jl is a static method overlay tool which operates during Julia type inference. It allows you to (precisely) replace CodeInfo, pre-optimize CodeInfo, and create other forms of static analysis tools on uninferred CodeInfo as part of Julia's native type inference system.

In many respects, it is similar to Cassette -- but it is completely static.

Interfaces

using Mixtape
import Mixtape: CompilationContext, 
                transform, 
                allow_transform, 
                show_after_inference,
                show_after_optimization, 
                debug,
                jit

Mixtape.jl exports a set of interfaces which allows you to customize parts of Julia type inference as part of a custom code generation pipeline. This code generation pipeline works through the LLVM.jl and GPUCompiler.jl infrastructure.

Usage typically proceeds as follows.

Example

We may start with some unassuming code.

# Unassuming code in an unassuming module...
module SubFoo

function h(x)
    x = rand()
    y = rand()
    return x + y
end

function f(x)
    z = rand()
    return h(x)
end

end

Now, we define a new subtype of CompilationContext which we will use to parametrize the pipeline using dispatch.

# 101: How2Mix
struct MyMix <: CompilationContext end

# Operates on IRTools.IR.
# Must return IRTools.IR when finished.
function transform(::MyMix, ir)
    for (v, st) in ir
        st.expr isa Expr || continue
        st.expr.head == :call || continue
        st.expr.args[1] == Base.rand || continue
        ir[v] = 5
    end
    display(ir)
    return ir
end

# MyMix will only transform functions which you explicitly allow.
allow_transform(ctx::MyMix, fn::typeof(SubFoo.h), a...) = true

# You can greenlight whole modules, if you so desire.
allow_transform(ctx::MyMix, m::Module) = m == SubFoo

# Debug printing.
show_after_inference(ctx::MyMix) = false
show_after_optimization(ctx::MyMix) = false
debug(ctx::MyMix) = true

When applying jit with a new instance of MyMix, the pipeline is applied.

fn = Mixtape.jit(MyMix(), SubFoo.f, Tuple{Float64})
@time fn = Mixtape.jit(MyMix(), SubFoo.f, Tuple{Float64})

display(fn(5.0))
display(SubFoo.f(5.0))

We get to see our transformed IRTools.IR as part of the call to transform.

1: (%1 :: typeof(Main.How2Mix.SubFoo.f), %2 :: Float64)
  %3 = (rand)()
  %4 = (Main.How2Mix.SubFoo.h)(%2)
  return %4
1: (%1 :: typeof(Main.How2Mix.SubFoo.h), %2 :: Float64)
  %3 = (rand)()
  %4 = (rand)()
  %5 = (+)(%3, %4)
  return %5
  0.000007 seconds
10
1.1235863744100292

Package contribution

  1. Completely static -- does not rely on recursive pollution of the call stack (alla the overdub issue).
  2. Pre-type inference -- all semantic-intruding changes happen before type inference runs on the lowered method body.
  3. Mixtape.jl manages its own code cache -- doesn't interact with the native runtime system (see above).

However, there are some downsides. Mixtape.jl may choke on code which also causes Cassette.jl to choke. However, debugging should be easier. In addition, because Mixtape.jl uses a custom execution engine through GPUCompiler.jl -- code which causes GPUCompiler.jl to fail will also cause Mixtape.jl to fail. Both of these downsides should be worked out over time.

mixtape.jl's People

Contributors

femtomc avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.