Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend llvmcall with support for entering declarations. #8740

Closed
wants to merge 2 commits into from
Closed

Extend llvmcall with support for entering declarations. #8740

wants to merge 2 commits into from

Conversation

maleadt
Copy link
Member

@maleadt maleadt commented Oct 20, 2014

This is a copy of PR #8673 -- I accidentally created the old PR from my master branch, which isn't very clean.

This PR adds support to llvmcall for inserting required declarations in the module. This is useful when you want to llvmcall intrinsics, because LLVM refuses to parse IR with undeclared identifiers (for example, see this thread).

The idea behind this solution is to allow passing a tuple (decls, ir)as first argument to llvmcall, where currently only an IR string or a pointer to a LLVM function is allowed. The decls IR is then pasted before the function llvmcall generates. In order to avoid duplicate declarations, which LLVM does not like, a global set keeps track of the declarations added to the current module.

Example usage:

function floor(x::Float64)
  llvmcall(
    ("""declare double @llvm.floor.f64(double)""",
     """%2 = call double @llvm.floor.f64(double %0)
        ret double %2"""),
    Float64, (Float64,), x)
end

As mentioned in issue #8308 I also tried other approaches, such as automatically parsing identifiers in the IR, but that turned out pretty fragile.

@simonbyrne
Copy link
Contributor

It would be great to have something like this to be able to use LLVM intrinsics from with llvmcall. @Keno You mentioned on another thread about making declarations automatic?

@SimonDanisch
Copy link
Contributor

Is this related?
It seems, that llvmcall does try to declare the type correctly, but it somehow doesn't get through with it.
Bug, not implemented or my fault?

immutable Matrix4x4
    i_1::Float64
    i_2::Float64
    i_3::Float64
    i_4::Float64
    i_5::Float64
    i_6::Float64
    i_7::Float64
    i_8::Float64
    i_9::Float64
    i_10::Float64
    i_11::Float64
    i_12::Float64
    i_13::Float64
    i_14::Float64
    i_15::Float64
    i_16::Float64
end

function mulllvm(a::Matrix4x4, b::Matrix4x4)
    Base.llvmcall("""
        %ptr = getelementptr <4 double>, %Matrix4x4* %0, i64 0, i64 0
        %3 = load <4 x double>, <4 double>* %ptr
        ret <4 x double> %3
        """, NTuple{4, Float64},
        (Matrix4x4, Matrix4x4),
        a,b)
end
const x = Matrix4x4(rand(16)...)
println(mulllvm(x,x))
#yields:
ERROR: LoadError: error compiling mulllvm: Failed to parse LLVM Assembly: 
julia: <string>:2:56: error: expected ')' at end of argument list
define <4 x double> @"julia_mulllvm_426531"(%Matrix4x4 = type { double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double } ,%Matrix4x4 = type { double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double } ) {
                                                       ^

 in include_from_node1 at loading.jl:129
while loading C:\Users\Sim\.julia\v0.4\playground.jl\llvmcall.jl, in expression starting on line 30

@ihnorton
Copy link
Member

ihnorton commented Apr 1, 2015

This would be really helpful for debugging codegen issues.

@ihnorton ihnorton added needs decision A decision on this change is needed compiler:codegen Generation of LLVM IR and native code labels Apr 2, 2015
@tkelman
Copy link
Contributor

tkelman commented Jun 6, 2015

Closing in favor of #11604, thanks to @vchuravy for picking up this work

@tkelman tkelman closed this Jun 6, 2015
@maleadt maleadt deleted the pr_llvmcall branch October 29, 2015 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:codegen Generation of LLVM IR and native code needs decision A decision on this change is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants