Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

several additions to Rllvm #7

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ Collate: llvmVersion.R classDefs.R manual_generics.R BinaryOpEnums.R
z_enumDefs_3.4.R
z_enumDefs_3.5.R
z_enumDefs_3.6.R
Function.R
Function.R
DIBuilder.R
19 changes: 16 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ exportClasses(
SExtInst,
StoreInst,
StructType,
functionType,
TerminatorInst,
Type,
UnaryInstruction,
Expand Down Expand Up @@ -114,6 +113,7 @@ FCMP_ULT,
FCMP_UNE,
FCMP_UNO,
FDiv,
finalizeEngine,
FIRST_FCMP_PREDICATE,
FIRST_ICMP_PREDICATE,
FloatPtrType,
Expand Down Expand Up @@ -186,7 +186,8 @@ exportMethods(Optimize)

export(setLinkage)

export(findFunction, getPointerToFunction)
export(findFunction, getPointerToFunction,
getNativePointerToFunction)

export(createDoubleConstant)

Expand Down Expand Up @@ -303,7 +304,7 @@ export(getIntegerType)
export(setAlignment)
exportMethods(setAlignment)

#export(getFunctionType)
export(functionType)

export(addModule)

Expand Down Expand Up @@ -470,3 +471,15 @@ export(insertAtEnd)
export(isLandingPad, getLandingPadInst)

export(createPHI,createPhi)

export(PHIAddIncoming)

export(DIBuilder,
finalizeDIBuilder,
newDebugCU,
newDebugFunction,
newDebugLocalVariable,
newDebugBasicType,
newDebugFunctionType,
newDebugPointerType,
debugSetLocation)
54 changes: 54 additions & 0 deletions R/DIBuilder.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
DIBuilder =
function(module)
{
.Call("R_new_DIBuilder", module)
}

finalizeDIBuilder =
function(builder)
{
invisible(.Call("R_finalize_DIBuilder", builder))
}

newDebugCU =
function(builder, filename, path)
{
.Call("R_new_DIBuilder_CU",builder, filename, path)
}

newDebugFunction =
function(builder, cu, func, type, line)
{
.Call("R_new_DIBuilder_Function",builder, cu, func, type, line)
}

newDebugLocalVariable =
function(builder, ir_builder, sp, var, lineNo, cu, di_type, idx)
{
.Call("R_DIBuilder_CreateLocalVariable",builder, ir_builder, sp, var, lineNo, cu, di_type, idx)
}

newDebugBasicType =
function(builder, name, size, align, dwarf_type) {
.Call("R_IRBuilder_CreateBasicType", builder, name, as.integer(size), as.integer(align), as.integer(dwarf_type))
}

newDebugFunctionType =
function(builder, args, cu)
{
.Call("R_IRBuilder_CreateFunctionType", builder, as.vector(args), cu)
}

newDebugPointerType =
function(builder,baseType, name) {
.Call("R_IRBuilder_CreatePointerType", builder, baseType, name)
}

debugSetLocation =
function(builder, func, lineNo, colNo)
{
.Call("R_IRBuilder_SetLocation", builder, func, as.integer(lineNo), as.integer(colNo))
}



58 changes: 49 additions & 9 deletions R/ExecutionEngine.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,42 @@ function(val, values)
}

ExecutionEngine =
function(module, optimizationLevel = CodeGenOpt_Default)
function(module, optimizationLevel = CodeGenOpt_Default, useMCJIT)
{
optimizationLevel = matchEnum(optimizationLevel, CodeGenOptEnum)

if (missing(useMCJIT)) {
if(all(llvmVersion() >= c(3, 6))) {
#defaulting to mc jit on llvm 3.6
useMCJIT=TRUE
} else {
useMCJIT=FALSE
}
}

.Call("R_create_ExecutionEngine", as(module, "Module"), as.integer(optimizationLevel))
res=.Call("R_create_ExecutionEngine", as(module, "Module"), as.integer(optimizationLevel),as.logical(useMCJIT))

res@useMCJIT=useMCJIT
res@finalized=FALSE

res
}

