Declaring an external function

124 views
Skip to first unread message

Håvard Wormdal Høiby

unread,
Nov 21, 2014, 5:21:00 AM11/21/14
to juli...@googlegroups.com
My aim is to run a subset of julia on a nvidia gpu without extending the compiler to much.
I've come to a place where I want to declare an external function in Julia code.

Proposed example:
@external get_global_id(Int32) = Int64

function add(x,y,z)
  i = get_global_id(int32(0))

  z[i] = x[i] + y[i]
  
  return
end

My goal is that calling @code_llvm on add should produce:

define ...add...(...) {
  ...
  ... = call i64 @get_global_id(i32 0)
  ...
}

The get_global_id function has not been inlined nor name mangled.

My current solution is to extend the compiler with a @noinline macro analogous to @inline.
And @code_llvm will produce:

define ...add...(...) {
  ...
  ... = call i64 @"julia_get_global_id;81930"(i32 0)
  ...
}

Thus the most important task of not inlining the function is handled but I have to account for name mangling manually, and I have to add code to inline_worthy. 

A further proposition, that would make my implementation more robust, is to add a @code_module macro. The macro takes the same arguments as @code_llvm, but returns a llvm module definition with the call graph (i.e. definitions of the function it calls) and the metadata of the of the functions preserved. In the example above this module would include a declaration of the get_global_id function but no definition for it.

Does anyone have any thoughts on the subject, other ways of doing this or perhaps other applications of this suggestion?

Thanks. Håvard

Isaiah Norton

unread,
Nov 21, 2014, 7:39:56 AM11/21/14
to juli...@googlegroups.com
Have you looked at Base.llvmcall?
Reply all
Reply to author
Forward
0 new messages