Skip to content

Commit

Permalink
adaption to Windows (#151)
Browse files Browse the repository at this point in the history
* adaption to windows: use clang to generate executable, skip GPUCompiler

*  a number of simplifications and bugfixes
---------

Co-authored-by: C. Brenhin Keller <cbkeller@dartmouth.edu>
  • Loading branch information
Thomas008 and brenhinkeller authored Mar 4, 2024
1 parent 1e6116b commit e672f35
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
version:
- '1.8'
- '1.9'
- '1.10.0-rc1'
- '1.10'
os:
- ubuntu-latest
- macOS-latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StaticCompiler"
uuid = "81625895-6c0f-48fc-b932-11a18313743c"
authors = ["Tom Short and contributors"]
version = "0.7.0"
version = "0.7.1"


[deps]
Expand Down
70 changes: 47 additions & 23 deletions src/StaticCompiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ compile_executable(f::Function, types::Tuple, path::String, [name::String=string
cflags=``, # Specify libraries you would like to link against, and other compiler options here
also_expose=[],
target::StaticTarget=StaticTarget(),
llvm_to_clang = Sys.iswindows(),
method_table=StaticCompiler.method_table,
kwargs...
)
Expand Down Expand Up @@ -98,17 +99,18 @@ shell> ./hello
Hello, world!
```
"""
function compile_executable(f::Function, types=(), path::String="./", name=fix_name(f);
function compile_executable(f::Function, types=(), path::String=pwd(), name=fix_name(f);
also_expose=Tuple{Function, Tuple{DataType}}[], target::StaticTarget=StaticTarget(),
kwargs...)
compile_executable(vcat([(f, types)], also_expose), path, name; target, kwargs...)
end

function compile_executable(funcs::Union{Array,Tuple}, path::String="./", name=fix_name(first(first(funcs)));
function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name=fix_name(first(first(funcs)));
filename = name,
demangle = true,
cflags = ``,
target::StaticTarget=StaticTarget(),
llvm_to_clang = Sys.iswindows(),
kwargs...
)

Expand All @@ -122,20 +124,20 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String="./", name=f
nativetype = isprimitivetype(rt) || isa(rt, Ptr)
nativetype || @warn "Return type `$rt` of `$f$types` does not appear to be a native type. Consider returning only a single value of a native machine type (i.e., a single float, int/uint, bool, or pointer). \n\nIgnoring this warning may result in Undefined Behavior!"

generate_executable(funcs, path, name, filename; demangle, cflags, target, kwargs...)
generate_executable(funcs, path, name, filename; demangle, cflags, target, llvm_to_clang, kwargs...)
joinpath(abspath(path), filename)
end

"""
```julia
compile_shlib(f::Function, types::Tuple, [path::String="./"], [name::String=string(nameof(f))];
compile_shlib(f::Function, types::Tuple, [path::String=pwd()], [name::String=string(nameof(f))];
filename::String=name,
cflags=``,
method_table=StaticCompiler.method_table,
target::StaticTarget=StaticTarget(),
kwargs...)
compile_shlib(funcs::Array, [path::String="./"];
compile_shlib(funcs::Array, [path::String=pwd()];
filename="libfoo",
demangle=true,
cflags=``,
Expand Down Expand Up @@ -172,15 +174,15 @@ julia> ccall(("test", "test.dylib"), Float64, (Int64,), 100_000)
5.2564961094956075
```
"""
function compile_shlib(f::Function, types=(), path::String="./", name=fix_name(f);
function compile_shlib(f::Function, types=(), path::String=pwd(), name=fix_name(f);
filename=name,
target::StaticTarget=StaticTarget(),
kwargs...
)
compile_shlib(((f, types),), path; filename, target, kwargs...)
end
# As above, but taking an array of functions and returning a single shlib
function compile_shlib(funcs::Union{Array,Tuple}, path::String="./";
function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd();
filename = "libfoo",
demangle = true,
cflags = ``,
Expand Down Expand Up @@ -289,10 +291,11 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi
demangle = true,
cflags = ``,
target::StaticTarget=StaticTarget(),
llvm_to_clang::Bool = Sys.iswindows(),
kwargs...
)
exec_path = joinpath(path, filename)
_, obj_path = generate_obj(funcs, path, filename; demangle, target, kwargs...)
_, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, kwargs...)
# Pick a compiler
if !isnothing(target.compiler)
cc = `$(target.compiler)`
Expand All @@ -301,10 +304,10 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi
end

# Compile!
if Sys.isapple()
if Sys.isapple() && !llvm_to_clang
# Apple no longer uses _start, so we can just specify a custom entry
entry = demangle ? "_$name" : "_julia_$name"
run(`$cc -e $entry $cflags $obj_path -o $exec_path`)
run(`$cc -e $entry $cflags $obj_or_ir_path -o $exec_path`)
else
fn = demangle ? "$name" : "julia_$name"
# Write a minimal wrapper to avoid having to specify a custom entry
Expand All @@ -319,9 +322,22 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi
return 0;
}""")
close(f)
run(`$cc $wrapper_path $cflags $obj_path -o $exec_path`)
if llvm_to_clang # (required on Windows)
# Use clang (llc) to generate an executable from the LLVM IR
cclang = if Sys.iswindows()
`cmd \c clang` # Not clear if the `cmd \c` is necessary
elseif Sys.isapple()
`clang`
else
clang()
end
run(`$cclang -Wno-override-module $wrapper_path $obj_or_ir_path -o $exec_path`)
else
run(`$cc $wrapper_path $cflags $obj_or_ir_path -o $exec_path`)
end

# Clean up
run(`rm $wrapper_path`)
rm(wrapper_path)
end
path, name
end
Expand Down Expand Up @@ -482,7 +498,6 @@ generate_obj(f, tt, path::String = tempname(), filenamebase::String="obj";
demangle = true,
strip_llvm = false,
strip_asm = true,
opt_level = 3,
kwargs...)
```
Low level interface for compiling object code (`.o`) for for function `f` given
Expand Down Expand Up @@ -519,10 +534,10 @@ end
```julia
generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj";
target::StaticTarget=StaticTarget(),
demangle =false,
demangle = false,
emit_llvm_only = false,
strip_llvm = false,
strip_asm = true,
opt_level=3,
kwargs...)
```
Low level interface for compiling object code (`.o`) for an array of Tuples
Expand All @@ -534,25 +549,34 @@ This is a struct of the type StaticTarget()
The defaults compile to the native target.
"""
function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj";
target::StaticTarget=StaticTarget(),
demangle = true,
emit_llvm_only = false,
strip_llvm = false,
strip_asm = true,
opt_level = 3,
target::StaticTarget=StaticTarget(),
kwargs...)
f, tt = funcs[1]
mkpath(path)
obj_path = joinpath(path, "$filenamebase.o")
mod = static_llvm_module(funcs; demangle, kwargs...)
obj = GPUCompiler.JuliaContext() do ctx

if emit_llvm_only # (Required on Windows)
ir_path = joinpath(path, "$filenamebase.ll")
open(ir_path, "w") do io
write(io, string(mod))
end
return path, ir_path
else
obj_path = joinpath(path, "$filenamebase.o")
obj = GPUCompiler.JuliaContext() do ctx
fakejob, _ = static_job(f, tt; target, kwargs...)
obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile)
obj
end
open(obj_path, "w") do io
write(io, obj)
end
return path, obj_path
end
open(obj_path, "w") do io
write(io, obj)
end
path, obj_path
end

end # module
10 changes: 10 additions & 0 deletions test/testcore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ end
@test isa(r, Base.Process)
@test r.exitcode == 0

filepath = compile_executable(foo, (), workdir, llvm_to_clang=true)
r = run(`$filepath`);
@test isa(r, Base.Process)
@test r.exitcode == 0


@inline function _puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates
Base.llvmcall(("""
Expand Down Expand Up @@ -85,6 +90,11 @@ end
@test isa(r, Base.Process)
@test r.exitcode == 0

filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), workdir, llvm_to_clang=true)
r = run(`$filepath Hello, world!`);
@test isa(r, Base.Process)
@test r.exitcode == 0


# Compile a function that definitely fails
@inline foo_err() = UInt64(-1)
Expand Down

2 comments on commit e672f35

@brenhinkeller
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/102255

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.1 -m "<description of version>" e672f352352097108e3049383e1edfcf508c57af
git push origin v0.7.1

Please sign in to comment.