-
Notifications
You must be signed in to change notification settings - Fork 10
/
ptx.R
62 lines (40 loc) · 1.49 KB
/
ptx.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
library(Rllvm)
#InitializeAllTargets()
#InitializeAllTargetMCs()
#InitializeAllAsmPrinters()
#InitializeAllAsmParsers()
#.Call("R_initPassRegistry", NULL)
m = parseIR("simple-gpu64.ll")
# setMetadata(m, "nvvm.annotation", list(m$fib, "kernel", 1L))
#Rllvm:::setCallingConv(m$simple, as(71L, "CallingConv"))
InitializeNVPTXTarget()
arch = "nvptx64"
tri <- getDefaultTargetTriple()
setTargetTriple(m, tri)
#Used to be trgt = lookupTarget("nvptx64")
trgt = lookupTarget(tri, arch)
# sm_20 is the CPU type and can be sm_20, sm_30 or a variety of others understood by the NVPTX backend
machine = createTargetMachine(trgt, tri, "sm_20")
# Now add the passes to generate the code.
pm = passManager(m, FALSE)
trgtLibInfo = targetLibraryInfo(tri)
addPass(pm, trgtLibInfo)
addAnalysisPasses(machine, pm)
dataLayout = getDataLayout(machine)
addPass(pm, dataLayout)
# We'll write the code to a string stream rather than a file
#out = .Call("R_new_raw_string_ostream", "")
#stream = rawFDOstream("/tmp/foo.ptx")
stream = rawStringOstream()
out = formattedRawOstream(stream)
if(addPassesToEmitFile(machine, pm, out, 0L))
stop("failed in addPassesToEmitFile. Is this type of file supported by the manager?")
run(pm, m)
# Garbage collect out so that the buffer is flushed
#rm(out); gc()
#.Call("R_flush_formatted_raw_ostream", out)
#flush(out)
code = as(stream, "character")
print(nchar(code))
#.Call("R_raw_ostream_close", stream, FALSE)
# cat("File size:", file.info("/tmp/foo.ptx")[1, "size"], "\n")