Permalink
Browse files

generate C++ code

  • Loading branch information...
1 parent a8c80a0 commit cc0735f4dafd77027991f1172fae5ffe4867c48e @duncantl committed Jun 23, 2013
Showing with 322 additions and 18 deletions.
  1. +1 −1 DESCRIPTION
  2. +9 −0 NAMESPACE
  3. +1 −1 Notes
  4. +12 −3 Paper/GNUmakefile
  5. +12 −3 Paper/Rllvm.tex
  6. +0 −1 Paper/jss.cls
  7. +0 −1 Paper/jsslogo.jpg
  8. +6 −1 R/PassManager.R
  9. +5 −0 R/classDefs.R
  10. +21 −0 R/ostream.R
  11. +12 −0 R/targetMachine.R
  12. +2 −0 Todo.xml
  13. +1 −1 experiments/fib.c
  14. +5 −4 experiments/struct.c
  15. +16 −0 experiments/structCall.c
  16. +6 −0 explorations/backend.R
  17. +40 −0 explorations/cpp.R
  18. +12 −1 explorations/nvptrx.Rdb
  19. +25 −0 explorations/ptx.R
  20. +122 −1 src/Target.cpp
  21. +14 −0 src/optimize.cpp
View
@@ -30,5 +30,5 @@ Collate: llvmVersion.R classDefs.R manual_generics.R BinaryOpEnums.R Function.R
IntrinsicEnums.R sexpTypes.R utils.R targets.R
simpleFunction.R PassManager.R llvm.R
declareFunction.R typeName.R
-
+ ostream.R targetMachine.R
View
@@ -126,6 +126,8 @@ ICMP_ULE,
ICMP_ULT,
ICmpInst.Predicate,
InitializeNativeTarget,
+InitializeNVPTXTarget,
+InitializeCppBackendTarget,
Int16Type,
Int1Type,
Int32PtrType,
@@ -356,3 +358,10 @@ export(getBlocks)
export(sapply)
exportMethods(sapply)
+
+exportClasses(raw_string_ostream, formatted_raw_ostream)
+
+
+export(addAnalysisPasses, addPassesToEmitFile)
+export(rawFDOstream, formattedRawOstream, stringRawOstream)
+
View
2 Notes
@@ -25,4 +25,4 @@ Activating Different Targets
A call instruction is represented as
arg1, arg2, ..., called routine
-This the order of the opernds.
+This the order of the operands.
View
@@ -1,10 +1,19 @@
Rllvm.pdf:
+JSS_DIR=$(HOME)/Projects/JSS/JSSstyle
+
Rllvm.pdf: Rllvm.tex rllvm.bib GNUmakefile jssMacros.tex
- -pdflatex $(<F)
- -pdflatex $(<F)
+ -$(run-tex)
+ -$(run-tex)
+# -pdflatex $(<F)
-bibtex Rllvm
- -pdflatex $(<F)
+ -$(run-tex)
+# -pdflatex $(<F)
clean:
-rm Rllvm.{aux,log,out,blg}
+
+
+define run-tex
+ (export TEXINPUTS=$(JSS_DIR):$(TEXINPUTS):: ; pdflatex $(<F))
+endef
View
@@ -36,7 +36,16 @@
%% an abstract and keywords
\Abstract{
-
+The \Rpkg{Rllvm} package is an \R{} interface to the Low-Level Virtual
+Machine (LLVM). This is a compiler toolkit for generating native code
+for different computational devices (e.g. CPUs and GPUs). These tools
+in \R{} allow us to explore compiling both \R{} code and also
+domain-specific languages (DSLs). They also allow us to exchange code
+with other systems using an intermediate representation of the native
+code. This paper describes the computational model and functionality
+provided by \Rpkg{Rllvm} and illustrates these with examples of
+low-level compilation. We leave discussion of more general
+strategies for compiling \R{} code to \Rpkg{RLLVMCompile}.
}
\Keywords{\R, \Rpkg{Rllvm} package, compiled code}
\Plainkeywords{R, RCIndex} %% without formatting
@@ -312,7 +321,7 @@ \section{Instructions}
add two real-valued numbers, assign/store
a value to a variable, load the value of a variable,
access an element in an array or via a pointer,
-compare two values (e.g. $<$, $>$, $ge$)
+compare two values (e.g. $<$, $>$, $\ge$)
branch to another block, cast a value to a different type, and so on.
Let's look at the \texttt{if.then} block in the
@@ -465,7 +474,7 @@ \subsection{IRBuilder}
% Constants
\subsection{Creating Instructions}
-
+There are many
\subsection{Branching}
View
View
View
@@ -18,4 +18,9 @@ function(module, execEngine = ExecutionEngine(module), level = 3L)
}
#setMethod("run", c("FunctionPassManager", "Function")
-
+
+
+setMethod("run", c("PassManager"),
+ function(.x, mod, ...) {
+ .Call("R_PassManager_run", .x, mod)
+ })
View
@@ -2,6 +2,11 @@ setClass("IntrinsicID", contains = "integer")
setClass("RC++Reference", representation(ref = "externalptr"))
+setClass("raw_ostream", contains = "RC++Reference")
+setClass("raw_fd_ostream", contains = "raw_ostream")
+setClass("raw_string_ostream", contains = "raw_ostream")
+setClass("formatted_raw_ostream", contains = "raw_ostream")
+
setClass("Module", contains = "RC++Reference")
setClass("IRBuilder", contains = "RC++Reference")
setClass("LLVMContext", contains = "RC++Reference")
View
@@ -0,0 +1,21 @@
+rawFDOstream =
+function(filename)
+{
+ f = path.expand(filename)
+ .Call("R_new_raw_fd_ostream", f)
+}
+
+formattedRawOstream =
+function(stream, delete = FALSE)
+{
+ if(is.character(stream))
+ stream = rawFDOstream(stream)
+
+ .Call("R_new_formatted_raw_ostream", stream, as.logical(delete))
+}
+
+stringRawOstream =
+function(value = "")
+{
+ .Call("R_new_raw_string_ostream", as.character(value))
+}
View
@@ -0,0 +1,12 @@
+addAnalysisPasses =
+function(machine, passManager)
+{
+ .Call("R_TargetMachine_addAnalysisPasses", machine, passManager)
+}
+
+addPassesToEmitFile =
+function(machine, passManager, outStream, fileType = 0L)
+{
+ .Call("R_TargetMachine_addPassesToEmitFile", machine, passManager, outStream, as.integer(fileType))
+}
+
View
@@ -39,6 +39,8 @@ LinkageType , CodeGenOpt, {VoidType, etc.} in typeDefs.R
<item>
See how to generate C++ from the IR, and also PTX.
+
+http://llvm.org/docs/NVPTXUsage.html
</item>
View
@@ -1,4 +1,4 @@
-int fib(int);
+//int fib(int);
int fib(int n) {
return n < 2 ? n : fib(n-1) + fib(n-2);
View
@@ -1,24 +1,25 @@
typedef struct {
+ char c;
int i;
double d;
double vals[10];
} Foo;
-Foo f;
+Foo g;
-int
+Foo
foo(int i, double d)
{
Foo f;
f.i = i;
f.d = d;
f.vals[0] = d;
- return(0);
+ return(f);
}
void
global()
{
- f.i = 7;
+ g.i = 7;
}
View
@@ -9,13 +9,29 @@ typedef struct {
double d;
} Tmp;
+typedef struct {
+ char c1;
+ char c2;
+ short s;
+ int i;
+ double d;
+} Bar;
+
+
void
foo(Tmp t)
{
t.i[7] = 1;
}
+Bar
+setBar(Bar b)
+{
+ b.c1 = 0;
+ return(b);
+}
+
int bar(CXCursor b)
{
View
@@ -0,0 +1,6 @@
+library(Rllvm)
+
+m = parseIR("fib.ll")
+
+showModule(m)
+
View
@@ -0,0 +1,40 @@
+library(Rllvm)
+
+if(FALSE) {
+ .C("R_InitializeAllTargetInfos")
+ m = Module("cpp")
+ createGlobalVariable("fooVar", m, val = createIntegerConstant(1L))
+ f = Function("foo", Int32Type, module = m)
+ b = Block(f)
+ ir = IRBuilder(b)
+ ir$createReturn(createIntegerConstant(1L))
+ verifyModule(m)
+} else
+ m = parseIR("experiments/fib.ll")
+
+############
+InitializeCppBackendTarget()
+tri = "cpp"
+
+setTargetTriple(m, tri)
+trgt = lookupTarget(tri)
+machine = createTargetMachine(trgt, tri, "")
+trgtLibInfo = targetLibraryInfo(tri)
+
+pm = passManager(m, FALSE)
+addPass(pm, trgtLibInfo)
+addAnalysisPasses(machine, pm)
+#dataLayout = getDataLayout(m)
+#addPass(pm, dataLayout)
+
+stream = rawFDOstream("/tmp/foo.cpp")
+out = formattedRawOstream(stream)
+if(addPassesToEmitFile(machine, pm, out, 0L))
+ stop("emitting this type of file is not supported by the manager")
+
+print(pm)
+print(m@ref)
+run(pm, m)
+.Call("R_raw_ostream_close", stream)
+
+
View
@@ -44,7 +44,8 @@ library(Rllvm)
m = Module("ptx")
.C("R_InitializeNVPTXTarget")
-tri = "nvptx64--"
+tri = "nvptx64"
+
setTargetTriple(m, tri)
getTargetTriple(m)
</r:code>
@@ -72,7 +73,16 @@ dataLayout = getDataLayout(machine)
Now create a pass manager object and add the data layout to it.
<r:code>
pm = passManager(m)
+addPass(pm, trgtLibInfo)
+.Call("R_PassManager_addAnalysisPasses", pm)
addPass(pm, dataLayout)
+
+
+out = .Call("R_new_raw_string_ostream", "")
+out = .Call("R_new_formatted_raw_ostream", out, FALSE)
+.Call("R_TargetMachine_addPassesToEmitFile", machine, pm, out, 1L)
+
+run(pm, m)
</r:code>
</para>
@@ -95,6 +105,7 @@ for run-time, e.g. exp, cosf, atan, alloc and delete.
<para>
Look at translate.cpp in libNVVM.
+CodeGen.cpp in Halide.
<r:code>
setDataLayout(module, layoutStr)
View
@@ -0,0 +1,25 @@
+library(Rllvm)
+m = Module("ptx")
+
+InitializeNVPTXTarget()
+tri = "nvptx64"
+
+setTargetTriple(m, tri)
+
+
+trgt = lookupTarget(tri)
+machine = createTargetMachine(trgt, tri, "sm_20")
+trgtLibInfo = targetLibraryInfo(tri)
+dataLayout = getDataLayout(machine)
+
+pm = passManager(m, FALSE)
+addPass(pm, trgtLibInfo)
+addAnalysisPasses(machine, pm)
+addPass(pm, dataLayout)
+
+
+#out = .Call("R_new_raw_string_ostream", "")
+out = formattedRawOstream("/tmp/foo.cpp")
+addPassesToEmitFile(machine, pm, out, 1L)
+
+run(pm, m)
Oops, something went wrong.

0 comments on commit cc0735f

Please sign in to comment.