Nvidia不允许在用CUDA C/C++编写的GPU内核编译流程中访问生成的LLVM IR。我想知道,如果我使用Alea,这是否可能?换句话说,Alea编译过程是否允许保留生成的优化/未优化的LLVM IR代码?
发布于 2015-07-02 14:13:24
是的,你是对的,Nvidia没有向你展示LLVM IR,你只能得到PTX代码。虽然Alea允许您以多种方式访问LLVM IR:
方法1
使用基于工作流的方法将GPU模块编码为模板,然后将模板编译为LLVM IR模块,然后将LLVM IRModule (可选地与其他IR模块)链接到PTX模块。最后,将PTX模块加载到GPU工作程序中。当您获得LLVM IRModule时,可以调用它的方法Dump()将IR代码打印到控制台。或者你可以得到比特码作为byte[]。
我建议你在这里读更多的细节:
F#应该是这样的:
let template = cuda {
// define your kernel functions or other gpu moudle stuff
let! kernel = <@ fun .... @> |> Compiler.DefineKernel
// return an entry pointer for this module, something like the
// main() function for a C program
return Entry(fun program ->
let worker = program.Worker
let kernel = program.Apply kernel
let main() = ....
main ) }
let irModule = Compiler.Compile(template).IRModule
irModule.Dump() // dump the IR code
let ptxModule = Compiler.Link(irModule).PTXModule
ptxModule.Dump()
use program = worker.LoadProgram(ptxModule)
program.Run(...)方法2
如果使用方法基或基于实例方式编码GPU模块,则可以为通过Alea.CUDA.Events生成的LLVM IR代码和PTX添加事件处理程序。F#中的代码如下所示:
let desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
let (@@) a b = Path.Combine(a, b)
Events.Instance.IRCode.Add(fun ircode ->
File.WriteAllBytes(desktopFolder @@ "module.ir", ircode))
Events.Instance.PTXCode.Add(fun ptxcode ->
File.WriteAllBytes(desktopFolder @@ "module.ptx", ptxcode))使用LLVM代码扩展GPU功能
最后,有一种无文档化的方法,允许您直接操作LLVM代码来构造函数。它是通过属性实现的,实现了一些红外建筑界面。下面是一个简单的示例,它接受参数,并打印它(在编译时),然后返回:
[<AttributeUsage(AttributeTargets.Method, AllowMultiple = false)>]
type IdentityAttribute() =
inherit Attribute()
interface ICustomCallBuilder with
member this.Build(ctx, irObject, info, irParams) =
match irObject, irParams with
| None, irParam :: [] ->
// the irParam is of type IRValue, which you
// can get the LLVM native handle, by irParam.LLVM
// Also, you can get the type by irParam.Type, which
// is of type IRType, again, you can get LLVMTypeRef
// handle by irParam.Type.LLVM
// You can optionally construct LLVM instructions here.
printfn "irParam: %A" irParam
Some irParam
| _ -> None
[<Identity>]
let identity(x:'T) : 'T = failwith "this is device function, better not call it from host"https://stackoverflow.com/questions/31174248
复制相似问题