Skip to content

[compiler] The compiler hangs when trying to use the same array twice as input and output in any kernel #650

@mikepapadim

Description

@mikepapadim

Describe the bug

When using the same array for both input and output parameters in any tornado method, the TornadoVM compiler enters an infinite loop during the sketcher phase. This happens specifically when trying to resolve data dependencies between read and write operations on the same memory location

How To Reproduce

public class NormalizeScaleBugTest {
    public static void main(String[] args) {
        // Set up minimal test case
        int size = 1024;
        FloatArray array = new FloatArray(size);
        FloatArray weights = new FloatArray(size);
        FloatArray scalingFactor = new FloatArray(1);
        
        // Initialize data
        for (int i = 0; i < size; i++) {
            array.set(i, i * 0.01f);
            weights.set(i, 1.0f);
        }
        scalingFactor.set(0, 0.5f);
        
        // This works fine - different input/output arrays
        FloatArray outputArray = new FloatArray(size);
        KernelContext context = new KernelContext();
        TaskSchedule ts1 = new TaskSchedule("workingCase")
            .task("normalize", TornadoVMCompute::normalizeAndScale, context, array, outputArray, weights, scalingFactor, size)
            .streamOut(outputArray);
        ts1.execute();
        
        // This causes the compiler to hang - same array for input/output
        TaskSchedule ts2 = new TaskSchedule("bugCase")
            .task("normalize", TornadoVMCompute::normalizeAndScale, context, array, array, weights, scalingFactor, size)
            .streamOut(array);
        ts2.execute(); // Compiler hangs here during sketcher phase
    }
    
    public static void normalizeAndScale(KernelContext context, FloatArray x, FloatArray output, FloatArray weight, FloatArray scalingFactorBuffer, int size) {
        int globalIdx = context.globalIdx;

        if (globalIdx < size) {
            float scaledValue = weight.get(globalIdx) * (scalingFactorBuffer.get(0) * x.get(globalIdx));
            output.set(globalIdx, scaledValue);
        }
    }
}

Expected behavior

The compiler will hang indefinitely during the sketcher phase as it attempts to resolve the circular data dependency.


Metadata

Metadata

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions