Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ArielG-NV opened this issue Aug 20, 2024 · 1 comment · May be fixed by #4894
Open

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

ArielG-NV opened this issue Aug 20, 2024 · 1 comment · May be fixed by #4894
Assignees
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang kind:bug something doesn't work like it should

Comments

@ArielG-NV
Copy link
Contributor

ArielG-NV commented Aug 20, 2024

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;
}

...

@ArielG-NV ArielG-NV added kind:bug something doesn't work like it should goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang labels Aug 20, 2024
@ArielG-NV ArielG-NV self-assigned this Aug 20, 2024
@ArielG-NV ArielG-NV changed the title Global initializers with resource variables do not hoist resource variables Resource variables used in a global initializer does not hoist Aug 20, 2024
@ArielG-NV ArielG-NV added this to the Q3 2024 (Summer) milestone Aug 20, 2024
@ArielG-NV ArielG-NV changed the title Resource variables used in a global initializer does not hoist Resource variables used in a global initializer function do not hoist correctly Aug 21, 2024
ArielG-NV added a commit to ArielG-NV/slang that referenced this issue Aug 21, 2024
Fixes: shader-slang#4874
Hoist resource variables using a global initializer function in such a way that the initializer function is passed into every entry-point opposed to emitting a local-variable asignment in global scope.
@ArielG-NV
Copy link
Contributor Author

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.

@ArielG-NV ArielG-NV added the beyond-sprint Label for issues which will be worked on but not completed during a sprint. label Sep 3, 2024
ArielG-NV added a commit to ArielG-NV/slang that referenced this issue Sep 3, 2024
@ArielG-NV ArielG-NV removed the beyond-sprint Label for issues which will be worked on but not completed during a sprint. label Sep 4, 2024
@ArielG-NV ArielG-NV removed their assignment Sep 6, 2024
kaizhangNV pushed a commit to kaizhangNV/slang that referenced this issue Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang kind:bug something doesn't work like it should
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants