# Using LLVM intrinsic functions inside \`llvmcall\`

**URL:** https://discourse.julialang.org/t/using-llvm-intrinsic-functions-inside-llvmcall/87955
**Category:** Performance
**Tags:** llvm
**Created:** [September 28, 2022, 10:11pm UTC](https://discourse.julialang.org/t/using-llvm-intrinsic-functions-inside-llvmcall/87955 "2022-09-28T22:11:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [September 28, 2022, 10:11pm UTC](https://discourse.julialang.org/t/using-llvm-intrinsic-functions-inside-llvmcall/87955/1 "2022-09-28T22:11:29Z")

</div>

I’m playing around with `llvmcall`. I can use LLVM IR instructions (like [`add`](https://llvm.org/docs/LangRef.html#add-instruction)), but I cannot get intrinsic functions (like [`@llvm.abs.i32`](https://llvm.org/docs/LangRef.html#llvm-abs-intrinsic)) to work. From the post [Debugging float SIMD Intrinsics via llvmcall](https://discourse.julialang.org/t/debugging-float-simd-intrinsics-via-llvmcall/27855) I get the impression that it should possible. However, when I try to run the example discussed in that post, I get the error “Module IR does not contain specified entry function”. (Have the arguments to `llvmcall` possibly changed since then?) Anyway, an example of calling an intrinsic function would be appreciated.

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [September 28, 2022, 10:21pm UTC](https://discourse.julialang.org/t/using-llvm-intrinsic-functions-inside-llvmcall/87955/2 "2022-09-28T22:21:06Z")

</div>

Did you declare the intrinsic before calling it? Like in the example?

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [September 28, 2022, 10:36pm UTC](https://discourse.julialang.org/t/using-llvm-intrinsic-functions-inside-llvmcall/87955/3 "2022-09-28T22:36:59Z")

</div>

I think so. Following the [advice](https://discourse.julialang.org/t/debugging-float-simd-intrinsics-via-llvmcall/27855/4) about the input argument type, I wrote:

```julia
using SIMD
function rsqrt(f)
    Base.llvmcall(("declare <4 x float> @llvm.x86.sse.rsqrt.ps(<4 x float>) nounwind readnone", " 
              %2 = call <4 x float> @llvm.x86.sse.rsqrt.ps(<4 x float> %0)
              ret <4 x float> %2"), Vec{4, Float32}, Tuple{NTuple{4, VecElement{Float32}},},
        (VecElement(f[1]),VecElement(f[2]),VecElement(f[3]),VecElement(f[4])))
end

```

Then I get

```julia
julia> rsqrt(Vec(Float32(1),Float32(1),Float32(1),Float32(1)))
ERROR: Module IR does not contain specified entry function

```

The [documentation](https://docs.julialang.org/en/v1/base/c/#Core.Intrinsics.llvmcall) of `llvmcall` says that if the first argument is a tuple, then its second member must be “a string representing the name of the entry-point function to call”. In the example, this doesn’t seem to be the case.

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [September 28, 2022, 10:42pm UTC](https://discourse.julialang.org/t/using-llvm-intrinsic-functions-inside-llvmcall/87955/4 "2022-09-28T22:42:00Z")

</div>

I see the issue here, you are passing the IR via a tuple, that method requires a tuple with an LLVM IR module with defined functions and what function to call. The way you are using it, all the IR should be in the same string without a tuple.

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [September 28, 2022, 10:47pm UTC](https://discourse.julialang.org/t/using-llvm-intrinsic-functions-inside-llvmcall/87955/5 "2022-09-28T22:47:41Z")

</div>

Could you post a working example? I’ve already tried

```julia
@generated function f(x::Int64)
    quote
        Base.llvmcall(("""
    declare i64 @llvm.abs.i64(i64, i1)
    %x = call i64 @llvm.abs.64(i64 %0, i1 0)
    ret i64 %x
            """), Int64, Tuple{Int64}, x)
    end
end

```

but that gives

```julia
julia> f(1)
ERROR: Failed to parse LLVM assembly: 
<string>:3:1: error: expected instruction opcode
declare i64 @llvm.abs.i64(i64, i1)
^

```

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [September 28, 2022, 11:00pm UTC](https://discourse.julialang.org/t/using-llvm-intrinsic-functions-inside-llvmcall/87955/6 "2022-09-28T23:00:58Z")

</div>

I’m sorry, in your case I think you have to use the module level IR.  
with declarations and a function definition

```julia
function foo(x)
       Base.llvmcall(("""
                  declare i64 @llvm.abs.i64(i64, i1)
                  define i64 @entry(i64) {
                  %x = call i64 @llvm.abs.i64(i64 %0, i1 0)
                  ret i64 %x
                  }
                      ""","entry"), Int64, Tuple{Int64}, x)
                          end

```

There are a bunch of examples in [julia/llvmcall.jl at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/test/llvmcall.jl) and  
[julia/llvmcall2.jl at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/test/llvmcall2.jl)

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [September 28, 2022, 11:16pm UTC](https://discourse.julialang.org/t/using-llvm-intrinsic-functions-inside-llvmcall/87955/7 "2022-09-28T23:16:19Z")

</div>

Great! Thanks!
