Skip to content

Commit

Permalink
generating ptx code
Browse files Browse the repository at this point in the history
  • Loading branch information
duncantl committed Jul 8, 2013
1 parent f0e3a2b commit 34ff1dc
Show file tree
Hide file tree
Showing 26 changed files with 366 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Expand Up @@ -25,4 +25,6 @@ a.out.dSYM/.*
\.tu
explorations/.*\.png
explorations/globalString
src/Rllc.cpp
src/llc.cpp

2 changes: 2 additions & 0 deletions Changelog
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions DESCRIPTION
Expand Up @@ -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
12 changes: 12 additions & 0 deletions NAMESPACE
Expand Up @@ -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)


2 changes: 1 addition & 1 deletion Paper/OtherFuncs
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions R/Function.R
Expand Up @@ -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")
1 change: 1 addition & 0 deletions R/classDefs.R
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions R/classof.R
@@ -0,0 +1,6 @@
isa =
function(obj, classname = class(obj))
{
.Call("R_Instruction_classof", as(obj, "Instruction"), as.character(classname))
}

16 changes: 16 additions & 0 deletions R/initialize.R
@@ -0,0 +1,16 @@
InitializeAllTargets =
function()
.Call("R_InitializeAllTargets")

InitializeAllTargetMCs =
function()
.Call("R_InitializeAllTargetMCs")

InitializeAllAsmPrinters =
function()
.Call("R_InitializeAllAsmPrinters")

InitializeAllAsmParsers =
function()
.Call("R_InitializeAllAsmParsers")

9 changes: 8 additions & 1 deletion R/module.R
Expand Up @@ -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",
Expand Down
14 changes: 12 additions & 2 deletions R/ostream.R
Expand Up @@ -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))
4 changes: 2 additions & 2 deletions R/targets.R.in
Expand Up @@ -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))
}


Expand Down
12 changes: 12 additions & 0 deletions R/utils.R
Expand Up @@ -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]]
}
2 changes: 1 addition & 1 deletion explorations/cpp.R
Expand Up @@ -10,7 +10,7 @@ if(FALSE) {
ir$createReturn(createIntegerConstant(1L))
verifyModule(m)
} else
m = parseIR("experiments/fib.ll")
m = parseIR("../experiments/fib.ll")

############
InitializeCppBackendTarget()
Expand Down
49 changes: 41 additions & 8 deletions explorations/ptx.R
@@ -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")
9 changes: 8 additions & 1 deletion inst/TU/clang.R
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions inst/TU/llvm.c
Expand Up @@ -43,6 +43,7 @@
#include <llvm/Assembly/PrintModulePass.h>

#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/CallingConv.h>

#include <algorithm>

Expand Down
6 changes: 0 additions & 6 deletions src/ExecEngine.cpp
Expand Up @@ -51,12 +51,6 @@ R_InitializeCppBackendTarget()
}
#endif

extern "C"
void
R_InitializeAllTargetInfos()
{
llvm::InitializeAllTargetInfos();
}

extern "C"
SEXP
Expand Down
2 changes: 1 addition & 1 deletion src/Function.cpp
Expand Up @@ -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] );


Expand Down
8 changes: 4 additions & 4 deletions src/Instruction.cpp
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down
42 changes: 42 additions & 0 deletions src/Pass.cpp
@@ -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);
}








0 comments on commit 34ff1dc

Please sign in to comment.