Permalink
Browse files

generating ptx code

  • Loading branch information...
1 parent f0e3a2b commit 34ff1dc6b790246f60f2f5c9061ced521c0327d6 @duncantl committed Jul 8, 2013
Showing with 366 additions and 36 deletions.
  1. +2 −0 .Rbuildignore
  2. +2 −0 Changelog
  3. +1 −0 DESCRIPTION
  4. +12 −0 NAMESPACE
  5. +1 −1 Paper/OtherFuncs
  6. +9 −0 R/Function.R
  7. +1 −0 R/classDefs.R
  8. +6 −0 R/classof.R
  9. +16 −0 R/initialize.R
  10. +8 −1 R/module.R
  11. +12 −2 R/ostream.R
  12. +2 −2 R/targets.R.in
  13. +12 −0 R/utils.R
  14. +1 −1 explorations/cpp.R
  15. +41 −8 explorations/ptx.R
  16. +8 −1 inst/TU/clang.R
  17. +1 −0 inst/TU/llvm.c
  18. +0 −6 src/ExecEngine.cpp
  19. +1 −1 src/Function.cpp
  20. +4 −4 src/Instruction.cpp
  21. +42 −0 src/Pass.cpp
  22. +77 −4 src/Target.cpp
  23. +88 −0 src/auto_classof.h
  24. +16 −0 src/classof.cpp
  25. +0 −3 src/optimize.cpp
  26. +3 −2 tests/moveInstructions.R
View
@@ -25,4 +25,6 @@ a.out.dSYM/.*
\.tu
explorations/.*\.png
explorations/globalString
+src/Rllc.cpp
+src/llc.cpp
View
@@ -6,6 +6,8 @@ Version 0.6-0
* insertBefore(), insertAfter(), moveBefore()
+ * isa() for Instruction classes.
+
Version 0.5-0
* show() methods for Module, Function, Type to make the
View
@@ -33,3 +33,4 @@ Collate: llvmVersion.R classDefs.R manual_generics.R BinaryOpEnums.R Function.R
ostream.R targetMachine.R
metadata.R
opCodeClassMap.R
+ classof.R initialize.R
View
@@ -378,3 +378,15 @@ export(LLVMAttributes)
export(insertBefore, insertAfter, moveBefore)
+export(isa)
+
+export(setCallingConv, getCallingConv)
+export(InitializeAllTargets, InitializeAllTargetMCs, InitializeAllAsmPrinters, InitializeAllAsmParsers)
+
+
+#Temporary
+#export(llc)
+
+export(getDefaultTargetTriple)
+
+
View
@@ -21,7 +21,7 @@ Context
% Mention in exporting/importing IR code with other languages.
I/O of code
-{write,read}BitCode, parseIR, parseAssembly
+[done] {write,read}BitCode, parseIR, parseAssembly
Pass manager
getPassManager
View
@@ -228,3 +228,12 @@ function(vals)
+setClass("CallingConv", contains = "integer")
+
+setCallingConv =
+function(fun, conv)
+ .Call("R_Function_setCallingConv", as(fun, "Function"), as(conv, "CallingConv"))
+
+getCallingConv =
+function(fun)
+ as(.Call("R_Function_setCallingConv", as(fun, "Function")), "CallingConv")
View
@@ -173,6 +173,7 @@ setClass("TargetMachine", contains = "RC++Reference")
setClass("TargetLibraryInfo", contains = "RC++Reference")
+setClass("PassRegistry", contains = "RC++Reference")
setClass("Pass", contains = "RC++Reference")
setClass("ImmutablePass", contains = "Pass")
setClass("DataLayout", contains = "ImmutablePass")
View
@@ -0,0 +1,6 @@
+isa =
+function(obj, classname = class(obj))
+{
+ .Call("R_Instruction_classof", as(obj, "Instruction"), as.character(classname))
+}
+
View
@@ -0,0 +1,16 @@
+InitializeAllTargets =
+ function()
+ .Call("R_InitializeAllTargets")
+
+InitializeAllTargetMCs =
+ function()
+ .Call("R_InitializeAllTargetMCs")
+
+InitializeAllAsmPrinters =
+ function()
+ .Call("R_InitializeAllAsmPrinters")
+
+InitializeAllAsmParsers =
+ function()
+ .Call("R_InitializeAllAsmParsers")
+
View
@@ -241,7 +241,14 @@ setTriple = setTargetTriple =
function(m, str)
{
.Call("R_Module_setTargetTriple", m, as.character(str))
-}
+}
+
+
+getDefaultTargetTriple =
+function()
+{
+ .Call("R_getDefaultTargetTriple")
+}
setMethod("getDataLayout", "Module",
View
@@ -6,16 +6,26 @@ function(filename)
}
formattedRawOstream =
-function(stream, delete = FALSE)
+function(stream, delete = FALSE, finalize = TRUE)
{
if(is.character(stream))
stream = rawFDOstream(stream)
- .Call("R_new_formatted_raw_ostream", stream, as.logical(delete))
+ ans = .Call("R_new_formatted_raw_ostream", stream, as.logical(delete))
+
+ if(finalize)
+ # could allow the caller to specify their own routine for the finalizer.
+ .Call("R_setFinalizer_formatted_raw_ostream", ans@ref)
+
+ ans
}
stringRawOstream =
function(value = "")
{
.Call("R_new_raw_string_ostream", as.character(value))
}
+
+setAs("raw_string_ostream", "character",
+ function(from)
+ .Call("R_raw_string_ostream_str", from))
View
@@ -43,9 +43,9 @@ function(target, triple, cpu, features = "", options = list())
}
lookupTarget =
-function(triple)
+function(triple, arch = character())
{
- .Call("R_TargetRegistry_lookupTarget", as.character(triple))
+ .Call("R_TargetRegistry_lookupTarget", as.character(triple), as.character(arch))
}
View
@@ -26,3 +26,15 @@ function(msg, class)
structure(list(msg = paste(msg, collapse = " ")), class = c(class, c("simpleError", "error", "condition")))
}
+
+
+
+llc =
+function(file, args, march = "nvptx64", out = NA)
+{
+ # "/Users/duncan/Projects/GPUs/Rnvvm/inst/sampleCode/simple-gpu64.ll",
+ args = c("Rllc", args, paste0("-march=", march), path.expand(file))
+ if(!is.na(out))
+ args = c(args, "-o", out)
+ .C("R_llc", length(args), args)[[1]]
+}
View
@@ -10,7 +10,7 @@ if(FALSE) {
ir$createReturn(createIntegerConstant(1L))
verifyModule(m)
} else
- m = parseIR("experiments/fib.ll")
+ m = parseIR("../experiments/fib.ll")
############
InitializeCppBackendTarget()
View
@@ -1,25 +1,58 @@
library(Rllvm)
-m = parseIR("experiments/fib.ll")
+
+#InitializeAllTargets()
+#InitializeAllTargetMCs()
+#InitializeAllAsmPrinters()
+#InitializeAllAsmParsers()
+
+.Call("R_initPassRegistry", NULL)
+
+if(FALSE) {
+ m = parseIR("../experiments/fib.ll")
+ setMetadata(m, "nvvm.annotation", list(m$fib, "kernel", 1L))
+} else
+ m = parseIR("~/Projects/GPUs/Rnvvm/inst/sampleCode/simple-gpu64.ll")
+
+#Rllvm:::setCallingConv(m$simple, as(71L, "CallingConv"))
+
InitializeNVPTXTarget()
-tri = "nvptx64"
+tri = arch = "nvptx64"
+
+tri = Rllvm:::getDefaultTargetTriple()
setTargetTriple(m, tri)
-trgt = lookupTarget(tri)
-machine = createTargetMachine(trgt, tri, "sm_20")
-trgtLibInfo = targetLibraryInfo(tri)
-dataLayout = getDataLayout(machine)
+#trgt = lookupTarget(tri)
+trgt = lookupTarget(tri, arch)
+machine = createTargetMachine(trgt, tri, "sm_20") # sm_20
pm = passManager(m, FALSE)
+
+trgtLibInfo = targetLibraryInfo(tri)
addPass(pm, trgtLibInfo)
+
addAnalysisPasses(machine, pm)
+
+dataLayout = getDataLayout(machine)
addPass(pm, dataLayout)
#out = .Call("R_new_raw_string_ostream", "")
-out = formattedRawOstream("/tmp/foo.ptx")
-addPassesToEmitFile(machine, pm, out, 0L)
+#stream = rawFDOstream("/tmp/foo.ptx")
+
+stream = stringRawOstream()
+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)
+
+rm(out); gc()
+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")
View
@@ -46,11 +46,18 @@ baseClass = sapply(insClass, function(x) gsub("class ", "", sapply(x@superClass
instClasses = getAllDescendantClasses(insClass, , baseClass, namespace = "llvm")
-code = c("CHAR(STRING_ELT"
+insClass = insClass[ gsub("llvm::", "", instClasses) ]
+
+#code = c("CHAR(STRING_ELT"
sprintf('if(strcmp(targetClass, "%s") == 0)\n\tans = static_cast<%s*>(ptr);', gsub("llvm::", "", instClasses), instClasses)
+classof = sprintf('%s if(strcmp(targetClass, "%s") == 0)\n\tans = %s::classof(val);',
+ c("", rep("else", length(instClasses) -1)), gsub("llvm::", "", instClasses), instClasses)
+
+cat(classof, sep = "\n", file = "../../src/auto_classof.h")
+
###########
methodNames = setdiff(unique(unlist(sapply(insClass, function(x) names(x@methods)))), names(insClass))
View
@@ -43,6 +43,7 @@
#include <llvm/Assembly/PrintModulePass.h>
#include <llvm/IR/IRBuilder.h>
+#include <llvm/IR/CallingConv.h>
#include <algorithm>
View
@@ -51,12 +51,6 @@ R_InitializeCppBackendTarget()
}
#endif
-extern "C"
-void
-R_InitializeAllTargetInfos()
-{
- llvm::InitializeAllTargetInfos();
-}
extern "C"
SEXP
View
@@ -178,7 +178,7 @@ R_Argument_setAttrs(llvm::Argument *arg, SEXP r_vals)
{
/* now have the parameter, so set the values. */
llvm::AttrBuilder builder;
- for(int i = 0 ; i < (unsigned) Rf_length(r_vals); i++)
+ for(unsigned i = 0 ; i < (unsigned) Rf_length(r_vals); i++)
builder.addAttribute( (AttrKind) INTEGER(r_vals)[i] );
View
@@ -152,9 +152,9 @@ R_Instruction_getOperand(SEXP r_inst, SEXP r_i)
llvm::Instruction *inst = GET_REF(r_inst, Instruction);
if(!inst) return(R_NilValue);
llvm::Value *el;
- int i = INTEGER(r_i)[0] - 1;
- if(i >= inst->getNumOperands()) {
- PROBLEM "index of operand too large"
+ unsigned i = INTEGER(r_i)[0] - 1;
+ if(i < 0 || i >= inst->getNumOperands()) {
+ PROBLEM "index of operand is incorrect"
ERROR;
}
@@ -210,7 +210,7 @@ R_Instruction_moveBefore(SEXP r_base, SEXP r_inst)
{
llvm::Instruction *base = GET_REF(r_base, Instruction);
llvm::Instruction *inst = GET_REF(r_inst, Instruction);
-fprintf(stderr, "moveBefore: %p, %p\n", base, inst);
+
base->moveBefore(inst);
return(R_NilValue);
}
View
@@ -0,0 +1,42 @@
+#include "Rllvm.h"
+
+extern "C"
+SEXP
+R_getPassRegistry()
+{
+ llvm::PassRegistry *reg = llvm::PassRegistry::getPassRegistry();
+ return(R_createRef(reg, "PassRegistry"));
+}
+
+
+extern "C"
+SEXP
+R_initPassRegistry(SEXP r_registry)
+{
+ llvm::PassRegistry *Registry;
+
+ if(Rf_length(r_registry))
+ Registry = GET_REF(r_registry, PassRegistry);
+ else
+ Registry = llvm::PassRegistry::getPassRegistry();
+
+ if(!Registry) {
+ PROBLEM "no registry"
+ ERROR;
+ }
+ llvm::initializeCore(*Registry);
+ llvm::initializeCodeGen(*Registry);
+ llvm::initializeLoopStrengthReducePass(*Registry);
+ llvm::initializeLowerIntrinsicsPass(*Registry);
+ llvm::initializeUnreachableBlockElimPass(*Registry);
+
+ return(R_NilValue);
+}
+
+
+
+
+
+
+
+
Oops, something went wrong.

0 comments on commit 34ff1dc

Please sign in to comment.