Skip to content

Commit 156210c

Browse files
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
1 parent 080181f commit 156210c

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StaticCompiler"
22
uuid = "81625895-6c0f-48fc-b932-11a18313743c"
33
authors = ["Tom Short and contributors"]
4-
version = "0.4.7"
4+
version = "0.4.8"
55

66
[deps]
77
Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100"

src/StaticCompiler.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function compile(f, _tt, path::String = tempname(); name = GPUCompiler.safe_nam
9494
tt = Base.to_tuple_type(_tt)
9595
isconcretetype(tt) || error("input type signature $_tt is not concrete")
9696

97-
rt = only(native_code_typed(f, tt))[2]
97+
rt = last(only(native_code_typed(f, tt)))
9898
isconcretetype(rt) || error("$f on $_tt did not infer to a concrete type. Got $rt")
9999

100100
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
236236
)
237237

238238
tt = Base.to_tuple_type(types)
239-
# tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}} || error("input type signature $types must be either () or (Int, Ptr{Ptr{UInt8}})")
239+
isexecutableargtype = tt == Tuple{} || tt == Tuple{Int, Ptr{Ptr{UInt8}}}
240+
isexecutableargtype || @warn "input type signature $types should be either `()` or `(Int, Ptr{Ptr{UInt8}})` for standard executables"
240241

241-
rt = only(native_code_typed(f, tt))[2]
242-
isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt")
242+
rt = last(only(native_code_typed(f, tt)))
243+
isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`")
244+
nativetype = isprimitivetype(rt) || isa(rt, Ptr)
245+
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!"
243246

244247
# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals
245248
# 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
263266
)
264267

265268
tt = Base.to_tuple_type(types)
266-
isconcretetype(tt) || error("input type signature $types is not concrete")
269+
isconcretetype(tt) || error("input type signature `$types` is not concrete")
267270

268-
rt = only(native_code_typed(f, tt))[2]
269-
isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt")
271+
rt = last(only(native_code_typed(f, tt)))
272+
isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`")
273+
nativetype = isprimitivetype(rt) || isa(rt, Ptr)
274+
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!"
270275

271276
# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals
272277
# Keep an eye on https://github.com/JuliaLang/julia/pull/43747 for this
@@ -544,10 +549,12 @@ function compile_shlib(funcs::Array, path::String="./";
544549
for func in funcs
545550
f, types = func
546551
tt = Base.to_tuple_type(types)
547-
isconcretetype(tt) || error("input type signature $types is not concrete")
552+
isconcretetype(tt) || error("input type signature `$types` is not concrete")
548553

549-
rt = only(native_code_typed(f, tt))[2]
550-
isconcretetype(rt) || error("$f$types did not infer to a concrete type. Got $rt")
554+
rt = last(only(native_code_typed(f, tt)))
555+
isconcretetype(rt) || error("`$f$types` did not infer to a concrete type. Got `$rt`")
556+
nativetype = isprimitivetype(rt) || isa(rt, Ptr)
557+
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!"
551558
end
552559

553560
# Would be nice to use a compiler pass or something to check if there are any heap allocations or references to globals

0 commit comments

Comments
 (0)