Skip to content

[Bug] RuntimeError: Failed to emit HLS code (array has unsupported element type) during s.build() #576

@Jyrheeee

Description

@Jyrheeee

Describe the bug

Description

I encountered a RuntimeError: Failed to emit HLS code caused by an array has unsupported element type MLIR error. The error occurs during s.build() when trying to compile a TPU-like module targeting vitis_hls.

It appears that the Allo compiler has issues lowering arrays (either @stateful buffers or function arguments) that use the float16 data type.

Reproduction

Steps to Reproduce

Here is the complete code to reproduce the issue. It defines stateful float16 buffers and passes float16 arrays as arguments to the top-level function.

import allo
from allo import Memory
from allo.ir.types import int32, float16, float32, stateful
import numpy as np

M = 16
MEM_SIZE = M * M * 8
IMEM_SIZE = 32
BW = 16

def matmul(mem: float16[MEM_SIZE], addr1: int32, addr2: int32, addr3: int32, accum: int32):
    local_A: float16[M, M]
    local_B: float16[M, M]
    local_C: float16[M, M]
    
    for i, j in allo.grid(M, M, name="load_A"):
        local_A[i, j] = mem[addr1 + i * M + j]
    for i, j in allo.grid(M, M, name="load_B"):
        local_B[i, j] = mem[addr2 + i * M + j]
    for i, j in allo.grid(M, M, name="load_C"):
        if accum == 1:
            local_C[i, j] = mem[addr3 + i * M + j]
        else:
            local_C[i, j] = 0.0

    for m, n in allo.grid(M, M, name="PE"):
        c: float32 = local_C[m, n]
        for k in range(M):
            c = c + local_A[m, k] * local_B[k, n]
        local_C[m, n] = c
    
    for i, j in allo.grid(M, M, name="store_C"):
        mem[addr3 + i * M + j] = local_C[i, j]

def tpu(ctrl: int32, d_addr: int32, data_in: float16[BW], inst_in: int32[BW], outval: float16[BW], size: int32):
    mem: float16[MEM_SIZE] @ stateful = 0.0 
    imem: int32[IMEM_SIZE * 4] @ stateful = 0
    inv_sqrt_table: float16[512] @ stateful = 0.0 

    if ctrl == 0: # H2D
        for offset in range(size):
            mem[d_addr + offset] = data_in[offset]
    if ctrl == 1: # D2H
        for offset in range(size):
            outval[offset] = mem[d_addr + offset]
    if ctrl == 3: # RUN_PROG
        matmul(mem, 0, 16, 32, 0)

s = allo.customize(tpu)
mod = s.build(
    target="vitis_hls", 
    mode="sw_emu", 
    project="tpu_v0_fp16",
    configs={"device": "u280"}
)

### Buggy output

loc("-":6:3): error: array has unsupported element type.
loc("-":6:3): error: array has unsupported element type.
loc("-":6:3): error: array has unsupported element type.
loc("-":6:3): error: array has unsupported element type.
Traceback (most recent call last):
  File "minitpu_v0_fp16.py", line 318, in <module>
    mod = s.build(
          ^^^^^^^^
  File "/allo/customize.py", line 1298, in build
    return HLSModule(
           ^^^^^^^^^
  File "/allo/backend/hls.py", line 248, in __init__
    raise RuntimeError(
RuntimeError: Failed to emit HLS code. Check error messages above for details. Common issues: nested functions with multi-dimensional arrays when wrap_io=False.

### Expected behavior

The s.build() function should successfully lower the float16 arrays and stateful buffers, emit the corresponding Vitis HLS C++ code (e.g., using half data types in C++), and proceed with the software emulation without throwing an MLIR element type error.

### Additional context

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions