julia icon indicating copy to clipboard operation
julia copied to clipboard

Make code_llvm return an object that overloads show

Open oxinabox opened this issue 1 year ago • 7 comments

Motivation is i want clipboard(@code_llvm foo()) to work. Which it currently doesn't as @code_llvm foo() returns nothing and just prints directly.

I suspect that the code_* functions are older than our current show infrastructure. And they don't follow its conventions, that rather than printing directly, prefer returning objects that overload show. This fixes it for @code_llvm, while leaving in place the methods that take an IO as the first argument

oxinabox avatar Feb 06 '24 05:02 oxinabox

I wonder if we might change the fields of LLVMCode in the future. Thus my suggestions follow.

  1. Clearly document that the internal fields of LLVMCode are not public API.
  2. Provide a convert(String, ::LLVMCode) and String(::LLVMCode) methods as a public API to obtain a string representation of LLVMCode.

mkitti avatar Feb 06 '24 21:02 mkitti

I suspect that the code_* functions are older than our current show infrastructure.

Not really, no, they are likely newer. These functions are similar to much of the infrastructure for expressing show in specific formats, such as Base.Ryu.writefixed or join or dump or such. The oddballs are really the other code_typed/code_lowered functions, since (unlike these and code_warntype), they intermix both IO formatting questions (e.g. debuginfo flags) and computational questions (e.g. optimize=false) as they return objects with custom show methods.

vtjnash avatar Feb 07 '24 01:02 vtjnash

I wonder if we might change the fields of LLVMCode in the future.

Unlikely. Note that this is InteractiveUtils.LLVMCode, not part of Core.Compiler, nor part of Base's reflection functionality. Odds are if we were to do something more complex than a string, it would be there. and If we did we would likely rename this type to avoid confusion.

  1. Clearly document that the internal fields of LLVMCode are not public API.

All internal fields are not public API unless documented otherwise. This type is internal and not public.

  1. Provide a convert(String, ::LLVMCode) and String(::LLVMCode) methods as a public API to obtain a string representation of LLVMCode.

string(::LLVMCode) and repr(::LLVMCode) should already work to do that (you get them for free by implementing show). and those are already defined as the way to get string representations of things.

oxinabox avatar Feb 07 '24 02:02 oxinabox

and those are already defined as the way to get string representations of things

That seems not quite a correct string representation then, since the result of show should usually be somewhat round-trip capable. So the show would typically be LLVMCode("llvm_module\ngoes\nhere\nunreadable....skipping 1234 bytes...{0} = {1}")

vtjnash avatar Feb 07 '24 02:02 vtjnash

To me, this looks more like a deficiency with clipboard than with all methods that output things to an IO.

KristofferC avatar Feb 08 '24 12:02 KristofferC

Maybe we need a version of macro version @sprint, so it can be written as clipboard(@sprint @code_llvm foo()) ?

When using the function form of IO functions such as code_llvm, you would typically write this: clipboard(sprint(code_llvm, foo, ()))

vtjnash avatar Feb 08 '24 18:02 vtjnash

As an alternative solution, would it make sense to allow for an optional io=BLA option to @code_llvm etc.?

fingolfin avatar Feb 15 '24 22:02 fingolfin