# What functions are lowered to native instructions?

**URL:** <https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [September 24, 2022, 12:10pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741 "2022-09-24T12:10:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [September 24, 2022, 12:10pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741/1 "2022-09-24T12:10:50Z")

</div>

Many times, when seeing others’ code or Base, I run into comments like “This function is used instead of X because it has native instructions for many architectures”.

Is there any way to know which functions behave like this?

E.G.: [Base.bitarray](https://github.com/JuliaLang/julia/blob/master/base/bitarray.jl#L121)

---

<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 24, 2022, 12:53pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741/2 "2022-09-24T12:53:39Z")

</div>

[Bit Twiddling Hacks](https://graphics.stanford.edu/~seander/bithacks.html) is a really good guide to the processor side of this.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 24, 2022, 1:44pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741/3 "2022-09-24T13:44:26Z")

</div>

Every function in Julia is compiled to a sequence of native CPU instructions.

But I guess you are asking what functions compile to a _single_ CPU instruction. That depends mainly on the instruction set of your CPU architecture — if the instruction exists, then LLVM will typically produce it from the most obvious corresponding high-level code. You can find many guides online to the instruction sets of various CPUs (though they are not light reading!).

Alternatively, you can use the [`@code_native` macro](https://docs.julialang.org/en/v1/stdlib/InteractiveUtils/#InteractiveUtils.@code_native) to see the compiled code for a given function, and you can decipher this in simple cases to figure out whether a single instruction is produced. For example, in the case you linked:

```julia
julia> f(x) = x & (x-1)
f (generic function with 1 method)

julia> @code_native f(3)
	.section	__TEXT,__ text,regular,pure_instructions
	.build_version macos, 12, 0
	.globl	_julia_f_332 ## -- Begin function julia_f_332
	.p2align	4, 0x90
_julia_f_332: ## @julia_f_332
; ┌ @ REPL[10]:1 within `f`
	.cfi_startproc
## %bb.0: ## %top
; │┌ @ int.jl:340 within `&`
	blsrq	%rdi, %rax
; │└
	retq
	.cfi_endproc
; └
                                        ## -- End function
.subsections_via_symbols

```

which tells you that `x & (x-1)` for `x::Int64` is compiled on x86\_64 to a single [BLSR (reset lowest set bit)](https://www.felixcloutier.com/x86/blsr) instruction.

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [September 24, 2022, 3:20pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741/4 "2022-09-24T15:20:09Z")

</div>

Yep, this is the kind of things I was referring to.

I knew about `@code_native`, but this allows to know for specific functions. Is there a comprehensive guide for which functions are lowered to single instructions?

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [September 24, 2022, 3:39pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741/5 "2022-09-24T15:39:07Z")

</div>

What do you mean you comprehensive? A review of all functions in Base that compile to a single instruction?

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [September 24, 2022, 3:51pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741/6 "2022-09-24T15:51:39Z")

</div>

Yep? 😄

Maybe it is not possible, I was just curious. It could be quite educational material.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 24, 2022, 4:01pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741/7 "2022-09-24T16:01:56Z")

</div>

I doubt there is such a thing, and that’d also be architecture-dependent. The function `f` above compiles to two instructions on aarch64:

```julia
julia> @code_native debuginfo=:none f(3)
        .text
        .file "f"
        .globl julia_f_137 // -- Begin function julia_f_137
        .p2align 3
        .type julia_f_137,@function
julia_f_137: // @julia_f_137
        .cfi_startproc
// %bb.0: // %top
        sub x8, x0, #1
        and x0, x8, x0
        ret
.Lfunc_end0:
        .size julia_f_137, .Lfunc_end0-julia_f_137
        .cfi_endproc
                                        // -- End function
        .section ".note.GNU-stack","",@progbits

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [September 24, 2022, 4:05pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741/8 "2022-09-24T16:05:46Z")

</div>

It could be cool. It seems like a great exercise for someone with a passion for the topic 🙂

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 24, 2022, 5:54pm UTC](https://discourse.julialang.org/t/what-functions-are-lowered-to-native-instructions/87741/9 "2022-09-24T17:54:18Z")

</div>

It would be very difficult to guarantee that anything compiles to a single native instruction since Julia is used on many platforms. What compiles to a single x86\_64 instruction may not compile to a single aarch64 instruction.

Going up a level of abstraction there is LLVM IR. You can view that via `@code_llvm`. In this case, we have a facility in `Core.intrinsics.llvmcall`:

[https://docs.julialang.org/en/v1/base/c/#Core.Intrinsics.llvmcall](https://docs.julialang.org/en/v1/base/c/#Core.Intrinsics.llvmcall)

From there you can use LLVM intrinsics:  
[https://llvm.org/docs/LangRef.html](https://llvm.org/docs/LangRef.html)

One example of how llvmcall is used can be found in SIMD.jl:

> <https://github.com/eschnett/SIMD.jl/blob/master/src/LLVM_intrinsics.jl>

Another example is VectorizationBase.jl

> <https://github.com/JuliaSIMD/VectorizationBase.jl/blob/master/src/llvm_intrin/unary_ops.jl>

More often than not the LLVM intrinsics correspond to one or a few native instructions.
