Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions .github/workflows/julia.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,43 @@ jobs:
strategy:
fail-fast: false
matrix:
julia-version: [1.2.0]
julia-version: [1.3]
os: [ubuntu-18.04]

steps:
- name: "Checkout Enzyme.jl code"
uses: actions/checkout@v1
with:
repository: wsmoses/Enzyme.jl
clean: false
path: Enzyme.jl
token: ${{secrets.enzymejl_secret}}
ref: master
- name: "Checkout Enzyme code"
uses: actions/checkout@v1
with:
fetch-depth: 1
- name: "Set up Julia"
uses: julia-actions/setup-julia@v0.2
uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.julia-version }}
- name: "Download LLVM"
run: |
julia --project=contrib -e 'using Pkg; pkg"instantiate"'
julia --project=contrib contrib/build_LLVM.v6.0.1.jl
julia -e 'using Pkg; pkg"add LLVM_jll"'
ARTIFACT_DIR=`julia -e "using LLVM_jll; print(LLVM_jll.artifact_dir)"`
echo "ARTIFACT_DIR=${ARTIFACT_DIR}"
# workaround https://github.com/JuliaPackaging/Yggdrasil/issues/652
sudo mkdir -p /workspace
sudo ln -s `pwd`/contrib/usr /workspace/destdir
sudo ln -s `pwd`/contrib/usr/tools/opt `pwd`/contrib/usr/bin/opt
sudo ln -s `pwd`/contrib/usr/tools/FileCheck `pwd`/contrib/usr/bin/FileCheck
sudo ln -s `pwd`/contrib/usr/tools/llvm-config `pwd`/contrib/usr/bin/llvm-config
sudo ln -s ${ARTIFACT_DIR} /workspace/destdir
sudo mkdir -p /workspace/destdir/bin/
sudo ln -s /workspace/destdir/tools/opt /workspace/destdir/bin/opt
sudo ln -s /workspace/destdir/tools/FileCheck /workspace/destdir/bin/FileCheck
sudo ln -s /workspace/destdir/tools/llvm-config /workspace/destdir/bin/llvm-config
env:
NO_DEPS: true
JULIA_PROJECT: "/tmp/proj"

- name: "Build Enzyme"
run: |
mkdir build
cd build
cmake --version
ls /home/runner/work/Enzyme/Enzyme/enzyme/Enzyme/
cmake -DLLVM_DIR=../contrib/usr/lib/cmake/llvm -DLLVM_EXTERNAL_LIT=../contrib/usr/tools/lit/lit.py ../enzyme
cmake -DLLVM_DIR=/workspace/destdir/lib/cmake/llvm \
-DLLVM_EXTERNAL_LIT=/workspace/destdir/tools/lit/lit.py ../enzyme
make -j
- name: "Julia tests"
run: |
cd ../Enzyme.jl
julia --project=. -e 'using Pkg; pkg"instantiate"; pkg"add ReverseDiff"'
julia --project=. test/runtests.jl
ENZYME_PATH=`pwd`/build/Enzyme julia -e 'using Pkg; pkg"test"'
env:
ENZYME_PATH: "../Enzyme/build/Enzyme"
- name: "Check Enzyme"
run: |
#cd build
#make check-enzyme
JULIA_PROJECT: "enzyme/julia"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.swp
*.swo
build
.vscode
21 changes: 21 additions & 0 deletions enzyme/julia/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2019 William Moses, Valentin Churavy, and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
14 changes: 14 additions & 0 deletions enzyme/julia/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name = "Enzyme"
uuid = "7da242da-08ed-463a-9acd-ee780be4f1d9"
authors = ["William Moses <wmoses@mit.edu>", "Valentin Churavy <vchuravy@mit.edu>"]
version = "0.1.0"

[deps]
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
MCAnalyzer = "a81df072-f4bb-11e8-03d3-cfaeda626d18"

[compat]
julia = "1.3"
LLVM = "1.3"
MCAnalyzer = "0.1"
108 changes: 108 additions & 0 deletions enzyme/julia/src/Enzyme.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
module Enzyme

export autodiff

using LLVM
using LLVM.Interop
import MCAnalyzer: irgen

