use of de.mirkosertic.bytecoder.ssa.TypeRef in project Bytecoder by mirkosertic.
the class JSSSAWriter method print.
private void print(TypeConversionExpression aValue) {
TypeRef theTargetType = aValue.resolveType();
Value theValue = aValue.incomingDataFlows().get(0);
switch(theTargetType.resolve()) {
case FLOAT:
print(theValue);
break;
case DOUBLE:
print(theValue);
break;
default:
print("Math.floor(");
print(theValue);
print(")");
break;
}
}
use of de.mirkosertic.bytecoder.ssa.TypeRef in project Bytecoder by mirkosertic.
the class WASMSSAWriter method writeTypeConversion.
private void writeTypeConversion(TypeConversionExpression aValue) {
TypeRef theTargetType = aValue.resolveType();
Value theSource = aValue.incomingDataFlows().get(0);
if (Objects.equals(theTargetType.resolve(), theSource.resolveType().resolve())) {
// No conversion needed!
writeValue(theSource);
return;
}
switch(theSource.resolveType().resolve()) {
case DOUBLE:
case FLOAT:
{
// Convert floating point to something else
switch(aValue.resolveType().resolve()) {
case DOUBLE:
case FLOAT:
{
// No conversion needed
writeValue(theSource);
return;
}
case INT:
case SHORT:
case BYTE:
case LONG:
case CHAR:
{
// Convert f32 to i32
print("(i32.trunc_s/f32 ");
writeValue(theSource);
print(")");
return;
}
default:
throw new IllegalStateException("target type " + aValue.resolveType() + " not supported!");
}
}
case INT:
case LONG:
case BYTE:
case SHORT:
case CHAR:
{
// Convert floating point to something else
switch(aValue.resolveType().resolve()) {
case DOUBLE:
case FLOAT:
{
// Convert i32 to f32
print("(f32.convert_s/i32 ");
writeValue(theSource);
print(")");
return;
}
case INT:
case SHORT:
case BYTE:
case LONG:
case CHAR:
{
// No conversion needed
writeValue(theSource);
return;
}
default:
throw new IllegalStateException("target type " + aValue.resolveType() + " not supported!");
}
}
default:
throw new IllegalStateException("Conversion of " + theSource.resolveType() + " not supported!");
}
}
use of de.mirkosertic.bytecoder.ssa.TypeRef in project Bytecoder by mirkosertic.
the class OpenCLWriter method printReloopedKernel.
public void printReloopedKernel(Program aProgram, Relooper.Block aBlock) {
print("__kernel void BytecoderKernel(");
List<OpenCLInputOutputs.KernelArgument> theArguments = inputOutputs.arguments();
for (int i = 0; i < theArguments.size(); i++) {
if (i > 0) {
print(", ");
}
OpenCLInputOutputs.KernelArgument theArgument = theArguments.get(i);
TypeRef theTypeRef = TypeRef.toType(theArgument.getField().getValue().getTypeRef());
switch(theArgument.getType()) {
case INPUT:
print("const ");
print(toType(theTypeRef));
print(" ");
print(theArgument.getField().getValue().getName().stringValue());
break;
case OUTPUT:
case INPUTOUTPUT:
print(toType(theTypeRef));
print(" ");
print(theArgument.getField().getValue().getName().stringValue());
break;
}
}
println(") {");
OpenCLWriter theDeeper = withDeeperIndent();
theDeeper.println("int $__label__ = 0;");
for (Variable theVariable : aProgram.getVariables()) {
if (!theVariable.isSynthetic()) {
theDeeper.print(toType(theVariable.resolveType(), false));
theDeeper.print(" ");
theDeeper.print(theVariable.getName());
theDeeper.println(";");
}
}
theDeeper.print(aBlock);
println("}");
}
use of de.mirkosertic.bytecoder.ssa.TypeRef in project Bytecoder by mirkosertic.
the class OpenCLWriter method printReloopedInline.
public void printReloopedInline(BytecodeMethod aMethod, Program aProgram, Relooper.Block aBlock) {
BytecodeMethodSignature theSignature = aMethod.getSignature();
print("__inline ");
print(toType(TypeRef.toType(theSignature.getReturnType())));
print(" ");
print(aMethod.getName().stringValue());
print("(");
boolean theFirst = true;
List<OpenCLInputOutputs.KernelArgument> theArguments = inputOutputs.arguments();
for (OpenCLInputOutputs.KernelArgument theArgument1 : theArguments) {
if (theFirst) {
theFirst = false;
} else {
print(", ");
}
TypeRef theTypeRef = TypeRef.toType(theArgument1.getField().getValue().getTypeRef());
switch(theArgument1.getType()) {
case INPUT:
print("const ");
print(toType(theTypeRef));
print(" ");
print(theArgument1.getField().getValue().getName().stringValue());
break;
case OUTPUT:
case INPUTOUTPUT:
print(toType(theTypeRef));
print(" ");
print(theArgument1.getField().getValue().getName().stringValue());
break;
}
}
List<Program.Argument> theProgramArguments = aProgram.getArguments();
for (int i = 1; i < theProgramArguments.size(); i++) {
Program.Argument theArgument = theProgramArguments.get(i);
if (theFirst) {
theFirst = false;
} else {
print(", ");
}
print(toType(theArgument.getVariable().resolveType()));
print(" ");
print(theArgument.getVariable().getName());
}
println(") {");
OpenCLWriter theDeeper = withDeeperIndent();
theDeeper.println("int $__label__ = 0;");
for (Variable theVariable : aProgram.getVariables()) {
if (!theVariable.isSynthetic()) {
theDeeper.print(toType(theVariable.resolveType(), false));
theDeeper.print(" ");
theDeeper.print(theVariable.getName());
theDeeper.println(";");
}
}
theDeeper.print(aBlock);
println("}");
}
use of de.mirkosertic.bytecoder.ssa.TypeRef in project Bytecoder by mirkosertic.
the class OpenCLContext method compute.
public void compute(int aNumberOfStreams, Kernel aKernel) {
Class theKernelClass = aKernel.getClass();
CachedKernel theCachedKernel = kernelFor(aKernel);
// Construct the input and output elements based on object properties
List<OpenCLInputOutputs.KernelArgument> theArguments = theCachedKernel.inputOutputs.arguments();
cl_mem[] theMemObjects = new cl_mem[theArguments.size()];
Map<Integer, DataRef> theOutputs = new HashMap<>();
try {
for (int i = 0; i < theArguments.size(); i++) {
OpenCLInputOutputs.KernelArgument theArgument = theArguments.get(i);
TypeRef theFieldType = TypeRef.toType(theArgument.getField().getValue().getTypeRef());
DataRef theDataRef;
if (theFieldType.isArray()) {
TypeRef.ArrayTypeRef theArrayTypeRef = (TypeRef.ArrayTypeRef) theFieldType;
TypeRef theArrayElement = TypeRef.toType(theArrayTypeRef.arrayType().getType());
switch(theArrayElement.resolve()) {
case INT:
{
Field theField = theKernelClass.getDeclaredField(theArgument.getField().getValue().getName().stringValue());
theField.setAccessible(true);
int[] theData = (int[]) theField.get(aKernel);
theDataRef = new DataRef(Pointer.to(theData), Sizeof.cl_int * theData.length);
break;
}
case FLOAT:
{
Field theField = theKernelClass.getDeclaredField(theArgument.getField().getValue().getName().stringValue());
theField.setAccessible(true);
float[] theData = (float[]) theField.get(aKernel);
theDataRef = new DataRef(Pointer.to(theData), Sizeof.cl_float * theData.length);
break;
}
case REFERENCE:
{
Field theField = theKernelClass.getDeclaredField(theArgument.getField().getValue().getName().stringValue());
theField.setAccessible(true);
Object[] theData = (Object[]) theField.get(aKernel);
Class theObjectType = theData.getClass().getComponentType();
theDataRef = toDataRef(theData, theObjectType);
break;
}
default:
throw new IllegalArgumentException("Not supported array element type " + theArrayElement.resolve() + " for kernel argument " + theArgument.getField().getValue().getName());
}
} else {
throw new IllegalArgumentException("Type " + theFieldType + " is not supported for kernel argument " + theArgument.getField().getValue().getName().stringValue());
}
switch(theArgument.getType()) {
case INPUT:
/* theMemObjects[i] = clCreateBuffer(context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
theDataRef.size * aNumberOfStreams, theDataRef.pointer, null);
break;*/
case OUTPUT:
case INPUTOUTPUT:
theMemObjects[i] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, theDataRef.size, theDataRef.pointer, null);
theOutputs.put(i, theDataRef);
break;
}
clSetKernelArg(theCachedKernel.kernel, i, Sizeof.cl_mem, Pointer.to(theMemObjects[i]));
}
} catch (Exception e) {
throw new RuntimeException("Error extracting kernel parameter", e);
}
// Set the work-item dimensions
long[] global_work_size = new long[] { aNumberOfStreams };
// Let the driver guess the optimal size
// new long[] {32};
long[] local_work_size = null;
// Execute the kernel
clEnqueueNDRangeKernel(commandQueue, theCachedKernel.kernel, 1, null, global_work_size, local_work_size, 0, null, null);
// Wait till everything is done
clFinish(commandQueue);
// Read the output data
for (Map.Entry<Integer, DataRef> theEntry : theOutputs.entrySet()) {
DataRef theDataRef = theEntry.getValue();
clEnqueueReadBuffer(commandQueue, theMemObjects[theEntry.getKey()], CL_TRUE, 0, theDataRef.size, theDataRef.pointer, 0, null, null);
theDataRef.updateFromBuffer();
}
// Release memory
for (cl_mem theMem : theMemObjects) {
clReleaseMemObject(theMem);
}
}
Aggregations