cornell-zhang/hcl-dialect

[Op] Error: `affine.load` destroyed but still has uses

Closed this issue · 2 comments

Example

import heterocl as hcl

def load(instr):
    memory_type = hcl.scalar(instr[7:9], name="memory_type").v
    x_size = hcl.scalar(instr[64:80], name="y_size")
    with hcl.if_(x_size.v == 0):
        pass
    with hcl.elif_(memory_type == 0):
        pass
    with hcl.elif_(memory_type == 1):
        pass
    return

def kernel(instr):
    with hcl.for_(0, 10, tag='i') as i:
        load(instr[i])

    
instr = hcl.placeholder((10,))
s = hcl.create_schedule([instr], kernel)
print(hcl.lower(s))

Output:

error: 'affine.load' op operation destroyed but still has uses
LLVM ERROR: operation destroyed but still has uses
 #0 0x00007f72af8b805f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x00007f72af8b5a89 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f72badbc630 __restore_rt sigaction.c:0:0
 #3 0x00007f72ba104387 raise (/lib64/libc.so.6+0x36387)
 #4 0x00007f72ba105a78 abort (/lib64/libc.so.6+0x37a78)
 #5 0x00007f7235eb3d85 llvm::report_fatal_error(llvm::Twine const&, bool) (/work/shared/users/phd/nz264/mlir/hcl-dialect/build/tools/hcl/python_packages/hcl_core/hcl_mlir/_mlir_libs/libHCLMLIRAggregateCAPI.so.14+0xf00d85)

This happens because of memory_type is loaded once and used in more than one branch conditions.
If we write it this way it would be fine:

def load(instr):
    memory_type = hcl.scalar(instr[7:9], name="memory_type")
    x_size = hcl.scalar(instr[64:80], name="y_size")
    with hcl.if_(x_size.v == 0):
        pass
    with hcl.elif_(memory_type.v == 0):
        pass
    with hcl.elif_(memory_type.v == 1):
        pass
    return

The first style is common in VTA. I think we should detect such cases and throw out a proper error message, instead of letting LLVM crash and spitting out a scary stack trace.

Right. This issue is because I remove the original expression when building if_ condition. .v forces the compiler to build different variables.

This issue is resolved after we have HCL AST