Skip to content

[HLSL] Array return types #126568

@llvm-beanz

Description

@llvm-beanz

HLSL supports returning values of array type. For example:

typedef uint32_t4 uint32_t8[2];

export uint32_t8 bleh(uint32_t4 A, uint32_t4 B) {
    uint32_t8 output = {A, B};
    return output;
}

Compiler Explorer

In HLSL Arrays are passed by value, and can be returned by value. We should treat an array return type like a structure return type (sret), and pre-allocate the memory for the array and return into the pre-allocated memory.

FXC and DXC disagree about the parsing for these types. DXC supports array return types when the array is either a typedef or using the syntax <type> <function name>(<parameter list>)[<array size>].

FXC requires the use of a typedef and cannot parse the array-ness directly in the function declaration:

Tried here using the C-based syntax DXC supports -> https://shader-playground.timjones.io/4ee7eb08392d718f6c4361d3a4f0a2b8
Tried here using a more sane-seeming syntax -> https://shader-playground.timjones.io/63496da689c53fa78f697e56112f6290
Working with a typedef- > https://shader-playground.timjones.io/7f68b0373dfe7a5c863234c2245659eb

DXC's non-typedef syntax:

typedef uint32_t4 uint32_t8[2];

export uint32_t4 bleh(uint32_t4 A, uint32_t4 B)[2] {
    uint32_t4 output[] = {A, B};
    return output;
}

Compiler Explorer

Metadata

Metadata

Assignees

Labels

HLSLHLSL Language Supportclang:frontendLanguage frontend issues, e.g. anything involving "Sema"

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions