Skip to content

Commit

Permalink
ptx code output from existing ll file working. Something funky going …
Browse files Browse the repository at this point in the history
…on with the create instructions
  • Loading branch information
duncantl committed Jul 8, 2013
1 parent 9b23e30 commit bf21564
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 93 deletions.
2 changes: 2 additions & 0 deletions Changelog
Expand Up @@ -8,6 +8,8 @@ Version 0.6-0

* isa() for Instruction classes.

* More support for raw and formatted ostreams

Version 0.5-0

* show() methods for Module, Function, Type to make the
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -389,4 +389,7 @@ export(InitializeAllTargets, InitializeAllTargetMCs, InitializeAllAsmPrinters, I

export(getDefaultTargetTriple)

S3method(flush, formatted_raw_ostream)



8 changes: 8 additions & 0 deletions R/ostream.R
@@ -1,4 +1,7 @@
rawFDOstream =
# add a finalizer?
# If so, have to ensure the container (e.g. formatted_raw_ostream)
# keeps a hold of it.
function(filename)
{
f = path.expand(filename)
Expand Down Expand Up @@ -29,3 +32,8 @@ function(value = "")
setAs("raw_string_ostream", "character",
function(from)
.Call("R_raw_string_ostream_str", from))


flush.formatted_raw_ostream =
function(con)
.Call("R_flush_formatted_raw_ostream", con)
2 changes: 1 addition & 1 deletion explorations/dnorm.Rdb
Expand Up @@ -249,7 +249,7 @@ in which each operand is also a complex expression.
</para>

<para>
Let's compute <r:expr>1/(sd * sqrt(2*pi))</r:expr>. This is a binary
Let's generate the code to compute <r:expr>1/(sd * sqrt(2*pi))</r:expr>. This is a binary
operation (/) with a constant (1) and another expression. We can
compute the denominator first since the numerator is a simple constant
value. We can replace <r:expr>sqrt(2*pi)</r:expr> with 2.506628275
Expand Down
28 changes: 16 additions & 12 deletions explorations/ptx.R
Expand Up @@ -5,28 +5,27 @@ library(Rllvm)
#InitializeAllAsmPrinters()
#InitializeAllAsmParsers()

.Call("R_initPassRegistry", NULL)
#.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")
m = parseIR("~/Projects/GPUs/Rnvvm/inst/sampleCode/simple-gpu64.ll")

# setMetadata(m, "nvvm.annotation", list(m$fib, "kernel", 1L))
#Rllvm:::setCallingConv(m$simple, as(71L, "CallingConv"))


InitializeNVPTXTarget()
tri = arch = "nvptx64"

tri = Rllvm:::getDefaultTargetTriple()
arch = "nvptx64"

tri <- getDefaultTargetTriple()
setTargetTriple(m, tri)


#trgt = lookupTarget(tri)
#Used to be trgt = lookupTarget("nvptx64")
trgt = lookupTarget(tri, arch)
machine = createTargetMachine(trgt, tri, "sm_20") # sm_20
# 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)

Expand All @@ -39,6 +38,8 @@ 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")

Expand All @@ -50,7 +51,10 @@ if(addPassesToEmitFile(machine, pm, out, 0L))

run(pm, m)

rm(out); gc()
# 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))

Expand Down
63 changes: 63 additions & 0 deletions explorations/ptx_direct.R
@@ -0,0 +1,63 @@
library(Rllvm)

m = Module("ptx kernel")
#fun = Function("kern", VoidType, module = m)
fun = simpleFunction("kern", VoidType, mod = m)

fun$ir$createLocalVariable(Int32Type, "i")
ir$createReturn()



setMetadata(m, "nvvm.annotation", list(m$kern, "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 = 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)

# 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")
80 changes: 0 additions & 80 deletions src/Target.cpp
Expand Up @@ -142,86 +142,6 @@ R_Target_getName(SEXP r_target)
}


extern "C"
SEXP
R_new_raw_string_ostream(SEXP r_str)
{
std::string *str;
if(TYPEOF(r_str) == STRSXP)
str = new std::string(CHAR(STRING_ELT(r_str, 0)));
else
str = (std::string *) getRReference(r_str);

llvm::raw_string_ostream *ans = new llvm::raw_string_ostream(*str);
if(!ans) {
PROBLEM "error creating string stream"
ERROR;
}
return(R_createRef(ans, "raw_string_ostream"));
}

extern "C"
SEXP
R_new_formatted_raw_ostream(SEXP r_stream, SEXP r_delete)
{

llvm::raw_ostream *stream = GET_REF(r_stream, raw_ostream);
llvm::formatted_raw_ostream *ans = new llvm::formatted_raw_ostream(*stream, LOGICAL(r_delete)[0]);
return(R_createRef(ans, "formatted_raw_ostream"));
}

void
R_formatted_raw_ostream_finalizer(SEXP obj)
{
llvm::formatted_raw_ostream *stream = (llvm::formatted_raw_ostream *) R_ExternalPtrAddr(obj);
delete stream;
}

extern "C"
SEXP
R_setFinalizer_formatted_raw_ostream(SEXP r_stream)
{
// llvm::formatted_raw_ostream *stream = GET_REF(r_stream, formatted_raw_ostream);
R_RegisterCFinalizer(r_stream, R_formatted_raw_ostream_finalizer);
return(R_NilValue);
}


extern "C"
SEXP
R_new_raw_fd_ostream(SEXP r_filename)
{
std::string err;
llvm::raw_fd_ostream *ans;
ans = new llvm::raw_fd_ostream(CHAR(STRING_ELT(r_filename, 0)), err);
if(!err.empty()) {
PROBLEM "%s", err.c_str()
ERROR;
}
return(R_createRef(ans, "raw_fd_ostream"));
}


extern "C"
SEXP
R_raw_string_ostream_str(SEXP r_stream)
{
llvm::raw_string_ostream *stream = GET_REF(r_stream, raw_string_ostream);
std::string str = stream->str();
return(mkString(str.c_str() ? str.c_str() : ""));
}


extern "C"
SEXP
R_raw_ostream_close(SEXP r_stream, SEXP r_delete)
{

llvm::raw_fd_ostream *stream = GET_REF(r_stream, raw_fd_ostream);
stream->close();
return(R_NilValue);
}



/* Show the registered targets. One has to initialize them first.
Expand Down
9 changes: 9 additions & 0 deletions src/ostream.cpp
Expand Up @@ -44,6 +44,15 @@ R_setFinalizer_formatted_raw_ostream(SEXP r_stream)
return(R_NilValue);
}

extern "C"
SEXP
R_flush_formatted_raw_ostream(SEXP r_stream)
{
llvm::formatted_raw_ostream *stream = GET_REF(r_stream, formatted_raw_ostream);
stream->flush();
return(R_NilValue);
}


extern "C"
SEXP
Expand Down

0 comments on commit bf21564

Please sign in to comment.