Skip to content

[SPIRV] Invalid SPIRV generation from switch table generation #158129

@llvm-beanz

Description

@llvm-beanz

I'm seeing invalid SPIRV generation for cases where LLVM's switch lowering is generating switch tables. This reproduces whether you use a switch statement explicitly or an if/else if/else chain.

StructuredBuffer<int> InInt : register(t0);
RWStructuredBuffer<int> OutBroadcast : register(u1);
RWStructuredBuffer<int> OutShift : register(u2);
RWStructuredBuffer<int> OutMix : register(u3);

[numthreads(4,1,1)]
void main(uint3 TID : SV_GroupThreadID) {
  OutBroadcast[TID.x] = WaveReadLaneAt(InInt[TID.x], 2);

  uint PosShiftIndex = (TID.x + 1) % 4;
  int PosValue = WaveReadLaneAt(InInt[TID.x], PosShiftIndex);
  uint NegShiftIndex = (TID.x - 1) % 4;
  int NegValue = WaveReadLaneAt(InInt[TID.x], NegShiftIndex);
  OutShift[TID.x] = PosValue + NegValue;

  uint MixIndex = 0;
#if SWITCH
  switch (TID.x) {
    case 0:
    MixIndex = 2;
    break;
    case 1:
    MixIndex = 3;
    break;
    case 2:
    MixIndex = 1;
    break;
    default:
    break;
  }
#else
  if (TID.x == 0)
      MixIndex = 2;
  else if (TID.x == 1)
      MixIndex = 3;
  else if (TID.x == 2)
      MixIndex = 1;
#endif

  OutMix[TID.x] = WaveReadLaneAt(InInt[TID.x], MixIndex);
}

Compiler Explorer

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions