Search in sources :

Example 1 with TypeRef

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;
    }
}
Also used : BytecodeTypeRef(de.mirkosertic.bytecoder.core.BytecodeTypeRef) TypeRef(de.mirkosertic.bytecoder.ssa.TypeRef) BytecodeObjectTypeRef(de.mirkosertic.bytecoder.core.BytecodeObjectTypeRef) StringValue(de.mirkosertic.bytecoder.ssa.StringValue) ByteValue(de.mirkosertic.bytecoder.ssa.ByteValue) Value(de.mirkosertic.bytecoder.ssa.Value) ClassReferenceValue(de.mirkosertic.bytecoder.ssa.ClassReferenceValue) SelfReferenceParameterValue(de.mirkosertic.bytecoder.ssa.SelfReferenceParameterValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) NullValue(de.mirkosertic.bytecoder.ssa.NullValue) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) MethodParameterValue(de.mirkosertic.bytecoder.ssa.MethodParameterValue) ShortValue(de.mirkosertic.bytecoder.ssa.ShortValue)

Example 2 with TypeRef

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!");
    }
}
Also used : BytecodePrimitiveTypeRef(de.mirkosertic.bytecoder.core.BytecodePrimitiveTypeRef) BytecodeTypeRef(de.mirkosertic.bytecoder.core.BytecodeTypeRef) TypeRef(de.mirkosertic.bytecoder.ssa.TypeRef) BytecodeObjectTypeRef(de.mirkosertic.bytecoder.core.BytecodeObjectTypeRef) StringValue(de.mirkosertic.bytecoder.ssa.StringValue) ByteValue(de.mirkosertic.bytecoder.ssa.ByteValue) Value(de.mirkosertic.bytecoder.ssa.Value) ClassReferenceValue(de.mirkosertic.bytecoder.ssa.ClassReferenceValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) NullValue(de.mirkosertic.bytecoder.ssa.NullValue) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) ShortValue(de.mirkosertic.bytecoder.ssa.ShortValue)

Example 3 with TypeRef

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("}");
}
Also used : Variable(de.mirkosertic.bytecoder.ssa.Variable) TypeRef(de.mirkosertic.bytecoder.ssa.TypeRef) BytecodeObjectTypeRef(de.mirkosertic.bytecoder.core.BytecodeObjectTypeRef)

Example 4 with TypeRef

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("}");
}
Also used : Program(de.mirkosertic.bytecoder.ssa.Program) Variable(de.mirkosertic.bytecoder.ssa.Variable) BytecodeMethodSignature(de.mirkosertic.bytecoder.core.BytecodeMethodSignature) TypeRef(de.mirkosertic.bytecoder.ssa.TypeRef) BytecodeObjectTypeRef(de.mirkosertic.bytecoder.core.BytecodeObjectTypeRef)

Example 5 with TypeRef

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);
    }
}
Also used : HashMap(java.util.HashMap) TypeRef(de.mirkosertic.bytecoder.ssa.TypeRef) org.jocl.cl_mem(org.jocl.cl_mem) Field(java.lang.reflect.Field) CL.clReleaseMemObject(org.jocl.CL.clReleaseMemObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TypeRef (de.mirkosertic.bytecoder.ssa.TypeRef)5 BytecodeObjectTypeRef (de.mirkosertic.bytecoder.core.BytecodeObjectTypeRef)4 BytecodeTypeRef (de.mirkosertic.bytecoder.core.BytecodeTypeRef)2 ByteValue (de.mirkosertic.bytecoder.ssa.ByteValue)2 ClassReferenceValue (de.mirkosertic.bytecoder.ssa.ClassReferenceValue)2 DoubleValue (de.mirkosertic.bytecoder.ssa.DoubleValue)2 FloatValue (de.mirkosertic.bytecoder.ssa.FloatValue)2 IntegerValue (de.mirkosertic.bytecoder.ssa.IntegerValue)2 LongValue (de.mirkosertic.bytecoder.ssa.LongValue)2 NullValue (de.mirkosertic.bytecoder.ssa.NullValue)2 ShortValue (de.mirkosertic.bytecoder.ssa.ShortValue)2 StringValue (de.mirkosertic.bytecoder.ssa.StringValue)2 Value (de.mirkosertic.bytecoder.ssa.Value)2 Variable (de.mirkosertic.bytecoder.ssa.Variable)2 BytecodeMethodSignature (de.mirkosertic.bytecoder.core.BytecodeMethodSignature)1 BytecodePrimitiveTypeRef (de.mirkosertic.bytecoder.core.BytecodePrimitiveTypeRef)1 MethodParameterValue (de.mirkosertic.bytecoder.ssa.MethodParameterValue)1 Program (de.mirkosertic.bytecoder.ssa.Program)1 SelfReferenceParameterValue (de.mirkosertic.bytecoder.ssa.SelfReferenceParameterValue)1 Field (java.lang.reflect.Field)1