# Does Julia have "unlikely" hint similar to in C++ or a way to fix code generation here

**URL:** <https://discourse.julialang.org/t/does-julia-have-unlikely-hint-similar-to-in-c-or-a-way-to-fix-code-generation-here/46528>\
**Category:** General Usage\
**Created:** [September 12, 2020, 9:00pm UTC](https://discourse.julialang.org/t/does-julia-have-unlikely-hint-similar-to-in-c-or-a-way-to-fix-code-generation-here/46528 "2020-09-12T21:00:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 9:00pm UTC](https://discourse.julialang.org/t/does-julia-have-unlikely-hint-similar-to-in-c-or-a-way-to-fix-code-generation-here/46528/1 "2020-09-12T21:00:13Z")

</div>

[https://en.cppreference.com/w/cpp/language/attributes/likely](https://en.cppreference.com/w/cpp/language/attributes/likely)

I’m trying to get the division first, but the Julia code generation puts vdivsd %xmm0, %xmm1, %xmm1  
very late:

```julia
_sinc_threshold(::Type{Float64}) = 0.001
@inline function _sinc2(x::Union{T,Complex{T}}) where {T<:Union{Float32,Float64}}
           invx=1.0/x; a = abs(x)
           if a < _sinc_threshold(T) return evalpoly(x^2, (T(1), -T(pi)^2/6, T(pi)^4/120)) else return invx*sinpi(x) end
       end

@code_native _sinc2(11.4)

```

My guess is that Julia prefers the much cheaper evalpoly code path, even with it very unlikely to be taken. It’s not helpful to filp the condition. Any way to force my first statement to actually schedule first?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 12, 2020, 9:05pm UTC](https://discourse.julialang.org/t/does-julia-have-unlikely-hint-similar-to-in-c-or-a-way-to-fix-code-generation-here/46528/2 "2020-09-12T21:05:05Z")

</div>

For this case specifically, you might want to try `ifelse` I could see not taking the branch as a possible win (assuming `sinpi` isn’t too slow).

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [September 12, 2020, 10:01pm UTC](https://discourse.julialang.org/t/does-julia-have-unlikely-hint-similar-to-in-c-or-a-way-to-fix-code-generation-here/46528/3 "2020-09-12T22:01:49Z")

</div>

On Julia 1.5 and earlier:

```julia
@inline function expect(b::Bool)
    llvmcall(("declare i1 @llvm.expect.i1(i1, i1)", """
    %b = trunc i8 %0 to i1
    %actual = call i1 @llvm.expect.i1(i1 %b, i1 true)
    %byte = zext i1 %actual to i8
    ret i8 %byte
    """), Bool, Tuple{Bool}, b)
end

```

Edit: this would be a “likely” hint. For unlikely, make the second argument `false`.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 12, 2020, 10:13pm UTC](https://discourse.julialang.org/t/does-julia-have-unlikely-hint-similar-to-in-c-or-a-way-to-fix-code-generation-here/46528/4 "2020-09-12T22:13:26Z")

</div>

This at least does something as you say on Julia 1.5.1 (with this modification), not on 1.6, and I’m not sure _where_ to apply it (or fix for 1.6):

```julia
julia> @inline function expect(b::Bool)
                  Core.Intrinsics.llvmcall(("declare i1 @llvm.expect.i1(i1, i1)", """
                  %b = trunc i8 %0 to i1
                  %actual = call i1 @llvm.expect.i1(i1 %b, i1 true)
                  %byte = zext i1 %actual to i8
                  ret i8 %byte
                  """), Bool, Tuple{Bool}, b)
              end
expect (generic function with 1 method)

julia> expect(true)
ERROR: Module IR does not contain specified entry function
Stacktrace:
 [1] expect(b::Bool)
   @ Main ./REPL[112]:2
 [2] top-level scope
   @ REPL[113]:1

```

Anyway, ifelse, seemed to work, while the division wasn’t the exact first instruction 8maybe a good choice). It’s good to know, while it didn’t really help me optimize in this case.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 12, 2020, 10:15pm UTC](https://discourse.julialang.org/t/does-julia-have-unlikely-hint-similar-to-in-c-or-a-way-to-fix-code-generation-here/46528/5 "2020-09-12T22:15:59Z")

</div>

Is this effective (i.e., changes the machine code in a meaningful way)? I think I’ve read somewhere (probably in `julia`’s issue tracker) that the optimization pass for this is not enabled in `julia`.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [September 12, 2020, 10:41pm UTC](https://discourse.julialang.org/t/does-julia-have-unlikely-hint-similar-to-in-c-or-a-way-to-fix-code-generation-here/46528/6 "2020-09-12T22:41:55Z")

</div>

@Palli  
For 1.6:

```julia
@inline function expect(b::Bool)
    Bsae.llvmcall((" declare i1 @llvm.expect.i1(i1, i1)\n\n define i8 @entry(i8) alwaysinline {\n top:\n %b = trunc i8 %0 to i1\n%actual = call i1 @llvm.expect.i1(i1 %b, i1 true)\n
%byte = zext i1 %actual to i8\nret i8 %byte\n }\n", "entry"), Bool, Tuple{Bool}, b)
end

```

@tkf  
That would explain why I hadn’t notice it make any real differences.  
Perhaps it does through preventing optimizations, but I wouldn’t exactly call that desirable in general.

EDIT:  
Seems we need the [LowerExpectIntrinsic](https://llvm.org/doxygen/LowerExpectIntrinsic_8cpp_source.html) pass.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 13, 2020, 1:19am UTC](https://discourse.julialang.org/t/does-julia-have-unlikely-hint-similar-to-in-c-or-a-way-to-fix-code-generation-here/46528/7 "2020-09-13T01:19:21Z")

</div>

since Julia is a dynamic language, I’d imagine people are naturally more conscious about putting the most hit / possible branch at the top. Besides, Julia doesn’t have a `switch case` anyways.