include("utils.jl")
include("ad.jl")
include("opt.jl")

using .Opt: optimize!

function emit(f, args)
# Obtain the function and all it's dependencies in one handy module
diffetypes = []
autodifftypes = Type[f]
i = 1
while i <= length(args)
push!(autodifftypes, args[i])
dt = whatType(args[i])
push!(diffetypes, dt)
if dt == "diffe_dup"
i+=1
end
i+=1
end
mod, ccf = irgen(Tuple{autodifftypes...})

ctx = context(mod)
rettype = convert(LLVMType, Float64)

#argtypes2 = LLVMType[convert(LLVMType, T, true) for T in args]
argtypes2 = LLVMType[]

i = 1
j = 1
orig_params = parameters(ccf)
for p in orig_params
push!(argtypes2, llvmtype(p))
if diffetypes[i] == "diffe_dup"
push!(argtypes2, llvmtype(p))
i+=2
else
i+=1
end
end

# TODO get function type from ccf
ft2 = LLVM.FunctionType(rettype, argtypes2)

# create a wrapper Function that we will inline into the llvmcall
# generated by in the end `call_function`
llvmf = LLVM.Function(mod, "enzyme_entry", ft2)
push!(function_attributes(llvmf), EnumAttribute("alwaysinline", 0, ctx))

# Create the FunctionType and funtion decleration for the intrinsic
pt = LLVM.PointerType(LLVM.Int8Type(ctx))
ftd = LLVM.FunctionType(rettype, LLVMType[pt], true)
autodiff = LLVM.Function(mod, "__enzyme_autodiff", ftd)

params = LLVM.Value[]
i = 1
j = 1
llvm_params = parameters(llvmf)
while j <= length(args)
push!(params, MDString(diffetypes[i]))
if diffetypes[i] == "diffe_dup"
push!(params, llvm_params[j])
j+=1
end
push!(params, llvm_params[j])
j += 1
i += 1
end

Builder(ctx) do builder
entry = BasicBlock(llvmf, "entry", ctx)
position!(builder, entry)

tc = bitcast!(builder, ccf, pt)
pushfirst!(params, tc)

val = call!(builder, autodiff, params)

#if T === Nothing
# ret!(builder)
#else
ret!(builder, val)
#end
end

llvmf, mod
end

@generated function autodiff(f, args...)
llvmf, mod = emit(f, args)

# Run pipeline and Enzyme pass
optimize!(mod)
strip_debuginfo!(mod)

_args = (:(args[$i]) for i in 1:length(args))
call_function(llvmf, Float64, Tuple{args...}, Expr(:tuple, _args...))
end

end # module
66 changes: 66 additions & 0 deletions enzyme/julia/src/ad.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
@enum Diffe begin
Duplicate = 1
Output = 2
Constant = 3
end

function whatType(@nospecialize(dt))
if <:(dt, Array)
sub = whatType(eltype(dt))
if sub == "diffe_dup"
return "diffe_dup"
elseif sub == "diffe_out"
return "diffe_dup"
else
@assert(sub == "diffe_const")
return "diffe_const"
end
end
if <:(dt, Real)
return "diffe_out"
end
if <:(dt, Int)
return "diffe_const"
end
if <:(dt, String)
return "diffe_const"
end

if !hasfieldcount(dt)
# just be safe for now
return "diffe_dup"
end

@assert(hasfieldcount(dt))
@assert(isstructtype(dt))
passpointer = true
if passpointer
ty = "diffe_const"
for (ft, fn) in zip(fieldtypes(dt), fieldnames(dt))
sub = whatType(ft)
if sub == "diffe_dup"
ty = "diffe_dup"
elseif sub == "diffe_out"
ty = "diffe_dup"
else
@assert(sub == "diffe_const")
end
end
return ty
else
ty = "diffe_const"
for (ft, fn) in zip(fieldtypes(dt), fieldnames(dt))
sub = whatType(ft)
if sub == "diffe_dup"
ty = "diffe_dup"
elseif sub == "diffe_out"
if ty != "diffe_dup"
ty = "diffe_out"
end
else
@assert(sub == "diffe_const")
end
end
return ty
end
end
Loading