shader-slang/slang

Resource variables used in a global initializer function do not hoist correctly

Opened this issue · 1 comments

Problem:
Once compiled, code may leave assignments to resource variables in global scope with local variable assignments in global scope

Repro:

  • Run the following test
// type-legalize-global-with-init.slang
//
// Confirm that type legalization can handle a global constant
// with a resource type or a type that recursively contains
// resources.
//
//TEST(compute):COMPARE_COMPUTE: -shaderobj
//
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<uint> outputBuffer;

//TEST_INPUT:ubuffer(data=[1 2 3 4 5 6 7 8], stride=4):name=inputBuffer
RWStructuredBuffer<uint> inputBuffer;

static const RWStructuredBuffer<uint> gBuffer = inputBuffer;

struct Stuff
{
    __init(RWStructuredBuffer<uint> a1, RWStructuredBuffer<uint> b2)
    {
        a = a1;
        b = b2;
    }

    RWStructuredBuffer<uint> a;
    RWStructuredBuffer<uint> b;
}

static const Stuff gStuff = Stuff( inputBuffer, inputBuffer );

uint test(uint x)
{
    return gBuffer[x]
        + gStuff.a[x + 1] * 16
        + gStuff.b[x + 2] * 256;
}

[numthreads(4, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    let tid = dispatchThreadID.x;
    let inVal = tid;
    let outVal = test(inVal);
    outputBuffer[tid] = outVal;
}

...

Current plan to solve issue using IRGlobalConstants:

  1. If our IRGlobalConstant contains a 'ResourceType' or is a 'ResourceType', check the 'value'.
  2. If this 'value' type is an IRCall, process this IRGlobalConstant,
  3. when processing our IRCall do the following:
    • Find the IRReturn (if a Struct type, find the ResourceType'd member), find all uses, find where this value is stored into, legalize using this value

Limitations:

  1. Not dynamic
  2. can break in complex enough scenarios or when users are treating resources as variables.