In many cases, you can write generic code and llvm will attempt match it to
the optimal instruction sequence. (to your second question: no, ccall does
not substantially support llvm intrinsics, although llvm does intercept
some stdlib calls and replace them with intrinsics).
julia> f(x,y) = widemul(x,y)
f (generic function with 1 method)
julia> code_llvm(f, (Int64,Int64))
define i128 @julia_f_20526(i64, i64) {
top:
%2 = sext i64 %0 to i128
%3 = sext i64 %1 to i128
%4 = mul i128 %3, %2
ret i128 %4
}
julia> code_native(f,(Int64,Int64))
.section __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 1
pushq %rbp
movq %rsp, %rbp
Source line: 1
movq %rsi, %rax
imulq %rdi
popq %rbp
ret
On Sun, May 17, 2015 at 8:33 PM andrew cooke <[email protected]> wrote:
>
> more exactly, i think it's called llvm.x86.pclmulqdq and it may be
> possible to call it from ccall, as that seems to support llvm intrinsics (i
> am still reading around and am busy next week, so will probably try in
> about a week's time). andrew
>
>
> On Sunday, 17 May 2015 15:42:55 UTC-3, andrew cooke wrote:
>>
>>
>>
>>
>> http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20150126/255137.html
>>
>> On Sunday, 17 May 2015 15:23:31 UTC-3, Isaiah wrote:
>>>
>>> What instruction are you trying to call?
>>>
>>> On Sun, May 17, 2015 at 11:21 AM, andrew cooke <[email protected]>
>>> wrote:
>>>
>>>>
>>>> ah, thanks - that issue is a huge help.
>>>>
>>>> On Sunday, 17 May 2015 11:38:20 UTC-3, Isaiah wrote:
>>>>>
>>>>> i am going to look at the implementing code and work out what is
>>>>>> generated, i think, and then ask llvmdev for help.
>>>>>
>>>>>
>>>>> You should start with lli and make sure that you are writing the IR
>>>>> correctly; if it works in lli, then the issue is with Julia (as is most
>>>>> likely -- llvmcall is kind of brittle).
>>>>>
>>>>>
>>>>>> here's something i don't understand about the context in which the
>>>>>> llvm ir is inserted. "declare" instructions don't seem to be accepted,
>>>>>> for
>>>>>> example.
>>>>>
>>>>>
>>>>> See
>>>>> https://github.com/JuliaLang/julia/pull/8740
>>>>>
>>>>>
>>>>> On Sun, May 17, 2015 at 10:17 AM, andrew cooke <[email protected]>
>>>>> wrote:
>>>>>
>>>>>> thanks, but the reason i need lvmcall is because i want to call a
>>>>>> specific intel instruction (which llvm actually supports - i've found the
>>>>>> patch for it). but before i do that i am trying to just get "something"
>>>>>> to
>>>>>> work, and printf seemed like a good intermediate goal.
>>>>>>
>>>>>> i am going to look at the implementing code and work out what is
>>>>>> generated, i think, and then ask llvmdev for help. there's something i
>>>>>> don't understand about the context in which the llvm ir is inserted.
>>>>>> "declare" instructions don't seem to be accepted, for example. once i
>>>>>> can
>>>>>> pin that down i think i can ask a sensible question on llvmdev...
>>>>>>
>>>>>> cheers,
>>>>>> andrew
>>>>>>
>>>>>>
>>>>>> On Saturday, 16 May 2015 22:01:25 UTC-3, Yichao Yu wrote:
>>>>>>
>>>>>>> On Sat, May 16, 2015 at 5:01 PM, andrew cooke <[email protected]>
>>>>>>> wrote:
>>>>>>> >
>>>>>>> > Does anyone have a working example that calls printf via llvmcall?
>>>>>>> >
>>>>>>> > I realise I'm uncomfortably inbetween llvmdev and julia-users, but
>>>>>>> I'm
>>>>>>> > asking here first because I suspect my limitations are still more
>>>>>>> > julia-related.
>>>>>>> >
>>>>>>> > In particular,
>>>>>>> >
>>>>>>> > julia> g() = Base.llvmcall("""
>>>>>>> > call i32 (i8*, ...)* @printf(i8* c"hello world\00")
>>>>>>> > ret""",
>>>>>>> > Void, Tuple{})
>>>>>>> > g (generic function with 2 methods)
>>>>>>> >
>>>>>>> > julia> g()
>>>>>>> > ERROR: error compiling g: Failed to parse LLVM Assembly:
>>>>>>> > julia: llvmcall:3:35: error: expected string
>>>>>>> > call i32 (i8*, ...)* @printf(i8* c"hello world
>>>>>>> > ^
>>>>>>> >
>>>>>>> > seems like it's *almost* there...?
>>>>>>> >
>>>>>>> > Thanks,
>>>>>>> > Andrew
>>>>>>> >
>>>>>>> > (I suspect I also need something other than @printf, like
>>>>>>> > IntrinsicsX86.printf or something, but I can't find where I saw an
>>>>>>> example
>>>>>>> > like that... Related, declare doesn't seem to be accepted, or
>>>>>>> assignment to
>>>>>>> > global vsariables. But I am completely new to all this...)
>>>>>>> >
>>>>>>>
>>>>>>> I was also interested in knowning how to use `llvmcall` in general
>>>>>>> but
>>>>>>> at least for this limited case, (and I guess you probably know
>>>>>>> already) it is easier to user `ccall`
>>>>>>>
>>>>>>> ```julia
>>>>>>> julia> ccall(:printf, Int, (Ptr{Cchar},), "hellow world\n")
>>>>>>> hellow world
>>>>>>> 13
>>>>>>> ```
>>>>>>>
>>>>>>
>>>>>
>>>