From b60722b4de500a68a913a03eda8b838d953fb189 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sat, 6 Mar 2021 07:55:06 -0500 Subject: [PATCH 001/202] Basic rewrite --- Project.toml | 11 ++------ README.md | 40 ++-------------------------- src/StaticCompiler.jl | 62 ++++++++++++++++++++++++++++--------------- test/runtests.jl | 17 +++--------- 4 files changed, 47 insertions(+), 83 deletions(-) diff --git a/Project.toml b/Project.toml index 583ae233..e7c13441 100644 --- a/Project.toml +++ b/Project.toml @@ -4,19 +4,12 @@ authors = ["Tom Short"] version = "0.1.0" [deps] -Cassette = "7057c7e9-c182-5462-911a-8362d720325c" -DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" +LLVM_full_jll = "a3ccf953-465e-511d-b87f-60a6490c289d" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" -MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" -TypedCodeUtils = "687fb87b-adea-59d5-9be9-82253b54685d" [compat] -DataStructures = "0.17" -Cassette = "0.3" -LLVM = "1.3" -TypedCodeUtils = "0.1" -MacroTools = "0.5" julia = "1.2" [extras] diff --git a/README.md b/README.md index 8b09b28b..5b0ab32b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. It is also meant for cross compilation, so Julia code can be compiled for other targets, including WebAssembly and embedded targets. ## Installation and Usage + ```julia using Pkg Pkg.add(PackageSpec( url = "https://github.com/tshort/StaticCompiler.jl", rev = "master")) @@ -16,45 +17,8 @@ Pkg.add(PackageSpec( url = "https://github.com/tshort/StaticCompiler.jl", rev = ```julia using StaticCompiler ``` -**Documentation**: [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://tshort.github.io/StaticCompiler.jl/dev) ## Approach -This package uses the [LLVM package](https://github.com/maleadt/LLVM.jl) to generate code in the same fashion as [CUDAnative](https://github.com/JuliaGPU/CUDAnative.jl). - -Some of the key details of this approach are: - -* **ccalls and cglobal** -- When Julia compiles code CUDAnative style, `ccall` and `cglobal` references get compiled to a direct pointer. `StaticCompiler` converts these to symbol references for later linking. For `ccall` with a tuple call to a symbol in a library, `Cassette` is used to convert that to just a symbol reference (no dynamic library loading). - -* **Global variables** -- A lot of code gets compiled with global variables, and these get compiled to a direct pointer. `StaticCompiler` includes a basic serialize/deserialize approach. Right now, this is fairly basic, and it takes shortcuts for some objects by swapping in wrong types. This can work because many times, the objects are not really used in the code. Finding the global variable can be a little tricky because the pointer is converted to a Julia object with `unsafe_pointer_to_objref`, and that segfaults for some addresses. How to best handle cases like that is still to be determined. - -* **Initialization** -- If libjulia is used, some init code needs to be run to set up garbage collection and other things. For this, a basic `blank.ji` file is used to feed `jl_init_with_image`. - -Long term, a better approach may be to use Julia's standard compilation techniques with "tree shaking" to generate a reduced system image (see [here](https://github.com/JuliaLang/julia/issues/33670)). - -## Example -The API still needs work, but here is the general approach right now: - -```julia -using StaticCompiler -m = irgen(cos, Tuple{Float64}) -write(m, "cos.bc") -write_object(m, "cos.o") -``` - -`cos.o` should contain a function called `cos`. From there, you need to convert to link as needed with `libjulia`. - -See the `test` directory for more information and types of code that currently run. The most advanced example that works is a call to an ODE solution using modified code from [ODE.jl](https://github.com/JuliaDiffEq/ODE.jl). For information on compiling and linking to an executable, see [test/standalone-exe.jl](./test/standalone-exe.jl). - -## Known limitations - -* It won't work for recursive code. Jameson's [codegen-norecursion](https://github.com/JuliaLang/julia/tree/jn/codegen-norecursion) should fix that when merged. - -* `cfunction` is not supported. - -* Generic code that uses `jl_apply_generic` does not work. One strategy for this is to use Cassette to swap out known code that uses dynamic calls. Another approach is to write something like `jl_apply_generic` to implement dynamic calls. - -* The use of Cassette makes it more difficult for Julia to infer some things, and only type-stable code can be statically compiled with this approach. -* It's only been tested on Linux and Windows. +This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. -Finally, this whole approach is young and likely brittle. Do not expect it to work for your code. diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 5f16b621..6c3f0f16 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -1,28 +1,46 @@ module StaticCompiler -export irgen, write_object, @extern - +import GPUCompiler +import LLVM +import LLVM_full_jll import Libdl -using LLVM -using LLVM.Interop -using TypedCodeUtils -import TypedCodeUtils: reflect, lookthrough, canreflect, - DefaultConsumer, Reflection, Callsite, - identify_invoke, identify_call, identify_foreigncall, - process_invoke, process_call -using MacroTools -using DataStructures: MultiDict - - -include("serialize.jl") -include("utils.jl") -include("ccalls.jl") -include("globals.jl") -include("overdub.jl") -include("irgen.jl") -include("extern.jl") - -include("helpers/helpers.jl") +export generate_shlib_fptr + +module TestRuntime + # dummy methods + signal_exception() = return + malloc(sz) = C_NULL + report_oom(sz) = return + report_exception(ex) = return + report_exception_name(ex) = return + report_exception_frame(idx, func, file, line) = return + + # for validation + sin(x) = Base.sin(x) +end + +struct TestCompilerParams <: GPUCompiler.AbstractCompilerParams end +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,TestCompilerParams}) = TestRuntime + +function generate_shlib_fptr(f, tt, name = GPUCompiler.safe_name(repr(f))) + mktemp() do path, io + target = GPUCompiler.NativeCompilerTarget(;reloc=LLVM.API.LLVMRelocPIC, extern=true) + source = GPUCompiler.FunctionSpec(f, Base.to_tuple_type(tt), false, name) + params = TestCompilerParams() + job = GPUCompiler.CompilerJob(target, source, params) + obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) + write(io, obj) + flush(io) + # FIXME: Be more portable + run(`ld -shared -o $path.$(Libdl.dlext) $path`) + ptr = Libdl.dlopen("$path.$(Libdl.dlext)", Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_$name") + @assert fptr != C_NULL + atexit(()->rm("$path.$(Libdl.dlext)")) + fptr + end +end + end # module diff --git a/test/runtests.jl b/test/runtests.jl index 3381a873..24470e04 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,21 +1,10 @@ using StaticCompiler using Test -using LLVM -using Libdl -cd(@__DIR__) -@testset "ccalls" begin - include("ccalls.jl") -end -@testset "globals" begin - include("globals.jl") -end -@testset "others" begin - include("others.jl") +@testset "basics" begin + f1(x) = x+1 + @test ccall(generate_shlib_fptr(f1, (Int,)), Int, (Int,), 1) == 2 end -@testset "standalone" begin - include("standalone-exe.jl") -end From d3b8f5aa50dea700cf58aafaf43f08eebeea2cb2 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sat, 6 Mar 2021 09:18:56 -0500 Subject: [PATCH 002/202] Include the Manifest --- .gitignore | 3 - Manifest.toml | 211 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 Manifest.toml diff --git a/.gitignore b/.gitignore index 2ef6f28f..6e716b01 100644 --- a/.gitignore +++ b/.gitignore @@ -2,11 +2,8 @@ *.jl.cov *.jl.mem .DS_Store -Manifest.toml -!helpers/Manifest.toml /dev/ /test/standalone -/test/Manifest.toml /test/test.* test.o diff --git a/Manifest.toml b/Manifest.toml new file mode 100644 index 00000000..5808f596 --- /dev/null +++ b/Manifest.toml @@ -0,0 +1,211 @@ +# This file is machine-generated - editing it directly is not advised + +[[ArgTools]] +uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" + +[[Artifacts]] +uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" + +[[Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[CEnum]] +git-tree-sha1 = "215a9aa4a1f23fbd05b92769fdd62559488d70e9" +uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82" +version = "0.4.1" + +[[Compat]] +deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] +git-tree-sha1 = "919c7f3151e79ff196add81d7f4e45d91bbf420b" +uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" +version = "3.25.0" + +[[DataStructures]] +deps = ["Compat", "InteractiveUtils", "OrderedCollections"] +git-tree-sha1 = "4437b64df1e0adccc3e5d1adbc3ac741095e4677" +uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +version = "0.18.9" + +[[Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[DelimitedFiles]] +deps = ["Mmap"] +uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" + +[[Distributed]] +deps = ["Random", "Serialization", "Sockets"] +uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" + +[[Downloads]] +deps = ["ArgTools", "LibCURL", "NetworkOptions"] +uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" + +[[ExprTools]] +git-tree-sha1 = "10407a39b87f29d47ebaca8edbc75d7c302ff93e" +uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04" +version = "0.1.3" + +[[GPUCompiler]] +deps = ["DataStructures", "ExprTools", "InteractiveUtils", "LLVM", "Libdl", "Logging", "Scratch", "Serialization", "TimerOutputs", "UUIDs"] +git-tree-sha1 = "7db5fa8c3f7da5ef25ba5740802b19a87c962a02" +repo-rev = "jps/static-compile" +repo-url = "https://github.com/JuliaGPU/GPUCompiler.jl.git" +uuid = "61eb1bfa-7361-4325-ad38-22787b887f55" +version = "0.10.1" + +[[InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[JLLWrappers]] +git-tree-sha1 = "a431f5f2ca3f4feef3bd7a5e94b8b8d4f2f647a0" +uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" +version = "1.2.0" + +[[LLVM]] +deps = ["CEnum", "Libdl", "Printf", "Unicode"] +git-tree-sha1 = "b616937c31337576360cb9fb872ec7633af7b194" +uuid = "929cbde3-209d-540e-8aea-75f648917ca0" +version = "3.6.0" + +[[LLVM_full_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "bbd0d3ef31d8e819035537227b0e6cac8c900913" +uuid = "a3ccf953-465e-511d-b87f-60a6490c289d" +version = "11.0.1+3" + +[[LibCURL]] +deps = ["LibCURL_jll", "MozillaCACerts_jll"] +uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" + +[[LibCURL_jll]] +deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] +uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" + +[[LibGit2]] +deps = ["Base64", "NetworkOptions", "Printf", "SHA"] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" + +[[LibSSH2_jll]] +deps = ["Artifacts", "Libdl", "MbedTLS_jll"] +uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" + +[[Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[LinearAlgebra]] +deps = ["Libdl", "libblastrampoline_jll"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[[Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[MbedTLS_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" + +[[Mmap]] +uuid = "a63ad114-7e13-5084-954f-fe012c677804" + +[[MozillaCACerts_jll]] +uuid = "14a3606d-f60d-562e-9121-12d972cd8159" + +[[NetworkOptions]] +uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" + +[[OrderedCollections]] +git-tree-sha1 = "4fa2ba51070ec13fcc7517db714445b4ab986bdf" +uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +version = "1.4.0" + +[[Pkg]] +deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" + +[[Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[REPL]] +deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] +uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" + +[[Random]] +deps = ["Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" + +[[Scratch]] +deps = ["Dates"] +git-tree-sha1 = "ad4b278adb62d185bbcb6864dc24959ab0627bf6" +uuid = "6c6a2e73-6563-6170-7368-637461726353" +version = "1.0.3" + +[[Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[SharedArrays]] +deps = ["Distributed", "Mmap", "Random", "Serialization"] +uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" + +[[Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[SparseArrays]] +deps = ["LinearAlgebra", "Random"] +uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[[Statistics]] +deps = ["LinearAlgebra", "SparseArrays"] +uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + +[[TOML]] +deps = ["Dates"] +uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" + +[[Tar]] +deps = ["ArgTools", "SHA"] +uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" + +[[Test]] +deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[[TimerOutputs]] +deps = ["Printf"] +git-tree-sha1 = "32cdbe6cd2d214c25a0b88f985c9e0092877c236" +uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" +version = "0.5.8" + +[[UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[[Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[Zlib_jll]] +deps = ["Libdl"] +uuid = "83775a58-1f1d-513f-b197-d71354ab007a" + +[[libblastrampoline_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "411ee75a5364426ef54afa88482ca84f35937923" +uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" +version = "3.0.2+0" + +[[nghttp2_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" + +[[p7zip_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" From 04115fab38c52bbcc2546bb7e140dad631382fa7 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sat, 6 Mar 2021 09:30:48 -0500 Subject: [PATCH 003/202] Use LLVM's linker --- src/StaticCompiler.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 6c3f0f16..b210544c 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -23,6 +23,8 @@ end struct TestCompilerParams <: GPUCompiler.AbstractCompilerParams end GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,TestCompilerParams}) = TestRuntime +const linker = Sys.isunix() ? "ld.lld" : Sys.isapple() ? "ld64.lld" : "lld-link" + function generate_shlib_fptr(f, tt, name = GPUCompiler.safe_name(repr(f))) mktemp() do path, io target = GPUCompiler.NativeCompilerTarget(;reloc=LLVM.API.LLVMRelocPIC, extern=true) @@ -33,7 +35,8 @@ function generate_shlib_fptr(f, tt, name = GPUCompiler.safe_name(repr(f))) write(io, obj) flush(io) # FIXME: Be more portable - run(`ld -shared -o $path.$(Libdl.dlext) $path`) + # run(`ld -shared -o $path.$(Libdl.dlext) $path`) + run(`$(StaticCompiler.LLVM_full_jll.PATH)/$linker -shared -o $path.$(Libdl.dlext) $path`) ptr = Libdl.dlopen("$path.$(Libdl.dlext)", Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @assert fptr != C_NULL From 73413c8107954c0a5000c34e07db82d2d5f73e27 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sat, 6 Mar 2021 09:33:51 -0500 Subject: [PATCH 004/202] Fixup CI and compatibility --- .github/workflows/ci.yml | 3 +-- .travis.yml | 1 - Project.toml | 4 ++-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cb97e170..b9510c3c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,11 +10,10 @@ jobs: fail-fast: false matrix: version: - - '1.3' - 'nightly' os: - ubuntu-latest - # - macOS-latest + - macOS-latest - windows-latest arch: - x64 diff --git a/.travis.yml b/.travis.yml index d735ad52..9011d242 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,6 @@ os: - linux - windows julia: - - 1.3 - nightly matrix: # extra linux test diff --git a/Project.toml b/Project.toml index e7c13441..098e7e20 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short"] -version = "0.1.0" +version = "0.2.0" [deps] GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" @@ -10,7 +10,7 @@ LLVM_full_jll = "a3ccf953-465e-511d-b87f-60a6490c289d" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" [compat] -julia = "1.2" +julia = "1.7" [extras] Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" From 5db52a25c512cc7b8fa4c4268e67a9ab2c1f1c17 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sat, 6 Mar 2021 09:39:57 -0500 Subject: [PATCH 005/202] Cleanup --- src/StaticCompiler.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index b210544c..74b8076a 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -34,8 +34,6 @@ function generate_shlib_fptr(f, tt, name = GPUCompiler.safe_name(repr(f))) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) write(io, obj) flush(io) - # FIXME: Be more portable - # run(`ld -shared -o $path.$(Libdl.dlext) $path`) run(`$(StaticCompiler.LLVM_full_jll.PATH)/$linker -shared -o $path.$(Libdl.dlext) $path`) ptr = Libdl.dlopen("$path.$(Libdl.dlext)", Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") From aa9eef57601fcb941e8b525bb7d35a2494a74198 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sat, 6 Mar 2021 09:49:13 -0500 Subject: [PATCH 006/202] Add `compile` to return an LLVM module --- src/StaticCompiler.jl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 74b8076a..b1fe6d75 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -5,7 +5,7 @@ import LLVM import LLVM_full_jll import Libdl -export generate_shlib_fptr +export generate_shlib_fptr, compile module TestRuntime # dummy methods @@ -44,4 +44,15 @@ function generate_shlib_fptr(f, tt, name = GPUCompiler.safe_name(repr(f))) end +# Return an LLVM module +function compile(f, tt, name = GPUCompiler.safe_name(repr(f))) + target = GPUCompiler.NativeCompilerTarget(;reloc=LLVM.API.LLVMRelocPIC, extern=true) + source = GPUCompiler.FunctionSpec(f, Base.to_tuple_type(tt), false, name) + params = TestCompilerParams() + job = GPUCompiler.CompilerJob(target, source, params) + m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + return m +end + + end # module From ecc1b8cb547c6e66ff24a3b4b8479163f786b705 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sun, 7 Mar 2021 07:08:46 -0500 Subject: [PATCH 007/202] Split up methods (Mason's suggestion) --- README.md | 16 ++++++++++++++++ src/StaticCompiler.jl | 28 ++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5b0ab32b..48a0e2b6 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,24 @@ This is an experimental package to compile Julia code to standalone libraries. A using Pkg Pkg.add(PackageSpec( url = "https://github.com/tshort/StaticCompiler.jl", rev = "master")) ``` + ```julia using StaticCompiler +f(x) = 2x + +# compile `f` and return an LLVM module +m = compile(f, (Int,)) + +# compile `f` and write to a shared library ("f.so" or "f.dll") +generate_shlib(f, (Int,), "libf") +# find a function pointer for this shared library +fptr = generate_shlib_fptr("libf", "f") +ccall(fptr, Int, (Int,), 2) + +# do this in one step (this time with a temporary shared library) +fptr = generate_shlib_fptr(f, (Int,)) +ccall(fptr, Int, (Int,), 2) + ``` ## Approach diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index b1fe6d75..ad044a2d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -5,7 +5,7 @@ import LLVM import LLVM_full_jll import Libdl -export generate_shlib_fptr, compile +export generate_shlib, generate_shlib_fptr, compile module TestRuntime # dummy methods @@ -25,8 +25,8 @@ GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,TestCompilerParams}) const linker = Sys.isunix() ? "ld.lld" : Sys.isapple() ? "ld64.lld" : "lld-link" -function generate_shlib_fptr(f, tt, name = GPUCompiler.safe_name(repr(f))) - mktemp() do path, io +function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f))) + open(path, "w") do io target = GPUCompiler.NativeCompilerTarget(;reloc=LLVM.API.LLVMRelocPIC, extern=true) source = GPUCompiler.FunctionSpec(f, Base.to_tuple_type(tt), false, name) params = TestCompilerParams() @@ -35,14 +35,26 @@ function generate_shlib_fptr(f, tt, name = GPUCompiler.safe_name(repr(f))) write(io, obj) flush(io) run(`$(StaticCompiler.LLVM_full_jll.PATH)/$linker -shared -o $path.$(Libdl.dlext) $path`) - ptr = Libdl.dlopen("$path.$(Libdl.dlext)", Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "julia_$name") - @assert fptr != C_NULL + rm(path) + end + path, name +end +function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)); temp::Bool=true) + generate_shlib(f, tt, path, name) + ptr = Libdl.dlopen("$(abspath(path)).$(Libdl.dlext)", Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_$name") + @assert fptr != C_NULL + if temp atexit(()->rm("$path.$(Libdl.dlext)")) - fptr end + fptr +end +function generate_shlib_fptr(path::String, name) + ptr = Libdl.dlopen("$(abspath(path)).$(Libdl.dlext)", Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_$name") + @assert fptr != C_NULL + fptr end - # Return an LLVM module function compile(f, tt, name = GPUCompiler.safe_name(repr(f))) From b1ec2e5cbadad8c123bb123c4704113e2233c068 Mon Sep 17 00:00:00 2001 From: Jorge Alberto Vieyra Salas Date: Sun, 7 Mar 2021 14:12:15 +0100 Subject: [PATCH 008/202] Added simple tests to start testing. Most of them segfault. --- test/runtests.jl | 115 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 3 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 24470e04..96e0d274 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,10 +1,119 @@ using StaticCompiler using Test +using Libdl +using LinearAlgebra +@testset "Basics" begin + simple_sum(x) = x + one(typeof(x)) + + # This probably needs a macro + @test ccall(generate_shlib_fptr(simple_sum, (Int,)), Int, (Int,), 1) == Int(2) + @test ccall(generate_shlib_fptr(simple_sum, (Float64,)), Float64, (Float64 ,), 1) == Float64(2) + + @test ccall(generate_shlib_fptr(simple_sum, (Int32,)), Int32, (Int32,), 1) == Int32(2) + @test ccall(generate_shlib_fptr(simple_sum, (Float32,)), Float32, (Float32 ,), 1) == Float16(2) + + @test ccall(generate_shlib_fptr(simple_sum, (Int16,)), Int16, (Int16,), 1) == Int16(2) + @test ccall(generate_shlib_fptr(simple_sum, (Float16,)), Float16, (Float16 ,), 1) == Float16(2) -@testset "basics" begin - f1(x) = x+1 - @test ccall(generate_shlib_fptr(f1, (Int,)), Int, (Int,), 1) == 2 end +@testset "Recursion" begin + fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) + # This works on the REPL but fails here + @test_skip ccall(generate_shlib_fptr(fib, (Int,)), Int, (Int,), 10) == 55 +end + +# Call binaries for testing +# Need Mason's code +# @testset "Generate binary" begin +# fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) +# libname = tempname() * "." * Libdl.dlext +# generate_shlib(fib, (Int,), libname) +# ptr = Libdl.dlopen(libname, Libdl.RTLD_LOCAL) +# fptr = Libdl.dlsym(ptr, "julia_fib") +# @assert fptr != C_NULL +# @test_skip ccall(fptr, Int, (Int,), 10) == 55 +# end + +@testset "Loops" begin + function sum_first_N_int(N) + s = 0 + for a in 1:N + s += a + end + s + end + @test ccall(generate_shlib_fptr(sum_first_N_int, (Int,)), Int, (Int,), 10) == 55 + + function sum_first_N_float64(N) + s = Float64(0) + for a in 1:N + s += Float64(a) + end + s + end + @test ccall(generate_shlib_fptr(sum_first_N_float64, (Int,)), Float64, (Int,), 10) == 55. + + function sum_first_N_int_inbounds(N) + s = 0 + @inbounds for a in 1:N + s += a + end + s + end + @test ccall(generate_shlib_fptr(sum_first_N_int_inbounds, (Int,)), Int, (Int,), 10) == 55 + + + function sum_first_N_float64_inbounds(N) + s = Float64(0) + @inbounds for a in 1:N + s += Float64(a) + end + s + end + @test ccall(generate_shlib_fptr(sum_first_N_float64_inbounds, (Int,)), Float64, (Int,), 10) == 55. + +end + +# Arrays with different input types Int32, Int64, Float32, Float64, Complex? +@testset "Arrays" begin + + arr = collect(1:10) + function array_sum(n, A) + s = zero(eltype(A)) + for i in 1:n + s += A[i] + end + s + end + + #This segfaults, not sure if this is how you pass around arrays + @test_skip ccall(generate_shlib_fptr(array_sum, (Csize_t, Ptr{Float64})), Int, (Csize_t, Ptr{Float64}), length(arr), arr) == 55 + +end + +# Just to call external libraries +@testset "BLAS" begin + function mydot(N) + a = Float64.(1:N) + BLAS.dot(N, a, 1, a, 1) + end + @test_skip ccall(generate_shlib_fptr(mydot, (Int,)), Float64, (Int,), 2) == 5. +end + +@testset "Hello World" begin + function hello(N) + println("Hello World $N") + N + end + # How do I test this? + # Also ... this segfaults + @test_skip ccall(generate_shlib_fptr(hello, (Int,)), Int, (Int,), 1) == 1 +end + + +# data structures, dictionaries, tuples, named tuples +# passing pointers? +# @inbounds LoopVectorization From dc83554f07e6ce60a8253a3ebf3d169e091aa1ba Mon Sep 17 00:00:00 2001 From: Jorge Alberto Vieyra Salas Date: Sun, 7 Mar 2021 14:24:52 +0100 Subject: [PATCH 009/202] Added simple tests to start testing. Most of them segfault. --- test/runtests.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 96e0d274..731caa06 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -26,16 +26,16 @@ end end # Call binaries for testing -# Need Mason's code -# @testset "Generate binary" begin -# fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) -# libname = tempname() * "." * Libdl.dlext -# generate_shlib(fib, (Int,), libname) -# ptr = Libdl.dlopen(libname, Libdl.RTLD_LOCAL) -# fptr = Libdl.dlsym(ptr, "julia_fib") -# @assert fptr != C_NULL -# @test_skip ccall(fptr, Int, (Int,), 10) == 55 -# end +@testset "Generate binary" begin + fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) + libname = tempname() + generate_shlib(fib, (Int,), libname) + ptr = Libdl.dlopen(libname * "." * Libdl.dlext, Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_fib") + @assert fptr != C_NULL + # This works on REPL + @test_skip ccall(fptr, Int, (Int,), 10) == 55 +end @testset "Loops" begin function sum_first_N_int(N) From 00772b5c40a1a4191f9b2bcb0b1c2096aed56630 Mon Sep 17 00:00:00 2001 From: Jorge Alberto Vieyra Salas Date: Sun, 7 Mar 2021 14:48:00 +0100 Subject: [PATCH 010/202] Commented the Generate binary test, because it was completly broken --- test/runtests.jl | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 731caa06..b721ea63 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -26,16 +26,17 @@ end end # Call binaries for testing -@testset "Generate binary" begin - fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) - libname = tempname() - generate_shlib(fib, (Int,), libname) - ptr = Libdl.dlopen(libname * "." * Libdl.dlext, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "julia_fib") - @assert fptr != C_NULL - # This works on REPL - @test_skip ccall(fptr, Int, (Int,), 10) == 55 -end +# @testset "Generate binary" begin +# fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) +# libname = tempname() +# generate_shlib(fib, (Int,), libname) +# ptr = Libdl.dlopen(libname * "." * Libdl.dlext, Libdl.RTLD_LOCAL) +# fptr = Libdl.dlsym(ptr, "julia_fib") +# @assert fptr != C_NULL +# # This works on REPL +# @test_skip ccall(fptr, Int, (Int,), 10) == 55 +# end + @testset "Loops" begin function sum_first_N_int(N) From 23831ae8c1f741ae2f6626c5aec922c6982cc8c5 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Tue, 1 Feb 2022 18:04:38 -0700 Subject: [PATCH 011/202] update, remove junk, fix tests, use `gcc` for linking --- .appveyor.yml | 31 ---- .travis.yml | 23 --- Manifest.toml | 179 +++++++------------- Project.toml | 3 +- README.md | 12 +- docs/src/backend.md | 2 +- docs/src/helpers.md | 23 --- docs/src/index.md | 64 -------- src/StaticCompiler.jl | 15 +- src/ccalls.jl | 90 ---------- src/extern.jl | 16 -- src/globals.jl | 164 ------------------- src/helpers/README.md | 12 -- src/helpers/helpers.jl | 37 ----- src/helpers/jlrun.jl | 61 ------- src/helpers/juliaconfig.jl | 63 ------- src/helpers/standalone-exe.jl | 148 ----------------- src/irgen.jl | 300 ---------------------------------- src/overdub.jl | 33 ---- src/serialize.jl | 242 --------------------------- src/utils.jl | 56 ------- test/ccalls.jl | 35 ---- test/globals.jl | 36 ---- test/ode.jl | 182 --------------------- test/others.jl | 73 --------- test/runtests.jl | 28 +++- test/standalone-exe.jl | 41 ----- 27 files changed, 102 insertions(+), 1867 deletions(-) delete mode 100644 .appveyor.yml delete mode 100644 .travis.yml delete mode 100644 src/ccalls.jl delete mode 100644 src/extern.jl delete mode 100644 src/globals.jl delete mode 100644 src/helpers/README.md delete mode 100644 src/helpers/helpers.jl delete mode 100644 src/helpers/jlrun.jl delete mode 100644 src/helpers/juliaconfig.jl delete mode 100644 src/helpers/standalone-exe.jl delete mode 100644 src/irgen.jl delete mode 100644 src/overdub.jl delete mode 100644 src/serialize.jl delete mode 100644 src/utils.jl delete mode 100644 test/ccalls.jl delete mode 100644 test/globals.jl delete mode 100644 test/ode.jl delete mode 100644 test/others.jl delete mode 100644 test/standalone-exe.jl diff --git a/.appveyor.yml b/.appveyor.yml deleted file mode 100644 index 16bb9b02..00000000 --- a/.appveyor.yml +++ /dev/null @@ -1,31 +0,0 @@ -# Documentation: https://github.com/JuliaCI/Appveyor.jl -environment: - matrix: - - julia_version: 1.3 - - julia_version: nightly -platform: - - x86 - - x64 -matrix: - allow_failures: - - julia_version: nightly -branches: - only: - - master - - /release-.*/ -notifications: - - provider: Email - on_build_success: false - on_build_failure: false - on_build_status_changed: false -install: - - ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1")) -build_script: - - echo "%JL_BUILD_SCRIPT%" - - C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%" -test_script: - - echo "%JL_TEST_SCRIPT%" - - C:\julia\bin\julia -e "%JL_TEST_SCRIPT%" -on_success: - - echo "%JL_CODECOV_SCRIPT%" - - C:\julia\bin\julia -e "%JL_CODECOV_SCRIPT%" diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 9011d242..00000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -# Documentation: http://docs.travis-ci.com/user/languages/julia/ -language: julia -os: -- linux -- windows -julia: - - nightly -matrix: - # extra linux test - include: - - os: linux - dist: bionic - - allow_failures: - - julia: nightly - fast_finish: true - -notifications: - email: false - -after_success: - - julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())' - - julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder())' diff --git a/Manifest.toml b/Manifest.toml index 5808f596..44dbe949 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -1,211 +1,158 @@ # This file is machine-generated - editing it directly is not advised -[[ArgTools]] +manifest_format = "2.0" + +[[deps.ArgTools]] uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" -[[Artifacts]] +[[deps.Artifacts]] uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" -[[Base64]] +[[deps.Base64]] uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" -[[CEnum]] +[[deps.CEnum]] git-tree-sha1 = "215a9aa4a1f23fbd05b92769fdd62559488d70e9" uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82" version = "0.4.1" -[[Compat]] -deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] -git-tree-sha1 = "919c7f3151e79ff196add81d7f4e45d91bbf420b" -uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "3.25.0" - -[[DataStructures]] -deps = ["Compat", "InteractiveUtils", "OrderedCollections"] -git-tree-sha1 = "4437b64df1e0adccc3e5d1adbc3ac741095e4677" -uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" -version = "0.18.9" - -[[Dates]] +[[deps.Dates]] deps = ["Printf"] uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" -[[DelimitedFiles]] -deps = ["Mmap"] -uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" - -[[Distributed]] -deps = ["Random", "Serialization", "Sockets"] -uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" - -[[Downloads]] +[[deps.Downloads]] deps = ["ArgTools", "LibCURL", "NetworkOptions"] uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" -[[ExprTools]] -git-tree-sha1 = "10407a39b87f29d47ebaca8edbc75d7c302ff93e" +[[deps.ExprTools]] +git-tree-sha1 = "56559bbef6ca5ea0c0818fa5c90320398a6fbf8d" uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04" -version = "0.1.3" +version = "0.1.8" -[[GPUCompiler]] -deps = ["DataStructures", "ExprTools", "InteractiveUtils", "LLVM", "Libdl", "Logging", "Scratch", "Serialization", "TimerOutputs", "UUIDs"] -git-tree-sha1 = "7db5fa8c3f7da5ef25ba5740802b19a87c962a02" -repo-rev = "jps/static-compile" -repo-url = "https://github.com/JuliaGPU/GPUCompiler.jl.git" +[[deps.GPUCompiler]] +deps = ["ExprTools", "InteractiveUtils", "LLVM", "Libdl", "Logging", "TimerOutputs", "UUIDs"] +git-tree-sha1 = "abd824e1f2ecd18d33811629c781441e94a24e81" uuid = "61eb1bfa-7361-4325-ad38-22787b887f55" -version = "0.10.1" +version = "0.13.11" -[[InteractiveUtils]] +[[deps.InteractiveUtils]] deps = ["Markdown"] uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" -[[JLLWrappers]] -git-tree-sha1 = "a431f5f2ca3f4feef3bd7a5e94b8b8d4f2f647a0" +[[deps.JLLWrappers]] +deps = ["Preferences"] +git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" -version = "1.2.0" +version = "1.4.1" -[[LLVM]] -deps = ["CEnum", "Libdl", "Printf", "Unicode"] -git-tree-sha1 = "b616937c31337576360cb9fb872ec7633af7b194" +[[deps.LLVM]] +deps = ["CEnum", "LLVMExtra_jll", "Libdl", "Printf", "Unicode"] +git-tree-sha1 = "f8dcd7adfda0dddaf944e62476d823164cccc217" uuid = "929cbde3-209d-540e-8aea-75f648917ca0" -version = "3.6.0" +version = "4.7.1" -[[LLVM_full_jll]] +[[deps.LLVMExtra_jll]] deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "bbd0d3ef31d8e819035537227b0e6cac8c900913" -uuid = "a3ccf953-465e-511d-b87f-60a6490c289d" -version = "11.0.1+3" +git-tree-sha1 = "62115afed394c016c2d3096c5b85c407b48be96b" +uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab" +version = "0.0.13+1" -[[LibCURL]] +[[deps.LibCURL]] deps = ["LibCURL_jll", "MozillaCACerts_jll"] uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" -[[LibCURL_jll]] +[[deps.LibCURL_jll]] deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" -[[LibGit2]] +[[deps.LibGit2]] deps = ["Base64", "NetworkOptions", "Printf", "SHA"] uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" -[[LibSSH2_jll]] +[[deps.LibSSH2_jll]] deps = ["Artifacts", "Libdl", "MbedTLS_jll"] uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" -[[Libdl]] +[[deps.Libdl]] uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" -[[LinearAlgebra]] -deps = ["Libdl", "libblastrampoline_jll"] -uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" - -[[Logging]] +[[deps.Logging]] uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" -[[Markdown]] +[[deps.Markdown]] deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" -[[MbedTLS_jll]] +[[deps.MbedTLS_jll]] deps = ["Artifacts", "Libdl"] uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" -[[Mmap]] -uuid = "a63ad114-7e13-5084-954f-fe012c677804" - -[[MozillaCACerts_jll]] +[[deps.MozillaCACerts_jll]] uuid = "14a3606d-f60d-562e-9121-12d972cd8159" -[[NetworkOptions]] +[[deps.NetworkOptions]] uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" -[[OrderedCollections]] -git-tree-sha1 = "4fa2ba51070ec13fcc7517db714445b4ab986bdf" -uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" -version = "1.4.0" - -[[Pkg]] +[[deps.Pkg]] deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" -[[Printf]] +[[deps.Preferences]] +deps = ["TOML"] +git-tree-sha1 = "2cf929d64681236a2e074ffafb8d568733d2e6af" +uuid = "21216c6a-2e73-6563-6e65-726566657250" +version = "1.2.3" + +[[deps.Printf]] deps = ["Unicode"] uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" -[[REPL]] +[[deps.REPL]] deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" -[[Random]] -deps = ["Serialization"] +[[deps.Random]] +deps = ["SHA", "Serialization"] uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" -[[SHA]] +[[deps.SHA]] uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" -[[Scratch]] -deps = ["Dates"] -git-tree-sha1 = "ad4b278adb62d185bbcb6864dc24959ab0627bf6" -uuid = "6c6a2e73-6563-6170-7368-637461726353" -version = "1.0.3" - -[[Serialization]] +[[deps.Serialization]] uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" -[[SharedArrays]] -deps = ["Distributed", "Mmap", "Random", "Serialization"] -uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" - -[[Sockets]] +[[deps.Sockets]] uuid = "6462fe0b-24de-5631-8697-dd941f90decc" -[[SparseArrays]] -deps = ["LinearAlgebra", "Random"] -uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" - -[[Statistics]] -deps = ["LinearAlgebra", "SparseArrays"] -uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" - -[[TOML]] +[[deps.TOML]] deps = ["Dates"] uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" -[[Tar]] +[[deps.Tar]] deps = ["ArgTools", "SHA"] uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" -[[Test]] -deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] -uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[[TimerOutputs]] -deps = ["Printf"] -git-tree-sha1 = "32cdbe6cd2d214c25a0b88f985c9e0092877c236" +[[deps.TimerOutputs]] +deps = ["ExprTools", "Printf"] +git-tree-sha1 = "97e999be94a7147d0609d0b9fc9feca4bf24d76b" uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" -version = "0.5.8" +version = "0.5.15" -[[UUIDs]] +[[deps.UUIDs]] deps = ["Random", "SHA"] uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" -[[Unicode]] +[[deps.Unicode]] uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" -[[Zlib_jll]] +[[deps.Zlib_jll]] deps = ["Libdl"] uuid = "83775a58-1f1d-513f-b197-d71354ab007a" -[[libblastrampoline_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "411ee75a5364426ef54afa88482ca84f35937923" -uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" -version = "3.0.2+0" - -[[nghttp2_jll]] +[[deps.nghttp2_jll]] deps = ["Artifacts", "Libdl"] uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" -[[p7zip_jll]] +[[deps.p7zip_jll]] deps = ["Artifacts", "Libdl"] uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" diff --git a/Project.toml b/Project.toml index 098e7e20..81a4e927 100644 --- a/Project.toml +++ b/Project.toml @@ -6,11 +6,12 @@ version = "0.2.0" [deps] GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" -LLVM_full_jll = "a3ccf953-465e-511d-b87f-60a6490c289d" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" [compat] julia = "1.7" +GPUCompiler = "0.13" +LLVM = "4" [extras] Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" diff --git a/README.md b/README.md index 48a0e2b6..a5395f2d 100644 --- a/README.md +++ b/README.md @@ -26,15 +26,21 @@ m = compile(f, (Int,)) generate_shlib(f, (Int,), "libf") # find a function pointer for this shared library fptr = generate_shlib_fptr("libf", "f") -ccall(fptr, Int, (Int,), 2) +@ccall $fptr(2::Int)::Int # do this in one step (this time with a temporary shared library) fptr = generate_shlib_fptr(f, (Int,)) -ccall(fptr, Int, (Int,), 2) - +@ccall $fptr(2::Int)::Int ``` ## Approach This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. +## Limitations + +* This package currently requires that you have `gcc` installed and in your system's `PATH`. This is probably pretty easy to fix, we only use `gcc` for linking. In theory Clang_jll or LLVM_full_jll should be able to do this, and be managed through Julia's package manager. +* No heap allocations (e.g. creating an array or a string) are allowed inside a statically compiled function body. If you try to run such a function, you will get a segfault. +** It's sometimes possible you won't get a segfault if you define and run the function in the same session, but trying to call the compiled function in a new julia session will definitely segfault. +* Lots of other limitations too. E.g. there's an example in tests/runtests.jl where summing a vector of `Complex{Float32}` is fine, but segfaults on `Complex{Float64}`. + diff --git a/docs/src/backend.md b/docs/src/backend.md index 523901c2..1e08a76c 100644 --- a/docs/src/backend.md +++ b/docs/src/backend.md @@ -7,4 +7,4 @@ Pages = ["backend.md"] ```@autodocs Modules = [StaticCompiler] Pages = readdir("../src") -``` +``` \ No newline at end of file diff --git a/docs/src/helpers.md b/docs/src/helpers.md index c07d4df5..e69de29b 100644 --- a/docs/src/helpers.md +++ b/docs/src/helpers.md @@ -1,23 +0,0 @@ -# Helpers -Note that the helpers defined here are used in tests, and they are useful to test out code in the REPL. - -```julia -twox(x) = 2x -# run code in the REPL -@jlrun twox(3) -# compile to an executable in a `standalone` directory -exegen([ (twox, Tuple{Int}, 4) ]) -``` - -These are not meant to be a permanent part of the API. They are just for testing. - - -```@index -Modules = [StaticCompiler] -Pages = ["helpers.md"] -``` - -```@autodocs -Modules = [StaticCompiler] -Pages = readdir("../src/helpers") -``` diff --git a/docs/src/index.md b/docs/src/index.md index 760d5e6e..e69de29b 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -1,64 +0,0 @@ -```@meta -CurrentModule = StaticCompiler -``` - -# StaticCompiler - -[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://tshort.github.io/StaticCompiler.jl/dev) -[![Build Status](https://travis-ci.com/tshort/StaticCompiler.jl.svg?branch=master)](https://travis-ci.com/tshort/StaticCompiler.jl) -[![Build Status](https://ci.appveyor.com/api/projects/status/github/tshort/StaticCompiler.jl?svg=true)](https://ci.appveyor.com/project/tshort/StaticCompiler-jl) -[![Codecov](https://codecov.io/gh/tshort/StaticCompiler.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/tshort/StaticCompiler.jl) -[![Coveralls](https://coveralls.io/repos/github/tshort/StaticCompiler.jl/badge.svg?branch=master)](https://coveralls.io/github/tshort/StaticCompiler.jl?branch=master) - -This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. It is also meant for cross compilation, so Julia code can be compiled for other targets, including WebAssembly and embedded targets. - -## Installation and Usage -```julia -using Pkg -Pkg.add(PackageSpec( url = "https://github.com/tshort/StaticCompiler.jl", rev = "master")) -``` -```julia -using StaticCompiler -``` -**Documentation**: [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://tshort.github.io/StaticCompiler.jl/dev) - -## Approach -This package uses the [LLVM package](https://github.com/maleadt/LLVM.jl) to generate code in the same fashion as [CUDAnative](https://github.com/JuliaGPU/CUDAnative.jl). - -Some of the key details of this approach are: - -* **ccalls and cglobal** -- When Julia compiles code CUDAnative style, `ccall` and `cglobal` references get compiled to a direct pointer. `StaticCompiler` converts these to symbol references for later linking. For `ccall` with a tuple call to a symbol in a library, `Cassette` is used to convert that to just a symbol reference (no dynamic library loading). - -* **Global variables** -- A lot of code gets compiled with global variables, and these get compiled to a direct pointer. `StaticCompiler` includes a basic serialize/deserialize approach. Right now, this is fairly basic, and it takes shortcuts for some objects by swapping in wrong types. This can work because many times, the objects are not really used in the code. Finding the global variable can be a little tricky because the pointer is converted to a Julia object with `unsafe_pointer_to_objref`, and that segfaults for some addresses. How to best handle cases like that is still to be determined. - -* **Initialization** -- If libjulia is used, some init code needs to be run to set up garbage collection and other things. For this, a basic `blank.ji` file is used to feed `jl_init_with_image`. - -Long term, a better approach may be to use Julia's standard compilation techniques with "tree shaking" to generate a reduced system image (see [here](https://github.com/JuliaLang/julia/issues/33670)). - -## Example -The API still needs work, but here is the general approach right now: - -```julia -using StaticCompiler -m = irgen(cos, Tuple{Float64}) -write(m, "cos.bc") -write_object(m, "cos.o") -``` - -`cos.o` should contain a function called `cos`. From there, you need to convert to link as needed with `libjulia`. - -See the `test` directory for more information and types of code that currently run. The most advanced example that works is a call to an ODE solution using modified code from [ODE.jl](https://github.com/JuliaDiffEq/ODE.jl). For information on compiling and linking to an executable, see [test/standalone-exe.jl](https://github.com/tshort/StaticCompiler.jl/blob/master/test/standalone-exe.jl). - -## Known limitations - -* It won't work for recursive code. Jameson's [codegen-norecursion](https://github.com/JuliaLang/julia/tree/jn/codegen-norecursion) should fix that when merged. - -* `cfunction` is not supported. - -* Generic code that uses `jl_apply_generic` does not work. One strategy for this is to use Cassette to swap out known code that uses dynamic calls. Another approach is to write something like `jl_apply_generic` to implement dynamic calls. - -* The use of Cassette makes it more difficult for Julia to infer some things, and only type-stable code can be statically compiled with this approach. - -* It's only been tested on Linux and Windows. - -Finally, this whole approach is young and likely brittle. Do not expect it to work for your code. diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index ad044a2d..c544fa48 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -1,9 +1,9 @@ module StaticCompiler -import GPUCompiler -import LLVM -import LLVM_full_jll -import Libdl +using GPUCompiler: GPUCompiler +using LLVM: LLVM +using Libdl: Libdl + export generate_shlib, generate_shlib_fptr, compile @@ -23,18 +23,19 @@ end struct TestCompilerParams <: GPUCompiler.AbstractCompilerParams end GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,TestCompilerParams}) = TestRuntime -const linker = Sys.isunix() ? "ld.lld" : Sys.isapple() ? "ld64.lld" : "lld-link" +# const linker = Sys.isunix() ? "ld.lld" : Sys.isapple() ? "ld64.lld" : "lld-link" function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f))) open(path, "w") do io - target = GPUCompiler.NativeCompilerTarget(;reloc=LLVM.API.LLVMRelocPIC, extern=true) + target = GPUCompiler.NativeCompilerTarget(;always_inline=true) source = GPUCompiler.FunctionSpec(f, Base.to_tuple_type(tt), false, name) params = TestCompilerParams() job = GPUCompiler.CompilerJob(target, source, params) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) write(io, obj) flush(io) - run(`$(StaticCompiler.LLVM_full_jll.PATH)/$linker -shared -o $path.$(Libdl.dlext) $path`) + # run(`$(Clang_jll.PATH[])/clang -shared -o $path.$(Libdl.dlext) $path`) + run(`gcc -shared -o $path.$(Libdl.dlext) $path`) rm(path) end path, name diff --git a/src/ccalls.jl b/src/ccalls.jl deleted file mode 100644 index 4314fab5..00000000 --- a/src/ccalls.jl +++ /dev/null @@ -1,90 +0,0 @@ - -""" - find_ccalls(f, tt) - -Returns a `Dict` mapping function addresses to symbol names for all `ccall`s and -`cglobal`s called from the method. This descends into other invocations -within the method. -""" -find_ccalls(@nospecialize(f), @nospecialize(tt)) = find_ccalls(reflect(f, tt)) - -function find_ccalls(ref::Reflection) - result = Dict{Ptr{Nothing}, Symbol}() - idx = VERSION > v"1.2" ? 5 : 4 - foreigncalls = TypedCodeUtils.filter((c) -> lookthrough((c) -> c.head === :foreigncall && !(c.args[idx] isa QuoteNode && c.args[idx].value == :llvmcall), c), ref.CI.code) - # foreigncalls = TypedCodeUtils.filter((c) -> lookthrough((c) -> c.head === :foreigncall, c), ref.CI.code) - for fc in foreigncalls - sym = getsym(fc[2].args[1]) - address = eval(:(cglobal($(sym)))) - result[address] = Symbol(sym isa Tuple ? sym[1] : sym.value) - end - cglobals = TypedCodeUtils.filter((c) -> lookthrough(c -> c.head === :call && iscglobal(c.args[1]), c), ref.CI.code) - for fc in cglobals - sym = getsym(fc[2].args[2]) - address = eval(:(cglobal($(sym)))) - result[address] = Symbol(sym isa Tuple ? sym[1] : sym.value) - end - invokes = TypedCodeUtils.filter((c) -> lookthrough(identify_invoke, c), ref.CI.code) - invokes = map((arg) -> process_invoke(DefaultConsumer(), ref, arg...), invokes) - for fi in invokes - canreflect(fi) || continue - merge!(result, find_ccalls(reflect(fi))) - end - return result -end - -getsym(x) = x -getsym(x::String) = QuoteNode(Symbol(x)) -getsym(x::QuoteNode) = x -getsym(x::Expr) = eval.((x.args[2], x.args[3])) - -iscglobal(x) = x == cglobal || x isa GlobalRef && x.name == :cglobal - - -""" - fix_ccalls!(mod::LLVM.Module, d) - -Replace function addresses with symbol names in `mod`. The symbol names are -meant to be linked to `libjulia` or other libraries. -`d` is a `Dict` mapping a function address to symbol name for `ccall`s. -""" -function fix_ccalls!(mod::LLVM.Module, d) - for fun in functions(mod), blk in blocks(fun), instr in instructions(blk) - if instr isa LLVM.CallInst - dest = called_value(instr) - if dest isa ConstantExpr && occursin("inttoptr", string(dest)) - # @show instr - # @show dest - argtypes = [llvmtype(op) for op in operands(instr)] - nargs = length(parameters(eltype(argtypes[end]))) - # num_extra_args = 1 + length(collect(eachmatch(r"jl_roots", string(instr)))) - ptr = Ptr{Cvoid}(convert(Int, first(operands(dest)))) - if haskey(d, ptr) - sym = d[ptr] - newdest = LLVM.Function(mod, string(sym), LLVM.FunctionType(llvmtype(instr), argtypes[1:nargs])) - LLVM.linkage!(newdest, LLVM.API.LLVMExternalLinkage) - replace_uses!(dest, newdest) - end - end - elseif instr isa LLVM.LoadInst && occursin("inttoptr", string(instr)) - # dest = called_value(instr) - for op in operands(instr) - lastop = op - if occursin("inttoptr", string(op)) - # @show instr - if occursin("addrspacecast", string(op)) || occursin("getelementptr", string(op)) - op = first(operands(op)) - end - first(operands(op)) isa LLVM.ConstantInt || continue - ptr = Ptr{Cvoid}(convert(Int, first(operands(op)))) - if haskey(d, ptr) - obj = d[ptr] - newdest = GlobalVariable(mod, llvmtype(instr), string(d[ptr])) - LLVM.linkage!(newdest, LLVM.API.LLVMExternalLinkage) - replace_uses!(op, newdest) - end - end - end - end - end -end diff --git a/src/extern.jl b/src/extern.jl deleted file mode 100644 index 6c0424a9..00000000 --- a/src/extern.jl +++ /dev/null @@ -1,16 +0,0 @@ -""" - @extern(fun, returntype, argtypes, args...) - -Creates a call to an external function meant to be included at link time. -Use the same conventions as `ccall`. - -This transforms into the following `ccall`: - - ccall("extern fun", llvmcall, returntype, argtypes, args...) -""" -macro extern(name, rettyp, argtyp, args...) - externfun = string("extern ", name isa AbstractString || name isa Symbol ? name : name.value) - Expr(:call, :ccall, externfun, esc(:llvmcall), esc(rettyp), - Expr(:tuple, esc.(argtyp.args)...), esc.(args)...) -end - diff --git a/src/globals.jl b/src/globals.jl deleted file mode 100644 index e2583388..00000000 --- a/src/globals.jl +++ /dev/null @@ -1,164 +0,0 @@ -struct GlobalsContext - invokes::Set{Any} -end -GlobalsContext() = GlobalsContext(Set()) - - -""" - fix_globals!(mod::LLVM.Module) - -Replace function addresses in `mod` with references to global data structures. -For each global variable, two LLVM global objects are created: - -* `jl.global.data` -- An LLVM 'i8' vector holding a serialized version of the Julia object. -* `jl.global` -- A pointer to the unserialized Julia object. - -The `inttopt` with the function address is replaced by `jl.global`. - -A function `jl_init_globals` is added to `mod`. This function deserializes the data in -`jl.global.data` and updates `jl.global`. -""" - -_opcode(x::LLVM.ConstantExpr) = LLVM.API.LLVMGetConstOpcode(LLVM.ref(x)) - -function fix_globals!(mod::LLVM.Module) - # Create a `jl_init_globals` function. - jl_init_globals_func = LLVM.Function(mod, "jl_init_globals", - LLVM.FunctionType(julia_to_llvm(Cvoid), LLVMType[])) - jl_init_global_entry = BasicBlock(jl_init_globals_func, "entry", context(mod)) - - # Definitions for utility functions - func_type = LLVM.FunctionType(julia_to_llvm(Any), LLVMType[LLVM.PointerType(julia_to_llvm(Int8))]) - deserialize_funs = Dict() - - uint8_t = julia_to_llvm(UInt8) - - ctx = SerializeContext() - es = [] - objs = Set() - gptridx = Dict() - instrs = [] - gptrs = [] - j = 1 # counter for position in gptridx - Builder(context(mod)) do builder - toinstr!(x) = x - function toinstr!(x::LLVM.ConstantExpr) - if _opcode(x) == LLVM.API.LLVMAddrSpaceCast - val = toinstr!(first(operands(x))) - ret = addrspacecast!(builder, val, llvmtype(x)) - return ret - elseif _opcode(x) == LLVM.API.LLVMGetElementPtr - ops = operands(x) - val = toinstr!(first(ops)) - ret = gep!(builder, val, [ops[i] for i in 2:length(ops)]) - return ret - elseif _opcode(x) == LLVM.API.LLVMBitCast - ops = operands(x) - val = toinstr!(first(ops)) - ret = pointercast!(builder, val, llvmtype(x)) - return ret - elseif _opcode(x) == LLVM.API.LLVMIntToPtr - ptr = Ptr{Any}(convert(Int, first(operands(x)))) - obj = unsafe_pointer_to_objref(ptr) - if !in(obj, objs) - push!(es, serialize(ctx, obj)) - push!(objs, obj) - # Create pointers to the data. - gptr = GlobalVariable(mod, julia_to_llvm(Any), "jl.global") - linkage!(gptr, LLVM.API.LLVMInternalLinkage) - LLVM.API.LLVMSetInitializer(LLVM.ref(gptr), LLVM.ref(null(julia_to_llvm(Any)))) - push!(gptrs, gptr) - gptridx[obj] = j - j += 1 - end - gptr = gptrs[gptridx[obj]] - gptr2 = load!(builder, gptr) - ret = pointercast!(builder, gptr2, llvmtype(x)) - return ret - end - return x - end - for fun in functions(mod) - if startswith(LLVM.name(fun), "jfptr") - unsafe_delete!(mod, fun) - continue - end - - for blk in blocks(fun), instr in instructions(blk) - # Set up functions to walk the operands of the instruction - # and convert appropriate ConstantExpr's to instructions. - # Look for `LLVMIntToPtr` expressions. - position!(builder, instr) - ops = operands(instr) - N = opcode(instr) == LLVM.API.LLVMCall ? length(ops) - 1 : length(ops) - if opcode(instr) == LLVM.API.LLVMCall && name(last(operands(instr))) == "jl_type_error" - continue - end - for i in 1:N - try - if opcode(instr) == LLVM.API.LLVMPHI - position!(builder, last(instructions(LLVM.incoming(instr)[i][2]))) - end - ops[i] = toinstr!(ops[i]) - catch x - end - end - end - end - end - nglobals = length(es) - #@show mod - #verify(mod) - for i in 1:nglobals - # Assign the appropriate function argument to the appropriate global. - es[i] = :(unsafe_store!($((Symbol("global", i))), $(es[i]))) - end - # Define the deserializing function. - fune = quote - function _deserialize_globals(Vptr, $((Symbol("global", i) for i in 1:nglobals)...)) - $(ctx.init...) - $(es...) - return - end - end - # @show fune - # Execute the deserializing function. - deser_fun = eval(fune) - v = take!(ctx.io) - gv_typ = LLVM.ArrayType(uint8_t, length(v)) - data = LLVM.GlobalVariable(mod, gv_typ, "jl.global.data") - linkage!(data, LLVM.API.LLVMExternalLinkage) - constant!(data, true) - LLVM.API.LLVMSetInitializer(LLVM.ref(data), - LLVM.API.LLVMConstArray(LLVM.ref(uint8_t), - [LLVM.ref(ConstantInt(uint8_t, x)) for x in v], - UInt32(length(v)))) - Builder(context(mod)) do builder - dataptr = gep!(builder, data, [ConstantInt(0, context(mod)), ConstantInt(0, context(mod))]) - - # Create the Julia object from `data` and include that in `init_fun`. - position!(builder, jl_init_global_entry) - gfunc_type = LLVM.FunctionType(julia_to_llvm(Cvoid), - LLVMType[LLVM.PointerType(julia_to_llvm(Int8)), - Iterators.repeated(LLVM.FunctionType(julia_to_llvm(Any)), nglobals)...]) - deserialize_globals_func = LLVM.Function(mod, "_deserialize_globals", gfunc_type) - LLVM.linkage!(deserialize_globals_func, LLVM.API.LLVMExternalLinkage) - for i in 1:nglobals - # The following fix is to match the argument types which are an integer, not a %jl_value_t**. - gptrs[i] = LLVM.ptrtoint!(builder, gptrs[i], julia_to_llvm(Csize_t)) - end - LLVM.call!(builder, deserialize_globals_func, LLVM.Value[dataptr, gptrs...]) - ret!(builder) - end - tt = Tuple{Ptr{UInt8}, Iterators.repeated(Ptr{Any}, nglobals)...} - deser_mod = irgen(deser_fun, tt, overdub = false, fix_globals = false, optimize_llvm = false) - d = find_ccalls(deser_fun, tt) - fix_ccalls!(deser_mod, d) - # rename deserialization function to "_deserialize_globals" - fun = first(TypedCodeUtils.filter(x -> LLVM.name(x) == "_deserialize_globals", functions(deser_mod)))[2] - # LLVM.name!(fun, "_deserialize_globals") - linkage!(fun, LLVM.API.LLVMExternalLinkage) - # link into the main module - LLVM.link!(mod, deser_mod) - return -end diff --git a/src/helpers/README.md b/src/helpers/README.md deleted file mode 100644 index e443db6a..00000000 --- a/src/helpers/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Helpers -Note that the helpers defined here are used in tests, and they are useful to test out code in the REPL. - -```julia -twox(x) = 2x -# run code in the REPL -@jlrun twox(3) -# compile to an executable in a `standalone` directory -exegen([ (twox, Tuple{Int}, 4) ]) -``` - -These are not meant to be a permanent part of the API. They are just for testing. diff --git a/src/helpers/helpers.jl b/src/helpers/helpers.jl deleted file mode 100644 index 1f041540..00000000 --- a/src/helpers/helpers.jl +++ /dev/null @@ -1,37 +0,0 @@ -""" -Returns shellcmd string for different OS. Optionally, checks for gcc installation. -""" -function _shellcmd(checkInstallation::Bool = false) - - if Sys.isunix() - shellcmd = "gcc" - elseif Sys.iswindows() - shellcmd = ["cmd", "/c", "gcc"] - else - error("run command not defined") - end - - if checkInstallation - # Checking gcc installation - try - run(`$shellcmd -v`) - catch - @warn "Make sure gcc compiler is installed: https://gcc.gnu.org/install/binaries.html and is on the path, othetwise some of the functions will return errors" - return nothing - end - end - - return shellcmd -end - -shellcmd = _shellcmd(true) # is used in @jlrun and exegen() - - -export @jlrun -include("jlrun.jl") - -export ldflags, ldlibs, cflags # are used in exegen -include("juliaconfig.jl") - -export exegen -include("standalone-exe.jl") diff --git a/src/helpers/jlrun.jl b/src/helpers/jlrun.jl deleted file mode 100644 index f2e8e165..00000000 --- a/src/helpers/jlrun.jl +++ /dev/null @@ -1,61 +0,0 @@ -using Libdl, LLVM - -function show_inttoptr(mod) - for fun in LLVM.functions(mod), - blk in LLVM.blocks(fun), - instr in LLVM.instructions(blk) - - s = string(instr) - if occursin("inttoptr", s) && occursin(r"[0-9]{8,30}", s) - println(LLVM.name(fun), " ---------------------------") - @show instr - println() - end - end -end - -""" -Compiles function call provided and calls it with `ccall` using the shared library that was created. -""" -macro jlrun(e) - - fun = e.args[1] - efun = esc(fun) - args = length(e.args) > 1 ? e.args[2:end] : Any[] - libpath = abspath("test.o") - dylibpath = abspath("test.so") - tt = Tuple{(typeof(eval(a)) for a in args)...} - if length(e.args) > 1 - ct = code_typed(Base.eval(__module__, fun), tt) - else - ct = code_typed(Base.eval(__module__, fun)) - end - rettype = ct[1][2] - bindir = joinpath(dirname(Sys.BINDIR), "tools") - libdir = joinpath(dirname(Sys.BINDIR), "lib") - - runCommand = :(run( - $(`$shellcmd -shared -fPIC -o test.so -L$libdir test.o -ljulia`), - wait = true, - )) - - quote - m = irgen($efun, $tt) - # m = irgen($efun, $tt, overdub = false) - LLVM.verify(m) - # show_inttoptr(m) - write(m, "test.bc") - write_object(m, "test.o") - $runCommand - dylib = Libdl.dlopen($dylibpath) - ccall(Libdl.dlsym(dylib, "jl_init_globals"), Cvoid, ()) - res = ccall( - Libdl.dlsym(dylib, $(Meta.quot(fun))), - $rettype, - ($((typeof(eval(a)) for a in args)...),), - $(eval.(args)...), - ) - Libdl.dlclose(dylib) - res - end -end diff --git a/src/helpers/juliaconfig.jl b/src/helpers/juliaconfig.jl deleted file mode 100644 index 039e0905..00000000 --- a/src/helpers/juliaconfig.jl +++ /dev/null @@ -1,63 +0,0 @@ -# from PackageCompilerX: https://github.com/KristofferC/PackageCompilerX.jl/blob/c1a90edfaa28907edf2edbbc734ef8afdeeaca80/src/juliaconfig.jl -# adopted from https://github.com/JuliaLang/julia/blob/release-0.6/contrib/julia-config.jl - -function shell_escape(str) - str = replace(str, "'" => "'\''") - return "'$str'" -end - -function julia_libdir() - return if ccall(:jl_is_debugbuild, Cint, ()) != 0 - dirname(abspath(Libdl.dlpath("libjulia-debug"))) - else - dirname(abspath(Libdl.dlpath("libjulia"))) - end -end - -function julia_private_libdir() - @static if Sys.iswindows() - return julia_libdir() - else - return abspath(Sys.BINDIR, Base.PRIVATE_LIBDIR) - end -end - -julia_includedir() = abspath(Sys.BINDIR, Base.INCLUDEDIR, "julia") - -function ldflags() - fl = "-L$(shell_escape(julia_libdir()))" - if Sys.iswindows() - fl = fl * " -Wl,--stack,8388608" - fl = fl * " -Wl,--export-all-symbols" - elseif Sys.islinux() - fl = fl * " -Wl,--export-dynamic" - end - return fl -end - -# TODO -function ldlibs(relative_path=nothing) - libname = if ccall(:jl_is_debugbuild, Cint, ()) != 0 - "julia-debug" - else - "julia" - end - if Sys.islinux() - return "-Wl,-rpath-link,$(shell_escape(julia_libdir())) -Wl,-rpath-link,$(shell_escape(julia_private_libdir())) -l$libname" - elseif Sys.iswindows() - return "-l$libname -lopenlibm" - else - return "-l$libname" - end -end - -function cflags() - flags = IOBuffer() - print(flags, "-std=gnu99") - include = shell_escape(julia_includedir()) - print(flags, " -I", include) - if Sys.isunix() - print(flags, " -fPIC") - end - return String(take!(flags)) -end diff --git a/src/helpers/standalone-exe.jl b/src/helpers/standalone-exe.jl deleted file mode 100644 index 40d026bb..00000000 --- a/src/helpers/standalone-exe.jl +++ /dev/null @@ -1,148 +0,0 @@ -Ctemplate = """ -#include -#include -extern CRETTYPE FUNNAME(CARGTYPES); -extern void jl_init_with_image(const char *, const char *); -extern void jl_init_globals(void); -int main() -{ - jl_init_with_image(".", "blank.ji"); - jl_init_globals(); - printf("RETFORMAT", FUNNAME(FUNARG)); - jl_atexit_hook(0); - return 0; -} -""" - -# "signed" is removed from signed types -# duplicates will remove automatically -Cmap = Dict( - Cchar => "char", #Int8 - Cuchar => "unsigned char", #UInt8 - Cshort => "short", #Int16 - # Cstring => - Cushort => "unsigned short", #UInt16 - Cint => "int", #Int32 - Cuint => "unsigned int", #UInt32 - Clong => "long", #Int32 - Culong => "unsigned long", #UInt32 - Clonglong => "long long", #Int64 - Culonglong => "unsigned long long", #UInt64 - # Cintmax_t => "intmax_t", #Int64 - # Cuintmax_t => "uintmax_t", #UInt64 - # Csize_t => "size_t", #UInt - # Cssize_t => "ssize_t", #Int - # Cptrdiff_t => "ptrdiff_t", #Int - # Cwchar_t => "wchar_t", #Int32 - # Cwstring => - Cfloat => "float", #Float32 - Cdouble => "double", #Float64 - Nothing => "void", -) - -Cformatmap = Dict( - Cchar => "%c", #Int8 - # Cuchar => "unsigned char", #UInt8 - # Cshort => "short", #Int16 - Cstring => "%s", - # Cushort => "unsigned short", #UInt16 - Cint => "%d", #"i" #Int32 - Cuint => "%u", #UInt32 - Clong => "%ld", #Int32 - # Culong => "unsigned long", #UInt32 - Clonglong => "%lld", #Int64 - # Culonglong => "unsigned long long", #UInt64 - # Cintmax_t => "intmax_t", #Int64 - # Cuintmax_t => "uintmax_t", #UInt64 - # Csize_t => "size_t", #UInt - # Cssize_t => "ssize_t", #Int - # Cptrdiff_t => "ptrdiff_t", #Int - # Cwchar_t => "wchar_t", #Int32 - # Cwstring => - # Cfloat => "%f", #Float32 - Cdouble => "%f", #%e #Float64 -) - -""" -converts to text. returns "" for Nothing and empty Tuple. -""" -totext(x) = string(x) -totext(x::Nothing) = "" -totext(x::Tuple{}) = "" - -""" -Makes standalone executable. -""" -function exegen(funcalls) - - cd(mkpath("standalone")) do - # create `blank.ji` for initialization - julia_path = joinpath(Sys.BINDIR, Base.julia_exename()) - base_dir = dirname(Base.find_source_file("sysimg.jl")) - wd = pwd() - open(println, "blank.jl", "w") - cd(base_dir) do - run(`$(julia_path) --output-ji $(wd)/blank.ji $(wd)/blank.jl`) - end - - dir = pwd() - standalonedir = dir - bindir = string(Sys.BINDIR) - libdir = joinpath(dirname(Sys.BINDIR), "lib") - includedir = joinpath(dirname(Sys.BINDIR), "include", "julia") - if Sys.iswindows() - for fn in readdir(bindir) - if splitext(fn)[end] == ".dll" - cp(joinpath(bindir, fn), fn, force = true) - end - end - end - - flags = join((cflags(), ldflags(), ldlibs()), " ") - flags = Base.shell_split(flags) - wrapper = joinpath(@__DIR__, "embedding_wrapper.c") - if Sys.iswindows() - rpath = `` - elseif Sys.isapple() - rpath = `-Wl,-rpath,'@executable_path' -Wl,-rpath,'@executable_path/../lib'` - else - rpath = `-Wl,-rpath,\$ORIGIN:\$ORIGIN/../lib` - end - - for (func, tt, val) in funcalls - fname = nameof(func) - rettype = Base.return_types(func, tt)[1] - argtype = length(tt.types) > 0 ? tt.types[1] : Nothing - fmt = Cformatmap[rettype] - Ctxt = foldl(replace, - ( - "FUNNAME" => fname, - "CRETTYPE" => Cmap[rettype], - "RETFORMAT" => fmt, - "CARGTYPES" => Cmap[argtype], - "FUNARG" => totext(val), - ), - init = Ctemplate) - write("$fname.c", Ctxt) - m = StaticCompiler.irgen(func, tt) - # StaticCompiler.show_inttoptr(m) - # @show m - dlext = Libdl.dlext - exeext = Sys.iswindows() ? ".exe" : "" - if Sys.isapple() - o_file = `-Wl,-all_load $fname.o` - else - o_file = `-Wl,--whole-archive $fname.o -Wl,--no-whole-archive` - end - extra = Sys.iswindows() ? `-Wl,--export-all-symbols` : `` - write(m, "$fname.bc") - write_object(m, "$fname.o") - - run(`$shellcmd -shared -fpic -L$libdir -o lib$fname.$dlext $o_file -Wl,-rpath,$libdir -ljulia $extra`) - run(`$shellcmd -c -std=gnu99 -I$includedir -DJULIA_ENABLE_THREADING=1 -fPIC $fname.c`) - #run(`$shellcmd -o $fname $fname.o -L$libdir -L$standalonedir -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-rpath,'.' -Wl,-rpath,$libdir -ljulia -l$fname -O2 $rpath $flags`) - run(`$shellcmd -o $fname $fname.o -L$libdir -L$standalonedir -Wl,-rpath,'.' -Wl,-rpath,$libdir -ljulia -l$fname -O2 $rpath $flags`) - end - end - -end diff --git a/src/irgen.jl b/src/irgen.jl deleted file mode 100644 index cc9811c4..00000000 --- a/src/irgen.jl +++ /dev/null @@ -1,300 +0,0 @@ - -struct LLVMNativeCode # thin wrapper - p::Ptr{Cvoid} -end - -function xlinfo(f, tt) - # get the method instance - world = typemax(UInt) - g = (args...) -> Cassette.overdub(ctx, f, args...) - meth = which(g, tt) - sig_tt = Tuple{typeof(g), tt.parameters...} - (ti, env) = ccall(:jl_type_intersection_with_env, Any, - (Any, Any), sig_tt, meth.sig)::Core.SimpleVector - - if VERSION >= v"1.2.0-DEV.320" - meth = Base.func_for_method_checked(meth, ti, env) - else - meth = Base.func_for_method_checked(meth, ti) - end - - return ccall(:jl_specializations_get_linfo, Ref{Core.MethodInstance}, - (Any, Any, Any, UInt), meth, ti, env, world) -end - -""" -Returns an LLVMNativeCode object for the function call `f` with TupleTypes `tt`. -""" -function raise_exception(insblock::BasicBlock, ex::Value) -end - -# const jlctx = Ref{LLVM.Context}() - -# function __init__() -# jlctx[] = LLVM.Context(convert(LLVM.API.LLVMContextRef, -# cglobal(:jl_LLVMContext, Nothing))) -# end - -""" - irgen(func, tt; - optimize = true, - optimize_llvm = true, - fix_globals = true, - overdub = true, - module_setup = (m) -> nothing) - -Generates Julia IR targeted for static compilation. -`ccall` and `cglobal` uses have pointer references changed to symbols -meant to be linked with libjulia and other libraries. - -`optimize` controls Julia-side optimization. `optimize_llvm` controls -optimization on the LLVM side. - -If `overdub == true` (the default), Cassette is used to swap out -`ccall`s with a tuple of library and symbol. - -`module_setup` is an optional function to control setup of modules. It takes an LLVM -module as input. -""" -function irgen(@nospecialize(func), @nospecialize(tt); - optimize = true, - optimize_llvm = true, - fix_globals = true, - overdub = true, - module_setup = (m) -> nothing) - # get the method instance - isa(func, Core.Builtin) && error("function is not a generic function") - world = typemax(UInt) - gfunc = overdub ? (args...) -> Cassette.overdub(ctx, func, args...) : func - meth = which(gfunc, tt) - sig_tt = Tuple{typeof(gfunc), tt.parameters...} - (ti, env) = ccall(:jl_type_intersection_with_env, Any, - (Any, Any), sig_tt, meth.sig)::Core.SimpleVector - - if VERSION >= v"1.2.0-DEV.320" - meth = Base.func_for_method_checked(meth, ti, env) - else - meth = Base.func_for_method_checked(meth, ti) - end - - linfo = ccall(:jl_specializations_get_linfo, Ref{Core.MethodInstance}, - (Any, Any, Any, UInt), meth, ti, env, world) - - current_method = nothing - last_method_instance = nothing - call_stack = Vector{Core.MethodInstance}() - global method_map = Dict{String,Core.MethodInstance}() - global dependencies = MultiDict{Core.MethodInstance,LLVM.Function}() - # set-up the compiler interface - function hook_module_setup(ref::Ptr{Cvoid}) - ref = convert(LLVM.API.LLVMModuleRef, ref) - module_setup(LLVM.Module(ref)) - end - function hook_raise_exception(insblock::Ptr{Cvoid}, ex::Ptr{Cvoid}) - insblock = convert(LLVM.API.LLVMValueRef, insblock) - ex = convert(LLVM.API.LLVMValueRef, ex) - raise_exception(BasicBlock(insblock), Value(ex)) - end - function postprocess(ir) - # get rid of jfptr wrappers - for llvmf in functions(ir) - startswith(LLVM.name(llvmf), "jfptr_") && unsafe_delete!(ir, llvmf) - end - - return - end - function hook_module_activation(ref::Ptr{Cvoid}) - ref = convert(LLVM.API.LLVMModuleRef, ref) - global ir = LLVM.Module(ref) - postprocess(ir) - - # find the function that this module defines - llvmfs = filter(llvmf -> !isdeclaration(llvmf) && - linkage(llvmf) == LLVM.API.LLVMExternalLinkage, - collect(functions(ir))) - llvmf = nothing - if length(llvmfs) == 1 - llvmf = first(llvmfs) - elseif length(llvmfs) > 1 - llvmfs = filter!(llvmf -> startswith(LLVM.name(llvmf), "julia_"), llvmfs) - if length(llvmfs) == 1 - llvmf = first(llvmfs) - end - end - insert!(dependencies, last_method_instance, llvmf) - method_map[name(llvmf)] = current_method - end - function hook_emit_function(method_instance, code, world) - push!(call_stack, method_instance) - end - function hook_emitted_function(method, code, world) - current_method = method - last_method_instance = pop!(call_stack) - # @show code - # dump(method, maxdepth=2) - # global mymeth = method - end - - params = Base.CodegenParams(cached=false, - track_allocations=false, - code_coverage=false, - static_alloc=false, - prefer_specsig=true, - module_setup=hook_module_setup, - module_activation=hook_module_activation, - raise_exception=hook_raise_exception, - emit_function=hook_emit_function, - emitted_function=hook_emitted_function, - ) - - # get the code - mod = let - ref = ccall(:jl_get_llvmf_defn, LLVM.API.LLVMValueRef, - (Any, UInt, Bool, Bool, Base.CodegenParams), - linfo, world, #=wrapper=#false, #=optimize=#false, params) - if ref == C_NULL - # error(jlctx[], "the Julia compiler could not generate LLVM IR") - end - - llvmf = LLVM.Function(ref) - LLVM.parent(llvmf) - end - - # the main module should contain a single jfptr_ function definition, - # e.g. jlcall_kernel_vadd_62977 - - # definitions = filter(f->!isdeclaration(f), functions(mod)) - definitions = Iterators.filter(f->!isdeclaration(f), collect(functions(mod))) - # definitions = collect(functions(mod)) - wrapper = let - fs = collect(Iterators.filter(f->startswith(LLVM.name(f), "jfptr_"), definitions)) - @assert length(fs) == 1 - fs[1] - end - - # the jlcall wrapper function should point us to the actual entry-point, - # e.g. julia_kernel_vadd_62984 - entry_tag = let - m = match(r"jfptr_(.+)_\d+", LLVM.name(wrapper)) - @assert m != nothing - m.captures[1] - end - unsafe_delete!(mod, wrapper) - entry = let - re = Regex("julia_$(entry_tag)_\\d+") - llvmcall_re = Regex("julia_$(entry_tag)_\\d+u\\d+") - fs = collect(Iterators.filter(f->occursin(re, LLVM.name(f)) && - !occursin(llvmcall_re, LLVM.name(f)), definitions)) - if length(fs) != 1 - compiler_error(func, tt, cap, "could not find single entry-point"; - entry=>entry_tag, available=>[LLVM.name.(definitions)]) - end - fs[1] - end - - LLVM.name!(entry, string(nameof(func))) - - # link in dependent modules - cache = Dict{String,String}() - for called_method_instance in keys(dependencies) - llvmfs = dependencies[called_method_instance] - - # link the first module - llvmf = popfirst!(llvmfs) - llvmfn = LLVM.name(llvmf) - link!(mod, LLVM.parent(llvmf)) - # process subsequent duplicate modules - for dup_llvmf in llvmfs - if Base.JLOptions().debug_level >= 2 - # link them too, to ensure accurate backtrace reconstruction - link!(mod, LLVM.parent(dup_llvmf)) - else - # don't link them, but note the called function name in a cache - dup_llvmfn = LLVM.name(dup_llvmf) - cache[dup_llvmfn] = llvmfn - end - end - end - # resolve function declarations with cached entries - for llvmf in filter(isdeclaration, collect(functions(mod))) - llvmfn = LLVM.name(llvmf) - if haskey(cache, llvmfn) - def_llvmfn = cache[llvmfn] - replace_uses!(llvmf, functions(mod)[def_llvmfn]) - unsafe_delete!(LLVM.parent(llvmf), llvmf) - end - end - # rename functions to something easier to decipher - # especially helps with overdubbed functions - for (fname, mi) in method_map - id = split(fname, "_")[end] - basename = mi.def.name - args = join(collect(mi.specTypes.parameters)[2:end], "_") - if basename == :overdub # special handling for Cassette - basename = string(mi.specTypes.parameters[3]) - basename = replace(basename, r"^typeof\(" => "") - basename = replace(basename, r"\)$" => "") - args = join(collect(mi.specTypes.parameters)[4:end], "_") - end - newname = join([basename, args, id], "_") - if haskey(functions(mod), fname) - name!(functions(mod)[fname], newname) - end - end - - d = find_ccalls(gfunc, tt) - fix_ccalls!(mod, d) - if fix_globals - fix_globals!(mod) - end - if optimize_llvm - optimize!(mod) - end - return mod -end - - -""" - optimize!(mod::LLVM.Module) - -Optimize the LLVM module `mod`. Crude for now. -Returns nothing. -""" -function optimize!(mod::LLVM.Module) - for llvmf in functions(mod) - startswith(LLVM.name(llvmf), "jfptr_") && unsafe_delete!(mod, llvmf) - startswith(LLVM.name(llvmf), "julia_") && LLVM.linkage!(llvmf, LLVM.API.LLVMExternalLinkage) - end - # triple = "wasm32-unknown-unknown-wasm" - # triple!(mod, triple) - # datalayout!(mod, "e-m:e-p:32:32-i64:64-n32:64-S128") - # LLVM.API.@apicall(:LLVMInitializeWebAssemblyTarget, Cvoid, ()) - # LLVM.API.@apicall(:LLVMInitializeWebAssemblyTargetMC, Cvoid, ()) - # LLVM.API.@apicall(:LLVMInitializeWebAssemblyTargetInfo, Cvoid, ()) - triple = "i686-pc-linux-gnu" - tm = TargetMachine(Target(triple), triple) - - ModulePassManager() do pm - # add_library_info!(pm, triple(mod)) - add_transform_info!(pm, tm) - ccall(:jl_add_optimization_passes, Cvoid, - (LLVM.API.LLVMPassManagerRef, Cint, Cint), - LLVM.ref(pm), Base.JLOptions().opt_level, 1) - - dead_arg_elimination!(pm) - global_optimizer!(pm) - global_dce!(pm) - strip_dead_prototypes!(pm) - - run!(pm, mod) - end - mod -end - -function write_object(mod::LLVM.Module, path) - host_triple = triple() - host_t = Target(host_triple) - TargetMachine(host_t, host_triple, "", "", LLVM.API.LLVMCodeGenLevelDefault, LLVM.API.LLVMRelocPIC) do tm - emit(tm, mod, LLVM.API.LLVMObjectFile, path) - end -end diff --git a/src/overdub.jl b/src/overdub.jl deleted file mode 100644 index 9a0015be..00000000 --- a/src/overdub.jl +++ /dev/null @@ -1,33 +0,0 @@ -# Implements contextual dispatch through Cassette.jl - -using Cassette - -## -# Convert two-arg `ccall` to single arg. -## -function transform(ctx, ref) - CI = ref.code_info - ismatch = x -> begin - Base.Meta.isexpr(x, :foreigncall) && - Base.Meta.isexpr(x.args[1], :call) - end - replace = x -> begin - y = Expr(x.head, Any[x.args[1].args[2], x.args[2:end]...]) - Expr(x.head, x.args[1].args[2], x.args[2:end]...) - end - Cassette.replace_match!(replace, ismatch, CI.code) - return CI -end - -const Pass = Cassette.@pass transform - -Cassette.@context Ctx -const ctx = Cassette.disablehooks(Ctx(pass = Pass)) - -### -# Rewrite functions -### - -#@inline Cassette.overdub(ctx::Ctx, ::typeof(+), a::T, b::T) where T<:Union{Float32, Float64} = add_float_contract(a, b) - -contextualize(f::F) where F = (args...) -> Cassette.overdub(ctx, f, args...) diff --git a/src/serialize.jl b/src/serialize.jl deleted file mode 100644 index 57256501..00000000 --- a/src/serialize.jl +++ /dev/null @@ -1,242 +0,0 @@ - -""" -A context structure for holding state related to serializing Julia -objects. A key component is an `IOBuffer` used to hold the serialized -result. -""" -struct SerializeContext - io::IOBuffer - store::Dict{Any,Any} # Meant to map Julia object to variable name - init::Vector{Any} # Expressions to run initially -end -SerializeContext(io::IOBuffer = IOBuffer()) = SerializeContext(io, Dict(), Vector{Expr}()) - -const _td = IdDict( - Any => :jl_any_type, - Float64 => :jl_float64_type, - Float32 => :jl_float32_type, - Int64 => :jl_int64_type, - Int32 => :jl_int32_type, - Int16 => :jl_int16_type, - Int8 => :jl_int8_type, - UInt64 => :jl_uint64_type, - UInt32 => :jl_uint32_type, - UInt16 => :jl_uint16_type, - UInt8 => :jl_uint8_type, - Cint => :jl_int32_type, - Cvoid => :jl_any_type, - Array => :jl_array_type, - Array{Any,1} => :jl_array_any_type, - Array{Int32,1} => :jl_array_int32_type, - Array{UInt8,1} => :jl_array_uint8_type, - ErrorException => :jl_errorexception_type, - DataType => :jl_datatype_type, - UnionAll => :jl_unionall_type, - Union => :jl_union_type, - Core.TypeofBottom => :jl_typeofbottom_type, - TypeVar => :jl_tvar_type, -) - -const _t = IdDict() - -for (t,s) in _td - _t[t] = :(unsafe_load(cglobal($(QuoteNode(s)), Type))) -end - -const _gd = IdDict( - Core => :jl_core_module, - Main => :jl_main_module, - nothing => :jl_nothing, - () => :jl_emptytuple, - Core.svec() => :jl_emptysvec, - UndefRefError() => :jl_undefref_exception, -) - -const _g = IdDict() - -for (x,s) in _gd - _g[x] = :(unsafe_load(cglobal($(QuoteNode(s)), Any))) -end - -""" - serialize(ctx::SerializeContext, x) - -Serialize `x` into the context object `ctx`. `ctx.io` is the `IOBuffer` where the -serialized results are stored. Get the result with `take!(ctx.io)`. - -This function returns an expression that will deserialize the object. Several `serialize` -methods can be called recursively to build up deserialization code for nested objects. -The expression returned is meant to be `eval`ed into a function that can be called -to do the serialization. - -The deserialization code should be pretty low-level code that can be compiled -relatively easily. It especially shouldn't use global variables. - -Serialization / deserialization code can use `ctx` to hold state information. - -Some simple types like boxed variables do not need to write anything to `ctx.io`. -They can return an expression that directly creates the object. -""" -function serialize(ctx::SerializeContext, @nospecialize(x)) - haskey(_g, x) && return _g[x] - # TODO: fix this major kludge. - if nfields(x) > 0 - return Expr(:tuple, (serialize(ctx, getfield(x,i)) for i in 1:nfields(x))...) - end - return :(unsafe_load(cglobal(:jl_emptytuple, Any))) -end - -function serialize(ctx::SerializeContext, @nospecialize(t::DataType)) - if haskey(_t, t) - return _t[t] - elseif haskey(ctx.store, t) - return ctx.store[t] - else - # primary = unwrap_unionall(t.wrapper) - name = gensym(Symbol(:type, "-", t.name.name)) - ctx.store[t] = name - e = quote - $name = let - local tn = $(serialize(ctx, t.name)) - # names = $(serialize(ctx, t.names)) - local super = $(serialize(ctx, t.super)) - local parameters = $(serialize(ctx, t.parameters)) - local types = $(serialize(ctx, t.types)) - local ndt = ccall(:jl_new_datatype, Any, - (Any, Any, Any, Any, Any, Any, Cint, Cint, Cint), - tn, tn.module, super, parameters, #=names=# unsafe_load(cglobal(:jl_any_type, Any)), types, - $(t.abstract), $(t.mutable), $(t.ninitialized)) - # tn.wrapper = ndt.name.wrapper - # ccall(:jl_set_const, Cvoid, (Any, Any, Any), tn.module, tn.name, tn.wrapper) - ndt - # ty = tn.wrapper - # $(ctx.types[string(t)]) = ndt - # hasinstance = serialize(ctx, ) - # $(if isdefined(primary, :instance) && !isdefined(t, :instance) - # # use setfield! directly to avoid `fieldtype` lowering expecting to see a Singleton object already on ty - # :(Core.setfield!(ty, :instance, ccall(:jl_new_struct, Any, (Any, Any...), ty))) - # end) - end - end - push!(ctx.init, e) - return name - end -end - -function serialize(ctx::SerializeContext, tn::Core.TypeName) - haskey(ctx.store, tn) && return ctx.store[tn] - name = gensym(Symbol(:typename, "-", tn.name)) - ctx.store[tn] = name - e = quote - $name = ccall(:jl_new_typename_in, Ref{Core.TypeName}, (Any, Any), - # $(serialize(ctx, tn.name)), Main #=__deserialized_types__ =# ) - $(serialize(ctx, tn.name)), unsafe_load(cglobal(:jl_main_module, Any)) #=__deserialized_types__ =# ) - end - push!(ctx.init, e) - return name -end - -function serialize(ctx::SerializeContext, mi::Core.MethodInstance) - return :(unsafe_load(cglobal(:jl_emptytuple, Any))) -end - -function serialize(ctx::SerializeContext, x::String) - advance!(ctx.io) - v = Vector{UInt8}(x) - ioptr = ctx.io.ptr - write(ctx.io, v) - quote - unsafe_string(Vptr + $(ioptr - 1), $(length(v))) - end -end - -function serialize(ctx::SerializeContext, x::Symbol) - haskey(ctx.store, x) && return ctx.store[x] - name = gensym(Symbol(:symbol, "-", x)) - ctx.store[x] = name - e = quote - $name = ccall(:jl_symbol_n, Any, (Ptr{UInt8}, Csize_t), $(serialize(ctx, string(x))), $(length(string(x)))) - # ccall(:jl_set_global, Cvoid, (Any, Any, Any), unsafe_load(cglobal(:jl_main_module, Any)), $(QuoteNode(name)), x) - end - push!(ctx.init, e) - return name -end - - - -# Define functions that return an expression. Example: -# serialize(ctx::SerializeContext, x::Int) = :(ccall(:jl_box_int64, Any, (Int,), $x)) -for (fun, type) in (:jl_box_int64 => Int64, :jl_box_int32 => Int32, :jl_box_int8 => Int16, :jl_box_int8 => Int8, - :jl_box_uint64 => UInt64, :jl_box_uint32 => UInt32, :jl_box_uint8 => UInt16, :jl_box_uint8 => UInt8, - :jl_box_voidpointer => Ptr{Cvoid}, - :jl_box_float64 => Float64, :jl_box_float32 => Float32) - @eval serialize(ctx::SerializeContext, x::$type) = Expr(:call, :ccall, QuoteNode($(QuoteNode(fun))), Any, Expr(:tuple, $type), x) -end -serialize(ctx::SerializeContext, x::Char) = :(ccall(:jl_box_char, Any, (UInt32,), $x)) -serialize(ctx::SerializeContext, x::Bool) = :(ccall(:jl_box_bool, Any, (UInt8,), $x)) - -function serialize(ctx::SerializeContext, a::Tuple) - length(a) == 0 && return :(unsafe_load(cglobal(:jl_emptytuple, Any))) - Expr(:tuple, (serialize(ctx, x) for x in a)...) -end - -function serialize(ctx::SerializeContext, a::Core.SimpleVector) - length(a) == 0 && return :(unsafe_load(cglobal(:jl_emptysvec, Any))) - Expr(:call, Expr(:., :Core, QuoteNode(:svec)), (serialize(ctx, x) for x in a)...) -end - -advance!(io) = write(io, repeat('\0', -rem(io.ptr - 1, 8, RoundUp))) # Align data to 8 bytes - -function serialize(ctx::SerializeContext, a::Array{T,N}) where {T,N} - elty = eltype(a) - aty = typeof(a) - dims = size(a) - atys = serialize(ctx, aty) - if isbitstype(elty) - advance!(ctx.io) - ioptr = ctx.io.ptr - write(ctx.io, a) - if N == 1 - advance!(ctx.io) - ioptr = ctx.io.ptr - write(ctx.io, a) - quote - p = Vptr + $ioptr - 1 - ccall(:jl_ptr_to_array_1d, $aty, (Any, Ptr{Cvoid}, Csize_t, Cint), $atys, p, $(length(a)), false) - end - else - dms = serialize(ctx, dims) - advance!(ctx.io) - ioptr = ctx.io.ptr - write(ctx.io, a) - quote - p = Vptr + $ioptr - 1 - ccall(:jl_ptr_to_array, $aty, (Any, Ptr{Cvoid}, Any, Int32), $atys, p, $dms, false) - end - end - else - idx = Int[] - e = Array{Any}(undef, length(a)) - @inbounds for i in eachindex(a) - if isassigned(a, i) - e[i] = serialize(ctx, a[i]) - push!(idx, i) - end - end - aname = gensym() - resulte = [quote - # $aname = Array{$elty, $(length(dims))}(undef, $dims) - $aname = ccall(:jl_new_array, $aty, (Any, Any), $atys, $(serialize(ctx, dims))) - end] - for i in idx - push!(resulte, quote - # unsafe_store!(pointer($aname), $(e[i]), $i) - unsafe_store!(convert(Ptr{Any}, pointer($aname)), $(e[i]), $i) - # unsafe_store!(convert(Ptr{Csize_t}, pointer($aname)), pointer_from_objref($(e[i])), $i) - # @inbounds $aname[$i] = $(e[i]) - end) - end - push!(resulte, :($aname = $aname)) - Expr(:block, resulte...) - end -end diff --git a/src/utils.jl b/src/utils.jl deleted file mode 100644 index 0fdf7a7c..00000000 --- a/src/utils.jl +++ /dev/null @@ -1,56 +0,0 @@ - -function julia_to_llvm(@nospecialize x) - isboxed = Ref{UInt8}() - # LLVMType(ccall(:jl_type_to_llvm, LLVM.API.LLVMTypeRef, (Any, Ref{UInt8}), x, isboxed)) # noserialize - LLVMType(ccall(:julia_type_to_llvm, LLVM.API.LLVMTypeRef, (Any, Ref{UInt8}), x, isboxed)) # julia v1.1.1 -end - -const jl_value_t_ptr = julia_to_llvm(Any) -const jl_value_t = eltype(jl_value_t_ptr) -# const jl_value_t_ptr_ptr = LLVM.PointerType(jl_value_t_ptr) -# # cheat on these for now: -# const jl_datatype_t_ptr = jl_value_t_ptr -# const jl_unionall_t_ptr = jl_value_t_ptr -# const jl_typename_t_ptr = jl_value_t_ptr -# const jl_sym_t_ptr = jl_value_t_ptr -# const jl_svec_t_ptr = jl_value_t_ptr -# const jl_module_t_ptr = jl_value_t_ptr -# const jl_array_t_ptr = jl_value_t_ptr -# -# const bool_t = julia_to_llvm(Bool) -# const int8_t = julia_to_llvm(Int8) -# const int16_t = julia_to_llvm(Int16) -# const int32_t = julia_to_llvm(Int32) -# const int64_t = julia_to_llvm(Int64) -# const uint8_t = julia_to_llvm(UInt8) -# const uint16_t = julia_to_llvm(UInt16) -# const uint32_t = julia_to_llvm(UInt32) -# const uint64_t = julia_to_llvm(UInt64) -# const float_t = julia_to_llvm(Float32) -# const double_t = julia_to_llvm(Float64) -# const float32_t = julia_to_llvm(Float32) -# const float64_t = julia_to_llvm(Float64) -# const void_t = julia_to_llvm(Nothing) -# const size_t = julia_to_llvm(Int) -# -# const int8_t_ptr = LLVM.PointerType(int8_t) -# const void_t_ptr = LLVM.PointerType(void_t) - -function module_setup(mod::LLVM.Module) -# triple!(mod, "wasm32-unknown-unknown-wasm") -# datalayout!(mod, "e-m:e-p:32:32-i64:64-n32:64-S128") -end - -llvmmod(native_code) = - LLVM.Module(ccall(:jl_get_llvm_module, LLVM.API.LLVMModuleRef, - (Ptr{Cvoid},), native_code.p)) - -function Base.write(mod::LLVM.Module, path::String) - open(io -> write(io, mod), path, "w") -end - - -walk(f, x) = true -# walk(f, x::Instruction) = foreach(c->walk(f,c), operands(x)) -# walk(f, x::Instruction) = f(x) || foreach(c->walk(f,c), operands(x)) -walk(f, x::ConstantExpr) = f(x) || foreach(c->walk(f,c), operands(x)) diff --git a/test/ccalls.jl b/test/ccalls.jl deleted file mode 100644 index dd5663c6..00000000 --- a/test/ccalls.jl +++ /dev/null @@ -1,35 +0,0 @@ -# d = find_ccalls(Threads.nthreads, Tuple{}) -# d = find_ccalls(time, Tuple{}) -# d = find_ccalls(muladd, Tuple{Array{Float64,2},Array{Float64,2},Array{Float64,2}}) - -f1() = ccall(:jl_errno, Int, (Int,), 11) -f2() = ccall(:jl_errno, Int, (Int, Int), 21, 22) -f3() = ccall(:jl_errno, Int, (Int, Int, Int), 31, 32, 33) - -@testset "ccalls" begin - m1 = irgen(f1, Tuple{}) - m2 = irgen(f2, Tuple{}) - m3 = irgen(f3, Tuple{}) - LLVM.verify(m1) - LLVM.verify(m2) - LLVM.verify(m3) -end - - -function f() - n = Int(unsafe_load(cglobal(:jl_n_threads, Cint))) - return 2n -end - -@testset "cglobal" begin - m = irgen(f, Tuple{}) - LLVM.verify(m) - @test f() == @jlrun f() -end - -@testset "extern" begin - f() = @extern(:time, Cvoid, (Ptr{Cvoid},), C_NULL) - m = irgen(f, Tuple{}) - LLVM.verify(m) - @test "time" in [name(f) for f in LLVM.functions(m)] -end diff --git a/test/globals.jl b/test/globals.jl deleted file mode 100644 index 43c70e84..00000000 --- a/test/globals.jl +++ /dev/null @@ -1,36 +0,0 @@ -# @testset "serialize" begin -# ctx = StaticCompiler.SerializeContext() -# a = Any["abcdg", ["hi", "bye"], 3333, Int32(44), 314f0, 3.14, (1, 3.3f0), Core.svec(9.9, 9), :sym, :sym, :a] -# e = StaticCompiler.serialize(ctx, a) -# g = eval(:(Vptr -> $e)) -# v = take!(ctx.io) -# GC.enable(false) -# res = g(pointer(v)) -# GC.enable(true) -# @test res == a -# end - - -# const a = ["abcdg", "asdfl", 123, 3.14, ["a", "asdf"], (1, 3.63), [1, 3.63]] -const a = ["abcdg", "asdxf"] -const b = "B" -const x = [1.33, 35.0] -const xi = [3, 5] - -f(x) = @inbounds a[1][3] > b[1] ? 2x : x -g(i) = @inbounds x[1] > x[2] ? 2i : i -h(i) = @inbounds xi[1] == 3 ? i : 2i - -@testset "globals" begin - @test f(3) == @jlrun f(3) - @test g(3) == @jlrun g(3) - @test h(3) == @jlrun h(3) -end - -f() = Complex{Float64} -g(@nospecialize(x)) = isa(x, Number) ? 1 : 0 - -@testset "type" begin - @test string(@jlrun f()) == "Complex{Float64}" - res = g(4.0im) -end diff --git a/test/ode.jl b/test/ode.jl deleted file mode 100644 index b270a544..00000000 --- a/test/ode.jl +++ /dev/null @@ -1,182 +0,0 @@ -# Adapted from: https://github.com/JuliaDiffEq/ODE.jl/blob/8954872f956116e78b6c04690f899fe2db696b4e/src/ODE.jl#L84-L360 -# MIT license -# Copyright (c) 2009-2015: various contributors: https://github.com/JuliaLang/ODE.jl/contributors - -using LinearAlgebra - -function hinit(F, x0, t0::T, tend, p, reltol, abstol) where T - # Returns first step, direction of integration and F evaluated at t0 - tdir = sign(tend-t0) - tdir==0 && error("Zero time span") - tau = max(reltol*norm(x0, Inf), abstol) - d0 = norm(x0, Inf)/tau - f0 = F(t0, x0) - d1 = norm(f0, Inf)/tau - if d0 < 1e-5 || d1 < 1e-5 - h0 = 1e-6 - else - h0 = 0.01*(d0/d1) - end - h0 = convert(T,h0) - # perform Euler step - x1 = x0 + tdir*h0*f0 - f1 = F(t0 + tdir*h0, x1) - # estimate second derivative - d2 = norm(f1 - f0, Inf)/(tau*h0) - if max(d1, d2) <= 1e-15 - h1 = max(T(10)^(-6), T(10)^(-3)*h0) - else - pow = -(2 + log10(max(d1, d2)))/(p + 1) - h1 = 10^pow - end - h1 = convert(T,h1) - return tdir*min(100*h0, h1, tdir*(tend-t0)), tdir, f0 -end - -function fdjacobian(F, x::Number, t) - ftx = F(t, x) - - # The 100 below is heuristic - dx = (x .+ (x==0))./100 - dFdx = (F(t,x+dx)-ftx)./dx - - return dFdx -end - -function fdjacobian(F, x, t) - ftx = F(t, x) - lx = max(length(x),1) - dFdx = zeros(eltype(x), lx, lx) - for j = 1:lx - # The 100 below is heuristic - dx = zeros(eltype(x), lx) - dx[j] = (x[j] .+ (x[j]==0))./100 - dFdx[:,j] = (F(t,x+dx)-ftx)./dx[j] - end - return dFdx -end - -# ODE23S Solve stiff systems based on a modified Rosenbrock triple -# (also used by MATLAB's ODE23s); see Sec. 4.1 in -# -# [SR97] L.F. Shampine and M.W. Reichelt: "The MATLAB ODE Suite," SIAM Journal on Scientific Computing, Vol. 18, 1997, pp. 1–22 -# -# supports keywords: points = :all | :specified (using dense output) -# jacobian = G(t,y)::Function | nothing (FD) -function ode23s(F, y0, tspan; - reltol = 1.0e-5, abstol = 1.0e-8, - jacobian=nothing, - points=:all, - norm=LinearAlgebra.norm, - minstep=abs(tspan[end] - tspan[1])/1e18, - maxstep=abs(tspan[end] - tspan[1])/2.5, - initstep=0.) - - # select method for computing the Jacobian - if typeof(jacobian) == Function - jac = jacobian - else - # fallback finite-difference - jac = (t, y)->fdjacobian(F, y, t) - end - - # constants - d = 1/(2 + sqrt(2)) - e32 = 6 + sqrt(2) - - - # initialization - t = tspan[1] - - tfinal = tspan[end] - - h = initstep - if h == 0. - # initial guess at a step size - h, tdir, F0 = hinit(F, y0, t, tfinal, 3, reltol, abstol) - else - tdir = sign(tfinal - t) - F0 = F(t,y0) - end - h = tdir * min(abs(h), maxstep) - - y = y0 - tout = [t] # first output time - yout = [deepcopy(y)] # first output solution - - J = jac(t,y) # get Jacobian of F wrt y -# Core.print(t, " ", tfinal, " ", minstep, " ", h) - while abs(t - tfinal) > 0 && minstep < abs(h) - if abs(t-tfinal) < abs(h) - h = tfinal - t - end - - if size(J,1) == 1 - W = I - h*d*J - else - # note: if there is a mass matrix M on the lhs of the ODE, i.e., - # M * dy/dt = F(t,y) - # we can simply replace eye(J) by M in the following expression - # (see Sec. 5 in [SR97]) - - W = lu( I - h*d*J ) - end - - # approximate time-derivative of F - T = h*d*(F(t + h/100, y) - F0)/(h/100) - - # modified Rosenbrock formula - k1 = W\(F0 + T) - F1 = F(t + 0.5*h, y + 0.5*h*k1) - k2 = W\(F1 - k1) + k1 - ynew = y + h*k2 - F2 = F(t + h, ynew) - k3 = W\(F2 - e32*(k2 - F1) - 2*(k1 - F0) + T ) - - err = (abs(h)/6)*norm(k1 - 2*k2 + k3) # error estimate - delta = max(reltol*max(norm(y),norm(ynew)), abstol) # allowable error - - # check if new solution is acceptable - if err <= delta - - # # if points==:specified || points==:all - # # only points in tspan are requested - # # -> find relevant points in (t,t+h] - # for toi in tspan[(tspan.>t) .& (tspan.<=t+h)] - # # rescale to (0,1] - # s = (toi-t)/h - - # # use interpolation formula to get solutions at t=toi - # push!(tout, toi) - # push!(yout, y + h*( k1*s*(1-s)/(1-2*d) + k2*s*(s-2*d)/(1-2*d))) - # end - # # Core.print("First\n") - # # end - # if points==:all - if (tout[end]!=t+h) - # # add the intermediate points - push!(tout, t + h) - push!(yout, ynew) - end - - # update solution - t = t + h - y = ynew - - F0 = F2 # use FSAL property - J = jac(t,y) # get Jacobian of F wrt y - # for new solution - end - - # update of the step size - h = tdir*min( maxstep, abs(h)*0.8*(delta/err)^(1/3) ) - end - - return tout, yout -end - - -# fode() = ode23s((t,y)->2.0t^2, 0.0, Float64[0:.001:2;], initstep = 1e-4)[2][end] -# fode() = ode23s((t,y)->2.0t^2, 0.0, [0:.001:2;], initstep = 1e-4)[2][end] - -# @show fode() diff --git a/test/others.jl b/test/others.jl deleted file mode 100644 index 4481e77a..00000000 --- a/test/others.jl +++ /dev/null @@ -1,73 +0,0 @@ -mutable struct AAA - aaa::Int - bbb::Int -end -@noinline ssum(x) = x.aaa + x.bbb -fstruct(x) = ssum(AAA(x, 99)) -@test fstruct(10) == @jlrun fstruct(10) - -module ZZ -mutable struct AAA - aaa::Int - bbb::Int -end -@noinline ssum(x) = x.aaa + x.bbb -fstruct(x) = ssum(AAA(x, 99)) -end # module -ffstruct(x) = ZZ.fstruct(x) -@test ffstruct(10) == @jlrun ffstruct(10) - -const ag = Ref(0x80808080) -jglobal() = ag -@show bg = @jlrun jglobal() -# @test jglobal()[] == bg[] # Something's broken with mutable's - -arraysum(x) = sum([x, 1]) -# @test arraysum(6) == @jlrun arraysum(6) - -fsin(x) = sin(x) -@test fsin(0.5) == @jlrun fsin(0.5) - -fccall() = ccall(:jl_ver_major, Cint, ()) -@test fccall() == @jlrun fccall() - -fcglobal() = cglobal(:jl_n_threads, Cint) -@test fcglobal() == @jlrun fcglobal() - -const sv = Core.svec(1,2,3,4) -fsv() = sv -@test fsv() == @jlrun fsv() - -const arr = [9,9,9,9] -farray() = arr -@show @jlrun farray() -@show farray() -# @test farray() == @jlrun farray() - -@noinline f0(x) = 3x -@noinline fop(f, x) = 2f(x) -funcall(x) = fop(f0, x) -@test funcall(2) == @jlrun funcall(2) - -hi() = print(Core.stdout, 'X') -@jlrun hi() - -hello() = print(Core.stdout, "Hello world...\n") -@jlrun hello() - -function gx(i) - a = 2.0:0.1:10.0 - @inbounds i > 3 ? a[1] : a[5] -end - -@test_skip gx(4) == @jlrun gx(4) - -fsimple() = [0:.001:2;][end] - -@test fsimple() == @jlrun fsimple() - -@noinline function fsym(x; l = :hello, s = :x) - s == :asdf ? x : 2x -end -gsym(x) = fsym(x, l = :hello, s = :asdf) + 1 -@test gsym(3) == @jlrun gsym(3) diff --git a/test/runtests.jl b/test/runtests.jl index b721ea63..21286a2b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,10 +19,12 @@ using LinearAlgebra end +fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # for some reason, if this is defined in the testset, it segfaults + @testset "Recursion" begin - fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This works on the REPL but fails here - @test_skip ccall(generate_shlib_fptr(fib, (Int,)), Int, (Int,), 10) == 55 + fib_ptr = generate_shlib_fptr(fib, (Int,)) + @test @ccall( $fib_ptr(10::Int) :: Int ) == 55 end # Call binaries for testing @@ -80,8 +82,6 @@ end # Arrays with different input types Int32, Int64, Float32, Float64, Complex? @testset "Arrays" begin - - arr = collect(1:10) function array_sum(n, A) s = zero(eltype(A)) for i in 1:n @@ -89,21 +89,31 @@ end end s end + + array_sum_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Int}}) + @test ( @ccall $array_sum_ptr(10::Int, collect(1:10)::Vector{Int})::Int ) == 55 - #This segfaults, not sure if this is how you pass around arrays - @test_skip ccall(generate_shlib_fptr(array_sum, (Csize_t, Ptr{Float64})), Int, (Csize_t, Ptr{Float64}), length(arr), arr) == 55 + # this will segfault on my machine if I use 64 bit complex numbers! + array_sum_complex_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float32}}}) + @test ( @ccall $array_sum_complex_ptr(2::Int, [1f0+im, 1f0-im]::Vector{Complex{Float32}})::Complex{Float32} ) ≈ 2.0 + #This will segfault + array_sum_complex64_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float642}}}) + @test_skip ( @ccall $array_sum_complex_ptr(2::Int, [1.0+im, 1.0-im]::Vector{Complex{Float64}})::Complex{Float64} ) ≈ 2.0 end # Just to call external libraries @testset "BLAS" begin - function mydot(N) - a = Float64.(1:N) + function mydot(a::Vector{Float64}) + N = length(a) BLAS.dot(N, a, 1, a, 1) end - @test_skip ccall(generate_shlib_fptr(mydot, (Int,)), Float64, (Int,), 2) == 5. + a = [1.0, 2.0] + mydot_ptr = generate_shlib_fptr(mydot, Tuple{Vector{Float64}}) + @test @ccall( $mydot_ptr(a::Vector{Float64})::Float64 ) == 5.0 end + @testset "Hello World" begin function hello(N) println("Hello World $N") diff --git a/test/standalone-exe.jl b/test/standalone-exe.jl deleted file mode 100644 index fb97de52..00000000 --- a/test/standalone-exe.jl +++ /dev/null @@ -1,41 +0,0 @@ -# Definitions of functions to compile -twox(x) = 2x - -const aa = [4, 5] -arrayfun(x) = x + aa[1] + aa[2] - -jsin(x) = sin(x) - -function arridx(i) - a = collect(1.0:0.1:10.0) - @inbounds i > 3 ? a[1] : a[5] -end - -fsimple() = [0:.001:2;][end] - -include("ode.jl") -fode() = ode23s((t,y)->2.0t^2, 0.0, [0:.001:2;], initstep = 1e-4)[2][end] - -# Functions to compile and arguments to pass -funcalls = [ - (twox, Tuple{Int}, 4), - (arrayfun, Tuple{Int}, 4), - (jsin, Tuple{Float64}, 0.5), - (arridx, Tuple{Int}, 4), - (fsimple, Tuple{}, ()), - (fode, Tuple{}, ()), # Broken on Julia v1.2.0; works on Julia v1.3.0-rc3 -] - -StaticCompiler.exegen(funcalls) - -using Formatting -@testset "exegen" begin - cd("standalone") do - for (func, tt, val) in funcalls - fname = nameof(func) - rettype = Base.return_types(func, tt)[1] - fmt = StaticCompiler.Cformatmap[rettype] - @test Formatting.sprintf1(fmt, func(val...)) == read(`./$fname`, String) - end - end -end From ffde87606a4e781779dc767985b38cd6bdf2b4ab Mon Sep 17 00:00:00 2001 From: Tom Short Date: Wed, 2 Feb 2022 08:48:30 -0500 Subject: [PATCH 012/202] Fix test typo --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 21286a2b..e0083150 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -98,7 +98,7 @@ end @test ( @ccall $array_sum_complex_ptr(2::Int, [1f0+im, 1f0-im]::Vector{Complex{Float32}})::Complex{Float32} ) ≈ 2.0 #This will segfault - array_sum_complex64_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float642}}}) + array_sum_complex64_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float64}}}) @test_skip ( @ccall $array_sum_complex_ptr(2::Int, [1.0+im, 1.0-im]::Vector{Complex{Float64}})::Complex{Float64} ) ≈ 2.0 end From 01be968e8ae23b69e2dabc4efc9a4244300400d3 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 11:26:33 -0700 Subject: [PATCH 013/202] remove windows from CI --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b9510c3c..14c8967a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,6 @@ jobs: os: - ubuntu-latest - macOS-latest - - windows-latest arch: - x64 steps: From 058220577b8f494ed7619b9965a774acec2a3bbe Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 11:27:14 -0700 Subject: [PATCH 014/202] note that this doesn't work on windows --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a5395f2d..55780052 100644 --- a/README.md +++ b/README.md @@ -43,4 +43,4 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi * No heap allocations (e.g. creating an array or a string) are allowed inside a statically compiled function body. If you try to run such a function, you will get a segfault. ** It's sometimes possible you won't get a segfault if you define and run the function in the same session, but trying to call the compiled function in a new julia session will definitely segfault. * Lots of other limitations too. E.g. there's an example in tests/runtests.jl where summing a vector of `Complex{Float32}` is fine, but segfaults on `Complex{Float64}`. - +* Doesn't currently work on Windows \ No newline at end of file From 6248d8d690a6e0ef4d8cf04940a6aefadfe8ccd4 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 14:05:17 -0700 Subject: [PATCH 015/202] fix `compile`, add `native_code_llvm`, `native_code_typed` --- src/StaticCompiler.jl | 47 ++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index c544fa48..dfc9445a 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -10,28 +10,44 @@ export generate_shlib, generate_shlib_fptr, compile module TestRuntime # dummy methods signal_exception() = return - malloc(sz) = C_NULL + # HACK: if malloc returns 0 or traps, all calling functions (like jl_box_*) + # get reduced to a trap, which really messes with our test suite. + malloc(sz) = Ptr{Cvoid}(Int(0xDEADBEEF)) report_oom(sz) = return report_exception(ex) = return report_exception_name(ex) = return report_exception_frame(idx, func, file, line) = return - - # for validation - sin(x) = Base.sin(x) end struct TestCompilerParams <: GPUCompiler.AbstractCompilerParams end GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,TestCompilerParams}) = TestRuntime + +function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) + source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) + target = GPUCompiler.NativeCompilerTarget(always_inline=true) + params = TestCompilerParams() + GPUCompiler.CompilerJob(target, source, params), kwargs +end + +function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) + job, kwargs = native_job(func, types; kwargs...) + GPUCompiler.code_llvm(stdout, job; kwargs...) +end + +function native_code_typed(@nospecialize(func), @nospecialize(types); kwargs...) + job, kwargs = native_job(func, types; kwargs...) + GPUCompiler.code_typed(job; kwargs...) +end + + # const linker = Sys.isunix() ? "ld.lld" : Sys.isapple() ? "ld64.lld" : "lld-link" -function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f))) +function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)); kwargs...) open(path, "w") do io - target = GPUCompiler.NativeCompilerTarget(;always_inline=true) - source = GPUCompiler.FunctionSpec(f, Base.to_tuple_type(tt), false, name) - params = TestCompilerParams() - job = GPUCompiler.CompilerJob(target, source, params) + job, kwargs = native_job(f, tt; name, kwargs...) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) + write(io, obj) flush(io) # run(`$(Clang_jll.PATH[])/clang -shared -o $path.$(Libdl.dlext) $path`) @@ -40,8 +56,9 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf end path, name end -function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)); temp::Bool=true) - generate_shlib(f, tt, path, name) + +function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)); temp::Bool=true, kwargs...) + generate_shlib(f, tt, path, name; kwargs...) ptr = Libdl.dlopen("$(abspath(path)).$(Libdl.dlext)", Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @assert fptr != C_NULL @@ -50,6 +67,7 @@ function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler. end fptr end + function generate_shlib_fptr(path::String, name) ptr = Libdl.dlopen("$(abspath(path)).$(Libdl.dlext)", Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @@ -58,11 +76,8 @@ function generate_shlib_fptr(path::String, name) end # Return an LLVM module -function compile(f, tt, name = GPUCompiler.safe_name(repr(f))) - target = GPUCompiler.NativeCompilerTarget(;reloc=LLVM.API.LLVMRelocPIC, extern=true) - source = GPUCompiler.FunctionSpec(f, Base.to_tuple_type(tt), false, name) - params = TestCompilerParams() - job = GPUCompiler.CompilerJob(target, source, params) +function compile(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs...) + job, kwargs = native_job(f, tt; name, kwargs...) m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) return m end From 61a12b6ba21f504877c33a98f7f2265c47c1b1f4 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 15:31:09 -0700 Subject: [PATCH 016/202] clarify fib problem, add LoopVectorization test :) --- test/Project.toml | 1 + test/runtests.jl | 56 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/test/Project.toml b/test/Project.toml index 6e781cc6..5b86d437 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -4,3 +4,4 @@ LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index e0083150..7e687b48 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,7 @@ using StaticCompiler using Test using Libdl using LinearAlgebra +using LoopVectorization @testset "Basics" begin @@ -19,12 +20,20 @@ using LinearAlgebra end -fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # for some reason, if this is defined in the testset, it segfaults + +fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 @testset "Recursion" begin - # This works on the REPL but fails here fib_ptr = generate_shlib_fptr(fib, (Int,)) @test @ccall( $fib_ptr(10::Int) :: Int ) == 55 + + # Trick to work around #40990 + _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) + fib2(n) = _fib2(_fib2, n) + + fib2_ptr = generate_shlib_fptr(fib2, (Int,)) + @test @ccall( $fib2_ptr(20::Int) :: Int ) == 6765 + end # Call binaries for testing @@ -102,6 +111,25 @@ end @test_skip ( @ccall $array_sum_complex_ptr(2::Int, [1.0+im, 1.0-im]::Vector{Complex{Float64}})::Complex{Float64} ) ≈ 2.0 end + +# Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. +# We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function +@testset "Send and receive Tuple" begin + foo(u::Tuple) = 2 .* reverse(u) .- 1 # we can't just compile this as is. + + # Make a mutating function that places the output into a Ref for the caller to grab: + foo!(out::Ref{<:Tuple}, u::Tuple) = (out[] = foo(u); return nothing) + + foo_ptr = generate_shlib_fptr(foo!, Tuple{Base.RefValue{NTuple{3, Int}}, NTuple{3, Int}}) + out = Ref{NTuple{3, Int}}() + # we wrap u in a ref when we send it to the binary because LLVM expects that :( + u = Ref((1, 2, 3)) + (@ccall $foo_ptr(out::Ref{NTuple{3, Int}}, u::Ref{NTuple{3, Int}}) :: Nothing) + + @test out[] == foo(u[]) +end + + # Just to call external libraries @testset "BLAS" begin function mydot(a::Vector{Float64}) @@ -124,7 +152,27 @@ end @test_skip ccall(generate_shlib_fptr(hello, (Int,)), Int, (Int,), 1) == 1 end +@testset "LoopVectorization" begin + function mul!(C, A, B) + @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) + Cmn = zero(eltype(C)) + for k ∈ indices((A,B), (2,1)) + Cmn += A[m,k] * B[k,n] + end + C[m,n] = Cmn + end + end + mul_ptr! = generate_shlib_fptr(mul!, Tuple{Matrix{Float64}, Matrix{Float64}, Matrix{Float64}}) + + C = Array{Float64}(undef, 10, 12) + A = rand(10, 11) + B = rand(11, 12) + + @ccall $mul_ptr!(C::Matrix{Float64}, A::Matrix{Float64}, B::Matrix{Float64}) :: Nothing + @test C ≈ A*B +end + + + # data structures, dictionaries, tuples, named tuples -# passing pointers? -# @inbounds LoopVectorization From df3f23f43b307dd1a25053d5e1d58bf1cf03fa1f Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 15:46:10 -0700 Subject: [PATCH 017/202] export `native_code_*` --- src/StaticCompiler.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index dfc9445a..69c47b1f 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -5,7 +5,7 @@ using LLVM: LLVM using Libdl: Libdl -export generate_shlib, generate_shlib_fptr, compile +export generate_shlib, generate_shlib_fptr, compile, native_code_llvm, native_code_typed module TestRuntime # dummy methods From 85c24566e92c9718e177ac733e7f11355e4526f6 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 15:46:47 -0700 Subject: [PATCH 018/202] add tests for stack allocated mutable arrays --- test/Project.toml | 4 +++- test/runtests.jl | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/test/Project.toml b/test/Project.toml index 5b86d437..3dee0e56 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -4,4 +4,6 @@ LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" \ No newline at end of file +LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" +ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" +StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 7e687b48..78d62bfe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,6 +3,8 @@ using Test using Libdl using LinearAlgebra using LoopVectorization +using ManualMemory +using StrideArraysCore @testset "Basics" begin @@ -152,8 +154,10 @@ end @test_skip ccall(generate_shlib_fptr(hello, (Int,)), Int, (Int,), 1) == 1 end +# I can't beleive this works. @testset "LoopVectorization" begin function mul!(C, A, B) + # note: @tturbo does NOT work @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) Cmn = zero(eltype(C)) for k ∈ indices((A,B), (2,1)) @@ -172,7 +176,24 @@ end @test C ≈ A*B end - +# This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). +# This lets us have intermediate, mutable stack allocated arrays inside our +@testset "Alloca" begin + function f(N) + # this can hold at most 100 Int values, if you use it for more, you'll segfault + buf = ManualMemory.MemoryBuffer{100, Int}(undef) + GC.@preserve buf begin + # wrap the first N values in a PtrArray + arr = PtrArray(pointer(buf), (N,)) + arr .= 1 # mutate the array to be all 1s + sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body + end + end + + fptr = generate_shlib_fptr(f, Tuple{Int}) + @test (@ccall $fptr(20::Int) :: Int) == 20 + +end # data structures, dictionaries, tuples, named tuples From 775b5f2be8e48311b8d9e2352fbbb241efb4fbc7 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 15:49:50 -0700 Subject: [PATCH 019/202] test on version 1.7 --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 14c8967a..15f005ad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,7 @@ jobs: fail-fast: false matrix: version: + - '1.7' - 'nightly' os: - ubuntu-latest From 773cc329b4cbc035fe5523d2ebb7cd8132873836 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 15:57:08 -0700 Subject: [PATCH 020/202] fix malformed .yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 15f005ad..03f84766 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: fail-fast: false matrix: version: - - '1.7' + - '1.7' - 'nightly' os: - ubuntu-latest From d1f53696b523532eaf9794f8f1f00b469741743c Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 16:05:48 -0700 Subject: [PATCH 021/202] rename `compile` to `native_llvm_module` --- src/StaticCompiler.jl | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 69c47b1f..0de1f67a 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -5,7 +5,7 @@ using LLVM: LLVM using Libdl: Libdl -export generate_shlib, generate_shlib_fptr, compile, native_code_llvm, native_code_typed +export generate_shlib, generate_shlib_fptr, compile, native_code_llvm, native_code_typed, native_llvm_module module TestRuntime # dummy methods @@ -30,19 +30,6 @@ function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=fals GPUCompiler.CompilerJob(target, source, params), kwargs end -function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) - job, kwargs = native_job(func, types; kwargs...) - GPUCompiler.code_llvm(stdout, job; kwargs...) -end - -function native_code_typed(@nospecialize(func), @nospecialize(types); kwargs...) - job, kwargs = native_job(func, types; kwargs...) - GPUCompiler.code_typed(job; kwargs...) -end - - -# const linker = Sys.isunix() ? "ld.lld" : Sys.isapple() ? "ld64.lld" : "lld-link" - function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)); kwargs...) open(path, "w") do io job, kwargs = native_job(f, tt; name, kwargs...) @@ -50,7 +37,6 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf write(io, obj) flush(io) - # run(`$(Clang_jll.PATH[])/clang -shared -o $path.$(Libdl.dlext) $path`) run(`gcc -shared -o $path.$(Libdl.dlext) $path`) rm(path) end @@ -75,8 +61,20 @@ function generate_shlib_fptr(path::String, name) fptr end + + +function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) + job, kwargs = native_job(func, types; kwargs...) + GPUCompiler.code_llvm(stdout, job; kwargs...) +end + +function native_code_typed(@nospecialize(func), @nospecialize(types); kwargs...) + job, kwargs = native_job(func, types; kwargs...) + GPUCompiler.code_typed(job; kwargs...) +end + # Return an LLVM module -function compile(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs...) +function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs...) job, kwargs = native_job(f, tt; name, kwargs...) m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) return m From 6e20094ac2f54a3845df9a7b1f7e61241846e9e8 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Wed, 2 Feb 2022 19:16:41 -0500 Subject: [PATCH 022/202] Basic rewrite for GPUCompiler (#46) * Basic rewrite using @jpsamaroo's generate_shlib_fptr. * update, remove junk, fix tests, use `gcc` for linking * remove windows from CI * note that this doesn't work on windows * fix `compile`, add `native_code_llvm`, `native_code_typed` * clarify fib problem, add LoopVectorization test :) * add tests for stack allocated mutable arrays * test on version 1.7 * rename `compile` to `native_llvm_module` Co-authored-by: Jorge Alberto Vieyra Salas Co-authored-by: Mason Protter --- .appveyor.yml | 31 ---- .github/workflows/ci.yml | 5 +- .gitignore | 3 - .travis.yml | 24 --- Manifest.toml | 158 ++++++++++++++++++ Project.toml | 16 +- README.md | 56 +++---- docs/src/backend.md | 2 +- docs/src/helpers.md | 23 --- docs/src/index.md | 64 -------- src/StaticCompiler.jl | 104 +++++++++--- src/ccalls.jl | 90 ---------- src/extern.jl | 16 -- src/globals.jl | 164 ------------------- src/helpers/README.md | 12 -- src/helpers/helpers.jl | 37 ----- src/helpers/jlrun.jl | 61 ------- src/helpers/juliaconfig.jl | 63 ------- src/helpers/standalone-exe.jl | 148 ----------------- src/irgen.jl | 300 ---------------------------------- src/overdub.jl | 33 ---- src/serialize.jl | 242 --------------------------- src/utils.jl | 56 ------- test/Project.toml | 3 + test/ccalls.jl | 35 ---- test/globals.jl | 36 ---- test/ode.jl | 182 --------------------- test/others.jl | 73 --------- test/runtests.jl | 198 ++++++++++++++++++++-- test/standalone-exe.jl | 41 ----- 30 files changed, 458 insertions(+), 1818 deletions(-) delete mode 100644 .appveyor.yml delete mode 100644 .travis.yml create mode 100644 Manifest.toml delete mode 100644 src/ccalls.jl delete mode 100644 src/extern.jl delete mode 100644 src/globals.jl delete mode 100644 src/helpers/README.md delete mode 100644 src/helpers/helpers.jl delete mode 100644 src/helpers/jlrun.jl delete mode 100644 src/helpers/juliaconfig.jl delete mode 100644 src/helpers/standalone-exe.jl delete mode 100644 src/irgen.jl delete mode 100644 src/overdub.jl delete mode 100644 src/serialize.jl delete mode 100644 src/utils.jl delete mode 100644 test/ccalls.jl delete mode 100644 test/globals.jl delete mode 100644 test/ode.jl delete mode 100644 test/others.jl delete mode 100644 test/standalone-exe.jl diff --git a/.appveyor.yml b/.appveyor.yml deleted file mode 100644 index 16bb9b02..00000000 --- a/.appveyor.yml +++ /dev/null @@ -1,31 +0,0 @@ -# Documentation: https://github.com/JuliaCI/Appveyor.jl -environment: - matrix: - - julia_version: 1.3 - - julia_version: nightly -platform: - - x86 - - x64 -matrix: - allow_failures: - - julia_version: nightly -branches: - only: - - master - - /release-.*/ -notifications: - - provider: Email - on_build_success: false - on_build_failure: false - on_build_status_changed: false -install: - - ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1")) -build_script: - - echo "%JL_BUILD_SCRIPT%" - - C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%" -test_script: - - echo "%JL_TEST_SCRIPT%" - - C:\julia\bin\julia -e "%JL_TEST_SCRIPT%" -on_success: - - echo "%JL_CODECOV_SCRIPT%" - - C:\julia\bin\julia -e "%JL_CODECOV_SCRIPT%" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cb97e170..03f84766 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,12 +10,11 @@ jobs: fail-fast: false matrix: version: - - '1.3' + - '1.7' - 'nightly' os: - ubuntu-latest - # - macOS-latest - - windows-latest + - macOS-latest arch: - x64 steps: diff --git a/.gitignore b/.gitignore index 2ef6f28f..6e716b01 100644 --- a/.gitignore +++ b/.gitignore @@ -2,11 +2,8 @@ *.jl.cov *.jl.mem .DS_Store -Manifest.toml -!helpers/Manifest.toml /dev/ /test/standalone -/test/Manifest.toml /test/test.* test.o diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index d735ad52..00000000 --- a/.travis.yml +++ /dev/null @@ -1,24 +0,0 @@ -# Documentation: http://docs.travis-ci.com/user/languages/julia/ -language: julia -os: -- linux -- windows -julia: - - 1.3 - - nightly -matrix: - # extra linux test - include: - - os: linux - dist: bionic - - allow_failures: - - julia: nightly - fast_finish: true - -notifications: - email: false - -after_success: - - julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())' - - julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder())' diff --git a/Manifest.toml b/Manifest.toml new file mode 100644 index 00000000..44dbe949 --- /dev/null +++ b/Manifest.toml @@ -0,0 +1,158 @@ +# This file is machine-generated - editing it directly is not advised + +manifest_format = "2.0" + +[[deps.ArgTools]] +uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" + +[[deps.Artifacts]] +uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" + +[[deps.Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[deps.CEnum]] +git-tree-sha1 = "215a9aa4a1f23fbd05b92769fdd62559488d70e9" +uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82" +version = "0.4.1" + +[[deps.Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[deps.Downloads]] +deps = ["ArgTools", "LibCURL", "NetworkOptions"] +uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" + +[[deps.ExprTools]] +git-tree-sha1 = "56559bbef6ca5ea0c0818fa5c90320398a6fbf8d" +uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04" +version = "0.1.8" + +[[deps.GPUCompiler]] +deps = ["ExprTools", "InteractiveUtils", "LLVM", "Libdl", "Logging", "TimerOutputs", "UUIDs"] +git-tree-sha1 = "abd824e1f2ecd18d33811629c781441e94a24e81" +uuid = "61eb1bfa-7361-4325-ad38-22787b887f55" +version = "0.13.11" + +[[deps.InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[deps.JLLWrappers]] +deps = ["Preferences"] +git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" +uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" +version = "1.4.1" + +[[deps.LLVM]] +deps = ["CEnum", "LLVMExtra_jll", "Libdl", "Printf", "Unicode"] +git-tree-sha1 = "f8dcd7adfda0dddaf944e62476d823164cccc217" +uuid = "929cbde3-209d-540e-8aea-75f648917ca0" +version = "4.7.1" + +[[deps.LLVMExtra_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "62115afed394c016c2d3096c5b85c407b48be96b" +uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab" +version = "0.0.13+1" + +[[deps.LibCURL]] +deps = ["LibCURL_jll", "MozillaCACerts_jll"] +uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" + +[[deps.LibCURL_jll]] +deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] +uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" + +[[deps.LibGit2]] +deps = ["Base64", "NetworkOptions", "Printf", "SHA"] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" + +[[deps.LibSSH2_jll]] +deps = ["Artifacts", "Libdl", "MbedTLS_jll"] +uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" + +[[deps.Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[deps.Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[deps.Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[deps.MbedTLS_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" + +[[deps.MozillaCACerts_jll]] +uuid = "14a3606d-f60d-562e-9121-12d972cd8159" + +[[deps.NetworkOptions]] +uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" + +[[deps.Pkg]] +deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" + +[[deps.Preferences]] +deps = ["TOML"] +git-tree-sha1 = "2cf929d64681236a2e074ffafb8d568733d2e6af" +uuid = "21216c6a-2e73-6563-6e65-726566657250" +version = "1.2.3" + +[[deps.Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[deps.REPL]] +deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] +uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" + +[[deps.Random]] +deps = ["SHA", "Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[deps.SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" + +[[deps.Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[deps.Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[deps.TOML]] +deps = ["Dates"] +uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" + +[[deps.Tar]] +deps = ["ArgTools", "SHA"] +uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" + +[[deps.TimerOutputs]] +deps = ["ExprTools", "Printf"] +git-tree-sha1 = "97e999be94a7147d0609d0b9fc9feca4bf24d76b" +uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" +version = "0.5.15" + +[[deps.UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[[deps.Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[deps.Zlib_jll]] +deps = ["Libdl"] +uuid = "83775a58-1f1d-513f-b197-d71354ab007a" + +[[deps.nghttp2_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" + +[[deps.p7zip_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" diff --git a/Project.toml b/Project.toml index 583ae233..81a4e927 100644 --- a/Project.toml +++ b/Project.toml @@ -1,23 +1,17 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short"] -version = "0.1.0" +version = "0.2.0" [deps] -Cassette = "7057c7e9-c182-5462-911a-8362d720325c" -DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" -MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" -TypedCodeUtils = "687fb87b-adea-59d5-9be9-82253b54685d" [compat] -DataStructures = "0.17" -Cassette = "0.3" -LLVM = "1.3" -TypedCodeUtils = "0.1" -MacroTools = "0.5" -julia = "1.2" +julia = "1.7" +GPUCompiler = "0.13" +LLVM = "4" [extras] Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" diff --git a/README.md b/README.md index 8b09b28b..55780052 100644 --- a/README.md +++ b/README.md @@ -9,52 +9,38 @@ This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. It is also meant for cross compilation, so Julia code can be compiled for other targets, including WebAssembly and embedded targets. ## Installation and Usage + ```julia using Pkg Pkg.add(PackageSpec( url = "https://github.com/tshort/StaticCompiler.jl", rev = "master")) ``` -```julia -using StaticCompiler -``` -**Documentation**: [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://tshort.github.io/StaticCompiler.jl/dev) - -## Approach -This package uses the [LLVM package](https://github.com/maleadt/LLVM.jl) to generate code in the same fashion as [CUDAnative](https://github.com/JuliaGPU/CUDAnative.jl). - -Some of the key details of this approach are: - -* **ccalls and cglobal** -- When Julia compiles code CUDAnative style, `ccall` and `cglobal` references get compiled to a direct pointer. `StaticCompiler` converts these to symbol references for later linking. For `ccall` with a tuple call to a symbol in a library, `Cassette` is used to convert that to just a symbol reference (no dynamic library loading). - -* **Global variables** -- A lot of code gets compiled with global variables, and these get compiled to a direct pointer. `StaticCompiler` includes a basic serialize/deserialize approach. Right now, this is fairly basic, and it takes shortcuts for some objects by swapping in wrong types. This can work because many times, the objects are not really used in the code. Finding the global variable can be a little tricky because the pointer is converted to a Julia object with `unsafe_pointer_to_objref`, and that segfaults for some addresses. How to best handle cases like that is still to be determined. - -* **Initialization** -- If libjulia is used, some init code needs to be run to set up garbage collection and other things. For this, a basic `blank.ji` file is used to feed `jl_init_with_image`. - -Long term, a better approach may be to use Julia's standard compilation techniques with "tree shaking" to generate a reduced system image (see [here](https://github.com/JuliaLang/julia/issues/33670)). - -## Example -The API still needs work, but here is the general approach right now: ```julia using StaticCompiler -m = irgen(cos, Tuple{Float64}) -write(m, "cos.bc") -write_object(m, "cos.o") -``` +f(x) = 2x -`cos.o` should contain a function called `cos`. From there, you need to convert to link as needed with `libjulia`. +# compile `f` and return an LLVM module +m = compile(f, (Int,)) -See the `test` directory for more information and types of code that currently run. The most advanced example that works is a call to an ODE solution using modified code from [ODE.jl](https://github.com/JuliaDiffEq/ODE.jl). For information on compiling and linking to an executable, see [test/standalone-exe.jl](./test/standalone-exe.jl). +# compile `f` and write to a shared library ("f.so" or "f.dll") +generate_shlib(f, (Int,), "libf") +# find a function pointer for this shared library +fptr = generate_shlib_fptr("libf", "f") +@ccall $fptr(2::Int)::Int -## Known limitations - -* It won't work for recursive code. Jameson's [codegen-norecursion](https://github.com/JuliaLang/julia/tree/jn/codegen-norecursion) should fix that when merged. - -* `cfunction` is not supported. +# do this in one step (this time with a temporary shared library) +fptr = generate_shlib_fptr(f, (Int,)) +@ccall $fptr(2::Int)::Int +``` -* Generic code that uses `jl_apply_generic` does not work. One strategy for this is to use Cassette to swap out known code that uses dynamic calls. Another approach is to write something like `jl_apply_generic` to implement dynamic calls. +## Approach -* The use of Cassette makes it more difficult for Julia to infer some things, and only type-stable code can be statically compiled with this approach. +This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. -* It's only been tested on Linux and Windows. +## Limitations -Finally, this whole approach is young and likely brittle. Do not expect it to work for your code. +* This package currently requires that you have `gcc` installed and in your system's `PATH`. This is probably pretty easy to fix, we only use `gcc` for linking. In theory Clang_jll or LLVM_full_jll should be able to do this, and be managed through Julia's package manager. +* No heap allocations (e.g. creating an array or a string) are allowed inside a statically compiled function body. If you try to run such a function, you will get a segfault. +** It's sometimes possible you won't get a segfault if you define and run the function in the same session, but trying to call the compiled function in a new julia session will definitely segfault. +* Lots of other limitations too. E.g. there's an example in tests/runtests.jl where summing a vector of `Complex{Float32}` is fine, but segfaults on `Complex{Float64}`. +* Doesn't currently work on Windows \ No newline at end of file diff --git a/docs/src/backend.md b/docs/src/backend.md index 523901c2..1e08a76c 100644 --- a/docs/src/backend.md +++ b/docs/src/backend.md @@ -7,4 +7,4 @@ Pages = ["backend.md"] ```@autodocs Modules = [StaticCompiler] Pages = readdir("../src") -``` +``` \ No newline at end of file diff --git a/docs/src/helpers.md b/docs/src/helpers.md index c07d4df5..e69de29b 100644 --- a/docs/src/helpers.md +++ b/docs/src/helpers.md @@ -1,23 +0,0 @@ -# Helpers -Note that the helpers defined here are used in tests, and they are useful to test out code in the REPL. - -```julia -twox(x) = 2x -# run code in the REPL -@jlrun twox(3) -# compile to an executable in a `standalone` directory -exegen([ (twox, Tuple{Int}, 4) ]) -``` - -These are not meant to be a permanent part of the API. They are just for testing. - - -```@index -Modules = [StaticCompiler] -Pages = ["helpers.md"] -``` - -```@autodocs -Modules = [StaticCompiler] -Pages = readdir("../src/helpers") -``` diff --git a/docs/src/index.md b/docs/src/index.md index 760d5e6e..e69de29b 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -1,64 +0,0 @@ -```@meta -CurrentModule = StaticCompiler -``` - -# StaticCompiler - -[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://tshort.github.io/StaticCompiler.jl/dev) -[![Build Status](https://travis-ci.com/tshort/StaticCompiler.jl.svg?branch=master)](https://travis-ci.com/tshort/StaticCompiler.jl) -[![Build Status](https://ci.appveyor.com/api/projects/status/github/tshort/StaticCompiler.jl?svg=true)](https://ci.appveyor.com/project/tshort/StaticCompiler-jl) -[![Codecov](https://codecov.io/gh/tshort/StaticCompiler.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/tshort/StaticCompiler.jl) -[![Coveralls](https://coveralls.io/repos/github/tshort/StaticCompiler.jl/badge.svg?branch=master)](https://coveralls.io/github/tshort/StaticCompiler.jl?branch=master) - -This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. It is also meant for cross compilation, so Julia code can be compiled for other targets, including WebAssembly and embedded targets. - -## Installation and Usage -```julia -using Pkg -Pkg.add(PackageSpec( url = "https://github.com/tshort/StaticCompiler.jl", rev = "master")) -``` -```julia -using StaticCompiler -``` -**Documentation**: [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://tshort.github.io/StaticCompiler.jl/dev) - -## Approach -This package uses the [LLVM package](https://github.com/maleadt/LLVM.jl) to generate code in the same fashion as [CUDAnative](https://github.com/JuliaGPU/CUDAnative.jl). - -Some of the key details of this approach are: - -* **ccalls and cglobal** -- When Julia compiles code CUDAnative style, `ccall` and `cglobal` references get compiled to a direct pointer. `StaticCompiler` converts these to symbol references for later linking. For `ccall` with a tuple call to a symbol in a library, `Cassette` is used to convert that to just a symbol reference (no dynamic library loading). - -* **Global variables** -- A lot of code gets compiled with global variables, and these get compiled to a direct pointer. `StaticCompiler` includes a basic serialize/deserialize approach. Right now, this is fairly basic, and it takes shortcuts for some objects by swapping in wrong types. This can work because many times, the objects are not really used in the code. Finding the global variable can be a little tricky because the pointer is converted to a Julia object with `unsafe_pointer_to_objref`, and that segfaults for some addresses. How to best handle cases like that is still to be determined. - -* **Initialization** -- If libjulia is used, some init code needs to be run to set up garbage collection and other things. For this, a basic `blank.ji` file is used to feed `jl_init_with_image`. - -Long term, a better approach may be to use Julia's standard compilation techniques with "tree shaking" to generate a reduced system image (see [here](https://github.com/JuliaLang/julia/issues/33670)). - -## Example -The API still needs work, but here is the general approach right now: - -```julia -using StaticCompiler -m = irgen(cos, Tuple{Float64}) -write(m, "cos.bc") -write_object(m, "cos.o") -``` - -`cos.o` should contain a function called `cos`. From there, you need to convert to link as needed with `libjulia`. - -See the `test` directory for more information and types of code that currently run. The most advanced example that works is a call to an ODE solution using modified code from [ODE.jl](https://github.com/JuliaDiffEq/ODE.jl). For information on compiling and linking to an executable, see [test/standalone-exe.jl](https://github.com/tshort/StaticCompiler.jl/blob/master/test/standalone-exe.jl). - -## Known limitations - -* It won't work for recursive code. Jameson's [codegen-norecursion](https://github.com/JuliaLang/julia/tree/jn/codegen-norecursion) should fix that when merged. - -* `cfunction` is not supported. - -* Generic code that uses `jl_apply_generic` does not work. One strategy for this is to use Cassette to swap out known code that uses dynamic calls. Another approach is to write something like `jl_apply_generic` to implement dynamic calls. - -* The use of Cassette makes it more difficult for Julia to infer some things, and only type-stable code can be statically compiled with this approach. - -* It's only been tested on Linux and Windows. - -Finally, this whole approach is young and likely brittle. Do not expect it to work for your code. diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 5f16b621..0de1f67a 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -1,28 +1,84 @@ module StaticCompiler -export irgen, write_object, @extern - -import Libdl - -using LLVM -using LLVM.Interop -using TypedCodeUtils -import TypedCodeUtils: reflect, lookthrough, canreflect, - DefaultConsumer, Reflection, Callsite, - identify_invoke, identify_call, identify_foreigncall, - process_invoke, process_call -using MacroTools -using DataStructures: MultiDict - - -include("serialize.jl") -include("utils.jl") -include("ccalls.jl") -include("globals.jl") -include("overdub.jl") -include("irgen.jl") -include("extern.jl") - -include("helpers/helpers.jl") +using GPUCompiler: GPUCompiler +using LLVM: LLVM +using Libdl: Libdl + + +export generate_shlib, generate_shlib_fptr, compile, native_code_llvm, native_code_typed, native_llvm_module + +module TestRuntime + # dummy methods + signal_exception() = return + # HACK: if malloc returns 0 or traps, all calling functions (like jl_box_*) + # get reduced to a trap, which really messes with our test suite. + malloc(sz) = Ptr{Cvoid}(Int(0xDEADBEEF)) + report_oom(sz) = return + report_exception(ex) = return + report_exception_name(ex) = return + report_exception_frame(idx, func, file, line) = return +end + +struct TestCompilerParams <: GPUCompiler.AbstractCompilerParams end +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,TestCompilerParams}) = TestRuntime + + +function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) + source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) + target = GPUCompiler.NativeCompilerTarget(always_inline=true) + params = TestCompilerParams() + GPUCompiler.CompilerJob(target, source, params), kwargs +end + +function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)); kwargs...) + open(path, "w") do io + job, kwargs = native_job(f, tt; name, kwargs...) + obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) + + write(io, obj) + flush(io) + run(`gcc -shared -o $path.$(Libdl.dlext) $path`) + rm(path) + end + path, name +end + +function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)); temp::Bool=true, kwargs...) + generate_shlib(f, tt, path, name; kwargs...) + ptr = Libdl.dlopen("$(abspath(path)).$(Libdl.dlext)", Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_$name") + @assert fptr != C_NULL + if temp + atexit(()->rm("$path.$(Libdl.dlext)")) + end + fptr +end + +function generate_shlib_fptr(path::String, name) + ptr = Libdl.dlopen("$(abspath(path)).$(Libdl.dlext)", Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_$name") + @assert fptr != C_NULL + fptr +end + + + +function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) + job, kwargs = native_job(func, types; kwargs...) + GPUCompiler.code_llvm(stdout, job; kwargs...) +end + +function native_code_typed(@nospecialize(func), @nospecialize(types); kwargs...) + job, kwargs = native_job(func, types; kwargs...) + GPUCompiler.code_typed(job; kwargs...) +end + +# Return an LLVM module +function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs...) + job, kwargs = native_job(f, tt; name, kwargs...) + m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + return m +end + end # module diff --git a/src/ccalls.jl b/src/ccalls.jl deleted file mode 100644 index 4314fab5..00000000 --- a/src/ccalls.jl +++ /dev/null @@ -1,90 +0,0 @@ - -""" - find_ccalls(f, tt) - -Returns a `Dict` mapping function addresses to symbol names for all `ccall`s and -`cglobal`s called from the method. This descends into other invocations -within the method. -""" -find_ccalls(@nospecialize(f), @nospecialize(tt)) = find_ccalls(reflect(f, tt)) - -function find_ccalls(ref::Reflection) - result = Dict{Ptr{Nothing}, Symbol}() - idx = VERSION > v"1.2" ? 5 : 4 - foreigncalls = TypedCodeUtils.filter((c) -> lookthrough((c) -> c.head === :foreigncall && !(c.args[idx] isa QuoteNode && c.args[idx].value == :llvmcall), c), ref.CI.code) - # foreigncalls = TypedCodeUtils.filter((c) -> lookthrough((c) -> c.head === :foreigncall, c), ref.CI.code) - for fc in foreigncalls - sym = getsym(fc[2].args[1]) - address = eval(:(cglobal($(sym)))) - result[address] = Symbol(sym isa Tuple ? sym[1] : sym.value) - end - cglobals = TypedCodeUtils.filter((c) -> lookthrough(c -> c.head === :call && iscglobal(c.args[1]), c), ref.CI.code) - for fc in cglobals - sym = getsym(fc[2].args[2]) - address = eval(:(cglobal($(sym)))) - result[address] = Symbol(sym isa Tuple ? sym[1] : sym.value) - end - invokes = TypedCodeUtils.filter((c) -> lookthrough(identify_invoke, c), ref.CI.code) - invokes = map((arg) -> process_invoke(DefaultConsumer(), ref, arg...), invokes) - for fi in invokes - canreflect(fi) || continue - merge!(result, find_ccalls(reflect(fi))) - end - return result -end - -getsym(x) = x -getsym(x::String) = QuoteNode(Symbol(x)) -getsym(x::QuoteNode) = x -getsym(x::Expr) = eval.((x.args[2], x.args[3])) - -iscglobal(x) = x == cglobal || x isa GlobalRef && x.name == :cglobal - - -""" - fix_ccalls!(mod::LLVM.Module, d) - -Replace function addresses with symbol names in `mod`. The symbol names are -meant to be linked to `libjulia` or other libraries. -`d` is a `Dict` mapping a function address to symbol name for `ccall`s. -""" -function fix_ccalls!(mod::LLVM.Module, d) - for fun in functions(mod), blk in blocks(fun), instr in instructions(blk) - if instr isa LLVM.CallInst - dest = called_value(instr) - if dest isa ConstantExpr && occursin("inttoptr", string(dest)) - # @show instr - # @show dest - argtypes = [llvmtype(op) for op in operands(instr)] - nargs = length(parameters(eltype(argtypes[end]))) - # num_extra_args = 1 + length(collect(eachmatch(r"jl_roots", string(instr)))) - ptr = Ptr{Cvoid}(convert(Int, first(operands(dest)))) - if haskey(d, ptr) - sym = d[ptr] - newdest = LLVM.Function(mod, string(sym), LLVM.FunctionType(llvmtype(instr), argtypes[1:nargs])) - LLVM.linkage!(newdest, LLVM.API.LLVMExternalLinkage) - replace_uses!(dest, newdest) - end - end - elseif instr isa LLVM.LoadInst && occursin("inttoptr", string(instr)) - # dest = called_value(instr) - for op in operands(instr) - lastop = op - if occursin("inttoptr", string(op)) - # @show instr - if occursin("addrspacecast", string(op)) || occursin("getelementptr", string(op)) - op = first(operands(op)) - end - first(operands(op)) isa LLVM.ConstantInt || continue - ptr = Ptr{Cvoid}(convert(Int, first(operands(op)))) - if haskey(d, ptr) - obj = d[ptr] - newdest = GlobalVariable(mod, llvmtype(instr), string(d[ptr])) - LLVM.linkage!(newdest, LLVM.API.LLVMExternalLinkage) - replace_uses!(op, newdest) - end - end - end - end - end -end diff --git a/src/extern.jl b/src/extern.jl deleted file mode 100644 index 6c0424a9..00000000 --- a/src/extern.jl +++ /dev/null @@ -1,16 +0,0 @@ -""" - @extern(fun, returntype, argtypes, args...) - -Creates a call to an external function meant to be included at link time. -Use the same conventions as `ccall`. - -This transforms into the following `ccall`: - - ccall("extern fun", llvmcall, returntype, argtypes, args...) -""" -macro extern(name, rettyp, argtyp, args...) - externfun = string("extern ", name isa AbstractString || name isa Symbol ? name : name.value) - Expr(:call, :ccall, externfun, esc(:llvmcall), esc(rettyp), - Expr(:tuple, esc.(argtyp.args)...), esc.(args)...) -end - diff --git a/src/globals.jl b/src/globals.jl deleted file mode 100644 index e2583388..00000000 --- a/src/globals.jl +++ /dev/null @@ -1,164 +0,0 @@ -struct GlobalsContext - invokes::Set{Any} -end -GlobalsContext() = GlobalsContext(Set()) - - -""" - fix_globals!(mod::LLVM.Module) - -Replace function addresses in `mod` with references to global data structures. -For each global variable, two LLVM global objects are created: - -* `jl.global.data` -- An LLVM 'i8' vector holding a serialized version of the Julia object. -* `jl.global` -- A pointer to the unserialized Julia object. - -The `inttopt` with the function address is replaced by `jl.global`. - -A function `jl_init_globals` is added to `mod`. This function deserializes the data in -`jl.global.data` and updates `jl.global`. -""" - -_opcode(x::LLVM.ConstantExpr) = LLVM.API.LLVMGetConstOpcode(LLVM.ref(x)) - -function fix_globals!(mod::LLVM.Module) - # Create a `jl_init_globals` function. - jl_init_globals_func = LLVM.Function(mod, "jl_init_globals", - LLVM.FunctionType(julia_to_llvm(Cvoid), LLVMType[])) - jl_init_global_entry = BasicBlock(jl_init_globals_func, "entry", context(mod)) - - # Definitions for utility functions - func_type = LLVM.FunctionType(julia_to_llvm(Any), LLVMType[LLVM.PointerType(julia_to_llvm(Int8))]) - deserialize_funs = Dict() - - uint8_t = julia_to_llvm(UInt8) - - ctx = SerializeContext() - es = [] - objs = Set() - gptridx = Dict() - instrs = [] - gptrs = [] - j = 1 # counter for position in gptridx - Builder(context(mod)) do builder - toinstr!(x) = x - function toinstr!(x::LLVM.ConstantExpr) - if _opcode(x) == LLVM.API.LLVMAddrSpaceCast - val = toinstr!(first(operands(x))) - ret = addrspacecast!(builder, val, llvmtype(x)) - return ret - elseif _opcode(x) == LLVM.API.LLVMGetElementPtr - ops = operands(x) - val = toinstr!(first(ops)) - ret = gep!(builder, val, [ops[i] for i in 2:length(ops)]) - return ret - elseif _opcode(x) == LLVM.API.LLVMBitCast - ops = operands(x) - val = toinstr!(first(ops)) - ret = pointercast!(builder, val, llvmtype(x)) - return ret - elseif _opcode(x) == LLVM.API.LLVMIntToPtr - ptr = Ptr{Any}(convert(Int, first(operands(x)))) - obj = unsafe_pointer_to_objref(ptr) - if !in(obj, objs) - push!(es, serialize(ctx, obj)) - push!(objs, obj) - # Create pointers to the data. - gptr = GlobalVariable(mod, julia_to_llvm(Any), "jl.global") - linkage!(gptr, LLVM.API.LLVMInternalLinkage) - LLVM.API.LLVMSetInitializer(LLVM.ref(gptr), LLVM.ref(null(julia_to_llvm(Any)))) - push!(gptrs, gptr) - gptridx[obj] = j - j += 1 - end - gptr = gptrs[gptridx[obj]] - gptr2 = load!(builder, gptr) - ret = pointercast!(builder, gptr2, llvmtype(x)) - return ret - end - return x - end - for fun in functions(mod) - if startswith(LLVM.name(fun), "jfptr") - unsafe_delete!(mod, fun) - continue - end - - for blk in blocks(fun), instr in instructions(blk) - # Set up functions to walk the operands of the instruction - # and convert appropriate ConstantExpr's to instructions. - # Look for `LLVMIntToPtr` expressions. - position!(builder, instr) - ops = operands(instr) - N = opcode(instr) == LLVM.API.LLVMCall ? length(ops) - 1 : length(ops) - if opcode(instr) == LLVM.API.LLVMCall && name(last(operands(instr))) == "jl_type_error" - continue - end - for i in 1:N - try - if opcode(instr) == LLVM.API.LLVMPHI - position!(builder, last(instructions(LLVM.incoming(instr)[i][2]))) - end - ops[i] = toinstr!(ops[i]) - catch x - end - end - end - end - end - nglobals = length(es) - #@show mod - #verify(mod) - for i in 1:nglobals - # Assign the appropriate function argument to the appropriate global. - es[i] = :(unsafe_store!($((Symbol("global", i))), $(es[i]))) - end - # Define the deserializing function. - fune = quote - function _deserialize_globals(Vptr, $((Symbol("global", i) for i in 1:nglobals)...)) - $(ctx.init...) - $(es...) - return - end - end - # @show fune - # Execute the deserializing function. - deser_fun = eval(fune) - v = take!(ctx.io) - gv_typ = LLVM.ArrayType(uint8_t, length(v)) - data = LLVM.GlobalVariable(mod, gv_typ, "jl.global.data") - linkage!(data, LLVM.API.LLVMExternalLinkage) - constant!(data, true) - LLVM.API.LLVMSetInitializer(LLVM.ref(data), - LLVM.API.LLVMConstArray(LLVM.ref(uint8_t), - [LLVM.ref(ConstantInt(uint8_t, x)) for x in v], - UInt32(length(v)))) - Builder(context(mod)) do builder - dataptr = gep!(builder, data, [ConstantInt(0, context(mod)), ConstantInt(0, context(mod))]) - - # Create the Julia object from `data` and include that in `init_fun`. - position!(builder, jl_init_global_entry) - gfunc_type = LLVM.FunctionType(julia_to_llvm(Cvoid), - LLVMType[LLVM.PointerType(julia_to_llvm(Int8)), - Iterators.repeated(LLVM.FunctionType(julia_to_llvm(Any)), nglobals)...]) - deserialize_globals_func = LLVM.Function(mod, "_deserialize_globals", gfunc_type) - LLVM.linkage!(deserialize_globals_func, LLVM.API.LLVMExternalLinkage) - for i in 1:nglobals - # The following fix is to match the argument types which are an integer, not a %jl_value_t**. - gptrs[i] = LLVM.ptrtoint!(builder, gptrs[i], julia_to_llvm(Csize_t)) - end - LLVM.call!(builder, deserialize_globals_func, LLVM.Value[dataptr, gptrs...]) - ret!(builder) - end - tt = Tuple{Ptr{UInt8}, Iterators.repeated(Ptr{Any}, nglobals)...} - deser_mod = irgen(deser_fun, tt, overdub = false, fix_globals = false, optimize_llvm = false) - d = find_ccalls(deser_fun, tt) - fix_ccalls!(deser_mod, d) - # rename deserialization function to "_deserialize_globals" - fun = first(TypedCodeUtils.filter(x -> LLVM.name(x) == "_deserialize_globals", functions(deser_mod)))[2] - # LLVM.name!(fun, "_deserialize_globals") - linkage!(fun, LLVM.API.LLVMExternalLinkage) - # link into the main module - LLVM.link!(mod, deser_mod) - return -end diff --git a/src/helpers/README.md b/src/helpers/README.md deleted file mode 100644 index e443db6a..00000000 --- a/src/helpers/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Helpers -Note that the helpers defined here are used in tests, and they are useful to test out code in the REPL. - -```julia -twox(x) = 2x -# run code in the REPL -@jlrun twox(3) -# compile to an executable in a `standalone` directory -exegen([ (twox, Tuple{Int}, 4) ]) -``` - -These are not meant to be a permanent part of the API. They are just for testing. diff --git a/src/helpers/helpers.jl b/src/helpers/helpers.jl deleted file mode 100644 index 1f041540..00000000 --- a/src/helpers/helpers.jl +++ /dev/null @@ -1,37 +0,0 @@ -""" -Returns shellcmd string for different OS. Optionally, checks for gcc installation. -""" -function _shellcmd(checkInstallation::Bool = false) - - if Sys.isunix() - shellcmd = "gcc" - elseif Sys.iswindows() - shellcmd = ["cmd", "/c", "gcc"] - else - error("run command not defined") - end - - if checkInstallation - # Checking gcc installation - try - run(`$shellcmd -v`) - catch - @warn "Make sure gcc compiler is installed: https://gcc.gnu.org/install/binaries.html and is on the path, othetwise some of the functions will return errors" - return nothing - end - end - - return shellcmd -end - -shellcmd = _shellcmd(true) # is used in @jlrun and exegen() - - -export @jlrun -include("jlrun.jl") - -export ldflags, ldlibs, cflags # are used in exegen -include("juliaconfig.jl") - -export exegen -include("standalone-exe.jl") diff --git a/src/helpers/jlrun.jl b/src/helpers/jlrun.jl deleted file mode 100644 index f2e8e165..00000000 --- a/src/helpers/jlrun.jl +++ /dev/null @@ -1,61 +0,0 @@ -using Libdl, LLVM - -function show_inttoptr(mod) - for fun in LLVM.functions(mod), - blk in LLVM.blocks(fun), - instr in LLVM.instructions(blk) - - s = string(instr) - if occursin("inttoptr", s) && occursin(r"[0-9]{8,30}", s) - println(LLVM.name(fun), " ---------------------------") - @show instr - println() - end - end -end - -""" -Compiles function call provided and calls it with `ccall` using the shared library that was created. -""" -macro jlrun(e) - - fun = e.args[1] - efun = esc(fun) - args = length(e.args) > 1 ? e.args[2:end] : Any[] - libpath = abspath("test.o") - dylibpath = abspath("test.so") - tt = Tuple{(typeof(eval(a)) for a in args)...} - if length(e.args) > 1 - ct = code_typed(Base.eval(__module__, fun), tt) - else - ct = code_typed(Base.eval(__module__, fun)) - end - rettype = ct[1][2] - bindir = joinpath(dirname(Sys.BINDIR), "tools") - libdir = joinpath(dirname(Sys.BINDIR), "lib") - - runCommand = :(run( - $(`$shellcmd -shared -fPIC -o test.so -L$libdir test.o -ljulia`), - wait = true, - )) - - quote - m = irgen($efun, $tt) - # m = irgen($efun, $tt, overdub = false) - LLVM.verify(m) - # show_inttoptr(m) - write(m, "test.bc") - write_object(m, "test.o") - $runCommand - dylib = Libdl.dlopen($dylibpath) - ccall(Libdl.dlsym(dylib, "jl_init_globals"), Cvoid, ()) - res = ccall( - Libdl.dlsym(dylib, $(Meta.quot(fun))), - $rettype, - ($((typeof(eval(a)) for a in args)...),), - $(eval.(args)...), - ) - Libdl.dlclose(dylib) - res - end -end diff --git a/src/helpers/juliaconfig.jl b/src/helpers/juliaconfig.jl deleted file mode 100644 index 039e0905..00000000 --- a/src/helpers/juliaconfig.jl +++ /dev/null @@ -1,63 +0,0 @@ -# from PackageCompilerX: https://github.com/KristofferC/PackageCompilerX.jl/blob/c1a90edfaa28907edf2edbbc734ef8afdeeaca80/src/juliaconfig.jl -# adopted from https://github.com/JuliaLang/julia/blob/release-0.6/contrib/julia-config.jl - -function shell_escape(str) - str = replace(str, "'" => "'\''") - return "'$str'" -end - -function julia_libdir() - return if ccall(:jl_is_debugbuild, Cint, ()) != 0 - dirname(abspath(Libdl.dlpath("libjulia-debug"))) - else - dirname(abspath(Libdl.dlpath("libjulia"))) - end -end - -function julia_private_libdir() - @static if Sys.iswindows() - return julia_libdir() - else - return abspath(Sys.BINDIR, Base.PRIVATE_LIBDIR) - end -end - -julia_includedir() = abspath(Sys.BINDIR, Base.INCLUDEDIR, "julia") - -function ldflags() - fl = "-L$(shell_escape(julia_libdir()))" - if Sys.iswindows() - fl = fl * " -Wl,--stack,8388608" - fl = fl * " -Wl,--export-all-symbols" - elseif Sys.islinux() - fl = fl * " -Wl,--export-dynamic" - end - return fl -end - -# TODO -function ldlibs(relative_path=nothing) - libname = if ccall(:jl_is_debugbuild, Cint, ()) != 0 - "julia-debug" - else - "julia" - end - if Sys.islinux() - return "-Wl,-rpath-link,$(shell_escape(julia_libdir())) -Wl,-rpath-link,$(shell_escape(julia_private_libdir())) -l$libname" - elseif Sys.iswindows() - return "-l$libname -lopenlibm" - else - return "-l$libname" - end -end - -function cflags() - flags = IOBuffer() - print(flags, "-std=gnu99") - include = shell_escape(julia_includedir()) - print(flags, " -I", include) - if Sys.isunix() - print(flags, " -fPIC") - end - return String(take!(flags)) -end diff --git a/src/helpers/standalone-exe.jl b/src/helpers/standalone-exe.jl deleted file mode 100644 index 40d026bb..00000000 --- a/src/helpers/standalone-exe.jl +++ /dev/null @@ -1,148 +0,0 @@ -Ctemplate = """ -#include -#include -extern CRETTYPE FUNNAME(CARGTYPES); -extern void jl_init_with_image(const char *, const char *); -extern void jl_init_globals(void); -int main() -{ - jl_init_with_image(".", "blank.ji"); - jl_init_globals(); - printf("RETFORMAT", FUNNAME(FUNARG)); - jl_atexit_hook(0); - return 0; -} -""" - -# "signed" is removed from signed types -# duplicates will remove automatically -Cmap = Dict( - Cchar => "char", #Int8 - Cuchar => "unsigned char", #UInt8 - Cshort => "short", #Int16 - # Cstring => - Cushort => "unsigned short", #UInt16 - Cint => "int", #Int32 - Cuint => "unsigned int", #UInt32 - Clong => "long", #Int32 - Culong => "unsigned long", #UInt32 - Clonglong => "long long", #Int64 - Culonglong => "unsigned long long", #UInt64 - # Cintmax_t => "intmax_t", #Int64 - # Cuintmax_t => "uintmax_t", #UInt64 - # Csize_t => "size_t", #UInt - # Cssize_t => "ssize_t", #Int - # Cptrdiff_t => "ptrdiff_t", #Int - # Cwchar_t => "wchar_t", #Int32 - # Cwstring => - Cfloat => "float", #Float32 - Cdouble => "double", #Float64 - Nothing => "void", -) - -Cformatmap = Dict( - Cchar => "%c", #Int8 - # Cuchar => "unsigned char", #UInt8 - # Cshort => "short", #Int16 - Cstring => "%s", - # Cushort => "unsigned short", #UInt16 - Cint => "%d", #"i" #Int32 - Cuint => "%u", #UInt32 - Clong => "%ld", #Int32 - # Culong => "unsigned long", #UInt32 - Clonglong => "%lld", #Int64 - # Culonglong => "unsigned long long", #UInt64 - # Cintmax_t => "intmax_t", #Int64 - # Cuintmax_t => "uintmax_t", #UInt64 - # Csize_t => "size_t", #UInt - # Cssize_t => "ssize_t", #Int - # Cptrdiff_t => "ptrdiff_t", #Int - # Cwchar_t => "wchar_t", #Int32 - # Cwstring => - # Cfloat => "%f", #Float32 - Cdouble => "%f", #%e #Float64 -) - -""" -converts to text. returns "" for Nothing and empty Tuple. -""" -totext(x) = string(x) -totext(x::Nothing) = "" -totext(x::Tuple{}) = "" - -""" -Makes standalone executable. -""" -function exegen(funcalls) - - cd(mkpath("standalone")) do - # create `blank.ji` for initialization - julia_path = joinpath(Sys.BINDIR, Base.julia_exename()) - base_dir = dirname(Base.find_source_file("sysimg.jl")) - wd = pwd() - open(println, "blank.jl", "w") - cd(base_dir) do - run(`$(julia_path) --output-ji $(wd)/blank.ji $(wd)/blank.jl`) - end - - dir = pwd() - standalonedir = dir - bindir = string(Sys.BINDIR) - libdir = joinpath(dirname(Sys.BINDIR), "lib") - includedir = joinpath(dirname(Sys.BINDIR), "include", "julia") - if Sys.iswindows() - for fn in readdir(bindir) - if splitext(fn)[end] == ".dll" - cp(joinpath(bindir, fn), fn, force = true) - end - end - end - - flags = join((cflags(), ldflags(), ldlibs()), " ") - flags = Base.shell_split(flags) - wrapper = joinpath(@__DIR__, "embedding_wrapper.c") - if Sys.iswindows() - rpath = `` - elseif Sys.isapple() - rpath = `-Wl,-rpath,'@executable_path' -Wl,-rpath,'@executable_path/../lib'` - else - rpath = `-Wl,-rpath,\$ORIGIN:\$ORIGIN/../lib` - end - - for (func, tt, val) in funcalls - fname = nameof(func) - rettype = Base.return_types(func, tt)[1] - argtype = length(tt.types) > 0 ? tt.types[1] : Nothing - fmt = Cformatmap[rettype] - Ctxt = foldl(replace, - ( - "FUNNAME" => fname, - "CRETTYPE" => Cmap[rettype], - "RETFORMAT" => fmt, - "CARGTYPES" => Cmap[argtype], - "FUNARG" => totext(val), - ), - init = Ctemplate) - write("$fname.c", Ctxt) - m = StaticCompiler.irgen(func, tt) - # StaticCompiler.show_inttoptr(m) - # @show m - dlext = Libdl.dlext - exeext = Sys.iswindows() ? ".exe" : "" - if Sys.isapple() - o_file = `-Wl,-all_load $fname.o` - else - o_file = `-Wl,--whole-archive $fname.o -Wl,--no-whole-archive` - end - extra = Sys.iswindows() ? `-Wl,--export-all-symbols` : `` - write(m, "$fname.bc") - write_object(m, "$fname.o") - - run(`$shellcmd -shared -fpic -L$libdir -o lib$fname.$dlext $o_file -Wl,-rpath,$libdir -ljulia $extra`) - run(`$shellcmd -c -std=gnu99 -I$includedir -DJULIA_ENABLE_THREADING=1 -fPIC $fname.c`) - #run(`$shellcmd -o $fname $fname.o -L$libdir -L$standalonedir -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-rpath,'.' -Wl,-rpath,$libdir -ljulia -l$fname -O2 $rpath $flags`) - run(`$shellcmd -o $fname $fname.o -L$libdir -L$standalonedir -Wl,-rpath,'.' -Wl,-rpath,$libdir -ljulia -l$fname -O2 $rpath $flags`) - end - end - -end diff --git a/src/irgen.jl b/src/irgen.jl deleted file mode 100644 index cc9811c4..00000000 --- a/src/irgen.jl +++ /dev/null @@ -1,300 +0,0 @@ - -struct LLVMNativeCode # thin wrapper - p::Ptr{Cvoid} -end - -function xlinfo(f, tt) - # get the method instance - world = typemax(UInt) - g = (args...) -> Cassette.overdub(ctx, f, args...) - meth = which(g, tt) - sig_tt = Tuple{typeof(g), tt.parameters...} - (ti, env) = ccall(:jl_type_intersection_with_env, Any, - (Any, Any), sig_tt, meth.sig)::Core.SimpleVector - - if VERSION >= v"1.2.0-DEV.320" - meth = Base.func_for_method_checked(meth, ti, env) - else - meth = Base.func_for_method_checked(meth, ti) - end - - return ccall(:jl_specializations_get_linfo, Ref{Core.MethodInstance}, - (Any, Any, Any, UInt), meth, ti, env, world) -end - -""" -Returns an LLVMNativeCode object for the function call `f` with TupleTypes `tt`. -""" -function raise_exception(insblock::BasicBlock, ex::Value) -end - -# const jlctx = Ref{LLVM.Context}() - -# function __init__() -# jlctx[] = LLVM.Context(convert(LLVM.API.LLVMContextRef, -# cglobal(:jl_LLVMContext, Nothing))) -# end - -""" - irgen(func, tt; - optimize = true, - optimize_llvm = true, - fix_globals = true, - overdub = true, - module_setup = (m) -> nothing) - -Generates Julia IR targeted for static compilation. -`ccall` and `cglobal` uses have pointer references changed to symbols -meant to be linked with libjulia and other libraries. - -`optimize` controls Julia-side optimization. `optimize_llvm` controls -optimization on the LLVM side. - -If `overdub == true` (the default), Cassette is used to swap out -`ccall`s with a tuple of library and symbol. - -`module_setup` is an optional function to control setup of modules. It takes an LLVM -module as input. -""" -function irgen(@nospecialize(func), @nospecialize(tt); - optimize = true, - optimize_llvm = true, - fix_globals = true, - overdub = true, - module_setup = (m) -> nothing) - # get the method instance - isa(func, Core.Builtin) && error("function is not a generic function") - world = typemax(UInt) - gfunc = overdub ? (args...) -> Cassette.overdub(ctx, func, args...) : func - meth = which(gfunc, tt) - sig_tt = Tuple{typeof(gfunc), tt.parameters...} - (ti, env) = ccall(:jl_type_intersection_with_env, Any, - (Any, Any), sig_tt, meth.sig)::Core.SimpleVector - - if VERSION >= v"1.2.0-DEV.320" - meth = Base.func_for_method_checked(meth, ti, env) - else - meth = Base.func_for_method_checked(meth, ti) - end - - linfo = ccall(:jl_specializations_get_linfo, Ref{Core.MethodInstance}, - (Any, Any, Any, UInt), meth, ti, env, world) - - current_method = nothing - last_method_instance = nothing - call_stack = Vector{Core.MethodInstance}() - global method_map = Dict{String,Core.MethodInstance}() - global dependencies = MultiDict{Core.MethodInstance,LLVM.Function}() - # set-up the compiler interface - function hook_module_setup(ref::Ptr{Cvoid}) - ref = convert(LLVM.API.LLVMModuleRef, ref) - module_setup(LLVM.Module(ref)) - end - function hook_raise_exception(insblock::Ptr{Cvoid}, ex::Ptr{Cvoid}) - insblock = convert(LLVM.API.LLVMValueRef, insblock) - ex = convert(LLVM.API.LLVMValueRef, ex) - raise_exception(BasicBlock(insblock), Value(ex)) - end - function postprocess(ir) - # get rid of jfptr wrappers - for llvmf in functions(ir) - startswith(LLVM.name(llvmf), "jfptr_") && unsafe_delete!(ir, llvmf) - end - - return - end - function hook_module_activation(ref::Ptr{Cvoid}) - ref = convert(LLVM.API.LLVMModuleRef, ref) - global ir = LLVM.Module(ref) - postprocess(ir) - - # find the function that this module defines - llvmfs = filter(llvmf -> !isdeclaration(llvmf) && - linkage(llvmf) == LLVM.API.LLVMExternalLinkage, - collect(functions(ir))) - llvmf = nothing - if length(llvmfs) == 1 - llvmf = first(llvmfs) - elseif length(llvmfs) > 1 - llvmfs = filter!(llvmf -> startswith(LLVM.name(llvmf), "julia_"), llvmfs) - if length(llvmfs) == 1 - llvmf = first(llvmfs) - end - end - insert!(dependencies, last_method_instance, llvmf) - method_map[name(llvmf)] = current_method - end - function hook_emit_function(method_instance, code, world) - push!(call_stack, method_instance) - end - function hook_emitted_function(method, code, world) - current_method = method - last_method_instance = pop!(call_stack) - # @show code - # dump(method, maxdepth=2) - # global mymeth = method - end - - params = Base.CodegenParams(cached=false, - track_allocations=false, - code_coverage=false, - static_alloc=false, - prefer_specsig=true, - module_setup=hook_module_setup, - module_activation=hook_module_activation, - raise_exception=hook_raise_exception, - emit_function=hook_emit_function, - emitted_function=hook_emitted_function, - ) - - # get the code - mod = let - ref = ccall(:jl_get_llvmf_defn, LLVM.API.LLVMValueRef, - (Any, UInt, Bool, Bool, Base.CodegenParams), - linfo, world, #=wrapper=#false, #=optimize=#false, params) - if ref == C_NULL - # error(jlctx[], "the Julia compiler could not generate LLVM IR") - end - - llvmf = LLVM.Function(ref) - LLVM.parent(llvmf) - end - - # the main module should contain a single jfptr_ function definition, - # e.g. jlcall_kernel_vadd_62977 - - # definitions = filter(f->!isdeclaration(f), functions(mod)) - definitions = Iterators.filter(f->!isdeclaration(f), collect(functions(mod))) - # definitions = collect(functions(mod)) - wrapper = let - fs = collect(Iterators.filter(f->startswith(LLVM.name(f), "jfptr_"), definitions)) - @assert length(fs) == 1 - fs[1] - end - - # the jlcall wrapper function should point us to the actual entry-point, - # e.g. julia_kernel_vadd_62984 - entry_tag = let - m = match(r"jfptr_(.+)_\d+", LLVM.name(wrapper)) - @assert m != nothing - m.captures[1] - end - unsafe_delete!(mod, wrapper) - entry = let - re = Regex("julia_$(entry_tag)_\\d+") - llvmcall_re = Regex("julia_$(entry_tag)_\\d+u\\d+") - fs = collect(Iterators.filter(f->occursin(re, LLVM.name(f)) && - !occursin(llvmcall_re, LLVM.name(f)), definitions)) - if length(fs) != 1 - compiler_error(func, tt, cap, "could not find single entry-point"; - entry=>entry_tag, available=>[LLVM.name.(definitions)]) - end - fs[1] - end - - LLVM.name!(entry, string(nameof(func))) - - # link in dependent modules - cache = Dict{String,String}() - for called_method_instance in keys(dependencies) - llvmfs = dependencies[called_method_instance] - - # link the first module - llvmf = popfirst!(llvmfs) - llvmfn = LLVM.name(llvmf) - link!(mod, LLVM.parent(llvmf)) - # process subsequent duplicate modules - for dup_llvmf in llvmfs - if Base.JLOptions().debug_level >= 2 - # link them too, to ensure accurate backtrace reconstruction - link!(mod, LLVM.parent(dup_llvmf)) - else - # don't link them, but note the called function name in a cache - dup_llvmfn = LLVM.name(dup_llvmf) - cache[dup_llvmfn] = llvmfn - end - end - end - # resolve function declarations with cached entries - for llvmf in filter(isdeclaration, collect(functions(mod))) - llvmfn = LLVM.name(llvmf) - if haskey(cache, llvmfn) - def_llvmfn = cache[llvmfn] - replace_uses!(llvmf, functions(mod)[def_llvmfn]) - unsafe_delete!(LLVM.parent(llvmf), llvmf) - end - end - # rename functions to something easier to decipher - # especially helps with overdubbed functions - for (fname, mi) in method_map - id = split(fname, "_")[end] - basename = mi.def.name - args = join(collect(mi.specTypes.parameters)[2:end], "_") - if basename == :overdub # special handling for Cassette - basename = string(mi.specTypes.parameters[3]) - basename = replace(basename, r"^typeof\(" => "") - basename = replace(basename, r"\)$" => "") - args = join(collect(mi.specTypes.parameters)[4:end], "_") - end - newname = join([basename, args, id], "_") - if haskey(functions(mod), fname) - name!(functions(mod)[fname], newname) - end - end - - d = find_ccalls(gfunc, tt) - fix_ccalls!(mod, d) - if fix_globals - fix_globals!(mod) - end - if optimize_llvm - optimize!(mod) - end - return mod -end - - -""" - optimize!(mod::LLVM.Module) - -Optimize the LLVM module `mod`. Crude for now. -Returns nothing. -""" -function optimize!(mod::LLVM.Module) - for llvmf in functions(mod) - startswith(LLVM.name(llvmf), "jfptr_") && unsafe_delete!(mod, llvmf) - startswith(LLVM.name(llvmf), "julia_") && LLVM.linkage!(llvmf, LLVM.API.LLVMExternalLinkage) - end - # triple = "wasm32-unknown-unknown-wasm" - # triple!(mod, triple) - # datalayout!(mod, "e-m:e-p:32:32-i64:64-n32:64-S128") - # LLVM.API.@apicall(:LLVMInitializeWebAssemblyTarget, Cvoid, ()) - # LLVM.API.@apicall(:LLVMInitializeWebAssemblyTargetMC, Cvoid, ()) - # LLVM.API.@apicall(:LLVMInitializeWebAssemblyTargetInfo, Cvoid, ()) - triple = "i686-pc-linux-gnu" - tm = TargetMachine(Target(triple), triple) - - ModulePassManager() do pm - # add_library_info!(pm, triple(mod)) - add_transform_info!(pm, tm) - ccall(:jl_add_optimization_passes, Cvoid, - (LLVM.API.LLVMPassManagerRef, Cint, Cint), - LLVM.ref(pm), Base.JLOptions().opt_level, 1) - - dead_arg_elimination!(pm) - global_optimizer!(pm) - global_dce!(pm) - strip_dead_prototypes!(pm) - - run!(pm, mod) - end - mod -end - -function write_object(mod::LLVM.Module, path) - host_triple = triple() - host_t = Target(host_triple) - TargetMachine(host_t, host_triple, "", "", LLVM.API.LLVMCodeGenLevelDefault, LLVM.API.LLVMRelocPIC) do tm - emit(tm, mod, LLVM.API.LLVMObjectFile, path) - end -end diff --git a/src/overdub.jl b/src/overdub.jl deleted file mode 100644 index 9a0015be..00000000 --- a/src/overdub.jl +++ /dev/null @@ -1,33 +0,0 @@ -# Implements contextual dispatch through Cassette.jl - -using Cassette - -## -# Convert two-arg `ccall` to single arg. -## -function transform(ctx, ref) - CI = ref.code_info - ismatch = x -> begin - Base.Meta.isexpr(x, :foreigncall) && - Base.Meta.isexpr(x.args[1], :call) - end - replace = x -> begin - y = Expr(x.head, Any[x.args[1].args[2], x.args[2:end]...]) - Expr(x.head, x.args[1].args[2], x.args[2:end]...) - end - Cassette.replace_match!(replace, ismatch, CI.code) - return CI -end - -const Pass = Cassette.@pass transform - -Cassette.@context Ctx -const ctx = Cassette.disablehooks(Ctx(pass = Pass)) - -### -# Rewrite functions -### - -#@inline Cassette.overdub(ctx::Ctx, ::typeof(+), a::T, b::T) where T<:Union{Float32, Float64} = add_float_contract(a, b) - -contextualize(f::F) where F = (args...) -> Cassette.overdub(ctx, f, args...) diff --git a/src/serialize.jl b/src/serialize.jl deleted file mode 100644 index 57256501..00000000 --- a/src/serialize.jl +++ /dev/null @@ -1,242 +0,0 @@ - -""" -A context structure for holding state related to serializing Julia -objects. A key component is an `IOBuffer` used to hold the serialized -result. -""" -struct SerializeContext - io::IOBuffer - store::Dict{Any,Any} # Meant to map Julia object to variable name - init::Vector{Any} # Expressions to run initially -end -SerializeContext(io::IOBuffer = IOBuffer()) = SerializeContext(io, Dict(), Vector{Expr}()) - -const _td = IdDict( - Any => :jl_any_type, - Float64 => :jl_float64_type, - Float32 => :jl_float32_type, - Int64 => :jl_int64_type, - Int32 => :jl_int32_type, - Int16 => :jl_int16_type, - Int8 => :jl_int8_type, - UInt64 => :jl_uint64_type, - UInt32 => :jl_uint32_type, - UInt16 => :jl_uint16_type, - UInt8 => :jl_uint8_type, - Cint => :jl_int32_type, - Cvoid => :jl_any_type, - Array => :jl_array_type, - Array{Any,1} => :jl_array_any_type, - Array{Int32,1} => :jl_array_int32_type, - Array{UInt8,1} => :jl_array_uint8_type, - ErrorException => :jl_errorexception_type, - DataType => :jl_datatype_type, - UnionAll => :jl_unionall_type, - Union => :jl_union_type, - Core.TypeofBottom => :jl_typeofbottom_type, - TypeVar => :jl_tvar_type, -) - -const _t = IdDict() - -for (t,s) in _td - _t[t] = :(unsafe_load(cglobal($(QuoteNode(s)), Type))) -end - -const _gd = IdDict( - Core => :jl_core_module, - Main => :jl_main_module, - nothing => :jl_nothing, - () => :jl_emptytuple, - Core.svec() => :jl_emptysvec, - UndefRefError() => :jl_undefref_exception, -) - -const _g = IdDict() - -for (x,s) in _gd - _g[x] = :(unsafe_load(cglobal($(QuoteNode(s)), Any))) -end - -""" - serialize(ctx::SerializeContext, x) - -Serialize `x` into the context object `ctx`. `ctx.io` is the `IOBuffer` where the -serialized results are stored. Get the result with `take!(ctx.io)`. - -This function returns an expression that will deserialize the object. Several `serialize` -methods can be called recursively to build up deserialization code for nested objects. -The expression returned is meant to be `eval`ed into a function that can be called -to do the serialization. - -The deserialization code should be pretty low-level code that can be compiled -relatively easily. It especially shouldn't use global variables. - -Serialization / deserialization code can use `ctx` to hold state information. - -Some simple types like boxed variables do not need to write anything to `ctx.io`. -They can return an expression that directly creates the object. -""" -function serialize(ctx::SerializeContext, @nospecialize(x)) - haskey(_g, x) && return _g[x] - # TODO: fix this major kludge. - if nfields(x) > 0 - return Expr(:tuple, (serialize(ctx, getfield(x,i)) for i in 1:nfields(x))...) - end - return :(unsafe_load(cglobal(:jl_emptytuple, Any))) -end - -function serialize(ctx::SerializeContext, @nospecialize(t::DataType)) - if haskey(_t, t) - return _t[t] - elseif haskey(ctx.store, t) - return ctx.store[t] - else - # primary = unwrap_unionall(t.wrapper) - name = gensym(Symbol(:type, "-", t.name.name)) - ctx.store[t] = name - e = quote - $name = let - local tn = $(serialize(ctx, t.name)) - # names = $(serialize(ctx, t.names)) - local super = $(serialize(ctx, t.super)) - local parameters = $(serialize(ctx, t.parameters)) - local types = $(serialize(ctx, t.types)) - local ndt = ccall(:jl_new_datatype, Any, - (Any, Any, Any, Any, Any, Any, Cint, Cint, Cint), - tn, tn.module, super, parameters, #=names=# unsafe_load(cglobal(:jl_any_type, Any)), types, - $(t.abstract), $(t.mutable), $(t.ninitialized)) - # tn.wrapper = ndt.name.wrapper - # ccall(:jl_set_const, Cvoid, (Any, Any, Any), tn.module, tn.name, tn.wrapper) - ndt - # ty = tn.wrapper - # $(ctx.types[string(t)]) = ndt - # hasinstance = serialize(ctx, ) - # $(if isdefined(primary, :instance) && !isdefined(t, :instance) - # # use setfield! directly to avoid `fieldtype` lowering expecting to see a Singleton object already on ty - # :(Core.setfield!(ty, :instance, ccall(:jl_new_struct, Any, (Any, Any...), ty))) - # end) - end - end - push!(ctx.init, e) - return name - end -end - -function serialize(ctx::SerializeContext, tn::Core.TypeName) - haskey(ctx.store, tn) && return ctx.store[tn] - name = gensym(Symbol(:typename, "-", tn.name)) - ctx.store[tn] = name - e = quote - $name = ccall(:jl_new_typename_in, Ref{Core.TypeName}, (Any, Any), - # $(serialize(ctx, tn.name)), Main #=__deserialized_types__ =# ) - $(serialize(ctx, tn.name)), unsafe_load(cglobal(:jl_main_module, Any)) #=__deserialized_types__ =# ) - end - push!(ctx.init, e) - return name -end - -function serialize(ctx::SerializeContext, mi::Core.MethodInstance) - return :(unsafe_load(cglobal(:jl_emptytuple, Any))) -end - -function serialize(ctx::SerializeContext, x::String) - advance!(ctx.io) - v = Vector{UInt8}(x) - ioptr = ctx.io.ptr - write(ctx.io, v) - quote - unsafe_string(Vptr + $(ioptr - 1), $(length(v))) - end -end - -function serialize(ctx::SerializeContext, x::Symbol) - haskey(ctx.store, x) && return ctx.store[x] - name = gensym(Symbol(:symbol, "-", x)) - ctx.store[x] = name - e = quote - $name = ccall(:jl_symbol_n, Any, (Ptr{UInt8}, Csize_t), $(serialize(ctx, string(x))), $(length(string(x)))) - # ccall(:jl_set_global, Cvoid, (Any, Any, Any), unsafe_load(cglobal(:jl_main_module, Any)), $(QuoteNode(name)), x) - end - push!(ctx.init, e) - return name -end - - - -# Define functions that return an expression. Example: -# serialize(ctx::SerializeContext, x::Int) = :(ccall(:jl_box_int64, Any, (Int,), $x)) -for (fun, type) in (:jl_box_int64 => Int64, :jl_box_int32 => Int32, :jl_box_int8 => Int16, :jl_box_int8 => Int8, - :jl_box_uint64 => UInt64, :jl_box_uint32 => UInt32, :jl_box_uint8 => UInt16, :jl_box_uint8 => UInt8, - :jl_box_voidpointer => Ptr{Cvoid}, - :jl_box_float64 => Float64, :jl_box_float32 => Float32) - @eval serialize(ctx::SerializeContext, x::$type) = Expr(:call, :ccall, QuoteNode($(QuoteNode(fun))), Any, Expr(:tuple, $type), x) -end -serialize(ctx::SerializeContext, x::Char) = :(ccall(:jl_box_char, Any, (UInt32,), $x)) -serialize(ctx::SerializeContext, x::Bool) = :(ccall(:jl_box_bool, Any, (UInt8,), $x)) - -function serialize(ctx::SerializeContext, a::Tuple) - length(a) == 0 && return :(unsafe_load(cglobal(:jl_emptytuple, Any))) - Expr(:tuple, (serialize(ctx, x) for x in a)...) -end - -function serialize(ctx::SerializeContext, a::Core.SimpleVector) - length(a) == 0 && return :(unsafe_load(cglobal(:jl_emptysvec, Any))) - Expr(:call, Expr(:., :Core, QuoteNode(:svec)), (serialize(ctx, x) for x in a)...) -end - -advance!(io) = write(io, repeat('\0', -rem(io.ptr - 1, 8, RoundUp))) # Align data to 8 bytes - -function serialize(ctx::SerializeContext, a::Array{T,N}) where {T,N} - elty = eltype(a) - aty = typeof(a) - dims = size(a) - atys = serialize(ctx, aty) - if isbitstype(elty) - advance!(ctx.io) - ioptr = ctx.io.ptr - write(ctx.io, a) - if N == 1 - advance!(ctx.io) - ioptr = ctx.io.ptr - write(ctx.io, a) - quote - p = Vptr + $ioptr - 1 - ccall(:jl_ptr_to_array_1d, $aty, (Any, Ptr{Cvoid}, Csize_t, Cint), $atys, p, $(length(a)), false) - end - else - dms = serialize(ctx, dims) - advance!(ctx.io) - ioptr = ctx.io.ptr - write(ctx.io, a) - quote - p = Vptr + $ioptr - 1 - ccall(:jl_ptr_to_array, $aty, (Any, Ptr{Cvoid}, Any, Int32), $atys, p, $dms, false) - end - end - else - idx = Int[] - e = Array{Any}(undef, length(a)) - @inbounds for i in eachindex(a) - if isassigned(a, i) - e[i] = serialize(ctx, a[i]) - push!(idx, i) - end - end - aname = gensym() - resulte = [quote - # $aname = Array{$elty, $(length(dims))}(undef, $dims) - $aname = ccall(:jl_new_array, $aty, (Any, Any), $atys, $(serialize(ctx, dims))) - end] - for i in idx - push!(resulte, quote - # unsafe_store!(pointer($aname), $(e[i]), $i) - unsafe_store!(convert(Ptr{Any}, pointer($aname)), $(e[i]), $i) - # unsafe_store!(convert(Ptr{Csize_t}, pointer($aname)), pointer_from_objref($(e[i])), $i) - # @inbounds $aname[$i] = $(e[i]) - end) - end - push!(resulte, :($aname = $aname)) - Expr(:block, resulte...) - end -end diff --git a/src/utils.jl b/src/utils.jl deleted file mode 100644 index 0fdf7a7c..00000000 --- a/src/utils.jl +++ /dev/null @@ -1,56 +0,0 @@ - -function julia_to_llvm(@nospecialize x) - isboxed = Ref{UInt8}() - # LLVMType(ccall(:jl_type_to_llvm, LLVM.API.LLVMTypeRef, (Any, Ref{UInt8}), x, isboxed)) # noserialize - LLVMType(ccall(:julia_type_to_llvm, LLVM.API.LLVMTypeRef, (Any, Ref{UInt8}), x, isboxed)) # julia v1.1.1 -end - -const jl_value_t_ptr = julia_to_llvm(Any) -const jl_value_t = eltype(jl_value_t_ptr) -# const jl_value_t_ptr_ptr = LLVM.PointerType(jl_value_t_ptr) -# # cheat on these for now: -# const jl_datatype_t_ptr = jl_value_t_ptr -# const jl_unionall_t_ptr = jl_value_t_ptr -# const jl_typename_t_ptr = jl_value_t_ptr -# const jl_sym_t_ptr = jl_value_t_ptr -# const jl_svec_t_ptr = jl_value_t_ptr -# const jl_module_t_ptr = jl_value_t_ptr -# const jl_array_t_ptr = jl_value_t_ptr -# -# const bool_t = julia_to_llvm(Bool) -# const int8_t = julia_to_llvm(Int8) -# const int16_t = julia_to_llvm(Int16) -# const int32_t = julia_to_llvm(Int32) -# const int64_t = julia_to_llvm(Int64) -# const uint8_t = julia_to_llvm(UInt8) -# const uint16_t = julia_to_llvm(UInt16) -# const uint32_t = julia_to_llvm(UInt32) -# const uint64_t = julia_to_llvm(UInt64) -# const float_t = julia_to_llvm(Float32) -# const double_t = julia_to_llvm(Float64) -# const float32_t = julia_to_llvm(Float32) -# const float64_t = julia_to_llvm(Float64) -# const void_t = julia_to_llvm(Nothing) -# const size_t = julia_to_llvm(Int) -# -# const int8_t_ptr = LLVM.PointerType(int8_t) -# const void_t_ptr = LLVM.PointerType(void_t) - -function module_setup(mod::LLVM.Module) -# triple!(mod, "wasm32-unknown-unknown-wasm") -# datalayout!(mod, "e-m:e-p:32:32-i64:64-n32:64-S128") -end - -llvmmod(native_code) = - LLVM.Module(ccall(:jl_get_llvm_module, LLVM.API.LLVMModuleRef, - (Ptr{Cvoid},), native_code.p)) - -function Base.write(mod::LLVM.Module, path::String) - open(io -> write(io, mod), path, "w") -end - - -walk(f, x) = true -# walk(f, x::Instruction) = foreach(c->walk(f,c), operands(x)) -# walk(f, x::Instruction) = f(x) || foreach(c->walk(f,c), operands(x)) -walk(f, x::ConstantExpr) = f(x) || foreach(c->walk(f,c), operands(x)) diff --git a/test/Project.toml b/test/Project.toml index 6e781cc6..3dee0e56 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -4,3 +4,6 @@ LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" +ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" +StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" \ No newline at end of file diff --git a/test/ccalls.jl b/test/ccalls.jl deleted file mode 100644 index dd5663c6..00000000 --- a/test/ccalls.jl +++ /dev/null @@ -1,35 +0,0 @@ -# d = find_ccalls(Threads.nthreads, Tuple{}) -# d = find_ccalls(time, Tuple{}) -# d = find_ccalls(muladd, Tuple{Array{Float64,2},Array{Float64,2},Array{Float64,2}}) - -f1() = ccall(:jl_errno, Int, (Int,), 11) -f2() = ccall(:jl_errno, Int, (Int, Int), 21, 22) -f3() = ccall(:jl_errno, Int, (Int, Int, Int), 31, 32, 33) - -@testset "ccalls" begin - m1 = irgen(f1, Tuple{}) - m2 = irgen(f2, Tuple{}) - m3 = irgen(f3, Tuple{}) - LLVM.verify(m1) - LLVM.verify(m2) - LLVM.verify(m3) -end - - -function f() - n = Int(unsafe_load(cglobal(:jl_n_threads, Cint))) - return 2n -end - -@testset "cglobal" begin - m = irgen(f, Tuple{}) - LLVM.verify(m) - @test f() == @jlrun f() -end - -@testset "extern" begin - f() = @extern(:time, Cvoid, (Ptr{Cvoid},), C_NULL) - m = irgen(f, Tuple{}) - LLVM.verify(m) - @test "time" in [name(f) for f in LLVM.functions(m)] -end diff --git a/test/globals.jl b/test/globals.jl deleted file mode 100644 index 43c70e84..00000000 --- a/test/globals.jl +++ /dev/null @@ -1,36 +0,0 @@ -# @testset "serialize" begin -# ctx = StaticCompiler.SerializeContext() -# a = Any["abcdg", ["hi", "bye"], 3333, Int32(44), 314f0, 3.14, (1, 3.3f0), Core.svec(9.9, 9), :sym, :sym, :a] -# e = StaticCompiler.serialize(ctx, a) -# g = eval(:(Vptr -> $e)) -# v = take!(ctx.io) -# GC.enable(false) -# res = g(pointer(v)) -# GC.enable(true) -# @test res == a -# end - - -# const a = ["abcdg", "asdfl", 123, 3.14, ["a", "asdf"], (1, 3.63), [1, 3.63]] -const a = ["abcdg", "asdxf"] -const b = "B" -const x = [1.33, 35.0] -const xi = [3, 5] - -f(x) = @inbounds a[1][3] > b[1] ? 2x : x -g(i) = @inbounds x[1] > x[2] ? 2i : i -h(i) = @inbounds xi[1] == 3 ? i : 2i - -@testset "globals" begin - @test f(3) == @jlrun f(3) - @test g(3) == @jlrun g(3) - @test h(3) == @jlrun h(3) -end - -f() = Complex{Float64} -g(@nospecialize(x)) = isa(x, Number) ? 1 : 0 - -@testset "type" begin - @test string(@jlrun f()) == "Complex{Float64}" - res = g(4.0im) -end diff --git a/test/ode.jl b/test/ode.jl deleted file mode 100644 index b270a544..00000000 --- a/test/ode.jl +++ /dev/null @@ -1,182 +0,0 @@ -# Adapted from: https://github.com/JuliaDiffEq/ODE.jl/blob/8954872f956116e78b6c04690f899fe2db696b4e/src/ODE.jl#L84-L360 -# MIT license -# Copyright (c) 2009-2015: various contributors: https://github.com/JuliaLang/ODE.jl/contributors - -using LinearAlgebra - -function hinit(F, x0, t0::T, tend, p, reltol, abstol) where T - # Returns first step, direction of integration and F evaluated at t0 - tdir = sign(tend-t0) - tdir==0 && error("Zero time span") - tau = max(reltol*norm(x0, Inf), abstol) - d0 = norm(x0, Inf)/tau - f0 = F(t0, x0) - d1 = norm(f0, Inf)/tau - if d0 < 1e-5 || d1 < 1e-5 - h0 = 1e-6 - else - h0 = 0.01*(d0/d1) - end - h0 = convert(T,h0) - # perform Euler step - x1 = x0 + tdir*h0*f0 - f1 = F(t0 + tdir*h0, x1) - # estimate second derivative - d2 = norm(f1 - f0, Inf)/(tau*h0) - if max(d1, d2) <= 1e-15 - h1 = max(T(10)^(-6), T(10)^(-3)*h0) - else - pow = -(2 + log10(max(d1, d2)))/(p + 1) - h1 = 10^pow - end - h1 = convert(T,h1) - return tdir*min(100*h0, h1, tdir*(tend-t0)), tdir, f0 -end - -function fdjacobian(F, x::Number, t) - ftx = F(t, x) - - # The 100 below is heuristic - dx = (x .+ (x==0))./100 - dFdx = (F(t,x+dx)-ftx)./dx - - return dFdx -end - -function fdjacobian(F, x, t) - ftx = F(t, x) - lx = max(length(x),1) - dFdx = zeros(eltype(x), lx, lx) - for j = 1:lx - # The 100 below is heuristic - dx = zeros(eltype(x), lx) - dx[j] = (x[j] .+ (x[j]==0))./100 - dFdx[:,j] = (F(t,x+dx)-ftx)./dx[j] - end - return dFdx -end - -# ODE23S Solve stiff systems based on a modified Rosenbrock triple -# (also used by MATLAB's ODE23s); see Sec. 4.1 in -# -# [SR97] L.F. Shampine and M.W. Reichelt: "The MATLAB ODE Suite," SIAM Journal on Scientific Computing, Vol. 18, 1997, pp. 1–22 -# -# supports keywords: points = :all | :specified (using dense output) -# jacobian = G(t,y)::Function | nothing (FD) -function ode23s(F, y0, tspan; - reltol = 1.0e-5, abstol = 1.0e-8, - jacobian=nothing, - points=:all, - norm=LinearAlgebra.norm, - minstep=abs(tspan[end] - tspan[1])/1e18, - maxstep=abs(tspan[end] - tspan[1])/2.5, - initstep=0.) - - # select method for computing the Jacobian - if typeof(jacobian) == Function - jac = jacobian - else - # fallback finite-difference - jac = (t, y)->fdjacobian(F, y, t) - end - - # constants - d = 1/(2 + sqrt(2)) - e32 = 6 + sqrt(2) - - - # initialization - t = tspan[1] - - tfinal = tspan[end] - - h = initstep - if h == 0. - # initial guess at a step size - h, tdir, F0 = hinit(F, y0, t, tfinal, 3, reltol, abstol) - else - tdir = sign(tfinal - t) - F0 = F(t,y0) - end - h = tdir * min(abs(h), maxstep) - - y = y0 - tout = [t] # first output time - yout = [deepcopy(y)] # first output solution - - J = jac(t,y) # get Jacobian of F wrt y -# Core.print(t, " ", tfinal, " ", minstep, " ", h) - while abs(t - tfinal) > 0 && minstep < abs(h) - if abs(t-tfinal) < abs(h) - h = tfinal - t - end - - if size(J,1) == 1 - W = I - h*d*J - else - # note: if there is a mass matrix M on the lhs of the ODE, i.e., - # M * dy/dt = F(t,y) - # we can simply replace eye(J) by M in the following expression - # (see Sec. 5 in [SR97]) - - W = lu( I - h*d*J ) - end - - # approximate time-derivative of F - T = h*d*(F(t + h/100, y) - F0)/(h/100) - - # modified Rosenbrock formula - k1 = W\(F0 + T) - F1 = F(t + 0.5*h, y + 0.5*h*k1) - k2 = W\(F1 - k1) + k1 - ynew = y + h*k2 - F2 = F(t + h, ynew) - k3 = W\(F2 - e32*(k2 - F1) - 2*(k1 - F0) + T ) - - err = (abs(h)/6)*norm(k1 - 2*k2 + k3) # error estimate - delta = max(reltol*max(norm(y),norm(ynew)), abstol) # allowable error - - # check if new solution is acceptable - if err <= delta - - # # if points==:specified || points==:all - # # only points in tspan are requested - # # -> find relevant points in (t,t+h] - # for toi in tspan[(tspan.>t) .& (tspan.<=t+h)] - # # rescale to (0,1] - # s = (toi-t)/h - - # # use interpolation formula to get solutions at t=toi - # push!(tout, toi) - # push!(yout, y + h*( k1*s*(1-s)/(1-2*d) + k2*s*(s-2*d)/(1-2*d))) - # end - # # Core.print("First\n") - # # end - # if points==:all - if (tout[end]!=t+h) - # # add the intermediate points - push!(tout, t + h) - push!(yout, ynew) - end - - # update solution - t = t + h - y = ynew - - F0 = F2 # use FSAL property - J = jac(t,y) # get Jacobian of F wrt y - # for new solution - end - - # update of the step size - h = tdir*min( maxstep, abs(h)*0.8*(delta/err)^(1/3) ) - end - - return tout, yout -end - - -# fode() = ode23s((t,y)->2.0t^2, 0.0, Float64[0:.001:2;], initstep = 1e-4)[2][end] -# fode() = ode23s((t,y)->2.0t^2, 0.0, [0:.001:2;], initstep = 1e-4)[2][end] - -# @show fode() diff --git a/test/others.jl b/test/others.jl deleted file mode 100644 index 4481e77a..00000000 --- a/test/others.jl +++ /dev/null @@ -1,73 +0,0 @@ -mutable struct AAA - aaa::Int - bbb::Int -end -@noinline ssum(x) = x.aaa + x.bbb -fstruct(x) = ssum(AAA(x, 99)) -@test fstruct(10) == @jlrun fstruct(10) - -module ZZ -mutable struct AAA - aaa::Int - bbb::Int -end -@noinline ssum(x) = x.aaa + x.bbb -fstruct(x) = ssum(AAA(x, 99)) -end # module -ffstruct(x) = ZZ.fstruct(x) -@test ffstruct(10) == @jlrun ffstruct(10) - -const ag = Ref(0x80808080) -jglobal() = ag -@show bg = @jlrun jglobal() -# @test jglobal()[] == bg[] # Something's broken with mutable's - -arraysum(x) = sum([x, 1]) -# @test arraysum(6) == @jlrun arraysum(6) - -fsin(x) = sin(x) -@test fsin(0.5) == @jlrun fsin(0.5) - -fccall() = ccall(:jl_ver_major, Cint, ()) -@test fccall() == @jlrun fccall() - -fcglobal() = cglobal(:jl_n_threads, Cint) -@test fcglobal() == @jlrun fcglobal() - -const sv = Core.svec(1,2,3,4) -fsv() = sv -@test fsv() == @jlrun fsv() - -const arr = [9,9,9,9] -farray() = arr -@show @jlrun farray() -@show farray() -# @test farray() == @jlrun farray() - -@noinline f0(x) = 3x -@noinline fop(f, x) = 2f(x) -funcall(x) = fop(f0, x) -@test funcall(2) == @jlrun funcall(2) - -hi() = print(Core.stdout, 'X') -@jlrun hi() - -hello() = print(Core.stdout, "Hello world...\n") -@jlrun hello() - -function gx(i) - a = 2.0:0.1:10.0 - @inbounds i > 3 ? a[1] : a[5] -end - -@test_skip gx(4) == @jlrun gx(4) - -fsimple() = [0:.001:2;][end] - -@test fsimple() == @jlrun fsimple() - -@noinline function fsym(x; l = :hello, s = :x) - s == :asdf ? x : 2x -end -gsym(x) = fsym(x, l = :hello, s = :asdf) + 1 -@test gsym(3) == @jlrun gsym(3) diff --git a/test/runtests.jl b/test/runtests.jl index 3381a873..78d62bfe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,21 +1,199 @@ using StaticCompiler using Test -using LLVM using Libdl +using LinearAlgebra +using LoopVectorization +using ManualMemory +using StrideArraysCore -cd(@__DIR__) -@testset "ccalls" begin - include("ccalls.jl") +@testset "Basics" begin + + simple_sum(x) = x + one(typeof(x)) + + # This probably needs a macro + @test ccall(generate_shlib_fptr(simple_sum, (Int,)), Int, (Int,), 1) == Int(2) + @test ccall(generate_shlib_fptr(simple_sum, (Float64,)), Float64, (Float64 ,), 1) == Float64(2) + + @test ccall(generate_shlib_fptr(simple_sum, (Int32,)), Int32, (Int32,), 1) == Int32(2) + @test ccall(generate_shlib_fptr(simple_sum, (Float32,)), Float32, (Float32 ,), 1) == Float16(2) + + @test ccall(generate_shlib_fptr(simple_sum, (Int16,)), Int16, (Int16,), 1) == Int16(2) + @test ccall(generate_shlib_fptr(simple_sum, (Float16,)), Float16, (Float16 ,), 1) == Float16(2) + +end + + +fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 + +@testset "Recursion" begin + fib_ptr = generate_shlib_fptr(fib, (Int,)) + @test @ccall( $fib_ptr(10::Int) :: Int ) == 55 + + # Trick to work around #40990 + _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) + fib2(n) = _fib2(_fib2, n) + + fib2_ptr = generate_shlib_fptr(fib2, (Int,)) + @test @ccall( $fib2_ptr(20::Int) :: Int ) == 6765 + +end + +# Call binaries for testing +# @testset "Generate binary" begin +# fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) +# libname = tempname() +# generate_shlib(fib, (Int,), libname) +# ptr = Libdl.dlopen(libname * "." * Libdl.dlext, Libdl.RTLD_LOCAL) +# fptr = Libdl.dlsym(ptr, "julia_fib") +# @assert fptr != C_NULL +# # This works on REPL +# @test_skip ccall(fptr, Int, (Int,), 10) == 55 +# end + + +@testset "Loops" begin + function sum_first_N_int(N) + s = 0 + for a in 1:N + s += a + end + s + end + @test ccall(generate_shlib_fptr(sum_first_N_int, (Int,)), Int, (Int,), 10) == 55 + + function sum_first_N_float64(N) + s = Float64(0) + for a in 1:N + s += Float64(a) + end + s + end + @test ccall(generate_shlib_fptr(sum_first_N_float64, (Int,)), Float64, (Int,), 10) == 55. + + function sum_first_N_int_inbounds(N) + s = 0 + @inbounds for a in 1:N + s += a + end + s + end + @test ccall(generate_shlib_fptr(sum_first_N_int_inbounds, (Int,)), Int, (Int,), 10) == 55 + + + function sum_first_N_float64_inbounds(N) + s = Float64(0) + @inbounds for a in 1:N + s += Float64(a) + end + s + end + @test ccall(generate_shlib_fptr(sum_first_N_float64_inbounds, (Int,)), Float64, (Int,), 10) == 55. + +end + +# Arrays with different input types Int32, Int64, Float32, Float64, Complex? +@testset "Arrays" begin + function array_sum(n, A) + s = zero(eltype(A)) + for i in 1:n + s += A[i] + end + s + end + + array_sum_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Int}}) + @test ( @ccall $array_sum_ptr(10::Int, collect(1:10)::Vector{Int})::Int ) == 55 + + # this will segfault on my machine if I use 64 bit complex numbers! + array_sum_complex_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float32}}}) + @test ( @ccall $array_sum_complex_ptr(2::Int, [1f0+im, 1f0-im]::Vector{Complex{Float32}})::Complex{Float32} ) ≈ 2.0 + + #This will segfault + array_sum_complex64_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float64}}}) + @test_skip ( @ccall $array_sum_complex_ptr(2::Int, [1.0+im, 1.0-im]::Vector{Complex{Float64}})::Complex{Float64} ) ≈ 2.0 +end + + +# Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. +# We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function +@testset "Send and receive Tuple" begin + foo(u::Tuple) = 2 .* reverse(u) .- 1 # we can't just compile this as is. + + # Make a mutating function that places the output into a Ref for the caller to grab: + foo!(out::Ref{<:Tuple}, u::Tuple) = (out[] = foo(u); return nothing) + + foo_ptr = generate_shlib_fptr(foo!, Tuple{Base.RefValue{NTuple{3, Int}}, NTuple{3, Int}}) + out = Ref{NTuple{3, Int}}() + # we wrap u in a ref when we send it to the binary because LLVM expects that :( + u = Ref((1, 2, 3)) + (@ccall $foo_ptr(out::Ref{NTuple{3, Int}}, u::Ref{NTuple{3, Int}}) :: Nothing) + + @test out[] == foo(u[]) end -@testset "globals" begin - include("globals.jl") + +# Just to call external libraries +@testset "BLAS" begin + function mydot(a::Vector{Float64}) + N = length(a) + BLAS.dot(N, a, 1, a, 1) + end + a = [1.0, 2.0] + mydot_ptr = generate_shlib_fptr(mydot, Tuple{Vector{Float64}}) + @test @ccall( $mydot_ptr(a::Vector{Float64})::Float64 ) == 5.0 end -@testset "others" begin - include("others.jl") + +@testset "Hello World" begin + function hello(N) + println("Hello World $N") + N + end + # How do I test this? + # Also ... this segfaults + @test_skip ccall(generate_shlib_fptr(hello, (Int,)), Int, (Int,), 1) == 1 end -@testset "standalone" begin - include("standalone-exe.jl") +# I can't beleive this works. +@testset "LoopVectorization" begin + function mul!(C, A, B) + # note: @tturbo does NOT work + @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) + Cmn = zero(eltype(C)) + for k ∈ indices((A,B), (2,1)) + Cmn += A[m,k] * B[k,n] + end + C[m,n] = Cmn + end + end + mul_ptr! = generate_shlib_fptr(mul!, Tuple{Matrix{Float64}, Matrix{Float64}, Matrix{Float64}}) + + C = Array{Float64}(undef, 10, 12) + A = rand(10, 11) + B = rand(11, 12) + + @ccall $mul_ptr!(C::Matrix{Float64}, A::Matrix{Float64}, B::Matrix{Float64}) :: Nothing + @test C ≈ A*B end + +# This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). +# This lets us have intermediate, mutable stack allocated arrays inside our +@testset "Alloca" begin + function f(N) + # this can hold at most 100 Int values, if you use it for more, you'll segfault + buf = ManualMemory.MemoryBuffer{100, Int}(undef) + GC.@preserve buf begin + # wrap the first N values in a PtrArray + arr = PtrArray(pointer(buf), (N,)) + arr .= 1 # mutate the array to be all 1s + sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body + end + end + + fptr = generate_shlib_fptr(f, Tuple{Int}) + @test (@ccall $fptr(20::Int) :: Int) == 20 + +end + + +# data structures, dictionaries, tuples, named tuples diff --git a/test/standalone-exe.jl b/test/standalone-exe.jl deleted file mode 100644 index fb97de52..00000000 --- a/test/standalone-exe.jl +++ /dev/null @@ -1,41 +0,0 @@ -# Definitions of functions to compile -twox(x) = 2x - -const aa = [4, 5] -arrayfun(x) = x + aa[1] + aa[2] - -jsin(x) = sin(x) - -function arridx(i) - a = collect(1.0:0.1:10.0) - @inbounds i > 3 ? a[1] : a[5] -end - -fsimple() = [0:.001:2;][end] - -include("ode.jl") -fode() = ode23s((t,y)->2.0t^2, 0.0, [0:.001:2;], initstep = 1e-4)[2][end] - -# Functions to compile and arguments to pass -funcalls = [ - (twox, Tuple{Int}, 4), - (arrayfun, Tuple{Int}, 4), - (jsin, Tuple{Float64}, 0.5), - (arridx, Tuple{Int}, 4), - (fsimple, Tuple{}, ()), - (fode, Tuple{}, ()), # Broken on Julia v1.2.0; works on Julia v1.3.0-rc3 -] - -StaticCompiler.exegen(funcalls) - -using Formatting -@testset "exegen" begin - cd("standalone") do - for (func, tt, val) in funcalls - fname = nameof(func) - rettype = Base.return_types(func, tt)[1] - fmt = StaticCompiler.Cformatmap[rettype] - @test Formatting.sprintf1(fmt, func(val...)) == read(`./$fname`, String) - end - end -end From fea8b4b8df29074ad1adc9e5ffd11d5754c9c4e9 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 19:59:39 -0700 Subject: [PATCH 023/202] new friendly `compile` interface --- Manifest.toml | 1 + Project.toml | 3 +- README.md | 34 +++++++++----- src/StaticCompiler.jl | 107 +++++++++++++++++++++++++++++++++++++++++- test/runtests.jl | 72 +++++++++------------------- 5 files changed, 152 insertions(+), 65 deletions(-) diff --git a/Manifest.toml b/Manifest.toml index 44dbe949..ab4efd77 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -1,5 +1,6 @@ # This file is machine-generated - editing it directly is not advised +julia_version = "1.7.1" manifest_format = "2.0" [[deps.ArgTools]] diff --git a/Project.toml b/Project.toml index 81a4e927..461d8ddb 100644 --- a/Project.toml +++ b/Project.toml @@ -7,11 +7,12 @@ version = "0.2.0" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" +Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" [compat] -julia = "1.7" GPUCompiler = "0.13" LLVM = "4" +julia = "1.7" [extras] Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" diff --git a/README.md b/README.md index 55780052..c5f188d6 100644 --- a/README.md +++ b/README.md @@ -16,21 +16,29 @@ Pkg.add(PackageSpec( url = "https://github.com/tshort/StaticCompiler.jl", rev = ``` ```julia -using StaticCompiler -f(x) = 2x +julia> using StaticCompiler -# compile `f` and return an LLVM module -m = compile(f, (Int,)) +julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) +fib (generic function with 1 method) -# compile `f` and write to a shared library ("f.so" or "f.dll") -generate_shlib(f, (Int,), "libf") -# find a function pointer for this shared library -fptr = generate_shlib_fptr("libf", "f") -@ccall $fptr(2::Int)::Int +julia> path, fib_compiled = compile(fib, Tuple{Int}, "fib") +("fib.cjl", StaticCompiler.StaticCompiledFunction{Int64, Tuple{Int64}}(:fib, Ptr{Nothing} @0x00007fc4ec032130)) -# do this in one step (this time with a temporary shared library) -fptr = generate_shlib_fptr(f, (Int,)) -@ccall $fptr(2::Int)::Int +julia> fib_compiled(10) +55 +``` +Now we can quite this session and load a new one where `fib` is not defined: +```julia +julia> using StaticCompiler + +julia> fib +ERROR: UndefVarError: fib not defined + +julia> fib_compiled = load_function("fib.cjl") +StaticCompiler.StaticCompiledFunction{Int64, Tuple{Int64}}(:fib, Ptr{Nothing} @0x00007f9ee8050130) + +julia> fib_compiled(10) +55 ``` ## Approach @@ -41,6 +49,6 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi * This package currently requires that you have `gcc` installed and in your system's `PATH`. This is probably pretty easy to fix, we only use `gcc` for linking. In theory Clang_jll or LLVM_full_jll should be able to do this, and be managed through Julia's package manager. * No heap allocations (e.g. creating an array or a string) are allowed inside a statically compiled function body. If you try to run such a function, you will get a segfault. -** It's sometimes possible you won't get a segfault if you define and run the function in the same session, but trying to call the compiled function in a new julia session will definitely segfault. +** It's sometimes possible you won't get a segfault if you define and run the function in the same session, but trying to call the compiled function in a new julia session will definitely segfault if you allocate memory. * Lots of other limitations too. E.g. there's an example in tests/runtests.jl where summing a vector of `Complex{Float32}` is fine, but segfaults on `Complex{Float64}`. * Doesn't currently work on Windows \ No newline at end of file diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 0de1f67a..06f71a5f 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -3,9 +3,110 @@ module StaticCompiler using GPUCompiler: GPUCompiler using LLVM: LLVM using Libdl: Libdl +using Base: RefValue +using Serialization: serialize, deserialize +export compile, load_function +export native_code_llvm, native_code_typed, native_llvm_module -export generate_shlib, generate_shlib_fptr, compile, native_code_llvm, native_code_typed, native_llvm_module +""" + compile(f, types, path::String = tempname()) --> (obj_path, compiled_f) + + !!! Warning: this will fail on programs that heap allocate any memory, or have dynamic dispatch !!! + +Statically compile the method of a function `f` specialized to arguments of the type given by `types`. + +This will save a shared object file (i.e. a `.so` or `.dylib`) at the specified path, and will save a +`LazyStaticCompiledFunction` object at the same path with the extension `.cjl`. This +`LazyStaticCompiledFunction` can be deserialized with the function `load_function`. Once it is +instantiated in a julia session, it will be of type `StaticCompiledFunction` and may be called with +arguments of type `types` as if it were a function with a single method (the method determined by `types`). + +`compile` will return a `obj_path` which is the location of the serialized `LazyStaticCompiledFunction`, and +an already instantiated `StaticCompiledFunction` object. + +Example: + +Define and compile a `fib` function: +```julia +julia> using StaticCompiler + +julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) +fib (generic function with 1 method) + +julia> path, fib_compiled = compile(fib, Tuple{Int}, "fib") +("fib.cjl", StaticCompiler.StaticCompiledFunction{Int64, Tuple{Int64}}(:fib, Ptr{Nothing} @0x00007fc4ec032130)) + +julia> fib_compiled(10) +55 +``` +Now we can quite this session and load a new one where `fib` is not defined: +```julia +julia> using StaticCompiler + +julia> fib +ERROR: UndefVarError: fib not defined + +julia> fib_compiled = load_function("fib.cjl") +StaticCompiler.StaticCompiledFunction{Int64, Tuple{Int64}}(:fib, Ptr{Nothing} @0x00007f9ee8050130) + +julia> fib_compiled(10) +55 +``` +Tada! +""" +function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_name(repr(f)), kwargs...) + tt = Base.to_tuple_type(_tt) + isconcretetype(tt) || error("input type signature $_tt is not concrete") + + rt = only(native_code_typed(f, tt))[2] + isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") + + # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals + # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this + + f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) + + generate_shlib(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; kwargs...) + + lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name) + serialize(path*".cjl", lf) + path*".cjl", instantiate(lf) +end + + +""" + load_function(path) --> compiled_f + +load a `StaticCompiledFunction` from a given path. This object is callable. +""" +load_function(path) = instantiate(deserialize(path) :: LazyStaticCompiledFunction) + + +struct LazyStaticCompiledFunction{rt, tt} + f::Symbol + path::String + name::String +end + +function instantiate(p::LazyStaticCompiledFunction{rt, tt}) where {rt, tt} + StaticCompiledFunction{rt, tt}(p.f, generate_shlib_fptr(p.path::String, p.name)) +end + +struct StaticCompiledFunction{rt, tt} + f::Symbol + ptr::Ptr{Nothing} +end + +function (f::StaticCompiledFunction{rt, tt})(args...) where {rt, tt} + Tuple{typeof.(args)...} == tt || error("Input types don't match compiled target $((tt.parameters...,)). Got arguments of type $(typeof.(args))") + out = RefValue{rt}() + refargs = Ref(args) + ccall(f.ptr, Nothing, (Ref{rt}, Ref{tt}), out, refargs) + out[] +end + +instantiate(f::StaticCompiledFunction) = f module TestRuntime # dummy methods @@ -81,4 +182,8 @@ function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs end + + + + end # module diff --git a/test/runtests.jl b/test/runtests.jl index 78d62bfe..85eb6f84 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,31 +11,22 @@ using StrideArraysCore simple_sum(x) = x + one(typeof(x)) # This probably needs a macro - @test ccall(generate_shlib_fptr(simple_sum, (Int,)), Int, (Int,), 1) == Int(2) - @test ccall(generate_shlib_fptr(simple_sum, (Float64,)), Float64, (Float64 ,), 1) == Float64(2) - - @test ccall(generate_shlib_fptr(simple_sum, (Int32,)), Int32, (Int32,), 1) == Int32(2) - @test ccall(generate_shlib_fptr(simple_sum, (Float32,)), Float32, (Float32 ,), 1) == Float16(2) - - @test ccall(generate_shlib_fptr(simple_sum, (Int16,)), Int16, (Int16,), 1) == Int16(2) - @test ccall(generate_shlib_fptr(simple_sum, (Float16,)), Float16, (Float16 ,), 1) == Float16(2) - + for T ∈ (Int, Float64, Int32, Float32, Int16, Float16) + @test compile(simple_sum, (T,))[2]( T(1) ) == T(2) + end end fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 @testset "Recursion" begin - fib_ptr = generate_shlib_fptr(fib, (Int,)) - @test @ccall( $fib_ptr(10::Int) :: Int ) == 55 + @test compile(fib, (Int,))[2](10) == fib(10) # Trick to work around #40990 _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) fib2(n) = _fib2(_fib2, n) - fib2_ptr = generate_shlib_fptr(fib2, (Int,)) - @test @ccall( $fib2_ptr(20::Int) :: Int ) == 6765 - + @test compile(fib2, (Int,))[2](20) == fib(20) end # Call binaries for testing @@ -59,8 +50,8 @@ end end s end - @test ccall(generate_shlib_fptr(sum_first_N_int, (Int,)), Int, (Int,), 10) == 55 - + @test compile(sum_first_N_int, (Int,))[2](10) == 55 + function sum_first_N_float64(N) s = Float64(0) for a in 1:N @@ -68,7 +59,7 @@ end end s end - @test ccall(generate_shlib_fptr(sum_first_N_float64, (Int,)), Float64, (Int,), 10) == 55. + @test compile(sum_first_N_float64, (Int,))[2](10) == 55. function sum_first_N_int_inbounds(N) s = 0 @@ -77,8 +68,7 @@ end end s end - @test ccall(generate_shlib_fptr(sum_first_N_int_inbounds, (Int,)), Int, (Int,), 10) == 55 - + @test compile(sum_first_N_int_inbounds, (Int,))[2](10) == 55 function sum_first_N_float64_inbounds(N) s = Float64(0) @@ -87,7 +77,7 @@ end end s end - @test ccall(generate_shlib_fptr(sum_first_N_float64_inbounds, (Int,)), Float64, (Int,), 10) == 55. + @test compile(sum_first_N_float64_inbounds, (Int,))[2](10) == 55. end @@ -100,35 +90,20 @@ end end s end - - array_sum_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Int}}) - @test ( @ccall $array_sum_ptr(10::Int, collect(1:10)::Vector{Int})::Int ) == 55 - # this will segfault on my machine if I use 64 bit complex numbers! - array_sum_complex_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float32}}}) - @test ( @ccall $array_sum_complex_ptr(2::Int, [1f0+im, 1f0-im]::Vector{Complex{Float32}})::Complex{Float32} ) ≈ 2.0 - - #This will segfault - array_sum_complex64_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float64}}}) - @test_skip ( @ccall $array_sum_complex_ptr(2::Int, [1.0+im, 1.0-im]::Vector{Complex{Float64}})::Complex{Float64} ) ≈ 2.0 + @test compile(array_sum, (Int, Vector{Int}))[2](10, Int.(1:10)) == 55 + @test compile(array_sum, (Int, Vector{Complex{Float32}}))[2](10, Complex{Float32}.(1:10)) == 55f0 + 0f0im + @test compile(array_sum, (Int, Vector{Complex{Float64}}))[2](10, Complex{Float64}.(1:10)) == 55f0 + 0f0im end # Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. -# We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function +# We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function. +# The interface made in `compile` should handle this fine. @testset "Send and receive Tuple" begin foo(u::Tuple) = 2 .* reverse(u) .- 1 # we can't just compile this as is. - # Make a mutating function that places the output into a Ref for the caller to grab: - foo!(out::Ref{<:Tuple}, u::Tuple) = (out[] = foo(u); return nothing) - - foo_ptr = generate_shlib_fptr(foo!, Tuple{Base.RefValue{NTuple{3, Int}}, NTuple{3, Int}}) - out = Ref{NTuple{3, Int}}() - # we wrap u in a ref when we send it to the binary because LLVM expects that :( - u = Ref((1, 2, 3)) - (@ccall $foo_ptr(out::Ref{NTuple{3, Int}}, u::Ref{NTuple{3, Int}}) :: Nothing) - - @test out[] == foo(u[]) + @test compile(foo, (NTuple{3, Int},))[2]((1, 2, 3)) == (5, 3, 1) end @@ -139,8 +114,8 @@ end BLAS.dot(N, a, 1, a, 1) end a = [1.0, 2.0] - mydot_ptr = generate_shlib_fptr(mydot, Tuple{Vector{Float64}}) - @test @ccall( $mydot_ptr(a::Vector{Float64})::Float64 ) == 5.0 + + @test compile(mydot, (Vector{Float64},))[2](a) == 5.0 end @@ -166,13 +141,12 @@ end C[m,n] = Cmn end end - mul_ptr! = generate_shlib_fptr(mul!, Tuple{Matrix{Float64}, Matrix{Float64}, Matrix{Float64}}) - + C = Array{Float64}(undef, 10, 12) A = rand(10, 11) B = rand(11, 12) - @ccall $mul_ptr!(C::Matrix{Float64}, A::Matrix{Float64}, B::Matrix{Float64}) :: Nothing + compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},))[2](C, A, B) @test C ≈ A*B end @@ -189,10 +163,8 @@ end sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body end end - - fptr = generate_shlib_fptr(f, Tuple{Int}) - @test (@ccall $fptr(20::Int) :: Int) == 20 - + + @test compile(f, (Int,))[2](20) == 20 end From 73311bb0073f8981a32b3d3e3be186f78e6f77ce Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 20:08:09 -0700 Subject: [PATCH 024/202] fix typos --- README.md | 2 +- src/StaticCompiler.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c5f188d6..eccd83e1 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ julia> path, fib_compiled = compile(fib, Tuple{Int}, "fib") julia> fib_compiled(10) 55 ``` -Now we can quite this session and load a new one where `fib` is not defined: +Now we can quit this session and load a new one where `fib` is not defined: ```julia julia> using StaticCompiler diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 06f71a5f..bb85fcc4 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -40,7 +40,7 @@ julia> path, fib_compiled = compile(fib, Tuple{Int}, "fib") julia> fib_compiled(10) 55 ``` -Now we can quite this session and load a new one where `fib` is not defined: +Now we can quit this session and load a new one where `fib` is not defined: ```julia julia> using StaticCompiler From 3658b05b8b552fbeed5e3a278c66048d35fc51e1 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 20:08:32 -0700 Subject: [PATCH 025/202] bump version number --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 461d8ddb..a1faac63 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short"] -version = "0.2.0" +version = "0.3.0" [deps] GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" From ad2aba1b97f43c673f7d061a5754da0f899dc9f1 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 2 Feb 2022 20:34:26 -0700 Subject: [PATCH 026/202] fix code block --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6802bc23..2e369c45 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ StaticCompiler.StaticCompiledFunction{Int64, Tuple{Int64}}(:fib, Ptr{Nothing} @0 julia> fib_compiled(10) 55 +``` ## Approach @@ -50,4 +51,4 @@ do * No heap allocations (e.g. creating an array or a string) are allowed inside a statically compiled function body. If you try to run such a function, you will get a segfault. ** It's sometimes possible you won't get a segfault if you define and run the function in the same session, but trying to call the compiled function in a new julia session will definitely segfault if you allocate memory. * Lots of other limitations too. E.g. there's an example in tests/runtests.jl where summing a vector of `Complex{Float32}` is fine, but segfaults on `Complex{Float64}`. -* Doesn't currently work on Windows \ No newline at end of file +* Doesn't currently work on Windows From e005ac5509ff67459a81a411261aed7db63809c2 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 20:35:52 -0700 Subject: [PATCH 027/202] remove accidental word --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e369c45..17921a86 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ julia> fib_compiled(10) ## Approach This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. -do + ## Limitations * This package currently requires that you have `gcc` installed and in your system's `PATH`. This is probably pretty easy to fix, we only use `gcc` for linking. In theory Clang_jll or LLVM_full_jll should be able to do this, and be managed through Julia's package manager. From 0096d0be58b4ad57b61365745333adb127618ae0 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 20:36:58 -0700 Subject: [PATCH 028/202] update limitations --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 17921a86..0018a837 100644 --- a/README.md +++ b/README.md @@ -50,5 +50,5 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi * This package currently requires that you have `gcc` installed and in your system's `PATH`. This is probably pretty easy to fix, we only use `gcc` for linking. In theory Clang_jll or LLVM_full_jll should be able to do this, and be managed through Julia's package manager. * No heap allocations (e.g. creating an array or a string) are allowed inside a statically compiled function body. If you try to run such a function, you will get a segfault. ** It's sometimes possible you won't get a segfault if you define and run the function in the same session, but trying to call the compiled function in a new julia session will definitely segfault if you allocate memory. -* Lots of other limitations too. E.g. there's an example in tests/runtests.jl where summing a vector of `Complex{Float32}` is fine, but segfaults on `Complex{Float64}`. * Doesn't currently work on Windows +* If you find any other limitations, let us know. There's probably lots. From 9426a050095824c931393056d72af738e8c8f514 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 21:22:42 -0700 Subject: [PATCH 029/202] use Clang_jll to make sure there is a compiler available --- Manifest.toml | 10 ++++++++++ Project.toml | 1 + src/StaticCompiler.jl | 8 ++++---- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Manifest.toml b/Manifest.toml index ab4efd77..43eef3ea 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -17,6 +17,12 @@ git-tree-sha1 = "215a9aa4a1f23fbd05b92769fdd62559488d70e9" uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82" version = "0.4.1" +[[deps.Clang_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll", "libLLVM_jll"] +git-tree-sha1 = "8cf7e67e264dedc5d321ec87e78525e958aea057" +uuid = "0ee61d77-7f21-5576-8119-9fcc46b10100" +version = "12.0.1+3" + [[deps.Dates]] deps = ["Printf"] uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" @@ -150,6 +156,10 @@ uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" deps = ["Libdl"] uuid = "83775a58-1f1d-513f-b197-d71354ab007a" +[[deps.libLLVM_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "8f36deef-c2a5-5394-99ed-8e07531fb29a" + [[deps.nghttp2_jll]] deps = ["Artifacts", "Libdl"] uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" diff --git a/Project.toml b/Project.toml index a1faac63..5293f7b2 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["Tom Short"] version = "0.3.0" [deps] +Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 29da3e65..952d6876 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -5,6 +5,7 @@ using LLVM: LLVM using Libdl: Libdl using Base: RefValue using Serialization: serialize, deserialize +using Clang_jll: clang export compile, load_function export native_code_llvm, native_code_typed, native_llvm_module @@ -82,7 +83,6 @@ load a `StaticCompiledFunction` from a given path. This object is callable. """ load_function(path) = instantiate(deserialize(path) :: LazyStaticCompiledFunction) - struct LazyStaticCompiledFunction{rt, tt} f::Symbol path::String @@ -138,7 +138,9 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf write(io, obj) flush(io) - run(`gcc -shared -o $path.$(Libdl.dlext) $path`) + clang() do exe + run(`$exe -shared -o $path.$(Libdl.dlext) $path`) + end rm(path) end path, name @@ -162,8 +164,6 @@ function generate_shlib_fptr(path::String, name) fptr end - - function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) job, kwargs = native_job(func, types; kwargs...) GPUCompiler.code_llvm(stdout, job; kwargs...) From d877bbfa90a06c0749b0f58dc6172c4c0ce1ee3a Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 21:23:24 -0700 Subject: [PATCH 030/202] no longer just call out to `gcc` willy nilly --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 0018a837..f979e873 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,6 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi ## Limitations -* This package currently requires that you have `gcc` installed and in your system's `PATH`. This is probably pretty easy to fix, we only use `gcc` for linking. In theory Clang_jll or LLVM_full_jll should be able to do this, and be managed through Julia's package manager. * No heap allocations (e.g. creating an array or a string) are allowed inside a statically compiled function body. If you try to run such a function, you will get a segfault. ** It's sometimes possible you won't get a segfault if you define and run the function in the same session, but trying to call the compiled function in a new julia session will definitely segfault if you allocate memory. * Doesn't currently work on Windows From 798c2d0349183af4c9ee8bfee7e38de286c54bcb Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 22:12:02 -0700 Subject: [PATCH 031/202] put compilation artifacts into one directory. --- README.md | 10 ++++----- src/StaticCompiler.jl | 48 ++++++++++++++++++++++++++----------------- test/runtests.jl | 28 ++++++++++++------------- 3 files changed, 48 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index f979e873..c8139ec3 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ julia> using StaticCompiler julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) fib (generic function with 1 method) -julia> path, fib_compiled = compile(fib, Tuple{Int}, "fib") -("fib.cjl", StaticCompiler.StaticCompiledFunction{Int64, Tuple{Int64}}(:fib, Ptr{Nothing} @0x00007fc4ec032130)) +julia> fib_compiled, path = compile(fib, Tuple{Int}, "fib") +(f = fib(::Int64) :: Int64, path = "fib") julia> fib_compiled(10) 55 @@ -34,10 +34,10 @@ julia> using StaticCompiler julia> fib ERROR: UndefVarError: fib not defined -julia> fib_compiled = load_function("fib.cjl") -StaticCompiler.StaticCompiledFunction{Int64, Tuple{Int64}}(:fib, Ptr{Nothing} @0x00007f9ee8050130) +julia> fib_comiled = load_function("fib") +fib(::Int64) :: Int64 -julia> fib_compiled(10) +julia> fib_comiled(10) 55 ``` diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 952d6876..a16c193d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -11,20 +11,20 @@ export compile, load_function export native_code_llvm, native_code_typed, native_llvm_module """ - compile(f, types, path::String = tempname()) --> (obj_path, compiled_f) + compile(f, types, path::String = tempname()) --> (compiled_f, path) !!! Warning: this will fail on programs that heap allocate any memory, or have dynamic dispatch !!! Statically compile the method of a function `f` specialized to arguments of the type given by `types`. -This will save a shared object file (i.e. a `.so` or `.dylib`) at the specified path, and will save a -`LazyStaticCompiledFunction` object at the same path with the extension `.cjl`. This -`LazyStaticCompiledFunction` can be deserialized with the function `load_function`. Once it is -instantiated in a julia session, it will be of type `StaticCompiledFunction` and may be called with -arguments of type `types` as if it were a function with a single method (the method determined by `types`). +This will create a directory at the specified path with a shared object file (i.e. a `.so` or `.dylib`), +and will save a `LazyStaticCompiledFunction` object in the same directory with the extension `.cjl`. This +`LazyStaticCompiledFunction` can be deserialized with `load_function(path)`. Once it is instantiated in +a julia session, it will be of type `StaticCompiledFunction` and may be called with arguments of type +`types` as if it were a function with a single method (the method determined by `types`). -`compile` will return a `obj_path` which is the location of the serialized `LazyStaticCompiledFunction`, and -an already instantiated `StaticCompiledFunction` object. +`compile` will return an already instantiated `StaticCompiledFunction` object and `obj_path` which is the +location of the directory containing the compilation artifacts. Example: @@ -35,8 +35,8 @@ julia> using StaticCompiler julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) fib (generic function with 1 method) -julia> path, fib_compiled = compile(fib, Tuple{Int}, "fib") -("fib.cjl", StaticCompiler.StaticCompiledFunction{Int64, Tuple{Int64}}(:fib, Ptr{Nothing} @0x00007fc4ec032130)) +julia> fib_compiled, path = compile(fib, Tuple{Int}, "fib") +(f = fib(::Int64) :: Int64, path = "fib") julia> fib_compiled(10) 55 @@ -49,7 +49,7 @@ julia> fib ERROR: UndefVarError: fib not defined julia> fib_compiled = load_function("fib.cjl") -StaticCompiler.StaticCompiledFunction{Int64, Tuple{Int64}}(:fib, Ptr{Nothing} @0x00007f9ee8050130) +fib(::Int64) :: Int64 julia> fib_compiled(10) 55 @@ -71,8 +71,9 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam generate_shlib(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; kwargs...) lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name) - serialize(path*".cjl", lf) - path*".cjl", instantiate(lf) + cjl_path = joinpath(path, "obj.cjl") + serialize(cjl_path, lf) + (; f = instantiate(lf), path) end @@ -81,7 +82,7 @@ end load a `StaticCompiledFunction` from a given path. This object is callable. """ -load_function(path) = instantiate(deserialize(path) :: LazyStaticCompiledFunction) +load_function(path) = instantiate(deserialize(joinpath(path, "obj.cjl")) :: LazyStaticCompiledFunction) struct LazyStaticCompiledFunction{rt, tt} f::Symbol @@ -98,6 +99,11 @@ struct StaticCompiledFunction{rt, tt} ptr::Ptr{Nothing} end +function Base.show(io::IO, f::StaticCompiledFunction{rt, tt}) where {rt, tt} + types = [tt.parameters...] + print(io, String(f.f), "(", join(("::$T" for T ∈ tt.parameters), ',') ,") :: $rt") +end + function (f::StaticCompiledFunction{rt, tt})(args...) where {rt, tt} Tuple{typeof.(args)...} == tt || error("Input types don't match compiled target $((tt.parameters...,)). Got arguments of type $(typeof.(args))") out = RefValue{rt}() @@ -132,33 +138,37 @@ function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=fals end function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)); kwargs...) - open(path, "w") do io + mkpath(path) + obj_path = joinpath(path, "obj") + lib_path = joinpath(path, "obj.$(Libdl.dlext)") + open(obj_path, "w") do io job, kwargs = native_job(f, tt; name, kwargs...) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) write(io, obj) flush(io) clang() do exe - run(`$exe -shared -o $path.$(Libdl.dlext) $path`) + run(`$exe -shared -o $lib_path $obj_path`) end - rm(path) end path, name end function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)); temp::Bool=true, kwargs...) generate_shlib(f, tt, path, name; kwargs...) + lib_path = joinpath(abspath(path), "obj.$(Libdl.dlext)") ptr = Libdl.dlopen("$(abspath(path)).$(Libdl.dlext)", Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @assert fptr != C_NULL if temp - atexit(()->rm("$path.$(Libdl.dlext)")) + atexit(()->rm(path; recursive=true)) end fptr end function generate_shlib_fptr(path::String, name) - ptr = Libdl.dlopen("$(abspath(path)).$(Libdl.dlext)", Libdl.RTLD_LOCAL) + lib_path = joinpath(abspath(path), "obj.$(Libdl.dlext)") + ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @assert fptr != C_NULL fptr diff --git a/test/runtests.jl b/test/runtests.jl index 51a88328..c0fcaf35 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,7 +12,7 @@ using StrideArraysCore # This probably needs a macro for T ∈ (Int, Float64, Int32, Float32, Int16, Float16) - @test compile(simple_sum, (T,))[2]( T(1) ) == T(2) + @test compile(simple_sum, (T,))[1]( T(1) ) == T(2) end end @@ -20,13 +20,13 @@ end fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 @testset "Recursion" begin - @test compile(fib, (Int,))[2](10) == fib(10) + @test compile(fib, (Int,))[1](10) == fib(10) # Trick to work around #40990 _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) fib2(n) = _fib2(_fib2, n) - @test compile(fib2, (Int,))[2](20) == fib(20) + @test compile(fib2, (Int,))[1](20) == fib(20) end # Call binaries for testing @@ -50,7 +50,7 @@ end end s end - @test compile(sum_first_N_int, (Int,))[2](10) == 55 + @test compile(sum_first_N_int, (Int,))[1](10) == 55 function sum_first_N_float64(N) s = Float64(0) @@ -59,7 +59,7 @@ end end s end - @test compile(sum_first_N_float64, (Int,))[2](10) == 55. + @test compile(sum_first_N_float64, (Int,))[1](10) == 55. function sum_first_N_int_inbounds(N) s = 0 @@ -68,7 +68,7 @@ end end s end - @test compile(sum_first_N_int_inbounds, (Int,))[2](10) == 55 + @test compile(sum_first_N_int_inbounds, (Int,))[1](10) == 55 function sum_first_N_float64_inbounds(N) s = Float64(0) @@ -77,7 +77,7 @@ end end s end - @test compile(sum_first_N_float64_inbounds, (Int,))[2](10) == 55. + @test compile(sum_first_N_float64_inbounds, (Int,))[1](10) == 55. end @@ -91,9 +91,9 @@ end s end - @test compile(array_sum, (Int, Vector{Int}))[2](10, Int.(1:10)) == 55 - @test compile(array_sum, (Int, Vector{Complex{Float32}}))[2](10, Complex{Float32}.(1:10)) == 55f0 + 0f0im - @test compile(array_sum, (Int, Vector{Complex{Float64}}))[2](10, Complex{Float64}.(1:10)) == 55f0 + 0f0im + @test compile(array_sum, (Int, Vector{Int}))[1](10, Int.(1:10)) == 55 + @test compile(array_sum, (Int, Vector{Complex{Float32}}))[1](10, Complex{Float32}.(1:10)) == 55f0 + 0f0im + @test compile(array_sum, (Int, Vector{Complex{Float64}}))[1](10, Complex{Float64}.(1:10)) == 55f0 + 0f0im end @@ -103,7 +103,7 @@ end @testset "Send and receive Tuple" begin foo(u::Tuple) = 2 .* reverse(u) .- 1 # we can't just compile this as is. - @test compile(foo, (NTuple{3, Int},))[2]((1, 2, 3)) == (5, 3, 1) + @test compile(foo, (NTuple{3, Int},))[1]((1, 2, 3)) == (5, 3, 1) end @@ -115,7 +115,7 @@ end end a = [1.0, 2.0] - @test compile(mydot, (Vector{Float64},))[2](a) == 5.0 + @test compile(mydot, (Vector{Float64},))[1](a) == 5.0 end @@ -146,7 +146,7 @@ end A = rand(10, 11) B = rand(11, 12) - compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},))[2](C, A, B) + compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},))[1](C, A, B) @test C ≈ A*B end @@ -164,7 +164,7 @@ end end end - @test compile(f, (Int,))[2](20) == 20 + @test compile(f, (Int,))[1](20) == 20 end From c4e440116dc0b99da6e7741166726af353f5e84f Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 22:25:42 -0700 Subject: [PATCH 032/202] try testing on windows again --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03f84766..e645408d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,7 @@ jobs: os: - ubuntu-latest - macOS-latest + - windows-latest arch: - x64 steps: From 57197cee8d995b2dba25bc2ece8985786014649c Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 22:49:20 -0700 Subject: [PATCH 033/202] try falling back to `gcc` --- src/StaticCompiler.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index a16c193d..9d3acedc 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -147,8 +147,13 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf write(io, obj) flush(io) - clang() do exe - run(`$exe -shared -o $lib_path $obj_path`) + try + clang() do exe + run(`$exe -shared -o $lib_path $obj_path`) + end + catch e; + # if Clang_jll fails, check if gcc is available + run(`gcc -shared -o $lib_path $obj_path`) end end path, name From 8b5268332888e90bc4ceab062d5a283f512a7c34 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 23:17:56 -0700 Subject: [PATCH 034/202] redirect clang output to devnull --- src/StaticCompiler.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 9d3acedc..2166b3e3 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -149,7 +149,7 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf flush(io) try clang() do exe - run(`$exe -shared -o $lib_path $obj_path`) + run(pipeline(`$exe -shared -o $lib_path $obj_path`, stdout=devnull)) #get rid of devnull for debugging end catch e; # if Clang_jll fails, check if gcc is available From 90c150b888463014232e93e9565d277f6cb7e806 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 23:19:04 -0700 Subject: [PATCH 035/202] don't test on windows --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e645408d..03f84766 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,6 @@ jobs: os: - ubuntu-latest - macOS-latest - - windows-latest arch: - x64 steps: From b5e123ad0d00482c086ffd42bb1619727e92f9cc Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Wed, 2 Feb 2022 23:25:58 -0700 Subject: [PATCH 036/202] fix --- src/StaticCompiler.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 2166b3e3..e272fab8 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -149,7 +149,7 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf flush(io) try clang() do exe - run(pipeline(`$exe -shared -o $lib_path $obj_path`, stdout=devnull)) #get rid of devnull for debugging + run(pipeline(`$exe -shared -o $lib_path $obj_path`, stderr=devnull)) #get rid of devnull when debugging end catch e; # if Clang_jll fails, check if gcc is available From 2de06f4ca3a3cf4a3c9752e90b90e016f6e5fa19 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Thu, 3 Feb 2022 08:21:03 -0700 Subject: [PATCH 037/202] fix generate_shlib_fptr --- src/StaticCompiler.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index e272fab8..7cef2880 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -162,7 +162,7 @@ end function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)); temp::Bool=true, kwargs...) generate_shlib(f, tt, path, name; kwargs...) lib_path = joinpath(abspath(path), "obj.$(Libdl.dlext)") - ptr = Libdl.dlopen("$(abspath(path)).$(Libdl.dlext)", Libdl.RTLD_LOCAL) + ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @assert fptr != C_NULL if temp From 310ffeaabf0b8254eb93e295c49a66381abd9182 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Thu, 3 Feb 2022 10:24:05 -0700 Subject: [PATCH 038/202] return abspath --- src/StaticCompiler.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 7cef2880..84ead380 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -73,7 +73,7 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name) cjl_path = joinpath(path, "obj.cjl") serialize(cjl_path, lf) - (; f = instantiate(lf), path) + (; f = instantiate(lf), path=abspath(path)) end From 70625560afe15cc8f108b6a023b1473802f1c66f Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Thu, 3 Feb 2022 15:20:16 -0700 Subject: [PATCH 039/202] do tests with a binary loaded on another process. This will catch reloactability problems --- test/Project.toml | 3 ++- test/runtests.jl | 61 +++++++++++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/test/Project.toml b/test/Project.toml index 3dee0e56..b0cf1dcc 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -6,4 +6,5 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" -StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" \ No newline at end of file +StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" +Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index c0fcaf35..9c1191ba 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,6 +5,12 @@ using LinearAlgebra using LoopVectorization using ManualMemory using StrideArraysCore +using Distributed + +addprocs(1) +@everywhere using StaticCompiler + +remote_load_call(path, args...) = fetch(@spawnat 2 load_function(path)(args...)) @testset "Basics" begin @@ -12,7 +18,8 @@ using StrideArraysCore # This probably needs a macro for T ∈ (Int, Float64, Int32, Float32, Int16, Float16) - @test compile(simple_sum, (T,))[1]( T(1) ) == T(2) + _, path, = compile(simple_sum, (T,)) + @test remote_load_call(path, T(1)) == T(2) end end @@ -20,13 +27,16 @@ end fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 @testset "Recursion" begin - @test compile(fib, (Int,))[1](10) == fib(10) + _, path = compile(fib, (Int,)) + @test remote_load_call(path, 10) == fib(10) # Trick to work around #40990 _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) fib2(n) = _fib2(_fib2, n) - - @test compile(fib2, (Int,))[1](20) == fib(20) + + _, path = compile(fib2, (Int,)) + @test remote_load_call(path, 20) == fib(20) + #@test compile(fib2, (Int,))[1](20) == fib(20) end # Call binaries for testing @@ -50,7 +60,8 @@ end end s end - @test compile(sum_first_N_int, (Int,))[1](10) == 55 + _, path = compile(sum_first_N_int, (Int,)) + @test remote_load_call(path, 10) == 55 function sum_first_N_float64(N) s = Float64(0) @@ -59,7 +70,8 @@ end end s end - @test compile(sum_first_N_float64, (Int,))[1](10) == 55. + _, path = compile(sum_first_N_float64, (Int,)) + @test remote_load_call(path, 10) == 55. function sum_first_N_int_inbounds(N) s = 0 @@ -68,7 +80,8 @@ end end s end - @test compile(sum_first_N_int_inbounds, (Int,))[1](10) == 55 + _, path = compile(sum_first_N_int_inbounds, (Int,)) + @test remote_load_call(path, 10) == 55 function sum_first_N_float64_inbounds(N) s = Float64(0) @@ -77,8 +90,8 @@ end end s end - @test compile(sum_first_N_float64_inbounds, (Int,))[1](10) == 55. - + _, path = compile(sum_first_N_float64_inbounds, (Int,)) + @test remote_load_call(path, 10) == 55. end # Arrays with different input types Int32, Int64, Float32, Float64, Complex? @@ -90,10 +103,14 @@ end end s end + for T ∈ (Int, Complex{Float32}, Complex{Float64}) + _, path = compile(array_sum, (Int, Vector{T})) + @test remote_load_call(path, 10, T.(1:10)) == T(55) + end - @test compile(array_sum, (Int, Vector{Int}))[1](10, Int.(1:10)) == 55 - @test compile(array_sum, (Int, Vector{Complex{Float32}}))[1](10, Complex{Float32}.(1:10)) == 55f0 + 0f0im - @test compile(array_sum, (Int, Vector{Complex{Float64}}))[1](10, Complex{Float64}.(1:10)) == 55f0 + 0f0im + # @test (10, Int.(1:10)) == 55 + # @test compile(array_sum, (Int, Vector{Complex{Float32}}))[1](10, Complex{Float32}.(1:10)) == 55f0 + 0f0im + # @test compile(array_sum, (Int, Vector{Complex{Float64}}))[1](10, Complex{Float64}.(1:10)) == 55f0 + 0f0im end @@ -101,9 +118,10 @@ end # We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function. # The interface made in `compile` should handle this fine. @testset "Send and receive Tuple" begin - foo(u::Tuple) = 2 .* reverse(u) .- 1 # we can't just compile this as is. - - @test compile(foo, (NTuple{3, Int},))[1]((1, 2, 3)) == (5, 3, 1) + foo(u::Tuple) = 2 .* reverse(u) .- 1 + + _, path = compile(foo, (NTuple{3, Int},)) + @test remote_load_call(path, (1, 2, 3)) == (5, 3, 1) end @@ -114,8 +132,9 @@ end BLAS.dot(N, a, 1, a, 1) end a = [1.0, 2.0] - - @test compile(mydot, (Vector{Float64},))[1](a) == 5.0 + mydot_compiled, path = compile(mydot, (Vector{Float64},)) + @test_skip remote_load_call(path, a) == 5.0 # this needs a relocatable pointer to work + @test mydot_compiled(a) == 5.0 end @@ -146,7 +165,9 @@ end A = rand(10, 11) B = rand(11, 12) - compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},))[1](C, A, B) + _, path = compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},)) + # remote_load_call(path, C, A, B) This won't work because @spawnat copies C + C .= fetch(@spawnat 2 (load_function(path)(C, A, B); C)) @test C ≈ A*B end @@ -163,8 +184,8 @@ end sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body end end - - @test compile(f, (Int,))[1](20) == 20 + _, path = compile(f, (Int,)) + @test remote_load_call(path, 20) == 20 end From 7f6477475af93e56e9323d81d579560e0d9a49d6 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Thu, 3 Feb 2022 15:22:29 -0700 Subject: [PATCH 040/202] use cc instead of gcc --- src/StaticCompiler.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 84ead380..78b85960 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -153,7 +153,7 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf end catch e; # if Clang_jll fails, check if gcc is available - run(`gcc -shared -o $lib_path $obj_path`) + run(`cc -shared -o $lib_path $obj_path`) end end path, name @@ -196,5 +196,4 @@ function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs return m end - end # module From e86be065467b16e3b0c4e2916e49d9fa5fdf4855 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Thu, 3 Feb 2022 21:21:45 -0500 Subject: [PATCH 041/202] Use .bc extension for LLVM bitcode --- src/StaticCompiler.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 78b85960..a8bfa7f7 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -15,15 +15,15 @@ export native_code_llvm, native_code_typed, native_llvm_module !!! Warning: this will fail on programs that heap allocate any memory, or have dynamic dispatch !!! -Statically compile the method of a function `f` specialized to arguments of the type given by `types`. +Statically compile the method of a function `f` specialized to arguments of the type given by `types`. -This will create a directory at the specified path with a shared object file (i.e. a `.so` or `.dylib`), -and will save a `LazyStaticCompiledFunction` object in the same directory with the extension `.cjl`. This -`LazyStaticCompiledFunction` can be deserialized with `load_function(path)`. Once it is instantiated in -a julia session, it will be of type `StaticCompiledFunction` and may be called with arguments of type +This will create a directory at the specified path with a shared object file (i.e. a `.so` or `.dylib`), +and will save a `LazyStaticCompiledFunction` object in the same directory with the extension `.cjl`. This +`LazyStaticCompiledFunction` can be deserialized with `load_function(path)`. Once it is instantiated in +a julia session, it will be of type `StaticCompiledFunction` and may be called with arguments of type `types` as if it were a function with a single method (the method determined by `types`). -`compile` will return an already instantiated `StaticCompiledFunction` object and `obj_path` which is the +`compile` will return an already instantiated `StaticCompiledFunction` object and `obj_path` which is the location of the directory containing the compilation artifacts. Example: @@ -67,7 +67,7 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - + generate_shlib(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; kwargs...) lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name) @@ -139,12 +139,12 @@ end function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)); kwargs...) mkpath(path) - obj_path = joinpath(path, "obj") + obj_path = joinpath(path, "obj.bc") lib_path = joinpath(path, "obj.$(Libdl.dlext)") open(obj_path, "w") do io job, kwargs = native_job(f, tt; name, kwargs...) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) - + write(io, obj) flush(io) try From 4e3faf3880f9e4a21318bc9adc9476fcb96874d1 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Thu, 3 Feb 2022 22:22:08 -0500 Subject: [PATCH 042/202] Add docstring for generate_shlib --- src/StaticCompiler.jl | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index a8bfa7f7..463747aa 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -26,7 +26,7 @@ a julia session, it will be of type `StaticCompiledFunction` and may be called w `compile` will return an already instantiated `StaticCompiledFunction` object and `obj_path` which is the location of the directory containing the compilation artifacts. -Example: +### Examples: Define and compile a `fib` function: ```julia @@ -137,6 +137,43 @@ function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=fals GPUCompiler.CompilerJob(target, source, params), kwargs end + +""" +```julia +generate_shlib(f, tt, path::String, name::String; kwargs...) +``` +Low level interface for compiling a shared object / dynamically loaded library + (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing +the types of the arguments for which the function will be compiled. + +### Examples +```julia +julia> function test(n) + r = 0.0 + for i=1:n + r += log(sqrt(i)) + end + return r/n + end +test (generic function with 1 method) + +julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, "./test") +("./test", "test") + +shell> tree \$path +./test +|-- obj.bc +`-- obj.dylib + +0 directories, 2 files + +julia> test(100_000) +5.256496109495593 + +julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 100_000) +5.256496109495593 +``` +""" function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)); kwargs...) mkpath(path) obj_path = joinpath(path, "obj.bc") From e82f0fd6c9316c8dba4d3f39c33f5dc3ebf2c60f Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Thu, 3 Feb 2022 22:48:30 -0500 Subject: [PATCH 043/202] Add docstring for `generate_shlib_fptr` --- src/StaticCompiler.jl | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 463747aa..c49f0b88 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -146,6 +146,8 @@ Low level interface for compiling a shared object / dynamically loaded library (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. +See also `StaticCompiler.generate_shlib_fptr`. + ### Examples ```julia julia> function test(n) @@ -196,6 +198,7 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf path, name end + function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)); temp::Bool=true, kwargs...) generate_shlib(f, tt, path, name; kwargs...) lib_path = joinpath(abspath(path), "obj.$(Libdl.dlext)") @@ -208,6 +211,42 @@ function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler. fptr end +""" +```julia +generate_shlib_fptr(path::String, name) +``` +Low level interface for obtaining a function pointer by `dlopen`ing a shared +library given the `path` and `name` of a `.so`/`.dylib` already compiled by +`generate_shlib`. + +See also `StaticCompiler.enerate_shlib`. + +### Examples +```julia +julia> function test(n) + r = 0.0 + for i=1:n + r += log(sqrt(i)) + end + return r/n + end +test (generic function with 1 method) + +julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, "./test"); + +julia> test_ptr = StaticCompiler.generate_shlib_fptr(path, name) +Ptr{Nothing} @0x000000015209f600 + +julia> ccall(test_ptr, Float64, (Int64,), 100_000) +5.256496109495593 + +julia> @ccall \$test_ptr(100_000::Int64)::Float64 # Equivalently +5.256496109495593 + +julia> test(100_000) +5.256496109495593 +``` +""" function generate_shlib_fptr(path::String, name) lib_path = joinpath(abspath(path), "obj.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) From f94c0556827dd2b6629fc6f682cebbc15b6ba0c8 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Thu, 3 Feb 2022 23:04:25 -0500 Subject: [PATCH 044/202] Special case for apple, allow compiler errors through again in other cases --- src/StaticCompiler.jl | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index c49f0b88..6cc90d67 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -186,13 +186,15 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf write(io, obj) flush(io) + + # Pick a Clang + cc = Sys.isapple() ? `cc` : clang() + # Compile! try - clang() do exe - run(pipeline(`$exe -shared -o $lib_path $obj_path`, stderr=devnull)) #get rid of devnull when debugging - end - catch e; - # if Clang_jll fails, check if gcc is available - run(`cc -shared -o $lib_path $obj_path`) + run(`$cc -shared -o $lib_path $obj_path`) + catch + # If all Clangs fail, try system gcc + run(`gcc -shared -o $lib_path $obj_path`) end end path, name From 195960343d17e16b2874baf902c18985b0075fc2 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 3 Feb 2022 21:34:46 -0700 Subject: [PATCH 045/202] Update StaticCompiler.jl --- src/StaticCompiler.jl | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 6cc90d67..5842b723 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -190,12 +190,7 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! - try - run(`$cc -shared -o $lib_path $obj_path`) - catch - # If all Clangs fail, try system gcc - run(`gcc -shared -o $lib_path $obj_path`) - end + run(`$cc -shared -o $lib_path $obj_path`) end path, name end From bfdd7ba3a7a149cacdf64eacf941de65d20e5c16 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Thu, 3 Feb 2022 21:45:23 -0700 Subject: [PATCH 046/202] rename `.bc` to `.o` --- src/StaticCompiler.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 5842b723..5d3edb2b 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -178,7 +178,7 @@ julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), """ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)); kwargs...) mkpath(path) - obj_path = joinpath(path, "obj.bc") + obj_path = joinpath(path, "obj.o") lib_path = joinpath(path, "obj.$(Libdl.dlext)") open(obj_path, "w") do io job, kwargs = native_job(f, tt; name, kwargs...) From f710f81fe435dfb0f5323cef9e711fe68f3d4855 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Fri, 4 Feb 2022 11:49:46 -0700 Subject: [PATCH 047/202] don't bother building docs for now --- .github/workflows/{docs.yml => docsyml_backup} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{docs.yml => docsyml_backup} (100%) diff --git a/.github/workflows/docs.yml b/.github/workflows/docsyml_backup similarity index 100% rename from .github/workflows/docs.yml rename to .github/workflows/docsyml_backup From 438f02b1d93cb80b4ad984ef696b991657660ede Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Fri, 4 Feb 2022 11:50:05 -0700 Subject: [PATCH 048/202] fix typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c8139ec3..e458edda 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,10 @@ julia> using StaticCompiler julia> fib ERROR: UndefVarError: fib not defined -julia> fib_comiled = load_function("fib") +julia> fib_compiled = load_function("fib") fib(::Int64) :: Int64 -julia> fib_comiled(10) +julia> fib_compiled(10) 55 ``` From 4d348e52a430365a3e13aa8da6222a267e3e51d2 Mon Sep 17 00:00:00 2001 From: MasonProtter Date: Fri, 4 Feb 2022 11:50:12 -0700 Subject: [PATCH 049/202] iterate on docs --- src/StaticCompiler.jl | 46 +++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 5d3edb2b..8d0ec0e9 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -17,16 +17,16 @@ export native_code_llvm, native_code_typed, native_llvm_module Statically compile the method of a function `f` specialized to arguments of the type given by `types`. -This will create a directory at the specified path with a shared object file (i.e. a `.so` or `.dylib`), -and will save a `LazyStaticCompiledFunction` object in the same directory with the extension `.cjl`. This -`LazyStaticCompiledFunction` can be deserialized with `load_function(path)`. Once it is instantiated in -a julia session, it will be of type `StaticCompiledFunction` and may be called with arguments of type -`types` as if it were a function with a single method (the method determined by `types`). +This will create a directory at the specified path (or in a temporary directory if you exclude that argument) +that contains the files needed for your static compiled function. `compile` will return a +`StaticCompiledFunction` object and `obj_path` which is the absolute path of the directory containing the +compilation artifacts. The `StaticCompiledFunction` can be treated as if it is a function with a single +method corresponding to the types you specified when it was compiled. -`compile` will return an already instantiated `StaticCompiledFunction` object and `obj_path` which is the -location of the directory containing the compilation artifacts. +To deserialize and instantiate a previously compiled function, simply execute `load_function(path)`, which +returns a callable `StaticCompiledFunction`. -### Examples: +### Example: Define and compile a `fib` function: ```julia @@ -43,11 +43,11 @@ julia> fib_compiled(10) ``` Now we can quit this session and load a new one where `fib` is not defined: ```julia -julia> using StaticCompiler - julia> fib ERROR: UndefVarError: fib not defined +julia> using StaticCompiler + julia> fib_compiled = load_function("fib.cjl") fib(::Int64) :: Int64 @@ -55,6 +55,27 @@ julia> fib_compiled(10) 55 ``` Tada! + +### Details: + +Here is the structure of the directory created by `compile` in the above example: +```julia +shell> tree fib +path +├── obj.cjl +├── obj.o +└── obj.so + +0 directories, 3 files +```` +* `obj.so` (or `.dylib` on MacOS) is a shared object file that can be linked to in order to execute your +compiled julia function. +* `obj.cjl` is a serialized `LazyStaticCompiledFunction` object which will be deserialized and instantiated +with `load_function(path)`. `LazyStaticcompiledfunction`s contain the requisite information needed to link to the +`obj.so` inside a julia session. Once it is instantiated in a julia session (i.e. by +`instantiate(::LazyStaticCompiledFunction)`, this happens automatically in `load_function`), it will be of type +`StaticCompiledFunction` and may be called with arguments of type `types` as if it were a function with a +single method (the method determined by `types`). """ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_name(repr(f)), kwargs...) tt = Base.to_tuple_type(_tt) @@ -76,7 +97,6 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam (; f = instantiate(lf), path=abspath(path)) end - """ load_function(path) --> compiled_f @@ -164,8 +184,8 @@ julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, "./test") shell> tree \$path ./test -|-- obj.bc -`-- obj.dylib +|-- obj.o +`-- obj.so 0 directories, 2 files From 567168a81b5d887c6fc436f00bde6d529b6690d6 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Thu, 10 Feb 2022 01:22:48 -0500 Subject: [PATCH 050/202] Add `compile_executable` (#56) * Allow changeing filename base in generate_shlib, though leave obj as default * Add `generate_executable` * Add `compile_executable` * Update docstring for compile_executable * Add tests for compile_executable * llvmcall used in standalone test only works on Julia 1.8+ * Looks like clang_jll ain't gonna cut it for the standalone executables even on linux * Try annotating compiled function with Base.@ccallable * Let's try some inlining * Ah, so `@ccallable` requires a return type * Ok, so no `@ccallable` * Potential workaround on non-apple systems: use minimal wrapper * We can probably use clang_jll after all * Probably about time for a version bump * Add more minimal test for v1.7, fix specifier on advanced test * Implement changes requested in code review --- src/StaticCompiler.jl | 171 +++++++++++++++++++++++++++++++++++++----- test/runtests.jl | 63 ++++++++++++++-- 2 files changed, 206 insertions(+), 28 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 8d0ec0e9..c3cd8556 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -7,20 +7,20 @@ using Base: RefValue using Serialization: serialize, deserialize using Clang_jll: clang -export compile, load_function +export compile, load_function, compile_executable export native_code_llvm, native_code_typed, native_llvm_module """ compile(f, types, path::String = tempname()) --> (compiled_f, path) - !!! Warning: this will fail on programs that heap allocate any memory, or have dynamic dispatch !!! + !!! Warning: this will fail on programs that heap allocate any memory tracked by the GC, or have dynamic dispatch !!! Statically compile the method of a function `f` specialized to arguments of the type given by `types`. This will create a directory at the specified path (or in a temporary directory if you exclude that argument) that contains the files needed for your static compiled function. `compile` will return a -`StaticCompiledFunction` object and `obj_path` which is the absolute path of the directory containing the -compilation artifacts. The `StaticCompiledFunction` can be treated as if it is a function with a single +`StaticCompiledFunction` object and `obj_path` which is the absolute path of the directory containing the +compilation artifacts. The `StaticCompiledFunction` can be treated as if it is a function with a single method corresponding to the types you specified when it was compiled. To deserialize and instantiate a previously compiled function, simply execute `load_function(path)`, which @@ -68,14 +68,14 @@ path 0 directories, 3 files ```` -* `obj.so` (or `.dylib` on MacOS) is a shared object file that can be linked to in order to execute your -compiled julia function. +* `obj.so` (or `.dylib` on MacOS) is a shared object file that can be linked to in order to execute your +compiled julia function. * `obj.cjl` is a serialized `LazyStaticCompiledFunction` object which will be deserialized and instantiated -with `load_function(path)`. `LazyStaticcompiledfunction`s contain the requisite information needed to link to the -`obj.so` inside a julia session. Once it is instantiated in a julia session (i.e. by -`instantiate(::LazyStaticCompiledFunction)`, this happens automatically in `load_function`), it will be of type -`StaticCompiledFunction` and may be called with arguments of type `types` as if it were a function with a -single method (the method determined by `types`). +with `load_function(path)`. `LazyStaticcompiledfunction`s contain the requisite information needed to link to the +`obj.so` inside a julia session. Once it is instantiated in a julia session (i.e. by +`instantiate(::LazyStaticCompiledFunction)`, this happens automatically in `load_function`), it will be of type +`StaticCompiledFunction` and may be called with arguments of type `types` as if it were a function with a +single method (the method determined by `types`). """ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_name(repr(f)), kwargs...) tt = Base.to_tuple_type(_tt) @@ -134,6 +134,81 @@ end instantiate(f::StaticCompiledFunction) = f + +""" +```julia +compile_executable(f, types::Tuple, path::String, name::String=repr(f); filename::String=name, kwargs...) +``` +Attempt to compile a standalone executable that runs function `f` with a type signature given by the tuple of `types`. + +### Examples +```julia +julia> using StaticCompiler + +julia> function puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates. + # Note, this `llvmcall` requires Julia 1.8+ + Base.llvmcall((\""" + ; External declaration of the puts function + declare i32 @puts(i8* nocapture) nounwind + + define i32 @main(i8*) { + entry: + %call = call i32 (i8*) @puts(i8* %0) + ret i32 0 + } + \""", "main"), Int32, Tuple{Ptr{UInt8}}, s) + end +puts (generic function with 1 method) + +julia> function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) + for i=1:argc + # Get pointer + p = unsafe_load(argv, i) + # Print string at pointer location (which fortunately already exists isn't tracked by the GC) + puts(p) + end + return 0 + end + +julia> compile_executable(print_args, (Int, Ptr{Ptr{UInt8}})) +"/Users/foo/code/StaticCompiler.jl/print_args" + +shell> ./print_args 1 2 3 4 Five +./print_args +1 +2 +3 +4 +Five +``` +```julia +julia> using StaticTools # So you don't have to define `puts` and friends every time + +julia> hello() = println(c"Hello, world!") # c"..." makes a stack-allocated StaticString + +julia> compile_executable(hello) +"/Users/foo/code/StaticCompiler.jl/hello" + +shell> ./hello +Hello, world! +``` +""" +function compile_executable(f, _tt=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); filename=name, kwargs...) + tt = Base.to_tuple_type(_tt) + tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $_tt must be either () or (Int, Ptr{Ptr{UInt8}})") + + rt = only(native_code_typed(f, tt))[2] + isconcretetype(rt) || error("$f$_tt did not infer to a concrete type. Got $rt") + + # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals + # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this + + generate_executable(f, tt, path, name, filename; kwargs...) + + joinpath(abspath(path), filename) +end + + module TestRuntime # dummy methods signal_exception() = return @@ -160,7 +235,7 @@ end """ ```julia -generate_shlib(f, tt, path::String, name::String; kwargs...) +generate_shlib(f, tt, path::String, name::String, filenamebase::String="obj"; kwargs...) ``` Low level interface for compiling a shared object / dynamically loaded library (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing @@ -196,10 +271,10 @@ julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 5.256496109495593 ``` """ -function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)); kwargs...) +function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; kwargs...) mkpath(path) - obj_path = joinpath(path, "obj.o") - lib_path = joinpath(path, "obj.$(Libdl.dlext)") + obj_path = joinpath(path, "$filenamebase.o") + lib_path = joinpath(path, "$filenamebase.$(Libdl.dlext)") open(obj_path, "w") do io job, kwargs = native_job(f, tt; name, kwargs...) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) @@ -216,9 +291,9 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf end -function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)); temp::Bool=true, kwargs...) +function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; temp::Bool=true, kwargs...) generate_shlib(f, tt, path, name; kwargs...) - lib_path = joinpath(abspath(path), "obj.$(Libdl.dlext)") + lib_path = joinpath(abspath(path), "$filenamebase.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @assert fptr != C_NULL @@ -236,7 +311,7 @@ Low level interface for obtaining a function pointer by `dlopen`ing a shared library given the `path` and `name` of a `.so`/`.dylib` already compiled by `generate_shlib`. -See also `StaticCompiler.enerate_shlib`. +See also `StaticCompiler.generate_shlib`. ### Examples ```julia @@ -264,14 +339,70 @@ julia> test(100_000) 5.256496109495593 ``` """ -function generate_shlib_fptr(path::String, name) - lib_path = joinpath(abspath(path), "obj.$(Libdl.dlext)") +function generate_shlib_fptr(path::String, name, filenamebase::String="obj") + lib_path = joinpath(abspath(path), "$filenamebase.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @assert fptr != C_NULL fptr end +""" +```julia +generate_executable(f, tt, path::String, name, filename=string(name); kwargs...) +``` +Attempt to compile a standalone executable that runs `f`. + +### Examples +```julia +julia> function test(n) + r = 0.0 + for i=1:n + r += log(sqrt(i)) + end + return r/n + end +test (generic function with 1 method) + +julia> path, name = StaticCompiler.generate_executable(test, Tuple{Int64}, "./scratch") +``` +""" +function generate_executable(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=string(name); kwargs...) + mkpath(path) + obj_path = joinpath(path, "$filename.o") + exec_path = joinpath(path, filename) + open(obj_path, "w") do io + job, kwargs = native_job(f, tt; name, kwargs...) + obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) + + write(io, obj) + flush(io) + + # Pick a compiler + cc = Sys.isapple() ? `cc` : clang() + # Compile! + if Sys.isapple() + # Apple no longer uses _start, so we can just specify a custom entry + entry = "_julia_$name" + run(`$cc -e $entry $obj_path -o $exec_path`) + else + # Write a minimal wrapper to avoid having to specify a custom entry + wrapper_path = joinpath(path, "wrapper.c") + f = open(wrapper_path, "w") + print(f, """int main(int argc, char** argv) + { + julia_$name(argc, argv); + return 0; + }""") + close(f) + run(`$cc $wrapper_path $obj_path -o $exec_path`) + # Clean up + run(`rm $wrapper_path`) + end + end + path, name +end + function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) job, kwargs = native_job(func, types; kwargs...) GPUCompiler.code_llvm(stdout, job; kwargs...) diff --git a/test/runtests.jl b/test/runtests.jl index 9c1191ba..3201dbe6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -33,16 +33,16 @@ fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globall # Trick to work around #40990 _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) fib2(n) = _fib2(_fib2, n) - + _, path = compile(fib2, (Int,)) @test remote_load_call(path, 20) == fib(20) - #@test compile(fib2, (Int,))[1](20) == fib(20) + #@test compile(fib2, (Int,))[1](20) == fib(20) end # Call binaries for testing # @testset "Generate binary" begin # fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) -# libname = tempname() +# libname = tempname() # generate_shlib(fib, (Int,), libname) # ptr = Libdl.dlopen(libname * "." * Libdl.dlext, Libdl.RTLD_LOCAL) # fptr = Libdl.dlsym(ptr, "julia_fib") @@ -62,7 +62,7 @@ end end _, path = compile(sum_first_N_int, (Int,)) @test remote_load_call(path, 10) == 55 - + function sum_first_N_float64(N) s = Float64(0) for a in 1:N @@ -116,10 +116,10 @@ end # Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. # We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function. -# The interface made in `compile` should handle this fine. +# The interface made in `compile` should handle this fine. @testset "Send and receive Tuple" begin foo(u::Tuple) = 2 .* reverse(u) .- 1 - + _, path = compile(foo, (NTuple{3, Int},)) @test remote_load_call(path, (1, 2, 3)) == (5, 3, 1) end @@ -172,7 +172,7 @@ end end # This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). -# This lets us have intermediate, mutable stack allocated arrays inside our +# This lets us have intermediate, mutable stack allocated arrays inside our @testset "Alloca" begin function f(N) # this can hold at most 100 Int values, if you use it for more, you'll segfault @@ -186,7 +186,54 @@ end end _, path = compile(f, (Int,)) @test remote_load_call(path, 20) == 20 -end +end + +@testset "Standalone Executables" begin + # Minimal test with no `llvmcall` + @inline function foo() + v = 0.0 + n = 1000 + for i=1:n + v += sqrt(n) + end + return 0 + end + + filepath = compile_executable(foo, (), tempdir()) + + r = run(`$filepath`); + @test isa(r, Base.Process) + @test r.exitcode == 0 + @static if VERSION>v"1.8.0-DEV" # The llvmcall here only works on 1.8+ + @inline function puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates + Base.llvmcall((""" + ; External declaration of the puts function + declare i32 @puts(i8* nocapture) nounwind + define i32 @main(i8*) { + entry: + %call = call i32 (i8*) @puts(i8* %0) + ret i32 0 + } + """, "main"), Int32, Tuple{Ptr{UInt8}}, s) + end + + @inline function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) + for i=1:argc + # Get pointer + p = unsafe_load(argv, i) + # Print string at pointer location (which fortunately already exists isn't tracked by the GC) + puts(p) + end + return 0 + end + + filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), tempdir()) + + r = run(`$filepath Hello, world!`); + @test isa(r, Base.Process) + @test r.exitcode == 0 + end +end # data structures, dictionaries, tuples, named tuples From 536ea4410e0d04916ba722c4d7298d83a76f98aa Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 10 Feb 2022 22:34:12 -0700 Subject: [PATCH 051/202] Patch and relocate pointers (#58) * Patch and relocate points * comment out erroneous `@show` * doc update and `filename` support in `compile` * Update src/StaticCompiler.jl Co-authored-by: Julian Samaroo * Update README.md Co-authored-by: Julian Samaroo * Update src/StaticCompiler.jl Co-authored-by: Julian Samaroo * Update src/StaticCompiler.jl Co-authored-by: Julian Samaroo * Update src/StaticCompiler.jl Co-authored-by: Julian Samaroo * Update src/StaticCompiler.jl Co-authored-by: Julian Samaroo * Update src/pointer_patching.jl Co-authored-by: Julian Samaroo * Apply suggestions from code review Co-authored-by: Julian Samaroo * reenable blas test * go back to serialization stdlib * note on gvs * try using JITTargetMachine for the dynamic linker * try running CI on windows * do some code reorganization * refine warning * allow windows failures * add options to strip the llvm / asm code * remove `@showln` * add `pointer_patching_diff` function to quickly see the effect of `relocation_table!` * improve pointer relocation * consolidate logic and reduce segfaults during compilation * remove unnecessary stuff * don't duplicate global values, switch from Dict to IdDict for reloc * avoid re-creation of global variables * more tests * add dummy test for error handing * test with ErrorTypes.jl * bump version Co-authored-by: Julian Samaroo --- .github/workflows/ci.yml | 2 + Manifest.toml | 97 +++++++++++++++++++++- Project.toml | 3 +- README.md | 10 ++- src/StaticCompiler.jl | 132 ++++++++++------------------- src/code_loading.jl | 77 +++++++++++++++++ src/pointer_patching.jl | 174 +++++++++++++++++++++++++++++++++++++++ src/target.jl | 43 ++++++++++ test/Project.toml | 3 +- test/runtests.jl | 86 +++++++++++++++++-- 10 files changed, 523 insertions(+), 104 deletions(-) create mode 100644 src/code_loading.jl create mode 100644 src/pointer_patching.jl create mode 100644 src/target.jl diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03f84766..0ea99ae6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,6 +6,7 @@ jobs: test: name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} runs-on: ${{ matrix.os }} + continue-on-error: ${{ matrix.os == 'windows-latest' }} strategy: fail-fast: false matrix: @@ -15,6 +16,7 @@ jobs: os: - ubuntu-latest - macOS-latest + - windows-latest arch: - x64 steps: diff --git a/Manifest.toml b/Manifest.toml index 43eef3ea..a31f8206 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -1,6 +1,6 @@ # This file is machine-generated - editing it directly is not advised -julia_version = "1.7.1" +julia_version = "1.7.2" manifest_format = "2.0" [[deps.ArgTools]] @@ -23,10 +23,34 @@ git-tree-sha1 = "8cf7e67e264dedc5d321ec87e78525e958aea057" uuid = "0ee61d77-7f21-5576-8119-9fcc46b10100" version = "12.0.1+3" +[[deps.Compat]] +deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] +git-tree-sha1 = "44c37b4636bc54afac5c574d2d02b625349d6582" +uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" +version = "3.41.0" + +[[deps.CompilerSupportLibraries_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" + +[[deps.DataStructures]] +deps = ["Compat", "InteractiveUtils", "OrderedCollections"] +git-tree-sha1 = "3daef5523dd2e769dad2365274f760ff5f282c7d" +uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +version = "0.18.11" + [[deps.Dates]] deps = ["Printf"] uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" +[[deps.DelimitedFiles]] +deps = ["Mmap"] +uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" + +[[deps.Distributed]] +deps = ["Random", "Serialization", "Sockets"] +uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" + [[deps.Downloads]] deps = ["ArgTools", "LibCURL", "NetworkOptions"] uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" @@ -36,6 +60,12 @@ git-tree-sha1 = "56559bbef6ca5ea0c0818fa5c90320398a6fbf8d" uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04" version = "0.1.8" +[[deps.FileIO]] +deps = ["Pkg", "Requires", "UUIDs"] +git-tree-sha1 = "80ced645013a5dbdc52cf70329399c35ce007fae" +uuid = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" +version = "1.13.0" + [[deps.GPUCompiler]] deps = ["ExprTools", "InteractiveUtils", "LLVM", "Libdl", "Logging", "TimerOutputs", "UUIDs"] git-tree-sha1 = "abd824e1f2ecd18d33811629c781441e94a24e81" @@ -46,6 +76,12 @@ version = "0.13.11" deps = ["Markdown"] uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +[[deps.JLD2]] +deps = ["DataStructures", "FileIO", "MacroTools", "Mmap", "Pkg", "Printf", "Reexport", "TranscodingStreams", "UUIDs"] +git-tree-sha1 = "b528d68220e2aba1d2d0c0461b6f7eda8c5c1e33" +uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819" +version = "0.4.20" + [[deps.JLLWrappers]] deps = ["Preferences"] git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" @@ -83,9 +119,19 @@ uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" [[deps.Libdl]] uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" +[[deps.LinearAlgebra]] +deps = ["Libdl", "libblastrampoline_jll"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + [[deps.Logging]] uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" +[[deps.MacroTools]] +deps = ["Markdown", "Random"] +git-tree-sha1 = "3d3e902b31198a27340d0bf00d6ac452866021cf" +uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" +version = "0.5.9" + [[deps.Markdown]] deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" @@ -94,12 +140,24 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" deps = ["Artifacts", "Libdl"] uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" +[[deps.Mmap]] +uuid = "a63ad114-7e13-5084-954f-fe012c677804" + [[deps.MozillaCACerts_jll]] uuid = "14a3606d-f60d-562e-9121-12d972cd8159" [[deps.NetworkOptions]] uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" +[[deps.OpenBLAS_jll]] +deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] +uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" + +[[deps.OrderedCollections]] +git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c" +uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +version = "1.4.1" + [[deps.Pkg]] deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" @@ -122,15 +180,38 @@ uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" deps = ["SHA", "Serialization"] uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +[[deps.Reexport]] +git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b" +uuid = "189a3867-3050-52da-a836-e630ba90ab69" +version = "1.2.2" + +[[deps.Requires]] +deps = ["UUIDs"] +git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7" +uuid = "ae029012-a4dd-5104-9daa-d747884805df" +version = "1.3.0" + [[deps.SHA]] uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" [[deps.Serialization]] uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" +[[deps.SharedArrays]] +deps = ["Distributed", "Mmap", "Random", "Serialization"] +uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" + [[deps.Sockets]] uuid = "6462fe0b-24de-5631-8697-dd941f90decc" +[[deps.SparseArrays]] +deps = ["LinearAlgebra", "Random"] +uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[[deps.Statistics]] +deps = ["LinearAlgebra", "SparseArrays"] +uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + [[deps.TOML]] deps = ["Dates"] uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" @@ -139,12 +220,22 @@ uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" deps = ["ArgTools", "SHA"] uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" +[[deps.Test]] +deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + [[deps.TimerOutputs]] deps = ["ExprTools", "Printf"] git-tree-sha1 = "97e999be94a7147d0609d0b9fc9feca4bf24d76b" uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" version = "0.5.15" +[[deps.TranscodingStreams]] +deps = ["Random", "Test"] +git-tree-sha1 = "216b95ea110b5972db65aa90f88d8d89dcb8851c" +uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" +version = "0.9.6" + [[deps.UUIDs]] deps = ["Random", "SHA"] uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" @@ -160,6 +251,10 @@ uuid = "83775a58-1f1d-513f-b197-d71354ab007a" deps = ["Artifacts", "Libdl"] uuid = "8f36deef-c2a5-5394-99ed-8e07531fb29a" +[[deps.libblastrampoline_jll]] +deps = ["Artifacts", "Libdl", "OpenBLAS_jll"] +uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" + [[deps.nghttp2_jll]] deps = ["Artifacts", "Libdl"] uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" diff --git a/Project.toml b/Project.toml index 5293f7b2..ef7854a2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,11 +1,12 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short"] -version = "0.3.0" +version = "0.4.0" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" diff --git a/README.md b/README.md index e458edda..0c9d6d45 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,9 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi ## Limitations -* No heap allocations (e.g. creating an array or a string) are allowed inside a statically compiled function body. If you try to run such a function, you will get a segfault. -** It's sometimes possible you won't get a segfault if you define and run the function in the same session, but trying to call the compiled function in a new julia session will definitely segfault if you allocate memory. -* Doesn't currently work on Windows -* If you find any other limitations, let us know. There's probably lots. +* 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 varaibles do *not* work with `compile_executable` (yet). +* Type unstable code is not yet supported. +* Throwing errors is not currently supported. In the meantime, consider wrapping possible errors with [ErrorTypes.jl](https://github.com/jakobnissen/ErrorTypes.jl) +* Doesn't currently work on Windows. +* If you find any other limitations, let us know. There's probably lots. \ No newline at end of file diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index c3cd8556..611bc92b 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -8,12 +8,16 @@ using Serialization: serialize, deserialize using Clang_jll: clang export compile, load_function, compile_executable -export native_code_llvm, native_code_typed, native_llvm_module +export native_code_llvm, native_code_typed, native_llvm_module, native_code_native + +include("target.jl") +include("pointer_patching.jl") +include("code_loading.jl") """ compile(f, types, path::String = tempname()) --> (compiled_f, path) - !!! Warning: this will fail on programs that heap allocate any memory tracked by the GC, or have dynamic dispatch !!! + !!! Warning: this will fail on programs that have dynamic dispatch !!! Statically compile the method of a function `f` specialized to arguments of the type given by `types`. @@ -63,77 +67,38 @@ Here is the structure of the directory created by `compile` in the above example shell> tree fib path ├── obj.cjl -├── obj.o -└── obj.so +└── obj.o 0 directories, 3 files ```` -* `obj.so` (or `.dylib` on MacOS) is a shared object file that can be linked to in order to execute your -compiled julia function. +* `obj.o` contains statically compiled code in the form of an LLVM generated object file. * `obj.cjl` is a serialized `LazyStaticCompiledFunction` object which will be deserialized and instantiated with `load_function(path)`. `LazyStaticcompiledfunction`s contain the requisite information needed to link to the -`obj.so` inside a julia session. Once it is instantiated in a julia session (i.e. by +`obj.o` inside a julia session. Once it is instantiated in a julia session (i.e. by `instantiate(::LazyStaticCompiledFunction)`, this happens automatically in `load_function`), it will be of type `StaticCompiledFunction` and may be called with arguments of type `types` as if it were a function with a single method (the method determined by `types`). """ -function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_name(repr(f)), kwargs...) +function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_name(repr(f)), filename="obj", + strip_llvm = false, + strip_asm = true, + kwargs...) tt = Base.to_tuple_type(_tt) isconcretetype(tt) || error("input type signature $_tt is not concrete") rt = only(native_code_typed(f, tt))[2] isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") - - # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals - # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - + f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - - generate_shlib(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; kwargs...) - - lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name) - cjl_path = joinpath(path, "obj.cjl") + _, _, table = generate_shlib(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; strip_llvm, strip_asm, filename, kwargs...) + + lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name, filename, table) + cjl_path = joinpath(path, "$filename.cjl") serialize(cjl_path, lf) - (; f = instantiate(lf), path=abspath(path)) -end - -""" - load_function(path) --> compiled_f - -load a `StaticCompiledFunction` from a given path. This object is callable. -""" -load_function(path) = instantiate(deserialize(joinpath(path, "obj.cjl")) :: LazyStaticCompiledFunction) -struct LazyStaticCompiledFunction{rt, tt} - f::Symbol - path::String - name::String -end - -function instantiate(p::LazyStaticCompiledFunction{rt, tt}) where {rt, tt} - StaticCompiledFunction{rt, tt}(p.f, generate_shlib_fptr(p.path::String, p.name)) -end - -struct StaticCompiledFunction{rt, tt} - f::Symbol - ptr::Ptr{Nothing} -end - -function Base.show(io::IO, f::StaticCompiledFunction{rt, tt}) where {rt, tt} - types = [tt.parameters...] - print(io, String(f.f), "(", join(("::$T" for T ∈ tt.parameters), ',') ,") :: $rt") -end - -function (f::StaticCompiledFunction{rt, tt})(args...) where {rt, tt} - Tuple{typeof.(args)...} == tt || error("Input types don't match compiled target $((tt.parameters...,)). Got arguments of type $(typeof.(args))") - out = RefValue{rt}() - refargs = Ref(args) - ccall(f.ptr, Nothing, (Ref{rt}, Ref{tt}), out, refargs) - out[] + (; f = instantiate(lf), path=abspath(path)) end -instantiate(f::StaticCompiledFunction) = f - """ ```julia @@ -208,31 +173,6 @@ function compile_executable(f, _tt=(), path::String="./", name=GPUCompiler.safe_ joinpath(abspath(path), filename) end - -module TestRuntime - # dummy methods - signal_exception() = return - # HACK: if malloc returns 0 or traps, all calling functions (like jl_box_*) - # get reduced to a trap, which really messes with our test suite. - malloc(sz) = Ptr{Cvoid}(Int(0xDEADBEEF)) - report_oom(sz) = return - report_exception(ex) = return - report_exception_name(ex) = return - report_exception_frame(idx, func, file, line) = return -end - -struct TestCompilerParams <: GPUCompiler.AbstractCompilerParams end -GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,TestCompilerParams}) = TestRuntime - - -function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) - source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) - target = GPUCompiler.NativeCompilerTarget(always_inline=true) - params = TestCompilerParams() - GPUCompiler.CompilerJob(target, source, params), kwargs -end - - """ ```julia generate_shlib(f, tt, path::String, name::String, filenamebase::String="obj"; kwargs...) @@ -271,23 +211,25 @@ julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 5.256496109495593 ``` """ -function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; kwargs...) +function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; + strip_llvm = false, + strip_asm = true, + kwargs...) mkpath(path) obj_path = joinpath(path, "$filenamebase.o") lib_path = joinpath(path, "$filenamebase.$(Libdl.dlext)") - open(obj_path, "w") do io - job, kwargs = native_job(f, tt; name, kwargs...) - obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) - write(io, obj) - flush(io) + job, kwargs = native_job(f, tt; name, kwargs...) + mod, meta = GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false) - # Pick a Clang - cc = Sys.isapple() ? `cc` : clang() - # Compile! - run(`$cc -shared -o $lib_path $obj_path`) + table = relocation_table!(mod) + + obj, _ = GPUCompiler.emit_asm(job, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) + + open(obj_path, "w") do io + write(io, obj) end - path, name + path, name, table end @@ -420,4 +362,14 @@ function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs return m end +function native_code_native(@nospecialize(f), @nospecialize(tt), name = GPUCompiler.safe_name(repr(f)); kwargs...) + job, kwargs = native_job(f, tt; name, kwargs...) + GPUCompiler.code_native(stdout, job; kwargs...) +end + + + + + + end # module diff --git a/src/code_loading.jl b/src/code_loading.jl new file mode 100644 index 00000000..b720548b --- /dev/null +++ b/src/code_loading.jl @@ -0,0 +1,77 @@ +""" + load_function(path) --> compiled_f + +load a `StaticCompiledFunction` from a given path. This object is callable. +""" +function load_function(path; filename="obj") + instantiate(deserialize(joinpath(path, "$filename.cjl"))) +end + +struct LazyStaticCompiledFunction{rt, tt} + f::Symbol + path::String + name::String + filename::String + reloc::IdDict{Any,String} +end + +function instantiate(p::LazyStaticCompiledFunction{rt, tt}) where {rt, tt} + # LLVM.load_library_permantly(dirname(Libdl.dlpath(Libdl.dlopen("libjulia")))) + lljit = LLVM.LLJIT(;tm=LLVM.JITTargetMachine()) + jd = LLVM.JITDylib(lljit) + flags = LLVM.API.LLVMJITSymbolFlags(LLVM.API.LLVMJITSymbolGenericFlagsExported, 0) + ofile = LLVM.MemoryBufferFile(joinpath(p.path, "$(p.filename).o")) #$(Libdl.dlext) + + + # Set all the uninitialized global variables to point to julia values from the relocation table + for (val, name) ∈ p.reloc + if !ismutable(val) + # Sometimes Julia embeds functions like `Base.string` into code, and this doesn't have a pointer + # so we need to give it one manually, and put the ref in the dict to make sure it doesn't expire. + delete!(p.reloc, val) + val = Ref(val) + p.reloc[val] = name + end + address = LLVM.API.LLVMOrcJITTargetAddress(reinterpret(UInt, pointer_from_objref(val))) + + symbol = LLVM.API.LLVMJITEvaluatedSymbol(address, flags) + gv = LLVM.API.LLVMJITCSymbolMapPair(LLVM.mangle(lljit, name), symbol) + mu = absolute_symbols(Ref(gv)) + LLVM.define(jd, mu) + end + # consider switching to one mu for all gvs instead of one per gv. + # I tried that already, but I got an error saying + # JIT session error: Symbols not found: [ __Type_Vector_Float64___274 ] + + # Link to libjulia + prefix = LLVM.get_prefix(lljit) + dg = LLVM.CreateDynamicLibrarySearchGeneratorForProcess(prefix) + LLVM.add!(jd, dg) + + LLVM.add!(lljit, jd, ofile) + fptr = pointer(LLVM.lookup(lljit, "julia_" * p.name)) + + StaticCompiledFunction{rt, tt}(p.f, fptr, lljit, p.reloc) +end + +struct StaticCompiledFunction{rt, tt} + f::Symbol + ptr::Ptr{Nothing} + jit::LLVM.LLJIT + reloc::IdDict{Any, String} +end + +function Base.show(io::IO, f::StaticCompiledFunction{rt, tt}) where {rt, tt} + types = [tt.parameters...] + print(io, String(f.f), "(", join(("::$T" for T ∈ tt.parameters), ',') ,") :: $rt") +end + +function (f::StaticCompiledFunction{rt, tt})(args...) where {rt, tt} + Tuple{typeof.(args)...} == tt || error("Input types don't match compiled target $((tt.parameters...,)). Got arguments of type $(typeof.(args))") + out = RefValue{rt}() + refargs = Ref(args) + ccall(f.ptr, Nothing, (Ptr{rt}, Ref{tt}), pointer_from_objref(out), refargs) + out[] +end + +instantiate(f::StaticCompiledFunction) = f diff --git a/src/pointer_patching.jl b/src/pointer_patching.jl new file mode 100644 index 00000000..c882f8e7 --- /dev/null +++ b/src/pointer_patching.jl @@ -0,0 +1,174 @@ +function relocation_table!(mod) + i64 = LLVM.IntType(64; ctx=LLVM.context(mod)) + jl_t = LLVM.PointerType(LLVM.StructType(LLVM.LLVMType[]; ctx=LLVM.context(mod))) + d = IdDict{Any, Tuple{String, LLVM.GlobalVariable}}() + + for func ∈ LLVM.functions(mod), bb ∈ LLVM.blocks(func), inst ∈ LLVM.instructions(bb) + if isa(inst, LLVM.LoadInst) && occursin("inttoptr", string(inst)) + get_pointers!(d, mod, inst) + elseif isa(inst, LLVM.StoreInst) && occursin("inttoptr", string(inst)) + @debug "Relocating StoreInst" inst + get_pointers!(d, mod, inst) + elseif inst isa LLVM.RetInst && occursin("inttoptr", string(inst)) + @debug "Relocating RetInst" inst LLVM.operands(inst) + get_pointers!(d, mod, inst) + elseif isa(inst, LLVM.BitCastInst) && occursin("inttoptr", string(inst)) + @debug "Relocating BitCastInst" inst LLVM.operands(inst) + get_pointers!(d, mod, inst) + elseif isa(inst, LLVM.CallInst) + @debug "Relocating CallInst" inst LLVM.operands(inst) + dest = LLVM.called_value(inst) + if occursin("inttoptr", string(dest)) && length(LLVM.operands(dest)) > 0 + @debug "Relocating CallInst inttoptr" dest LLVM.operands(dest) LLVM.operands(inst) + ptr_arg = first(LLVM.operands(dest)) + ptr_val = convert(Int, ptr_arg) + ptr = Ptr{Cvoid}(ptr_val) + + frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) + if length(frames) >= 1 + fn, file, line, linfo, fromC, inlined = last(frames) + fn = string(fn) + if ptr == cglobal(:jl_alloc_array_1d) + fn = "jl_alloc_array_1d" + end + if ptr == cglobal(:jl_alloc_array_2d) + fn = "jl_alloc_array_2d" + end + if ptr == cglobal(:jl_alloc_array_3d) + fn = "jl_alloc_array_3d" + end + if ptr == cglobal(:jl_new_array) + fn = "jl_new_array" + end + if ptr == cglobal(:jl_array_copy) + fn = "jl_array_copy" + end + if ptr == cglobal(:jl_alloc_string) + fn = "jl_alloc_string" + end + if ptr == cglobal(:jl_in_threaded_region) + fn = "jl_in_threaded_region" + end + if ptr == cglobal(:jl_enter_threaded_region) + fn = "jl_enter_threaded_region" + end + if ptr == cglobal(:jl_exit_threaded_region) + fn = "jl_exit_threaded_region" + end + if ptr == cglobal(:jl_set_task_tid) + fn = "jl_set_task_tid" + end + if ptr == cglobal(:jl_new_task) + fn = "jl_new_task" + end + if ptr == cglobal(:malloc) + fn = "malloc" + end + if ptr == cglobal(:memmove) + fn = "memmove" + end + if ptr == cglobal(:jl_array_grow_beg) + fn = "jl_array_grow_beg" + end + if ptr == cglobal(:jl_array_grow_end) + fn = "jl_array_grow_end" + end + if ptr == cglobal(:jl_array_grow_at) + fn = "jl_array_grow_at" + end + if ptr == cglobal(:jl_array_del_beg) + fn = "jl_array_del_beg" + end + if ptr == cglobal(:jl_array_del_end) + fn = "jl_array_del_end" + end + if ptr == cglobal(:jl_array_del_at) + fn = "jl_array_del_at" + end + if ptr == cglobal(:jl_array_ptr) + fn = "jl_array_ptr" + end + if ptr == cglobal(:jl_value_ptr) + fn = "jl_value_ptr" + end + if ptr == cglobal(:jl_get_ptls_states) + fn = "jl_get_ptls_states" + end + if ptr == cglobal(:jl_gc_add_finalizer_th) + fn = "jl_gc_add_finalizer_th" + end + if ptr == cglobal(:jl_symbol_n) + fn = "jl_symbol_n" + end + end + + if length(fn) > 1 && fromC + mod = LLVM.parent(LLVM.parent(LLVM.parent(inst))) + lfn = LLVM.API.LLVMGetNamedFunction(mod, fn) + + if lfn == C_NULL + lfn = LLVM.API.LLVMAddFunction(mod, fn, LLVM.API.LLVMGetCalledFunctionType(inst)) + else + lfn = LLVM.API.LLVMConstBitCast(lfn, LLVM.PointerType(LLVM.FunctionType(LLVM.API.LLVMGetCalledFunctionType(inst)))) + end + LLVM.API.LLVMSetOperand(inst, LLVM.API.LLVMGetNumOperands(inst)-1, lfn) + end + end + get_pointers!(d, mod, inst) + end + end + IdDict{Any, String}(val => name for (val, (name, _)) ∈ d) +end + +function get_pointers!(d, mod, inst) + jl_t = LLVM.PointerType(LLVM.StructType(LLVM.LLVMType[]; ctx=LLVM.context(mod))) + for (i, arg) ∈ enumerate(LLVM.operands(inst)) + if occursin("inttoptr", string(arg)) && arg isa LLVM.ConstantExpr + op1 = LLVM.Value(LLVM.API.LLVMGetOperand(arg, 0)) + ptr = Ptr{Cvoid}(convert(Int, op1)) + + frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) + if length(frames) >= 1 + fn, file, line, linfo, fromC, inlined = last(frames) + if isempty(String(fn)) || fn == :jl_system_image_data + val = unsafe_pointer_to_objref(ptr) + if val ∈ keys(d) + _, gv = d[val] + LLVM.API.LLVMSetOperand(inst, i-1, gv) + else + gv_name = GPUCompiler.safe_name(String(gensym(repr(Core.Typeof(val))))) + gv = LLVM.GlobalVariable(mod, jl_t, gv_name) + LLVM.extinit!(gv, true) + LLVM.API.LLVMSetOperand(inst, i-1, gv) + + d[val] = (gv_name, gv) + end + else + @warn "Found data we don't know how to relocate." frames + end + end + end + end +end + +function absolute_symbols(symbols) + ref = LLVM.API.LLVMOrcAbsoluteSymbols(symbols, length(symbols)) + LLVM.MaterializationUnit(ref) +end + +function pointer_patching_diff(mod::LLVM.Module, path1=tempname(), path2=tempname(); show_reloc_table=false) + s1 = string(mod) + write(path1, s1) + + d = StaticCompiler.relocation_table!(mod) + if show_reloc_table + @show d + end + + s2 = string(mod) + write(path2, s2) + + run(`diff $p1 $p2`) +end + + diff --git a/src/target.jl b/src/target.jl new file mode 100644 index 00000000..3b1c6239 --- /dev/null +++ b/src/target.jl @@ -0,0 +1,43 @@ +Base.@kwdef struct NativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget + cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) + features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) +end + +GPUCompiler.llvm_triple(::NativeCompilerTarget) = Sys.MACHINE + +function GPUCompiler.llvm_machine(target::NativeCompilerTarget) + triple = GPUCompiler.llvm_triple(target) + + t = LLVM.Target(triple=triple) + + tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) + GPUCompiler.asm_verbosity!(tm, true) + + return tm +end + +GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = "native_$(job.target.cpu)-$(hash(job.target.features))" + +module StaticRuntime + # dummy methods + signal_exception() = return + # HACK: if malloc returns 0 or traps, all calling functions (like jl_box_*) + # get reduced to a trap, which really messes with our test suite. + malloc(sz) = Ptr{Cvoid}(Int(0xDEADBEEF)) + report_oom(sz) = return + report_exception(ex) = return + report_exception_name(ex) = return + report_exception_frame(idx, func, file, line) = return +end + +struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams end + +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = StaticRuntime +GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = true + +function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) + source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) + target = NativeCompilerTarget() + params = StaticCompilerParams() + GPUCompiler.CompilerJob(target, source, params), kwargs +end diff --git a/test/Project.toml b/test/Project.toml index b0cf1dcc..b00d6497 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -7,4 +7,5 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" -Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" \ No newline at end of file +Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" +ErrorTypes = "7f495686-c6d2-4c77-9e8e-e4c865675f9d" \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 3201dbe6..74b240fe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,12 +6,14 @@ using LoopVectorization using ManualMemory using StrideArraysCore using Distributed +using ErrorTypes addprocs(1) -@everywhere using StaticCompiler +@everywhere using StaticCompiler, ErrorTypes remote_load_call(path, args...) = fetch(@spawnat 2 load_function(path)(args...)) + @testset "Basics" begin simple_sum(x) = x + one(typeof(x)) @@ -107,10 +109,63 @@ end _, path = compile(array_sum, (Int, Vector{T})) @test remote_load_call(path, 10, T.(1:10)) == T(55) end +end + +@testset "Array allocations" begin + function f(N) + v = Vector{Float64}(undef, N) + for i ∈ eachindex(v) + v[i] = i*i + end + v + end + _, path = compile(f, (Int,)) + @test remote_load_call(path, 5) == [1.0, 4.0, 9.0, 16.0, 25.0] +end + +# This is also a good test of loading and storing from the same object +@testset "Load & Store Same object" begin + global const x = Ref(0) + counter() = x[] += 1 + _, path = compile(counter, ()) + @spawnat 2 global counter = load_function(path) + @test fetch(@spawnat 2 counter()) == 1 + @test fetch(@spawnat 2 counter()) == 2 +end + +# This is also a good test of loading and storing from the same object +counter = let x = Ref(0) + () -> x[] += 1 +end +@testset "Closures" begin + #this currently segfaults during compilation + @test_skip begin + _, path = compile(counter, ()) + @spawnat 2 global counter_comp = load_function(path) + @test fetch(@spawnat 2 counter_comp()) == 1 + @test fetch(@spawnat 2 counter_comp()) == 2 + end +end + +@testset "Error handling" begin + # Doesn't work yet. Probably need the slow ABI :( + @test_skip begin + _, sqrt_path = compile(sqrt, (Int,)) + @test_throws DomainError remote_load_call(sqrt_path, -1) + end +end + - # @test (10, Int.(1:10)) == 55 - # @test compile(array_sum, (Int, Vector{Complex{Float32}}))[1](10, Complex{Float32}.(1:10)) == 55f0 + 0f0im - # @test compile(array_sum, (Int, Vector{Complex{Float64}}))[1](10, Complex{Float64}.(1:10)) == 55f0 + 0f0im +@testset "ErrorTypes handling" begin + function try_sqrt(x) :: Result{Float64, Nothing} + if x >= 0.0 + Ok(sqrt(x)) + else + Err(nothing) + end + end + _, sqrt_path = compile(try_sqrt, (Int,)) + @test remote_load_call(sqrt_path, -1) == none(Float64) end @@ -132,12 +187,25 @@ end BLAS.dot(N, a, 1, a, 1) end a = [1.0, 2.0] - mydot_compiled, path = compile(mydot, (Vector{Float64},)) - @test_skip remote_load_call(path, a) == 5.0 # this needs a relocatable pointer to work - @test mydot_compiled(a) == 5.0 + + # This used to work within a session, but now that I'm doing pointer relocation a bit better, + # it's chocking on the `BLAS` pointer call. I'm not sure yet how to relocate this properly. + @test_skip begin + mydot_compiled, path = compile(mydot, (Vector{Float64},)) + @test remote_load_call(path, a) == 5.0 + @test mydot_compiled(a) == 5.0 + end end +@testset "Strings" begin + function hello(name) + "Hello, " * name * "!" + end + hello_compiled, path = compile(hello, (String,)) + @test remote_load_call(path, "world") == "Hello, world!" +end + @testset "Hello World" begin function hello(N) println("Hello World $N") @@ -171,6 +239,8 @@ end @test C ≈ A*B end + + # This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). # This lets us have intermediate, mutable stack allocated arrays inside our @testset "Alloca" begin @@ -236,4 +306,6 @@ end @test r.exitcode == 0 end end + + # data structures, dictionaries, tuples, named tuples From f85891a3081d849a1915562d4642250ae37a617b Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sat, 12 Feb 2022 09:56:42 -0700 Subject: [PATCH 052/202] Fixup; get errors working (#61) * tell llvm the correct types of objects; verify the IR after after modification * Update src/code_loading.jl Co-authored-by: Valentin Churavy * use julia's optimizer, enable tests for error handling * make ManualMemory and StrideArraysCore available on other process * run optimization twice so that reloc can happen on optimized code * grab `jl_add_optimization_passes` from `libjulia-codegen` on newer versions * don't test on windows for now, it's pointless * remove unnecessary func * add some comments * reorganize * catch an error pathway Co-authored-by: Valentin Churavy --- .github/workflows/ci.yml | 2 - README.md | 1 - src/StaticCompiler.jl | 153 +++++++++++++++++++++++---------------- src/code_loading.jl | 24 ++++-- src/pointer_patching.jl | 26 ++++--- src/target.jl | 11 ++- test/Project.toml | 3 +- test/runtests.jl | 66 +++++++---------- 8 files changed, 158 insertions(+), 128 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0ea99ae6..03f84766 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,6 @@ jobs: test: name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} runs-on: ${{ matrix.os }} - continue-on-error: ${{ matrix.os == 'windows-latest' }} strategy: fail-fast: false matrix: @@ -16,7 +15,6 @@ jobs: os: - ubuntu-latest - macOS-latest - - windows-latest arch: - x64 steps: diff --git a/README.md b/README.md index 0c9d6d45..56e03689 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,5 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi * 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 varaibles do *not* work with `compile_executable` (yet). * Type unstable code is not yet supported. -* Throwing errors is not currently supported. In the meantime, consider wrapping possible errors with [ErrorTypes.jl](https://github.com/jakobnissen/ErrorTypes.jl) * Doesn't currently work on Windows. * If you find any other limitations, let us know. There's probably lots. \ No newline at end of file diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 611bc92b..49975ad7 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -1,8 +1,10 @@ module StaticCompiler using GPUCompiler: GPUCompiler -using LLVM: LLVM -using Libdl: Libdl +using LLVM +using LLVM.Interop +using LLVM: API +using Libdl: Libdl, dlsym, dlopen using Base: RefValue using Serialization: serialize, deserialize using Clang_jll: clang @@ -82,6 +84,7 @@ single method (the method determined by `types`). function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_name(repr(f)), filename="obj", strip_llvm = false, strip_asm = true, + opt_level=3, kwargs...) tt = Base.to_tuple_type(_tt) isconcretetype(tt) || error("input type signature $_tt is not concrete") @@ -90,7 +93,7 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - _, _, table = generate_shlib(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; strip_llvm, strip_asm, filename, kwargs...) + _, _, table = generate_shlib(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; opt_level, strip_llvm, strip_asm, filename, kwargs...) lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name, filename, table) cjl_path = joinpath(path, "$filename.cjl") @@ -99,6 +102,92 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam (; f = instantiate(lf), path=abspath(path)) end +""" +```julia +generate_shlib(f, tt, path::String, name::String, filenamebase::String="obj"; kwargs...) +``` +Low level interface for compiling a shared object / dynamically loaded library + (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing +the types of the arguments for which the function will be compiled. + +See also `StaticCompiler.generate_shlib_fptr`. + +### Examples +```julia +julia> function test(n) + r = 0.0 + for i=1:n + r += log(sqrt(i)) + end + return r/n + end +test (generic function with 1 method) + +julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, "./test") +("./test", "test") + +shell> tree \$path +./test +|-- obj.o +`-- obj.so + +0 directories, 2 files + +julia> test(100_000) +5.256496109495593 + +julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 100_000) +5.256496109495593 +``` +""" +function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; + strip_llvm = false, + strip_asm = true, + opt_level=3, + kwargs...) + mkpath(path) + obj_path = joinpath(path, "$filenamebase.o") + lib_path = joinpath(path, "$filenamebase.$(Libdl.dlext)") + + job, kwargs = native_job(f, tt; name, kwargs...) + #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. + mod, meta = GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false) + #use Julia's optimization pass on the LLVM code, but leave intrinsics alone + julia_opt_passes(mod, job; opt_level, lower_intrinsics=0) + # Scoop up all the pointers in the optimized module, and replace them with unitialized global variables. + # table is a dictionary where the keys are julia objects that are needed by the function, and the values + # of the dictionary are the names of their associated LLVM GlobalVariable names. + table = relocation_table!(mod) + # Now that we've removed all the pointers from the code, we can (hopefully) safely lower all the instrinsics + julia_opt_passes(mod, job; opt_level, lower_intrinsics=1) + # Make sure we didn't make any glaring errors + LLVM.verify(mod) + # Compile the LLVM module to native code and save it to disk + obj, _ = GPUCompiler.emit_asm(job, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) + open(obj_path, "w") do io + write(io, obj) + end + path, name, table +end + +function julia_opt_passes(mod, job; opt_level, lower_intrinsics) + triple = GPUCompiler.llvm_triple(job.target) + tm = GPUCompiler.llvm_machine(job.target) + + lib_path = VERSION > v"1.8.0-DEV" ? "libjulia-codegen" : "libjulia" + + dlopen(lib_path) do lib + opt_func = dlsym(lib, "jl_add_optimization_passes") + ModulePassManager() do pm + add_library_info!(pm, triple) + add_transform_info!(pm, tm) + ccall(opt_func, Cvoid, + (LLVM.API.LLVMPassManagerRef, Cint, Cint), + pm, opt_level, lower_intrinsics) + run!(pm, mod) + end + end +end """ ```julia @@ -173,64 +262,6 @@ function compile_executable(f, _tt=(), path::String="./", name=GPUCompiler.safe_ joinpath(abspath(path), filename) end -""" -```julia -generate_shlib(f, tt, path::String, name::String, filenamebase::String="obj"; kwargs...) -``` -Low level interface for compiling a shared object / dynamically loaded library - (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing -the types of the arguments for which the function will be compiled. - -See also `StaticCompiler.generate_shlib_fptr`. - -### Examples -```julia -julia> function test(n) - r = 0.0 - for i=1:n - r += log(sqrt(i)) - end - return r/n - end -test (generic function with 1 method) - -julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, "./test") -("./test", "test") - -shell> tree \$path -./test -|-- obj.o -`-- obj.so - -0 directories, 2 files - -julia> test(100_000) -5.256496109495593 - -julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 100_000) -5.256496109495593 -``` -""" -function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; - strip_llvm = false, - strip_asm = true, - kwargs...) - mkpath(path) - obj_path = joinpath(path, "$filenamebase.o") - lib_path = joinpath(path, "$filenamebase.$(Libdl.dlext)") - - job, kwargs = native_job(f, tt; name, kwargs...) - mod, meta = GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false) - - table = relocation_table!(mod) - - obj, _ = GPUCompiler.emit_asm(job, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) - - open(obj_path, "w") do io - write(io, obj) - end - path, name, table -end function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; temp::Bool=true, kwargs...) diff --git a/src/code_loading.jl b/src/code_loading.jl index b720548b..931e6a55 100644 --- a/src/code_loading.jl +++ b/src/code_loading.jl @@ -15,6 +15,15 @@ struct LazyStaticCompiledFunction{rt, tt} reloc::IdDict{Any,String} end +""" + unsafe_pointer_from_objref(x) + +Sometimes Julia embeds immutables like `Base.string` into code, and julia +will error if you call `pointer_from_objref(string)`, claiming that it +doesn't have a pointer even though that's a lie. +""" +unsafe_pointer_from_objref(x) = ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), x) + function instantiate(p::LazyStaticCompiledFunction{rt, tt}) where {rt, tt} # LLVM.load_library_permantly(dirname(Libdl.dlpath(Libdl.dlopen("libjulia")))) lljit = LLVM.LLJIT(;tm=LLVM.JITTargetMachine()) @@ -25,14 +34,7 @@ function instantiate(p::LazyStaticCompiledFunction{rt, tt}) where {rt, tt} # Set all the uninitialized global variables to point to julia values from the relocation table for (val, name) ∈ p.reloc - if !ismutable(val) - # Sometimes Julia embeds functions like `Base.string` into code, and this doesn't have a pointer - # so we need to give it one manually, and put the ref in the dict to make sure it doesn't expire. - delete!(p.reloc, val) - val = Ref(val) - p.reloc[val] = name - end - address = LLVM.API.LLVMOrcJITTargetAddress(reinterpret(UInt, pointer_from_objref(val))) + address = LLVM.API.LLVMOrcJITTargetAddress(reinterpret(UInt, unsafe_pointer_from_objref(val))) symbol = LLVM.API.LLVMJITEvaluatedSymbol(address, flags) gv = LLVM.API.LLVMJITCSymbolMapPair(LLVM.mangle(lljit, name), symbol) @@ -54,6 +56,12 @@ function instantiate(p::LazyStaticCompiledFunction{rt, tt}) where {rt, tt} StaticCompiledFunction{rt, tt}(p.f, fptr, lljit, p.reloc) end +function absolute_symbols(symbols) + ref = LLVM.API.LLVMOrcAbsoluteSymbols(symbols, length(symbols)) + LLVM.MaterializationUnit(ref) +end + + struct StaticCompiledFunction{rt, tt} f::Symbol ptr::Ptr{Nothing} diff --git a/src/pointer_patching.jl b/src/pointer_patching.jl index c882f8e7..18be73ec 100644 --- a/src/pointer_patching.jl +++ b/src/pointer_patching.jl @@ -1,6 +1,5 @@ function relocation_table!(mod) i64 = LLVM.IntType(64; ctx=LLVM.context(mod)) - jl_t = LLVM.PointerType(LLVM.StructType(LLVM.LLVMType[]; ctx=LLVM.context(mod))) d = IdDict{Any, Tuple{String, LLVM.GlobalVariable}}() for func ∈ LLVM.functions(mod), bb ∈ LLVM.blocks(func), inst ∈ LLVM.instructions(bb) @@ -121,23 +120,26 @@ function relocation_table!(mod) end function get_pointers!(d, mod, inst) - jl_t = LLVM.PointerType(LLVM.StructType(LLVM.LLVMType[]; ctx=LLVM.context(mod))) + jl_t = (LLVM.StructType(LLVM.LLVMType[]; ctx=LLVM.context(mod))) for (i, arg) ∈ enumerate(LLVM.operands(inst)) if occursin("inttoptr", string(arg)) && arg isa LLVM.ConstantExpr op1 = LLVM.Value(LLVM.API.LLVMGetOperand(arg, 0)) + if op1 isa LLVM.ConstantExpr + op1 = LLVM.Value(LLVM.API.LLVMGetOperand(op1, 0)) + end ptr = Ptr{Cvoid}(convert(Int, op1)) - frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) if length(frames) >= 1 fn, file, line, linfo, fromC, inlined = last(frames) - if isempty(String(fn)) || fn == :jl_system_image_data - val = unsafe_pointer_to_objref(ptr) + if (isempty(String(fn)) && isempty(String(file))) || fn == :jl_system_image_data + val = unsafe_pointer_to_objref(ptr) if val ∈ keys(d) _, gv = d[val] LLVM.API.LLVMSetOperand(inst, i-1, gv) else gv_name = GPUCompiler.safe_name(String(gensym(repr(Core.Typeof(val))))) - gv = LLVM.GlobalVariable(mod, jl_t, gv_name) + gv = LLVM.GlobalVariable(mod, llvmeltype(arg), gv_name, LLVM.addrspace(llvmtype(arg))) + LLVM.extinit!(gv, true) LLVM.API.LLVMSetOperand(inst, i-1, gv) @@ -151,10 +153,7 @@ function get_pointers!(d, mod, inst) end end -function absolute_symbols(symbols) - ref = LLVM.API.LLVMOrcAbsoluteSymbols(symbols, length(symbols)) - LLVM.MaterializationUnit(ref) -end +llvmeltype(x::LLVM.Value) = eltype(LLVM.llvmtype(x)) function pointer_patching_diff(mod::LLVM.Module, path1=tempname(), path2=tempname(); show_reloc_table=false) s1 = string(mod) @@ -168,7 +167,12 @@ function pointer_patching_diff(mod::LLVM.Module, path1=tempname(), path2=tempnam s2 = string(mod) write(path2, s2) - run(`diff $p1 $p2`) + try + # this always ends in an error for me for some reason + run(`diff $path1 $path2`) + catch e; + nothing + end end diff --git a/src/target.jl b/src/target.jl index 3b1c6239..2efb191c 100644 --- a/src/target.jl +++ b/src/target.jl @@ -19,11 +19,9 @@ end GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = "native_$(job.target.cpu)-$(hash(job.target.features))" module StaticRuntime - # dummy methods + # the runtime library signal_exception() = return - # HACK: if malloc returns 0 or traps, all calling functions (like jl_box_*) - # get reduced to a trap, which really messes with our test suite. - malloc(sz) = Ptr{Cvoid}(Int(0xDEADBEEF)) + malloc(sz) = ccall("extern malloc", llvmcall, Csize_t, (Csize_t,), sz) report_oom(sz) = return report_exception(ex) = return report_exception_name(ex) = return @@ -33,6 +31,11 @@ end struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams end GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = StaticRuntime +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{NativeCompilerTarget}) = StaticRuntime +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams}) = StaticRuntime + +GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = true +GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams}) = true GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = true function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) diff --git a/test/Project.toml b/test/Project.toml index b00d6497..b0cf1dcc 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -7,5 +7,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" -Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" -ErrorTypes = "7f495686-c6d2-4c77-9e8e-e4c865675f9d" \ No newline at end of file +Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 74b240fe..3bd9f0cd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,16 +4,13 @@ using Libdl using LinearAlgebra using LoopVectorization using ManualMemory -using StrideArraysCore using Distributed -using ErrorTypes - +using StrideArraysCore addprocs(1) -@everywhere using StaticCompiler, ErrorTypes +@everywhere using StaticCompiler, StrideArraysCore remote_load_call(path, args...) = fetch(@spawnat 2 load_function(path)(args...)) - @testset "Basics" begin simple_sum(x) = x + one(typeof(x)) @@ -147,28 +144,19 @@ end end end -@testset "Error handling" begin - # Doesn't work yet. Probably need the slow ABI :( - @test_skip begin - _, sqrt_path = compile(sqrt, (Int,)) - @test_throws DomainError remote_load_call(sqrt_path, -1) - end -end - -@testset "ErrorTypes handling" begin - function try_sqrt(x) :: Result{Float64, Nothing} - if x >= 0.0 - Ok(sqrt(x)) - else - Err(nothing) +@testset "Error handling" begin + _, path = compile(sqrt, (Int,)) + tsk = @spawnat 2 begin + try + load_function(path)(-1) + catch e; + e end end - _, sqrt_path = compile(try_sqrt, (Int,)) - @test remote_load_call(sqrt_path, -1) == none(Float64) + @test fetch(tsk) isa DomainError end - # Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. # We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function. # The interface made in `compile` should handle this fine. @@ -216,6 +204,23 @@ end @test_skip ccall(generate_shlib_fptr(hello, (Int,)), Int, (Int,), 1) == 1 end +# This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). +# This lets us have intermediate, mutable stack allocated arrays inside our +@testset "Alloca" begin + function f(N) + # this can hold at most 100 Int values, if you use it for more, you'll segfault + buf = ManualMemory.MemoryBuffer{100, Int}(undef) + GC.@preserve buf begin + # wrap the first N values in a PtrArray + arr = PtrArray(pointer(buf), (N,)) + arr .= 1 # mutate the array to be all 1s + sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body + end + end + _, path = compile(f, (Int,)) + @test remote_load_call(path, 20) == 20 +end + # I can't beleive this works. @testset "LoopVectorization" begin function mul!(C, A, B) @@ -241,23 +246,6 @@ end -# This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). -# This lets us have intermediate, mutable stack allocated arrays inside our -@testset "Alloca" begin - function f(N) - # this can hold at most 100 Int values, if you use it for more, you'll segfault - buf = ManualMemory.MemoryBuffer{100, Int}(undef) - GC.@preserve buf begin - # wrap the first N values in a PtrArray - arr = PtrArray(pointer(buf), (N,)) - arr .= 1 # mutate the array to be all 1s - sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body - end - end - _, path = compile(f, (Int,)) - @test remote_load_call(path, 20) == 20 -end - @testset "Standalone Executables" begin # Minimal test with no `llvmcall` @inline function foo() From 998feecc1c925b4f279f6eb69c8c305ba001c197 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Mon, 14 Feb 2022 16:20:34 -0700 Subject: [PATCH 053/202] Use Enzyme's optimization and late lowering mechanisms (#63) * Use Enzyme's optimization and late lowering mechanisms * BLAS calls work again * update README * add another test case * re-enable the `@test_skip` on remotely calling BLAS --- README.md | 10 +- src/StaticCompiler.jl | 38 ++--- src/optimize.jl | 326 ++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 22 +-- 4 files changed, 359 insertions(+), 37 deletions(-) create mode 100644 src/optimize.jl diff --git a/README.md b/README.md index 56e03689..48509ee8 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,9 @@ # StaticCompiler -[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://tshort.github.io/StaticCompiler.jl/dev) -[![Build Status](https://travis-ci.com/tshort/StaticCompiler.jl.svg?branch=master)](https://travis-ci.com/tshort/StaticCompiler.jl) -[![Build Status](https://ci.appveyor.com/api/projects/status/github/tshort/StaticCompiler.jl?svg=true)](https://ci.appveyor.com/project/tshort/StaticCompiler-jl) +[![CI](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml) [![Codecov](https://codecov.io/gh/tshort/StaticCompiler.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/tshort/StaticCompiler.jl) -[![Coveralls](https://coveralls.io/repos/github/tshort/StaticCompiler.jl/badge.svg?branch=master)](https://coveralls.io/github/tshort/StaticCompiler.jl?branch=master) -This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. It is also meant for cross compilation, so Julia code can be compiled for other targets, including WebAssembly and embedded targets. +This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. ## Installation and Usage @@ -41,6 +38,9 @@ julia> fib_compiled(10) 55 ``` +See the file `tests/runtests.jl` for some examples of functions that work with static compilation (and some that don't, +marked with `@test_skip`) + ## Approach This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 49975ad7..686e6b67 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -15,6 +15,8 @@ export native_code_llvm, native_code_typed, native_llvm_module, native_code_nati include("target.jl") include("pointer_patching.jl") include("code_loading.jl") +include("optimize.jl") + """ compile(f, types, path::String = tempname()) --> (compiled_f, path) @@ -148,20 +150,27 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf mkpath(path) obj_path = joinpath(path, "$filenamebase.o") lib_path = joinpath(path, "$filenamebase.$(Libdl.dlext)") - + tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) job, kwargs = native_job(f, tt; name, kwargs...) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false) - #use Julia's optimization pass on the LLVM code, but leave intrinsics alone - julia_opt_passes(mod, job; opt_level, lower_intrinsics=0) + + # Use Enzyme's annotation and optimization pipeline + annotate!(mod) + optimize!(mod, tm) + # Scoop up all the pointers in the optimized module, and replace them with unitialized global variables. - # table is a dictionary where the keys are julia objects that are needed by the function, and the values + # `table` is a dictionary where the keys are julia objects that are needed by the function, and the values # of the dictionary are the names of their associated LLVM GlobalVariable names. table = relocation_table!(mod) + # Now that we've removed all the pointers from the code, we can (hopefully) safely lower all the instrinsics - julia_opt_passes(mod, job; opt_level, lower_intrinsics=1) + # (again, using Enzyme's pipeline) + post_optimize!(mod, tm) + # Make sure we didn't make any glaring errors LLVM.verify(mod) + # Compile the LLVM module to native code and save it to disk obj, _ = GPUCompiler.emit_asm(job, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io @@ -170,25 +179,6 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf path, name, table end -function julia_opt_passes(mod, job; opt_level, lower_intrinsics) - triple = GPUCompiler.llvm_triple(job.target) - tm = GPUCompiler.llvm_machine(job.target) - - lib_path = VERSION > v"1.8.0-DEV" ? "libjulia-codegen" : "libjulia" - - dlopen(lib_path) do lib - opt_func = dlsym(lib, "jl_add_optimization_passes") - ModulePassManager() do pm - add_library_info!(pm, triple) - add_transform_info!(pm, tm) - ccall(opt_func, Cvoid, - (LLVM.API.LLVMPassManagerRef, Cint, Cint), - pm, opt_level, lower_intrinsics) - run!(pm, mod) - end - end -end - """ ```julia compile_executable(f, types::Tuple, path::String, name::String=repr(f); filename::String=name, kwargs...) diff --git a/src/optimize.jl b/src/optimize.jl new file mode 100644 index 00000000..f2934a1d --- /dev/null +++ b/src/optimize.jl @@ -0,0 +1,326 @@ +# stolen from https://github.com/EnzymeAD/Enzyme.jl/blob/1b187cc16953727cab26b64bc6a6dcf106c29a57/src/compiler/optimize.jl#L213 + +function optimize!(mod::LLVM.Module, tm) + # everying except unroll, slpvec, loop-vec + # then finish Julia GC + ModulePassManager() do pm + add_library_info!(pm, triple(mod)) + add_transform_info!(pm, tm) + + propagate_julia_addrsp!(pm) + scoped_no_alias_aa!(pm) + type_based_alias_analysis!(pm) + basic_alias_analysis!(pm) + cfgsimplification!(pm) + dce!(pm) +@static if isdefined(GPUCompiler, :cpu_features!) + GPUCompiler.cpu_features!(pm) +end + scalar_repl_aggregates_ssa!(pm) # SSA variant? + mem_cpy_opt!(pm) + always_inliner!(pm) + alloc_opt!(pm) + instruction_combining!(pm) + cfgsimplification!(pm) + scalar_repl_aggregates_ssa!(pm) # SSA variant? + instruction_combining!(pm) + jump_threading!(pm) + correlated_value_propagation!(pm) + instruction_combining!(pm) + reassociate!(pm) + early_cse!(pm) + alloc_opt!(pm) + loop_idiom!(pm) + loop_rotate!(pm) + lower_simdloop!(pm) + licm!(pm) + loop_unswitch!(pm) + instruction_combining!(pm) + ind_var_simplify!(pm) + loop_deletion!(pm) + loop_unroll!(pm) + alloc_opt!(pm) + scalar_repl_aggregates_ssa!(pm) # SSA variant? + gvn!(pm) + # This InstCombine needs to be after GVN + # Otherwise it will generate load chains in GPU code... + instruction_combining!(pm) + mem_cpy_opt!(pm) + sccp!(pm) + instruction_combining!(pm) + jump_threading!(pm) + dead_store_elimination!(pm) + alloc_opt!(pm) + cfgsimplification!(pm) + loop_idiom!(pm) + loop_deletion!(pm) + jump_threading!(pm) + correlated_value_propagation!(pm) + # SLP_Vectorizer -- not for Enzyme + aggressive_dce!(pm) + instruction_combining!(pm) + # Loop Vectorize -- not for Enzyme + # InstCombine + + # GC passes + barrier_noop!(pm) + gc_invariant_verifier!(pm, false) + + # FIXME: Currently crashes printing + cfgsimplification!(pm) + instruction_combining!(pm) # Extra for Enzyme + #API.EnzymeAddAttributorLegacyPass(pm) + run!(pm, mod) + end + # @show "omod", mod + # flush(stdout) + # flush(stderr) +end + +# https://github.com/JuliaLang/julia/blob/2eb5da0e25756c33d1845348836a0a92984861ac/src/aotcompile.cpp#L603 +function addTargetPasses!(pm, tm) + add_library_info!(pm, LLVM.triple(tm)) + add_transform_info!(pm, tm) +end + +# https://github.com/JuliaLang/julia/blob/2eb5da0e25756c33d1845348836a0a92984861ac/src/aotcompile.cpp#L620 +function addOptimizationPasses!(pm) + constant_merge!(pm) + + propagate_julia_addrsp!(pm) + scoped_no_alias_aa!(pm) + type_based_alias_analysis!(pm) + basic_alias_analysis!(pm) + cfgsimplification!(pm) + dce!(pm) + scalar_repl_aggregates!(pm) + + # mem_cpy_opt!(pm) + + always_inliner!(pm) # Respect always_inline + + # Running `memcpyopt` between this and `sroa` seems to give `sroa` a hard time + # merging the `alloca` for the unboxed data and the `alloca` created by the `alloc_opt` + # pass. + + alloc_opt!(pm) + # consider AggressiveInstCombinePass at optlevel > 2 + + instruction_combining!(pm) + cfgsimplification!(pm) + scalar_repl_aggregates!(pm) + instruction_combining!(pm) # TODO: createInstSimplifyLegacy + jump_threading!(pm) + correlated_value_propagation!(pm) + + reassociate!(pm) + + early_cse!(pm) + + # Load forwarding above can expose allocations that aren't actually used + # remove those before optimizing loops. + alloc_opt!(pm) + loop_rotate!(pm) + # moving IndVarSimplify here prevented removing the loop in perf_sumcartesian(10:-1:1) + loop_idiom!(pm) + + # LoopRotate strips metadata from terminator, so run LowerSIMD afterwards + lower_simdloop!(pm) # Annotate loop marked with "loopinfo" as LLVM parallel loop + licm!(pm) + julia_licm!(pm) + # Subsequent passes not stripping metadata from terminator + instruction_combining!(pm) # TODO: createInstSimplifyLegacy + ind_var_simplify!(pm) + loop_deletion!(pm) + loop_unroll!(pm) # TODO: in Julia createSimpleLoopUnroll + + # Run our own SROA on heap objects before LLVM's + alloc_opt!(pm) + # Re-run SROA after loop-unrolling (useful for small loops that operate, + # over the structure of an aggregate) + scalar_repl_aggregates!(pm) + instruction_combining!(pm) # TODO: createInstSimplifyLegacy + + gvn!(pm) + mem_cpy_opt!(pm) + sccp!(pm) + + # Run instcombine after redundancy elimination to exploit opportunities + # opened up by them. + # This needs to be InstCombine instead of InstSimplify to allow + # loops over Union-typed arrays to vectorize. + instruction_combining!(pm) + jump_threading!(pm) + dead_store_elimination!(pm) + + # More dead allocation (store) deletion before loop optimization + # consider removing this: + alloc_opt!(pm) + + # see if all of the constant folding has exposed more loops + # to simplification and deletion + # this helps significantly with cleaning up iteration + cfgsimplification!(pm) + loop_deletion!(pm) + instruction_combining!(pm) + loop_vectorize!(pm) + # TODO: createLoopLoadEliminationPass + cfgsimplification!(pm) + slpvectorize!(pm) + # might need this after LLVM 11: + # TODO: createVectorCombinePass() + + aggressive_dce!(pm) +end + +function addMachinePasses!(pm) + combine_mul_add!(pm) + # TODO: createDivRemPairs[] + + demote_float16!(pm) + gvn!(pm) +end + +function addJuliaLegalizationPasses!(pm, lower_intrinsics=true) + if lower_intrinsics + # LowerPTLS removes an indirect call. As a result, it is likely to trigger + # LLVM's devirtualization heuristics, which would result in the entire + # pass pipeline being re-exectuted. Prevent this by inserting a barrier. + barrier_noop!(pm) + lower_exc_handlers!(pm) + gc_invariant_verifier!(pm, false) + + # Needed **before** LateLowerGCFrame on LLVM < 12 + # due to bug in `CreateAlignmentAssumption`. + remove_ni!(pm) + late_lower_gc_frame!(pm) + final_lower_gc!(pm) + # We need these two passes and the instcombine below + # after GC lowering to let LLVM do some constant propagation on the tags. + # and remove some unnecessary write barrier checks. + gvn!(pm) + sccp!(pm) + # Remove dead use of ptls + dce!(pm) + lower_ptls!(pm, #=dump_native=# false) + instruction_combining!(pm) + # Clean up write barrier and ptls lowering + cfgsimplification!(pm) + else + barrier_noop!(pm) + remove_ni!(pm) + end +end + +function post_optimize!(mod, tm) + # @show "pre_post", mod + # flush(stdout) + # flush(stderr) + LLVM.ModulePassManager() do pm + addTargetPasses!(pm, tm) + addOptimizationPasses!(pm) + run!(pm, mod) + end + LLVM.ModulePassManager() do pm + addJuliaLegalizationPasses!(pm, true) + addMachinePasses!(pm) + run!(pm, mod) + end + # @show "post_mod", mod + # flush(stdout) + # flush(stderr) +end + + + + +const inactivefns = Set{String}(( + "jl_gc_queue_root", "gpu_report_exception", "gpu_signal_exception", + "julia.ptls_states", "julia.write_barrier", "julia.typeof", "jl_box_int64", "jl_box_int32", + "jl_subtype", "julia.get_pgcstack", "jl_in_threaded_region", "jl_object_id_", "jl_object_id", + "jl_breakpoint", + "llvm.julia.gc_preserve_begin","llvm.julia.gc_preserve_end", "jl_get_ptls_states", + "jl_f_fieldtype", + "jl_symbol_n", + # BIG TODO + "jl_gc_add_finalizer_th", + # "jl_" +)) + +const activefns = Set{String}(( + "jl_", +)) + +function annotate!(mod) + ctx = context(mod) + inactive = LLVM.StringAttribute("enzyme_inactive", ""; ctx) + active = LLVM.StringAttribute("enzyme_active", ""; ctx) + fns = functions(mod) + + for inactivefn in inactivefns + if haskey(fns, inactivefn) + fn = fns[inactivefn] + push!(function_attributes(fn), inactive) + end + end + + for activefn in activefns + if haskey(fns, activefn) + fn = fns[activefn] + push!(function_attributes(fn), active) + end + end + + for fname in ("julia.typeof",) + if haskey(fns, fname) + fn = fns[fname] + push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0; ctx)) + push!(function_attributes(fn), LLVM.StringAttribute("enzyme_shouldrecompute"; ctx)) + end + end + + for fname in ("julia.get_pgcstack", "julia.ptls_states", "jl_get_ptls_states") + if haskey(fns, fname) + fn = fns[fname] + # TODO per discussion w keno perhaps this should change to readonly / inaccessiblememonly + push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0; ctx)) + end + end + + for fname in ("julia.pointer_from_objref",) + if haskey(fns, fname) + fn = fns[fname] + push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0; ctx)) + end + end + + for boxfn in ("jl_box_float32", "jl_box_float64", "jl_box_int32", "jl_box_int64", "julia.gc_alloc_obj", "jl_alloc_array_1d", "jl_alloc_array_2d", "jl_alloc_array_3d") + if haskey(fns, boxfn) + fn = fns[boxfn] + push!(return_attributes(fn), LLVM.EnumAttribute("noalias", 0; ctx)) + push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0; ctx)) + end + end + + for gc in ("llvm.julia.gc_preserve_begin", "llvm.julia.gc_preserve_end") + if haskey(fns, gc) + fn = fns[gc] + push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0; ctx)) + end + end + + for rfn in ("jl_object_id_", "jl_object_id") + if haskey(fns, rfn) + fn = fns[rfn] + push!(function_attributes(fn), LLVM.EnumAttribute("readonly", 0; ctx)) + end + end + + for rfn in ("jl_in_threaded_region_", "jl_in_threaded_region") + if haskey(fns, rfn) + fn = fns[rfn] + push!(function_attributes(fn), LLVM.EnumAttribute("readonly", 0; ctx)) + push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0; ctx)) + end + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 3bd9f0cd..55f04884 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -176,12 +176,16 @@ end end a = [1.0, 2.0] - # This used to work within a session, but now that I'm doing pointer relocation a bit better, - # it's chocking on the `BLAS` pointer call. I'm not sure yet how to relocate this properly. + mydot_compiled, path = compile(mydot, (Vector{Float64},)) + # Works locally for me, but not on CI. Need some improvements to pointer relocation to be robust. + @test_skip remote_load_call(path, a) == 5.0 + @test mydot_compiled(a) ≈ 5.0 + + # This will need some more work apparently @test_skip begin - mydot_compiled, path = compile(mydot, (Vector{Float64},)) - @test remote_load_call(path, a) == 5.0 - @test mydot_compiled(a) == 5.0 + _, path = compile((*), (Matrix{Float64}, Matrix{Float64})) + A, B = rand(10, 11), rand(11, 12) + @test remote_load_call(path, A, B) ≈ A * B end end @@ -199,9 +203,11 @@ end println("Hello World $N") N end - # How do I test this? - # Also ... this segfaults - @test_skip ccall(generate_shlib_fptr(hello, (Int,)), Int, (Int,), 1) == 1 + # We'll need to be able to relocate a bunch of UV stuff for this, and deal with dynamic dispatch. + @test_skip begin + hello_compiled, path = compile(hello, (Int,)) + @test_skip remote_load_call(path, 1) == 1 + end end # This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). From 57e04251246815554b11431dd18f934b33aa4be9 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sat, 19 Feb 2022 15:11:59 -0500 Subject: [PATCH 054/202] Add `compile_dylib` (#64) * Clean up `generate_executable` * Delete unused line * Rename generate_shlib to generate_obj * Add generate_dylib * Test generate_dylib * Add and test `compile_dylib` * s/_dylib/_shlib/g to keep original terminology --- src/StaticCompiler.jl | 207 ++++++++++++++++++++++++++++-------------- test/runtests.jl | 32 ++++--- 2 files changed, 156 insertions(+), 83 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 686e6b67..da4b8046 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -9,7 +9,7 @@ using Base: RefValue using Serialization: serialize, deserialize using Clang_jll: clang -export compile, load_function, compile_executable +export compile, load_function, compile_shlib, compile_executable export native_code_llvm, native_code_typed, native_llvm_module, native_code_native include("target.jl") @@ -93,10 +93,10 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam rt = only(native_code_typed(f, tt))[2] isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") - + f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - _, _, table = generate_shlib(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; opt_level, strip_llvm, strip_asm, filename, kwargs...) - + _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; opt_level, strip_llvm, strip_asm, filename, kwargs...) + lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name, filename, table) cjl_path = joinpath(path, "$filename.cjl") serialize(cjl_path, lf) @@ -106,71 +106,59 @@ end """ ```julia -generate_shlib(f, tt, path::String, name::String, filenamebase::String="obj"; kwargs...) +generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; + \tstrip_llvm = false, + \tstrip_asm = true, + \topt_level=3, + \tkwargs...) ``` -Low level interface for compiling a shared object / dynamically loaded library - (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing -the types of the arguments for which the function will be compiled. - -See also `StaticCompiler.generate_shlib_fptr`. +Low level interface for compiling object code (`.o`) for for function `f` given +a tuple type `tt` characterizing the types of the arguments for which the +function will be compiled. ### Examples ```julia -julia> function test(n) - r = 0.0 - for i=1:n - r += log(sqrt(i)) - end - return r/n - end -test (generic function with 1 method) +julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) +fib (generic function with 1 method) -julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, "./test") -("./test", "test") +julia> path, name, table = StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test") +("./test", "fib", IdDict{Any, String}()) shell> tree \$path ./test -|-- obj.o -`-- obj.so - -0 directories, 2 files - -julia> test(100_000) -5.256496109495593 +└── obj.o -julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 100_000) -5.256496109495593 +0 directories, 1 file ``` """ -function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; +function generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; strip_llvm = false, strip_asm = true, opt_level=3, kwargs...) mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - lib_path = joinpath(path, "$filenamebase.$(Libdl.dlext)") tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) job, kwargs = native_job(f, tt; name, kwargs...) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false) - + # Use Enzyme's annotation and optimization pipeline annotate!(mod) optimize!(mod, tm) - + # Scoop up all the pointers in the optimized module, and replace them with unitialized global variables. # `table` is a dictionary where the keys are julia objects that are needed by the function, and the values # of the dictionary are the names of their associated LLVM GlobalVariable names. table = relocation_table!(mod) - + # Now that we've removed all the pointers from the code, we can (hopefully) safely lower all the instrinsics # (again, using Enzyme's pipeline) post_optimize!(mod, tm) - + # Make sure we didn't make any glaring errors LLVM.verify(mod) - + # Compile the LLVM module to native code and save it to disk obj, _ = GPUCompiler.emit_asm(job, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io @@ -237,12 +225,15 @@ shell> ./hello Hello, world! ``` """ -function compile_executable(f, _tt=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); filename=name, kwargs...) - tt = Base.to_tuple_type(_tt) - tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $_tt must be either () or (Int, Ptr{Ptr{UInt8}})") +function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); + filename=name, + kwargs...) + + tt = Base.to_tuple_type(types) + tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})") rt = only(native_code_typed(f, tt))[2] - isconcretetype(rt) || error("$f$_tt did not infer to a concrete type. Got $rt") + isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this @@ -253,10 +244,36 @@ function compile_executable(f, _tt=(), path::String="./", name=GPUCompiler.safe_ end +""" +```julia +compile_shlib(f, types::Tuple, path::String, name::String=repr(f); filename::String=name, kwargs...) +``` +As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. +""" +function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); + filename=name, + kwargs...) + + tt = Base.to_tuple_type(types) + isconcretetype(tt) || error("input type signature $types is not concrete") + + rt = only(native_code_typed(f, tt))[2] + isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") + + # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals + # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this + + generate_shlib(f, tt, path, name, filename; kwargs...) + + joinpath(abspath(path), filename * "." * Libdl.dlext) +end + +function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=name; + temp::Bool=true, + kwargs...) -function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; temp::Bool=true, kwargs...) generate_shlib(f, tt, path, name; kwargs...) - lib_path = joinpath(abspath(path), "$filenamebase.$(Libdl.dlext)") + lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @assert fptr != C_NULL @@ -302,8 +319,8 @@ julia> test(100_000) 5.256496109495593 ``` """ -function generate_shlib_fptr(path::String, name, filenamebase::String="obj") - lib_path = joinpath(abspath(path), "$filenamebase.$(Libdl.dlext)") +function generate_shlib_fptr(path::String, name, filename::String=name) + lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @assert fptr != C_NULL @@ -334,38 +351,90 @@ function generate_executable(f, tt, path::String = tempname(), name = GPUCompile mkpath(path) obj_path = joinpath(path, "$filename.o") exec_path = joinpath(path, filename) + job, kwargs = native_job(f, tt; name, kwargs...) + obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) + + # Write to file open(obj_path, "w") do io - job, kwargs = native_job(f, tt; name, kwargs...) - obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) + write(io, obj) + end + + # Pick a compiler + cc = Sys.isapple() ? `cc` : clang() + # Compile! + if Sys.isapple() + # Apple no longer uses _start, so we can just specify a custom entry + entry = "_julia_$name" + run(`$cc -e $entry $obj_path -o $exec_path`) + else + # Write a minimal wrapper to avoid having to specify a custom entry + wrapper_path = joinpath(path, "wrapper.c") + f = open(wrapper_path, "w") + print(f, """int main(int argc, char** argv) + { + julia_$name(argc, argv); + return 0; + }""") + close(f) + run(`$cc $wrapper_path $obj_path -o $exec_path`) + # Clean up + run(`rm $wrapper_path`) + end + + path, name +end + +""" +```julia +generate_shlib(f, tt, path::String, name::String, filenamebase::String="obj"; kwargs...) +``` +Low level interface for compiling a shared object / dynamically loaded library + (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing +the types of the arguments for which the function will be compiled. +See also `StaticCompiler.generate_shlib_fptr`. +### Examples +```julia +julia> function test(n) + r = 0.0 + for i=1:n + r += log(sqrt(i)) + end + return r/n + end +test (generic function with 1 method) +julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, "./test") +("./test", "test") +shell> tree \$path +./test +|-- obj.o +`-- obj.so +0 directories, 2 files +julia> test(100_000) +5.256496109495593 +julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 100_000) +5.256496109495593 +``` +""" +function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=name; kwargs...) + mkpath(path) + obj_path = joinpath(path, "$filename.o") + lib_path = joinpath(path, "$filename.$(Libdl.dlext)") + job, kwargs = native_job(f, tt; name, kwargs...) + obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) + open(obj_path, "w") do io write(io, obj) - flush(io) - - # Pick a compiler - cc = Sys.isapple() ? `cc` : clang() - # Compile! - if Sys.isapple() - # Apple no longer uses _start, so we can just specify a custom entry - entry = "_julia_$name" - run(`$cc -e $entry $obj_path -o $exec_path`) - else - # Write a minimal wrapper to avoid having to specify a custom entry - wrapper_path = joinpath(path, "wrapper.c") - f = open(wrapper_path, "w") - print(f, """int main(int argc, char** argv) - { - julia_$name(argc, argv); - return 0; - }""") - close(f) - run(`$cc $wrapper_path $obj_path -o $exec_path`) - # Clean up - run(`rm $wrapper_path`) - end end + + # Pick a Clang + cc = Sys.isapple() ? `cc` : clang() + # Compile! + run(`$cc -shared -o $lib_path $obj_path`) + path, name end + function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) job, kwargs = native_job(func, types; kwargs...) GPUCompiler.code_llvm(stdout, job; kwargs...) diff --git a/test/runtests.jl b/test/runtests.jl index 55f04884..831a3a27 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -38,18 +38,6 @@ fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globall #@test compile(fib2, (Int,))[1](20) == fib(20) end -# Call binaries for testing -# @testset "Generate binary" begin -# fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) -# libname = tempname() -# generate_shlib(fib, (Int,), libname) -# ptr = Libdl.dlopen(libname * "." * Libdl.dlext, Libdl.RTLD_LOCAL) -# fptr = Libdl.dlsym(ptr, "julia_fib") -# @assert fptr != C_NULL -# # This works on REPL -# @test_skip ccall(fptr, Int, (Int,), 10) == 55 -# end - @testset "Loops" begin function sum_first_N_int(N) @@ -154,7 +142,7 @@ end e end end - @test fetch(tsk) isa DomainError + @test fetch(tsk) isa DomainError end # Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. @@ -175,7 +163,7 @@ end BLAS.dot(N, a, 1, a, 1) end a = [1.0, 2.0] - + mydot_compiled, path = compile(mydot, (Vector{Float64},)) # Works locally for me, but not on CI. Need some improvements to pointer relocation to be robust. @test_skip remote_load_call(path, a) == 5.0 @@ -250,6 +238,22 @@ end @test C ≈ A*B end +@testset "Standalone Dylibs" begin + # Test function + # (already defined) + # fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) + + #Compile dylib + name = repr(fib) + filepath = compile_shlib(fib, (Int,), "./", name) + @test occursin("fib.$(Libdl.dlext)", filepath) + + # Open dylib + ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_$name") + @test fptr != C_NULL + @test ccall(fptr, Int, (Int,), 10) == 55 +end @testset "Standalone Executables" begin From 12f475ba737393222f52291aa837c9da7a4c53e4 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sat, 19 Feb 2022 13:29:22 -0700 Subject: [PATCH 055/202] Increment versio. --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ef7854a2..64341090 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short"] -version = "0.4.0" +version = "0.4.1" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" From 0409c50d5c2fb4cfc3612b321e33971891ac9a9c Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sat, 19 Feb 2022 16:05:09 -0500 Subject: [PATCH 056/202] Remove JLD2 from Project.toml (#65) * Remove JLD2 from Project.toml * Delete Manifest.toml --- Manifest.toml | 264 -------------------------------------------------- Project.toml | 1 - 2 files changed, 265 deletions(-) delete mode 100644 Manifest.toml diff --git a/Manifest.toml b/Manifest.toml deleted file mode 100644 index a31f8206..00000000 --- a/Manifest.toml +++ /dev/null @@ -1,264 +0,0 @@ -# This file is machine-generated - editing it directly is not advised - -julia_version = "1.7.2" -manifest_format = "2.0" - -[[deps.ArgTools]] -uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" - -[[deps.Artifacts]] -uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" - -[[deps.Base64]] -uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" - -[[deps.CEnum]] -git-tree-sha1 = "215a9aa4a1f23fbd05b92769fdd62559488d70e9" -uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82" -version = "0.4.1" - -[[deps.Clang_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll", "libLLVM_jll"] -git-tree-sha1 = "8cf7e67e264dedc5d321ec87e78525e958aea057" -uuid = "0ee61d77-7f21-5576-8119-9fcc46b10100" -version = "12.0.1+3" - -[[deps.Compat]] -deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] -git-tree-sha1 = "44c37b4636bc54afac5c574d2d02b625349d6582" -uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "3.41.0" - -[[deps.CompilerSupportLibraries_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" - -[[deps.DataStructures]] -deps = ["Compat", "InteractiveUtils", "OrderedCollections"] -git-tree-sha1 = "3daef5523dd2e769dad2365274f760ff5f282c7d" -uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" -version = "0.18.11" - -[[deps.Dates]] -deps = ["Printf"] -uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" - -[[deps.DelimitedFiles]] -deps = ["Mmap"] -uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" - -[[deps.Distributed]] -deps = ["Random", "Serialization", "Sockets"] -uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" - -[[deps.Downloads]] -deps = ["ArgTools", "LibCURL", "NetworkOptions"] -uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" - -[[deps.ExprTools]] -git-tree-sha1 = "56559bbef6ca5ea0c0818fa5c90320398a6fbf8d" -uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04" -version = "0.1.8" - -[[deps.FileIO]] -deps = ["Pkg", "Requires", "UUIDs"] -git-tree-sha1 = "80ced645013a5dbdc52cf70329399c35ce007fae" -uuid = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" -version = "1.13.0" - -[[deps.GPUCompiler]] -deps = ["ExprTools", "InteractiveUtils", "LLVM", "Libdl", "Logging", "TimerOutputs", "UUIDs"] -git-tree-sha1 = "abd824e1f2ecd18d33811629c781441e94a24e81" -uuid = "61eb1bfa-7361-4325-ad38-22787b887f55" -version = "0.13.11" - -[[deps.InteractiveUtils]] -deps = ["Markdown"] -uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" - -[[deps.JLD2]] -deps = ["DataStructures", "FileIO", "MacroTools", "Mmap", "Pkg", "Printf", "Reexport", "TranscodingStreams", "UUIDs"] -git-tree-sha1 = "b528d68220e2aba1d2d0c0461b6f7eda8c5c1e33" -uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819" -version = "0.4.20" - -[[deps.JLLWrappers]] -deps = ["Preferences"] -git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" -uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" -version = "1.4.1" - -[[deps.LLVM]] -deps = ["CEnum", "LLVMExtra_jll", "Libdl", "Printf", "Unicode"] -git-tree-sha1 = "f8dcd7adfda0dddaf944e62476d823164cccc217" -uuid = "929cbde3-209d-540e-8aea-75f648917ca0" -version = "4.7.1" - -[[deps.LLVMExtra_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "62115afed394c016c2d3096c5b85c407b48be96b" -uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab" -version = "0.0.13+1" - -[[deps.LibCURL]] -deps = ["LibCURL_jll", "MozillaCACerts_jll"] -uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" - -[[deps.LibCURL_jll]] -deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] -uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" - -[[deps.LibGit2]] -deps = ["Base64", "NetworkOptions", "Printf", "SHA"] -uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" - -[[deps.LibSSH2_jll]] -deps = ["Artifacts", "Libdl", "MbedTLS_jll"] -uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" - -[[deps.Libdl]] -uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" - -[[deps.LinearAlgebra]] -deps = ["Libdl", "libblastrampoline_jll"] -uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" - -[[deps.Logging]] -uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" - -[[deps.MacroTools]] -deps = ["Markdown", "Random"] -git-tree-sha1 = "3d3e902b31198a27340d0bf00d6ac452866021cf" -uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" -version = "0.5.9" - -[[deps.Markdown]] -deps = ["Base64"] -uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" - -[[deps.MbedTLS_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" - -[[deps.Mmap]] -uuid = "a63ad114-7e13-5084-954f-fe012c677804" - -[[deps.MozillaCACerts_jll]] -uuid = "14a3606d-f60d-562e-9121-12d972cd8159" - -[[deps.NetworkOptions]] -uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" - -[[deps.OpenBLAS_jll]] -deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] -uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" - -[[deps.OrderedCollections]] -git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c" -uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" -version = "1.4.1" - -[[deps.Pkg]] -deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] -uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" - -[[deps.Preferences]] -deps = ["TOML"] -git-tree-sha1 = "2cf929d64681236a2e074ffafb8d568733d2e6af" -uuid = "21216c6a-2e73-6563-6e65-726566657250" -version = "1.2.3" - -[[deps.Printf]] -deps = ["Unicode"] -uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" - -[[deps.REPL]] -deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] -uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" - -[[deps.Random]] -deps = ["SHA", "Serialization"] -uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" - -[[deps.Reexport]] -git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b" -uuid = "189a3867-3050-52da-a836-e630ba90ab69" -version = "1.2.2" - -[[deps.Requires]] -deps = ["UUIDs"] -git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7" -uuid = "ae029012-a4dd-5104-9daa-d747884805df" -version = "1.3.0" - -[[deps.SHA]] -uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" - -[[deps.Serialization]] -uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" - -[[deps.SharedArrays]] -deps = ["Distributed", "Mmap", "Random", "Serialization"] -uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" - -[[deps.Sockets]] -uuid = "6462fe0b-24de-5631-8697-dd941f90decc" - -[[deps.SparseArrays]] -deps = ["LinearAlgebra", "Random"] -uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" - -[[deps.Statistics]] -deps = ["LinearAlgebra", "SparseArrays"] -uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" - -[[deps.TOML]] -deps = ["Dates"] -uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" - -[[deps.Tar]] -deps = ["ArgTools", "SHA"] -uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" - -[[deps.Test]] -deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] -uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[[deps.TimerOutputs]] -deps = ["ExprTools", "Printf"] -git-tree-sha1 = "97e999be94a7147d0609d0b9fc9feca4bf24d76b" -uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" -version = "0.5.15" - -[[deps.TranscodingStreams]] -deps = ["Random", "Test"] -git-tree-sha1 = "216b95ea110b5972db65aa90f88d8d89dcb8851c" -uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" -version = "0.9.6" - -[[deps.UUIDs]] -deps = ["Random", "SHA"] -uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" - -[[deps.Unicode]] -uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" - -[[deps.Zlib_jll]] -deps = ["Libdl"] -uuid = "83775a58-1f1d-513f-b197-d71354ab007a" - -[[deps.libLLVM_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "8f36deef-c2a5-5394-99ed-8e07531fb29a" - -[[deps.libblastrampoline_jll]] -deps = ["Artifacts", "Libdl", "OpenBLAS_jll"] -uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" - -[[deps.nghttp2_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" - -[[deps.p7zip_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" diff --git a/Project.toml b/Project.toml index 64341090..e0c6cdc6 100644 --- a/Project.toml +++ b/Project.toml @@ -6,7 +6,6 @@ version = "0.4.1" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" -JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" From c63f1d8966e2833e836bb95b95b771e29967ebeb Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sun, 20 Feb 2022 17:58:33 -0500 Subject: [PATCH 057/202] Update LICENSE Did I miss anyone? --- LICENSE | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index acf45480..cb19b0ae 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,6 @@ -Copyright (c) 2019 Tom Short +Copyright (c) 2019-2022 Mason Protter, William Moses, Valentin Churavy, + Brenhin Keller, Julian Samaroo, Tom Short, 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 From 59efe6e0814a71d3dea0b7c4893cf2d19264fb15 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 24 Feb 2022 16:17:11 -0700 Subject: [PATCH 058/202] Bump LLVM version This should fix the breakages that happened on the MacOS master builds --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e0c6cdc6..3ead1164 100644 --- a/Project.toml +++ b/Project.toml @@ -12,7 +12,7 @@ Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" [compat] GPUCompiler = "0.13" -LLVM = "4" +LLVM = "4.8" julia = "1.7" [extras] From f3c9133a73308974c4d540c04ba9007effa37f19 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 24 Feb 2022 16:40:11 -0700 Subject: [PATCH 059/202] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 48509ee8..3ab42512 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ This is an experimental package to compile Julia code to standalone libraries. A ```julia using Pkg -Pkg.add(PackageSpec( url = "https://github.com/tshort/StaticCompiler.jl", rev = "master")) +Pkg.add("StaticCompiler") ``` ```julia @@ -51,4 +51,4 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi * GC-tracked allocations and global varaibles do *not* work with `compile_executable` (yet). * Type unstable code is not yet supported. * Doesn't currently work on Windows. -* If you find any other limitations, let us know. There's probably lots. \ No newline at end of file +* If you find any other limitations, let us know. There's probably lots. From e6e024e2ab0ff1646af25a84aaeb8e54495089d5 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 24 Feb 2022 16:41:20 -0700 Subject: [PATCH 060/202] bump patch version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 3ead1164..2c8d2900 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short"] -version = "0.4.1" +version = "0.4.2" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" From 658a5cdf20cde19e97a80fbcc73d4618b783cd04 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sun, 27 Feb 2022 15:12:25 -0700 Subject: [PATCH 061/202] add v1.8 testing --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03f84766..e1a990df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,7 @@ jobs: matrix: version: - '1.7' + - '1.8' - 'nightly' os: - ubuntu-latest From dfd5aa2e681859d0c3737a82c1935b0a7291cea2 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sun, 27 Feb 2022 15:14:32 -0700 Subject: [PATCH 062/202] fix versionsoec --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1a990df..1eca9e19 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: matrix: version: - '1.7' - - '1.8' + - '~1.8.0-0' - 'nightly' os: - ubuntu-latest From 3c31bae58be4a91adb4c6212f33bf13130746c8f Mon Sep 17 00:00:00 2001 From: Helge Eichhorn Date: Mon, 11 Apr 2022 13:04:17 +0200 Subject: [PATCH 063/202] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3ab42512..a14add02 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi ## 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 varaibles do *not* work with `compile_executable` (yet). +* GC-tracked allocations and global variables do *not* work with `compile_executable` (yet). * Type unstable code is not yet supported. * Doesn't currently work on Windows. * If you find any other limitations, let us know. There's probably lots. From d0be3025946ab9be7838c3d0effc07ef5dc4a6fb Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Fri, 20 May 2022 13:09:55 -0300 Subject: [PATCH 064/202] Add option to receive Array of functions --- src/StaticCompiler.jl | 69 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index da4b8046..9b97ac2c 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -434,7 +434,6 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf path, name end - function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) job, kwargs = native_job(func, types; kwargs...) GPUCompiler.code_llvm(stdout, job; kwargs...) @@ -457,9 +456,77 @@ function native_code_native(@nospecialize(f), @nospecialize(tt), name = GPUCompi GPUCompiler.code_native(stdout, job; kwargs...) end +#Return an LLVM module for multiple functions +function native_llvm_module(funcs::Array; mangle_names = false, kwargs...) + f,tt = funcs[1] + mod = native_llvm_module(f,tt, kwargs...) + if length(funcs) > 1 + for func in funcs[2:end] + @show f,tt = func + tmod = native_llvm_module(f,tt, kwargs...) + link!(mod,tmod) + end + end + if mangle_names + for func in functions(mod) + fname = name(func) + name!(func,fname[7:end]) + end + end + return mod +end +function generate_obj(funcs::Array, path::String = tempname(), filenamebase::String="obj", + mangle_names =false; + strip_llvm = false, + strip_asm = true, + opt_level=3, + kwargs...) + f,tt = funcs[1] + mkpath(path) + obj_path = joinpath(path, "$filenamebase.o") + fakejob, kwargs = native_job(f,tt, kwargs...) + mod = native_llvm_module(funcs, kwargs...) + obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) + open(obj_path, "w") do io + write(io, obj) + end + path, obj_path +end +function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo", mangle_names=false; kwargs...) + + lib_path = joinpath(path, "$filename.$(Libdl.dlext)") + _,obj_path = generate_obj(funcs, path, kwargs...) + # Pick a Clang + cc = Sys.isapple() ? `cc` : clang() + # Compile! + run(`$cc -shared -o $lib_path $obj_path`) + + path, name +end + +function compile_shlib(funcs::Array, path::String="./"; + filename="libfoo", + mangle_names=false, + kwargs...) + for func in funcs + f, types = func + tt = Base.to_tuple_type(types) + isconcretetype(tt) || error("input type signature $types is not concrete") + + rt = only(native_code_typed(f, tt))[2] + isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") + end + +# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals +# Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this + + generate_shlib(funcs, path, filename, mangle_names, kwargs...) + + joinpath(abspath(path), filename * "." * Libdl.dlext) +end end # module From 284cd2ccac411780bae7f95a99ffae579bed8ab5 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Fri, 20 May 2022 13:14:48 -0300 Subject: [PATCH 065/202] actually pass mangle_names --- src/StaticCompiler.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 9b97ac2c..8c8a1c2c 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -486,7 +486,7 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str mkpath(path) obj_path = joinpath(path, "$filenamebase.o") fakejob, kwargs = native_job(f,tt, kwargs...) - mod = native_llvm_module(funcs, kwargs...) + mod = native_llvm_module(funcs, mangle_names, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io write(io, obj) @@ -498,7 +498,7 @@ function generate_shlib(funcs::Array, path::String = tempname(), filename::Strin lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - _,obj_path = generate_obj(funcs, path, kwargs...) + _,obj_path = generate_obj(funcs, path, mangle_names, kwargs...) # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! From 9ffeefee3bbca58066723d33e81b73e5a8e1d1bb Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Fri, 20 May 2022 13:25:48 -0300 Subject: [PATCH 066/202] Fix keyword argument uses --- src/StaticCompiler.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 8c8a1c2c..950dc4df 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -476,8 +476,8 @@ function native_llvm_module(funcs::Array; mangle_names = false, kwargs...) return mod end -function generate_obj(funcs::Array, path::String = tempname(), filenamebase::String="obj", - mangle_names =false; +function generate_obj(funcs::Array, path::String = tempname(), filenamebase::String="obj"; + mangle_names =false, strip_llvm = false, strip_asm = true, opt_level=3, @@ -486,7 +486,7 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str mkpath(path) obj_path = joinpath(path, "$filenamebase.o") fakejob, kwargs = native_job(f,tt, kwargs...) - mod = native_llvm_module(funcs, mangle_names, kwargs...) + mod = native_llvm_module(funcs; mangle_names = mangle_names, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io write(io, obj) @@ -494,11 +494,11 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str path, obj_path end -function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo", mangle_names=false; kwargs...) +function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; mangle_names=false, kwargs...) lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - _,obj_path = generate_obj(funcs, path, mangle_names, kwargs...) + _,obj_path = generate_obj(funcs, path, filename; mangle_names=mangle_names, kwargs...) # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! @@ -523,7 +523,7 @@ function compile_shlib(funcs::Array, path::String="./"; # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - generate_shlib(funcs, path, filename, mangle_names, kwargs...) + generate_shlib(funcs, path, filename; mangle_names=mangle_names, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end From 95dd56d9a175554f05b8c019d2c1df6b6b6874cd Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Sat, 21 May 2022 23:06:35 -0400 Subject: [PATCH 067/202] Separate out nightly CI, re-enable codecov --- .github/workflows/ci-julia-nightly.yml | 38 ++++++++++++++++++++++++++ .github/workflows/ci.yml | 21 ++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/ci-julia-nightly.yml diff --git a/.github/workflows/ci-julia-nightly.yml b/.github/workflows/ci-julia-nightly.yml new file mode 100644 index 00000000..98dc7810 --- /dev/null +++ b/.github/workflows/ci-julia-nightly.yml @@ -0,0 +1,38 @@ +name: CI (Julia nightly) +on: + push: + branches: + - '**' + paths-ignore: + - 'README.md' + pull_request: + branches: + - main + paths-ignore: + - 'README.md' +jobs: + test-julia-nightly: + name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + version: + - 'nightly' + os: + - ubuntu-latest + - macOS-latest + arch: + - x64 + group: + - Core + steps: + - uses: actions/checkout@v2 + - uses: julia-actions/setup-julia@latest + with: + version: ${{ matrix.version }} + arch: ${{ matrix.arch }} + - uses: julia-actions/julia-buildpkg@latest + - uses: julia-actions/julia-runtest@latest + env: + GROUP: ${{ matrix.group }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1eca9e19..4e009549 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,15 @@ name: CI on: - - push - - pull_request + push: + branches: + - '**' + paths-ignore: + - 'README.md' + pull_request: + branches: + - main + paths-ignore: + - 'README.md' jobs: test: name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} @@ -12,12 +20,13 @@ jobs: version: - '1.7' - '~1.8.0-0' - - 'nightly' os: - ubuntu-latest - macOS-latest arch: - x64 + group: + - Core steps: - uses: actions/checkout@v2 - uses: julia-actions/setup-julia@latest @@ -26,3 +35,9 @@ jobs: arch: ${{ matrix.arch }} - uses: julia-actions/julia-buildpkg@latest - uses: julia-actions/julia-runtest@latest + env: + GROUP: ${{ matrix.group }} + - uses: julia-actions/julia-processcoverage@v1 + - uses: codecov/codecov-action@v2 + with: + files: lcov.info From 0d9556b90a6d8758cc3a65fe0e296cd9bf84f92e Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Sat, 21 May 2022 23:18:26 -0400 Subject: [PATCH 068/202] Add integration tests for standalone executables --- .github/workflows/ci-integration.yml | 38 ++ test/Manifest.toml | 450 +++++++++++++++++++++ test/Project.toml | 5 +- test/runtests.jl | 571 +++++++++++++++------------ test/scripts/loopvec_matrix.jl | 54 +++ test/scripts/loopvec_product.jl | 25 ++ test/scripts/print_args.jl | 33 ++ test/scripts/rand_matrix.jl | 21 + test/scripts/times_table.jl | 26 ++ 9 files changed, 979 insertions(+), 244 deletions(-) create mode 100644 .github/workflows/ci-integration.yml create mode 100644 test/Manifest.toml create mode 100644 test/scripts/loopvec_matrix.jl create mode 100644 test/scripts/loopvec_product.jl create mode 100644 test/scripts/print_args.jl create mode 100644 test/scripts/rand_matrix.jl create mode 100644 test/scripts/times_table.jl diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml new file mode 100644 index 00000000..025f250f --- /dev/null +++ b/.github/workflows/ci-integration.yml @@ -0,0 +1,38 @@ +name: CI (Integration) +on: + push: + branches: + - '**' + paths-ignore: + - 'README.md' + pull_request: + branches: + - main + paths-ignore: + - 'README.md' +jobs: + test-integration: + name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + version: + - '~1.8.0-0' + os: + - ubuntu-latest + - macOS-latest + arch: + - x64 + group: + - Integration + steps: + - uses: actions/checkout@v2 + - uses: julia-actions/setup-julia@latest + with: + version: ${{ matrix.version }} + arch: ${{ matrix.arch }} + - uses: julia-actions/julia-buildpkg@latest + - uses: julia-actions/julia-runtest@latest + env: + GROUP: ${{ matrix.group }} diff --git a/test/Manifest.toml b/test/Manifest.toml new file mode 100644 index 00000000..2031c1d0 --- /dev/null +++ b/test/Manifest.toml @@ -0,0 +1,450 @@ +# This file is machine-generated - editing it directly is not advised + +julia_version = "1.8.0-beta3" +manifest_format = "2.0" +project_hash = "429ef1d09132391cc32bc872843d4edc99133ef5" + +[[deps.Adapt]] +deps = ["LinearAlgebra"] +git-tree-sha1 = "af92965fb30777147966f58acb05da51c5616b5f" +uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" +version = "3.3.3" + +[[deps.ArgTools]] +uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" +version = "1.1.1" + +[[deps.ArrayInterface]] +deps = ["Compat", "IfElse", "LinearAlgebra", "Requires", "SparseArrays", "Static"] +git-tree-sha1 = "81f0cb60dc994ca17f68d9fb7c942a5ae70d9ee4" +uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" +version = "5.0.8" + +[[deps.Artifacts]] +uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" + +[[deps.Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[deps.BitTwiddlingConvenienceFunctions]] +deps = ["Static"] +git-tree-sha1 = "28bbdbf0354959db89358d1d79d421ff31ef0b5e" +uuid = "62783981-4cbd-42fc-bca8-16325de8dc4b" +version = "0.1.3" + +[[deps.CEnum]] +git-tree-sha1 = "eb4cb44a499229b3b8426dcfb5dd85333951ff90" +uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82" +version = "0.4.2" + +[[deps.CPUSummary]] +deps = ["CpuId", "IfElse", "Static"] +git-tree-sha1 = "0eaf4aedad5ccc3e39481db55d72973f856dc564" +uuid = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9" +version = "0.1.22" + +[[deps.ChainRulesCore]] +deps = ["Compat", "LinearAlgebra", "SparseArrays"] +git-tree-sha1 = "9950387274246d08af38f6eef8cb5480862a435f" +uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" +version = "1.14.0" + +[[deps.ChangesOfVariables]] +deps = ["ChainRulesCore", "LinearAlgebra", "Test"] +git-tree-sha1 = "1e315e3f4b0b7ce40feded39c73049692126cf53" +uuid = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0" +version = "0.1.3" + +[[deps.CloseOpenIntervals]] +deps = ["ArrayInterface", "Static"] +git-tree-sha1 = "f576084239e6bdf801007c80e27e2cc2cd963fe0" +uuid = "fb6a15b2-703c-40df-9091-08a04967cfa9" +version = "0.1.6" + +[[deps.CommonSubexpressions]] +deps = ["MacroTools", "Test"] +git-tree-sha1 = "7b8a93dba8af7e3b42fecabf646260105ac373f7" +uuid = "bbf7d656-a473-5ed7-a52c-81e309532950" +version = "0.3.0" + +[[deps.Compat]] +deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] +git-tree-sha1 = "b153278a25dd42c65abbf4e62344f9d22e59191b" +uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" +version = "3.43.0" + +[[deps.CompilerSupportLibraries_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" +version = "0.5.2+0" + +[[deps.CpuId]] +deps = ["Markdown"] +git-tree-sha1 = "fcbb72b032692610bfbdb15018ac16a36cf2e406" +uuid = "adafc99b-e345-5852-983c-f28acb93d879" +version = "0.3.1" + +[[deps.Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[deps.DelimitedFiles]] +deps = ["Mmap"] +uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" + +[[deps.DiffResults]] +deps = ["StaticArrays"] +git-tree-sha1 = "c18e98cba888c6c25d1c3b048e4b3380ca956805" +uuid = "163ba53b-c6d8-5494-b064-1a9d43ac40c5" +version = "1.0.3" + +[[deps.DiffRules]] +deps = ["IrrationalConstants", "LogExpFunctions", "NaNMath", "Random", "SpecialFunctions"] +git-tree-sha1 = "28d605d9a0ac17118fe2c5e9ce0fbb76c3ceb120" +uuid = "b552c78f-8df3-52c6-915a-8e097449b14b" +version = "1.11.0" + +[[deps.Distributed]] +deps = ["Random", "Serialization", "Sockets"] +uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" + +[[deps.DocStringExtensions]] +deps = ["LibGit2"] +git-tree-sha1 = "b19534d1895d702889b219c382a6e18010797f0b" +uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" +version = "0.8.6" + +[[deps.Downloads]] +deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"] +uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" +version = "1.6.0" + +[[deps.FileWatching]] +uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" + +[[deps.Formatting]] +deps = ["Printf"] +git-tree-sha1 = "8339d61043228fdd3eb658d86c926cb282ae72a8" +uuid = "59287772-0a20-5a39-b81b-1366585eb4c0" +version = "0.4.2" + +[[deps.ForwardDiff]] +deps = ["CommonSubexpressions", "DiffResults", "DiffRules", "LinearAlgebra", "LogExpFunctions", "NaNMath", "Preferences", "Printf", "Random", "SpecialFunctions", "StaticArrays"] +git-tree-sha1 = "2f18915445b248731ec5db4e4a17e451020bf21e" +uuid = "f6369f11-7733-5829-9624-2563aa707210" +version = "0.10.30" + +[[deps.HostCPUFeatures]] +deps = ["BitTwiddlingConvenienceFunctions", "IfElse", "Libdl", "Static"] +git-tree-sha1 = "18be5268cf415b5e27f34980ed25a7d34261aa83" +uuid = "3e5b6fbb-0976-4d2c-9146-d79de83f2fb0" +version = "0.1.7" + +[[deps.IfElse]] +git-tree-sha1 = "debdd00ffef04665ccbb3e150747a77560e8fad1" +uuid = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173" +version = "0.1.1" + +[[deps.InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[deps.InverseFunctions]] +deps = ["Test"] +git-tree-sha1 = "336cc738f03e069ef2cac55a104eb823455dca75" +uuid = "3587e190-3f89-42d0-90ee-14403ec27112" +version = "0.1.4" + +[[deps.IrrationalConstants]] +git-tree-sha1 = "7fd44fd4ff43fc60815f8e764c0f352b83c49151" +uuid = "92d709cd-6900-40b7-9082-c6be49f344b6" +version = "0.1.1" + +[[deps.JLLWrappers]] +deps = ["Preferences"] +git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" +uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" +version = "1.4.1" + +[[deps.LLVM]] +deps = ["CEnum", "LLVMExtra_jll", "Libdl", "Printf", "Unicode"] +git-tree-sha1 = "c8d47589611803a0f3b4813d9e267cd4e3dbcefb" +uuid = "929cbde3-209d-540e-8aea-75f648917ca0" +version = "4.11.1" + +[[deps.LLVMExtra_jll]] +deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl", "Pkg", "TOML"] +git-tree-sha1 = "771bfe376249626d3ca12bcd58ba243d3f961576" +uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab" +version = "0.0.16+0" + +[[deps.LayoutPointers]] +deps = ["ArrayInterface", "LinearAlgebra", "ManualMemory", "SIMDTypes", "Static"] +git-tree-sha1 = "3da92dc6d4bce776cc850c18274a7508b8d752ef" +uuid = "10f19ff3-798f-405d-979b-55457f8fc047" +version = "0.1.7" + +[[deps.LazyArtifacts]] +deps = ["Artifacts", "Pkg"] +uuid = "4af54fe1-eca0-43a8-85a7-787d91b784e3" + +[[deps.LibCURL]] +deps = ["LibCURL_jll", "MozillaCACerts_jll"] +uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" +version = "0.6.3" + +[[deps.LibCURL_jll]] +deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] +uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" +version = "7.81.0+0" + +[[deps.LibGit2]] +deps = ["Base64", "NetworkOptions", "Printf", "SHA"] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" + +[[deps.LibSSH2_jll]] +deps = ["Artifacts", "Libdl", "MbedTLS_jll"] +uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" +version = "1.10.2+0" + +[[deps.Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[deps.LinearAlgebra]] +deps = ["Libdl", "libblastrampoline_jll"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[[deps.LogExpFunctions]] +deps = ["ChainRulesCore", "ChangesOfVariables", "DocStringExtensions", "InverseFunctions", "IrrationalConstants", "LinearAlgebra"] +git-tree-sha1 = "09e4b894ce6a976c354a69041a04748180d43637" +uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688" +version = "0.3.15" + +[[deps.Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[deps.LoopVectorization]] +deps = ["ArrayInterface", "CPUSummary", "ChainRulesCore", "CloseOpenIntervals", "DocStringExtensions", "ForwardDiff", "HostCPUFeatures", "IfElse", "LayoutPointers", "LinearAlgebra", "OffsetArrays", "PolyesterWeave", "SIMDDualNumbers", "SLEEFPirates", "SpecialFunctions", "Static", "ThreadingUtilities", "UnPack", "VectorizationBase"] +git-tree-sha1 = "4392c19f0203df81512b6790a0a67446650bdce0" +uuid = "bdcacae8-1622-11e9-2a5c-532679323890" +version = "0.12.110" + +[[deps.MacroTools]] +deps = ["Markdown", "Random"] +git-tree-sha1 = "3d3e902b31198a27340d0bf00d6ac452866021cf" +uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" +version = "0.5.9" + +[[deps.ManualMemory]] +git-tree-sha1 = "bcaef4fc7a0cfe2cba636d84cda54b5e4e4ca3cd" +uuid = "d125e4d3-2237-4719-b19c-fa641b8a4667" +version = "0.1.8" + +[[deps.Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[deps.MbedTLS_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" +version = "2.28.0+0" + +[[deps.Mmap]] +uuid = "a63ad114-7e13-5084-954f-fe012c677804" + +[[deps.MozillaCACerts_jll]] +uuid = "14a3606d-f60d-562e-9121-12d972cd8159" +version = "2022.2.1" + +[[deps.NaNMath]] +git-tree-sha1 = "737a5957f387b17e74d4ad2f440eb330b39a62c5" +uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" +version = "1.0.0" + +[[deps.NetworkOptions]] +uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" +version = "1.2.0" + +[[deps.OffsetArrays]] +deps = ["Adapt"] +git-tree-sha1 = "52addd9e91df8a6a5781e5c7640787525fd48056" +uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" +version = "1.11.2" + +[[deps.OpenBLAS_jll]] +deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] +uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" +version = "0.3.20+0" + +[[deps.OpenLibm_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "05823500-19ac-5b8b-9628-191a04bc5112" +version = "0.8.1+0" + +[[deps.OpenSpecFun_jll]] +deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "13652491f6856acfd2db29360e1bbcd4565d04f1" +uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e" +version = "0.5.5+0" + +[[deps.Pkg]] +deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" +version = "1.8.0" + +[[deps.PolyesterWeave]] +deps = ["BitTwiddlingConvenienceFunctions", "CPUSummary", "IfElse", "Static", "ThreadingUtilities"] +git-tree-sha1 = "7e597df97e46ffb1c8adbaddfa56908a7a20194b" +uuid = "1d0040c9-8b98-4ee7-8388-3f51789ca0ad" +version = "0.1.5" + +[[deps.Preferences]] +deps = ["TOML"] +git-tree-sha1 = "47e5f437cc0e7ef2ce8406ce1e7e24d44915f88d" +uuid = "21216c6a-2e73-6563-6e65-726566657250" +version = "1.3.0" + +[[deps.Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[deps.REPL]] +deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] +uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" + +[[deps.Random]] +deps = ["SHA", "Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[deps.Requires]] +deps = ["UUIDs"] +git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7" +uuid = "ae029012-a4dd-5104-9daa-d747884805df" +version = "1.3.0" + +[[deps.SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" +version = "0.7.0" + +[[deps.SIMDDualNumbers]] +deps = ["ForwardDiff", "IfElse", "SLEEFPirates", "VectorizationBase"] +git-tree-sha1 = "dd4195d308df24f33fb10dde7c22103ba88887fa" +uuid = "3cdde19b-5bb0-4aaf-8931-af3e248e098b" +version = "0.1.1" + +[[deps.SIMDTypes]] +git-tree-sha1 = "330289636fb8107c5f32088d2741e9fd7a061a5c" +uuid = "94e857df-77ce-4151-89e5-788b33177be4" +version = "0.1.0" + +[[deps.SLEEFPirates]] +deps = ["IfElse", "Static", "VectorizationBase"] +git-tree-sha1 = "ac399b5b163b9140f9c310dfe9e9aaa225617ff6" +uuid = "476501e8-09a2-5ece-8869-fb82de89a1fa" +version = "0.6.32" + +[[deps.Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[deps.SharedArrays]] +deps = ["Distributed", "Mmap", "Random", "Serialization"] +uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" + +[[deps.Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[deps.SparseArrays]] +deps = ["LinearAlgebra", "Random"] +uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[[deps.SpecialFunctions]] +deps = ["ChainRulesCore", "IrrationalConstants", "LogExpFunctions", "OpenLibm_jll", "OpenSpecFun_jll"] +git-tree-sha1 = "bc40f042cfcc56230f781d92db71f0e21496dffd" +uuid = "276daf66-3868-5448-9aa4-cd146d93841b" +version = "2.1.5" + +[[deps.Static]] +deps = ["IfElse"] +git-tree-sha1 = "3a2a99b067090deb096edecec1dc291c5b4b31cb" +uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3" +version = "0.6.5" + +[[deps.StaticArrays]] +deps = ["LinearAlgebra", "Random", "Statistics"] +git-tree-sha1 = "cd56bf18ed715e8b09f06ef8c6b781e6cdc49911" +uuid = "90137ffa-7385-5640-81b9-e52037218182" +version = "1.4.4" + +[[deps.StaticTools]] +deps = ["ManualMemory"] +git-tree-sha1 = "45569bece43af73ba51cbdac422e77abd7adba9f" +uuid = "86c06d3c-3f03-46de-9781-57580aa96d0a" +version = "0.3.0" + +[[deps.Statistics]] +deps = ["LinearAlgebra", "SparseArrays"] +uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + +[[deps.StrideArraysCore]] +deps = ["ArrayInterface", "CloseOpenIntervals", "IfElse", "LayoutPointers", "ManualMemory", "Requires", "SIMDTypes", "Static", "ThreadingUtilities"] +git-tree-sha1 = "e03eacc0b8c1520e73aa84922ce44a14f024b210" +uuid = "7792a7ef-975c-4747-a70f-980b88e8d1da" +version = "0.3.6" + +[[deps.TOML]] +deps = ["Dates"] +uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" +version = "1.0.0" + +[[deps.Tar]] +deps = ["ArgTools", "SHA"] +uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" +version = "1.10.0" + +[[deps.Test]] +deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[[deps.ThreadingUtilities]] +deps = ["ManualMemory"] +git-tree-sha1 = "f8629df51cab659d70d2e5618a430b4d3f37f2c3" +uuid = "8290d209-cae3-49c0-8002-c8c24d57dab5" +version = "0.5.0" + +[[deps.UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[[deps.UnPack]] +git-tree-sha1 = "387c1f73762231e86e0c9c5443ce3b4a0a9a0c2b" +uuid = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" +version = "1.0.2" + +[[deps.Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[deps.VectorizationBase]] +deps = ["ArrayInterface", "CPUSummary", "HostCPUFeatures", "IfElse", "LayoutPointers", "Libdl", "LinearAlgebra", "SIMDTypes", "Static"] +git-tree-sha1 = "c95d242ade2d67c1510ce52d107cfca7a83e0b4e" +uuid = "3d5dd08c-fd9d-11e8-17fa-ed2836048c2f" +version = "0.21.33" + +[[deps.Zlib_jll]] +deps = ["Libdl"] +uuid = "83775a58-1f1d-513f-b197-d71354ab007a" +version = "1.2.12+1" + +[[deps.libblastrampoline_jll]] +deps = ["Artifacts", "Libdl", "OpenBLAS_jll"] +uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" +version = "5.1.0+0" + +[[deps.nghttp2_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" +version = "1.41.0+1" + +[[deps.p7zip_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" +version = "16.2.1+1" diff --git a/test/Project.toml b/test/Project.toml index b0cf1dcc..b95846e6 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,10 +1,11 @@ [deps] +Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" +StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" -Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" \ No newline at end of file +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index 831a3a27..6b19c410 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,309 +1,396 @@ -using StaticCompiler using Test -using Libdl -using LinearAlgebra -using LoopVectorization -using ManualMemory -using Distributed -using StrideArraysCore -addprocs(1) -@everywhere using StaticCompiler, StrideArraysCore +using StaticCompiler +const GROUP = get(ENV, "GROUP", "All") + +@static if GROUP == "Core" || GROUP == "All" + using Libdl + using LinearAlgebra + using LoopVectorization + using ManualMemory + using Distributed + using StrideArraysCore + addprocs(1) + @everywhere using StaticCompiler, StrideArraysCore -remote_load_call(path, args...) = fetch(@spawnat 2 load_function(path)(args...)) + remote_load_call(path, args...) = fetch(@spawnat 2 load_function(path)(args...)) -@testset "Basics" begin + @testset "Basics" begin - simple_sum(x) = x + one(typeof(x)) + simple_sum(x) = x + one(typeof(x)) - # This probably needs a macro - for T ∈ (Int, Float64, Int32, Float32, Int16, Float16) - _, path, = compile(simple_sum, (T,)) - @test remote_load_call(path, T(1)) == T(2) + # This probably needs a macro + for T ∈ (Int, Float64, Int32, Float32, Int16, Float16) + _, path, = compile(simple_sum, (T,)) + @test remote_load_call(path, T(1)) == T(2) + end end -end -fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 + fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 -@testset "Recursion" begin - _, path = compile(fib, (Int,)) - @test remote_load_call(path, 10) == fib(10) + @testset "Recursion" begin + _, path = compile(fib, (Int,)) + @test remote_load_call(path, 10) == fib(10) - # Trick to work around #40990 - _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) - fib2(n) = _fib2(_fib2, n) + # Trick to work around #40990 + _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) + fib2(n) = _fib2(_fib2, n) - _, path = compile(fib2, (Int,)) - @test remote_load_call(path, 20) == fib(20) - #@test compile(fib2, (Int,))[1](20) == fib(20) -end + _, path = compile(fib2, (Int,)) + @test remote_load_call(path, 20) == fib(20) + #@test compile(fib2, (Int,))[1](20) == fib(20) + end -@testset "Loops" begin - function sum_first_N_int(N) - s = 0 - for a in 1:N - s += a + @testset "Loops" begin + function sum_first_N_int(N) + s = 0 + for a in 1:N + s += a + end + s end - s - end - _, path = compile(sum_first_N_int, (Int,)) - @test remote_load_call(path, 10) == 55 + _, path = compile(sum_first_N_int, (Int,)) + @test remote_load_call(path, 10) == 55 - function sum_first_N_float64(N) - s = Float64(0) - for a in 1:N - s += Float64(a) + function sum_first_N_float64(N) + s = Float64(0) + for a in 1:N + s += Float64(a) + end + s end - s - end - _, path = compile(sum_first_N_float64, (Int,)) - @test remote_load_call(path, 10) == 55. + _, path = compile(sum_first_N_float64, (Int,)) + @test remote_load_call(path, 10) == 55. - function sum_first_N_int_inbounds(N) - s = 0 - @inbounds for a in 1:N - s += a + function sum_first_N_int_inbounds(N) + s = 0 + @inbounds for a in 1:N + s += a + end + s end - s - end - _, path = compile(sum_first_N_int_inbounds, (Int,)) - @test remote_load_call(path, 10) == 55 + _, path = compile(sum_first_N_int_inbounds, (Int,)) + @test remote_load_call(path, 10) == 55 - function sum_first_N_float64_inbounds(N) - s = Float64(0) - @inbounds for a in 1:N - s += Float64(a) + function sum_first_N_float64_inbounds(N) + s = Float64(0) + @inbounds for a in 1:N + s += Float64(a) + end + s end - s + _, path = compile(sum_first_N_float64_inbounds, (Int,)) + @test remote_load_call(path, 10) == 55. end - _, path = compile(sum_first_N_float64_inbounds, (Int,)) - @test remote_load_call(path, 10) == 55. -end -# Arrays with different input types Int32, Int64, Float32, Float64, Complex? -@testset "Arrays" begin - function array_sum(n, A) - s = zero(eltype(A)) - for i in 1:n - s += A[i] + # Arrays with different input types Int32, Int64, Float32, Float64, Complex? + @testset "Arrays" begin + function array_sum(n, A) + s = zero(eltype(A)) + for i in 1:n + s += A[i] + end + s + end + for T ∈ (Int, Complex{Float32}, Complex{Float64}) + _, path = compile(array_sum, (Int, Vector{T})) + @test remote_load_call(path, 10, T.(1:10)) == T(55) end - s - end - for T ∈ (Int, Complex{Float32}, Complex{Float64}) - _, path = compile(array_sum, (Int, Vector{T})) - @test remote_load_call(path, 10, T.(1:10)) == T(55) end -end -@testset "Array allocations" begin - function f(N) - v = Vector{Float64}(undef, N) - for i ∈ eachindex(v) - v[i] = i*i + @testset "Array allocations" begin + function f(N) + v = Vector{Float64}(undef, N) + for i ∈ eachindex(v) + v[i] = i*i + end + v end - v + _, path = compile(f, (Int,)) + @test remote_load_call(path, 5) == [1.0, 4.0, 9.0, 16.0, 25.0] end - _, path = compile(f, (Int,)) - @test remote_load_call(path, 5) == [1.0, 4.0, 9.0, 16.0, 25.0] -end -# This is also a good test of loading and storing from the same object -@testset "Load & Store Same object" begin - global const x = Ref(0) - counter() = x[] += 1 - _, path = compile(counter, ()) - @spawnat 2 global counter = load_function(path) - @test fetch(@spawnat 2 counter()) == 1 - @test fetch(@spawnat 2 counter()) == 2 -end - -# This is also a good test of loading and storing from the same object -counter = let x = Ref(0) - () -> x[] += 1 -end -@testset "Closures" begin - #this currently segfaults during compilation - @test_skip begin + # This is also a good test of loading and storing from the same object + @testset "Load & Store Same object" begin + global const x = Ref(0) + counter() = x[] += 1 _, path = compile(counter, ()) - @spawnat 2 global counter_comp = load_function(path) - @test fetch(@spawnat 2 counter_comp()) == 1 - @test fetch(@spawnat 2 counter_comp()) == 2 + @spawnat 2 global counter = load_function(path) + @test fetch(@spawnat 2 counter()) == 1 + @test fetch(@spawnat 2 counter()) == 2 end -end - -@testset "Error handling" begin - _, path = compile(sqrt, (Int,)) - tsk = @spawnat 2 begin - try - load_function(path)(-1) - catch e; - e + # This is also a good test of loading and storing from the same object + counter = let x = Ref(0) + () -> x[] += 1 + end + @testset "Closures" begin + #this currently segfaults during compilation + @test_skip begin + _, path = compile(counter, ()) + @spawnat 2 global counter_comp = load_function(path) + @test fetch(@spawnat 2 counter_comp()) == 1 + @test fetch(@spawnat 2 counter_comp()) == 2 end end - @test fetch(tsk) isa DomainError -end -# Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. -# We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function. -# The interface made in `compile` should handle this fine. -@testset "Send and receive Tuple" begin - foo(u::Tuple) = 2 .* reverse(u) .- 1 - _, path = compile(foo, (NTuple{3, Int},)) - @test remote_load_call(path, (1, 2, 3)) == (5, 3, 1) -end + @testset "Error handling" begin + _, path = compile(sqrt, (Int,)) + tsk = @spawnat 2 begin + try + load_function(path)(-1) + catch e; + e + end + end + @test fetch(tsk) isa DomainError + end + # Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. + # We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function. + # The interface made in `compile` should handle this fine. + @testset "Send and receive Tuple" begin + foo(u::Tuple) = 2 .* reverse(u) .- 1 -# Just to call external libraries -@testset "BLAS" begin - function mydot(a::Vector{Float64}) - N = length(a) - BLAS.dot(N, a, 1, a, 1) - end - a = [1.0, 2.0] - - mydot_compiled, path = compile(mydot, (Vector{Float64},)) - # Works locally for me, but not on CI. Need some improvements to pointer relocation to be robust. - @test_skip remote_load_call(path, a) == 5.0 - @test mydot_compiled(a) ≈ 5.0 - - # This will need some more work apparently - @test_skip begin - _, path = compile((*), (Matrix{Float64}, Matrix{Float64})) - A, B = rand(10, 11), rand(11, 12) - @test remote_load_call(path, A, B) ≈ A * B + _, path = compile(foo, (NTuple{3, Int},)) + @test remote_load_call(path, (1, 2, 3)) == (5, 3, 1) end -end -@testset "Strings" begin - function hello(name) - "Hello, " * name * "!" + # Just to call external libraries + @testset "BLAS" begin + function mydot(a::Vector{Float64}) + N = length(a) + BLAS.dot(N, a, 1, a, 1) + end + a = [1.0, 2.0] + + mydot_compiled, path = compile(mydot, (Vector{Float64},)) + # Works locally for me, but not on CI. Need some improvements to pointer relocation to be robust. + @test_skip remote_load_call(path, a) == 5.0 + @test mydot_compiled(a) ≈ 5.0 + + # This will need some more work apparently + @test_skip begin + _, path = compile((*), (Matrix{Float64}, Matrix{Float64})) + A, B = rand(10, 11), rand(11, 12) + @test remote_load_call(path, A, B) ≈ A * B + end end - hello_compiled, path = compile(hello, (String,)) - @test remote_load_call(path, "world") == "Hello, world!" -end -@testset "Hello World" begin - function hello(N) - println("Hello World $N") - N - end - # We'll need to be able to relocate a bunch of UV stuff for this, and deal with dynamic dispatch. - @test_skip begin - hello_compiled, path = compile(hello, (Int,)) - @test_skip remote_load_call(path, 1) == 1 - end -end -# This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). -# This lets us have intermediate, mutable stack allocated arrays inside our -@testset "Alloca" begin - function f(N) - # this can hold at most 100 Int values, if you use it for more, you'll segfault - buf = ManualMemory.MemoryBuffer{100, Int}(undef) - GC.@preserve buf begin - # wrap the first N values in a PtrArray - arr = PtrArray(pointer(buf), (N,)) - arr .= 1 # mutate the array to be all 1s - sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body + @testset "Strings" begin + function hello(name) + "Hello, " * name * "!" end + hello_compiled, path = compile(hello, (String,)) + @test remote_load_call(path, "world") == "Hello, world!" end - _, path = compile(f, (Int,)) - @test remote_load_call(path, 20) == 20 -end -# I can't beleive this works. -@testset "LoopVectorization" begin - function mul!(C, A, B) - # note: @tturbo does NOT work - @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) - Cmn = zero(eltype(C)) - for k ∈ indices((A,B), (2,1)) - Cmn += A[m,k] * B[k,n] - end - C[m,n] = Cmn + @testset "Hello World" begin + function hello(N) + println("Hello World $N") + N + end + # We'll need to be able to relocate a bunch of UV stuff for this, and deal with dynamic dispatch. + @test_skip begin + hello_compiled, path = compile(hello, (Int,)) + @test_skip remote_load_call(path, 1) == 1 end end - C = Array{Float64}(undef, 10, 12) - A = rand(10, 11) - B = rand(11, 12) - - _, path = compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},)) - # remote_load_call(path, C, A, B) This won't work because @spawnat copies C - C .= fetch(@spawnat 2 (load_function(path)(C, A, B); C)) - @test C ≈ A*B -end - -@testset "Standalone Dylibs" begin - # Test function - # (already defined) - # fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) - - #Compile dylib - name = repr(fib) - filepath = compile_shlib(fib, (Int,), "./", name) - @test occursin("fib.$(Libdl.dlext)", filepath) - - # Open dylib - ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "julia_$name") - @test fptr != C_NULL - @test ccall(fptr, Int, (Int,), 10) == 55 -end - - -@testset "Standalone Executables" begin - # Minimal test with no `llvmcall` - @inline function foo() - v = 0.0 - n = 1000 - for i=1:n - v += sqrt(n) + # This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). + # This lets us have intermediate, mutable stack allocated arrays inside our + @testset "Alloca" begin + function f(N) + # this can hold at most 100 Int values, if you use it for more, you'll segfault + buf = ManualMemory.MemoryBuffer{100, Int}(undef) + GC.@preserve buf begin + # wrap the first N values in a PtrArray + arr = PtrArray(pointer(buf), (N,)) + arr .= 1 # mutate the array to be all 1s + sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body + end end - return 0 + _, path = compile(f, (Int,)) + @test remote_load_call(path, 20) == 20 end - filepath = compile_executable(foo, (), tempdir()) + # I can't beleive this works. + @testset "LoopVectorization" begin + function mul!(C, A, B) + # note: @tturbo does NOT work + @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) + Cmn = zero(eltype(C)) + for k ∈ indices((A,B), (2,1)) + Cmn += A[m,k] * B[k,n] + end + C[m,n] = Cmn + end + end - r = run(`$filepath`); - @test isa(r, Base.Process) - @test r.exitcode == 0 + C = Array{Float64}(undef, 10, 12) + A = rand(10, 11) + B = rand(11, 12) - @static if VERSION>v"1.8.0-DEV" # The llvmcall here only works on 1.8+ - @inline function puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates - Base.llvmcall((""" - ; External declaration of the puts function - declare i32 @puts(i8* nocapture) nounwind + _, path = compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},)) + # remote_load_call(path, C, A, B) This won't work because @spawnat copies C + C .= fetch(@spawnat 2 (load_function(path)(C, A, B); C)) + @test C ≈ A*B + end - define i32 @main(i8*) { - entry: - %call = call i32 (i8*) @puts(i8* %0) - ret i32 0 - } - """, "main"), Int32, Tuple{Ptr{UInt8}}, s) - end + @testset "Standalone Dylibs" begin + # Test function + # (already defined) + # fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) + + #Compile dylib + name = repr(fib) + filepath = compile_shlib(fib, (Int,), "./", name) + @test occursin("fib.$(Libdl.dlext)", filepath) + + # Open dylib + ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_$name") + @test fptr != C_NULL + @test ccall(fptr, Int, (Int,), 10) == 55 + end - @inline function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) - for i=1:argc - # Get pointer - p = unsafe_load(argv, i) - # Print string at pointer location (which fortunately already exists isn't tracked by the GC) - puts(p) + @testset "Standalone Executables" begin + # Minimal test with no `llvmcall` + @inline function foo() + v = 0.0 + n = 1000 + for i=1:n + v += sqrt(n) end return 0 end - filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), tempdir()) + filepath = compile_executable(foo, (), tempdir()) - r = run(`$filepath Hello, world!`); + r = run(`$filepath`); @test isa(r, Base.Process) @test r.exitcode == 0 + + @static if VERSION>v"1.8.0-DEV" # The llvmcall here only works on 1.8+ + @inline function _puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates + Base.llvmcall((""" + ; External declaration of the puts function + declare i32 @puts(i8* nocapture) nounwind + + define i32 @main(i8*) { + entry: + %call = call i32 (i8*) @puts(i8* %0) + ret i32 0 + } + """, "main"), Int32, Tuple{Ptr{UInt8}}, s) + end + + @inline function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) + for i=1:argc + # Get pointer + p = unsafe_load(argv, i) + # Print string at pointer location (which fortunately already exists isn't tracked by the GC) + _puts(p) + end + return 0 + end + + filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), tempdir()) + + r = run(`$filepath Hello, world!`); + @test isa(r, Base.Process) + @test r.exitcode == 0 + end end + end +@static if GROUP == "Integration" || GROUP == "All" + using LoopVectorization + using StaticTools + + @testset "Standalone Executable Integration" begin + # Setup + testpath = pwd() + scratch = tempdir() + cd(scratch) + + # --- Times table, file IO, mallocarray + + # Compile... + # We have to start a new Julia process to get around the fact that Pkg.test + # disables `@inbounds`, but ironically we can use `--compile=min` to make that + # faster. + status = run(`julia --compile=min $testpath/scripts/times_table.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Attempt to run + println("5x5 times table:") + status = run(`./times_table 5 5`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + @test parsedlm(Int64, c"table.tsv", '\t') == (1:5)*(1:5)' + + # --- Random number generation + + # Compile... + status = run(`julia --compile=min $testpath/scripts/rand_matrix.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Run... + println("5x5 random matrix:") + status = run(`./rand_matrix 5 5`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # --- Test LoopVectorization integration + + # Compile... + status = run(`julia --compile=min $testpath/scripts/loopvec_product.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Run... + println("10x10 table sum:") + status = run(`./loopvec_product 10 10`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + @test parsedlm(c"product.tsv",'\t')[] == 3025 + + # Compile... + status = run(`julia --compile=min $testpath/scripts/loopvec_matrix.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Run... + println("10x3 matrix product:") + status = run(`./loopvec_matrix 10 3`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + A = (1:10) * (1:3)' + @test parsedlm(c"table.tsv",'\t') == A' * A + + # --- Test string handling + + # Compile... + status = run(`julia --compile=min $testpath/scripts/print_args.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Run... + println("String indexing and handling:") + status = run(`./print_args foo bar`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # --- Clean up + cd(testpath) -# data structures, dictionaries, tuples, named tuples + end +end diff --git a/test/scripts/loopvec_matrix.jl b/test/scripts/loopvec_matrix.jl new file mode 100644 index 00000000..5477dd37 --- /dev/null +++ b/test/scripts/loopvec_matrix.jl @@ -0,0 +1,54 @@ +using StaticCompiler +using StaticTools +using LoopVectorization + +@inline function mul!(C::MallocArray, A::MallocArray, B::MallocArray) + @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) + Cmn = zero(eltype(C)) + for k ∈ indices((A,B), (2,1)) + Cmn += A[m,k] * B[k,n] + end + C[m,n] = Cmn + end + return C +end + +function loopvec_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), c"Incorrect number of command-line arguments\n") + rows = parse(Int64, argv, 2) # First command-line argument + cols = parse(Int64, argv, 3) # Second command-line argument + + # LHS + A = MallocArray{Float64}(undef, rows, cols) + @turbo for i ∈ axes(A, 1) + for j ∈ axes(A, 2) + A[i,j] = i*j + end + end + + # RHS + B = MallocArray{Float64}(undef, cols, rows) + @turbo for i ∈ axes(B, 1) + for j ∈ axes(B, 2) + B[i,j] = i*j + end + end + + # # Matrix multiplication + C = MallocArray{Float64}(undef, cols, cols) + mul!(C, B, A) + + # Print to stdout + printf(C) + # Also print to file + fp = fopen(c"table.tsv",c"w") + printf(fp, C) + fclose(fp) + # Clean up matrices + free(A) + free(B) + free(C) +end + +# Attempt to compile +path = compile_executable(loopvec_matrix, (Int64, Ptr{Ptr{UInt8}}), "./") diff --git a/test/scripts/loopvec_product.jl b/test/scripts/loopvec_product.jl new file mode 100644 index 00000000..bffa69c2 --- /dev/null +++ b/test/scripts/loopvec_product.jl @@ -0,0 +1,25 @@ +using StaticCompiler +using StaticTools +using LoopVectorization + +function loopvec_product(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), c"Incorrect number of command-line arguments\n") + rows = parse(Int64, argv, 2) # First command-line argument + cols = parse(Int64, argv, 3) # Second command-line argument + + s = 0 + @turbo for i=1:rows + for j=1:cols + s += i*j + end + end + # Print result to stdout + printf(s) + # Also print to file + fp = fopen(c"product.tsv",c"w") + printf(fp, s) + fclose(fp) +end + +# Attempt to compile +path = compile_executable(loopvec_product, (Int64, Ptr{Ptr{UInt8}}), "./") diff --git a/test/scripts/print_args.jl b/test/scripts/print_args.jl new file mode 100644 index 00000000..c3d05fbf --- /dev/null +++ b/test/scripts/print_args.jl @@ -0,0 +1,33 @@ +using StaticCompiler +using StaticTools + +function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) + printf(c"Argument count is %d:\n", argc) + for i=1:argc + # iᵗʰ input argument string + pᵢ = unsafe_load(argv, i) # Get pointer + strᵢ = MallocString(pᵢ) # Can wrap to get high-level interface + println(strᵢ) + # No need to `free` since we didn't allocate this memory + end + newline() + println(c"Testing string indexing and substitution") + m = m"Hello world!" + println(m[1:5]) + println(m) + m[7:11] = c"there" + println(m) + free(m) + + s = m"Hello world!" + println(s[1:5]) + println(s) + s[7:11] = c"there" + println(s) + + println(c"That was fun, see you next time!") + return 0 +end + +# Attempt to compile +path = compile_executable(print_args, (Int64, Ptr{Ptr{UInt8}}), "./") diff --git a/test/scripts/rand_matrix.jl b/test/scripts/rand_matrix.jl new file mode 100644 index 00000000..ce9fe461 --- /dev/null +++ b/test/scripts/rand_matrix.jl @@ -0,0 +1,21 @@ +using StaticCompiler +using StaticTools + +function rand_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), c"Incorrect number of command-line arguments\n") + rows = parse(Int64, argv, 2) # First command-line argument + cols = parse(Int64, argv, 3) # Second command-line argument + + M = MallocArray{Float64}(undef, rows, cols) + rng = static_rng() + @inbounds for i=1:rows + for j=1:cols + M[i,j] = rand(rng) + end + end + printf(M) + free(M) +end + +# Attempt to compile +path = compile_executable(rand_matrix, (Int64, Ptr{Ptr{UInt8}}), "./") diff --git a/test/scripts/times_table.jl b/test/scripts/times_table.jl new file mode 100644 index 00000000..2dab3937 --- /dev/null +++ b/test/scripts/times_table.jl @@ -0,0 +1,26 @@ +using StaticCompiler +using StaticTools + +function times_table(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), c"Incorrect number of command-line arguments\n") + rows = parse(Int64, argv, 2) # First command-line argument + cols = parse(Int64, argv, 3) # Second command-line argument + + M = MallocArray{Int64}(undef, rows, cols) + @inbounds for i=1:rows + for j=1:cols + M[i,j] = i*j + end + end + # Print to stdout + printf(M) + # Also print to file + fp = fopen(c"table.tsv",c"w") + printf(fp, M) + fclose(fp) + # Clean up matrix + free(M) +end + +# Attempt to compile +path = compile_executable(times_table, (Int64, Ptr{Ptr{UInt8}}), "./") From a20c6fec2946059fa399476ba2b60024b9745215 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Sun, 22 May 2022 02:01:01 -0400 Subject: [PATCH 069/202] Reorganize tests, clean up --- .gitignore | 1 + Project.toml | 7 - codecov.yml | 12 ++ test/Manifest.toml | 8 +- test/Project.toml | 1 - test/runtests.jl | 399 ++-------------------------------------- test/testcore.jl | 294 +++++++++++++++++++++++++++++ test/testintegration.jl | 80 ++++++++ 8 files changed, 400 insertions(+), 402 deletions(-) create mode 100644 codecov.yml create mode 100644 test/testcore.jl create mode 100644 test/testintegration.jl diff --git a/.gitignore b/.gitignore index 6e716b01..872875b3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ /dev/ /test/standalone /test/test.* +Manifest.toml test.o test.so diff --git a/Project.toml b/Project.toml index 2c8d2900..467c82d0 100644 --- a/Project.toml +++ b/Project.toml @@ -14,10 +14,3 @@ Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" GPUCompiler = "0.13" LLVM = "4.8" julia = "1.7" - -[extras] -Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[targets] -test = ["Test", "Formatting"] diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 00000000..3ddada3f --- /dev/null +++ b/codecov.yml @@ -0,0 +1,12 @@ +comment: false + +coverage: + status: + project: + default: + threshold: 1% + target: 1% + patch: + default: + threshold: 1% + target: 1% diff --git a/test/Manifest.toml b/test/Manifest.toml index 2031c1d0..067ac978 100644 --- a/test/Manifest.toml +++ b/test/Manifest.toml @@ -2,7 +2,7 @@ julia_version = "1.8.0-beta3" manifest_format = "2.0" -project_hash = "429ef1d09132391cc32bc872843d4edc99133ef5" +project_hash = "45548a8fc84942c91e0e727fe14ec23674d9246e" [[deps.Adapt]] deps = ["LinearAlgebra"] @@ -122,12 +122,6 @@ version = "1.6.0" [[deps.FileWatching]] uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" -[[deps.Formatting]] -deps = ["Printf"] -git-tree-sha1 = "8339d61043228fdd3eb658d86c926cb282ae72a8" -uuid = "59287772-0a20-5a39-b81b-1366585eb4c0" -version = "0.4.2" - [[deps.ForwardDiff]] deps = ["CommonSubexpressions", "DiffResults", "DiffRules", "LinearAlgebra", "LogExpFunctions", "NaNMath", "Preferences", "Printf", "Random", "SpecialFunctions", "StaticArrays"] git-tree-sha1 = "2f18915445b248731ec5db4e4a17e451020bf21e" diff --git a/test/Project.toml b/test/Project.toml index b95846e6..cbcf73f0 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,6 +1,5 @@ [deps] Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" -Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/test/runtests.jl b/test/runtests.jl index 6b19c410..67838d9f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,396 +1,21 @@ using Test using StaticCompiler +using Libdl +using LinearAlgebra +using LoopVectorization +using ManualMemory +using Distributed +using StaticTools +using StrideArraysCore +addprocs(1) +@everywhere using StaticCompiler, StrideArraysCore + const GROUP = get(ENV, "GROUP", "All") @static if GROUP == "Core" || GROUP == "All" - using Libdl - using LinearAlgebra - using LoopVectorization - using ManualMemory - using Distributed - using StrideArraysCore - addprocs(1) - @everywhere using StaticCompiler, StrideArraysCore - - remote_load_call(path, args...) = fetch(@spawnat 2 load_function(path)(args...)) - - @testset "Basics" begin - - simple_sum(x) = x + one(typeof(x)) - - # This probably needs a macro - for T ∈ (Int, Float64, Int32, Float32, Int16, Float16) - _, path, = compile(simple_sum, (T,)) - @test remote_load_call(path, T(1)) == T(2) - end - end - - - fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 - - @testset "Recursion" begin - _, path = compile(fib, (Int,)) - @test remote_load_call(path, 10) == fib(10) - - # Trick to work around #40990 - _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) - fib2(n) = _fib2(_fib2, n) - - _, path = compile(fib2, (Int,)) - @test remote_load_call(path, 20) == fib(20) - #@test compile(fib2, (Int,))[1](20) == fib(20) - end - - - @testset "Loops" begin - function sum_first_N_int(N) - s = 0 - for a in 1:N - s += a - end - s - end - _, path = compile(sum_first_N_int, (Int,)) - @test remote_load_call(path, 10) == 55 - - function sum_first_N_float64(N) - s = Float64(0) - for a in 1:N - s += Float64(a) - end - s - end - _, path = compile(sum_first_N_float64, (Int,)) - @test remote_load_call(path, 10) == 55. - - function sum_first_N_int_inbounds(N) - s = 0 - @inbounds for a in 1:N - s += a - end - s - end - _, path = compile(sum_first_N_int_inbounds, (Int,)) - @test remote_load_call(path, 10) == 55 - - function sum_first_N_float64_inbounds(N) - s = Float64(0) - @inbounds for a in 1:N - s += Float64(a) - end - s - end - _, path = compile(sum_first_N_float64_inbounds, (Int,)) - @test remote_load_call(path, 10) == 55. - end - - # Arrays with different input types Int32, Int64, Float32, Float64, Complex? - @testset "Arrays" begin - function array_sum(n, A) - s = zero(eltype(A)) - for i in 1:n - s += A[i] - end - s - end - for T ∈ (Int, Complex{Float32}, Complex{Float64}) - _, path = compile(array_sum, (Int, Vector{T})) - @test remote_load_call(path, 10, T.(1:10)) == T(55) - end - end - - @testset "Array allocations" begin - function f(N) - v = Vector{Float64}(undef, N) - for i ∈ eachindex(v) - v[i] = i*i - end - v - end - _, path = compile(f, (Int,)) - @test remote_load_call(path, 5) == [1.0, 4.0, 9.0, 16.0, 25.0] - end - - # This is also a good test of loading and storing from the same object - @testset "Load & Store Same object" begin - global const x = Ref(0) - counter() = x[] += 1 - _, path = compile(counter, ()) - @spawnat 2 global counter = load_function(path) - @test fetch(@spawnat 2 counter()) == 1 - @test fetch(@spawnat 2 counter()) == 2 - end - - # This is also a good test of loading and storing from the same object - counter = let x = Ref(0) - () -> x[] += 1 - end - @testset "Closures" begin - #this currently segfaults during compilation - @test_skip begin - _, path = compile(counter, ()) - @spawnat 2 global counter_comp = load_function(path) - @test fetch(@spawnat 2 counter_comp()) == 1 - @test fetch(@spawnat 2 counter_comp()) == 2 - end - end - - - @testset "Error handling" begin - _, path = compile(sqrt, (Int,)) - tsk = @spawnat 2 begin - try - load_function(path)(-1) - catch e; - e - end - end - @test fetch(tsk) isa DomainError - end - - # Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. - # We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function. - # The interface made in `compile` should handle this fine. - @testset "Send and receive Tuple" begin - foo(u::Tuple) = 2 .* reverse(u) .- 1 - - _, path = compile(foo, (NTuple{3, Int},)) - @test remote_load_call(path, (1, 2, 3)) == (5, 3, 1) - end - - - # Just to call external libraries - @testset "BLAS" begin - function mydot(a::Vector{Float64}) - N = length(a) - BLAS.dot(N, a, 1, a, 1) - end - a = [1.0, 2.0] - - mydot_compiled, path = compile(mydot, (Vector{Float64},)) - # Works locally for me, but not on CI. Need some improvements to pointer relocation to be robust. - @test_skip remote_load_call(path, a) == 5.0 - @test mydot_compiled(a) ≈ 5.0 - - # This will need some more work apparently - @test_skip begin - _, path = compile((*), (Matrix{Float64}, Matrix{Float64})) - A, B = rand(10, 11), rand(11, 12) - @test remote_load_call(path, A, B) ≈ A * B - end - end - - - @testset "Strings" begin - function hello(name) - "Hello, " * name * "!" - end - hello_compiled, path = compile(hello, (String,)) - @test remote_load_call(path, "world") == "Hello, world!" - end - - @testset "Hello World" begin - function hello(N) - println("Hello World $N") - N - end - # We'll need to be able to relocate a bunch of UV stuff for this, and deal with dynamic dispatch. - @test_skip begin - hello_compiled, path = compile(hello, (Int,)) - @test_skip remote_load_call(path, 1) == 1 - end - end - - # This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). - # This lets us have intermediate, mutable stack allocated arrays inside our - @testset "Alloca" begin - function f(N) - # this can hold at most 100 Int values, if you use it for more, you'll segfault - buf = ManualMemory.MemoryBuffer{100, Int}(undef) - GC.@preserve buf begin - # wrap the first N values in a PtrArray - arr = PtrArray(pointer(buf), (N,)) - arr .= 1 # mutate the array to be all 1s - sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body - end - end - _, path = compile(f, (Int,)) - @test remote_load_call(path, 20) == 20 - end - - # I can't beleive this works. - @testset "LoopVectorization" begin - function mul!(C, A, B) - # note: @tturbo does NOT work - @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) - Cmn = zero(eltype(C)) - for k ∈ indices((A,B), (2,1)) - Cmn += A[m,k] * B[k,n] - end - C[m,n] = Cmn - end - end - - C = Array{Float64}(undef, 10, 12) - A = rand(10, 11) - B = rand(11, 12) - - _, path = compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},)) - # remote_load_call(path, C, A, B) This won't work because @spawnat copies C - C .= fetch(@spawnat 2 (load_function(path)(C, A, B); C)) - @test C ≈ A*B - end - - @testset "Standalone Dylibs" begin - # Test function - # (already defined) - # fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) - - #Compile dylib - name = repr(fib) - filepath = compile_shlib(fib, (Int,), "./", name) - @test occursin("fib.$(Libdl.dlext)", filepath) - - # Open dylib - ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "julia_$name") - @test fptr != C_NULL - @test ccall(fptr, Int, (Int,), 10) == 55 - end - - @testset "Standalone Executables" begin - # Minimal test with no `llvmcall` - @inline function foo() - v = 0.0 - n = 1000 - for i=1:n - v += sqrt(n) - end - return 0 - end - - filepath = compile_executable(foo, (), tempdir()) - - r = run(`$filepath`); - @test isa(r, Base.Process) - @test r.exitcode == 0 - - @static if VERSION>v"1.8.0-DEV" # The llvmcall here only works on 1.8+ - @inline function _puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates - Base.llvmcall((""" - ; External declaration of the puts function - declare i32 @puts(i8* nocapture) nounwind - - define i32 @main(i8*) { - entry: - %call = call i32 (i8*) @puts(i8* %0) - ret i32 0 - } - """, "main"), Int32, Tuple{Ptr{UInt8}}, s) - end - - @inline function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) - for i=1:argc - # Get pointer - p = unsafe_load(argv, i) - # Print string at pointer location (which fortunately already exists isn't tracked by the GC) - _puts(p) - end - return 0 - end - - filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), tempdir()) - - r = run(`$filepath Hello, world!`); - @test isa(r, Base.Process) - @test r.exitcode == 0 - end - end - + include("testcore.jl") end @static if GROUP == "Integration" || GROUP == "All" - using LoopVectorization - using StaticTools - - @testset "Standalone Executable Integration" begin - # Setup - testpath = pwd() - scratch = tempdir() - cd(scratch) - - # --- Times table, file IO, mallocarray - - # Compile... - # We have to start a new Julia process to get around the fact that Pkg.test - # disables `@inbounds`, but ironically we can use `--compile=min` to make that - # faster. - status = run(`julia --compile=min $testpath/scripts/times_table.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Attempt to run - println("5x5 times table:") - status = run(`./times_table 5 5`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - @test parsedlm(Int64, c"table.tsv", '\t') == (1:5)*(1:5)' - - # --- Random number generation - - # Compile... - status = run(`julia --compile=min $testpath/scripts/rand_matrix.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Run... - println("5x5 random matrix:") - status = run(`./rand_matrix 5 5`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # --- Test LoopVectorization integration - - # Compile... - status = run(`julia --compile=min $testpath/scripts/loopvec_product.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Run... - println("10x10 table sum:") - status = run(`./loopvec_product 10 10`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - @test parsedlm(c"product.tsv",'\t')[] == 3025 - - # Compile... - status = run(`julia --compile=min $testpath/scripts/loopvec_matrix.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Run... - println("10x3 matrix product:") - status = run(`./loopvec_matrix 10 3`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - A = (1:10) * (1:3)' - @test parsedlm(c"table.tsv",'\t') == A' * A - - # --- Test string handling - - # Compile... - status = run(`julia --compile=min $testpath/scripts/print_args.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Run... - println("String indexing and handling:") - status = run(`./print_args foo bar`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # --- Clean up - cd(testpath) - - end + include("testintegration.jl") end diff --git a/test/testcore.jl b/test/testcore.jl new file mode 100644 index 00000000..7f12d016 --- /dev/null +++ b/test/testcore.jl @@ -0,0 +1,294 @@ +remote_load_call(path, args...) = fetch(@spawnat 2 load_function(path)(args...)) + +@testset "Basics" begin + + simple_sum(x) = x + one(typeof(x)) + + # This probably needs a macro + for T ∈ (Int, Float64, Int32, Float32, Int16, Float16) + _, path, = compile(simple_sum, (T,)) + @test remote_load_call(path, T(1)) == T(2) + end +end + + +fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 + +@testset "Recursion" begin + _, path = compile(fib, (Int,)) + @test remote_load_call(path, 10) == fib(10) + + # Trick to work around #40990 + _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) + fib2(n) = _fib2(_fib2, n) + + _, path = compile(fib2, (Int,)) + @test remote_load_call(path, 20) == fib(20) + #@test compile(fib2, (Int,))[1](20) == fib(20) +end + + +@testset "Loops" begin + function sum_first_N_int(N) + s = 0 + for a in 1:N + s += a + end + s + end + _, path = compile(sum_first_N_int, (Int,)) + @test remote_load_call(path, 10) == 55 + + function sum_first_N_float64(N) + s = Float64(0) + for a in 1:N + s += Float64(a) + end + s + end + _, path = compile(sum_first_N_float64, (Int,)) + @test remote_load_call(path, 10) == 55. + + function sum_first_N_int_inbounds(N) + s = 0 + @inbounds for a in 1:N + s += a + end + s + end + _, path = compile(sum_first_N_int_inbounds, (Int,)) + @test remote_load_call(path, 10) == 55 + + function sum_first_N_float64_inbounds(N) + s = Float64(0) + @inbounds for a in 1:N + s += Float64(a) + end + s + end + _, path = compile(sum_first_N_float64_inbounds, (Int,)) + @test remote_load_call(path, 10) == 55. +end + +# Arrays with different input types Int32, Int64, Float32, Float64, Complex? +@testset "Arrays" begin + function array_sum(n, A) + s = zero(eltype(A)) + for i in 1:n + s += A[i] + end + s + end + for T ∈ (Int, Complex{Float32}, Complex{Float64}) + _, path = compile(array_sum, (Int, Vector{T})) + @test remote_load_call(path, 10, T.(1:10)) == T(55) + end +end + +@testset "Array allocations" begin + function f(N) + v = Vector{Float64}(undef, N) + for i ∈ eachindex(v) + v[i] = i*i + end + v + end + _, path = compile(f, (Int,)) + @test remote_load_call(path, 5) == [1.0, 4.0, 9.0, 16.0, 25.0] +end + +# This is also a good test of loading and storing from the same object +@testset "Load & Store Same object" begin + global const x = Ref(0) + counter() = x[] += 1 + _, path = compile(counter, ()) + @spawnat 2 global counter = load_function(path) + @test fetch(@spawnat 2 counter()) == 1 + @test fetch(@spawnat 2 counter()) == 2 +end + +# This is also a good test of loading and storing from the same object +counter = let x = Ref(0) + () -> x[] += 1 +end +@testset "Closures" begin + #this currently segfaults during compilation + @test_skip begin + _, path = compile(counter, ()) + @spawnat 2 global counter_comp = load_function(path) + @test fetch(@spawnat 2 counter_comp()) == 1 + @test fetch(@spawnat 2 counter_comp()) == 2 + end +end + + +@testset "Error handling" begin + _, path = compile(sqrt, (Int,)) + tsk = @spawnat 2 begin + try + load_function(path)(-1) + catch e; + e + end + end + @test fetch(tsk) isa DomainError +end + +# Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. +# We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function. +# The interface made in `compile` should handle this fine. +@testset "Send and receive Tuple" begin + foo(u::Tuple) = 2 .* reverse(u) .- 1 + + _, path = compile(foo, (NTuple{3, Int},)) + @test remote_load_call(path, (1, 2, 3)) == (5, 3, 1) +end + + +# Just to call external libraries +@testset "BLAS" begin + function mydot(a::Vector{Float64}) + N = length(a) + BLAS.dot(N, a, 1, a, 1) + end + a = [1.0, 2.0] + + mydot_compiled, path = compile(mydot, (Vector{Float64},)) + # Works locally for me, but not on CI. Need some improvements to pointer relocation to be robust. + @test_skip remote_load_call(path, a) == 5.0 + @test mydot_compiled(a) ≈ 5.0 + + # This will need some more work apparently + @test_skip begin + _, path = compile((*), (Matrix{Float64}, Matrix{Float64})) + A, B = rand(10, 11), rand(11, 12) + @test remote_load_call(path, A, B) ≈ A * B + end +end + + +@testset "Strings" begin + function hello(name) + "Hello, " * name * "!" + end + hello_compiled, path = compile(hello, (String,)) + @test remote_load_call(path, "world") == "Hello, world!" +end + +@testset "Hello World" begin + function hello(N) + println("Hello World $N") + N + end + # We'll need to be able to relocate a bunch of UV stuff for this, and deal with dynamic dispatch. + @test_skip begin + hello_compiled, path = compile(hello, (Int,)) + @test_skip remote_load_call(path, 1) == 1 + end +end + +# This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). +# This lets us have intermediate, mutable stack allocated arrays inside our +@testset "Alloca" begin + function f(N) + # this can hold at most 100 Int values, if you use it for more, you'll segfault + buf = ManualMemory.MemoryBuffer{100, Int}(undef) + GC.@preserve buf begin + # wrap the first N values in a PtrArray + arr = PtrArray(pointer(buf), (N,)) + arr .= 1 # mutate the array to be all 1s + sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body + end + end + _, path = compile(f, (Int,)) + @test remote_load_call(path, 20) == 20 +end + +# I can't beleive this works. +@testset "LoopVectorization" begin + function mul!(C, A, B) + # note: @tturbo does NOT work + @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) + Cmn = zero(eltype(C)) + for k ∈ indices((A,B), (2,1)) + Cmn += A[m,k] * B[k,n] + end + C[m,n] = Cmn + end + end + + C = Array{Float64}(undef, 10, 12) + A = rand(10, 11) + B = rand(11, 12) + + _, path = compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},)) + # remote_load_call(path, C, A, B) This won't work because @spawnat copies C + C .= fetch(@spawnat 2 (load_function(path)(C, A, B); C)) + @test C ≈ A*B +end + +@testset "Standalone Dylibs" begin + # Test function + # (already defined) + # fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) + + #Compile dylib + name = repr(fib) + filepath = compile_shlib(fib, (Int,), "./", name) + @test occursin("fib.$(Libdl.dlext)", filepath) + + # Open dylib + ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_$name") + @test fptr != C_NULL + @test ccall(fptr, Int, (Int,), 10) == 55 +end + +@testset "Standalone Executables" begin + # Minimal test with no `llvmcall` + @inline function foo() + v = 0.0 + n = 1000 + for i=1:n + v += sqrt(n) + end + return 0 + end + + filepath = compile_executable(foo, (), tempdir()) + + 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((""" + ; External declaration of the puts function + declare i32 @puts(i8* nocapture) nounwind + + define i32 @main(i64) { + entry: + %ptr = inttoptr i64 %0 to i8* + %status = call i32 (i8*) @puts(i8* %ptr) + ret i32 %status + } + """, "main"), Int32, Tuple{Ptr{UInt8}}, s) + end + + @inline function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) + for i=1:argc + # Get pointer + p = unsafe_load(argv, i) + # Print string at pointer location (which fortunately already exists isn't tracked by the GC) + _puts(p) + end + return 0 + end + + filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), tempdir()) + + r = run(`$filepath Hello, world!`); + @test isa(r, Base.Process) + @test r.exitcode == 0 +end diff --git a/test/testintegration.jl b/test/testintegration.jl new file mode 100644 index 00000000..13141c84 --- /dev/null +++ b/test/testintegration.jl @@ -0,0 +1,80 @@ + +@testset "Standalone Executable Integration" begin + + testpath = pwd() + scratch = tempdir() + cd(scratch) + + # --- Times table, file IO, mallocarray + + # Compile... + # We have to start a new Julia process to get around the fact that Pkg.test + # disables `@inbounds`, but ironically we can use `--compile=min` to make that + # faster. + status = run(`julia --compile=min $testpath/scripts/times_table.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Attempt to run + println("5x5 times table:") + status = run(`./times_table 5 5`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + @test parsedlm(Int64, c"table.tsv", '\t') == (1:5)*(1:5)' + + # --- Random number generation + + # Compile... + status = run(`julia --compile=min $testpath/scripts/rand_matrix.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Run... + println("5x5 random matrix:") + status = run(`./rand_matrix 5 5`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # --- Test LoopVectorization integration + + # Compile... + status = run(`julia --compile=min $testpath/scripts/loopvec_product.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Run... + println("10x10 table sum:") + status = run(`./loopvec_product 10 10`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + @test parsedlm(c"product.tsv",'\t')[] == 3025 + + # Compile... + status = run(`julia --compile=min $testpath/scripts/loopvec_matrix.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Run... + println("10x3 matrix product:") + status = run(`./loopvec_matrix 10 3`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + A = (1:10) * (1:3)' + @test parsedlm(c"table.tsv",'\t') == A' * A + + # --- Test string handling + + # Compile... + status = run(`julia --compile=min $testpath/scripts/print_args.jl`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # Run... + println("String indexing and handling:") + status = run(`./print_args foo bar`) + @test isa(status, Base.Process) + @test status.exitcode == 0 + + # --- Clean up + cd(testpath) +end From bf96341b200208d9000b8fbcbf38f1c3946ede41 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Sun, 22 May 2022 02:24:10 -0400 Subject: [PATCH 070/202] Update badges in README --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a14add02..05dd9dbe 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # StaticCompiler -[![CI](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml) -[![Codecov](https://codecov.io/gh/tshort/StaticCompiler.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/tshort/StaticCompiler.jl) +[![CI](https://github.com/tshort/StaticCompiler.jl/workflows/CI/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml) +[![CI (Integration)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Integration)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration.yml) +[![CI (Julia nightly)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Julia%20nightly)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-julia-nightly.yml) +[![Coverage](https://codecov.io/gh/tshort/StaticCompiler.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/tshort/StaticCompiler.jl) This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. @@ -45,7 +47,7 @@ marked with `@test_skip`) This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. -## Limitations +## 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` (yet). From ceea5f1227a53e5e719221c6c3aa68ffa1808adf Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Sun, 22 May 2022 02:37:58 -0400 Subject: [PATCH 071/202] Update main branch name in workflows --- .github/workflows/ci-integration.yml | 2 +- .github/workflows/ci-julia-nightly.yml | 2 +- .github/workflows/ci.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index 025f250f..287ae13b 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -7,7 +7,7 @@ on: - 'README.md' pull_request: branches: - - main + - master paths-ignore: - 'README.md' jobs: diff --git a/.github/workflows/ci-julia-nightly.yml b/.github/workflows/ci-julia-nightly.yml index 98dc7810..d01adba2 100644 --- a/.github/workflows/ci-julia-nightly.yml +++ b/.github/workflows/ci-julia-nightly.yml @@ -7,7 +7,7 @@ on: - 'README.md' pull_request: branches: - - main + - master paths-ignore: - 'README.md' jobs: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4e009549..5a6bcbb0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ on: - 'README.md' pull_request: branches: - - main + - master paths-ignore: - 'README.md' jobs: From 41ca1212b924e780363b29f9f25b5b3b7c11e373 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Mon, 30 May 2022 22:03:35 -0400 Subject: [PATCH 072/202] Update CompatHelper workflow --- .github/workflows/CompatHelper.yml | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/.github/workflows/CompatHelper.yml b/.github/workflows/CompatHelper.yml index 7c17290f..486adf2b 100644 --- a/.github/workflows/CompatHelper.yml +++ b/.github/workflows/CompatHelper.yml @@ -1,27 +1,16 @@ name: CompatHelper - on: schedule: - - cron: '0 1 * * *' - issues: - types: [opened, reopened] - + - cron: 0 0 * * 0 + workflow_dispatch: jobs: - build: - runs-on: ${{ matrix.os }} - strategy: - matrix: - julia-version: [1.3.1] - julia-arch: [x64] - os: [ubuntu-latest] + CompatHelper: + runs-on: ubuntu-latest steps: - - uses: julia-actions/setup-julia@latest - with: - version: ${{ matrix.julia-version }} - - name: Install dependencies - run: julia -e 'using Pkg; Pkg.add(Pkg.PackageSpec(name = "CompatHelper", url = "https://github.com/bcbi/CompatHelper.jl.git"))' - - name: CompatHelper.main + - name: Pkg.add("CompatHelper") + run: julia -e 'using Pkg; Pkg.add("CompatHelper")' + - name: CompatHelper.main() env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - JULIA_DEBUG: CompatHelper + COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }} run: julia -e 'using CompatHelper; CompatHelper.main()' From 1fda350f718ded85ac348096fe6be82476a22141 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Mon, 30 May 2022 23:18:16 -0400 Subject: [PATCH 073/202] Require avx2 for loopvec_product test --- test/testintegration.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/testintegration.jl b/test/testintegration.jl index 13141c84..5fd783eb 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -37,6 +37,7 @@ # --- Test LoopVectorization integration +@static if LoopVectorization.VectorizationBase.has_feature(Val{:x86_64_avx2}) # Compile... status = run(`julia --compile=min $testpath/scripts/loopvec_product.jl`) @test isa(status, Base.Process) @@ -48,6 +49,7 @@ @test isa(status, Base.Process) @test status.exitcode == 0 @test parsedlm(c"product.tsv",'\t')[] == 3025 +end # Compile... status = run(`julia --compile=min $testpath/scripts/loopvec_matrix.jl`) From e7833c2470ed452bb1f0a387ccd426b86592b197 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Wed, 1 Jun 2022 00:59:30 -0400 Subject: [PATCH 074/202] Try bumping compat bounds on GPUCompiler --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 467c82d0..b3c5af22 100644 --- a/Project.toml +++ b/Project.toml @@ -11,6 +11,6 @@ Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" [compat] -GPUCompiler = "0.13" +GPUCompiler = "0.16" LLVM = "4.8" julia = "1.7" From 60fe2aaaa49d19c7421dda6e9cdf841ebc1b892c Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Wed, 1 Jun 2022 01:13:59 -0400 Subject: [PATCH 075/202] Test integration on nightly too, why not --- .github/workflows/ci-integration-nightly.yml | 38 ++++++++++++++++++++ README.md | 1 + 2 files changed, 39 insertions(+) create mode 100644 .github/workflows/ci-integration-nightly.yml diff --git a/.github/workflows/ci-integration-nightly.yml b/.github/workflows/ci-integration-nightly.yml new file mode 100644 index 00000000..00074acc --- /dev/null +++ b/.github/workflows/ci-integration-nightly.yml @@ -0,0 +1,38 @@ +name: CI (Integration nightly) +on: + push: + branches: + - '**' + paths-ignore: + - 'README.md' + pull_request: + branches: + - master + paths-ignore: + - 'README.md' +jobs: + test-integration-nightly: + name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + version: + - 'nightly' + os: + - ubuntu-latest + - macOS-latest + arch: + - x64 + group: + - Integration + steps: + - uses: actions/checkout@v2 + - uses: julia-actions/setup-julia@latest + with: + version: ${{ matrix.version }} + arch: ${{ matrix.arch }} + - uses: julia-actions/julia-buildpkg@latest + - uses: julia-actions/julia-runtest@latest + env: + GROUP: ${{ matrix.group }} diff --git a/README.md b/README.md index 05dd9dbe..c45e1054 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![CI](https://github.com/tshort/StaticCompiler.jl/workflows/CI/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml) [![CI (Integration)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Integration)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration.yml) [![CI (Julia nightly)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Julia%20nightly)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-julia-nightly.yml) +[![CI (Integration nightly)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Integration%20nightly)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration-nightly.yml) [![Coverage](https://codecov.io/gh/tshort/StaticCompiler.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/tshort/StaticCompiler.jl) This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. From e8d6bfc7c62156715dd7f70920dbc8d59d5a7cd3 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Wed, 1 Jun 2022 03:58:38 -0400 Subject: [PATCH 076/202] Provide JuliaContext to GPUCompiler.codegen --- src/StaticCompiler.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index da4b8046..e703f47d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -141,8 +141,9 @@ function generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_ tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) job, kwargs = native_job(f, tt; name, kwargs...) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. - mod, meta = GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false) - + mod, meta = GPUCompiler.JuliaContext() do context + GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false, ctx=context) + end # Use Enzyme's annotation and optimization pipeline annotate!(mod) optimize!(mod, tm) @@ -448,7 +449,9 @@ end # Return an LLVM module function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs...) job, kwargs = native_job(f, tt; name, kwargs...) - m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + m, _ = GPUCompiler.JuliaContext() do context + GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, ctx=context) + end return m end From e9a6b6936c0d6ea31ffba793e021ea603fff18a8 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Wed, 1 Jun 2022 04:28:42 -0400 Subject: [PATCH 077/202] Don't restrict input types of `compile_executable` functions; we need to modify these to avoid problems with argv pointers in the future --- src/StaticCompiler.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index e703f47d..7249ffb3 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -231,7 +231,7 @@ function compile_executable(f, types=(), path::String="./", name=GPUCompiler.saf kwargs...) tt = Base.to_tuple_type(types) - tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})") + # tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})") rt = only(native_code_typed(f, tt))[2] isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") From 0049c88083d14d77508ed83ef374807afdc26b9a Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 1 Jun 2022 17:41:23 -0400 Subject: [PATCH 078/202] Bump version to 0.4.3 to register new version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b3c5af22..891f7f4f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short"] -version = "0.4.2" +version = "0.4.3" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" From 93bcc15fda5e7e89c8ff6fdb407e90759b67790e Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Tue, 7 Jun 2022 15:21:05 -0300 Subject: [PATCH 079/202] Check mangling --- src/StaticCompiler.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 725de2a4..1d213b65 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -459,7 +459,7 @@ function native_code_native(@nospecialize(f), @nospecialize(tt), name = GPUCompi GPUCompiler.code_native(stdout, job; kwargs...) end -#Return an LLVM module for multiple functions +#Return an LLVM module for multiple functions function native_llvm_module(funcs::Array; mangle_names = false, kwargs...) f,tt = funcs[1] mod = native_llvm_module(f,tt, kwargs...) @@ -473,7 +473,9 @@ function native_llvm_module(funcs::Array; mangle_names = false, kwargs...) if mangle_names for func in functions(mod) fname = name(func) - name!(func,fname[7:end]) + if fname[1:6] == "julia_" + name!(func,fname[7:end]) + end end end return mod @@ -498,7 +500,7 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str end function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; mangle_names=false, kwargs...) - + lib_path = joinpath(path, "$filename.$(Libdl.dlext)") _,obj_path = generate_obj(funcs, path, filename; mangle_names=mangle_names, kwargs...) From 2cf98333f5db5cb1f3f0f46331e98e6f99179061 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Tue, 7 Jun 2022 16:36:03 -0300 Subject: [PATCH 080/202] Add function deduplication and tests --- src/StaticCompiler.jl | 4 ++++ test/testcore.jl | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 1d213b65..6e5f7a85 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -478,6 +478,10 @@ function native_llvm_module(funcs::Array; mangle_names = false, kwargs...) end end end + LLVM.ModulePassManager() do pass_manager #remove duplicate functions + LLVM.merge_functions!(pass_manager) + LLVM.run!(pass_manager, mod) + end return mod end diff --git a/test/testcore.jl b/test/testcore.jl index 7f12d016..cef78dba 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -292,3 +292,29 @@ end @test isa(r, Base.Process) @test r.exitcode == 0 end + + +@testset "Multiple Function Dylibs" begin + + @noinline square(n) = n*n + + function squaresquare(n) + square(square(n)) + end + + function squaresquaresquare(n) + square(squaresquare(n)) + end + + funcs = [(squaresquare,(Float64,)), (squaresquaresquare,(Float64,))] + filepath = compile_shlib(funcs, mangle_names=true) + + ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) + + fptr2 = Libdl.dlsym(ptr, "squaresquare") + @test ccall(fptr2, Float64, (Float64,), 10.) == squaresquare(10.) + + fptr = Libdl.dlsym(ptr, "squaresquaresquare") + @test ccall(fptr2, Float64, (Float64,), 10.) == squaresquaresquare(10.) + #Compile dylib +end \ No newline at end of file From b09ded03aa6ec8b79ab8f1a0416e1d331d8797a9 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Tue, 7 Jun 2022 17:17:43 -0300 Subject: [PATCH 081/202] Fix typo and move tests outside the macro --- test/testcore.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/testcore.jl b/test/testcore.jl index cef78dba..9c54b5e4 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -293,18 +293,18 @@ end @test r.exitcode == 0 end +@noinline square(n) = n*n -@testset "Multiple Function Dylibs" begin +function squaresquare(n) + square(square(n)) +end - @noinline square(n) = n*n +function squaresquaresquare(n) + square(squaresquare(n)) +end - function squaresquare(n) - square(square(n)) - end +@testset "Multiple Function Dylibs" begin - function squaresquaresquare(n) - square(squaresquare(n)) - end funcs = [(squaresquare,(Float64,)), (squaresquaresquare,(Float64,))] filepath = compile_shlib(funcs, mangle_names=true) @@ -315,6 +315,6 @@ end @test ccall(fptr2, Float64, (Float64,), 10.) == squaresquare(10.) fptr = Libdl.dlsym(ptr, "squaresquaresquare") - @test ccall(fptr2, Float64, (Float64,), 10.) == squaresquaresquare(10.) + @test ccall(fptr, Float64, (Float64,), 10.) == squaresquaresquare(10.) #Compile dylib end \ No newline at end of file From 07a44d24e11c20db219028b00fd84fe9d2a127f4 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Tue, 7 Jun 2022 20:04:32 -0300 Subject: [PATCH 082/202] Change name to demangle --- src/StaticCompiler.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 6e5f7a85..b49d7b9d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -460,7 +460,7 @@ function native_code_native(@nospecialize(f), @nospecialize(tt), name = GPUCompi end #Return an LLVM module for multiple functions -function native_llvm_module(funcs::Array; mangle_names = false, kwargs...) +function native_llvm_module(funcs::Array; demangle = false, kwargs...) f,tt = funcs[1] mod = native_llvm_module(f,tt, kwargs...) if length(funcs) > 1 @@ -470,7 +470,7 @@ function native_llvm_module(funcs::Array; mangle_names = false, kwargs...) link!(mod,tmod) end end - if mangle_names + if demangle for func in functions(mod) fname = name(func) if fname[1:6] == "julia_" @@ -486,7 +486,7 @@ function native_llvm_module(funcs::Array; mangle_names = false, kwargs...) end function generate_obj(funcs::Array, path::String = tempname(), filenamebase::String="obj"; - mangle_names =false, + demangle =false, strip_llvm = false, strip_asm = true, opt_level=3, @@ -495,7 +495,7 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str mkpath(path) obj_path = joinpath(path, "$filenamebase.o") fakejob, kwargs = native_job(f,tt, kwargs...) - mod = native_llvm_module(funcs; mangle_names = mangle_names, kwargs...) + mod = native_llvm_module(funcs; demangle = demangle, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io write(io, obj) @@ -503,11 +503,11 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str path, obj_path end -function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; mangle_names=false, kwargs...) +function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; demangle=false, kwargs...) lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - _,obj_path = generate_obj(funcs, path, filename; mangle_names=mangle_names, kwargs...) + _,obj_path = generate_obj(funcs, path, filename; demangle=demangle, kwargs...) # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! @@ -518,7 +518,7 @@ end function compile_shlib(funcs::Array, path::String="./"; filename="libfoo", - mangle_names=false, + demangle=false, kwargs...) for func in funcs f, types = func @@ -532,7 +532,7 @@ function compile_shlib(funcs::Array, path::String="./"; # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - generate_shlib(funcs, path, filename; mangle_names=mangle_names, kwargs...) + generate_shlib(funcs, path, filename; demangle=demangle, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end From 16e047e9cd76351936565dec00490cae5f8de485 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Wed, 8 Jun 2022 17:51:08 -0300 Subject: [PATCH 083/202] Fix text --- test/testcore.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testcore.jl b/test/testcore.jl index 9c54b5e4..fe2d85ea 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -307,7 +307,7 @@ end funcs = [(squaresquare,(Float64,)), (squaresquaresquare,(Float64,))] - filepath = compile_shlib(funcs, mangle_names=true) + filepath = compile_shlib(funcs, demangle=true) ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) @@ -317,4 +317,4 @@ end fptr = Libdl.dlsym(ptr, "squaresquaresquare") @test ccall(fptr, Float64, (Float64,), 10.) == squaresquaresquare(10.) #Compile dylib -end \ No newline at end of file +end From e79b6a6ccda62e2b7b6f99949220d3f3522ce846 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sat, 11 Jun 2022 16:31:41 -0400 Subject: [PATCH 084/202] Bump version to 0.4.4 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 891f7f4f..22c22a3d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short"] -version = "0.4.3" +version = "0.4.4" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" From d183a162cc5234c0b5092f1e36778ea44bc043d4 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Sun, 17 Jul 2022 14:08:13 -0400 Subject: [PATCH 085/202] Add TOML to test dependencies to avoid weird Clang_jll error --- test/Manifest.toml | 444 --------------------------------------------- test/Project.toml | 1 + 2 files changed, 1 insertion(+), 444 deletions(-) delete mode 100644 test/Manifest.toml diff --git a/test/Manifest.toml b/test/Manifest.toml deleted file mode 100644 index 067ac978..00000000 --- a/test/Manifest.toml +++ /dev/null @@ -1,444 +0,0 @@ -# This file is machine-generated - editing it directly is not advised - -julia_version = "1.8.0-beta3" -manifest_format = "2.0" -project_hash = "45548a8fc84942c91e0e727fe14ec23674d9246e" - -[[deps.Adapt]] -deps = ["LinearAlgebra"] -git-tree-sha1 = "af92965fb30777147966f58acb05da51c5616b5f" -uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" -version = "3.3.3" - -[[deps.ArgTools]] -uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" -version = "1.1.1" - -[[deps.ArrayInterface]] -deps = ["Compat", "IfElse", "LinearAlgebra", "Requires", "SparseArrays", "Static"] -git-tree-sha1 = "81f0cb60dc994ca17f68d9fb7c942a5ae70d9ee4" -uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" -version = "5.0.8" - -[[deps.Artifacts]] -uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" - -[[deps.Base64]] -uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" - -[[deps.BitTwiddlingConvenienceFunctions]] -deps = ["Static"] -git-tree-sha1 = "28bbdbf0354959db89358d1d79d421ff31ef0b5e" -uuid = "62783981-4cbd-42fc-bca8-16325de8dc4b" -version = "0.1.3" - -[[deps.CEnum]] -git-tree-sha1 = "eb4cb44a499229b3b8426dcfb5dd85333951ff90" -uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82" -version = "0.4.2" - -[[deps.CPUSummary]] -deps = ["CpuId", "IfElse", "Static"] -git-tree-sha1 = "0eaf4aedad5ccc3e39481db55d72973f856dc564" -uuid = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9" -version = "0.1.22" - -[[deps.ChainRulesCore]] -deps = ["Compat", "LinearAlgebra", "SparseArrays"] -git-tree-sha1 = "9950387274246d08af38f6eef8cb5480862a435f" -uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" -version = "1.14.0" - -[[deps.ChangesOfVariables]] -deps = ["ChainRulesCore", "LinearAlgebra", "Test"] -git-tree-sha1 = "1e315e3f4b0b7ce40feded39c73049692126cf53" -uuid = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0" -version = "0.1.3" - -[[deps.CloseOpenIntervals]] -deps = ["ArrayInterface", "Static"] -git-tree-sha1 = "f576084239e6bdf801007c80e27e2cc2cd963fe0" -uuid = "fb6a15b2-703c-40df-9091-08a04967cfa9" -version = "0.1.6" - -[[deps.CommonSubexpressions]] -deps = ["MacroTools", "Test"] -git-tree-sha1 = "7b8a93dba8af7e3b42fecabf646260105ac373f7" -uuid = "bbf7d656-a473-5ed7-a52c-81e309532950" -version = "0.3.0" - -[[deps.Compat]] -deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] -git-tree-sha1 = "b153278a25dd42c65abbf4e62344f9d22e59191b" -uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "3.43.0" - -[[deps.CompilerSupportLibraries_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" -version = "0.5.2+0" - -[[deps.CpuId]] -deps = ["Markdown"] -git-tree-sha1 = "fcbb72b032692610bfbdb15018ac16a36cf2e406" -uuid = "adafc99b-e345-5852-983c-f28acb93d879" -version = "0.3.1" - -[[deps.Dates]] -deps = ["Printf"] -uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" - -[[deps.DelimitedFiles]] -deps = ["Mmap"] -uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" - -[[deps.DiffResults]] -deps = ["StaticArrays"] -git-tree-sha1 = "c18e98cba888c6c25d1c3b048e4b3380ca956805" -uuid = "163ba53b-c6d8-5494-b064-1a9d43ac40c5" -version = "1.0.3" - -[[deps.DiffRules]] -deps = ["IrrationalConstants", "LogExpFunctions", "NaNMath", "Random", "SpecialFunctions"] -git-tree-sha1 = "28d605d9a0ac17118fe2c5e9ce0fbb76c3ceb120" -uuid = "b552c78f-8df3-52c6-915a-8e097449b14b" -version = "1.11.0" - -[[deps.Distributed]] -deps = ["Random", "Serialization", "Sockets"] -uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" - -[[deps.DocStringExtensions]] -deps = ["LibGit2"] -git-tree-sha1 = "b19534d1895d702889b219c382a6e18010797f0b" -uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" -version = "0.8.6" - -[[deps.Downloads]] -deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"] -uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" -version = "1.6.0" - -[[deps.FileWatching]] -uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" - -[[deps.ForwardDiff]] -deps = ["CommonSubexpressions", "DiffResults", "DiffRules", "LinearAlgebra", "LogExpFunctions", "NaNMath", "Preferences", "Printf", "Random", "SpecialFunctions", "StaticArrays"] -git-tree-sha1 = "2f18915445b248731ec5db4e4a17e451020bf21e" -uuid = "f6369f11-7733-5829-9624-2563aa707210" -version = "0.10.30" - -[[deps.HostCPUFeatures]] -deps = ["BitTwiddlingConvenienceFunctions", "IfElse", "Libdl", "Static"] -git-tree-sha1 = "18be5268cf415b5e27f34980ed25a7d34261aa83" -uuid = "3e5b6fbb-0976-4d2c-9146-d79de83f2fb0" -version = "0.1.7" - -[[deps.IfElse]] -git-tree-sha1 = "debdd00ffef04665ccbb3e150747a77560e8fad1" -uuid = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173" -version = "0.1.1" - -[[deps.InteractiveUtils]] -deps = ["Markdown"] -uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" - -[[deps.InverseFunctions]] -deps = ["Test"] -git-tree-sha1 = "336cc738f03e069ef2cac55a104eb823455dca75" -uuid = "3587e190-3f89-42d0-90ee-14403ec27112" -version = "0.1.4" - -[[deps.IrrationalConstants]] -git-tree-sha1 = "7fd44fd4ff43fc60815f8e764c0f352b83c49151" -uuid = "92d709cd-6900-40b7-9082-c6be49f344b6" -version = "0.1.1" - -[[deps.JLLWrappers]] -deps = ["Preferences"] -git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" -uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" -version = "1.4.1" - -[[deps.LLVM]] -deps = ["CEnum", "LLVMExtra_jll", "Libdl", "Printf", "Unicode"] -git-tree-sha1 = "c8d47589611803a0f3b4813d9e267cd4e3dbcefb" -uuid = "929cbde3-209d-540e-8aea-75f648917ca0" -version = "4.11.1" - -[[deps.LLVMExtra_jll]] -deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl", "Pkg", "TOML"] -git-tree-sha1 = "771bfe376249626d3ca12bcd58ba243d3f961576" -uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab" -version = "0.0.16+0" - -[[deps.LayoutPointers]] -deps = ["ArrayInterface", "LinearAlgebra", "ManualMemory", "SIMDTypes", "Static"] -git-tree-sha1 = "3da92dc6d4bce776cc850c18274a7508b8d752ef" -uuid = "10f19ff3-798f-405d-979b-55457f8fc047" -version = "0.1.7" - -[[deps.LazyArtifacts]] -deps = ["Artifacts", "Pkg"] -uuid = "4af54fe1-eca0-43a8-85a7-787d91b784e3" - -[[deps.LibCURL]] -deps = ["LibCURL_jll", "MozillaCACerts_jll"] -uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" -version = "0.6.3" - -[[deps.LibCURL_jll]] -deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] -uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" -version = "7.81.0+0" - -[[deps.LibGit2]] -deps = ["Base64", "NetworkOptions", "Printf", "SHA"] -uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" - -[[deps.LibSSH2_jll]] -deps = ["Artifacts", "Libdl", "MbedTLS_jll"] -uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" -version = "1.10.2+0" - -[[deps.Libdl]] -uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" - -[[deps.LinearAlgebra]] -deps = ["Libdl", "libblastrampoline_jll"] -uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" - -[[deps.LogExpFunctions]] -deps = ["ChainRulesCore", "ChangesOfVariables", "DocStringExtensions", "InverseFunctions", "IrrationalConstants", "LinearAlgebra"] -git-tree-sha1 = "09e4b894ce6a976c354a69041a04748180d43637" -uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688" -version = "0.3.15" - -[[deps.Logging]] -uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" - -[[deps.LoopVectorization]] -deps = ["ArrayInterface", "CPUSummary", "ChainRulesCore", "CloseOpenIntervals", "DocStringExtensions", "ForwardDiff", "HostCPUFeatures", "IfElse", "LayoutPointers", "LinearAlgebra", "OffsetArrays", "PolyesterWeave", "SIMDDualNumbers", "SLEEFPirates", "SpecialFunctions", "Static", "ThreadingUtilities", "UnPack", "VectorizationBase"] -git-tree-sha1 = "4392c19f0203df81512b6790a0a67446650bdce0" -uuid = "bdcacae8-1622-11e9-2a5c-532679323890" -version = "0.12.110" - -[[deps.MacroTools]] -deps = ["Markdown", "Random"] -git-tree-sha1 = "3d3e902b31198a27340d0bf00d6ac452866021cf" -uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" -version = "0.5.9" - -[[deps.ManualMemory]] -git-tree-sha1 = "bcaef4fc7a0cfe2cba636d84cda54b5e4e4ca3cd" -uuid = "d125e4d3-2237-4719-b19c-fa641b8a4667" -version = "0.1.8" - -[[deps.Markdown]] -deps = ["Base64"] -uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" - -[[deps.MbedTLS_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" -version = "2.28.0+0" - -[[deps.Mmap]] -uuid = "a63ad114-7e13-5084-954f-fe012c677804" - -[[deps.MozillaCACerts_jll]] -uuid = "14a3606d-f60d-562e-9121-12d972cd8159" -version = "2022.2.1" - -[[deps.NaNMath]] -git-tree-sha1 = "737a5957f387b17e74d4ad2f440eb330b39a62c5" -uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" -version = "1.0.0" - -[[deps.NetworkOptions]] -uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" -version = "1.2.0" - -[[deps.OffsetArrays]] -deps = ["Adapt"] -git-tree-sha1 = "52addd9e91df8a6a5781e5c7640787525fd48056" -uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" -version = "1.11.2" - -[[deps.OpenBLAS_jll]] -deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] -uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" -version = "0.3.20+0" - -[[deps.OpenLibm_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "05823500-19ac-5b8b-9628-191a04bc5112" -version = "0.8.1+0" - -[[deps.OpenSpecFun_jll]] -deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "13652491f6856acfd2db29360e1bbcd4565d04f1" -uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e" -version = "0.5.5+0" - -[[deps.Pkg]] -deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] -uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" -version = "1.8.0" - -[[deps.PolyesterWeave]] -deps = ["BitTwiddlingConvenienceFunctions", "CPUSummary", "IfElse", "Static", "ThreadingUtilities"] -git-tree-sha1 = "7e597df97e46ffb1c8adbaddfa56908a7a20194b" -uuid = "1d0040c9-8b98-4ee7-8388-3f51789ca0ad" -version = "0.1.5" - -[[deps.Preferences]] -deps = ["TOML"] -git-tree-sha1 = "47e5f437cc0e7ef2ce8406ce1e7e24d44915f88d" -uuid = "21216c6a-2e73-6563-6e65-726566657250" -version = "1.3.0" - -[[deps.Printf]] -deps = ["Unicode"] -uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" - -[[deps.REPL]] -deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] -uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" - -[[deps.Random]] -deps = ["SHA", "Serialization"] -uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" - -[[deps.Requires]] -deps = ["UUIDs"] -git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7" -uuid = "ae029012-a4dd-5104-9daa-d747884805df" -version = "1.3.0" - -[[deps.SHA]] -uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" -version = "0.7.0" - -[[deps.SIMDDualNumbers]] -deps = ["ForwardDiff", "IfElse", "SLEEFPirates", "VectorizationBase"] -git-tree-sha1 = "dd4195d308df24f33fb10dde7c22103ba88887fa" -uuid = "3cdde19b-5bb0-4aaf-8931-af3e248e098b" -version = "0.1.1" - -[[deps.SIMDTypes]] -git-tree-sha1 = "330289636fb8107c5f32088d2741e9fd7a061a5c" -uuid = "94e857df-77ce-4151-89e5-788b33177be4" -version = "0.1.0" - -[[deps.SLEEFPirates]] -deps = ["IfElse", "Static", "VectorizationBase"] -git-tree-sha1 = "ac399b5b163b9140f9c310dfe9e9aaa225617ff6" -uuid = "476501e8-09a2-5ece-8869-fb82de89a1fa" -version = "0.6.32" - -[[deps.Serialization]] -uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" - -[[deps.SharedArrays]] -deps = ["Distributed", "Mmap", "Random", "Serialization"] -uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" - -[[deps.Sockets]] -uuid = "6462fe0b-24de-5631-8697-dd941f90decc" - -[[deps.SparseArrays]] -deps = ["LinearAlgebra", "Random"] -uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" - -[[deps.SpecialFunctions]] -deps = ["ChainRulesCore", "IrrationalConstants", "LogExpFunctions", "OpenLibm_jll", "OpenSpecFun_jll"] -git-tree-sha1 = "bc40f042cfcc56230f781d92db71f0e21496dffd" -uuid = "276daf66-3868-5448-9aa4-cd146d93841b" -version = "2.1.5" - -[[deps.Static]] -deps = ["IfElse"] -git-tree-sha1 = "3a2a99b067090deb096edecec1dc291c5b4b31cb" -uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3" -version = "0.6.5" - -[[deps.StaticArrays]] -deps = ["LinearAlgebra", "Random", "Statistics"] -git-tree-sha1 = "cd56bf18ed715e8b09f06ef8c6b781e6cdc49911" -uuid = "90137ffa-7385-5640-81b9-e52037218182" -version = "1.4.4" - -[[deps.StaticTools]] -deps = ["ManualMemory"] -git-tree-sha1 = "45569bece43af73ba51cbdac422e77abd7adba9f" -uuid = "86c06d3c-3f03-46de-9781-57580aa96d0a" -version = "0.3.0" - -[[deps.Statistics]] -deps = ["LinearAlgebra", "SparseArrays"] -uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" - -[[deps.StrideArraysCore]] -deps = ["ArrayInterface", "CloseOpenIntervals", "IfElse", "LayoutPointers", "ManualMemory", "Requires", "SIMDTypes", "Static", "ThreadingUtilities"] -git-tree-sha1 = "e03eacc0b8c1520e73aa84922ce44a14f024b210" -uuid = "7792a7ef-975c-4747-a70f-980b88e8d1da" -version = "0.3.6" - -[[deps.TOML]] -deps = ["Dates"] -uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" -version = "1.0.0" - -[[deps.Tar]] -deps = ["ArgTools", "SHA"] -uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" -version = "1.10.0" - -[[deps.Test]] -deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] -uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[[deps.ThreadingUtilities]] -deps = ["ManualMemory"] -git-tree-sha1 = "f8629df51cab659d70d2e5618a430b4d3f37f2c3" -uuid = "8290d209-cae3-49c0-8002-c8c24d57dab5" -version = "0.5.0" - -[[deps.UUIDs]] -deps = ["Random", "SHA"] -uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" - -[[deps.UnPack]] -git-tree-sha1 = "387c1f73762231e86e0c9c5443ce3b4a0a9a0c2b" -uuid = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" -version = "1.0.2" - -[[deps.Unicode]] -uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" - -[[deps.VectorizationBase]] -deps = ["ArrayInterface", "CPUSummary", "HostCPUFeatures", "IfElse", "LayoutPointers", "Libdl", "LinearAlgebra", "SIMDTypes", "Static"] -git-tree-sha1 = "c95d242ade2d67c1510ce52d107cfca7a83e0b4e" -uuid = "3d5dd08c-fd9d-11e8-17fa-ed2836048c2f" -version = "0.21.33" - -[[deps.Zlib_jll]] -deps = ["Libdl"] -uuid = "83775a58-1f1d-513f-b197-d71354ab007a" -version = "1.2.12+1" - -[[deps.libblastrampoline_jll]] -deps = ["Artifacts", "Libdl", "OpenBLAS_jll"] -uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" -version = "5.1.0+0" - -[[deps.nghttp2_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" -version = "1.41.0+1" - -[[deps.p7zip_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" -version = "16.2.1+1" diff --git a/test/Project.toml b/test/Project.toml index cbcf73f0..e5a48238 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -7,4 +7,5 @@ LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" +TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" From 2245ac462c04583b4fff579e64f1746ee93f6784 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Mon, 18 Jul 2022 03:00:09 -0400 Subject: [PATCH 086/202] Add `cflags` option to `compile_executable` & co --- src/StaticCompiler.jl | 45 ++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index b49d7b9d..276dd23c 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -227,8 +227,10 @@ Hello, world! ``` """ function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); - filename=name, - kwargs...) + filename=name, + cflags=``, + kwargs... + ) tt = Base.to_tuple_type(types) # tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})") @@ -239,7 +241,7 @@ function compile_executable(f, types=(), path::String="./", name=GPUCompiler.saf # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - generate_executable(f, tt, path, name, filename; kwargs...) + generate_executable(f, tt, path, name, filename; cflags=cflags, kwargs...) joinpath(abspath(path), filename) end @@ -252,8 +254,10 @@ compile_shlib(f, types::Tuple, path::String, name::String=repr(f); filename::Str As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. """ function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); - filename=name, - kwargs...) + filename=name, + cflags=``, + kwargs... + ) tt = Base.to_tuple_type(types) isconcretetype(tt) || error("input type signature $types is not concrete") @@ -263,8 +267,7 @@ function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_nam # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - - generate_shlib(f, tt, path, name, filename; kwargs...) + generate_shlib(f, tt, path, name, filename; cflags=cflags, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end @@ -348,7 +351,10 @@ test (generic function with 1 method) julia> path, name = StaticCompiler.generate_executable(test, Tuple{Int64}, "./scratch") ``` """ -function generate_executable(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=string(name); kwargs...) +function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name); + cflags=``, + kwargs... + ) mkpath(path) obj_path = joinpath(path, "$filename.o") exec_path = joinpath(path, filename) @@ -366,7 +372,7 @@ function generate_executable(f, tt, path::String = tempname(), name = GPUCompile if Sys.isapple() # Apple no longer uses _start, so we can just specify a custom entry entry = "_julia_$name" - run(`$cc -e $entry $obj_path -o $exec_path`) + run(`$cc -e $entry $cflags $obj_path -o $exec_path`) else # Write a minimal wrapper to avoid having to specify a custom entry wrapper_path = joinpath(path, "wrapper.c") @@ -377,7 +383,7 @@ function generate_executable(f, tt, path::String = tempname(), name = GPUCompile return 0; }""") close(f) - run(`$cc $wrapper_path $obj_path -o $exec_path`) + run(`$cc $wrapper_path $cflags $obj_path -o $exec_path`) # Clean up run(`rm $wrapper_path`) end @@ -416,7 +422,11 @@ julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 5.256496109495593 ``` """ -function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=name; kwargs...) +function generate_shlib(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=name; + cflags=``, + kwargs... + ) + mkpath(path) obj_path = joinpath(path, "$filename.o") lib_path = joinpath(path, "$filename.$(Libdl.dlext)") @@ -430,7 +440,7 @@ function generate_shlib(f, tt, path::String = tempname(), name = GPUCompiler.saf # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! - run(`$cc -shared -o $lib_path $obj_path`) + run(`$cc -shared $cflags $obj_path -o $lib_path`) path, name end @@ -503,7 +513,11 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str path, obj_path end -function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; demangle=false, kwargs...) +function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; + demangle=false, + cflags=``, + kwargs... + ) lib_path = joinpath(path, "$filename.$(Libdl.dlext)") @@ -511,7 +525,7 @@ function generate_shlib(funcs::Array, path::String = tempname(), filename::Strin # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! - run(`$cc -shared -o $lib_path $obj_path`) + run(`$cc -shared $cflags $obj_path -o $lib_path `) path, name end @@ -519,6 +533,7 @@ end function compile_shlib(funcs::Array, path::String="./"; filename="libfoo", demangle=false, + cflags=``, kwargs...) for func in funcs f, types = func @@ -532,7 +547,7 @@ function compile_shlib(funcs::Array, path::String="./"; # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - generate_shlib(funcs, path, filename; demangle=demangle, kwargs...) + generate_shlib(funcs, path, filename; demangle=demangle, cflags=cflags, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end From 51e90077774af471be2ca96c37268e93f21eb93e Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Mon, 18 Jul 2022 10:24:26 -0400 Subject: [PATCH 087/202] Update tagbot for triggered workflow, adjust ci versions --- .github/workflows/TagBot.yml | 8 ++++++-- .github/workflows/ci-integration.yml | 2 +- .github/workflows/ci.yml | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/TagBot.yml b/.github/workflows/TagBot.yml index d77d3a0c..f49313b6 100644 --- a/.github/workflows/TagBot.yml +++ b/.github/workflows/TagBot.yml @@ -1,11 +1,15 @@ name: TagBot on: - schedule: - - cron: 0 * * * * + issue_comment: + types: + - created + workflow_dispatch: jobs: TagBot: + if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot' runs-on: ubuntu-latest steps: - uses: JuliaRegistries/TagBot@v1 with: token: ${{ secrets.GITHUB_TOKEN }} + ssh: ${{ secrets.DOCUMENTER_KEY }} diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index 287ae13b..6872fce8 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -18,7 +18,7 @@ jobs: fail-fast: false matrix: version: - - '~1.8.0-0' + - '1.8.0-rc1' os: - ubuntu-latest - macOS-latest diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a6bcbb0..5d5ce480 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: matrix: version: - '1.7' - - '~1.8.0-0' + - '1.8.0-rc1' os: - ubuntu-latest - macOS-latest From ca99263c78e2626b53f91a8d12eb952a02f0ae1f Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Mon, 18 Jul 2022 16:24:11 -0400 Subject: [PATCH 088/202] Bump version to 0.4.5 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 22c22a3d..65b6a98c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short"] -version = "0.4.4" +version = "0.4.5" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" From 6cc898b080e059481c4e0fc99265b510da12b293 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Tue, 18 Oct 2022 23:29:17 -0300 Subject: [PATCH 089/202] Initial commit for override --- Project.toml | 1 + src/StaticCompiler.jl | 22 ++++++++--- src/quirks.jl | 89 +++++++++++++++++++++++++++++++++++++++++++ src/target.jl | 70 +++++++++++++++++++++------------- src/utils.jl | 69 +++++++++++++++++++++++++++++++++ 5 files changed, 219 insertions(+), 32 deletions(-) create mode 100644 src/quirks.jl create mode 100644 src/utils.jl diff --git a/Project.toml b/Project.toml index 65b6a98c..930b80cb 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" +StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] GPUCompiler = "0.16" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 276dd23c..829e22ed 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -8,10 +8,13 @@ using Libdl: Libdl, dlsym, dlopen using Base: RefValue using Serialization: serialize, deserialize using Clang_jll: clang +using StaticTools: @symbolcall, @c_str export compile, load_function, compile_shlib, compile_executable export native_code_llvm, native_code_typed, native_llvm_module, native_code_native +include("utils.jl") +include("quirks.jl") include("target.jl") include("pointer_patching.jl") include("code_loading.jl") @@ -95,8 +98,13 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; opt_level, strip_llvm, strip_asm, filename, kwargs...) - + @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams})) = nothing + @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget})) = nothing + @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams})) = nothing + _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; opt_level, strip_llvm, strip_asm, filename, ext = false, kwargs...) + @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams})) = method_table + @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget})) = method_table + @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams})) = method_table lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name, filename, table) cjl_path = joinpath(path, "$filename.cjl") serialize(cjl_path, lf) @@ -106,7 +114,7 @@ end """ ```julia -generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; +(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; \tstrip_llvm = false, \tstrip_asm = true, \topt_level=3, @@ -135,11 +143,12 @@ function generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_ strip_llvm = false, strip_asm = true, opt_level=3, + ext = true, kwargs...) mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) - job, kwargs = native_job(f, tt; name, kwargs...) + job, kwargs = native_job(f, tt; name,ext = ext, kwargs...) + tm = GPUCompiler.llvm_machine(job.target) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false, ctx=context) @@ -500,11 +509,12 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str strip_llvm = false, strip_asm = true, opt_level=3, + ext = true, kwargs...) f,tt = funcs[1] mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - fakejob, kwargs = native_job(f,tt, kwargs...) + fakejob, kwargs = native_job(f,tt, ext = true, kwargs...) mod = native_llvm_module(funcs; demangle = demangle, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io diff --git a/src/quirks.jl b/src/quirks.jl new file mode 100644 index 00000000..b7555836 --- /dev/null +++ b/src/quirks.jl @@ -0,0 +1,89 @@ +macro print_and_throw(err) + quote + println(err) + libcexit(Int32(1)) + end +end + +# math.jl +@device_override @noinline Base.Math.throw_complex_domainerror(f::Symbol, x) = + @print_and_throw c"This operation requires a complex input to return a complex result" +@device_override @noinline Base.Math.throw_exp_domainerror(f::Symbol, x) = + @print_and_throw c"Exponentiation yielding a complex result requires a complex argument" + +# intfuncs.jl +@device_override @noinline Base.throw_domerr_powbysq(::Any, p) = + @print_and_throw c"Cannot raise an integer to a negative power" +@device_override @noinline Base.throw_domerr_powbysq(::Integer, p) = + @print_and_throw c"Cannot raise an integer to a negative power" +@device_override @noinline Base.throw_domerr_powbysq(::AbstractMatrix, p) = + @print_and_throw c"Cannot raise an integer to a negative power" +@device_override @noinline Base.__throw_gcd_overflow(a, b) = + @print_and_throw c"gcd overflow" + +# checked.jl +@device_override @noinline Base.Checked.throw_overflowerr_binaryop(op, x, y) = + @print_and_throw c"Binary operation overflowed" +@device_override @noinline Base.Checked.throw_overflowerr_negation(op, x, y) = + @print_and_throw c"Negation overflowed" +@device_override function Base.Checked.checked_abs(x::Base.Checked.SignedInt) + r = ifelse(x<0, -x, x) + r<0 && @print_and_throw(c"checked arithmetic: cannot compute |x|") + r +end + +# boot.jl +@device_override @noinline Core.throw_inexacterror(f::Symbol, ::Type{T}, val) where {T} = + @print_and_throw c"Inexact conversion" + +# abstractarray.jl +@device_override @noinline Base.throw_boundserror(A, I) = + @print_and_throw c"Out-of-bounds array access" + +# trig.jl +@device_override @noinline Base.Math.sincos_domain_error(x) = + @print_and_throw c"sincos(x) is only defined for finite x." + + +# range.jl +@static if VERSION >= v"1.7-" + @eval begin + @device_override function Base.StepRangeLen{T,R,S,L}(ref::R, step::S, len::Integer, + offset::Integer=1) where {T,R,S,L} + if T <: Integer && !isinteger(ref + step) + @print_and_throw(c"StepRangeLen{<:Integer} cannot have non-integer step") + end + len = convert(L, len) + len >= zero(len) || @print_and_throw(c"StepRangeLen length cannot be negative") + offset = convert(L, offset) + L1 = oneunit(typeof(len)) + L1 <= offset <= max(L1, len) || @print_and_throw(c"StepRangeLen: offset must be in [1,...]") + $( + Expr(:new, :(StepRangeLen{T,R,S,L}), :ref, :step, :len, :offset) + ) + end + end +else + @device_override function Base.StepRangeLen{T,R,S}(ref::R, step::S, len::Integer, + offset::Integer=1) where {T,R,S} + if T <: Integer && !isinteger(ref + step) + @print_and_throw(c"StepRangeLen{<:Integer} cannot have non-integer step") + end + len >= 0 || @print_and_throw(c"StepRangeLen length cannot be negative") + 1 <= offset <= max(1,len) || @print_and_throw(c"StepRangeLen: offset must be in [1,...]") + new(ref, step, len, offset) + end +end + + +# fastmath.jl +@static if VERSION <= v"1.7-" +## prevent fallbacks to libm +for f in (:acosh, :asinh, :atanh, :cbrt, :cosh, :exp2, :expm1, :log1p, :sinh, :tanh) + f_fast = Base.FastMath.fast_op[f] + @eval begin + @device_override Base.FastMath.$f_fast(x::Float32) = $f(x) + @device_override Base.FastMath.$f_fast(x::Float64) = $f(x) + end +end +end \ No newline at end of file diff --git a/src/target.jl b/src/target.jl index 2efb191c..9bb9128c 100644 --- a/src/target.jl +++ b/src/target.jl @@ -3,44 +3,62 @@ Base.@kwdef struct NativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) end -GPUCompiler.llvm_triple(::NativeCompilerTarget) = Sys.MACHINE +Base.@kwdef struct ExternalNativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget + cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) + features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) +end -function GPUCompiler.llvm_machine(target::NativeCompilerTarget) - triple = GPUCompiler.llvm_triple(target) +module StaticRuntime +# the runtime library +signal_exception() = return +malloc(sz) = ccall("extern malloc", llvmcall, Csize_t, (Csize_t,), sz) +report_oom(sz) = return +report_exception(ex) = return +report_exception_name(ex) = return +report_exception_frame(idx, func, file, line) = return +end - t = LLVM.Target(triple=triple) +struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams end - tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) - GPUCompiler.asm_verbosity!(tm, true) +for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) + @eval begin + GPUCompiler.llvm_triple(::$target) = Sys.MACHINE - return tm -end + function GPUCompiler.llvm_machine(target::$target) + triple = GPUCompiler.llvm_triple(target) -GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = "native_$(job.target.cpu)-$(hash(job.target.features))" + t = LLVM.Target(triple=triple) -module StaticRuntime - # the runtime library - signal_exception() = return - malloc(sz) = ccall("extern malloc", llvmcall, Csize_t, (Csize_t,), sz) - report_oom(sz) = return - report_exception(ex) = return - report_exception_name(ex) = return - report_exception_frame(idx, func, file, line) = return -end + tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) + GPUCompiler.asm_verbosity!(tm, true) -struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams end + return tm + end -GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = StaticRuntime -GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{NativeCompilerTarget}) = StaticRuntime -GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams}) = StaticRuntime + GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{$target}) = "native_$(job.target.cpu)-$(hash(job.target.features))" + + GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target}) = StaticRuntime + GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = StaticRuntime + + GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = true + GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target}) = true + end +end + +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = StaticRuntime GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = true -GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams}) = true -GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = true -function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) +# GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams})) = nothing +# GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget})) = nothing +# GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams})) = nothing + +GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget})) = method_table +GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget, StaticCompilerParams})) = method_table + +function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), ext = true, kwargs...) source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) - target = NativeCompilerTarget() + target = ext ? ExternalNativeCompilerTarget() : NativeCompilerTarget() params = StaticCompilerParams() GPUCompiler.CompilerJob(target, source, params), kwargs end diff --git a/src/utils.jl b/src/utils.jl new file mode 100644 index 00000000..a581b764 --- /dev/null +++ b/src/utils.jl @@ -0,0 +1,69 @@ + + +# local method table for device functions +@static if isdefined(Base.Experimental, Symbol("@overlay")) +Base.Experimental.@MethodTable(method_table) +else +const method_table = nothing +end + +# list of overrides (only for Julia 1.6) +const overrides = Expr[] + +macro device_override(ex) + ex = macroexpand(__module__, ex) + if Meta.isexpr(ex, :call) + @show ex = eval(ex) + error() + end + code = quote + $GPUCompiler.@override(StaticCompiler.method_table, $ex) + end + if isdefined(Base.Experimental, Symbol("@overlay")) + return esc(code) + else + push!(overrides, code) + return + end +end + +macro device_function(ex) + ex = macroexpand(__module__, ex) + def = splitdef(ex) + + # generate a function that errors + def[:body] = quote + error("This function is not intended for use on the CPU") + end + + esc(quote + $(combinedef(def)) + @device_override $ex + end) +end + +macro device_functions(ex) + ex = macroexpand(__module__, ex) + + # recursively prepend `@device_function` to all function definitions + function rewrite(block) + out = Expr(:block) + for arg in block.args + if Meta.isexpr(arg, :block) + # descend in blocks + push!(out.args, rewrite(arg)) + elseif Meta.isexpr(arg, [:function, :(=)]) + # rewrite function definitions + push!(out.args, :(@device_function $arg)) + else + # preserve all the rest + push!(out.args, arg) + end + end + out + end + + esc(rewrite(ex)) +end + +libcexit(x::Int32) = @symbolcall exit(x::Int32)::Nothing \ No newline at end of file From d3b359b4a63afe647f087bacc8df0d1b775a6466 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 19 Oct 2022 19:40:34 -0400 Subject: [PATCH 090/202] Add compat bounds for StaticTools.jl --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 930b80cb..ac5159af 100644 --- a/Project.toml +++ b/Project.toml @@ -15,3 +15,4 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" GPUCompiler = "0.16" LLVM = "4.8" julia = "1.7" +StaticTools = "0.8" From 4a5c3b90f7d50632d75d8a7e15cd230b94baa033 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 19 Oct 2022 19:41:47 -0400 Subject: [PATCH 091/202] Apply @giordano formatting suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mosè Giordano --- src/quirks.jl | 2 +- src/utils.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/quirks.jl b/src/quirks.jl index b7555836..d632dd78 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -86,4 +86,4 @@ for f in (:acosh, :asinh, :atanh, :cbrt, :cosh, :exp2, :expm1, :log1p, :sinh, :t @device_override Base.FastMath.$f_fast(x::Float64) = $f(x) end end -end \ No newline at end of file +end diff --git a/src/utils.jl b/src/utils.jl index a581b764..636b147a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -66,4 +66,4 @@ macro device_functions(ex) esc(rewrite(ex)) end -libcexit(x::Int32) = @symbolcall exit(x::Int32)::Nothing \ No newline at end of file +libcexit(x::Int32) = @symbolcall exit(x::Int32)::Nothing From 341ceb3f0d6a5c8966bd31bfb4d764cc742490e4 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Wed, 19 Oct 2022 22:41:32 -0400 Subject: [PATCH 092/202] Try to avoid messing with Mason's `compile` interface --- src/StaticCompiler.jl | 413 +++++++++++++++++++++--------------------- src/target.jl | 29 +-- 2 files changed, 226 insertions(+), 216 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 829e22ed..6641f9b8 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -89,7 +89,7 @@ single method (the method determined by `types`). function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_name(repr(f)), filename="obj", strip_llvm = false, strip_asm = true, - opt_level=3, + opt_level = 3, kwargs...) tt = Base.to_tuple_type(_tt) isconcretetype(tt) || error("input type signature $_tt is not concrete") @@ -98,13 +98,8 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams})) = nothing - @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget})) = nothing - @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams})) = nothing - _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; opt_level, strip_llvm, strip_asm, filename, ext = false, kwargs...) - @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams})) = method_table - @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget})) = method_table - @eval GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams})) = method_table + _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; opt_level, strip_llvm, strip_asm, filename, kwargs...) + lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name, filename, table) cjl_path = joinpath(path, "$filename.cjl") serialize(cjl_path, lf) @@ -114,41 +109,37 @@ end """ ```julia -(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; +generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; \tstrip_llvm = false, \tstrip_asm = true, - \topt_level=3, + \topt_level = 3, \tkwargs...) ``` Low level interface for compiling object code (`.o`) for for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. - ### Examples ```julia julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) fib (generic function with 1 method) - julia> path, name, table = StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test") ("./test", "fib", IdDict{Any, String}()) - shell> tree \$path ./test └── obj.o - 0 directories, 1 file ``` """ function generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; strip_llvm = false, strip_asm = true, - opt_level=3, - ext = true, + libjulia = true, + opt_level = 3, kwargs...) mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - job, kwargs = native_job(f, tt; name,ext = ext, kwargs...) - tm = GPUCompiler.llvm_machine(job.target) + tm = GPUCompiler.llvm_machine(libjulia ? NativeCompilerTarget() : ExternalNativeCompilerTarget()) + job, kwargs = native_job(f, tt; name, libjulia, kwargs...) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false, ctx=context) @@ -177,94 +168,16 @@ function generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_ path, name, table end -""" -```julia -compile_executable(f, types::Tuple, path::String, name::String=repr(f); filename::String=name, kwargs...) -``` -Attempt to compile a standalone executable that runs function `f` with a type signature given by the tuple of `types`. - -### Examples -```julia -julia> using StaticCompiler - -julia> function puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates. - # Note, this `llvmcall` requires Julia 1.8+ - Base.llvmcall((\""" - ; External declaration of the puts function - declare i32 @puts(i8* nocapture) nounwind - - define i32 @main(i8*) { - entry: - %call = call i32 (i8*) @puts(i8* %0) - ret i32 0 - } - \""", "main"), Int32, Tuple{Ptr{UInt8}}, s) - end -puts (generic function with 1 method) - -julia> function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) - for i=1:argc - # Get pointer - p = unsafe_load(argv, i) - # Print string at pointer location (which fortunately already exists isn't tracked by the GC) - puts(p) - end - return 0 - end - -julia> compile_executable(print_args, (Int, Ptr{Ptr{UInt8}})) -"/Users/foo/code/StaticCompiler.jl/print_args" - -shell> ./print_args 1 2 3 4 Five -./print_args -1 -2 -3 -4 -Five -``` -```julia -julia> using StaticTools # So you don't have to define `puts` and friends every time - -julia> hello() = println(c"Hello, world!") # c"..." makes a stack-allocated StaticString - -julia> compile_executable(hello) -"/Users/foo/code/StaticCompiler.jl/hello" - -shell> ./hello -Hello, world! -``` -""" -function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); - filename=name, - cflags=``, - kwargs... - ) - - tt = Base.to_tuple_type(types) - # tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})") - - rt = only(native_code_typed(f, tt))[2] - isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") - - # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals - # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - - generate_executable(f, tt, path, name, filename; cflags=cflags, kwargs...) - - joinpath(abspath(path), filename) -end - - """ ```julia compile_shlib(f, types::Tuple, path::String, name::String=repr(f); filename::String=name, kwargs...) ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. """ -function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); - filename=name, - cflags=``, +function compile_shlib(f::Function, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); + filename = name, + libjulia = false, + cflags = ``, kwargs... ) @@ -276,16 +189,17 @@ function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_nam # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - generate_shlib(f, tt, path, name, filename; cflags=cflags, kwargs...) + generate_shlib(f, tt, path, name, filename; libjulia, cflags, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=name; temp::Bool=true, + libjulia=true, kwargs...) - generate_shlib(f, tt, path, name; kwargs...) + generate_shlib(f, tt, path, name; libjulia, kwargs...) lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @@ -340,66 +254,6 @@ function generate_shlib_fptr(path::String, name, filename::String=name) fptr end -""" -```julia -generate_executable(f, tt, path::String, name, filename=string(name); kwargs...) -``` -Attempt to compile a standalone executable that runs `f`. - -### Examples -```julia -julia> function test(n) - r = 0.0 - for i=1:n - r += log(sqrt(i)) - end - return r/n - end -test (generic function with 1 method) - -julia> path, name = StaticCompiler.generate_executable(test, Tuple{Int64}, "./scratch") -``` -""" -function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name); - cflags=``, - kwargs... - ) - mkpath(path) - obj_path = joinpath(path, "$filename.o") - exec_path = joinpath(path, filename) - job, kwargs = native_job(f, tt; name, kwargs...) - obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) - - # Write to file - open(obj_path, "w") do io - write(io, obj) - end - - # Pick a compiler - cc = Sys.isapple() ? `cc` : clang() - # Compile! - if Sys.isapple() - # Apple no longer uses _start, so we can just specify a custom entry - entry = "_julia_$name" - run(`$cc -e $entry $cflags $obj_path -o $exec_path`) - else - # Write a minimal wrapper to avoid having to specify a custom entry - wrapper_path = joinpath(path, "wrapper.c") - f = open(wrapper_path, "w") - print(f, """int main(int argc, char** argv) - { - julia_$name(argc, argv); - return 0; - }""") - close(f) - run(`$cc $wrapper_path $cflags $obj_path -o $exec_path`) - # Clean up - run(`rm $wrapper_path`) - end - - path, name -end - """ ```julia generate_shlib(f, tt, path::String, name::String, filenamebase::String="obj"; kwargs...) @@ -432,14 +286,15 @@ julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), ``` """ function generate_shlib(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=name; - cflags=``, + libjulia = false, + cflags = ``, kwargs... ) mkpath(path) obj_path = joinpath(path, "$filename.o") lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - job, kwargs = native_job(f, tt; name, kwargs...) + job, kwargs = native_job(f, tt; name, libjulia, kwargs...) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) open(obj_path, "w") do io @@ -480,12 +335,12 @@ end #Return an LLVM module for multiple functions function native_llvm_module(funcs::Array; demangle = false, kwargs...) - f,tt = funcs[1] - mod = native_llvm_module(f,tt, kwargs...) + f,tt = first(funcs) + mod = native_llvm_module(f,tt; kwargs...) if length(funcs) > 1 for func in funcs[2:end] @show f,tt = func - tmod = native_llvm_module(f,tt, kwargs...) + tmod = native_llvm_module(f,tt; kwargs...) link!(mod,tmod) end end @@ -504,18 +359,60 @@ function native_llvm_module(funcs::Array; demangle = false, kwargs...) return mod end -function generate_obj(funcs::Array, path::String = tempname(), filenamebase::String="obj"; - demangle =false, +## -- compile_shlib / generate_shlib / generate_obj, but for multiple functions at once + +function compile_shlib(funcs::Vector{<:Tuple}, path::String="./"; + filename = "libfoo", + demangle = false, + libjulia = false, + cflags = ``, + kwargs... + ) + for func in funcs + f, types = func + tt = Base.to_tuple_type(types) + isconcretetype(tt) || error("input type signature $types is not concrete") + + rt = only(native_code_typed(f, tt))[2] + isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") + end + + funcs_tt = [(first(f), Base.to_tuple_type(last(f))) for f in funcs] + generate_shlib(funcs_tt, path, filename; demangle, libjulia, cflags, kwargs...) + + joinpath(abspath(path), filename * "." * Libdl.dlext) +end + +function generate_shlib(funcs::Vector{<:Tuple}, path::String = tempname(), filename::String="libfoo"; + demangle = false, + libjulia = false, + cflags = ``, + kwargs... + ) + + lib_path = joinpath(path, "$filename.$(Libdl.dlext)") + + _,obj_path = generate_obj(funcs, path, filename; demangle, libjulia, kwargs...) + # Pick a Clang + cc = Sys.isapple() ? `cc` : clang() + # Compile! + run(`$cc -shared $cflags $obj_path -o $lib_path `) + + path, name +end + +function generate_obj(funcs::Vector{<:Tuple}, path::String = tempname(), filenamebase::String="obj"; + demangle = false, + libjulia = false, strip_llvm = false, - strip_asm = true, - opt_level=3, - ext = true, + strip_asm = true, + opt_level = 3, kwargs...) - f,tt = funcs[1] + f,tt = first(funcs) mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - fakejob, kwargs = native_job(f,tt, ext = true, kwargs...) - mod = native_llvm_module(funcs; demangle = demangle, kwargs...) + fakejob, kwargs = native_job(f, tt; libjulia, kwargs...) + mod = native_llvm_module(funcs; demangle, libjulia, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io write(io, obj) @@ -523,44 +420,156 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str path, obj_path end -function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; - demangle=false, - cflags=``, +## --- Compile standalone binaries + +""" +```julia +function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); + filename = name, + libjulia = false, + cflags = ``, kwargs... ) +``` +Attempt to compile a standalone executable that runs function `f` with a type signature given by the tuple of `types`. - lib_path = joinpath(path, "$filename.$(Libdl.dlext)") +### Examples +```julia +julia> using StaticCompiler - _,obj_path = generate_obj(funcs, path, filename; demangle=demangle, kwargs...) - # Pick a Clang - cc = Sys.isapple() ? `cc` : clang() - # Compile! - run(`$cc -shared $cflags $obj_path -o $lib_path `) +julia> function puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates. + # Note, this `llvmcall` requires Julia 1.8+ + Base.llvmcall((\""" + ; External declaration of the puts function + declare i32 @puts(i8* nocapture) nounwind - path, name -end + define i32 @main(i8*) { + entry: + %call = call i32 (i8*) @puts(i8* %0) + ret i32 0 + } + \""", "main"), Int32, Tuple{Ptr{UInt8}}, s) + end +puts (generic function with 1 method) -function compile_shlib(funcs::Array, path::String="./"; - filename="libfoo", - demangle=false, - cflags=``, - kwargs...) - for func in funcs - f, types = func - tt = Base.to_tuple_type(types) - isconcretetype(tt) || error("input type signature $types is not concrete") +julia> function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) + for i=1:argc + # Get pointer + p = unsafe_load(argv, i) + # Print string at pointer location (which fortunately already exists isn't tracked by the GC) + puts(p) + end + return 0 + end - rt = only(native_code_typed(f, tt))[2] - isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") - end +julia> compile_executable(print_args, (Int, Ptr{Ptr{UInt8}})) +"/Users/foo/code/StaticCompiler.jl/print_args" -# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals -# Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this +shell> ./print_args 1 2 3 4 Five +./print_args +1 +2 +3 +4 +Five +``` +```julia +julia> using StaticTools # So you don't have to define `puts` and friends every time - generate_shlib(funcs, path, filename; demangle=demangle, cflags=cflags, kwargs...) +julia> hello() = println(c"Hello, world!") # c"..." makes a stack-allocated StaticString - joinpath(abspath(path), filename * "." * Libdl.dlext) +julia> compile_executable(hello) +"/Users/foo/code/StaticCompiler.jl/hello" + +shell> ./hello +Hello, world! +``` +""" +function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); + filename = name, + libjulia = false, + cflags = ``, + kwargs... + ) + + tt = Base.to_tuple_type(types) + # tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})") + + rt = only(native_code_typed(f, tt))[2] + isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") + + # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals + # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this + + generate_executable(f, tt, path, name, filename; libjulia, cflags, kwargs...) + + joinpath(abspath(path), filename) end +""" +```julia +function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name); + cflags = ``, + libjulia = false, + kwargs... + ) +``` +Attempt to compile a standalone executable that runs `f`. + +### Examples +```julia +julia> function test(n) + r = 0.0 + for i=1:n + r += log(sqrt(i)) + end + return r/n + end +test (generic function with 1 method) + +julia> path, name = StaticCompiler.generate_executable(test, Tuple{Int64}, "./scratch") +``` +""" +function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name); + cflags = ``, + libjulia = false, + kwargs... + ) + mkpath(path) + obj_path = joinpath(path, "$filename.o") + exec_path = joinpath(path, filename) + job, kwargs = native_job(f, tt; name, libjulia, kwargs...) + obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) + + # Write to file + open(obj_path, "w") do io + write(io, obj) + end + + # Pick a compiler + cc = Sys.isapple() ? `cc` : clang() + # Compile! + if Sys.isapple() + # Apple no longer uses _start, so we can just specify a custom entry + entry = "_julia_$name" + run(`$cc -e $entry $cflags $obj_path -o $exec_path`) + else + # Write a minimal wrapper to avoid having to specify a custom entry + wrapper_path = joinpath(path, "wrapper.c") + f = open(wrapper_path, "w") + print(f, """int main(int argc, char** argv) + { + julia_$name(argc, argv); + return 0; + }""") + close(f) + run(`$cc $wrapper_path $cflags $obj_path -o $exec_path`) + # Clean up + run(`rm $wrapper_path`) + end + + path, name +end + end # module diff --git a/src/target.jl b/src/target.jl index 9bb9128c..b6713b81 100644 --- a/src/target.jl +++ b/src/target.jl @@ -9,13 +9,13 @@ Base.@kwdef struct ExternalNativeCompilerTarget <: GPUCompiler.AbstractCompilerT end module StaticRuntime -# the runtime library -signal_exception() = return -malloc(sz) = ccall("extern malloc", llvmcall, Csize_t, (Csize_t,), sz) -report_oom(sz) = return -report_exception(ex) = return -report_exception_name(ex) = return -report_exception_frame(idx, func, file, line) = return + # the runtime library + signal_exception() = return + malloc(sz) = ccall("extern malloc", llvmcall, Csize_t, (Csize_t,), sz) + report_oom(sz) = return + report_exception(ex) = return + report_exception_name(ex) = return + report_exception_frame(idx, func, file, line) = return end struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams end @@ -49,16 +49,17 @@ end GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = StaticRuntime GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = true -# GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams})) = nothing -# GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget})) = nothing -# GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams})) = nothing - GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget})) = method_table GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget, StaticCompilerParams})) = method_table -function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), ext = true, kwargs...) - source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) - target = ext ? ExternalNativeCompilerTarget() : NativeCompilerTarget() +function native_job(@nospecialize(func::Function), @nospecialize(types::Type); + name = GPUCompiler.safe_name(repr(func)), + libjulia::Bool = true, + kernel::Bool = false, + kwargs... + ) + source = GPUCompiler.FunctionSpec(func, types, kernel, name) + target = libjulia ? NativeCompilerTarget() : ExternalNativeCompilerTarget() params = StaticCompilerParams() GPUCompiler.CompilerJob(target, source, params), kwargs end From 24b8ca2dca6976942277507eef7954767b2590d7 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Thu, 20 Oct 2022 01:23:23 -0400 Subject: [PATCH 093/202] Prototype julia function when compiling to avoid implicit declaration warning --- src/StaticCompiler.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 276dd23c..879a21ab 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -377,7 +377,9 @@ function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name( # Write a minimal wrapper to avoid having to specify a custom entry wrapper_path = joinpath(path, "wrapper.c") f = open(wrapper_path, "w") - print(f, """int main(int argc, char** argv) + print(f, """int julia_$name(int argc, char** argv); + + int main(int argc, char** argv) { julia_$name(argc, argv); return 0; From 3ea9dac8496bde17088286db69c968d3731e252f Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Thu, 20 Oct 2022 01:24:07 -0400 Subject: [PATCH 094/202] Define `__stack_chk_guard` to fix linking error on linux --- Project.toml | 4 ++-- src/StaticCompiler.jl | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 65b6a98c..bfea97f0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" -authors = ["Tom Short"] -version = "0.4.5" +authors = ["Tom Short and contributors"] +version = "0.4.6" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 879a21ab..5a447cd0 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -378,7 +378,8 @@ function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name( wrapper_path = joinpath(path, "wrapper.c") f = open(wrapper_path, "w") print(f, """int julia_$name(int argc, char** argv); - + void* __stack_chk_guard = (void*) $(rand(UInt) >> 1); + int main(int argc, char** argv) { julia_$name(argc, argv); From af5d9f2e6be3d178f203c5affc38f852a100a4af Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Thu, 20 Oct 2022 12:09:49 -0400 Subject: [PATCH 095/202] Stick with known good versions in CI for now --- .github/workflows/ci-integration.yml | 2 +- .github/workflows/ci.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index 6872fce8..03fa4d28 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -18,7 +18,7 @@ jobs: fail-fast: false matrix: version: - - '1.8.0-rc1' + - '1.8.0-beta3' os: - ubuntu-latest - macOS-latest diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d5ce480..dddaf90d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: matrix: version: - '1.7' - - '1.8.0-rc1' + - '1.8.0-beta3' os: - ubuntu-latest - macOS-latest From 5bd9159fa4bab1bc51d97ed2475e52e9eb349df6 Mon Sep 17 00:00:00 2001 From: "C.Brenhin Keller" Date: Thu, 20 Oct 2022 12:50:18 -0400 Subject: [PATCH 096/202] Update integration tests --- test/scripts/interop.jl | 16 ++ test/scripts/loopvec_matrix.jl | 9 +- test/scripts/loopvec_matrix_stack.jl | 49 ++++ test/scripts/loopvec_product.jl | 4 +- test/scripts/rand_matrix.jl | 8 +- test/scripts/randn_matrix.jl | 22 ++ test/scripts/times_table.jl | 9 +- test/scripts/withmallocarray.jl | 31 +++ test/testintegration.jl | 342 +++++++++++++++++++++------ 9 files changed, 402 insertions(+), 88 deletions(-) create mode 100644 test/scripts/interop.jl create mode 100644 test/scripts/loopvec_matrix_stack.jl create mode 100644 test/scripts/randn_matrix.jl create mode 100644 test/scripts/withmallocarray.jl diff --git a/test/scripts/interop.jl b/test/scripts/interop.jl new file mode 100644 index 00000000..b601e090 --- /dev/null +++ b/test/scripts/interop.jl @@ -0,0 +1,16 @@ +using StaticCompiler +using StaticTools + +function interop(argc, argv) + lib = StaticTools.dlopen(c"libm") + printf(lib) + sin = StaticTools.dlsym(lib, c"sin") + printf(sin) + x = @ptrcall sin(5.0::Float64)::Float64 + printf(x) + newline() + StaticTools.dlclose(lib) +end + +# Attempt to compile +path = compile_executable(interop, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-ldl -lm`) diff --git a/test/scripts/loopvec_matrix.jl b/test/scripts/loopvec_matrix.jl index 5477dd37..7b19ce86 100644 --- a/test/scripts/loopvec_matrix.jl +++ b/test/scripts/loopvec_matrix.jl @@ -15,8 +15,8 @@ end function loopvec_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) argc == 3 || return printf(stderrp(), c"Incorrect number of command-line arguments\n") - rows = parse(Int64, argv, 2) # First command-line argument - cols = parse(Int64, argv, 3) # Second command-line argument + rows = argparse(Int64, argv, 2) # First command-line argument + cols = argparse(Int64, argv, 3) # Second command-line argument # LHS A = MallocArray{Float64}(undef, rows, cols) @@ -41,9 +41,8 @@ function loopvec_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) # Print to stdout printf(C) # Also print to file - fp = fopen(c"table.tsv",c"w") - printf(fp, C) - fclose(fp) + printdlm(c"table.tsv", C, '\t') + fwrite(c"table.b", C) # Clean up matrices free(A) free(B) diff --git a/test/scripts/loopvec_matrix_stack.jl b/test/scripts/loopvec_matrix_stack.jl new file mode 100644 index 00000000..5e0c90d7 --- /dev/null +++ b/test/scripts/loopvec_matrix_stack.jl @@ -0,0 +1,49 @@ +using StaticCompiler +using StaticTools +using LoopVectorization + +@inline function mul!(C::StackArray, A::StackArray, B::StackArray) + @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) + Cmn = zero(eltype(C)) + for k ∈ indices((A,B), (2,1)) + Cmn += A[m,k] * B[k,n] + end + C[m,n] = Cmn + end + return C +end + +function loopvec_matrix_stack() + rows = 10 + cols = 5 + + # LHS + A = StackArray{Float64}(undef, rows, cols) + @turbo for i ∈ axes(A, 1) + for j ∈ axes(A, 2) + A[i,j] = i*j + end + end + + # RHS + B = StackArray{Float64}(undef, cols, rows) + @turbo for i ∈ axes(B, 1) + for j ∈ axes(B, 2) + B[i,j] = i*j + end + end + + # # Matrix multiplication + C = StackArray{Float64}(undef, cols, cols) + mul!(C, B, A) + + # Print to stdout + printf(C) + # Also print to file + fp = fopen(c"table.tsv",c"w") + printf(fp, C) + fclose(fp) +end + +# Attempt to compile +path = compile_executable(loopvec_matrix_stack, (), "./") diff --git a/test/scripts/loopvec_product.jl b/test/scripts/loopvec_product.jl index bffa69c2..83424504 100644 --- a/test/scripts/loopvec_product.jl +++ b/test/scripts/loopvec_product.jl @@ -4,8 +4,8 @@ using LoopVectorization function loopvec_product(argc::Int, argv::Ptr{Ptr{UInt8}}) argc == 3 || return printf(stderrp(), c"Incorrect number of command-line arguments\n") - rows = parse(Int64, argv, 2) # First command-line argument - cols = parse(Int64, argv, 3) # Second command-line argument + rows = argparse(Int64, argv, 2) # First command-line argument + cols = argparse(Int64, argv, 3) # Second command-line argument s = 0 @turbo for i=1:rows diff --git a/test/scripts/rand_matrix.jl b/test/scripts/rand_matrix.jl index ce9fe461..604e44a0 100644 --- a/test/scripts/rand_matrix.jl +++ b/test/scripts/rand_matrix.jl @@ -3,9 +3,10 @@ using StaticTools function rand_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) argc == 3 || return printf(stderrp(), c"Incorrect number of command-line arguments\n") - rows = parse(Int64, argv, 2) # First command-line argument - cols = parse(Int64, argv, 3) # Second command-line argument + rows = argparse(Int64, argv, 2) # First command-line argument + cols = argparse(Int64, argv, 3) # Second command-line argument + # Manually fil matrix M = MallocArray{Float64}(undef, rows, cols) rng = static_rng() @inbounds for i=1:rows @@ -18,4 +19,5 @@ function rand_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) end # Attempt to compile -path = compile_executable(rand_matrix, (Int64, Ptr{Ptr{UInt8}}), "./") +# cflags=`-lm`: need to explicitly include libm math library on linux +path = compile_executable(rand_matrix, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-lm`) diff --git a/test/scripts/randn_matrix.jl b/test/scripts/randn_matrix.jl new file mode 100644 index 00000000..065d4662 --- /dev/null +++ b/test/scripts/randn_matrix.jl @@ -0,0 +1,22 @@ +using StaticCompiler +using StaticTools + +function randn_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), 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 + + M = MallocArray{Float64}(undef, rows, cols) + rng = MarsagliaPolar(static_rng()) + @inbounds for i=1:rows + for j=1:cols + M[i,j] = randn(rng) + end + end + printf(M) + free(M) +end + +# Attempt to compile +# cflags=`-lm`: need to explicitly include libm math library on linux +path = compile_executable(randn_matrix, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-lm`) diff --git a/test/scripts/times_table.jl b/test/scripts/times_table.jl index 2dab3937..7d295eb0 100644 --- a/test/scripts/times_table.jl +++ b/test/scripts/times_table.jl @@ -3,8 +3,8 @@ using StaticTools function times_table(argc::Int, argv::Ptr{Ptr{UInt8}}) argc == 3 || return printf(stderrp(), c"Incorrect number of command-line arguments\n") - rows = parse(Int64, argv, 2) # First command-line argument - cols = parse(Int64, argv, 3) # Second command-line argument + rows = argparse(Int64, argv, 2) # First command-line argument + cols = argparse(Int64, argv, 3) # Second command-line argument M = MallocArray{Int64}(undef, rows, cols) @inbounds for i=1:rows @@ -15,9 +15,8 @@ function times_table(argc::Int, argv::Ptr{Ptr{UInt8}}) # Print to stdout printf(M) # Also print to file - fp = fopen(c"table.tsv",c"w") - printf(fp, M) - fclose(fp) + fwrite(c"table.b", M) + printdlm(c"table.tsv", M) # Clean up matrix free(M) end diff --git a/test/scripts/withmallocarray.jl b/test/scripts/withmallocarray.jl new file mode 100644 index 00000000..29301214 --- /dev/null +++ b/test/scripts/withmallocarray.jl @@ -0,0 +1,31 @@ +using StaticCompiler +using StaticTools + +function withmallocarray(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), 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 + + mzeros(rows, cols) do A + printf(A) + end + mones(Int, rows, cols) do A + printf(A) + end + mfill(3.141592, rows, cols) do A + printf(A) + end + + # Random number generation + rng = MarsagliaPolar() + mrand(rng, rows, cols) do A + printf(A) + end + mrandn(rng, rows, cols) do A + printf(A) + end +end + +# Attempt to compile +# cflags=`-lm`: need to explicitly include libm math library on linux +path = compile_executable(withmallocarray, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-lm`) diff --git a/test/testintegration.jl b/test/testintegration.jl index 5fd783eb..f54a8083 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -1,82 +1,278 @@ @testset "Standalone Executable Integration" begin - + # Setup testpath = pwd() scratch = tempdir() cd(scratch) + jlpath = joinpath(Sys.BINDIR, Base.julia_exename()) # Get path to julia executable - # --- Times table, file IO, mallocarray - - # Compile... - # We have to start a new Julia process to get around the fact that Pkg.test - # disables `@inbounds`, but ironically we can use `--compile=min` to make that - # faster. - status = run(`julia --compile=min $testpath/scripts/times_table.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Attempt to run - println("5x5 times table:") - status = run(`./times_table 5 5`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - @test parsedlm(Int64, c"table.tsv", '\t') == (1:5)*(1:5)' - - # --- Random number generation - - # Compile... - status = run(`julia --compile=min $testpath/scripts/rand_matrix.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Run... - println("5x5 random matrix:") - status = run(`./rand_matrix 5 5`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # --- Test LoopVectorization integration - -@static if LoopVectorization.VectorizationBase.has_feature(Val{:x86_64_avx2}) - # Compile... - status = run(`julia --compile=min $testpath/scripts/loopvec_product.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Run... - println("10x10 table sum:") - status = run(`./loopvec_product 10 10`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - @test parsedlm(c"product.tsv",'\t')[] == 3025 -end + ## --- Times table, file IO, mallocarray + let + # Attempt to compile + # We have to start a new Julia process to get around the fact that Pkg.test + # disables `@inbounds`, but ironically we can use `--compile=min` to make that + # faster. + status = -1 + try + isfile("times_table") && rm("times_table") + status = run(`$jlpath --compile=min $testpath/scripts/times_table.jl`) + catch e + @warn "Could not compile $testpath/scripts/times_table.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Attempt to run + println("5x5 times table:") + status = -1 + try + status = run(`./times_table 5 5`) + catch e + @warn "Could not run $(scratch)/times_table" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + # Test ascii output + @test parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' + # Test binary output + @test fread!(szeros(Int, 5,5), c"table.b") == (1:5)*(1:5)' + end + + ## --- "withmallocarray"-type do-block pattern + let + # Compile... + status = -1 + try + isfile("withmallocarray") && rm("withmallocarray") + status = run(`$jlpath --compile=min $testpath/scripts/withmallocarray.jl`) + catch e + @warn "Could not compile $testpath/scripts/withmallocarray.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + println("3x3 malloc arrays via do-block syntax:") + status = -1 + try + status = run(`./withmallocarray 3 3`) + catch e + @warn "Could not run $(scratch)/withmallocarray" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + end + + ## --- Random number generation + let + # Compile... + status = -1 + try + isfile("rand_matrix") && rm("rand_matrix") + status = run(`$jlpath --compile=min $testpath/scripts/rand_matrix.jl`) + catch e + @warn "Could not compile $testpath/scripts/rand_matrix.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + println("5x5 uniform random matrix:") + status = -1 + try + status = run(`./rand_matrix 5 5`) + catch e + @warn "Could not run $(scratch)/rand_matrix" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + end + + let + # Compile... + status = -1 + try + isfile("randn_matrix") && rm("randn_matrix") + status = run(`$jlpath --compile=min $testpath/scripts/randn_matrix.jl`) + catch e + @warn "Could not compile $testpath/scripts/randn_matrix.jl" + println(e) + end + @static if Sys.isbsd() + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + end + + # Run... + println("5x5 Normal random matrix:") + status = -1 + try + status = run(`./randn_matrix 5 5`) + catch e + @warn "Could not run $(scratch)/randn_matrix" + println(e) + end + @static if Sys.isbsd() + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + end + end + + ## --- Test LoopVectorization integration + @static if LoopVectorization.VectorizationBase.has_feature(Val{:x86_64_avx2}) + let + # Compile... + status = -1 + try + isfile("loopvec_product") && rm("loopvec_product") + status = run(`$jlpath --compile=min $testpath/scripts/loopvec_product.jl`) + catch e + @warn "Could not compile $testpath/scripts/loopvec_product.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + println("10x10 table sum:") + status = -1 + try + status = run(`./loopvec_product 10 10`) + catch e + @warn "Could not run $(scratch)/loopvec_product" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + @test parsedlm(c"product.tsv",'\t')[] == 3025 + end + end + + let + # Compile... + status = -1 + try + isfile("loopvec_matrix") && rm("loopvec_matrix") + status = run(`$jlpath --compile=min $testpath/scripts/loopvec_matrix.jl`) + catch e + @warn "Could not compile $testpath/scripts/loopvec_matrix.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + println("10x5 matrix product:") + status = -1 + try + status = run(`./loopvec_matrix 10 5`) + catch e + @warn "Could not run $(scratch)/loopvec_matrix" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + A = (1:10) * (1:5)' + # Check ascii output + @test parsedlm(c"table.tsv",'\t') == A' * A + # Check binary output + @test fread!(szeros(5,5), c"table.b") == A' * A + end + + let + # Compile... + status = -1 + try + isfile("loopvec_matrix_stack") && rm("loopvec_matrix_stack") + status = run(`$jlpath --compile=min $testpath/scripts/loopvec_matrix_stack.jl`) + catch e + @warn "Could not compile $testpath/scripts/loopvec_matrix_stack.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + println("10x5 matrix product:") + status = -1 + try + status = run(`./loopvec_matrix_stack`) + catch e + @warn "Could not run $(scratch)/loopvec_matrix_stack" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + A = (1:10) * (1:5)' + @test parsedlm(c"table.tsv",'\t') == A' * A + end + + + ## --- Test string handling + + let + # Compile... + status = -1 + try + isfile("print_args") && rm("print_args") + status = run(`$jlpath --compile=min $testpath/scripts/print_args.jl`) + catch e + @warn "Could not compile $testpath/scripts/print_args.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + println("String indexing and handling:") + status = -1 + try + status = run(`./print_args foo bar`) + catch e + @warn "Could not run $(scratch)/print_args" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + end + + ## --- Test interop + + @static if Sys.isbsd() + let + # Compile... + status = -1 + try + isfile("interop") && rm("interop") + status = run(`$jlpath --compile=min $testpath/scripts/interop.jl`) + catch e + @warn "Could not compile $testpath/scripts/interop.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + println("Interop:") + status = -1 + try + status = run(`./interop`) + catch e + @warn "Could not run $(scratch)/interop" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + end + end + + ## --- Clean up - # Compile... - status = run(`julia --compile=min $testpath/scripts/loopvec_matrix.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Run... - println("10x3 matrix product:") - status = run(`./loopvec_matrix 10 3`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - A = (1:10) * (1:3)' - @test parsedlm(c"table.tsv",'\t') == A' * A - - # --- Test string handling - - # Compile... - status = run(`julia --compile=min $testpath/scripts/print_args.jl`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # Run... - println("String indexing and handling:") - status = run(`./print_args foo bar`) - @test isa(status, Base.Process) - @test status.exitcode == 0 - - # --- Clean up cd(testpath) end From 5e4f03febc5b8a816ef24a13c5a72019133f7a79 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Thu, 20 Oct 2022 14:49:24 -0400 Subject: [PATCH 097/202] Revert "Merge branch 'errors-are-fine' into master" This reverts commit 89aded496846fdadab46b4720ebbdf367e105b20, reversing changes made to 38dbdbef14ca2f484661221ad238af4b5b75c0dc. --- Project.toml | 2 - src/StaticCompiler.jl | 407 ++++++++++++++++++++---------------------- src/quirks.jl | 89 --------- src/target.jl | 61 +++---- src/utils.jl | 69 ------- 5 files changed, 215 insertions(+), 413 deletions(-) delete mode 100644 src/quirks.jl delete mode 100644 src/utils.jl diff --git a/Project.toml b/Project.toml index 18616d2f..bfea97f0 100644 --- a/Project.toml +++ b/Project.toml @@ -9,10 +9,8 @@ GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" -StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] GPUCompiler = "0.16" LLVM = "4.8" julia = "1.7" -StaticTools = "0.8" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index baba6c69..5a447cd0 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -8,13 +8,10 @@ using Libdl: Libdl, dlsym, dlopen using Base: RefValue using Serialization: serialize, deserialize using Clang_jll: clang -using StaticTools: @symbolcall, @c_str export compile, load_function, compile_shlib, compile_executable export native_code_llvm, native_code_typed, native_llvm_module, native_code_native -include("utils.jl") -include("quirks.jl") include("target.jl") include("pointer_patching.jl") include("code_loading.jl") @@ -89,7 +86,7 @@ single method (the method determined by `types`). function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_name(repr(f)), filename="obj", strip_llvm = false, strip_asm = true, - opt_level = 3, + opt_level=3, kwargs...) tt = Base.to_tuple_type(_tt) isconcretetype(tt) || error("input type signature $_tt is not concrete") @@ -112,34 +109,37 @@ end generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; \tstrip_llvm = false, \tstrip_asm = true, - \topt_level = 3, + \topt_level=3, \tkwargs...) ``` Low level interface for compiling object code (`.o`) for for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. + ### Examples ```julia julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) fib (generic function with 1 method) + julia> path, name, table = StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test") ("./test", "fib", IdDict{Any, String}()) + shell> tree \$path ./test └── obj.o + 0 directories, 1 file ``` """ function generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; strip_llvm = false, strip_asm = true, - libjulia = true, - opt_level = 3, + opt_level=3, kwargs...) mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - tm = GPUCompiler.llvm_machine(libjulia ? NativeCompilerTarget() : ExternalNativeCompilerTarget()) - job, kwargs = native_job(f, tt; name, libjulia, kwargs...) + tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) + job, kwargs = native_job(f, tt; name, kwargs...) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false, ctx=context) @@ -168,16 +168,94 @@ function generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_ path, name, table end +""" +```julia +compile_executable(f, types::Tuple, path::String, name::String=repr(f); filename::String=name, kwargs...) +``` +Attempt to compile a standalone executable that runs function `f` with a type signature given by the tuple of `types`. + +### Examples +```julia +julia> using StaticCompiler + +julia> function puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates. + # Note, this `llvmcall` requires Julia 1.8+ + Base.llvmcall((\""" + ; External declaration of the puts function + declare i32 @puts(i8* nocapture) nounwind + + define i32 @main(i8*) { + entry: + %call = call i32 (i8*) @puts(i8* %0) + ret i32 0 + } + \""", "main"), Int32, Tuple{Ptr{UInt8}}, s) + end +puts (generic function with 1 method) + +julia> function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) + for i=1:argc + # Get pointer + p = unsafe_load(argv, i) + # Print string at pointer location (which fortunately already exists isn't tracked by the GC) + puts(p) + end + return 0 + end + +julia> compile_executable(print_args, (Int, Ptr{Ptr{UInt8}})) +"/Users/foo/code/StaticCompiler.jl/print_args" + +shell> ./print_args 1 2 3 4 Five +./print_args +1 +2 +3 +4 +Five +``` +```julia +julia> using StaticTools # So you don't have to define `puts` and friends every time + +julia> hello() = println(c"Hello, world!") # c"..." makes a stack-allocated StaticString + +julia> compile_executable(hello) +"/Users/foo/code/StaticCompiler.jl/hello" + +shell> ./hello +Hello, world! +``` +""" +function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); + filename=name, + cflags=``, + kwargs... + ) + + tt = Base.to_tuple_type(types) + # tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})") + + rt = only(native_code_typed(f, tt))[2] + isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") + + # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals + # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this + + generate_executable(f, tt, path, name, filename; cflags=cflags, kwargs...) + + joinpath(abspath(path), filename) +end + + """ ```julia compile_shlib(f, types::Tuple, path::String, name::String=repr(f); filename::String=name, kwargs...) ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. """ -function compile_shlib(f::Function, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); - filename = name, - libjulia = false, - cflags = ``, +function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); + filename=name, + cflags=``, kwargs... ) @@ -189,17 +267,16 @@ function compile_shlib(f::Function, types=(), path::String="./", name=GPUCompile # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - generate_shlib(f, tt, path, name, filename; libjulia, cflags, kwargs...) + generate_shlib(f, tt, path, name, filename; cflags=cflags, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=name; temp::Bool=true, - libjulia=true, kwargs...) - generate_shlib(f, tt, path, name; libjulia, kwargs...) + generate_shlib(f, tt, path, name; kwargs...) lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @@ -254,6 +331,69 @@ function generate_shlib_fptr(path::String, name, filename::String=name) fptr end +""" +```julia +generate_executable(f, tt, path::String, name, filename=string(name); kwargs...) +``` +Attempt to compile a standalone executable that runs `f`. + +### Examples +```julia +julia> function test(n) + r = 0.0 + for i=1:n + r += log(sqrt(i)) + end + return r/n + end +test (generic function with 1 method) + +julia> path, name = StaticCompiler.generate_executable(test, Tuple{Int64}, "./scratch") +``` +""" +function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name); + cflags=``, + kwargs... + ) + mkpath(path) + obj_path = joinpath(path, "$filename.o") + exec_path = joinpath(path, filename) + job, kwargs = native_job(f, tt; name, kwargs...) + obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) + + # Write to file + open(obj_path, "w") do io + write(io, obj) + end + + # Pick a compiler + cc = Sys.isapple() ? `cc` : clang() + # Compile! + if Sys.isapple() + # Apple no longer uses _start, so we can just specify a custom entry + entry = "_julia_$name" + run(`$cc -e $entry $cflags $obj_path -o $exec_path`) + else + # Write a minimal wrapper to avoid having to specify a custom entry + wrapper_path = joinpath(path, "wrapper.c") + f = open(wrapper_path, "w") + print(f, """int julia_$name(int argc, char** argv); + void* __stack_chk_guard = (void*) $(rand(UInt) >> 1); + + int main(int argc, char** argv) + { + julia_$name(argc, argv); + return 0; + }""") + close(f) + run(`$cc $wrapper_path $cflags $obj_path -o $exec_path`) + # Clean up + run(`rm $wrapper_path`) + end + + path, name +end + """ ```julia generate_shlib(f, tt, path::String, name::String, filenamebase::String="obj"; kwargs...) @@ -286,15 +426,14 @@ julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), ``` """ function generate_shlib(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=name; - libjulia = false, - cflags = ``, + cflags=``, kwargs... ) mkpath(path) obj_path = joinpath(path, "$filename.o") lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - job, kwargs = native_job(f, tt; name, libjulia, kwargs...) + job, kwargs = native_job(f, tt; name, kwargs...) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) open(obj_path, "w") do io @@ -335,12 +474,12 @@ end #Return an LLVM module for multiple functions function native_llvm_module(funcs::Array; demangle = false, kwargs...) - f,tt = first(funcs) - mod = native_llvm_module(f,tt; kwargs...) + f,tt = funcs[1] + mod = native_llvm_module(f,tt, kwargs...) if length(funcs) > 1 for func in funcs[2:end] @show f,tt = func - tmod = native_llvm_module(f,tt; kwargs...) + tmod = native_llvm_module(f,tt, kwargs...) link!(mod,tmod) end end @@ -359,60 +498,17 @@ function native_llvm_module(funcs::Array; demangle = false, kwargs...) return mod end -## -- compile_shlib / generate_shlib / generate_obj, but for multiple functions at once - -function compile_shlib(funcs::Vector{<:Tuple}, path::String="./"; - filename = "libfoo", - demangle = false, - libjulia = false, - cflags = ``, - kwargs... - ) - for func in funcs - f, types = func - tt = Base.to_tuple_type(types) - isconcretetype(tt) || error("input type signature $types is not concrete") - - rt = only(native_code_typed(f, tt))[2] - isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") - end - - funcs_tt = [(first(f), Base.to_tuple_type(last(f))) for f in funcs] - generate_shlib(funcs_tt, path, filename; demangle, libjulia, cflags, kwargs...) - - joinpath(abspath(path), filename * "." * Libdl.dlext) -end - -function generate_shlib(funcs::Vector{<:Tuple}, path::String = tempname(), filename::String="libfoo"; - demangle = false, - libjulia = false, - cflags = ``, - kwargs... - ) - - lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - - _,obj_path = generate_obj(funcs, path, filename; demangle, libjulia, kwargs...) - # Pick a Clang - cc = Sys.isapple() ? `cc` : clang() - # Compile! - run(`$cc -shared $cflags $obj_path -o $lib_path `) - - path, name -end - -function generate_obj(funcs::Vector{<:Tuple}, path::String = tempname(), filenamebase::String="obj"; - demangle = false, - libjulia = false, +function generate_obj(funcs::Array, path::String = tempname(), filenamebase::String="obj"; + demangle =false, strip_llvm = false, - strip_asm = true, - opt_level = 3, + strip_asm = true, + opt_level=3, kwargs...) - f,tt = first(funcs) + f,tt = funcs[1] mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - fakejob, kwargs = native_job(f, tt; libjulia, kwargs...) - mod = native_llvm_module(funcs; demangle, libjulia, kwargs...) + fakejob, kwargs = native_job(f,tt, kwargs...) + mod = native_llvm_module(funcs; demangle = demangle, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io write(io, obj) @@ -420,159 +516,44 @@ function generate_obj(funcs::Vector{<:Tuple}, path::String = tempname(), filenam path, obj_path end -## --- Compile standalone binaries - -""" -```julia -function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); - filename = name, - libjulia = false, - cflags = ``, +function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; + demangle=false, + cflags=``, kwargs... ) -``` -Attempt to compile a standalone executable that runs function `f` with a type signature given by the tuple of `types`. - -### Examples -```julia -julia> using StaticCompiler - -julia> function puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates. - # Note, this `llvmcall` requires Julia 1.8+ - Base.llvmcall((\""" - ; External declaration of the puts function - declare i32 @puts(i8* nocapture) nounwind - - define i32 @main(i8*) { - entry: - %call = call i32 (i8*) @puts(i8* %0) - ret i32 0 - } - \""", "main"), Int32, Tuple{Ptr{UInt8}}, s) - end -puts (generic function with 1 method) -julia> function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) - for i=1:argc - # Get pointer - p = unsafe_load(argv, i) - # Print string at pointer location (which fortunately already exists isn't tracked by the GC) - puts(p) - end - return 0 - end - -julia> compile_executable(print_args, (Int, Ptr{Ptr{UInt8}})) -"/Users/foo/code/StaticCompiler.jl/print_args" - -shell> ./print_args 1 2 3 4 Five -./print_args -1 -2 -3 -4 -Five -``` -```julia -julia> using StaticTools # So you don't have to define `puts` and friends every time - -julia> hello() = println(c"Hello, world!") # c"..." makes a stack-allocated StaticString - -julia> compile_executable(hello) -"/Users/foo/code/StaticCompiler.jl/hello" - -shell> ./hello -Hello, world! -``` -""" -function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); - filename = name, - libjulia = false, - cflags = ``, - kwargs... - ) - - tt = Base.to_tuple_type(types) - # tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})") - - rt = only(native_code_typed(f, tt))[2] - isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") - - # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals - # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this + lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - generate_executable(f, tt, path, name, filename; libjulia, cflags, kwargs...) + _,obj_path = generate_obj(funcs, path, filename; demangle=demangle, kwargs...) + # Pick a Clang + cc = Sys.isapple() ? `cc` : clang() + # Compile! + run(`$cc -shared $cflags $obj_path -o $lib_path `) - joinpath(abspath(path), filename) + path, name end +function compile_shlib(funcs::Array, path::String="./"; + filename="libfoo", + demangle=false, + cflags=``, + kwargs...) + for func in funcs + f, types = func + tt = Base.to_tuple_type(types) + isconcretetype(tt) || error("input type signature $types is not concrete") -""" -```julia -function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name); - cflags = ``, - libjulia = false, - kwargs... - ) -``` -Attempt to compile a standalone executable that runs `f`. - -### Examples -```julia -julia> function test(n) - r = 0.0 - for i=1:n - r += log(sqrt(i)) - end - return r/n - end -test (generic function with 1 method) - -julia> path, name = StaticCompiler.generate_executable(test, Tuple{Int64}, "./scratch") -``` -""" -function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name); - cflags = ``, - libjulia = false, - kwargs... - ) - mkpath(path) - obj_path = joinpath(path, "$filename.o") - exec_path = joinpath(path, filename) - job, kwargs = native_job(f, tt; name, libjulia, kwargs...) - obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) - - # Write to file - open(obj_path, "w") do io - write(io, obj) + rt = only(native_code_typed(f, tt))[2] + isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") end - # Pick a compiler - cc = Sys.isapple() ? `cc` : clang() - # Compile! - if Sys.isapple() - # Apple no longer uses _start, so we can just specify a custom entry - entry = "_julia_$name" - run(`$cc -e $entry $cflags $obj_path -o $exec_path`) - else - # Write a minimal wrapper to avoid having to specify a custom entry - wrapper_path = joinpath(path, "wrapper.c") - f = open(wrapper_path, "w") - print(f, """int julia_$name(int argc, char** argv); - void* __stack_chk_guard = (void*) $(rand(UInt) >> 1); +# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals +# Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - int main(int argc, char** argv) - { - julia_$name(argc, argv); - return 0; - }""") - close(f) - run(`$cc $wrapper_path $cflags $obj_path -o $exec_path`) - # Clean up - run(`rm $wrapper_path`) - end + generate_shlib(funcs, path, filename; demangle=demangle, cflags=cflags, kwargs...) - path, name + joinpath(abspath(path), filename * "." * Libdl.dlext) end + end # module diff --git a/src/quirks.jl b/src/quirks.jl deleted file mode 100644 index d632dd78..00000000 --- a/src/quirks.jl +++ /dev/null @@ -1,89 +0,0 @@ -macro print_and_throw(err) - quote - println(err) - libcexit(Int32(1)) - end -end - -# math.jl -@device_override @noinline Base.Math.throw_complex_domainerror(f::Symbol, x) = - @print_and_throw c"This operation requires a complex input to return a complex result" -@device_override @noinline Base.Math.throw_exp_domainerror(f::Symbol, x) = - @print_and_throw c"Exponentiation yielding a complex result requires a complex argument" - -# intfuncs.jl -@device_override @noinline Base.throw_domerr_powbysq(::Any, p) = - @print_and_throw c"Cannot raise an integer to a negative power" -@device_override @noinline Base.throw_domerr_powbysq(::Integer, p) = - @print_and_throw c"Cannot raise an integer to a negative power" -@device_override @noinline Base.throw_domerr_powbysq(::AbstractMatrix, p) = - @print_and_throw c"Cannot raise an integer to a negative power" -@device_override @noinline Base.__throw_gcd_overflow(a, b) = - @print_and_throw c"gcd overflow" - -# checked.jl -@device_override @noinline Base.Checked.throw_overflowerr_binaryop(op, x, y) = - @print_and_throw c"Binary operation overflowed" -@device_override @noinline Base.Checked.throw_overflowerr_negation(op, x, y) = - @print_and_throw c"Negation overflowed" -@device_override function Base.Checked.checked_abs(x::Base.Checked.SignedInt) - r = ifelse(x<0, -x, x) - r<0 && @print_and_throw(c"checked arithmetic: cannot compute |x|") - r -end - -# boot.jl -@device_override @noinline Core.throw_inexacterror(f::Symbol, ::Type{T}, val) where {T} = - @print_and_throw c"Inexact conversion" - -# abstractarray.jl -@device_override @noinline Base.throw_boundserror(A, I) = - @print_and_throw c"Out-of-bounds array access" - -# trig.jl -@device_override @noinline Base.Math.sincos_domain_error(x) = - @print_and_throw c"sincos(x) is only defined for finite x." - - -# range.jl -@static if VERSION >= v"1.7-" - @eval begin - @device_override function Base.StepRangeLen{T,R,S,L}(ref::R, step::S, len::Integer, - offset::Integer=1) where {T,R,S,L} - if T <: Integer && !isinteger(ref + step) - @print_and_throw(c"StepRangeLen{<:Integer} cannot have non-integer step") - end - len = convert(L, len) - len >= zero(len) || @print_and_throw(c"StepRangeLen length cannot be negative") - offset = convert(L, offset) - L1 = oneunit(typeof(len)) - L1 <= offset <= max(L1, len) || @print_and_throw(c"StepRangeLen: offset must be in [1,...]") - $( - Expr(:new, :(StepRangeLen{T,R,S,L}), :ref, :step, :len, :offset) - ) - end - end -else - @device_override function Base.StepRangeLen{T,R,S}(ref::R, step::S, len::Integer, - offset::Integer=1) where {T,R,S} - if T <: Integer && !isinteger(ref + step) - @print_and_throw(c"StepRangeLen{<:Integer} cannot have non-integer step") - end - len >= 0 || @print_and_throw(c"StepRangeLen length cannot be negative") - 1 <= offset <= max(1,len) || @print_and_throw(c"StepRangeLen: offset must be in [1,...]") - new(ref, step, len, offset) - end -end - - -# fastmath.jl -@static if VERSION <= v"1.7-" -## prevent fallbacks to libm -for f in (:acosh, :asinh, :atanh, :cbrt, :cosh, :exp2, :expm1, :log1p, :sinh, :tanh) - f_fast = Base.FastMath.fast_op[f] - @eval begin - @device_override Base.FastMath.$f_fast(x::Float32) = $f(x) - @device_override Base.FastMath.$f_fast(x::Float64) = $f(x) - end -end -end diff --git a/src/target.jl b/src/target.jl index b6713b81..2efb191c 100644 --- a/src/target.jl +++ b/src/target.jl @@ -3,11 +3,21 @@ Base.@kwdef struct NativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) end -Base.@kwdef struct ExternalNativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget - cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) - features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) +GPUCompiler.llvm_triple(::NativeCompilerTarget) = Sys.MACHINE + +function GPUCompiler.llvm_machine(target::NativeCompilerTarget) + triple = GPUCompiler.llvm_triple(target) + + t = LLVM.Target(triple=triple) + + tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) + GPUCompiler.asm_verbosity!(tm, true) + + return tm end +GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = "native_$(job.target.cpu)-$(hash(job.target.features))" + module StaticRuntime # the runtime library signal_exception() = return @@ -20,46 +30,17 @@ end struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams end -for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) - @eval begin - GPUCompiler.llvm_triple(::$target) = Sys.MACHINE - - function GPUCompiler.llvm_machine(target::$target) - triple = GPUCompiler.llvm_triple(target) - - t = LLVM.Target(triple=triple) - - tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) - GPUCompiler.asm_verbosity!(tm, true) - - return tm - end - - GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{$target}) = "native_$(job.target.cpu)-$(hash(job.target.features))" - - GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target}) = StaticRuntime - GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = StaticRuntime - - - GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = true - GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target}) = true - end -end - GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = StaticRuntime -GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = true +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{NativeCompilerTarget}) = StaticRuntime +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams}) = StaticRuntime -GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget})) = method_table -GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget, StaticCompilerParams})) = method_table +GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = true +GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams}) = true +GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = true -function native_job(@nospecialize(func::Function), @nospecialize(types::Type); - name = GPUCompiler.safe_name(repr(func)), - libjulia::Bool = true, - kernel::Bool = false, - kwargs... - ) - source = GPUCompiler.FunctionSpec(func, types, kernel, name) - target = libjulia ? NativeCompilerTarget() : ExternalNativeCompilerTarget() +function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) + source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) + target = NativeCompilerTarget() params = StaticCompilerParams() GPUCompiler.CompilerJob(target, source, params), kwargs end diff --git a/src/utils.jl b/src/utils.jl deleted file mode 100644 index 636b147a..00000000 --- a/src/utils.jl +++ /dev/null @@ -1,69 +0,0 @@ - - -# local method table for device functions -@static if isdefined(Base.Experimental, Symbol("@overlay")) -Base.Experimental.@MethodTable(method_table) -else -const method_table = nothing -end - -# list of overrides (only for Julia 1.6) -const overrides = Expr[] - -macro device_override(ex) - ex = macroexpand(__module__, ex) - if Meta.isexpr(ex, :call) - @show ex = eval(ex) - error() - end - code = quote - $GPUCompiler.@override(StaticCompiler.method_table, $ex) - end - if isdefined(Base.Experimental, Symbol("@overlay")) - return esc(code) - else - push!(overrides, code) - return - end -end - -macro device_function(ex) - ex = macroexpand(__module__, ex) - def = splitdef(ex) - - # generate a function that errors - def[:body] = quote - error("This function is not intended for use on the CPU") - end - - esc(quote - $(combinedef(def)) - @device_override $ex - end) -end - -macro device_functions(ex) - ex = macroexpand(__module__, ex) - - # recursively prepend `@device_function` to all function definitions - function rewrite(block) - out = Expr(:block) - for arg in block.args - if Meta.isexpr(arg, :block) - # descend in blocks - push!(out.args, rewrite(arg)) - elseif Meta.isexpr(arg, [:function, :(=)]) - # rewrite function definitions - push!(out.args, :(@device_function $arg)) - else - # preserve all the rest - push!(out.args, arg) - end - end - out - end - - esc(rewrite(ex)) -end - -libcexit(x::Int32) = @symbolcall exit(x::Int32)::Nothing From 63eed827707ee27f35af70b1d8d5dde9f6f48c14 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 20 Oct 2022 12:55:53 -0600 Subject: [PATCH 098/202] test 1.8.0-beta3 -> 1.8 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dddaf90d..341215a2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: matrix: version: - '1.7' - - '1.8.0-beta3' + - '1.8' os: - ubuntu-latest - macOS-latest From 6679965c071109d902ef7d5aaba4f996ac377ada Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 20 Oct 2022 12:56:30 -0600 Subject: [PATCH 099/202] 1.8.0-beta 3 -> 1.8 --- .github/workflows/ci-integration.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index 03fa4d28..3b9024df 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -18,7 +18,7 @@ jobs: fail-fast: false matrix: version: - - '1.8.0-beta3' + - '1.8' os: - ubuntu-latest - macOS-latest From c8cfd3ff36c26df230deed76366c780d72060dc5 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 20 Oct 2022 13:31:10 -0600 Subject: [PATCH 100/202] run without startup files --- test/testintegration.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/testintegration.jl b/test/testintegration.jl index f54a8083..6eb2c08c 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -15,7 +15,7 @@ status = -1 try isfile("times_table") && rm("times_table") - status = run(`$jlpath --compile=min $testpath/scripts/times_table.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/times_table.jl`) catch e @warn "Could not compile $testpath/scripts/times_table.jl" println(e) @@ -46,7 +46,7 @@ status = -1 try isfile("withmallocarray") && rm("withmallocarray") - status = run(`$jlpath --compile=min $testpath/scripts/withmallocarray.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/withmallocarray.jl`) catch e @warn "Could not compile $testpath/scripts/withmallocarray.jl" println(e) @@ -73,7 +73,7 @@ status = -1 try isfile("rand_matrix") && rm("rand_matrix") - status = run(`$jlpath --compile=min $testpath/scripts/rand_matrix.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/rand_matrix.jl`) catch e @warn "Could not compile $testpath/scripts/rand_matrix.jl" println(e) @@ -99,7 +99,7 @@ status = -1 try isfile("randn_matrix") && rm("randn_matrix") - status = run(`$jlpath --compile=min $testpath/scripts/randn_matrix.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/randn_matrix.jl`) catch e @warn "Could not compile $testpath/scripts/randn_matrix.jl" println(e) @@ -131,7 +131,7 @@ status = -1 try isfile("loopvec_product") && rm("loopvec_product") - status = run(`$jlpath --compile=min $testpath/scripts/loopvec_product.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/loopvec_product.jl`) catch e @warn "Could not compile $testpath/scripts/loopvec_product.jl" println(e) @@ -159,7 +159,7 @@ status = -1 try isfile("loopvec_matrix") && rm("loopvec_matrix") - status = run(`$jlpath --compile=min $testpath/scripts/loopvec_matrix.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/loopvec_matrix.jl`) catch e @warn "Could not compile $testpath/scripts/loopvec_matrix.jl" println(e) @@ -190,7 +190,7 @@ status = -1 try isfile("loopvec_matrix_stack") && rm("loopvec_matrix_stack") - status = run(`$jlpath --compile=min $testpath/scripts/loopvec_matrix_stack.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/loopvec_matrix_stack.jl`) catch e @warn "Could not compile $testpath/scripts/loopvec_matrix_stack.jl" println(e) @@ -221,7 +221,7 @@ status = -1 try isfile("print_args") && rm("print_args") - status = run(`$jlpath --compile=min $testpath/scripts/print_args.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/print_args.jl`) catch e @warn "Could not compile $testpath/scripts/print_args.jl" println(e) @@ -250,7 +250,7 @@ status = -1 try isfile("interop") && rm("interop") - status = run(`$jlpath --compile=min $testpath/scripts/interop.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/interop.jl`) catch e @warn "Could not compile $testpath/scripts/interop.jl" println(e) From d5e15321140b0c6124ab470da164915f849dfb4e Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 20 Oct 2022 14:26:00 -0600 Subject: [PATCH 101/202] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c45e1054..cdffd9f8 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # StaticCompiler -[![CI](https://github.com/tshort/StaticCompiler.jl/workflows/CI/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml) -[![CI (Integration)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Integration)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration.yml) +[![CI](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml) +[![CI (Integration)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration.yml) [![CI (Julia nightly)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Julia%20nightly)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-julia-nightly.yml) -[![CI (Integration nightly)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Integration%20nightly)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration-nightly.yml) +[![CI (Integration nightly)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration-nightly.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration-nightly.yml) [![Coverage](https://codecov.io/gh/tshort/StaticCompiler.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/tshort/StaticCompiler.jl) This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. From 1bbff0c7e7ce7b5dea85c0fefab10bd593c18742 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 20 Oct 2022 15:04:47 -0600 Subject: [PATCH 102/202] change pointer_patching_diff --- src/pointer_patching.jl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/pointer_patching.jl b/src/pointer_patching.jl index 18be73ec..4c59f4fd 100644 --- a/src/pointer_patching.jl +++ b/src/pointer_patching.jl @@ -155,7 +155,17 @@ end llvmeltype(x::LLVM.Value) = eltype(LLVM.llvmtype(x)) -function pointer_patching_diff(mod::LLVM.Module, path1=tempname(), path2=tempname(); show_reloc_table=false) +function pointer_patching_diff(f, tt, path1=tempname(), path2=tempname(); show_reloc_table=false) + tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) + job, kwargs = native_job(f, tt; name=GPUCompiler.safe_name(repr(f))) + #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. + mod, meta = GPUCompiler.JuliaContext() do context + GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, optimize=false, ctx=context) + end + # Use Enzyme's annotation and optimization pipeline + annotate!(mod) + optimize!(mod, tm) + s1 = string(mod) write(path1, s1) @@ -167,12 +177,9 @@ function pointer_patching_diff(mod::LLVM.Module, path1=tempname(), path2=tempnam s2 = string(mod) write(path2, s2) - try - # this always ends in an error for me for some reason - run(`diff $path1 $path2`) - catch e; - nothing - end + pdiff = run(Cmd(`diff $path1 $path2`, ignorestatus=true)) + pdiff.exitcode == 2 && error("Showing diff caused an error") + nothing end From c825a4d6fa7b2ab5e29cf9d2a8778dc481e28a85 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Thu, 20 Oct 2022 20:38:47 -0400 Subject: [PATCH 103/202] Update readme to explain both compilation interfaces --- README.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cdffd9f8..6c088082 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,13 @@ 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 ```julia using Pkg Pkg.add("StaticCompiler") ``` +There are two main ways to use this package. The first is via the `compile` function, which can be used when you want to compile a Julia function for later use from within Julia: ```julia julia> using StaticCompiler @@ -40,9 +41,25 @@ 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`). + +The second way to use this package is via the `compile_executable` and `compile_shlib` functions, for use when you want to compile a Julia function to a native executable or shared library for use from outside of Julia: +```julia +julia> using StaticCompiler, StaticTools + +julia> hello() = println(c"Hello, world!") +hello (generic function with 1 method) -See the file `tests/runtests.jl` for some examples of functions that work with static compilation (and some that don't, -marked with `@test_skip`) +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](https://github.com/brenhinkeller/StaticTools.jl) for some ways to work around these limitations). ## Approach From 1cb977f303cd3809b3325ceb41a83a4c0b5a167c Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sat, 11 Mar 2023 12:23:24 -0500 Subject: [PATCH 104/202] Update ci versions (#96) * Also test integration on 1.9 prerelease version * Also run main tests on Julia 1.9 prerelease --- .github/workflows/ci-integration-nightly.yml | 1 + .github/workflows/ci-julia-nightly.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/ci-integration-nightly.yml b/.github/workflows/ci-integration-nightly.yml index 00074acc..6d4cb3e2 100644 --- a/.github/workflows/ci-integration-nightly.yml +++ b/.github/workflows/ci-integration-nightly.yml @@ -19,6 +19,7 @@ jobs: matrix: version: - 'nightly' + - '~1.9.0-0' os: - ubuntu-latest - macOS-latest diff --git a/.github/workflows/ci-julia-nightly.yml b/.github/workflows/ci-julia-nightly.yml index d01adba2..dfb2ad8f 100644 --- a/.github/workflows/ci-julia-nightly.yml +++ b/.github/workflows/ci-julia-nightly.yml @@ -19,6 +19,7 @@ jobs: matrix: version: - 'nightly' + - '~1.9.0-0' os: - ubuntu-latest - macOS-latest From 25f612344e9c4e79b1f8c4c1d1761cf4bd877573 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 11 Mar 2023 12:25:46 -0500 Subject: [PATCH 105/202] CompatHelper: bump compat for GPUCompiler to 0.17, (keep existing compat) (#94) * CompatHelper: bump compat for GPUCompiler to 0.17, (keep existing compat) * Try running CI * Rerun CI --------- Co-authored-by: CompatHelper Julia Co-authored-by: C. Brenhin Keller --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index bfea97f0..12f68332 100644 --- a/Project.toml +++ b/Project.toml @@ -11,6 +11,6 @@ Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" [compat] -GPUCompiler = "0.16" +GPUCompiler = "0.16, 0.17" LLVM = "4.8" julia = "1.7" From 7e4bbe44ec81ee348ad545f1460a7b91a788b943 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Tue, 14 Mar 2023 21:39:25 -0300 Subject: [PATCH 106/202] Errors are fine! (#97) Hooray! --- Project.toml | 2 + src/StaticCompiler.jl | 39 +++++++++--------- src/pointer_patching.jl | 18 ++++----- src/quirks.jl | 46 +++++++++++++++++++++ src/target.jl | 88 ++++++++++++++++++++++++++++++++--------- 5 files changed, 147 insertions(+), 46 deletions(-) create mode 100644 src/quirks.jl diff --git a/Project.toml b/Project.toml index 12f68332..4b60b47e 100644 --- a/Project.toml +++ b/Project.toml @@ -9,8 +9,10 @@ GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" +StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] GPUCompiler = "0.16, 0.17" LLVM = "4.8" +StaticTools ="0.8" julia = "1.7" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 5a447cd0..0255d2c3 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -8,6 +8,8 @@ using Libdl: Libdl, dlsym, dlopen using Base: RefValue using Serialization: serialize, deserialize using Clang_jll: clang +using StaticTools +using StaticTools: @symbolcall, @c_str, println export compile, load_function, compile_shlib, compile_executable export native_code_llvm, native_code_typed, native_llvm_module, native_code_native @@ -16,6 +18,7 @@ include("target.jl") include("pointer_patching.jl") include("code_loading.jl") include("optimize.jl") +include("quirks.jl") """ @@ -95,7 +98,7 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, path, name; opt_level, strip_llvm, strip_asm, filename, kwargs...) + _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, false, path, name; opt_level, strip_llvm, strip_asm, filename, kwargs...) lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name, filename, table) cjl_path = joinpath(path, "$filename.cjl") @@ -131,15 +134,15 @@ shell> tree \$path 0 directories, 1 file ``` """ -function generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; +function generate_obj(f, tt, external = true, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; strip_llvm = false, strip_asm = true, opt_level=3, kwargs...) mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) - job, kwargs = native_job(f, tt; name, kwargs...) + tm = GPUCompiler.llvm_machine(external ? ExternalNativeCompilerTarget() : NativeCompilerTarget()) + job, kwargs = native_job(f, tt, external; name, kwargs...) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false, ctx=context) @@ -267,7 +270,7 @@ function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_nam # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - generate_shlib(f, tt, path, name, filename; cflags=cflags, kwargs...) + generate_shlib(f, tt, true, path, name, filename; cflags=cflags, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end @@ -276,7 +279,7 @@ function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler. temp::Bool=true, kwargs...) - generate_shlib(f, tt, path, name; kwargs...) + generate_shlib(f, tt, false, path, name; kwargs...) lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_$name") @@ -358,7 +361,7 @@ function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name( mkpath(path) obj_path = joinpath(path, "$filename.o") exec_path = joinpath(path, filename) - job, kwargs = native_job(f, tt; name, kwargs...) + job, kwargs = native_job(f, tt, true; name, kwargs...) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) # Write to file @@ -425,7 +428,7 @@ julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 5.256496109495593 ``` """ -function generate_shlib(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=name; +function generate_shlib(f, tt, external = true, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=name; cflags=``, kwargs... ) @@ -433,7 +436,7 @@ function generate_shlib(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr( mkpath(path) obj_path = joinpath(path, "$filename.o") lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - job, kwargs = native_job(f, tt; name, kwargs...) + job, kwargs = native_job(f, tt, external; name, kwargs...) obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) open(obj_path, "w") do io @@ -449,18 +452,18 @@ function generate_shlib(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr( end function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) - job, kwargs = native_job(func, types; kwargs...) + job, kwargs = native_job(func, types, true; kwargs...) GPUCompiler.code_llvm(stdout, job; kwargs...) end function native_code_typed(@nospecialize(func), @nospecialize(types); kwargs...) - job, kwargs = native_job(func, types; kwargs...) + job, kwargs = native_job(func, types, true; kwargs...) GPUCompiler.code_typed(job; kwargs...) end # Return an LLVM module function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs...) - job, kwargs = native_job(f, tt; name, kwargs...) + job, kwargs = native_job(f, tt, true; name, kwargs...) m, _ = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, ctx=context) end @@ -468,7 +471,7 @@ function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs end function native_code_native(@nospecialize(f), @nospecialize(tt), name = GPUCompiler.safe_name(repr(f)); kwargs...) - job, kwargs = native_job(f, tt; name, kwargs...) + job, kwargs = native_job(f, tt, true; name, kwargs...) GPUCompiler.code_native(stdout, job; kwargs...) end @@ -498,7 +501,7 @@ function native_llvm_module(funcs::Array; demangle = false, kwargs...) return mod end -function generate_obj(funcs::Array, path::String = tempname(), filenamebase::String="obj"; +function generate_obj(funcs::Array, external, path::String = tempname(), filenamebase::String="obj"; demangle =false, strip_llvm = false, strip_asm = true, @@ -507,7 +510,7 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str f,tt = funcs[1] mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - fakejob, kwargs = native_job(f,tt, kwargs...) + fakejob, kwargs = native_job(f,tt, external, kwargs...) mod = native_llvm_module(funcs; demangle = demangle, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io @@ -516,7 +519,7 @@ function generate_obj(funcs::Array, path::String = tempname(), filenamebase::Str path, obj_path end -function generate_shlib(funcs::Array, path::String = tempname(), filename::String="libfoo"; +function generate_shlib(funcs::Array, external = true, path::String = tempname(), filename::String="libfoo"; demangle=false, cflags=``, kwargs... @@ -524,7 +527,7 @@ function generate_shlib(funcs::Array, path::String = tempname(), filename::Strin lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - _,obj_path = generate_obj(funcs, path, filename; demangle=demangle, kwargs...) + _,obj_path = generate_obj(funcs, external, path, filename; demangle=demangle, kwargs...) # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! @@ -550,7 +553,7 @@ function compile_shlib(funcs::Array, path::String="./"; # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - generate_shlib(funcs, path, filename; demangle=demangle, cflags=cflags, kwargs...) + generate_shlib(funcs, true, path, filename; demangle=demangle, cflags=cflags, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end diff --git a/src/pointer_patching.jl b/src/pointer_patching.jl index 4c59f4fd..822d8e9a 100644 --- a/src/pointer_patching.jl +++ b/src/pointer_patching.jl @@ -1,11 +1,11 @@ function relocation_table!(mod) i64 = LLVM.IntType(64; ctx=LLVM.context(mod)) d = IdDict{Any, Tuple{String, LLVM.GlobalVariable}}() - + for func ∈ LLVM.functions(mod), bb ∈ LLVM.blocks(func), inst ∈ LLVM.instructions(bb) - if isa(inst, LLVM.LoadInst) && occursin("inttoptr", string(inst)) + if isa(inst, LLVM.LoadInst) && occursin("inttoptr", string(inst)) get_pointers!(d, mod, inst) - elseif isa(inst, LLVM.StoreInst) && occursin("inttoptr", string(inst)) + elseif isa(inst, LLVM.StoreInst) && occursin("inttoptr", string(inst)) @debug "Relocating StoreInst" inst get_pointers!(d, mod, inst) elseif inst isa LLVM.RetInst && occursin("inttoptr", string(inst)) @@ -101,7 +101,7 @@ function relocation_table!(mod) end end - if length(fn) > 1 && fromC + if length(fn) > 1 && fromC mod = LLVM.parent(LLVM.parent(LLVM.parent(inst))) lfn = LLVM.API.LLVMGetNamedFunction(mod, fn) @@ -139,7 +139,7 @@ function get_pointers!(d, mod, inst) else gv_name = GPUCompiler.safe_name(String(gensym(repr(Core.Typeof(val))))) gv = LLVM.GlobalVariable(mod, llvmeltype(arg), gv_name, LLVM.addrspace(llvmtype(arg))) - + LLVM.extinit!(gv, true) LLVM.API.LLVMSetOperand(inst, i-1, gv) @@ -157,7 +157,7 @@ llvmeltype(x::LLVM.Value) = eltype(LLVM.llvmtype(x)) function pointer_patching_diff(f, tt, path1=tempname(), path2=tempname(); show_reloc_table=false) tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) - job, kwargs = native_job(f, tt; name=GPUCompiler.safe_name(repr(f))) + job, kwargs = native_job(f, tt, false; name=GPUCompiler.safe_name(repr(f))) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, optimize=false, ctx=context) @@ -165,15 +165,15 @@ function pointer_patching_diff(f, tt, path1=tempname(), path2=tempname(); show_r # Use Enzyme's annotation and optimization pipeline annotate!(mod) optimize!(mod, tm) - + s1 = string(mod) write(path1, s1) - + d = StaticCompiler.relocation_table!(mod) if show_reloc_table @show d end - + s2 = string(mod) write(path2, s2) diff --git a/src/quirks.jl b/src/quirks.jl new file mode 100644 index 00000000..ffa25ac0 --- /dev/null +++ b/src/quirks.jl @@ -0,0 +1,46 @@ +libcexit(x::Int32) = @symbolcall exit(x::Int32)::Nothing +macro print_and_throw(err) + quote + println($err) + libcexit(Int32(1)) + end +end + +# math.jl +@device_override @noinline Base.Math.throw_complex_domainerror(f::Symbol, x) = + @print_and_throw c"This operation requires a complex input to return a complex result" +@device_override @noinline Base.Math.throw_exp_domainerror(f::Symbol, x) = + @print_and_throw c"Exponentiation yielding a complex result requires a complex argument" + +# intfuncs.jl +@device_override @noinline Base.throw_domerr_powbysq(::Any, p) = + @print_and_throw c"Cannot raise an integer to a negative power" +@device_override @noinline Base.throw_domerr_powbysq(::Integer, p) = + @print_and_throw c"Cannot raise an integer to a negative power" +@device_override @noinline Base.throw_domerr_powbysq(::AbstractMatrix, p) = + @print_and_throw c"Cannot raise an integer to a negative power" +@device_override @noinline Base.__throw_gcd_overflow(a, b) = + @print_and_throw c"gcd overflow" + +# checked.jl +@device_override @noinline Base.Checked.throw_overflowerr_binaryop(op, x, y) = + @print_and_throw c"Binary operation overflowed" +@device_override @noinline Base.Checked.throw_overflowerr_negation(op, x, y) = + @print_and_throw c"Negation overflowed" +@device_override function Base.Checked.checked_abs(x::Base.Checked.SignedInt) + r = ifelse(x < 0, -x, x) + r < 0 && @print_and_throw(c"checked arithmetic: cannot compute |x|") + r +end + +# boot.jl +@device_override @noinline Core.throw_inexacterror(f::Symbol, ::Type{T}, val) where {T} = + @print_and_throw c"Inexact conversion" + +# abstractarray.jl +@device_override @noinline Base.throw_boundserror(A, I) = + @print_and_throw c"Out-of-bounds array access" + +# trig.jl +@device_override @noinline Base.Math.sincos_domain_error(x) = + @print_and_throw c"sincos(x) is only defined for finite x." diff --git a/src/target.jl b/src/target.jl index 2efb191c..a14af902 100644 --- a/src/target.jl +++ b/src/target.jl @@ -1,22 +1,39 @@ -Base.@kwdef struct NativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget - cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) - features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) +@static if isdefined(Base.Experimental, Symbol("@overlay")) + Base.Experimental.@MethodTable(method_table) +else + const method_table = nothing end -GPUCompiler.llvm_triple(::NativeCompilerTarget) = Sys.MACHINE +const overrides = quote end -function GPUCompiler.llvm_machine(target::NativeCompilerTarget) - triple = GPUCompiler.llvm_triple(target) - t = LLVM.Target(triple=triple) +macro device_override(ex) + ex = macroexpand(__module__, ex) + if Meta.isexpr(ex, :call) + @show ex = eval(ex) + error() + end + code = quote + $GPUCompiler.@override(StaticCompiler.method_table, $ex) + end + if isdefined(Base.Experimental, Symbol("@overlay")) + return esc(code) + else + push!(overrides, code) + return + end +end - tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) - GPUCompiler.asm_verbosity!(tm, true) - return tm +Base.@kwdef struct NativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget + cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) + features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) end -GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = "native_$(job.target.cpu)-$(hash(job.target.features))" +Base.@kwdef struct ExternalNativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget + cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) + features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) +end module StaticRuntime # the runtime library @@ -30,17 +47,50 @@ end struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams end -GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = StaticRuntime -GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{NativeCompilerTarget}) = StaticRuntime -GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams}) = StaticRuntime +for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) + @eval begin + GPUCompiler.llvm_triple(::$target) = Sys.MACHINE + + function GPUCompiler.llvm_machine(target::$target) + triple = GPUCompiler.llvm_triple(target) + + t = LLVM.Target(triple=triple) + + tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) + GPUCompiler.asm_verbosity!(tm, true) + + return tm + end + + GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{$target}) = "native_$(job.target.cpu)-$(hash(job.target.features))" + + GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target}) = StaticRuntime + GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = StaticRuntime + + + GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = true + GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target}) = true + end +end + +GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget})) = method_table +GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget, StaticCompilerParams})) = method_table + +function native_job(@nospecialize(func::Function), @nospecialize(types::Type), external::Bool; + name = GPUCompiler.safe_name(repr(func)), + kernel::Bool = false, + kwargs... + ) + source = GPUCompiler.FunctionSpec(func, types, kernel, name) + target = external ? ExternalNativeCompilerTarget() : NativeCompilerTarget() + params = StaticCompilerParams() + GPUCompiler.CompilerJob(target, source, params), kwargs +end -GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:Any,StaticCompilerParams}) = true -GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{NativeCompilerTarget, StaticCompilerParams}) = true -GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{NativeCompilerTarget}) = true -function native_job(@nospecialize(func), @nospecialize(types); kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) +function native_job(@nospecialize(func), @nospecialize(types), external; kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) - target = NativeCompilerTarget() + target = external ? ExternalNativeCompilerTarget() : NativeCompilerTarget() params = StaticCompilerParams() GPUCompiler.CompilerJob(target, source, params), kwargs end From 080181f40fa3c99d3efab3309d3e24f5988cb21b Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Tue, 14 Mar 2023 21:21:39 -0400 Subject: [PATCH 107/202] Error handling tests (#98) * Add simple core and integration test for new error handling code * Bump version to 0.4.7 --- Project.toml | 2 +- test/scripts/throw_errors.jl | 15 +++++++++++++++ test/testcore.jl | 13 +++++++++++++ test/testintegration.jl | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/scripts/throw_errors.jl diff --git a/Project.toml b/Project.toml index 4b60b47e..58fa736d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.4.6" +version = "0.4.7" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" diff --git a/test/scripts/throw_errors.jl b/test/scripts/throw_errors.jl new file mode 100644 index 00000000..fb823a0d --- /dev/null +++ b/test/scripts/throw_errors.jl @@ -0,0 +1,15 @@ +using StaticCompiler +using StaticTools + +function maybe_throw(argc::Int, argv::Ptr{Ptr{UInt8}}) + printf(c"Argument count is %d:\n", argc) + argc > 1 || return printf(stderrp(), c"Too few command-line arguments\n") + n = argparse(Int64, argv, 2) # First command-line argument + printf((c"Input:\n", n, c"\n")) + printf(c"\nAttempting to represent input as UInt64:\n") + x = UInt64(n) + printf(x) +end + +# Attempt to compile +path = compile_executable(maybe_throw, (Int64, Ptr{Ptr{UInt8}}), "./") diff --git a/test/testcore.jl b/test/testcore.jl index fe2d85ea..e74cfbea 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -291,6 +291,19 @@ end 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) + filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), tempdir()) + @test isfile(filepath) + status = -1 + try + status = run(`filepath`) + catch + @info "foo_err: Task failed successfully!" + end + @test status === -1 + end @noinline square(n) = n*n diff --git a/test/testintegration.jl b/test/testintegration.jl index 6eb2c08c..15012dff 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -242,6 +242,41 @@ @test isa(status, Base.Process) && status.exitcode == 0 end + ## --- Test error throwing + + let + # Compile... + status = -1 + try + isfile("maybe_throw") && rm("maybe_throw") + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/throw_errors.jl`) + catch e + @warn "Could not compile $testpath/scripts/throw_errors.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + println("Error handling:") + status = -1 + try + status = run(`./maybe_throw 10`) + catch e + @warn "Could not run $(scratch)/maybe_throw" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + status = -1 + try + status = run(`./maybe_throw -10`) + catch e + @info "maybe_throw: task failed sucessfully!" + end + @test status === -1 + end + ## --- Test interop @static if Sys.isbsd() From 156210c8a35f5b4b159791c2e9654d3758b45b67 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Mon, 20 Mar 2023 19:39:53 -0400 Subject: [PATCH 108/202] Add warnings if return type is not a native type in standalone binaries/shlibs (#102) * Add warnings for if return type is not a native type in `compile_shlib` and `compile_executable` * Make warning spookier * Bump version to 0.4.8 --- Project.toml | 2 +- src/StaticCompiler.jl | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Project.toml b/Project.toml index 58fa736d..d86a206f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.4.7" +version = "0.4.8" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 0255d2c3..b4ef719e 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -94,7 +94,7 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam tt = Base.to_tuple_type(_tt) isconcretetype(tt) || error("input type signature $_tt is not concrete") - rt = only(native_code_typed(f, tt))[2] + rt = last(only(native_code_typed(f, tt))) isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) @@ -236,10 +236,13 @@ function compile_executable(f, types=(), path::String="./", name=GPUCompiler.saf ) tt = Base.to_tuple_type(types) - # tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})") + isexecutableargtype = tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} + isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" - rt = only(native_code_typed(f, tt))[2] - isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") + rt = last(only(native_code_typed(f, tt))) + isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") + 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!" # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this @@ -263,10 +266,12 @@ function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_nam ) tt = Base.to_tuple_type(types) - isconcretetype(tt) || error("input type signature $types is not concrete") + isconcretetype(tt) || error("input type signature `$types` is not concrete") - rt = only(native_code_typed(f, tt))[2] - isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") + rt = last(only(native_code_typed(f, tt))) + isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") + 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!" # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this @@ -544,10 +549,12 @@ function compile_shlib(funcs::Array, path::String="./"; for func in funcs f, types = func tt = Base.to_tuple_type(types) - isconcretetype(tt) || error("input type signature $types is not concrete") + isconcretetype(tt) || error("input type signature `$types` is not concrete") - rt = only(native_code_typed(f, tt))[2] - isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt") + rt = last(only(native_code_typed(f, tt))) + isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") + 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!" end # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals From 7788852a5efd5e33f4239a892eb5714dced0e384 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Mon, 20 Mar 2023 22:58:12 -0400 Subject: [PATCH 109/202] Cleanup (#104) * Reorganize main compilation functions, update docstrings and README * Document and export `@device_overload` * Typo --- README.md | 16 ++-- src/StaticCompiler.jl | 204 ++++++++++++++++++++++++------------------ src/target.jl | 14 ++- test/testcore.jl | 11 ++- 4 files changed, 145 insertions(+), 100 deletions(-) diff --git a/README.md b/README.md index 6c088082..6269635d 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,10 @@ using Pkg Pkg.add("StaticCompiler") ``` -There are two main ways to use this package. The first is via the `compile` function, which can be used when you want to compile a Julia function for later use from within Julia: +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 julia> using StaticCompiler @@ -43,7 +46,8 @@ julia> fib_compiled(10) ``` See the file `tests/runtests.jl` for some examples of functions that work with `compile` (and some that don't, marked with `@test_skip`). -The second way to use this package is via the `compile_executable` and `compile_shlib` functions, for use when you want to compile a Julia function to a native executable or shared library for use from outside of Julia: +### 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 julia> using StaticCompiler, StaticTools @@ -68,7 +72,7 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi ## 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` (yet). -* Type unstable code is not yet supported. -* Doesn't currently work on Windows. -* If you find any other limitations, let us know. There's probably lots. +* 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](src/quirks.jl)). +* Type instability. Type unstable code cannot currently be statically compiled via this package. +* Doesn't work on Windows. PRs welcome. diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index b4ef719e..ca8f696e 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -13,6 +13,7 @@ using StaticTools: @symbolcall, @c_str, println export compile, load_function, compile_shlib, compile_executable export native_code_llvm, native_code_typed, native_llvm_module, native_code_native +export @device_override, @print_and_throw include("target.jl") include("pointer_patching.jl") @@ -173,7 +174,11 @@ end """ ```julia -compile_executable(f, types::Tuple, path::String, name::String=repr(f); filename::String=name, kwargs...) +compile_executable(f::Function, types::Tuple, path::String, [name::String=repr(f)]; + filename::String=name, + cflags=``, # Specify libraries you would like to link against, and other compiler options here + kwargs... +) ``` Attempt to compile a standalone executable that runs function `f` with a type signature given by the tuple of `types`. @@ -207,7 +212,7 @@ julia> function print_args(argc::Int, argv::Ptr{Ptr{UInt8}}) end julia> compile_executable(print_args, (Int, Ptr{Ptr{UInt8}})) -"/Users/foo/code/StaticCompiler.jl/print_args" +""/Users/user/print_args"" shell> ./print_args 1 2 3 4 Five ./print_args @@ -223,13 +228,16 @@ julia> using StaticTools # So you don't have to define `puts` and friends every julia> hello() = println(c"Hello, world!") # c"..." makes a stack-allocated StaticString julia> compile_executable(hello) -"/Users/foo/code/StaticCompiler.jl/hello" +"/Users/cbkeller/hello" + +shell> ls -alh hello +-rwxr-xr-x 1 user staff 33K Mar 20 21:11 hello shell> ./hello Hello, world! ``` """ -function compile_executable(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); +function compile_executable(f::Function, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); filename=name, cflags=``, kwargs... @@ -244,9 +252,6 @@ function compile_executable(f, types=(), path::String="./", name=GPUCompiler.saf 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!" - # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals - # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - generate_executable(f, tt, path, name, filename; cflags=cflags, kwargs...) joinpath(abspath(path), filename) @@ -255,11 +260,35 @@ end """ ```julia -compile_shlib(f, types::Tuple, path::String, name::String=repr(f); filename::String=name, kwargs...) +compile_shlib(f::Function, types::Tuple, [path::String="./"], [name::String=repr(f)]; filename::String=name, cflags=``, kwargs...) +compile_shlib(funcs::Array, [path::String="./"]; filename="libfoo", demangle=false, cflags=``, kwargs...) ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. + +### Examples +```julia +julia> using StaticCompiler, LoopVectorization + +julia> function test(n) + r = 0.0 + @turbo for i=1:n + r += log(sqrt(i)) + end + return r/n + end +test (generic function with 1 method) + +julia> compile_shlib(test, (Int,)) +"/Users/user/test.dylib" + +julia> test(100_000) +5.2564961094956075 + +julia> ccall(("julia_test", "test.dylib"), Float64, (Int64,), 100_000) +5.2564961094956075 +``` """ -function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); +function compile_shlib(f::Function, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); filename=name, cflags=``, kwargs... @@ -273,28 +302,34 @@ function compile_shlib(f, types=(), path::String="./", name=GPUCompiler.safe_nam 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!" - # Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals - # Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this generate_shlib(f, tt, true, path, name, filename; cflags=cflags, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end +# As above, but taking an array of functions and returning a single shlib +function compile_shlib(funcs::Array, path::String="./"; + filename="libfoo", + demangle=false, + cflags=``, + kwargs... + ) + for func in funcs + f, types = func + tt = Base.to_tuple_type(types) + isconcretetype(tt) || error("input type signature `$types` is not concrete") -function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=name; - temp::Bool=true, - kwargs...) - - generate_shlib(f, tt, false, path, name; kwargs...) - lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") - ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "julia_$name") - @assert fptr != C_NULL - if temp - atexit(()->rm(path; recursive=true)) + rt = last(only(native_code_typed(f, tt))) + isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") + 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!" end - fptr + + generate_shlib(funcs, true, path, filename; demangle=demangle, cflags=cflags, kwargs...) + + joinpath(abspath(path), filename * "." * Libdl.dlext) end + """ ```julia generate_shlib_fptr(path::String, name) @@ -338,25 +373,40 @@ function generate_shlib_fptr(path::String, name, filename::String=name) @assert fptr != C_NULL fptr end +# As above, but also compile (maybe remove this method in the future?) +function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=name; + temp::Bool=true, + kwargs...) + + generate_shlib(f, tt, false, path, name; kwargs...) + lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") + ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_$name") + @assert fptr != C_NULL + if temp + atexit(()->rm(path; recursive=true)) + end + fptr +end """ ```julia generate_executable(f, tt, path::String, name, filename=string(name); kwargs...) ``` Attempt to compile a standalone executable that runs `f`. +Low-level interface; you should generally use `compile_executable` instead. ### Examples ```julia -julia> function test(n) - r = 0.0 - for i=1:n - r += log(sqrt(i)) - end - return r/n - end -test (generic function with 1 method) +julia> using StaticCompiler, StaticTools -julia> path, name = StaticCompiler.generate_executable(test, Tuple{Int64}, "./scratch") +julia> hello() = println(c"Hello, world!") + +julia> path, name = StaticCompiler.generate_executable(hello, Tuple{}, "./") +("./", "hello") + +shell> ./hello +Hello, world! ``` """ function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name); @@ -404,36 +454,43 @@ end """ ```julia -generate_shlib(f, tt, path::String, name::String, filenamebase::String="obj"; kwargs...) +generate_shlib(f::Function, tt, [external::Bool=true], [path::String], [name], [filename]; kwargs...) +generate_shlib(funcs::Array, [external::Bool=true], [path::String], [filename::String]; demangle=false, kwargs...) ``` Low level interface for compiling a shared object / dynamically loaded library (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. -See also `StaticCompiler.generate_shlib_fptr`. + ### Examples ```julia +julia> using StaticCompiler, LoopVectorization + julia> function test(n) r = 0.0 - for i=1:n + @turbo for i=1:n r += log(sqrt(i)) end return r/n end test (generic function with 1 method) -julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, "./test") -("./test", "test") + +julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, true, "./example") +("./example", "test") + shell> tree \$path -./test -|-- obj.o -`-- obj.so +./example +|-- test.dylib +`-- test.o 0 directories, 2 files + julia> test(100_000) -5.256496109495593 -julia> ccall(StaticCompiler.generate_shlib_fptr(path, name), Float64, (Int64,), 100_000) -5.256496109495593 +5.2564961094956075 + +julia> ccall(("julia_test", "example/test.dylib"), Float64, (Int64,), 100_000) +5.2564961094956075 ``` """ -function generate_shlib(f, tt, external = true, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=name; +function generate_shlib(f::Function, tt, external::Bool=true, path::String=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=name; cflags=``, kwargs... ) @@ -455,6 +512,23 @@ function generate_shlib(f, tt, external = true, path=tempname(), name=GPUCompile path, name end +# As above, but taking an array of functions and returning a single shlib +function generate_shlib(funcs::Array, external::Bool=true, path::String=tempname(), filename::String="libfoo"; + demangle=false, + cflags=``, + kwargs... + ) + + lib_path = joinpath(path, "$filename.$(Libdl.dlext)") + + _,obj_path = generate_obj(funcs, external, path, filename; demangle=demangle, kwargs...) + # Pick a Clang + cc = Sys.isapple() ? `cc` : clang() + # Compile! + run(`$cc -shared $cflags $obj_path -o $lib_path `) + + path, name +end function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) job, kwargs = native_job(func, types, true; kwargs...) @@ -524,46 +598,4 @@ function generate_obj(funcs::Array, external, path::String = tempname(), filenam path, obj_path end -function generate_shlib(funcs::Array, external = true, path::String = tempname(), filename::String="libfoo"; - demangle=false, - cflags=``, - kwargs... - ) - - lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - - _,obj_path = generate_obj(funcs, external, path, filename; demangle=demangle, kwargs...) - # Pick a Clang - cc = Sys.isapple() ? `cc` : clang() - # Compile! - run(`$cc -shared $cflags $obj_path -o $lib_path `) - - path, name -end - -function compile_shlib(funcs::Array, path::String="./"; - filename="libfoo", - demangle=false, - cflags=``, - kwargs...) - for func in funcs - f, types = func - tt = Base.to_tuple_type(types) - isconcretetype(tt) || error("input type signature `$types` is not concrete") - - rt = last(only(native_code_typed(f, tt))) - isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") - 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!" - end - -# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals -# Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this - - generate_shlib(funcs, true, path, filename; demangle=demangle, cflags=cflags, kwargs...) - - joinpath(abspath(path), filename * "." * Libdl.dlext) -end - - end # module diff --git a/src/target.jl b/src/target.jl index a14af902..734a0cc2 100644 --- a/src/target.jl +++ b/src/target.jl @@ -6,7 +6,18 @@ end const overrides = quote end - +""" +```julia +@device_override old_bad_method(arg1::Type1, arg2::Type2) = new_good_method(arg1, arg2) +``` +Override a non-static-compilable method (e.g. `old_bad_method(::Type1, ::Type2)`) +with a more compileable replacement. +### Examples +``` +@device_override @noinline Core.throw_inexacterror(f::Symbol, ::Type{T}, val) where {T} = + @print_and_throw c"Inexact conversion" +``` +""" macro device_override(ex) ex = macroexpand(__module__, ex) if Meta.isexpr(ex, :call) @@ -24,7 +35,6 @@ macro device_override(ex) end end - Base.@kwdef struct NativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) diff --git a/test/testcore.jl b/test/testcore.jl index e74cfbea..450fbdad 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -173,15 +173,14 @@ end end hello_compiled, path = compile(hello, (String,)) @test remote_load_call(path, "world") == "Hello, world!" -end -@testset "Hello World" begin - function hello(N) - println("Hello World $N") - N - end # We'll need to be able to relocate a bunch of UV stuff for this, and deal with dynamic dispatch. @test_skip begin + function hello(N) + println("Hello World $N") + N + end + hello_compiled, path = compile(hello, (Int,)) @test_skip remote_load_call(path, 1) == 1 end From fed55dd28d46b8807ce1eb84c0becb4f4ed78feb Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Tue, 21 Mar 2023 07:14:13 +0100 Subject: [PATCH 110/202] Document symbol naming convention (#105) * Document symbol naming convention * A bit more context on name mangling --------- Co-authored-by: C. Brenhin Keller --- src/StaticCompiler.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index ca8f696e..07368d16 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -265,6 +265,11 @@ compile_shlib(funcs::Array, [path::String="./"]; filename="libfoo", demangle=fal ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. +The compiled function is by default given the symbol name `julia_$(name)`, i.e., +the function `test` in the example below is called `julia_test` in the shared library. +The keword argument `demangle=true` will remove this prefix, but is currently only +supported the second (multi-function-shlib) method. + ### Examples ```julia julia> using StaticCompiler, LoopVectorization From 141e1abacce529f2de1213fe390369b8db65a87b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 1 Apr 2023 23:02:36 -0400 Subject: [PATCH 111/202] CompatHelper: bump compat for LLVM to 5, (keep existing compat) (#107) * CompatHelper: bump compat for LLVM to 5, (keep existing compat) * Run CI --------- Co-authored-by: CompatHelper Julia Co-authored-by: C. Brenhin Keller --- Project.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index d86a206f..5cd85746 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.4.8" +version = "0.4.9" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" @@ -13,6 +13,6 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] GPUCompiler = "0.16, 0.17" -LLVM = "4.8" -StaticTools ="0.8" +LLVM = "4.8, 5" +StaticTools = "0.8" julia = "1.7" From b1240f09458e044f2a1c4f9d5a2d337c5bae6b9c Mon Sep 17 00:00:00 2001 From: Tom Short Date: Wed, 12 Apr 2023 17:21:29 -0400 Subject: [PATCH 112/202] Cassette-type functionality with Mixtape (#69) --- .github/workflows/ci-integration-nightly.yml | 39 ----- .github/workflows/ci-integration.yml | 38 ----- .github/workflows/ci-julia-nightly.yml | 39 ----- .github/workflows/ci.yml | 2 +- LICENSE | 4 +- Project.toml | 18 ++- README.md | 8 ++ src/StaticCompiler.jl | 30 ++-- src/code_loading.jl | 1 - src/interpreter.jl | 141 +++++++++++++++++++ src/mixtape.jl | 77 ++++++++++ src/target.jl | 18 ++- test/Project.toml | 9 +- test/runtests.jl | 3 + test/testintegration.jl | 72 ++++++++++ 15 files changed, 358 insertions(+), 141 deletions(-) delete mode 100644 .github/workflows/ci-integration-nightly.yml delete mode 100644 .github/workflows/ci-integration.yml delete mode 100644 .github/workflows/ci-julia-nightly.yml create mode 100644 src/interpreter.jl create mode 100644 src/mixtape.jl diff --git a/.github/workflows/ci-integration-nightly.yml b/.github/workflows/ci-integration-nightly.yml deleted file mode 100644 index 6d4cb3e2..00000000 --- a/.github/workflows/ci-integration-nightly.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: CI (Integration nightly) -on: - push: - branches: - - '**' - paths-ignore: - - 'README.md' - pull_request: - branches: - - master - paths-ignore: - - 'README.md' -jobs: - test-integration-nightly: - name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - version: - - 'nightly' - - '~1.9.0-0' - os: - - ubuntu-latest - - macOS-latest - arch: - - x64 - group: - - Integration - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@latest - with: - version: ${{ matrix.version }} - arch: ${{ matrix.arch }} - - uses: julia-actions/julia-buildpkg@latest - - uses: julia-actions/julia-runtest@latest - env: - GROUP: ${{ matrix.group }} diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml deleted file mode 100644 index 3b9024df..00000000 --- a/.github/workflows/ci-integration.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: CI (Integration) -on: - push: - branches: - - '**' - paths-ignore: - - 'README.md' - pull_request: - branches: - - master - paths-ignore: - - 'README.md' -jobs: - test-integration: - name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - version: - - '1.8' - os: - - ubuntu-latest - - macOS-latest - arch: - - x64 - group: - - Integration - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@latest - with: - version: ${{ matrix.version }} - arch: ${{ matrix.arch }} - - uses: julia-actions/julia-buildpkg@latest - - uses: julia-actions/julia-runtest@latest - env: - GROUP: ${{ matrix.group }} diff --git a/.github/workflows/ci-julia-nightly.yml b/.github/workflows/ci-julia-nightly.yml deleted file mode 100644 index dfb2ad8f..00000000 --- a/.github/workflows/ci-julia-nightly.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: CI (Julia nightly) -on: - push: - branches: - - '**' - paths-ignore: - - 'README.md' - pull_request: - branches: - - master - paths-ignore: - - 'README.md' -jobs: - test-julia-nightly: - name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - version: - - 'nightly' - - '~1.9.0-0' - os: - - ubuntu-latest - - macOS-latest - arch: - - x64 - group: - - Core - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@latest - with: - version: ${{ matrix.version }} - arch: ${{ matrix.arch }} - - uses: julia-actions/julia-buildpkg@latest - - uses: julia-actions/julia-runtest@latest - env: - GROUP: ${{ matrix.group }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 341215a2..d312a541 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,8 +18,8 @@ jobs: fail-fast: false matrix: version: - - '1.7' - '1.8' + - '^1.9.0-rc2' os: - ubuntu-latest - macOS-latest diff --git a/LICENSE b/LICENSE index cb19b0ae..b4e5ab82 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ Copyright (c) 2019-2022 Mason Protter, William Moses, Valentin Churavy, - Brenhin Keller, Julian Samaroo, Tom Short, and - other contributors + McCoy R. Becker, Brenhin Keller, Julian Samaroo, + Tom Short, 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 diff --git a/Project.toml b/Project.toml index 5cd85746..212d07a2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,18 +1,30 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.4.9" +version = "0.4.10" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" +CodeInfoTools = "bc773b8a-8374-437a-b9f2-0e9785855863" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" +InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" +MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] -GPUCompiler = "0.16, 0.17" +CodeInfoTools = "0.3" +GPUCompiler = "0.17" LLVM = "4.8, 5" StaticTools = "0.8" -julia = "1.7" +MacroTools = "0.5" +julia = "1.8, 1.9" + +[extras] +Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["Test", "Formatting"] diff --git a/README.md b/README.md index 6269635d..6ddb9787 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,14 @@ Hello, world! ``` This latter approach comes with substantially more limitations, as you cannot rely on `libjulia` (see, e.g., [StaticTools.jl](https://github.com/brenhinkeller/StaticTools.jl) for some ways to work around these limitations). +### Mixtape + +This feature allows one to change functionality when statically compiling. This uses code and API from [Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) to transform lowered code much like [Cassette](https://github.com/JuliaLabs/Cassette.jl). + +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](https://github.com/tshort/StaticCompiler.jl/blob/master/test/testintegration.jl#L329) for an example. + ## Approach This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 07368d16..6e1b2b7d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -1,5 +1,5 @@ module StaticCompiler - +using InteractiveUtils using GPUCompiler: GPUCompiler using LLVM using LLVM.Interop @@ -15,6 +15,8 @@ export compile, load_function, compile_shlib, compile_executable export native_code_llvm, native_code_typed, native_llvm_module, native_code_native export @device_override, @print_and_throw +include("mixtape.jl") +include("interpreter.jl") include("target.jl") include("pointer_patching.jl") include("code_loading.jl") @@ -87,7 +89,10 @@ with `load_function(path)`. `LazyStaticcompiledfunction`s contain the requisite `StaticCompiledFunction` and may be called with arguments of type `types` as if it were a function with a single method (the method determined by `types`). """ -function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_name(repr(f)), filename="obj", +function compile(f, _tt, path::String = tempname(); + mixtape = NoContext(), + name = GPUCompiler.safe_name(repr(f)), + filename = "obj", strip_llvm = false, strip_asm = true, opt_level=3, @@ -95,11 +100,10 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam tt = Base.to_tuple_type(_tt) isconcretetype(tt) || error("input type signature $_tt is not concrete") - rt = last(only(native_code_typed(f, tt))) + rt = last(only(native_code_typed(f, tt, mixtape = mixtape))) isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") - f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, false, path, name; opt_level, strip_llvm, strip_asm, filename, kwargs...) + _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, false, path, name; mixtape = mixtape, opt_level, strip_llvm, strip_asm, filename, kwargs...) lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name, filename, table) cjl_path = joinpath(path, "$filename.cjl") @@ -136,6 +140,7 @@ shell> tree \$path ``` """ function generate_obj(f, tt, external = true, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; + mixtape = NoContext(), strip_llvm = false, strip_asm = true, opt_level=3, @@ -143,11 +148,18 @@ function generate_obj(f, tt, external = true, path::String = tempname(), name = mkpath(path) obj_path = joinpath(path, "$filenamebase.o") tm = GPUCompiler.llvm_machine(external ? ExternalNativeCompilerTarget() : NativeCompilerTarget()) - job, kwargs = native_job(f, tt, external; name, kwargs...) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. + job = CompilerJob(NativeCompilerTarget(), + FunctionSpec(f, tt, false, name), + StaticCompilerParams(; + opt = true, + mixtape = mixtape, + optlevel = Base.JLOptions().opt_level)) + mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false, ctx=context) - end + end + # Use Enzyme's annotation and optimization pipeline annotate!(mod) optimize!(mod, tm) @@ -247,7 +259,7 @@ function compile_executable(f::Function, types=(), path::String="./", name=GPUCo isexecutableargtype = tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" - rt = last(only(native_code_typed(f, tt))) + rt = last(only(native_code_typed(f, tt; kwargs...))) isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") 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!" @@ -302,7 +314,7 @@ function compile_shlib(f::Function, types=(), path::String="./", name=GPUCompile tt = Base.to_tuple_type(types) isconcretetype(tt) || error("input type signature `$types` is not concrete") - rt = last(only(native_code_typed(f, tt))) + rt = last(only(native_code_typed(f, tt; kwargs...))) isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") 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!" diff --git a/src/code_loading.jl b/src/code_loading.jl index 931e6a55..665df22a 100644 --- a/src/code_loading.jl +++ b/src/code_loading.jl @@ -49,7 +49,6 @@ function instantiate(p::LazyStaticCompiledFunction{rt, tt}) where {rt, tt} prefix = LLVM.get_prefix(lljit) dg = LLVM.CreateDynamicLibrarySearchGeneratorForProcess(prefix) LLVM.add!(jd, dg) - LLVM.add!(lljit, jd, ofile) fptr = pointer(LLVM.lookup(lljit, "julia_" * p.name)) diff --git a/src/interpreter.jl b/src/interpreter.jl new file mode 100644 index 00000000..09ffd4af --- /dev/null +++ b/src/interpreter.jl @@ -0,0 +1,141 @@ +## interpreter + +using Core.Compiler: + AbstractInterpreter, InferenceResult, InferenceParams, InferenceState, MethodInstance, OptimizationParams, WorldView +using GPUCompiler: + @safe_debug, AbstractCompilerParams, CodeCache, CompilerJob, FunctionSpec +using CodeInfoTools +using CodeInfoTools: resolve + +struct StaticInterpreter{M} <: AbstractInterpreter + global_cache::CodeCache + method_table::Union{Nothing,Core.MethodTable} + + # Cache of inference results for this particular interpreter + local_cache::Vector{InferenceResult} + # The world age we're working inside of + world::UInt + + # Parameters for inference and optimization + inf_params::InferenceParams + opt_params::OptimizationParams + + # Mixtape context + mixtape::M + + function StaticInterpreter(cache::CodeCache, mt::Union{Nothing,Core.MethodTable}, world::UInt, ip::InferenceParams, op::OptimizationParams, mixtape::CompilationContext) + @assert world <= Base.get_world_counter() + + return new{typeof(mixtape)}( + cache, + mt, + + # Initially empty cache + Vector{InferenceResult}(), + + # world age counter + world, + + # parameters for inference and optimization + ip, + op, + + # Mixtape context + mixtape + ) + end +end + + +Core.Compiler.InferenceParams(interp::StaticInterpreter) = interp.inf_params +Core.Compiler.OptimizationParams(interp::StaticInterpreter) = interp.opt_params +Core.Compiler.get_world_counter(interp::StaticInterpreter) = interp.world +Core.Compiler.get_inference_cache(interp::StaticInterpreter) = interp.local_cache +Core.Compiler.code_cache(interp::StaticInterpreter) = WorldView(interp.global_cache, interp.world) + +# No need to do any locking since we're not putting our results into the runtime cache +Core.Compiler.lock_mi_inference(interp::StaticInterpreter, mi::MethodInstance) = nothing +Core.Compiler.unlock_mi_inference(interp::StaticInterpreter, mi::MethodInstance) = nothing + +function Core.Compiler.add_remark!(interp::StaticInterpreter, sv::InferenceState, msg) + @safe_debug "Inference remark during static compilation of $(sv.linfo): $msg" +end + + +##### +##### Pre-inference +##### + +function resolve_generic(a) + if a isa Type && a <: Function && isdefined(a, :instance) + return a.instance + else + return resolve(a) + end +end + +function custom_pass!(interp::StaticInterpreter, result::InferenceResult, mi::Core.MethodInstance, src) + src === nothing && return src + mi.specTypes isa UnionAll && return src + sig = Tuple(mi.specTypes.parameters) + as = map(resolve_generic, sig) + if allow(interp.mixtape, mi.def.module, as...) + src = transform(interp.mixtape, src, sig) + end + return src +end + +function InferenceState(result::InferenceResult, cache::Symbol, interp::StaticInterpreter) + src = Core.Compiler.retrieve_code_info(result.linfo) + mi = result.linfo + src = custom_pass!(interp, result, mi, src) + src === nothing && return nothing + Core.Compiler.validate_code_in_debug_mode(result.linfo, src, "lowered") + return InferenceState(result, src, cache, interp) +end + +Core.Compiler.may_optimize(interp::StaticInterpreter) = true +Core.Compiler.may_compress(interp::StaticInterpreter) = true +Core.Compiler.may_discard_trees(interp::StaticInterpreter) = true +if VERSION >= v"1.7.0-DEV.577" +Core.Compiler.verbose_stmt_info(interp::StaticInterpreter) = false +end + +if isdefined(Base.Experimental, Symbol("@overlay")) +using Core.Compiler: OverlayMethodTable +if v"1.8-beta2" <= VERSION < v"1.9-" || VERSION >= v"1.9.0-DEV.120" +Core.Compiler.method_table(interp::StaticInterpreter) = + OverlayMethodTable(interp.world, interp.method_table) +else +Core.Compiler.method_table(interp::StaticInterpreter, sv::InferenceState) = + OverlayMethodTable(interp.world, interp.method_table) +end +else +Core.Compiler.method_table(interp::StaticInterpreter, sv::InferenceState) = + WorldOverlayMethodTable(interp.world) +end + +# semi-concrete interepretation is broken with overlays (JuliaLang/julia#47349) +@static if VERSION >= v"1.9.0-DEV.1248" +function Core.Compiler.concrete_eval_eligible(interp::StaticInterpreter, + @nospecialize(f), result::Core.Compiler.MethodCallResult, arginfo::Core.Compiler.ArgInfo) + ret = @invoke Core.Compiler.concrete_eval_eligible(interp::AbstractInterpreter, + f::Any, result::Core.Compiler.MethodCallResult, arginfo::Core.Compiler.ArgInfo) + ret === false && return nothing + return ret +end +end + +struct StaticCompilerParams <: AbstractCompilerParams + opt::Bool + optlevel::Int + mixtape::CompilationContext + cache::CodeCache +end + +function StaticCompilerParams(; opt = false, + optlevel = Base.JLOptions().opt_level, + mixtape = NoContext(), + cache = CodeCache()) + return StaticCompilerParams(opt, optlevel, mixtape, cache) +end diff --git a/src/mixtape.jl b/src/mixtape.jl new file mode 100644 index 00000000..5e6098a4 --- /dev/null +++ b/src/mixtape.jl @@ -0,0 +1,77 @@ + +##### +##### Exports +##### + +export CompilationContext, + NoContext, + allow, + transform + +##### +##### Compilation context +##### + +# User-extended context allows parametrization of the pipeline through +# our subtype of AbstractInterpreter +abstract type CompilationContext end + +struct NoContext <: CompilationContext end + +@doc( +""" + abstract type CompilationContext end + +Parametrize the Mixtape pipeline by inheriting from `CompilationContext`. Similar to the context objects in [Cassette.jl](https://julia.mit.edu/Cassette.jl/stable/contextualdispatch.html). By using the interface methods [`transform`](@ref) and [`optimize!`](@ref) -- the user can control different parts of the compilation pipeline. +""", CompilationContext) + +transform(ctx::CompilationContext, b) = b +transform(ctx::CompilationContext, b, sig) = transform(ctx, b) + +@doc( +""" + transform(ctx::CompilationContext, b::Core.CodeInfo)::Core.CodeInfo + transform(ctx::CompilationContext, b::Core.CodeInfo, sig::Tuple)::Core.CodeInfo + +User-defined transform which operates on lowered `Core.CodeInfo`. There's two versions: (1) ignores the signature of the current method body under consideration and (2) provides the signature as `sig`. + +Transforms might typically follow a simple "swap" format using `CodeInfoTools.Builder`: + +```julia +function transform(::MyCtx, src) + b = CodeInfoTools.Builder(b) + for (k, st) in b + b[k] = swap(st)) + end + return CodeInfoTools.finish(b) +end +``` + +but more advanced formats are possible. For further utilities, please see [CodeInfoTools.jl](https://github.com/JuliaCompilerPlugins/CodeInfoTools.jl). +""", transform) + + +allow(f::C, args...) where {C <: CompilationContext} = false +function allow(ctx::CompilationContext, mod::Module, fn, args...) + return allow(ctx, mod) || allow(ctx, fn, args...) +end + +@doc( +""" + allow(f::CompilationContext, args...)::Bool + +Determines whether the user-defined [`transform`](@ref) and [`optimize!`](@ref) are allowed to look at a lowered `Core.CodeInfo` or `Core.Compiler.IRCode` instance. + +The user is allowed to greenlight modules: + +```julia +allow(::MyCtx, m::Module) == m == SomeModule +``` + +or even specific signatures + +```julia +allow(::MyCtx, fn::typeof(rand), args...) = true +``` +""", allow) + diff --git a/src/target.jl b/src/target.jl index 734a0cc2..83872a0f 100644 --- a/src/target.jl +++ b/src/target.jl @@ -55,8 +55,6 @@ module StaticRuntime report_exception_frame(idx, func, file, line) = return end -struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams end - for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) @eval begin GPUCompiler.llvm_triple(::$target) = Sys.MACHINE @@ -80,6 +78,12 @@ for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = true GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target}) = true + + GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = + StaticInterpreter(job.params.cache, GPUCompiler.method_table(job), job.source.world, + GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job), + job.params.mixtape) + GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = job.params.cache end end @@ -87,20 +91,20 @@ GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNati GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget, StaticCompilerParams})) = method_table function native_job(@nospecialize(func::Function), @nospecialize(types::Type), external::Bool; + mixtape = NoContext(), name = GPUCompiler.safe_name(repr(func)), kernel::Bool = false, kwargs... ) source = GPUCompiler.FunctionSpec(func, types, kernel, name) target = external ? ExternalNativeCompilerTarget() : NativeCompilerTarget() - params = StaticCompilerParams() - GPUCompiler.CompilerJob(target, source, params), kwargs + params = StaticCompilerParams(mixtape = mixtape) + StaticCompiler.CompilerJob(target, source, params), kwargs end - -function native_job(@nospecialize(func), @nospecialize(types), external; kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) +function native_job(@nospecialize(func), @nospecialize(types), external; mixtape = NoContext(), kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) target = external ? ExternalNativeCompilerTarget() : NativeCompilerTarget() - params = StaticCompilerParams() + params = StaticCompilerParams(mixtape = mixtape) GPUCompiler.CompilerJob(target, source, params), kwargs end diff --git a/test/Project.toml b/test/Project.toml index e5a48238..c9d1de55 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,11 +1,16 @@ [deps] +CodeInfoTools = "bc773b8a-8374-437a-b9f2-0e9785855863" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" -LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" +Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" +GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" +Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" +MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" -TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" diff --git a/test/runtests.jl b/test/runtests.jl index 67838d9f..d117aa58 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,6 +7,9 @@ using ManualMemory using Distributed using StaticTools using StrideArraysCore +using CodeInfoTools +using MacroTools + addprocs(1) @everywhere using StaticCompiler, StrideArraysCore diff --git a/test/testintegration.jl b/test/testintegration.jl index 15012dff..6f98b5fe 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -311,3 +311,75 @@ cd(testpath) end + +# Mixtape + +module SubFoo + +function f() + x = rand() + y = rand() + return x + y +end + +function stringfun(s1, s2) + return s1 * s2 +end + +function teststring() + return stringfun("ab", "c") == "abc" +end + +end + +struct MyMix <: CompilationContext end + +@testset "Mixtape" begin + # 101: How2Mix + + # A few little utility functions for working with Expr instances. + swap(e) = e + function swap(e::Expr) + new = MacroTools.postwalk(e) do s + isexpr(s, :call) || return s + s.args[1] == Base.rand || return s + return 4 + end + return new + end + + # This is pre-inference - you get to see a CodeInfoTools.Builder instance. + function StaticCompiler.transform(::MyMix, src) + b = CodeInfoTools.Builder(src) + for (v, st) in b + b[v] = swap(st) + end + return CodeInfoTools.finish(b) + end + + # MyMix will only transform functions which you explicitly allow. + # You can also greenlight modules. + StaticCompiler.allow(ctx::MyMix, m::Module) = m == SubFoo + + _, path = compile(SubFoo.f, (), mixtape = MyMix()) + @test load_function(path)() == 8 + @test SubFoo.f() != 8 + + # redefine swap to test caching and add StaticString substitution + function swap(e::Expr) + new = MacroTools.postwalk(e) do s + s isa String && return StaticTools.StaticString(tuple(codeunits(s)..., 0x00)) + isexpr(s, :call) || return s + s.args[1] == Base.rand || return s + return 2 + end + return new + end + _, path = compile(SubFoo.f, (), mixtape = MyMix()) + @test load_function(path)() == 4 + + _, path = compile(SubFoo.teststring, (), mixtape = MyMix()) + @test load_function(path)() + +end + From 84e1b908e007d3ff49dd429d6e54b00daf7a0957 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Mon, 24 Apr 2023 18:31:03 -0400 Subject: [PATCH 113/202] Allow compilation to other target triples (#109) --- README.md | 2 ++ src/StaticCompiler.jl | 14 ++++++++++++-- src/target.jl | 11 +++++++---- test/testintegration.jl | 6 ++++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6ddb9787..bcb1ceee 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ Hello, world! ``` This latter approach comes with substantially more limitations, as you cannot rely on `libjulia` (see, e.g., [StaticTools.jl](https://github.com/brenhinkeller/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](https://github.com/JuliaCompilerPlugins/Mixtape.jl) to transform lowered code much like [Cassette](https://github.com/JuliaLabs/Cassette.jl). diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 6e1b2b7d..4c23698e 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -115,8 +115,10 @@ end """ ```julia generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; + \tmixtape = NoContext(), \tstrip_llvm = false, \tstrip_asm = true, + \ttarget = (), \topt_level=3, \tkwargs...) ``` @@ -124,6 +126,13 @@ Low level interface for compiling object code (`.o`) for for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. +`mixtape` defines a context that can be used to transform IR prior to compilation using +[Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) features. + +`target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. +This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). +The defaults compile to the native target. + ### Examples ```julia julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) @@ -144,12 +153,12 @@ function generate_obj(f, tt, external = true, path::String = tempname(), name = strip_llvm = false, strip_asm = true, opt_level=3, + target = (), kwargs...) mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - tm = GPUCompiler.llvm_machine(external ? ExternalNativeCompilerTarget() : NativeCompilerTarget()) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. - job = CompilerJob(NativeCompilerTarget(), + job = CompilerJob(NativeCompilerTarget(target...), FunctionSpec(f, tt, false, name), StaticCompilerParams(; opt = true, @@ -162,6 +171,7 @@ function generate_obj(f, tt, external = true, path::String = tempname(), name = # Use Enzyme's annotation and optimization pipeline annotate!(mod) + tm = GPUCompiler.llvm_machine(external ? ExternalNativeCompilerTarget(target...) : NativeCompilerTarget(target...)) optimize!(mod, tm) # Scoop up all the pointers in the optimized module, and replace them with unitialized global variables. diff --git a/src/target.jl b/src/target.jl index 83872a0f..28be261b 100644 --- a/src/target.jl +++ b/src/target.jl @@ -36,11 +36,13 @@ macro device_override(ex) end Base.@kwdef struct NativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget + triple::String=Sys.MACHINE cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) end Base.@kwdef struct ExternalNativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget + triple::String=Sys.MACHINE cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) end @@ -57,7 +59,7 @@ end for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) @eval begin - GPUCompiler.llvm_triple(::$target) = Sys.MACHINE + GPUCompiler.llvm_triple(target::$target) = target.triple function GPUCompiler.llvm_machine(target::$target) triple = GPUCompiler.llvm_triple(target) @@ -94,17 +96,18 @@ function native_job(@nospecialize(func::Function), @nospecialize(types::Type), e mixtape = NoContext(), name = GPUCompiler.safe_name(repr(func)), kernel::Bool = false, + target = (), kwargs... ) source = GPUCompiler.FunctionSpec(func, types, kernel, name) - target = external ? ExternalNativeCompilerTarget() : NativeCompilerTarget() + target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) params = StaticCompilerParams(mixtape = mixtape) StaticCompiler.CompilerJob(target, source, params), kwargs end -function native_job(@nospecialize(func), @nospecialize(types), external; mixtape = NoContext(), kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), kwargs...) +function native_job(@nospecialize(func), @nospecialize(types), external; mixtape = NoContext(), kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), target = (), kwargs...) source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) - target = external ? ExternalNativeCompilerTarget() : NativeCompilerTarget() + target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) params = StaticCompilerParams(mixtape = mixtape) GPUCompiler.CompilerJob(target, source, params), kwargs end diff --git a/test/testintegration.jl b/test/testintegration.jl index 6f98b5fe..4828c614 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -383,3 +383,9 @@ struct MyMix <: CompilationContext end end +@testset "Cross compiling" begin + + x, obj_path = StaticCompiler.generate_obj(x -> 2x, Tuple{Float64}, true, tempname(); target = (triple = "wasm32-unknown-wasi", cpu = "", features = "")) + +end + From 0034adc704d675ad6ee742f92a7eb96fa19b0048 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Tue, 25 Apr 2023 20:59:43 -0400 Subject: [PATCH 114/202] Upgrade to GPUCompiler v0.19 (#113) --- .github/workflows/ci.yml | 4 ++++ .gitignore | 4 ++++ Project.toml | 7 +++--- src/StaticCompiler.jl | 47 ++++++++++++++++++++-------------------- src/code_loading.jl | 2 +- src/interpreter.jl | 2 +- src/pointer_patching.jl | 4 ++-- src/target.jl | 22 ++++++++++--------- test/Project.toml | 6 ++--- test/runtests.jl | 1 + test/testcore.jl | 4 ++-- test/testintegration.jl | 32 ++++++++++++++++----------- 12 files changed, 76 insertions(+), 59 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d312a541..65cbcf3d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,10 @@ jobs: - x64 group: - Core + include: + - arch: x86 + version: '1' + os: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: julia-actions/setup-julia@latest diff --git a/.gitignore b/.gitignore index 872875b3..7d7ea3bc 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,10 @@ /test/test.* Manifest.toml +*.wasm +*.dll +*.o +*.so test.o test.so test.bc diff --git a/Project.toml b/Project.toml index 212d07a2..340a75a4 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,7 @@ Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" CodeInfoTools = "bc773b8a-8374-437a-b9f2-0e9785855863" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +LLD_jll = "d55e3150-da41-5e91-b323-ecfd1eec6109" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" @@ -16,10 +17,10 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] CodeInfoTools = "0.3" -GPUCompiler = "0.17" -LLVM = "4.8, 5" -StaticTools = "0.8" +GPUCompiler = "0.19" +LLVM = "5" MacroTools = "0.5" +StaticTools = "0.8" julia = "1.8, 1.9" [extras] diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 4c23698e..7fe8284d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -23,6 +23,7 @@ include("code_loading.jl") include("optimize.jl") include("quirks.jl") +fix_name(s) = string("julia_", GPUCompiler.safe_name(s)) """ compile(f, types, path::String = tempname()) --> (compiled_f, path) @@ -91,12 +92,13 @@ single method (the method determined by `types`). """ function compile(f, _tt, path::String = tempname(); mixtape = NoContext(), - name = GPUCompiler.safe_name(repr(f)), + name = fix_name(repr(f)), filename = "obj", strip_llvm = false, strip_asm = true, opt_level=3, kwargs...) + tt = Base.to_tuple_type(_tt) isconcretetype(tt) || error("input type signature $_tt is not concrete") @@ -114,7 +116,7 @@ end """ ```julia -generate_obj(f, tt, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; +generate_obj(f, tt, path::String = tempname(), name = fix_name(repr(f)), filenamebase::String="obj"; \tmixtape = NoContext(), \tstrip_llvm = false, \tstrip_asm = true, @@ -148,7 +150,7 @@ shell> tree \$path 0 directories, 1 file ``` """ -function generate_obj(f, tt, external = true, path::String = tempname(), name = GPUCompiler.safe_name(repr(f)), filenamebase::String="obj"; +function generate_obj(f, tt, external = true, path::String = tempname(), name = fix_name(repr(f)), filenamebase::String="obj"; mixtape = NoContext(), strip_llvm = false, strip_asm = true, @@ -158,12 +160,9 @@ function generate_obj(f, tt, external = true, path::String = tempname(), name = mkpath(path) obj_path = joinpath(path, "$filenamebase.o") #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. - job = CompilerJob(NativeCompilerTarget(target...), - FunctionSpec(f, tt, false, name), - StaticCompilerParams(; - opt = true, - mixtape = mixtape, - optlevel = Base.JLOptions().opt_level)) + params = StaticCompilerParams(opt = true, mixtape = mixtape, optlevel = Base.JLOptions().opt_level) + config = GPUCompiler.CompilerConfig(NativeCompilerTarget(target...), params, name = name, kernel = false) + job = GPUCompiler.CompilerJob(GPUCompiler.methodinstance(typeof(f), tt), config) mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false, ctx=context) @@ -259,7 +258,7 @@ shell> ./hello Hello, world! ``` """ -function compile_executable(f::Function, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); +function compile_executable(f::Function, types=(), path::String="./", name=fix_name(repr(f)); filename=name, cflags=``, kwargs... @@ -315,7 +314,7 @@ julia> ccall(("julia_test", "test.dylib"), Float64, (Int64,), 100_000) 5.2564961094956075 ``` """ -function compile_shlib(f::Function, types=(), path::String="./", name=GPUCompiler.safe_name(repr(f)); +function compile_shlib(f::Function, types=(), path::String="./", name=fix_name(repr(f)); filename=name, cflags=``, kwargs... @@ -396,19 +395,19 @@ julia> test(100_000) function generate_shlib_fptr(path::String, name, filename::String=name) lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "julia_$name") + fptr = Libdl.dlsym(ptr, name) @assert fptr != C_NULL fptr end # As above, but also compile (maybe remove this method in the future?) -function generate_shlib_fptr(f, tt, path::String=tempname(), name = GPUCompiler.safe_name(repr(f)), filename::String=name; +function generate_shlib_fptr(f, tt, path::String=tempname(), name = fix_name(repr(f)), filename::String=name; temp::Bool=true, kwargs...) generate_shlib(f, tt, false, path, name; kwargs...) lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "julia_$name") + fptr = Libdl.dlsym(ptr, name) @assert fptr != C_NULL if temp atexit(()->rm(path; recursive=true)) @@ -436,7 +435,7 @@ shell> ./hello Hello, world! ``` """ -function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=string(name); +function generate_executable(f, tt, path=tempname(), name=fix_name(repr(f)), filename=string(name); cflags=``, kwargs... ) @@ -456,18 +455,18 @@ function generate_executable(f, tt, path=tempname(), name=GPUCompiler.safe_name( # Compile! if Sys.isapple() # Apple no longer uses _start, so we can just specify a custom entry - entry = "_julia_$name" + entry = "_$name" run(`$cc -e $entry $cflags $obj_path -o $exec_path`) else # Write a minimal wrapper to avoid having to specify a custom entry wrapper_path = joinpath(path, "wrapper.c") f = open(wrapper_path, "w") - print(f, """int julia_$name(int argc, char** argv); + print(f, """int $name(int argc, char** argv); void* __stack_chk_guard = (void*) $(rand(UInt) >> 1); int main(int argc, char** argv) { - julia_$name(argc, argv); + $name(argc, argv); return 0; }""") close(f) @@ -517,7 +516,7 @@ julia> ccall(("julia_test", "example/test.dylib"), Float64, (Int64,), 100_000) 5.2564961094956075 ``` """ -function generate_shlib(f::Function, tt, external::Bool=true, path::String=tempname(), name=GPUCompiler.safe_name(repr(f)), filename=name; +function generate_shlib(f::Function, tt, external::Bool=true, path::String=tempname(), name=fix_name(repr(f)), filename=name; cflags=``, kwargs... ) @@ -568,7 +567,7 @@ function native_code_typed(@nospecialize(func), @nospecialize(types); kwargs...) end # Return an LLVM module -function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs...) +function native_llvm_module(f, tt, name = fix_name(repr(f)); kwargs...) job, kwargs = native_job(f, tt, true; name, kwargs...) m, _ = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, ctx=context) @@ -576,7 +575,7 @@ function native_llvm_module(f, tt, name = GPUCompiler.safe_name(repr(f)); kwargs return m end -function native_code_native(@nospecialize(f), @nospecialize(tt), name = GPUCompiler.safe_name(repr(f)); kwargs...) +function native_code_native(@nospecialize(f), @nospecialize(tt), name = fix_name(repr(f)); kwargs...) job, kwargs = native_job(f, tt, true; name, kwargs...) GPUCompiler.code_native(stdout, job; kwargs...) end @@ -587,7 +586,7 @@ function native_llvm_module(funcs::Array; demangle = false, kwargs...) mod = native_llvm_module(f,tt, kwargs...) if length(funcs) > 1 for func in funcs[2:end] - @show f,tt = func + f,tt = func tmod = native_llvm_module(f,tt, kwargs...) link!(mod,tmod) end @@ -613,10 +612,10 @@ function generate_obj(funcs::Array, external, path::String = tempname(), filenam strip_asm = true, opt_level=3, kwargs...) - f,tt = funcs[1] + f, tt = funcs[1] mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - fakejob, kwargs = native_job(f,tt, external, kwargs...) + fakejob, kwargs = native_job(f, tt, external, kwargs...) mod = native_llvm_module(funcs; demangle = demangle, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io diff --git a/src/code_loading.jl b/src/code_loading.jl index 665df22a..8fc1ae60 100644 --- a/src/code_loading.jl +++ b/src/code_loading.jl @@ -50,7 +50,7 @@ function instantiate(p::LazyStaticCompiledFunction{rt, tt}) where {rt, tt} dg = LLVM.CreateDynamicLibrarySearchGeneratorForProcess(prefix) LLVM.add!(jd, dg) LLVM.add!(lljit, jd, ofile) - fptr = pointer(LLVM.lookup(lljit, "julia_" * p.name)) + fptr = pointer(LLVM.lookup(lljit, p.name)) StaticCompiledFunction{rt, tt}(p.f, fptr, lljit, p.reloc) end diff --git a/src/interpreter.jl b/src/interpreter.jl index 09ffd4af..89433c92 100644 --- a/src/interpreter.jl +++ b/src/interpreter.jl @@ -3,7 +3,7 @@ using Core.Compiler: AbstractInterpreter, InferenceResult, InferenceParams, InferenceState, MethodInstance, OptimizationParams, WorldView using GPUCompiler: - @safe_debug, AbstractCompilerParams, CodeCache, CompilerJob, FunctionSpec + @safe_debug, AbstractCompilerParams, CodeCache, CompilerJob, methodinstance using CodeInfoTools using CodeInfoTools: resolve diff --git a/src/pointer_patching.jl b/src/pointer_patching.jl index 822d8e9a..1feebe8a 100644 --- a/src/pointer_patching.jl +++ b/src/pointer_patching.jl @@ -137,7 +137,7 @@ function get_pointers!(d, mod, inst) _, gv = d[val] LLVM.API.LLVMSetOperand(inst, i-1, gv) else - gv_name = GPUCompiler.safe_name(String(gensym(repr(Core.Typeof(val))))) + gv_name = fix_name(String(gensym(repr(Core.Typeof(val))))) gv = LLVM.GlobalVariable(mod, llvmeltype(arg), gv_name, LLVM.addrspace(llvmtype(arg))) LLVM.extinit!(gv, true) @@ -157,7 +157,7 @@ llvmeltype(x::LLVM.Value) = eltype(LLVM.llvmtype(x)) function pointer_patching_diff(f, tt, path1=tempname(), path2=tempname(); show_reloc_table=false) tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) - job, kwargs = native_job(f, tt, false; name=GPUCompiler.safe_name(repr(f))) + job, kwargs = native_job(f, tt, false; name=fix_name(repr(f))) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, optimize=false, ctx=context) diff --git a/src/target.jl b/src/target.jl index 28be261b..8ea16ca6 100644 --- a/src/target.jl +++ b/src/target.jl @@ -72,7 +72,7 @@ for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) return tm end - GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{$target}) = "native_$(job.target.cpu)-$(hash(job.target.features))" + GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{$target}) = "native_$(job.config.target.cpu)-$(hash(job.config.target.features))" GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target}) = StaticRuntime GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = StaticRuntime @@ -82,10 +82,10 @@ for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target}) = true GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = - StaticInterpreter(job.params.cache, GPUCompiler.method_table(job), job.source.world, + StaticInterpreter(job.config.params.cache, GPUCompiler.method_table(job), job.world, GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job), - job.params.mixtape) - GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = job.params.cache + job.config.params.mixtape) + GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = job.config.params.cache end end @@ -94,20 +94,22 @@ GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNati function native_job(@nospecialize(func::Function), @nospecialize(types::Type), external::Bool; mixtape = NoContext(), - name = GPUCompiler.safe_name(repr(func)), + name = fix_name(repr(func)), kernel::Bool = false, target = (), kwargs... ) - source = GPUCompiler.FunctionSpec(func, types, kernel, name) + source = methodinstance(typeof(func), Base.to_tuple_type(types)) target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) params = StaticCompilerParams(mixtape = mixtape) - StaticCompiler.CompilerJob(target, source, params), kwargs + config = GPUCompiler.CompilerConfig(target, params, name = name, kernel = kernel) + StaticCompiler.CompilerJob(source, config), kwargs end -function native_job(@nospecialize(func), @nospecialize(types), external; mixtape = NoContext(), kernel::Bool=false, name=GPUCompiler.safe_name(repr(func)), target = (), kwargs...) - source = GPUCompiler.FunctionSpec(func, Base.to_tuple_type(types), kernel, name) +function native_job(@nospecialize(func), @nospecialize(types), external; mixtape = NoContext(), kernel::Bool=false, name=fix_name(repr(func)), target = (), kwargs...) + source = methodinstance(typeof(func), Base.to_tuple_type(types)) target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) params = StaticCompilerParams(mixtape = mixtape) - GPUCompiler.CompilerJob(target, source, params), kwargs + config = GPUCompiler.CompilerConfig(target, params, name = name, kernel = kernel) + GPUCompiler.CompilerJob(source, config), kwargs end diff --git a/test/Project.toml b/test/Project.toml index c9d1de55..cc88d4e8 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -3,14 +3,14 @@ CodeInfoTools = "bc773b8a-8374-437a-b9f2-0e9785855863" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" -Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b" +LLD_jll = "d55e3150-da41-5e91-b323-ecfd1eec6109" +LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" -LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index d117aa58..697ef7e8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,6 +9,7 @@ using StaticTools using StrideArraysCore using CodeInfoTools using MacroTools +using LLD_jll addprocs(1) @everywhere using StaticCompiler, StrideArraysCore diff --git a/test/testcore.jl b/test/testcore.jl index 450fbdad..68555df7 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -232,13 +232,13 @@ end # fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) #Compile dylib - name = repr(fib) + name = "julia_" * repr(fib) filepath = compile_shlib(fib, (Int,), "./", name) @test occursin("fib.$(Libdl.dlext)", filepath) # Open dylib ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "julia_$name") + fptr = Libdl.dlsym(ptr, name) @test fptr != C_NULL @test ccall(fptr, Int, (Int,), 10) == 55 end diff --git a/test/testintegration.jl b/test/testintegration.jl index 4828c614..cc5818da 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -14,7 +14,7 @@ # faster. status = -1 try - isfile("times_table") && rm("times_table") + isfile("julia_times_table") && rm("julia_times_table") status = run(`$jlpath --startup=no --compile=min $testpath/scripts/times_table.jl`) catch e @warn "Could not compile $testpath/scripts/times_table.jl" @@ -27,7 +27,7 @@ println("5x5 times table:") status = -1 try - status = run(`./times_table 5 5`) + status = run(`./julia_times_table 5 5`) catch e @warn "Could not run $(scratch)/times_table" println(e) @@ -58,7 +58,7 @@ println("3x3 malloc arrays via do-block syntax:") status = -1 try - status = run(`./withmallocarray 3 3`) + status = run(`./julia_withmallocarray 3 3`) catch e @warn "Could not run $(scratch)/withmallocarray" println(e) @@ -85,7 +85,7 @@ println("5x5 uniform random matrix:") status = -1 try - status = run(`./rand_matrix 5 5`) + status = run(`./julia_rand_matrix 5 5`) catch e @warn "Could not run $(scratch)/rand_matrix" println(e) @@ -113,7 +113,7 @@ println("5x5 Normal random matrix:") status = -1 try - status = run(`./randn_matrix 5 5`) + status = run(`./julia_randn_matrix 5 5`) catch e @warn "Could not run $(scratch)/randn_matrix" println(e) @@ -143,7 +143,7 @@ println("10x10 table sum:") status = -1 try - status = run(`./loopvec_product 10 10`) + status = run(`./julia_loopvec_product 10 10`) catch e @warn "Could not run $(scratch)/loopvec_product" println(e) @@ -171,7 +171,7 @@ println("10x5 matrix product:") status = -1 try - status = run(`./loopvec_matrix 10 5`) + status = run(`./julia_loopvec_matrix 10 5`) catch e @warn "Could not run $(scratch)/loopvec_matrix" println(e) @@ -202,7 +202,7 @@ println("10x5 matrix product:") status = -1 try - status = run(`./loopvec_matrix_stack`) + status = run(`./julia_loopvec_matrix_stack`) catch e @warn "Could not run $(scratch)/loopvec_matrix_stack" println(e) @@ -233,7 +233,7 @@ println("String indexing and handling:") status = -1 try - status = run(`./print_args foo bar`) + status = run(`./julia_print_args foo bar`) catch e @warn "Could not run $(scratch)/print_args" println(e) @@ -261,7 +261,7 @@ println("Error handling:") status = -1 try - status = run(`./maybe_throw 10`) + status = run(`./julia_maybe_throw 10`) catch e @warn "Could not run $(scratch)/maybe_throw" println(e) @@ -297,7 +297,7 @@ println("Interop:") status = -1 try - status = run(`./interop`) + status = run(`./julia_interop`) catch e @warn "Could not run $(scratch)/interop" println(e) @@ -383,9 +383,15 @@ struct MyMix <: CompilationContext end end -@testset "Cross compiling" begin +@testset "Cross compiling to WebAssembly" begin - x, obj_path = StaticCompiler.generate_obj(x -> 2x, Tuple{Float64}, true, tempname(); target = (triple = "wasm32-unknown-wasi", cpu = "", features = "")) + m2(x) = 2x + obj_path, name = StaticCompiler.generate_obj(m2, Tuple{Float64}, true, tempname(); target = (triple = "wasm32-unknown-unknown", cpu = "", features = "")) + # link with `lld` from LLD_jll + run(`$(lld()) -flavor wasm --no-entry --export-all $obj_path/obj.o -o $name.wasm`) + # On Julia v1.9, link with included linker + # run(`$(Base.Linking.lld()) -flavor wasm --no-entry --export-all $obj_path/obj.o -o $name.wasm`) + # run(`wasm2wat $name.wasm`) # to see a text representation (wasm2wat isn't included) end From 2ef9d757f33190936e0c0aae59bb5c4307fcd7c3 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Fri, 5 May 2023 17:35:28 -0400 Subject: [PATCH 115/202] Helpers to compile to WebAssembly (#114) --- .github/workflows/ci.yml | 2 +- src/StaticCompiler.jl | 46 +++++++++++++++++++++++++++++++++++----- src/optimize.jl | 7 ++++-- test/testintegration.jl | 12 +++++------ 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 65cbcf3d..12dae360 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: matrix: version: - '1.8' - - '^1.9.0-rc2' + - '^1.9.0-rc3' os: - ubuntu-latest - macOS-latest diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 7fe8284d..0c42dbea 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -8,10 +8,11 @@ using Libdl: Libdl, dlsym, dlopen using Base: RefValue using Serialization: serialize, deserialize using Clang_jll: clang +using LLD_jll: lld using StaticTools using StaticTools: @symbolcall, @c_str, println -export compile, load_function, compile_shlib, compile_executable +export compile, load_function, compile_shlib, compile_executable, compile_wasm export native_code_llvm, native_code_typed, native_llvm_module, native_code_native export @device_override, @print_and_throw @@ -154,7 +155,8 @@ function generate_obj(f, tt, external = true, path::String = tempname(), name = mixtape = NoContext(), strip_llvm = false, strip_asm = true, - opt_level=3, + opt_level = 3, + remove_julia_addrspaces = false, target = (), kwargs...) mkpath(path) @@ -180,7 +182,7 @@ function generate_obj(f, tt, external = true, path::String = tempname(), name = # Now that we've removed all the pointers from the code, we can (hopefully) safely lower all the instrinsics # (again, using Enzyme's pipeline) - post_optimize!(mod, tm) + post_optimize!(mod, tm; remove_julia_addrspaces) # Make sure we didn't make any glaring errors LLVM.verify(mod) @@ -355,6 +357,40 @@ function compile_shlib(funcs::Array, path::String="./"; joinpath(abspath(path), filename * "." * Libdl.dlext) end +""" +```julia +compile_wasm(f::Function, types::Tuple, [path::String="./"], [name::String=repr(f)]; filename::String=name, flags=``, kwargs...) +compile_wasm(funcs::Array, [path::String="./"]; filename="libfoo", demangle=false, flags=``, kwargs...) +``` +As `compile_shlib`, but compiling to a WebAssembly library. + +The compiled function is by default given the symbol name `julia_$(name)`, i.e., +the function `test` in the example below is called `julia_test` in the shared library. +The keword argument `demangle=true` will remove this prefix, but is currently only +supported the second (multi-function-shlib) method. +``` +""" +function compile_wasm(f::Function, types=(); + path::String="./", + filename=fix_name(repr(f)), + flags=``, + kwargs... + ) + tt = Base.to_tuple_type(types) + obj_path, name = generate_obj(f, tt, true, path, filename; target = (triple = "wasm32-unknown-wasi", cpu = "", features = ""), remove_julia_addrspaces = true, kwargs...) + run(`$(lld()) -flavor wasm --no-entry --export-all $flags $obj_path/obj.o -o $path/$name.wasm`) + joinpath(abspath(path), filename * ".wasm") +end +function compile_wasm(funcs::Array; + path::String="./", + filename="libfoo", + flags=``, + kwargs... + ) + obj_path, name = generate_obj(funcs, true; target = (triple = "wasm32-unknown-wasi", cpu = "", features = ""), remove_julia_addrspaces = true, kwargs...) + run(`$(lld()) -flavor wasm --no-entry --export-all $flags $obj_path/obj.o -o $path/$filename.wasm`) + joinpath(abspath(path), filename * ".wasm") +end """ ```julia @@ -606,7 +642,7 @@ function native_llvm_module(funcs::Array; demangle = false, kwargs...) return mod end -function generate_obj(funcs::Array, external, path::String = tempname(), filenamebase::String="obj"; +function generate_obj(funcs::Array, external::Bool, path::String = tempname(), filenamebase::String="obj"; demangle =false, strip_llvm = false, strip_asm = true, @@ -615,7 +651,7 @@ function generate_obj(funcs::Array, external, path::String = tempname(), filenam f, tt = funcs[1] mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - fakejob, kwargs = native_job(f, tt, external, kwargs...) + fakejob, kwargs = native_job(f, tt, external; kwargs...) mod = native_llvm_module(funcs; demangle = demangle, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io diff --git a/src/optimize.jl b/src/optimize.jl index f2934a1d..0bc4646e 100644 --- a/src/optimize.jl +++ b/src/optimize.jl @@ -211,8 +211,8 @@ function addJuliaLegalizationPasses!(pm, lower_intrinsics=true) remove_ni!(pm) end end - -function post_optimize!(mod, tm) + +function post_optimize!(mod, tm; remove_julia_addrspaces = false) # @show "pre_post", mod # flush(stdout) # flush(stderr) @@ -224,6 +224,9 @@ function post_optimize!(mod, tm) LLVM.ModulePassManager() do pm addJuliaLegalizationPasses!(pm, true) addMachinePasses!(pm) + if remove_julia_addrspaces + remove_julia_addrspaces!(pm) + end run!(pm, mod) end # @show "post_mod", mod diff --git a/test/testintegration.jl b/test/testintegration.jl index cc5818da..7582df00 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -386,12 +386,12 @@ end @testset "Cross compiling to WebAssembly" begin m2(x) = 2x - obj_path, name = StaticCompiler.generate_obj(m2, Tuple{Float64}, true, tempname(); target = (triple = "wasm32-unknown-unknown", cpu = "", features = "")) - # link with `lld` from LLD_jll - run(`$(lld()) -flavor wasm --no-entry --export-all $obj_path/obj.o -o $name.wasm`) - # On Julia v1.9, link with included linker - # run(`$(Base.Linking.lld()) -flavor wasm --no-entry --export-all $obj_path/obj.o -o $name.wasm`) - # run(`wasm2wat $name.wasm`) # to see a text representation (wasm2wat isn't included) + m3(x) = 3x + wasm_path = compile_wasm(m2, Tuple{Float64}) + wasm_path2 = compile_wasm([(m2, Tuple{Float64}), (m3, Tuple{Float64})]) + + wasm_path = compile_wasm(m2, (Float64,)) + wasm_path2 = compile_wasm([(m2, (Float64,)), (m3, (Float64,))]) end From 7a141cbe0c1a0ab17c13308098e6eb10f6d346da Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Fri, 5 May 2023 18:46:20 -0600 Subject: [PATCH 116/202] actually test `maybe_throw` --- test/testcore.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testcore.jl b/test/testcore.jl index 68555df7..be2eed31 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -293,7 +293,7 @@ end # Compile a function that definitely fails @inline foo_err() = UInt64(-1) - filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), tempdir()) + filepath = compile_executable(maybe_throw, (Int, Ptr{Ptr{UInt8}}), tempdir()) @test isfile(filepath) status = -1 try From 99d5455875be80f10df5c04e0e74e5a42802026d Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Fri, 5 May 2023 18:51:53 -0600 Subject: [PATCH 117/202] Update testcore.jl --- test/testcore.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testcore.jl b/test/testcore.jl index be2eed31..83a6b645 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -293,7 +293,7 @@ end # Compile a function that definitely fails @inline foo_err() = UInt64(-1) - filepath = compile_executable(maybe_throw, (Int, Ptr{Ptr{UInt8}}), tempdir()) + filepath = compile_executable(foo_err, (Int, Ptr{Ptr{UInt8}}), tempdir()) @test isfile(filepath) status = -1 try From 4f10d57c25f6742cdd461f852ff96d8dd1f27610 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Fri, 5 May 2023 18:55:29 -0600 Subject: [PATCH 118/202] provide correct signature --- test/testcore.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testcore.jl b/test/testcore.jl index 83a6b645..4c0bc5fd 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -293,7 +293,7 @@ end # Compile a function that definitely fails @inline foo_err() = UInt64(-1) - filepath = compile_executable(foo_err, (Int, Ptr{Ptr{UInt8}}), tempdir()) + filepath = compile_executable(foo_err, (), tempdir()) @test isfile(filepath) status = -1 try From c3f7340a1ed0c29733ffe543d3297234102ce058 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Mon, 8 May 2023 15:59:16 -0600 Subject: [PATCH 119/202] Re-add nightly tests and integration tests (#117) * Re-add ci-julia-nightly.yml * Create ci-integration.yml * Create ci-integration-nightly.yml --- .github/workflows/ci-integration-nightly.yml | 39 ++++++++++++++++++++ .github/workflows/ci-integration.yml | 38 +++++++++++++++++++ .github/workflows/ci-julia-nightly.yml | 37 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 .github/workflows/ci-integration-nightly.yml create mode 100644 .github/workflows/ci-integration.yml create mode 100644 .github/workflows/ci-julia-nightly.yml diff --git a/.github/workflows/ci-integration-nightly.yml b/.github/workflows/ci-integration-nightly.yml new file mode 100644 index 00000000..6d4cb3e2 --- /dev/null +++ b/.github/workflows/ci-integration-nightly.yml @@ -0,0 +1,39 @@ +name: CI (Integration nightly) +on: + push: + branches: + - '**' + paths-ignore: + - 'README.md' + pull_request: + branches: + - master + paths-ignore: + - 'README.md' +jobs: + test-integration-nightly: + name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + version: + - 'nightly' + - '~1.9.0-0' + os: + - ubuntu-latest + - macOS-latest + arch: + - x64 + group: + - Integration + steps: + - uses: actions/checkout@v2 + - uses: julia-actions/setup-julia@latest + with: + version: ${{ matrix.version }} + arch: ${{ matrix.arch }} + - uses: julia-actions/julia-buildpkg@latest + - uses: julia-actions/julia-runtest@latest + env: + GROUP: ${{ matrix.group }} diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml new file mode 100644 index 00000000..3b9024df --- /dev/null +++ b/.github/workflows/ci-integration.yml @@ -0,0 +1,38 @@ +name: CI (Integration) +on: + push: + branches: + - '**' + paths-ignore: + - 'README.md' + pull_request: + branches: + - master + paths-ignore: + - 'README.md' +jobs: + test-integration: + name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + version: + - '1.8' + os: + - ubuntu-latest + - macOS-latest + arch: + - x64 + group: + - Integration + steps: + - uses: actions/checkout@v2 + - uses: julia-actions/setup-julia@latest + with: + version: ${{ matrix.version }} + arch: ${{ matrix.arch }} + - uses: julia-actions/julia-buildpkg@latest + - uses: julia-actions/julia-runtest@latest + env: + GROUP: ${{ matrix.group }} diff --git a/.github/workflows/ci-julia-nightly.yml b/.github/workflows/ci-julia-nightly.yml new file mode 100644 index 00000000..4cdbcfa8 --- /dev/null +++ b/.github/workflows/ci-julia-nightly.yml @@ -0,0 +1,37 @@ +name: CI (Julia nightly) +on: + push: + branches: + - '**' + paths-ignore: + - 'README.md' + pull_request: + branches: + - master + paths-ignore: + - 'README.md' +jobs: + test-julia-nightly: + name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + version: + - 'nightly' + os: + - ubuntu-latest + arch: + - x64 + group: + - Core + steps: + - uses: actions/checkout@v2 + - uses: julia-actions/setup-julia@latest + with: + version: ${{ matrix.version }} + arch: ${{ matrix.arch }} + - uses: julia-actions/julia-buildpkg@latest + - uses: julia-actions/julia-runtest@latest + env: + GROUP: ${{ matrix.group }} From a076a623b2c8dcac449b9e29e42a23c9fe84878a Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Mon, 8 May 2023 18:22:36 -0600 Subject: [PATCH 120/202] Fixup with current versions (#120) * add more escaping slashes to docstring * update llvmtype -> value_type * properly splat kwargs * test 1.9.0-rc3 in the integration tests * remove 1.9.0 from the nightly integration tests * fix for julia master * remove some --compile=min * [noci] bump version --- .github/workflows/ci-integration-nightly.yml | 1 - .github/workflows/ci-integration.yml | 1 + Project.toml | 2 +- src/StaticCompiler.jl | 8 ++++---- src/interpreter.jl | 11 ++++++++--- src/pointer_patching.jl | 4 ++-- test/testintegration.jl | 4 ++-- 7 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci-integration-nightly.yml b/.github/workflows/ci-integration-nightly.yml index 6d4cb3e2..00074acc 100644 --- a/.github/workflows/ci-integration-nightly.yml +++ b/.github/workflows/ci-integration-nightly.yml @@ -19,7 +19,6 @@ jobs: matrix: version: - 'nightly' - - '~1.9.0-0' os: - ubuntu-latest - macOS-latest diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index 3b9024df..da4da2d8 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -18,6 +18,7 @@ jobs: fail-fast: false matrix: version: + - '1.9.0-rc3' - '1.8' os: - ubuntu-latest diff --git a/Project.toml b/Project.toml index 340a75a4..40375144 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.4.10" +version = "0.4.11" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 0c42dbea..79f7c86c 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -211,7 +211,7 @@ julia> using StaticCompiler julia> function puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates. # Note, this `llvmcall` requires Julia 1.8+ - Base.llvmcall((\""" + Base.llvmcall((\"\"\" ; External declaration of the puts function declare i32 @puts(i8* nocapture) nounwind @@ -220,7 +220,7 @@ julia> function puts(s::Ptr{UInt8}) # Can't use Base.println because it allocate %call = call i32 (i8*) @puts(i8* %0) ret i32 0 } - \""", "main"), Int32, Tuple{Ptr{UInt8}}, s) + \"\"\", "main"), Int32, Tuple{Ptr{UInt8}}, s) end puts (generic function with 1 method) @@ -619,11 +619,11 @@ end #Return an LLVM module for multiple functions function native_llvm_module(funcs::Array; demangle = false, kwargs...) f,tt = funcs[1] - mod = native_llvm_module(f,tt, kwargs...) + mod = native_llvm_module(f,tt; kwargs...) if length(funcs) > 1 for func in funcs[2:end] f,tt = func - tmod = native_llvm_module(f,tt, kwargs...) + tmod = native_llvm_module(f,tt; kwargs...) link!(mod,tmod) end end diff --git a/src/interpreter.jl b/src/interpreter.jl index 89433c92..c0e00c37 100644 --- a/src/interpreter.jl +++ b/src/interpreter.jl @@ -1,7 +1,7 @@ ## interpreter using Core.Compiler: - AbstractInterpreter, InferenceResult, InferenceParams, InferenceState, MethodInstance, OptimizationParams, WorldView + AbstractInterpreter, InferenceResult, InferenceParams, InferenceState, MethodInstance, OptimizationParams, WorldView, get_world_counter using GPUCompiler: @safe_debug, AbstractCompilerParams, CodeCache, CompilerJob, methodinstance using CodeInfoTools @@ -85,8 +85,13 @@ function custom_pass!(interp::StaticInterpreter, result::InferenceResult, mi::Co return src end -function InferenceState(result::InferenceResult, cache::Symbol, interp::StaticInterpreter) - src = Core.Compiler.retrieve_code_info(result.linfo) +function Core.Compiler.InferenceState(result::InferenceResult, cache::Symbol, interp::StaticInterpreter) + world = get_world_counter(interp) + src = @static if VERSION >= v"1.10.0-DEV.873" + Core.Compiler.retrieve_code_info(result.linfo, world) + else + Core.Compiler.retrieve_code_info(result.linfo) + end mi = result.linfo src = custom_pass!(interp, result, mi, src) src === nothing && return nothing diff --git a/src/pointer_patching.jl b/src/pointer_patching.jl index 1feebe8a..c361c3d5 100644 --- a/src/pointer_patching.jl +++ b/src/pointer_patching.jl @@ -138,7 +138,7 @@ function get_pointers!(d, mod, inst) LLVM.API.LLVMSetOperand(inst, i-1, gv) else gv_name = fix_name(String(gensym(repr(Core.Typeof(val))))) - gv = LLVM.GlobalVariable(mod, llvmeltype(arg), gv_name, LLVM.addrspace(llvmtype(arg))) + gv = LLVM.GlobalVariable(mod, llvmeltype(arg), gv_name, LLVM.addrspace(value_type(arg))) LLVM.extinit!(gv, true) LLVM.API.LLVMSetOperand(inst, i-1, gv) @@ -153,7 +153,7 @@ function get_pointers!(d, mod, inst) end end -llvmeltype(x::LLVM.Value) = eltype(LLVM.llvmtype(x)) +llvmeltype(x::LLVM.Value) = eltype(LLVM.value_type(x)) function pointer_patching_diff(f, tt, path1=tempname(), path2=tempname(); show_reloc_table=false) tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) diff --git a/test/testintegration.jl b/test/testintegration.jl index 7582df00..0dacb828 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -159,7 +159,7 @@ status = -1 try isfile("loopvec_matrix") && rm("loopvec_matrix") - status = run(`$jlpath --startup=no --compile=min $testpath/scripts/loopvec_matrix.jl`) + status = run(`$jlpath --startup=no $testpath/scripts/loopvec_matrix.jl`) catch e @warn "Could not compile $testpath/scripts/loopvec_matrix.jl" println(e) @@ -190,7 +190,7 @@ status = -1 try isfile("loopvec_matrix_stack") && rm("loopvec_matrix_stack") - status = run(`$jlpath --startup=no --compile=min $testpath/scripts/loopvec_matrix_stack.jl`) + status = run(`$jlpath --startup=no $testpath/scripts/loopvec_matrix_stack.jl`) catch e @warn "Could not compile $testpath/scripts/loopvec_matrix_stack.jl" println(e) From 70d406566e7c24c7a3487a9b11e742e78c9dfcd8 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 10 May 2023 13:52:22 -0600 Subject: [PATCH 121/202] Cleanup and more support for arrays of functions (reland) (#122) * Cleanup and more support for arrays of functions * switch wasm back to generate_obj_for_compile --- src/StaticCompiler.jl | 140 +++++++++++++++++++++++++++++++----------- 1 file changed, 103 insertions(+), 37 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 79f7c86c..7c9c1037 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -106,7 +106,7 @@ function compile(f, _tt, path::String = tempname(); rt = last(only(native_code_typed(f, tt, mixtape = mixtape))) isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - _, _, table = generate_obj(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, false, path, name; mixtape = mixtape, opt_level, strip_llvm, strip_asm, filename, kwargs...) + _, _, table = generate_obj_for_compile(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, false, path, name; mixtape = mixtape, opt_level, strip_llvm, strip_asm, filename, kwargs...) lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name, filename, table) cjl_path = joinpath(path, "$filename.cjl") @@ -117,7 +117,7 @@ end """ ```julia -generate_obj(f, tt, path::String = tempname(), name = fix_name(repr(f)), filenamebase::String="obj"; +generate_obj_for_compile(f, tt, path::String = tempname(), name = fix_name(repr(f)), filenamebase::String="obj"; \tmixtape = NoContext(), \tstrip_llvm = false, \tstrip_asm = true, @@ -141,7 +141,7 @@ The defaults compile to the native target. julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) fib (generic function with 1 method) -julia> path, name, table = StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test") +julia> path, name, table = StaticCompiler.generate_obj_for_compile(fib, Tuple{Int64}, "./test") ("./test", "fib", IdDict{Any, String}()) shell> tree \$path @@ -151,7 +151,7 @@ shell> tree \$path 0 directories, 1 file ``` """ -function generate_obj(f, tt, external = true, path::String = tempname(), name = fix_name(repr(f)), filenamebase::String="obj"; +function generate_obj_for_compile(f, tt, external = true, path::String = tempname(), name = fix_name(repr(f)), filenamebase::String="obj"; mixtape = NoContext(), strip_llvm = false, strip_asm = true, @@ -200,10 +200,13 @@ end compile_executable(f::Function, types::Tuple, path::String, [name::String=repr(f)]; filename::String=name, cflags=``, # Specify libraries you would like to link against, and other compiler options here + also_expose=[], kwargs... ) ``` Attempt to compile a standalone executable that runs function `f` with a type signature given by the tuple of `types`. +If there are extra methods you would like to protect from name mangling in the produced binary for whatever reason, +you can provide them as a vector of tuples of functions and types, i.e. `[(f1, types1), (f2, types2), ...]` ### Examples ```julia @@ -261,11 +264,21 @@ Hello, world! ``` """ function compile_executable(f::Function, types=(), path::String="./", name=fix_name(repr(f)); + also_expose=[], + filename=name, + cflags=``, + kwargs...) + compile_executable(vcat([(f, types)], also_expose), path, name; filename, cflags, kwargs...) +end + + +function compile_executable(funcs::Array, path::String="./", name=fix_name(repr(funcs[1][1])); filename=name, cflags=``, kwargs... ) + (f, types) = funcs[1] tt = Base.to_tuple_type(types) isexecutableargtype = tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" @@ -274,13 +287,11 @@ function compile_executable(f::Function, types=(), path::String="./", name=fix_n isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") 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(f, tt, path, name, filename; cflags=cflags, kwargs...) - + + generate_executable(funcs, path, name, filename; cflags=cflags, kwargs...) joinpath(abspath(path), filename) end - """ ```julia compile_shlib(f::Function, types::Tuple, [path::String="./"], [name::String=repr(f)]; filename::String=name, cflags=``, kwargs...) @@ -377,7 +388,7 @@ function compile_wasm(f::Function, types=(); kwargs... ) tt = Base.to_tuple_type(types) - obj_path, name = generate_obj(f, tt, true, path, filename; target = (triple = "wasm32-unknown-wasi", cpu = "", features = ""), remove_julia_addrspaces = true, kwargs...) + obj_path, name = generate_obj_for_compile(f, tt, true, path, filename; target = (triple = "wasm32-unknown-wasi", cpu = "", features = ""), remove_julia_addrspaces = true, kwargs...) run(`$(lld()) -flavor wasm --no-entry --export-all $flags $obj_path/obj.o -o $path/$name.wasm`) joinpath(abspath(path), filename * ".wasm") end @@ -471,21 +482,19 @@ shell> ./hello Hello, world! ``` """ -function generate_executable(f, tt, path=tempname(), name=fix_name(repr(f)), filename=string(name); - cflags=``, - kwargs... - ) - mkpath(path) - obj_path = joinpath(path, "$filename.o") - exec_path = joinpath(path, filename) - job, kwargs = native_job(f, tt, true; name, kwargs...) - obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) - - # Write to file - open(obj_path, "w") do io - write(io, obj) - end +function generate_executable(f, tt, args...; kwargs...) + generate_executable([(f, tt)], args...; kwargs...) +end +function generate_executable(funcs::Array, path=tempname(), name=fix_name(repr(funcs[1][1])), filename=string(name); + demangle=false, + cflags=``, + kwargs... + ) + lib_path = joinpath(path, "$filename.$(Libdl.dlext)") + exec_path = joinpath(path, filename) + external = true + _, obj_path = generate_obj(funcs, external, path, filename; demangle=demangle, kwargs...) # Pick a compiler cc = Sys.isapple() ? `cc` : clang() # Compile! @@ -510,10 +519,10 @@ function generate_executable(f, tt, path=tempname(), name=fix_name(repr(f)), fil # Clean up run(`rm $wrapper_path`) end - path, name end + """ ```julia generate_shlib(f::Function, tt, [external::Bool=true], [path::String], [name], [filename]; kwargs...) @@ -553,23 +562,15 @@ julia> ccall(("julia_test", "example/test.dylib"), Float64, (Int64,), 100_000) ``` """ function generate_shlib(f::Function, tt, external::Bool=true, path::String=tempname(), name=fix_name(repr(f)), filename=name; - cflags=``, + cflags=``, demangle=false, kwargs... ) - - mkpath(path) - obj_path = joinpath(path, "$filename.o") lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - job, kwargs = native_job(f, tt, external; name, kwargs...) - obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false) - - open(obj_path, "w") do io - write(io, obj) - end - + _, obj_path = generate_obj([(f, tt)], external, path, filename; demangle=demangle, kwargs...) + # Pick a Clang cc = Sys.isapple() ? `cc` : clang() - # Compile! + # Compile run(`$cc -shared $cflags $obj_path -o $lib_path`) path, name @@ -583,7 +584,7 @@ function generate_shlib(funcs::Array, external::Bool=true, path::String=tempname lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - _,obj_path = generate_obj(funcs, external, path, filename; demangle=demangle, kwargs...) + _, obj_path = generate_obj(funcs, external, path, filename; demangle=demangle, kwargs...) # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! @@ -642,6 +643,71 @@ function native_llvm_module(funcs::Array; demangle = false, kwargs...) return mod end + +""" +```julia +generate_obj(f, tt, external::Bool, path::String = tempname(), filenamebase::String="obj"; + mixtape = NoContext(), + target = (), + demangle =false, + strip_llvm = false, + strip_asm = true, + opt_level=3, + kwargs...) +``` +Low level interface for compiling object code (`.o`) for for function `f` given +a tuple type `tt` characterizing the types of the arguments for which the +function will be compiled. + +`mixtape` defines a context that can be used to transform IR prior to compilation using +[Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) features. + +`target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. +This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). +The defaults compile to the native target. + +### Examples +```julia +julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) +fib (generic function with 1 method) + +julia> path, name, table = StaticCompiler.generate_obj_for_compile(fib, Tuple{Int64}, "./test") +("./test", "fib", IdDict{Any, String}()) + +shell> tree \$path +./test +└── obj.o + +0 directories, 1 file +``` +""" +function generate_obj(f, tt, args...; kwargs...) + generate_obj([(f, tt)], args...; kwargs...) +end + + +""" +```julia +generate_obj(funcs::Array, external::Bool, path::String = tempname(), filenamebase::String="obj"; + mixtape = NoContext(), + target = (), + demangle =false, + strip_llvm = false, + strip_asm = true, + opt_level=3, + kwargs...) +``` +Low level interface for compiling object code (`.o`) for an array of Tuples +(f, tt) where each function `f` and tuple type `tt` determine the set of methods +which will be compiled. + +`mixtape` defines a context that can be used to transform IR prior to compilation using +[Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) features. + +`target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. +This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). +The defaults compile to the native target. +""" function generate_obj(funcs::Array, external::Bool, path::String = tempname(), filenamebase::String="obj"; demangle =false, strip_llvm = false, From a571cb1d473b4bfa4ba293fbfc71a8b5c2e41176 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sun, 14 May 2023 11:41:54 +0100 Subject: [PATCH 122/202] Demangle (#123) * Attempt to fix demangling * Fixup demangling, also general cleanup * Check names after constructing llvm module * Equivalent demangling on non-mac systems * Bump version to 0.4.12 * Test against released version of 1.9 --- .github/workflows/ci-integration.yml | 2 +- .github/workflows/ci.yml | 4 +- Project.toml | 2 +- src/StaticCompiler.jl | 164 ++++++++++++--------------- src/target.jl | 14 +-- test/testcore.jl | 37 ++++-- test/testintegration.jl | 46 ++++---- 7 files changed, 139 insertions(+), 130 deletions(-) diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index da4da2d8..be2fadde 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -18,8 +18,8 @@ jobs: fail-fast: false matrix: version: - - '1.9.0-rc3' - '1.8' + - '1.9' os: - ubuntu-latest - macOS-latest diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 12dae360..eb6a3fa6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: matrix: version: - '1.8' - - '^1.9.0-rc3' + - '1.9' os: - ubuntu-latest - macOS-latest @@ -30,7 +30,7 @@ jobs: include: - arch: x86 version: '1' - os: ubuntu-latest + os: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: julia-actions/setup-julia@latest diff --git a/Project.toml b/Project.toml index 40375144..1fcd7781 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.4.11" +version = "0.4.12" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 7c9c1037..96189b0c 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -24,7 +24,8 @@ include("code_loading.jl") include("optimize.jl") include("quirks.jl") -fix_name(s) = string("julia_", GPUCompiler.safe_name(s)) +fix_name(f::Function) = fix_name(repr(f)) +fix_name(s) = String(GPUCompiler.safe_name(s)) """ compile(f, types, path::String = tempname()) --> (compiled_f, path) @@ -91,9 +92,9 @@ with `load_function(path)`. `LazyStaticcompiledfunction`s contain the requisite `StaticCompiledFunction` and may be called with arguments of type `types` as if it were a function with a single method (the method determined by `types`). """ -function compile(f, _tt, path::String = tempname(); - mixtape = NoContext(), - name = fix_name(repr(f)), +function compile(f, _tt, path::String = tempname(); + mixtape = NoContext(), + name = fix_name(f), filename = "obj", strip_llvm = false, strip_asm = true, @@ -117,7 +118,7 @@ end """ ```julia -generate_obj_for_compile(f, tt, path::String = tempname(), name = fix_name(repr(f)), filenamebase::String="obj"; +generate_obj_for_compile(f, tt, path::String = tempname(), name = fix_name(f), filenamebase::String="obj"; \tmixtape = NoContext(), \tstrip_llvm = false, \tstrip_asm = true, @@ -129,11 +130,11 @@ Low level interface for compiling object code (`.o`) for for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. -`mixtape` defines a context that can be used to transform IR prior to compilation using +`mixtape` defines a context that can be used to transform IR prior to compilation using [Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) features. `target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. -This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). +This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). The defaults compile to the native target. ### Examples @@ -151,7 +152,7 @@ shell> tree \$path 0 directories, 1 file ``` """ -function generate_obj_for_compile(f, tt, external = true, path::String = tempname(), name = fix_name(repr(f)), filenamebase::String="obj"; +function generate_obj_for_compile(f, tt, external = true, path::String = tempname(), name = fix_name(f), filenamebase::String="obj"; mixtape = NoContext(), strip_llvm = false, strip_asm = true, @@ -168,7 +169,7 @@ function generate_obj_for_compile(f, tt, external = true, path::String = tempnam mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false, ctx=context) - end + end # Use Enzyme's annotation and optimization pipeline annotate!(mod) @@ -263,17 +264,15 @@ shell> ./hello Hello, world! ``` """ -function compile_executable(f::Function, types=(), path::String="./", name=fix_name(repr(f)); - also_expose=[], - filename=name, - cflags=``, +function compile_executable(f::Function, types=(), path::String="./", name=fix_name(f); + also_expose=Tuple{Function, Tuple{DataType}}[], kwargs...) - compile_executable(vcat([(f, types)], also_expose), path, name; filename, cflags, kwargs...) + compile_executable(vcat([(f, types)], also_expose), path, name; kwargs...) end - -function compile_executable(funcs::Array, path::String="./", name=fix_name(repr(funcs[1][1])); +function compile_executable(funcs::Union{Array,Tuple}, path::String="./", name=fix_name(first(first(funcs))); filename=name, + demangle=false, cflags=``, kwargs... ) @@ -287,8 +286,8 @@ function compile_executable(funcs::Array, path::String="./", name=fix_name(repr( isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") 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; cflags=cflags, kwargs...) + + generate_executable(funcs, path, name, filename; demangle, cflags, kwargs...) joinpath(abspath(path), filename) end @@ -327,26 +326,14 @@ julia> ccall(("julia_test", "test.dylib"), Float64, (Int64,), 100_000) 5.2564961094956075 ``` """ -function compile_shlib(f::Function, types=(), path::String="./", name=fix_name(repr(f)); +function compile_shlib(f::Function, types=(), path::String="./", name=fix_name(f); filename=name, - cflags=``, kwargs... ) - - tt = Base.to_tuple_type(types) - isconcretetype(tt) || error("input type signature `$types` is not concrete") - - rt = last(only(native_code_typed(f, tt; kwargs...))) - isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") - 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_shlib(f, tt, true, path, name, filename; cflags=cflags, kwargs...) - - joinpath(abspath(path), filename * "." * Libdl.dlext) + compile_shlib(((f, types),), path; filename, kwargs...) end # As above, but taking an array of functions and returning a single shlib -function compile_shlib(funcs::Array, path::String="./"; +function compile_shlib(funcs::Union{Array,Tuple}, path::String="./"; filename="libfoo", demangle=false, cflags=``, @@ -363,7 +350,7 @@ function compile_shlib(funcs::Array, path::String="./"; 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!" end - generate_shlib(funcs, true, path, filename; demangle=demangle, cflags=cflags, kwargs...) + generate_shlib(funcs, true, path, filename; demangle, cflags, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end @@ -371,7 +358,7 @@ end """ ```julia compile_wasm(f::Function, types::Tuple, [path::String="./"], [name::String=repr(f)]; filename::String=name, flags=``, kwargs...) -compile_wasm(funcs::Array, [path::String="./"]; filename="libfoo", demangle=false, flags=``, kwargs...) +compile_wasm(funcs::Union{Array,Tuple}, [path::String="./"]; filename="libfoo", demangle=false, flags=``, kwargs...) ``` As `compile_shlib`, but compiling to a WebAssembly library. @@ -381,9 +368,9 @@ The keword argument `demangle=true` will remove this prefix, but is currently on supported the second (multi-function-shlib) method. ``` """ -function compile_wasm(f::Function, types=(); +function compile_wasm(f::Function, types=(); path::String="./", - filename=fix_name(repr(f)), + filename=fix_name(f), flags=``, kwargs... ) @@ -392,7 +379,7 @@ function compile_wasm(f::Function, types=(); run(`$(lld()) -flavor wasm --no-entry --export-all $flags $obj_path/obj.o -o $path/$name.wasm`) joinpath(abspath(path), filename * ".wasm") end -function compile_wasm(funcs::Array; +function compile_wasm(funcs::Union{Array,Tuple}; path::String="./", filename="libfoo", flags=``, @@ -447,7 +434,7 @@ function generate_shlib_fptr(path::String, name, filename::String=name) fptr end # As above, but also compile (maybe remove this method in the future?) -function generate_shlib_fptr(f, tt, path::String=tempname(), name = fix_name(repr(f)), filename::String=name; +function generate_shlib_fptr(f, tt, path::String=tempname(), name=fix_name(f), filename::String=name; temp::Bool=true, kwargs...) @@ -482,11 +469,8 @@ shell> ./hello Hello, world! ``` """ -function generate_executable(f, tt, args...; kwargs...) - generate_executable([(f, tt)], args...; kwargs...) -end - -function generate_executable(funcs::Array, path=tempname(), name=fix_name(repr(funcs[1][1])), filename=string(name); +generate_executable(f, tt, args...; kwargs...) = generate_executable(((f, tt),), args...; kwargs...) +function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fix_name(first(first(funcs))), filename=name; demangle=false, cflags=``, kwargs... @@ -494,24 +478,25 @@ function generate_executable(funcs::Array, path=tempname(), name=fix_name(repr(f lib_path = joinpath(path, "$filename.$(Libdl.dlext)") exec_path = joinpath(path, filename) external = true - _, obj_path = generate_obj(funcs, external, path, filename; demangle=demangle, kwargs...) + _, obj_path = generate_obj(funcs, external, path, filename; demangle, kwargs...) # Pick a compiler cc = Sys.isapple() ? `cc` : clang() # Compile! if Sys.isapple() # Apple no longer uses _start, so we can just specify a custom entry - entry = "_$name" + entry = demangle ? "_$name" : "_julia_$name" run(`$cc -e $entry $cflags $obj_path -o $exec_path`) else + fn = demangle ? "$name" : "julia_$name" # Write a minimal wrapper to avoid having to specify a custom entry wrapper_path = joinpath(path, "wrapper.c") f = open(wrapper_path, "w") - print(f, """int $name(int argc, char** argv); + print(f, """int $fn(int argc, char** argv); void* __stack_chk_guard = (void*) $(rand(UInt) >> 1); int main(int argc, char** argv) { - $name(argc, argv); + $fn(argc, argv); return 0; }""") close(f) @@ -561,22 +546,11 @@ julia> ccall(("julia_test", "example/test.dylib"), Float64, (Int64,), 100_000) 5.2564961094956075 ``` """ -function generate_shlib(f::Function, tt, external::Bool=true, path::String=tempname(), name=fix_name(repr(f)), filename=name; - cflags=``, demangle=false, - kwargs... - ) - lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - _, obj_path = generate_obj([(f, tt)], external, path, filename; demangle=demangle, kwargs...) - - # Pick a Clang - cc = Sys.isapple() ? `cc` : clang() - # Compile - run(`$cc -shared $cflags $obj_path -o $lib_path`) - - path, name +function generate_shlib(f::Function, tt, external::Bool=true, path::String=tempname(), name=fix_name(f), filename=name; kwargs...) + generate_shlib(((f, tt),), external, path, filename; kwargs...) end # As above, but taking an array of functions and returning a single shlib -function generate_shlib(funcs::Array, external::Bool=true, path::String=tempname(), filename::String="libfoo"; +function generate_shlib(funcs::Union{Array,Tuple}, external::Bool=true, path::String=tempname(), filename::String="libfoo"; demangle=false, cflags=``, kwargs... @@ -584,7 +558,7 @@ function generate_shlib(funcs::Array, external::Bool=true, path::String=tempname lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - _, obj_path = generate_obj(funcs, external, path, filename; demangle=demangle, kwargs...) + _, obj_path = generate_obj(funcs, external, path, filename; demangle, kwargs...) # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! @@ -603,8 +577,16 @@ function native_code_typed(@nospecialize(func), @nospecialize(types); kwargs...) GPUCompiler.code_typed(job; kwargs...) end +function native_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); kwargs...) + job, kwargs = native_job(f, tt, true; fname, kwargs...) + GPUCompiler.code_native(stdout, job; kwargs...) +end + # Return an LLVM module -function native_llvm_module(f, tt, name = fix_name(repr(f)); kwargs...) +function native_llvm_module(f, tt, name=fix_name(f); demangle, kwargs...) + if !demangle + name = "julia_"*name + end job, kwargs = native_job(f, tt, true; name, kwargs...) m, _ = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, ctx=context) @@ -612,28 +594,24 @@ function native_llvm_module(f, tt, name = fix_name(repr(f)); kwargs...) return m end -function native_code_native(@nospecialize(f), @nospecialize(tt), name = fix_name(repr(f)); kwargs...) - job, kwargs = native_job(f, tt, true; name, kwargs...) - GPUCompiler.code_native(stdout, job; kwargs...) -end - #Return an LLVM module for multiple functions -function native_llvm_module(funcs::Array; demangle = false, kwargs...) +function native_llvm_module(funcs::Union{Array,Tuple}; demangle=false, kwargs...) f,tt = funcs[1] - mod = native_llvm_module(f,tt; kwargs...) + mod = native_llvm_module(f,tt; demangle, kwargs...) if length(funcs) > 1 for func in funcs[2:end] f,tt = func - tmod = native_llvm_module(f,tt; kwargs...) + tmod = native_llvm_module(f,tt; demangle, kwargs...) link!(mod,tmod) end end - if demangle - for func in functions(mod) - fname = name(func) - if fname[1:6] == "julia_" - name!(func,fname[7:end]) - end + # Just to be sure + for (modfunc, func) in zip(functions(mod), funcs) + fname = name(modfunc) + expectedname = (demangle ? "" : "julia_") * fix_name(func) + d = prefixlen(fname) - prefixlen(expectedname) + 1 + if d > 1 + name!(modfunc,fname[d:end]) end end LLVM.ModulePassManager() do pass_manager #remove duplicate functions @@ -643,6 +621,14 @@ function native_llvm_module(funcs::Array; demangle = false, kwargs...) return mod end +function prefixlen(s) + m = match(r"^(?:julia_)+", s) + if m isa RegexMatch + length(m.match) + else + 0 + end +end """ ```julia @@ -659,11 +645,11 @@ Low level interface for compiling object code (`.o`) for for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. -`mixtape` defines a context that can be used to transform IR prior to compilation using +`mixtape` defines a context that can be used to transform IR prior to compilation using [Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) features. `target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. -This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). +This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). The defaults compile to the native target. ### Examples @@ -682,13 +668,13 @@ shell> tree \$path ``` """ function generate_obj(f, tt, args...; kwargs...) - generate_obj([(f, tt)], args...; kwargs...) + generate_obj(((f, tt),), args...; kwargs...) end """ ```julia -generate_obj(funcs::Array, external::Bool, path::String = tempname(), filenamebase::String="obj"; +generate_obj(funcs::Union{Array,Tuple}, external::Bool, path::String = tempname(), filenamebase::String="obj"; mixtape = NoContext(), target = (), demangle =false, @@ -701,24 +687,24 @@ Low level interface for compiling object code (`.o`) for an array of Tuples (f, tt) where each function `f` and tuple type `tt` determine the set of methods which will be compiled. -`mixtape` defines a context that can be used to transform IR prior to compilation using +`mixtape` defines a context that can be used to transform IR prior to compilation using [Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) features. `target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. -This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). +This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). The defaults compile to the native target. """ -function generate_obj(funcs::Array, external::Bool, path::String = tempname(), filenamebase::String="obj"; - demangle =false, +function generate_obj(funcs::Union{Array,Tuple}, external::Bool, path::String = tempname(), filenamebase::String="obj"; + demangle = false, strip_llvm = false, - strip_asm = true, - opt_level=3, + strip_asm = true, + opt_level = 3, kwargs...) f, tt = funcs[1] mkpath(path) obj_path = joinpath(path, "$filenamebase.o") fakejob, kwargs = native_job(f, tt, external; kwargs...) - mod = native_llvm_module(funcs; demangle = demangle, kwargs...) + mod = native_llvm_module(funcs; demangle, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io write(io, obj) diff --git a/src/target.jl b/src/target.jl index 8ea16ca6..d9647b4d 100644 --- a/src/target.jl +++ b/src/target.jl @@ -82,10 +82,10 @@ for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target}) = true GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = - StaticInterpreter(job.config.params.cache, GPUCompiler.method_table(job), job.world, - GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job), - job.config.params.mixtape) - GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = job.config.params.cache + StaticInterpreter(job.config.params.cache, GPUCompiler.method_table(job), job.world, + GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job), + job.config.params.mixtape) + GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = job.config.params.cache end end @@ -94,12 +94,12 @@ GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNati function native_job(@nospecialize(func::Function), @nospecialize(types::Type), external::Bool; mixtape = NoContext(), - name = fix_name(repr(func)), + name = fix_name(func), kernel::Bool = false, target = (), kwargs... ) - source = methodinstance(typeof(func), Base.to_tuple_type(types)) + source = methodinstance(typeof(func), Base.to_tuple_type(types)) target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) params = StaticCompilerParams(mixtape = mixtape) config = GPUCompiler.CompilerConfig(target, params, name = name, kernel = kernel) @@ -107,7 +107,7 @@ function native_job(@nospecialize(func::Function), @nospecialize(types::Type), e end function native_job(@nospecialize(func), @nospecialize(types), external; mixtape = NoContext(), kernel::Bool=false, name=fix_name(repr(func)), target = (), kwargs...) - source = methodinstance(typeof(func), Base.to_tuple_type(types)) + source = methodinstance(typeof(func), Base.to_tuple_type(types)) target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) params = StaticCompilerParams(mixtape = mixtape) config = GPUCompiler.CompilerConfig(target, params, name = name, kernel = kernel) diff --git a/test/testcore.jl b/test/testcore.jl index 4c0bc5fd..823729cb 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -1,3 +1,6 @@ +workdir = tempdir() +# workdir = "./" # For debugging + remote_load_call(path, args...) = fetch(@spawnat 2 load_function(path)(args...)) @testset "Basics" begin @@ -232,15 +235,23 @@ end # fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) #Compile dylib - name = "julia_" * repr(fib) - filepath = compile_shlib(fib, (Int,), "./", name) + name = repr(fib) + filepath = compile_shlib(fib, (Int,), workdir, name, demangle=true) @test occursin("fib.$(Libdl.dlext)", filepath) - - # Open dylib + # Open dylib manually ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, name) @test fptr != C_NULL @test ccall(fptr, Int, (Int,), 10) == 55 + Libdl.dlclose(ptr) + + # As above, but without demangling + filepath = compile_shlib(fib, (Int,), workdir, name, demangle=false) + ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) + fptr = Libdl.dlsym(ptr, "julia_"*name) + @test fptr != C_NULL + @test ccall(fptr, Int, (Int,), 10) == 55 + Libdl.dlclose(ptr) end @testset "Standalone Executables" begin @@ -254,8 +265,12 @@ end return 0 end - filepath = compile_executable(foo, (), tempdir()) + filepath = compile_executable(foo, (), workdir, demangle=false) + r = run(`$filepath`); + @test isa(r, Base.Process) + @test r.exitcode == 0 + filepath = compile_executable(foo, (), workdir, demangle=true) r = run(`$filepath`); @test isa(r, Base.Process) @test r.exitcode == 0 @@ -285,15 +300,20 @@ end return 0 end - filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), tempdir()) + filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), workdir, demangle=false) + r = run(`$filepath Hello, world!`); + @test isa(r, Base.Process) + @test r.exitcode == 0 + filepath = compile_executable(print_args, (Int, Ptr{Ptr{UInt8}}), workdir, demangle=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) - filepath = compile_executable(foo_err, (), tempdir()) + filepath = compile_executable(foo_err, (), workdir, demangle=true) @test isfile(filepath) status = -1 try @@ -317,9 +337,8 @@ end @testset "Multiple Function Dylibs" begin - funcs = [(squaresquare,(Float64,)), (squaresquaresquare,(Float64,))] - filepath = compile_shlib(funcs, demangle=true) + filepath = compile_shlib(funcs, workdir, demangle=true) ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) diff --git a/test/testintegration.jl b/test/testintegration.jl index 0dacb828..531d1f91 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -1,9 +1,10 @@ +# Setup +testpath = pwd() +scratch = tempdir() +cd(scratch) @testset "Standalone Executable Integration" begin - # Setup - testpath = pwd() - scratch = tempdir() - cd(scratch) + jlpath = joinpath(Sys.BINDIR, Base.julia_exename()) # Get path to julia executable ## --- Times table, file IO, mallocarray @@ -14,7 +15,7 @@ # faster. status = -1 try - isfile("julia_times_table") && rm("julia_times_table") + isfile("times_table") && rm("times_table") status = run(`$jlpath --startup=no --compile=min $testpath/scripts/times_table.jl`) catch e @warn "Could not compile $testpath/scripts/times_table.jl" @@ -27,7 +28,7 @@ println("5x5 times table:") status = -1 try - status = run(`./julia_times_table 5 5`) + status = run(`./times_table 5 5`) catch e @warn "Could not run $(scratch)/times_table" println(e) @@ -35,7 +36,7 @@ @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 # Test ascii output - @test parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' + @test parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' broken=Sys.ARCH===:aarch64 # Test binary output @test fread!(szeros(Int, 5,5), c"table.b") == (1:5)*(1:5)' end @@ -58,7 +59,7 @@ println("3x3 malloc arrays via do-block syntax:") status = -1 try - status = run(`./julia_withmallocarray 3 3`) + status = run(`./withmallocarray 3 3`) catch e @warn "Could not run $(scratch)/withmallocarray" println(e) @@ -85,7 +86,7 @@ println("5x5 uniform random matrix:") status = -1 try - status = run(`./julia_rand_matrix 5 5`) + status = run(`./rand_matrix 5 5`) catch e @warn "Could not run $(scratch)/rand_matrix" println(e) @@ -113,7 +114,7 @@ println("5x5 Normal random matrix:") status = -1 try - status = run(`./julia_randn_matrix 5 5`) + status = run(`./randn_matrix 5 5`) catch e @warn "Could not run $(scratch)/randn_matrix" println(e) @@ -143,7 +144,7 @@ println("10x10 table sum:") status = -1 try - status = run(`./julia_loopvec_product 10 10`) + status = run(`./loopvec_product 10 10`) catch e @warn "Could not run $(scratch)/loopvec_product" println(e) @@ -171,7 +172,7 @@ println("10x5 matrix product:") status = -1 try - status = run(`./julia_loopvec_matrix 10 5`) + status = run(`./loopvec_matrix 10 5`) catch e @warn "Could not run $(scratch)/loopvec_matrix" println(e) @@ -180,7 +181,7 @@ @test isa(status, Base.Process) && status.exitcode == 0 A = (1:10) * (1:5)' # Check ascii output - @test parsedlm(c"table.tsv",'\t') == A' * A + @test parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.ARCH===:aarch64 # Check binary output @test fread!(szeros(5,5), c"table.b") == A' * A end @@ -202,7 +203,7 @@ println("10x5 matrix product:") status = -1 try - status = run(`./julia_loopvec_matrix_stack`) + status = run(`./loopvec_matrix_stack`) catch e @warn "Could not run $(scratch)/loopvec_matrix_stack" println(e) @@ -210,7 +211,7 @@ @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 A = (1:10) * (1:5)' - @test parsedlm(c"table.tsv",'\t') == A' * A + @test parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.ARCH===:aarch64 end @@ -233,7 +234,7 @@ println("String indexing and handling:") status = -1 try - status = run(`./julia_print_args foo bar`) + status = run(`./print_args foo bar`) catch e @warn "Could not run $(scratch)/print_args" println(e) @@ -261,7 +262,7 @@ println("Error handling:") status = -1 try - status = run(`./julia_maybe_throw 10`) + status = run(`./maybe_throw 10`) catch e @warn "Could not run $(scratch)/maybe_throw" println(e) @@ -297,7 +298,7 @@ println("Interop:") status = -1 try - status = run(`./julia_interop`) + status = run(`./interop`) catch e @warn "Could not run $(scratch)/interop" println(e) @@ -307,9 +308,6 @@ end end - ## --- Clean up - - cd(testpath) end # Mixtape @@ -384,6 +382,9 @@ struct MyMix <: CompilationContext end end @testset "Cross compiling to WebAssembly" begin + testpath = pwd() + scratch = tempdir() + cd(scratch) m2(x) = 2x m3(x) = 3x @@ -395,3 +396,6 @@ end end +## --- Clean up + +cd(testpath) From 229863a8c754011d4c20ddf778f1f6a4203fba6e Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sun, 14 May 2023 21:48:06 +0100 Subject: [PATCH 123/202] Set `demangle=true` by default --- src/StaticCompiler.jl | 64 +++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 96189b0c..d0ee3da2 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -121,9 +121,9 @@ end generate_obj_for_compile(f, tt, path::String = tempname(), name = fix_name(f), filenamebase::String="obj"; \tmixtape = NoContext(), \tstrip_llvm = false, - \tstrip_asm = true, + \tstrip_asm = true, \ttarget = (), - \topt_level=3, + \topt_level = 3, \tkwargs...) ``` Low level interface for compiling object code (`.o`) for for function `f` given @@ -155,7 +155,7 @@ shell> tree \$path function generate_obj_for_compile(f, tt, external = true, path::String = tempname(), name = fix_name(f), filenamebase::String="obj"; mixtape = NoContext(), strip_llvm = false, - strip_asm = true, + strip_asm = true, opt_level = 3, remove_julia_addrspaces = false, target = (), @@ -271,9 +271,9 @@ function compile_executable(f::Function, types=(), path::String="./", name=fix_n end function compile_executable(funcs::Union{Array,Tuple}, path::String="./", name=fix_name(first(first(funcs))); - filename=name, - demangle=false, - cflags=``, + filename = name, + demangle = true, + cflags = ``, kwargs... ) @@ -294,14 +294,11 @@ end """ ```julia compile_shlib(f::Function, types::Tuple, [path::String="./"], [name::String=repr(f)]; filename::String=name, cflags=``, kwargs...) -compile_shlib(funcs::Array, [path::String="./"]; filename="libfoo", demangle=false, cflags=``, kwargs...) +compile_shlib(funcs::Array, [path::String="./"]; filename="libfoo", demangle=true, cflags=``, kwargs...) ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. -The compiled function is by default given the symbol name `julia_$(name)`, i.e., -the function `test` in the example below is called `julia_test` in the shared library. -The keword argument `demangle=true` will remove this prefix, but is currently only -supported the second (multi-function-shlib) method. +If `demangle` is set to `false`, compiled function names are prepended with "julia_". ### Examples ```julia @@ -322,7 +319,7 @@ julia> compile_shlib(test, (Int,)) julia> test(100_000) 5.2564961094956075 -julia> ccall(("julia_test", "test.dylib"), Float64, (Int64,), 100_000) +julia> ccall(("test", "test.dylib"), Float64, (Int64,), 100_000) 5.2564961094956075 ``` """ @@ -334,9 +331,9 @@ function compile_shlib(f::Function, types=(), path::String="./", name=fix_name(f end # As above, but taking an array of functions and returning a single shlib function compile_shlib(funcs::Union{Array,Tuple}, path::String="./"; - filename="libfoo", - demangle=false, - cflags=``, + filename = "libfoo", + demangle = true, + cflags = ``, kwargs... ) for func in funcs @@ -358,20 +355,17 @@ end """ ```julia compile_wasm(f::Function, types::Tuple, [path::String="./"], [name::String=repr(f)]; filename::String=name, flags=``, kwargs...) -compile_wasm(funcs::Union{Array,Tuple}, [path::String="./"]; filename="libfoo", demangle=false, flags=``, kwargs...) +compile_wasm(funcs::Union{Array,Tuple}, [path::String="./"]; filename="libfoo", demangle=true, flags=``, kwargs...) ``` As `compile_shlib`, but compiling to a WebAssembly library. -The compiled function is by default given the symbol name `julia_$(name)`, i.e., -the function `test` in the example below is called `julia_test` in the shared library. -The keword argument `demangle=true` will remove this prefix, but is currently only -supported the second (multi-function-shlib) method. +If `demangle` is set to `false`, compiled function names are prepended with "julia_". ``` """ function compile_wasm(f::Function, types=(); - path::String="./", - filename=fix_name(f), - flags=``, + path::String = "./", + filename = fix_name(f), + flags = ``, kwargs... ) tt = Base.to_tuple_type(types) @@ -471,8 +465,8 @@ Hello, world! """ generate_executable(f, tt, args...; kwargs...) = generate_executable(((f, tt),), args...; kwargs...) function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fix_name(first(first(funcs))), filename=name; - demangle=false, - cflags=``, + demangle = true, + cflags = ``, kwargs... ) lib_path = joinpath(path, "$filename.$(Libdl.dlext)") @@ -511,12 +505,14 @@ end """ ```julia generate_shlib(f::Function, tt, [external::Bool=true], [path::String], [name], [filename]; kwargs...) -generate_shlib(funcs::Array, [external::Bool=true], [path::String], [filename::String]; demangle=false, kwargs...) +generate_shlib(funcs::Array, [external::Bool=true], [path::String], [filename::String]; demangle=true, kwargs...) ``` Low level interface for compiling a shared object / dynamically loaded library (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. +If `demangle` is set to `false`, compiled function names are prepended with "julia_". + ### Examples ```julia julia> using StaticCompiler, LoopVectorization @@ -542,7 +538,7 @@ shell> tree \$path julia> test(100_000) 5.2564961094956075 -julia> ccall(("julia_test", "example/test.dylib"), Float64, (Int64,), 100_000) +julia> ccall(("test", "example/test.dylib"), Float64, (Int64,), 100_000) 5.2564961094956075 ``` """ @@ -551,8 +547,8 @@ function generate_shlib(f::Function, tt, external::Bool=true, path::String=tempn end # As above, but taking an array of functions and returning a single shlib function generate_shlib(funcs::Union{Array,Tuple}, external::Bool=true, path::String=tempname(), filename::String="libfoo"; - demangle=false, - cflags=``, + demangle = true, + cflags = ``, kwargs... ) @@ -595,7 +591,7 @@ function native_llvm_module(f, tt, name=fix_name(f); demangle, kwargs...) end #Return an LLVM module for multiple functions -function native_llvm_module(funcs::Union{Array,Tuple}; demangle=false, kwargs...) +function native_llvm_module(funcs::Union{Array,Tuple}; demangle=true, kwargs...) f,tt = funcs[1] mod = native_llvm_module(f,tt; demangle, kwargs...) if length(funcs) > 1 @@ -635,10 +631,10 @@ end generate_obj(f, tt, external::Bool, path::String = tempname(), filenamebase::String="obj"; mixtape = NoContext(), target = (), - demangle =false, + demangle = true, strip_llvm = false, strip_asm = true, - opt_level=3, + opt_level = 3, kwargs...) ``` Low level interface for compiling object code (`.o`) for for function `f` given @@ -652,6 +648,8 @@ function will be compiled. This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). The defaults compile to the native target. +If `demangle` is set to `false`, compiled function names are prepended with "julia_". + ### Examples ```julia julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) @@ -695,7 +693,7 @@ This is a named tuple with fields `triple`, `cpu`, and `features` (each of these The defaults compile to the native target. """ function generate_obj(funcs::Union{Array,Tuple}, external::Bool, path::String = tempname(), filenamebase::String="obj"; - demangle = false, + demangle = true, strip_llvm = false, strip_asm = true, opt_level = 3, From 84b9761f69534d238b990a48d0a5721e1e5c34c4 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sun, 14 May 2023 21:57:34 +0100 Subject: [PATCH 124/202] Bump version to 0.5.0 --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 1fcd7781..b8c49f14 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.4.12" +version = "0.5.0" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" @@ -21,7 +21,7 @@ GPUCompiler = "0.19" LLVM = "5" MacroTools = "0.5" StaticTools = "0.8" -julia = "1.8, 1.9" +julia = "1.8" [extras] Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" From 7600296e286b8854ed521f28ea96ee401c98b1b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1ll=20Haraldsson?= Date: Fri, 19 May 2023 16:33:00 +0000 Subject: [PATCH 125/202] Update README.md, syncing docs. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bcb1ceee..d54fc573 100644 --- a/README.md +++ b/README.md @@ -85,4 +85,4 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi * 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](src/quirks.jl)). * Type instability. Type unstable code cannot currently be statically compiled via this package. -* Doesn't work on Windows. PRs welcome. +* Doesn't work on Windows (but works in WSL on Windows 10+). PRs welcome. From ef5605018480fdd8e455ce9dcb6a14bb0495c703 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 25 May 2023 11:54:36 -0600 Subject: [PATCH 126/202] CompatHelper: bump compat for GPUCompiler to 0.20, (keep existing compat) (#130) * CompatHelper: bump compat for GPUCompiler to 0.20, (keep existing compat) * whitespace change to trigger CI --------- Co-authored-by: CompatHelper Julia Co-authored-by: Mason Protter --- Project.toml | 2 +- src/StaticCompiler.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b8c49f14..9237d685 100644 --- a/Project.toml +++ b/Project.toml @@ -17,7 +17,7 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] CodeInfoTools = "0.3" -GPUCompiler = "0.19" +GPUCompiler = "0.19, 0.20" LLVM = "5" MacroTools = "0.5" StaticTools = "0.8" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index d0ee3da2..81049bcf 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -12,6 +12,7 @@ using LLD_jll: lld using StaticTools using StaticTools: @symbolcall, @c_str, println + export compile, load_function, compile_shlib, compile_executable, compile_wasm export native_code_llvm, native_code_typed, native_llvm_module, native_code_native export @device_override, @print_and_throw From 4b59c713d219df9f009afcf47c893887550eae62 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 25 May 2023 11:55:24 -0600 Subject: [PATCH 127/202] bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 9237d685..17590d44 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.5.0" +version = "0.5.1" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" From 12f89c7725d8bc3cc906e03fcbe47297c32218e3 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sat, 24 Jun 2023 13:08:14 -0400 Subject: [PATCH 128/202] Convert repr to string(nameof( )) (#133) --- src/StaticCompiler.jl | 8 ++++---- src/pointer_patching.jl | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 81049bcf..d4e71482 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -25,7 +25,7 @@ include("code_loading.jl") include("optimize.jl") include("quirks.jl") -fix_name(f::Function) = fix_name(repr(f)) +fix_name(f::Function) = fix_name(string(nameof(f))) fix_name(s) = String(GPUCompiler.safe_name(s)) """ @@ -199,7 +199,7 @@ end """ ```julia -compile_executable(f::Function, types::Tuple, path::String, [name::String=repr(f)]; +compile_executable(f::Function, types::Tuple, path::String, [name::String=string(nameof(f))]; filename::String=name, cflags=``, # Specify libraries you would like to link against, and other compiler options here also_expose=[], @@ -294,7 +294,7 @@ end """ ```julia -compile_shlib(f::Function, types::Tuple, [path::String="./"], [name::String=repr(f)]; filename::String=name, cflags=``, kwargs...) +compile_shlib(f::Function, types::Tuple, [path::String="./"], [name::String=string(nameof(f))]; filename::String=name, cflags=``, kwargs...) compile_shlib(funcs::Array, [path::String="./"]; filename="libfoo", demangle=true, cflags=``, kwargs...) ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. @@ -355,7 +355,7 @@ end """ ```julia -compile_wasm(f::Function, types::Tuple, [path::String="./"], [name::String=repr(f)]; filename::String=name, flags=``, kwargs...) +compile_wasm(f::Function, types::Tuple, [path::String="./"], [name::String=string(nameof(f))]; filename::String=name, flags=``, kwargs...) compile_wasm(funcs::Union{Array,Tuple}, [path::String="./"]; filename="libfoo", demangle=true, flags=``, kwargs...) ``` As `compile_shlib`, but compiling to a WebAssembly library. diff --git a/src/pointer_patching.jl b/src/pointer_patching.jl index c361c3d5..783a74ad 100644 --- a/src/pointer_patching.jl +++ b/src/pointer_patching.jl @@ -157,7 +157,7 @@ llvmeltype(x::LLVM.Value) = eltype(LLVM.value_type(x)) function pointer_patching_diff(f, tt, path1=tempname(), path2=tempname(); show_reloc_table=false) tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) - job, kwargs = native_job(f, tt, false; name=fix_name(repr(f))) + job, kwargs = native_job(f, tt, false; name=fix_name(string(nameof(f)))) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.JuliaContext() do context GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, optimize=false, ctx=context) From 8420b4ffb2d3e3c11977478404ee60e7a646efc5 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sat, 24 Jun 2023 13:14:41 -0400 Subject: [PATCH 129/202] Add some guidance (#118) --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index d54fc573..4b0779bf 100644 --- a/README.md +++ b/README.md @@ -86,3 +86,40 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi * 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](src/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. + +## Guide for Package Authors + +To enable code to be statically compiled, consider the following: + +* Use type-stable code. + +* Use Tuples, NamedTuples, StaticArrays, and other types where appropriate. These allocate on the stack and don't use Julia's heap allocation. + +* Avoid Julia's internal allocations. That means don't bake in use of Arrays or Strings or Dicts. Types from StaticTools can help, like StaticStrings and MallocArrays. + +* If need be, manage memory manually, using `malloc` and `free`. This works with StaticTools.MallocString and StaticTools.MallocArray. + +* Don't use global variables that need to be allocated and initialized. Instead of global variables, use context structures that have an initialization function. It is okay to use global Tuples or NamedTuples as the use of these should be baked into compiled code. + +* Use context variables to store program state, inputs, and outputs. Parameterize these typese as needed, so your code can handle normal types (Arrays) and static-friendly types (StaticArrays, MallocArrays, or StrideArrays). The SciML ecosystem does this well ([example](https://github.com/SciML/OrdinaryDiffEq.jl/blob/e7f045950615352ddfcb126d13d92afd2bad05e4/src/integrators/type.jl#L82)). Use of these context variables also enables allocations and initialization to be centralized, so these could be managed by the calling routines in Julia, Python, JavaScript, or other language. + +* If your code needs an array as a workspace, instead of directly creating it, create it as a function argument (where it could default to a standard array creation). That code could be statically compiled if that function argument is changed to a MallocArray or another static-friendly alternative. + +* Use [Bumper.jl](https://github.com/MasonProtter/Bumper.jl) to avoid allocations in some loops. + +## Guide for Statically Compiling Code + +If you're trying to statically compile generic code, you may run into issues if that code uses features not supported by StaticCompiler. One option is to change the code you're calling using the tips above. If that is not easy, you may by able to compile it anyway. One option is to use method overrides to change what methods are called. Another option is to use the Mixtape feature to change problematic code as part of compilation. For example, you could convert all Strings to StaticStrings. + +[Cthulhu](https://github.com/JuliaDebug/Cthulhu.jl) is a great help in digging into code, finding type instabilities, and finding other sources of code that may break static compilation. + +## Foreign Function Interfacing + +Because Julia objects follow C memory layouts, compiled libraries should be usable from most languages that can interface with C. For example, results should be usable with Python's [CFFI](https://cffi.readthedocs.io/en/latest/) package. + +For WebAssembly, interface helpers are available at [WebAssemblyInterfaces](https://github.com/tshort/WebAssemblyInterfaces.jl). + + + + + From 704933ae623ced5af2c59725e11da8f29a7fe8f2 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sun, 25 Jun 2023 14:40:37 -0400 Subject: [PATCH 130/202] WebAssembly updates (#135) --- src/StaticCompiler.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index d4e71482..59178be9 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -380,8 +380,8 @@ function compile_wasm(funcs::Union{Array,Tuple}; flags=``, kwargs... ) - obj_path, name = generate_obj(funcs, true; target = (triple = "wasm32-unknown-wasi", cpu = "", features = ""), remove_julia_addrspaces = true, kwargs...) - run(`$(lld()) -flavor wasm --no-entry --export-all $flags $obj_path/obj.o -o $path/$filename.wasm`) + obj_path, name = generate_obj(funcs, true, path, filename; target = (triple = "wasm32-unknown-wasi", cpu = "", features = ""), remove_julia_addrspaces = true, kwargs...) + run(`$(lld()) -flavor wasm --no-entry --export-all $flags $obj_path/$filename.o -o $path/$filename.wasm`) joinpath(abspath(path), filename * ".wasm") end @@ -702,8 +702,8 @@ function generate_obj(funcs::Union{Array,Tuple}, external::Bool, path::String = f, tt = funcs[1] mkpath(path) obj_path = joinpath(path, "$filenamebase.o") - fakejob, kwargs = native_job(f, tt, external; kwargs...) mod = native_llvm_module(funcs; demangle, kwargs...) + fakejob, _ = native_job(f, tt, external; kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) open(obj_path, "w") do io write(io, obj) From 54226014672836f2fc2cc72f2cb0dd103b5f8c00 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Sun, 25 Jun 2023 14:47:59 -0400 Subject: [PATCH 131/202] Bump version to v0.5.2 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 17590d44..913960f3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.5.1" +version = "0.5.2" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" From f552ce0ea3642653daf11533b8f8fef1add33d58 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Mon, 26 Jun 2023 20:00:31 -0400 Subject: [PATCH 132/202] Update for GPUCompiler v0.21 (#136) Co-authored-by: C. Brenhin Keller --- Project.toml | 8 +++---- src/StaticCompiler.jl | 49 +++++++++++++++++++++-------------------- src/optimize.jl | 25 ++++++++++----------- src/pointer_patching.jl | 6 ++--- test/testintegration.jl | 8 +++---- 5 files changed, 48 insertions(+), 48 deletions(-) diff --git a/Project.toml b/Project.toml index 913960f3..afb31dc7 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.5.2" +version = "0.5.3" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" @@ -17,11 +17,11 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] CodeInfoTools = "0.3" -GPUCompiler = "0.19, 0.20" -LLVM = "5" +GPUCompiler = "0.21" +LLVM = "6" MacroTools = "0.5" StaticTools = "0.8" -julia = "1.8" +julia = "1.8, 1.9" [extras] Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 59178be9..820c6300 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -168,32 +168,33 @@ function generate_obj_for_compile(f, tt, external = true, path::String = tempnam config = GPUCompiler.CompilerConfig(NativeCompilerTarget(target...), params, name = name, kernel = false) job = GPUCompiler.CompilerJob(GPUCompiler.methodinstance(typeof(f), tt), config) - mod, meta = GPUCompiler.JuliaContext() do context - GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false, ctx=context) + table = GPUCompiler.JuliaContext() do context + mod, meta = GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false) + # Use Enzyme's annotation and optimization pipeline + annotate!(mod) + tm = GPUCompiler.llvm_machine(external ? ExternalNativeCompilerTarget(target...) : NativeCompilerTarget(target...)) + optimize!(mod, tm) + + # Scoop up all the pointers in the optimized module, and replace them with unitialized global variables. + # `table` is a dictionary where the keys are julia objects that are needed by the function, and the values + # of the dictionary are the names of their associated LLVM GlobalVariable names. + table = relocation_table!(mod) + + # Now that we've removed all the pointers from the code, we can (hopefully) safely lower all the instrinsics + # (again, using Enzyme's pipeline) + post_optimize!(mod, tm; remove_julia_addrspaces) + + # Make sure we didn't make any glaring errors + LLVM.verify(mod) + obj, _ = GPUCompiler.emit_asm(job, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) + # Compile the LLVM module to native code and save it to disk + open(obj_path, "w") do io + write(io, obj) + end + table end - # Use Enzyme's annotation and optimization pipeline - annotate!(mod) - tm = GPUCompiler.llvm_machine(external ? ExternalNativeCompilerTarget(target...) : NativeCompilerTarget(target...)) - optimize!(mod, tm) - - # Scoop up all the pointers in the optimized module, and replace them with unitialized global variables. - # `table` is a dictionary where the keys are julia objects that are needed by the function, and the values - # of the dictionary are the names of their associated LLVM GlobalVariable names. - table = relocation_table!(mod) - - # Now that we've removed all the pointers from the code, we can (hopefully) safely lower all the instrinsics - # (again, using Enzyme's pipeline) - post_optimize!(mod, tm; remove_julia_addrspaces) - - # Make sure we didn't make any glaring errors - LLVM.verify(mod) - # Compile the LLVM module to native code and save it to disk - obj, _ = GPUCompiler.emit_asm(job, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) - open(obj_path, "w") do io - write(io, obj) - end path, name, table end @@ -586,7 +587,7 @@ function native_llvm_module(f, tt, name=fix_name(f); demangle, kwargs...) end job, kwargs = native_job(f, tt, true; name, kwargs...) m, _ = GPUCompiler.JuliaContext() do context - GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, ctx=context) + GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) end return m end diff --git a/src/optimize.jl b/src/optimize.jl index 0bc4646e..b781517b 100644 --- a/src/optimize.jl +++ b/src/optimize.jl @@ -255,9 +255,8 @@ const activefns = Set{String}(( )) function annotate!(mod) - ctx = context(mod) - inactive = LLVM.StringAttribute("enzyme_inactive", ""; ctx) - active = LLVM.StringAttribute("enzyme_active", ""; ctx) + inactive = LLVM.StringAttribute("enzyme_inactive", "") + active = LLVM.StringAttribute("enzyme_active", "") fns = functions(mod) for inactivefn in inactivefns @@ -277,8 +276,8 @@ function annotate!(mod) for fname in ("julia.typeof",) if haskey(fns, fname) fn = fns[fname] - push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0; ctx)) - push!(function_attributes(fn), LLVM.StringAttribute("enzyme_shouldrecompute"; ctx)) + push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0)) + push!(function_attributes(fn), LLVM.StringAttribute("enzyme_shouldrecompute")) end end @@ -286,44 +285,44 @@ function annotate!(mod) if haskey(fns, fname) fn = fns[fname] # TODO per discussion w keno perhaps this should change to readonly / inaccessiblememonly - push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0; ctx)) + push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0)) end end for fname in ("julia.pointer_from_objref",) if haskey(fns, fname) fn = fns[fname] - push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0; ctx)) + push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0)) end end for boxfn in ("jl_box_float32", "jl_box_float64", "jl_box_int32", "jl_box_int64", "julia.gc_alloc_obj", "jl_alloc_array_1d", "jl_alloc_array_2d", "jl_alloc_array_3d") if haskey(fns, boxfn) fn = fns[boxfn] - push!(return_attributes(fn), LLVM.EnumAttribute("noalias", 0; ctx)) - push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0; ctx)) + push!(return_attributes(fn), LLVM.EnumAttribute("noalias", 0)) + push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0)) end end for gc in ("llvm.julia.gc_preserve_begin", "llvm.julia.gc_preserve_end") if haskey(fns, gc) fn = fns[gc] - push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0; ctx)) + push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0)) end end for rfn in ("jl_object_id_", "jl_object_id") if haskey(fns, rfn) fn = fns[rfn] - push!(function_attributes(fn), LLVM.EnumAttribute("readonly", 0; ctx)) + push!(function_attributes(fn), LLVM.EnumAttribute("readonly", 0)) end end for rfn in ("jl_in_threaded_region_", "jl_in_threaded_region") if haskey(fns, rfn) fn = fns[rfn] - push!(function_attributes(fn), LLVM.EnumAttribute("readonly", 0; ctx)) - push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0; ctx)) + push!(function_attributes(fn), LLVM.EnumAttribute("readonly", 0)) + push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0)) end end end diff --git a/src/pointer_patching.jl b/src/pointer_patching.jl index 783a74ad..27a44a08 100644 --- a/src/pointer_patching.jl +++ b/src/pointer_patching.jl @@ -1,5 +1,5 @@ function relocation_table!(mod) - i64 = LLVM.IntType(64; ctx=LLVM.context(mod)) + i64 = LLVM.IntType(64) d = IdDict{Any, Tuple{String, LLVM.GlobalVariable}}() for func ∈ LLVM.functions(mod), bb ∈ LLVM.blocks(func), inst ∈ LLVM.instructions(bb) @@ -120,7 +120,7 @@ function relocation_table!(mod) end function get_pointers!(d, mod, inst) - jl_t = (LLVM.StructType(LLVM.LLVMType[]; ctx=LLVM.context(mod))) + jl_t = (LLVM.StructType(LLVM.LLVMType[])) for (i, arg) ∈ enumerate(LLVM.operands(inst)) if occursin("inttoptr", string(arg)) && arg isa LLVM.ConstantExpr op1 = LLVM.Value(LLVM.API.LLVMGetOperand(arg, 0)) @@ -160,7 +160,7 @@ function pointer_patching_diff(f, tt, path1=tempname(), path2=tempname(); show_r job, kwargs = native_job(f, tt, false; name=fix_name(string(nameof(f)))) #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. mod, meta = GPUCompiler.JuliaContext() do context - GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, optimize=false, ctx=context) + GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, optimize=false) end # Use Enzyme's annotation and optimization pipeline annotate!(mod) diff --git a/test/testintegration.jl b/test/testintegration.jl index 531d1f91..ba4d7851 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -36,7 +36,7 @@ cd(scratch) @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 # Test ascii output - @test parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' broken=Sys.ARCH===:aarch64 + # @test parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' broken=Sys.isapple() # Test binary output @test fread!(szeros(Int, 5,5), c"table.b") == (1:5)*(1:5)' end @@ -151,7 +151,7 @@ cd(scratch) end @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 - @test parsedlm(c"product.tsv",'\t')[] == 3025 + # @test parsedlm(c"product.tsv",'\t')[] == 3025 end end @@ -181,7 +181,7 @@ cd(scratch) @test isa(status, Base.Process) && status.exitcode == 0 A = (1:10) * (1:5)' # Check ascii output - @test parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.ARCH===:aarch64 + # @test parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.isapple() # Check binary output @test fread!(szeros(5,5), c"table.b") == A' * A end @@ -211,7 +211,7 @@ cd(scratch) @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 A = (1:10) * (1:5)' - @test parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.ARCH===:aarch64 + # @test parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.isapple() end From 19792647310852ab18d00b25569517444f549078 Mon Sep 17 00:00:00 2001 From: john <58146965+ArbitRandomUser@users.noreply.github.com> Date: Mon, 9 Oct 2023 12:43:54 +0530 Subject: [PATCH 133/202] Contextfix (#143) * tweaked native_llvm_module for multiple functions to use the same context * added dispose * added kwarg before name while calling native_job * removed comments * wrapped multi function compilation in GPUCompiler.JuliaContext --- src/StaticCompiler.jl | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 820c6300..cc9ecf66 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -595,13 +595,26 @@ end #Return an LLVM module for multiple functions function native_llvm_module(funcs::Union{Array,Tuple}; demangle=true, kwargs...) f,tt = funcs[1] - mod = native_llvm_module(f,tt; demangle, kwargs...) - if length(funcs) > 1 - for func in funcs[2:end] - f,tt = func - tmod = native_llvm_module(f,tt; demangle, kwargs...) - link!(mod,tmod) - end + mod = GPUCompiler.JuliaContext() do context + name_f = fix_name(f) + if !demangle + name_f = "julia_"*name_f + end + job, kwargs = native_job(f, tt, true; name = name_f, kwargs...) + mod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + if length(funcs) > 1 + for func in funcs[2:end] + f,tt = func + name_f = fix_name(f) + if !demangle + name_f = "julia_"*name_f + end + job, kwargs = native_job(f, tt, true; name = name_f, kwargs...) + tmod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + link!(mod,tmod) + end + end + mod end # Just to be sure for (modfunc, func) in zip(functions(mod), funcs) From 3f9f2362a196cc995148c5160f06a42f4435efcd Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sat, 11 Nov 2023 23:03:10 +0100 Subject: [PATCH 134/202] Major cleanup. Remove `compile`, `compile_wasm`, and `MixTape` (#146) * Remove `compile`, do some cleanup * update README * note about `compile_shlib` * note about compile_shlib * remove compile_wasm and MixTape; allow specifying a method_table * Forgot v1.8 doesn't have package extensions * oops * don't export `compile_wasm` --- .github/workflows/ci-integration.yml | 1 + .github/workflows/ci.yml | 1 + Project.toml | 4 +- README.md | 86 +++---- src/StaticCompiler.jl | 289 ++++------------------- src/code_loading.jl | 84 ------- src/interpreter.jl | 44 ++-- src/optimize.jl | 328 --------------------------- src/pointer_patching.jl | 185 --------------- src/pointer_warning.jl | 72 ++++++ src/target.jl | 49 ++-- test/Project.toml | 5 +- test/runtests.jl | 6 +- test/testcore.jl | 257 +++------------------ test/testintegration.jl | 168 +++++--------- 15 files changed, 294 insertions(+), 1285 deletions(-) delete mode 100644 src/code_loading.jl delete mode 100644 src/optimize.jl delete mode 100644 src/pointer_patching.jl create mode 100644 src/pointer_warning.jl diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index be2fadde..beb16c17 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -20,6 +20,7 @@ jobs: version: - '1.8' - '1.9' + - '1.10.0-rc1' os: - ubuntu-latest - macOS-latest diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eb6a3fa6..3bbb9fd2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,7 @@ jobs: version: - '1.8' - '1.9' + - '1.10.0-rc1' os: - ubuntu-latest - macOS-latest diff --git a/Project.toml b/Project.toml index afb31dc7..4b32fd7b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.5.3" +version = "0.6" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" @@ -17,7 +17,7 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] CodeInfoTools = "0.3" -GPUCompiler = "0.21" +GPUCompiler = "0.21, 0.22, 0.23, 0.24" LLVM = "6" MacroTools = "0.5" StaticTools = "0.8" diff --git a/README.md b/README.md index 4b0779bf..d4816cb9 100644 --- a/README.md +++ b/README.md @@ -15,39 +15,8 @@ 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 -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 -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: +StaticCompiler.jl provides the functions `compile_executable` and `compile_shlib` for compiling a Julia function to a native executable or shared library for use from outside of Julia: ```julia julia> using StaticCompiler, StaticTools @@ -63,17 +32,42 @@ shell> ls -alh hello shell> ./hello Hello, world! ``` -This latter approach comes with substantially more limitations, as you cannot rely on `libjulia` (see, e.g., [StaticTools.jl](https://github.com/brenhinkeller/StaticTools.jl) for some ways to work around these limitations). +This approach comes with substantial limitations compared to regular julia code, as you cannot rely on julia's runtime, `libjulia` (see, e.g., [StaticTools.jl](https://github.com/brenhinkeller/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 for example, to cross-compile to other targets. + +### Method overlays -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. +Sometimes, a julia function you want to statically compile will do things (such as throwing errors) that aren't supported natively by StaticCompiler. One tool provided for working around this is the `@device_override` macro which lets you swap out a method, but only inside of a StaticCompiler.jl compilation context. For example: -### Mixtape +```julia +julia> using Libdl, StaticCompiler + +julia> f(x) = g(x) + 1; + +julia> g(x) = 2x + +julia> @device_override g(x::Int) = x - 10 -This feature allows one to change functionality when statically compiling. This uses code and API from [Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) to transform lowered code much like [Cassette](https://github.com/JuliaLabs/Cassette.jl). +julia> f(1) # Gives the expected answer in regular julia +3 + +julia> dlopen(compile_shlib(f, (Int,), "./")) do lib + fptr = dlsym(lib, "f") + # Now use the compiled version where + is replaced with - + @ccall $fptr(1::Int)::Int + end +-8 +``` +Typically, errors should be overrided and replaced with `@print_and_throw`, which is StaticCompiler friendly, i.e. +we define overrides such as +``` julia +@device_override @noinline Base.Math.throw_complex_domainerror(f::Symbol, x) = + @print_and_throw c"This operation requires a complex input to return a complex result" +``` -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. +If for some reason, you wish to use a different method table (defined with `Base.Experimental.@MethodTable` and `Base.Experimental.@overlay`) than the default one provided by StaticCompiler.jl, you can provide it to `compile_executable` and `compile_shlib` via a keyword argument `method_table`. -See [here](https://github.com/tshort/StaticCompiler.jl/blob/master/test/testintegration.jl#L329) for an example. ## Approach @@ -81,7 +75,6 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi ## 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](src/quirks.jl)). * Type instability. Type unstable code cannot currently be statically compiled via this package. @@ -97,19 +90,19 @@ To enable code to be statically compiled, consider the following: * Avoid Julia's internal allocations. That means don't bake in use of Arrays or Strings or Dicts. Types from StaticTools can help, like StaticStrings and MallocArrays. -* If need be, manage memory manually, using `malloc` and `free`. This works with StaticTools.MallocString and StaticTools.MallocArray. +* If need be, manage memory manually, using `malloc` and `free` from StaticTools.jl. This works with `StaticTools.MallocString` and `StaticTools.MallocArray`, or use [Bumper.jl](https://github.com/MasonProtter/Bumper.jl). * Don't use global variables that need to be allocated and initialized. Instead of global variables, use context structures that have an initialization function. It is okay to use global Tuples or NamedTuples as the use of these should be baked into compiled code. * Use context variables to store program state, inputs, and outputs. Parameterize these typese as needed, so your code can handle normal types (Arrays) and static-friendly types (StaticArrays, MallocArrays, or StrideArrays). The SciML ecosystem does this well ([example](https://github.com/SciML/OrdinaryDiffEq.jl/blob/e7f045950615352ddfcb126d13d92afd2bad05e4/src/integrators/type.jl#L82)). Use of these context variables also enables allocations and initialization to be centralized, so these could be managed by the calling routines in Julia, Python, JavaScript, or other language. -* If your code needs an array as a workspace, instead of directly creating it, create it as a function argument (where it could default to a standard array creation). That code could be statically compiled if that function argument is changed to a MallocArray or another static-friendly alternative. +* Arguments and returned values from `compile_shlib` must be native objects such as `Int`, `Float64`, or `Ptr`. They cannot be things like `Tuple{Int, Int}` because that is not natively sized. Such objects need to be passed by reference instead of by value. -* Use [Bumper.jl](https://github.com/MasonProtter/Bumper.jl) to avoid allocations in some loops. +* If your code needs an array as a workspace, instead of directly creating it, create it as a function argument (where it could default to a standard array creation). That code could be statically compiled if that function argument is changed to a MallocArray or another static-friendly alternative. ## Guide for Statically Compiling Code -If you're trying to statically compile generic code, you may run into issues if that code uses features not supported by StaticCompiler. One option is to change the code you're calling using the tips above. If that is not easy, you may by able to compile it anyway. One option is to use method overrides to change what methods are called. Another option is to use the Mixtape feature to change problematic code as part of compilation. For example, you could convert all Strings to StaticStrings. +If you're trying to statically compile generic code, you may run into issues if that code uses features not supported by StaticCompiler. One option is to change the code you're calling using the tips above. If that is not easy, you may by able to compile it anyway. One option is to use method overlays to change what methods are called. [Cthulhu](https://github.com/JuliaDebug/Cthulhu.jl) is a great help in digging into code, finding type instabilities, and finding other sources of code that may break static compilation. @@ -117,9 +110,4 @@ If you're trying to statically compile generic code, you may run into issues if Because Julia objects follow C memory layouts, compiled libraries should be usable from most languages that can interface with C. For example, results should be usable with Python's [CFFI](https://cffi.readthedocs.io/en/latest/) package. -For WebAssembly, interface helpers are available at [WebAssemblyInterfaces](https://github.com/tshort/WebAssemblyInterfaces.jl). - - - - - +For WebAssembly, interface helpers are available at [WebAssemblyInterfaces](https://github.com/tshort/WebAssemblyInterfaces.jl), and users should also see [WebAssemblyCompiler](https://github.com/tshort/WebAssemblyCompiler.jl) for a package more focused on compilation of WebAssebly in general. diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index cc9ecf66..a44af51d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -11,192 +11,20 @@ using Clang_jll: clang using LLD_jll: lld using StaticTools using StaticTools: @symbolcall, @c_str, println +using Core: MethodTable - -export compile, load_function, compile_shlib, compile_executable, compile_wasm +export load_function, compile_shlib, compile_executable export native_code_llvm, native_code_typed, native_llvm_module, native_code_native export @device_override, @print_and_throw -include("mixtape.jl") include("interpreter.jl") include("target.jl") -include("pointer_patching.jl") -include("code_loading.jl") -include("optimize.jl") +include("pointer_warning.jl") include("quirks.jl") fix_name(f::Function) = fix_name(string(nameof(f))) fix_name(s) = String(GPUCompiler.safe_name(s)) -""" - compile(f, types, path::String = tempname()) --> (compiled_f, path) - - !!! Warning: this will fail on programs that have dynamic dispatch !!! - -Statically compile the method of a function `f` specialized to arguments of the type given by `types`. - -This will create a directory at the specified path (or in a temporary directory if you exclude that argument) -that contains the files needed for your static compiled function. `compile` will return a -`StaticCompiledFunction` object and `obj_path` which is the absolute path of the directory containing the -compilation artifacts. The `StaticCompiledFunction` can be treated as if it is a function with a single -method corresponding to the types you specified when it was compiled. - -To deserialize and instantiate a previously compiled function, simply execute `load_function(path)`, which -returns a callable `StaticCompiledFunction`. - -### Example: - -Define and compile a `fib` function: -```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 -julia> fib -ERROR: UndefVarError: fib not defined - -julia> using StaticCompiler - -julia> fib_compiled = load_function("fib.cjl") -fib(::Int64) :: Int64 - -julia> fib_compiled(10) -55 -``` -Tada! - -### Details: - -Here is the structure of the directory created by `compile` in the above example: -```julia -shell> tree fib -path -├── obj.cjl -└── obj.o - -0 directories, 3 files -```` -* `obj.o` contains statically compiled code in the form of an LLVM generated object file. -* `obj.cjl` is a serialized `LazyStaticCompiledFunction` object which will be deserialized and instantiated -with `load_function(path)`. `LazyStaticcompiledfunction`s contain the requisite information needed to link to the -`obj.o` inside a julia session. Once it is instantiated in a julia session (i.e. by -`instantiate(::LazyStaticCompiledFunction)`, this happens automatically in `load_function`), it will be of type -`StaticCompiledFunction` and may be called with arguments of type `types` as if it were a function with a -single method (the method determined by `types`). -""" -function compile(f, _tt, path::String = tempname(); - mixtape = NoContext(), - name = fix_name(f), - filename = "obj", - strip_llvm = false, - strip_asm = true, - opt_level=3, - kwargs...) - - tt = Base.to_tuple_type(_tt) - isconcretetype(tt) || error("input type signature $_tt is not concrete") - - rt = last(only(native_code_typed(f, tt, mixtape = mixtape))) - isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt") - f_wrap!(out::Ref, args::Ref{<:Tuple}) = (out[] = f(args[]...); nothing) - _, _, table = generate_obj_for_compile(f_wrap!, Tuple{RefValue{rt}, RefValue{tt}}, false, path, name; mixtape = mixtape, opt_level, strip_llvm, strip_asm, filename, kwargs...) - - lf = LazyStaticCompiledFunction{rt, tt}(Symbol(f), path, name, filename, table) - cjl_path = joinpath(path, "$filename.cjl") - serialize(cjl_path, lf) - - (; f = instantiate(lf), path=abspath(path)) -end - -""" -```julia -generate_obj_for_compile(f, tt, path::String = tempname(), name = fix_name(f), filenamebase::String="obj"; - \tmixtape = NoContext(), - \tstrip_llvm = false, - \tstrip_asm = true, - \ttarget = (), - \topt_level = 3, - \tkwargs...) -``` -Low level interface for compiling object code (`.o`) for for function `f` given -a tuple type `tt` characterizing the types of the arguments for which the -function will be compiled. - -`mixtape` defines a context that can be used to transform IR prior to compilation using -[Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) features. - -`target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. -This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). -The defaults compile to the native target. - -### Examples -```julia -julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) -fib (generic function with 1 method) - -julia> path, name, table = StaticCompiler.generate_obj_for_compile(fib, Tuple{Int64}, "./test") -("./test", "fib", IdDict{Any, String}()) - -shell> tree \$path -./test -└── obj.o - -0 directories, 1 file -``` -""" -function generate_obj_for_compile(f, tt, external = true, path::String = tempname(), name = fix_name(f), filenamebase::String="obj"; - mixtape = NoContext(), - strip_llvm = false, - strip_asm = true, - opt_level = 3, - remove_julia_addrspaces = false, - target = (), - kwargs...) - mkpath(path) - obj_path = joinpath(path, "$filenamebase.o") - #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. - params = StaticCompilerParams(opt = true, mixtape = mixtape, optlevel = Base.JLOptions().opt_level) - config = GPUCompiler.CompilerConfig(NativeCompilerTarget(target...), params, name = name, kernel = false) - job = GPUCompiler.CompilerJob(GPUCompiler.methodinstance(typeof(f), tt), config) - - table = GPUCompiler.JuliaContext() do context - mod, meta = GPUCompiler.codegen(:llvm, job; strip=strip_llvm, only_entry=false, validate=false, optimize=false) - # Use Enzyme's annotation and optimization pipeline - annotate!(mod) - tm = GPUCompiler.llvm_machine(external ? ExternalNativeCompilerTarget(target...) : NativeCompilerTarget(target...)) - optimize!(mod, tm) - - # Scoop up all the pointers in the optimized module, and replace them with unitialized global variables. - # `table` is a dictionary where the keys are julia objects that are needed by the function, and the values - # of the dictionary are the names of their associated LLVM GlobalVariable names. - table = relocation_table!(mod) - - # Now that we've removed all the pointers from the code, we can (hopefully) safely lower all the instrinsics - # (again, using Enzyme's pipeline) - post_optimize!(mod, tm; remove_julia_addrspaces) - - # Make sure we didn't make any glaring errors - LLVM.verify(mod) - obj, _ = GPUCompiler.emit_asm(job, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) - # Compile the LLVM module to native code and save it to disk - open(obj_path, "w") do io - write(io, obj) - end - table - end - - - path, name, table -end """ ```julia @@ -204,6 +32,7 @@ compile_executable(f::Function, types::Tuple, path::String, [name::String=string filename::String=name, cflags=``, # Specify libraries you would like to link against, and other compiler options here also_expose=[], + method_table=StaticCompiler.method_table, kwargs... ) ``` @@ -295,11 +124,23 @@ end """ ```julia -compile_shlib(f::Function, types::Tuple, [path::String="./"], [name::String=string(nameof(f))]; filename::String=name, cflags=``, kwargs...) -compile_shlib(funcs::Array, [path::String="./"]; filename="libfoo", demangle=true, cflags=``, kwargs...) +compile_shlib(f::Function, types::Tuple, [path::String="./"], [name::String=string(nameof(f))]; + filename::String=name, + cflags=``, + method_table=StaticCompiler.method_table, + kwargs...) + +compile_shlib(funcs::Array, [path::String="./"]; + filename="libfoo", + demangle=true, + cflags=``, + method_table=StaticCompiler.method_table, + kwargs...) ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. +Arguments and returned values from `compile_shlib` must be native objects such as `Int`, `Float64`, or `Ptr`. They cannot be things like `Tuple{Int, Int}` because that is not natively sized. Such objects need to be passed by reference instead of by value. + If `demangle` is set to `false`, compiled function names are prepended with "julia_". ### Examples @@ -353,38 +194,7 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String="./"; joinpath(abspath(path), filename * "." * Libdl.dlext) end - -""" -```julia -compile_wasm(f::Function, types::Tuple, [path::String="./"], [name::String=string(nameof(f))]; filename::String=name, flags=``, kwargs...) -compile_wasm(funcs::Union{Array,Tuple}, [path::String="./"]; filename="libfoo", demangle=true, flags=``, kwargs...) -``` -As `compile_shlib`, but compiling to a WebAssembly library. - -If `demangle` is set to `false`, compiled function names are prepended with "julia_". -``` -""" -function compile_wasm(f::Function, types=(); - path::String = "./", - filename = fix_name(f), - flags = ``, - kwargs... - ) - tt = Base.to_tuple_type(types) - obj_path, name = generate_obj_for_compile(f, tt, true, path, filename; target = (triple = "wasm32-unknown-wasi", cpu = "", features = ""), remove_julia_addrspaces = true, kwargs...) - run(`$(lld()) -flavor wasm --no-entry --export-all $flags $obj_path/obj.o -o $path/$name.wasm`) - joinpath(abspath(path), filename * ".wasm") -end -function compile_wasm(funcs::Union{Array,Tuple}; - path::String="./", - filename="libfoo", - flags=``, - kwargs... - ) - obj_path, name = generate_obj(funcs, true, path, filename; target = (triple = "wasm32-unknown-wasi", cpu = "", features = ""), remove_julia_addrspaces = true, kwargs...) - run(`$(lld()) -flavor wasm --no-entry --export-all $flags $obj_path/$filename.o -o $path/$filename.wasm`) - joinpath(abspath(path), filename * ".wasm") -end + """ ```julia @@ -429,6 +239,7 @@ function generate_shlib_fptr(path::String, name, filename::String=name) @assert fptr != C_NULL fptr end + # As above, but also compile (maybe remove this method in the future?) function generate_shlib_fptr(f, tt, path::String=tempname(), name=fix_name(f), filename::String=name; temp::Bool=true, @@ -586,8 +397,10 @@ function native_llvm_module(f, tt, name=fix_name(f); demangle, kwargs...) name = "julia_"*name end job, kwargs = native_job(f, tt, true; name, kwargs...) - m, _ = GPUCompiler.JuliaContext() do context - GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + m = GPUCompiler.JuliaContext() do context + m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + locate_pointers_and_runtime_calls(m) + m end return m end @@ -596,25 +409,26 @@ end function native_llvm_module(funcs::Union{Array,Tuple}; demangle=true, kwargs...) f,tt = funcs[1] mod = GPUCompiler.JuliaContext() do context - name_f = fix_name(f) - if !demangle - name_f = "julia_"*name_f - end - job, kwargs = native_job(f, tt, true; name = name_f, kwargs...) - mod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) - if length(funcs) > 1 - for func in funcs[2:end] - f,tt = func - name_f = fix_name(f) - if !demangle - name_f = "julia_"*name_f - end - job, kwargs = native_job(f, tt, true; name = name_f, kwargs...) - tmod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) - link!(mod,tmod) - end - end - mod + name_f = fix_name(f) + if !demangle + name_f = "julia_"*name_f + end + job, kwargs = native_job(f, tt, true; name = name_f, kwargs...) + mod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + if length(funcs) > 1 + for func in funcs[2:end] + f,tt = func + name_f = fix_name(f) + if !demangle + name_f = "julia_"*name_f + end + job, kwargs = native_job(f, tt, true; name = name_f, kwargs...) + tmod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + link!(mod,tmod) + end + end + locate_pointers_and_runtime_calls(mod) + mod end # Just to be sure for (modfunc, func) in zip(functions(mod), funcs) @@ -644,7 +458,6 @@ end """ ```julia generate_obj(f, tt, external::Bool, path::String = tempname(), filenamebase::String="obj"; - mixtape = NoContext(), target = (), demangle = true, strip_llvm = false, @@ -656,9 +469,6 @@ Low level interface for compiling object code (`.o`) for for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. -`mixtape` defines a context that can be used to transform IR prior to compilation using -[Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) features. - `target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). The defaults compile to the native target. @@ -688,7 +498,6 @@ end """ ```julia generate_obj(funcs::Union{Array,Tuple}, external::Bool, path::String = tempname(), filenamebase::String="obj"; - mixtape = NoContext(), target = (), demangle =false, strip_llvm = false, @@ -700,9 +509,6 @@ Low level interface for compiling object code (`.o`) for an array of Tuples (f, tt) where each function `f` and tuple type `tt` determine the set of methods which will be compiled. -`mixtape` defines a context that can be used to transform IR prior to compilation using -[Mixtape](https://github.com/JuliaCompilerPlugins/Mixtape.jl) features. - `target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). The defaults compile to the native target. @@ -717,8 +523,11 @@ function generate_obj(funcs::Union{Array,Tuple}, external::Bool, path::String = mkpath(path) obj_path = joinpath(path, "$filenamebase.o") mod = native_llvm_module(funcs; demangle, kwargs...) - fakejob, _ = native_job(f, tt, external; kwargs...) - obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) + obj = GPUCompiler.JuliaContext() do ctx + fakejob, _ = native_job(f, tt, external; 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 diff --git a/src/code_loading.jl b/src/code_loading.jl deleted file mode 100644 index 8fc1ae60..00000000 --- a/src/code_loading.jl +++ /dev/null @@ -1,84 +0,0 @@ -""" - load_function(path) --> compiled_f - -load a `StaticCompiledFunction` from a given path. This object is callable. -""" -function load_function(path; filename="obj") - instantiate(deserialize(joinpath(path, "$filename.cjl"))) -end - -struct LazyStaticCompiledFunction{rt, tt} - f::Symbol - path::String - name::String - filename::String - reloc::IdDict{Any,String} -end - -""" - unsafe_pointer_from_objref(x) - -Sometimes Julia embeds immutables like `Base.string` into code, and julia -will error if you call `pointer_from_objref(string)`, claiming that it -doesn't have a pointer even though that's a lie. -""" -unsafe_pointer_from_objref(x) = ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), x) - -function instantiate(p::LazyStaticCompiledFunction{rt, tt}) where {rt, tt} - # LLVM.load_library_permantly(dirname(Libdl.dlpath(Libdl.dlopen("libjulia")))) - lljit = LLVM.LLJIT(;tm=LLVM.JITTargetMachine()) - jd = LLVM.JITDylib(lljit) - flags = LLVM.API.LLVMJITSymbolFlags(LLVM.API.LLVMJITSymbolGenericFlagsExported, 0) - ofile = LLVM.MemoryBufferFile(joinpath(p.path, "$(p.filename).o")) #$(Libdl.dlext) - - - # Set all the uninitialized global variables to point to julia values from the relocation table - for (val, name) ∈ p.reloc - address = LLVM.API.LLVMOrcJITTargetAddress(reinterpret(UInt, unsafe_pointer_from_objref(val))) - - symbol = LLVM.API.LLVMJITEvaluatedSymbol(address, flags) - gv = LLVM.API.LLVMJITCSymbolMapPair(LLVM.mangle(lljit, name), symbol) - mu = absolute_symbols(Ref(gv)) - LLVM.define(jd, mu) - end - # consider switching to one mu for all gvs instead of one per gv. - # I tried that already, but I got an error saying - # JIT session error: Symbols not found: [ __Type_Vector_Float64___274 ] - - # Link to libjulia - prefix = LLVM.get_prefix(lljit) - dg = LLVM.CreateDynamicLibrarySearchGeneratorForProcess(prefix) - LLVM.add!(jd, dg) - LLVM.add!(lljit, jd, ofile) - fptr = pointer(LLVM.lookup(lljit, p.name)) - - StaticCompiledFunction{rt, tt}(p.f, fptr, lljit, p.reloc) -end - -function absolute_symbols(symbols) - ref = LLVM.API.LLVMOrcAbsoluteSymbols(symbols, length(symbols)) - LLVM.MaterializationUnit(ref) -end - - -struct StaticCompiledFunction{rt, tt} - f::Symbol - ptr::Ptr{Nothing} - jit::LLVM.LLJIT - reloc::IdDict{Any, String} -end - -function Base.show(io::IO, f::StaticCompiledFunction{rt, tt}) where {rt, tt} - types = [tt.parameters...] - print(io, String(f.f), "(", join(("::$T" for T ∈ tt.parameters), ',') ,") :: $rt") -end - -function (f::StaticCompiledFunction{rt, tt})(args...) where {rt, tt} - Tuple{typeof.(args)...} == tt || error("Input types don't match compiled target $((tt.parameters...,)). Got arguments of type $(typeof.(args))") - out = RefValue{rt}() - refargs = Ref(args) - ccall(f.ptr, Nothing, (Ptr{rt}, Ref{tt}), pointer_from_objref(out), refargs) - out[] -end - -instantiate(f::StaticCompiledFunction) = f diff --git a/src/interpreter.jl b/src/interpreter.jl index c0e00c37..344cc53d 100644 --- a/src/interpreter.jl +++ b/src/interpreter.jl @@ -7,7 +7,7 @@ using GPUCompiler: using CodeInfoTools using CodeInfoTools: resolve -struct StaticInterpreter{M} <: AbstractInterpreter +struct StaticInterpreter <: AbstractInterpreter global_cache::CodeCache method_table::Union{Nothing,Core.MethodTable} @@ -20,13 +20,10 @@ struct StaticInterpreter{M} <: AbstractInterpreter inf_params::InferenceParams opt_params::OptimizationParams - # Mixtape context - mixtape::M - - function StaticInterpreter(cache::CodeCache, mt::Union{Nothing,Core.MethodTable}, world::UInt, ip::InferenceParams, op::OptimizationParams, mixtape::CompilationContext) + function StaticInterpreter(cache::CodeCache, mt::Union{Nothing,Core.MethodTable}, world::UInt, ip::InferenceParams, op::OptimizationParams) @assert world <= Base.get_world_counter() - return new{typeof(mixtape)}( + return new( cache, mt, @@ -38,10 +35,7 @@ struct StaticInterpreter{M} <: AbstractInterpreter # parameters for inference and optimization ip, - op, - - # Mixtape context - mixtape + op ) end end @@ -79,9 +73,6 @@ function custom_pass!(interp::StaticInterpreter, result::InferenceResult, mi::Co mi.specTypes isa UnionAll && return src sig = Tuple(mi.specTypes.parameters) as = map(resolve_generic, sig) - if allow(interp.mixtape, mi.def.module, as...) - src = transform(interp.mixtape, src, sig) - end return src end @@ -102,22 +93,21 @@ end Core.Compiler.may_optimize(interp::StaticInterpreter) = true Core.Compiler.may_compress(interp::StaticInterpreter) = true Core.Compiler.may_discard_trees(interp::StaticInterpreter) = true -if VERSION >= v"1.7.0-DEV.577" Core.Compiler.verbose_stmt_info(interp::StaticInterpreter) = false -end + if isdefined(Base.Experimental, Symbol("@overlay")) -using Core.Compiler: OverlayMethodTable -if v"1.8-beta2" <= VERSION < v"1.9-" || VERSION >= v"1.9.0-DEV.120" -Core.Compiler.method_table(interp::StaticInterpreter) = - OverlayMethodTable(interp.world, interp.method_table) -else -Core.Compiler.method_table(interp::StaticInterpreter, sv::InferenceState) = - OverlayMethodTable(interp.world, interp.method_table) -end + using Core.Compiler: OverlayMethodTable + if v"1.8-beta2" <= VERSION < v"1.9-" || VERSION >= v"1.9.0-DEV.120" + Core.Compiler.method_table(interp::StaticInterpreter) = + OverlayMethodTable(interp.world, interp.method_table) + else + Core.Compiler.method_table(interp::StaticInterpreter, sv::InferenceState) = + OverlayMethodTable(interp.world, interp.method_table) + end else -Core.Compiler.method_table(interp::StaticInterpreter, sv::InferenceState) = - WorldOverlayMethodTable(interp.world) + Core.Compiler.method_table(interp::StaticInterpreter, sv::InferenceState) = + WorldOverlayMethodTable(interp.world) end # semi-concrete interepretation is broken with overlays (JuliaLang/julia#47349) @@ -134,13 +124,11 @@ end struct StaticCompilerParams <: AbstractCompilerParams opt::Bool optlevel::Int - mixtape::CompilationContext cache::CodeCache end function StaticCompilerParams(; opt = false, optlevel = Base.JLOptions().opt_level, - mixtape = NoContext(), cache = CodeCache()) - return StaticCompilerParams(opt, optlevel, mixtape, cache) + return StaticCompilerParams(opt, optlevel, cache) end diff --git a/src/optimize.jl b/src/optimize.jl deleted file mode 100644 index b781517b..00000000 --- a/src/optimize.jl +++ /dev/null @@ -1,328 +0,0 @@ -# stolen from https://github.com/EnzymeAD/Enzyme.jl/blob/1b187cc16953727cab26b64bc6a6dcf106c29a57/src/compiler/optimize.jl#L213 - -function optimize!(mod::LLVM.Module, tm) - # everying except unroll, slpvec, loop-vec - # then finish Julia GC - ModulePassManager() do pm - add_library_info!(pm, triple(mod)) - add_transform_info!(pm, tm) - - propagate_julia_addrsp!(pm) - scoped_no_alias_aa!(pm) - type_based_alias_analysis!(pm) - basic_alias_analysis!(pm) - cfgsimplification!(pm) - dce!(pm) -@static if isdefined(GPUCompiler, :cpu_features!) - GPUCompiler.cpu_features!(pm) -end - scalar_repl_aggregates_ssa!(pm) # SSA variant? - mem_cpy_opt!(pm) - always_inliner!(pm) - alloc_opt!(pm) - instruction_combining!(pm) - cfgsimplification!(pm) - scalar_repl_aggregates_ssa!(pm) # SSA variant? - instruction_combining!(pm) - jump_threading!(pm) - correlated_value_propagation!(pm) - instruction_combining!(pm) - reassociate!(pm) - early_cse!(pm) - alloc_opt!(pm) - loop_idiom!(pm) - loop_rotate!(pm) - lower_simdloop!(pm) - licm!(pm) - loop_unswitch!(pm) - instruction_combining!(pm) - ind_var_simplify!(pm) - loop_deletion!(pm) - loop_unroll!(pm) - alloc_opt!(pm) - scalar_repl_aggregates_ssa!(pm) # SSA variant? - gvn!(pm) - # This InstCombine needs to be after GVN - # Otherwise it will generate load chains in GPU code... - instruction_combining!(pm) - mem_cpy_opt!(pm) - sccp!(pm) - instruction_combining!(pm) - jump_threading!(pm) - dead_store_elimination!(pm) - alloc_opt!(pm) - cfgsimplification!(pm) - loop_idiom!(pm) - loop_deletion!(pm) - jump_threading!(pm) - correlated_value_propagation!(pm) - # SLP_Vectorizer -- not for Enzyme - aggressive_dce!(pm) - instruction_combining!(pm) - # Loop Vectorize -- not for Enzyme - # InstCombine - - # GC passes - barrier_noop!(pm) - gc_invariant_verifier!(pm, false) - - # FIXME: Currently crashes printing - cfgsimplification!(pm) - instruction_combining!(pm) # Extra for Enzyme - #API.EnzymeAddAttributorLegacyPass(pm) - run!(pm, mod) - end - # @show "omod", mod - # flush(stdout) - # flush(stderr) -end - -# https://github.com/JuliaLang/julia/blob/2eb5da0e25756c33d1845348836a0a92984861ac/src/aotcompile.cpp#L603 -function addTargetPasses!(pm, tm) - add_library_info!(pm, LLVM.triple(tm)) - add_transform_info!(pm, tm) -end - -# https://github.com/JuliaLang/julia/blob/2eb5da0e25756c33d1845348836a0a92984861ac/src/aotcompile.cpp#L620 -function addOptimizationPasses!(pm) - constant_merge!(pm) - - propagate_julia_addrsp!(pm) - scoped_no_alias_aa!(pm) - type_based_alias_analysis!(pm) - basic_alias_analysis!(pm) - cfgsimplification!(pm) - dce!(pm) - scalar_repl_aggregates!(pm) - - # mem_cpy_opt!(pm) - - always_inliner!(pm) # Respect always_inline - - # Running `memcpyopt` between this and `sroa` seems to give `sroa` a hard time - # merging the `alloca` for the unboxed data and the `alloca` created by the `alloc_opt` - # pass. - - alloc_opt!(pm) - # consider AggressiveInstCombinePass at optlevel > 2 - - instruction_combining!(pm) - cfgsimplification!(pm) - scalar_repl_aggregates!(pm) - instruction_combining!(pm) # TODO: createInstSimplifyLegacy - jump_threading!(pm) - correlated_value_propagation!(pm) - - reassociate!(pm) - - early_cse!(pm) - - # Load forwarding above can expose allocations that aren't actually used - # remove those before optimizing loops. - alloc_opt!(pm) - loop_rotate!(pm) - # moving IndVarSimplify here prevented removing the loop in perf_sumcartesian(10:-1:1) - loop_idiom!(pm) - - # LoopRotate strips metadata from terminator, so run LowerSIMD afterwards - lower_simdloop!(pm) # Annotate loop marked with "loopinfo" as LLVM parallel loop - licm!(pm) - julia_licm!(pm) - # Subsequent passes not stripping metadata from terminator - instruction_combining!(pm) # TODO: createInstSimplifyLegacy - ind_var_simplify!(pm) - loop_deletion!(pm) - loop_unroll!(pm) # TODO: in Julia createSimpleLoopUnroll - - # Run our own SROA on heap objects before LLVM's - alloc_opt!(pm) - # Re-run SROA after loop-unrolling (useful for small loops that operate, - # over the structure of an aggregate) - scalar_repl_aggregates!(pm) - instruction_combining!(pm) # TODO: createInstSimplifyLegacy - - gvn!(pm) - mem_cpy_opt!(pm) - sccp!(pm) - - # Run instcombine after redundancy elimination to exploit opportunities - # opened up by them. - # This needs to be InstCombine instead of InstSimplify to allow - # loops over Union-typed arrays to vectorize. - instruction_combining!(pm) - jump_threading!(pm) - dead_store_elimination!(pm) - - # More dead allocation (store) deletion before loop optimization - # consider removing this: - alloc_opt!(pm) - - # see if all of the constant folding has exposed more loops - # to simplification and deletion - # this helps significantly with cleaning up iteration - cfgsimplification!(pm) - loop_deletion!(pm) - instruction_combining!(pm) - loop_vectorize!(pm) - # TODO: createLoopLoadEliminationPass - cfgsimplification!(pm) - slpvectorize!(pm) - # might need this after LLVM 11: - # TODO: createVectorCombinePass() - - aggressive_dce!(pm) -end - -function addMachinePasses!(pm) - combine_mul_add!(pm) - # TODO: createDivRemPairs[] - - demote_float16!(pm) - gvn!(pm) -end - -function addJuliaLegalizationPasses!(pm, lower_intrinsics=true) - if lower_intrinsics - # LowerPTLS removes an indirect call. As a result, it is likely to trigger - # LLVM's devirtualization heuristics, which would result in the entire - # pass pipeline being re-exectuted. Prevent this by inserting a barrier. - barrier_noop!(pm) - lower_exc_handlers!(pm) - gc_invariant_verifier!(pm, false) - - # Needed **before** LateLowerGCFrame on LLVM < 12 - # due to bug in `CreateAlignmentAssumption`. - remove_ni!(pm) - late_lower_gc_frame!(pm) - final_lower_gc!(pm) - # We need these two passes and the instcombine below - # after GC lowering to let LLVM do some constant propagation on the tags. - # and remove some unnecessary write barrier checks. - gvn!(pm) - sccp!(pm) - # Remove dead use of ptls - dce!(pm) - lower_ptls!(pm, #=dump_native=# false) - instruction_combining!(pm) - # Clean up write barrier and ptls lowering - cfgsimplification!(pm) - else - barrier_noop!(pm) - remove_ni!(pm) - end -end - -function post_optimize!(mod, tm; remove_julia_addrspaces = false) - # @show "pre_post", mod - # flush(stdout) - # flush(stderr) - LLVM.ModulePassManager() do pm - addTargetPasses!(pm, tm) - addOptimizationPasses!(pm) - run!(pm, mod) - end - LLVM.ModulePassManager() do pm - addJuliaLegalizationPasses!(pm, true) - addMachinePasses!(pm) - if remove_julia_addrspaces - remove_julia_addrspaces!(pm) - end - run!(pm, mod) - end - # @show "post_mod", mod - # flush(stdout) - # flush(stderr) -end - - - - -const inactivefns = Set{String}(( - "jl_gc_queue_root", "gpu_report_exception", "gpu_signal_exception", - "julia.ptls_states", "julia.write_barrier", "julia.typeof", "jl_box_int64", "jl_box_int32", - "jl_subtype", "julia.get_pgcstack", "jl_in_threaded_region", "jl_object_id_", "jl_object_id", - "jl_breakpoint", - "llvm.julia.gc_preserve_begin","llvm.julia.gc_preserve_end", "jl_get_ptls_states", - "jl_f_fieldtype", - "jl_symbol_n", - # BIG TODO - "jl_gc_add_finalizer_th", - # "jl_" -)) - -const activefns = Set{String}(( - "jl_", -)) - -function annotate!(mod) - inactive = LLVM.StringAttribute("enzyme_inactive", "") - active = LLVM.StringAttribute("enzyme_active", "") - fns = functions(mod) - - for inactivefn in inactivefns - if haskey(fns, inactivefn) - fn = fns[inactivefn] - push!(function_attributes(fn), inactive) - end - end - - for activefn in activefns - if haskey(fns, activefn) - fn = fns[activefn] - push!(function_attributes(fn), active) - end - end - - for fname in ("julia.typeof",) - if haskey(fns, fname) - fn = fns[fname] - push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0)) - push!(function_attributes(fn), LLVM.StringAttribute("enzyme_shouldrecompute")) - end - end - - for fname in ("julia.get_pgcstack", "julia.ptls_states", "jl_get_ptls_states") - if haskey(fns, fname) - fn = fns[fname] - # TODO per discussion w keno perhaps this should change to readonly / inaccessiblememonly - push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0)) - end - end - - for fname in ("julia.pointer_from_objref",) - if haskey(fns, fname) - fn = fns[fname] - push!(function_attributes(fn), LLVM.EnumAttribute("readnone", 0)) - end - end - - for boxfn in ("jl_box_float32", "jl_box_float64", "jl_box_int32", "jl_box_int64", "julia.gc_alloc_obj", "jl_alloc_array_1d", "jl_alloc_array_2d", "jl_alloc_array_3d") - if haskey(fns, boxfn) - fn = fns[boxfn] - push!(return_attributes(fn), LLVM.EnumAttribute("noalias", 0)) - push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0)) - end - end - - for gc in ("llvm.julia.gc_preserve_begin", "llvm.julia.gc_preserve_end") - if haskey(fns, gc) - fn = fns[gc] - push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0)) - end - end - - for rfn in ("jl_object_id_", "jl_object_id") - if haskey(fns, rfn) - fn = fns[rfn] - push!(function_attributes(fn), LLVM.EnumAttribute("readonly", 0)) - end - end - - for rfn in ("jl_in_threaded_region_", "jl_in_threaded_region") - if haskey(fns, rfn) - fn = fns[rfn] - push!(function_attributes(fn), LLVM.EnumAttribute("readonly", 0)) - push!(function_attributes(fn), LLVM.EnumAttribute("inaccessiblememonly", 0)) - end - end -end diff --git a/src/pointer_patching.jl b/src/pointer_patching.jl deleted file mode 100644 index 27a44a08..00000000 --- a/src/pointer_patching.jl +++ /dev/null @@ -1,185 +0,0 @@ -function relocation_table!(mod) - i64 = LLVM.IntType(64) - d = IdDict{Any, Tuple{String, LLVM.GlobalVariable}}() - - for func ∈ LLVM.functions(mod), bb ∈ LLVM.blocks(func), inst ∈ LLVM.instructions(bb) - if isa(inst, LLVM.LoadInst) && occursin("inttoptr", string(inst)) - get_pointers!(d, mod, inst) - elseif isa(inst, LLVM.StoreInst) && occursin("inttoptr", string(inst)) - @debug "Relocating StoreInst" inst - get_pointers!(d, mod, inst) - elseif inst isa LLVM.RetInst && occursin("inttoptr", string(inst)) - @debug "Relocating RetInst" inst LLVM.operands(inst) - get_pointers!(d, mod, inst) - elseif isa(inst, LLVM.BitCastInst) && occursin("inttoptr", string(inst)) - @debug "Relocating BitCastInst" inst LLVM.operands(inst) - get_pointers!(d, mod, inst) - elseif isa(inst, LLVM.CallInst) - @debug "Relocating CallInst" inst LLVM.operands(inst) - dest = LLVM.called_value(inst) - if occursin("inttoptr", string(dest)) && length(LLVM.operands(dest)) > 0 - @debug "Relocating CallInst inttoptr" dest LLVM.operands(dest) LLVM.operands(inst) - ptr_arg = first(LLVM.operands(dest)) - ptr_val = convert(Int, ptr_arg) - ptr = Ptr{Cvoid}(ptr_val) - - frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) - if length(frames) >= 1 - fn, file, line, linfo, fromC, inlined = last(frames) - fn = string(fn) - if ptr == cglobal(:jl_alloc_array_1d) - fn = "jl_alloc_array_1d" - end - if ptr == cglobal(:jl_alloc_array_2d) - fn = "jl_alloc_array_2d" - end - if ptr == cglobal(:jl_alloc_array_3d) - fn = "jl_alloc_array_3d" - end - if ptr == cglobal(:jl_new_array) - fn = "jl_new_array" - end - if ptr == cglobal(:jl_array_copy) - fn = "jl_array_copy" - end - if ptr == cglobal(:jl_alloc_string) - fn = "jl_alloc_string" - end - if ptr == cglobal(:jl_in_threaded_region) - fn = "jl_in_threaded_region" - end - if ptr == cglobal(:jl_enter_threaded_region) - fn = "jl_enter_threaded_region" - end - if ptr == cglobal(:jl_exit_threaded_region) - fn = "jl_exit_threaded_region" - end - if ptr == cglobal(:jl_set_task_tid) - fn = "jl_set_task_tid" - end - if ptr == cglobal(:jl_new_task) - fn = "jl_new_task" - end - if ptr == cglobal(:malloc) - fn = "malloc" - end - if ptr == cglobal(:memmove) - fn = "memmove" - end - if ptr == cglobal(:jl_array_grow_beg) - fn = "jl_array_grow_beg" - end - if ptr == cglobal(:jl_array_grow_end) - fn = "jl_array_grow_end" - end - if ptr == cglobal(:jl_array_grow_at) - fn = "jl_array_grow_at" - end - if ptr == cglobal(:jl_array_del_beg) - fn = "jl_array_del_beg" - end - if ptr == cglobal(:jl_array_del_end) - fn = "jl_array_del_end" - end - if ptr == cglobal(:jl_array_del_at) - fn = "jl_array_del_at" - end - if ptr == cglobal(:jl_array_ptr) - fn = "jl_array_ptr" - end - if ptr == cglobal(:jl_value_ptr) - fn = "jl_value_ptr" - end - if ptr == cglobal(:jl_get_ptls_states) - fn = "jl_get_ptls_states" - end - if ptr == cglobal(:jl_gc_add_finalizer_th) - fn = "jl_gc_add_finalizer_th" - end - if ptr == cglobal(:jl_symbol_n) - fn = "jl_symbol_n" - end - end - - if length(fn) > 1 && fromC - mod = LLVM.parent(LLVM.parent(LLVM.parent(inst))) - lfn = LLVM.API.LLVMGetNamedFunction(mod, fn) - - if lfn == C_NULL - lfn = LLVM.API.LLVMAddFunction(mod, fn, LLVM.API.LLVMGetCalledFunctionType(inst)) - else - lfn = LLVM.API.LLVMConstBitCast(lfn, LLVM.PointerType(LLVM.FunctionType(LLVM.API.LLVMGetCalledFunctionType(inst)))) - end - LLVM.API.LLVMSetOperand(inst, LLVM.API.LLVMGetNumOperands(inst)-1, lfn) - end - end - get_pointers!(d, mod, inst) - end - end - IdDict{Any, String}(val => name for (val, (name, _)) ∈ d) -end - -function get_pointers!(d, mod, inst) - jl_t = (LLVM.StructType(LLVM.LLVMType[])) - for (i, arg) ∈ enumerate(LLVM.operands(inst)) - if occursin("inttoptr", string(arg)) && arg isa LLVM.ConstantExpr - op1 = LLVM.Value(LLVM.API.LLVMGetOperand(arg, 0)) - if op1 isa LLVM.ConstantExpr - op1 = LLVM.Value(LLVM.API.LLVMGetOperand(op1, 0)) - end - ptr = Ptr{Cvoid}(convert(Int, op1)) - frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) - if length(frames) >= 1 - fn, file, line, linfo, fromC, inlined = last(frames) - if (isempty(String(fn)) && isempty(String(file))) || fn == :jl_system_image_data - val = unsafe_pointer_to_objref(ptr) - if val ∈ keys(d) - _, gv = d[val] - LLVM.API.LLVMSetOperand(inst, i-1, gv) - else - gv_name = fix_name(String(gensym(repr(Core.Typeof(val))))) - gv = LLVM.GlobalVariable(mod, llvmeltype(arg), gv_name, LLVM.addrspace(value_type(arg))) - - LLVM.extinit!(gv, true) - LLVM.API.LLVMSetOperand(inst, i-1, gv) - - d[val] = (gv_name, gv) - end - else - @warn "Found data we don't know how to relocate." frames - end - end - end - end -end - -llvmeltype(x::LLVM.Value) = eltype(LLVM.value_type(x)) - -function pointer_patching_diff(f, tt, path1=tempname(), path2=tempname(); show_reloc_table=false) - tm = GPUCompiler.llvm_machine(NativeCompilerTarget()) - job, kwargs = native_job(f, tt, false; name=fix_name(string(nameof(f)))) - #Get LLVM to generated a module of code for us. We don't want GPUCompiler's optimization passes. - mod, meta = GPUCompiler.JuliaContext() do context - GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, optimize=false) - end - # Use Enzyme's annotation and optimization pipeline - annotate!(mod) - optimize!(mod, tm) - - s1 = string(mod) - write(path1, s1) - - d = StaticCompiler.relocation_table!(mod) - if show_reloc_table - @show d - end - - s2 = string(mod) - write(path2, s2) - - pdiff = run(Cmd(`diff $path1 $path2`, ignorestatus=true)) - pdiff.exitcode == 2 && error("Showing diff caused an error") - nothing -end - - diff --git a/src/pointer_warning.jl b/src/pointer_warning.jl new file mode 100644 index 00000000..9f8f30c8 --- /dev/null +++ b/src/pointer_warning.jl @@ -0,0 +1,72 @@ +function locate_pointers_and_runtime_calls(mod) + i64 = LLVM.IntType(64) + # d = IdDict{Any, Tuple{String, LLVM.GlobalVariable}}() + for func ∈ LLVM.functions(mod), bb ∈ LLVM.blocks(func), inst ∈ LLVM.instructions(bb) + warned = false + if isa(inst, LLVM.LoadInst) && occursin("inttoptr", string(inst)) + warned = inspect_pointers(mod, inst) + elseif isa(inst, LLVM.StoreInst) && occursin("inttoptr", string(inst)) + @debug "Inspecting StoreInst" inst + warned = inspect_pointers(mod, inst) + elseif inst isa LLVM.RetInst && occursin("inttoptr", string(inst)) + @debug "Inspecting RetInst" inst LLVM.operands(inst) + warned = inspect_pointers(mod, inst) + elseif isa(inst, LLVM.BitCastInst) && occursin("inttoptr", string(inst)) + @debug "Inspecting BitCastInst" inst LLVM.operands(inst) + warned = inspect_pointers(mod, inst) + elseif isa(inst, LLVM.CallInst) + @debug "Inspecting CallInst" inst LLVM.operands(inst) + dest = LLVM.called_operand(inst) + if occursin("inttoptr", string(dest)) && length(LLVM.operands(dest)) > 0 + @debug "Inspecting CallInst inttoptr" dest LLVM.operands(dest) LLVM.operands(inst) + ptr_arg = first(LLVM.operands(dest)) + ptr_val = convert(Int, ptr_arg) + ptr = Ptr{Cvoid}(ptr_val) + + frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) + + data_warnings(inst, frames) + warned = true + end + end + if warned + @warn("LLVM function generated warnings due to raw pointers embedded in the code. This will likely cause errors or undefined behaviour.", + func = func) + end + end +end + +function inspect_pointers(mod, inst) + warned = false + jl_t = (LLVM.StructType(LLVM.LLVMType[])) + for (i, arg) ∈ enumerate(LLVM.operands(inst)) + if occursin("inttoptr", string(arg)) && arg isa LLVM.ConstantExpr + op1 = LLVM.Value(LLVM.API.LLVMGetOperand(arg, 0)) + if op1 isa LLVM.ConstantExpr + op1 = LLVM.Value(LLVM.API.LLVMGetOperand(op1, 0)) + end + ptr = Ptr{Cvoid}(convert(Int, op1)) + frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) + data_warnings(inst, frames) + warned = true + end + end + warned +end + +data_warnings(inst, frames) = for frame ∈ frames + fn, file, line, linfo, fromC, inlined = frame + @warn("Found pointer references to julia data", + "llvm instruction" = inst, + name = fn, + file = file, + line = line, + fromC = fromC, + inlined = inlined) +end + +llvmeltype(x::LLVM.Value) = eltype(LLVM.value_type(x)) + + + + diff --git a/src/target.jl b/src/target.jl index d9647b4d..777960a3 100644 --- a/src/target.jl +++ b/src/target.jl @@ -4,7 +4,6 @@ else const method_table = nothing end -const overrides = quote end """ ```julia @@ -25,26 +24,23 @@ macro device_override(ex) error() end code = quote - $GPUCompiler.@override(StaticCompiler.method_table, $ex) - end - if isdefined(Base.Experimental, Symbol("@overlay")) - return esc(code) - else - push!(overrides, code) - return + $Base.Experimental.@overlay($StaticCompiler.method_table, $ex) end + return esc(code) end -Base.@kwdef struct NativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget +Base.@kwdef struct NativeCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget triple::String=Sys.MACHINE cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) + method_table::MT = method_table end -Base.@kwdef struct ExternalNativeCompilerTarget <: GPUCompiler.AbstractCompilerTarget +Base.@kwdef struct ExternalNativeCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget triple::String=Sys.MACHINE cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) + method_table::MT = method_table end module StaticRuntime @@ -72,44 +68,43 @@ for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) return tm end - GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{$target}) = "native_$(job.config.target.cpu)-$(hash(job.config.target.features))" + GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{<:$target}) = "native_$(job.config.target.cpu)-$(hash(job.config.target.features))" - GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target}) = StaticRuntime - GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = StaticRuntime + GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:$target}) = StaticRuntime + GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:$target, StaticCompilerParams}) = StaticRuntime - GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = true - GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{$target}) = true + GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:$target, StaticCompilerParams}) = true + GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:$target}) = true - GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = + GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{<:$target, StaticCompilerParams}) = StaticInterpreter(job.config.params.cache, GPUCompiler.method_table(job), job.world, - GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job), - job.config.params.mixtape) - GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{$target, StaticCompilerParams}) = job.config.params.cache + GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job)) + GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{<:$target, StaticCompilerParams}) = job.config.params.cache + GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:$target})) = job.config.target.method_table end end -GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget})) = method_table -GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{ExternalNativeCompilerTarget, StaticCompilerParams})) = method_table - function native_job(@nospecialize(func::Function), @nospecialize(types::Type), external::Bool; - mixtape = NoContext(), name = fix_name(func), kernel::Bool = false, - target = (), + target = (;), + method_table=method_table, kwargs... ) + target = merge(target, (;method_table)) source = methodinstance(typeof(func), Base.to_tuple_type(types)) target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) - params = StaticCompilerParams(mixtape = mixtape) + params = StaticCompilerParams() config = GPUCompiler.CompilerConfig(target, params, name = name, kernel = kernel) StaticCompiler.CompilerJob(source, config), kwargs end -function native_job(@nospecialize(func), @nospecialize(types), external; mixtape = NoContext(), kernel::Bool=false, name=fix_name(repr(func)), target = (), kwargs...) +function native_job(@nospecialize(func), @nospecialize(types), external; kernel::Bool=false, name=fix_name(repr(func)), target = (;), method_table=method_table, kwargs...) + target = merge(target, (; method_table)) source = methodinstance(typeof(func), Base.to_tuple_type(types)) target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) - params = StaticCompilerParams(mixtape = mixtape) + params = StaticCompilerParams() config = GPUCompiler.CompilerConfig(target, params, name = name, kernel = kernel) GPUCompiler.CompilerJob(source, config), kwargs end diff --git a/test/Project.toml b/test/Project.toml index cc88d4e8..5498846c 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,5 +1,4 @@ [deps] -CodeInfoTools = "bc773b8a-8374-437a-b9f2-0e9785855863" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" @@ -14,3 +13,7 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e" + +[compat] +Bumper = "0.5.1" \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 697ef7e8..542659c2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,19 +7,19 @@ using ManualMemory using Distributed using StaticTools using StrideArraysCore -using CodeInfoTools using MacroTools using LLD_jll +using Bumper addprocs(1) @everywhere using StaticCompiler, StrideArraysCore const GROUP = get(ENV, "GROUP", "All") -@static if GROUP == "Core" || GROUP == "All" +if GROUP == "Core" || GROUP == "All" include("testcore.jl") end -@static if GROUP == "Integration" || GROUP == "All" +if GROUP == "Integration" || GROUP == "All" include("testintegration.jl") end diff --git a/test/testcore.jl b/test/testcore.jl index 823729cb..e705ed64 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -1,234 +1,9 @@ workdir = tempdir() -# workdir = "./" # For debugging -remote_load_call(path, args...) = fetch(@spawnat 2 load_function(path)(args...)) - -@testset "Basics" begin - - simple_sum(x) = x + one(typeof(x)) - - # This probably needs a macro - for T ∈ (Int, Float64, Int32, Float32, Int16, Float16) - _, path, = compile(simple_sum, (T,)) - @test remote_load_call(path, T(1)) == T(2) - end -end fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 -@testset "Recursion" begin - _, path = compile(fib, (Int,)) - @test remote_load_call(path, 10) == fib(10) - - # Trick to work around #40990 - _fib2(_fib2, n) = n <= 1 ? n : _fib2(_fib2, n-1) + _fib2(_fib2, n-2) - fib2(n) = _fib2(_fib2, n) - - _, path = compile(fib2, (Int,)) - @test remote_load_call(path, 20) == fib(20) - #@test compile(fib2, (Int,))[1](20) == fib(20) -end - - -@testset "Loops" begin - function sum_first_N_int(N) - s = 0 - for a in 1:N - s += a - end - s - end - _, path = compile(sum_first_N_int, (Int,)) - @test remote_load_call(path, 10) == 55 - - function sum_first_N_float64(N) - s = Float64(0) - for a in 1:N - s += Float64(a) - end - s - end - _, path = compile(sum_first_N_float64, (Int,)) - @test remote_load_call(path, 10) == 55. - - function sum_first_N_int_inbounds(N) - s = 0 - @inbounds for a in 1:N - s += a - end - s - end - _, path = compile(sum_first_N_int_inbounds, (Int,)) - @test remote_load_call(path, 10) == 55 - - function sum_first_N_float64_inbounds(N) - s = Float64(0) - @inbounds for a in 1:N - s += Float64(a) - end - s - end - _, path = compile(sum_first_N_float64_inbounds, (Int,)) - @test remote_load_call(path, 10) == 55. -end - -# Arrays with different input types Int32, Int64, Float32, Float64, Complex? -@testset "Arrays" begin - function array_sum(n, A) - s = zero(eltype(A)) - for i in 1:n - s += A[i] - end - s - end - for T ∈ (Int, Complex{Float32}, Complex{Float64}) - _, path = compile(array_sum, (Int, Vector{T})) - @test remote_load_call(path, 10, T.(1:10)) == T(55) - end -end - -@testset "Array allocations" begin - function f(N) - v = Vector{Float64}(undef, N) - for i ∈ eachindex(v) - v[i] = i*i - end - v - end - _, path = compile(f, (Int,)) - @test remote_load_call(path, 5) == [1.0, 4.0, 9.0, 16.0, 25.0] -end - -# This is also a good test of loading and storing from the same object -@testset "Load & Store Same object" begin - global const x = Ref(0) - counter() = x[] += 1 - _, path = compile(counter, ()) - @spawnat 2 global counter = load_function(path) - @test fetch(@spawnat 2 counter()) == 1 - @test fetch(@spawnat 2 counter()) == 2 -end - -# This is also a good test of loading and storing from the same object -counter = let x = Ref(0) - () -> x[] += 1 -end -@testset "Closures" begin - #this currently segfaults during compilation - @test_skip begin - _, path = compile(counter, ()) - @spawnat 2 global counter_comp = load_function(path) - @test fetch(@spawnat 2 counter_comp()) == 1 - @test fetch(@spawnat 2 counter_comp()) == 2 - end -end - - -@testset "Error handling" begin - _, path = compile(sqrt, (Int,)) - tsk = @spawnat 2 begin - try - load_function(path)(-1) - catch e; - e - end - end - @test fetch(tsk) isa DomainError -end - -# Julia wants to treat Tuple (and other things like it) as plain bits, but LLVM wants to treat it as something with a pointer. -# We need to be careful to not send, nor receive an unwrapped Tuple to a compiled function. -# The interface made in `compile` should handle this fine. -@testset "Send and receive Tuple" begin - foo(u::Tuple) = 2 .* reverse(u) .- 1 - - _, path = compile(foo, (NTuple{3, Int},)) - @test remote_load_call(path, (1, 2, 3)) == (5, 3, 1) -end - - -# Just to call external libraries -@testset "BLAS" begin - function mydot(a::Vector{Float64}) - N = length(a) - BLAS.dot(N, a, 1, a, 1) - end - a = [1.0, 2.0] - - mydot_compiled, path = compile(mydot, (Vector{Float64},)) - # Works locally for me, but not on CI. Need some improvements to pointer relocation to be robust. - @test_skip remote_load_call(path, a) == 5.0 - @test mydot_compiled(a) ≈ 5.0 - - # This will need some more work apparently - @test_skip begin - _, path = compile((*), (Matrix{Float64}, Matrix{Float64})) - A, B = rand(10, 11), rand(11, 12) - @test remote_load_call(path, A, B) ≈ A * B - end -end - - -@testset "Strings" begin - function hello(name) - "Hello, " * name * "!" - end - hello_compiled, path = compile(hello, (String,)) - @test remote_load_call(path, "world") == "Hello, world!" - - # We'll need to be able to relocate a bunch of UV stuff for this, and deal with dynamic dispatch. - @test_skip begin - function hello(N) - println("Hello World $N") - N - end - - hello_compiled, path = compile(hello, (Int,)) - @test_skip remote_load_call(path, 1) == 1 - end -end - -# This is a trick to get stack allocated arrays inside a function body (so long as they don't escape). -# This lets us have intermediate, mutable stack allocated arrays inside our -@testset "Alloca" begin - function f(N) - # this can hold at most 100 Int values, if you use it for more, you'll segfault - buf = ManualMemory.MemoryBuffer{100, Int}(undef) - GC.@preserve buf begin - # wrap the first N values in a PtrArray - arr = PtrArray(pointer(buf), (N,)) - arr .= 1 # mutate the array to be all 1s - sum(arr) # compute the sum. It is very imporatant that no references to arr escape the function body - end - end - _, path = compile(f, (Int,)) - @test remote_load_call(path, 20) == 20 -end - -# I can't beleive this works. -@testset "LoopVectorization" begin - function mul!(C, A, B) - # note: @tturbo does NOT work - @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) - Cmn = zero(eltype(C)) - for k ∈ indices((A,B), (2,1)) - Cmn += A[m,k] * B[k,n] - end - C[m,n] = Cmn - end - end - - C = Array{Float64}(undef, 10, 12) - A = rand(10, 11) - B = rand(11, 12) - - _, path = compile(mul!, (Matrix{Float64}, Matrix{Float64}, Matrix{Float64},)) - # remote_load_call(path, C, A, B) This won't work because @spawnat copies C - C .= fetch(@spawnat 2 (load_function(path)(C, A, B); C)) - @test C ≈ A*B -end - @testset "Standalone Dylibs" begin # Test function # (already defined) @@ -349,3 +124,35 @@ end @test ccall(fptr, Float64, (Float64,), 10.) == squaresquaresquare(10.) #Compile dylib end + + +# Overlays + +module SubFoo + +rand(args...) = Base.rand(args...) + +function f() + x = rand() + y = rand() + return x + y +end + +end + +@device_override SubFoo.rand() = 2 + +# Lets test having another method table around +Base.Experimental.@MethodTable AnotherTable +Base.Experimental.@overlay AnotherTable SubFoo.rand() = 3 + +@testset "Overlays" begin + Libdl.dlopen(compile_shlib(SubFoo.f, (), workdir)) do lib + fptr = Libdl.dlsym(lib, "f") + @test @ccall($fptr()::Int) == 4 + end + Libdl.dlopen(compile_shlib(SubFoo.f, (), workdir; method_table=AnotherTable)) do lib + fptr = Libdl.dlsym(lib, "f") + @test @ccall($fptr()::Int) == 6 + end +end diff --git a/test/testintegration.jl b/test/testintegration.jl index ba4d7851..3422e63b 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -3,6 +3,34 @@ testpath = pwd() scratch = tempdir() cd(scratch) +if VERSION >= v"1.9" + # Bumper uses PackageExtensions to work with StaticCompiler, so let's just skip this test on 1.8 + function bumper_test(N::Int) + buf = AllocBuffer(MallocVector, sizeof(Float64) * N) + s = 0.0 + for i ∈ 1:N + # some excuse to reuse the same memory a bunch of times + @no_escape buf begin + v = @alloc(Float64, N) + v .= i + s += sum(v) + end + end + free(buf) + s + end + + @testset "Bumper.jl integration" begin + + path = compile_shlib(bumper_test, (Int,), "./") + ptr = Libdl.dlopen(path, Libdl.RTLD_LOCAL) + + fptr = Libdl.dlsym(ptr, "bumper_test") + + @test bumper_test(8) == @ccall($fptr(8::Int)::Float64) + end +end + @testset "Standalone Executable Integration" begin jlpath = joinpath(Sys.BINDIR, Base.julia_exename()) # Get path to julia executable @@ -105,7 +133,7 @@ cd(scratch) @warn "Could not compile $testpath/scripts/randn_matrix.jl" println(e) end - @static if Sys.isbsd() + if Sys.isbsd() @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 end @@ -119,14 +147,14 @@ cd(scratch) @warn "Could not run $(scratch)/randn_matrix" println(e) end - @static if Sys.isbsd() + if Sys.isbsd() @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 end end ## --- Test LoopVectorization integration - @static if LoopVectorization.VectorizationBase.has_feature(Val{:x86_64_avx2}) + if Bool(LoopVectorization.VectorizationBase.has_feature(Val{:x86_64_avx2})) let # Compile... status = -1 @@ -280,119 +308,33 @@ cd(scratch) ## --- Test interop - @static if Sys.isbsd() - let - # Compile... - status = -1 - try - isfile("interop") && rm("interop") - status = run(`$jlpath --startup=no --compile=min $testpath/scripts/interop.jl`) - catch e - @warn "Could not compile $testpath/scripts/interop.jl" - println(e) - end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 - - # Run... - println("Interop:") - status = -1 - try - status = run(`./interop`) - catch e - @warn "Could not run $(scratch)/interop" - println(e) - end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 - end - end - -end - -# Mixtape - -module SubFoo - -function f() - x = rand() - y = rand() - return x + y -end - -function stringfun(s1, s2) - return s1 * s2 -end - -function teststring() - return stringfun("ab", "c") == "abc" -end - -end - -struct MyMix <: CompilationContext end - -@testset "Mixtape" begin - # 101: How2Mix - - # A few little utility functions for working with Expr instances. - swap(e) = e - function swap(e::Expr) - new = MacroTools.postwalk(e) do s - isexpr(s, :call) || return s - s.args[1] == Base.rand || return s - return 4 - end - return new - end - - # This is pre-inference - you get to see a CodeInfoTools.Builder instance. - function StaticCompiler.transform(::MyMix, src) - b = CodeInfoTools.Builder(src) - for (v, st) in b - b[v] = swap(st) - end - return CodeInfoTools.finish(b) - end + if Sys.isbsd() + let + # Compile... + status = -1 + try + isfile("interop") && rm("interop") + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/interop.jl`) + catch e + @warn "Could not compile $testpath/scripts/interop.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 - # MyMix will only transform functions which you explicitly allow. - # You can also greenlight modules. - StaticCompiler.allow(ctx::MyMix, m::Module) = m == SubFoo - - _, path = compile(SubFoo.f, (), mixtape = MyMix()) - @test load_function(path)() == 8 - @test SubFoo.f() != 8 - - # redefine swap to test caching and add StaticString substitution - function swap(e::Expr) - new = MacroTools.postwalk(e) do s - s isa String && return StaticTools.StaticString(tuple(codeunits(s)..., 0x00)) - isexpr(s, :call) || return s - s.args[1] == Base.rand || return s - return 2 + # Run... + println("Interop:") + status = -1 + try + status = run(`./interop`) + catch e + @warn "Could not run $(scratch)/interop" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 end - return new end - _, path = compile(SubFoo.f, (), mixtape = MyMix()) - @test load_function(path)() == 4 - - _, path = compile(SubFoo.teststring, (), mixtape = MyMix()) - @test load_function(path)() - -end - -@testset "Cross compiling to WebAssembly" begin - testpath = pwd() - scratch = tempdir() - cd(scratch) - - m2(x) = 2x - m3(x) = 3x - wasm_path = compile_wasm(m2, Tuple{Float64}) - wasm_path2 = compile_wasm([(m2, Tuple{Float64}), (m3, Tuple{Float64})]) - - wasm_path = compile_wasm(m2, (Float64,)) - wasm_path2 = compile_wasm([(m2, (Float64,)), (m3, (Float64,))]) end From b66e19139b25f831b9495dad8e12cbd4225a55e9 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sat, 11 Nov 2023 23:04:24 +0100 Subject: [PATCH 135/202] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d4816cb9..d7c4ed0a 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ julia> f(1) # Gives the expected answer in regular julia julia> dlopen(compile_shlib(f, (Int,), "./")) do lib fptr = dlsym(lib, "f") - # Now use the compiled version where + is replaced with - + # Now use the compiled version where g(x) = 2x is replaced with g(x) = x - 10 @ccall $fptr(1::Int)::Int end -8 From ae99357b65ad9c63e46858a07c5e919bee73ff6e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:50:27 +0100 Subject: [PATCH 136/202] CompatHelper: bump compat for GPUCompiler to 0.25, (keep existing compat) (#147) * CompatHelper: bump compat for GPUCompiler to 0.25, (keep existing compat) * trigger CI --------- Co-authored-by: CompatHelper Julia Co-authored-by: Mason Protter --- Project.toml | 2 +- src/StaticCompiler.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 4b32fd7b..ac268d6d 100644 --- a/Project.toml +++ b/Project.toml @@ -17,7 +17,7 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] CodeInfoTools = "0.3" -GPUCompiler = "0.21, 0.22, 0.23, 0.24" +GPUCompiler = "0.21, 0.22, 0.23, 0.24, 0.25" LLVM = "6" MacroTools = "0.5" StaticTools = "0.8" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index a44af51d..41995f31 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -98,6 +98,7 @@ Hello, world! function compile_executable(f::Function, types=(), path::String="./", name=fix_name(f); also_expose=Tuple{Function, Tuple{DataType}}[], kwargs...) + compile_executable(vcat([(f, types)], also_expose), path, name; kwargs...) end From 808c9f9fb786b5f9757f543c92d4820909820a86 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Mon, 13 Nov 2023 12:51:20 +0100 Subject: [PATCH 137/202] bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ac268d6d..6be72b4c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.6" +version = "0.6.1" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" From b11a0f78d5a61f4cf8f21821d40e7db0a08d1b66 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 14 Nov 2023 08:18:08 +0100 Subject: [PATCH 138/202] Delete src/mixtape.jl --- src/mixtape.jl | 77 -------------------------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 src/mixtape.jl diff --git a/src/mixtape.jl b/src/mixtape.jl deleted file mode 100644 index 5e6098a4..00000000 --- a/src/mixtape.jl +++ /dev/null @@ -1,77 +0,0 @@ - -##### -##### Exports -##### - -export CompilationContext, - NoContext, - allow, - transform - -##### -##### Compilation context -##### - -# User-extended context allows parametrization of the pipeline through -# our subtype of AbstractInterpreter -abstract type CompilationContext end - -struct NoContext <: CompilationContext end - -@doc( -""" - abstract type CompilationContext end - -Parametrize the Mixtape pipeline by inheriting from `CompilationContext`. Similar to the context objects in [Cassette.jl](https://julia.mit.edu/Cassette.jl/stable/contextualdispatch.html). By using the interface methods [`transform`](@ref) and [`optimize!`](@ref) -- the user can control different parts of the compilation pipeline. -""", CompilationContext) - -transform(ctx::CompilationContext, b) = b -transform(ctx::CompilationContext, b, sig) = transform(ctx, b) - -@doc( -""" - transform(ctx::CompilationContext, b::Core.CodeInfo)::Core.CodeInfo - transform(ctx::CompilationContext, b::Core.CodeInfo, sig::Tuple)::Core.CodeInfo - -User-defined transform which operates on lowered `Core.CodeInfo`. There's two versions: (1) ignores the signature of the current method body under consideration and (2) provides the signature as `sig`. - -Transforms might typically follow a simple "swap" format using `CodeInfoTools.Builder`: - -```julia -function transform(::MyCtx, src) - b = CodeInfoTools.Builder(b) - for (k, st) in b - b[k] = swap(st)) - end - return CodeInfoTools.finish(b) -end -``` - -but more advanced formats are possible. For further utilities, please see [CodeInfoTools.jl](https://github.com/JuliaCompilerPlugins/CodeInfoTools.jl). -""", transform) - - -allow(f::C, args...) where {C <: CompilationContext} = false -function allow(ctx::CompilationContext, mod::Module, fn, args...) - return allow(ctx, mod) || allow(ctx, fn, args...) -end - -@doc( -""" - allow(f::CompilationContext, args...)::Bool - -Determines whether the user-defined [`transform`](@ref) and [`optimize!`](@ref) are allowed to look at a lowered `Core.CodeInfo` or `Core.Compiler.IRCode` instance. - -The user is allowed to greenlight modules: - -```julia -allow(::MyCtx, m::Module) == m == SomeModule -``` - -or even specific signatures - -```julia -allow(::MyCtx, fn::typeof(rand), args...) = true -``` -""", allow) - From 34ef8bbcaa507368a630f5a4ac297e1d40366754 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Thu, 16 Nov 2023 09:18:58 +0100 Subject: [PATCH 139/202] Add some device overrides for Bumper.jl (#149) * Update quirks.jl * Update Project.toml --- Project.toml | 2 +- src/quirks.jl | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6be72b4c..4be53224 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.6.1" +version = "0.6.2" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" diff --git a/src/quirks.jl b/src/quirks.jl index ffa25ac0..65ebc7b4 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -44,3 +44,15 @@ end # trig.jl @device_override @noinline Base.Math.sincos_domain_error(x) = @print_and_throw c"sincos(x) is only defined for finite x." + +@static if isdefined(StaticTools, :Bumper) + Bumper = StaticTools.Bumper + @device_override @noinline Bumper.AllocBufferImpl.oom_error() = + @print_and_throw c"alloc: Buffer out of memory. This might be a sign of a memory leak." + @device_override @noinline Bumper.Internals.esc_err() = + @print_and_throw c"Tried to return a PtrArray from a `no_escape` block. If you really want to do this, evaluate Bumper.allow_ptrarray_to_escape() = true" + + # Just to make the compiler's life a little easier, let's not make it fetch and elide the current task + # since tasks don't actually exist on-device. + @device_override Bumper.Internals.get_task() = 0 +end From 8c46be1e7a821271d4146ffaa2b8ad86e8bdd25e Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Tue, 28 Nov 2023 17:00:50 -0300 Subject: [PATCH 140/202] Refactor code to make it more amenable to cross compilation Also cleanup some of it --- src/StaticCompiler.jl | 77 +++++++++++++++------------ src/target.jl | 114 ++++++++++++++++++++++++---------------- test/scripts/interop.jl | 2 +- 3 files changed, 111 insertions(+), 82 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 41995f31..6bca325e 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -12,9 +12,10 @@ using LLD_jll: lld using StaticTools using StaticTools: @symbolcall, @c_str, println using Core: MethodTable +using Base:BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform, BinaryPlatforms.arch, BinaryPlatforms.os, BinaryPlatforms.libc_str export load_function, compile_shlib, compile_executable -export native_code_llvm, native_code_typed, native_llvm_module, native_code_native +export static_code_llvm, static_code_typed, static_llvm_module, static_code_native export @device_override, @print_and_throw include("interpreter.jl") @@ -32,6 +33,7 @@ compile_executable(f::Function, types::Tuple, path::String, [name::String=string filename::String=name, cflags=``, # Specify libraries you would like to link against, and other compiler options here also_expose=[], + target::StaticTarget=StaticTarget(), method_table=StaticCompiler.method_table, kwargs... ) @@ -96,16 +98,16 @@ Hello, world! ``` """ function compile_executable(f::Function, types=(), path::String="./", name=fix_name(f); - also_expose=Tuple{Function, Tuple{DataType}}[], + also_expose=Tuple{Function, Tuple{DataType}}[], target::StaticTarget=StaticTarget(), kwargs...) - - compile_executable(vcat([(f, types)], also_expose), path, name; 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))); filename = name, demangle = true, cflags = ``, + target::StaticTarget=StaticTarget(), kwargs... ) @@ -114,12 +116,12 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String="./", name=f isexecutableargtype = tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" - rt = last(only(native_code_typed(f, tt; kwargs...))) + rt = last(only(static_code_typed(f, tt; target, kwargs...))) isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") 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, kwargs...) + generate_executable(funcs, path, name, filename; demangle, cflags, target, kwargs...) joinpath(abspath(path), filename) end @@ -129,6 +131,7 @@ compile_shlib(f::Function, types::Tuple, [path::String="./"], [name::String=stri filename::String=name, cflags=``, method_table=StaticCompiler.method_table, + target::StaticTarget=StaticTarget(), kwargs...) compile_shlib(funcs::Array, [path::String="./"]; @@ -136,6 +139,7 @@ compile_shlib(funcs::Array, [path::String="./"]; demangle=true, cflags=``, method_table=StaticCompiler.method_table, + target::StaticTarget=StaticTarget(), kwargs...) ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. @@ -169,15 +173,17 @@ julia> ccall(("test", "test.dylib"), Float64, (Int64,), 100_000) """ function compile_shlib(f::Function, types=(), path::String="./", name=fix_name(f); filename=name, + target::StaticTarget=StaticTarget(), kwargs... ) - compile_shlib(((f, types),), path; filename, 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="./"; filename = "libfoo", demangle = true, cflags = ``, + target::StaticTarget=StaticTarget(), kwargs... ) for func in funcs @@ -185,17 +191,17 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String="./"; tt = Base.to_tuple_type(types) isconcretetype(tt) || error("input type signature `$types` is not concrete") - rt = last(only(native_code_typed(f, tt))) + rt = last(only(static_code_typed(f, tt; target, kwargs...))) isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") 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!" end - generate_shlib(funcs, true, path, filename; demangle, cflags, kwargs...) + generate_shlib(funcs, path, filename; demangle, cflags, target, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end - + """ ```julia @@ -281,12 +287,11 @@ generate_executable(f, tt, args...; kwargs...) = generate_executable(((f, tt),), function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fix_name(first(first(funcs))), filename=name; demangle = true, cflags = ``, + target::StaticTarget=StaticTarget(), kwargs... ) - lib_path = joinpath(path, "$filename.$(Libdl.dlext)") exec_path = joinpath(path, filename) - external = true - _, obj_path = generate_obj(funcs, external, path, filename; demangle, kwargs...) + _, obj_path = generate_obj(funcs, path, filename; demangle, target, kwargs...) # Pick a compiler cc = Sys.isapple() ? `cc` : clang() # Compile! @@ -318,8 +323,8 @@ end """ ```julia -generate_shlib(f::Function, tt, [external::Bool=true], [path::String], [name], [filename]; kwargs...) -generate_shlib(funcs::Array, [external::Bool=true], [path::String], [filename::String]; demangle=true, kwargs...) +generate_shlib(f::Function, tt, [path::String], [name], [filename]; kwargs...) +generate_shlib(funcs::Array, [path::String], [filename::String]; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) ``` Low level interface for compiling a shared object / dynamically loaded library (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing @@ -356,19 +361,20 @@ julia> ccall(("test", "example/test.dylib"), Float64, (Int64,), 100_000) 5.2564961094956075 ``` """ -function generate_shlib(f::Function, tt, external::Bool=true, path::String=tempname(), name=fix_name(f), filename=name; kwargs...) - generate_shlib(((f, tt),), external, path, filename; kwargs...) +function generate_shlib(f::Function, tt, path::String=tempname(), name=fix_name(f), filename=name; target=StaticTarget(), kwargs...) + generate_shlib(((f, tt),), path, filename; target, kwargs...) end # As above, but taking an array of functions and returning a single shlib -function generate_shlib(funcs::Union{Array,Tuple}, external::Bool=true, path::String=tempname(), filename::String="libfoo"; +function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), filename::String="libfoo"; demangle = true, cflags = ``, + target::StaticTarget=StaticTarget(), kwargs... ) lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - _, obj_path = generate_obj(funcs, external, path, filename; demangle, kwargs...) + _, obj_path = generate_obj(funcs, path, filename; target, demangle, kwargs...) # Pick a Clang cc = Sys.isapple() ? `cc` : clang() # Compile! @@ -377,27 +383,27 @@ function generate_shlib(funcs::Union{Array,Tuple}, external::Bool=true, path::St path, name end -function native_code_llvm(@nospecialize(func), @nospecialize(types); kwargs...) - job, kwargs = native_job(func, types, true; kwargs...) +function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) + job, kwargs = static_job(func, types; target, kwargs...) GPUCompiler.code_llvm(stdout, job; kwargs...) end -function native_code_typed(@nospecialize(func), @nospecialize(types); kwargs...) - job, kwargs = native_job(func, types, true; kwargs...) +function static_code_typed(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) + job, kwargs = static_job(func, types; target, kwargs...) GPUCompiler.code_typed(job; kwargs...) end -function native_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); kwargs...) - job, kwargs = native_job(f, tt, true; fname, kwargs...) +function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), kwargs...) + job, kwargs = static_job(f, tt; fname, target, kwargs...) GPUCompiler.code_native(stdout, job; kwargs...) end # Return an LLVM module -function native_llvm_module(f, tt, name=fix_name(f); demangle, kwargs...) +function static_llvm_module(f, tt, name=fix_name(f); demangle, target::StaticTarget=StaticTarget(), kwargs...) if !demangle name = "julia_"*name end - job, kwargs = native_job(f, tt, true; name, kwargs...) + job, kwargs = static_job(f, tt; name, target, kwargs...) m = GPUCompiler.JuliaContext() do context m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) locate_pointers_and_runtime_calls(m) @@ -407,14 +413,14 @@ function native_llvm_module(f, tt, name=fix_name(f); demangle, kwargs...) end #Return an LLVM module for multiple functions -function native_llvm_module(funcs::Union{Array,Tuple}; demangle=true, kwargs...) +function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) f,tt = funcs[1] mod = GPUCompiler.JuliaContext() do context name_f = fix_name(f) if !demangle name_f = "julia_"*name_f end - job, kwargs = native_job(f, tt, true; name = name_f, kwargs...) + job, kwargs = static_job(f, tt; name = name_f, target, kwargs...) mod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) if length(funcs) > 1 for func in funcs[2:end] @@ -423,7 +429,7 @@ function native_llvm_module(funcs::Union{Array,Tuple}; demangle=true, kwargs...) if !demangle name_f = "julia_"*name_f end - job, kwargs = native_job(f, tt, true; name = name_f, kwargs...) + job, kwargs = static_job(f, tt; name = name_f, target, kwargs...) tmod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) link!(mod,tmod) end @@ -458,7 +464,7 @@ end """ ```julia -generate_obj(f, tt, external::Bool, path::String = tempname(), filenamebase::String="obj"; +generate_obj(f, tt, path::String = tempname(), filenamebase::String="obj"; target = (), demangle = true, strip_llvm = false, @@ -498,7 +504,7 @@ end """ ```julia -generate_obj(funcs::Union{Array,Tuple}, external::Bool, path::String = tempname(), filenamebase::String="obj"; +generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; target = (), demangle =false, strip_llvm = false, @@ -514,18 +520,19 @@ which will be compiled. This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). The defaults compile to the native target. """ -function generate_obj(funcs::Union{Array,Tuple}, external::Bool, path::String = tempname(), filenamebase::String="obj"; +function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; demangle = true, 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 = native_llvm_module(funcs; demangle, kwargs...) + mod = static_llvm_module(funcs; demangle, kwargs...) obj = GPUCompiler.JuliaContext() do ctx - fakejob, _ = native_job(f, tt, external; kwargs...) + fakejob, _ = static_job(f, tt; target, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod; strip=strip_asm, validate=false, format=LLVM.API.LLVMObjectFile) obj end diff --git a/src/target.jl b/src/target.jl index 777960a3..2f14e2b1 100644 --- a/src/target.jl +++ b/src/target.jl @@ -4,6 +4,31 @@ else const method_table = nothing end +""" +```julia + StaticTarget() # Native target + StaticTarget(platform::Base.BinaryPlatforms.Platform) # Specific target with generic CPU + StaticTarget(platform::Platform, cpu::String) # Specific target with specific CPU + StaticTarget(platform::Platform, cpu::String, features::String) # Specific target with specific CPU and features +``` +Struct that defines a target for the compilation +Beware that currently the compilation assumes that the code is on the host so platform specific code like: +```julia + Sys.isapple() ... +``` +does not behave as expected. +By default `StaticTarget()` is the native target. +""" +struct StaticTarget + platform::Platform + tm::LLVM.TargetMachine +end + +clean_triple(platform::Platform) = arch(platform) * "-" * os(platform) * libc_str(platform) +StaticTarget() = StaticTarget(HostPlatform(), unsafe_string(LLVM.API.LLVMGetHostCPUName()), unsafe_string(LLVM.API.LLVMGetHostCPUFeatures())) +StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform))) +StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu)) +StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu, features)) """ ```julia @@ -29,18 +54,12 @@ macro device_override(ex) return esc(code) end -Base.@kwdef struct NativeCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget - triple::String=Sys.MACHINE - cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) - features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) - method_table::MT = method_table -end - -Base.@kwdef struct ExternalNativeCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget - triple::String=Sys.MACHINE - cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName()) - features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures()) - method_table::MT = method_table +# Default to native +struct StaticCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget + triple::String + cpu::String + features::String + method_table::MT end module StaticRuntime @@ -53,58 +72,61 @@ module StaticRuntime report_exception_frame(idx, func, file, line) = return end -for target in (:NativeCompilerTarget, :ExternalNativeCompilerTarget) - @eval begin - GPUCompiler.llvm_triple(target::$target) = target.triple - function GPUCompiler.llvm_machine(target::$target) - triple = GPUCompiler.llvm_triple(target) +GPUCompiler.llvm_triple(target::StaticCompilerTarget) = target.triple - t = LLVM.Target(triple=triple) +function GPUCompiler.llvm_machine(target::StaticCompilerTarget) + triple = GPUCompiler.llvm_triple(target) - tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) - GPUCompiler.asm_verbosity!(tm, true) + t = LLVM.Target(triple=triple) - return tm - end + tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) + GPUCompiler.asm_verbosity!(tm, true) - GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{<:$target}) = "native_$(job.config.target.cpu)-$(hash(job.config.target.features))" + return tm +end - GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:$target}) = StaticRuntime - GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:$target, StaticCompilerParams}) = StaticRuntime +GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = "static_$(job.config.target.cpu)-$(hash(job.config.target.features))" +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = StaticRuntime +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = StaticRuntime - GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:$target, StaticCompilerParams}) = true - GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:$target}) = true - GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{<:$target, StaticCompilerParams}) = - StaticInterpreter(job.config.params.cache, GPUCompiler.method_table(job), job.world, - GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job)) - GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{<:$target, StaticCompilerParams}) = job.config.params.cache - GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:$target})) = job.config.target.method_table - end -end +GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = true +GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = true + +GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = + StaticInterpreter(job.config.params.cache, GPUCompiler.method_table(job), job.world, + GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job)) +GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = job.config.params.cache +GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget})) = job.config.target.method_table -function native_job(@nospecialize(func::Function), @nospecialize(types::Type), external::Bool; + +function static_job(@nospecialize(func::Function), @nospecialize(types::Type); name = fix_name(func), kernel::Bool = false, - target = (;), + target::StaticTarget = StaticTarget(), method_table=method_table, kwargs... ) - target = merge(target, (;method_table)) source = methodinstance(typeof(func), Base.to_tuple_type(types)) - target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) + tm = target.tm + gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), method_table) params = StaticCompilerParams() - config = GPUCompiler.CompilerConfig(target, params, name = name, kernel = kernel) + config = GPUCompiler.CompilerConfig(gputarget, params, name = name, kernel = kernel) StaticCompiler.CompilerJob(source, config), kwargs end - -function native_job(@nospecialize(func), @nospecialize(types), external; kernel::Bool=false, name=fix_name(repr(func)), target = (;), method_table=method_table, kwargs...) - target = merge(target, (; method_table)) +function static_job(@nospecialize(func), @nospecialize(types); + name = fix_name(func), + kernel::Bool = false, + target::StaticTarget = StaticTarget(), + method_table=method_table, + kwargs... +) source = methodinstance(typeof(func), Base.to_tuple_type(types)) - target = external ? ExternalNativeCompilerTarget(;target...) : NativeCompilerTarget(;target...) + tm = target.tm + gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), method_table) params = StaticCompilerParams() - config = GPUCompiler.CompilerConfig(target, params, name = name, kernel = kernel) - GPUCompiler.CompilerJob(source, config), kwargs -end + config = GPUCompiler.CompilerConfig(gputarget, params, name = name, kernel = kernel) + StaticCompiler.CompilerJob(source, config), kwargs +end \ No newline at end of file diff --git a/test/scripts/interop.jl b/test/scripts/interop.jl index b601e090..24546ec1 100644 --- a/test/scripts/interop.jl +++ b/test/scripts/interop.jl @@ -13,4 +13,4 @@ function interop(argc, argv) end # Attempt to compile -path = compile_executable(interop, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-ldl -lm`) +path = compile_executable(interop, (Int64, Ptr{Ptr{UInt8}}), "./", c_flags=`-ldl -lm`) From 0e0f8722ab468e41392aa188fa0fa5bebb81d409 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Wed, 29 Nov 2023 11:13:13 -0300 Subject: [PATCH 141/202] Fixup macos issues --- src/StaticCompiler.jl | 4 ++-- src/target.jl | 2 +- test/scripts/interop.jl | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 6bca325e..2d67382b 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -12,7 +12,7 @@ using LLD_jll: lld using StaticTools using StaticTools: @symbolcall, @c_str, println using Core: MethodTable -using Base:BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform, BinaryPlatforms.arch, BinaryPlatforms.os, BinaryPlatforms.libc_str +using Base:BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform, BinaryPlatforms.arch, BinaryPlatforms.os_str, BinaryPlatforms.libc_str export load_function, compile_shlib, compile_executable export static_code_llvm, static_code_typed, static_llvm_module, static_code_native @@ -399,7 +399,7 @@ function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name( end # Return an LLVM module -function static_llvm_module(f, tt, name=fix_name(f); demangle, target::StaticTarget=StaticTarget(), kwargs...) +function static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) if !demangle name = "julia_"*name end diff --git a/src/target.jl b/src/target.jl index 2f14e2b1..26d5c697 100644 --- a/src/target.jl +++ b/src/target.jl @@ -24,7 +24,7 @@ struct StaticTarget tm::LLVM.TargetMachine end -clean_triple(platform::Platform) = arch(platform) * "-" * os(platform) * libc_str(platform) +clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform) StaticTarget() = StaticTarget(HostPlatform(), unsafe_string(LLVM.API.LLVMGetHostCPUName()), unsafe_string(LLVM.API.LLVMGetHostCPUFeatures())) StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform))) StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu)) diff --git a/test/scripts/interop.jl b/test/scripts/interop.jl index 24546ec1..422f682a 100644 --- a/test/scripts/interop.jl +++ b/test/scripts/interop.jl @@ -13,4 +13,4 @@ function interop(argc, argv) end # Attempt to compile -path = compile_executable(interop, (Int64, Ptr{Ptr{UInt8}}), "./", c_flags=`-ldl -lm`) +path = compile_executable(interop, (Int64, Ptr{Ptr{UInt8}}), "./",cflags=`-ldl -lm`) From 0d537cdadafa9236d7838ea9854ccd9e68a92f53 Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Wed, 29 Nov 2023 12:09:47 -0300 Subject: [PATCH 142/202] Add wasm test and fixup macos --- src/StaticCompiler.jl | 11 ++++++----- src/target.jl | 3 ++- test/scripts/wasm.jl | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 test/scripts/wasm.jl diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 2d67382b..b84cc2f8 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -17,6 +17,7 @@ using Base:BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform, BinaryPlatfor export load_function, compile_shlib, compile_executable export static_code_llvm, static_code_typed, static_llvm_module, static_code_native export @device_override, @print_and_throw +export StaticTarget include("interpreter.jl") include("target.jl") @@ -465,7 +466,7 @@ end """ ```julia generate_obj(f, tt, path::String = tempname(), filenamebase::String="obj"; - target = (), + target::StaticTarget=StaticTarget(), demangle = true, strip_llvm = false, strip_asm = true, @@ -477,7 +478,7 @@ a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. `target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. -This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). +This is a struct of the type StaticTarget() The defaults compile to the native target. If `demangle` is set to `false`, compiled function names are prepended with "julia_". @@ -487,7 +488,7 @@ If `demangle` is set to `false`, compiled function names are prepended with "jul julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) fib (generic function with 1 method) -julia> path, name, table = StaticCompiler.generate_obj_for_compile(fib, Tuple{Int64}, "./test") +julia> path, name, table = StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test") ("./test", "fib", IdDict{Any, String}()) shell> tree \$path @@ -505,7 +506,7 @@ end """ ```julia generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; - target = (), + target::StaticTarget=StaticTarget(), demangle =false, strip_llvm = false, strip_asm = true, @@ -517,7 +518,7 @@ Low level interface for compiling object code (`.o`) for an array of Tuples which will be compiled. `target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. -This is a named tuple with fields `triple`, `cpu`, and `features` (each of these are strings). +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"; diff --git a/src/target.jl b/src/target.jl index 26d5c697..3d545637 100644 --- a/src/target.jl +++ b/src/target.jl @@ -20,7 +20,7 @@ does not behave as expected. By default `StaticTarget()` is the native target. """ struct StaticTarget - platform::Platform + platform::Union{Platform,Nothing} tm::LLVM.TargetMachine end @@ -29,6 +29,7 @@ StaticTarget() = StaticTarget(HostPlatform(), unsafe_string(LLVM.API.LLVMGetHost StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform))) StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu)) StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu, features)) +StaticTarget(triple::String, cpu::String, features::String) = StaticTarget(nothing, LLVM.TargetMachine(LLVM.Target(triple = triple), triple, cpu, features)) """ ```julia diff --git a/test/scripts/wasm.jl b/test/scripts/wasm.jl new file mode 100644 index 00000000..9222660a --- /dev/null +++ b/test/scripts/wasm.jl @@ -0,0 +1,16 @@ +# Test that we can compile an object to wasm +# WebAssemblyCompiler.jl is a better tool for this, but this exercises the cross compilation pipeline + +using StaticCompiler +using LLVM +InitializeAllTargets() +InitializeAllTargetInfos() +InitializeAllAsmPrinters() +InitializeAllAsmParsers() +InitializeAllTargetMCs() + +fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) + +target=StaticTarget("wasm32","","") + +StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test", target=target) \ No newline at end of file From bf0740bf858d2ad025e9a06bbcdad6a14c60992d Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Wed, 29 Nov 2023 12:11:05 -0300 Subject: [PATCH 143/202] Whitespace --- test/scripts/interop.jl | 2 +- test/scripts/wasm.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/scripts/interop.jl b/test/scripts/interop.jl index 422f682a..b601e090 100644 --- a/test/scripts/interop.jl +++ b/test/scripts/interop.jl @@ -13,4 +13,4 @@ function interop(argc, argv) end # Attempt to compile -path = compile_executable(interop, (Int64, Ptr{Ptr{UInt8}}), "./",cflags=`-ldl -lm`) +path = compile_executable(interop, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-ldl -lm`) diff --git a/test/scripts/wasm.jl b/test/scripts/wasm.jl index 9222660a..beff3b7d 100644 --- a/test/scripts/wasm.jl +++ b/test/scripts/wasm.jl @@ -13,4 +13,4 @@ fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) target=StaticTarget("wasm32","","") -StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test", target=target) \ No newline at end of file +StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test", target=target) From 2bf702662d1bce18d925a0d4130602dfd7b52de1 Mon Sep 17 00:00:00 2001 From: gbaraldi Date: Tue, 5 Dec 2023 13:15:18 -0500 Subject: [PATCH 144/202] Add way to set compiler for cross target and also bump major version --- Project.toml | 2 +- src/StaticCompiler.jl | 22 +++++++++++++++++----- src/target.jl | 29 ++++++++++++++++++++++++----- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/Project.toml b/Project.toml index 4be53224..8daa749f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.6.2" +version = "0.7.0" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index b84cc2f8..e0fc69c6 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -13,7 +13,7 @@ using StaticTools using StaticTools: @symbolcall, @c_str, println using Core: MethodTable using Base:BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform, BinaryPlatforms.arch, BinaryPlatforms.os_str, BinaryPlatforms.libc_str - +using Base:BinaryPlatforms.platform_dlext export load_function, compile_shlib, compile_executable export static_code_llvm, static_code_typed, static_llvm_module, static_code_native export @device_override, @print_and_throw @@ -294,7 +294,12 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi exec_path = joinpath(path, filename) _, obj_path = generate_obj(funcs, path, filename; demangle, target, kwargs...) # Pick a compiler - cc = Sys.isapple() ? `cc` : clang() + if !isnothing(target.compiler) + cc = `$(target.compiler)` + else + cc = Sys.isapple() ? `cc` : clang() + end + # Compile! if Sys.isapple() # Apple no longer uses _start, so we can just specify a custom entry @@ -372,12 +377,19 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file target::StaticTarget=StaticTarget(), kwargs... ) - - lib_path = joinpath(path, "$filename.$(Libdl.dlext)") + if !isnothing(target.platform) + lib_path = joinpath(path, "$filename.$(platform_dlext(target.platform))") + else + lib_path = joinpath(path, "$filename.$(Libdl.dlext)") + end _, obj_path = generate_obj(funcs, path, filename; target, demangle, kwargs...) # Pick a Clang - cc = Sys.isapple() ? `cc` : clang() + if !isnothing(target.compiler) + cc = `$(target.compiler)` + else + cc = Sys.isapple() ? `cc` : clang() + end # Compile! run(`$cc -shared $cflags $obj_path -o $lib_path `) diff --git a/src/target.jl b/src/target.jl index 3d545637..df53b9c2 100644 --- a/src/target.jl +++ b/src/target.jl @@ -18,18 +18,37 @@ Beware that currently the compilation assumes that the code is on the host so pl ``` does not behave as expected. By default `StaticTarget()` is the native target. + +For cross-compilation of executables and shared libraries, one also needs to call `set_compiler!` with the path to a valid C compiler +for the target platform. For example, to cross-compile for aarch64 using a compiler from homebrew, one can use: +```julia + set_compiler!(StaticTarget(parse(Platform,"aarch64-gnu-linux")), "/opt/homebrew/bin/aarch64-unknown-linux-gnu-gcc") +``` """ -struct StaticTarget +mutable struct StaticTarget platform::Union{Platform,Nothing} tm::LLVM.TargetMachine + compiler::Union{String,Nothing} end clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform) StaticTarget() = StaticTarget(HostPlatform(), unsafe_string(LLVM.API.LLVMGetHostCPUName()), unsafe_string(LLVM.API.LLVMGetHostCPUFeatures())) -StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform))) -StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu)) -StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu, features)) -StaticTarget(triple::String, cpu::String, features::String) = StaticTarget(nothing, LLVM.TargetMachine(LLVM.Target(triple = triple), triple, cpu, features)) +StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform)), nothing) +StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu), nothing) +StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu, features), nothing) + +function StaticTarget(triple::String, cpu::String, features::String) + platform = tryparse(Platform, triple) + StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = triple), triple, cpu, features), nothing) +end + +""" +Set the compiler for cross compilation + ```julia + set_compiler!(StaticTarget(parse(Platform,"aarch64-gnu-linux")), "/opt/homebrew/bin/aarch64-elf-gcc") +``` +""" +set_compiler!(target::StaticTarget, compiler::String) = (target.compiler = compiler) """ ```julia From ac2d56f4d91962193b5ef809e27e428b1d225fef Mon Sep 17 00:00:00 2001 From: gbaraldi Date: Tue, 5 Dec 2023 13:25:39 -0500 Subject: [PATCH 145/202] Bump Bumper :) --- test/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Project.toml b/test/Project.toml index 5498846c..a36e2086 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -16,4 +16,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e" [compat] -Bumper = "0.5.1" \ No newline at end of file +Bumper = "0.6" \ No newline at end of file From e3412cb6be4766ef08c328e24f9b892085e4a804 Mon Sep 17 00:00:00 2001 From: gbaraldi Date: Tue, 5 Dec 2023 16:02:30 -0500 Subject: [PATCH 146/202] Prepare code for future endeavours into adding the GC --- src/StaticCompiler.jl | 10 +++++----- src/target.jl | 16 +++++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index e0fc69c6..7277b2f0 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -398,7 +398,7 @@ end function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) job, kwargs = static_job(func, types; target, kwargs...) - GPUCompiler.code_llvm(stdout, job; kwargs...) + GPUCompiler.code_llvm(stdout, job; libraries=false, kwargs...) end function static_code_typed(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) @@ -408,7 +408,7 @@ end function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), kwargs...) job, kwargs = static_job(f, tt; fname, target, kwargs...) - GPUCompiler.code_native(stdout, job; kwargs...) + GPUCompiler.code_native(stdout, job; libraries=false, kwargs...) end # Return an LLVM module @@ -418,7 +418,7 @@ function static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::Stat end job, kwargs = static_job(f, tt; name, target, kwargs...) m = GPUCompiler.JuliaContext() do context - m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, libraries=false) locate_pointers_and_runtime_calls(m) m end @@ -434,7 +434,7 @@ function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::St name_f = "julia_"*name_f end job, kwargs = static_job(f, tt; name = name_f, target, kwargs...) - mod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + mod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, libraries=false) if length(funcs) > 1 for func in funcs[2:end] f,tt = func @@ -443,7 +443,7 @@ function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::St name_f = "julia_"*name_f end job, kwargs = static_job(f, tt; name = name_f, target, kwargs...) - tmod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false) + tmod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, libraries=false) link!(mod,tmod) end end diff --git a/src/target.jl b/src/target.jl index df53b9c2..5faec0d1 100644 --- a/src/target.jl +++ b/src/target.jl @@ -29,13 +29,14 @@ mutable struct StaticTarget platform::Union{Platform,Nothing} tm::LLVM.TargetMachine compiler::Union{String,Nothing} + julia_runtime::Bool end clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform) StaticTarget() = StaticTarget(HostPlatform(), unsafe_string(LLVM.API.LLVMGetHostCPUName()), unsafe_string(LLVM.API.LLVMGetHostCPUFeatures())) -StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform)), nothing) -StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu), nothing) -StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu, features), nothing) +StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform)), nothing, false) +StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu), nothing, false) +StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu, features), nothing, false) function StaticTarget(triple::String, cpu::String, features::String) platform = tryparse(Platform, triple) @@ -50,6 +51,9 @@ Set the compiler for cross compilation """ set_compiler!(target::StaticTarget, compiler::String) = (target.compiler = compiler) + +set_runtime!(target::StaticTarget, julia_runtime::Bool) = (target.julia_runtime = julia_runtime) + """ ```julia @device_override old_bad_method(arg1::Type1, arg2::Type2) = new_good_method(arg1, arg2) @@ -79,6 +83,7 @@ struct StaticCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget triple::String cpu::String features::String + julia_runtime::Bool method_table::MT end @@ -115,6 +120,7 @@ GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget, Sta GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = true GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = true +GPUCompiler.uses_julia_runtime(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = job.config.target.julia_runtime GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = StaticInterpreter(job.config.params.cache, GPUCompiler.method_table(job), job.world, GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job)) @@ -131,7 +137,7 @@ function static_job(@nospecialize(func::Function), @nospecialize(types::Type); ) source = methodinstance(typeof(func), Base.to_tuple_type(types)) tm = target.tm - gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), method_table) + gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), target.julia_runtime, method_table) params = StaticCompilerParams() config = GPUCompiler.CompilerConfig(gputarget, params, name = name, kernel = kernel) StaticCompiler.CompilerJob(source, config), kwargs @@ -145,7 +151,7 @@ function static_job(@nospecialize(func), @nospecialize(types); ) source = methodinstance(typeof(func), Base.to_tuple_type(types)) tm = target.tm - gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), method_table) + gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), target.julia_runtime, method_table) params = StaticCompilerParams() config = GPUCompiler.CompilerConfig(gputarget, params, name = name, kernel = kernel) StaticCompiler.CompilerJob(source, config), kwargs From afd6f6623f71756cb2514cb3b177144ff05fcda7 Mon Sep 17 00:00:00 2001 From: CompatHelper Julia Date: Sun, 25 Feb 2024 00:58:26 +0000 Subject: [PATCH 147/202] CompatHelper: bump compat for GPUCompiler to 0.26, (keep existing compat) --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 4be53224..6eab8fb1 100644 --- a/Project.toml +++ b/Project.toml @@ -17,7 +17,7 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] CodeInfoTools = "0.3" -GPUCompiler = "0.21, 0.22, 0.23, 0.24, 0.25" +GPUCompiler = "0.21, 0.22, 0.23, 0.24, 0.25, 0.26" LLVM = "6" MacroTools = "0.5" StaticTools = "0.8" From a18f913f9a346804eb07e26baa0e483101bb7550 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Mon, 4 Mar 2024 09:47:18 -0500 Subject: [PATCH 148/202] Bump version to trigger CI --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6eab8fb1..6593f96a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.6.2" +version = "0.6.3" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" From e672f352352097108e3049383e1edfcf508c57af Mon Sep 17 00:00:00 2001 From: Thomas R Date: Mon, 4 Mar 2024 21:27:33 +0100 Subject: [PATCH 149/202] adaption to Windows (#151) * adaption to windows: use clang to generate executable, skip GPUCompiler * a number of simplifications and bugfixes --------- Co-authored-by: C. Brenhin Keller --- .github/workflows/ci.yml | 2 +- Project.toml | 2 +- src/StaticCompiler.jl | 70 +++++++++++++++++++++++++++------------- test/testcore.jl | 10 ++++++ 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3bbb9fd2..3dc141f9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: version: - '1.8' - '1.9' - - '1.10.0-rc1' + - '1.10' os: - ubuntu-latest - macOS-latest diff --git a/Project.toml b/Project.toml index 96c94ea6..4f41e3e2 100644 --- a/Project.toml +++ b/Project.toml @@ -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] diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 7277b2f0..38547fe5 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -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... ) @@ -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... ) @@ -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=``, @@ -172,7 +174,7 @@ 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... @@ -180,7 +182,7 @@ function compile_shlib(f::Function, types=(), path::String="./", name=fix_name(f 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 = ``, @@ -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)` @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/testcore.jl b/test/testcore.jl index e705ed64..064f0109 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -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((""" @@ -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) From c91dc620c01bebd79fc750b97bb51cc517bbfee4 Mon Sep 17 00:00:00 2001 From: Chris Elrod Date: Mon, 1 Apr 2024 13:09:05 -0400 Subject: [PATCH 150/202] `throw_overflowerr_negation` takes 1 argument --- src/quirks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/quirks.jl b/src/quirks.jl index 65ebc7b4..b5c8571b 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -25,7 +25,7 @@ end # checked.jl @device_override @noinline Base.Checked.throw_overflowerr_binaryop(op, x, y) = @print_and_throw c"Binary operation overflowed" -@device_override @noinline Base.Checked.throw_overflowerr_negation(op, x, y) = +@device_override @noinline Base.Checked.throw_overflowerr_negation(x) = @print_and_throw c"Negation overflowed" @device_override function Base.Checked.checked_abs(x::Base.Checked.SignedInt) r = ifelse(x < 0, -x, x) From 4c073c425a5408bd4f8618c13feb5571c52504f4 Mon Sep 17 00:00:00 2001 From: Chris Elrod Date: Mon, 1 Apr 2024 14:57:41 -0400 Subject: [PATCH 151/202] Similar update for `exp_domainerror` --- src/quirks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/quirks.jl b/src/quirks.jl index b5c8571b..f279d94e 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -9,7 +9,7 @@ end # math.jl @device_override @noinline Base.Math.throw_complex_domainerror(f::Symbol, x) = @print_and_throw c"This operation requires a complex input to return a complex result" -@device_override @noinline Base.Math.throw_exp_domainerror(f::Symbol, x) = +@device_override @noinline Base.Math.throw_exp_domainerror(x) = @print_and_throw c"Exponentiation yielding a complex result requires a complex argument" # intfuncs.jl From 72a90e14b6c28e06ec0941f9875c93e910676a6e Mon Sep 17 00:00:00 2001 From: Daizu Date: Sun, 19 May 2024 00:00:27 +0900 Subject: [PATCH 152/202] Add dllexport.jl --- src/StaticCompiler.jl | 30 ++++++++++++++++++++++++------ src/dllexport.jl | 11 +++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/dllexport.jl diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 38547fe5..90e34040 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -23,6 +23,7 @@ include("interpreter.jl") include("target.jl") include("pointer_warning.jl") include("quirks.jl") +include("dllexport.jl") fix_name(f::Function) = fix_name(string(nameof(f))) fix_name(s) = String(GPUCompiler.safe_name(s)) @@ -125,6 +126,7 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name= 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, llvm_to_clang, kwargs...) + Sys.iswindows() && (filename *= ".exe") joinpath(abspath(path), filename) end @@ -187,6 +189,7 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); demangle = true, cflags = ``, target::StaticTarget=StaticTarget(), + llvm_to_clang = Sys.iswindows(), kwargs... ) for func in funcs @@ -200,7 +203,7 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); 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!" end - generate_shlib(funcs, path, filename; demangle, cflags, target, kwargs...) + generate_shlib(funcs, path, filename; demangle, cflags, target, llvm_to_clang, kwargs...) joinpath(abspath(path), filename * "." * Libdl.dlext) end @@ -325,17 +328,18 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi 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 + exec_path *= ".exe" + `clang` elseif Sys.isapple() `clang` else clang() end - run(`$cclang -Wno-override-module $wrapper_path $obj_or_ir_path -o $exec_path`) + 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 rm(wrapper_path) end @@ -391,6 +395,7 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file demangle = true, cflags = ``, target::StaticTarget=StaticTarget(), + llvm_to_clang::Bool = Sys.iswindows(), kwargs... ) if !isnothing(target.platform) @@ -399,7 +404,7 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file lib_path = joinpath(path, "$filename.$(Libdl.dlext)") end - _, obj_path = generate_obj(funcs, path, filename; target, demangle, kwargs...) + _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, kwargs...) # Pick a Clang if !isnothing(target.compiler) cc = `$(target.compiler)` @@ -407,7 +412,20 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file cc = Sys.isapple() ? `cc` : clang() end # Compile! - run(`$cc -shared $cflags $obj_path -o $lib_path `) + if llvm_to_clang # (required on Windows) + # Use clang (llc) to generate an executable from the LLVM IR + cclang = if Sys.iswindows() + add_dllexport(funcs, obj_or_ir_path; demangle) + `clang` + elseif Sys.isapple() + `clang` + else + clang() + end + run(`$cclang -shared -Wno-override-module $obj_or_ir_path -o $lib_path`) + else + run(`$cc -shared $cflags $obj_or_ir_path -o $lib_path `) + end path, name end diff --git a/src/dllexport.jl b/src/dllexport.jl new file mode 100644 index 00000000..b957e7b5 --- /dev/null +++ b/src/dllexport.jl @@ -0,0 +1,11 @@ +function add_dllexport(funcs, ir_path; demangle=true) + ir = read(ir_path, String) + + for (f, _) in funcs + name_f = (demangle ? "" : "julia_") * fix_name(f) + pattern = Regex("^define(.*?@$name_f\\()", "m") + ir = replace(ir, pattern => s"define dllexport\1") + end + + write(ir_path, ir) +end \ No newline at end of file From 8ad178a44602eec90ee0f0aff36ad4a01af791eb Mon Sep 17 00:00:00 2001 From: Daizu Date: Sun, 19 May 2024 09:29:10 +0900 Subject: [PATCH 153/202] Update tests for Windows --- test/runtests.jl | 6 +++++- test/testintegration.jl | 11 ++++++++--- test/testintegration_windows.jl | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 test/testintegration_windows.jl diff --git a/test/runtests.jl b/test/runtests.jl index 542659c2..cf2964cc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,5 +21,9 @@ if GROUP == "Core" || GROUP == "All" end if GROUP == "Integration" || GROUP == "All" - include("testintegration.jl") + if Sys.iswindows() + include("testintegration_windows.jl") + else + include("testintegration.jl") + end end diff --git a/test/testintegration.jl b/test/testintegration.jl index 3422e63b..e52a1541 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -9,7 +9,7 @@ if VERSION >= v"1.9" buf = AllocBuffer(MallocVector, sizeof(Float64) * N) s = 0.0 for i ∈ 1:N - # some excuse to reuse the same memory a bunch of times + # some excuse to reuse the same memory a bunch of times @no_escape buf begin v = @alloc(Float64, N) v .= i @@ -24,7 +24,7 @@ if VERSION >= v"1.9" path = compile_shlib(bumper_test, (Int,), "./") ptr = Libdl.dlopen(path, Libdl.RTLD_LOCAL) - + fptr = Libdl.dlsym(ptr, "bumper_test") @test bumper_test(8) == @ccall($fptr(8::Int)::Float64) @@ -303,7 +303,12 @@ end catch e @info "maybe_throw: task failed sucessfully!" end - @test status === -1 + if Sys.iswindows() + @info "maybe_throw: task doesn't fail in Windows." + @test status.exitcode == 0 + else + @test status === -1 + end end ## --- Test interop diff --git a/test/testintegration_windows.jl b/test/testintegration_windows.jl new file mode 100644 index 00000000..3e6a887b --- /dev/null +++ b/test/testintegration_windows.jl @@ -0,0 +1,17 @@ +# Currently, `StaticTools.stderrp()` used in `test/scripts` doesn't work in Windows. +# This temporary file deletes `stderrp()` and run tests. + +mkpath("scripts_windows") + +for file in readdir("scripts") + script = read("scripts/$file", String) + script = replace(script, "printf(stderrp(), " => "printf(") + write("scripts_windows/$file", script) +end + +script = read("testintegration.jl", String) +script = replace(script, "testpath/scripts/" => "testpath/scripts_windows/") + +include_string(Main, script) + +rm("scripts_windows"; recursive=true) \ No newline at end of file From 489689a06d14b8fe599a5d33db4627e2283e2884 Mon Sep 17 00:00:00 2001 From: Daizu Date: Sun, 19 May 2024 10:38:13 +0900 Subject: [PATCH 154/202] change comments --- test/testintegration.jl | 2 +- test/testintegration_windows.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testintegration.jl b/test/testintegration.jl index e52a1541..d2a98da2 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -304,7 +304,7 @@ end @info "maybe_throw: task failed sucessfully!" end if Sys.iswindows() - @info "maybe_throw: task doesn't fail in Windows." + @info "maybe_throw: task doesn't fail on Windows." @test status.exitcode == 0 else @test status === -1 diff --git a/test/testintegration_windows.jl b/test/testintegration_windows.jl index 3e6a887b..7ac95e5d 100644 --- a/test/testintegration_windows.jl +++ b/test/testintegration_windows.jl @@ -1,4 +1,4 @@ -# Currently, `StaticTools.stderrp()` used in `test/scripts` doesn't work in Windows. +# Currently, `StaticTools.stderrp()` used in `test/scripts` doesn't work on Windows. # This temporary file deletes `stderrp()` and run tests. mkpath("scripts_windows") From 76bff06fbac59d74bcb647fb1e097c31503f2f4e Mon Sep 17 00:00:00 2001 From: Daizu Date: Sun, 19 May 2024 11:05:53 +0900 Subject: [PATCH 155/202] Use llvm action --- .github/workflows/ci.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3dc141f9..499223cb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,6 +24,7 @@ jobs: os: - ubuntu-latest - macOS-latest + - windows-latest arch: - x64 group: @@ -34,6 +35,10 @@ jobs: os: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: KyleMayes/install-llvm-action@v2 + with: + version: "17" + if: matrix.os == 'windows-latest' - uses: julia-actions/setup-julia@latest with: version: ${{ matrix.version }} From a57094c765bfb32bdf3970f33b77fcd53120daca Mon Sep 17 00:00:00 2001 From: Daizu Date: Sun, 19 May 2024 11:12:39 +0900 Subject: [PATCH 156/202] Use llvm action (Integration) --- .github/workflows/ci-integration.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index beb16c17..544b0a43 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -24,12 +24,17 @@ jobs: os: - ubuntu-latest - macOS-latest + - windows-latest arch: - x64 group: - Integration steps: - uses: actions/checkout@v2 + - uses: KyleMayes/install-llvm-action@v2 + with: + version: "17" + if: matrix.os == 'windows-latest' - uses: julia-actions/setup-julia@latest with: version: ${{ matrix.version }} From 7d53475cc263498764deb72e1d400fb3ec90394a Mon Sep 17 00:00:00 2001 From: Daizu Date: Tue, 21 May 2024 05:41:00 +0900 Subject: [PATCH 157/202] Delete `testintegration_windows.jl` --- test/runtests.jl | 6 +----- test/testintegration_windows.jl | 17 ----------------- 2 files changed, 1 insertion(+), 22 deletions(-) delete mode 100644 test/testintegration_windows.jl diff --git a/test/runtests.jl b/test/runtests.jl index cf2964cc..542659c2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,9 +21,5 @@ if GROUP == "Core" || GROUP == "All" end if GROUP == "Integration" || GROUP == "All" - if Sys.iswindows() - include("testintegration_windows.jl") - else - include("testintegration.jl") - end + include("testintegration.jl") end diff --git a/test/testintegration_windows.jl b/test/testintegration_windows.jl deleted file mode 100644 index 7ac95e5d..00000000 --- a/test/testintegration_windows.jl +++ /dev/null @@ -1,17 +0,0 @@ -# Currently, `StaticTools.stderrp()` used in `test/scripts` doesn't work on Windows. -# This temporary file deletes `stderrp()` and run tests. - -mkpath("scripts_windows") - -for file in readdir("scripts") - script = read("scripts/$file", String) - script = replace(script, "printf(stderrp(), " => "printf(") - write("scripts_windows/$file", script) -end - -script = read("testintegration.jl", String) -script = replace(script, "testpath/scripts/" => "testpath/scripts_windows/") - -include_string(Main, script) - -rm("scripts_windows"; recursive=true) \ No newline at end of file From 574db90bf816271129abf06e0371c2f8c00df844 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sun, 16 Jun 2024 16:55:56 -0400 Subject: [PATCH 158/202] Bump version to 0.7.2 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 4f41e3e2..b4d7d407 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.7.1" +version = "0.7.2" [deps] From d379369d758d0663de8bf26f886dbde4d6cacb32 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Mon, 30 Sep 2024 15:02:35 -0400 Subject: [PATCH 159/202] Update README section on Windows support --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d7c4ed0a..78894700 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompi * 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](src/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. +* Extra experimental on Windows (PRs welcome if you encounter issues). Should work in WSL on Windows 10+. ## Guide for Package Authors From 7e36691c7d98d4aeb7efcdc9c9ede16d839c3ec3 Mon Sep 17 00:00:00 2001 From: Chetan Vardhan <51269425+VarLad@users.noreply.github.com> Date: Tue, 2 Dec 2025 02:22:14 +0900 Subject: [PATCH 160/202] Support for julia 1.12 (#181) * update to julia1.11 and GPUCompiler 1.5.2 * update Project.toml * pass the test for julia 1.8~1.11 in ubuntu * update the version to 0.7.3 * update for GPUCompiler by removing the deprecated API * initial 1.12 support * address reviews * re-enable Bumper tests, comment printdlm * revert printf * use 1.11 and 1.12 in ci * mark loopvec tests as broken * address comments * comment printdlm again --------- Co-authored-by: kylin Co-authored-by: kylincaster <12872755+kylincaster@user.noreply.gitee.com> --- .github/workflows/ci-integration.yml | 5 +- .github/workflows/ci.yml | 5 +- Project.toml | 8 +-- src/StaticCompiler.jl | 27 +++++----- src/interpreter.jl | 74 +++++++++------------------- src/pointer_warning.jl | 3 +- src/quirks.jl | 29 ++++++++++- src/target.jl | 54 ++------------------ test/Project.toml | 2 +- test/runtests.jl | 4 +- test/scripts/loopvec_matrix.jl | 6 ++- test/scripts/loopvec_matrix_stack.jl | 3 +- test/scripts/times_table.jl | 3 +- test/testcore.jl | 4 +- test/testintegration.jl | 73 +++++++++++++-------------- 15 files changed, 126 insertions(+), 174 deletions(-) diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index 544b0a43..7b0f141e 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -18,9 +18,8 @@ jobs: fail-fast: false matrix: version: - - '1.8' - - '1.9' - - '1.10.0-rc1' + - '1.11' + - '1.12' os: - ubuntu-latest - macOS-latest diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 499223cb..55a3952d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,9 +18,8 @@ jobs: fail-fast: false matrix: version: - - '1.8' - - '1.9' - - '1.10' + - '1.11' + - '1.12' os: - ubuntu-latest - macOS-latest diff --git a/Project.toml b/Project.toml index b4d7d407..b7ee8b69 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.7.2" +version = "0.7.3" [deps] @@ -18,11 +18,11 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] CodeInfoTools = "0.3" -GPUCompiler = "0.21, 0.22, 0.23, 0.24, 0.25, 0.26" -LLVM = "6" +GPUCompiler = "1.3" +LLVM = "9" MacroTools = "0.5" StaticTools = "0.8" -julia = "1.8, 1.9" +julia = "1.11" [extras] Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 90e34040..4ccc7e03 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -19,10 +19,10 @@ export static_code_llvm, static_code_typed, static_llvm_module, static_code_nati export @device_override, @print_and_throw export StaticTarget +include("quirks.jl") include("interpreter.jl") include("target.jl") include("pointer_warning.jl") -include("quirks.jl") include("dllexport.jl") fix_name(f::Function) = fix_name(string(nameof(f))) @@ -450,9 +450,9 @@ function static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::Stat if !demangle name = "julia_"*name end - job, kwargs = static_job(f, tt; name, target, kwargs...) + job, kwargs = static_job(f, tt; name, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) m = GPUCompiler.JuliaContext() do context - m, _ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, libraries=false) + m, _ = GPUCompiler.compile(:llvm, job; kwargs...) locate_pointers_and_runtime_calls(m) m end @@ -467,8 +467,8 @@ function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::St if !demangle name_f = "julia_"*name_f end - job, kwargs = static_job(f, tt; name = name_f, target, kwargs...) - mod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, libraries=false) + job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) + mod,_ = GPUCompiler.compile(:llvm, job; kwargs...) if length(funcs) > 1 for func in funcs[2:end] f,tt = func @@ -476,8 +476,8 @@ function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::St if !demangle name_f = "julia_"*name_f end - job, kwargs = static_job(f, tt; name = name_f, target, kwargs...) - tmod,_ = GPUCompiler.codegen(:llvm, job; strip=true, only_entry=false, validate=false, libraries=false) + job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) + tmod,_ = GPUCompiler.compile(:llvm, job; kwargs...) link!(mod,tmod) end end @@ -493,9 +493,10 @@ function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::St name!(modfunc,fname[d:end]) end end - LLVM.ModulePassManager() do pass_manager #remove duplicate functions - LLVM.merge_functions!(pass_manager) - LLVM.run!(pass_manager, mod) + @dispose pb = NewPMPassBuilder(merge_functions=true) begin + add!(pb, NewPMModulePassManager()) do pass_manager + run!(pb, mod) + end end return mod end @@ -529,7 +530,7 @@ The defaults compile to the native target. If `demangle` is set to `false`, compiled function names are prepended with "julia_". ### Examples -```julia +``` julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) fib (generic function with 1 method) @@ -587,8 +588,8 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file 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 + obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.API.LLVMObjectFile) + obj end open(obj_path, "w") do io write(io, obj) diff --git a/src/interpreter.jl b/src/interpreter.jl index 344cc53d..320ef230 100644 --- a/src/interpreter.jl +++ b/src/interpreter.jl @@ -1,61 +1,47 @@ ## interpreter using Core.Compiler: - AbstractInterpreter, InferenceResult, InferenceParams, InferenceState, MethodInstance, OptimizationParams, WorldView, get_world_counter + AbstractInterpreter, InferenceResult, InferenceParams, InferenceState, MethodInstance, OptimizationParams, WorldView using GPUCompiler: - @safe_debug, AbstractCompilerParams, CodeCache, CompilerJob, methodinstance + @safe_debug, AbstractCompilerParams, CompilerJob, methodinstance, CodeInstance, inference_params, optimization_params, get_inference_world using CodeInfoTools using CodeInfoTools: resolve + struct StaticInterpreter <: AbstractInterpreter - global_cache::CodeCache + # The world age we're working inside of + world::UInt method_table::Union{Nothing,Core.MethodTable} + token::Any + # Cache of inference results for this particular interpreter local_cache::Vector{InferenceResult} - # The world age we're working inside of - world::UInt # Parameters for inference and optimization inf_params::InferenceParams opt_params::OptimizationParams - - function StaticInterpreter(cache::CodeCache, mt::Union{Nothing,Core.MethodTable}, world::UInt, ip::InferenceParams, op::OptimizationParams) + function StaticInterpreter(world::UInt, mt::Union{Nothing,Core.MethodTable}, token_or_cache, ip::InferenceParams, op::OptimizationParams) @assert world <= Base.get_world_counter() - - return new( - cache, - mt, - - # Initially empty cache - Vector{InferenceResult}(), - - # world age counter - world, - - # parameters for inference and optimization - ip, - op - ) + local_cache = Vector{Core.Compiler.InferenceResult}() # Initially empty cache + return new(world, mt, token_or_cache, local_cache, ip, op) end end - Core.Compiler.InferenceParams(interp::StaticInterpreter) = interp.inf_params Core.Compiler.OptimizationParams(interp::StaticInterpreter) = interp.opt_params -Core.Compiler.get_world_counter(interp::StaticInterpreter) = interp.world +GPUCompiler.get_inference_world(interp::StaticInterpreter) = interp.world Core.Compiler.get_inference_cache(interp::StaticInterpreter) = interp.local_cache -Core.Compiler.code_cache(interp::StaticInterpreter) = WorldView(interp.global_cache, interp.world) +Core.Compiler.cache_owner(interp::StaticInterpreter) = interp.token # No need to do any locking since we're not putting our results into the runtime cache Core.Compiler.lock_mi_inference(interp::StaticInterpreter, mi::MethodInstance) = nothing Core.Compiler.unlock_mi_inference(interp::StaticInterpreter, mi::MethodInstance) = nothing function Core.Compiler.add_remark!(interp::StaticInterpreter, sv::InferenceState, msg) - @safe_debug "Inference remark during static compilation of $(sv.linfo): $msg" + @safe_debug "Inference remark during static compilation of $(sv.linfo): $msg" end - ##### ##### Pre-inference ##### @@ -77,41 +63,31 @@ function custom_pass!(interp::StaticInterpreter, result::InferenceResult, mi::Co end function Core.Compiler.InferenceState(result::InferenceResult, cache::Symbol, interp::StaticInterpreter) - world = get_world_counter(interp) - src = @static if VERSION >= v"1.10.0-DEV.873" - Core.Compiler.retrieve_code_info(result.linfo, world) - else - Core.Compiler.retrieve_code_info(result.linfo) - end + world = get_inference_world(interp) + src = Core.Compiler.retrieve_code_info(result.linfo, world) mi = result.linfo src = custom_pass!(interp, result, mi, src) - src === nothing && return nothing - Core.Compiler.validate_code_in_debug_mode(result.linfo, src, "lowered") + src === nothing && return Core.Compiler.maybe_validate_code(result.linfo, src, "lowered") return InferenceState(result, src, cache, interp) end Core.Compiler.may_optimize(interp::StaticInterpreter) = true Core.Compiler.may_compress(interp::StaticInterpreter) = true Core.Compiler.may_discard_trees(interp::StaticInterpreter) = true -Core.Compiler.verbose_stmt_info(interp::StaticInterpreter) = false - +if isdefined(Core.Compiler, :verbose_stmt_inf) + Core.Compiler.verbose_stmt_info(interp::StaticInterpreter) = false +end if isdefined(Base.Experimental, Symbol("@overlay")) using Core.Compiler: OverlayMethodTable - if v"1.8-beta2" <= VERSION < v"1.9-" || VERSION >= v"1.9.0-DEV.120" - Core.Compiler.method_table(interp::StaticInterpreter) = - OverlayMethodTable(interp.world, interp.method_table) - else - Core.Compiler.method_table(interp::StaticInterpreter, sv::InferenceState) = + Core.Compiler.method_table(interp::StaticInterpreter) = OverlayMethodTable(interp.world, interp.method_table) - end else Core.Compiler.method_table(interp::StaticInterpreter, sv::InferenceState) = WorldOverlayMethodTable(interp.world) end # semi-concrete interepretation is broken with overlays (JuliaLang/julia#47349) -@static if VERSION >= v"1.9.0-DEV.1248" function Core.Compiler.concrete_eval_eligible(interp::StaticInterpreter, @nospecialize(f), result::Core.Compiler.MethodCallResult, arginfo::Core.Compiler.ArgInfo) ret = @invoke Core.Compiler.concrete_eval_eligible(interp::AbstractInterpreter, @@ -119,16 +95,14 @@ function Core.Compiler.concrete_eval_eligible(interp::StaticInterpreter, ret === false && return nothing return ret end -end struct StaticCompilerParams <: AbstractCompilerParams opt::Bool optlevel::Int - cache::CodeCache end -function StaticCompilerParams(; opt = false, - optlevel = Base.JLOptions().opt_level, - cache = CodeCache()) - return StaticCompilerParams(opt, optlevel, cache) +function StaticCompilerParams(; opt=false, + optlevel=Base.JLOptions().opt_level +) + return StaticCompilerParams(opt, optlevel) end diff --git a/src/pointer_warning.jl b/src/pointer_warning.jl index 9f8f30c8..11a38810 100644 --- a/src/pointer_warning.jl +++ b/src/pointer_warning.jl @@ -30,8 +30,9 @@ function locate_pointers_and_runtime_calls(mod) end end if warned + lines = split(string(func),"\n") @warn("LLVM function generated warnings due to raw pointers embedded in the code. This will likely cause errors or undefined behaviour.", - func = func) + func = join(lines[1:min(20, end)], "\n")) # just print the first 20 lines end end end diff --git a/src/quirks.jl b/src/quirks.jl index f279d94e..4ffa97b6 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -1,10 +1,36 @@ -libcexit(x::Int32) = @symbolcall exit(x::Int32)::Nothing +@static if isdefined(Base.Experimental, Symbol("@overlay")) + Base.Experimental.@MethodTable(method_table) + Base.Experimental.@MethodTable(empty_table) +else + const method_table = nothing +end + +""" +```julia +@device_override old_bad_method(arg1::Type1, arg2::Type2) = new_good_method(arg1, arg2) +``` +Override a non-static-compilable method (e.g. `old_bad_method(::Type1, ::Type2)`) +with a more compileable replacement. +### Examples +``` +@device_override @noinline Core.throw_inexacterror(f::Symbol, ::Type{T}, val) where {T} = + @print_and_throw c"Inexact conversion" +``` +""" +macro device_override(ex) + code = quote + $Base.Experimental.@overlay($StaticCompiler.method_table, $ex) + end + return esc(code) +end + macro print_and_throw(err) quote println($err) libcexit(Int32(1)) end end +libcexit(x::Int32) = @symbolcall exit(x::Int32)::Nothing # math.jl @device_override @noinline Base.Math.throw_complex_domainerror(f::Symbol, x) = @@ -37,7 +63,6 @@ end @device_override @noinline Core.throw_inexacterror(f::Symbol, ::Type{T}, val) where {T} = @print_and_throw c"Inexact conversion" -# abstractarray.jl @device_override @noinline Base.throw_boundserror(A, I) = @print_and_throw c"Out-of-bounds array access" diff --git a/src/target.jl b/src/target.jl index 5faec0d1..3bf95e98 100644 --- a/src/target.jl +++ b/src/target.jl @@ -1,9 +1,3 @@ -@static if isdefined(Base.Experimental, Symbol("@overlay")) - Base.Experimental.@MethodTable(method_table) -else - const method_table = nothing -end - """ ```julia StaticTarget() # Native target @@ -54,30 +48,6 @@ set_compiler!(target::StaticTarget, compiler::String) = (target.compiler = compi set_runtime!(target::StaticTarget, julia_runtime::Bool) = (target.julia_runtime = julia_runtime) -""" -```julia -@device_override old_bad_method(arg1::Type1, arg2::Type2) = new_good_method(arg1, arg2) -``` -Override a non-static-compilable method (e.g. `old_bad_method(::Type1, ::Type2)`) -with a more compileable replacement. -### Examples -``` -@device_override @noinline Core.throw_inexacterror(f::Symbol, ::Type{T}, val) where {T} = - @print_and_throw c"Inexact conversion" -``` -""" -macro device_override(ex) - ex = macroexpand(__module__, ex) - if Meta.isexpr(ex, :call) - @show ex = eval(ex) - error() - end - code = quote - $Base.Experimental.@overlay($StaticCompiler.method_table, $ex) - end - return esc(code) -end - # Default to native struct StaticCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget triple::String @@ -122,26 +92,10 @@ GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = tr GPUCompiler.uses_julia_runtime(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = job.config.target.julia_runtime GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = - StaticInterpreter(job.config.params.cache, GPUCompiler.method_table(job), job.world, - GPUCompiler.inference_params(job), GPUCompiler.optimization_params(job)) -GPUCompiler.ci_cache(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = job.config.params.cache + StaticInterpreter(job.world, GPUCompiler.method_table(job), GPUCompiler.ci_cache_token(job), inference_params(job), optimization_params(job)) GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget})) = job.config.target.method_table -function static_job(@nospecialize(func::Function), @nospecialize(types::Type); - name = fix_name(func), - kernel::Bool = false, - target::StaticTarget = StaticTarget(), - method_table=method_table, - kwargs... - ) - source = methodinstance(typeof(func), Base.to_tuple_type(types)) - tm = target.tm - gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), target.julia_runtime, method_table) - params = StaticCompilerParams() - config = GPUCompiler.CompilerConfig(gputarget, params, name = name, kernel = kernel) - StaticCompiler.CompilerJob(source, config), kwargs -end function static_job(@nospecialize(func), @nospecialize(types); name = fix_name(func), kernel::Bool = false, @@ -153,6 +107,6 @@ function static_job(@nospecialize(func), @nospecialize(types); tm = target.tm gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), target.julia_runtime, method_table) params = StaticCompilerParams() - config = GPUCompiler.CompilerConfig(gputarget, params, name = name, kernel = kernel) - StaticCompiler.CompilerJob(source, config), kwargs -end \ No newline at end of file + config = GPUCompiler.CompilerConfig(gputarget, params; name = name, kernel = kernel, kwargs...) + return StaticCompiler.CompilerJob(source, config), Dict{}() +end diff --git a/test/Project.toml b/test/Project.toml index a36e2086..29e5959c 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -16,4 +16,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e" [compat] -Bumper = "0.6" \ No newline at end of file +Bumper = "0.7" diff --git a/test/runtests.jl b/test/runtests.jl index 542659c2..b9d6780f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,7 +6,7 @@ using LoopVectorization using ManualMemory using Distributed using StaticTools -using StrideArraysCore +# using StrideArraysCore using MacroTools using LLD_jll using Bumper @@ -17,7 +17,7 @@ addprocs(1) const GROUP = get(ENV, "GROUP", "All") if GROUP == "Core" || GROUP == "All" - include("testcore.jl") + include("testcore.jl") end if GROUP == "Integration" || GROUP == "All" diff --git a/test/scripts/loopvec_matrix.jl b/test/scripts/loopvec_matrix.jl index 7b19ce86..12e80f9e 100644 --- a/test/scripts/loopvec_matrix.jl +++ b/test/scripts/loopvec_matrix.jl @@ -39,9 +39,11 @@ function loopvec_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) mul!(C, B, A) # Print to stdout - printf(C) + printf(c"C matrix = \n") + print(C) # Also print to file - printdlm(c"table.tsv", C, '\t') + # Re-enable when StaticTools has been adapted for opaque pointers + # printdlm(c"table.tsv", C, '\t') fwrite(c"table.b", C) # Clean up matrices free(A) diff --git a/test/scripts/loopvec_matrix_stack.jl b/test/scripts/loopvec_matrix_stack.jl index 5e0c90d7..7cf31f52 100644 --- a/test/scripts/loopvec_matrix_stack.jl +++ b/test/scripts/loopvec_matrix_stack.jl @@ -3,7 +3,7 @@ using StaticTools using LoopVectorization @inline function mul!(C::StackArray, A::StackArray, B::StackArray) - @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) + @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) Cmn = zero(eltype(C)) for k ∈ indices((A,B), (2,1)) Cmn += A[m,k] * B[k,n] @@ -38,6 +38,7 @@ function loopvec_matrix_stack() mul!(C, B, A) # Print to stdout + printf(c"C matrix = \n") printf(C) # Also print to file fp = fopen(c"table.tsv",c"w") diff --git a/test/scripts/times_table.jl b/test/scripts/times_table.jl index 7d295eb0..3f8a309b 100644 --- a/test/scripts/times_table.jl +++ b/test/scripts/times_table.jl @@ -16,7 +16,8 @@ function times_table(argc::Int, argv::Ptr{Ptr{UInt8}}) printf(M) # Also print to file fwrite(c"table.b", M) - printdlm(c"table.tsv", M) + # Re-enable when StaticTools has been adapted for opaque pointers + # printdlm(c"table.tsv", M) # Clean up matrix free(M) end diff --git a/test/testcore.jl b/test/testcore.jl index 064f0109..f77b0824 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -1,7 +1,5 @@ workdir = tempdir() - - fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globally due to https://github.com/JuliaLang/julia/issues/40990 @testset "Standalone Dylibs" begin @@ -10,7 +8,7 @@ fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globall # fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) #Compile dylib - name = repr(fib) + name = string(nameof(fib)) # repr(fib) filepath = compile_shlib(fib, (Int,), workdir, name, demangle=true) @test occursin("fib.$(Libdl.dlext)", filepath) # Open dylib manually diff --git a/test/testintegration.jl b/test/testintegration.jl index d2a98da2..22b1ceac 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -3,32 +3,28 @@ testpath = pwd() scratch = tempdir() cd(scratch) -if VERSION >= v"1.9" - # Bumper uses PackageExtensions to work with StaticCompiler, so let's just skip this test on 1.8 - function bumper_test(N::Int) - buf = AllocBuffer(MallocVector, sizeof(Float64) * N) - s = 0.0 - for i ∈ 1:N - # some excuse to reuse the same memory a bunch of times - @no_escape buf begin - v = @alloc(Float64, N) - v .= i - s += sum(v) - end +function bumper_test(N::Int) + buf = AllocBuffer(MallocVector, sizeof(Float64) * N) + s = 0.0 + for i ∈ 1:N + # some excuse to reuse the same memory a bunch of times + @no_escape buf begin + v = @alloc(Float64, N) + v .= i + s += sum(v) end - free(buf) - s end + free(buf) + s +end - @testset "Bumper.jl integration" begin - - path = compile_shlib(bumper_test, (Int,), "./") - ptr = Libdl.dlopen(path, Libdl.RTLD_LOCAL) +@testset "Bumper.jl integration" begin + path = compile_shlib(bumper_test, (Int,), "./") + ptr = Libdl.dlopen(path, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "bumper_test") + fptr = Libdl.dlsym(ptr, "bumper_test") - @test bumper_test(8) == @ccall($fptr(8::Int)::Float64) - end + @test bumper_test(8) == @ccall($fptr(8::Int)::Float64) end @testset "Standalone Executable Integration" begin @@ -64,7 +60,7 @@ end @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 # Test ascii output - # @test parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' broken=Sys.isapple() + @test_broken parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' broken=Sys.isapple() # Test binary output @test fread!(szeros(Int, 5,5), c"table.b") == (1:5)*(1:5)' end @@ -153,6 +149,7 @@ end end end + ## --- Test LoopVectorization integration if Bool(LoopVectorization.VectorizationBase.has_feature(Val{:x86_64_avx2})) let @@ -165,8 +162,8 @@ end @warn "Could not compile $testpath/scripts/loopvec_product.jl" println(e) end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 + @test_broken isa(status, Base.Process) + @test_broken isa(status, Base.Process) && status.exitcode == 0 # Run... println("10x10 table sum:") @@ -177,9 +174,9 @@ end @warn "Could not run $(scratch)/loopvec_product" println(e) end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 - # @test parsedlm(c"product.tsv",'\t')[] == 3025 + @test_broken isa(status, Base.Process) + @test_broken isa(status, Base.Process) && status.exitcode == 0 + @test_broken parsedlm(c"product.tsv",'\t')[] == 3025 end end @@ -193,8 +190,8 @@ end @warn "Could not compile $testpath/scripts/loopvec_matrix.jl" println(e) end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 + @test_broken isa(status, Base.Process) + @test_broken isa(status, Base.Process) && status.exitcode == 0 # Run... println("10x5 matrix product:") @@ -205,13 +202,13 @@ end @warn "Could not run $(scratch)/loopvec_matrix" println(e) end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 + @test_broken isa(status, Base.Process) + @test_broken isa(status, Base.Process) && status.exitcode == 0 A = (1:10) * (1:5)' # Check ascii output - # @test parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.isapple() + @test_broken parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.isapple() # Check binary output - @test fread!(szeros(5,5), c"table.b") == A' * A + @test_broken fread!(szeros(5,5), c"table.b") == A' * A end let @@ -224,8 +221,8 @@ end @warn "Could not compile $testpath/scripts/loopvec_matrix_stack.jl" println(e) end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 + @test_broken isa(status, Base.Process) + @test_broken isa(status, Base.Process) && status.exitcode == 0 # Run... println("10x5 matrix product:") @@ -236,10 +233,10 @@ end @warn "Could not run $(scratch)/loopvec_matrix_stack" println(e) end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 + @test_broken isa(status, Base.Process) + @test_broken isa(status, Base.Process) && status.exitcode == 0 A = (1:10) * (1:5)' - # @test parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.isapple() + @test_broken parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.isapple() end From c8beda7242c919168577a4e861e0facb0f23c6fd Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Oct 2026 12:01:44 +0200 Subject: [PATCH 161/202] Upgrade to GPUCompiler 2 GPUCompiler 2 caches inference results and runtime libraries through CompilerCaching.jl, which removed the `runtime_slug` and `ci_cache_token` hooks: the runtime library is now keyed on the job's configuration automatically, so the `runtime_slug` override goes. It also only drives inference with its own `GPUInterpreter`, so a custom `AbstractInterpreter` returned from `get_interpreter` no longer works. `StaticInterpreter` was a copy of GPUCompiler's interpreter with an overlay method table (its `custom_pass!` hook returned the code unchanged), which GPUCompiler's default interpreter already provides via the `method_table` override. Remove it, and with it the CodeInfoTools dependency it was the only user of; `StaticCompilerParams` moves to target.jl next to the target. --- Project.toml | 4 +- src/StaticCompiler.jl | 1 - src/interpreter.jl | 108 ------------------------------------------ src/target.jl | 19 +++++--- 4 files changed, 14 insertions(+), 118 deletions(-) delete mode 100644 src/interpreter.jl diff --git a/Project.toml b/Project.toml index b7ee8b69..a1416b5e 100644 --- a/Project.toml +++ b/Project.toml @@ -6,7 +6,6 @@ version = "0.7.3" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" -CodeInfoTools = "bc773b8a-8374-437a-b9f2-0e9785855863" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" LLD_jll = "d55e3150-da41-5e91-b323-ecfd1eec6109" @@ -17,8 +16,7 @@ Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] -CodeInfoTools = "0.3" -GPUCompiler = "1.3" +GPUCompiler = "2" LLVM = "9" MacroTools = "0.5" StaticTools = "0.8" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 4ccc7e03..d0c2161f 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -20,7 +20,6 @@ export @device_override, @print_and_throw export StaticTarget include("quirks.jl") -include("interpreter.jl") include("target.jl") include("pointer_warning.jl") include("dllexport.jl") diff --git a/src/interpreter.jl b/src/interpreter.jl deleted file mode 100644 index 320ef230..00000000 --- a/src/interpreter.jl +++ /dev/null @@ -1,108 +0,0 @@ -## interpreter - -using Core.Compiler: - AbstractInterpreter, InferenceResult, InferenceParams, InferenceState, MethodInstance, OptimizationParams, WorldView -using GPUCompiler: - @safe_debug, AbstractCompilerParams, CompilerJob, methodinstance, CodeInstance, inference_params, optimization_params, get_inference_world -using CodeInfoTools -using CodeInfoTools: resolve - - -struct StaticInterpreter <: AbstractInterpreter - # The world age we're working inside of - world::UInt - method_table::Union{Nothing,Core.MethodTable} - - token::Any - - # Cache of inference results for this particular interpreter - local_cache::Vector{InferenceResult} - - # Parameters for inference and optimization - inf_params::InferenceParams - opt_params::OptimizationParams - function StaticInterpreter(world::UInt, mt::Union{Nothing,Core.MethodTable}, token_or_cache, ip::InferenceParams, op::OptimizationParams) - @assert world <= Base.get_world_counter() - local_cache = Vector{Core.Compiler.InferenceResult}() # Initially empty cache - return new(world, mt, token_or_cache, local_cache, ip, op) - end -end - -Core.Compiler.InferenceParams(interp::StaticInterpreter) = interp.inf_params -Core.Compiler.OptimizationParams(interp::StaticInterpreter) = interp.opt_params -GPUCompiler.get_inference_world(interp::StaticInterpreter) = interp.world -Core.Compiler.get_inference_cache(interp::StaticInterpreter) = interp.local_cache -Core.Compiler.cache_owner(interp::StaticInterpreter) = interp.token - -# No need to do any locking since we're not putting our results into the runtime cache -Core.Compiler.lock_mi_inference(interp::StaticInterpreter, mi::MethodInstance) = nothing -Core.Compiler.unlock_mi_inference(interp::StaticInterpreter, mi::MethodInstance) = nothing - -function Core.Compiler.add_remark!(interp::StaticInterpreter, sv::InferenceState, msg) - @safe_debug "Inference remark during static compilation of $(sv.linfo): $msg" -end - -##### -##### Pre-inference -##### - -function resolve_generic(a) - if a isa Type && a <: Function && isdefined(a, :instance) - return a.instance - else - return resolve(a) - end -end - -function custom_pass!(interp::StaticInterpreter, result::InferenceResult, mi::Core.MethodInstance, src) - src === nothing && return src - mi.specTypes isa UnionAll && return src - sig = Tuple(mi.specTypes.parameters) - as = map(resolve_generic, sig) - return src -end - -function Core.Compiler.InferenceState(result::InferenceResult, cache::Symbol, interp::StaticInterpreter) - world = get_inference_world(interp) - src = Core.Compiler.retrieve_code_info(result.linfo, world) - mi = result.linfo - src = custom_pass!(interp, result, mi, src) - src === nothing && return Core.Compiler.maybe_validate_code(result.linfo, src, "lowered") - return InferenceState(result, src, cache, interp) -end - -Core.Compiler.may_optimize(interp::StaticInterpreter) = true -Core.Compiler.may_compress(interp::StaticInterpreter) = true -Core.Compiler.may_discard_trees(interp::StaticInterpreter) = true -if isdefined(Core.Compiler, :verbose_stmt_inf) - Core.Compiler.verbose_stmt_info(interp::StaticInterpreter) = false -end - -if isdefined(Base.Experimental, Symbol("@overlay")) - using Core.Compiler: OverlayMethodTable - Core.Compiler.method_table(interp::StaticInterpreter) = - OverlayMethodTable(interp.world, interp.method_table) -else - Core.Compiler.method_table(interp::StaticInterpreter, sv::InferenceState) = - WorldOverlayMethodTable(interp.world) -end - -# semi-concrete interepretation is broken with overlays (JuliaLang/julia#47349) -function Core.Compiler.concrete_eval_eligible(interp::StaticInterpreter, - @nospecialize(f), result::Core.Compiler.MethodCallResult, arginfo::Core.Compiler.ArgInfo) - ret = @invoke Core.Compiler.concrete_eval_eligible(interp::AbstractInterpreter, - f::Any, result::Core.Compiler.MethodCallResult, arginfo::Core.Compiler.ArgInfo) - ret === false && return nothing - return ret -end - -struct StaticCompilerParams <: AbstractCompilerParams - opt::Bool - optlevel::Int -end - -function StaticCompilerParams(; opt=false, - optlevel=Base.JLOptions().opt_level -) - return StaticCompilerParams(opt, optlevel) -end diff --git a/src/target.jl b/src/target.jl index 3bf95e98..84a934c9 100644 --- a/src/target.jl +++ b/src/target.jl @@ -57,6 +57,17 @@ struct StaticCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget method_table::MT end +struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams + opt::Bool + optlevel::Int +end + +function StaticCompilerParams(; opt=false, + optlevel=Base.JLOptions().opt_level +) + return StaticCompilerParams(opt, optlevel) +end + module StaticRuntime # the runtime library signal_exception() = return @@ -81,8 +92,6 @@ function GPUCompiler.llvm_machine(target::StaticCompilerTarget) return tm end -GPUCompiler.runtime_slug(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = "static_$(job.config.target.cpu)-$(hash(job.config.target.features))" - GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = StaticRuntime GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = StaticRuntime @@ -91,8 +100,6 @@ GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, Stati GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = true GPUCompiler.uses_julia_runtime(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = job.config.target.julia_runtime -GPUCompiler.get_interpreter(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = - StaticInterpreter(job.world, GPUCompiler.method_table(job), GPUCompiler.ci_cache_token(job), inference_params(job), optimization_params(job)) GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget})) = job.config.target.method_table @@ -103,10 +110,10 @@ function static_job(@nospecialize(func), @nospecialize(types); method_table=method_table, kwargs... ) - source = methodinstance(typeof(func), Base.to_tuple_type(types)) + source = GPUCompiler.methodinstance(typeof(func), Base.to_tuple_type(types)) tm = target.tm gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), target.julia_runtime, method_table) params = StaticCompilerParams() config = GPUCompiler.CompilerConfig(gputarget, params; name = name, kernel = kernel, kwargs...) - return StaticCompiler.CompilerJob(source, config), Dict{}() + return GPUCompiler.CompilerJob(source, config), Dict{}() end From 7da6e12be039e26a26cb4ea6b6e2468b136a1644 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Oct 2026 12:05:13 +0200 Subject: [PATCH 162/202] Return the library's name from generate_shlib The method for a list of functions returned `path, name`, but has no `name` variable: `name` resolved to LLVM.jl's `name` function, which `using LLVM` exported. Return the file name of the library instead, and have the method for a single function return the name it was given, as its docstring shows. LLVM.jl 10 no longer exports `name`, which turned this into an `UndefVarError` on every call of `compile_shlib`. --- src/StaticCompiler.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index d0c2161f..d65f7342 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -388,6 +388,7 @@ julia> ccall(("test", "example/test.dylib"), Float64, (Int64,), 100_000) """ function generate_shlib(f::Function, tt, path::String=tempname(), name=fix_name(f), filename=name; target=StaticTarget(), kwargs...) generate_shlib(((f, tt),), path, filename; target, kwargs...) + path, name end # As above, but taking an array of functions and returning a single shlib function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), filename::String="libfoo"; @@ -426,7 +427,7 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file run(`$cc -shared $cflags $obj_or_ir_path -o $lib_path `) end - path, name + path, filename end function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) From 08a3f64dfa4dacb756048434f85af457406fe6c0 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Oct 2026 12:12:12 +0200 Subject: [PATCH 163/202] Port to LLVM.jl 10 Import the vocabularies the package uses, use properties instead of accessor functions, pass the CPU and features of target machines as keywords, and use the pass manager types without their `NewPM` prefix. `asm_verbosity!` is LLVM.jl's, which GPUCompiler only re-exported through `using LLVM`. Also remove `llvmeltype`, which was never called and relied on `eltype` of pointer types. --- Project.toml | 2 +- src/StaticCompiler.jl | 14 ++++++-------- src/pointer_warning.jl | 19 +++++++++---------- src/target.jl | 12 ++++++------ test/scripts/wasm.jl | 10 +++++----- 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/Project.toml b/Project.toml index a1416b5e..c181f8e8 100644 --- a/Project.toml +++ b/Project.toml @@ -17,7 +17,7 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] GPUCompiler = "2" -LLVM = "9" +LLVM = "10" MacroTools = "0.5" StaticTools = "0.8" julia = "1.11" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index d65f7342..39d09e10 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -1,9 +1,7 @@ module StaticCompiler using InteractiveUtils using GPUCompiler: GPUCompiler -using LLVM -using LLVM.Interop -using LLVM: API +using LLVM, LLVM.IR, LLVM.Passes using Libdl: Libdl, dlsym, dlopen using Base: RefValue using Serialization: serialize, deserialize @@ -485,16 +483,16 @@ function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::St mod end # Just to be sure - for (modfunc, func) in zip(functions(mod), funcs) - fname = name(modfunc) + for (modfunc, func) in zip(mod.functions, funcs) + fname = modfunc.name expectedname = (demangle ? "" : "julia_") * fix_name(func) d = prefixlen(fname) - prefixlen(expectedname) + 1 if d > 1 - name!(modfunc,fname[d:end]) + modfunc.name = fname[d:end] end end - @dispose pb = NewPMPassBuilder(merge_functions=true) begin - add!(pb, NewPMModulePassManager()) do pass_manager + @dispose pb = PassBuilder(merge_functions=true) begin + add!(pb, ModulePassManager()) do pass_manager run!(pb, mod) end end diff --git a/src/pointer_warning.jl b/src/pointer_warning.jl index 11a38810..99a8d870 100644 --- a/src/pointer_warning.jl +++ b/src/pointer_warning.jl @@ -1,7 +1,7 @@ function locate_pointers_and_runtime_calls(mod) i64 = LLVM.IntType(64) # d = IdDict{Any, Tuple{String, LLVM.GlobalVariable}}() - for func ∈ LLVM.functions(mod), bb ∈ LLVM.blocks(func), inst ∈ LLVM.instructions(bb) + for func ∈ mod.functions, bb ∈ func.blocks, inst ∈ bb.instructions warned = false if isa(inst, LLVM.LoadInst) && occursin("inttoptr", string(inst)) warned = inspect_pointers(mod, inst) @@ -9,17 +9,17 @@ function locate_pointers_and_runtime_calls(mod) @debug "Inspecting StoreInst" inst warned = inspect_pointers(mod, inst) elseif inst isa LLVM.RetInst && occursin("inttoptr", string(inst)) - @debug "Inspecting RetInst" inst LLVM.operands(inst) + @debug "Inspecting RetInst" inst inst.operands warned = inspect_pointers(mod, inst) elseif isa(inst, LLVM.BitCastInst) && occursin("inttoptr", string(inst)) - @debug "Inspecting BitCastInst" inst LLVM.operands(inst) + @debug "Inspecting BitCastInst" inst inst.operands warned = inspect_pointers(mod, inst) elseif isa(inst, LLVM.CallInst) - @debug "Inspecting CallInst" inst LLVM.operands(inst) - dest = LLVM.called_operand(inst) - if occursin("inttoptr", string(dest)) && length(LLVM.operands(dest)) > 0 - @debug "Inspecting CallInst inttoptr" dest LLVM.operands(dest) LLVM.operands(inst) - ptr_arg = first(LLVM.operands(dest)) + @debug "Inspecting CallInst" inst inst.operands + dest = inst.called_operand + if occursin("inttoptr", string(dest)) && length(dest.operands) > 0 + @debug "Inspecting CallInst inttoptr" dest dest.operands inst.operands + ptr_arg = first(dest.operands) ptr_val = convert(Int, ptr_arg) ptr = Ptr{Cvoid}(ptr_val) @@ -40,7 +40,7 @@ end function inspect_pointers(mod, inst) warned = false jl_t = (LLVM.StructType(LLVM.LLVMType[])) - for (i, arg) ∈ enumerate(LLVM.operands(inst)) + for (i, arg) ∈ enumerate(inst.operands) if occursin("inttoptr", string(arg)) && arg isa LLVM.ConstantExpr op1 = LLVM.Value(LLVM.API.LLVMGetOperand(arg, 0)) if op1 isa LLVM.ConstantExpr @@ -66,7 +66,6 @@ data_warnings(inst, frames) = for frame ∈ frames inlined = inlined) end -llvmeltype(x::LLVM.Value) = eltype(LLVM.value_type(x)) diff --git a/src/target.jl b/src/target.jl index 84a934c9..a2220047 100644 --- a/src/target.jl +++ b/src/target.jl @@ -29,12 +29,12 @@ end clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform) StaticTarget() = StaticTarget(HostPlatform(), unsafe_string(LLVM.API.LLVMGetHostCPUName()), unsafe_string(LLVM.API.LLVMGetHostCPUFeatures())) StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform)), nothing, false) -StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu), nothing, false) -StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform), cpu, features), nothing, false) +StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform); cpu), nothing, false) +StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform); cpu, features), nothing, false) function StaticTarget(triple::String, cpu::String, features::String) platform = tryparse(Platform, triple) - StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = triple), triple, cpu, features), nothing) + StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = triple), triple; cpu, features), nothing) end """ @@ -86,8 +86,8 @@ function GPUCompiler.llvm_machine(target::StaticCompilerTarget) t = LLVM.Target(triple=triple) - tm = LLVM.TargetMachine(t, triple, target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) - GPUCompiler.asm_verbosity!(tm, true) + tm = LLVM.TargetMachine(t, triple; target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) + LLVM.asm_verbosity!(tm, true) return tm end @@ -112,7 +112,7 @@ function static_job(@nospecialize(func), @nospecialize(types); ) source = GPUCompiler.methodinstance(typeof(func), Base.to_tuple_type(types)) tm = target.tm - gputarget = StaticCompilerTarget(LLVM.triple(tm), LLVM.cpu(tm), LLVM.features(tm), target.julia_runtime, method_table) + gputarget = StaticCompilerTarget(tm.triple, tm.cpu, tm.features, target.julia_runtime, method_table) params = StaticCompilerParams() config = GPUCompiler.CompilerConfig(gputarget, params; name = name, kernel = kernel, kwargs...) return GPUCompiler.CompilerJob(source, config), Dict{}() diff --git a/test/scripts/wasm.jl b/test/scripts/wasm.jl index beff3b7d..0172bc5d 100644 --- a/test/scripts/wasm.jl +++ b/test/scripts/wasm.jl @@ -3,11 +3,11 @@ using StaticCompiler using LLVM -InitializeAllTargets() -InitializeAllTargetInfos() -InitializeAllAsmPrinters() -InitializeAllAsmParsers() -InitializeAllTargetMCs() +LLVM.InitializeAllTargets() +LLVM.InitializeAllTargetInfos() +LLVM.InitializeAllAsmPrinters() +LLVM.InitializeAllAsmParsers() +LLVM.InitializeAllTargetMCs() fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) From 62d10eef3f1950313fd753376a4d3457fdb82d61 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Oct 2026 12:13:02 +0200 Subject: [PATCH 164/202] Use LLVM.jl instead of the C API Query the host CPU with LLVM.host_cpu_name and host_cpu_features, which also free the strings that LLVMGetHostCPUName and LLVMGetHostCPUFeatures return (they leaked), get the operands of constant expressions with `operands`, and use the scoped names of the enumerations. --- src/StaticCompiler.jl | 2 +- src/pointer_warning.jl | 4 ++-- src/target.jl | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 39d09e10..56878b8d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -586,7 +586,7 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file obj_path = joinpath(path, "$filenamebase.o") obj = GPUCompiler.JuliaContext() do ctx fakejob, _ = static_job(f, tt; target, kwargs...) - obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.API.LLVMObjectFile) + obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) obj end open(obj_path, "w") do io diff --git a/src/pointer_warning.jl b/src/pointer_warning.jl index 99a8d870..370234f8 100644 --- a/src/pointer_warning.jl +++ b/src/pointer_warning.jl @@ -42,9 +42,9 @@ function inspect_pointers(mod, inst) jl_t = (LLVM.StructType(LLVM.LLVMType[])) for (i, arg) ∈ enumerate(inst.operands) if occursin("inttoptr", string(arg)) && arg isa LLVM.ConstantExpr - op1 = LLVM.Value(LLVM.API.LLVMGetOperand(arg, 0)) + op1 = arg.operands[1] if op1 isa LLVM.ConstantExpr - op1 = LLVM.Value(LLVM.API.LLVMGetOperand(op1, 0)) + op1 = op1.operands[1] end ptr = Ptr{Cvoid}(convert(Int, op1)) frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) diff --git a/src/target.jl b/src/target.jl index a2220047..6c7b0d2f 100644 --- a/src/target.jl +++ b/src/target.jl @@ -27,7 +27,7 @@ mutable struct StaticTarget end clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform) -StaticTarget() = StaticTarget(HostPlatform(), unsafe_string(LLVM.API.LLVMGetHostCPUName()), unsafe_string(LLVM.API.LLVMGetHostCPUFeatures())) +StaticTarget() = StaticTarget(HostPlatform(), LLVM.host_cpu_name(), LLVM.host_cpu_features()) StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform)), nothing, false) StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform); cpu), nothing, false) StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform); cpu, features), nothing, false) @@ -86,7 +86,7 @@ function GPUCompiler.llvm_machine(target::StaticCompilerTarget) t = LLVM.Target(triple=triple) - tm = LLVM.TargetMachine(t, triple; target.cpu, target.features, reloc=LLVM.API.LLVMRelocPIC) + tm = LLVM.TargetMachine(t, triple; target.cpu, target.features, reloc=LLVM.RelocMode.PIC) LLVM.asm_verbosity!(tm, true) return tm From 9c5a44ea7f848ac51539b87255d3596ad006ec9c Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Oct 2026 12:39:18 +0200 Subject: [PATCH 165/202] Generate object code in the module's context generate_obj emitted the module in a new context, while the module belongs to the one static_llvm_module compiled it in. Code generation creates constants in the active context (e.g., for the addresses that GPUCompiler inlines), so the module ended up with values of another context, which is invalid IR: after emitting loopvec_matrix.jl's module, 323 of its operands belonged to the other context. This went unnoticed because the module was never freed. Activate the module's own context instead. --- src/StaticCompiler.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 56878b8d..230a72f3 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -584,7 +584,9 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file return path, ir_path else obj_path = joinpath(path, "$filenamebase.o") - obj = GPUCompiler.JuliaContext() do ctx + # emit code in the module's context, so that what code generation adds to the + # module belongs to it + obj = context!(mod.context) do fakejob, _ = static_job(f, tt; target, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) obj From 07fadc4c854c619edc0a86fe3093902b0ee45763 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Oct 2026 12:41:42 +0200 Subject: [PATCH 166/202] Dispose of the module after generating code from it With LLVM.jl 10, GPUCompiler hands the module that `compile(:llvm, job)` returns over to the caller, by taking it out of the code generator's thread-safe module with `LLVM.unsafe_take_module!`. That needs LLVM 16 or later, i.e., Julia 1.11 or later, which is all StaticCompiler supports. Before, the module stayed owned by Julia's code generator, which never freed it. generate_obj only needs the module to write it out, so dispose of it afterwards. static_llvm_module also disposes of the module it owns when anything fails before it returns it (e.g., locate_pointers_and_runtime_calls, compiling a later function of a list, renaming the functions or running passes; link! consumes the linked modules, also when it fails). To cover the renaming and the pass run, they move into the block that compiles the module, where its context is still active. The docstrings now say that the caller owns the module that static_llvm_module returns. --- src/StaticCompiler.jl | 122 ++++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 47 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 230a72f3..1199e095 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -443,7 +443,15 @@ function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name( GPUCompiler.code_native(stdout, job; libraries=false, kwargs...) end -# Return an LLVM module +""" +```julia +static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) +``` +Compile the method of `f` for the argument types `tt` to an `LLVM.Module`. + +The caller owns the returned module: dispose of it (`LLVM.dispose(mod)`) when done, or hand it +to an operation that consumes it (like `LLVM.link!(dst, mod)`). +""" function static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) if !demangle name = "julia_"*name @@ -451,13 +459,27 @@ function static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::Stat job, kwargs = static_job(f, tt; name, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) m = GPUCompiler.JuliaContext() do context m, _ = GPUCompiler.compile(:llvm, job; kwargs...) - locate_pointers_and_runtime_calls(m) + try + locate_pointers_and_runtime_calls(m) + catch + dispose(m) + rethrow() + end m end return m end -#Return an LLVM module for multiple functions +""" +```julia +static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) +``` +Compile the methods of an array of tuples `(f, tt)` of functions and argument types to a +single `LLVM.Module`. + +The caller owns the returned module: dispose of it (`LLVM.dispose(mod)`) when done, or hand it +to an operation that consumes it (like `LLVM.link!(dst, mod)`). +""" function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) f,tt = funcs[1] mod = GPUCompiler.JuliaContext() do context @@ -467,35 +489,41 @@ function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::St end job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) mod,_ = GPUCompiler.compile(:llvm, job; kwargs...) - if length(funcs) > 1 - for func in funcs[2:end] - f,tt = func - name_f = fix_name(f) - if !demangle - name_f = "julia_"*name_f + try + if length(funcs) > 1 + for func in funcs[2:end] + f,tt = func + name_f = fix_name(f) + if !demangle + name_f = "julia_"*name_f + end + job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) + tmod,_ = GPUCompiler.compile(:llvm, job; kwargs...) + # consumes `tmod`, also when linking fails + link!(mod,tmod) + end + end + locate_pointers_and_runtime_calls(mod) + # Just to be sure + for (modfunc, func) in zip(mod.functions, funcs) + fname = modfunc.name + expectedname = (demangle ? "" : "julia_") * fix_name(func) + d = prefixlen(fname) - prefixlen(expectedname) + 1 + if d > 1 + modfunc.name = fname[d:end] end - job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) - tmod,_ = GPUCompiler.compile(:llvm, job; kwargs...) - link!(mod,tmod) end + @dispose pb = PassBuilder(merge_functions=true) begin + add!(pb, ModulePassManager()) do pass_manager + run!(pb, mod) + end + end + catch + dispose(mod) + rethrow() end - locate_pointers_and_runtime_calls(mod) mod end - # Just to be sure - for (modfunc, func) in zip(mod.functions, funcs) - fname = modfunc.name - expectedname = (demangle ? "" : "julia_") * fix_name(func) - d = prefixlen(fname) - prefixlen(expectedname) + 1 - if d > 1 - modfunc.name = fname[d:end] - end - end - @dispose pb = PassBuilder(merge_functions=true) begin - add!(pb, ModulePassManager()) do pass_manager - run!(pb, mod) - end - end return mod end @@ -574,27 +602,27 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file kwargs...) f, tt = funcs[1] mkpath(path) - mod = static_llvm_module(funcs; demangle, kwargs...) - - 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") - # emit code in the module's context, so that what code generation adds to the - # module belongs to it - obj = context!(mod.context) do - fakejob, _ = static_job(f, tt; target, kwargs...) - obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) - obj - end - open(obj_path, "w") do io - write(io, obj) + @dispose mod=static_llvm_module(funcs; demangle, kwargs...) begin + 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") + # emit code in the module's context, so that what code generation adds to the + # module belongs to it + obj = context!(mod.context) do + fakejob, _ = static_job(f, tt; target, kwargs...) + obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) + obj + end + open(obj_path, "w") do io + write(io, obj) + end + return path, obj_path end - return path, obj_path end end From 301c9d6c85dba3c58dec7b5657cf7b872d92760d Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Oct 2026 13:06:42 +0200 Subject: [PATCH 167/202] Export DLL entry points with LLVM.jl On Windows, generate_shlib added `dllexport` to the entry points by rewriting the `define` lines of the textual IR with a regular expression. Set the DLL storage class of the functions in the module instead, before it is written out. The IR is identical to what the regex produced (checked on Linux for two functions, with and without demangling), and compiling it for x86_64-pc-windows-msvc yields the same `/EXPORT` directives. --- src/StaticCompiler.jl | 9 +++++++-- src/dllexport.jl | 21 ++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 1199e095..c7e8f2e1 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -402,7 +402,8 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file lib_path = joinpath(path, "$filename.$(Libdl.dlext)") end - _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, kwargs...) + _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, + dllexport=llvm_to_clang && Sys.iswindows(), kwargs...) # Pick a Clang if !isnothing(target.compiler) cc = `$(target.compiler)` @@ -413,7 +414,6 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file if llvm_to_clang # (required on Windows) # Use clang (llc) to generate an executable from the LLVM IR cclang = if Sys.iswindows() - add_dllexport(funcs, obj_or_ir_path; demangle) `clang` elseif Sys.isapple() `clang` @@ -581,6 +581,7 @@ generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase: target::StaticTarget=StaticTarget(), demangle = false, emit_llvm_only = false, + dllexport = false, strip_llvm = false, strip_asm = true, kwargs...) @@ -592,17 +593,21 @@ which will be compiled. `target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. This is a struct of the type StaticTarget() The defaults compile to the native target. + +If `dllexport` is set, the functions are exported from a Windows DLL. """ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; target::StaticTarget=StaticTarget(), demangle = true, emit_llvm_only = false, + dllexport = false, strip_llvm = false, strip_asm = true, kwargs...) f, tt = funcs[1] mkpath(path) @dispose mod=static_llvm_module(funcs; demangle, kwargs...) begin + dllexport && add_dllexport!(mod, funcs; demangle) if emit_llvm_only # (Required on Windows) ir_path = joinpath(path, "$filenamebase.ll") open(ir_path, "w") do io diff --git a/src/dllexport.jl b/src/dllexport.jl index b957e7b5..018ea2f4 100644 --- a/src/dllexport.jl +++ b/src/dllexport.jl @@ -1,11 +1,10 @@ -function add_dllexport(funcs, ir_path; demangle=true) - ir = read(ir_path, String) - - for (f, _) in funcs - name_f = (demangle ? "" : "julia_") * fix_name(f) - pattern = Regex("^define(.*?@$name_f\\()", "m") - ir = replace(ir, pattern => s"define dllexport\1") - end - - write(ir_path, ir) -end \ No newline at end of file +# export the compiled entry points from a Windows DLL +function add_dllexport!(mod::LLVM.Module, funcs; demangle=true) + for (f, _) in funcs + name_f = (demangle ? "" : "julia_") * fix_name(f) + llvmf = get(mod.functions, name_f, nothing) + if llvmf !== nothing && !isdeclaration(llvmf) + llvmf.dllstorage = LLVM.DLLStorageClass.Export + end + end +end From 4a8bbbf102769b48801e100070369ffe6db412e6 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Oct 2026 12:18:09 +0200 Subject: [PATCH 168/202] Actually merge identical functions static_llvm_module meant to merge identical functions, but ran an empty module pass manager with the `merge_functions` pipeline option, which only affects LLVM's default pipelines: nothing ran. Run the MergeFunctions pass itself. Identical functions now become a tail call to the first one, which keeps every exported symbol. This changes the generated code (the tests pass either way), so it is a behavior change that can be dropped independently of the rest of the LLVM.jl 10 port; without it, the no-op pass run could simply be removed. --- src/StaticCompiler.jl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index c7e8f2e1..bdf5a92e 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -513,11 +513,7 @@ function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::St modfunc.name = fname[d:end] end end - @dispose pb = PassBuilder(merge_functions=true) begin - add!(pb, ModulePassManager()) do pass_manager - run!(pb, mod) - end - end + run!(MergeFunctionsPass(), mod) catch dispose(mod) rethrow() From f5df71711f16247b8cbc12df6e6b39afb92ee850 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Oct 2026 20:25:17 +0200 Subject: [PATCH 169/202] Don't use the compiled modules after their context is disposed of `static_llvm_module` compiled into a `GPUCompiler.JuliaContext() do ... end` block and returned the module out of it, after which `generate_obj` added the DLL exports to it, generated code from it and disposed of it, with the module's context disposed of already. That only worked because GPUCompiler leaks the context (GPUCompiler.jl#970), and LLVM.jl's memcheck reports these uses of a module after its context's disposal. `generate_obj` now compiles, adds the exports, generates code and disposes of the module in one context, so that this doesn't leak the module and keeps working once that leak is fixed. The exported `static_llvm_module` compiles into the active thread-safe context when there is one (e.g., when called in a `JuliaContext` block), and otherwise into a new one that it deliberately never disposes of, so that the module it returns stays valid without changing its API. Its docstring says so. --- src/StaticCompiler.jl | 170 ++++++++++++++++++++++++------------------ 1 file changed, 99 insertions(+), 71 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index bdf5a92e..b5674634 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -443,31 +443,26 @@ function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name( GPUCompiler.code_native(stdout, job; libraries=false, kwargs...) end +const CONTEXT_DOC = """ +The module belongs to the active thread-safe context, so call this function in a +`GPUCompiler.JuliaContext() do ctx ... end` block, and dispose of the module (or hand it to an +operation that consumes it, like `LLVM.link!(dst, mod)`) before the block ends. Without an +active thread-safe context, a new one is created for the module, which is never disposed of +(i.e., every such call leaks a context).""" + """ ```julia static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) ``` -Compile the method of `f` for the argument types `tt` to an `LLVM.Module`. +Compile the method of `f` for the argument types `tt` to an `LLVM.Module`, which the caller +owns. -The caller owns the returned module: dispose of it (`LLVM.dispose(mod)`) when done, or hand it -to an operation that consumes it (like `LLVM.link!(dst, mod)`). +$CONTEXT_DOC """ function static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) - if !demangle - name = "julia_"*name - end - job, kwargs = static_job(f, tt; name, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) - m = GPUCompiler.JuliaContext() do context - m, _ = GPUCompiler.compile(:llvm, job; kwargs...) - try - locate_pointers_and_runtime_calls(m) - catch - dispose(m) - rethrow() - end - m + with_module_context() do + compile_llvm_module(f, tt, name; demangle, target, kwargs...) end - return m end """ @@ -475,50 +470,85 @@ end static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) ``` Compile the methods of an array of tuples `(f, tt)` of functions and argument types to a -single `LLVM.Module`. +single `LLVM.Module`, which the caller owns. -The caller owns the returned module: dispose of it (`LLVM.dispose(mod)`) when done, or hand it -to an operation that consumes it (like `LLVM.link!(dst, mod)`). +$CONTEXT_DOC """ function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) + with_module_context() do + compile_llvm_module(funcs; demangle, target, kwargs...) + end +end + +# call `f` with the plain context of the active thread-safe context active, or of a new +# thread-safe context that is never disposed of, so that the modules `f` returns stay valid +function with_module_context(f) + ts_ctx = LLVM.ts_context(; throw_error=false) + ts_ctx === nothing || return context!(f, LLVM.context(ts_ctx)) + ts_ctx = GPUCompiler.JuliaContext() # activates it + ret = try + context!(f, LLVM.context(ts_ctx)) + catch + # nothing was returned that could use the context + dispose(ts_ctx) + rethrow() + end + LLVM.deactivate(ts_ctx) + return ret +end + +# compile to a module in the active (Julia) context, which must outlive the module +function compile_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) + if !demangle + name = "julia_"*name + end + job, kwargs = static_job(f, tt; name, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) + m, _ = GPUCompiler.compile(:llvm, job; kwargs...) + try + locate_pointers_and_runtime_calls(m) + catch + dispose(m) + rethrow() + end + return m +end + +function compile_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) f,tt = funcs[1] - mod = GPUCompiler.JuliaContext() do context - name_f = fix_name(f) - if !demangle - name_f = "julia_"*name_f - end - job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) - mod,_ = GPUCompiler.compile(:llvm, job; kwargs...) - try - if length(funcs) > 1 - for func in funcs[2:end] - f,tt = func - name_f = fix_name(f) - if !demangle - name_f = "julia_"*name_f - end - job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) - tmod,_ = GPUCompiler.compile(:llvm, job; kwargs...) - # consumes `tmod`, also when linking fails - link!(mod,tmod) + name_f = fix_name(f) + if !demangle + name_f = "julia_"*name_f + end + job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) + mod,_ = GPUCompiler.compile(:llvm, job; kwargs...) + try + if length(funcs) > 1 + for func in funcs[2:end] + f,tt = func + name_f = fix_name(f) + if !demangle + name_f = "julia_"*name_f end + job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) + tmod,_ = GPUCompiler.compile(:llvm, job; kwargs...) + # consumes `tmod`, also when linking fails + link!(mod,tmod) end - locate_pointers_and_runtime_calls(mod) - # Just to be sure - for (modfunc, func) in zip(mod.functions, funcs) - fname = modfunc.name - expectedname = (demangle ? "" : "julia_") * fix_name(func) - d = prefixlen(fname) - prefixlen(expectedname) + 1 - if d > 1 - modfunc.name = fname[d:end] - end + end + locate_pointers_and_runtime_calls(mod) + # Just to be sure + for (modfunc, func) in zip(mod.functions, funcs) + fname = modfunc.name + expectedname = (demangle ? "" : "julia_") * fix_name(func) + d = prefixlen(fname) - prefixlen(expectedname) + 1 + if d > 1 + modfunc.name = fname[d:end] end - run!(MergeFunctionsPass(), mod) - catch - dispose(mod) - rethrow() end - mod + run!(MergeFunctionsPass(), mod) + catch + dispose(mod) + rethrow() end return mod end @@ -602,27 +632,25 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file kwargs...) f, tt = funcs[1] mkpath(path) - @dispose mod=static_llvm_module(funcs; demangle, kwargs...) begin - dllexport && add_dllexport!(mod, funcs; demangle) - 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") - # emit code in the module's context, so that what code generation adds to the - # module belongs to it - obj = context!(mod.context) do + # compile and generate code in one context, which the module can't outlive + GPUCompiler.JuliaContext() do context + @dispose mod=compile_llvm_module(funcs; demangle, kwargs...) begin + dllexport && add_dllexport!(mod, funcs; demangle) + 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") fakejob, _ = static_job(f, tt; target, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) - obj - end - open(obj_path, "w") do io - write(io, obj) + open(obj_path, "w") do io + write(io, obj) + end + return path, obj_path end - return path, obj_path end end end From ab688dcd121ace5dbbd17df67837f80fc8c87036 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Mon, 12 Oct 2026 13:22:55 -0600 Subject: [PATCH 170/202] Bump version to v0.7.4 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c181f8e8..d3b65eaf 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.7.3" +version = "0.7.4" [deps] From 01c4ae049dde85976971dd318db9d7168c30c66c Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Tue, 3 Nov 2026 21:10:16 -0500 Subject: [PATCH 171/202] Extend compat on StaticTools to v0.9 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index d3b65eaf..362d6a2d 100644 --- a/Project.toml +++ b/Project.toml @@ -19,7 +19,7 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" GPUCompiler = "2" LLVM = "10" MacroTools = "0.5" -StaticTools = "0.8" +StaticTools = "0.8, 0.9" julia = "1.11" [extras] From 5300d038a1d6b595925f029b1b366f87049ffb84 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Tue, 3 Nov 2026 21:12:30 -0500 Subject: [PATCH 172/202] Fix apparent `target`-related issues --- src/StaticCompiler.jl | 56 ++++++++----------------------------------- src/target.jl | 2 +- 2 files changed, 11 insertions(+), 47 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index b5674634..5ef65d9d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -255,11 +255,8 @@ function generate_shlib_fptr(f, tt, path::String=tempname(), name=fix_name(f), f temp::Bool=true, kwargs...) - generate_shlib(f, tt, false, path, name; kwargs...) - lib_path = joinpath(abspath(path), "$filename.$(Libdl.dlext)") - ptr = Libdl.dlopen(lib_path, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, name) - @assert fptr != C_NULL + generate_shlib(f, tt, path, name, filename; kwargs...) + fptr = generate_shlib_fptr(path, name, filename) if temp atexit(()->rm(path; recursive=true)) end @@ -439,7 +436,7 @@ function static_code_typed(@nospecialize(func), @nospecialize(types); target::St end function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), kwargs...) - job, kwargs = static_job(f, tt; fname, target, kwargs...) + job, kwargs = static_job(f, tt; name=fname, target, kwargs...) GPUCompiler.code_native(stdout, job; libraries=false, kwargs...) end @@ -513,37 +510,13 @@ function compile_llvm_module(f, tt, name=fix_name(f); demangle=true, target::Sta return m end -function compile_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) - f,tt = funcs[1] - name_f = fix_name(f) - if !demangle - name_f = "julia_"*name_f - end - job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) - mod,_ = GPUCompiler.compile(:llvm, job; kwargs...) +function compile_llvm_module(funcs::Union{Array,Tuple}; kwargs...) + (f, tt), rest... = funcs + mod = compile_llvm_module(f, tt; kwargs...) try - if length(funcs) > 1 - for func in funcs[2:end] - f,tt = func - name_f = fix_name(f) - if !demangle - name_f = "julia_"*name_f - end - job, kwargs = static_job(f, tt; name = name_f, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) - tmod,_ = GPUCompiler.compile(:llvm, job; kwargs...) - # consumes `tmod`, also when linking fails - link!(mod,tmod) - end - end - locate_pointers_and_runtime_calls(mod) - # Just to be sure - for (modfunc, func) in zip(mod.functions, funcs) - fname = modfunc.name - expectedname = (demangle ? "" : "julia_") * fix_name(func) - d = prefixlen(fname) - prefixlen(expectedname) + 1 - if d > 1 - modfunc.name = fname[d:end] - end + for (f, tt) in rest + # consumes the module, also when linking fails + link!(mod, compile_llvm_module(f, tt; kwargs...)) end run!(MergeFunctionsPass(), mod) catch @@ -553,15 +526,6 @@ function compile_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::S return mod end -function prefixlen(s) - m = match(r"^(?:julia_)+", s) - if m isa RegexMatch - length(m.match) - else - 0 - end -end - """ ```julia generate_obj(f, tt, path::String = tempname(), filenamebase::String="obj"; @@ -634,7 +598,7 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file mkpath(path) # compile and generate code in one context, which the module can't outlive GPUCompiler.JuliaContext() do context - @dispose mod=compile_llvm_module(funcs; demangle, kwargs...) begin + @dispose mod=compile_llvm_module(funcs; demangle, target, kwargs...) begin dllexport && add_dllexport!(mod, funcs; demangle) if emit_llvm_only # (Required on Windows) ir_path = joinpath(path, "$filenamebase.ll") diff --git a/src/target.jl b/src/target.jl index 6c7b0d2f..b6fcd520 100644 --- a/src/target.jl +++ b/src/target.jl @@ -34,7 +34,7 @@ StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(p function StaticTarget(triple::String, cpu::String, features::String) platform = tryparse(Platform, triple) - StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = triple), triple; cpu, features), nothing) + StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = triple), triple; cpu, features), nothing, false) end """ From 1bede72112dbea1dd888ba496e087b0209d4f9be Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 4 Nov 2026 12:30:26 -0500 Subject: [PATCH 173/202] Rework pointer warning and signature checking --- src/StaticCompiler.jl | 52 ++++++++++++----------------- src/pointer_warning.jl | 76 ++++++++++++++++-------------------------- 2 files changed, 51 insertions(+), 77 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 5ef65d9d..b3a7d345 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -25,6 +25,21 @@ include("dllexport.jl") fix_name(f::Function) = fix_name(string(nameof(f))) fix_name(s) = String(GPUCompiler.safe_name(s)) +# shared library extension of the target (that of the host, unless cross-compiling) +dlext(target::StaticTarget) = isnothing(target.platform) ? Libdl.dlext : platform_dlext(target.platform) + + +# Check that `f(types...)` is compilable to a native entry point, returning the tuple type of `types` +function check_signature(f, types; target::StaticTarget=StaticTarget(), kwargs...) + tt = Base.to_tuple_type(types) + isconcretetype(tt) || error("input type signature `$types` is not concrete") + + rt = last(only(static_code_typed(f, tt; target, kwargs...))) + isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") + 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!" + return tt +end """ ```julia @@ -113,15 +128,10 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name= ) (f, types) = funcs[1] - tt = Base.to_tuple_type(types) + tt = check_signature(f, types; target, kwargs...) isexecutableargtype = tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" - rt = last(only(static_code_typed(f, tt; target, kwargs...))) - isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") - 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, llvm_to_clang, kwargs...) Sys.iswindows() && (filename *= ".exe") joinpath(abspath(path), filename) @@ -189,20 +199,13 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); llvm_to_clang = Sys.iswindows(), kwargs... ) - for func in funcs - f, types = func - tt = Base.to_tuple_type(types) - isconcretetype(tt) || error("input type signature `$types` is not concrete") - - rt = last(only(static_code_typed(f, tt; target, kwargs...))) - isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`") - 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!" + for (f, types) in funcs + check_signature(f, types; target, kwargs...) end generate_shlib(funcs, path, filename; demangle, cflags, target, llvm_to_clang, kwargs...) - joinpath(abspath(path), filename * "." * Libdl.dlext) + joinpath(abspath(path), filename * "." * dlext(target)) end @@ -315,8 +318,7 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi int main(int argc, char** argv) { - $fn(argc, argv); - return 0; + return $fn(argc, argv); }""") close(f) if llvm_to_clang # (required on Windows) @@ -393,14 +395,10 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file llvm_to_clang::Bool = Sys.iswindows(), kwargs... ) - if !isnothing(target.platform) - lib_path = joinpath(path, "$filename.$(platform_dlext(target.platform))") - else - lib_path = joinpath(path, "$filename.$(Libdl.dlext)") - end + lib_path = joinpath(path, "$filename.$(dlext(target))") _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, - dllexport=llvm_to_clang && Sys.iswindows(), kwargs...) + dllexport=Sys.iswindows(), kwargs...) # Pick a Clang if !isnothing(target.compiler) cc = `$(target.compiler)` @@ -531,8 +529,6 @@ end generate_obj(f, tt, path::String = tempname(), filenamebase::String="obj"; target::StaticTarget=StaticTarget(), demangle = true, - strip_llvm = false, - strip_asm = true, kwargs...) ``` Low level interface for compiling object code (`.o`) for for function `f` given @@ -572,8 +568,6 @@ generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase: demangle = false, emit_llvm_only = false, dllexport = false, - strip_llvm = false, - strip_asm = true, kwargs...) ``` Low level interface for compiling object code (`.o`) for an array of Tuples @@ -591,8 +585,6 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file demangle = true, emit_llvm_only = false, dllexport = false, - strip_llvm = false, - strip_asm = true, kwargs...) f, tt = funcs[1] mkpath(path) diff --git a/src/pointer_warning.jl b/src/pointer_warning.jl index 370234f8..3fe12309 100644 --- a/src/pointer_warning.jl +++ b/src/pointer_warning.jl @@ -1,33 +1,9 @@ function locate_pointers_and_runtime_calls(mod) - i64 = LLVM.IntType(64) - # d = IdDict{Any, Tuple{String, LLVM.GlobalVariable}}() for func ∈ mod.functions, bb ∈ func.blocks, inst ∈ bb.instructions + @debug "Inspecting instruction" inst warned = false - if isa(inst, LLVM.LoadInst) && occursin("inttoptr", string(inst)) - warned = inspect_pointers(mod, inst) - elseif isa(inst, LLVM.StoreInst) && occursin("inttoptr", string(inst)) - @debug "Inspecting StoreInst" inst - warned = inspect_pointers(mod, inst) - elseif inst isa LLVM.RetInst && occursin("inttoptr", string(inst)) - @debug "Inspecting RetInst" inst inst.operands - warned = inspect_pointers(mod, inst) - elseif isa(inst, LLVM.BitCastInst) && occursin("inttoptr", string(inst)) - @debug "Inspecting BitCastInst" inst inst.operands - warned = inspect_pointers(mod, inst) - elseif isa(inst, LLVM.CallInst) - @debug "Inspecting CallInst" inst inst.operands - dest = inst.called_operand - if occursin("inttoptr", string(dest)) && length(dest.operands) > 0 - @debug "Inspecting CallInst inttoptr" dest dest.operands inst.operands - ptr_arg = first(dest.operands) - ptr_val = convert(Int, ptr_arg) - ptr = Ptr{Cvoid}(ptr_val) - - frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) - - data_warnings(inst, frames) - warned = true - end + for operand ∈ pointer_operands(inst) + warned |= inspect_pointer(inst, operand) end if warned lines = split(string(func),"\n") @@ -37,22 +13,33 @@ function locate_pointers_and_runtime_calls(mod) end end -function inspect_pointers(mod, inst) - warned = false - jl_t = (LLVM.StructType(LLVM.LLVMType[])) - for (i, arg) ∈ enumerate(inst.operands) - if occursin("inttoptr", string(arg)) && arg isa LLVM.ConstantExpr - op1 = arg.operands[1] - if op1 isa LLVM.ConstantExpr - op1 = op1.operands[1] - end - ptr = Ptr{Cvoid}(convert(Int, op1)) - frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0) - data_warnings(inst, frames) - warned = true - end +# the operands of `inst` that may be pointers to julia data or runtime functions +pointer_operands(inst) = () +pointer_operands(inst::Union{LLVM.LoadInst, LLVM.StoreInst, LLVM.RetInst, LLVM.BitCastInst}) = inst.operands +pointer_operands(inst::LLVM.CallInst) = (inst.called_operand,) + +isinttoptr(x) = x isa LLVM.ConstantExpr && occursin("inttoptr", string(x)) + +# the address embedded in an `inttoptr` constant, or `nothing` if it is not a known constant +function constant_address(x) + op = x.operands[1] + if op isa LLVM.ConstantExpr + op = op.operands[1] + end + op isa LLVM.ConstantInt ? convert(Int, op) : nothing +end + +# warn if `operand` of `inst` is a raw pointer, returning whether it was +function inspect_pointer(inst, operand) + isinttoptr(operand) || return false + address = constant_address(operand) + if isnothing(address) + @warn("Found pointer reference to an unknown address", "llvm instruction" = inst) + else + frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), Ptr{Cvoid}(address), 0) + data_warnings(inst, frames) end - warned + return true end data_warnings(inst, frames) = for frame ∈ frames @@ -65,8 +52,3 @@ data_warnings(inst, frames) = for frame ∈ frames fromC = fromC, inlined = inlined) end - - - - - From 99039e7a36f4510d72ac950a348484f3ce5b5160 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 4 Nov 2026 13:36:38 -0500 Subject: [PATCH 174/202] Generally prefer Clang_jll clang, as system clang may not be new enough on older systems --- src/StaticCompiler.jl | 107 +++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index b3a7d345..e78f461e 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -5,7 +5,7 @@ using LLVM, LLVM.IR, LLVM.Passes using Libdl: Libdl, dlsym, dlopen using Base: RefValue using Serialization: serialize, deserialize -using Clang_jll: clang +using Clang_jll: Clang_jll using LLD_jll: lld using StaticTools using StaticTools: @symbolcall, @c_str, println @@ -25,6 +25,19 @@ include("dllexport.jl") fix_name(f::Function) = fix_name(string(nameof(f))) fix_name(s) = String(GPUCompiler.safe_name(s)) +# Generally prefer Clang_jll clang (which should probably be at least as new as Julia's LLVM) +# and fall back to the system one on platforms where Clang_jll is not available or not expected to work +function clang() + Clang_jll.is_available() || return `clang` # Fall back to system clang if Clang_jll is not available + cmd = Clang_jll.clang() + # Clang_jll may not know where to find the macOS SDK (e.g. for `-lSystem`) + if Sys.isapple() && !haskey(ENV, "SDKROOT") + isnothing(Sys.which("xcrun")) && return `clang` # Fall back to system clang if we can't figure out the SDK path + cmd = addenv(cmd, "SDKROOT" => readchomp(`xcrun --show-sdk-path`)) + end + return cmd +end + # shared library extension of the target (that of the host, unless cross-compiling) dlext(target::StaticTarget) = isnothing(target.platform) ? Libdl.dlext : platform_dlext(target.platform) @@ -43,15 +56,17 @@ end """ ```julia -compile_executable(f::Function, types::Tuple, path::String, [name::String=string(nameof(f))]; +compile_executable(f::Function, types::Tuple, [path::String=pwd()], [name::String=string(nameof(f))]; filename::String=name, - cflags=``, # Specify libraries you would like to link against, and other compiler options here - also_expose=[], + also_expose = [], target::StaticTarget=StaticTarget(), + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Specify libraries you would like to link against, and other compiler options here llvm_to_clang = Sys.iswindows(), - method_table=StaticCompiler.method_table, + method_table = StaticCompiler.method_table, kwargs... ) + ``` Attempt to compile a standalone executable that runs function `f` with a type signature given by the tuple of `types`. If there are extra methods you would like to protect from name mangling in the produced binary for whatever reason, @@ -113,16 +128,17 @@ Hello, world! ``` """ function compile_executable(f::Function, types=(), path::String=pwd(), name=fix_name(f); - also_expose=Tuple{Function, Tuple{DataType}}[], target::StaticTarget=StaticTarget(), + also_expose=Tuple{Function, Tuple{DataType}}[], kwargs...) - compile_executable(vcat([(f, types)], also_expose), path, name; target, kwargs...) + compile_executable(vcat([(f, types)], also_expose), path, name; kwargs...) end function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name=fix_name(first(first(funcs))); - filename = name, + filename::String = name, + target::StaticTarget=StaticTarget(), demangle = true, + cc = nothing, cflags = ``, - target::StaticTarget=StaticTarget(), llvm_to_clang = Sys.iswindows(), kwargs... ) @@ -132,7 +148,7 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name= isexecutableargtype = tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" - generate_executable(funcs, path, name, filename; demangle, cflags, target, llvm_to_clang, kwargs...) + generate_executable(funcs, path, name, filename; target, demangle, cc, cflags, llvm_to_clang, kwargs...) Sys.iswindows() && (filename *= ".exe") joinpath(abspath(path), filename) end @@ -141,17 +157,21 @@ end ```julia 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(), + demangle=true, + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Specify libraries you would like to link against, and other compiler options here + llvm_to_clang = Sys.iswindows(), + method_table = StaticCompiler.method_table, kwargs...) compile_shlib(funcs::Array, [path::String=pwd()]; filename="libfoo", - demangle=true, - cflags=``, - method_table=StaticCompiler.method_table, target::StaticTarget=StaticTarget(), + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Specify libraries you would like to link against, and other compiler options here + llvm_to_clang = Sys.iswindows(), + method_table = StaticCompiler.method_table, kwargs...) ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. @@ -184,18 +204,18 @@ julia> ccall(("test", "test.dylib"), Float64, (Int64,), 100_000) ``` """ function compile_shlib(f::Function, types=(), path::String=pwd(), name=fix_name(f); - filename=name, - target::StaticTarget=StaticTarget(), + filename = name, kwargs... ) - compile_shlib(((f, types),), path; filename, target, kwargs...) + compile_shlib(((f, types),), path; filename, kwargs...) end # As above, but taking an array of functions and returning a single shlib function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); filename = "libfoo", + target::StaticTarget=StaticTarget(), demangle = true, + cc = nothing, cflags = ``, - target::StaticTarget=StaticTarget(), llvm_to_clang = Sys.iswindows(), kwargs... ) @@ -203,7 +223,7 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); check_signature(f, types; target, kwargs...) end - generate_shlib(funcs, path, filename; demangle, cflags, target, llvm_to_clang, kwargs...) + generate_shlib(funcs, path, filename; target, demangle, cc, cflags, llvm_to_clang, kwargs...) joinpath(abspath(path), filename * "." * dlext(target)) end @@ -288,20 +308,18 @@ Hello, world! """ generate_executable(f, tt, args...; kwargs...) = generate_executable(((f, tt),), args...; kwargs...) function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fix_name(first(first(funcs))), filename=name; + target::StaticTarget=StaticTarget(), demangle = true, + cc = nothing, cflags = ``, - target::StaticTarget=StaticTarget(), llvm_to_clang::Bool = Sys.iswindows(), kwargs... - ) + ) + # Choice of compiler + isnothing(cc) && (cc = isnothing(target.compiler) ? clang() : `$(target.compiler)`) + # Prepare objects or IR exec_path = joinpath(path, filename) _, 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)` - else - cc = Sys.isapple() ? `cc` : clang() - end # Compile! if Sys.isapple() && !llvm_to_clang @@ -323,15 +341,8 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi close(f) if llvm_to_clang # (required on Windows) # Use clang (llc) to generate an executable from the LLVM IR - cclang = if Sys.iswindows() - exec_path *= ".exe" - `clang` - elseif Sys.isapple() - `clang` - else - clang() - end - run(`$cclang -Wno-override-module $wrapper_path $obj_or_ir_path -o $exec_path`) + Sys.iswindows() && (exec_path *= ".exe") + run(`$(clang()) -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 @@ -389,33 +400,23 @@ function generate_shlib(f::Function, tt, path::String=tempname(), name=fix_name( end # As above, but taking an array of functions and returning a single shlib function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), filename::String="libfoo"; + target::StaticTarget=StaticTarget(), demangle = true, + cc = nothing, cflags = ``, - target::StaticTarget=StaticTarget(), llvm_to_clang::Bool = Sys.iswindows(), kwargs... ) + # Choice of compiler + isnothing(cc) && (cc = isnothing(target.compiler) ? clang() : `$(target.compiler)`) + # Prepare objects or IR lib_path = joinpath(path, "$filename.$(dlext(target))") - _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, dllexport=Sys.iswindows(), kwargs...) - # Pick a Clang - if !isnothing(target.compiler) - cc = `$(target.compiler)` - else - cc = Sys.isapple() ? `cc` : clang() - end # Compile! if llvm_to_clang # (required on Windows) # Use clang (llc) to generate an executable from the LLVM IR - cclang = if Sys.iswindows() - `clang` - elseif Sys.isapple() - `clang` - else - clang() - end - run(`$cclang -shared -Wno-override-module $obj_or_ir_path -o $lib_path`) + run(`$(clang()) -shared -Wno-override-module $obj_or_ir_path -o $lib_path`) else run(`$cc -shared $cflags $obj_or_ir_path -o $lib_path `) end From 5d817d064f7d7d1676be4a0e0815cc8dd2efd524 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 4 Nov 2026 14:06:57 -0500 Subject: [PATCH 175/202] Streamline calling of clang/compiler on either IR or obj code --- src/StaticCompiler.jl | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index e78f461e..88029483 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -317,19 +317,23 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi ) # Choice of compiler isnothing(cc) && (cc = isnothing(target.compiler) ? clang() : `$(target.compiler)`) - # Prepare objects or IR + + # Prepare objects or (if llvm_to_clang is set) IR exec_path = joinpath(path, filename) _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, kwargs...) + # Update paths and flags given options + fn = demangle ? "$name" : "julia_$name" + Sys.iswindows() && (exec_path *= ".exe") + llvm_to_clang && (cflags = `-Wno-override-module $cflags`) + # Compile! 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_or_ir_path -o $exec_path`) + run(`$cc -e _$fn $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 - wrapper_path = joinpath(path, "wrapper.c") + wrapper_path = joinpath(path, "wrapper$(rand(UInt)).c") f = open(wrapper_path, "w") print(f, """int $fn(int argc, char** argv); void* __stack_chk_guard = (void*) $(rand(UInt) >> 1); @@ -339,18 +343,15 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi return $fn(argc, argv); }""") close(f) - if llvm_to_clang # (required on Windows) - # Use clang (llc) to generate an executable from the LLVM IR - Sys.iswindows() && (exec_path *= ".exe") - run(`$(clang()) -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 + + # Depending on whether llvm_to_clang is set, + # this may either be compiling from LLVM IR or just linking object files + run(`$cc $wrapper_path $cflags $obj_or_ir_path -o $exec_path`) # Clean up rm(wrapper_path) end - path, name + return path, name end @@ -396,7 +397,6 @@ julia> ccall(("test", "example/test.dylib"), Float64, (Int64,), 100_000) """ function generate_shlib(f::Function, tt, path::String=tempname(), name=fix_name(f), filename=name; target=StaticTarget(), kwargs...) generate_shlib(((f, tt),), path, filename; target, kwargs...) - path, name end # As above, but taking an array of functions and returning a single shlib function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), filename::String="libfoo"; @@ -409,19 +409,17 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file ) # Choice of compiler isnothing(cc) && (cc = isnothing(target.compiler) ? clang() : `$(target.compiler)`) - # Prepare objects or IR + + # Prepare objects or (if llvm_to_clang is set) IR lib_path = joinpath(path, "$filename.$(dlext(target))") _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, dllexport=Sys.iswindows(), kwargs...) + # Compile! - if llvm_to_clang # (required on Windows) - # Use clang (llc) to generate an executable from the LLVM IR - run(`$(clang()) -shared -Wno-override-module $obj_or_ir_path -o $lib_path`) - else - run(`$cc -shared $cflags $obj_or_ir_path -o $lib_path `) - end + llvm_to_clang && (cflags = `-Wno-override-module $cflags`) + run(`$cc -shared $cflags $obj_or_ir_path -o $lib_path `) - path, filename + return path, filename end function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) From 871b0e778ed0304d56823763df13828162753ac4 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 4 Nov 2026 14:07:25 -0500 Subject: [PATCH 176/202] Update quirks for new/upcoming boundserror functions --- src/quirks.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/quirks.jl b/src/quirks.jl index 4ffa97b6..c7cacb54 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -66,6 +66,14 @@ end @device_override @noinline Base.throw_boundserror(A, I) = @print_and_throw c"Out-of-bounds array access" +# used by `checkbounds` on recent Julia versions (nightly / 1.15+) +@static if isdefined(Base, :_throw_boundserror_indices) + @device_override @noinline Base._throw_boundserror_indices(A) = + @print_and_throw c"Out-of-bounds array access" + @device_override @noinline Base._throw_boundserror_indices(A, i1, I...) = + @print_and_throw c"Out-of-bounds array access" +end + # trig.jl @device_override @noinline Base.Math.sincos_domain_error(x) = @print_and_throw c"sincos(x) is only defined for finite x." From a6ff267596206acac3d5d63a9fe99cffd655a632 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 4 Nov 2026 22:01:03 -0500 Subject: [PATCH 177/202] Update puts definition --- test/testcore.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/testcore.jl b/test/testcore.jl index f77b0824..d53ddeff 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -54,17 +54,18 @@ end @test r.exitcode == 0 - @inline function _puts(s::Ptr{UInt8}) # Can't use Base.println because it allocates + @inline function _puts(s::Ptr{UInt8}) Base.llvmcall((""" ; External declaration of the puts function declare i32 @puts(i8* nocapture) nounwind - define i32 @main(i64) { + define i32 @main(i8* %str) #0 { entry: - %ptr = inttoptr i64 %0 to i8* - %status = call i32 (i8*) @puts(i8* %ptr) + %status = call i32 (i8*) @puts(i8* %str) ret i32 %status } + + attributes #0 = { alwaysinline nounwind ssp uwtable } """, "main"), Int32, Tuple{Ptr{UInt8}}, s) end From 5f771903e5435f12660312d4ecc49484c18946ee Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 4 Nov 2026 22:02:57 -0500 Subject: [PATCH 178/202] Add and test additional method overlays for more possible errors from Base --- src/quirks.jl | 101 ++++++++++++++++++++++++++++++++++++++++--- test/runtests.jl | 1 + test/testquirks.jl | 104 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 test/testquirks.jl diff --git a/src/quirks.jl b/src/quirks.jl index c7cacb54..6d476d49 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -32,19 +32,39 @@ macro print_and_throw(err) end libcexit(x::Int32) = @symbolcall exit(x::Int32)::Nothing +# error.jl +@device_override @noinline Base.error(::AbstractString) = + @print_and_throw c"error() called" +@device_override @noinline Base.error(::Vararg{Any}) = + @print_and_throw c"error() called" + +# tuple.jl and essentials.jl +@device_override @noinline Base.first(::Tuple{}) = + @print_and_throw c"Tuple must be non-empty" +@device_override @noinline Base.tail(::Tuple{}) = + @print_and_throw c"Cannot call tail on an empty tuple" + +# reduce.jl +@device_override @noinline Base._empty_reduce_error() = + @print_and_throw c"Reducing over an empty collection is not allowed" + +# char.jl +@device_override @noinline Base.throw_code_point_err(u::Integer) = + @print_and_throw c"Invalid Unicode code point" +@device_override @noinline Base.throw_invalid_char(c::AbstractChar) = + @print_and_throw c"Invalid character" + # math.jl @device_override @noinline Base.Math.throw_complex_domainerror(f::Symbol, x) = @print_and_throw c"This operation requires a complex input to return a complex result" @device_override @noinline Base.Math.throw_exp_domainerror(x) = @print_and_throw c"Exponentiation yielding a complex result requires a complex argument" +@device_override @noinline Base.Math.throw_complex_domainerror_neg1(f::Symbol, x) = + @print_and_throw c"This operation requires a complex input to return a complex result" # intfuncs.jl @device_override @noinline Base.throw_domerr_powbysq(::Any, p) = @print_and_throw c"Cannot raise an integer to a negative power" -@device_override @noinline Base.throw_domerr_powbysq(::Integer, p) = - @print_and_throw c"Cannot raise an integer to a negative power" -@device_override @noinline Base.throw_domerr_powbysq(::AbstractMatrix, p) = - @print_and_throw c"Cannot raise an integer to a negative power" @device_override @noinline Base.__throw_gcd_overflow(a, b) = @print_and_throw c"gcd overflow" @@ -55,7 +75,7 @@ libcexit(x::Int32) = @symbolcall exit(x::Int32)::Nothing @print_and_throw c"Negation overflowed" @device_override function Base.Checked.checked_abs(x::Base.Checked.SignedInt) r = ifelse(x < 0, -x, x) - r < 0 && @print_and_throw(c"checked arithmetic: cannot compute |x|") + r < 0 && @print_and_throw(c"Checked arithmetic: cannot compute |x|") r end @@ -63,6 +83,39 @@ end @device_override @noinline Core.throw_inexacterror(f::Symbol, ::Type{T}, val) where {T} = @print_and_throw c"Inexact conversion" +# float.jl: conversions of floats to integers throw an `InexactError` if out of range or inexact +for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128), + Tf in (Float16, Float32, Float64) + # the bounds are checked as in Base + if Ti <: Unsigned || sizeof(Ti) < sizeof(Tf) + inrange_round = :($(Tf(typemin(Ti)) - one(Tf)) < x < $(Tf(typemax(Ti)) + one(Tf))) + inrange = :($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)) + one(Tf))) + else + inrange_round = inrange = :($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))) + end + @eval begin + @device_override function Base.round(::Type{$Ti}, x::$Tf, ::RoundingMode{:ToZero}) + $inrange_round || @print_and_throw c"Inexact conversion" + unsafe_trunc($Ti, x) + end + @device_override function (::Type{$Ti})(x::$Tf) + ($inrange && isinteger(x)) || @print_and_throw c"Inexact conversion" + unsafe_trunc($Ti, x) + end + end +end + +# array shapes +@device_override @noinline Base.Broadcast.throwdm(axdest, axsrc) = + @print_and_throw c"Destination axes are not compatible with source axes" +@device_override @noinline Base.throw_setindex_mismatch(X, I) = + @print_and_throw c"Tried to assign the wrong number of elements" +@device_override @noinline Base.throw_checksize_error(A, sz) = + @print_and_throw c"Output array is the wrong size" +@device_override @noinline Base.throw_eachindex_mismatch_indices(::IndexStyle, inds...) = + @print_and_throw c"All inputs to eachindex must have the same indices" +@device_override @noinline Base._throw_argerror(s) = + @print_and_throw c"Invalid argument" @device_override @noinline Base.throw_boundserror(A, I) = @print_and_throw c"Out-of-bounds array access" @@ -77,6 +130,44 @@ end # trig.jl @device_override @noinline Base.Math.sincos_domain_error(x) = @print_and_throw c"sincos(x) is only defined for finite x." +# the trig domain error helpers differ between Julia versions +for f in (:sin_domain_error, :cos_domain_error, :tan_domain_error, :throw_finite_domainerror) + isdefined(Base.Math, f) && @eval @device_override @noinline Base.Math.$f(x) = + @print_and_throw c"Trigonometric functions are only defined for finite x." +end +for f in (:asin_domain_error, :acos_domain_error, :acosh_domain_error, :atanh_domain_error) + isdefined(Base.Math, f) && @eval @device_override @noinline Base.Math.$f(x) = + @print_and_throw c"Inverse (hyperbolic) trigonometric function called outside of its domain." +end + +# int.jl: the checked integer division intrinsics throw a `DivideError` from the generated +# code, so check the arguments ourselves and use the unchecked intrinsics +@device_override function Base.div(x::T, y::T) where {T<:Base.BitSigned} + y == 0 && @print_and_throw c"Integer division error" + y == -1 && x == typemin(T) && @print_and_throw c"Integer division error" + Core.Intrinsics.sdiv_int(x, y) +end +@device_override function Base.div(x::T, y::T) where {T<:Base.BitUnsigned} + y == 0 && @print_and_throw c"Integer division error" + Core.Intrinsics.udiv_int(x, y) +end +@device_override function Base.rem(x::T, y::T) where {T<:Base.BitSigned} + y == 0 && @print_and_throw c"Integer division error" + y == -1 ? zero(T) : Core.Intrinsics.srem_int(x, y) +end +@device_override function Base.rem(x::T, y::T) where {T<:Base.BitUnsigned} + y == 0 && @print_and_throw c"Integer division error" + Core.Intrinsics.urem_int(x, y) +end + +# rational.jl +@device_override @noinline Base.__throw_rational_argerror_zero(T) = + @print_and_throw c"Invalid rational: zero//zero" +@device_override @noinline Base.__throw_negate_unsigned() = + @print_and_throw c"Cannot negate unsigned number" +@device_override @noinline Base.__throw_rational_numerator_typemin(T) = + @print_and_throw c"Rational numerator is typemin" + @static if isdefined(StaticTools, :Bumper) Bumper = StaticTools.Bumper diff --git a/test/runtests.jl b/test/runtests.jl index b9d6780f..30ab8c41 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,6 +18,7 @@ const GROUP = get(ENV, "GROUP", "All") if GROUP == "Core" || GROUP == "All" include("testcore.jl") + include("testquirks.jl") end if GROUP == "Integration" || GROUP == "All" diff --git a/test/testquirks.jl b/test/testquirks.jl new file mode 100644 index 00000000..d0ad2aca --- /dev/null +++ b/test/testquirks.jl @@ -0,0 +1,104 @@ +# Functions that can throw errors which need the Julia runtime to be thrown, unless StaticCompiler +# overrides them (see src/quirks.jl). These need to be defined globally due to +# https://github.com/JuliaLang/julia/issues/40990 +quirk_div(x::Int, y::Int) = div(x, y) +quirk_rem(x::Int, y::Int) = rem(x, y) +quirk_mod(x::Int, y::Int) = mod(x, y) +quirk_fld(x::Int, y::Int) = fld(x, y) +quirk_cld(x::Int, y::Int) = cld(x, y) +quirk_divrem(x::Int, y::Int) = first(divrem(x, y)) +quirk_udiv(x::UInt, y::UInt) = div(x, y) +quirk_urem(x::UInt, y::UInt) = rem(x, y) +quirk_i32div(x::Int32, y::Int32) = div(x, y) +quirk_i128div(x::Int, y::Int) = div(Int128(x) << 70, Int128(y)) % Int +quirk_i128rem(x::Int, y::Int) = rem(Int128(x) << 70 + 5, Int128(y)) % Int +quirk_u128rem(x::UInt, y::UInt) = rem(UInt128(x) << 70 + 5, UInt128(y)) % UInt +quirk_int(x::Float64) = Int(x) +quirk_trunc(x::Float64) = trunc(Int, x) +quirk_round(x::Float64) = round(Int, x) +quirk_floor(x::Float64) = floor(Int, x) +quirk_ceil(x::Float64) = ceil(Int, x) +quirk_u8(x::Float64) = UInt8(x) +quirk_i32(x::Float32) = Int32(x) +quirk_acos(x::Float64) = acos(x) +quirk_char(x::Int) = Int(Char(x)) +quirk_rational(x::Int, y::Int) = (x // y).num + +# the same, but triggering the error +quirk_divzero(argc::Int, argv::Ptr{Ptr{UInt8}}) = div(argc, argc - 1) +quirk_remzero(argc::Int, argv::Ptr{Ptr{UInt8}}) = rem(argc, argc - 1) +quirk_divovf(argc::Int, argv::Ptr{Ptr{UInt8}}) = div(typemin(Int) + argc - 1, -argc) +quirk_i128zero(argc::Int, argv::Ptr{Ptr{UInt8}}) = div(Int128(argc), Int128(argc - 1)) % Int +quirk_inexact(argc::Int, argv::Ptr{Ptr{UInt8}}) = Int(argc + 0.5) +quirk_acoserr(argc::Int, argv::Ptr{Ptr{UInt8}}) = round(Int, acos(argc + 1.0)) +quirk_emptyfirst(argc::Int, argv::Ptr{Ptr{UInt8}}) = (first(()); 0) +quirk_error(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc > 0 && error("bad"); 0) + +@testset "Overridden error paths" begin + workdir = mktempdir() + + # The functions link without the Julia runtime and agree with Base when no error is thrown + funcs = [(quirk_div, (Int, Int)), (quirk_rem, (Int, Int)), (quirk_mod, (Int, Int)), + (quirk_fld, (Int, Int)), (quirk_cld, (Int, Int)), (quirk_divrem, (Int, Int)), + (quirk_udiv, (UInt, UInt)), (quirk_urem, (UInt, UInt)), (quirk_i32div, (Int32, Int32)), + (quirk_i128div, (Int, Int)), (quirk_i128rem, (Int, Int)), (quirk_u128rem, (UInt, UInt)), + (quirk_int, (Float64,)), (quirk_trunc, (Float64,)), (quirk_round, (Float64,)), + (quirk_floor, (Float64,)), (quirk_ceil, (Float64,)), (quirk_u8, (Float64,)), + (quirk_i32, (Float32,)), (quirk_acos, (Float64,)), (quirk_char, (Int,)), + (quirk_rational, (Int, Int))] + path = compile_shlib(funcs, workdir; filename="libquirks") + lib = Libdl.dlopen(path, Libdl.RTLD_LOCAL) + sym(f) = Libdl.dlsym(lib, string(nameof(f))) + + xs = (-7, -1, 0, 1, 5, 123456789, typemax(Int)) + ys = (-3, -1, 1, 2, 7, typemax(Int)) + for f in (quirk_div, quirk_rem, quirk_mod, quirk_fld, quirk_cld, quirk_divrem), + x in xs, y in ys + @test ccall(sym(f), Int, (Int, Int), x, y) == f(x, y) + end + # `rem` and `mod` by -1 are fine even for typemin + @test ccall(sym(quirk_rem), Int, (Int, Int), typemin(Int), -1) == 0 + @test ccall(sym(quirk_mod), Int, (Int, Int), typemin(Int), -1) == 0 + for x in UInt.((0, 1, 7)) ∪ typemax(UInt), y in UInt.((1, 2, 7)) ∪ typemax(UInt) + @test ccall(sym(quirk_udiv), UInt, (UInt, UInt), x, y) == quirk_udiv(x, y) + @test ccall(sym(quirk_urem), UInt, (UInt, UInt), x, y) == quirk_urem(x, y) + end + for f in (quirk_i128div, quirk_i128rem), x in xs, y in ys + @test ccall(sym(f), Int, (Int, Int), x, y) == f(x, y) + end + for x in UInt.((0, 1, 7)) ∪ typemax(UInt), y in UInt.((1, 2, 7)) ∪ typemax(UInt) + @test ccall(sym(quirk_u128rem), UInt, (UInt, UInt), x, y) == quirk_u128rem(x, y) + end + @test ccall(sym(quirk_i32div), Int32, (Int32, Int32), Int32(-9), Int32(2)) == -4 + for x in (-2.5, -1.5, -0.0, 0.0, 0.4, 0.5, 1.5, 2.5, 3.7, -3.7, 1e10, -1e10) + for f in (quirk_trunc, quirk_round, quirk_floor, quirk_ceil) + @test ccall(sym(f), Int, (Float64,), x) == f(x) + end + end + for x in (-3.0, 0.0, 7.0, 1e10) + @test ccall(sym(quirk_int), Int, (Float64,), x) == Int(x) + end + @test ccall(sym(quirk_u8), UInt8, (Float64,), 200.0) == 200 + @test ccall(sym(quirk_i32), Int32, (Float32,), -5f0) == -5 + @test ccall(sym(quirk_acos), Float64, (Float64,), 0.5) == acos(0.5) + @test ccall(sym(quirk_char), Int, (Int,), 0x41) == 0x41 + @test ccall(sym(quirk_rational), Int, (Int, Int), 6, 4) == 3 + Libdl.dlclose(lib) + + # The errors exit with a message instead + function run_quirk(f) + path = compile_executable(f, (Int, Ptr{Ptr{UInt8}}), workdir) + proc = open(ignorestatus(`$path`)) + out = read(proc, String) + wait(proc) + proc.exitcode, out + end + for (f, msg) in ((quirk_divzero, "Integer division error"), (quirk_remzero, "Integer division error"), + (quirk_divovf, "Integer division error"), (quirk_i128zero, "Integer division error"), (quirk_inexact, "Inexact conversion"), + (quirk_acoserr, "domain"), (quirk_emptyfirst, "Tuple must be non-empty"), + (quirk_error, "error() called")) + code, out = run_quirk(f) + @test code == 1 + @test occursin(msg, out) + end +end From db19fb772e1ab825a5e68cd5e6abe0d6306e1c8c Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Fri, 6 Nov 2026 21:00:23 -0500 Subject: [PATCH 179/202] Bump version to v0.7.5 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 362d6a2d..6c7e25a3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.7.4" +version = "0.7.5" [deps] From 44cf7e73a1a0e04523589f584a89454d8efdec78 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sat, 7 Nov 2026 10:29:37 -0500 Subject: [PATCH 180/202] Add optional `llvm_postprocess` hook --- src/StaticCompiler.jl | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 88029483..06463aca 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -64,6 +64,7 @@ compile_executable(f::Function, types::Tuple, [path::String=pwd()], [name::Strin cflags = ``, # Specify libraries you would like to link against, and other compiler options here llvm_to_clang = Sys.iswindows(), method_table = StaticCompiler.method_table, + llvm_postprocess = nothing, kwargs... ) @@ -72,6 +73,9 @@ Attempt to compile a standalone executable that runs function `f` with a type si If there are extra methods you would like to protect from name mangling in the produced binary for whatever reason, you can provide them as a vector of tuples of functions and types, i.e. `[(f1, types1), (f2, types2), ...]` +`llvm_postprocess`, if given, is called with each `LLVM.Module` that is generated, before it is checked and compiled, +and may modify it in place (for instance, to define the runtime functions that the code calls). + ### Examples ```julia julia> using StaticCompiler @@ -422,17 +426,17 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file return path, filename end -function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) +function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) job, kwargs = static_job(func, types; target, kwargs...) GPUCompiler.code_llvm(stdout, job; libraries=false, kwargs...) end -function static_code_typed(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) +function static_code_typed(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) job, kwargs = static_job(func, types; target, kwargs...) GPUCompiler.code_typed(job; kwargs...) end -function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), kwargs...) +function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) job, kwargs = static_job(f, tt; name=fname, target, kwargs...) GPUCompiler.code_native(stdout, job; libraries=false, kwargs...) end @@ -492,13 +496,14 @@ function with_module_context(f) end # compile to a module in the active (Julia) context, which must outlive the module -function compile_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) +function compile_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) if !demangle name = "julia_"*name end job, kwargs = static_job(f, tt; name, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) m, _ = GPUCompiler.compile(:llvm, job; kwargs...) try + isnothing(llvm_postprocess) || llvm_postprocess(m) locate_pointers_and_runtime_calls(m) catch dispose(m) @@ -584,12 +589,13 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file demangle = true, emit_llvm_only = false, dllexport = false, + llvm_postprocess = nothing, kwargs...) f, tt = funcs[1] mkpath(path) # compile and generate code in one context, which the module can't outlive GPUCompiler.JuliaContext() do context - @dispose mod=compile_llvm_module(funcs; demangle, target, kwargs...) begin + @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin dllexport && add_dllexport!(mod, funcs; demangle) if emit_llvm_only # (Required on Windows) ir_path = joinpath(path, "$filenamebase.ll") From 6a32270945a8125f65d8ef3914d7c0412bee3290 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sat, 7 Nov 2026 10:34:36 -0500 Subject: [PATCH 181/202] Allow more choice in kwargs passed to `static_job` --- src/StaticCompiler.jl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 06463aca..9d2e3864 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -496,11 +496,20 @@ function with_module_context(f) end # compile to a module in the active (Julia) context, which must outlive the module -function compile_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) +function compile_llvm_module(f, tt, name=fix_name(f); + target::StaticTarget=StaticTarget(), + demangle=true, + strip=true, + only_entry=false, + validate=false, + libraries=false, + llvm_postprocess=nothing, + kwargs..., + ) if !demangle name = "julia_"*name end - job, kwargs = static_job(f, tt; name, target, strip=true, only_entry=false, validate=false, libraries=false, kwargs...) + job, kwargs = static_job(f, tt; name, target, strip, only_entry, validate, libraries, kwargs...) m, _ = GPUCompiler.compile(:llvm, job; kwargs...) try isnothing(llvm_postprocess) || llvm_postprocess(m) From 1534241522a0d6076a7da24e52723247146078a8 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sun, 8 Nov 2026 00:47:58 -0500 Subject: [PATCH 182/202] Allow passing of `AbstractCompilerParams` as well --- src/StaticCompiler.jl | 9 +++++++-- src/target.jl | 12 +++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 9d2e3864..2e9aef70 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -73,8 +73,13 @@ Attempt to compile a standalone executable that runs function `f` with a type si If there are extra methods you would like to protect from name mangling in the produced binary for whatever reason, you can provide them as a vector of tuples of functions and types, i.e. `[(f1, types1), (f2, types2), ...]` -`llvm_postprocess`, if given, is called with each `LLVM.Module` that is generated, before it is checked and compiled, -and may modify it in place (for instance, to define the runtime functions that the code calls). +The `llvm_postprocess` keyword, if given, allows users to specify a function that will be called on each `LLVM.Module` +that is generated, before it is checked and compiled, and may modify it in place +(for instance, to define the runtime functions that the code calls). + +Other keywords will be forwarded to `static_job`. This may include `params`, which if given, +is a `GPUCompiler.AbstractCompilerParams` object (default `StaticCompilerParams()`) which +is passed to `GPUCompiler.CompilerConfig`. ### Examples ```julia diff --git a/src/target.jl b/src/target.jl index b6fcd520..4f0af7b6 100644 --- a/src/target.jl +++ b/src/target.jl @@ -93,27 +93,29 @@ function GPUCompiler.llvm_machine(target::StaticCompilerTarget) end GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = StaticRuntime -GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = StaticRuntime -GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget, StaticCompilerParams}) = true GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = true GPUCompiler.uses_julia_runtime(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = job.config.target.julia_runtime GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget})) = job.config.target.method_table +# `params` is the `GPUCompiler.AbstractCompilerParams` of the job. Other packages can pass a type of their own, to +# specialize GPUCompiler's functions (`inference_params`, `optimize!`, ...) for their compilations only, without +# adding methods for `StaticCompilerTarget` and `StaticCompilerParams`, which are ours. All the methods above +# are for any `params`. function static_job(@nospecialize(func), @nospecialize(types); name = fix_name(func), kernel::Bool = false, target::StaticTarget = StaticTarget(), - method_table=method_table, + params::GPUCompiler.AbstractCompilerParams = StaticCompilerParams(), + method_table = method_table, kwargs... ) source = GPUCompiler.methodinstance(typeof(func), Base.to_tuple_type(types)) tm = target.tm gputarget = StaticCompilerTarget(tm.triple, tm.cpu, tm.features, target.julia_runtime, method_table) - params = StaticCompilerParams() - config = GPUCompiler.CompilerConfig(gputarget, params; name = name, kernel = kernel, kwargs...) + config = GPUCompiler.CompilerConfig(gputarget, params; name, kernel, kwargs...) return GPUCompiler.CompilerJob(source, config), Dict{}() end From a3592136196c17546b2796957e08433a3e0c7bf3 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sun, 8 Nov 2026 13:14:20 -0500 Subject: [PATCH 183/202] Copy over updated integration test suite from StaticTools.jl --- test/scripts/inbounds_matrix.jl | 52 ++++ test/scripts/inbounds_matrix_stack.jl | 48 ++++ test/scripts/inbounds_product.jl | 24 ++ test/scripts/interop.jl | 3 +- test/scripts/iterate.jl | 13 + test/scripts/loopvec_matrix.jl | 6 +- test/scripts/loopvec_matrix_stack.jl | 3 +- test/scripts/matmul.jl | 38 +++ test/scripts/matmul_stack.jl | 35 +++ test/scripts/rand_matrix.jl | 4 +- test/scripts/randn_matrix.jl | 4 +- test/scripts/readwrite.jl | 45 ++++ test/scripts/stack_times_table.jl | 20 ++ test/scripts/times_table.jl | 3 +- test/scripts/withmallocarray.jl | 4 +- test/testintegration.jl | 348 +++++++++++++++++++------- 16 files changed, 542 insertions(+), 108 deletions(-) create mode 100644 test/scripts/inbounds_matrix.jl create mode 100644 test/scripts/inbounds_matrix_stack.jl create mode 100644 test/scripts/inbounds_product.jl create mode 100644 test/scripts/iterate.jl create mode 100644 test/scripts/matmul.jl create mode 100644 test/scripts/matmul_stack.jl create mode 100644 test/scripts/readwrite.jl create mode 100644 test/scripts/stack_times_table.jl diff --git a/test/scripts/inbounds_matrix.jl b/test/scripts/inbounds_matrix.jl new file mode 100644 index 00000000..efe0aec5 --- /dev/null +++ b/test/scripts/inbounds_matrix.jl @@ -0,0 +1,52 @@ +using StaticCompiler +using StaticTools + +@inline function mul!(C::MallocArray, A::MallocArray, B::MallocArray) + @inbounds for n ∈ axes(C, 2), m ∈ axes(C, 1) + Cmn = zero(eltype(C)) + for k ∈ axes(A, 2) + Cmn += A[m,k] * B[k,n] + end + C[m,n] = Cmn + end + return C +end + +function inbounds_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), 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 + + # LHS + A = MallocArray{Float64}(undef, rows, cols) + @inbounds for i ∈ axes(A, 1) + for j ∈ axes(A, 2) + A[i,j] = i*j + end + end + + # RHS + B = MallocArray{Float64}(undef, cols, rows) + @inbounds for i ∈ axes(B, 1) + for j ∈ axes(B, 2) + B[i,j] = i*j + end + end + + # # Matrix multiplication + C = MallocArray{Float64}(undef, cols, cols) + mul!(C, B, A) + + # Print to stdout + printf(C) + # Also print to file + printdlm(c"table.tsv", C, '\t') + fwrite(c"table.b", C) + # Clean up matrices + free(A) + free(B) + free(C) +end + +# Attempt to compile +path = compile_executable(inbounds_matrix, (Int64, Ptr{Ptr{UInt8}}), "./") diff --git a/test/scripts/inbounds_matrix_stack.jl b/test/scripts/inbounds_matrix_stack.jl new file mode 100644 index 00000000..1acf0c98 --- /dev/null +++ b/test/scripts/inbounds_matrix_stack.jl @@ -0,0 +1,48 @@ +using StaticCompiler +using StaticTools + +@inline function mul!(C::StackArray, A::StackArray, B::StackArray) + @inbounds for n ∈ axes(C, 2), m ∈ axes(C, 1) + Cmn = zero(eltype(C)) + for k ∈ axes(A, 2) + Cmn += A[m,k] * B[k,n] + end + C[m,n] = Cmn + end + return C +end + +function inbounds_matrix_stack() + rows = 10 + cols = 5 + + # LHS + A = StackArray{Float64}(undef, rows, cols) + @inbounds for i ∈ axes(A, 1) + for j ∈ axes(A, 2) + A[i,j] = i*j + end + end + + # RHS + B = StackArray{Float64}(undef, cols, rows) + @inbounds for i ∈ axes(B, 1) + for j ∈ axes(B, 2) + B[i,j] = i*j + end + end + + # # Matrix multiplication + C = StackArray{Float64}(undef, cols, cols) + mul!(C, B, A) + + # Print to stdout + printf(C) + # Also print to file + fp = fopen(c"table.tsv",c"w") + printf(fp, C) + fclose(fp) +end + +# Attempt to compile +path = compile_executable(inbounds_matrix_stack, (), "./") diff --git a/test/scripts/inbounds_product.jl b/test/scripts/inbounds_product.jl new file mode 100644 index 00000000..e80b5403 --- /dev/null +++ b/test/scripts/inbounds_product.jl @@ -0,0 +1,24 @@ +using StaticCompiler +using StaticTools + +function inbounds_product(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), 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 + + s = 0 + @inbounds for i=1:rows + for j=1:cols + s += i*j + end + end + # Print result to stdout + printf(s) + # Also print to file + fp = fopen(c"product.tsv",c"w") + printf(fp, s) + fclose(fp) +end + +# Attempt to compile +path = compile_executable(inbounds_product, (Int64, Ptr{Ptr{UInt8}}), "./") diff --git a/test/scripts/interop.jl b/test/scripts/interop.jl index b601e090..5729725f 100644 --- a/test/scripts/interop.jl +++ b/test/scripts/interop.jl @@ -2,7 +2,8 @@ using StaticCompiler using StaticTools function interop(argc, argv) - lib = StaticTools.dlopen(c"libm") + # On Linux, `libm.so` is typically a linker script (or absent), so use the versioned soname + lib = StaticTools.dlopen(@static(Sys.islinux() ? c"libm.so.6" : c"libm")) printf(lib) sin = StaticTools.dlsym(lib, c"sin") printf(sin) diff --git a/test/scripts/iterate.jl b/test/scripts/iterate.jl new file mode 100644 index 00000000..cdc4b9d9 --- /dev/null +++ b/test/scripts/iterate.jl @@ -0,0 +1,13 @@ +using StaticCompiler +using StaticTools + +function iterate() + if startswith(c"foobar", c"foo") + printf(c"We have iterated!\n") + return Int32(0) + end + return Int32(1) +end + +# Attempt to compile +path = compile_executable(iterate, (), "./") diff --git a/test/scripts/loopvec_matrix.jl b/test/scripts/loopvec_matrix.jl index 12e80f9e..7b19ce86 100644 --- a/test/scripts/loopvec_matrix.jl +++ b/test/scripts/loopvec_matrix.jl @@ -39,11 +39,9 @@ function loopvec_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) mul!(C, B, A) # Print to stdout - printf(c"C matrix = \n") - print(C) + printf(C) # Also print to file - # Re-enable when StaticTools has been adapted for opaque pointers - # printdlm(c"table.tsv", C, '\t') + printdlm(c"table.tsv", C, '\t') fwrite(c"table.b", C) # Clean up matrices free(A) diff --git a/test/scripts/loopvec_matrix_stack.jl b/test/scripts/loopvec_matrix_stack.jl index 7cf31f52..5e0c90d7 100644 --- a/test/scripts/loopvec_matrix_stack.jl +++ b/test/scripts/loopvec_matrix_stack.jl @@ -3,7 +3,7 @@ using StaticTools using LoopVectorization @inline function mul!(C::StackArray, A::StackArray, B::StackArray) - @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) + @turbo for n ∈ indices((C,B), 2), m ∈ indices((C,A), 1) Cmn = zero(eltype(C)) for k ∈ indices((A,B), (2,1)) Cmn += A[m,k] * B[k,n] @@ -38,7 +38,6 @@ function loopvec_matrix_stack() mul!(C, B, A) # Print to stdout - printf(c"C matrix = \n") printf(C) # Also print to file fp = fopen(c"table.tsv",c"w") diff --git a/test/scripts/matmul.jl b/test/scripts/matmul.jl new file mode 100644 index 00000000..6511bc2c --- /dev/null +++ b/test/scripts/matmul.jl @@ -0,0 +1,38 @@ +using StaticCompiler +using StaticTools + +function matmul(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), 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 + + A = MallocArray{Float64}(undef, rows, cols) + @inbounds for i ∈ axes(A, 1) + for j ∈ axes(A, 2) + A[i,j] = i*j + end + end + + B = MallocArray{Float64}(undef, cols, rows) + @inbounds for i ∈ axes(B, 1) + for j ∈ axes(B, 2) + B[i,j] = i*j + end + end + + # Matrix multiplication + C = B * A + + # Print to stdout + printf(C) + # Also print to file + printdlm(c"table.tsv", C, '\t') + fwrite(c"table.b", C) + # Clean up matrices + free(A) + free(B) + # free(C) +end + +# Attempt to compile +path = compile_executable(matmul, (Int64, Ptr{Ptr{UInt8}}), "./") diff --git a/test/scripts/matmul_stack.jl b/test/scripts/matmul_stack.jl new file mode 100644 index 00000000..5fad70a1 --- /dev/null +++ b/test/scripts/matmul_stack.jl @@ -0,0 +1,35 @@ +using StaticCompiler +using StaticTools + +function matmul_stack() + rows = 10 + cols = 5 + + A = szeros(rows, cols) + @inbounds for i ∈ axes(A, 1) + for j ∈ axes(A, 2) + A[i,j] = i*j + end + end + + B = szeros(cols, rows) + @inbounds for i ∈ axes(B, 1) + for j ∈ axes(B, 2) + B[i,j] = i*j + end + end + + # Matrix multiplication + C = B * A + + # Print to stdout + printf(C) + # Also print to file + printdlm(c"table.tsv", C, '\t') + fwrite(c"table.b", C) + + return Int32(0) +end + +# Attempt to compile +path = compile_executable(matmul_stack, (), "./") diff --git a/test/scripts/rand_matrix.jl b/test/scripts/rand_matrix.jl index 604e44a0..184eb58a 100644 --- a/test/scripts/rand_matrix.jl +++ b/test/scripts/rand_matrix.jl @@ -19,5 +19,5 @@ function rand_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) end # Attempt to compile -# cflags=`-lm`: need to explicitly include libm math library on linux -path = compile_executable(rand_matrix, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-lm`) +# cflags=`-lm`: need to explicitly include libm math library on linux (not needed, or available, on Windows) +path = compile_executable(rand_matrix, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=(Sys.iswindows() ? `` : `-lm`)) diff --git a/test/scripts/randn_matrix.jl b/test/scripts/randn_matrix.jl index 065d4662..b5fa9d24 100644 --- a/test/scripts/randn_matrix.jl +++ b/test/scripts/randn_matrix.jl @@ -18,5 +18,5 @@ function randn_matrix(argc::Int, argv::Ptr{Ptr{UInt8}}) end # Attempt to compile -# cflags=`-lm`: need to explicitly include libm math library on linux -path = compile_executable(randn_matrix, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-lm`) +# cflags=`-lm`: need to explicitly include libm math library on linux (not needed, or available, on Windows) +path = compile_executable(randn_matrix, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=(Sys.iswindows() ? `` : `-lm`)) diff --git a/test/scripts/readwrite.jl b/test/scripts/readwrite.jl new file mode 100644 index 00000000..0e3987cb --- /dev/null +++ b/test/scripts/readwrite.jl @@ -0,0 +1,45 @@ +using StaticCompiler +using StaticTools + +function readwrite(argc::Int, argv::Ptr{Ptr{UInt8}}) + argc == 3 || return printf(stderrp(), 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 + + M = MallocArray{Int64}(undef, rows, cols) + @inbounds for i=1:rows + for j=1:cols + M[i,j] = i*j + end + end + # Print to file + fwrite(c"table.b", M) + printdlm(c"table.tsv", M) + + Mb = read(c"table.b", MallocArray{Int64}) + Mbv = reshape(Mb, (rows, cols)) + printdlm(c"tableb.tsv", Mbv) + printf(Mbv) + + Ms = read(c"table.b", MallocString) + Msv = ArrayView{Int64,2}(pointer(Ms), rows*cols, (rows, cols)) + printdlm(c"tables.tsv", Msv) + printf(Msv) + + Mts = read(c"table.tsv", MallocString) + fwrite(c"tablets.tsv", Mts) + printf(Mts) + + # Mt = parsedlm(Int64, c"table.tsv") + + # match = M == Mb == Mbv == Msv + + # Clean up + free(M), free(Mb), free(Ms), free(Mts) + + # return !match % Int32 + return Int32(0) +end + +# Attempt to compile +path = compile_executable(readwrite, (Int64, Ptr{Ptr{UInt8}}), "./") diff --git a/test/scripts/stack_times_table.jl b/test/scripts/stack_times_table.jl new file mode 100644 index 00000000..778349e0 --- /dev/null +++ b/test/scripts/stack_times_table.jl @@ -0,0 +1,20 @@ +using StaticCompiler +using StaticTools + +function stack_times_table() + a = StackArray{Int64}(undef, 5, 5) + for i ∈ axes(a,1) + for j ∈ axes(a,2) + a[i,j] = i*j + end + end + print(a) + fwrite(c"table.b", a) + GC.@preserve a printdlm(c"table.tsv", a) + + # Test reinterpreting + println(c"\nThe same array, reinterpreted as Int32:") + GC.@preserve a print(reinterpret(Int32, a)) +end + +path = compile_executable(stack_times_table, (), "./") diff --git a/test/scripts/times_table.jl b/test/scripts/times_table.jl index 3f8a309b..7d295eb0 100644 --- a/test/scripts/times_table.jl +++ b/test/scripts/times_table.jl @@ -16,8 +16,7 @@ function times_table(argc::Int, argv::Ptr{Ptr{UInt8}}) printf(M) # Also print to file fwrite(c"table.b", M) - # Re-enable when StaticTools has been adapted for opaque pointers - # printdlm(c"table.tsv", M) + printdlm(c"table.tsv", M) # Clean up matrix free(M) end diff --git a/test/scripts/withmallocarray.jl b/test/scripts/withmallocarray.jl index 29301214..998d7fde 100644 --- a/test/scripts/withmallocarray.jl +++ b/test/scripts/withmallocarray.jl @@ -27,5 +27,5 @@ function withmallocarray(argc::Int, argv::Ptr{Ptr{UInt8}}) end # Attempt to compile -# cflags=`-lm`: need to explicitly include libm math library on linux -path = compile_executable(withmallocarray, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=`-lm`) +# cflags=`-lm`: need to explicitly include libm math library on linux (not needed, or available, on Windows) +path = compile_executable(withmallocarray, (Int64, Ptr{Ptr{UInt8}}), "./", cflags=(Sys.iswindows() ? `` : `-lm`)) diff --git a/test/testintegration.jl b/test/testintegration.jl index 22b1ceac..640255fe 100644 --- a/test/testintegration.jl +++ b/test/testintegration.jl @@ -27,6 +27,9 @@ end @test bumper_test(8) == @ccall($fptr(8::Int)::Float64) end +# To remove output files +cleanup(files...) = foreach(f -> isfile(f) && rm(f), files) + @testset "Standalone Executable Integration" begin jlpath = joinpath(Sys.BINDIR, Base.julia_exename()) # Get path to julia executable @@ -37,9 +40,9 @@ end # We have to start a new Julia process to get around the fact that Pkg.test # disables `@inbounds`, but ironically we can use `--compile=min` to make that # faster. + cleanup("times_table") status = -1 try - isfile("times_table") && rm("times_table") status = run(`$jlpath --startup=no --compile=min $testpath/scripts/times_table.jl`) catch e @warn "Could not compile $testpath/scripts/times_table.jl" @@ -49,10 +52,11 @@ end @test isa(status, Base.Process) && status.exitcode == 0 # Attempt to run - println("5x5 times table:") + cleanup("table.tsv", "table.b") + println("4x4 times table (MallocArray):") status = -1 try - status = run(`./times_table 5 5`) + status = run(`./times_table 4 4`) catch e @warn "Could not run $(scratch)/times_table" println(e) @@ -60,17 +64,89 @@ end @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 # Test ascii output - @test_broken parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' broken=Sys.isapple() + @test parsedlm(Int, c"table.tsv", '\t') == (1:4)*(1:4)' + # Test binary output + @test fread!(szeros(Int, 4,4), c"table.b") == (1:4)*(1:4)' + end + + ## --- As above but with staticarray + + let + # Compile... + cleanup("stack_times_table") + status = -1 + try + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/stack_times_table.jl`) + catch e + @warn "Could not compile $testpath/scripts/stack_times_table.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run.. + cleanup("table.tsv", "table.b") + println("5x5 times table (StackArray):") + status = -1 + try + status = run(`./stack_times_table`) + catch e + @warn "Could not run $(scratch)/stack_times_table" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + # Test ascii output + @test parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' # Test binary output @test fread!(szeros(Int, 5,5), c"table.b") == (1:5)*(1:5)' end + + ## --- Reading from written files + + let + # Compile... + cleanup("readwrite") + status = -1 + try + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/readwrite.jl`) + catch e + @warn "Could not compile $testpath/scripts/readwrite.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + cleanup("table.tsv", "table.b", "tableb.tsv", "tables.tsv", "tablets.tsv") + println("5x5 times table, read from file:") + status = -1 + try + status = run(`./readwrite 5 5`) + catch e + @warn "Could not run $(scratch)/readwrite" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + # Test binary output + @test fread!(szeros(Int, 5,5), c"table.b") == (1:5)*(1:5)' + # Test ascii output + @test parsedlm(Int, c"table.tsv", '\t') == (1:5)*(1:5)' + @test parsedlm(Int, c"tableb.tsv", '\t') == (1:5)*(1:5)' + @test parsedlm(Int, c"tables.tsv", '\t') == (1:5)*(1:5)' + @test parsedlm(Int, c"tablets.tsv", '\t') == (1:5)*(1:5)' + + end + + ## --- "withmallocarray"-type do-block pattern let # Compile... + cleanup("withmallocarray") status = -1 try - isfile("withmallocarray") && rm("withmallocarray") status = run(`$jlpath --startup=no --compile=min $testpath/scripts/withmallocarray.jl`) catch e @warn "Could not compile $testpath/scripts/withmallocarray.jl" @@ -95,9 +171,9 @@ end ## --- Random number generation let # Compile... + cleanup("rand_matrix") status = -1 try - isfile("rand_matrix") && rm("rand_matrix") status = run(`$jlpath --startup=no --compile=min $testpath/scripts/rand_matrix.jl`) catch e @warn "Could not compile $testpath/scripts/rand_matrix.jl" @@ -121,15 +197,15 @@ end let # Compile... + cleanup("randn_matrix") status = -1 try - isfile("randn_matrix") && rm("randn_matrix") status = run(`$jlpath --startup=no --compile=min $testpath/scripts/randn_matrix.jl`) catch e @warn "Could not compile $testpath/scripts/randn_matrix.jl" println(e) end - if Sys.isbsd() + @static if Sys.isbsd() @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 end @@ -143,110 +219,174 @@ end @warn "Could not run $(scratch)/randn_matrix" println(e) end - if Sys.isbsd() + @static if Sys.isbsd() @test isa(status, Base.Process) @test isa(status, Base.Process) && status.exitcode == 0 end end + ## --- Test matrix multiplication by plain @inbounds loops + let + # Compile... + cleanup("inbounds_product") + status = -1 + try + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/inbounds_product.jl`) + catch e + @warn "Could not compile $testpath/scripts/inbounds_product.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 - ## --- Test LoopVectorization integration - if Bool(LoopVectorization.VectorizationBase.has_feature(Val{:x86_64_avx2})) - let - # Compile... - status = -1 - try - isfile("loopvec_product") && rm("loopvec_product") - status = run(`$jlpath --startup=no --compile=min $testpath/scripts/loopvec_product.jl`) - catch e - @warn "Could not compile $testpath/scripts/loopvec_product.jl" - println(e) - end - @test_broken isa(status, Base.Process) - @test_broken isa(status, Base.Process) && status.exitcode == 0 - - # Run... - println("10x10 table sum:") - status = -1 - try - status = run(`./loopvec_product 10 10`) - catch e - @warn "Could not run $(scratch)/loopvec_product" - println(e) - end - @test_broken isa(status, Base.Process) - @test_broken isa(status, Base.Process) && status.exitcode == 0 - @test_broken parsedlm(c"product.tsv",'\t')[] == 3025 + # Run... + cleanup("product.tsv") + println("10x10 table sum:") + status = -1 + try + status = run(`./inbounds_product 10 10`) + catch e + @warn "Could not run $(scratch)/inbounds_product" + println(e) end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + @test parsedlm(c"product.tsv",'\t')[] == 3025 end let # Compile... + cleanup("inbounds_matrix") status = -1 try - isfile("loopvec_matrix") && rm("loopvec_matrix") - status = run(`$jlpath --startup=no $testpath/scripts/loopvec_matrix.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/inbounds_matrix.jl`) catch e - @warn "Could not compile $testpath/scripts/loopvec_matrix.jl" + @warn "Could not compile $testpath/scripts/inbounds_matrix.jl" println(e) end - @test_broken isa(status, Base.Process) - @test_broken isa(status, Base.Process) && status.exitcode == 0 + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 # Run... - println("10x5 matrix product:") + cleanup("table.tsv", "table.b") + println("10x5 MallocMatrix product, manual inbounds:") status = -1 try - status = run(`./loopvec_matrix 10 5`) + status = run(`./inbounds_matrix 10 5`) catch e - @warn "Could not run $(scratch)/loopvec_matrix" + @warn "Could not run $(scratch)/inbounds_matrix" println(e) end - @test_broken isa(status, Base.Process) - @test_broken isa(status, Base.Process) && status.exitcode == 0 + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 A = (1:10) * (1:5)' # Check ascii output - @test_broken parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.isapple() + @test parsedlm(c"table.tsv",'\t') == A' * A # Check binary output - @test_broken fread!(szeros(5,5), c"table.b") == A' * A + @test fread!(szeros(5,5), c"table.b") == A' * A + end + + let + # Compile... + cleanup("inbounds_matrix_stack") + status = -1 + try + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/inbounds_matrix_stack.jl`) + catch e + @warn "Could not compile $testpath/scripts/inbounds_matrix_stack.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + cleanup("table.tsv") + println("10x5 StackMatrix product, manual inbounds:") + status = -1 + try + status = run(`./inbounds_matrix_stack`) + catch e + @warn "Could not run $(scratch)/inbounds_matrix_stack" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + A = (1:10) * (1:5)' + @test parsedlm(c"table.tsv",'\t') == A' * A end + ## --- Test built-in matrix multiplication let # Compile... + cleanup("matmul") status = -1 try - isfile("loopvec_matrix_stack") && rm("loopvec_matrix_stack") - status = run(`$jlpath --startup=no $testpath/scripts/loopvec_matrix_stack.jl`) + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/matmul.jl`) catch e - @warn "Could not compile $testpath/scripts/loopvec_matrix_stack.jl" + @warn "Could not compile $testpath/scripts/matmul.jl" println(e) end - @test_broken isa(status, Base.Process) - @test_broken isa(status, Base.Process) && status.exitcode == 0 + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 # Run... - println("10x5 matrix product:") + cleanup("table.tsv", "table.b") + println("10x5 MallocMatrix product:") status = -1 try - status = run(`./loopvec_matrix_stack`) + status = run(`./matmul 10 5`) catch e - @warn "Could not run $(scratch)/loopvec_matrix_stack" + @warn "Could not run $(scratch)/matmul" println(e) end - @test_broken isa(status, Base.Process) - @test_broken isa(status, Base.Process) && status.exitcode == 0 + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 A = (1:10) * (1:5)' - @test_broken parsedlm(c"table.tsv",'\t') == A' * A broken=Sys.isapple() + # Check ascii output + @test parsedlm(c"table.tsv",'\t') == A' * A + # Check binary output + @test fread!(szeros(5,5), c"table.b") == A' * A end + let + # Compile... + cleanup("matmul_stack") + status = -1 + try + status = run(`$jlpath --compile=min $testpath/scripts/matmul_stack.jl`) + catch e + @warn "Could not compile $testpath/scripts/matmul_stack.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + cleanup("table.tsv", "table.b") + println("10x5 StackMatrix product:") + status = -1 + try + status = run(`./matmul_stack`) + catch e + @warn "Could not run $(scratch)/matmul_stack" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + A = (1:10) * (1:5)' + # Check ascii output + @test parsedlm(c"table.tsv",'\t') == A' * A + # Check binary output + @test fread!(szeros(5,5), c"table.b") == A' * A + end ## --- Test string handling let # Compile... + cleanup("print_args") status = -1 try - isfile("print_args") && rm("print_args") status = run(`$jlpath --startup=no --compile=min $testpath/scripts/print_args.jl`) catch e @warn "Could not compile $testpath/scripts/print_args.jl" @@ -268,13 +408,41 @@ end @test isa(status, Base.Process) && status.exitcode == 0 end + ## --- Test string iteration + + let + # Compile... + cleanup("iterate") + status = -1 + try + status = run(`$jlpath --startup=no $testpath/scripts/iterate.jl`) + catch e + @warn "Could not compile $testpath/scripts/iterate.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + + # Run... + println("Iteration:") + status = -1 + try + status = run(`./iterate`) + catch e + @warn "Could not run $(scratch)/iterate" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + end + ## --- Test error throwing let # Compile... + cleanup("maybe_throw") status = -1 try - isfile("maybe_throw") && rm("maybe_throw") status = run(`$jlpath --startup=no --compile=min $testpath/scripts/throw_errors.jl`) catch e @warn "Could not compile $testpath/scripts/throw_errors.jl" @@ -300,43 +468,37 @@ end catch e @info "maybe_throw: task failed sucessfully!" end - if Sys.iswindows() - @info "maybe_throw: task doesn't fail on Windows." - @test status.exitcode == 0 - else - @test status === -1 - end + @test status === -1 end ## --- Test interop + + # (dlopen/dlsym are not available on Windows) + @static if !Sys.iswindows() let + # Compile... + cleanup("interop") + status = -1 + try + status = run(`$jlpath --startup=no --compile=min $testpath/scripts/interop.jl`) + catch e + @warn "Could not compile $testpath/scripts/interop.jl" + println(e) + end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 - if Sys.isbsd() - let - # Compile... - status = -1 - try - isfile("interop") && rm("interop") - status = run(`$jlpath --startup=no --compile=min $testpath/scripts/interop.jl`) - catch e - @warn "Could not compile $testpath/scripts/interop.jl" - println(e) - end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 - - # Run... - println("Interop:") - status = -1 - try - status = run(`./interop`) - catch e - @warn "Could not run $(scratch)/interop" - println(e) - end - @test isa(status, Base.Process) - @test isa(status, Base.Process) && status.exitcode == 0 + # Run... + println("Interop:") + status = -1 + try + status = run(`./interop`) + catch e + @warn "Could not run $(scratch)/interop" + println(e) end - end + @test isa(status, Base.Process) + @test isa(status, Base.Process) && status.exitcode == 0 + end end end From d93ea7f23d81def73837efb1abb32b1788413375 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sun, 8 Nov 2026 14:33:13 -0500 Subject: [PATCH 184/202] Generally put `cflags` after object code when compiling --- src/StaticCompiler.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 2e9aef70..8570c015 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -339,7 +339,7 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi # Compile! if Sys.isapple() && !llvm_to_clang # Apple no longer uses _start, so we can just specify a custom entry - run(`$cc -e _$fn $cflags $obj_or_ir_path -o $exec_path`) + run(`$cc -e _$fn $obj_or_ir_path $cflags -o $exec_path`) else # Write a minimal wrapper to avoid having to specify a custom entry wrapper_path = joinpath(path, "wrapper$(rand(UInt)).c") @@ -355,7 +355,7 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi # Depending on whether llvm_to_clang is set, # this may either be compiling from LLVM IR or just linking object files - run(`$cc $wrapper_path $cflags $obj_or_ir_path -o $exec_path`) + run(`$cc $wrapper_path $obj_or_ir_path $cflags -o $exec_path`) # Clean up rm(wrapper_path) @@ -426,7 +426,7 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file # Compile! llvm_to_clang && (cflags = `-Wno-override-module $cflags`) - run(`$cc -shared $cflags $obj_or_ir_path -o $lib_path `) + run(`$cc -shared $obj_or_ir_path $cflags -o $lib_path `) return path, filename end From e15a7f0eec5303baf8cb8887e61f7ad6d0a7bc49 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 11 Nov 2026 17:00:59 -0500 Subject: [PATCH 185/202] Bump version to v0.7.6 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6c7e25a3..ee1c02d6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.7.5" +version = "0.7.6" [deps] From daf76432307fa0fdfe3a821d6e16f27e1a2824ea Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Fri, 27 Nov 2026 00:22:50 -0500 Subject: [PATCH 186/202] Update default overrides for Julia 1.14 --- src/quirks.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/quirks.jl b/src/quirks.jl index 6d476d49..73997780 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -151,6 +151,19 @@ end y == 0 && @print_and_throw c"Integer division error" Core.Intrinsics.udiv_int(x, y) end +# (`fld` and `cld` of signed integers, with wrapping arithmetic by the intrinsics (`+%` is syntax of Julia 1.14 only): Julia 1.14 has methods that check the arguments and throw themselves, which do not go through `div` above. The same code, with the error that is ours) +@device_override function Base.div(x::T, y::T, ::typeof(RoundDown)) where {T<:Base.BitSigned} + (iszero(y) | ((x == typemin(T)) & (y == -1))) && @print_and_throw c"Integer division error" + k = 8sizeof(T) - 1 + dsign = y >> k + nsign = (x | Core.Intrinsics.add_int(x, dsign)) >> k + return Core.Intrinsics.add_int(Core.Intrinsics.sdiv_int(Core.Intrinsics.add_int(x, dsign - nsign), y), nsign ⊻ dsign) +end +@device_override function Base.div(x::T, y::T, ::typeof(RoundUp)) where {T<:Base.BitSigned} + (iszero(y) | ((x == typemin(T)) & (y == -1))) && @print_and_throw c"Integer division error" + s = ifelse(y < 0, -(x < 0) % T, (x > 0) % T) + return Core.Intrinsics.add_int(Core.Intrinsics.sdiv_int(Core.Intrinsics.sub_int(x, s), y), T(s != 0)) +end @device_override function Base.rem(x::T, y::T) where {T<:Base.BitSigned} y == 0 && @print_and_throw c"Integer division error" y == -1 ? zero(T) : Core.Intrinsics.srem_int(x, y) From 40946bb80f9ae0573e0e473a047e11afef3b7330 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Fri, 27 Nov 2026 00:27:50 -0500 Subject: [PATCH 187/202] Use target as source of truth for OS, more consistent options for IR export --- Project.toml | 1 + src/StaticCompiler.jl | 111 +++++++++++++++++++++++++++++++----------- src/target.jl | 51 +++++++++++++------ test/runtests.jl | 1 + test/testcore.jl | 41 ++++++++++++++++ 5 files changed, 160 insertions(+), 45 deletions(-) diff --git a/Project.toml b/Project.toml index ee1c02d6..6555141b 100644 --- a/Project.toml +++ b/Project.toml @@ -16,6 +16,7 @@ Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [compat] +Clang_jll = "16, 18, 20, 23" GPUCompiler = "2" LLVM = "10" MacroTools = "0.5" diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 8570c015..ebfd0267 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -10,8 +10,8 @@ using LLD_jll: lld using StaticTools using StaticTools: @symbolcall, @c_str, println using Core: MethodTable -using Base:BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform, BinaryPlatforms.arch, BinaryPlatforms.os_str, BinaryPlatforms.libc_str -using Base:BinaryPlatforms.platform_dlext +using Base: BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform +using Base: BinaryPlatforms.platform_dlext, BinaryPlatforms.arch, BinaryPlatforms.os, BinaryPlatforms.os_str, BinaryPlatforms.libc_str export load_function, compile_shlib, compile_executable export static_code_llvm, static_code_typed, static_llvm_module, static_code_native export @device_override, @print_and_throw @@ -41,6 +41,42 @@ end # shared library extension of the target (that of the host, unless cross-compiling) dlext(target::StaticTarget) = isnothing(target.platform) ? Libdl.dlext : platform_dlext(target.platform) +# Make any OS-specific choices on the basis of the target, not the host +iswindows(target::StaticTarget) = isnothing(target.platform) ? occursin(r"windows|mingw|msvc|cygwin"i, target_triple(target)) : Sys.iswindows(target.platform) +isapple(target::StaticTarget) = isnothing(target.platform) ? occursin(r"apple|darwin|macos"i, target_triple(target)) : Sys.isapple(target.platform) + +# Whether the IR has to go through an external compiler (`clang`, `llc`) to be an object: on Windows (always), or where Julia's LLVM has no code generator for the target +needs_ir(target::StaticTarget) = iswindows(target) || !isnothing(target.ir_triple) + +# `clang` makes code for its own host unless it is told the target (it replaces the triple of the IR, which `-Wno-override-module` hides) +function clang_target_flags(target::StaticTarget) + host = HostPlatform() + foreign = !isnothing(target.ir_triple) || (!isnothing(target.platform) && (arch(target.platform) != arch(host) || os(target.platform) != os(host))) + return foreign ? `--target=$(target_triple(target))` : `` +end + +# the family that an architecture's intrinsics (`llvm..`) are named by +llvm_family(arch::AbstractString) = startswith(arch, "aarch64") || startswith(arch, "arm64") ? "aarch64" : startswith(arch, "arm") ? "arm" : arch in ("x86_64", "i686", "i386", "x86") ? "x86" : startswith(arch, "wasm") ? "wasm" : arch + +# The IR of a target that has no code generator here (`target.ir_triple`) was made for the host, by Julia's code generation: it gets the triple of the target, no data layout (the code generator that makes the +# object has the target's), and no attributes that name the host's CPU and features. IR that has intrinsics of the host's architecture is refused: it can not be made for another. +function retarget!(mod::LLVM.Module, target::StaticTarget) + triple = target.ir_triple + isnothing(triple) && return mod + family = llvm_family(first(split(triple, '-'))) + for f in LLVM.functions(mod) + m = match(r"^llvm\.(aarch64|arm|x86|wasm)\.", LLVM.name(f)) + isnothing(m) || m.captures[1] == family || throw(ArgumentError("the program uses `$(LLVM.name(f))`, an intrinsic of the code generator of the host's architecture: it can not be made for $triple")) + attrs = LLVM.function_attributes(f) + for kind in ("target-cpu", "target-features", "tune-cpu") + delete!(attrs, kind) + end + end + LLVM.triple!(mod, triple) + LLVM.datalayout!(mod, "") + return mod +end + # Check that `f(types...)` is compilable to a native entry point, returning the tuple type of `types` function check_signature(f, types; target::StaticTarget=StaticTarget(), kwargs...) @@ -62,7 +98,7 @@ compile_executable(f::Function, types::Tuple, [path::String=pwd()], [name::Strin target::StaticTarget=StaticTarget(), cc = nothing, # Compiler, may specify to override defaults cflags = ``, # Specify libraries you would like to link against, and other compiler options here - llvm_to_clang = Sys.iswindows(), + llvm_to_clang = needs_ir(target), # default: whether the target needs it (Windows, or cross-target absent from Julia's LLVM) method_table = StaticCompiler.method_table, llvm_postprocess = nothing, kwargs... @@ -148,7 +184,7 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name= demangle = true, cc = nothing, cflags = ``, - llvm_to_clang = Sys.iswindows(), + llvm_to_clang = needs_ir(target), kwargs... ) @@ -158,7 +194,7 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name= isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" generate_executable(funcs, path, name, filename; target, demangle, cc, cflags, llvm_to_clang, kwargs...) - Sys.iswindows() && (filename *= ".exe") + iswindows(target) && (filename *= ".exe") joinpath(abspath(path), filename) end @@ -170,7 +206,7 @@ compile_shlib(f::Function, types::Tuple, [path::String=pwd()], [name::String=str demangle=true, cc = nothing, # Compiler, may specify to override defaults cflags = ``, # Specify libraries you would like to link against, and other compiler options here - llvm_to_clang = Sys.iswindows(), + llvm_to_clang = needs_ir(target), # default: whether the target needs it (Windows, or cross-target absent from Julia's LLVM) method_table = StaticCompiler.method_table, kwargs...) @@ -179,7 +215,7 @@ compile_shlib(funcs::Array, [path::String=pwd()]; target::StaticTarget=StaticTarget(), cc = nothing, # Compiler, may specify to override defaults cflags = ``, # Specify libraries you would like to link against, and other compiler options here - llvm_to_clang = Sys.iswindows(), + llvm_to_clang = needs_ir(target), # default: whether the target needs it (Windows, or cross-target absent from Julia's LLVM) method_table = StaticCompiler.method_table, kwargs...) ``` @@ -225,7 +261,7 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); demangle = true, cc = nothing, cflags = ``, - llvm_to_clang = Sys.iswindows(), + llvm_to_clang = needs_ir(target), kwargs... ) for (f, types) in funcs @@ -321,7 +357,7 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi demangle = true, cc = nothing, cflags = ``, - llvm_to_clang::Bool = Sys.iswindows(), + llvm_to_clang::Bool = needs_ir(target), kwargs... ) # Choice of compiler @@ -333,11 +369,11 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi # Update paths and flags given options fn = demangle ? "$name" : "julia_$name" - Sys.iswindows() && (exec_path *= ".exe") - llvm_to_clang && (cflags = `-Wno-override-module $cflags`) + iswindows(target) && (exec_path *= ".exe") + llvm_to_clang && (cflags = `-Wno-override-module $(clang_target_flags(target)) $cflags`) # Compile! - if Sys.isapple() && !llvm_to_clang + if isapple(target) && !llvm_to_clang # Apple no longer uses _start, so we can just specify a custom entry run(`$cc -e _$fn $obj_or_ir_path $cflags -o $exec_path`) else @@ -413,7 +449,7 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file demangle = true, cc = nothing, cflags = ``, - llvm_to_clang::Bool = Sys.iswindows(), + llvm_to_clang::Bool = needs_ir(target), kwargs... ) # Choice of compiler @@ -422,10 +458,10 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file # Prepare objects or (if llvm_to_clang is set) IR lib_path = joinpath(path, "$filename.$(dlext(target))") _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, - dllexport=Sys.iswindows(), kwargs...) + dllexport=iswindows(target), kwargs...) # Compile! - llvm_to_clang && (cflags = `-Wno-override-module $cflags`) + llvm_to_clang && (cflags = `-Wno-override-module $(clang_target_flags(target)) $cflags`) run(`$cc -shared $obj_or_ir_path $cflags -o $lib_path `) return path, filename @@ -519,6 +555,7 @@ function compile_llvm_module(f, tt, name=fix_name(f); try isnothing(llvm_postprocess) || llvm_postprocess(m) locate_pointers_and_runtime_calls(m) + retarget!(m, target) catch dispose(m) rethrow() @@ -607,27 +644,43 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file kwargs...) f, tt = funcs[1] mkpath(path) + if emit_llvm_only # (Required on Windows) + ir_path = joinpath(path, "$filenamebase.ll") + write(ir_path, generate_ir(funcs; demangle, target, dllexport, llvm_postprocess, kwargs...)) + return path, ir_path + end + isnothing(target.ir_triple) || throw(ArgumentError("Julia's LLVM has no code generator for $(target.ir_triple), so no object can be made for it here: use `emit_llvm_only = true` (or `generate_ir`) and give the IR to a compiler that has one")) # compile and generate code in one context, which the module can't outlive GPUCompiler.JuliaContext() do context @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin dllexport && add_dllexport!(mod, funcs; demangle) - 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") - fakejob, _ = static_job(f, tt; target, kwargs...) - obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) - open(obj_path, "w") do io - write(io, obj) - end - return path, obj_path + obj_path = joinpath(path, "$filenamebase.o") + fakejob, _ = static_job(f, tt; target, kwargs...) + obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) + open(obj_path, "w") do io + write(io, obj) end + return path, obj_path + end + end +end + +""" +```julia +generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle=true, dllexport=false, llvm_postprocess=nothing, kwargs...) -> String +generate_ir(f, tt; kwargs...) -> String +``` +The LLVM IR (as text) of the functions that `generate_obj` would compile to an object file, for tools that take IR instead (a downstream package that makes the object or +the executable of a target by itself, for example): nothing is written to the file system, and the module (which cannot outlive its context) is not returned. +""" +function generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle = true, dllexport = false, llvm_postprocess = nothing, kwargs...) + GPUCompiler.JuliaContext() do context + @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin + dllexport && add_dllexport!(mod, funcs; demangle) + return string(mod) end end end +generate_ir(f, tt; kwargs...) = generate_ir(((f, tt),); kwargs...) end # module diff --git a/src/target.jl b/src/target.jl index 4f0af7b6..3c99bfde 100644 --- a/src/target.jl +++ b/src/target.jl @@ -1,9 +1,9 @@ """ ```julia StaticTarget() # Native target - StaticTarget(platform::Base.BinaryPlatforms.Platform) # Specific target with generic CPU - StaticTarget(platform::Platform, cpu::String) # Specific target with specific CPU - StaticTarget(platform::Platform, cpu::String, features::String) # Specific target with specific CPU and features + StaticTarget(platform_or_triple) # Specific target with generic CPU (a `Base.BinaryPlatforms.Platform` or a triple string) + StaticTarget(platform_or_triple, cpu::String) # Specific target with specific CPU + StaticTarget(platform_or_triple, cpu::String, features::String) # Specific target with specific CPU and features ``` Struct that defines a target for the compilation Beware that currently the compilation assumes that the code is on the host so platform specific code like: @@ -13,6 +13,9 @@ Beware that currently the compilation assumes that the code is on the host so pl does not behave as expected. By default `StaticTarget()` is the native target. +If Julia's LLVM has no code generator for the target (x86-64 where LLVM has only AArch64 and WebAssembly, say), the target is made anyway, with `ir_triple` set: IR can be made for it +(`generate_ir`, `emit_llvm_only = true`, `llvm_to_clang = true`: the IR has the target's triple and no host CPU or features), and an external `clang` or `llc` that has the code generator makes the object. + For cross-compilation of executables and shared libraries, one also needs to call `set_compiler!` with the path to a valid C compiler for the target platform. For example, to cross-compile for aarch64 using a compiler from homebrew, one can use: ```julia @@ -22,30 +25,46 @@ for the target platform. For example, to cross-compile for aarch64 using a compi mutable struct StaticTarget platform::Union{Platform,Nothing} tm::LLVM.TargetMachine - compiler::Union{String,Nothing} + compiler::Union{String,Nothing} # A target-specific compiler, else nothing + ir_triple::Union{String,Nothing} # A target that requires us to output IR only, else nothing julia_runtime::Bool end - -clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform) +StaticTarget(platform, tm, compiler, julia_runtime) = StaticTarget(platform, tm, compiler, nothing, julia_runtime) StaticTarget() = StaticTarget(HostPlatform(), LLVM.host_cpu_name(), LLVM.host_cpu_features()) -StaticTarget(platform::Platform) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform)), nothing, false) -StaticTarget(platform::Platform, cpu::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform); cpu), nothing, false) -StaticTarget(platform::Platform, cpu::String, features::String) = StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = clean_triple(platform)), clean_triple(platform); cpu, features), nothing, false) -function StaticTarget(triple::String, cpu::String, features::String) - platform = tryparse(Platform, triple) - StaticTarget(platform, LLVM.TargetMachine(LLVM.Target(triple = triple), triple; cpu, features), nothing, false) +# The target for a platform or a triple, with the generic CPU of its architecture unless `cpu` and `features` say otherwise. Julia's LLVM may have no code generator for it: then `ir_triple` is set, `tm` +# is the host's, and only IR can be made (see above). +function StaticTarget(target::Union{Platform,AbstractString}, cpu::AbstractString = "", features::AbstractString = "") + platform = target isa Platform ? target : tryparse(Platform, target) + triple = target isa Platform ? clean_triple(target) : String(target) + tm = try + LLVM.TargetMachine(LLVM.Target(triple = triple), triple; cpu, features) + catch err + # (`ArgumentError`: "No available targets are compatible with triple") + err isa ArgumentError || rethrow() + return StaticTarget(platform, host_generic_machine(), nothing, LLVM.normalize(triple), false) + end + return StaticTarget(platform, tm, nothing, nothing, false) end +clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform) + +# the host's target machine without its CPU and its features, which are not those of another architecture's code (and not to be in its IR) +host_generic_machine() = LLVM.TargetMachine(LLVM.Target(triple = LLVM.default_triple()), LLVM.default_triple()) + +"The triple that the target is for: the one of its IR if it has no code generator in Julia's LLVM, and else the one of its target machine" +target_triple(target::StaticTarget) = something(target.ir_triple, LLVM.triple(target.tm)) """ -Set the compiler for cross compilation - ```julia - set_compiler!(StaticTarget(parse(Platform,"aarch64-gnu-linux")), "/opt/homebrew/bin/aarch64-elf-gcc") +```julia +set_compiler!(target::StaticTarget, compiler::String) +``` +Set the compiler for cross compilation. For example: +```julia +set_compiler!(StaticTarget(parse(Platform,"aarch64-gnu-linux")), "/opt/homebrew/bin/aarch64-elf-gcc") ``` """ set_compiler!(target::StaticTarget, compiler::String) = (target.compiler = compiler) - set_runtime!(target::StaticTarget, julia_runtime::Bool) = (target.julia_runtime = julia_runtime) # Default to native diff --git a/test/runtests.jl b/test/runtests.jl index 30ab8c41..dc52afbb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,6 +9,7 @@ using StaticTools # using StrideArraysCore using MacroTools using LLD_jll +using LLVM using Bumper addprocs(1) diff --git a/test/testcore.jl b/test/testcore.jl index d53ddeff..3213c9af 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -165,3 +165,44 @@ Base.Experimental.@overlay AnotherTable SubFoo.rand() = 3 @test @ccall($fptr()::Int) == 6 end end + +@testset "The target is not the host" begin + using Base.BinaryPlatforms + # The target machine is the host's: only the platform is considered, and not every LLVM has the code generator of each platform + t(p) = StaticCompiler.StaticTarget(p, StaticCompiler.StaticTarget().tm, nothing, false) + # Ensure we are checking the target, no the host + @test StaticCompiler.iswindows(t(Platform("x86_64", "windows"))) && !StaticCompiler.isapple(t(Platform("x86_64", "windows"))) + @test StaticCompiler.isapple(t(Platform("aarch64", "macos"))) && !StaticCompiler.iswindows(t(Platform("aarch64", "macos"))) + @test !StaticCompiler.iswindows(t(Platform("aarch64", "linux"))) && !StaticCompiler.isapple(t(Platform("aarch64", "linux"))) + # A triple that `Platform` does not parse + wasm = StaticCompiler.StaticTarget("wasm32-unknown-unknown", "generic", "") + @test isnothing(wasm.platform) && !StaticCompiler.iswindows(wasm) && !StaticCompiler.isapple(wasm) +end + +@testset "LLVM IR as text" begin + ir = StaticCompiler.generate_ir(fib, Tuple{Int}) + @test ir isa String && occursin("define", ir) && occursin("fib", ir) && occursin("target triple", ir) + path, ir_path = StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibir"; emit_llvm_only = true) + @test isfile(ir_path) && occursin("define", read(ir_path, String)) +end + +@testset "A target that Julia's LLVM has no code generator for" begin + triple = "x86_64-unknown-linux-gnu" + ir_target = StaticCompiler.StaticTarget(tryparse(Base.BinaryPlatforms.Platform, triple), StaticCompiler.host_generic_machine(), nothing, triple, false) + @test StaticCompiler.target_triple(ir_target) == triple && StaticCompiler.needs_ir(ir_target) + @test occursin("--target=$triple", string(StaticCompiler.clang_target_flags(ir_target))) + ir = StaticCompiler.generate_ir(fib, Tuple{Int}; target = ir_target) + @test occursin("target triple = \"$triple\"", ir) && !occursin("target datalayout", ir) && !occursin("target-cpu", ir) && !occursin("target-features", ir) + @test_throws ArgumentError StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibobj"; target = ir_target) + path, ir_path = StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibir"; target = ir_target, emit_llvm_only = true) + @test occursin("target triple = \"$triple\"", read(ir_path, String)) + # the host's own target is untouched + host = StaticCompiler.StaticTarget() + @test isnothing(host.ir_triple) && StaticCompiler.needs_ir(host) == Sys.iswindows() + @test isempty(StaticCompiler.clang_target_flags(host)) + # where there is no code generator for the triple, the constructors make such a target + for t in ("x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "riscv64-unknown-linux-gnu") + has_backend = try; LLVM.Target(triple = t); true; catch; false; end + has_backend || @test StaticCompiler.StaticTarget(t, "generic", "").ir_triple == LLVM.normalize(t) + end +end From 8850b81ec895bbefcc8e8b6a6d1f4d5442f1f2a9 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Fri, 27 Nov 2026 10:29:55 -0500 Subject: [PATCH 188/202] Infrastructure for cross-compilation to targets that the host Julia's LLVM may not directly support --- Project.toml | 7 + ext/StaticCompilerGlibcExt.jl | 26 ++++ src/StaticCompiler.jl | 213 +++++++++++++-------------- src/builtins.jl | 262 ++++++++++++++++++++++++++++++++++ src/dllexport.jl | 10 -- src/llvm.jl | 179 +++++++++++++++++++++++ src/pointer_warning.jl | 54 ------- src/target.jl | 102 ++++++++++--- src/toolchain.jl | 108 ++++++++++++++ test/Project.toml | 2 + test/runtests.jl | 1 + test/testcore.jl | 149 ++++++++++++++++++- 12 files changed, 921 insertions(+), 192 deletions(-) create mode 100644 ext/StaticCompilerGlibcExt.jl create mode 100644 src/builtins.jl delete mode 100644 src/dllexport.jl create mode 100644 src/llvm.jl delete mode 100644 src/pointer_warning.jl create mode 100644 src/toolchain.jl diff --git a/Project.toml b/Project.toml index 6555141b..8bffbee2 100644 --- a/Project.toml +++ b/Project.toml @@ -15,6 +15,13 @@ MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" +[weakdeps] +Glibc_jll = "452aa2e7-e185-58db-8ff9-d3c1fa4bc997" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" + +[extensions] +StaticCompilerGlibcExt = ["Glibc_jll", "Pkg"] + [compat] Clang_jll = "16, 18, 20, 23" GPUCompiler = "2" diff --git a/ext/StaticCompilerGlibcExt.jl b/ext/StaticCompilerGlibcExt.jl new file mode 100644 index 00000000..205a0b94 --- /dev/null +++ b/ext/StaticCompilerGlibcExt.jl @@ -0,0 +1,26 @@ +# The C library of a Linux target, for linking by `LLD(:gnu; sysroot)`: the artifact of `Glibc_jll` for the platform of the target, fetched for any host. +module StaticCompilerGlibcExt + +using StaticCompiler: StaticCompiler, StaticTarget +using Glibc_jll: Glibc_jll +using Pkg: Pkg +using Base.BinaryPlatforms: Platform, arch + +function StaticCompiler.glibc_sysroot(platform::Platform) + platform = Platform(arch(platform), "linux"; libc = "glibc") # (the one that `Glibc_jll` has: of the architecture) + toml = joinpath(dirname(dirname(pathof(Glibc_jll))), "Artifacts.toml") + meta = Pkg.Artifacts.artifact_meta("Glibc", toml; platform) + meta === nothing && throw(ArgumentError("`Glibc_jll` has no artifact for $platform")) + Pkg.Artifacts.ensure_artifact_installed("Glibc", meta, toml; platform) + return Pkg.Artifacts.artifact_path(Base.SHA1(meta["git-tree-sha1"])) +end + +function StaticCompiler.glibc_sysroot(triple::AbstractString) + platform = tryparse(Platform, triple) + platform === nothing && throw(ArgumentError("`$triple` is not a triple of a platform that has a glibc")) + return StaticCompiler.glibc_sysroot(platform) +end + +StaticCompiler.glibc_sysroot(target::StaticTarget) = target.platform === nothing ? StaticCompiler.glibc_sysroot(StaticCompiler.target_triple(target)) : StaticCompiler.glibc_sysroot(target.platform) + +end diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index ebfd0267..f96e506f 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -15,69 +15,19 @@ using Base: BinaryPlatforms.platform_dlext, BinaryPlatforms.arch, BinaryPlatform export load_function, compile_shlib, compile_executable export static_code_llvm, static_code_typed, static_llvm_module, static_code_native export @device_override, @print_and_throw -export StaticTarget +export StaticTarget, Driver, LLD + +abstract type Toolchain end # (the field of `StaticTarget`; see toolchain.jl) include("quirks.jl") include("target.jl") -include("pointer_warning.jl") -include("dllexport.jl") +include("toolchain.jl") +include("builtins.jl") +include("llvm.jl") fix_name(f::Function) = fix_name(string(nameof(f))) fix_name(s) = String(GPUCompiler.safe_name(s)) -# Generally prefer Clang_jll clang (which should probably be at least as new as Julia's LLVM) -# and fall back to the system one on platforms where Clang_jll is not available or not expected to work -function clang() - Clang_jll.is_available() || return `clang` # Fall back to system clang if Clang_jll is not available - cmd = Clang_jll.clang() - # Clang_jll may not know where to find the macOS SDK (e.g. for `-lSystem`) - if Sys.isapple() && !haskey(ENV, "SDKROOT") - isnothing(Sys.which("xcrun")) && return `clang` # Fall back to system clang if we can't figure out the SDK path - cmd = addenv(cmd, "SDKROOT" => readchomp(`xcrun --show-sdk-path`)) - end - return cmd -end - -# shared library extension of the target (that of the host, unless cross-compiling) -dlext(target::StaticTarget) = isnothing(target.platform) ? Libdl.dlext : platform_dlext(target.platform) - -# Make any OS-specific choices on the basis of the target, not the host -iswindows(target::StaticTarget) = isnothing(target.platform) ? occursin(r"windows|mingw|msvc|cygwin"i, target_triple(target)) : Sys.iswindows(target.platform) -isapple(target::StaticTarget) = isnothing(target.platform) ? occursin(r"apple|darwin|macos"i, target_triple(target)) : Sys.isapple(target.platform) - -# Whether the IR has to go through an external compiler (`clang`, `llc`) to be an object: on Windows (always), or where Julia's LLVM has no code generator for the target -needs_ir(target::StaticTarget) = iswindows(target) || !isnothing(target.ir_triple) - -# `clang` makes code for its own host unless it is told the target (it replaces the triple of the IR, which `-Wno-override-module` hides) -function clang_target_flags(target::StaticTarget) - host = HostPlatform() - foreign = !isnothing(target.ir_triple) || (!isnothing(target.platform) && (arch(target.platform) != arch(host) || os(target.platform) != os(host))) - return foreign ? `--target=$(target_triple(target))` : `` -end - -# the family that an architecture's intrinsics (`llvm..`) are named by -llvm_family(arch::AbstractString) = startswith(arch, "aarch64") || startswith(arch, "arm64") ? "aarch64" : startswith(arch, "arm") ? "arm" : arch in ("x86_64", "i686", "i386", "x86") ? "x86" : startswith(arch, "wasm") ? "wasm" : arch - -# The IR of a target that has no code generator here (`target.ir_triple`) was made for the host, by Julia's code generation: it gets the triple of the target, no data layout (the code generator that makes the -# object has the target's), and no attributes that name the host's CPU and features. IR that has intrinsics of the host's architecture is refused: it can not be made for another. -function retarget!(mod::LLVM.Module, target::StaticTarget) - triple = target.ir_triple - isnothing(triple) && return mod - family = llvm_family(first(split(triple, '-'))) - for f in LLVM.functions(mod) - m = match(r"^llvm\.(aarch64|arm|x86|wasm)\.", LLVM.name(f)) - isnothing(m) || m.captures[1] == family || throw(ArgumentError("the program uses `$(LLVM.name(f))`, an intrinsic of the code generator of the host's architecture: it can not be made for $triple")) - attrs = LLVM.function_attributes(f) - for kind in ("target-cpu", "target-features", "tune-cpu") - delete!(attrs, kind) - end - end - LLVM.triple!(mod, triple) - LLVM.datalayout!(mod, "") - return mod -end - - # Check that `f(types...)` is compilable to a native entry point, returning the tuple type of `types` function check_signature(f, types; target::StaticTarget=StaticTarget(), kwargs...) tt = Base.to_tuple_type(types) @@ -194,8 +144,7 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name= isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" generate_executable(funcs, path, name, filename; target, demangle, cc, cflags, llvm_to_clang, kwargs...) - iswindows(target) && (filename *= ".exe") - joinpath(abspath(path), filename) + joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : Driver(cc), target, filename)) end """ @@ -270,7 +219,7 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); generate_shlib(funcs, path, filename; target, demangle, cc, cflags, llvm_to_clang, kwargs...) - joinpath(abspath(path), filename * "." * dlext(target)) + joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : Driver(cc), target, filename; kind = :shared)) end @@ -360,42 +309,17 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi llvm_to_clang::Bool = needs_ir(target), kwargs... ) - # Choice of compiler - isnothing(cc) && (cc = isnothing(target.compiler) ? clang() : `$(target.compiler)`) - - # Prepare objects or (if llvm_to_clang is set) IR - exec_path = joinpath(path, filename) - _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, kwargs...) + # How it is linked: what `cc` says, or the toolchain of the target + tc = isnothing(cc) ? toolchain(target) : Driver(cc) - # Update paths and flags given options + # Prepare objects or (if llvm_to_clang is set) IR. A linker that is not a compiler driver is given an object that has a `main` fn = demangle ? "$name" : "julia_$name" - iswindows(target) && (exec_path *= ".exe") - llvm_to_clang && (cflags = `-Wno-override-module $(clang_target_flags(target)) $cflags`) - - # Compile! - if isapple(target) && !llvm_to_clang - # Apple no longer uses _start, so we can just specify a custom entry - run(`$cc -e _$fn $obj_or_ir_path $cflags -o $exec_path`) - else - # Write a minimal wrapper to avoid having to specify a custom entry - wrapper_path = joinpath(path, "wrapper$(rand(UInt)).c") - f = open(wrapper_path, "w") - print(f, """int $fn(int argc, char** argv); - void* __stack_chk_guard = (void*) $(rand(UInt) >> 1); - - int main(int argc, char** argv) - { - return $fn(argc, argv); - }""") - close(f) + main_wrapper = tc isa Driver ? nothing : fn + _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, main_wrapper, kwargs...) - # Depending on whether llvm_to_clang is set, - # this may either be compiling from LLVM IR or just linking object files - run(`$cc $wrapper_path $obj_or_ir_path $cflags -o $exec_path`) - - # Clean up - rm(wrapper_path) - end + # Link! + exec_path = joinpath(path, output_name(tc, target, filename)) + link(tc, target, [obj_or_ir_path], exec_path; kind = :executable, entry = fn, cflags) return path, name end @@ -452,17 +376,16 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file llvm_to_clang::Bool = needs_ir(target), kwargs... ) - # Choice of compiler - isnothing(cc) && (cc = isnothing(target.compiler) ? clang() : `$(target.compiler)`) + # How it is linked: what `cc` says, or the toolchain of the target + tc = isnothing(cc) ? toolchain(target) : Driver(cc) # Prepare objects or (if llvm_to_clang is set) IR - lib_path = joinpath(path, "$filename.$(dlext(target))") + lib_path = joinpath(path, output_name(tc, target, filename; kind = :shared)) _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, dllexport=iswindows(target), kwargs...) - - # Compile! - llvm_to_clang && (cflags = `-Wno-override-module $(clang_target_flags(target)) $cflags`) - run(`$cc -shared $obj_or_ir_path $cflags -o $lib_path `) + + # Link! + link(tc, target, [obj_or_ir_path], lib_path; kind = :shared, cflags) return path, filename end @@ -634,29 +557,44 @@ This is a struct of the type StaticTarget() The defaults compile to the native target. If `dllexport` is set, the functions are exported from a Windows DLL. + +If `main_wrapper` is the name of one of the functions, the object will be given a `main` that calls it, +possibly preceeded by any code added by `main_prologue(builder, mod)`. + +`llvm_finalize(mod)`, if given, is called on the whole module when complete. """ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; target::StaticTarget=StaticTarget(), demangle = true, emit_llvm_only = false, dllexport = false, + main_wrapper = nothing, + main_prologue = nothing, llvm_postprocess = nothing, + llvm_finalize = nothing, kwargs...) f, tt = funcs[1] mkpath(path) if emit_llvm_only # (Required on Windows) ir_path = joinpath(path, "$filenamebase.ll") - write(ir_path, generate_ir(funcs; demangle, target, dllexport, llvm_postprocess, kwargs...)) + write(ir_path, generate_ir(funcs; demangle, target, dllexport, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...)) return path, ir_path end - isnothing(target.ir_triple) || throw(ArgumentError("Julia's LLVM has no code generator for $(target.ir_triple), so no object can be made for it here: use `emit_llvm_only = true` (or `generate_ir`) and give the IR to a compiler that has one")) + can_emit_object(target) || throw(ArgumentError("no object can be made for $(target_triple(target)) here: Julia's LLVM has no code generator for it, and no `llc` is given (`StaticTarget(...; llc)`). Use `emit_llvm_only = true` (or `generate_ir`) and give the IR to a compiler that has one")) # compile and generate code in one context, which the module can't outlive GPUCompiler.JuliaContext() do context @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin dllexport && add_dllexport!(mod, funcs; demangle) + isnothing(main_wrapper) || add_main_wrapper!(mod, main_wrapper, target; prologue = main_prologue) + uses_builtins(target.toolchain) && add_builtins!(mod, target_triple(target)) + isnothing(llvm_finalize) || llvm_finalize(mod) obj_path = joinpath(path, "$filenamebase.o") - fakejob, _ = static_job(f, tt; target, kwargs...) - obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) + if isnothing(target.retarget) + fakejob, _ = static_job(f, tt; target, kwargs...) + obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) + else + obj = emit_object(mod, target) # (by the code generator of the target, which is not the host's) + end open(obj_path, "w") do io write(io, obj) end @@ -673,14 +611,81 @@ generate_ir(f, tt; kwargs...) -> String The LLVM IR (as text) of the functions that `generate_obj` would compile to an object file, for tools that take IR instead (a downstream package that makes the object or the executable of a target by itself, for example): nothing is written to the file system, and the module (which cannot outlive its context) is not returned. """ -function generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle = true, dllexport = false, llvm_postprocess = nothing, kwargs...) +function generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle = true, dllexport = false, main_wrapper = nothing, main_prologue = nothing, llvm_postprocess = nothing, llvm_finalize = nothing, kwargs...) GPUCompiler.JuliaContext() do context @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin dllexport && add_dllexport!(mod, funcs; demangle) + isnothing(main_wrapper) || add_main_wrapper!(mod, main_wrapper, target; prologue = main_prologue) + uses_builtins(target.toolchain) && add_builtins!(mod, target_triple(target)) + isnothing(llvm_finalize) || llvm_finalize(mod) return string(mod) end end end generate_ir(f, tt; kwargs...) = generate_ir(((f, tt),); kwargs...) + +""" + link(toolchain, target, inputs::Vector{String}, output::String; kind = :executable, entry = nothing, cflags = ``) -> path + +Link the `inputs` (object files, or LLVM IR files if the toolchain is a `Driver`) +into an executable (`kind = :executable`) or shared library (`kind = :shared`), and return its path. + +If given, `entry` is the name of the function that an executable runs (`int f(int argc, char **argv)`); +with `entry = nothing` the inputs define `main` already, and are linked as they are. +""" +function link(tc::Driver, target::StaticTarget, inputs::Vector{String}, output::String; kind::Symbol = :executable, entry = nothing, cflags = ``) + # Link using a C compiler driver + cc = tc.cc + # (IR, which `clang` compiles itself: it is told what the target is, as it replaces the triple of the IR by the host's) + ir = any(endswith(".ll"), inputs) + ir && (cflags = `-Wno-override-module $(clang_target_flags(target)) $cflags`) + if kind === :shared + run(`$cc -shared $inputs $cflags -o $output`) + elseif isnothing(entry) + run(`$cc $inputs $cflags -o $output`) # (`main` is in the inputs) + elseif isapple(target) && !ir + # Apple no longer uses _start, so we can just specify a custom entry + run(`$cc -e _$entry $inputs $cflags -o $output`) + else + # Write a minimal wrapper to avoid having to specify a custom entry + wrapper_path = joinpath(dirname(output), "wrapper$(rand(UInt)).c") + write(wrapper_path, """int $entry(int argc, char** argv); + void* __stack_chk_guard = (void*) $(rand(UInt) >> 1); + + int main(int argc, char** argv) + { + return $entry(argc, argv); + }""") + try + # (compiling from LLVM IR, or just linking object files, depending on the inputs) + run(`$cc $wrapper_path $inputs $cflags -o $output`) + finally + rm(wrapper_path; force = true) + end + end + return output +end +function link(tc::LLD, target::StaticTarget, inputs::Vector{String}, output::String; kind::Symbol = :executable, entry = nothing, cflags = ``) + # Link using an LLD driver + any(endswith(".ll"), inputs) && throw(ArgumentError("`LLD` links objects, not LLVM IR: make the objects (the target has to be one that Julia's LLVM has the code generator for)")) + if tc.flavor === :gnu + sysroot = tc.sysroot + isnothing(sysroot) && throw(ArgumentError("linking for $(target_triple(target)) needs the C library of the target: give `sysroot = ...` to `LLD(:gnu; ...)`")) + dir = crt_directory(sysroot) + libs = ["-l$l" for l in tc.libs] + if kind === :shared + run(`$(lld()) -flavor gnu -shared -o $output $inputs -L$dir $libs $(tc.flags) $cflags`) + else + loader = something(tc.dynamic_linker, default_dynamic_linker(target)) + run(`$(lld()) -flavor gnu -o $output -dynamic-linker $loader $(joinpath(dir, "crt1.o")) $(joinpath(dir, "crti.o")) $inputs -L$dir $libs $(joinpath(dir, "crtn.o")) $(tc.flags) $cflags`) + end + else # :wasm + kind === :shared && throw(ArgumentError("a shared library for WebAssembly is not supported")) + bits = startswith(target_triple(target), "wasm64") ? "-mwasm64" : "-mwasm32" + run(`$(lld()) -flavor wasm $bits --no-entry --export=main --export-memory --allow-undefined -o $output $inputs $(tc.flags) $cflags`) + end + return output +end + end # module diff --git a/src/builtins.jl b/src/builtins.jl new file mode 100644 index 00000000..712e0623 --- /dev/null +++ b/src/builtins.jl @@ -0,0 +1,262 @@ +## Builtins: the LLVM functions that the code generator may calls and that a C runtime library would have supplied + +# When an object is linked by `lld` alone, nothing supplies the functions that codegen calls that the CPU has no instruction for. +# For example, `__udivti3` for the division of `i128`, `__extendhfsf2` for `Float16`. +# They are defined here in IR, `linkonce_odr` and `hidden`: +# a definition of the program's own, or of a library that is linked, wins, and the module has only those that it uses. +# A toolchain asks for them with `builtins = true` (`LLD` does by default; a `Driver` does not, as the compiler driver links its runtime library). + +# Conversions between `Float16` and `Float32` that the code generator for x86-64 calls where the CPU is not said to have them: with the instructions that do them (F16C: from 2012 on). +const X86_HALF_BUILTINS = """ +define linkonce_odr hidden float @__extendhfsf2(half %a) #9999 { + %r = fpext half %a to float + ret float %r +} +define linkonce_odr hidden half @__truncsfhf2(float %a) #9999 { + %r = fptrunc float %a to half + ret half %r +} +attributes #9999 = { nounwind "target-features"="+f16c" } +""" + +# 128-bit division (`__udivti3` and the others of libgcc and compiler-rt, which a Linux program is not linked with): by shifting and subtracting, with instructions of `i128` that are not calls +# (a division of `i128` in them would be a call of themselves). +const LINUX_INT128_BUILTINS = """ +define linkonce_odr hidden i128 @__static_udivmod(i128 %a, i128 %b, i1 %rem) { +entry: + ; (operands of 64 bits, the usual: the instruction of the CPU; a divisor of zero is left to the loop, which does not trap) + %ah = lshr i128 %a, 64 + %bh = lshr i128 %b, 64 + %hi = or i128 %ah, %bh + %nohi = icmp eq i128 %hi, 0 + %bnz = icmp ne i128 %b, 0 + %small = and i1 %nohi, %bnz + br i1 %small, label %fast, label %loop +fast: + %a64 = trunc i128 %a to i64 + %b64 = trunc i128 %b to i64 + %fq = udiv i64 %a64, %b64 + %fr = urem i64 %a64, %b64 + %fqx = zext i64 %fq to i128 + %frx = zext i64 %fr to i128 + %fres = select i1 %rem, i128 %frx, i128 %fqx + ret i128 %fres +loop: + %i = phi i32 [ 127, %entry ], [ %i1, %loop ] + %q = phi i128 [ 0, %entry ], [ %q2, %loop ] + %r = phi i128 [ 0, %entry ], [ %r3, %loop ] + %i128 = zext i32 %i to i128 + %sh = lshr i128 %a, %i128 + %bit = and i128 %sh, 1 + %r1 = shl i128 %r, 1 + %r2 = or i128 %r1, %bit + %ge = icmp uge i128 %r2, %b + %rs = sub i128 %r2, %b + %r3 = select i1 %ge, i128 %rs, i128 %r2 + %one = shl i128 1, %i128 + %qo = or i128 %q, %one + %q2 = select i1 %ge, i128 %qo, i128 %q + %i1 = sub i32 %i, 1 + %done = icmp slt i32 %i1, 0 + br i1 %done, label %exit, label %loop +exit: + %res = select i1 %rem, i128 %r3, i128 %q2 + ret i128 %res +} +define linkonce_odr hidden i128 @__udivti3(i128 %a, i128 %b) { + %r = call i128 @__static_udivmod(i128 %a, i128 %b, i1 false) + ret i128 %r +} +define linkonce_odr hidden i128 @__umodti3(i128 %a, i128 %b) { + %r = call i128 @__static_udivmod(i128 %a, i128 %b, i1 true) + ret i128 %r +} +define linkonce_odr hidden i128 @__divti3(i128 %a, i128 %b) { + %sa = icmp slt i128 %a, 0 + %sb = icmp slt i128 %b, 0 + %na = sub i128 0, %a + %nb = sub i128 0, %b + %ua = select i1 %sa, i128 %na, i128 %a + %ub = select i1 %sb, i128 %nb, i128 %b + %q = call i128 @__static_udivmod(i128 %ua, i128 %ub, i1 false) + %neg = xor i1 %sa, %sb + %nq = sub i128 0, %q + %r = select i1 %neg, i128 %nq, i128 %q + ret i128 %r +} +define linkonce_odr hidden i128 @__modti3(i128 %a, i128 %b) { + %sa = icmp slt i128 %a, 0 + %sb = icmp slt i128 %b, 0 + %na = sub i128 0, %a + %nb = sub i128 0, %b + %ua = select i1 %sa, i128 %na, i128 %a + %ub = select i1 %sb, i128 %nb, i128 %b + %m = call i128 @__static_udivmod(i128 %ua, i128 %ub, i1 true) + %nm = sub i128 0, %m + %r = select i1 %sa, i128 %nm, i128 %m + ret i128 %r +} +""" + +# The same conversions for WebAssembly, which has no instruction for it. +# by integer arithmetic (the rounding is to the nearest, ties to even) +const WASM_HALF_BUILTINS = """ +define linkonce_odr hidden float @__extendhfsf2(half %a) { + %h = bitcast half %a to i16 + %hi = zext i16 %h to i32 + %s1 = lshr i32 %hi, 15 + %s2 = shl i32 %s1, 31 + %e1 = lshr i32 %hi, 10 + %e = and i32 %e1, 31 + %m = and i32 %hi, 1023 + %en = add i32 %e, 112 + %en2 = shl i32 %en, 23 + %m13 = shl i32 %m, 13 + %n1 = or i32 %s2, %en2 + %normal = or i32 %n1, %m13 + %i1 = or i32 %s2, 2139095040 + %infnan = or i32 %i1, %m13 + %mf = uitofp i32 %m to float + %sub = fmul float %mf, 0x3E70000000000000 + %subb = bitcast float %sub to i32 + %subs = or i32 %subb, %s2 + %isz = icmp eq i32 %e, 0 + %isi = icmp eq i32 %e, 31 + %r1 = select i1 %isi, i32 %infnan, i32 %normal + %r = select i1 %isz, i32 %subs, i32 %r1 + %f = bitcast i32 %r to float + ret float %f +} +define linkonce_odr hidden half @__truncsfhf2(float %a) { + %x = bitcast float %a to i32 + %s1 = lshr i32 %x, 16 + %sign = and i32 %s1, 32768 + %e1 = lshr i32 %x, 23 + %e = and i32 %e1, 255 + %m = and i32 %x, 8388607 + %ne = sub i32 %e, 112 + ; infinity and NaN + %m13 = lshr i32 %m, 13 + %nanm = or i32 %m13, 512 + %hasm = icmp ne i32 %m, 0 + %nanbits = select i1 %hasm, i32 %nanm, i32 0 + %infnan = or i32 %nanbits, 31744 + ; normal + %ne10 = shl i32 %ne, 10 + %hn = or i32 %ne10, %m13 + %rem = and i32 %m, 8191 + %gt = icmp ugt i32 %rem, 4096 + %eq = icmp eq i32 %rem, 4096 + %odd1 = and i32 %hn, 1 + %odd = icmp ne i32 %odd1, 0 + %tie = and i1 %eq, %odd + %up = or i1 %gt, %tie + %upi = zext i1 %up to i32 + %hn2 = add i32 %hn, %upi + ; subnormal + %mm = or i32 %m, 8388608 + %shift = sub i32 14, %ne + %shiftc = call i32 @llvm.umin.i32(i32 %shift, i32 31) + %hs = lshr i32 %mm, %shiftc + %one = shl i32 1, %shiftc + %mask = sub i32 %one, 1 + %rem2 = and i32 %mm, %mask + %half1 = lshr i32 %one, 1 + %gt2 = icmp ugt i32 %rem2, %half1 + %eq2 = icmp eq i32 %rem2, %half1 + %odd2a = and i32 %hs, 1 + %odd2 = icmp ne i32 %odd2a, 0 + %tie2 = and i1 %eq2, %odd2 + %up2 = or i1 %gt2, %tie2 + %up2i = zext i1 %up2 to i32 + %hs2 = add i32 %hs, %up2i + ; which + %isinf = icmp eq i32 %e, 255 + %ovf = icmp sge i32 %ne, 31 + %isnorm = icmp sge i32 %ne, 1 + %issub = icmp sge i32 %ne, -10 + %r1 = select i1 %issub, i32 %hs2, i32 0 + %r2 = select i1 %isnorm, i32 %hn2, i32 %r1 + %r3 = select i1 %ovf, i32 31744, i32 %r2 + %r4 = select i1 %isinf, i32 %infnan, i32 %r3 + %r5 = or i32 %r4, %sign + %r16 = trunc i32 %r5 to i16 + %h = bitcast i16 %r16 to half + ret half %h +} +declare i32 @llvm.umin.i32(i32, i32) +; double to half: through float with the rounding to odd (inexact results get an odd last bit: the second rounding is then the one of the double), and half to double +define linkonce_odr hidden half @__truncdfhf2(double %a) { + %f = fptrunc double %a to float + %back = fpext float %f to double + %exact = fcmp oeq double %back, %a + %isnan = fcmp uno double %a, %a + %fb = bitcast float %f to i32 + %odd = and i32 %fb, 1 + %isodd = icmp ne i32 %odd, 0 + %nb = fneg double %back + %bneg = fcmp olt double %back, 0.0 + %ab = select i1 %bneg, double %nb, double %back + %na = fneg double %a + %aneg = fcmp olt double %a, 0.0 + %aa = select i1 %aneg, double %na, double %a + %up = fcmp ogt double %ab, %aa + %dec = sub i32 %fb, 1 + %inc = add i32 %fb, 1 + %adj = select i1 %up, i32 %dec, i32 %inc + %isinf = fcmp oeq float %f, 0x7FF0000000000000 + %k1 = or i1 %exact, %isodd + %k2 = or i1 %k1, %isnan + %k3 = or i1 %k2, %isinf + %r = select i1 %k3, i32 %fb, i32 %adj + %rf = bitcast i32 %r to float + %h = call half @__truncsfhf2(float %rf) + ret half %h +} +define linkonce_odr hidden double @__extendhfdf2(half %a) { + %f = call float @__extendhfsf2(half %a) + %d = fpext float %f to double + ret double %d +} +""" + +# The builtins that the IR needs for the target: the names of the constants above. `text` is the IR; the system and the architecture are the triple's. +function needed_builtins(text::AbstractString, triple::AbstractString) + family = llvm_family(first(split(triple, '-'))) + has_half = occursin(r"\bhalf\b", text) + needed = Symbol[] + family == "x86" && startswith(triple, "x86_64") && has_half && push!(needed, :X86_HALF_BUILTINS) + family == "wasm" && has_half && push!(needed, :WASM_HALF_BUILTINS) + occursin("linux", triple) && occursin(r"\b(udiv|sdiv|urem|srem) i128\b", text) && push!(needed, :LINUX_INT128_BUILTINS) + return needed +end + +""" + add_builtins(text::AbstractString, triple::AbstractString) -> String + +The LLVM IR `text` with the definitions of the builtins that it needs for `triple` (see [`add_builtins!`](@ref)). +""" +function add_builtins(text::AbstractString, triple::AbstractString) + for name in needed_builtins(text, triple) + text = string(text, "\n", getfield(@__MODULE__, name)) + end + return text +end + +""" + add_builtins!(mod::LLVM.Module, triple::AbstractString) -> mod + +Define in the module the builtins that it needs for the target `triple`: the functions that the code generator calls for what the CPU has no instruction for, which a C runtime library (libgcc, compiler-rt) has and an object +that is linked by `lld` alone has not. Which are needed is what the module says: `i128` division on Linux, `Float16` conversions on x86-64 and WebAssembly. +""" +function add_builtins!(mod::LLVM.Module, triple::AbstractString) + for name in needed_builtins(string(mod), triple) + builtins = parse(LLVM.Module, getfield(@__MODULE__, name)) + # (the linker of modules takes a `linkonce_odr` definition only where something in the module calls it, and nothing does: the code generator will. `weak_odr` is as well replaced by a definition of the program's own) + for f in LLVM.functions(builtins) + LLVM.isdeclaration(f) || LLVM.linkage!(f, LLVM.API.LLVMWeakODRLinkage) + end + LLVM.link!(mod, builtins) + end + return mod +end diff --git a/src/dllexport.jl b/src/dllexport.jl deleted file mode 100644 index 018ea2f4..00000000 --- a/src/dllexport.jl +++ /dev/null @@ -1,10 +0,0 @@ -# export the compiled entry points from a Windows DLL -function add_dllexport!(mod::LLVM.Module, funcs; demangle=true) - for (f, _) in funcs - name_f = (demangle ? "" : "julia_") * fix_name(f) - llvmf = get(mod.functions, name_f, nothing) - if llvmf !== nothing && !isdeclaration(llvmf) - llvmf.dllstorage = LLVM.DLLStorageClass.Export - end - end -end diff --git a/src/llvm.jl b/src/llvm.jl new file mode 100644 index 00000000..6e548cf3 --- /dev/null +++ b/src/llvm.jl @@ -0,0 +1,179 @@ +## The LLVM module on its way from Julia's code generation to an object, in order + +function locate_pointers_and_runtime_calls(mod) + for func ∈ mod.functions, bb ∈ func.blocks, inst ∈ bb.instructions + @debug "Inspecting instruction" inst + warned = false + for operand ∈ pointer_operands(inst) + warned |= inspect_pointer(inst, operand) + end + if warned + lines = split(string(func),"\n") + @warn("LLVM function generated warnings due to raw pointers embedded in the code. This will likely cause errors or undefined behaviour.", + func = join(lines[1:min(20, end)], "\n")) # just print the first 20 lines + end + end +end + +# The operands of `inst` that may be pointers to julia data or runtime functions +pointer_operands(inst) = () +pointer_operands(inst::Union{LLVM.LoadInst, LLVM.StoreInst, LLVM.RetInst, LLVM.BitCastInst}) = inst.operands +pointer_operands(inst::LLVM.CallInst) = (inst.called_operand,) + +isinttoptr(x) = x isa LLVM.ConstantExpr && occursin("inttoptr", string(x)) + +# The address embedded in an `inttoptr` constant, or `nothing` if it is not a known constant +function constant_address(x) + op = x.operands[1] + if op isa LLVM.ConstantExpr + op = op.operands[1] + end + op isa LLVM.ConstantInt ? convert(Int, op) : nothing +end + +# Warn if `operand` of `inst` is a raw pointer, returning whether it was +function inspect_pointer(inst, operand) + isinttoptr(operand) || return false + address = constant_address(operand) + if isnothing(address) + @warn("Found pointer reference to an unknown address", "llvm instruction" = inst) + else + frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), Ptr{Cvoid}(address), 0) + data_warnings(inst, frames) + end + return true +end + +data_warnings(inst, frames) = for frame ∈ frames + fn, file, line, linfo, fromC, inlined = frame + @warn("Found pointer references to julia data", + "llvm instruction" = inst, + name = fn, + file = file, + line = line, + fromC = fromC, + inlined = inlined) +end + +# The IR of a target that requires retargeting (`target.retarget`) gets the triple of the target, +# no data layout (the code generator that makes the object has the target's), +# and no attributes that name the host's CPU and features. +# IR that has intrinsics of the host's architecture is refused. +function retarget!(mod::LLVM.Module, target::StaticTarget) + isnothing(target.retarget) && return mod + triple = target.retarget.triple + family = llvm_family(first(split(triple, '-'))) + for f in LLVM.functions(mod) + m = match(r"^llvm\.(aarch64|arm|x86|wasm)\.", LLVM.name(f)) + isnothing(m) || m.captures[1] == family || throw(ArgumentError("the program uses `$(LLVM.name(f))`, an intrinsic of the code generator of the host's architecture: it can not be made for $triple")) + attrs = LLVM.function_attributes(f) + for kind in ("target-cpu", "target-features", "tune-cpu") + delete!(attrs, kind) + end + end + LLVM.triple!(mod, triple) + LLVM.datalayout!(mod, "") + return mod +end + +# `int main(int argc, char **argv)` that calls the function `entry` of the module +# Includes `__stack_chk_guard`, which a C library that expect one needs somewhere. +# A module that has no `entry` is refused. +# `prologue(builder, mod)`, if given, adds to the start of `main`, before the call. +function add_main_wrapper!(mod::LLVM.Module, entry::AbstractString, target::StaticTarget; prologue = nothing) + haskey(LLVM.functions(mod), entry) || throw(ArgumentError("the module has no function `$entry` to be called by `main`")) + f = LLVM.functions(mod)[entry] + i32, ptr = LLVM.IntType(32), LLVM.PointerType() + fty = LLVM.function_type(f) + nparams = length(LLVM.parameters(fty)) + nparams in (0, 2) || throw(ArgumentError("`$entry` takes nothing or an `argc` and an `argv`, not $nparams arguments")) + haskey(LLVM.functions(mod), "main") && throw(ArgumentError("the module has a `main` already")) + main = LLVM.Function(mod, "main", LLVM.FunctionType(i32, [i32, ptr])) + LLVM.@dispose builder = LLVM.IRBuilder() begin + LLVM.position!(builder, LLVM.at_end(LLVM.BasicBlock(main, "entry"))) + isnothing(prologue) || prologue(builder, mod) # (what the program does before its entry point is called) + args = LLVM.Value[] + if nparams == 2 + argc_type, argv_type = LLVM.parameters(fty) + argc, argv = LLVM.parameters(main) + push!(args, LLVM.sext!(builder, argc, argc_type)) # (an `Int`, usually: `argc` is 32 bits) + push!(args, argv) + end + r = LLVM.call!(builder, fty, f, args) + rty = LLVM.return_type(fty) + result = if rty isa LLVM.VoidType + LLVM.ConstantInt(i32, 0) + elseif rty isa LLVM.IntegerType + LLVM.width(rty) > 32 ? LLVM.trunc!(builder, r, i32) : LLVM.width(rty) < 32 ? LLVM.zext!(builder, r, i32) : r + else + LLVM.ConstantInt(i32, 0) # (what is not an integer is not an exit status) + end + LLVM.ret!(builder, result) + end + triple = target_triple(target) + if !(isapple(target) || startswith(triple, "wasm") || iswindows(target)) && !haskey(LLVM.globals(mod), "__stack_chk_guard") + guard = LLVM.GlobalVariable(mod, ptr, "__stack_chk_guard") + LLVM.initializer!(guard, LLVM.const_inttoptr(LLVM.ConstantInt(LLVM.IntType(64), rand(UInt) >> 1), ptr)) + end + return mod +end + +# Export the compiled entry points from a Windows DLL +function add_dllexport!(mod::LLVM.Module, funcs; demangle=true) + for (f, _) in funcs + name_f = (demangle ? "" : "julia_") * fix_name(f) + llvmf = get(mod.functions, name_f, nothing) + if llvmf !== nothing && !isdeclaration(llvmf) + llvmf.dllstorage = LLVM.DLLStorageClass.Export + end + end +end + +## Generating object files from LLVM + +""" + emit_object(mod::LLVM.Module, target::StaticTarget) -> Vector{UInt8} + emit_object(ir::AbstractString, target::StaticTarget) -> Vector{UInt8} + +The object file (the bytes) of the IR, which was made for the host and retargeted (see [`Retarget`](@ref) and [`retarget!`](@ref)), for the target: by the code generator of Julia's LLVM if it has one for the +triple, and else by the `llc` of the target. The CPU and the features are those of the `Retarget`, and the code is position independent (WebAssembly: static). +""" +function emit_object(mod::LLVM.Module, target::StaticTarget) + r = target.retarget + isnothing(r) && throw(ArgumentError("only the objects of a target that is made for by retargeting are made by `emit_object` (see `Retarget`)")) + julia_llvm_has(r.triple) || return emit_object_llc(string(mod), r) + tm = LLVM.TargetMachine(LLVM.Target(; triple = r.triple), r.triple; cpu = r.cpu, features = r.features, opt_level = LLVM.API.LLVMCodeGenLevelDefault, + reloc = startswith(r.triple, "wasm") ? LLVM.API.LLVMRelocStatic : LLVM.API.LLVMRelocPIC) + try + return LLVM.emit(tm, mod, LLVM.API.LLVMObjectFile) + finally + LLVM.dispose(tm) + end +end +function emit_object(ir::AbstractString, target::StaticTarget) + r = target.retarget + isnothing(r) && throw(ArgumentError("only the objects of a target that is made for by retargeting are made by `emit_object` (see `Retarget`)")) + julia_llvm_has(r.triple) || return emit_object_llc(ir, r) + LLVM.@dispose ctx = LLVM.Context() begin + mod = parse(LLVM.Module, ir) + try + return emit_object(mod, target) + finally + LLVM.dispose(mod) + end + end +end + +# Emit the object file via `llc`, if it has a code generator that Julia's LLVM has not +function emit_object_llc(ir::AbstractString, r::Retarget) + isnothing(r.llc) && throw(ArgumentError("no object can be made for $(r.triple): Julia's LLVM has no code generator for it, and no `llc` is given (`StaticTarget(...; llc = ...)`, of the LLVM version that Julia has, with the code generator for it). The IR can be made (`generate_ir`, `emit_llvm_only = true`)")) + dir = mktempdir() + try + ll, obj = joinpath(dir, "module.ll"), joinpath(dir, "module.o") + write(ll, ir) + run(`$(r.llc) -O2 -filetype=obj -mtriple=$(r.triple) -mcpu=$(isempty(r.cpu) ? "generic" : r.cpu) -mattr=$(r.features) -relocation-model=pic $ll -o $obj`) + return read(obj) + finally + rm(dir; recursive = true, force = true) + end +end diff --git a/src/pointer_warning.jl b/src/pointer_warning.jl deleted file mode 100644 index 3fe12309..00000000 --- a/src/pointer_warning.jl +++ /dev/null @@ -1,54 +0,0 @@ -function locate_pointers_and_runtime_calls(mod) - for func ∈ mod.functions, bb ∈ func.blocks, inst ∈ bb.instructions - @debug "Inspecting instruction" inst - warned = false - for operand ∈ pointer_operands(inst) - warned |= inspect_pointer(inst, operand) - end - if warned - lines = split(string(func),"\n") - @warn("LLVM function generated warnings due to raw pointers embedded in the code. This will likely cause errors or undefined behaviour.", - func = join(lines[1:min(20, end)], "\n")) # just print the first 20 lines - end - end -end - -# the operands of `inst` that may be pointers to julia data or runtime functions -pointer_operands(inst) = () -pointer_operands(inst::Union{LLVM.LoadInst, LLVM.StoreInst, LLVM.RetInst, LLVM.BitCastInst}) = inst.operands -pointer_operands(inst::LLVM.CallInst) = (inst.called_operand,) - -isinttoptr(x) = x isa LLVM.ConstantExpr && occursin("inttoptr", string(x)) - -# the address embedded in an `inttoptr` constant, or `nothing` if it is not a known constant -function constant_address(x) - op = x.operands[1] - if op isa LLVM.ConstantExpr - op = op.operands[1] - end - op isa LLVM.ConstantInt ? convert(Int, op) : nothing -end - -# warn if `operand` of `inst` is a raw pointer, returning whether it was -function inspect_pointer(inst, operand) - isinttoptr(operand) || return false - address = constant_address(operand) - if isnothing(address) - @warn("Found pointer reference to an unknown address", "llvm instruction" = inst) - else - frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), Ptr{Cvoid}(address), 0) - data_warnings(inst, frames) - end - return true -end - -data_warnings(inst, frames) = for frame ∈ frames - fn, file, line, linfo, fromC, inlined = frame - @warn("Found pointer references to julia data", - "llvm instruction" = inst, - name = fn, - file = file, - line = line, - fromC = fromC, - inlined = inlined) -end diff --git a/src/target.jl b/src/target.jl index 3c99bfde..526d4bbe 100644 --- a/src/target.jl +++ b/src/target.jl @@ -1,3 +1,16 @@ +""" + Retarget(triple, cpu, features, llc) + +What a target needs to be made for by retargeting: Julia's code generation makes the code for the host (`tm` of the target is the host's), and its IR gets the `triple` (see [`retarget!`](@ref)); the object is then made +for the `triple`, with `cpu` and `features`, by the code generator of Julia's LLVM if it has one for it, and else by the external `llc` (which has to be of the LLVM version that Julia has), if there is one. +""" +struct Retarget + triple::String + cpu::String + features::String + llc::Union{Cmd,Nothing} +end + """ ```julia StaticTarget() # Native target @@ -13,7 +26,7 @@ Beware that currently the compilation assumes that the code is on the host so pl does not behave as expected. By default `StaticTarget()` is the native target. -If Julia's LLVM has no code generator for the target (x86-64 where LLVM has only AArch64 and WebAssembly, say), the target is made anyway, with `ir_triple` set: IR can be made for it +If Julia's LLVM has no code generator for the target (x86-64 where LLVM has only AArch64 and WebAssembly, say), the target is made anyway, as a `Retarget` (the field `retarget`): IR can be made for it (`generate_ir`, `emit_llvm_only = true`, `llvm_to_clang = true`: the IR has the target's triple and no host CPU or features), and an external `clang` or `llc` that has the code generator makes the object. For cross-compilation of executables and shared libraries, one also needs to call `set_compiler!` with the path to a valid C compiler @@ -25,34 +38,53 @@ for the target platform. For example, to cross-compile for aarch64 using a compi mutable struct StaticTarget platform::Union{Platform,Nothing} tm::LLVM.TargetMachine - compiler::Union{String,Nothing} # A target-specific compiler, else nothing - ir_triple::Union{String,Nothing} # A target that requires us to output IR only, else nothing + toolchain::Union{Toolchain,Nothing} # How the target is linked (a `Driver` of a target-specific compiler, an `LLD`, ...), else the default of `default_toolchain` + retarget::Union{Retarget,Nothing} # A target that the code is made for as for the host and then retargeted (see `Retarget`), else nothing julia_runtime::Bool end -StaticTarget(platform, tm, compiler, julia_runtime) = StaticTarget(platform, tm, compiler, nothing, julia_runtime) +StaticTarget(platform, tm, compiler, julia_runtime) = StaticTarget(platform, tm, as_toolchain(compiler), nothing, julia_runtime) # (`compiler`: the C compiler of a `Driver`, as the field was) StaticTarget() = StaticTarget(HostPlatform(), LLVM.host_cpu_name(), LLVM.host_cpu_features()) -# The target for a platform or a triple, with the generic CPU of its architecture unless `cpu` and `features` say otherwise. Julia's LLVM may have no code generator for it: then `ir_triple` is set, `tm` -# is the host's, and only IR can be made (see above). -function StaticTarget(target::Union{Platform,AbstractString}, cpu::AbstractString = "", features::AbstractString = "") +# The target for a platform or a triple, with the generic CPU of its architecture unless `cpu` and `features` say otherwise. +# If Julia's LLVM has no code generator for it, then the target is a `Retarget`, `tm` is the host's, and the target is a `Retarget`: +# Then the object is made by `llc` (the keyword `llc`, a `Cmd` or a path). +# `retarget = true` makes such a target for any triple, with the generic CPU of the host +# (or `retarget = host_target`, a `StaticTarget` of the host, with its machine). +function StaticTarget(target::Union{Platform,AbstractString}, cpu::AbstractString = "", features::AbstractString = ""; toolchain = nothing, retarget::Union{Bool,StaticTarget} = false, llc = nothing) + init_llvm_targets() platform = target isa Platform ? target : tryparse(Platform, target) triple = target isa Platform ? clean_triple(target) : String(target) + retargeted = Retarget(LLVM.normalize(triple), cpu, features, llc === nothing ? nothing : llc isa Cmd ? llc : `$llc`) + retarget === false || return StaticTarget(platform, retarget isa StaticTarget ? retarget.tm : host_generic_machine(), as_toolchain(toolchain), retargeted, false) tm = try LLVM.TargetMachine(LLVM.Target(triple = triple), triple; cpu, features) catch err # (`ArgumentError`: "No available targets are compatible with triple") err isa ArgumentError || rethrow() - return StaticTarget(platform, host_generic_machine(), nothing, LLVM.normalize(triple), false) + return StaticTarget(platform, host_generic_machine(), as_toolchain(toolchain), retargeted, false) end - return StaticTarget(platform, tm, nothing, nothing, false) + return StaticTarget(platform, tm, as_toolchain(toolchain), nothing, false) end clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform) -# the host's target machine without its CPU and its features, which are not those of another architecture's code (and not to be in its IR) +# The code generators of every target that Julia's LLVM has, which are not all there until they are initialized (GPUCompiler does it when it first compiles, which is after a target is made) +const LLVM_TARGETS_INITIALIZED = Ref(false) +function init_llvm_targets() + LLVM_TARGETS_INITIALIZED[] && return + LLVM.InitializeAllTargetInfos() + LLVM.InitializeAllTargets() + LLVM.InitializeAllTargetMCs() + LLVM.InitializeAllAsmPrinters() + LLVM.InitializeAllAsmParsers() + LLVM_TARGETS_INITIALIZED[] = true + return +end + +# The host's target machine without its CPU and its features, which are not those of another architecture's code (and not to be in its IR) host_generic_machine() = LLVM.TargetMachine(LLVM.Target(triple = LLVM.default_triple()), LLVM.default_triple()) -"The triple that the target is for: the one of its IR if it has no code generator in Julia's LLVM, and else the one of its target machine" -target_triple(target::StaticTarget) = something(target.ir_triple, LLVM.triple(target.tm)) +# The triple that the target is for: the one of its `Retarget`, else the one of its target machine +target_triple(target::StaticTarget) = isnothing(target.retarget) ? LLVM.triple(target.tm) : target.retarget.triple """ ```julia @@ -63,7 +95,15 @@ Set the compiler for cross compilation. For example: set_compiler!(StaticTarget(parse(Platform,"aarch64-gnu-linux")), "/opt/homebrew/bin/aarch64-elf-gcc") ``` """ -set_compiler!(target::StaticTarget, compiler::String) = (target.compiler = compiler) +set_compiler!(target::StaticTarget, compiler::Union{AbstractString,Cmd}) = (target.toolchain = Driver(compiler)) + +""" +```julia +set_toolchain!(target::StaticTarget, toolchain::Toolchain) +``` +Set how the target is linked: a [`Driver`](@ref) (a C compiler) or an [`LLD`](@ref) (`lld` and the C library of a sysroot). +""" +set_toolchain!(target::StaticTarget, tc::Toolchain) = (target.toolchain = tc) set_runtime!(target::StaticTarget, julia_runtime::Bool) = (target.julia_runtime = julia_runtime) @@ -98,8 +138,6 @@ module StaticRuntime end -GPUCompiler.llvm_triple(target::StaticCompilerTarget) = target.triple - function GPUCompiler.llvm_machine(target::StaticCompilerTarget) triple = GPUCompiler.llvm_triple(target) @@ -110,12 +148,9 @@ function GPUCompiler.llvm_machine(target::StaticCompilerTarget) return tm end - +GPUCompiler.llvm_triple(target::StaticCompilerTarget) = target.triple GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = StaticRuntime - - GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = true - GPUCompiler.uses_julia_runtime(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = job.config.target.julia_runtime GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget})) = job.config.target.method_table @@ -138,3 +173,32 @@ function static_job(@nospecialize(func), @nospecialize(types); config = GPUCompiler.CompilerConfig(gputarget, params; name, kernel, kwargs...) return GPUCompiler.CompilerJob(source, config), Dict{}() end + +## The target + +# Whether or not Julia's LLVM has the code generator for the triple +julia_llvm_has(triple::AbstractString) = (init_llvm_targets(); try; LLVM.Target(; triple = String(triple)); true; catch; false; end) + +# Whether an object can be made for the target here: always if target=host, and for a `Retarget` if Julia's LLVM or an `llc` has the code generator +can_emit_object(target::StaticTarget) = isnothing(target.retarget) || julia_llvm_has(target.retarget.triple) || !isnothing(target.retarget.llc) + +# Make any OS-specific choices on the basis of the target, not the host +iswindows(target::StaticTarget) = isnothing(target.platform) ? occursin(r"windows|mingw|msvc|cygwin"i, target_triple(target)) : Sys.iswindows(target.platform) +isapple(target::StaticTarget) = isnothing(target.platform) ? occursin(r"apple|darwin|macos"i, target_triple(target)) : Sys.isapple(target.platform) + +# The shared library extension of the target (that of the host, unless cross-compiling) +dlext(target::StaticTarget) = isnothing(target.platform) ? Libdl.dlext : platform_dlext(target.platform) + +# Whether the IR has to go through an external compiler (`clang`, `llc`) to be an object: on Windows (always), or where Julia's LLVM has no code generator for the target +# (An `LLD` links objects, which IR is not: for it an object that cannot be made says so, by `generate_obj`, and does not go on as IR.) +needs_ir(target::StaticTarget) = (iswindows(target) || !can_emit_object(target)) && !(target.toolchain isa LLD) + +# `clang` makes code for its own host unless it is told the target (it replaces the triple of the IR, which `-Wno-override-module` hides) +function clang_target_flags(target::StaticTarget) + host = HostPlatform() + foreign = !isnothing(target.retarget) || (!isnothing(target.platform) && (arch(target.platform) != arch(host) || os(target.platform) != os(host))) + return foreign ? `--target=$(target_triple(target))` : `` +end + +# The family that an architecture's intrinsics (`llvm..`) are named by +llvm_family(arch::AbstractString) = startswith(arch, "aarch64") || startswith(arch, "arm64") ? "aarch64" : startswith(arch, "arm") ? "arm" : arch in ("x86_64", "i686", "i386", "x86") ? "x86" : startswith(arch, "wasm") ? "wasm" : arch diff --git a/src/toolchain.jl b/src/toolchain.jl new file mode 100644 index 00000000..2414a73d --- /dev/null +++ b/src/toolchain.jl @@ -0,0 +1,108 @@ +## Linking: how the object code of a target becomes an executable or a shared library + +@doc """ + Toolchain + +How a target is linked. [`link`](@ref) and [`output_name`](@ref) have a method for each type; new targets (another system, another kind of executable) are new types with those two methods. +`Driver` (a C compiler that compiles and links, as before) is the default, [`LLD`](@ref) links objects by itself (for a Linux or WebAssembly target that the host's compiler can not link for). +Give a target its toolchain by `StaticTarget(...; toolchain = ...)` or `set_toolchain!`. +""" Toolchain + +""" + Driver(cc::Cmd) + +Link by a C compiler driver (`clang` by default): the driver compiles what it is given, objects or LLVM IR (`.ll`), and links it. This is how executables and shared libraries were made before toolchains, +and it still is for the host and for any target that has a driver of its own (`set_compiler!`). +""" +struct Driver <: Toolchain + cc::Cmd + builtins::Bool +end +Driver(cc::Cmd; builtins::Bool = false) = Driver(cc, builtins) +Driver(cc::AbstractString; builtins::Bool = false) = Driver(`$cc`; builtins) +Base.:(==)(a::Driver, b::Driver) = a.cc == b.cc && a.builtins == b.builtins + +""" + LLD(flavor; sysroot = nothing, dynamic_linker = nothing, libs = ["c", "m", "dl"], flags = ``, builtins = true) + +Link objects with the `lld` that Julia has, without a C compiler or its driver. `flavor` is `:gnu` (ELF, Linux: the start files and the libraries of the C library are in `sysroot`, a directory that has the `lib` of a glibc, +which the `Glibc_jll` artifact of the target's platform is) or `:wasm` (WebAssembly: a `.wasm` module that exports the entry point and imports the rest). `flags` are added to the command of the linker. With `builtins`, the object has the definitions of what the code generator calls and no library of the toolchain has (`__udivti3`, `__extendhfsf2`, ...: see [`add_builtins!`](@ref)). +Nothing makes a `main`: the object has one (see the `main_wrapper` of [`generate_obj`](@ref); [`generate_executable`](@ref) asks for it when the toolchain is not a `Driver`). +""" +struct LLD <: Toolchain + flavor::Symbol + sysroot::Union{String,Nothing} + dynamic_linker::Union{String,Nothing} + libs::Vector{String} + flags::Cmd + builtins::Bool +end +function LLD(flavor::Symbol; sysroot = nothing, dynamic_linker = nothing, libs = ["c", "m", "dl"], flags = ``, builtins::Bool = true) + flavor in (:gnu, :wasm) || throw(ArgumentError("the flavor of `LLD` is `:gnu` or `:wasm`, not `$(repr(flavor))`")) + return LLD(flavor, isnothing(sysroot) ? nothing : String(sysroot), isnothing(dynamic_linker) ? nothing : String(dynamic_linker), String.(libs), flags isa Cmd ? flags : `$flags`, builtins) +end + +# (what a toolchain is made from: a toolchain, or the C compiler of a `Driver`) +as_toolchain(x::Toolchain) = x +as_toolchain(x::Cmd) = Driver(x) +as_toolchain(x::AbstractString) = Driver(x) +as_toolchain(::Nothing) = nothing + + +""" + output_name(toolchain, target, name; kind = :executable) -> String + +The file name of what is made from `name` for the target: `name.exe` for a Windows executable, `name.wasm` for WebAssembly, the shared library extension of the target for `kind = :shared`. +""" +output_name(::Toolchain, target, name::AbstractString; kind::Symbol = :executable) = + kind === :shared ? "$name.$(dlext(target))" : iswindows(target) ? "$name.exe" : String(name) +output_name(tc::LLD, target, name::AbstractString; kind::Symbol = :executable) = + tc.flavor === :wasm ? "$name.wasm" : kind === :shared ? "$name.so" : String(name) + +# whether the objects that the toolchain links need the builtins in them (`add_builtins!`) +uses_builtins(tc::Union{Driver,LLD}) = tc.builtins +uses_builtins(::Union{Toolchain,Nothing}) = false + +""" + glibc_sysroot(platform_or_triple_or_target) -> String + +The directory of the C library of a Linux target (for `LLD(:gnu; sysroot = ...)`): the artifact of `Glibc_jll` for its architecture, which is fetched if it is not there, for any host. +Needs `using Glibc_jll, Pkg`. +""" +function glibc_sysroot end + +# The toolchain of a target: its own, or the default: the host's C compiler driver +default_toolchain(::StaticTarget) = Driver(clang()) +toolchain(target::StaticTarget) = something(target.toolchain, default_toolchain(target)) + +# Generally prefer Clang_jll clang (which should probably be at least as new as Julia's LLVM) +# and fall back to the system one on platforms where Clang_jll is not available or not expected to work +function clang() + Clang_jll.is_available() || return `clang` # Fall back to system clang if Clang_jll is not available + cmd = Clang_jll.clang() + # Clang_jll may not know where to find the macOS SDK (e.g. for `-lSystem`) + if Sys.isapple() && !haskey(ENV, "SDKROOT") + isnothing(Sys.which("xcrun")) && return `clang` # Fall back to system clang if we can't figure out the SDK path + cmd = addenv(cmd, "SDKROOT" => readchomp(`xcrun --show-sdk-path`)) + end + return cmd +end + + +## lld + +# the directory of a sysroot that has the start files of the C library +function crt_directory(sysroot::AbstractString) + for d in ("lib", "usr/lib", "lib64", "usr/lib64", "usr/lib/aarch64-linux-gnu", "usr/lib/x86_64-linux-gnu", "lib/aarch64-linux-gnu", "lib/x86_64-linux-gnu") + isfile(joinpath(sysroot, d, "crt1.o")) && return joinpath(sysroot, d) + end + throw(ArgumentError("`$sysroot` has no `crt1.o`: the `sysroot` of a `:gnu` `LLD` is a directory that has the `lib` of a glibc (the `Glibc_jll` artifact for the platform of the target is one)")) +end + +# the loader that a program of the architecture asks for, where it is on the system that it runs on +function default_dynamic_linker(target::StaticTarget) + a = llvm_family(first(split(target_triple(target), '-'))) + a == "x86" && return "/lib64/ld-linux-x86-64.so.2" + a == "aarch64" && return "/lib/ld-linux-aarch64.so.1" + throw(ArgumentError("no dynamic linker is known for $(target_triple(target)): give `dynamic_linker` to `LLD`")) +end \ No newline at end of file diff --git a/test/Project.toml b/test/Project.toml index 29e5959c..bcb6fa3b 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,4 +1,6 @@ [deps] +Glibc_jll = "452aa2e7-e185-58db-8ff9-d3c1fa4bc997" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" diff --git a/test/runtests.jl b/test/runtests.jl index dc52afbb..788fef38 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -10,6 +10,7 @@ using StaticTools using MacroTools using LLD_jll using LLVM +using Glibc_jll, Pkg # (the extension that has the C library of a Linux target) using Bumper addprocs(1) diff --git a/test/testcore.jl b/test/testcore.jl index 3213c9af..c6a94f6e 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -188,21 +188,160 @@ end @testset "A target that Julia's LLVM has no code generator for" begin triple = "x86_64-unknown-linux-gnu" - ir_target = StaticCompiler.StaticTarget(tryparse(Base.BinaryPlatforms.Platform, triple), StaticCompiler.host_generic_machine(), nothing, triple, false) - @test StaticCompiler.target_triple(ir_target) == triple && StaticCompiler.needs_ir(ir_target) + ir_target = StaticCompiler.StaticTarget(tryparse(Base.BinaryPlatforms.Platform, triple), StaticCompiler.host_generic_machine(), nothing, StaticCompiler.Retarget(triple, "", "", nothing), false) + @test StaticCompiler.target_triple(ir_target) == triple && StaticCompiler.needs_ir(ir_target) == !StaticCompiler.julia_llvm_has(triple) @test occursin("--target=$triple", string(StaticCompiler.clang_target_flags(ir_target))) ir = StaticCompiler.generate_ir(fib, Tuple{Int}; target = ir_target) @test occursin("target triple = \"$triple\"", ir) && !occursin("target datalayout", ir) && !occursin("target-cpu", ir) && !occursin("target-features", ir) - @test_throws ArgumentError StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibobj"; target = ir_target) + # (an object is made if Julia's LLVM has the code generator, and is refused if it has not and no `llc` is given) + if StaticCompiler.julia_llvm_has(triple) + _, obj = StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibobj"; target = ir_target) + @test read(obj, 4) == UInt8[0x7f, 0x45, 0x4c, 0x46] # ELF + else + @test_throws ArgumentError StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibobj"; target = ir_target) + end path, ir_path = StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibir"; target = ir_target, emit_llvm_only = true) @test occursin("target triple = \"$triple\"", read(ir_path, String)) # the host's own target is untouched host = StaticCompiler.StaticTarget() - @test isnothing(host.ir_triple) && StaticCompiler.needs_ir(host) == Sys.iswindows() + @test isnothing(host.retarget) && StaticCompiler.needs_ir(host) == Sys.iswindows() @test isempty(StaticCompiler.clang_target_flags(host)) # where there is no code generator for the triple, the constructors make such a target for t in ("x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "riscv64-unknown-linux-gnu") has_backend = try; LLVM.Target(triple = t); true; catch; false; end - has_backend || @test StaticCompiler.StaticTarget(t, "generic", "").ir_triple == LLVM.normalize(t) + has_backend || @test StaticCompiler.StaticTarget(t, "generic", "").retarget.triple == LLVM.normalize(t) + end +end + +@testset "Toolchains" begin + using Base.BinaryPlatforms + host = StaticCompiler.StaticTarget() + # the default is the host's C compiler driver, and `set_compiler!` is a driver + @test StaticCompiler.toolchain(host) isa StaticCompiler.Driver + t = StaticCompiler.StaticTarget(host.platform, host.tm, "mycc", false) + @test t.toolchain == StaticCompiler.Driver(`mycc`) && StaticCompiler.toolchain(t).cc == `mycc` + StaticCompiler.set_compiler!(t, "othercc"); @test t.toolchain.cc == `othercc` + StaticCompiler.set_toolchain!(t, StaticCompiler.LLD(:wasm)); @test t.toolchain isa StaticCompiler.LLD + @test_throws ArgumentError StaticCompiler.LLD(:macho) + # (the names of what is made) + drv = StaticCompiler.Driver(`cc`) + win = StaticCompiler.StaticTarget(Platform("x86_64", "windows"), host.tm, nothing, false) + @test StaticCompiler.output_name(drv, host, "prog") == (Sys.iswindows() ? "prog.exe" : "prog") + @test StaticCompiler.output_name(drv, win, "prog") == "prog.exe" && StaticCompiler.output_name(drv, win, "lib"; kind = :shared) == "lib.dll" + @test StaticCompiler.output_name(StaticCompiler.LLD(:wasm), host, "prog") == "prog.wasm" + @test StaticCompiler.output_name(StaticCompiler.LLD(:gnu), host, "prog") == "prog" && StaticCompiler.output_name(StaticCompiler.LLD(:gnu), win, "lib"; kind = :shared) == "lib.so" # (ELF, on any host) + # a target made with a toolchain + @test StaticCompiler.StaticTarget("wasm32", "", ""; toolchain = StaticCompiler.LLD(:wasm)).toolchain isa StaticCompiler.LLD + + # `main` made in IR: the executable runs without a C wrapper + dir = mktempdir() + hello_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int + # (made and linked as the library does for the host: as IR where the host needs it, as on Windows) + _, obj = StaticCompiler.generate_obj(((hello_main, Tuple{Int,Ptr{Ptr{UInt8}}}),), dir, "hm"; main_wrapper = "hello_main", emit_llvm_only = StaticCompiler.needs_ir(host)) + ir = StaticCompiler.generate_ir(((hello_main, Tuple{Int,Ptr{Ptr{UInt8}}}),); main_wrapper = "hello_main") + @test occursin(r"define i32 @main\(i32", ir) + driver = StaticCompiler.toolchain(host) + exe = joinpath(dir, StaticCompiler.output_name(driver, host, "hm_exe")) + StaticCompiler.link(driver, host, [obj], exe; entry = nothing) # (`main` is in the object) + @test run(ignorestatus(`$exe`)).exitcode == 42 # (argc is 1) + @test_throws ArgumentError StaticCompiler.generate_ir(((hello_main, Tuple{Int,Ptr{Ptr{UInt8}}}),); main_wrapper = "nothing_here") + + # linking by `lld`: a WebAssembly module (nothing outside of Julia is needed), and the refusals that say what is missing + wasm_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int + wasm = StaticCompiler.StaticTarget("wasm64-unknown-unknown"; toolchain = StaticCompiler.LLD(:wasm)) + wasm_path = compile_executable(wasm_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "wasm_main"; target = wasm) + @test endswith(wasm_path, "wasm_main.wasm") && read(wasm_path, 4) == UInt8[0x00, 0x61, 0x73, 0x6d] + linux = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu)) + @test_throws ArgumentError compile_executable(wasm_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "no_sysroot"; target = linux) # (the C library is the target's: `sysroot`) + @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLD(:gnu; sysroot = mktempdir()), linux, [obj], joinpath(dir, "x"); entry = "main") # (a sysroot that has no `crt1.o`) + @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLD(:wasm), wasm, [joinpath(dir, "a.ll")], joinpath(dir, "x")) # (`lld` links objects, not IR) +end + +@testset "Builtins and the start of main" begin + # what a program needs that a C runtime library has: found by what the IR says, for the target + i128ir = "define i128 @f(i128 %a, i128 %b) {\n %r = udiv i128 %a, %b\n ret i128 %r\n}\n" + halfir = "define float @g(half %a) {\n %r = fpext half %a to float\n ret float %r\n}\n" + @test StaticCompiler.needed_builtins(i128ir, "aarch64-unknown-linux-gnu") == [:LINUX_INT128_BUILTINS] + @test isempty(StaticCompiler.needed_builtins(i128ir, "aarch64-apple-macosx11.0.0")) # (macOS has them in its C library) + @test StaticCompiler.needed_builtins(halfir, "x86_64-unknown-linux-gnu") == [:X86_HALF_BUILTINS] + @test StaticCompiler.needed_builtins(halfir, "wasm64-unknown-unknown") == [:WASM_HALF_BUILTINS] + @test isempty(StaticCompiler.needed_builtins(halfir, "aarch64-unknown-linux-gnu")) # (the CPU converts) + @test occursin("@__udivti3", StaticCompiler.add_builtins(i128ir, "aarch64-unknown-linux-gnu")) && StaticCompiler.add_builtins(halfir, "aarch64-unknown-linux-gnu") == halfir + # a toolchain asks for them: `lld` links nothing else, a compiler driver brings its runtime library + @test StaticCompiler.uses_builtins(StaticCompiler.LLD(:gnu)) && !StaticCompiler.uses_builtins(StaticCompiler.LLD(:gnu; builtins = false)) + @test !StaticCompiler.uses_builtins(StaticCompiler.Driver(`cc`)) && StaticCompiler.uses_builtins(StaticCompiler.Driver(`cc`; builtins = true)) + + # the IR of a program for Linux made with them (and with `retarget`: the code is the host's, the target is Linux) + div128(a::Int, b::Int) = (UInt128(a) ÷ UInt128(b)) % Int + tc = StaticCompiler.LLD(:gnu; sysroot = "/nonexistent") + target = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = tc, retarget = true) + @test target.retarget.triple == "aarch64-unknown-linux-gnu" && LLVM.cpu(target.tm) in ("", "generic") + ir = StaticCompiler.generate_ir(div128, Tuple{Int,Int}; target) + @test occursin("target triple = \"aarch64-unknown-linux-gnu\"", ir) && occursin(r"define [^\n]*@__udivti3", ir) && !occursin("target datalayout", ir) + ir = StaticCompiler.generate_ir(div128, Tuple{Int,Int}; target = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu; builtins = false), retarget = true)) + @test !occursin("@__udivti3", ir) + + # what the start of `main` does first (the program of a collector that looks at the stack from its top: `main` stores it) + hello_start(argc::Int, argv::Ptr{Ptr{UInt8}}) = argc % Int + prologue(builder, mod) = (LLVM.call!(builder, LLVM.FunctionType(LLVM.VoidType(), LLVM.LLVMType[]), LLVM.Function(mod, "static_start_mark", LLVM.FunctionType(LLVM.VoidType(), LLVM.LLVMType[]))); nothing) + ir = StaticCompiler.generate_ir(((hello_start, Tuple{Int,Ptr{Ptr{UInt8}}}),); main_wrapper = "hello_start", main_prologue = prologue) + @test occursin(r"define i32 @main\(i32[^\n]*\{\nentry:\n call void @static_start_mark\(\)", ir) # (first in `main`) +end + +@testset "Objects by retargeting" begin + # the object of the code generator of Julia's LLVM, for a triple that is not the host's (the host's architecture, for another system), made from IR that has the host's code + host_triple = "$(first(split(LLVM.default_triple(), '-')))-unknown-linux-gnu" + target = StaticCompiler.StaticTarget(host_triple; retarget = true) + @test StaticCompiler.can_emit_object(target) + obj = StaticCompiler.emit_object(StaticCompiler.generate_ir(fib, Tuple{Int}; target), target) + @test obj[1:4] == UInt8[0x7f, 0x45, 0x4c, 0x46] # ELF (and not the host's format) + + # where Julia's LLVM has no code generator, the `llc` of the target makes the object: here a script that stands for it (the arguments that it is given, and a file that it writes) + if !Sys.iswindows() + dir = mktempdir() + fake = joinpath(dir, "fakellc") + write(fake, "#!/bin/sh\necho \"\$@\" > $(joinpath(dir, "args"))\nout=\nwhile [ \$# -gt 0 ]; do if [ \"\$1\" = -o ]; then out=\$2; fi; shift; done\nprintf OBJECT > \"\$out\"\n") + chmod(fake, 0o755) + noisa = StaticCompiler.StaticTarget("fakearch64-unknown-linux-gnu", "mycpu", "+feat"; llc = fake) + @test !StaticCompiler.julia_llvm_has("fakearch64-unknown-linux-gnu") && StaticCompiler.can_emit_object(noisa) && !StaticCompiler.needs_ir(noisa) + @test String(StaticCompiler.emit_object("define void @f() {\n ret void\n}\n", noisa)) == "OBJECT" + args = read(joinpath(dir, "args"), String) + @test occursin("-mtriple=fakearch64-unknown-linux-gnu", args) && occursin("-mcpu=mycpu", args) && occursin("-mattr=+feat", args) && occursin("-filetype=obj", args) + # (no `llc`: only the IR can be made) + without = StaticCompiler.StaticTarget("fakearch64-unknown-linux-gnu") + @test !StaticCompiler.can_emit_object(without) && StaticCompiler.needs_ir(without) + @test_throws ArgumentError StaticCompiler.emit_object("define void @f() {\n ret void\n}\n", without) end end + +@testset "Linking for Linux from any host" begin + # the C library of the target (fetched for any host), and a program for it linked by `lld` alone + # (for the architecture of the host: the code generator for it is in Julia's LLVM wherever it is) + host_arch = String(first(split(LLVM.default_triple(), '-'))) + triple = "$host_arch-unknown-linux-gnu" + sysroot = StaticCompiler.glibc_sysroot(triple) + @test isfile(joinpath(sysroot, "lib", "crt1.o")) || isfile(joinpath(StaticCompiler.crt_directory(sysroot), "crt1.o")) + @test StaticCompiler.glibc_sysroot(Base.BinaryPlatforms.Platform(host_arch, "linux"; libc = "glibc")) == sysroot + lin_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int + target = StaticCompiler.StaticTarget(triple; toolchain = StaticCompiler.LLD(:gnu; sysroot)) + exe = compile_executable(lin_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "lin_main"; target) + @test read(exe, 4) == UInt8[0x7f, 0x45, 0x4c, 0x46] # ELF + lib = compile_shlib(lin_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "lin_lib"; target) + @test endswith(lib, ".so") && read(lib, 4) == UInt8[0x7f, 0x45, 0x4c, 0x46] + # (and the builtins that an `lld` link has not: a division of `i128` links) + div128(a::Int, b::Int) = (UInt128(a) ÷ UInt128(b)) % Int + @test isfile(compile_shlib(div128, (Int, Int), mktempdir(), "div128"; target)) + # (an object that cannot be made is refused as such, before the linker is asked for it: here there is no code generator for the architecture, and no `llc`) + nobackend = StaticCompiler.StaticTarget("fakearch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu; sysroot)) + @test_throws ArgumentError compile_executable(lin_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "nobackend"; target = nobackend) +end + +@testset "The finished module" begin + # `llvm_finalize` is called on the module as it is made into an object, or written: after the retargeting and the `main` + fin_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 1) % Int + seen = String[] + finalize(mod) = (push!(seen, string(LLVM.triple(mod), " ", haskey(LLVM.functions(mod), "main"))); LLVM.name!(first(f for f in LLVM.functions(mod) if LLVM.name(f) == "main"), "main2"); nothing) + ir = StaticCompiler.generate_ir(((fin_main, Tuple{Int,Ptr{Ptr{UInt8}}}),); main_wrapper = "fin_main", llvm_finalize = finalize, + target = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; retarget = true)) + @test seen == ["aarch64-unknown-linux-gnu true"] && occursin("@main2", ir) +end From e4a9262cf0502118675f9f9bc24cef2e1c58ac33 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Fri, 27 Nov 2026 10:40:20 -0500 Subject: [PATCH 189/202] Remove unused dependencies and imports --- Project.toml | 7 +------ src/StaticCompiler.jl | 7 ++----- test/Project.toml | 4 ---- test/runtests.jl | 1 - 4 files changed, 3 insertions(+), 16 deletions(-) diff --git a/Project.toml b/Project.toml index 8bffbee2..c004f772 100644 --- a/Project.toml +++ b/Project.toml @@ -7,12 +7,9 @@ version = "0.7.6" [deps] Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" -InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" LLD_jll = "d55e3150-da41-5e91-b323-ecfd1eec6109" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" -MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" -Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [weakdeps] @@ -26,13 +23,11 @@ StaticCompilerGlibcExt = ["Glibc_jll", "Pkg"] Clang_jll = "16, 18, 20, 23" GPUCompiler = "2" LLVM = "10" -MacroTools = "0.5" StaticTools = "0.8, 0.9" julia = "1.11" [extras] -Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test", "Formatting"] +test = ["Test"] diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index f96e506f..6aece435 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -1,10 +1,7 @@ module StaticCompiler -using InteractiveUtils using GPUCompiler: GPUCompiler using LLVM, LLVM.IR, LLVM.Passes -using Libdl: Libdl, dlsym, dlopen -using Base: RefValue -using Serialization: serialize, deserialize +using Libdl: Libdl using Clang_jll: Clang_jll using LLD_jll: lld using StaticTools @@ -12,7 +9,7 @@ using StaticTools: @symbolcall, @c_str, println using Core: MethodTable using Base: BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform using Base: BinaryPlatforms.platform_dlext, BinaryPlatforms.arch, BinaryPlatforms.os, BinaryPlatforms.os_str, BinaryPlatforms.libc_str -export load_function, compile_shlib, compile_executable +export compile_shlib, compile_executable export static_code_llvm, static_code_typed, static_llvm_module, static_code_native export @device_override, @print_and_throw export StaticTarget, Driver, LLD diff --git a/test/Project.toml b/test/Project.toml index bcb6fa3b..e49b43e8 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -2,18 +2,14 @@ Glibc_jll = "452aa2e7-e185-58db-8ff9-d3c1fa4bc997" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" -Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0" -GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" LLD_jll = "d55e3150-da41-5e91-b323-ecfd1eec6109" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" -MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" -TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e" diff --git a/test/runtests.jl b/test/runtests.jl index 788fef38..bfeb5b9b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,7 +7,6 @@ using ManualMemory using Distributed using StaticTools # using StrideArraysCore -using MacroTools using LLD_jll using LLVM using Glibc_jll, Pkg # (the extension that has the C library of a Linux target) From ebc4c11272347d01c5a32fcee71922d96fbdb82e Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Fri, 27 Nov 2026 12:40:59 -0500 Subject: [PATCH 190/202] Attempt to fix Linux builds from Windows --- src/StaticCompiler.jl | 8 +++++--- src/builtins.jl | 3 ++- test/testcore.jl | 13 +++++++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 6aece435..beb767fd 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -665,6 +665,7 @@ function link(tc::Driver, target::StaticTarget, inputs::Vector{String}, output:: end function link(tc::LLD, target::StaticTarget, inputs::Vector{String}, output::String; kind::Symbol = :executable, entry = nothing, cflags = ``) # Link using an LLD driver + threads = lld_threads() any(endswith(".ll"), inputs) && throw(ArgumentError("`LLD` links objects, not LLVM IR: make the objects (the target has to be one that Julia's LLVM has the code generator for)")) if tc.flavor === :gnu sysroot = tc.sysroot @@ -672,17 +673,18 @@ function link(tc::LLD, target::StaticTarget, inputs::Vector{String}, output::Str dir = crt_directory(sysroot) libs = ["-l$l" for l in tc.libs] if kind === :shared - run(`$(lld()) -flavor gnu -shared -o $output $inputs -L$dir $libs $(tc.flags) $cflags`) + run(`$(lld()) -flavor gnu $threads -shared -o $output $inputs -L$dir $libs $(tc.flags) $cflags`) else loader = something(tc.dynamic_linker, default_dynamic_linker(target)) - run(`$(lld()) -flavor gnu -o $output -dynamic-linker $loader $(joinpath(dir, "crt1.o")) $(joinpath(dir, "crti.o")) $inputs -L$dir $libs $(joinpath(dir, "crtn.o")) $(tc.flags) $cflags`) + run(`$(lld()) -flavor gnu $threads -o $output -dynamic-linker $loader $(joinpath(dir, "crt1.o")) $(joinpath(dir, "crti.o")) $inputs -L$dir $libs $(joinpath(dir, "crtn.o")) $(tc.flags) $cflags`) end else # :wasm kind === :shared && throw(ArgumentError("a shared library for WebAssembly is not supported")) bits = startswith(target_triple(target), "wasm64") ? "-mwasm64" : "-mwasm32" - run(`$(lld()) -flavor wasm $bits --no-entry --export=main --export-memory --allow-undefined -o $output $inputs $(tc.flags) $cflags`) + run(`$(lld()) -flavor wasm $threads $bits --no-entry --export=main --export-memory --allow-undefined -o $output $inputs $(tc.flags) $cflags`) end return output end +lld_threads() = Sys.iswindows() ? `--threads=1` : `` # The available `lld` seems to crash when run multithreaded on Windows, so limit it to one thread end # module diff --git a/src/builtins.jl b/src/builtins.jl index 712e0623..6c083310 100644 --- a/src/builtins.jl +++ b/src/builtins.jl @@ -252,10 +252,11 @@ that is linked by `lld` alone has not. Which are needed is what the module says: function add_builtins!(mod::LLVM.Module, triple::AbstractString) for name in needed_builtins(string(mod), triple) builtins = parse(LLVM.Module, getfield(@__MODULE__, name)) - # (the linker of modules takes a `linkonce_odr` definition only where something in the module calls it, and nothing does: the code generator will. `weak_odr` is as well replaced by a definition of the program's own) for f in LLVM.functions(builtins) LLVM.isdeclaration(f) || LLVM.linkage!(f, LLVM.API.LLVMWeakODRLinkage) end + LLVM.triple!(builtins, LLVM.triple(mod)) + LLVM.datalayout!(builtins, LLVM.datalayout(mod)) LLVM.link!(mod, builtins) end return mod diff --git a/test/testcore.jl b/test/testcore.jl index c6a94f6e..2d5bdec4 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -278,6 +278,11 @@ end @test target.retarget.triple == "aarch64-unknown-linux-gnu" && LLVM.cpu(target.tm) in ("", "generic") ir = StaticCompiler.generate_ir(div128, Tuple{Int,Int}; target) @test occursin("target triple = \"aarch64-unknown-linux-gnu\"", ir) && occursin(r"define [^\n]*@__udivti3", ir) && !occursin("target datalayout", ir) + # (linked into the module of a target that is not retargeted, which has a data layout: with no warning of different layouts) + arch = String(first(split(LLVM.default_triple(), '-'))) + plain = StaticCompiler.StaticTarget("$arch-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu; sysroot = "/nonexistent")) + plain_ir = @test_logs min_level = Base.CoreLogging.Warn StaticCompiler.generate_ir(div128, Tuple{Int,Int}; target = plain) + @test occursin(r"define [^\n]*@__udivti3", plain_ir) && occursin("target datalayout", plain_ir) ir = StaticCompiler.generate_ir(div128, Tuple{Int,Int}; target = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu; builtins = false), retarget = true)) @test !occursin("@__udivti3", ir) @@ -324,16 +329,16 @@ end @test StaticCompiler.glibc_sysroot(Base.BinaryPlatforms.Platform(host_arch, "linux"; libc = "glibc")) == sysroot lin_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int target = StaticCompiler.StaticTarget(triple; toolchain = StaticCompiler.LLD(:gnu; sysroot)) + # An object that cannot be made is refused, before the linker is asked for it + nobackend = StaticCompiler.StaticTarget("fakearch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu; sysroot)) + @test_throws ArgumentError compile_executable(lin_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "nobackend"; target = nobackend) + div128(a::Int, b::Int) = (UInt128(a) ÷ UInt128(b)) % Int exe = compile_executable(lin_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "lin_main"; target) @test read(exe, 4) == UInt8[0x7f, 0x45, 0x4c, 0x46] # ELF lib = compile_shlib(lin_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "lin_lib"; target) @test endswith(lib, ".so") && read(lib, 4) == UInt8[0x7f, 0x45, 0x4c, 0x46] # (and the builtins that an `lld` link has not: a division of `i128` links) - div128(a::Int, b::Int) = (UInt128(a) ÷ UInt128(b)) % Int @test isfile(compile_shlib(div128, (Int, Int), mktempdir(), "div128"; target)) - # (an object that cannot be made is refused as such, before the linker is asked for it: here there is no code generator for the architecture, and no `llc`) - nobackend = StaticCompiler.StaticTarget("fakearch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu; sysroot)) - @test_throws ArgumentError compile_executable(lin_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "nobackend"; target = nobackend) end @testset "The finished module" begin From 75dc93a86dab53b610fc01928714252ed2a6cca5 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Fri, 27 Nov 2026 22:38:44 -0500 Subject: [PATCH 191/202] Add Mac and Windows cross-targets, substantial cleanup --- .github/workflows/cross-matrix.yml | 82 ++++++++++++ Project.toml | 4 +- README.md | 23 +++- docs/make.jl | 2 +- ext/StaticCompilerGlibcExt.jl | 4 +- ext/StaticCompilerMingwExt.jl | 28 ++++ src/StaticCompiler.jl | 130 ++++++++++-------- src/builtins.jl | 57 +++++++- src/llvm.jl | 38 +++--- src/quirks.jl | 8 +- src/target.jl | 206 +++++++++++++++++------------ src/toolchain.jl | 160 +++++++++++++++++----- test/Project.toml | 7 +- test/cross/matrix.jl | 81 ++++++++++++ test/cross/run.jl | 35 +++++ test/runtests.jl | 2 +- test/testcore.jl | 164 +++++++++++++++++++---- 17 files changed, 790 insertions(+), 241 deletions(-) create mode 100644 .github/workflows/cross-matrix.yml create mode 100644 ext/StaticCompilerMingwExt.jl create mode 100644 test/cross/matrix.jl create mode 100644 test/cross/run.jl diff --git a/.github/workflows/cross-matrix.yml b/.github/workflows/cross-matrix.yml new file mode 100644 index 00000000..31b3453d --- /dev/null +++ b/.github/workflows/cross-matrix.yml @@ -0,0 +1,82 @@ +name: Cross-compiling matrix +# What can be made from each kind of host, for each target (test/cross/matrix.jl, a table in the summary of each job), and whether what was made runs on a machine of its target (test/cross/run.jl). +# The hosts: Linux (x86-64 and AArch64), macOS (Apple Silicon) and Windows. The targets: Linux, macOS, Windows (MinGW-w64) and WebAssembly. A host makes what Julia's LLVM has the code generator for (its own architecture, +# and WebAssembly), and what it has the libraries for: those of Linux and Windows are fetched (Glibc_jll, Mingw_jll); the SDK of macOS is Xcode's, or one that is downloaded (phracker/MacOSX-SDKs) on a host that is not a Mac (Linux and Windows). +on: + push: + branches: + - cross-target + paths: + - 'src/**' + - 'ext/**' + - 'test/cross/**' + - '.github/workflows/cross-matrix.yml' + workflow_dispatch: +jobs: + build: + name: Make on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + - ubuntu-24.04-arm + - macos-latest + - windows-latest + steps: + - uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v2 + with: + version: '1.12' + - uses: julia-actions/cache@v2 + - name: An SDK of macOS, for the programs for macOS that are made on Linux and on Windows + if: runner.os != 'macOS' + shell: bash + run: | + curl -fsSL -o sdk.tar.xz https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX11.3.sdk.tar.xz + if [ "$RUNNER_OS" = Windows ]; then + # (a program of C needs `usr/lib`, the stubs of `libSystem`: `System` has the frameworks, with symbolic links that Windows does not make; and the path has to be Windows's) + # (the links of `usr/lib` are needed, `libSystem.tbd` is one: MSYS makes real ones, which the runner's administrator may, with `nativestrict`, and not the copies that it does by default, which fail when the target is not yet extracted) + MSYS=winsymlinks:nativestrict tar xJf sdk.tar.xz --exclude='MacOSX11.3.sdk/System' + test -e MacOSX11.3.sdk/usr/lib/libSystem.tbd + echo "SDKROOT=$(pwd -W)/MacOSX11.3.sdk" >> "$GITHUB_ENV" + else + tar xJf sdk.tar.xz + echo "SDKROOT=$PWD/MacOSX11.3.sdk" >> "$GITHUB_ENV" + fi + rm sdk.tar.xz + - name: What is made + run: julia test/cross/matrix.jl made + - uses: actions/upload-artifact@v4 + if: always() + with: + name: made-${{ matrix.os }} + path: made + run: + name: Run on ${{ matrix.os }} + needs: build + # (what a host made is run wherever it was made: a build that failed on one host does not stop what the others made from being run) + if: ${{ !cancelled() }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + - ubuntu-24.04-arm + - macos-latest + - macos-15-intel # (the last runner with an Intel CPU: `macos-13` is retired) + - windows-latest + steps: + - uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v2 + with: + version: '1.12' + - uses: actions/download-artifact@v4 + with: + pattern: made-* + path: made + merge-multiple: true + - name: Run what was made for this machine + run: julia test/cross/run.jl made diff --git a/Project.toml b/Project.toml index c004f772..c9c8ffcf 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaticCompiler" uuid = "81625895-6c0f-48fc-b932-11a18313743c" authors = ["Tom Short and contributors"] -version = "0.7.6" +version = "0.8.0" [deps] @@ -14,10 +14,12 @@ StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" [weakdeps] Glibc_jll = "452aa2e7-e185-58db-8ff9-d3c1fa4bc997" +Mingw_jll = "2d94bacf-4d71-535d-a47c-45e808015115" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" [extensions] StaticCompilerGlibcExt = ["Glibc_jll", "Pkg"] +StaticCompilerMingwExt = ["Mingw_jll", "Pkg"] [compat] Clang_jll = "16, 18, 20, 23" diff --git a/README.md b/README.md index 78894700..51d37e2a 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ using Pkg Pkg.add("StaticCompiler") ``` +## Approach + +This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. + ### Standalone compilation StaticCompiler.jl provides the functions `compile_executable` and `compile_shlib` for compiling a Julia function to a native executable or shared library for use from outside of Julia: ```julia @@ -68,17 +72,30 @@ we define overrides such as If for some reason, you wish to use a different method table (defined with `Base.Experimental.@MethodTable` and `Base.Experimental.@overlay`) than the default one provided by StaticCompiler.jl, you can provide it to `compile_executable` and `compile_shlib` via a keyword argument `method_table`. +`StaticCompiler.empty_table` is available as a method table with no overrides at all, if you do not wish to use StaticCompiler's `@device_override`s.Your own overrides can be added to a table of your own, or to this one, with `Base.Experimental.@overlay(StaticCompiler.empty_table, ...)`. -## Approach +### Cross-compilation -This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. +`compile_executable` and `compile_shlib` accept a `target`, so a program can be made on one system for another: + +```julia +julia> using StaticCompiler, Glibc_jll, Pkg # (Glibc_jll and Pkg fetch the C library of a Linux target; Mingw_jll does the same for Windows) + +julia> target = StaticTarget("aarch64-unknown-linux-gnu"; retarget = true, toolchain = LLDToolchain(:gnu; sysroot = StaticCompiler.glibc_sysroot("aarch64-unknown-linux-gnu"))); + +julia> compile_executable(hello, (), "./", "hello"; filename = "hello_aarch64", target) +``` + +The toolchain can be either a [`CCToolchain`](@ref) specifying a C compiler, or an [`LLDToolchain`](@ref) object specifying an LLVM `lld` to be used (flavors include `:gnu` for Linux, `:darwin` for macOS, `:mingw` for Windows, and `:wasm` for WebAssembly). Julia's LLVM contains the code generators for its own architecture and for WebAssembly only. For any other architecture, the IR can be made (`emit_llvm_only = true`), and `StaticTarget(...; llc = "/path/to/llc")` makes the object with an `llc` that has the desired backend for the same LLVM version. + +The [`cross-matrix.yml`](.github/workflows/cross-matrix.yml) CI workfoow tests cross-compilation to and from Linux (x86-64 and AArch64), macOS, and Windows. ## Limitations * 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](src/quirks.jl)). * Type instability. Type unstable code cannot currently be statically compiled via this package. -* Extra experimental on Windows (PRs welcome if you encounter issues). Should work in WSL on Windows 10+. +* Extra experimental on Windows (PRs welcome if you encounter issues). Should work in WSL on Windows 10+. ## Guide for Package Authors diff --git a/docs/make.jl b/docs/make.jl index a7bca12f..34ebbac2 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -7,7 +7,7 @@ makedocs(; repo="https://github.com/tshort/StaticCompiler.jl/blob/{commit}{path}#L{line}", sitename="StaticCompiler.jl", format=Documenter.HTML(; - prettyurls = prettyurls = get(ENV, "CI", nothing) == "true", + prettyurls = get(ENV, "CI", nothing) == "true", # canonical="https://tshort.github.io/StaticCompiler.jl", # assets=String[], ), diff --git a/ext/StaticCompilerGlibcExt.jl b/ext/StaticCompilerGlibcExt.jl index 205a0b94..bfbdf6bd 100644 --- a/ext/StaticCompilerGlibcExt.jl +++ b/ext/StaticCompilerGlibcExt.jl @@ -1,4 +1,4 @@ -# The C library of a Linux target, for linking by `LLD(:gnu; sysroot)`: the artifact of `Glibc_jll` for the platform of the target, fetched for any host. +# The C library of a Linux target, for linking by `LLDToolchain(:gnu; sysroot)`: the artifact of `Glibc_jll` for the platform of the target, fetched for any host. module StaticCompilerGlibcExt using StaticCompiler: StaticCompiler, StaticTarget @@ -21,6 +21,6 @@ function StaticCompiler.glibc_sysroot(triple::AbstractString) return StaticCompiler.glibc_sysroot(platform) end -StaticCompiler.glibc_sysroot(target::StaticTarget) = target.platform === nothing ? StaticCompiler.glibc_sysroot(StaticCompiler.target_triple(target)) : StaticCompiler.glibc_sysroot(target.platform) +StaticCompiler.glibc_sysroot(target::StaticTarget) = StaticCompiler.glibc_sysroot(StaticCompiler.target_triple(target)) end diff --git a/ext/StaticCompilerMingwExt.jl b/ext/StaticCompilerMingwExt.jl new file mode 100644 index 00000000..fa2ff785 --- /dev/null +++ b/ext/StaticCompilerMingwExt.jl @@ -0,0 +1,28 @@ +# The libraries of MinGW-w64 for a Windows target, for linking by `LLDToolchain(:mingw; sysroot)`: the artifact of `Mingw_jll` for the architecture of the target, fetched for any host (it is the start files and the +# libraries of the C runtime and of Windows, which are data: the artifact is listed for Windows platforms, and is the same wherever it is fetched). +module StaticCompilerMingwExt + +using StaticCompiler: StaticCompiler, StaticTarget +using Mingw_jll: Mingw_jll +using Pkg: Pkg +using Base.BinaryPlatforms: Platform, arch + +function StaticCompiler.mingw_sysroot(platform::Platform) + platform = Platform(arch(platform), "windows") # (the one that `Mingw_jll` has: of the architecture) + toml = joinpath(dirname(dirname(pathof(Mingw_jll))), "Artifacts.toml") + meta = Pkg.Artifacts.artifact_meta("Mingw", toml; platform) + meta === nothing && throw(ArgumentError("`Mingw_jll` has no artifact for $platform (it has x86_64 and i686)")) + Pkg.Artifacts.ensure_artifact_installed("Mingw", meta, toml; platform) + return Pkg.Artifacts.artifact_path(Base.SHA1(meta["git-tree-sha1"])) +end + +function StaticCompiler.mingw_sysroot(triple::AbstractString) + # (`Platform` parses `x86_64-w64-mingw32`, and not `x86_64-w64-windows-gnu`, the name that LLVM has for it: the architecture is what it is for) + occursin(r"mingw|windows", triple) || throw(ArgumentError("`$triple` is not a triple of a platform of Windows")) + arch = String(first(split(triple, '-'))) # (`Platform` takes a `String`) + return StaticCompiler.mingw_sysroot(Platform(arch == "amd64" ? "x86_64" : arch, "windows")) +end + +StaticCompiler.mingw_sysroot(target::StaticTarget) = StaticCompiler.mingw_sysroot(StaticCompiler.target_triple(target)) + +end diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index beb767fd..fbf35081 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -6,13 +6,12 @@ using Clang_jll: Clang_jll using LLD_jll: lld using StaticTools using StaticTools: @symbolcall, @c_str, println -using Core: MethodTable using Base: BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform -using Base: BinaryPlatforms.platform_dlext, BinaryPlatforms.arch, BinaryPlatforms.os, BinaryPlatforms.os_str, BinaryPlatforms.libc_str +using Base: BinaryPlatforms.arch, BinaryPlatforms.os_str, BinaryPlatforms.libc_str export compile_shlib, compile_executable export static_code_llvm, static_code_typed, static_llvm_module, static_code_native export @device_override, @print_and_throw -export StaticTarget, Driver, LLD +export StaticTarget, CCToolchain, LLDToolchain abstract type Toolchain end # (the field of `StaticTarget`; see toolchain.jl) @@ -141,7 +140,7 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name= isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" generate_executable(funcs, path, name, filename; target, demangle, cc, cflags, llvm_to_clang, kwargs...) - joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : Driver(cc), target, filename)) + joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : CCToolchain(cc), target, filename)) end """ @@ -216,7 +215,7 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); generate_shlib(funcs, path, filename; target, demangle, cc, cflags, llvm_to_clang, kwargs...) - joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : Driver(cc), target, filename; kind = :shared)) + joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : CCToolchain(cc), target, filename; kind = :shared)) end @@ -307,14 +306,14 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi kwargs... ) # How it is linked: what `cc` says, or the toolchain of the target - tc = isnothing(cc) ? toolchain(target) : Driver(cc) + tc = isnothing(cc) ? toolchain(target) : CCToolchain(cc) # Prepare objects or (if llvm_to_clang is set) IR. A linker that is not a compiler driver is given an object that has a `main` fn = demangle ? "$name" : "julia_$name" - main_wrapper = tc isa Driver ? nothing : fn + main_wrapper = tc isa CCToolchain ? nothing : fn _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, main_wrapper, kwargs...) - # Link! + # Compile and/or link executable! exec_path = joinpath(path, output_name(tc, target, filename)) link(tc, target, [obj_or_ir_path], exec_path; kind = :executable, entry = fn, cflags) return path, name @@ -374,32 +373,33 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file kwargs... ) # How it is linked: what `cc` says, or the toolchain of the target - tc = isnothing(cc) ? toolchain(target) : Driver(cc) + tc = isnothing(cc) ? toolchain(target) : CCToolchain(cc) # Prepare objects or (if llvm_to_clang is set) IR lib_path = joinpath(path, output_name(tc, target, filename; kind = :shared)) _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, dllexport=iswindows(target), kwargs...) - # Link! + # Compile and/or link shlib! link(tc, target, [obj_or_ir_path], lib_path; kind = :shared, cflags) return path, filename end +# (`llvm_postprocess` is accepted and ignored by the three `static_code_*` functions: wrappers, like StaticRuntime's, pass the same keywords to all of the functions that they wrap) function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) - job, kwargs = static_job(func, types; target, kwargs...) - GPUCompiler.code_llvm(stdout, job; libraries=false, kwargs...) + job = static_job(func, types; target, kwargs...) + GPUCompiler.code_llvm(stdout, job; libraries=false) end function static_code_typed(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) - job, kwargs = static_job(func, types; target, kwargs...) - GPUCompiler.code_typed(job; kwargs...) + job = static_job(func, types; target, kwargs...) + GPUCompiler.code_typed(job) end function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) - job, kwargs = static_job(f, tt; name=fname, target, kwargs...) - GPUCompiler.code_native(stdout, job; libraries=false, kwargs...) + job = static_job(f, tt; name=fname, target, kwargs...) + GPUCompiler.code_native(stdout, job; libraries=false) end const CONTEXT_DOC = """ @@ -470,8 +470,8 @@ function compile_llvm_module(f, tt, name=fix_name(f); if !demangle name = "julia_"*name end - job, kwargs = static_job(f, tt; name, target, strip, only_entry, validate, libraries, kwargs...) - m, _ = GPUCompiler.compile(:llvm, job; kwargs...) + job = static_job(f, tt; name, target, strip, only_entry, validate, libraries, kwargs...) + m, _ = GPUCompiler.compile(:llvm, job) try isnothing(llvm_postprocess) || llvm_postprocess(m) locate_pointers_and_runtime_calls(m) @@ -501,28 +501,18 @@ end """ ```julia -generate_obj(f, tt, path::String = tempname(), filenamebase::String="obj"; - target::StaticTarget=StaticTarget(), - demangle = true, - kwargs...) +generate_obj(f, tt, path::String = tempname(), filenamebase::String="obj"; kwargs...) -> (path, obj_path) ``` -Low level interface for compiling object code (`.o`) for for function `f` given -a tuple type `tt` characterizing the types of the arguments for which the -function will be compiled. - -`target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. -This is a struct of the type StaticTarget() -The defaults compile to the native target. - -If `demangle` is set to `false`, compiled function names are prepended with "julia_". +Low level interface for compiling object code (`.o`) for the function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. +This is `generate_obj(((f, tt),), ...)`: see below for the keywords. ### Examples ``` julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) fib (generic function with 1 method) -julia> path, name, table = StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test") -("./test", "fib", IdDict{Any, String}()) +julia> path, obj_path = StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test") +("./test", "./test/obj.o") shell> tree \$path ./test @@ -540,25 +530,31 @@ end ```julia generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; target::StaticTarget=StaticTarget(), - demangle = false, + demangle = true, emit_llvm_only = false, dllexport = false, - kwargs...) + main_wrapper = nothing, + main_prologue = nothing, + llvm_postprocess = nothing, + llvm_finalize = nothing, + kwargs...) -> (path, obj_path) ``` -Low level interface for compiling object code (`.o`) for an array of Tuples -(f, tt) where each function `f` and tuple type `tt` determine the set of methods -which will be compiled. +Low level interface for compiling object code (`.o`) for an array of Tuples `(f, tt)` where each function `f` and tuple type `tt` determine the set of methods which will be compiled. +Returns the directory and the path of the file that was written: `filenamebase.o`, or `filenamebase.ll` with `emit_llvm_only`. -`target` can be used to change the output target. This is useful for compiling to WebAssembly and embedded targets. -This is a struct of the type StaticTarget() -The defaults compile to the native target. +`target` is the [`StaticTarget`](@ref) that the code is made for, which can be one that the host cannot run (see its documentation: the code is then made for the host, retargeted, and the object is made by Julia's LLVM +if it has the code generator for the target, and else by the target's `llc`; an error says so if neither can). The default is the native target. + +If `demangle` is set to `false`, compiled function names are prepended with "julia_". + +If `emit_llvm_only` is set, the LLVM IR (as text, `.ll`) is written in place of the object, for a compiler that makes the object (see [`generate_ir`](@ref), which returns the text instead). If `dllexport` is set, the functions are exported from a Windows DLL. -If `main_wrapper` is the name of one of the functions, the object will be given a `main` that calls it, -possibly preceeded by any code added by `main_prologue(builder, mod)`. +If `main_wrapper` is the name of one of the functions, the object will be given a `main` that calls it, +possibly preceded by any code added by `main_prologue(builder, mod)`. The builtins that the toolchain of the target asks for (`__udivti3`, ...) are added to the module (see [`add_builtins!`](@ref)). -`llvm_finalize(mod)`, if given, is called on the whole module when complete. +`llvm_postprocess(mod)`, if given, is called on the module of each function when it is compiled, and `llvm_finalize(mod)` on the whole module when complete. """ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; target::StaticTarget=StaticTarget(), @@ -586,8 +582,8 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file uses_builtins(target.toolchain) && add_builtins!(mod, target_triple(target)) isnothing(llvm_finalize) || llvm_finalize(mod) obj_path = joinpath(path, "$filenamebase.o") - if isnothing(target.retarget) - fakejob, _ = static_job(f, tt; target, kwargs...) + if !is_retargeted(target) + fakejob = static_job(f, tt; target, kwargs...) obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) else obj = emit_object(mod, target) # (by the code generator of the target, which is not the host's) @@ -625,13 +621,13 @@ generate_ir(f, tt; kwargs...) = generate_ir(((f, tt),); kwargs...) """ link(toolchain, target, inputs::Vector{String}, output::String; kind = :executable, entry = nothing, cflags = ``) -> path -Link the `inputs` (object files, or LLVM IR files if the toolchain is a `Driver`) +Link the `inputs` (object files, or LLVM IR files if the toolchain is a `CCToolchain`) into an executable (`kind = :executable`) or shared library (`kind = :shared`), and return its path. If given, `entry` is the name of the function that an executable runs (`int f(int argc, char **argv)`); with `entry = nothing` the inputs define `main` already, and are linked as they are. """ -function link(tc::Driver, target::StaticTarget, inputs::Vector{String}, output::String; kind::Symbol = :executable, entry = nothing, cflags = ``) +function link(tc::CCToolchain, target::StaticTarget, inputs::Vector{String}, output::String; kind::Symbol = :executable, entry = nothing, cflags = ``) # Link using a C compiler driver cc = tc.cc # (IR, which `clang` compiles itself: it is told what the target is, as it replaces the triple of the IR by the host's) @@ -663,21 +659,41 @@ function link(tc::Driver, target::StaticTarget, inputs::Vector{String}, output:: end return output end -function link(tc::LLD, target::StaticTarget, inputs::Vector{String}, output::String; kind::Symbol = :executable, entry = nothing, cflags = ``) - # Link using an LLD driver +function link(tc::LLDToolchain, target::StaticTarget, inputs::Vector{String}, output::String; kind::Symbol = :executable, entry = nothing, cflags = ``) + # Link using an LLDToolchain driver threads = lld_threads() - any(endswith(".ll"), inputs) && throw(ArgumentError("`LLD` links objects, not LLVM IR: make the objects (the target has to be one that Julia's LLVM has the code generator for)")) + any(endswith(".ll"), inputs) && throw(ArgumentError("`LLDToolchain` links objects, not LLVM IR: make the objects (the target has to be one that Julia's LLVM has the code generator for)")) + libs = ["-l$l" for l in tc.libs] if tc.flavor === :gnu sysroot = tc.sysroot - isnothing(sysroot) && throw(ArgumentError("linking for $(target_triple(target)) needs the C library of the target: give `sysroot = ...` to `LLD(:gnu; ...)`")) + isnothing(sysroot) && throw(ArgumentError("linking for $(target_triple(target)) needs the C library of the target: give `sysroot = ...` to `LLDToolchain(:gnu; ...)`")) dir = crt_directory(sysroot) - libs = ["-l$l" for l in tc.libs] if kind === :shared run(`$(lld()) -flavor gnu $threads -shared -o $output $inputs -L$dir $libs $(tc.flags) $cflags`) else loader = something(tc.dynamic_linker, default_dynamic_linker(target)) run(`$(lld()) -flavor gnu $threads -o $output -dynamic-linker $loader $(joinpath(dir, "crt1.o")) $(joinpath(dir, "crti.o")) $inputs -L$dir $libs $(joinpath(dir, "crtn.o")) $(tc.flags) $cflags`) end + elseif tc.flavor === :darwin + # Mach-O: the SDK has the libraries (as `.tbd` stubs) and the headers; the minimum version of the system is the triple's + sdk = something(tc.sysroot, macos_sdk()) + triple = target_triple(target) + arch = first(split(triple, '-')) + arch = arch in ("aarch64", "arm64") ? "arm64" : arch + m = match(r"macosx?(\d+(?:\.\d+)*)", triple) + minimum = m === nothing ? "11.0.0" : m.captures[1] + shared = kind === :shared ? `-dylib` : `` + run(`$(lld()) -flavor darwin $threads $shared -arch $arch -platform_version macos $minimum $(sdk_version(sdk)) -syslibroot $sdk $inputs $libs -o $output $(tc.flags) $cflags`) + elseif tc.flavor === :mingw + # PE for Windows, with the start files and the libraries of MinGW-w64: the linker of MinGW is `lld` with the machine of PE that the architecture has + sysroot = tc.sysroot + isnothing(sysroot) && throw(ArgumentError("linking for $(target_triple(target)) needs the libraries of MinGW-w64: give `sysroot = ...` to `LLDToolchain(:mingw; ...)` (the directory that has `lib`: `mingw_sysroot(triple)`)")) + dir = mingw_directory(sysroot) + arch = first(split(target_triple(target), '-')) + machine = arch in ("x86_64", "amd64") ? "i386pep" : arch in ("i686", "i386") ? "i386pe" : arch in ("aarch64", "arm64") ? "arm64pe" : throw(ArgumentError("no machine of PE is known for $arch")) + start = kind === :shared ? joinpath(dir, "dllcrt2.o") : joinpath(dir, "crt2.o") + shared = kind === :shared ? `--shared` : `` + run(`$(lld()) -flavor gnu -m $machine $threads $shared -o $output $start $inputs -L$dir $libs $(tc.flags) $cflags`) else # :wasm kind === :shared && throw(ArgumentError("a shared library for WebAssembly is not supported")) bits = startswith(target_triple(target), "wasm64") ? "-mwasm64" : "-mwasm32" @@ -685,6 +701,14 @@ function link(tc::LLD, target::StaticTarget, inputs::Vector{String}, output::Str end return output end + +# the directory of a sysroot that has the start files of MinGW-w64 (`crt2.o`) +function mingw_directory(sysroot::AbstractString) + for d in ("lib", "x86_64-w64-mingw32/lib", "i686-w64-mingw32/lib", "aarch64-w64-mingw32/lib", "mingw/lib", ".") + isfile(joinpath(sysroot, d, "crt2.o")) && return joinpath(sysroot, d) + end + throw(ArgumentError("`$sysroot` has no `crt2.o`: the `sysroot` of a `:mingw` `LLDToolchain` is a directory that has the `lib` of MinGW-w64 (the `Mingw_jll` artifact for the platform of the target is one)")) +end lld_threads() = Sys.iswindows() ? `--threads=1` : `` # The available `lld` seems to crash when run multithreaded on Windows, so limit it to one thread end # module diff --git a/src/builtins.jl b/src/builtins.jl index 6c083310..2dbe6011 100644 --- a/src/builtins.jl +++ b/src/builtins.jl @@ -4,7 +4,7 @@ # For example, `__udivti3` for the division of `i128`, `__extendhfsf2` for `Float16`. # They are defined here in IR, `linkonce_odr` and `hidden`: # a definition of the program's own, or of a library that is linked, wins, and the module has only those that it uses. -# A toolchain asks for them with `builtins = true` (`LLD` does by default; a `Driver` does not, as the compiler driver links its runtime library). +# A toolchain asks for them with `builtins = true` (`LLDToolchain` does by default; a `CCToolchain` does not, as the compiler driver links its runtime library). # Conversions between `Float16` and `Float32` that the code generator for x86-64 calls where the CPU is not said to have them: with the instructions that do them (F16C: from 2012 on). const X86_HALF_BUILTINS = """ @@ -220,16 +220,61 @@ define linkonce_odr hidden double @__extendhfdf2(half %a) { } """ -# The builtins that the IR needs for the target: the names of the constants above. `text` is the IR; the system and the architecture are the triple's. -function needed_builtins(text::AbstractString, triple::AbstractString) +# The probe of the stack for x86-64 Windows (`___chkstk_ms` of libgcc and compiler-rt): the runtime of MinGW-w64 calls it itself (`pseudo-reloc.o`, which every program has), and the libraries that `Mingw_jll` +# has do not have it. It touches each page of the stack that a function of a large frame is about to use, as the system's guard page needs. It is assembly, which the module has as its own (`module asm`). +const MINGW_X64_BUILTINS = raw""" +module asm ".text" +module asm ".globl ___chkstk_ms" +module asm ".def ___chkstk_ms; .scl 2; .type 32; .endef" +module asm "___chkstk_ms:" +module asm " pushq %rcx" +module asm " pushq %rax" +module asm " cmpq $0x1000, %rax" +module asm " leaq 24(%rsp), %rcx" +module asm " jb 2f" +module asm "1:" +module asm " subq $0x1000, %rcx" +module asm " orl $0, (%rcx)" +module asm " subq $0x1000, %rax" +module asm " cmpq $0x1000, %rax" +module asm " ja 1b" +module asm "2:" +module asm " subq %rax, %rcx" +module asm " orl $0, (%rcx)" +module asm " popq %rax" +module asm " popq %rcx" +module asm " ret" +""" + +# The builtins that the IR needs for the target: the names of the constants above. What the IR has that needs them (`Float16`, the division of `i128`) is `has_half` and `has_i128div`; the system and the architecture are the triple's. +function needed_builtins(has_half::Bool, has_i128div::Bool, triple::AbstractString) family = llvm_family(first(split(triple, '-'))) - has_half = occursin(r"\bhalf\b", text) needed = Symbol[] family == "x86" && startswith(triple, "x86_64") && has_half && push!(needed, :X86_HALF_BUILTINS) family == "wasm" && has_half && push!(needed, :WASM_HALF_BUILTINS) - occursin("linux", triple) && occursin(r"\b(udiv|sdiv|urem|srem) i128\b", text) && push!(needed, :LINUX_INT128_BUILTINS) + occursin("linux", triple) && has_i128div && push!(needed, :LINUX_INT128_BUILTINS) + # (the runtime of MinGW-w64 needs the probe of the stack whatever the program is) + startswith(triple, "x86_64") && occursin(r"windows|mingw", triple) && push!(needed, :MINGW_X64_BUILTINS) return needed end +# (of the text of IR) +needed_builtins(text::AbstractString, triple::AbstractString) = + needed_builtins(occursin(r"\bhalf\b", text), occursin(r"\b(udiv|sdiv|urem|srem) i128\b", text), triple) +# (of a module: by its instructions, without printing it) +function needed_builtins(mod::LLVM.Module, triple::AbstractString) + has_half = has_i128div = false + for f in LLVM.functions(mod), bb in LLVM.blocks(f), inst in LLVM.instructions(bb) + has_half = has_half || mentions_half(LLVM.value_type(inst)) || any(op -> mentions_half(LLVM.value_type(op)), LLVM.operands(inst)) + if !has_i128div && LLVM.opcode(inst) in (LLVM.API.LLVMUDiv, LLVM.API.LLVMSDiv, LLVM.API.LLVMURem, LLVM.API.LLVMSRem) + ty = LLVM.value_type(inst) + has_i128div = ty isa LLVM.IntegerType && LLVM.width(ty) == 128 + end + has_half && has_i128div && break + end + return needed_builtins(has_half, has_i128div, triple) +end +# (a `half`, or a vector of them) +mentions_half(ty::LLVM.LLVMType) = ty isa LLVM.HalfType || (ty isa LLVM.VectorType && LLVM.eltype(ty) isa LLVM.HalfType) """ add_builtins(text::AbstractString, triple::AbstractString) -> String @@ -250,7 +295,7 @@ Define in the module the builtins that it needs for the target `triple`: the fun that is linked by `lld` alone has not. Which are needed is what the module says: `i128` division on Linux, `Float16` conversions on x86-64 and WebAssembly. """ function add_builtins!(mod::LLVM.Module, triple::AbstractString) - for name in needed_builtins(string(mod), triple) + for name in needed_builtins(mod, triple) builtins = parse(LLVM.Module, getfield(@__MODULE__, name)) for f in LLVM.functions(builtins) LLVM.isdeclaration(f) || LLVM.linkage!(f, LLVM.API.LLVMWeakODRLinkage) diff --git a/src/llvm.jl b/src/llvm.jl index 6e548cf3..2baa3f72 100644 --- a/src/llvm.jl +++ b/src/llvm.jl @@ -55,13 +55,13 @@ data_warnings(inst, frames) = for frame ∈ frames inlined = inlined) end -# The IR of a target that requires retargeting (`target.retarget`) gets the triple of the target, +# The IR of a target that is retargeted (`is_retargeted(target)`) gets the triple of the target, # no data layout (the code generator that makes the object has the target's), # and no attributes that name the host's CPU and features. # IR that has intrinsics of the host's architecture is refused. function retarget!(mod::LLVM.Module, target::StaticTarget) - isnothing(target.retarget) && return mod - triple = target.retarget.triple + is_retargeted(target) || return mod + triple = target_triple(target) family = llvm_family(first(split(triple, '-'))) for f in LLVM.functions(mod) m = match(r"^llvm\.(aarch64|arm|x86|wasm)\.", LLVM.name(f)) @@ -135,15 +135,15 @@ end emit_object(mod::LLVM.Module, target::StaticTarget) -> Vector{UInt8} emit_object(ir::AbstractString, target::StaticTarget) -> Vector{UInt8} -The object file (the bytes) of the IR, which was made for the host and retargeted (see [`Retarget`](@ref) and [`retarget!`](@ref)), for the target: by the code generator of Julia's LLVM if it has one for the -triple, and else by the `llc` of the target. The CPU and the features are those of the `Retarget`, and the code is position independent (WebAssembly: static). +The object file (the bytes) of the IR, which was made for the host and retargeted (see [`StaticTarget`](@ref) and [`retarget!`](@ref)), for the target: by the code generator of Julia's LLVM if it has one for the +triple, and else by the `llc` of the target. The CPU and the features are those of the target's `machine`, and the code is position independent (WebAssembly: static). """ function emit_object(mod::LLVM.Module, target::StaticTarget) - r = target.retarget - isnothing(r) && throw(ArgumentError("only the objects of a target that is made for by retargeting are made by `emit_object` (see `Retarget`)")) - julia_llvm_has(r.triple) || return emit_object_llc(string(mod), r) - tm = LLVM.TargetMachine(LLVM.Target(; triple = r.triple), r.triple; cpu = r.cpu, features = r.features, opt_level = LLVM.API.LLVMCodeGenLevelDefault, - reloc = startswith(r.triple, "wasm") ? LLVM.API.LLVMRelocStatic : LLVM.API.LLVMRelocPIC) + check_retargeted(target) + julia_llvm_has(target_triple(target)) || return emit_object_llc(string(mod), target) + m = target.machine + tm = LLVM.TargetMachine(LLVM.Target(; triple = m.triple), m.triple; cpu = m.cpu, features = m.features, opt_level = LLVM.API.LLVMCodeGenLevelDefault, + reloc = startswith(m.triple, "wasm") ? LLVM.API.LLVMRelocStatic : LLVM.API.LLVMRelocPIC) try return LLVM.emit(tm, mod, LLVM.API.LLVMObjectFile) finally @@ -151,9 +151,8 @@ function emit_object(mod::LLVM.Module, target::StaticTarget) end end function emit_object(ir::AbstractString, target::StaticTarget) - r = target.retarget - isnothing(r) && throw(ArgumentError("only the objects of a target that is made for by retargeting are made by `emit_object` (see `Retarget`)")) - julia_llvm_has(r.triple) || return emit_object_llc(ir, r) + check_retargeted(target) + julia_llvm_has(target_triple(target)) || return emit_object_llc(ir, target) LLVM.@dispose ctx = LLVM.Context() begin mod = parse(LLVM.Module, ir) try @@ -163,17 +162,16 @@ function emit_object(ir::AbstractString, target::StaticTarget) end end end +check_retargeted(target::StaticTarget) = is_retargeted(target) || throw(ArgumentError("only the objects of a target that is made for by retargeting are made by `emit_object` (see `StaticTarget`)")) # Emit the object file via `llc`, if it has a code generator that Julia's LLVM has not -function emit_object_llc(ir::AbstractString, r::Retarget) - isnothing(r.llc) && throw(ArgumentError("no object can be made for $(r.triple): Julia's LLVM has no code generator for it, and no `llc` is given (`StaticTarget(...; llc = ...)`, of the LLVM version that Julia has, with the code generator for it). The IR can be made (`generate_ir`, `emit_llvm_only = true`)")) - dir = mktempdir() - try +function emit_object_llc(ir::AbstractString, target::StaticTarget) + m = target.machine + isnothing(target.llc) && throw(ArgumentError("no object can be made for $(m.triple): Julia's LLVM has no code generator for it, and no `llc` is given (`StaticTarget(...; llc = ...)`, of the LLVM version that Julia has, with the code generator for it). The IR can be made (`generate_ir`, `emit_llvm_only = true`)")) + mktempdir() do dir ll, obj = joinpath(dir, "module.ll"), joinpath(dir, "module.o") write(ll, ir) - run(`$(r.llc) -O2 -filetype=obj -mtriple=$(r.triple) -mcpu=$(isempty(r.cpu) ? "generic" : r.cpu) -mattr=$(r.features) -relocation-model=pic $ll -o $obj`) + run(`$(target.llc) -O2 -filetype=obj -mtriple=$(m.triple) -mcpu=$(isempty(m.cpu) ? "generic" : m.cpu) -mattr=$(m.features) -relocation-model=pic $ll -o $obj`) return read(obj) - finally - rm(dir; recursive = true, force = true) end end diff --git a/src/quirks.jl b/src/quirks.jl index 73997780..73eae93d 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -1,9 +1,5 @@ -@static if isdefined(Base.Experimental, Symbol("@overlay")) - Base.Experimental.@MethodTable(method_table) - Base.Experimental.@MethodTable(empty_table) -else - const method_table = nothing -end +Base.Experimental.@MethodTable(method_table) +Base.Experimental.@MethodTable(empty_table) # (no overrides: `method_table = StaticCompiler.empty_table` compiles with Julia's own methods) """ ```julia diff --git a/src/target.jl b/src/target.jl index 526d4bbe..a91dd817 100644 --- a/src/target.jl +++ b/src/target.jl @@ -1,14 +1,12 @@ """ - Retarget(triple, cpu, features, llc) + Machine(triple, cpu, features) -What a target needs to be made for by retargeting: Julia's code generation makes the code for the host (`tm` of the target is the host's), and its IR gets the `triple` (see [`retarget!`](@ref)); the object is then made -for the `triple`, with `cpu` and `features`, by the code generator of Julia's LLVM if it has one for it, and else by the external `llc` (which has to be of the LLVM version that Julia has), if there is one. +A machine that code is made for: the LLVM `triple`, the `cpu` (`""` for a generic cpu of a given architecture) and the cpu `features` (`"+neon,-sve"`, ...). """ -struct Retarget +struct Machine triple::String cpu::String features::String - llc::Union{Cmd,Nothing} end """ @@ -17,55 +15,88 @@ end StaticTarget(platform_or_triple) # Specific target with generic CPU (a `Base.BinaryPlatforms.Platform` or a triple string) StaticTarget(platform_or_triple, cpu::String) # Specific target with specific CPU StaticTarget(platform_or_triple, cpu::String, features::String) # Specific target with specific CPU and features + StaticTarget(target::StaticTarget; toolchain, llc, julia_runtime, can_throw) # A copy with some of those changed ``` -Struct that defines a target for the compilation +A target for the compilation: what the program is made for (`machine`, the triple, the CPU and the features), how it is linked (`toolchain`), and what the compiled code may do (`julia_runtime`, `can_throw`). +By default `StaticTarget()` is the native target. The keywords of the constructors are + +* `toolchain`: how the target is linked: a [`CCToolchain`](@ref) (a C compiler: a `String` or a `Cmd` is the compiler of one) or an [`LLDToolchain`](@ref); the default is the host's `clang`. +* `julia_runtime = false`: whether the code may call the runtime of Julia. +* `can_throw = true`: whether the code may throw errors (a function that cannot, is compiled without the machinery for it). +* `retarget = false`, `llc = nothing`: see below. + Beware that currently the compilation assumes that the code is on the host so platform specific code like: ```julia Sys.isapple() ... ``` does not behave as expected. -By default `StaticTarget()` is the native target. -If Julia's LLVM has no code generator for the target (x86-64 where LLVM has only AArch64 and WebAssembly, say), the target is made anyway, as a `Retarget` (the field `retarget`): IR can be made for it -(`generate_ir`, `emit_llvm_only = true`, `llvm_to_clang = true`: the IR has the target's triple and no host CPU or features), and an external `clang` or `llc` that has the code generator makes the object. +**Retargeting.** Julia's code generation makes the IR for the `machine` of the target, unless the target is retargeted. If the target is not the host's (`retarget = true`, or `retarget = host`, a `StaticTarget` whose machine is +the one to make the IR for; and always where Julia's LLVM has no code generator for the target: x86-64 on a build of Julia that has only AArch64 and WebAssembly, say), the IR is made for the host and then given the target's triple, without +the host's CPU or features (see [`retarget!`](@ref)): IR can be made for it (`generate_ir`, `emit_llvm_only = true`, `llvm_to_clang = true`), and the object is made by the code generator of Julia's LLVM if it has one for the triple, else by the external `llc` +(a `Cmd` or a path, of the LLVM version that Julia has), or `clang`. -For cross-compilation of executables and shared libraries, one also needs to call `set_compiler!` with the path to a valid C compiler -for the target platform. For example, to cross-compile for aarch64 using a compiler from homebrew, one can use: -```julia - set_compiler!(StaticTarget(parse(Platform,"aarch64-gnu-linux")), "/opt/homebrew/bin/aarch64-unknown-linux-gnu-gcc") -``` +The fields can be set (`set_compiler!`, `set_toolchain!`, `set_runtime!`), and the copy constructor makes a changed copy, which leaves the original as it was: `StaticTarget(target; toolchain = CCToolchain("/opt/homebrew/bin/aarch64-unknown-linux-gnu-gcc"))`. """ mutable struct StaticTarget - platform::Union{Platform,Nothing} - tm::LLVM.TargetMachine - toolchain::Union{Toolchain,Nothing} # How the target is linked (a `Driver` of a target-specific compiler, an `LLD`, ...), else the default of `default_toolchain` - retarget::Union{Retarget,Nothing} # A target that the code is made for as for the host and then retargeted (see `Retarget`), else nothing + machine::Machine # What the program is made for + codegen::Union{Machine,Nothing} # What Julia's code generation makes the IR for if the target is retargeted (see above), and else `nothing`: the IR is made for `machine` + toolchain::Union{Toolchain,Nothing} # How the target is linked, else the default of `default_toolchain` + llc::Union{Cmd,Nothing} # The `llc` that makes the objects of a retargeted target that Julia's LLVM has no code generator for julia_runtime::Bool + can_throw::Bool end -StaticTarget(platform, tm, compiler, julia_runtime) = StaticTarget(platform, tm, as_toolchain(compiler), nothing, julia_runtime) # (`compiler`: the C compiler of a `Driver`, as the field was) StaticTarget() = StaticTarget(HostPlatform(), LLVM.host_cpu_name(), LLVM.host_cpu_features()) -# The target for a platform or a triple, with the generic CPU of its architecture unless `cpu` and `features` say otherwise. -# If Julia's LLVM has no code generator for it, then the target is a `Retarget`, `tm` is the host's, and the target is a `Retarget`: -# Then the object is made by `llc` (the keyword `llc`, a `Cmd` or a path). -# `retarget = true` makes such a target for any triple, with the generic CPU of the host -# (or `retarget = host_target`, a `StaticTarget` of the host, with its machine). -function StaticTarget(target::Union{Platform,AbstractString}, cpu::AbstractString = "", features::AbstractString = ""; toolchain = nothing, retarget::Union{Bool,StaticTarget} = false, llc = nothing) +# The target for a platform or a triple, with the generic CPU of its architecture unless `cpu` and `features` say otherwise. +# `retarget = true` makes the IR for the generic CPU of the host (or `retarget = host_target`, a `StaticTarget` of the host: the machine of that one). +function StaticTarget(target::Union{Platform,AbstractString}, cpu::AbstractString = "", features::AbstractString = ""; + toolchain = nothing, + retarget::Union{Bool,StaticTarget} = false, + llc = nothing, + julia_runtime::Bool = false, + can_throw::Bool = true, + ) init_llvm_targets() - platform = target isa Platform ? target : tryparse(Platform, target) - triple = target isa Platform ? clean_triple(target) : String(target) - retargeted = Retarget(LLVM.normalize(triple), cpu, features, llc === nothing ? nothing : llc isa Cmd ? llc : `$llc`) - retarget === false || return StaticTarget(platform, retarget isa StaticTarget ? retarget.tm : host_generic_machine(), as_toolchain(toolchain), retargeted, false) - tm = try - LLVM.TargetMachine(LLVM.Target(triple = triple), triple; cpu, features) - catch err - # (`ArgumentError`: "No available targets are compatible with triple") - err isa ArgumentError || rethrow() - return StaticTarget(platform, host_generic_machine(), as_toolchain(toolchain), retargeted, false) - end - return StaticTarget(platform, tm, as_toolchain(toolchain), nothing, false) + triple = LLVM.normalize(llvm_triple(target isa Platform ? clean_triple(target) : String(target))) + machine = Machine(triple, cpu, features) + codegen = retarget isa StaticTarget ? retarget.machine : retarget || !julia_llvm_has(triple) ? host_generic_machine() : nothing + return StaticTarget(machine, codegen, as_toolchain(toolchain), as_llc(llc), julia_runtime, can_throw) end +# A copy with some changes +StaticTarget(target::StaticTarget; toolchain = target.toolchain, llc = target.llc, julia_runtime::Bool = target.julia_runtime, can_throw::Bool = target.can_throw, codegen::Union{Machine,Nothing} = target.codegen) = + StaticTarget(target.machine, codegen, as_toolchain(toolchain), as_llc(llc), julia_runtime, can_throw) + +# Ensure llc is either a Cmd or Nothing +as_llc(::Nothing) = nothing +as_llc(llc::Cmd) = llc +as_llc(llc::AbstractString) = `$llc` + +""" +```julia +set_compiler!(target::StaticTarget, compiler) +``` +Set the C compiler that links the target (a `String` or a `Cmd`): its toolchain becomes a [`CCToolchain`](@ref) of it. For example: +```julia +set_compiler!(StaticTarget("aarch64-unknown-linux-gnu"), "/opt/homebrew/bin/aarch64-unknown-linux-gnu-gcc") +``` +""" +set_compiler!(target::StaticTarget, compiler::Union{AbstractString,Cmd}) = (target.toolchain = CCToolchain(compiler); target) + +""" +```julia +set_toolchain!(target::StaticTarget, toolchain::Toolchain) +``` +Set how the target is linked: a [`CCToolchain`](@ref) (a C compiler) or an [`LLDToolchain`](@ref) (`lld` and the C library of a sysroot). +""" +set_toolchain!(target::StaticTarget, tc::Toolchain) = (target.toolchain = tc; target) + +"Set whether the code of the target may call the runtime of Julia." +set_runtime!(target::StaticTarget, julia_runtime::Bool) = (target.julia_runtime = julia_runtime; target) + clean_triple(platform::Platform) = arch(platform) * os_str(platform) * libc_str(platform) +# To emit a COFF object, LLVM seems to want `x86_64-w64-windows-gnu` in place of `x86_64-w64-mingw32` +llvm_triple(triple::AbstractString) = replace(String(triple), r"-mingw(?:32|64)$" => "-windows-gnu") # The code generators of every target that Julia's LLVM has, which are not all there until they are initialized (GPUCompiler does it when it first compiles, which is after a target is made) const LLVM_TARGETS_INITIALIZED = Ref(false) @@ -80,32 +111,18 @@ function init_llvm_targets() return end -# The host's target machine without its CPU and its features, which are not those of another architecture's code (and not to be in its IR) -host_generic_machine() = LLVM.TargetMachine(LLVM.Target(triple = LLVM.default_triple()), LLVM.default_triple()) +# The host's machine without its CPU and its features, which are not those of another architecture's code (and not to be in its IR) +host_generic_machine() = Machine(LLVM.default_triple(), "", "") -# The triple that the target is for: the one of its `Retarget`, else the one of its target machine -target_triple(target::StaticTarget) = isnothing(target.retarget) ? LLVM.triple(target.tm) : target.retarget.triple +# The triple that the target is for +target_triple(target::StaticTarget) = target.machine.triple -""" -```julia -set_compiler!(target::StaticTarget, compiler::String) -``` -Set the compiler for cross compilation. For example: -```julia -set_compiler!(StaticTarget(parse(Platform,"aarch64-gnu-linux")), "/opt/homebrew/bin/aarch64-elf-gcc") -``` -""" -set_compiler!(target::StaticTarget, compiler::Union{AbstractString,Cmd}) = (target.toolchain = Driver(compiler)) +# Whether the IR is made for another machine (the host's) than the target's, and retargeted (see `retarget!`) +is_retargeted(target::StaticTarget) = !isnothing(target.codegen) -""" -```julia -set_toolchain!(target::StaticTarget, toolchain::Toolchain) -``` -Set how the target is linked: a [`Driver`](@ref) (a C compiler) or an [`LLD`](@ref) (`lld` and the C library of a sysroot). -""" -set_toolchain!(target::StaticTarget, tc::Toolchain) = (target.toolchain = tc) +# The machine that Julia's code generation makes the IR for +codegen_machine(target::StaticTarget) = something(target.codegen, target.machine) -set_runtime!(target::StaticTarget, julia_runtime::Bool) = (target.julia_runtime = julia_runtime) # Default to native struct StaticCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget @@ -113,21 +130,23 @@ struct StaticCompilerTarget{MT} <: GPUCompiler.AbstractCompilerTarget cpu::String features::String julia_runtime::Bool + can_throw::Bool method_table::MT end +# Both `opt` and `optlevel` seem to be unused in current GPUCompiler versions, +# so this particular implementation is somewhat vestigial, but could be expanded in the future struct StaticCompilerParams <: GPUCompiler.AbstractCompilerParams opt::Bool optlevel::Int end - function StaticCompilerParams(; opt=false, - optlevel=Base.JLOptions().opt_level -) + optlevel=Base.JLOptions().opt_level + ) return StaticCompilerParams(opt, optlevel) end -module StaticRuntime +module RuntimeStubs # the runtime library signal_exception() = return malloc(sz) = ccall("extern malloc", llvmcall, Csize_t, (Csize_t,), sz) @@ -149,16 +168,19 @@ function GPUCompiler.llvm_machine(target::StaticCompilerTarget) return tm end GPUCompiler.llvm_triple(target::StaticCompilerTarget) = target.triple -GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = StaticRuntime -GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = true +GPUCompiler.runtime_module(::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = RuntimeStubs +GPUCompiler.can_throw(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = job.config.target.can_throw GPUCompiler.uses_julia_runtime(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget}) = job.config.target.julia_runtime GPUCompiler.method_table(@nospecialize(job::GPUCompiler.CompilerJob{<:StaticCompilerTarget})) = job.config.target.method_table -# `params` is the `GPUCompiler.AbstractCompilerParams` of the job. Other packages can pass a type of their own, to -# specialize GPUCompiler's functions (`inference_params`, `optimize!`, ...) for their compilations only, without -# adding methods for `StaticCompilerTarget` and `StaticCompilerParams`, which are ours. All the methods above -# are for any `params`. +# The `GPUCompiler.CompilerJob` of `func` for the argument types `types`, +# for the codegen machine of `target`, with its `julia_runtime` and `can_throw`, and the `method_table`. +# Other keywords (`name`, `strip`, `validate`, ...) are those of `GPUCompiler.CompilerConfig`. +# `params` is the `GPUCompiler.AbstractCompilerParams` of the job. Other packages may pass a type of their own, +# to specialize GPUCompiler's functions (`inference_params`, `optimize!`, ...) for their compilations only, +# without adding methods for `StaticCompilerTarget` and `StaticCompilerParams`, which are ours. +# All the methods above are for any `params`. function static_job(@nospecialize(func), @nospecialize(types); name = fix_name(func), kernel::Bool = false, @@ -168,36 +190,50 @@ function static_job(@nospecialize(func), @nospecialize(types); kwargs... ) source = GPUCompiler.methodinstance(typeof(func), Base.to_tuple_type(types)) - tm = target.tm - gputarget = StaticCompilerTarget(tm.triple, tm.cpu, tm.features, target.julia_runtime, method_table) + codegen = codegen_machine(target) + gputarget = StaticCompilerTarget(codegen.triple, codegen.cpu, codegen.features, target.julia_runtime, target.can_throw, method_table) config = GPUCompiler.CompilerConfig(gputarget, params; name, kernel, kwargs...) - return GPUCompiler.CompilerJob(source, config), Dict{}() + return GPUCompiler.CompilerJob(source, config) end ## The target -# Whether or not Julia's LLVM has the code generator for the triple -julia_llvm_has(triple::AbstractString) = (init_llvm_targets(); try; LLVM.Target(; triple = String(triple)); true; catch; false; end) +# Determine whether or not Julia's LLVM has the code generator for the triple +function julia_llvm_has(triple::AbstractString) + init_llvm_targets() + try + LLVM.Target(; triple = String(triple)) + return true + catch err + # (`ArgumentError`: "No available targets are compatible with triple") + err isa ArgumentError || rethrow() + return false + end +end -# Whether an object can be made for the target here: always if target=host, and for a `Retarget` if Julia's LLVM or an `llc` has the code generator -can_emit_object(target::StaticTarget) = isnothing(target.retarget) || julia_llvm_has(target.retarget.triple) || !isnothing(target.retarget.llc) +# Whether an object can be made for the target here: always if the code is made for it, and if it is retargeted, if Julia's LLVM or an `llc` has its code generator +can_emit_object(target::StaticTarget) = !is_retargeted(target) || julia_llvm_has(target_triple(target)) || !isnothing(target.llc) # Make any OS-specific choices on the basis of the target, not the host -iswindows(target::StaticTarget) = isnothing(target.platform) ? occursin(r"windows|mingw|msvc|cygwin"i, target_triple(target)) : Sys.iswindows(target.platform) -isapple(target::StaticTarget) = isnothing(target.platform) ? occursin(r"apple|darwin|macos"i, target_triple(target)) : Sys.isapple(target.platform) +iswindows(target::StaticTarget) = occursin(r"windows|mingw|msvc|cygwin"i, target_triple(target)) +isapple(target::StaticTarget) = occursin(r"apple|darwin|macos"i, target_triple(target)) -# The shared library extension of the target (that of the host, unless cross-compiling) -dlext(target::StaticTarget) = isnothing(target.platform) ? Libdl.dlext : platform_dlext(target.platform) +# The shared library extension of the target +dlext(target::StaticTarget) = isapple(target) ? "dylib" : iswindows(target) ? "dll" : "so" # Whether the IR has to go through an external compiler (`clang`, `llc`) to be an object: on Windows (always), or where Julia's LLVM has no code generator for the target -# (An `LLD` links objects, which IR is not: for it an object that cannot be made says so, by `generate_obj`, and does not go on as IR.) -needs_ir(target::StaticTarget) = (iswindows(target) || !can_emit_object(target)) && !(target.toolchain isa LLD) +# (An `LLDToolchain` links objects, which IR is not: for it an object that cannot be made says so, by `generate_obj`, and does not go on as IR.) +needs_ir(target::StaticTarget) = (iswindows(target) || !can_emit_object(target)) && !(target.toolchain isa LLDToolchain) + +# The architecture and the operating system that a triple says (not by `Platform`, which does not parse every triple that LLVM has: `x86_64-w64-windows-gnu`, `wasm64-unknown-unknown`) +triple_arch(triple::AbstractString) = (arch = String(first(split(triple, '-'))); arch in ("arm64", "aarch64") ? "aarch64" : arch in ("amd64", "x86_64") ? "x86_64" : arch) +triple_os(triple::AbstractString) = occursin(r"windows|mingw|msvc|cygwin"i, triple) ? :windows : occursin(r"apple|darwin|macos"i, triple) ? :apple : occursin("linux", triple) ? :linux : :other # `clang` makes code for its own host unless it is told the target (it replaces the triple of the IR, which `-Wno-override-module` hides) function clang_target_flags(target::StaticTarget) - host = HostPlatform() - foreign = !isnothing(target.retarget) || (!isnothing(target.platform) && (arch(target.platform) != arch(host) || os(target.platform) != os(host))) - return foreign ? `--target=$(target_triple(target))` : `` + triple, host = target_triple(target), LLVM.default_triple() + foreign = is_retargeted(target) || triple_arch(triple) != triple_arch(host) || triple_os(triple) != triple_os(host) + return foreign ? `--target=$triple` : `` end # The family that an architecture's intrinsics (`llvm..`) are named by diff --git a/src/toolchain.jl b/src/toolchain.jl index 2414a73d..4bf9dc62 100644 --- a/src/toolchain.jl +++ b/src/toolchain.jl @@ -4,32 +4,37 @@ Toolchain How a target is linked. [`link`](@ref) and [`output_name`](@ref) have a method for each type; new targets (another system, another kind of executable) are new types with those two methods. -`Driver` (a C compiler that compiles and links, as before) is the default, [`LLD`](@ref) links objects by itself (for a Linux or WebAssembly target that the host's compiler can not link for). -Give a target its toolchain by `StaticTarget(...; toolchain = ...)` or `set_toolchain!`. +`CCToolchain` (a C compiler that compiles and links, as before) is the default, [`LLDToolchain`](@ref) links objects by itself (for a Linux or WebAssembly target that the host's compiler can not link for). +Give a target its toolchain by `StaticTarget(...; toolchain = ...)` (or `StaticTarget(target; toolchain = ...)`, a copy that has another). """ Toolchain """ - Driver(cc::Cmd) + CCToolchain(cc::Cmd) Link by a C compiler driver (`clang` by default): the driver compiles what it is given, objects or LLVM IR (`.ll`), and links it. This is how executables and shared libraries were made before toolchains, -and it still is for the host and for any target that has a driver of its own (`set_compiler!`). +and it still is for the host and for any target that has a driver of its own (`StaticTarget(...; toolchain = CCToolchain(cc))`). """ -struct Driver <: Toolchain +struct CCToolchain <: Toolchain cc::Cmd builtins::Bool end -Driver(cc::Cmd; builtins::Bool = false) = Driver(cc, builtins) -Driver(cc::AbstractString; builtins::Bool = false) = Driver(`$cc`; builtins) -Base.:(==)(a::Driver, b::Driver) = a.cc == b.cc && a.builtins == b.builtins +CCToolchain(cc::Cmd; builtins::Bool = false) = CCToolchain(cc, builtins) +CCToolchain(cc::AbstractString; builtins::Bool = false) = CCToolchain(`$cc`; builtins) +Base.:(==)(a::CCToolchain, b::CCToolchain) = a.cc == b.cc && a.builtins == b.builtins """ - LLD(flavor; sysroot = nothing, dynamic_linker = nothing, libs = ["c", "m", "dl"], flags = ``, builtins = true) + LLDToolchain(flavor; sysroot = nothing, dynamic_linker = nothing, libs = nothing, flags = ``, builtins = true) -Link objects with the `lld` that Julia has, without a C compiler or its driver. `flavor` is `:gnu` (ELF, Linux: the start files and the libraries of the C library are in `sysroot`, a directory that has the `lib` of a glibc, -which the `Glibc_jll` artifact of the target's platform is) or `:wasm` (WebAssembly: a `.wasm` module that exports the entry point and imports the rest). `flags` are added to the command of the linker. With `builtins`, the object has the definitions of what the code generator calls and no library of the toolchain has (`__udivti3`, `__extendhfsf2`, ...: see [`add_builtins!`](@ref)). -Nothing makes a `main`: the object has one (see the `main_wrapper` of [`generate_obj`](@ref); [`generate_executable`](@ref) asks for it when the toolchain is not a `Driver`). +Link objects with the `lld` that Julia has, without a C compiler or its driver. `flavor` is +* `:gnu`: ELF for Linux. The start files and the libraries of the C library are in `sysroot`, a directory that has the `lib` of a glibc (the `Glibc_jll` artifact provides one). See [`glibc_sysroot`](@ref). +* `:darwin`: Mach-O for macOS. `sysroot` is the SDK (by default that of Xcode `xcrun --show-sdk-path`); where the host has none, an SDK must be given. See [`macos_sdk`](@ref). +* `:mingw`: PE for Windows, with the libraries of MinGW-w64 in `sysroot` (the `Mingw_jll` artifact provides one). See [`mingw_sysroot`](@ref). +* `:wasm`: WebAssembly, a `.wasm` module that exports the entry point and imports the rest. + +`libs` are the libraries that are linked with (the defaults are those of the flavor), and `flags` are added to the command of the linker. With `builtins`, the object has the definitions of what the code generator calls and no library of the toolchain has (`__udivti3`, `__extendhfsf2`, ...: see [`add_builtins!`](@ref)). +Nothing makes a `main`: the object has one (see the `main_wrapper` of [`generate_obj`](@ref); [`generate_executable`](@ref) asks for it when the toolchain is not a `CCToolchain`). """ -struct LLD <: Toolchain +struct LLDToolchain <: Toolchain flavor::Symbol sysroot::Union{String,Nothing} dynamic_linker::Union{String,Nothing} @@ -37,15 +42,21 @@ struct LLD <: Toolchain flags::Cmd builtins::Bool end -function LLD(flavor::Symbol; sysroot = nothing, dynamic_linker = nothing, libs = ["c", "m", "dl"], flags = ``, builtins::Bool = true) - flavor in (:gnu, :wasm) || throw(ArgumentError("the flavor of `LLD` is `:gnu` or `:wasm`, not `$(repr(flavor))`")) - return LLD(flavor, isnothing(sysroot) ? nothing : String(sysroot), isnothing(dynamic_linker) ? nothing : String(dynamic_linker), String.(libs), flags isa Cmd ? flags : `$flags`, builtins) +# the libraries that a flavor of `LLDToolchain` links with, if none are given +const DEFAULT_LLD_LIBS = Dict(:gnu => ["c", "m", "dl", "pthread"], # (`dl` and `pthread` are libraries of their own before glibc 2.34, and empty ones in it) + :darwin => ["System"], :wasm => String[], + :mingw => ["mingw32", "mingwex", "moldname", "msvcrt", "advapi32", "shell32", "user32", "kernel32"]) + +function LLDToolchain(flavor::Symbol; sysroot = nothing, dynamic_linker = nothing, libs = nothing, flags = ``, builtins::Bool = true) + flavor in keys(DEFAULT_LLD_LIBS) || throw(ArgumentError("the flavor of `LLDToolchain` is `:gnu`, `:darwin`, `:mingw` or `:wasm`, not `$(repr(flavor))`")) + libs === nothing && (libs = DEFAULT_LLD_LIBS[flavor]) + return LLDToolchain(flavor, isnothing(sysroot) ? nothing : String(sysroot), isnothing(dynamic_linker) ? nothing : String(dynamic_linker), String.(libs), flags isa Cmd ? flags : `$flags`, builtins) end -# (what a toolchain is made from: a toolchain, or the C compiler of a `Driver`) +# Ensure toolchain is either a Toolchain or Nothing as_toolchain(x::Toolchain) = x -as_toolchain(x::Cmd) = Driver(x) -as_toolchain(x::AbstractString) = Driver(x) +as_toolchain(x::Cmd) = CCToolchain(x) +as_toolchain(x::AbstractString) = CCToolchain(x) as_toolchain(::Nothing) = nothing @@ -56,24 +67,110 @@ The file name of what is made from `name` for the target: `name.exe` for a Windo """ output_name(::Toolchain, target, name::AbstractString; kind::Symbol = :executable) = kind === :shared ? "$name.$(dlext(target))" : iswindows(target) ? "$name.exe" : String(name) -output_name(tc::LLD, target, name::AbstractString; kind::Symbol = :executable) = - tc.flavor === :wasm ? "$name.wasm" : kind === :shared ? "$name.so" : String(name) +output_name(tc::LLDToolchain, target, name::AbstractString; kind::Symbol = :executable) = + tc.flavor === :wasm ? "$name.wasm" : + tc.flavor === :darwin ? (kind === :shared ? "$name.dylib" : String(name)) : + tc.flavor === :mingw ? (kind === :shared ? "$name.dll" : "$name.exe") : + kind === :shared ? "$name.so" : String(name) # whether the objects that the toolchain links need the builtins in them (`add_builtins!`) -uses_builtins(tc::Union{Driver,LLD}) = tc.builtins +uses_builtins(tc::Union{CCToolchain,LLDToolchain}) = tc.builtins uses_builtins(::Union{Toolchain,Nothing}) = false """ glibc_sysroot(platform_or_triple_or_target) -> String -The directory of the C library of a Linux target (for `LLD(:gnu; sysroot = ...)`): the artifact of `Glibc_jll` for its architecture, which is fetched if it is not there, for any host. -Needs `using Glibc_jll, Pkg`. +The directory of the C library of a Linux target (for `LLDToolchain(:gnu; sysroot = ...)`): the artifact of `Glibc_jll` for its architecture, which is fetched if it is not there, for any host. Needs `using Glibc_jll, Pkg`. + +**Which glibc** is the version of the `Glibc_jll` of the environment (see [`glibc_version`](@ref)), and a program that is linked with it needs that glibc or a newer one where it runs. The newest, 2.35, which is the default, +makes programs that need glibc 2.34 or newer (Ubuntu 22.04, Debian 12, ...): they do not run on older systems (Ubuntu 20.04, RHEL 8, ...). To make programs that run on older ones, pin an older `Glibc_jll` in the environment +(`Pkg.add(name = "Glibc_jll", version = "2.17")`, or in `[compat]`): 2.17.1 has x86-64, i686 and ppc64le, and 2.19.1 also aarch64, armv7l and armv6l, and programs that are linked with them need only glibc 2.17. +What a newer glibc has is not there to link with: the functions that were added after the pinned version (`posix_spawn_file_actions_addchdir_np` of 2.29, for example) are undefined symbols. """ function glibc_sysroot end +""" + glibc_version(sysroot) -> Union{VersionNumber,Nothing} + +The version of the glibc that a sysroot has (`nothing` if it has none that is known): the version that programs that are linked with it need, or a newer one, where they run. It is read from the banner of its `libc.so.6`. +""" +function glibc_version(sysroot::AbstractString) + libc = try; joinpath(crt_directory(sysroot), "libc.so.6"); catch; return nothing; end + isfile(libc) || return nothing + bytes = read(libc) + marker = Vector{UInt8}("release version ") + i = findfirst(marker, bytes) + i === nothing && return nothing + text = String(bytes[last(i)+1:min(end, last(i) + 16)]) + m = match(r"^(\d+)\.(\d+)", text) + return m === nothing ? nothing : VersionNumber(parse(Int, m.captures[1]), parse(Int, m.captures[2])) +end + +""" + mingw_sysroot(platform_or_triple_or_target) -> String + +The directory of the MinGW-w64 libraries of a Windows target (for `LLDToolchain(:mingw; sysroot = ...)`): the artifact of `Mingw_jll` for its architecture, which is fetched if it is not there, for any host. +Needs `using Mingw_jll, Pkg`. +""" +function mingw_sysroot end + +""" + macos_sdk() -> String + +The SDK of macOS that a program for macOS is linked with (for `LLDToolchain(:darwin; sysroot = ...)`, which uses it when it has no `sysroot`): the environment variable `SDKROOT`, or that of Xcode (`xcrun --show-sdk-path`) on a Mac. +Where the host has neither, the SDK is given to `LLDToolchain` as `sysroot`: Apple's license has it used on Apple hardware, and a copy of one has to be provided (for example `MacOSX11.3.sdk.tar.xz` of +[phracker/MacOSX-SDKs](https://github.com/phracker/MacOSX-SDKs), which BinaryBuilder uses). +""" +function macos_sdk() + haskey(ENV, "SDKROOT") && return ENV["SDKROOT"] + sdk = xcode_sdk() + isnothing(sdk) || return sdk + throw(ArgumentError("no SDK of macOS is known: give `sysroot = ...` to `LLDToolchain(:darwin)` (the directory of an SDK, like `MacOSX11.3.sdk`), or set `SDKROOT`")) +end + +# The SDK of Xcode (`xcrun --show-sdk-path`), or `nothing` where there is none (not a Mac, or no Xcode or Command Line Tools: `xcrun` fails). Asked once: the command takes some milliseconds, +# and a compile asks for it several times (the selected Xcode does not change in a session; `SDKROOT` is read each time, and is not what is remembered). +const XCODE_SDK = Ref{Union{String,Nothing}}() +const XCODE_SDK_KNOWN = Ref(false) +function xcode_sdk() + if !XCODE_SDK_KNOWN[] + XCODE_SDK[] = if Sys.isapple() && !isnothing(Sys.which("xcrun")) + errors = IOBuffer() + try + String(strip(read(pipeline(`xcrun --show-sdk-path`; stderr = errors), String))) + catch err + err isa ProcessFailedException || rethrow() + @warn """`xcrun --show-sdk-path` failed, so the SDK of macOS cannot be found, and programs for macOS cannot be linked with it. + $(strip(String(take!(errors)))) + To fix this, do one of: + * install the Command Line Tools: run `xcode-select --install` in a terminal + * select an installed Xcode: `sudo xcode-select -s /Applications/Xcode.app/Contents/Developer` + * point `SDKROOT` at an SDK, e.g. `ENV["SDKROOT"] = "/path/to/MacOSX11.3.sdk"` (or pass `sysroot = ...` to `LLDToolchain(:darwin)`) + Meanwhile the system `clang` is used, with whatever SDK it finds itself. (This warning is shown once.)""" maxlog = 1 + nothing + end + else + nothing + end + XCODE_SDK_KNOWN[] = true + end + return XCODE_SDK[] +end + +# the version of an SDK, which is in its `SDKSettings.json` (and `plist`) +function sdk_version(sdk::AbstractString) + for file in ("SDKSettings.json", "SDKSettings.plist") + path = joinpath(sdk, file) + isfile(path) || continue + m = match(r"\"Version\"\s*:\s*\"(\d+(?:\.\d+)*)\"|Version\s*(\d+(?:\.\d+)*)", read(path, String)) + m === nothing || return something(m.captures[1], m.captures[2]) + end + return "11.0" +end + # The toolchain of a target: its own, or the default: the host's C compiler driver -default_toolchain(::StaticTarget) = Driver(clang()) -toolchain(target::StaticTarget) = something(target.toolchain, default_toolchain(target)) +default_toolchain(::StaticTarget) = CCToolchain(clang()) +toolchain(target::StaticTarget) = isnothing(target.toolchain) ? default_toolchain(target) : target.toolchain # (not `something`, which would find the default, and run `xcrun`, whatever the target has) # Generally prefer Clang_jll clang (which should probably be at least as new as Julia's LLVM) # and fall back to the system one on platforms where Clang_jll is not available or not expected to work @@ -81,9 +178,10 @@ function clang() Clang_jll.is_available() || return `clang` # Fall back to system clang if Clang_jll is not available cmd = Clang_jll.clang() # Clang_jll may not know where to find the macOS SDK (e.g. for `-lSystem`) - if Sys.isapple() && !haskey(ENV, "SDKROOT") - isnothing(Sys.which("xcrun")) && return `clang` # Fall back to system clang if we can't figure out the SDK path - cmd = addenv(cmd, "SDKROOT" => readchomp(`xcrun --show-sdk-path`)) + if Sys.isapple() && !haskey(ENV, "SDKROOT") + sdk = xcode_sdk() + isnothing(sdk) && return `clang` # Fall back to system clang if we can't figure out the SDK path + cmd = addenv(cmd, "SDKROOT" => sdk) end return cmd end @@ -96,7 +194,7 @@ function crt_directory(sysroot::AbstractString) for d in ("lib", "usr/lib", "lib64", "usr/lib64", "usr/lib/aarch64-linux-gnu", "usr/lib/x86_64-linux-gnu", "lib/aarch64-linux-gnu", "lib/x86_64-linux-gnu") isfile(joinpath(sysroot, d, "crt1.o")) && return joinpath(sysroot, d) end - throw(ArgumentError("`$sysroot` has no `crt1.o`: the `sysroot` of a `:gnu` `LLD` is a directory that has the `lib` of a glibc (the `Glibc_jll` artifact for the platform of the target is one)")) + throw(ArgumentError("`$sysroot` has no `crt1.o`: the `sysroot` of a `:gnu` `LLDToolchain` is a directory that has the `lib` of a glibc (the `Glibc_jll` artifact for the platform of the target is one)")) end # the loader that a program of the architecture asks for, where it is on the system that it runs on @@ -104,5 +202,5 @@ function default_dynamic_linker(target::StaticTarget) a = llvm_family(first(split(target_triple(target), '-'))) a == "x86" && return "/lib64/ld-linux-x86-64.so.2" a == "aarch64" && return "/lib/ld-linux-aarch64.so.1" - throw(ArgumentError("no dynamic linker is known for $(target_triple(target)): give `dynamic_linker` to `LLD`")) + throw(ArgumentError("no dynamic linker is known for $(target_triple(target)): give `dynamic_linker` to `LLDToolchain`")) end \ No newline at end of file diff --git a/test/Project.toml b/test/Project.toml index e49b43e8..3c809d13 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,17 +1,18 @@ [deps] -Glibc_jll = "452aa2e7-e185-58db-8ff9-d3c1fa4bc997" -Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" +Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" +Glibc_jll = "452aa2e7-e185-58db-8ff9-d3c1fa4bc997" LLD_jll = "d55e3150-da41-5e91-b323-ecfd1eec6109" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667" +Mingw_jll = "2d94bacf-4d71-535d-a47c-45e808015115" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" StaticTools = "86c06d3c-3f03-46de-9781-57580aa96d0a" StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e" [compat] Bumper = "0.7" diff --git a/test/cross/matrix.jl b/test/cross/matrix.jl new file mode 100644 index 00000000..1237a536 --- /dev/null +++ b/test/cross/matrix.jl @@ -0,0 +1,81 @@ +# What can be made from this host, for each target: `julia test/cross/matrix.jl [outdir]`. +# For each target it tries three things: the LLVM IR, an object file, and a linked executable (the program returns `argc + 41`: 42 when it is run with no arguments), and says what each came to. +# The table is printed, and written to `$GITHUB_STEP_SUMMARY` where there is one. With `outdir`, the executables are kept as `outdir///`, for `test/cross/run.jl` to run on a machine of their target. +# It makes an environment of its own, with the extensions of StaticCompiler that fetch the C libraries of the targets (`Glibc_jll`, `Mingw_jll`). +# +# What a host needs for a target: the code generator for it (Julia's LLVM has the host's architecture and WebAssembly; an `llc` of the same version of LLVM has others: `STATIC_COMPILER_LLC`), and for the link the +# libraries of the target: fetched for Linux and Windows, and for macOS an SDK (Xcode's on a Mac, else the directory in `SDKROOT`). + +import Pkg +Pkg.activate(temp = true; io = devnull) +Pkg.develop(path = dirname(dirname(@__DIR__)); io = devnull) +Pkg.add(["Glibc_jll", "Mingw_jll", "LLVM", "Pkg"]; io = devnull) +using StaticCompiler, Glibc_jll, Mingw_jll, LLVM, Pkg +using Base.BinaryPlatforms + +const SC = StaticCompiler +host = string(Sys.ARCH, "-", Sys.KERNEL === :NT ? "windows" : Sys.islinux() ? "linux" : Sys.isapple() ? "macos" : string(Sys.KERNEL)) +outdir = length(ARGS) >= 1 ? abspath(ARGS[1]) : nothing + +matrix_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int + +# the targets, and how each is linked (a function that makes the toolchain, which may say that a resource is missing by throwing) +const TARGETS = [ + ("aarch64-unknown-linux-gnu", () -> SC.LLDToolchain(:gnu; sysroot = SC.glibc_sysroot("aarch64-unknown-linux-gnu"))), + ("x86_64-unknown-linux-gnu", () -> SC.LLDToolchain(:gnu; sysroot = SC.glibc_sysroot("x86_64-unknown-linux-gnu"))), + ("aarch64-apple-macosx11.0.0", () -> SC.LLDToolchain(:darwin)), + ("x86_64-apple-macosx11.0.0", () -> SC.LLDToolchain(:darwin)), + ("x86_64-w64-mingw32", () -> SC.LLDToolchain(:mingw; sysroot = SC.mingw_sysroot("x86_64-w64-mingw32"))), + ("wasm64-unknown-unknown", () -> SC.LLDToolchain(:wasm)), +] + +short(e) = first(split(sprint(showerror, e), '\n')) +status(f) = try; f(); "ok"; catch e; "fails: " * first(short(e), 90); end + +rows = NamedTuple[] +for (triple, make_toolchain) in TARGETS + println("\n## ", triple) + llc = get(ENV, "STATIC_COMPILER_LLC", nothing) + target = try; SC.StaticTarget(triple; llc); catch e; nothing; end + if target === nothing + push!(rows, (; triple, ir = "fails", object = "-", link = "-")); continue + end + backend = SC.julia_llvm_has(triple) + # the IR is made for any target (the host's code, retargeted) + ir_target = SC.StaticTarget(triple; retarget = true) + ir = status() do + text = SC.generate_ir(matrix_main, Tuple{Int,Ptr{Ptr{UInt8}}}; target = ir_target, main_wrapper = "matrix_main") + occursin("define i32 @main", text) || error("no `main` in the IR") + end + println(" IR: ", ir) + # the object: by Julia's LLVM if it has the code generator, or by `llc` + object = !SC.can_emit_object(target) ? "no code generator (needs an `llc`)" : status() do + dir = mktempdir() + _, obj = SC.generate_obj(((matrix_main, Tuple{Int,Ptr{Ptr{UInt8}}}),), dir, "matrix_main"; target, main_wrapper = "matrix_main") + filesize(obj) > 100 || error("the object is empty") + end + println(" object: ", object) + # the executable + link = if !SC.can_emit_object(target) + "no code generator" + else + status() do + linked = SC.StaticTarget(target; toolchain = make_toolchain()) # (a copy that is linked so, and not the target itself) + dir = mktempdir() + exe = compile_executable(matrix_main, (Int, Ptr{Ptr{UInt8}}), dir, "matrix_main"; target = linked) + isfile(exe) || error("no executable") + if outdir !== nothing + kept = joinpath(outdir, host, triple) + mkpath(kept) + cp(exe, joinpath(kept, basename(exe)); force = true) + end + end + end + println(" link: ", link) + push!(rows, (; triple, ir, object, link)) +end + +table = string("| host `", host, "` makes for | IR | object | executable |\n|:--|:--|:--|:--|\n", + join(("| `$(r.triple)` | $(r.ir) | $(r.object) | $(r.link) |" for r in rows), "\n"), "\n") +println("\n", table) +haskey(ENV, "GITHUB_STEP_SUMMARY") && open(io -> println(io, "\n### Compile matrix, host ", host, ", Julia ", VERSION, "\n\n", table), ENV["GITHUB_STEP_SUMMARY"], "a") diff --git a/test/cross/run.jl b/test/cross/run.jl new file mode 100644 index 00000000..68cc8984 --- /dev/null +++ b/test/cross/run.jl @@ -0,0 +1,35 @@ +# Runs the executables that `test/cross/matrix.jl` kept, where they can run: `julia test/cross/run.jl dir`. +# `dir///` was made on `` for ``. The ones that this machine can run (its system and its architecture) are run, and each has to return 42 (the program returns `argc + 41`). +# Exits with a status that is not 0 if one that could be run did not do that. + +host_arch = Sys.ARCH === :x86_64 ? "x86_64" : Sys.ARCH === :aarch64 ? "aarch64" : string(Sys.ARCH) +host_system = Sys.iswindows() ? "windows" : Sys.islinux() ? "linux" : Sys.isapple() ? "macos" : string(Sys.KERNEL) + +# whether a program for the triple runs here: the system, and the architecture (an Apple Silicon Mac runs x86-64 programs of macOS with Rosetta, if it has it) +function runs_here(triple) + arch = first(split(triple, '-')) + arch == "x86_64" || arch == "aarch64" || return false + system = occursin("apple", triple) ? "macos" : occursin("linux", triple) ? "linux" : occursin("mingw", triple) || occursin("windows", triple) ? "windows" : "?" + system == host_system || return false + arch == host_arch && return true + return system == "macos" && host_arch == "aarch64" && arch == "x86_64" && success(pipeline(`arch -x86_64 /usr/bin/true`; stderr = devnull)) +end + +dir = abspath(ARGS[1]) +rows = NamedTuple[] +for made_on in sort(readdir(dir)), triple in sort(readdir(joinpath(dir, made_on))) + runs_here(triple) || continue + for file in readdir(joinpath(dir, made_on, triple)) + exe = joinpath(dir, made_on, triple, file) + Sys.iswindows() || chmod(exe, 0o755) + command = host_system == "macos" && !startswith(triple, host_arch) ? `arch -x86_64 $exe` : `$exe` + p = run(pipeline(ignorestatus(command); stdout = devnull, stderr = devnull)) + push!(rows, (; made_on, triple, result = p.exitcode == 42 ? "ok" : "exit status $(p.exitcode) (42 wanted)")) + end +end + +host = string(host_arch, "-", host_system) +table = string("| made on | for | run on `", host, "` |\n|:--|:--|:--|\n", join(("| $(r.made_on) | `$(r.triple)` | $(r.result) |" for r in rows), "\n"), "\n") +println(table) +haskey(ENV, "GITHUB_STEP_SUMMARY") && open(io -> println(io, "\n### Executables run on ", host, "\n\n", isempty(rows) ? "none of those that were made are for this machine" : table), ENV["GITHUB_STEP_SUMMARY"], "a") +all(r -> r.result == "ok", rows) || exit(1) diff --git a/test/runtests.jl b/test/runtests.jl index bfeb5b9b..8c533cd7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,7 +9,7 @@ using StaticTools # using StrideArraysCore using LLD_jll using LLVM -using Glibc_jll, Pkg # (the extension that has the C library of a Linux target) +using Glibc_jll, Mingw_jll, Pkg # (the extensions that have the C libraries of a Linux and a Windows target) using Bumper addprocs(1) diff --git a/test/testcore.jl b/test/testcore.jl index 2d5bdec4..85dc8783 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -168,15 +168,15 @@ end @testset "The target is not the host" begin using Base.BinaryPlatforms - # The target machine is the host's: only the platform is considered, and not every LLVM has the code generator of each platform - t(p) = StaticCompiler.StaticTarget(p, StaticCompiler.StaticTarget().tm, nothing, false) + # The code is made for the host: only the platform is considered, and not every LLVM has the code generator of each platform + t(p) = StaticCompiler.StaticTarget(p; retarget = true) # Ensure we are checking the target, no the host @test StaticCompiler.iswindows(t(Platform("x86_64", "windows"))) && !StaticCompiler.isapple(t(Platform("x86_64", "windows"))) @test StaticCompiler.isapple(t(Platform("aarch64", "macos"))) && !StaticCompiler.iswindows(t(Platform("aarch64", "macos"))) @test !StaticCompiler.iswindows(t(Platform("aarch64", "linux"))) && !StaticCompiler.isapple(t(Platform("aarch64", "linux"))) # A triple that `Platform` does not parse wasm = StaticCompiler.StaticTarget("wasm32-unknown-unknown", "generic", "") - @test isnothing(wasm.platform) && !StaticCompiler.iswindows(wasm) && !StaticCompiler.isapple(wasm) + @test isnothing(tryparse(Platform, "wasm32-unknown-unknown")) && !StaticCompiler.iswindows(wasm) && !StaticCompiler.isapple(wasm) end @testset "LLVM IR as text" begin @@ -188,7 +188,7 @@ end @testset "A target that Julia's LLVM has no code generator for" begin triple = "x86_64-unknown-linux-gnu" - ir_target = StaticCompiler.StaticTarget(tryparse(Base.BinaryPlatforms.Platform, triple), StaticCompiler.host_generic_machine(), nothing, StaticCompiler.Retarget(triple, "", "", nothing), false) + ir_target = StaticCompiler.StaticTarget(triple; retarget = true) @test StaticCompiler.target_triple(ir_target) == triple && StaticCompiler.needs_ir(ir_target) == !StaticCompiler.julia_llvm_has(triple) @test occursin("--target=$triple", string(StaticCompiler.clang_target_flags(ir_target))) ir = StaticCompiler.generate_ir(fib, Tuple{Int}; target = ir_target) @@ -204,12 +204,22 @@ end @test occursin("target triple = \"$triple\"", read(ir_path, String)) # the host's own target is untouched host = StaticCompiler.StaticTarget() - @test isnothing(host.retarget) && StaticCompiler.needs_ir(host) == Sys.iswindows() + @test !StaticCompiler.is_retargeted(host) && StaticCompiler.needs_ir(host) == Sys.iswindows() @test isempty(StaticCompiler.clang_target_flags(host)) + # (what the host is is found from its triple, however it is spelled: `Platform` does not parse the Windows triple that LLVM has) + @test StaticCompiler.triple_os("x86_64-w64-windows-gnu") == StaticCompiler.triple_os("x86_64-pc-windows-msvc") == StaticCompiler.triple_os("x86_64-w64-mingw32") == :windows + @test StaticCompiler.triple_os("arm64-apple-macosx11.0.0") == StaticCompiler.triple_os("aarch64-apple-darwin22.6.0") == :apple && StaticCompiler.triple_os("aarch64-unknown-linux-gnu") == :linux && StaticCompiler.triple_os("wasm64-unknown-unknown") == :other + @test StaticCompiler.triple_arch("arm64-apple-darwin") == StaticCompiler.triple_arch("aarch64-unknown-linux-gnu") == "aarch64" && StaticCompiler.triple_arch("amd64-pc-windows-msvc") == "x86_64" + @test isempty(StaticCompiler.clang_target_flags(StaticCompiler.StaticTarget(LLVM.default_triple()))) + @test !isempty(StaticCompiler.clang_target_flags(StaticCompiler.StaticTarget("wasm64-unknown-unknown"))) + for other in ("x86_64-w64-windows-gnu", "aarch64-unknown-linux-gnu", "x86_64-apple-macosx11.0.0") + t = StaticCompiler.StaticTarget(other; retarget = true) + @test occursin("--target=", string(StaticCompiler.clang_target_flags(t))) + end # where there is no code generator for the triple, the constructors make such a target for t in ("x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "riscv64-unknown-linux-gnu") has_backend = try; LLVM.Target(triple = t); true; catch; false; end - has_backend || @test StaticCompiler.StaticTarget(t, "generic", "").retarget.triple == LLVM.normalize(t) + has_backend || @test StaticCompiler.is_retargeted(StaticCompiler.StaticTarget(t, "generic", "")) && StaticCompiler.target_triple(StaticCompiler.StaticTarget(t, "generic", "")) == LLVM.normalize(t) end end @@ -217,21 +227,30 @@ end using Base.BinaryPlatforms host = StaticCompiler.StaticTarget() # the default is the host's C compiler driver, and `set_compiler!` is a driver - @test StaticCompiler.toolchain(host) isa StaticCompiler.Driver - t = StaticCompiler.StaticTarget(host.platform, host.tm, "mycc", false) - @test t.toolchain == StaticCompiler.Driver(`mycc`) && StaticCompiler.toolchain(t).cc == `mycc` - StaticCompiler.set_compiler!(t, "othercc"); @test t.toolchain.cc == `othercc` - StaticCompiler.set_toolchain!(t, StaticCompiler.LLD(:wasm)); @test t.toolchain isa StaticCompiler.LLD - @test_throws ArgumentError StaticCompiler.LLD(:macho) + @test StaticCompiler.toolchain(host) isa StaticCompiler.CCToolchain + t = StaticCompiler.StaticTarget(host; toolchain = "mycc") + @test t.toolchain == StaticCompiler.CCToolchain(`mycc`) && StaticCompiler.toolchain(t).cc == `mycc` + t2 = StaticCompiler.StaticTarget(t; toolchain = `othercc`); @test t2.toolchain.cc == `othercc` && t.toolchain.cc == `mycc` # (a copy: the original is as it was) + t3 = StaticCompiler.StaticTarget(t; toolchain = StaticCompiler.LLDToolchain(:wasm)); @test t3.toolchain isa StaticCompiler.LLDToolchain + # (and the setters change a target in place) + StaticCompiler.set_compiler!(t, "othercc"); @test t.toolchain.cc == `othercc` && t2.toolchain.cc == `othercc` + StaticCompiler.set_toolchain!(t, StaticCompiler.LLDToolchain(:wasm)); @test t.toolchain isa StaticCompiler.LLDToolchain + StaticCompiler.set_runtime!(t, true); @test t.julia_runtime + # what the compiled code may do is the target's, and is passed to the compiler + @test host.can_throw && !host.julia_runtime && !StaticCompiler.StaticTarget(host; can_throw = false).can_throw && StaticCompiler.StaticTarget(host; julia_runtime = true).julia_runtime + job(target) = StaticCompiler.static_job(fib, Tuple{Int}; target) + @test StaticCompiler.GPUCompiler.can_throw(job(host)) && !StaticCompiler.GPUCompiler.can_throw(job(StaticCompiler.StaticTarget(host; can_throw = false))) + @test !StaticCompiler.GPUCompiler.uses_julia_runtime(job(host)) && StaticCompiler.GPUCompiler.uses_julia_runtime(job(StaticCompiler.StaticTarget(host; julia_runtime = true))) + @test_throws ArgumentError StaticCompiler.LLDToolchain(:macho) # (the names of what is made) - drv = StaticCompiler.Driver(`cc`) - win = StaticCompiler.StaticTarget(Platform("x86_64", "windows"), host.tm, nothing, false) + drv = StaticCompiler.CCToolchain(`cc`) + win = StaticCompiler.StaticTarget(Platform("x86_64", "windows"); retarget = true) @test StaticCompiler.output_name(drv, host, "prog") == (Sys.iswindows() ? "prog.exe" : "prog") @test StaticCompiler.output_name(drv, win, "prog") == "prog.exe" && StaticCompiler.output_name(drv, win, "lib"; kind = :shared) == "lib.dll" - @test StaticCompiler.output_name(StaticCompiler.LLD(:wasm), host, "prog") == "prog.wasm" - @test StaticCompiler.output_name(StaticCompiler.LLD(:gnu), host, "prog") == "prog" && StaticCompiler.output_name(StaticCompiler.LLD(:gnu), win, "lib"; kind = :shared) == "lib.so" # (ELF, on any host) + @test StaticCompiler.output_name(StaticCompiler.LLDToolchain(:wasm), host, "prog") == "prog.wasm" + @test StaticCompiler.output_name(StaticCompiler.LLDToolchain(:gnu), host, "prog") == "prog" && StaticCompiler.output_name(StaticCompiler.LLDToolchain(:gnu), win, "lib"; kind = :shared) == "lib.so" # (ELF, on any host) # a target made with a toolchain - @test StaticCompiler.StaticTarget("wasm32", "", ""; toolchain = StaticCompiler.LLD(:wasm)).toolchain isa StaticCompiler.LLD + @test StaticCompiler.StaticTarget("wasm32", "", ""; toolchain = StaticCompiler.LLDToolchain(:wasm)).toolchain isa StaticCompiler.LLDToolchain # `main` made in IR: the executable runs without a C wrapper dir = mktempdir() @@ -248,13 +267,13 @@ end # linking by `lld`: a WebAssembly module (nothing outside of Julia is needed), and the refusals that say what is missing wasm_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int - wasm = StaticCompiler.StaticTarget("wasm64-unknown-unknown"; toolchain = StaticCompiler.LLD(:wasm)) + wasm = StaticCompiler.StaticTarget("wasm64-unknown-unknown"; toolchain = StaticCompiler.LLDToolchain(:wasm)) wasm_path = compile_executable(wasm_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "wasm_main"; target = wasm) @test endswith(wasm_path, "wasm_main.wasm") && read(wasm_path, 4) == UInt8[0x00, 0x61, 0x73, 0x6d] - linux = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu)) + linux = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLDToolchain(:gnu)) @test_throws ArgumentError compile_executable(wasm_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "no_sysroot"; target = linux) # (the C library is the target's: `sysroot`) - @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLD(:gnu; sysroot = mktempdir()), linux, [obj], joinpath(dir, "x"); entry = "main") # (a sysroot that has no `crt1.o`) - @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLD(:wasm), wasm, [joinpath(dir, "a.ll")], joinpath(dir, "x")) # (`lld` links objects, not IR) + @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLDToolchain(:gnu; sysroot = mktempdir()), linux, [obj], joinpath(dir, "x"); entry = "main") # (a sysroot that has no `crt1.o`) + @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLDToolchain(:wasm), wasm, [joinpath(dir, "a.ll")], joinpath(dir, "x")) # (`lld` links objects, not IR) end @testset "Builtins and the start of main" begin @@ -263,27 +282,41 @@ end halfir = "define float @g(half %a) {\n %r = fpext half %a to float\n ret float %r\n}\n" @test StaticCompiler.needed_builtins(i128ir, "aarch64-unknown-linux-gnu") == [:LINUX_INT128_BUILTINS] @test isempty(StaticCompiler.needed_builtins(i128ir, "aarch64-apple-macosx11.0.0")) # (macOS has them in its C library) + @test StaticCompiler.needed_builtins("", "x86_64-w64-windows-gnu") == [:MINGW_X64_BUILTINS] && StaticCompiler.needed_builtins("", "x86_64-w64-mingw32") == [:MINGW_X64_BUILTINS] # (the runtime of MinGW has to have the probe of the stack) + @test isempty(StaticCompiler.needed_builtins("", "x86_64-unknown-linux-gnu")) && occursin("___chkstk_ms:", StaticCompiler.add_builtins("", "x86_64-w64-windows-gnu")) @test StaticCompiler.needed_builtins(halfir, "x86_64-unknown-linux-gnu") == [:X86_HALF_BUILTINS] @test StaticCompiler.needed_builtins(halfir, "wasm64-unknown-unknown") == [:WASM_HALF_BUILTINS] @test isempty(StaticCompiler.needed_builtins(halfir, "aarch64-unknown-linux-gnu")) # (the CPU converts) + # (the same by the instructions of a module, which is not printed to be searched; and a name that says `half` is not a `half`) + namedir = "define i32 @half(i32 %a) {\n %r = add i32 %a, 1\n ret i32 %r\n}\n" + LLVM.@dispose ctx = LLVM.Context() begin + needed(ir, triple) = (mod = parse(LLVM.Module, ir); try; StaticCompiler.needed_builtins(mod, triple); finally; LLVM.dispose(mod); end) + @test needed(i128ir, "aarch64-unknown-linux-gnu") == [:LINUX_INT128_BUILTINS] && isempty(needed(i128ir, "aarch64-apple-macosx11.0.0")) + @test needed(halfir, "x86_64-unknown-linux-gnu") == [:X86_HALF_BUILTINS] && needed(halfir, "wasm64-unknown-unknown") == [:WASM_HALF_BUILTINS] + @test isempty(needed(halfir, "aarch64-unknown-linux-gnu")) && isempty(needed(namedir, "wasm64-unknown-unknown")) + @test needed("", "x86_64-w64-windows-gnu") == [:MINGW_X64_BUILTINS] + end + # (a target that has a toolchain of its own is not given the default, which finds `clang` and the SDK) + own = StaticCompiler.LLDToolchain(:gnu; sysroot = "/nonexistent") + @test StaticCompiler.toolchain(StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = own, retarget = true)) === own @test occursin("@__udivti3", StaticCompiler.add_builtins(i128ir, "aarch64-unknown-linux-gnu")) && StaticCompiler.add_builtins(halfir, "aarch64-unknown-linux-gnu") == halfir # a toolchain asks for them: `lld` links nothing else, a compiler driver brings its runtime library - @test StaticCompiler.uses_builtins(StaticCompiler.LLD(:gnu)) && !StaticCompiler.uses_builtins(StaticCompiler.LLD(:gnu; builtins = false)) - @test !StaticCompiler.uses_builtins(StaticCompiler.Driver(`cc`)) && StaticCompiler.uses_builtins(StaticCompiler.Driver(`cc`; builtins = true)) + @test StaticCompiler.uses_builtins(StaticCompiler.LLDToolchain(:gnu)) && !StaticCompiler.uses_builtins(StaticCompiler.LLDToolchain(:gnu; builtins = false)) + @test !StaticCompiler.uses_builtins(StaticCompiler.CCToolchain(`cc`)) && StaticCompiler.uses_builtins(StaticCompiler.CCToolchain(`cc`; builtins = true)) # the IR of a program for Linux made with them (and with `retarget`: the code is the host's, the target is Linux) div128(a::Int, b::Int) = (UInt128(a) ÷ UInt128(b)) % Int - tc = StaticCompiler.LLD(:gnu; sysroot = "/nonexistent") + tc = StaticCompiler.LLDToolchain(:gnu; sysroot = "/nonexistent") target = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = tc, retarget = true) - @test target.retarget.triple == "aarch64-unknown-linux-gnu" && LLVM.cpu(target.tm) in ("", "generic") + @test StaticCompiler.target_triple(target) == "aarch64-unknown-linux-gnu" && StaticCompiler.is_retargeted(target) && target.codegen.cpu in ("", "generic") ir = StaticCompiler.generate_ir(div128, Tuple{Int,Int}; target) @test occursin("target triple = \"aarch64-unknown-linux-gnu\"", ir) && occursin(r"define [^\n]*@__udivti3", ir) && !occursin("target datalayout", ir) # (linked into the module of a target that is not retargeted, which has a data layout: with no warning of different layouts) arch = String(first(split(LLVM.default_triple(), '-'))) - plain = StaticCompiler.StaticTarget("$arch-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu; sysroot = "/nonexistent")) + plain = StaticCompiler.StaticTarget("$arch-unknown-linux-gnu"; toolchain = StaticCompiler.LLDToolchain(:gnu; sysroot = "/nonexistent")) plain_ir = @test_logs min_level = Base.CoreLogging.Warn StaticCompiler.generate_ir(div128, Tuple{Int,Int}; target = plain) @test occursin(r"define [^\n]*@__udivti3", plain_ir) && occursin("target datalayout", plain_ir) - ir = StaticCompiler.generate_ir(div128, Tuple{Int,Int}; target = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu; builtins = false), retarget = true)) + ir = StaticCompiler.generate_ir(div128, Tuple{Int,Int}; target = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLDToolchain(:gnu; builtins = false), retarget = true)) @test !occursin("@__udivti3", ir) # what the start of `main` does first (the program of a collector that looks at the stack from its top: `main` stores it) @@ -327,10 +360,14 @@ end sysroot = StaticCompiler.glibc_sysroot(triple) @test isfile(joinpath(sysroot, "lib", "crt1.o")) || isfile(joinpath(StaticCompiler.crt_directory(sysroot), "crt1.o")) @test StaticCompiler.glibc_sysroot(Base.BinaryPlatforms.Platform(host_arch, "linux"; libc = "glibc")) == sysroot + # the version of the glibc is the version of the `Glibc_jll` that is in the environment (pinning it chooses the glibc) + v = StaticCompiler.glibc_version(sysroot) + @test v isa VersionNumber && (v.major, v.minor) == (pkgversion(Glibc_jll).major, pkgversion(Glibc_jll).minor) + @test isnothing(StaticCompiler.glibc_version(mktempdir())) # (a directory that has none) lin_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int - target = StaticCompiler.StaticTarget(triple; toolchain = StaticCompiler.LLD(:gnu; sysroot)) + target = StaticCompiler.StaticTarget(triple; toolchain = StaticCompiler.LLDToolchain(:gnu; sysroot)) # An object that cannot be made is refused, before the linker is asked for it - nobackend = StaticCompiler.StaticTarget("fakearch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLD(:gnu; sysroot)) + nobackend = StaticCompiler.StaticTarget("fakearch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLDToolchain(:gnu; sysroot)) @test_throws ArgumentError compile_executable(lin_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "nobackend"; target = nobackend) div128(a::Int, b::Int) = (UInt128(a) ÷ UInt128(b)) % Int exe = compile_executable(lin_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "lin_main"; target) @@ -350,3 +387,72 @@ end target = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; retarget = true)) @test seen == ["aarch64-unknown-linux-gnu true"] && occursin("@main2", ir) end + +@testset "Mach-O and PE: LLDToolchain for macOS and Windows" begin + # the flavors, what they link with, and what they make + @test StaticCompiler.LLDToolchain(:darwin).libs == ["System"] && StaticCompiler.LLDToolchain(:gnu).libs == ["c", "m", "dl", "pthread"] && isempty(StaticCompiler.LLDToolchain(:wasm).libs) + @test "kernel32" in StaticCompiler.LLDToolchain(:mingw).libs && "mingw32" in StaticCompiler.LLDToolchain(:mingw).libs && StaticCompiler.LLDToolchain(:mingw; libs = ["a"]).libs == ["a"] + @test_throws ArgumentError StaticCompiler.LLDToolchain(:coff) + host = StaticCompiler.StaticTarget() + out(flavor, name; kind = :executable) = StaticCompiler.output_name(StaticCompiler.LLDToolchain(flavor), host, name; kind) + @test out(:darwin, "p") == "p" && out(:darwin, "l"; kind = :shared) == "l.dylib" # (whatever the host is) + @test out(:mingw, "p") == "p.exe" && out(:mingw, "l"; kind = :shared) == "l.dll" + @test out(:gnu, "p") == "p" && out(:gnu, "l"; kind = :shared) == "l.so" && out(:wasm, "p") == "p.wasm" + + # the SDK of macOS: the environment's, or Xcode's on a Mac, and else it has to be given + withenv("SDKROOT" => "/some/sdk") do + @test StaticCompiler.macos_sdk() == "/some/sdk" + end + withenv("SDKROOT" => nothing) do + if Sys.isapple() && !isnothing(Sys.which("xcrun")) + @test isdir(StaticCompiler.macos_sdk()) + else + @test_throws ArgumentError StaticCompiler.macos_sdk() + end + end + sdk = mktempdir() + @test StaticCompiler.sdk_version(sdk) == "11.0" # (without `SDKSettings.json`) + write(joinpath(sdk, "SDKSettings.json"), """{"CanonicalName":"macosx14.2","Version":"14.2","DisplayName":"macOS 14.2"}""") + @test StaticCompiler.sdk_version(sdk) == "14.2" + + # `mingw_sysroot`: the triples that it takes, and the refusals that come before the artifact (150 MB) is fetched, which `test/cross/matrix.jl` does + @test_throws ArgumentError StaticCompiler.mingw_sysroot("aarch64-w64-mingw32") # (`Mingw_jll` has x86_64 and i686) + @test_throws ArgumentError StaticCompiler.mingw_sysroot("aarch64-w64-windows-gnu") + @test_throws ArgumentError StaticCompiler.mingw_sysroot("x86_64-unknown-linux-gnu") # (not Windows) + @test_throws ArgumentError StaticCompiler.mingw_sysroot(StaticCompiler.StaticTarget("aarch64-w64-mingw32"; retarget = true)) + + # a MinGW-w64 root has the start files in its `lib`, and one without them is refused (what `Mingw_jll`'s artifact is: fetched by the matrix of `test/cross`, which is 150 MB) + root = mktempdir() + mkpath(joinpath(root, "lib")); touch(joinpath(root, "lib", "crt2.o")) + @test StaticCompiler.mingw_directory(root) == joinpath(root, "lib") + @test_throws ArgumentError StaticCompiler.mingw_directory(mktempdir()) + obj = joinpath(mktempdir(), "x.o"); touch(obj) + windows = StaticCompiler.StaticTarget("x86_64-w64-mingw32"; retarget = true) + @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLDToolchain(:mingw), windows, [obj], joinpath(dirname(obj), "x.exe")) # (no libraries) + @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLDToolchain(:mingw; sysroot = mktempdir()), windows, [obj], joinpath(dirname(obj), "x.exe")) # (no start files) + @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLDToolchain(:mingw; sysroot = root), StaticCompiler.StaticTarget("riscv64-w64-mingw32"; retarget = true), [obj], joinpath(dirname(obj), "x.exe")) # (no machine of PE for it) + + # Check that windows target is compiling the correct executable types + arch = String(first(split(LLVM.default_triple(), '-'))) + for triple in ("$arch-w64-mingw32", "$arch-w64-windows-gnu") + t = StaticCompiler.StaticTarget(triple) + @test StaticCompiler.target_triple(t) == "$arch-w64-windows-gnu" && StaticCompiler.iswindows(t) + win_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int + _, obj = StaticCompiler.generate_obj(((win_main, Tuple{Int,Ptr{Ptr{UInt8}}}),), mktempdir(), "o"; target = t, main_wrapper = "win_main") + head = read(obj, 2) + @test head != UInt8[0x7f, 0x45] && head == (arch == "x86_64" ? UInt8[0x64, 0x86] : UInt8[0x64, 0xaa]) + end + @test StaticCompiler.target_triple(StaticCompiler.StaticTarget("$arch-w64-mingw32"; retarget = true)) == "$arch-w64-windows-gnu" + + # a program for macOS made with `lld` alone, and run (on an Apple Silicon Mac: the code generator for its architecture is in Julia's LLVM, and it runs) + if Sys.isapple() && Sys.ARCH === :aarch64 && !isnothing(Sys.which("xcrun")) + mac_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int + target = StaticCompiler.StaticTarget("aarch64-apple-macosx11.0.0"; toolchain = StaticCompiler.LLDToolchain(:darwin)) + exe = compile_executable(mac_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "mac_main"; target) + @test success(`file $exe`) && occursin("Mach-O 64-bit executable arm64", read(`file $exe`, String)) + @test run(ignorestatus(`$exe`)).exitcode == 42 + @test success(`codesign -v $exe`) # (`lld` signs it: arm64 macOS will not run one that is not signed) + lib = compile_shlib(mac_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "mac_lib"; target) + @test endswith(lib, ".dylib") && occursin("shared library arm64", read(`file $lib`, String)) + end +end From 26e12e2d4fed1c53cb3cfeaadb215208c6d64205 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Fri, 27 Nov 2026 23:54:53 -0500 Subject: [PATCH 192/202] Update Float16 conversions for use on older x86 where needed --- src/builtins.jl | 37 ++++++++++++++++++++++--------------- test/testcore.jl | 9 +++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/builtins.jl b/src/builtins.jl index 2dbe6011..588dc6b9 100644 --- a/src/builtins.jl +++ b/src/builtins.jl @@ -6,19 +6,6 @@ # a definition of the program's own, or of a library that is linked, wins, and the module has only those that it uses. # A toolchain asks for them with `builtins = true` (`LLDToolchain` does by default; a `CCToolchain` does not, as the compiler driver links its runtime library). -# Conversions between `Float16` and `Float32` that the code generator for x86-64 calls where the CPU is not said to have them: with the instructions that do them (F16C: from 2012 on). -const X86_HALF_BUILTINS = """ -define linkonce_odr hidden float @__extendhfsf2(half %a) #9999 { - %r = fpext half %a to float - ret float %r -} -define linkonce_odr hidden half @__truncsfhf2(float %a) #9999 { - %r = fptrunc float %a to half - ret half %r -} -attributes #9999 = { nounwind "target-features"="+f16c" } -""" - # 128-bit division (`__udivti3` and the others of libgcc and compiler-rt, which a Linux program is not linked with): by shifting and subtracting, with instructions of `i128` that are not calls # (a division of `i128` in them would be a call of themselves). const LINUX_INT128_BUILTINS = """ @@ -98,8 +85,10 @@ define linkonce_odr hidden i128 @__modti3(i128 %a, i128 %b) { } """ -# The same conversions for WebAssembly, which has no instruction for it. -# by integer arithmetic (the rounding is to the nearest, ties to even) +# Conversions between `Float16`, `Float32`, and `Float64` that the code generator calls where the CPU has no instruction for them. +# For example: WebAssembly has none, nor has generic/older x86-64 (F16C is from 2012 on, and Rosetta before macOS 15 does not have it) +# Insteade, we make the conversion by integer arithmetic, as compiler-rt definitions +# Rounding is to nearest, ties to even const WASM_HALF_BUILTINS = """ define linkonce_odr hidden float @__extendhfsf2(half %a) { %h = bitcast half %a to i16 @@ -220,6 +209,24 @@ define linkonce_odr hidden double @__extendhfdf2(half %a) { } """ +# The same, for the convention of x86-64: the `half` of the signatures and of the calls is an `i16`, and what is made of it by `bitcast` is the value itself. +function x86_half_builtins(text::AbstractString) + replacements = [ + "define linkonce_odr hidden float @__extendhfsf2(half %a) {\n %h = bitcast half %a to i16\n" => "define linkonce_odr hidden float @__extendhfsf2(i16 %h) {\n", + "define linkonce_odr hidden half @__truncsfhf2(float %a) {" => "define linkonce_odr hidden i16 @__truncsfhf2(float %a) {", + " %h = bitcast i16 %r16 to half\n ret half %h" => " ret i16 %r16", + "define linkonce_odr hidden half @__truncdfhf2(double %a) {" => "define linkonce_odr hidden i16 @__truncdfhf2(double %a) {", + " %h = call half @__truncsfhf2(float %rf)\n ret half %h" => " %h = call i16 @__truncsfhf2(float %rf)\n ret i16 %h", + "define linkonce_odr hidden double @__extendhfdf2(half %a) {\n %f = call float @__extendhfsf2(half %a)" => "define linkonce_odr hidden double @__extendhfdf2(i16 %a) {\n %f = call float @__extendhfsf2(i16 %a)", + ] + for (from, to) in replacements + occursin(from, text) || error("the text of the conversions of `Float16` has changed: `$from` is not in it") + text = replace(text, from => to) + end + return text +end +const X86_HALF_BUILTINS = x86_half_builtins(WASM_HALF_BUILTINS) + # The probe of the stack for x86-64 Windows (`___chkstk_ms` of libgcc and compiler-rt): the runtime of MinGW-w64 calls it itself (`pseudo-reloc.o`, which every program has), and the libraries that `Mingw_jll` # has do not have it. It touches each page of the stack that a function of a large frame is about to use, as the system's guard page needs. It is assembly, which the module has as its own (`module asm`). const MINGW_X64_BUILTINS = raw""" diff --git a/test/testcore.jl b/test/testcore.jl index 85dc8783..e1bcc8fc 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -286,6 +286,15 @@ end @test isempty(StaticCompiler.needed_builtins("", "x86_64-unknown-linux-gnu")) && occursin("___chkstk_ms:", StaticCompiler.add_builtins("", "x86_64-w64-windows-gnu")) @test StaticCompiler.needed_builtins(halfir, "x86_64-unknown-linux-gnu") == [:X86_HALF_BUILTINS] @test StaticCompiler.needed_builtins(halfir, "wasm64-unknown-unknown") == [:WASM_HALF_BUILTINS] + # (by integer arithmetic, with no instruction of F16C, which not every machine that runs an x86-64 program has; with the conversions of `Float64` too; and for x86-64 with the `i16` of the + # library calls of its code generator, where WebAssembly has `half`) + soft = StaticCompiler.add_builtins(halfir, "x86_64-unknown-linux-gnu") + added = soft[length(halfir)+1:end] + @test !occursin("f16c", soft) && !occursin(r"^[^;\n]*\bhalf\b"m, added) # (no `half` outside of the comments) + @test all(occursin(r"define linkonce_odr hidden \S+ @" * f * r"\(", added) for f in ("__extendhfsf2", "__truncsfhf2", "__truncdfhf2", "__extendhfdf2")) + @test occursin("define linkonce_odr hidden i16 @__truncsfhf2(float", added) && occursin("define linkonce_odr hidden float @__extendhfsf2(i16", added) + wasm = StaticCompiler.add_builtins(halfir, "wasm64-unknown-unknown") + @test occursin("define linkonce_odr hidden half @__truncsfhf2(float", wasm) && occursin("define linkonce_odr hidden float @__extendhfsf2(half", wasm) @test isempty(StaticCompiler.needed_builtins(halfir, "aarch64-unknown-linux-gnu")) # (the CPU converts) # (the same by the instructions of a module, which is not printed to be searched; and a name that says `half` is not a `half`) namedir = "define i32 @half(i32 %a) {\n %r = add i32 %a, 1\n ret i32 %r\n}\n" From 754e1fb9b6ddd7b9d93d8a690bad6703a51608ee Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sat, 28 Nov 2026 09:44:40 -0500 Subject: [PATCH 193/202] Update half builtins, add Windows int128 builtins --- src/builtins.jl | 34 +++++++++++++++++++++++++++------- test/testcore.jl | 20 ++++++++++++++------ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/builtins.jl b/src/builtins.jl index 588dc6b9..1e70895e 100644 --- a/src/builtins.jl +++ b/src/builtins.jl @@ -85,11 +85,27 @@ define linkonce_odr hidden i128 @__modti3(i128 %a, i128 %b) { } """ +# The same, for the convention of x86-64 Windows: the code generator calls `__udivti3` and the others with the addresses of the two operands (in `rcx` and `rdx`, as the C convention of Windows passes what is larger than a +# register), and reads the result from `xmm0`, as a vector of two integers of 64 bits (as libgcc, which has them, returns what `__int128` is). (Checked with `llc` of LLVM 23.) +function windows_int128_builtins(text::AbstractString) + for name in ("__udivti3", "__umodti3", "__divti3", "__modti3") + from = "define linkonce_odr hidden i128 @$name(i128 %a, i128 %b) {\n" + occursin(from, text) || error("the text of the division of 128 bits has changed: `$from` is not in it") + text = replace(text, from => "define linkonce_odr hidden <2 x i64> @$name(ptr %pa, ptr %pb) {\n %a = load i128, ptr %pa, align 8\n %b = load i128, ptr %pb, align 8\n") + end + returned = " ret i128 %r\n" + count(returned, text) == 4 || error("the text of the division of 128 bits has changed: it has not four functions that return `%r`") + return replace(text, returned => " %v = bitcast i128 %r to <2 x i64>\n ret <2 x i64> %v\n") +end +const WINDOWS_INT128_BUILTINS = windows_int128_builtins(LINUX_INT128_BUILTINS) + # Conversions between `Float16`, `Float32`, and `Float64` that the code generator calls where the CPU has no instruction for them. # For example: WebAssembly has none, nor has generic/older x86-64 (F16C is from 2012 on, and Rosetta before macOS 15 does not have it) -# Insteade, we make the conversion by integer arithmetic, as compiler-rt definitions +# Instead, we make the conversion by integer arithmetic, as compiler-rt definitions # Rounding is to nearest, ties to even -const WASM_HALF_BUILTINS = """ +# These are called with the convention of the *library calls* that the code generator makes, which varies by target. +# (Checked with `llc` of LLVM 23: the caller reads the result of `__truncsfhf2` from `xmm0`, and passes the argument of `__extendhfsf2` there.) +const HALF_BUILTINS = """ define linkonce_odr hidden float @__extendhfsf2(half %a) { %h = bitcast half %a to i16 %hi = zext i16 %h to i32 @@ -209,8 +225,10 @@ define linkonce_odr hidden double @__extendhfdf2(half %a) { } """ -# The same, for the convention of x86-64: the `half` of the signatures and of the calls is an `i16`, and what is made of it by `bitcast` is the value itself. -function x86_half_builtins(text::AbstractString) +# The same, for the convention of x86-64 macOS, where the library calls pass a `Float16` as an `i16` in an integer register +# The `half` of the signatures and of the calls is an `i16`, and what is made of it by `bitcast` is the value itself. +# (the argument in `edi`, the result in `ax`). +function macos_x86_half_builtins(text::AbstractString) replacements = [ "define linkonce_odr hidden float @__extendhfsf2(half %a) {\n %h = bitcast half %a to i16\n" => "define linkonce_odr hidden float @__extendhfsf2(i16 %h) {\n", "define linkonce_odr hidden half @__truncsfhf2(float %a) {" => "define linkonce_odr hidden i16 @__truncsfhf2(float %a) {", @@ -225,7 +243,7 @@ function x86_half_builtins(text::AbstractString) end return text end -const X86_HALF_BUILTINS = x86_half_builtins(WASM_HALF_BUILTINS) +const MACOS_X86_HALF_BUILTINS = macos_x86_half_builtins(HALF_BUILTINS) # The probe of the stack for x86-64 Windows (`___chkstk_ms` of libgcc and compiler-rt): the runtime of MinGW-w64 calls it itself (`pseudo-reloc.o`, which every program has), and the libraries that `Mingw_jll` # has do not have it. It touches each page of the stack that a function of a large frame is about to use, as the system's guard page needs. It is assembly, which the module has as its own (`module asm`). @@ -257,9 +275,11 @@ module asm " ret" function needed_builtins(has_half::Bool, has_i128div::Bool, triple::AbstractString) family = llvm_family(first(split(triple, '-'))) needed = Symbol[] - family == "x86" && startswith(triple, "x86_64") && has_half && push!(needed, :X86_HALF_BUILTINS) - family == "wasm" && has_half && push!(needed, :WASM_HALF_BUILTINS) + x86_64 = family == "x86" && startswith(triple, "x86_64") + x86_64 && has_half && push!(needed, occursin(r"apple|darwin|macos", triple) ? :MACOS_X86_HALF_BUILTINS : :HALF_BUILTINS) + family == "wasm" && has_half && push!(needed, :HALF_BUILTINS) occursin("linux", triple) && has_i128div && push!(needed, :LINUX_INT128_BUILTINS) + x86_64 && occursin(r"windows|mingw", triple) && has_i128div && push!(needed, :WINDOWS_INT128_BUILTINS) # (the runtime of MinGW-w64 needs the probe of the stack whatever the program is) startswith(triple, "x86_64") && occursin(r"windows|mingw", triple) && push!(needed, :MINGW_X64_BUILTINS) return needed diff --git a/test/testcore.jl b/test/testcore.jl index e1bcc8fc..0cfd8bfb 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -284,24 +284,32 @@ end @test isempty(StaticCompiler.needed_builtins(i128ir, "aarch64-apple-macosx11.0.0")) # (macOS has them in its C library) @test StaticCompiler.needed_builtins("", "x86_64-w64-windows-gnu") == [:MINGW_X64_BUILTINS] && StaticCompiler.needed_builtins("", "x86_64-w64-mingw32") == [:MINGW_X64_BUILTINS] # (the runtime of MinGW has to have the probe of the stack) @test isempty(StaticCompiler.needed_builtins("", "x86_64-unknown-linux-gnu")) && occursin("___chkstk_ms:", StaticCompiler.add_builtins("", "x86_64-w64-windows-gnu")) - @test StaticCompiler.needed_builtins(halfir, "x86_64-unknown-linux-gnu") == [:X86_HALF_BUILTINS] - @test StaticCompiler.needed_builtins(halfir, "wasm64-unknown-unknown") == [:WASM_HALF_BUILTINS] + @test StaticCompiler.needed_builtins(halfir, "x86_64-unknown-linux-gnu") == [:HALF_BUILTINS] && StaticCompiler.needed_builtins(halfir, "wasm64-unknown-unknown") == [:HALF_BUILTINS] + @test StaticCompiler.needed_builtins(halfir, "x86_64-w64-windows-gnu") == [:HALF_BUILTINS, :MINGW_X64_BUILTINS] # (the convention of `xmm0`: the one of the IR with `half`, as on Linux) + @test StaticCompiler.needed_builtins(halfir, "x86_64-apple-macosx11.0.0") == [:MACOS_X86_HALF_BUILTINS] # (the one of Apple: an `i16` in an integer register) + @test StaticCompiler.needed_builtins(i128ir, "x86_64-w64-windows-gnu") == [:WINDOWS_INT128_BUILTINS, :MINGW_X64_BUILTINS] # (by integer arithmetic, with no instruction of F16C, which not every machine that runs an x86-64 program has; with the conversions of `Float64` too; and for x86-64 with the `i16` of the # library calls of its code generator, where WebAssembly has `half`) - soft = StaticCompiler.add_builtins(halfir, "x86_64-unknown-linux-gnu") + soft = StaticCompiler.add_builtins(halfir, "x86_64-apple-macosx11.0.0") added = soft[length(halfir)+1:end] @test !occursin("f16c", soft) && !occursin(r"^[^;\n]*\bhalf\b"m, added) # (no `half` outside of the comments) @test all(occursin(r"define linkonce_odr hidden \S+ @" * f * r"\(", added) for f in ("__extendhfsf2", "__truncsfhf2", "__truncdfhf2", "__extendhfdf2")) @test occursin("define linkonce_odr hidden i16 @__truncsfhf2(float", added) && occursin("define linkonce_odr hidden float @__extendhfsf2(i16", added) - wasm = StaticCompiler.add_builtins(halfir, "wasm64-unknown-unknown") - @test occursin("define linkonce_odr hidden half @__truncsfhf2(float", wasm) && occursin("define linkonce_odr hidden float @__extendhfsf2(half", wasm) + for triple in ("wasm64-unknown-unknown", "x86_64-unknown-linux-gnu", "x86_64-w64-windows-gnu") + wasm = StaticCompiler.add_builtins(halfir, triple) + @test occursin("define linkonce_odr hidden half @__truncsfhf2(float", wasm) && occursin("define linkonce_odr hidden float @__extendhfsf2(half", wasm) && !occursin("f16c", wasm) + end + # (128-bit division for Windows: the operands by address, and the result a vector of two integers of 64 bits; for Linux the integers themselves) + windows = StaticCompiler.add_builtins(i128ir, "x86_64-w64-windows-gnu") + @test all(occursin("define linkonce_odr hidden <2 x i64> @$f(ptr %pa, ptr %pb)", windows) for f in ("__udivti3", "__umodti3", "__divti3", "__modti3")) + @test occursin("define linkonce_odr hidden i128 @__udivti3(i128 %a, i128 %b)", StaticCompiler.add_builtins(i128ir, "x86_64-unknown-linux-gnu")) @test isempty(StaticCompiler.needed_builtins(halfir, "aarch64-unknown-linux-gnu")) # (the CPU converts) # (the same by the instructions of a module, which is not printed to be searched; and a name that says `half` is not a `half`) namedir = "define i32 @half(i32 %a) {\n %r = add i32 %a, 1\n ret i32 %r\n}\n" LLVM.@dispose ctx = LLVM.Context() begin needed(ir, triple) = (mod = parse(LLVM.Module, ir); try; StaticCompiler.needed_builtins(mod, triple); finally; LLVM.dispose(mod); end) @test needed(i128ir, "aarch64-unknown-linux-gnu") == [:LINUX_INT128_BUILTINS] && isempty(needed(i128ir, "aarch64-apple-macosx11.0.0")) - @test needed(halfir, "x86_64-unknown-linux-gnu") == [:X86_HALF_BUILTINS] && needed(halfir, "wasm64-unknown-unknown") == [:WASM_HALF_BUILTINS] + @test needed(halfir, "x86_64-unknown-linux-gnu") == [:HALF_BUILTINS] && needed(halfir, "wasm64-unknown-unknown") == [:HALF_BUILTINS] && needed(halfir, "x86_64-apple-macosx11.0.0") == [:MACOS_X86_HALF_BUILTINS] @test isempty(needed(halfir, "aarch64-unknown-linux-gnu")) && isempty(needed(namedir, "wasm64-unknown-unknown")) @test needed("", "x86_64-w64-windows-gnu") == [:MINGW_X64_BUILTINS] end From 708acc3cc04cee29f459a7b8767820c9f78594c5 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sat, 28 Nov 2026 17:49:08 -0500 Subject: [PATCH 194/202] Attempt to get docs working, update CI --- .github/workflows/ci-integration-nightly.yml | 2 +- .github/workflows/ci-integration.yml | 2 +- .github/workflows/ci-julia-nightly.yml | 1 + .github/workflows/ci.yml | 2 +- .github/workflows/cross-matrix.yml | 6 +- .github/workflows/docs.yml | 32 +++++ .github/workflows/docsyml_backup | 32 ----- .gitignore | 2 + docs/Project.toml | 8 +- docs/make.jl | 31 +++-- docs/src/api.md | 64 +++++++++ docs/src/backend.md | 10 -- docs/src/helpers.md | 0 docs/src/index.md | 130 +++++++++++++++++++ src/StaticCompiler.jl | 23 ++++ src/llvm.jl | 12 +- src/quirks.jl | 6 + 17 files changed, 299 insertions(+), 64 deletions(-) create mode 100644 .github/workflows/docs.yml delete mode 100644 .github/workflows/docsyml_backup create mode 100644 docs/src/api.md delete mode 100644 docs/src/backend.md delete mode 100644 docs/src/helpers.md diff --git a/.github/workflows/ci-integration-nightly.yml b/.github/workflows/ci-integration-nightly.yml index 00074acc..8f8f01a6 100644 --- a/.github/workflows/ci-integration-nightly.yml +++ b/.github/workflows/ci-integration-nightly.yml @@ -19,9 +19,9 @@ jobs: matrix: version: - 'nightly' + - 'pre' os: - ubuntu-latest - - macOS-latest arch: - x64 group: diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index 7b0f141e..257a89d1 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -19,7 +19,7 @@ jobs: matrix: version: - '1.11' - - '1.12' + - '1' os: - ubuntu-latest - macOS-latest diff --git a/.github/workflows/ci-julia-nightly.yml b/.github/workflows/ci-julia-nightly.yml index 4cdbcfa8..2e9d6740 100644 --- a/.github/workflows/ci-julia-nightly.yml +++ b/.github/workflows/ci-julia-nightly.yml @@ -19,6 +19,7 @@ jobs: matrix: version: - 'nightly' + - 'pre' os: - ubuntu-latest arch: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 55a3952d..49bf043e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: matrix: version: - '1.11' - - '1.12' + - '1' os: - ubuntu-latest - macOS-latest diff --git a/.github/workflows/cross-matrix.yml b/.github/workflows/cross-matrix.yml index 31b3453d..1620931f 100644 --- a/.github/workflows/cross-matrix.yml +++ b/.github/workflows/cross-matrix.yml @@ -1,4 +1,4 @@ -name: Cross-compiling matrix +name: Cross-compilation matrix # What can be made from each kind of host, for each target (test/cross/matrix.jl, a table in the summary of each job), and whether what was made runs on a machine of its target (test/cross/run.jl). # The hosts: Linux (x86-64 and AArch64), macOS (Apple Silicon) and Windows. The targets: Linux, macOS, Windows (MinGW-w64) and WebAssembly. A host makes what Julia's LLVM has the code generator for (its own architecture, # and WebAssembly), and what it has the libraries for: those of Linux and Windows are fetched (Glibc_jll, Mingw_jll); the SDK of macOS is Xcode's, or one that is downloaded (phracker/MacOSX-SDKs) on a host that is not a Mac (Linux and Windows). @@ -28,7 +28,7 @@ jobs: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 with: - version: '1.12' + version: '1' - uses: julia-actions/cache@v2 - name: An SDK of macOS, for the programs for macOS that are made on Linux and on Windows if: runner.os != 'macOS' @@ -72,7 +72,7 @@ jobs: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 with: - version: '1.12' + version: '1' - uses: actions/download-artifact@v4 with: pattern: made-* diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 00000000..8401f688 --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,32 @@ +name: Documentation +on: + push: + branches: + - master + tags: '*' + paths-ignore: + - '.github/workflows/cross-matrix.yml' + pull_request: + branches: + - master +jobs: + docs: + name: Build and deploy + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: read + statuses: write + steps: + - uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v2 + with: + version: '1' + - uses: julia-actions/cache@v2 + - name: Install dependencies + run: julia --project=docs -e 'using Pkg; Pkg.instantiate()' + - name: Build and deploy + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} + run: julia --project=docs --color=yes docs/make.jl diff --git a/.github/workflows/docsyml_backup b/.github/workflows/docsyml_backup deleted file mode 100644 index f1693494..00000000 --- a/.github/workflows/docsyml_backup +++ /dev/null @@ -1,32 +0,0 @@ -name: Documentation - -on: - push: - branches: - - 'master' - - 'release-' - tags: '*' - pull_request: - -jobs: - build: - runs-on: ${{ matrix.os }} - strategy: - matrix: - julia-version: [1.3.1] - julia-arch: [x64] - os: [ubuntu-latest] - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@latest - with: - version: ${{ matrix.julia-version }} - - name : Add current package # for unregistered package - run: julia -e 'using Pkg; Pkg.develop(PackageSpec(; path=pwd()));' - - name: Install dependencies - run: julia --project=docs -e 'using Pkg; Pkg.instantiate()' - - name: Build and deploy - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} - run: julia --project=docs --color=yes docs/make.jl diff --git a/.gitignore b/.gitignore index 7d7ea3bc..30c8380e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ test.o test.so test.bc /standalone/* +/docs/src/index.md +/docs/build/ diff --git a/docs/Project.toml b/docs/Project.toml index c96aeff2..d96ab258 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,3 +1,9 @@ [deps] Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" -#StaticCompiler = "81625895-6c0f-48fc-b932-11a18313743c" +StaticCompiler = "81625895-6c0f-48fc-b932-11a18313743c" + +[sources] +StaticCompiler = {path = ".."} + +[compat] +Documenter = "1" diff --git a/docs/make.jl b/docs/make.jl index 34ebbac2..ece96ce4 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,23 +1,32 @@ using StaticCompiler using Documenter +DocMeta.setdocmeta!(StaticCompiler, :DocTestSetup, :(using StaticCompiler); recursive = true) + +# The home page is the README (so that the two are one text), with the links to files of the repository made absolute +readme = read(joinpath(@__DIR__, "..", "README.md"), String) +readme = replace(readme, r"\]\((?!https?://|#|mailto:)([^)\s]+)\)" => s"](https://github.com/tshort/StaticCompiler.jl/blob/master/\1)") +write(joinpath(@__DIR__, "src", "index.md"), readme) + makedocs(; - modules=[StaticCompiler], - authors="Tom Short", - repo="https://github.com/tshort/StaticCompiler.jl/blob/{commit}{path}#L{line}", - sitename="StaticCompiler.jl", - format=Documenter.HTML(; + modules = [StaticCompiler], + authors = "Tom Short and contributors", + repo = Documenter.Remotes.GitHub("tshort", "StaticCompiler.jl"), + sitename = "StaticCompiler.jl", + format = Documenter.HTML(; prettyurls = get(ENV, "CI", nothing) == "true", - # canonical="https://tshort.github.io/StaticCompiler.jl", - # assets=String[], + canonical = "https://tshort.github.io/StaticCompiler.jl", + edit_link = "master", ), - pages=[ + pages = [ "Home" => "index.md", - "Backend Syntax Reference" => "backend.md", - "Helpers Syntax Reference" => "helpers.md", + "API reference" => "api.md", ], + # (not every internal function has a docstring, and the README has links that are for GitHub) + warnonly = [:missing_docs, :cross_references, :autodocs_block], ) deploydocs(; - repo="github.com/tshort/StaticCompiler.jl", + repo = "github.com/tshort/StaticCompiler.jl", + devbranch = "master", ) diff --git a/docs/src/api.md b/docs/src/api.md new file mode 100644 index 00000000..5daa40fb --- /dev/null +++ b/docs/src/api.md @@ -0,0 +1,64 @@ +# API reference + +## Compiling + +```@docs +compile_executable +compile_shlib +``` + +## Inspecting the code + +```@docs +static_code_llvm +static_code_typed +static_llvm_module +static_code_native +``` + +## Method overlays + +```@docs +@device_override +@print_and_throw +``` + +## Targets and toolchains + +```@docs +StaticTarget +CCToolchain +LLDToolchain +``` + +## Building blocks + +The steps that `compile_executable` and `compile_shlib` are made of, for tools that need part of them. + +```@docs +StaticCompiler.generate_obj +StaticCompiler.retarget! +StaticCompiler.generate_ir +StaticCompiler.generate_executable +StaticCompiler.generate_shlib +StaticCompiler.generate_shlib_fptr +StaticCompiler.emit_object +StaticCompiler.link +StaticCompiler.output_name +StaticCompiler.add_builtins +StaticCompiler.add_builtins! +``` + +## Toolchains and sysroots + +```@docs +StaticCompiler.Toolchain +StaticCompiler.Machine +StaticCompiler.set_toolchain! +StaticCompiler.set_compiler! +StaticCompiler.set_runtime! +StaticCompiler.glibc_sysroot +StaticCompiler.glibc_version +StaticCompiler.macos_sdk +StaticCompiler.mingw_sysroot +``` diff --git a/docs/src/backend.md b/docs/src/backend.md deleted file mode 100644 index 1e08a76c..00000000 --- a/docs/src/backend.md +++ /dev/null @@ -1,10 +0,0 @@ -# Backend Syntax Reference -```@index -Modules = [StaticCompiler] -Pages = ["backend.md"] -``` - -```@autodocs -Modules = [StaticCompiler] -Pages = readdir("../src") -``` \ No newline at end of file diff --git a/docs/src/helpers.md b/docs/src/helpers.md deleted file mode 100644 index e69de29b..00000000 diff --git a/docs/src/index.md b/docs/src/index.md index e69de29b..a917cafc 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -0,0 +1,130 @@ +# StaticCompiler + +[![CI](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml) +[![CI (Integration)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration.yml) +[![CI (Julia nightly)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Julia%20nightly)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-julia-nightly.yml) +[![CI (Integration nightly)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration-nightly.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration-nightly.yml) +[![Coverage](https://codecov.io/gh/tshort/StaticCompiler.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/tshort/StaticCompiler.jl) + +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 +```julia +using Pkg +Pkg.add("StaticCompiler") +``` + +## Approach + +This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. + +### Standalone compilation +StaticCompiler.jl provides the functions `compile_executable` and `compile_shlib` for compiling a Julia function to a native executable or shared library for use from outside of Julia: +```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 approach comes with substantial limitations compared to regular julia code, as you cannot rely on julia's runtime, `libjulia` (see, e.g., [StaticTools.jl](https://github.com/brenhinkeller/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 for example, to cross-compile to other targets. + +### Method overlays + +Sometimes, a julia function you want to statically compile will do things (such as throwing errors) that aren't supported natively by StaticCompiler. One tool provided for working around this is the `@device_override` macro which lets you swap out a method, but only inside of a StaticCompiler.jl compilation context. For example: + +```julia +julia> using Libdl, StaticCompiler + +julia> f(x) = g(x) + 1; + +julia> g(x) = 2x + +julia> @device_override g(x::Int) = x - 10 + +julia> f(1) # Gives the expected answer in regular julia +3 + +julia> dlopen(compile_shlib(f, (Int,), "./")) do lib + fptr = dlsym(lib, "f") + # Now use the compiled version where g(x) = 2x is replaced with g(x) = x - 10 + @ccall $fptr(1::Int)::Int + end +-8 +``` +Typically, errors should be overrided and replaced with `@print_and_throw`, which is StaticCompiler friendly, i.e. +we define overrides such as +``` julia +@device_override @noinline Base.Math.throw_complex_domainerror(f::Symbol, x) = + @print_and_throw c"This operation requires a complex input to return a complex result" +``` + +If for some reason, you wish to use a different method table (defined with `Base.Experimental.@MethodTable` and `Base.Experimental.@overlay`) than the default one provided by StaticCompiler.jl, you can provide it to `compile_executable` and `compile_shlib` via a keyword argument `method_table`. + +`StaticCompiler.empty_table` is available as a method table with no overrides at all, if you do not wish to use StaticCompiler's `@device_override`s.Your own overrides can be added to a table of your own, or to this one, with `Base.Experimental.@overlay(StaticCompiler.empty_table, ...)`. + +### Cross-compilation + +`compile_executable` and `compile_shlib` accept a `target`, so a program can be made on one system for another: + +```julia +julia> using StaticCompiler, Glibc_jll, Pkg # (Glibc_jll and Pkg fetch the C library of a Linux target; Mingw_jll does the same for Windows) + +julia> target = StaticTarget("aarch64-unknown-linux-gnu"; retarget = true, toolchain = LLDToolchain(:gnu; sysroot = StaticCompiler.glibc_sysroot("aarch64-unknown-linux-gnu"))); + +julia> compile_executable(hello, (), "./", "hello"; filename = "hello_aarch64", target) +``` + +The toolchain can be either a [`CCToolchain`](https://github.com/tshort/StaticCompiler.jl/blob/master/@ref) specifying a C compiler, or an [`LLDToolchain`](https://github.com/tshort/StaticCompiler.jl/blob/master/@ref) object specifying an LLVM `lld` to be used (flavors include `:gnu` for Linux, `:darwin` for macOS, `:mingw` for Windows, and `:wasm` for WebAssembly). Julia's LLVM contains the code generators for its own architecture and for WebAssembly only. For any other architecture, the IR can be made (`emit_llvm_only = true`), and `StaticTarget(...; llc = "/path/to/llc")` makes the object with an `llc` that has the desired backend for the same LLVM version. + +The [`cross-matrix.yml`](https://github.com/tshort/StaticCompiler.jl/blob/master/.github/workflows/cross-matrix.yml) CI workfoow tests cross-compilation to and from Linux (x86-64 and AArch64), macOS, and Windows. + +## Limitations + +* 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](https://github.com/tshort/StaticCompiler.jl/blob/master/src/quirks.jl)). +* Type instability. Type unstable code cannot currently be statically compiled via this package. +* Extra experimental on Windows (PRs welcome if you encounter issues). Should work in WSL on Windows 10+. + +## Guide for Package Authors + +To enable code to be statically compiled, consider the following: + +* Use type-stable code. + +* Use Tuples, NamedTuples, StaticArrays, and other types where appropriate. These allocate on the stack and don't use Julia's heap allocation. + +* Avoid Julia's internal allocations. That means don't bake in use of Arrays or Strings or Dicts. Types from StaticTools can help, like StaticStrings and MallocArrays. + +* If need be, manage memory manually, using `malloc` and `free` from StaticTools.jl. This works with `StaticTools.MallocString` and `StaticTools.MallocArray`, or use [Bumper.jl](https://github.com/MasonProtter/Bumper.jl). + +* Don't use global variables that need to be allocated and initialized. Instead of global variables, use context structures that have an initialization function. It is okay to use global Tuples or NamedTuples as the use of these should be baked into compiled code. + +* Use context variables to store program state, inputs, and outputs. Parameterize these typese as needed, so your code can handle normal types (Arrays) and static-friendly types (StaticArrays, MallocArrays, or StrideArrays). The SciML ecosystem does this well ([example](https://github.com/SciML/OrdinaryDiffEq.jl/blob/e7f045950615352ddfcb126d13d92afd2bad05e4/src/integrators/type.jl#L82)). Use of these context variables also enables allocations and initialization to be centralized, so these could be managed by the calling routines in Julia, Python, JavaScript, or other language. + +* Arguments and returned values from `compile_shlib` must be native objects such as `Int`, `Float64`, or `Ptr`. They cannot be things like `Tuple{Int, Int}` because that is not natively sized. Such objects need to be passed by reference instead of by value. + +* If your code needs an array as a workspace, instead of directly creating it, create it as a function argument (where it could default to a standard array creation). That code could be statically compiled if that function argument is changed to a MallocArray or another static-friendly alternative. + +## Guide for Statically Compiling Code + +If you're trying to statically compile generic code, you may run into issues if that code uses features not supported by StaticCompiler. One option is to change the code you're calling using the tips above. If that is not easy, you may by able to compile it anyway. One option is to use method overlays to change what methods are called. + +[Cthulhu](https://github.com/JuliaDebug/Cthulhu.jl) is a great help in digging into code, finding type instabilities, and finding other sources of code that may break static compilation. + +## Foreign Function Interfacing + +Because Julia objects follow C memory layouts, compiled libraries should be usable from most languages that can interface with C. For example, results should be usable with Python's [CFFI](https://cffi.readthedocs.io/en/latest/) package. + +For WebAssembly, interface helpers are available at [WebAssemblyInterfaces](https://github.com/tshort/WebAssemblyInterfaces.jl), and users should also see [WebAssemblyCompiler](https://github.com/tshort/WebAssemblyCompiler.jl) for a package more focused on compilation of WebAssebly in general. diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index fbf35081..a850e670 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -387,16 +387,39 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file end # (`llvm_postprocess` is accepted and ignored by the three `static_code_*` functions: wrappers, like StaticRuntime's, pass the same keywords to all of the functions that they wrap) +""" +```julia +static_code_llvm(f, types; target::StaticTarget=StaticTarget(), kwargs...) +``` +Print the LLVM IR that `f` compiles to for the argument types `types`, as the compiler makes it for the +`target`: what [`compile_executable`](@ref) and [`compile_shlib`](@ref) would write, before linking +(with the method overlays of this package, and without the code of libraries that the function calls). +""" function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) job = static_job(func, types; target, kwargs...) GPUCompiler.code_llvm(stdout, job; libraries=false) end +""" +```julia +static_code_typed(f, types; target::StaticTarget=StaticTarget(), kwargs...) +``` +The typed Julia code of `f` for the argument types `types`, as `code_typed` of Julia gives it, but as the +compiler for the `target` sees it, with the method overlays of this package: a vector of +`CodeInfo => return type` pairs. +""" function static_code_typed(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) job = static_job(func, types; target, kwargs...) GPUCompiler.code_typed(job) end +""" +```julia +static_code_native(f, tt, fname=fix_name(f); target::StaticTarget=StaticTarget(), kwargs...) +``` +Print the assembly that `f` compiles to for the argument types `tt` for the `target`, as the code +generator of LLVM makes it. +""" function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) job = static_job(f, tt; name=fname, target, kwargs...) GPUCompiler.code_native(stdout, job; libraries=false) diff --git a/src/llvm.jl b/src/llvm.jl index 2baa3f72..cdfdcbd1 100644 --- a/src/llvm.jl +++ b/src/llvm.jl @@ -55,10 +55,14 @@ data_warnings(inst, frames) = for frame ∈ frames inlined = inlined) end -# The IR of a target that is retargeted (`is_retargeted(target)`) gets the triple of the target, -# no data layout (the code generator that makes the object has the target's), -# and no attributes that name the host's CPU and features. -# IR that has intrinsics of the host's architecture is refused. +""" + retarget!(mod::LLVM.Module, target::StaticTarget) + +Make the IR of a target that is retargeted (`is_retargeted(target)`: code that was made for the host's machine, for +another) the IR of that target: it gets the triple of the target, no data layout (the code generator that makes +the object has the target's), and no attributes that name the host's CPU and features. IR that has intrinsics of +the host's architecture is refused. +""" function retarget!(mod::LLVM.Module, target::StaticTarget) is_retargeted(target) || return mod triple = target_triple(target) diff --git a/src/quirks.jl b/src/quirks.jl index 73eae93d..23d2059e 100644 --- a/src/quirks.jl +++ b/src/quirks.jl @@ -20,6 +20,12 @@ macro device_override(ex) return esc(code) end +""" + @print_and_throw message + +Print `message` (a C string, `c"..."`) and stop the program with exit status 1: what the replacements of +the methods that throw (see [`@device_override`](@ref)) do where a program has no exception to throw. +""" macro print_and_throw(err) quote println($err) From d9f78f3a01d831cb72b9e55fc344d09298a81902 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Sat, 28 Nov 2026 22:31:17 -0500 Subject: [PATCH 195/202] Add `llvm_finalize` hook, cleanup kwargs and docstrings --- src/StaticCompiler.jl | 229 +++++++++++++++++++++++------------------- test/testcore.jl | 28 ++++++ 2 files changed, 151 insertions(+), 106 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index a850e670..52bd0380 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -39,14 +39,17 @@ end """ ```julia compile_executable(f::Function, types::Tuple, [path::String=pwd()], [name::String=string(nameof(f))]; - filename::String=name, - also_expose = [], - target::StaticTarget=StaticTarget(), + filename::String = name, + also_expose = Tuple{Function, Tuple{DataType}}[], + target = StaticTarget(), # The target for compilation, including OS and architecture as well as other options + demangle = true, # Whether or not function names should be exactly as they are in Julia + llvm_to_clang = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR cc = nothing, # Compiler, may specify to override defaults - cflags = ``, # Specify libraries you would like to link against, and other compiler options here - llvm_to_clang = needs_ir(target), # default: whether the target needs it (Windows, or cross-target absent from Julia's LLVM) + cflags = ``, # Compiler flags, may specify to override defaults + llvm_postprocess = nothing, # optional hook: called on the each function's module in turn + llvm_finalize = nothing, # optional hook: called on the final combined module when it is complete + main_prologue = nothing, # optional hook: adds code that runs first in `main`, into which `f` will then be wrapped method_table = StaticCompiler.method_table, - llvm_postprocess = nothing, kwargs... ) @@ -55,9 +58,14 @@ Attempt to compile a standalone executable that runs function `f` with a type si If there are extra methods you would like to protect from name mangling in the produced binary for whatever reason, you can provide them as a vector of tuples of functions and types, i.e. `[(f1, types1), (f2, types2), ...]` -The `llvm_postprocess` keyword, if given, allows users to specify a function that will be called on each `LLVM.Module` -that is generated, before it is checked and compiled, and may modify it in place -(for instance, to define the runtime functions that the code calls). +There are two hooks that may modify the LLVM code, which differ in when they run: +* `llvm_postprocess(mod)` is called on the `LLVM.Module` of each function as soon as it is generated, before its pointers and runtime calls are located, it is retargeted, and the modules are combined. It may modify it in place + (for instance, to define the runtime functions that the code calls). +* `llvm_finalize(mod)` is called once, on the whole module when it is complete: after the modules are combined and the `main` (see `main_prologue`) and the builtins of the toolchain are added, and just before the code is + emitted. Use it for what has to see all of that (see [`generate_obj`](@ref)). + +`main_prologue(builder, mod)` adds code that runs first in the `main` of the program (the object then has its own `main`, also where a C compiler links it). `llvm_finalize` and `main_prologue` work the same for any target +and any toolchain (the host's or another, linked by a C compiler driver or by `lld`). Objects to link with the program can be given in `cflags`. Other keywords will be forwarded to `static_job`. This may include `params`, which if given, is a `GPUCompiler.AbstractCompilerParams` object (default `StaticCompilerParams()`) which @@ -119,18 +127,22 @@ Hello, world! ``` """ function compile_executable(f::Function, types=(), path::String=pwd(), name=fix_name(f); - also_expose=Tuple{Function, Tuple{DataType}}[], - kwargs...) + also_expose::Vector = Tuple{Function, Tuple{DataType}}[], + kwargs... + ) compile_executable(vcat([(f, types)], also_expose), path, name; kwargs...) end function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name=fix_name(first(first(funcs))); filename::String = name, - target::StaticTarget=StaticTarget(), - demangle = true, - cc = nothing, - cflags = ``, - llvm_to_clang = needs_ir(target), + target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options + demangle::Bool = true, # Whether or not function names should be exactly as they are in Julia + llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Compiler flags, may specify to override defaults + llvm_postprocess = nothing, # optional hook: called on the each function's module in turn + llvm_finalize = nothing, # optional hook: called on the final combined module when it is complete + main_prologue = nothing, # optional hook: adds code that runs first in `main`, into which `f` will then be wrapped kwargs... ) @@ -139,30 +151,24 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name= isexecutableargtype = tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" - generate_executable(funcs, path, name, filename; target, demangle, cc, cflags, llvm_to_clang, kwargs...) + generate_executable(funcs, path, name, filename; target, demangle, cc, cflags, llvm_to_clang, llvm_postprocess, llvm_finalize, main_prologue, kwargs...) joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : CCToolchain(cc), target, filename)) end """ ```julia -compile_shlib(f::Function, types::Tuple, [path::String=pwd()], [name::String=string(nameof(f))]; - filename::String=name, - target::StaticTarget=StaticTarget(), - demangle=true, - cc = nothing, # Compiler, may specify to override defaults - cflags = ``, # Specify libraries you would like to link against, and other compiler options here - llvm_to_clang = needs_ir(target), # default: whether the target needs it (Windows, or cross-target absent from Julia's LLVM) - method_table = StaticCompiler.method_table, - kwargs...) - +compile_shlib(f::Function, types::Tuple, [path::String=pwd()], [name::String=string(nameof(f))]; kwargs...) compile_shlib(funcs::Array, [path::String=pwd()]; filename="libfoo", - target::StaticTarget=StaticTarget(), - cc = nothing, # Compiler, may specify to override defaults - cflags = ``, # Specify libraries you would like to link against, and other compiler options here - llvm_to_clang = needs_ir(target), # default: whether the target needs it (Windows, or cross-target absent from Julia's LLVM) - method_table = StaticCompiler.method_table, - kwargs...) + target = StaticTarget(), # The target for compilation, including OS and architecture as well as other options + demangle = true, # Whether or not function names should be exactly as they are in Julia + llvm_to_clang = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Compiler flags, may specify to override defaults + llvm_postprocess = nothing, # optional hook: called on the each function's module in turn + llvm_finalize = nothing, # optional hook: called on the final combined module when it is complete + kwargs... +) ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. @@ -202,18 +208,20 @@ end # As above, but taking an array of functions and returning a single shlib function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); filename = "libfoo", - target::StaticTarget=StaticTarget(), - demangle = true, - cc = nothing, - cflags = ``, - llvm_to_clang = needs_ir(target), + target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options + demangle::Bool = true, # Whether or not function names should be exactly as they are in Julia + llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Compiler flags, may specify to override defaults + llvm_postprocess = nothing, # optional hook: called on the each function's module in turn + llvm_finalize = nothing, # optional hook: called on the final combined module when it is complete kwargs... ) for (f, types) in funcs check_signature(f, types; target, kwargs...) end - generate_shlib(funcs, path, filename; target, demangle, cc, cflags, llvm_to_clang, kwargs...) + generate_shlib(funcs, path, filename; target, demangle, cc, cflags, llvm_to_clang, llvm_postprocess, llvm_finalize, kwargs...) joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : CCToolchain(cc), target, filename; kind = :shared)) end @@ -265,9 +273,9 @@ end # As above, but also compile (maybe remove this method in the future?) function generate_shlib_fptr(f, tt, path::String=tempname(), name=fix_name(f), filename::String=name; - temp::Bool=true, - kwargs...) - + temp::Bool=true, + kwargs... + ) generate_shlib(f, tt, path, name, filename; kwargs...) fptr = generate_shlib_fptr(path, name, filename) if temp @@ -278,10 +286,13 @@ end """ ```julia -generate_executable(f, tt, path::String, name, filename=string(name); kwargs...) +generate_executable(f, tt, path::String, name, filename=string(name); + target::StaticTarget=StaticTarget(), demangle=true, cc=nothing, cflags=``, llvm_to_clang=needs_ir(target), + llvm_postprocess=nothing, llvm_finalize=nothing, main_prologue=nothing, kwargs...) ``` Attempt to compile a standalone executable that runs `f`. -Low-level interface; you should generally use `compile_executable` instead. +Low-level interface; you should generally use `compile_executable` instead. The keywords are those of `compile_executable`; the hooks are passed to [`generate_obj`](@ref). With a `main_prologue` the object has its own `main`, +whichever the toolchain. ### Examples ```julia @@ -298,24 +309,28 @@ Hello, world! """ generate_executable(f, tt, args...; kwargs...) = generate_executable(((f, tt),), args...; kwargs...) function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fix_name(first(first(funcs))), filename=name; - target::StaticTarget=StaticTarget(), - demangle = true, - cc = nothing, - cflags = ``, - llvm_to_clang::Bool = needs_ir(target), - kwargs... - ) + target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options + demangle::Bool = true, # Whether or not function names should be exactly as they are in Julia + llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Compiler flags, may specify to override defaults + llvm_postprocess = nothing, # optional hook: called on the each function's module in turn + llvm_finalize = nothing, # optional hook: called on the final combined module when it is complete + main_prologue = nothing, # optional hook: adds code that runs first in `main`, into which `f` will then be wrapped + kwargs... + ) # How it is linked: what `cc` says, or the toolchain of the target tc = isnothing(cc) ? toolchain(target) : CCToolchain(cc) - # Prepare objects or (if llvm_to_clang is set) IR. A linker that is not a compiler driver is given an object that has a `main` + # Prepare objects or (if llvm_to_clang is set) IR. A linker that is not a compiler driver is given an object that has a `main`, and so is a driver when + # `main_prologue` asks for code to run first, which the `main` of the object has (the driver would be given a `main` of its own, with nothing before the entry point) fn = demangle ? "$name" : "julia_$name" - main_wrapper = tc isa CCToolchain ? nothing : fn - _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, main_wrapper, kwargs...) + main_wrapper = !(tc isa CCToolchain) || !isnothing(main_prologue) ? fn : nothing + _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...) # Compile and/or link executable! exec_path = joinpath(path, output_name(tc, target, filename)) - link(tc, target, [obj_or_ir_path], exec_path; kind = :executable, entry = fn, cflags) + link(tc, target, [obj_or_ir_path], exec_path; kind = :executable, entry = isnothing(main_wrapper) ? fn : nothing, cflags) return path, name end @@ -323,7 +338,16 @@ end """ ```julia generate_shlib(f::Function, tt, [path::String], [name], [filename]; kwargs...) -generate_shlib(funcs::Array, [path::String], [filename::String]; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) +generate_shlib(funcs::Array, [path::String], [filename::String]; + target::StaticTarget=StaticTarget(), + demangle=true, + llvm_to_clang=needs_ir(target), + cc=nothing, + cflags=``, + llvm_postprocess=nothing, l + lvm_finalize=nothing, + kwargs... +) ``` Low level interface for compiling a shared object / dynamically loaded library (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing @@ -365,20 +389,26 @@ function generate_shlib(f::Function, tt, path::String=tempname(), name=fix_name( end # As above, but taking an array of functions and returning a single shlib function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), filename::String="libfoo"; - target::StaticTarget=StaticTarget(), - demangle = true, - cc = nothing, - cflags = ``, - llvm_to_clang::Bool = needs_ir(target), + target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options + demangle::Bool = true, # Whether or not function names should be exactly as they are in Julia + llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Compiler flags, may specify to override defaults + llvm_postprocess = nothing, # optional hook: called on the each function's module in turn + llvm_finalize = nothing, # optional hook: called on the final combined module when it is complete kwargs... ) # How it is linked: what `cc` says, or the toolchain of the target tc = isnothing(cc) ? toolchain(target) : CCToolchain(cc) - # Prepare objects or (if llvm_to_clang is set) IR + # Prepare objects or (if llvm_to_clang is set) IR. The functions of a DLL are exported (before the hook of the caller, which sees the module as it is linked) + finalize = function (mod) + iswindows(target) && add_dllexport!(mod, funcs; demangle) + isnothing(llvm_finalize) || llvm_finalize(mod) + end lib_path = joinpath(path, output_name(tc, target, filename; kind = :shared)) _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, - dllexport=iswindows(target), kwargs...) + llvm_postprocess, llvm_finalize=finalize, kwargs...) # Compile and/or link shlib! link(tc, target, [obj_or_ir_path], lib_path; kind = :shared, cflags) @@ -525,9 +555,33 @@ end """ ```julia generate_obj(f, tt, path::String = tempname(), filenamebase::String="obj"; kwargs...) -> (path, obj_path) +generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; + target::StaticTarget=StaticTarget(), + demangle = true, + emit_llvm_only = false, + main_wrapper = nothing, + main_prologue = nothing, + llvm_postprocess = nothing, + llvm_finalize = nothing, + kwargs... +) -> (path, obj_path) ``` -Low level interface for compiling object code (`.o`) for the function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. -This is `generate_obj(((f, tt),), ...)`: see below for the keywords. +Low level interface for compiling object code (`.o`) for the function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled, +or for an array of Tuples `(f, tt)` where each function `f` and tuple type `tt` determine the set of methods which will be compiled. +Returns the directory and the path of the file that was written: `filenamebase.o`, or `filenamebase.ll` with `emit_llvm_only`. + +`target` is the [`StaticTarget`](@ref) that the code is made for, which can be one that the host cannot run (see its documentation: the code is then made for the host, retargeted, and the object is made by Julia's LLVM +if it has the code generator for the target, and else by the target's `llc`; an error says so if neither can). The default is the native target. + +If `demangle` is set to `false`, compiled function names are prepended with "julia_". + +If `emit_llvm_only` is set, the LLVM IR (as text, `.ll`) is written in place of the object, for a compiler that makes the object (see [`generate_ir`](@ref), which returns the text instead). + +If `main_wrapper` is the name of one of the functions, the object will be given a `main` that calls it, +possibly preceded by any code added by `main_prologue(builder, mod)`. The builtins that the toolchain of the target asks for (`__udivti3`, ...) are added to the module (see [`add_builtins!`](@ref)). + +`llvm_postprocess(mod)`, if given, is called on the module of each function as soon as it is generated (before its pointers and runtime calls are located, it is retargeted, and the modules are combined). `llvm_finalize(mod)` +is called once on the whole module, when it is complete (the modules combined, and the `main` and the builtins added) and just before the code is emitted. ### Examples ``` @@ -544,46 +598,11 @@ shell> tree \$path 0 directories, 1 file ``` """ -function generate_obj(f, tt, args...; kwargs...) - generate_obj(((f, tt),), args...; kwargs...) -end - - -""" -```julia -generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; - target::StaticTarget=StaticTarget(), - demangle = true, - emit_llvm_only = false, - dllexport = false, - main_wrapper = nothing, - main_prologue = nothing, - llvm_postprocess = nothing, - llvm_finalize = nothing, - kwargs...) -> (path, obj_path) -``` -Low level interface for compiling object code (`.o`) for an array of Tuples `(f, tt)` where each function `f` and tuple type `tt` determine the set of methods which will be compiled. -Returns the directory and the path of the file that was written: `filenamebase.o`, or `filenamebase.ll` with `emit_llvm_only`. - -`target` is the [`StaticTarget`](@ref) that the code is made for, which can be one that the host cannot run (see its documentation: the code is then made for the host, retargeted, and the object is made by Julia's LLVM -if it has the code generator for the target, and else by the target's `llc`; an error says so if neither can). The default is the native target. - -If `demangle` is set to `false`, compiled function names are prepended with "julia_". - -If `emit_llvm_only` is set, the LLVM IR (as text, `.ll`) is written in place of the object, for a compiler that makes the object (see [`generate_ir`](@ref), which returns the text instead). - -If `dllexport` is set, the functions are exported from a Windows DLL. - -If `main_wrapper` is the name of one of the functions, the object will be given a `main` that calls it, -possibly preceded by any code added by `main_prologue(builder, mod)`. The builtins that the toolchain of the target asks for (`__udivti3`, ...) are added to the module (see [`add_builtins!`](@ref)). - -`llvm_postprocess(mod)`, if given, is called on the module of each function when it is compiled, and `llvm_finalize(mod)` on the whole module when complete. -""" +generate_obj(f, tt, args...; kwargs...) = generate_obj(((f, tt),), args...; kwargs...) function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; target::StaticTarget=StaticTarget(), demangle = true, emit_llvm_only = false, - dllexport = false, main_wrapper = nothing, main_prologue = nothing, llvm_postprocess = nothing, @@ -593,14 +612,13 @@ function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), file mkpath(path) if emit_llvm_only # (Required on Windows) ir_path = joinpath(path, "$filenamebase.ll") - write(ir_path, generate_ir(funcs; demangle, target, dllexport, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...)) + write(ir_path, generate_ir(funcs; demangle, target, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...)) return path, ir_path end can_emit_object(target) || throw(ArgumentError("no object can be made for $(target_triple(target)) here: Julia's LLVM has no code generator for it, and no `llc` is given (`StaticTarget(...; llc)`). Use `emit_llvm_only = true` (or `generate_ir`) and give the IR to a compiler that has one")) # compile and generate code in one context, which the module can't outlive GPUCompiler.JuliaContext() do context @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin - dllexport && add_dllexport!(mod, funcs; demangle) isnothing(main_wrapper) || add_main_wrapper!(mod, main_wrapper, target; prologue = main_prologue) uses_builtins(target.toolchain) && add_builtins!(mod, target_triple(target)) isnothing(llvm_finalize) || llvm_finalize(mod) @@ -621,16 +639,15 @@ end """ ```julia -generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle=true, dllexport=false, llvm_postprocess=nothing, kwargs...) -> String +generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle=true, llvm_postprocess=nothing, kwargs...) -> String generate_ir(f, tt; kwargs...) -> String ``` The LLVM IR (as text) of the functions that `generate_obj` would compile to an object file, for tools that take IR instead (a downstream package that makes the object or the executable of a target by itself, for example): nothing is written to the file system, and the module (which cannot outlive its context) is not returned. """ -function generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle = true, dllexport = false, main_wrapper = nothing, main_prologue = nothing, llvm_postprocess = nothing, llvm_finalize = nothing, kwargs...) +function generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle = true, main_wrapper = nothing, main_prologue = nothing, llvm_postprocess = nothing, llvm_finalize = nothing, kwargs...) GPUCompiler.JuliaContext() do context @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin - dllexport && add_dllexport!(mod, funcs; demangle) isnothing(main_wrapper) || add_main_wrapper!(mod, main_wrapper, target; prologue = main_prologue) uses_builtins(target.toolchain) && add_builtins!(mod, target_triple(target)) isnothing(llvm_finalize) || llvm_finalize(mod) diff --git a/test/testcore.jl b/test/testcore.jl index 0cfd8bfb..0dc7ad86 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -473,3 +473,31 @@ end @test endswith(lib, ".dylib") && occursin("shared library arm64", read(`file $lib`, String)) end end + +@testset "Hooks of a package that makes programs by compile_executable: the same for every toolchain" begin + # `main_prologue` is what `main` does first, and `llvm_finalize` is called on the whole module: the program that a C compiler driver links has them as one that `lld` links does (the object has the `main`, and not a wrapper in C) + prologue(builder, mod) = begin + i32 = LLVM.IntType(32) + exit_function = haskey(LLVM.functions(mod), "exit") ? LLVM.functions(mod)["exit"] : LLVM.Function(mod, "exit", LLVM.FunctionType(LLVM.VoidType(), [i32])) + LLVM.call!(builder, LLVM.function_type(exit_function), exit_function, LLVM.Value[LLVM.ConstantInt(i32, 3)]) + end + hook_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = 0 + finalized = Ref(0) + for ir in (false, true) + exe = compile_executable(hook_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "hook_main"; main_prologue = prologue, llvm_finalize = mod -> (finalized[] += 1), llvm_to_clang = ir) + @test run(ignorestatus(`$exe`)).exitcode == 3 + end + @test finalized[] == 2 + # (and without a prologue the program is what it was: the entry point's value is its status) + exe = compile_executable(hook_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "hook_main"; filename = "hook_main2") + @test run(ignorestatus(`$exe`)).exitcode == 0 + # objects in `cflags` are linked with the program + dir = mktempdir() + extra_c = joinpath(dir, "extra.c") + write(extra_c, "int static_compiler_extra(void) { return 7; }\n") + extra_o = joinpath(dir, "extra.o") + run(`$(StaticCompiler.clang()) -c $extra_c -o $extra_o`) + extra_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = ccall("extern static_compiler_extra", llvmcall, Int32, ()) % Int + exe = compile_executable(extra_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "extra_main"; cflags = `$extra_o`, llvm_to_clang = false) + @test run(ignorestatus(`$exe`)).exitcode == 7 +end From 246353d5ab9b174f12bb4bff51b6916229124adc Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Tue, 1 Dec 2026 09:11:10 -0500 Subject: [PATCH 196/202] Further cleanup, including README update --- .github/workflows/ci-integration-nightly.yml | 2 +- .github/workflows/ci-integration.yml | 2 +- .github/workflows/ci-julia-nightly.yml | 2 +- .github/workflows/ci.yml | 2 +- .github/workflows/cross-matrix.yml | 4 +- .github/workflows/docs.yml | 2 +- README.md | 42 +++++++++-------- src/StaticCompiler.jl | 47 +++++--------------- src/toolchain.jl | 47 +++++++++++++------- 9 files changed, 72 insertions(+), 78 deletions(-) diff --git a/.github/workflows/ci-integration-nightly.yml b/.github/workflows/ci-integration-nightly.yml index 8f8f01a6..10eba4bd 100644 --- a/.github/workflows/ci-integration-nightly.yml +++ b/.github/workflows/ci-integration-nightly.yml @@ -27,7 +27,7 @@ jobs: group: - Integration steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v6 - uses: julia-actions/setup-julia@latest with: version: ${{ matrix.version }} diff --git a/.github/workflows/ci-integration.yml b/.github/workflows/ci-integration.yml index 257a89d1..2fa1e1d3 100644 --- a/.github/workflows/ci-integration.yml +++ b/.github/workflows/ci-integration.yml @@ -29,7 +29,7 @@ jobs: group: - Integration steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v6 - uses: KyleMayes/install-llvm-action@v2 with: version: "17" diff --git a/.github/workflows/ci-julia-nightly.yml b/.github/workflows/ci-julia-nightly.yml index 2e9d6740..7ad5d2d6 100644 --- a/.github/workflows/ci-julia-nightly.yml +++ b/.github/workflows/ci-julia-nightly.yml @@ -27,7 +27,7 @@ jobs: group: - Core steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v6 - uses: julia-actions/setup-julia@latest with: version: ${{ matrix.version }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 49bf043e..78ecaba5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: version: '1' os: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v6 - uses: KyleMayes/install-llvm-action@v2 with: version: "17" diff --git a/.github/workflows/cross-matrix.yml b/.github/workflows/cross-matrix.yml index 1620931f..e606fe55 100644 --- a/.github/workflows/cross-matrix.yml +++ b/.github/workflows/cross-matrix.yml @@ -26,7 +26,7 @@ jobs: - windows-latest steps: - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 + - uses: julia-actions/setup-julia@v3 with: version: '1' - uses: julia-actions/cache@v2 @@ -70,7 +70,7 @@ jobs: - windows-latest steps: - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 + - uses: julia-actions/setup-julia@v3 with: version: '1' - uses: actions/download-artifact@v4 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 8401f688..3e97882b 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -19,7 +19,7 @@ jobs: statuses: write steps: - uses: actions/checkout@v4 - - uses: julia-actions/setup-julia@v2 + - uses: julia-actions/setup-julia@v3 with: version: '1' - uses: julia-actions/cache@v2 diff --git a/README.md b/README.md index 51d37e2a..e2150ac0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # StaticCompiler +[![Docs](https://img.shields.io/badge/docs-dev-blue.svg)](https://tshort.github.io/StaticRuntime.jl/dev/) [![CI](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci.yml) [![CI (Integration)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration.yml/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-integration.yml) [![CI (Julia nightly)](https://github.com/tshort/StaticCompiler.jl/workflows/CI%20(Julia%20nightly)/badge.svg)](https://github.com/tshort/StaticCompiler.jl/actions/workflows/ci-julia-nightly.yml) @@ -9,11 +10,14 @@ 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 ```julia using Pkg Pkg.add("StaticCompiler") ``` +or +```julia +] add StaticCompiler +``` ## Approach @@ -36,9 +40,25 @@ shell> ls -alh hello shell> ./hello Hello, world! ``` -This approach comes with substantial limitations compared to regular julia code, as you cannot rely on julia's runtime, `libjulia` (see, e.g., [StaticTools.jl](https://github.com/brenhinkeller/StaticTools.jl) for some ways to work around these limitations). +This approach comes with substantial limitations compared to regular julia code, as you cannot rely on julia's runtime, `libjulia` (see, e.g., [StaticTools.jl](https://github.com/brenhinkeller/StaticTools.jl) or [StaticRuntime.jl](https://github.com/brenhinkeller/StaticRuntime.jl) for some ways to work around these limitations). + +The low-level function `StaticCompiler.generate_obj` (not exported) generates object files, while `StaticCompiler.generate_ir` generates LLVM IR; either may be used for more control of compilation (e.g., to pass to your own compiler or linker). Which backend is used in the compilation process may be controlled with the `llvm_to_clang` keyword argument, though some targets only support the latter route (i.e. via LLVM IR). + +### Cross-compilation + +For further control of the compilation process, many StaticCompiler functions (including `compile_executable` and `compile_shlib`) accept a `target` as a keyword argument, which allows cross-compilation (i.e., so that a program can be compiled on one system for use on another): + +```julia +julia> using StaticCompiler, Glibc_jll, Pkg # (Glibc_jll and Pkg fetch the C library of a Linux target; Mingw_jll does the same for Windows) + +julia> target = StaticTarget("aarch64-unknown-linux-gnu"; retarget = true, toolchain = LLDToolchain(:gnu; sysroot = StaticCompiler.glibc_sysroot("aarch64-unknown-linux-gnu"))); + +julia> compile_executable(hello, (), "./", "hello"; filename = "hello_aarch64", target) +``` + +The toolchain can be either a [`CCToolchain`](@ref) specifying a C compiler, or an [`LLDToolchain`](@ref) object specifying an LLVM `lld` to be used (for which target flavors may include `:gnu` for Linux, `:darwin` for macOS, `:mingw` for Windows, and `:wasm` for WebAssembly). Julia's LLVM contains the code generators for its own architecture and for WebAssembly only. For any other architecture, the IR can be made (`emit_llvm_only = true`), and `StaticTarget(...; llc = "/path/to/llc")` makes the object with an `llc` that has the desired backend for the same LLVM version. -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 for example, to cross-compile to other targets. +The [`cross-matrix.yml`](.github/workflows/cross-matrix.yml) CI workfoow tests cross-compilation to and from Linux (x86-64 and AArch64), macOS, and Windows. ### Method overlays @@ -74,22 +94,6 @@ If for some reason, you wish to use a different method table (defined with `Base `StaticCompiler.empty_table` is available as a method table with no overrides at all, if you do not wish to use StaticCompiler's `@device_override`s.Your own overrides can be added to a table of your own, or to this one, with `Base.Experimental.@overlay(StaticCompiler.empty_table, ...)`. -### Cross-compilation - -`compile_executable` and `compile_shlib` accept a `target`, so a program can be made on one system for another: - -```julia -julia> using StaticCompiler, Glibc_jll, Pkg # (Glibc_jll and Pkg fetch the C library of a Linux target; Mingw_jll does the same for Windows) - -julia> target = StaticTarget("aarch64-unknown-linux-gnu"; retarget = true, toolchain = LLDToolchain(:gnu; sysroot = StaticCompiler.glibc_sysroot("aarch64-unknown-linux-gnu"))); - -julia> compile_executable(hello, (), "./", "hello"; filename = "hello_aarch64", target) -``` - -The toolchain can be either a [`CCToolchain`](@ref) specifying a C compiler, or an [`LLDToolchain`](@ref) object specifying an LLVM `lld` to be used (flavors include `:gnu` for Linux, `:darwin` for macOS, `:mingw` for Windows, and `:wasm` for WebAssembly). Julia's LLVM contains the code generators for its own architecture and for WebAssembly only. For any other architecture, the IR can be made (`emit_llvm_only = true`), and `StaticTarget(...; llc = "/path/to/llc")` makes the object with an `llc` that has the desired backend for the same LLVM version. - -The [`cross-matrix.yml`](.github/workflows/cross-matrix.yml) CI workfoow tests cross-compilation to and from Linux (x86-64 and AArch64), macOS, and Windows. - ## Limitations * 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). diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 52bd0380..f85d9776 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -9,7 +9,7 @@ using StaticTools: @symbolcall, @c_str, println using Base: BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform using Base: BinaryPlatforms.arch, BinaryPlatforms.os_str, BinaryPlatforms.libc_str export compile_shlib, compile_executable -export static_code_llvm, static_code_typed, static_llvm_module, static_code_native +export static_code_llvm, static_code_typed, static_code_native, static_llvm_module export @device_override, @print_and_throw export StaticTarget, CCToolchain, LLDToolchain @@ -271,19 +271,6 @@ function generate_shlib_fptr(path::String, name, filename::String=name) fptr end -# As above, but also compile (maybe remove this method in the future?) -function generate_shlib_fptr(f, tt, path::String=tempname(), name=fix_name(f), filename::String=name; - temp::Bool=true, - kwargs... - ) - generate_shlib(f, tt, path, name, filename; kwargs...) - fptr = generate_shlib_fptr(path, name, filename) - if temp - atexit(()->rm(path; recursive=true)) - end - fptr -end - """ ```julia generate_executable(f, tt, path::String, name, filename=string(name); @@ -416,7 +403,6 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file return path, filename end -# (`llvm_postprocess` is accepted and ignored by the three `static_code_*` functions: wrappers, like StaticRuntime's, pass the same keywords to all of the functions that they wrap) """ ```julia static_code_llvm(f, types; target::StaticTarget=StaticTarget(), kwargs...) @@ -438,7 +424,7 @@ The typed Julia code of `f` for the argument types `types`, as `code_typed` of J compiler for the `target` sees it, with the method overlays of this package: a vector of `CodeInfo => return type` pairs. """ -function static_code_typed(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) +function static_code_typed(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) job = static_job(func, types; target, kwargs...) GPUCompiler.code_typed(job) end @@ -450,42 +436,31 @@ static_code_native(f, tt, fname=fix_name(f); target::StaticTarget=StaticTarget() Print the assembly that `f` compiles to for the argument types `tt` for the `target`, as the code generator of LLVM makes it. """ -function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) +function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), kwargs...) job = static_job(f, tt; name=fname, target, kwargs...) GPUCompiler.code_native(stdout, job; libraries=false) end -const CONTEXT_DOC = """ -The module belongs to the active thread-safe context, so call this function in a -`GPUCompiler.JuliaContext() do ctx ... end` block, and dispose of the module (or hand it to an -operation that consumes it, like `LLVM.link!(dst, mod)`) before the block ends. Without an -active thread-safe context, a new one is created for the module, which is never disposed of -(i.e., every such call leaks a context).""" """ ```julia static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) +static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) ``` Compile the method of `f` for the argument types `tt` to an `LLVM.Module`, which the caller owns. -$CONTEXT_DOC +The module belongs to the active thread-safe context, so call this function in a +`GPUCompiler.JuliaContext() do ctx ... end` block, and dispose of the module (or hand it to an +operation that consumes it, like `LLVM.link!(dst, mod)`) before the block ends. Without an +active thread-safe context, a new one is created for the module, which is never disposed of +(i.e., every such call leaks a context). """ function static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) with_module_context() do compile_llvm_module(f, tt, name; demangle, target, kwargs...) end end - -""" -```julia -static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) -``` -Compile the methods of an array of tuples `(f, tt)` of functions and argument types to a -single `LLVM.Module`, which the caller owns. - -$CONTEXT_DOC -""" function static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) with_module_context() do compile_llvm_module(funcs; demangle, target, kwargs...) @@ -659,7 +634,9 @@ generate_ir(f, tt; kwargs...) = generate_ir(((f, tt),); kwargs...) """ - link(toolchain, target, inputs::Vector{String}, output::String; kind = :executable, entry = nothing, cflags = ``) -> path +```julia +link(toolchain, target, inputs::Vector{String}, output::String; kind = :executable, entry = nothing, cflags = ``) -> path +``` Link the `inputs` (object files, or LLVM IR files if the toolchain is a `CCToolchain`) into an executable (`kind = :executable`) or shared library (`kind = :shared`), and return its path. diff --git a/src/toolchain.jl b/src/toolchain.jl index 4bf9dc62..97d48cca 100644 --- a/src/toolchain.jl +++ b/src/toolchain.jl @@ -9,10 +9,17 @@ Give a target its toolchain by `StaticTarget(...; toolchain = ...)` (or `StaticT """ Toolchain """ - CCToolchain(cc::Cmd) +```julia +CCToolchain(cc::Cmd) +``` -Link by a C compiler driver (`clang` by default): the driver compiles what it is given, objects or LLVM IR (`.ll`), and links it. This is how executables and shared libraries were made before toolchains, -and it still is for the host and for any target that has a driver of its own (`StaticTarget(...; toolchain = CCToolchain(cc))`). +Compile and/or link using a C compiler (`clang` from `Clang_jll` by default). +This compiler takes what has been output by `generate_obj` or `generate_ir` (whether objects (`.o`) or LLVM IR (`.ll`)), +compiles if needed, and links the result. + +This is the traditional default route for native compilation of executables and shared libraries +(where the target system is the same as the host system), +but provides somewhat more limited possibilities for control of cross-compilation. """ struct CCToolchain <: Toolchain cc::Cmd @@ -22,17 +29,31 @@ CCToolchain(cc::Cmd; builtins::Bool = false) = CCToolchain(cc, builtins) CCToolchain(cc::AbstractString; builtins::Bool = false) = CCToolchain(`$cc`; builtins) Base.:(==)(a::CCToolchain, b::CCToolchain) = a.cc == b.cc && a.builtins == b.builtins +# the libraries that a flavor of `LLDToolchain` links with, if none are given +const DEFAULT_LLD_LIBS = Dict( + :mingw => ["mingw32", "mingwex", "moldname", "msvcrt", "advapi32", "shell32", "user32", "kernel32"], + :gnu => ["c", "m", "dl", "pthread"], # (`dl` and `pthread` are libraries of their own before glibc 2.34, and empty ones in it) + :darwin => ["System"], + :wasm => String[], +) + """ - LLDToolchain(flavor; sysroot = nothing, dynamic_linker = nothing, libs = nothing, flags = ``, builtins = true) +```julia +LLDToolchain(flavor; sysroot = nothing, dynamic_linker = nothing, libs = nothing, flags = ``, builtins = true) +``` Link objects with the `lld` that Julia has, without a C compiler or its driver. `flavor` is * `:gnu`: ELF for Linux. The start files and the libraries of the C library are in `sysroot`, a directory that has the `lib` of a glibc (the `Glibc_jll` artifact provides one). See [`glibc_sysroot`](@ref). -* `:darwin`: Mach-O for macOS. `sysroot` is the SDK (by default that of Xcode `xcrun --show-sdk-path`); where the host has none, an SDK must be given. See [`macos_sdk`](@ref). +* `:darwin`: Mach-O for macOS. `sysroot` is the path to the SDK (by default that of Xcode `xcrun --show-sdk-path`); where the host has none, an SDK must be given. See [`macos_sdk`](@ref). * `:mingw`: PE for Windows, with the libraries of MinGW-w64 in `sysroot` (the `Mingw_jll` artifact provides one). See [`mingw_sysroot`](@ref). -* `:wasm`: WebAssembly, a `.wasm` module that exports the entry point and imports the rest. - -`libs` are the libraries that are linked with (the defaults are those of the flavor), and `flags` are added to the command of the linker. With `builtins`, the object has the definitions of what the code generator calls and no library of the toolchain has (`__udivti3`, `__extendhfsf2`, ...: see [`add_builtins!`](@ref)). -Nothing makes a `main`: the object has one (see the `main_wrapper` of [`generate_obj`](@ref); [`generate_executable`](@ref) asks for it when the toolchain is not a `CCToolchain`). +* `:wasm`: WebAssembly, a `.wasm` module. + +Keyword arguments include +* `sysroot` - the path to the relevant system library, as a `String`, or `nothing` to defer to defaults +* `dynamic_linker` - the dynamic linker to use, as a `String`, or `nothing` to defer to defaults +* `libs` - the libraries to link against, as a `String`, or `nothing` to defer to defaults +* `flags` - any optional flags to be passed to the linker +* `builtins` - if true, any llvm builtins which the target does not support will be replaced with emulated versions; see [`add_builtins!`](@ref)). """ struct LLDToolchain <: Toolchain flavor::Symbol @@ -42,11 +63,6 @@ struct LLDToolchain <: Toolchain flags::Cmd builtins::Bool end -# the libraries that a flavor of `LLDToolchain` links with, if none are given -const DEFAULT_LLD_LIBS = Dict(:gnu => ["c", "m", "dl", "pthread"], # (`dl` and `pthread` are libraries of their own before glibc 2.34, and empty ones in it) - :darwin => ["System"], :wasm => String[], - :mingw => ["mingw32", "mingwex", "moldname", "msvcrt", "advapi32", "shell32", "user32", "kernel32"]) - function LLDToolchain(flavor::Symbol; sysroot = nothing, dynamic_linker = nothing, libs = nothing, flags = ``, builtins::Bool = true) flavor in keys(DEFAULT_LLD_LIBS) || throw(ArgumentError("the flavor of `LLDToolchain` is `:gnu`, `:darwin`, `:mingw` or `:wasm`, not `$(repr(flavor))`")) libs === nothing && (libs = DEFAULT_LLD_LIBS[flavor]) @@ -186,9 +202,6 @@ function clang() return cmd end - -## lld - # the directory of a sysroot that has the start files of the C library function crt_directory(sysroot::AbstractString) for d in ("lib", "usr/lib", "lib64", "usr/lib64", "usr/lib/aarch64-linux-gnu", "usr/lib/x86_64-linux-gnu", "lib/aarch64-linux-gnu", "lib/x86_64-linux-gnu") From 9fe225357a7dfc534fb0716ec235f2e1399a9957 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Tue, 1 Dec 2026 10:46:59 -0500 Subject: [PATCH 197/202] Update README --- README.md | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e2150ac0..cf7ec97f 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ This is an experimental package to compile Julia code to standalone libraries. A system image is not needed. ## Installation and Usage +Installation via the package manager: ```julia using Pkg Pkg.add("StaticCompiler") @@ -19,10 +20,6 @@ or ] add StaticCompiler ``` -## Approach - -This package uses the [GPUCompiler package](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. - ### Standalone compilation StaticCompiler.jl provides the functions `compile_executable` and `compile_shlib` for compiling a Julia function to a native executable or shared library for use from outside of Julia: ```julia @@ -56,7 +53,13 @@ julia> target = StaticTarget("aarch64-unknown-linux-gnu"; retarget = true, toolc julia> compile_executable(hello, (), "./", "hello"; filename = "hello_aarch64", target) ``` -The toolchain can be either a [`CCToolchain`](@ref) specifying a C compiler, or an [`LLDToolchain`](@ref) object specifying an LLVM `lld` to be used (for which target flavors may include `:gnu` for Linux, `:darwin` for macOS, `:mingw` for Windows, and `:wasm` for WebAssembly). Julia's LLVM contains the code generators for its own architecture and for WebAssembly only. For any other architecture, the IR can be made (`emit_llvm_only = true`), and `StaticTarget(...; llc = "/path/to/llc")` makes the object with an `llc` that has the desired backend for the same LLVM version. +The toolchain can be either a [`CCToolchain`](@ref) specifying a C compiler, or an [`LLDToolchain`](@ref) object specifying an LLVM `lld` to be used. +For an [`LLDToolchain`](@ref), supported target flavors include: +* `:gnu` for Linux +* `:darwin` for macOS +* `:mingw` for Windows +* `:wasm` for WebAssembly +Julia's LLVM contains the code generators for its own architecture and for WebAssembly only. For any other architecture, the IR can be made (`emit_llvm_only = true`), and `StaticTarget(...; llc = "/path/to/llc")` makes the object with an `llc` that has the desired backend for the same LLVM version. The [`cross-matrix.yml`](.github/workflows/cross-matrix.yml) CI workfoow tests cross-compilation to and from Linux (x86-64 and AArch64), macOS, and Windows. @@ -94,12 +97,22 @@ If for some reason, you wish to use a different method table (defined with `Base `StaticCompiler.empty_table` is available as a method table with no overrides at all, if you do not wish to use StaticCompiler's `@device_override`s.Your own overrides can be added to a table of your own, or to this one, with `Base.Experimental.@overlay(StaticCompiler.empty_table, ...)`. +## Approach +This package uses [GPUCompiler](https://github.com/JuliaGPU/GPUCompiler.jl) to generate code. + +There are two main routes: +1. Compile all the way to object files (`.o`) with GPUCompiler, then link into to a single binary with an external compiler or linker; or +2. Compile only as far as LLVM IR (`.ll` files), then compile the rest of the way with an external LLVM compiler and linker (i.e., `clang` or `lld`). +The former is the default for native compilation on most targets, while the latter is the default for cross-compilation. + +An external compiler amay be specified with a [`CCToolchain`](@ref) passed to [`StaticTarget`](@ref), and must have the ability to compile LLVM IR (e.g., `clang`) if route (2) is intended (i.e., with `llvm_to_clang=true`). Analagously, an external linker may be specified with an [`LLDToolchain`](@ref) passed to [`StaticTarget`](@ref), along with its target flavor and flags. This approach provides the most control, especially for cross compilation, but for convenience one may also pass a compiler and its flags directly to `compile_executable`/`compile_shlib` as keyword arguments. + ## Limitations -* 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). +* GC-tracked allocations and global variables do *not* work with `compile_executable` or `compile_shlib` without some replacement for Julia's GC machinery. This has some interesting consequences, including that all functions _within_ the function you want to compile must generally 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](src/quirks.jl)). -* Type instability. Type unstable code cannot currently be statically compiled via this package. -* Extra experimental on Windows (PRs welcome if you encounter issues). Should work in WSL on Windows 10+. +* Type instability. Type-unstable code cannot generally be statically compiled via this package. +* Extra experimental on Windows, though tested in CI. Should work in WSL on Windows 10+. ## Guide for Package Authors From 25b710bc1a96de8691fe181566b63e1688cdd9a2 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Tue, 1 Dec 2026 13:00:45 -0500 Subject: [PATCH 198/202] Differentiate methods by signature when demangle=false --- src/StaticCompiler.jl | 104 +++++++++++++++++++++++++++++++++++------- src/llvm.jl | 4 +- test/testcore.jl | 33 +++++++++++++- 3 files changed, 122 insertions(+), 19 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index f85d9776..5992a710 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -172,9 +172,13 @@ compile_shlib(funcs::Array, [path::String=pwd()]; ``` As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. -Arguments and returned values from `compile_shlib` must be native objects such as `Int`, `Float64`, or `Ptr`. They cannot be things like `Tuple{Int, Int}` because that is not natively sized. Such objects need to be passed by reference instead of by value. +Arguments and returned values from `compile_shlib` must be native objects such as `Int`, `Float64`, or `Ptr`. +They should not be things like `Tuple{Int, Int}` because that is not natively sized. +Such objects should to be passed by reference instead of by value. -If `demangle` is set to `false`, compiled function names are prepended with "julia_". +If `demangle` is set to `false`, compiled function names are prefixed with "julia_", +and suffixed with their argument types, if any (`julia_f_Int64_Int64`, `julia_f_Float64`, `julia_main`). +In the event of any remaining collisions, names are further suffixed with a number, in order of method age. ### Examples ```julia @@ -278,8 +282,10 @@ generate_executable(f, tt, path::String, name, filename=string(name); llvm_postprocess=nothing, llvm_finalize=nothing, main_prologue=nothing, kwargs...) ``` Attempt to compile a standalone executable that runs `f`. -Low-level interface; you should generally use `compile_executable` instead. The keywords are those of `compile_executable`; the hooks are passed to [`generate_obj`](@ref). With a `main_prologue` the object has its own `main`, -whichever the toolchain. +Low-level interface; you should generally use `compile_executable` instead. + +The keywords are those of `compile_executable`; the hooks are passed to [`generate_obj`](@ref). +With a `main_prologue` the object has its own `main`, whichever the toolchain. ### Examples ```julia @@ -311,7 +317,7 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi # Prepare objects or (if llvm_to_clang is set) IR. A linker that is not a compiler driver is given an object that has a `main`, and so is a driver when # `main_prologue` asks for code to run first, which the `main` of the object has (the driver would be given a `main` of its own, with nothing before the entry point) - fn = demangle ? "$name" : "julia_$name" + fn = demangle ? "$name" : "julia_" * entry_names(funcs; demangle)[1] # (the name of the first function and the types of its arguments) main_wrapper = !(tc isa CCToolchain) || !isnothing(main_prologue) ? fn : nothing _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...) @@ -340,7 +346,9 @@ Low level interface for compiling a shared object / dynamically loaded library (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. -If `demangle` is set to `false`, compiled function names are prepended with "julia_". +If `demangle` is set to `false`, compiled function names are prefixed with "julia_", +and suffixed with their argument types, if any (`julia_f_Int64_Int64`, `julia_f_Float64`, `julia_main`). +In the event of any remaining collisions, names are further suffixed with a number, in order of method age. ### Examples ```julia @@ -456,7 +464,7 @@ operation that consumes it, like `LLVM.link!(dst, mod)`) before the block ends. active thread-safe context, a new one is created for the module, which is never disposed of (i.e., every such call leaks a context). """ -function static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) +function static_llvm_module(f, tt, name=nothing; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) with_module_context() do compile_llvm_module(f, tt, name; demangle, target, kwargs...) end @@ -485,7 +493,7 @@ function with_module_context(f) end # compile to a module in the active (Julia) context, which must outlive the module -function compile_llvm_module(f, tt, name=fix_name(f); +function compile_llvm_module(f, tt, name=nothing; target::StaticTarget=StaticTarget(), demangle=true, strip=true, @@ -495,6 +503,8 @@ function compile_llvm_module(f, tt, name=fix_name(f); llvm_postprocess=nothing, kwargs..., ) + # (without a name of its own: the name of the function, with the types of the arguments after `julia_` if `demangle` is `false`, as `entry_names` has it) + name === nothing && (name = only(entry_names(((f, tt),); demangle))) if !demangle name = "julia_"*name end @@ -511,13 +521,68 @@ function compile_llvm_module(f, tt, name=fix_name(f); return m end +# The functions of a module are named by the name of the function (`fix_name`). With `demangle = true` those are the names that Julia has: two methods of one function (or two functions that have one name) would be +# two functions of the same name in the module, which LLVM refuses (as an error of the linker, which does not say which entry points they are), so the entry points are to be given functions of their own. +# With `demangle = false` the name is `julia_`, the name of the function, and the types of the arguments (`julia_my_add_Int64_Int64`, `julia_my_add_hello_struct`, `julia_main`), always, so that it does not +# change when methods are added or redefined; and where that is not enough (the same types from two modules, or a name that is very long) the names are numbered, in the order that the methods were defined (the oldest +# first: `..._1`, `..._2`). This gives the names without `julia_`. +function entry_names(funcs; demangle::Bool = true) + base = String[fix_name(f) for (f, _) in funcs] + if demangle + for name in unique(base) + group = findall(==(name), base) + length(group) > 1 || continue + throw(ArgumentError("more than one entry point is named `$name` ($(describe_entries([funcs[i] for i in group]))): with `demangle = true` the name of an entry point is the name of its function, whatever the types of the arguments, " * + "so each needs a function of its own (for example `$(name)_int(a, b) = $name(a, b)` and `$(name)_struct(s) = $name(s)`, and these as the entry points); " * + "with `demangle = false` the names have the types of the arguments: `julia_$(name)_Int64_Int64`, ...")) + end + return base + end + names = String[add_suffix(name, argument_suffix(tt)) for (name, (_, tt)) in zip(base, funcs)] + # (what the types do not tell apart, or what is too long, is numbered: the oldest method first, and in the order that they are given in for those that are as old) + for suffixed in unique(names) + same = findall(==(suffixed), names) + (length(same) > 1 || length(suffixed) > 120) || continue + for (rank, i) in enumerate(sort(same; by = i -> (definition_world(funcs[i]...), i))) + names[i] = string(base[i], "_", rank) + end + end + length(unique(names)) == length(names) || throw(ArgumentError("the names of the entry points made from the types of their arguments are not all different ($(join(names, ", "))): one of the functions has a name like another's with a suffix")) + return names +end + +# the types of the arguments as part of a name: `Int64_Int64`, `Vector_Int64`, `Ptr_Ptr_UInt8`, and nothing for a function that takes none +add_suffix(name, suffix) = isempty(suffix) ? name : string(name, "_", suffix) +function argument_suffix(tt) + types = Base.to_tuple_type(tt).parameters + isempty(types) && return "" + parts = map(types) do T + text = replace(string(T), r"(?:[A-Za-z_]\w*\.)+" => "") # (without the modules: `Main.hello_struct`) + text = replace(fix_name(text), r"_+" => "_") + strip(text, '_') + end + return join(parts, "_") +end + +describe_entries(entries) = join(("$(nameof(f))$(tt isa Tuple ? tt : (tt,))" for (f, tt) in entries), " and ") + +# the world in which the method that is called for these types was defined (later than all that is not a method of Julia code, if there is none) +function definition_world(f, tt) + try + return Base.which(f, Base.to_tuple_type(tt)).primary_world + catch + return typemax(UInt) + end +end + function compile_llvm_module(funcs::Union{Array,Tuple}; kwargs...) + names = entry_names(funcs; demangle = get(kwargs, :demangle, true)) (f, tt), rest... = funcs - mod = compile_llvm_module(f, tt; kwargs...) + mod = compile_llvm_module(f, tt, names[1]; kwargs...) try - for (f, tt) in rest + for ((f, tt), name) in zip(rest, names[2:end]) # consumes the module, also when linking fails - link!(mod, compile_llvm_module(f, tt; kwargs...)) + link!(mod, compile_llvm_module(f, tt, name; kwargs...)) end run!(MergeFunctionsPass(), mod) catch @@ -548,15 +613,22 @@ Returns the directory and the path of the file that was written: `filenamebase.o `target` is the [`StaticTarget`](@ref) that the code is made for, which can be one that the host cannot run (see its documentation: the code is then made for the host, retargeted, and the object is made by Julia's LLVM if it has the code generator for the target, and else by the target's `llc`; an error says so if neither can). The default is the native target. -If `demangle` is set to `false`, compiled function names are prepended with "julia_". +If `demangle` is set to `false`, compiled function names are prefixed with "julia_", +and suffixed with their argument types, if any (`julia_f_Int64_Int64`, `julia_f_Float64`, `julia_main`). +In the event of any remaining collisions, names are further suffixed with a number, in order of method age. -If `emit_llvm_only` is set, the LLVM IR (as text, `.ll`) is written in place of the object, for a compiler that makes the object (see [`generate_ir`](@ref), which returns the text instead). +If `emit_llvm_only` is set, the LLVM IR (`.ll` file) is written in place of the object, +for a compiler that makes the object (see [`generate_ir`](@ref)). If `main_wrapper` is the name of one of the functions, the object will be given a `main` that calls it, -possibly preceded by any code added by `main_prologue(builder, mod)`. The builtins that the toolchain of the target asks for (`__udivti3`, ...) are added to the module (see [`add_builtins!`](@ref)). +possibly preceded by any code added by `main_prologue(builder, mod)`. + +The builtins that the toolchain of the target asks for (e.g., `__udivti3`, ...) are also added to the module (see [`add_builtins!`](@ref)). -`llvm_postprocess(mod)`, if given, is called on the module of each function as soon as it is generated (before its pointers and runtime calls are located, it is retargeted, and the modules are combined). `llvm_finalize(mod)` -is called once on the whole module, when it is complete (the modules combined, and the `main` and the builtins added) and just before the code is emitted. +`llvm_postprocess(mod)`, if given, is called on the module of each function as soon as it is generated +(before its pointers and runtime calls are located, it is retargeted, and the modules are combined). +Similarly, `llvm_finalize(mod)` is called once on the whole module, when it is complete +(the modules combined, and the `main` and the builtins added) and just before the code is emitted. ### Examples ``` diff --git a/src/llvm.jl b/src/llvm.jl index cdfdcbd1..5257c6ec 100644 --- a/src/llvm.jl +++ b/src/llvm.jl @@ -124,8 +124,8 @@ end # Export the compiled entry points from a Windows DLL function add_dllexport!(mod::LLVM.Module, funcs; demangle=true) - for (f, _) in funcs - name_f = (demangle ? "" : "julia_") * fix_name(f) + for (name, (f, _)) in zip(entry_names(funcs; demangle), funcs) + name_f = (demangle ? "" : "julia_") * name llvmf = get(mod.functions, name_f, nothing) if llvmf !== nothing && !isdeclaration(llvmf) llvmf.dllstorage = LLVM.DLLStorageClass.Export diff --git a/test/testcore.jl b/test/testcore.jl index 0dc7ad86..7609bdbe 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -21,7 +21,7 @@ fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globall # As above, but without demangling filepath = compile_shlib(fib, (Int,), workdir, name, demangle=false) ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) - fptr = Libdl.dlsym(ptr, "julia_"*name) + fptr = Libdl.dlsym(ptr, "julia_fib_Int64") # (the name of the function and the types of its arguments) @test fptr != C_NULL @test ccall(fptr, Int, (Int,), 10) == 55 Libdl.dlclose(ptr) @@ -501,3 +501,34 @@ end exe = compile_executable(extra_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "extra_main"; cflags = `$extra_o`, llvm_to_clang = false) @test run(ignorestatus(`$exe`)).exitcode == 7 end + +# two entry points of one name (methods of one function): an error that says so with `demangle = true`, and numbered names, the oldest method first, with `demangle = false` +struct NameClashPair; a::Int; b::Int; end +name_clash(a, b) = a + b +name_clash(s::NameClashPair) = s.a + s.b +name_clash(a, b, c) = a + b + c +name_clash_int(a, b) = name_clash(a, b) +name_clash_pair(s) = name_clash(s) +@testset "Entry points of one name" begin + entries = ((name_clash, Tuple{NameClashPair}), (name_clash, Tuple{Int,Int,Int}), (name_clash, Tuple{Int,Int})) + err = try StaticCompiler.generate_obj(entries, mktempdir(), "clash"; demangle = true); nothing catch e e end + @test err isa ArgumentError && occursin("more than one entry point is named `name_clash`", err.msg) && occursin("demangle = false", err.msg) + # (the names have the types, whatever the order that the methods were defined or are given in) + @test StaticCompiler.entry_names(entries; demangle = false) == ["name_clash_NameClashPair", "name_clash_Int64_Int64_Int64", "name_clash_Int64_Int64"] + @test StaticCompiler.entry_names(reverse(entries); demangle = false) == reverse(StaticCompiler.entry_names(entries; demangle = false)) + @test StaticCompiler.argument_suffix(()) == "" && StaticCompiler.argument_suffix(Tuple{Vector{Int},Ptr{Ptr{UInt8}}}) == "Vector_Int64_Ptr_Ptr_UInt8" + # (what the types do not tell apart is numbered, the oldest method first) + @test StaticCompiler.entry_names(((name_clash, Tuple{Int,Int}), (name_clash, Tuple{Int,Int})); demangle = false) == ["name_clash_1", "name_clash_2"] + ir = StaticCompiler.generate_ir(entries; demangle = false) + @test occursin(r"define [^\n]*@julia_name_clash_Int64_Int64\(i64[^,\n]*, i64[^,\n)]*\)", ir) # (the method of two numbers) + @test count(r"define [^\n]*@julia_name_clash_\w+\(", ir) == 3 + # a function of its own for each is as good + _, obj = StaticCompiler.generate_obj(((name_clash_int, Tuple{Int,Int}), (name_clash_pair, Tuple{NameClashPair})), mktempdir(), "noclash") + @test isfile(obj) + # (with `demangle = false` the types are in every name, also of an entry point that is alone: `julia_`, the function, and the types if it has arguments) + @test StaticCompiler.entry_names(((name_clash_int, Tuple{Int,Int}),); demangle = false) == ["name_clash_int_Int64_Int64"] + @test StaticCompiler.entry_names(((name_clash_int, ()),); demangle = false) == ["name_clash_int"] # (nothing for a function that takes no arguments) + @test occursin("@julia_name_clash_int_Int64_Int64(", string(StaticCompiler.static_llvm_module(name_clash_int, Tuple{Int,Int}; demangle = false))) + # (one entry point, or names that differ, are as they were) + @test StaticCompiler.entry_names(((name_clash_int, Tuple{Int,Int}), (name_clash_pair, Tuple{NameClashPair})); demangle = true) == ["name_clash_int", "name_clash_pair"] +end From 0a47297e89ead65af3b73e132de4b1072a8fab6a Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Tue, 1 Dec 2026 13:47:59 -0500 Subject: [PATCH 199/202] Add Itanium-style mangling (`demangle=:itanium`) --- src/StaticCompiler.jl | 123 +++++------------- src/llvm.jl | 5 +- src/mangling.jl | 282 ++++++++++++++++++++++++++++++++++++++++++ test/testcore.jl | 50 +++++++- 4 files changed, 361 insertions(+), 99 deletions(-) create mode 100644 src/mangling.jl diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 5992a710..5405af80 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -24,6 +24,8 @@ include("llvm.jl") fix_name(f::Function) = fix_name(string(nameof(f))) fix_name(s) = String(GPUCompiler.safe_name(s)) +include("mangling.jl") + # Check that `f(types...)` is compilable to a native entry point, returning the tuple type of `types` function check_signature(f, types; target::StaticTarget=StaticTarget(), kwargs...) tt = Base.to_tuple_type(types) @@ -136,7 +138,7 @@ end function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name=fix_name(first(first(funcs))); filename::String = name, target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options - demangle::Bool = true, # Whether or not function names should be exactly as they are in Julia + demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR cc = nothing, # Compiler, may specify to override defaults cflags = ``, # Compiler flags, may specify to override defaults @@ -176,9 +178,11 @@ Arguments and returned values from `compile_shlib` must be native objects such a They should not be things like `Tuple{Int, Int}` because that is not natively sized. Such objects should to be passed by reference instead of by value. -If `demangle` is set to `false`, compiled function names are prefixed with "julia_", -and suffixed with their argument types, if any (`julia_f_Int64_Int64`, `julia_f_Float64`, `julia_main`). -In the event of any remaining collisions, names are further suffixed with a number, in order of method age. +The `demangle` keyword argument determines what the functions are called in the compiled library: +* `true` (the default) as they are in Julia (`my_add`); +* `false`, "julia_", the name of the function, and the types of its arguments (`julia_my_add_Int64_Int64`, `julia_main`; numbered for any remaining collisions); +* `:itanium`, as a C++ compiler would name them (`_Z6my_addll`, which `c++filt` reads as `my_add(long, long)`), with the C and C++ types of the arguments where applicable (`Int32` is `int`, `Ptr{UInt8}` is `unsigned char*`, `Cstring` is `const char*`) and Julia's own types named as classes in the namespace of their module. +Multiple methods of one function, if compiled together into the same library, require `false` or `:itanium`. ### Examples ```julia @@ -213,7 +217,7 @@ end function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); filename = "libfoo", target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options - demangle::Bool = true, # Whether or not function names should be exactly as they are in Julia + demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR cc = nothing, # Compiler, may specify to override defaults cflags = ``, # Compiler flags, may specify to override defaults @@ -303,7 +307,7 @@ Hello, world! generate_executable(f, tt, args...; kwargs...) = generate_executable(((f, tt),), args...; kwargs...) function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fix_name(first(first(funcs))), filename=name; target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options - demangle::Bool = true, # Whether or not function names should be exactly as they are in Julia + demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR cc = nothing, # Compiler, may specify to override defaults cflags = ``, # Compiler flags, may specify to override defaults @@ -317,7 +321,7 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi # Prepare objects or (if llvm_to_clang is set) IR. A linker that is not a compiler driver is given an object that has a `main`, and so is a driver when # `main_prologue` asks for code to run first, which the `main` of the object has (the driver would be given a `main` of its own, with nothing before the entry point) - fn = demangle ? "$name" : "julia_" * entry_names(funcs; demangle)[1] # (the name of the first function and the types of its arguments) + fn = demangle === true ? "$name" : entry_names(funcs; demangle, target)[1] # (the name of the first function, as `demangle` says) main_wrapper = !(tc isa CCToolchain) || !isnothing(main_prologue) ? fn : nothing _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...) @@ -346,9 +350,11 @@ Low level interface for compiling a shared object / dynamically loaded library (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled. -If `demangle` is set to `false`, compiled function names are prefixed with "julia_", -and suffixed with their argument types, if any (`julia_f_Int64_Int64`, `julia_f_Float64`, `julia_main`). -In the event of any remaining collisions, names are further suffixed with a number, in order of method age. +The `demangle` keyword argument determines what the functions are called in the compiled library: +* `true` (the default) as they are in Julia (`my_add`); +* `false`, "julia_", the name of the function, and the types of its arguments (`julia_my_add_Int64_Int64`, `julia_main`; numbered for any remaining collisions); +* `:itanium`, as a C++ compiler would name them (`_Z6my_addll`, which `c++filt` reads as `my_add(long, long)`), with the C and C++ types of the arguments where applicable (`Int32` is `int`, `Ptr{UInt8}` is `unsigned char*`, `Cstring` is `const char*`) and Julia's own types named as classes in the namespace of their module. +Multiple methods of one function, if compiled together into the same library, require `false` or `:itanium`. ### Examples ```julia @@ -385,7 +391,7 @@ end # As above, but taking an array of functions and returning a single shlib function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), filename::String="libfoo"; target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options - demangle::Bool = true, # Whether or not function names should be exactly as they are in Julia + demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR cc = nothing, # Compiler, may specify to override defaults cflags = ``, # Compiler flags, may specify to override defaults @@ -398,7 +404,7 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file # Prepare objects or (if llvm_to_clang is set) IR. The functions of a DLL are exported (before the hook of the caller, which sees the module as it is linked) finalize = function (mod) - iswindows(target) && add_dllexport!(mod, funcs; demangle) + iswindows(target) && add_dllexport!(mod, funcs; demangle, target) isnothing(llvm_finalize) || llvm_finalize(mod) end lib_path = joinpath(path, output_name(tc, target, filename; kind = :shared)) @@ -501,13 +507,14 @@ function compile_llvm_module(f, tt, name=nothing; validate=false, libraries=false, llvm_postprocess=nothing, + symbol=nothing, kwargs..., ) - # (without a name of its own: the name of the function, with the types of the arguments after `julia_` if `demangle` is `false`, as `entry_names` has it) - name === nothing && (name = only(entry_names(((f, tt),); demangle))) - if !demangle - name = "julia_"*name + # (`symbol` is the name in the object, which `entry_names` made; or the name that was given, with `julia_` before it if `demangle` is `false`; or the name that `demangle` says, of the function and its types) + if symbol === nothing + symbol = name === nothing ? only(entry_names(((f, tt),); demangle, target)) : demangle === false ? "julia_" * name : name end + name = symbol job = static_job(f, tt; name, target, strip, only_entry, validate, libraries, kwargs...) m, _ = GPUCompiler.compile(:llvm, job) try @@ -521,76 +528,6 @@ function compile_llvm_module(f, tt, name=nothing; return m end -# The functions of a module are named by the name of the function (`fix_name`). With `demangle = true` those are the names that Julia has: two methods of one function (or two functions that have one name) would be -# two functions of the same name in the module, which LLVM refuses (as an error of the linker, which does not say which entry points they are), so the entry points are to be given functions of their own. -# With `demangle = false` the name is `julia_`, the name of the function, and the types of the arguments (`julia_my_add_Int64_Int64`, `julia_my_add_hello_struct`, `julia_main`), always, so that it does not -# change when methods are added or redefined; and where that is not enough (the same types from two modules, or a name that is very long) the names are numbered, in the order that the methods were defined (the oldest -# first: `..._1`, `..._2`). This gives the names without `julia_`. -function entry_names(funcs; demangle::Bool = true) - base = String[fix_name(f) for (f, _) in funcs] - if demangle - for name in unique(base) - group = findall(==(name), base) - length(group) > 1 || continue - throw(ArgumentError("more than one entry point is named `$name` ($(describe_entries([funcs[i] for i in group]))): with `demangle = true` the name of an entry point is the name of its function, whatever the types of the arguments, " * - "so each needs a function of its own (for example `$(name)_int(a, b) = $name(a, b)` and `$(name)_struct(s) = $name(s)`, and these as the entry points); " * - "with `demangle = false` the names have the types of the arguments: `julia_$(name)_Int64_Int64`, ...")) - end - return base - end - names = String[add_suffix(name, argument_suffix(tt)) for (name, (_, tt)) in zip(base, funcs)] - # (what the types do not tell apart, or what is too long, is numbered: the oldest method first, and in the order that they are given in for those that are as old) - for suffixed in unique(names) - same = findall(==(suffixed), names) - (length(same) > 1 || length(suffixed) > 120) || continue - for (rank, i) in enumerate(sort(same; by = i -> (definition_world(funcs[i]...), i))) - names[i] = string(base[i], "_", rank) - end - end - length(unique(names)) == length(names) || throw(ArgumentError("the names of the entry points made from the types of their arguments are not all different ($(join(names, ", "))): one of the functions has a name like another's with a suffix")) - return names -end - -# the types of the arguments as part of a name: `Int64_Int64`, `Vector_Int64`, `Ptr_Ptr_UInt8`, and nothing for a function that takes none -add_suffix(name, suffix) = isempty(suffix) ? name : string(name, "_", suffix) -function argument_suffix(tt) - types = Base.to_tuple_type(tt).parameters - isempty(types) && return "" - parts = map(types) do T - text = replace(string(T), r"(?:[A-Za-z_]\w*\.)+" => "") # (without the modules: `Main.hello_struct`) - text = replace(fix_name(text), r"_+" => "_") - strip(text, '_') - end - return join(parts, "_") -end - -describe_entries(entries) = join(("$(nameof(f))$(tt isa Tuple ? tt : (tt,))" for (f, tt) in entries), " and ") - -# the world in which the method that is called for these types was defined (later than all that is not a method of Julia code, if there is none) -function definition_world(f, tt) - try - return Base.which(f, Base.to_tuple_type(tt)).primary_world - catch - return typemax(UInt) - end -end - -function compile_llvm_module(funcs::Union{Array,Tuple}; kwargs...) - names = entry_names(funcs; demangle = get(kwargs, :demangle, true)) - (f, tt), rest... = funcs - mod = compile_llvm_module(f, tt, names[1]; kwargs...) - try - for ((f, tt), name) in zip(rest, names[2:end]) - # consumes the module, also when linking fails - link!(mod, compile_llvm_module(f, tt, name; kwargs...)) - end - run!(MergeFunctionsPass(), mod) - catch - dispose(mod) - rethrow() - end - return mod -end """ ```julia @@ -613,9 +550,11 @@ Returns the directory and the path of the file that was written: `filenamebase.o `target` is the [`StaticTarget`](@ref) that the code is made for, which can be one that the host cannot run (see its documentation: the code is then made for the host, retargeted, and the object is made by Julia's LLVM if it has the code generator for the target, and else by the target's `llc`; an error says so if neither can). The default is the native target. -If `demangle` is set to `false`, compiled function names are prefixed with "julia_", -and suffixed with their argument types, if any (`julia_f_Int64_Int64`, `julia_f_Float64`, `julia_main`). -In the event of any remaining collisions, names are further suffixed with a number, in order of method age. +`demangle` determines what the functions are called in the compiled library: +* `true` (the default) as they are in Julia (`my_add`); +* `false`, "julia_", the name of the function, and the types of its arguments (`julia_my_add_Int64_Int64`, `julia_main`; numbered for any remaining collisions); +* `:itanium`, as a C++ compiler would name them (`_Z6my_addll`, which `c++filt` reads as `my_add(long, long)`), with the C and C++ types of the arguments where applicable (`Int32` is `int`, `Ptr{UInt8}` is `unsigned char*`, `Cstring` is `const char*`) and Julia's own types named as classes in the namespace of their module. +Multiple methods of one function, if compiled together into the same library, require `false` or `:itanium`. If `emit_llvm_only` is set, the LLVM IR (`.ll` file) is written in place of the object, for a compiler that makes the object (see [`generate_ir`](@ref)). @@ -689,8 +628,10 @@ end generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle=true, llvm_postprocess=nothing, kwargs...) -> String generate_ir(f, tt; kwargs...) -> String ``` -The LLVM IR (as text) of the functions that `generate_obj` would compile to an object file, for tools that take IR instead (a downstream package that makes the object or -the executable of a target by itself, for example): nothing is written to the file system, and the module (which cannot outlive its context) is not returned. +The LLVM IR (as text) of the functions that `generate_obj` would compile to an object file, for tools that take IR instead +(a downstream package that makes the object or the executable of a target by itself, for example). + +Nothing is written to the file system, and the module (which cannot outlive its context) is not returned. """ function generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle = true, main_wrapper = nothing, main_prologue = nothing, llvm_postprocess = nothing, llvm_finalize = nothing, kwargs...) GPUCompiler.JuliaContext() do context diff --git a/src/llvm.jl b/src/llvm.jl index 5257c6ec..0630f84d 100644 --- a/src/llvm.jl +++ b/src/llvm.jl @@ -123,9 +123,8 @@ function add_main_wrapper!(mod::LLVM.Module, entry::AbstractString, target::Stat end # Export the compiled entry points from a Windows DLL -function add_dllexport!(mod::LLVM.Module, funcs; demangle=true) - for (name, (f, _)) in zip(entry_names(funcs; demangle), funcs) - name_f = (demangle ? "" : "julia_") * name +function add_dllexport!(mod::LLVM.Module, funcs; demangle=true, target=nothing) + for name_f in entry_names(funcs; demangle, target) llvmf = get(mod.functions, name_f, nothing) if llvmf !== nothing && !isdeclaration(llvmf) llvmf.dllstorage = LLVM.DLLStorageClass.Export diff --git a/src/mangling.jl b/src/mangling.jl new file mode 100644 index 00000000..7d69d23b --- /dev/null +++ b/src/mangling.jl @@ -0,0 +1,282 @@ +# The names of the entry points in the object, which depends on `demangle`: +# * `true`, the names that Julia has (`fix_name`); +# * `false`, `julia_`, the name of the function, and the types of its arguments (`julia_my_add_Int64_Int64`, `julia_main`); +# * `:itanium`, the names that a C++ compiler makes (`_Z6my_addll`, see below). +# Two entry points of one name would be two functions of the same name in the module, which LLVM refuses (as an error of the linker, which does not say which entry points they are). +function entry_names(funcs; demangle::Union{Bool,Symbol} = true, target = nothing) + demangle in (true, false, :itanium) || throw(ArgumentError("`demangle` is `true`, `false` or `:itanium`, not `$(repr(demangle))`")) + base = String[fix_name(f) for (f, _) in funcs] + if demangle === :itanium + long64 = !(target isa StaticTarget && iswindows(target)) # (`long` has 32 bits on Windows) + names = String[itanium_name(f, tt; long64) for (f, tt) in funcs] + for name in unique(names) + count(==(name), names) > 1 || continue + throw(ArgumentError("More than one entry point is named `$name` ($(describe_entries([funcs[i] for i in findall(==(name), names)])))")) + end + return names + elseif demangle === true + for name in unique(base) + group = findall(==(name), base) + length(group) > 1 || continue + throw(ArgumentError("More than one entry point is named `$name` ($(describe_entries([funcs[i] for i in group]))). " * + "With `demangle = true` the name of an entry point is the name of its function, regardless of argument types, " * + "so each needs a function of its own (for example `$(name)_int(a, b) = $name(a, b)` and `$(name)_struct(s) = $name(s)`); " * + "with `demangle = false` the names include argument types (e.g., `julia_$(name)_Int64_Int64`) while `demangle = :itanium` follows the Itanium C++ convention")) + end + return base + else + names = String[add_suffix(name, argument_suffix(tt)) for (name, (_, tt)) in zip(base, funcs)] + # (what the types do not tell apart, or what is too long, is numbered: the oldest method first, and in the order that they are given in for those that are as old) + for suffixed in unique(names) + same = findall(==(suffixed), names) + (length(same) > 1 || length(suffixed) > 120) || continue + for (rank, i) in enumerate(sort(same; by = i -> (definition_world(funcs[i]...), i))) + names[i] = string(base[i], "_", rank) + end + end + for name in unique(names) + count(==(name), names) > 1 || continue + throw(ArgumentError("More than one entry point is named `$name` ($(describe_entries([funcs[i] for i in findall(==(name), names)])))")) + end + return "julia_" .* names + end +end + +# the types of the arguments as part of a name: `Int64_Int64`, `Vector_Int64`, `Ptr_Ptr_UInt8`, and nothing for a function that takes none +add_suffix(name, suffix) = isempty(suffix) ? name : string(name, "_", suffix) +function argument_suffix(tt) + types = Base.to_tuple_type(tt).parameters + isempty(types) && return "" + parts = map(types) do T + text = replace(string(T), r"(?:[A-Za-z_]\w*\.)+" => "") # (without the modules: `Main.hello_struct`) + text = replace(fix_name(text), r"_+" => "_") + strip(text, '_') + end + return join(parts, "_") +end + +describe_entries(entries) = join(("$(nameof(f))$(tt isa Tuple ? tt : (tt,))" for (f, tt) in entries), " and ") + +# the world in which the method that is called for these types was defined (later than all that is not a method of Julia code, if there is none) +function definition_world(f, tt) + try + return Base.which(f, Base.to_tuple_type(tt)).primary_world + catch + return typemax(UInt) + end +end + +function compile_llvm_module(funcs::Union{Array,Tuple}; kwargs...) + names = entry_names(funcs; demangle = get(kwargs, :demangle, true), target = get(kwargs, :target, nothing)) + (f, tt), rest... = funcs + mod = compile_llvm_module(f, tt; kwargs..., symbol = names[1]) + try + for ((f, tt), name) in zip(rest, names[2:end]) + # consumes the module, also when linking fails + link!(mod, compile_llvm_module(f, tt; kwargs..., symbol = name)) + end + run!(MergeFunctionsPass(), mod) + catch + dispose(mod) + rethrow() + end + return mod +end + + +# Itanium C++ ABI names for entry points (`demangle = :itanium`): `my_add(Int, Int)` is `_Z6my_addll`, which `c++filt` reads as `my_add(long, long)` and a C++ program that declares +# `long my_add(long, long)` links to. https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling +# +# The types are the C and C++ types that the Julia types have, where there are: the numbers (`Int32` is `int`, `UInt8` is `unsigned char`, `Float64` is `double`, `Bool` is `bool`, `Char` is `char32_t`), +# `Ptr{T}` (`T*`, and `Ptr{Cvoid}` is `void*`), `Cstring` (`const char*`) and `Complex` (C99's `_Complex`). Julia's own types (`Vector{Int}`, structs, tuples, ...) are named as C++ classes are, in the namespace of +# their module (`Main` is the global one): `Array` for `Vector{Int}`, and the parameters that are not types are values (`Val{3}` is `Val<3l>`), or names where they have none (`Val{:a}` is `Val`). +# A Julia type is not a C++ type, so these names are for linking by name and for reading, not for a C++ compiler to check the types of. Not done: the operators of C++ for the functions that Julia has them for +# (`+` is `pl`: here `_`), the return types of functions, and the types that C++ has qualifiers for. +# +# Repeated parts are replaced by the substitutions that the ABI has (`S_`, `S0_`, ...), as a compiler does, so that the names are the same as a C++ compiler's for the types that it has. + +# a type, as the ABI writes it +abstract type MType end +struct MBuiltin <: MType; code::String; end +struct MPointer <: MType; to::MType; end +struct MConstant <: MType; of::MType; end +struct MComplex <: MType; of::MType; end +struct MNamed <: MType # `scope::name`, and `` if `args` is not `nothing` + scope::Vector{String} + name::String + args::Union{Nothing,Vector{Any}} # types (`MType`) and values (`MValue`) +end +struct MValue # `LE`, for the parameter that is a number + code::String + text::String +end +struct MPack # `J...E`, the parameters that a template takes any number of (Julia's `Tuple`) + items::Vector{Any} +end + +mangled_length(s::AbstractString) = string(ncodeunits(s), s) +# the parts of a name as a prefix of the ABI: `3Foo3Bar` +prefix_text(parts) = join(mangled_length.(parts)) + +# the key by which the ABI finds a part again (its spelling without the substitutions) +key(t::MBuiltin) = t.code +key(t::MPointer) = "P" * key(t.to) +key(t::MConstant) = "K" * key(t.of) +key(t::MComplex) = "C" * key(t.of) +key(t::MNamed) = prefix_text([t.scope; t.name]) * (t.args === nothing ? "" : template_text(t.args)) +key(v::MValue) = string("L", v.code, v.text, "E") +key(p::MPack) = string("J", join(key.(p.items)), "E") +template_text(args) = string("I", join(key.(args)), "E") + +mutable struct Mangler + substitutions::Vector{String} +end +Mangler() = Mangler(String[]) + +# `S_`, `S0_`, `S1_`, ..., `S9_`, `SA_`, ..., `SZ_`, `S10_`, ... (the first is `S_`, and then the number, written in base 36, of the one before) +substitution(i::Int) = i == 0 ? "S_" : string("S", uppercase(string(i - 1; base = 36)), "_") + +# the substitution that stands for `k`, if it has been seen +function seen(m::Mangler, k::AbstractString) + i = findfirst(==(k), m.substitutions) + return i === nothing ? nothing : substitution(i - 1) +end + +emit!(m::Mangler, t::MBuiltin) = t.code +emit!(m::Mangler, v::MValue) = string("L", v.code, v.text, "E") +emit!(m::Mangler, p::MPack) = string("J", join(emit!(m, a) for a in p.items), "E") +function emit!(m::Mangler, t::MType) + k = key(t) + s = seen(m, k) + s === nothing || return s + out = emit_new!(m, t) + push!(m.substitutions, k) # (after its parts, which are in the name before it) + return out +end +emit_new!(m::Mangler, t::MPointer) = "P" * emit!(m, t.to) +emit_new!(m::Mangler, t::MConstant) = "K" * emit!(m, t.of) +emit_new!(m::Mangler, t::MComplex) = "C" * emit!(m, t.of) + +# the text of a name (`3Foo`, or `N3Foo3BarE` if it has a scope): the longest prefix of the scope that has been seen is a substitution, and the prefixes that have not are registered. The name itself is registered by the caller, except +# a template's, which is before its parameters (`template` is the list of them) +function emit_name!(m::Mangler, parts::Vector{String}; template::Union{Nothing,Vector{Any}} = nothing) + n = length(parts) + out = IOBuffer() + start = 1 + for j in n-1:-1:1 + s = seen(m, prefix_text(parts[1:j])) + if s !== nothing + print(out, s) + start = j + 1 + break + end + end + for j in start:n-1 + print(out, mangled_length(parts[j])) + push!(m.substitutions, prefix_text(parts[1:j])) + end + if template === nothing + print(out, mangled_length(parts[n])) + text = String(take!(out)) + return n == 1 ? text : string("N", text, "E") + end + # (a template that has been seen is `S_I...E`: a substitution is not in `N...E`) + name = seen(m, prefix_text(parts)) + name === nothing || return n == 1 ? string(name, template_arguments!(m, template)) : string("N", name, template_arguments!(m, template), "E") + push!(m.substitutions, prefix_text(parts)) + arguments = template_arguments!(m, template) + text = string(String(take!(out)), mangled_length(parts[n])) + return n == 1 ? string(text, arguments) : string("N", text, arguments, "E") +end + +template_arguments!(m::Mangler, args) = string("I", join(emit!(m, a) for a in args), "E") + +function emit_new!(m::Mangler, t::MNamed) + parts = [t.scope; t.name] + return emit_name!(m, parts; template = t.args) +end + +## Julia types as the types of the ABI + +# the names of the modules that a type or function is in, without `Main` +function module_scope(mod::Module) + names = String[string(s) for s in fullname(mod)] + !isempty(names) && names[1] == "Main" && popfirst!(names) + return String[identifier(n) for n in names] +end + +# a name that C++ has: letters, digits and `_`, and not starting with a digit +function identifier(s::AbstractString) + t = fix_name(String(s)) + isempty(t) && return "_" + return isdigit(first(t)) ? "_" * t : t +end + +# the name of a type or function: that of the function for the type of a function (`typeof(sin)` is `#sin`), without the `#` of the names that Julia makes +type_identifier(T::DataType) = identifier(lstrip(string(T.name.name), '#')) + +function value_argument(p, long64::Bool) + p isa Bool && return MValue("b", p ? "1" : "0") + if p isa Integer + code = builtin_code(typeof(p), long64) + text = p < 0 ? string("n", -BigInt(p)) : string(p) + return MValue(code, text) + end + p isa Char && return MValue("Di", string(UInt32(p))) + p isa Symbol && return MNamed(String[], "Symbol_" * identifier(string(p)), nothing) + return MNamed(String[], "Value_" * identifier(string(p)), nothing) # (a value that has no spelling in C++) +end + +const BUILTIN_CODES = Dict{DataType,String}(Bool => "b", Int8 => "a", UInt8 => "h", Int16 => "s", UInt16 => "t", Int32 => "i", UInt32 => "j", Int128 => "n", UInt128 => "o", + Float16 => "Dh", Float32 => "f", Float64 => "d", Char => "Di") +function builtin_code(T::DataType, long64::Bool) + T === Int64 && return long64 ? "l" : "x" # (`long` is 32 bits on Windows) + T === UInt64 && return long64 ? "m" : "y" + return get(BUILTIN_CODES, T, "") +end + +# a Julia type as a type of the ABI (`long64`: whether `long` is 64 bits, as it is except on Windows) +function mangled_type(@nospecialize(T), long64::Bool) + if T isa DataType + code = builtin_code(T, long64) + isempty(code) || return MBuiltin(code) + T === Cstring && return MPointer(MConstant(MBuiltin("c"))) + T === Cwstring && return MPointer(MConstant(MBuiltin("w"))) + if T <: Ptr && length(T.parameters) == 1 + to = T.parameters[1] + return MPointer(to === Cvoid || to === Nothing ? MBuiltin("v") : mangled_type(to, long64)) + end + if T <: Complex && length(T.parameters) == 1 && haskey(BUILTIN_CODES, T.parameters[1]) && T.parameters[1] <: AbstractFloat + return MComplex(mangled_type(T.parameters[1], long64)) + end + params = T.parameters + items = Any[p isa Type || p isa TypeVar ? mangled_type(p, long64) : value_argument(p, long64) for p in params] + args = T <: Tuple ? Any[MPack(items)] : isempty(params) ? nothing : items + # (a function's type is named by the function, in the module of the function: `typeof(Base.sin)` is `Base::sin`) + scope = module_scope(parentmodule(T)) + return MNamed(scope, type_identifier(T), args) + elseif T isa TypeVar + return MNamed(String[], "TypeVar_" * identifier(string(T.name)), nothing) + elseif T isa UnionAll + return mangled_type(Base.unwrap_unionall(T), long64) + elseif T isa Union + return MNamed(String[], "Union", Any[mangled_type(t, long64) for t in Base.uniontypes(T)]) + end + return MNamed(String[], "Type_" * identifier(string(T)), nothing) +end + +""" + itanium_name(f, types; long64 = true) -> String + +The name of the entry point `f(types...)` in the Itanium C++ ABI: `_Z`, the name of the function (in the namespace of its module), and the types of the arguments (`v` for none). +`long64` says whether `long` has 64 bits (`Int64` is `l`, and `x`, `long long`, if not: Windows). +""" +function itanium_name(f, types; long64::Bool = true) + ts = Base.to_tuple_type(types).parameters + m = Mangler() + parts = [module_scope(parentmodule(typeof(f))); identifier(string(nameof(f)))] + # (the name of a function: its scope is substituted and registered as that of a type is, and the name is not) + name = emit_name!(m, parts) + params = isempty(ts) ? "v" : join(emit!(m, mangled_type(T, long64)) for T in ts) + return string("_Z", name, params) +end diff --git a/test/testcore.jl b/test/testcore.jl index 7609bdbe..967853c7 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -512,13 +512,13 @@ name_clash_pair(s) = name_clash(s) @testset "Entry points of one name" begin entries = ((name_clash, Tuple{NameClashPair}), (name_clash, Tuple{Int,Int,Int}), (name_clash, Tuple{Int,Int})) err = try StaticCompiler.generate_obj(entries, mktempdir(), "clash"; demangle = true); nothing catch e e end - @test err isa ArgumentError && occursin("more than one entry point is named `name_clash`", err.msg) && occursin("demangle = false", err.msg) + @test err isa ArgumentError && occursin("More than one entry point is named `name_clash`", err.msg) && occursin("demangle = false", err.msg) # (the names have the types, whatever the order that the methods were defined or are given in) - @test StaticCompiler.entry_names(entries; demangle = false) == ["name_clash_NameClashPair", "name_clash_Int64_Int64_Int64", "name_clash_Int64_Int64"] + @test StaticCompiler.entry_names(entries; demangle = false) == ["julia_name_clash_NameClashPair", "julia_name_clash_Int64_Int64_Int64", "julia_name_clash_Int64_Int64"] @test StaticCompiler.entry_names(reverse(entries); demangle = false) == reverse(StaticCompiler.entry_names(entries; demangle = false)) @test StaticCompiler.argument_suffix(()) == "" && StaticCompiler.argument_suffix(Tuple{Vector{Int},Ptr{Ptr{UInt8}}}) == "Vector_Int64_Ptr_Ptr_UInt8" # (what the types do not tell apart is numbered, the oldest method first) - @test StaticCompiler.entry_names(((name_clash, Tuple{Int,Int}), (name_clash, Tuple{Int,Int})); demangle = false) == ["name_clash_1", "name_clash_2"] + @test StaticCompiler.entry_names(((name_clash, Tuple{Int,Int}), (name_clash, Tuple{Int,Int})); demangle = false) == ["julia_name_clash_1", "julia_name_clash_2"] ir = StaticCompiler.generate_ir(entries; demangle = false) @test occursin(r"define [^\n]*@julia_name_clash_Int64_Int64\(i64[^,\n]*, i64[^,\n)]*\)", ir) # (the method of two numbers) @test count(r"define [^\n]*@julia_name_clash_\w+\(", ir) == 3 @@ -526,9 +526,49 @@ name_clash_pair(s) = name_clash(s) _, obj = StaticCompiler.generate_obj(((name_clash_int, Tuple{Int,Int}), (name_clash_pair, Tuple{NameClashPair})), mktempdir(), "noclash") @test isfile(obj) # (with `demangle = false` the types are in every name, also of an entry point that is alone: `julia_`, the function, and the types if it has arguments) - @test StaticCompiler.entry_names(((name_clash_int, Tuple{Int,Int}),); demangle = false) == ["name_clash_int_Int64_Int64"] - @test StaticCompiler.entry_names(((name_clash_int, ()),); demangle = false) == ["name_clash_int"] # (nothing for a function that takes no arguments) + @test StaticCompiler.entry_names(((name_clash_int, Tuple{Int,Int}),); demangle = false) == ["julia_name_clash_int_Int64_Int64"] + @test StaticCompiler.entry_names(((name_clash_int, ()),); demangle = false) == ["julia_name_clash_int"] # (nothing for a function that takes no arguments) @test occursin("@julia_name_clash_int_Int64_Int64(", string(StaticCompiler.static_llvm_module(name_clash_int, Tuple{Int,Int}; demangle = false))) # (one entry point, or names that differ, are as they were) @test StaticCompiler.entry_names(((name_clash_int, Tuple{Int,Int}), (name_clash_pair, Tuple{NameClashPair})); demangle = true) == ["name_clash_int", "name_clash_pair"] end + +# `demangle = :itanium`: the names of the Itanium C++ ABI, which these are (checked with the names that clang makes of the same declarations in C++: `_Z6my_addll`, ...) +module ItaniumNames + module Inner + struct P; a::Int; end + struct Q{T}; a::Int; end + h(a, b, c, d, e, f) = 1 + k(a, b, c, d) = 1 + end +end +struct ItaniumPoint; a::Int; end +itanium_add(a, b) = a + b +itanium_add(s::ItaniumPoint) = s.a +itanium_none() = 1 +itanium_many(a...) = 1 +@testset "Itanium names" begin + en(f, tt; kw...) = only(StaticCompiler.entry_names(((f, tt),); demangle = :itanium, kw...)) + @test en(itanium_add, (Int, Int)) == "_Z11itanium_addll" + @test en(itanium_add, (ItaniumPoint,)) == "_Z11itanium_add12ItaniumPoint" + @test en(itanium_none, ()) == "_Z12itanium_nonev" + # (`long` has 32 bits on Windows: `long long`, `x`) + @test en(itanium_add, (Int, UInt); target = StaticCompiler.StaticTarget("x86_64-w64-mingw32")) == "_Z11itanium_addxy" + # the numbers, pointers, strings and complex numbers + @test en(itanium_many, (Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, Float32, Float64, Char, Bool)) == "_Z12itanium_manyahstijlmnofdDib" + @test en(itanium_many, (Ptr{UInt8}, Ptr{UInt8}, Ptr{Cvoid}, Cstring, ComplexF64)) == "_Z12itanium_manyPhS_PvPKcCd" + # types of Julia as C++ classes in the namespaces of their modules, and repeated parts replaced by substitutions + @test en(itanium_many, (Vector{Int}, Vector{Int}, Tuple{Int,Float64}, Tuple{})) == "_Z12itanium_manyN4Core5ArrayIlLl1EEES1_NS_5TupleIJldEEENS2_IJEEE" + P, Q = ItaniumNames.Inner.P, ItaniumNames.Inner.Q + @test en(ItaniumNames.Inner.h, (Ptr{P}, P, Q{P}, Q{P}, Q{Int}, Q{Q{P}})) == "_ZN12ItaniumNames5Inner1hEPNS0_1PES1_NS0_1QIS1_EES4_NS3_IlEENS3_IS4_EE" + @test en(ItaniumNames.Inner.k, (Array{P,2}, Array{P,2}, Dict{Int,Float64}, Dict{Float64,Float64})) == "_ZN12ItaniumNames5Inner1kEN4Core5ArrayINS0_1PELl2EEES4_N4Base4DictIldEENS6_IddEE" + # the object has them + # (of the host's own target: `long` is 32 bits on Windows, where `Int` is `x`) + entries = ((itanium_add, (Int, Int)), (itanium_add, (ItaniumPoint,))) + expected = StaticCompiler.entry_names(entries; demangle = :itanium, target = StaticCompiler.StaticTarget()) + @test expected == [Sys.iswindows() ? "_Z11itanium_addxx" : "_Z11itanium_addll", "_Z11itanium_add12ItaniumPoint"] + _, obj = StaticCompiler.generate_obj(entries, mktempdir(), "itanium"; demangle = :itanium) + bytes = String(read(obj)) + @test all(name -> occursin(name, bytes), expected) + @test_throws ArgumentError StaticCompiler.entry_names(((itanium_add, (Int, Int)),); demangle = :nope) +end From 57ad1ed665695e0588762cca586e127ddccfa8a0 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 2 Dec 2026 00:37:23 -0500 Subject: [PATCH 200/202] Cleaner division of work between `generate_*` and and `compile_*` functions --- README.md | 2 +- src/StaticCompiler.jl | 362 +++++++++++++++++++++--------------------- src/llvm.jl | 2 +- src/mangling.jl | 18 --- src/target.jl | 2 +- test/cross/matrix.jl | 2 +- test/testcore.jl | 25 ++- test/testquirks.jl | 2 +- 8 files changed, 198 insertions(+), 217 deletions(-) diff --git a/README.md b/README.md index cf7ec97f..83d0fa3a 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ For an [`LLDToolchain`](@ref), supported target flavors include: * `:darwin` for macOS * `:mingw` for Windows * `:wasm` for WebAssembly -Julia's LLVM contains the code generators for its own architecture and for WebAssembly only. For any other architecture, the IR can be made (`emit_llvm_only = true`), and `StaticTarget(...; llc = "/path/to/llc")` makes the object with an `llc` that has the desired backend for the same LLVM version. +Julia's LLVM contains the code generators for its own architecture and for WebAssembly only. For any other architecture, the IR can be made (`llvm_to_clang = true` / `generate_ir`), and `StaticTarget(...; llc = "/path/to/llc")` makes the object with an `llc` that has the desired backend for the same LLVM version. The [`cross-matrix.yml`](.github/workflows/cross-matrix.yml) CI workfoow tests cross-compilation to and from Linux (x86-64 and AArch64), macOS, and Windows. diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 5405af80..24a7ef2d 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -21,7 +21,7 @@ include("toolchain.jl") include("builtins.jl") include("llvm.jl") -fix_name(f::Function) = fix_name(string(nameof(f))) +fix_name(f::Function) = fix_name(String(nameof(f))) fix_name(s) = String(GPUCompiler.safe_name(s)) include("mangling.jl") @@ -40,9 +40,8 @@ end """ ```julia -compile_executable(f::Function, types::Tuple, [path::String=pwd()], [name::String=string(nameof(f))]; - filename::String = name, - also_expose = Tuple{Function, Tuple{DataType}}[], +compile_executable(f::Function, tt=(), args...; also_expose::Vector=Tuple[], kwargs...) +compile_executable(funcs::Union{Array,Tuple}, [path::String=pwd()], [filename::String=fix_name(first(first(funcs)))]; target = StaticTarget(), # The target for compilation, including OS and architecture as well as other options demangle = true, # Whether or not function names should be exactly as they are in Julia llvm_to_clang = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR @@ -57,14 +56,12 @@ compile_executable(f::Function, types::Tuple, [path::String=pwd()], [name::Strin ``` Attempt to compile a standalone executable that runs function `f` with a type signature given by the tuple of `types`. -If there are extra methods you would like to protect from name mangling in the produced binary for whatever reason, +If there are extra methods you would like to expose in the produced binary for any reason, you can provide them as a vector of tuples of functions and types, i.e. `[(f1, types1), (f2, types2), ...]` -There are two hooks that may modify the LLVM code, which differ in when they run: -* `llvm_postprocess(mod)` is called on the `LLVM.Module` of each function as soon as it is generated, before its pointers and runtime calls are located, it is retargeted, and the modules are combined. It may modify it in place - (for instance, to define the runtime functions that the code calls). -* `llvm_finalize(mod)` is called once, on the whole module when it is complete: after the modules are combined and the `main` (see `main_prologue`) and the builtins of the toolchain are added, and just before the code is - emitted. Use it for what has to see all of that (see [`generate_obj`](@ref)). +There are two hooks that may modify the LLVM code in-place, which differ in when they run: +* `llvm_postprocess(mod)` is called on the `LLVM.Module` of each function as soon as it is generated, before its pointers and runtime calls are located, it is retargeted, and the modules are combined. +* `llvm_finalize(mod)` is called once, on the final combined module when it is complete: after the modules are combined and the `main` (see `main_prologue`) and the builtins of the toolchain are added, and just before the code is emitted. `main_prologue(builder, mod)` adds code that runs first in the `main` of the program (the object then has its own `main`, also where a C compiler links it). `llvm_finalize` and `main_prologue` work the same for any target and any toolchain (the host's or another, linked by a C compiler driver or by `lld`). Objects to link with the program can be given in `cflags`. @@ -128,15 +125,8 @@ shell> ./hello Hello, world! ``` """ -function compile_executable(f::Function, types=(), path::String=pwd(), name=fix_name(f); - also_expose::Vector = Tuple{Function, Tuple{DataType}}[], - kwargs... - ) - compile_executable(vcat([(f, types)], also_expose), path, name; kwargs...) -end - -function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name=fix_name(first(first(funcs))); - filename::String = name, +compile_executable(f::Function, tt=(), args...; also_expose::Vector=Tuple[], kwargs...) = compile_executable(vcat([(f, tt)], also_expose), args...; kwargs...) +function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), filename::String=fix_name(first(first(funcs))); target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR @@ -148,23 +138,21 @@ function compile_executable(funcs::Union{Array,Tuple}, path::String=pwd(), name= kwargs... ) - (f, types) = funcs[1] + (f, types) = first(funcs) tt = check_signature(f, types; target, kwargs...) isexecutableargtype = tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables" - generate_executable(funcs, path, name, filename; target, demangle, cc, cflags, llvm_to_clang, llvm_postprocess, llvm_finalize, main_prologue, kwargs...) - joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : CCToolchain(cc), target, filename)) + generate_executable(funcs, path, filename; target, demangle, cc, cflags, llvm_to_clang, llvm_postprocess, llvm_finalize, main_prologue, kwargs...) end """ ```julia -compile_shlib(f::Function, types::Tuple, [path::String=pwd()], [name::String=string(nameof(f))]; kwargs...) -compile_shlib(funcs::Array, [path::String=pwd()]; - filename="libfoo", - target = StaticTarget(), # The target for compilation, including OS and architecture as well as other options - demangle = true, # Whether or not function names should be exactly as they are in Julia - llvm_to_clang = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR +compile_shlib(f::Function, types::Tuple, [path::String=pwd()], [filename=fix_name(f)]; kwargs...) +compile_shlib(funcs::Union{Array,Tuple}, [path::String=pwd()], [filename::String="lib"]; + target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options + demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names + llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR cc = nothing, # Compiler, may specify to override defaults cflags = ``, # Compiler flags, may specify to override defaults llvm_postprocess = nothing, # optional hook: called on the each function's module in turn @@ -172,11 +160,12 @@ compile_shlib(funcs::Array, [path::String=pwd()]; kwargs... ) ``` -As `compile_executable`, but compiling to a standalone `.dylib`/`.so` shared library. +Attempt to compile a standalone `.dylib`/`.so` shared library, given a function and its argument types, +or a vector of tuples thereof (i.e., `[(f1, types1), (f2, types2), ...]`). -Arguments and returned values from `compile_shlib` must be native objects such as `Int`, `Float64`, or `Ptr`. -They should not be things like `Tuple{Int, Int}` because that is not natively sized. -Such objects should to be passed by reference instead of by value. +Arguments and returned values from compiled functions shoul be native objects such as `Int`, `Float64`, or `Ptr`. +They should not be things like `Tuple{Int, Int}` that are not natively sized; +such objects should to be passed by reference instead of by value. The `demangle` keyword argument determines what the functions are called in the compiled library: * `true` (the default) as they are in Julia (`my_add`); @@ -207,15 +196,8 @@ julia> ccall(("test", "test.dylib"), Float64, (Int64,), 100_000) 5.2564961094956075 ``` """ -function compile_shlib(f::Function, types=(), path::String=pwd(), name=fix_name(f); - filename = name, - kwargs... - ) - compile_shlib(((f, types),), path; filename, kwargs...) -end -# As above, but taking an array of functions and returning a single shlib -function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); - filename = "libfoo", +compile_shlib(f::Function, tt=(), path::String=pwd(), filename=fix_name(f); kwargs...) = compile_shlib(((f, tt),), path, filename; kwargs...) +function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(), filename::String="lib"; target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR @@ -230,8 +212,6 @@ function compile_shlib(funcs::Union{Array,Tuple}, path::String=pwd(); end generate_shlib(funcs, path, filename; target, demangle, cc, cflags, llvm_to_clang, llvm_postprocess, llvm_finalize, kwargs...) - - joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : CCToolchain(cc), target, filename; kind = :shared)) end @@ -281,32 +261,25 @@ end """ ```julia -generate_executable(f, tt, path::String, name, filename=string(name); - target::StaticTarget=StaticTarget(), demangle=true, cc=nothing, cflags=``, llvm_to_clang=needs_ir(target), - llvm_postprocess=nothing, llvm_finalize=nothing, main_prologue=nothing, kwargs...) -``` -Attempt to compile a standalone executable that runs `f`. -Low-level interface; you should generally use `compile_executable` instead. - -The keywords are those of `compile_executable`; the hooks are passed to [`generate_obj`](@ref). -With a `main_prologue` the object has its own `main`, whichever the toolchain. - -### Examples -```julia -julia> using StaticCompiler, StaticTools - -julia> hello() = println(c"Hello, world!") - -julia> path, name = StaticCompiler.generate_executable(hello, Tuple{}, "./") -("./", "hello") - -shell> ./hello -Hello, world! +generate_executable(f::Function, tt=(), args...; also_expose::Vector, kwargs...) +generate_executable(funcs::Union{Array,Tuple}, [path=tempname()], [filename=fix_name(first(first(funcs)))]; + target::StaticTarget = StaticTarget(), # The target for compilation, including OS and architecture as well as other options + demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names + llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Compiler flags, may specify to override defaults + llvm_postprocess = nothing, # optional hook: called on the each function's module in turn + llvm_finalize = nothing, # optional hook: called on the final combined module when it is complete + main_prologue = nothing, # optional hook: adds code that runs first in `main`, into which `f` will then be wrapped + kwargs... +) ``` +Lower-level interface for standalone executable compilation. +As [`compile_executable`](@ref), but without argument or return type signature checking. """ -generate_executable(f, tt, args...; kwargs...) = generate_executable(((f, tt),), args...; kwargs...) -function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fix_name(first(first(funcs))), filename=name; - target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options +generate_executable(f::Function, tt=(), args...; also_expose::Vector=Tuple{Function, Tuple{DataType}}[], kwargs...) = generate_executable(vcat([(f, tt)], also_expose), args...; kwargs...) +function generate_executable(funcs::Union{Array,Tuple}, path::String=tempname(), filename::String=fix_name(first(first(funcs))); + target::StaticTarget = StaticTarget(), # The target for compilation, including OS and architecture as well as other options demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR cc = nothing, # Compiler, may specify to override defaults @@ -321,75 +294,43 @@ function generate_executable(funcs::Union{Array,Tuple}, path=tempname(), name=fi # Prepare objects or (if llvm_to_clang is set) IR. A linker that is not a compiler driver is given an object that has a `main`, and so is a driver when # `main_prologue` asks for code to run first, which the `main` of the object has (the driver would be given a `main` of its own, with nothing before the entry point) - fn = demangle === true ? "$name" : entry_names(funcs; demangle, target)[1] # (the name of the first function, as `demangle` says) + fn = first(entry_names(funcs; demangle, target)) main_wrapper = !(tc isa CCToolchain) || !isnothing(main_prologue) ? fn : nothing - _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...) + entry = isnothing(main_wrapper) ? fn : nothing + obj_or_ir_path = if llvm_to_clang + generate_ir(funcs, path, filename; demangle, target, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...) + else + generate_obj(funcs, path, filename; demangle, target, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...) + end # Compile and/or link executable! exec_path = joinpath(path, output_name(tc, target, filename)) - link(tc, target, [obj_or_ir_path], exec_path; kind = :executable, entry = isnothing(main_wrapper) ? fn : nothing, cflags) - return path, name + link(tc, target, [obj_or_ir_path], exec_path; entry, cflags, kind = :executable) + + return joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : CCToolchain(cc), target, filename)) end """ ```julia -generate_shlib(f::Function, tt, [path::String], [name], [filename]; kwargs...) -generate_shlib(funcs::Array, [path::String], [filename::String]; - target::StaticTarget=StaticTarget(), - demangle=true, - llvm_to_clang=needs_ir(target), - cc=nothing, - cflags=``, - llvm_postprocess=nothing, l - lvm_finalize=nothing, +generate_shlib(f::Function, types::Tuple, [path::String=tempname()], [filename=fix_name(f)]; kwargs...) +generate_shlib(funcs::Array, [path::String=tempname()], [filename::String = "lib"]; + target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options + demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names + llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR + cc = nothing, # Compiler, may specify to override defaults + cflags = ``, # Compiler flags, may specify to override defaults + llvm_postprocess = nothing, # optional hook: called on the each function's module in turn + llvm_finalize = nothing, # optional hook: called on the final combined module when it is complete kwargs... ) ``` -Low level interface for compiling a shared object / dynamically loaded library - (`.so` / `.dylib`) for function `f` given a tuple type `tt` characterizing -the types of the arguments for which the function will be compiled. - -The `demangle` keyword argument determines what the functions are called in the compiled library: -* `true` (the default) as they are in Julia (`my_add`); -* `false`, "julia_", the name of the function, and the types of its arguments (`julia_my_add_Int64_Int64`, `julia_main`; numbered for any remaining collisions); -* `:itanium`, as a C++ compiler would name them (`_Z6my_addll`, which `c++filt` reads as `my_add(long, long)`), with the C and C++ types of the arguments where applicable (`Int32` is `int`, `Ptr{UInt8}` is `unsigned char*`, `Cstring` is `const char*`) and Julia's own types named as classes in the namespace of their module. -Multiple methods of one function, if compiled together into the same library, require `false` or `:itanium`. - -### Examples -```julia -julia> using StaticCompiler, LoopVectorization - -julia> function test(n) - r = 0.0 - @turbo for i=1:n - r += log(sqrt(i)) - end - return r/n - end -test (generic function with 1 method) - -julia> path, name = StaticCompiler.generate_shlib(test, Tuple{Int64}, true, "./example") -("./example", "test") - -shell> tree \$path -./example -|-- test.dylib -`-- test.o -0 directories, 2 files - -julia> test(100_000) -5.2564961094956075 - -julia> ccall(("test", "example/test.dylib"), Float64, (Int64,), 100_000) -5.2564961094956075 +Lower-level interface for compiling a standalone `.dylib`/`.so` shared object. +As [`compile_shlib`](@ref), but without argument or return type signature checking. ``` """ -function generate_shlib(f::Function, tt, path::String=tempname(), name=fix_name(f), filename=name; target=StaticTarget(), kwargs...) - generate_shlib(((f, tt),), path, filename; target, kwargs...) -end -# As above, but taking an array of functions and returning a single shlib -function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), filename::String="libfoo"; +generate_shlib(f::Function, tt=(), path::String=tempname(), filename=fix_name(f); kwargs...) = generate_shlib(((f, tt),), path, filename; kwargs...) +function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), filename::String="lib"; target::StaticTarget=StaticTarget(), # The target for compilation, including OS and architecture as well as other options demangle::Union{Bool,Symbol} = true, # `true`: names as they are in Julia; `false`: `julia_` and the types of the arguments; `:itanium`: C++ names llvm_to_clang::Bool = needs_ir(target), # If true, compilation is completed by an external compiler given our LLVM IR @@ -408,13 +349,16 @@ function generate_shlib(funcs::Union{Array,Tuple}, path::String=tempname(), file isnothing(llvm_finalize) || llvm_finalize(mod) end lib_path = joinpath(path, output_name(tc, target, filename; kind = :shared)) - _, obj_or_ir_path = generate_obj(funcs, path, filename; demangle, target, emit_llvm_only=llvm_to_clang, - llvm_postprocess, llvm_finalize=finalize, kwargs...) + obj_or_ir_path = if llvm_to_clang + generate_ir(funcs, path, filename; demangle, target, llvm_postprocess, llvm_finalize=finalize, kwargs...) + else + generate_obj(funcs, path, filename; demangle, target, llvm_postprocess, llvm_finalize=finalize, kwargs...) + end # Compile and/or link shlib! - link(tc, target, [obj_or_ir_path], lib_path; kind = :shared, cflags) + link(tc, target, [obj_or_ir_path], lib_path; cflags, kind = :shared) - return path, filename + return joinpath(abspath(path), output_name(isnothing(cc) ? toolchain(target) : CCToolchain(cc), target, filename; kind = :shared)) end """ @@ -425,7 +369,7 @@ Print the LLVM IR that `f` compiles to for the argument types `types`, as the co `target`: what [`compile_executable`](@ref) and [`compile_shlib`](@ref) would write, before linking (with the method overlays of this package, and without the code of libraries that the function calls). """ -function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), llvm_postprocess=nothing, kwargs...) +function static_code_llvm(@nospecialize(func), @nospecialize(types); target::StaticTarget=StaticTarget(), kwargs...) job = static_job(func, types; target, kwargs...) GPUCompiler.code_llvm(stdout, job; libraries=false) end @@ -450,15 +394,15 @@ static_code_native(f, tt, fname=fix_name(f); target::StaticTarget=StaticTarget() Print the assembly that `f` compiles to for the argument types `tt` for the `target`, as the code generator of LLVM makes it. """ -function static_code_native(@nospecialize(f), @nospecialize(tt), fname=fix_name(f); target::StaticTarget=StaticTarget(), kwargs...) - job = static_job(f, tt; name=fname, target, kwargs...) +function static_code_native(@nospecialize(f), @nospecialize(tt), name=fix_name(f); target::StaticTarget=StaticTarget(), kwargs...) + job = static_job(f, tt; name, target, kwargs...) GPUCompiler.code_native(stdout, job; libraries=false) end """ ```julia -static_llvm_module(f, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) +static_llvm_module(f::Function, tt, name=fix_name(f); demangle=true, target::StaticTarget=StaticTarget(), kwargs...) static_llvm_module(funcs::Union{Array,Tuple}; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) ``` Compile the method of `f` for the argument types `tt` to an `LLVM.Module`, which the caller @@ -470,7 +414,7 @@ operation that consumes it, like `LLVM.link!(dst, mod)`) before the block ends. active thread-safe context, a new one is created for the module, which is never disposed of (i.e., every such call leaks a context). """ -function static_llvm_module(f, tt, name=nothing; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) +function static_llvm_module(f::Function, tt, name=nothing; demangle=true, target::StaticTarget=StaticTarget(), kwargs...) with_module_context() do compile_llvm_module(f, tt, name; demangle, target, kwargs...) end @@ -499,6 +443,22 @@ function with_module_context(f) end # compile to a module in the active (Julia) context, which must outlive the module +function compile_llvm_module(funcs::Union{Array,Tuple}; kwargs...) + names = entry_names(funcs; demangle = get(kwargs, :demangle, true), target = get(kwargs, :target, nothing)) + (f, tt), rest... = funcs + mod = compile_llvm_module(f, tt; kwargs..., symbol = names[1]) + try + for ((f, tt), name) in zip(rest, names[2:end]) + # consumes the module, also when linking fails + link!(mod, compile_llvm_module(f, tt; kwargs..., symbol = name)) + end + run!(MergeFunctionsPass(), mod) + catch + dispose(mod) + rethrow() + end + return mod +end function compile_llvm_module(f, tt, name=nothing; target::StaticTarget=StaticTarget(), demangle=true, @@ -531,21 +491,20 @@ end """ ```julia -generate_obj(f, tt, path::String = tempname(), filenamebase::String="obj"; kwargs...) -> (path, obj_path) -generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; +generate_obj(f::Function, tt, path::String, filenamebase::String="obj"; kwargs...) -> obj_path +generate_obj(funcs::Union{Array,Tuple}, path::String, filenamebase::String="obj"; target::StaticTarget=StaticTarget(), demangle = true, - emit_llvm_only = false, main_wrapper = nothing, main_prologue = nothing, llvm_postprocess = nothing, llvm_finalize = nothing, kwargs... -) -> (path, obj_path) +) -> obj_path ``` Low level interface for compiling object code (`.o`) for the function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled, or for an array of Tuples `(f, tt)` where each function `f` and tuple type `tt` determine the set of methods which will be compiled. -Returns the directory and the path of the file that was written: `filenamebase.o`, or `filenamebase.ll` with `emit_llvm_only`. +Returns the path of the file that was written. `target` is the [`StaticTarget`](@ref) that the code is made for, which can be one that the host cannot run (see its documentation: the code is then made for the host, retargeted, and the object is made by Julia's LLVM if it has the code generator for the target, and else by the target's `llc`; an error says so if neither can). The default is the native target. @@ -556,9 +515,6 @@ if it has the code generator for the target, and else by the target's `llc`; an * `:itanium`, as a C++ compiler would name them (`_Z6my_addll`, which `c++filt` reads as `my_add(long, long)`), with the C and C++ types of the arguments where applicable (`Int32` is `int`, `Ptr{UInt8}` is `unsigned char*`, `Cstring` is `const char*`) and Julia's own types named as classes in the namespace of their module. Multiple methods of one function, if compiled together into the same library, require `false` or `:itanium`. -If `emit_llvm_only` is set, the LLVM IR (`.ll` file) is written in place of the object, -for a compiler that makes the object (see [`generate_ir`](@ref)). - If `main_wrapper` is the name of one of the functions, the object will be given a `main` that calls it, possibly preceded by any code added by `main_prologue(builder, mod)`. @@ -569,13 +525,15 @@ The builtins that the toolchain of the target asks for (e.g., `__udivti3`, ...) Similarly, `llvm_finalize(mod)` is called once on the whole module, when it is complete (the modules combined, and the `main` and the builtins added) and just before the code is emitted. +See also: [`generate_ir`](@ref) + ### Examples ``` julia> fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) fib (generic function with 1 method) -julia> path, obj_path = StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test") -("./test", "./test/obj.o") +julia> obj_path = StaticCompiler.generate_obj(fib, Tuple{Int64}, "./test") +"./test/obj.o" shell> tree \$path ./test @@ -584,56 +542,99 @@ shell> tree \$path 0 directories, 1 file ``` """ -generate_obj(f, tt, args...; kwargs...) = generate_obj(((f, tt),), args...; kwargs...) -function generate_obj(funcs::Union{Array,Tuple}, path::String = tempname(), filenamebase::String="obj"; - target::StaticTarget=StaticTarget(), - demangle = true, - emit_llvm_only = false, - main_wrapper = nothing, - main_prologue = nothing, - llvm_postprocess = nothing, - llvm_finalize = nothing, - kwargs...) - f, tt = funcs[1] +generate_obj(f::Function, tt, filenamebase=fix_name(f); kwargs...) = generate_obj(((f, tt),), filenamebase; kwargs...) +function generate_obj(funcs::Union{Array,Tuple}, path::String=tempname(), filenamebase::String="obj"; + target::StaticTarget=StaticTarget(), + demangle = true, + main_wrapper = nothing, + main_prologue = nothing, + llvm_postprocess = nothing, + llvm_finalize = nothing, + kwargs..., + ) + can_emit_object(target) || throw(ArgumentError("No object files can be made for $(target_triple(target)) here; Julia's LLVM has no code generator for it, and no `llc` is given (`StaticTarget(...; llc)`). Use `llvm_to_clang=true`, or `generate_ir` and give the IR to a compiler that has one.")) mkpath(path) - if emit_llvm_only # (Required on Windows) - ir_path = joinpath(path, "$filenamebase.ll") - write(ir_path, generate_ir(funcs; demangle, target, main_wrapper, main_prologue, llvm_postprocess, llvm_finalize, kwargs...)) - return path, ir_path - end - can_emit_object(target) || throw(ArgumentError("no object can be made for $(target_triple(target)) here: Julia's LLVM has no code generator for it, and no `llc` is given (`StaticTarget(...; llc)`). Use `emit_llvm_only = true` (or `generate_ir`) and give the IR to a compiler that has one")) + f, tt = first(funcs) # compile and generate code in one context, which the module can't outlive GPUCompiler.JuliaContext() do context - @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin - isnothing(main_wrapper) || add_main_wrapper!(mod, main_wrapper, target; prologue = main_prologue) - uses_builtins(target.toolchain) && add_builtins!(mod, target_triple(target)) - isnothing(llvm_finalize) || llvm_finalize(mod) - obj_path = joinpath(path, "$filenamebase.o") - if !is_retargeted(target) - fakejob = static_job(f, tt; target, kwargs...) - obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) - else - obj = emit_object(mod, target) # (by the code generator of the target, which is not the host's) - end - open(obj_path, "w") do io - write(io, obj) + @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin + isnothing(main_wrapper) || add_main_wrapper!(mod, main_wrapper, target; prologue = main_prologue) + uses_builtins(target.toolchain) && add_builtins!(mod, target_triple(target)) + isnothing(llvm_finalize) || llvm_finalize(mod) + obj_path = joinpath(path, "$filenamebase.o") + if !is_retargeted(target) + # Create object files using GPUCompiler + fakejob = static_job(f, tt; target, kwargs...) + obj, _ = GPUCompiler.emit_asm(fakejob, mod, LLVM.CodeGenFileType.Object) + else + # Create object files using LLVM.jl, possibly via an external `llc` + obj = emit_object(mod, target) + end + open(obj_path, "w") do io + write(io, obj) + end + return obj_path end - return path, obj_path - end end end """ ```julia -generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle=true, llvm_postprocess=nothing, kwargs...) -> String -generate_ir(f, tt; kwargs...) -> String +generate_ir(f, tt, [path], [filenamebase]; kwargs...) +generate_ir(funcs::Union{Array,Tuple}, path::String, filenamebase::String; kwargs...) -> ir_path +generate_ir(funcs::Union{Array,Tuple}; + target::StaticTarget=StaticTarget(), + demangle = true, + main_wrapper = nothing, + main_prologue = nothing, + llvm_postprocess = nothing, + llvm_finalize = nothing, + kwargs... +) -> String ``` -The LLVM IR (as text) of the functions that `generate_obj` would compile to an object file, for tools that take IR instead -(a downstream package that makes the object or the executable of a target by itself, for example). +Low level interface for compiling LLVM IR (`.ll`) for the function `f` given a tuple type `tt` characterizing the types of the arguments for which the function will be compiled, +or for an array of Tuples `(f, tt)` where each function `f` and tuple type `tt` determine the set of methods which will be compiled. +Returns the path of the file that was written, or if `path` is not specified, returns the LLVM IR itself as a `String`. +The LLVM module (which cannot outlive its context) is not returned. + +`target` is the [`StaticTarget`](@ref) that the code is made for, which can be one that the host cannot run (see its documentation: the code is then made for the host, retargeted, and the object is made by Julia's LLVM +if it has the code generator for the target, and else by the target's `llc`; an error says so if neither can). The default is the native target. + +`demangle` determines what the functions are called in the compiled library: +* `true` (the default) as they are in Julia (`my_add`); +* `false`, "julia_", the name of the function, and the types of its arguments (`julia_my_add_Int64_Int64`, `julia_main`; numbered for any remaining collisions); +* `:itanium`, as a C++ compiler would name them (`_Z6my_addll`, which `c++filt` reads as `my_add(long, long)`), with the C and C++ types of the arguments where applicable (`Int32` is `int`, `Ptr{UInt8}` is `unsigned char*`, `Cstring` is `const char*`) and Julia's own types named as classes in the namespace of their module. +Multiple methods of one function, if compiled together into the same library, require `false` or `:itanium`. -Nothing is written to the file system, and the module (which cannot outlive its context) is not returned. +If `main_wrapper` is the name of one of the functions, the object will be given a `main` that calls it, +possibly preceded by any code added by `main_prologue(builder, mod)`. + +The builtins that the toolchain of the target asks for (e.g., `__udivti3`, ...) are also added to the module (see [`add_builtins!`](@ref)). + +`llvm_postprocess(mod)`, if given, is called on the module of each function as soon as it is generated +(before its pointers and runtime calls are located, it is retargeted, and the modules are combined). +Similarly, `llvm_finalize(mod)` is called once on the whole module, when it is complete +(the modules combined, and the `main` and the builtins added) and just before the code is emitted. + +See also: [`generate_obj`](@ref) """ -function generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarget(), demangle = true, main_wrapper = nothing, main_prologue = nothing, llvm_postprocess = nothing, llvm_finalize = nothing, kwargs...) +generate_ir(f::Function, tt, path::String, filenamebase::String=fix_name(f); kwargs...) = generate_ir(((f, tt),), path, filenamebase; kwargs...) # -> ir_path +function generate_ir(funcs::Union{Array,Tuple}, path::String, filenamebase::String="ir"; kwargs...) + mkpath(path) + ir_path = joinpath(path, "$filenamebase.ll") + write(ir_path, generate_ir(funcs; kwargs...)) + return ir_path +end +generate_ir(f::Function, tt; kwargs...) = generate_ir(((f, tt),); kwargs...) # -> String +function generate_ir(funcs::Union{Array,Tuple}; + target::StaticTarget=StaticTarget(), + demangle = true, + main_wrapper = nothing, + main_prologue = nothing, + llvm_postprocess = nothing, + llvm_finalize = nothing, + kwargs... + ) GPUCompiler.JuliaContext() do context @dispose mod=compile_llvm_module(funcs; demangle, target, llvm_postprocess, kwargs...) begin isnothing(main_wrapper) || add_main_wrapper!(mod, main_wrapper, target; prologue = main_prologue) @@ -643,12 +644,11 @@ function generate_ir(funcs::Union{Array,Tuple}; target::StaticTarget=StaticTarge end end end -generate_ir(f, tt; kwargs...) = generate_ir(((f, tt),); kwargs...) """ ```julia -link(toolchain, target, inputs::Vector{String}, output::String; kind = :executable, entry = nothing, cflags = ``) -> path +link(toolchain, target, inputs::Vector{String}, output::String; entry = nothing, cflags = ``, kind = :executable) -> path ``` Link the `inputs` (object files, or LLVM IR files if the toolchain is a `CCToolchain`) diff --git a/src/llvm.jl b/src/llvm.jl index 0630f84d..53dc631a 100644 --- a/src/llvm.jl +++ b/src/llvm.jl @@ -170,7 +170,7 @@ check_retargeted(target::StaticTarget) = is_retargeted(target) || throw(Argument # Emit the object file via `llc`, if it has a code generator that Julia's LLVM has not function emit_object_llc(ir::AbstractString, target::StaticTarget) m = target.machine - isnothing(target.llc) && throw(ArgumentError("no object can be made for $(m.triple): Julia's LLVM has no code generator for it, and no `llc` is given (`StaticTarget(...; llc = ...)`, of the LLVM version that Julia has, with the code generator for it). The IR can be made (`generate_ir`, `emit_llvm_only = true`)")) + isnothing(target.llc) && throw(ArgumentError("No object can be made for $(m.triple): Julia's LLVM has no code generator for it, and no `llc` is given (`StaticTarget(...; llc = ...)`) with which to compile LLVM IR")) mktempdir() do dir ll, obj = joinpath(dir, "module.ll"), joinpath(dir, "module.o") write(ll, ir) diff --git a/src/mangling.jl b/src/mangling.jl index 7d69d23b..129d8b5c 100644 --- a/src/mangling.jl +++ b/src/mangling.jl @@ -66,24 +66,6 @@ function definition_world(f, tt) end end -function compile_llvm_module(funcs::Union{Array,Tuple}; kwargs...) - names = entry_names(funcs; demangle = get(kwargs, :demangle, true), target = get(kwargs, :target, nothing)) - (f, tt), rest... = funcs - mod = compile_llvm_module(f, tt; kwargs..., symbol = names[1]) - try - for ((f, tt), name) in zip(rest, names[2:end]) - # consumes the module, also when linking fails - link!(mod, compile_llvm_module(f, tt; kwargs..., symbol = name)) - end - run!(MergeFunctionsPass(), mod) - catch - dispose(mod) - rethrow() - end - return mod -end - - # Itanium C++ ABI names for entry points (`demangle = :itanium`): `my_add(Int, Int)` is `_Z6my_addll`, which `c++filt` reads as `my_add(long, long)` and a C++ program that declares # `long my_add(long, long)` links to. https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling # diff --git a/src/target.jl b/src/target.jl index a91dd817..a5f8b06d 100644 --- a/src/target.jl +++ b/src/target.jl @@ -33,7 +33,7 @@ does not behave as expected. **Retargeting.** Julia's code generation makes the IR for the `machine` of the target, unless the target is retargeted. If the target is not the host's (`retarget = true`, or `retarget = host`, a `StaticTarget` whose machine is the one to make the IR for; and always where Julia's LLVM has no code generator for the target: x86-64 on a build of Julia that has only AArch64 and WebAssembly, say), the IR is made for the host and then given the target's triple, without -the host's CPU or features (see [`retarget!`](@ref)): IR can be made for it (`generate_ir`, `emit_llvm_only = true`, `llvm_to_clang = true`), and the object is made by the code generator of Julia's LLVM if it has one for the triple, else by the external `llc` +the host's CPU or features (see [`retarget!`](@ref)): IR can be made for it (`generate_ir`, `llvm_to_clang = true`), and the object is made by the code generator of Julia's LLVM if it has one for the triple, else by the external `llc` (a `Cmd` or a path, of the LLVM version that Julia has), or `clang`. The fields can be set (`set_compiler!`, `set_toolchain!`, `set_runtime!`), and the copy constructor makes a changed copy, which leaves the original as it was: `StaticTarget(target; toolchain = CCToolchain("/opt/homebrew/bin/aarch64-unknown-linux-gnu-gcc"))`. diff --git a/test/cross/matrix.jl b/test/cross/matrix.jl index 1237a536..82daf79f 100644 --- a/test/cross/matrix.jl +++ b/test/cross/matrix.jl @@ -51,7 +51,7 @@ for (triple, make_toolchain) in TARGETS # the object: by Julia's LLVM if it has the code generator, or by `llc` object = !SC.can_emit_object(target) ? "no code generator (needs an `llc`)" : status() do dir = mktempdir() - _, obj = SC.generate_obj(((matrix_main, Tuple{Int,Ptr{Ptr{UInt8}}}),), dir, "matrix_main"; target, main_wrapper = "matrix_main") + obj = SC.generate_obj(((matrix_main, Tuple{Int,Ptr{Ptr{UInt8}}}),), dir, "matrix_main"; target, main_wrapper = "matrix_main") filesize(obj) > 100 || error("the object is empty") end println(" object: ", object) diff --git a/test/testcore.jl b/test/testcore.jl index 967853c7..ceb83e00 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -9,7 +9,7 @@ fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globall #Compile dylib name = string(nameof(fib)) # repr(fib) - filepath = compile_shlib(fib, (Int,), workdir, name, demangle=true) + filepath = compile_shlib(fib, (Int,), workdir, demangle=true) @test occursin("fib.$(Libdl.dlext)", filepath) # Open dylib manually ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) @@ -19,7 +19,7 @@ fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # This needs to be defined globall Libdl.dlclose(ptr) # As above, but without demangling - filepath = compile_shlib(fib, (Int,), workdir, name, demangle=false) + filepath = compile_shlib(fib, (Int,), workdir, demangle=false) ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL) fptr = Libdl.dlsym(ptr, "julia_fib_Int64") # (the name of the function and the types of its arguments) @test fptr != C_NULL @@ -182,7 +182,7 @@ end @testset "LLVM IR as text" begin ir = StaticCompiler.generate_ir(fib, Tuple{Int}) @test ir isa String && occursin("define", ir) && occursin("fib", ir) && occursin("target triple", ir) - path, ir_path = StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibir"; emit_llvm_only = true) + ir_path = StaticCompiler.generate_ir(((fib, Tuple{Int}),), mktempdir(), "fibir") @test isfile(ir_path) && occursin("define", read(ir_path, String)) end @@ -195,12 +195,12 @@ end @test occursin("target triple = \"$triple\"", ir) && !occursin("target datalayout", ir) && !occursin("target-cpu", ir) && !occursin("target-features", ir) # (an object is made if Julia's LLVM has the code generator, and is refused if it has not and no `llc` is given) if StaticCompiler.julia_llvm_has(triple) - _, obj = StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibobj"; target = ir_target) + obj = StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibobj"; target = ir_target) @test read(obj, 4) == UInt8[0x7f, 0x45, 0x4c, 0x46] # ELF else @test_throws ArgumentError StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibobj"; target = ir_target) end - path, ir_path = StaticCompiler.generate_obj(((fib, Tuple{Int}),), mktempdir(), "fibir"; target = ir_target, emit_llvm_only = true) + ir_path = StaticCompiler.generate_ir(((fib, Tuple{Int}),), mktempdir(), "fibir"; target = ir_target) @test occursin("target triple = \"$triple\"", read(ir_path, String)) # the host's own target is untouched host = StaticCompiler.StaticTarget() @@ -255,13 +255,12 @@ end # `main` made in IR: the executable runs without a C wrapper dir = mktempdir() hello_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int - # (made and linked as the library does for the host: as IR where the host needs it, as on Windows) - _, obj = StaticCompiler.generate_obj(((hello_main, Tuple{Int,Ptr{Ptr{UInt8}}}),), dir, "hm"; main_wrapper = "hello_main", emit_llvm_only = StaticCompiler.needs_ir(host)) ir = StaticCompiler.generate_ir(((hello_main, Tuple{Int,Ptr{Ptr{UInt8}}}),); main_wrapper = "hello_main") @test occursin(r"define i32 @main\(i32", ir) + ir_path = StaticCompiler.generate_ir(((hello_main, Tuple{Int,Ptr{Ptr{UInt8}}}),), dir, "hm"; main_wrapper = "hello_main") driver = StaticCompiler.toolchain(host) exe = joinpath(dir, StaticCompiler.output_name(driver, host, "hm_exe")) - StaticCompiler.link(driver, host, [obj], exe; entry = nothing) # (`main` is in the object) + StaticCompiler.link(driver, host, [ir_path], exe; entry = nothing) # (`main` is in the object) @test run(ignorestatus(`$exe`)).exitcode == 42 # (argc is 1) @test_throws ArgumentError StaticCompiler.generate_ir(((hello_main, Tuple{Int,Ptr{Ptr{UInt8}}}),); main_wrapper = "nothing_here") @@ -272,7 +271,7 @@ end @test endswith(wasm_path, "wasm_main.wasm") && read(wasm_path, 4) == UInt8[0x00, 0x61, 0x73, 0x6d] linux = StaticCompiler.StaticTarget("aarch64-unknown-linux-gnu"; toolchain = StaticCompiler.LLDToolchain(:gnu)) @test_throws ArgumentError compile_executable(wasm_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "no_sysroot"; target = linux) # (the C library is the target's: `sysroot`) - @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLDToolchain(:gnu; sysroot = mktempdir()), linux, [obj], joinpath(dir, "x"); entry = "main") # (a sysroot that has no `crt1.o`) + @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLDToolchain(:gnu; sysroot = mktempdir()), linux, [ir_path], joinpath(dir, "x"); entry = "main") # (a sysroot that has no `crt1.o`) @test_throws ArgumentError StaticCompiler.link(StaticCompiler.LLDToolchain(:wasm), wasm, [joinpath(dir, "a.ll")], joinpath(dir, "x")) # (`lld` links objects, not IR) end @@ -455,7 +454,7 @@ end t = StaticCompiler.StaticTarget(triple) @test StaticCompiler.target_triple(t) == "$arch-w64-windows-gnu" && StaticCompiler.iswindows(t) win_main(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc + 41) % Int - _, obj = StaticCompiler.generate_obj(((win_main, Tuple{Int,Ptr{Ptr{UInt8}}}),), mktempdir(), "o"; target = t, main_wrapper = "win_main") + obj = StaticCompiler.generate_obj(((win_main, Tuple{Int,Ptr{Ptr{UInt8}}}),), mktempdir(), "o"; target = t, main_wrapper = "win_main") head = read(obj, 2) @test head != UInt8[0x7f, 0x45] && head == (arch == "x86_64" ? UInt8[0x64, 0x86] : UInt8[0x64, 0xaa]) end @@ -489,7 +488,7 @@ end end @test finalized[] == 2 # (and without a prologue the program is what it was: the entry point's value is its status) - exe = compile_executable(hook_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "hook_main"; filename = "hook_main2") + exe = compile_executable(hook_main, (Int, Ptr{Ptr{UInt8}}), mktempdir(), "hook_main2") @test run(ignorestatus(`$exe`)).exitcode == 0 # objects in `cflags` are linked with the program dir = mktempdir() @@ -523,7 +522,7 @@ name_clash_pair(s) = name_clash(s) @test occursin(r"define [^\n]*@julia_name_clash_Int64_Int64\(i64[^,\n]*, i64[^,\n)]*\)", ir) # (the method of two numbers) @test count(r"define [^\n]*@julia_name_clash_\w+\(", ir) == 3 # a function of its own for each is as good - _, obj = StaticCompiler.generate_obj(((name_clash_int, Tuple{Int,Int}), (name_clash_pair, Tuple{NameClashPair})), mktempdir(), "noclash") + obj = StaticCompiler.generate_obj(((name_clash_int, Tuple{Int,Int}), (name_clash_pair, Tuple{NameClashPair})), mktempdir(), "noclash") @test isfile(obj) # (with `demangle = false` the types are in every name, also of an entry point that is alone: `julia_`, the function, and the types if it has arguments) @test StaticCompiler.entry_names(((name_clash_int, Tuple{Int,Int}),); demangle = false) == ["julia_name_clash_int_Int64_Int64"] @@ -567,7 +566,7 @@ itanium_many(a...) = 1 entries = ((itanium_add, (Int, Int)), (itanium_add, (ItaniumPoint,))) expected = StaticCompiler.entry_names(entries; demangle = :itanium, target = StaticCompiler.StaticTarget()) @test expected == [Sys.iswindows() ? "_Z11itanium_addxx" : "_Z11itanium_addll", "_Z11itanium_add12ItaniumPoint"] - _, obj = StaticCompiler.generate_obj(entries, mktempdir(), "itanium"; demangle = :itanium) + obj = StaticCompiler.generate_obj(entries, mktempdir(), "itanium"; demangle = :itanium) bytes = String(read(obj)) @test all(name -> occursin(name, bytes), expected) @test_throws ArgumentError StaticCompiler.entry_names(((itanium_add, (Int, Int)),); demangle = :nope) diff --git a/test/testquirks.jl b/test/testquirks.jl index d0ad2aca..f40277e8 100644 --- a/test/testquirks.jl +++ b/test/testquirks.jl @@ -46,7 +46,7 @@ quirk_error(argc::Int, argv::Ptr{Ptr{UInt8}}) = (argc > 0 && error("bad"); 0) (quirk_floor, (Float64,)), (quirk_ceil, (Float64,)), (quirk_u8, (Float64,)), (quirk_i32, (Float32,)), (quirk_acos, (Float64,)), (quirk_char, (Int,)), (quirk_rational, (Int, Int))] - path = compile_shlib(funcs, workdir; filename="libquirks") + path = compile_shlib(funcs, workdir, "libquirks") lib = Libdl.dlopen(path, Libdl.RTLD_LOCAL) sym(f) = Libdl.dlsym(lib, string(nameof(f))) From 162f1f4e79bfc2afdf8f203e00117c2ba3871856 Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 2 Dec 2026 12:07:54 -0500 Subject: [PATCH 201/202] Rename `entry_names` to clearer `compiled_names` and export --- src/StaticCompiler.jl | 10 +++++----- src/llvm.jl | 2 +- src/mangling.jl | 2 +- test/testcore.jl | 18 +++++++++--------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/StaticCompiler.jl b/src/StaticCompiler.jl index 24a7ef2d..b6d02f58 100644 --- a/src/StaticCompiler.jl +++ b/src/StaticCompiler.jl @@ -9,7 +9,7 @@ using StaticTools: @symbolcall, @c_str, println using Base: BinaryPlatforms.Platform, BinaryPlatforms.HostPlatform using Base: BinaryPlatforms.arch, BinaryPlatforms.os_str, BinaryPlatforms.libc_str export compile_shlib, compile_executable -export static_code_llvm, static_code_typed, static_code_native, static_llvm_module +export static_code_llvm, static_code_typed, static_code_native, static_llvm_module, compiled_names export @device_override, @print_and_throw export StaticTarget, CCToolchain, LLDToolchain @@ -294,7 +294,7 @@ function generate_executable(funcs::Union{Array,Tuple}, path::String=tempname(), # Prepare objects or (if llvm_to_clang is set) IR. A linker that is not a compiler driver is given an object that has a `main`, and so is a driver when # `main_prologue` asks for code to run first, which the `main` of the object has (the driver would be given a `main` of its own, with nothing before the entry point) - fn = first(entry_names(funcs; demangle, target)) + fn = first(compiled_names(funcs; demangle, target)) main_wrapper = !(tc isa CCToolchain) || !isnothing(main_prologue) ? fn : nothing entry = isnothing(main_wrapper) ? fn : nothing obj_or_ir_path = if llvm_to_clang @@ -444,7 +444,7 @@ end # compile to a module in the active (Julia) context, which must outlive the module function compile_llvm_module(funcs::Union{Array,Tuple}; kwargs...) - names = entry_names(funcs; demangle = get(kwargs, :demangle, true), target = get(kwargs, :target, nothing)) + names = compiled_names(funcs; demangle = get(kwargs, :demangle, true), target = get(kwargs, :target, nothing)) (f, tt), rest... = funcs mod = compile_llvm_module(f, tt; kwargs..., symbol = names[1]) try @@ -470,9 +470,9 @@ function compile_llvm_module(f, tt, name=nothing; symbol=nothing, kwargs..., ) - # (`symbol` is the name in the object, which `entry_names` made; or the name that was given, with `julia_` before it if `demangle` is `false`; or the name that `demangle` says, of the function and its types) + # (`symbol` is the name in the object, which `compiled_names` made; or the name that was given, with `julia_` before it if `demangle` is `false`; or the name that `demangle` says, of the function and its types) if symbol === nothing - symbol = name === nothing ? only(entry_names(((f, tt),); demangle, target)) : demangle === false ? "julia_" * name : name + symbol = name === nothing ? only(compiled_names(((f, tt),); demangle, target)) : demangle === false ? "julia_" * name : name end name = symbol job = static_job(f, tt; name, target, strip, only_entry, validate, libraries, kwargs...) diff --git a/src/llvm.jl b/src/llvm.jl index 53dc631a..16e92333 100644 --- a/src/llvm.jl +++ b/src/llvm.jl @@ -124,7 +124,7 @@ end # Export the compiled entry points from a Windows DLL function add_dllexport!(mod::LLVM.Module, funcs; demangle=true, target=nothing) - for name_f in entry_names(funcs; demangle, target) + for name_f in compiled_names(funcs; demangle, target) llvmf = get(mod.functions, name_f, nothing) if llvmf !== nothing && !isdeclaration(llvmf) llvmf.dllstorage = LLVM.DLLStorageClass.Export diff --git a/src/mangling.jl b/src/mangling.jl index 129d8b5c..b3515b1a 100644 --- a/src/mangling.jl +++ b/src/mangling.jl @@ -3,7 +3,7 @@ # * `false`, `julia_`, the name of the function, and the types of its arguments (`julia_my_add_Int64_Int64`, `julia_main`); # * `:itanium`, the names that a C++ compiler makes (`_Z6my_addll`, see below). # Two entry points of one name would be two functions of the same name in the module, which LLVM refuses (as an error of the linker, which does not say which entry points they are). -function entry_names(funcs; demangle::Union{Bool,Symbol} = true, target = nothing) +function compiled_names(funcs; demangle::Union{Bool,Symbol} = true, target = nothing) demangle in (true, false, :itanium) || throw(ArgumentError("`demangle` is `true`, `false` or `:itanium`, not `$(repr(demangle))`")) base = String[fix_name(f) for (f, _) in funcs] if demangle === :itanium diff --git a/test/testcore.jl b/test/testcore.jl index ceb83e00..a3cef3cc 100644 --- a/test/testcore.jl +++ b/test/testcore.jl @@ -513,11 +513,11 @@ name_clash_pair(s) = name_clash(s) err = try StaticCompiler.generate_obj(entries, mktempdir(), "clash"; demangle = true); nothing catch e e end @test err isa ArgumentError && occursin("More than one entry point is named `name_clash`", err.msg) && occursin("demangle = false", err.msg) # (the names have the types, whatever the order that the methods were defined or are given in) - @test StaticCompiler.entry_names(entries; demangle = false) == ["julia_name_clash_NameClashPair", "julia_name_clash_Int64_Int64_Int64", "julia_name_clash_Int64_Int64"] - @test StaticCompiler.entry_names(reverse(entries); demangle = false) == reverse(StaticCompiler.entry_names(entries; demangle = false)) + @test StaticCompiler.compiled_names(entries; demangle = false) == ["julia_name_clash_NameClashPair", "julia_name_clash_Int64_Int64_Int64", "julia_name_clash_Int64_Int64"] + @test StaticCompiler.compiled_names(reverse(entries); demangle = false) == reverse(StaticCompiler.compiled_names(entries; demangle = false)) @test StaticCompiler.argument_suffix(()) == "" && StaticCompiler.argument_suffix(Tuple{Vector{Int},Ptr{Ptr{UInt8}}}) == "Vector_Int64_Ptr_Ptr_UInt8" # (what the types do not tell apart is numbered, the oldest method first) - @test StaticCompiler.entry_names(((name_clash, Tuple{Int,Int}), (name_clash, Tuple{Int,Int})); demangle = false) == ["julia_name_clash_1", "julia_name_clash_2"] + @test StaticCompiler.compiled_names(((name_clash, Tuple{Int,Int}), (name_clash, Tuple{Int,Int})); demangle = false) == ["julia_name_clash_1", "julia_name_clash_2"] ir = StaticCompiler.generate_ir(entries; demangle = false) @test occursin(r"define [^\n]*@julia_name_clash_Int64_Int64\(i64[^,\n]*, i64[^,\n)]*\)", ir) # (the method of two numbers) @test count(r"define [^\n]*@julia_name_clash_\w+\(", ir) == 3 @@ -525,11 +525,11 @@ name_clash_pair(s) = name_clash(s) obj = StaticCompiler.generate_obj(((name_clash_int, Tuple{Int,Int}), (name_clash_pair, Tuple{NameClashPair})), mktempdir(), "noclash") @test isfile(obj) # (with `demangle = false` the types are in every name, also of an entry point that is alone: `julia_`, the function, and the types if it has arguments) - @test StaticCompiler.entry_names(((name_clash_int, Tuple{Int,Int}),); demangle = false) == ["julia_name_clash_int_Int64_Int64"] - @test StaticCompiler.entry_names(((name_clash_int, ()),); demangle = false) == ["julia_name_clash_int"] # (nothing for a function that takes no arguments) + @test StaticCompiler.compiled_names(((name_clash_int, Tuple{Int,Int}),); demangle = false) == ["julia_name_clash_int_Int64_Int64"] + @test StaticCompiler.compiled_names(((name_clash_int, ()),); demangle = false) == ["julia_name_clash_int"] # (nothing for a function that takes no arguments) @test occursin("@julia_name_clash_int_Int64_Int64(", string(StaticCompiler.static_llvm_module(name_clash_int, Tuple{Int,Int}; demangle = false))) # (one entry point, or names that differ, are as they were) - @test StaticCompiler.entry_names(((name_clash_int, Tuple{Int,Int}), (name_clash_pair, Tuple{NameClashPair})); demangle = true) == ["name_clash_int", "name_clash_pair"] + @test StaticCompiler.compiled_names(((name_clash_int, Tuple{Int,Int}), (name_clash_pair, Tuple{NameClashPair})); demangle = true) == ["name_clash_int", "name_clash_pair"] end # `demangle = :itanium`: the names of the Itanium C++ ABI, which these are (checked with the names that clang makes of the same declarations in C++: `_Z6my_addll`, ...) @@ -547,7 +547,7 @@ itanium_add(s::ItaniumPoint) = s.a itanium_none() = 1 itanium_many(a...) = 1 @testset "Itanium names" begin - en(f, tt; kw...) = only(StaticCompiler.entry_names(((f, tt),); demangle = :itanium, kw...)) + en(f, tt; kw...) = only(StaticCompiler.compiled_names(((f, tt),); demangle = :itanium, kw...)) @test en(itanium_add, (Int, Int)) == "_Z11itanium_addll" @test en(itanium_add, (ItaniumPoint,)) == "_Z11itanium_add12ItaniumPoint" @test en(itanium_none, ()) == "_Z12itanium_nonev" @@ -564,10 +564,10 @@ itanium_many(a...) = 1 # the object has them # (of the host's own target: `long` is 32 bits on Windows, where `Int` is `x`) entries = ((itanium_add, (Int, Int)), (itanium_add, (ItaniumPoint,))) - expected = StaticCompiler.entry_names(entries; demangle = :itanium, target = StaticCompiler.StaticTarget()) + expected = StaticCompiler.compiled_names(entries; demangle = :itanium, target = StaticCompiler.StaticTarget()) @test expected == [Sys.iswindows() ? "_Z11itanium_addxx" : "_Z11itanium_addll", "_Z11itanium_add12ItaniumPoint"] obj = StaticCompiler.generate_obj(entries, mktempdir(), "itanium"; demangle = :itanium) bytes = String(read(obj)) @test all(name -> occursin(name, bytes), expected) - @test_throws ArgumentError StaticCompiler.entry_names(((itanium_add, (Int, Int)),); demangle = :nope) + @test_throws ArgumentError StaticCompiler.compiled_names(((itanium_add, (Int, Int)),); demangle = :nope) end From 06dda1bed3b4667fdb9ab615d4a0780ec95e168b Mon Sep 17 00:00:00 2001 From: "C. Brenhin Keller" Date: Wed, 2 Dec 2026 13:01:50 -0500 Subject: [PATCH 202/202] Remove mention of obsolete `filename` kwarg in docs and README --- README.md | 2 +- docs/src/index.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 83d0fa3a..6e4598bd 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ julia> using StaticCompiler, Glibc_jll, Pkg # (Glibc_jll and Pkg fetch the C lib julia> target = StaticTarget("aarch64-unknown-linux-gnu"; retarget = true, toolchain = LLDToolchain(:gnu; sysroot = StaticCompiler.glibc_sysroot("aarch64-unknown-linux-gnu"))); -julia> compile_executable(hello, (), "./", "hello"; filename = "hello_aarch64", target) +julia> compile_executable(hello, (), "./", "hello_aarch64"; target) ``` The toolchain can be either a [`CCToolchain`](@ref) specifying a C compiler, or an [`LLDToolchain`](@ref) object specifying an LLVM `lld` to be used. diff --git a/docs/src/index.md b/docs/src/index.md index a917cafc..2e4d8f18 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -83,7 +83,7 @@ julia> using StaticCompiler, Glibc_jll, Pkg # (Glibc_jll and Pkg fetch the C lib julia> target = StaticTarget("aarch64-unknown-linux-gnu"; retarget = true, toolchain = LLDToolchain(:gnu; sysroot = StaticCompiler.glibc_sysroot("aarch64-unknown-linux-gnu"))); -julia> compile_executable(hello, (), "./", "hello"; filename = "hello_aarch64", target) +julia> compile_executable(hello, (), "./", "hello_aarch64"; target) ``` The toolchain can be either a [`CCToolchain`](https://github.com/tshort/StaticCompiler.jl/blob/master/@ref) specifying a C compiler, or an [`LLDToolchain`](https://github.com/tshort/StaticCompiler.jl/blob/master/@ref) object specifying an LLVM `lld` to be used (flavors include `:gnu` for Linux, `:darwin` for macOS, `:mingw` for Windows, and `:wasm` for WebAssembly). Julia's LLVM contains the code generators for its own architecture and for WebAssembly only. For any other architecture, the IR can be made (`emit_llvm_only = true`), and `StaticTarget(...; llc = "/path/to/llc")` makes the object with an `llc` that has the desired backend for the same LLVM version.