Revision df3da0582a7800dab72bf4c7b1f1b69cd6605744 authored by Shuhei Kadowaki on 23 August 2022, 05:19:59 UTC, committed by GitHub on 23 August 2022, 05:19:59 UTC
This builtin is useful to control compiler behavior.
It could be considered as a more robust and generalized version of `inferencebarrier`.

I scratched the following docstring for `compilerbarrier`, that hopefully
explains its purpose.

    Base.compilerbarrier(setting::Symbol, val)

This function puts a barrier at a specified compilation phase.
It is supposed to only influence the compilation behavior according to `setting`,
and its runtime semantics is just to return the second argument `val` (except that
this function will perform additional checks on `setting` in a case when `setting`
isn't known precisely at compile-time.)

Currently either of the following `setting`s is allowed:
- Barriers on abstract interpretation:
* `:type`: the return type of this function call will be inferred as `Any` always
(the strongest barrier on abstract interpretation)
* `:const`: the return type of this function call will be inferred with widening
constant information on `val`
* `:conditional`: the return type of this function call will be inferred with widening
conditional information on `val` (see the example below)
- Any barriers on optimization aren't implemented yet

!!! note
    This function is supposed to be used _with `setting` known precisely at compile-time_.
    Note that in a case when the `setting` isn't known precisely at compile-time, the compiler
    currently will put the most strongest barrier(s) rather than emitting a compile-time warning.

\# Examples

```julia
julia> Base.return_types((Int,)) do a
       x = compilerbarrier(:type, a) # `x` won't be inferred as `x::Int`
       return x
   end |> only
Any

julia> Base.return_types() do
       x = compilerbarrier(:const, 42)
       if x == 42 # no constant information here, so inference also accounts for the else branch
           return x # but `x` is still inferred as `x::Int` at least here
       else
           return nothing
       end
   end |> only
Union{Nothing, Int64}

julia> Base.return_types((Union{Int,Nothing},)) do a
       if compilerbarrier(:conditional, isa(a, Int))
           # the conditional information `a::Int` isn't available here (leading to less accurate return type inference)
           return a
       else
           return nothing
       end
   end |> only
Union{Nothing, Int64}
```

As a result, `Base.inferencebarrier` is now defined as
```julia
inferencebarrier(@nospecialize(x)) = compilerbarrier(:type, x)
```
1 parent 80e50b5
History
File Mode Size
checksums
patches
tools
valgrind
.gitignore -rw-r--r-- 26 bytes
Makefile -rw-r--r-- 5.4 KB
blastrampoline.mk -rw-r--r-- 1.5 KB
blastrampoline.version -rw-r--r-- 196 bytes
clang.version -rw-r--r-- 123 bytes
csl.mk -rw-r--r-- 4.6 KB
csl.version -rw-r--r-- 57 bytes
curl.mk -rw-r--r-- 3.4 KB
curl.version -rw-r--r-- 96 bytes
dsfmt.mk -rw-r--r-- 2.2 KB
dsfmt.version -rw-r--r-- 76 bytes
gfortblas.alias -rw-r--r-- 706 bytes
gfortblas.c -rw-r--r-- 4.4 KB
gmp.mk -rw-r--r-- 3.2 KB
gmp.version -rw-r--r-- 70 bytes
libgit2.mk -rw-r--r-- 4.2 KB
libgit2.version -rw-r--r-- 410 bytes
libssh2.mk -rw-r--r-- 2.5 KB
libssh2.version -rw-r--r-- 167 bytes
libsuitesparse.mk -rw-r--r-- 5.0 KB
libsuitesparse.version -rw-r--r-- 101 bytes
libuv.mk -rw-r--r-- 1.8 KB
libuv.version -rw-r--r-- 154 bytes
libwhich.mk -rw-r--r-- 1.2 KB
libwhich.version -rw-r--r-- 78 bytes
lld.version -rw-r--r-- 60 bytes
llvm-options.mk -rw-r--r-- 564 bytes
llvm-tools.version -rw-r--r-- 182 bytes
llvm-ver.make -rw-r--r-- 595 bytes
llvm.mk -rw-r--r-- 10.7 KB
llvm.version -rw-r--r-- 161 bytes
llvmunwind.version -rw-r--r-- 95 bytes
mbedtls.mk -rw-r--r-- 3.4 KB
mbedtls.version -rw-r--r-- 83 bytes
mpfr.mk -rw-r--r-- 2.6 KB
mpfr.version -rw-r--r-- 313 bytes
nghttp2.mk -rw-r--r-- 2.0 KB
nghttp2.version -rw-r--r-- 102 bytes
objconv.mk -rw-r--r-- 1.0 KB
objconv.version -rw-r--r-- 264 bytes
openblas.mk -rw-r--r-- 8.3 KB
openblas.version -rw-r--r-- 208 bytes
openlibm.mk -rw-r--r-- 1.3 KB
openlibm.version -rw-r--r-- 163 bytes
p7zip.mk -rw-r--r-- 1.6 KB
p7zip.version -rw-r--r-- 76 bytes
patchelf.mk -rw-r--r-- 2.2 KB
patchelf.version -rw-r--r-- 112 bytes
pcre.mk -rw-r--r-- 2.1 KB
pcre.version -rw-r--r-- 74 bytes
unwind.mk -rw-r--r-- 7.3 KB
unwind.version -rw-r--r-- 104 bytes
utf8proc.mk -rw-r--r-- 1.5 KB
utf8proc.version -rw-r--r-- 78 bytes
zlib.mk -rw-r--r-- 1.4 KB
zlib.version -rw-r--r-- 145 bytes

back to top