# Code\_llvm and and code\_native should return their output

**URL:** https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581
**Category:** Internals & Design
**Created:** [July 13, 2021, 3:18pm UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581 "2021-07-13T15:18:11Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [July 13, 2021, 3:18pm UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/1 "2021-07-13T15:18:12Z")

</div>

As it stands `code_typed` returns its output, but `code_llvm` and `code_native` return `Nothing` and instead print their output to an io stream. I would like to propose that all three have no side effects and return some kind of Julia object which displays nicely.

> **Examples**
>
> ```nohighlight
> julia> code_typed(abs, (Int,)) |> typeof
> Vector{Any} (alias for Array{Any, 1})
> 
> julia> code_llvm(abs, (Int,)) |> typeof
> ; @ int.jl:170 within `abs'
> define i64 @julia_abs_1497(i64 signext %0) {
> top:
> ; ┌ @ int.jl:130 within `flipsign'
> %1 = icmp slt i64 %0, 0
> %2 = sub i64 0, %0
> %3 = select i1 %1, i64 %2, i64 %0
> ; └
> ret i64 %3
> }
> Nothing
> 
> julia> code_native(abs, (Int,)) |> typeof
> .section __TEXT,__ text,regular,pure_instructions
> ; ┌ @ int.jl:170 within `abs'
> ; │┌ @ int.jl:130 within `flipsign'
> movq %rdi, %rax
> negq %rax
> cmovlq %rdi, %rax
> ; │└
> retq
> nopl (%rax,%rax)
> ; └
> Nothing
> 
> ```

I ran into this when trying to use these functions in a Pluto.jl notebook. `code_typed` works just fine but the other two don’t display their output in the notebook, which is rather annoying. You can get around this by redirecting stdout or by using `sprint` and returning a `String`, but it doesn’t look nice that way.

Would the way to fix this be to have equivalents of the `CodeInfo` type like a `LLVMInfo` and `NativeInfo` types, that just contain a string and get displayed nicely if `show`ed?

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [July 13, 2021, 8:48pm UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/2 "2021-07-13T20:48:47Z")

</div>

I opened an issue at [Proposal: `code_llvm` and and `code_native` should return their output · Issue #41570 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/41570).

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 13, 2021, 10:11pm UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/3 "2021-07-13T22:11:29Z")

</div>

As a reference, the way e.g. `code_llvm` work is that it calls into LLVM and llvm does the printing:

> <https://github.com/JuliaLang/julia/blob/7d0f7693fd9999f88623dc435e0f83dc9681fbd1/src/disasm.cpp#L457>

So there is no preceding Julia object that is being printed here.

---

<div class="post-metadata">

### Author: ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)
#### Post date: [July 14, 2021, 2:00am UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/4 "2021-07-14T02:00:12Z")

</div>

`code_llvm` and `code_naive` have an optional parameter `io` to redirect IO:

```julia
help?> code_llvm
search: code_llvm @code_llvm

  code_llvm([io=stdout,], f, types; raw=false, dump_module=false, optimize=true, debuginfo=:default)

  Prints the LLVM bitcodes generated for running the method matching the given generic function and type signature to io.

```

So if you want to get the string, you can do:

```julia
io = IOBuffer()
code_llvm(io,sin,(Float64,))
String(take!(io))

```

Then you get the string of output.  
I admit it’s annoying, but I think you can wrap above code into a new macro to get around this problem?

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [July 14, 2021, 5:28am UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/5 "2021-07-14T05:28:06Z")

</div>

Yeah, you can also do this with `sprint`. The problem is, that a Julia `String` does not get displayed by `print`ing it. Instead, the raw form gets shown in the REPL, which is rather ugly.

```nohighlight
julia> my_code_native(args...; kwargs...) = sprint() do io
           code_native(io, args...; kwargs...)
       end
my_code_native (generic function with 1 method)

julia> my_code_native(abs, (Int,))
"\t.section\t __TEXT,__ text,regular,pure_instructions\n; ┌ @ int.jl:170 within `abs'\n; │┌ @ int.jl:130 within `flipsign'\n\tmovq\t%rdi, %rax\n\tnegq\t%rax\n\tcmovlq\t%rdi, %rax\n; │└\n\tretq\n\tnopl\t(%rax,%rax)\n; └\n"

```

So, I was proposing something more like this

```nohighlight
import Base: show
using InteractiveUtils: code_llvm, code_native

struct CodeLLVMInfo{F <: Function, Ts <: Tuple}
    f::F
    types::Ts
end

function show(io::IO, ::MIME"text/plain", info::CodeLLVMInfo)
    code_llvm(io, info.f, info.types)
end

proposed_code_llvm(f, types) = CodeLLVMInfo(f, types)

display(proposed_code_llvm(abs, (Int,)))

```

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [July 14, 2021, 6:09am UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/6 "2021-07-14T06:09:53Z")

</div>

Another solution would be to make all three return `nothing` and print their output. You could make them consistent like that, too.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 14, 2021, 7:12am UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/7 "2021-07-14T07:12:30Z")

</div>

Doesn’t Pluto have a way to display strings nicely? So you would compose `sprint` with that?

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [July 14, 2021, 9:02am UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/8 "2021-07-14T09:02:05Z")

</div>

I don’t know if Pluto has a way of showing printed strings. I tested the `sprint` code and it doesn’t display nicely. May be there’s some other way to do it though.

In any case, I think the three inspection functions should be consistent. So, either all should print or all should return their output.

Here’s what I tried.

```nohighlight
### A Pluto.jl notebook ###
# v0.15.1

using Markdown
using InteractiveUtils

# ╔═╡ fd998ae0-e480-11eb-2aa1-8317fbbf5333
my_code_native(args...; kwargs...) = sprint() do io
   code_native(io, args...; kwargs...)
end

# ╔═╡ 3fa04cfe-ebd5-4a7d-885d-6a6eae4a5a53
my_code_native(abs, (Int,))

# ╔═╡ Cell order:
# ╠═fd998ae0-e480-11eb-2aa1-8317fbbf5333
# ╠═3fa04cfe-ebd5-4a7d-885d-6a6eae4a5a53

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 14, 2021, 9:05am UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/9 "2021-07-14T09:05:50Z")

</div>

Looks like you can use `Text`:

![bild](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a67bc124e13d7a268c74ff47ce2a9c68a7f273a.png)

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [July 14, 2021, 9:46am UTC](https://discourse.julialang.org/t/code-llvm-and-and-code-native-should-return-their-output/64581/10 "2021-07-14T09:46:07Z")

</div>

Thanks, that solves one issue.