#finalize must be called before invoking code that has been
#compiled using MC Jit
finalizeEngine =
function(engine)
{
if(!is(engine, "ExecutionEngine"))
stop("can only finalize an ExecutionEngine")

if(engine@finalized)
stop("engine has already been finalized")

invisible(.Call("R_ExecutionEngine_finalize", engine))
}


addModule =
function(engine, ...)
{
Expand Down Expand Up @@ -71,15 +100,20 @@ function(.x, ..., .args = list(...), .ee = ExecutionEngine(as(.x, "Module")), .a
if(!is(.x, "Function"))
stop("argument to .llvm must be a Function")

# If an argument is a Function, we probably want to treat it as a function pointer and so want
# its address which can be obtained via getPointerToFunction() with the exec engine also.
# .args = lapply(.args, function(x) if(is(x, "Function")) getPointerToFunction(x, .ee)@ref else x)
if (.ee@useMCJIT) {
stop("calling .llvm/runFunction is not supported with MC-JIT")
} else {

if(length(.duplicate))
.args[.duplicate] = lapply(.args[.duplicate], function(x) .Call('Rf_duplicate', x))
# If an argument is a Function, we probably want to treat it as a function pointer and so want
# its address which can be obtained via getPointerToFunction() with the exec engine also.
# .args = lapply(.args, function(x) if(is(x, "Function")) getPointerToFunction(x, .ee)@ref else x)


ans = .Call("R_callFunction", .x, .args, .ee)
if(length(.duplicate))
.args[.duplicate] = lapply(.args[.duplicate], function(x) .Call('Rf_duplicate', x))


ans = .Call("R_callFunction", .x, .args, .ee)
}

if(.all)
append(ans, structure(.args, names = names(.args)))
Expand Down Expand Up @@ -123,6 +157,12 @@ function(fun, execEngine)
.Call("R_ExecutionEngine_getPointerToFunction", execEngine, fun)
}

getNativePointerToFunction =
function(fun, execEngine)
{
.Call("R_ExecutionEngine_getNativePointerToFunction", execEngine, fun)
}

getPointerToGlobal =
function(var, execEngine)
{
Expand Down
10 changes: 4 additions & 6 deletions R/IRBuilder.R
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,13 @@ function(builder, vec, elt, idx)
.Call("R_IRBuilder_CreateInsertElement", builder, vec, elt, idx)
}


createPHI =
function(builder, type, numReservedVals)
PHIAddIncoming =
function(phiNode, value, block)
{
.Call("R_IRBuilder_CreatePHI", builder, type, as.integer(numReservedVals))
.Call("R_PHINode_addIncoming", phiNode, value, block)
}



if(FALSE) {
# Old version < 3.4
CastOps = structure(33:44, .Names = c("Trunc", "ZExt", "SExt", "FPToUI",
Expand Down Expand Up @@ -511,7 +509,7 @@ function(builder, value, type, id = "")


createPhi = createPHI =
function(build, type, numReservedValues, id = character())
function(builder, type, numReservedValues, id = character())
{
.Call("R_IRBuilder_CreatePHI", as(builder, "IRBuilder"), as(type, "Type"), as.integer(numReservedValues), as.character(id))
}
9 changes: 8 additions & 1 deletion R/classDefs.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ setClass("formatted_raw_ostream", contains = "raw_ostream")
setClass("Module", contains = "RC++Reference")
setClass("IRBuilder", contains = "RC++Reference")
setClass("LLVMContext", contains = "RC++Reference")
setClass("ExecutionEngine", contains = "RC++Reference")
setClass("ExecutionEngine", representation(useMCJIT = "logical", finalized = "logical"), contains = "RC++Reference")
setClass("Value", contains = "RC++Reference")
setClass("BasicBlock", contains = "Value")

Expand Down Expand Up @@ -92,6 +92,13 @@ setClass("NamedMDNode", contains = "RC++Reference")
setClass("MDNode", contains = "Value")
setClass("MDString", contains = "Value")

#Classes for DIBuilder
setClass("DIBuilder", contains = "RC++Reference")
setClass("DICompileUnit", contains = "RC++Reference")
setClass("DIDescriptor", contains = "RC++Reference")
setClass("DISubprogram", contains = "RC++Reference")
setClass("DIType", contains = "RC++Reference")
setClass("DICompositeType", contains = "DIType")


setClass("Type", contains = "RC++Reference")
Expand Down
11 changes: 8 additions & 3 deletions R/onLoad.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
getTypeDefs =
function()
function(namespace)
{
types = .Call("R_getTypeDefinitions")
typeClass= getClass("Type", .Force=TRUE, where=namespace)
types = .Call("R_getTypeDefinitions", typeClass)

names(types) = c("Void", "Label", "Float", "Double", "Int1", "Int8", "Int16",
"Int32", "Int64", "FloatPtr", "DoublePtr", "Int32Ptr", "String")
types
Expand All @@ -10,8 +12,11 @@ function()
.onLoad =
function(...)
{
types = getTypeDefs()

e = getNamespace("Rllvm")

types = getTypeDefs(e)

mapply(utils::assignInNamespace,
paste(names(types), "Type", sep = ""),
types,
Expand Down
102 changes: 102 additions & 0 deletions experiments/DIBuilder.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# The following script illuminates how to add Debug Information to LLVM Code
# the following function is used to show linenumbers in gdb

fun2 = function(x) {
res=x*2
return(res)
}

# dont move the uper function in the file, because the line numbers are encoded in the debug data!!
#set the path to the script here, otherwise gdb cant display source lines

absPath="/home/chris/dev/llvm/Rllvm/experiments/DIBuilder.R"

#if gdb doesnt stop in the function, check if you can set a breakpoint in __jit_debug_register_code
#see https://sourceware.org/gdb/onlinedocs/gdb/JIT-Interface.html and http://llvm.org/docs/DebuggingJITedCode.html


#end of preambel, start of code

library(Rllvm)

cat (paste("Is this script really saved in: ", absPath))
cat (paste("attach gdb to process ", Sys.getpid(), " and set breakpoint on fun2"))
cat("then come back here, enter a character and press return!")
line <- scan("stdin", character(), n=1)

m = Module()

# Create a simple routines that takes a double value and returns that value multiplied by 2.

f2 = Function("fun2", VoidType, list(x = pointerType(DoubleType)), module = m)
b = Block(f2)
ir = IRBuilder(b)
param_x = getParameters(f2)$x
var_res=createLocalVariable(ir,DoubleType, "res")

#creating the Debug Builder and tools
debugBuilder = DIBuilder(m)




#generate CompilationUnit
debugCompUnit = newDebugCU(debugBuilder, basename(absPath), dirname(absPath))

#Type
debugDouble=newDebugBasicType(debugBuilder, "double", 64, 64, 4 ) #4 = dwarf::DW_ATE_float
debugVoid=newDebugBasicType(debugBuilder, "void", 64, 64, 5 ) #5 = dwarf::DW_ATE_signed
debugDoublePtr=newDebugPointerType(debugBuilder, debugDouble, "double*")


#Function for Debug
debugSignature=c(debugVoid,list(x=debugDoublePtr))
debugFunSignature=newDebugFunctionType(debugBuilder, debugSignature, debugCompUnit)
debugFun=newDebugFunction(debugBuilder, debugCompUnit, f2, debugFunSignature, absPath)

#adding function parameters
newDebugLocalVariable(debugBuilder, ir, debugFun, param_x, absPath, debugCompUnit, debugDoublePtr, 1)

#adding local variable res
newDebugLocalVariable(debugBuilder, ir, debugFun, var_res, absPath, debugCompUnit, debugDouble, 0)



#
# START emitting IR
#
#emit first source location
debugSetLocation(ir, debugFun, 5, 1)

x2= ir$createLoad(param_x)
two = createConstant(, 2, context = getContext(m))
res=ir$binOp(FMul, x2, two)
ir$createStore(res,var_res)

#emit second source location
debugSetLocation(ir, debugFun, 6, 1)
ir$createStore(res,param_x)
ir$createReturn()




#
# FINISH Emitting IR
# now cleaning up and executing
#

finalizeDIBuilder(debugBuilder)


# Now we create the EEs
eeNew = ExecutionEngine(m, useMCJIT=TRUE)


# getNativePointerToFunction returns a pointer that can be used by .C et al
fnPtr = getNativePointerToFunction(f2, eeNew)

# the MC JIT must be finalized before callin into the code
finalizeEngine(eeNew)
print(.C(fnPtr,10.4))

Loading