`@code_typed (-2 <= 1 <= 2)` fails

I’m stumped by this:

julia> -2 <= 1 <= 2
true

julia> @code_typed (-2 <= 1 <= 2)
ERROR: expression is not a function call, or is too complex for @code_typed to analyze; break it down to simpler parts if possible. In some cases, you may want to use Meta.@lower.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] top-level scope
   @ /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/InteractiveUtils/src/macros.jl:222

Same thing happens with @code_llvm. OTOH @code_typed (-2 <= 1) works fine. Why can’t I see what these compile down to?

1 Like

The error message gives the right hint. It has to be a function call, or be simple ‘enough’ (-2 <= 1 is just a single function call).

This works:

jl> foo() = (-2 <= 1 <= 2)
foo (generic function with 1 method)

jl> @code_typed foo()
CodeInfo(
1 ─     return true
) => Bool

but is perhaps not that interesting, it’s just a constant. You can try this instead:

jl> foo(x) = (-2 <= x <= 2)
foo (generic function with 2 methods)

jl> @code_typed foo(1)
CodeInfo(
1 ─ %1 = Base.sle_int(-2, x)::Bool
└──      goto #3 if not %1
2 ─ %3 = Base.sle_int(x, 2)::Bool
└──      return %3
3 ─      return false
) => Bool
4 Likes

I see… The boundary between “simple enough” and “too complex” is vague to me. But thank you!

Well, if it’s a single function call, then it’s simple enough. And -2 <= 1 is a single call to the function <= with two input arguments, so it’s <=(-2, 1).

So, if in doubt, just create a function that calls your code.

1 Like