-
Notifications
You must be signed in to change notification settings - Fork 187
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
Hoist resource variables using a global initializer function correctly #4894
base: master
Are you sure you want to change the base?
Hoist resource variables using a global initializer function correctly #4894
Conversation
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.
…ttps://github.com/ArielG-NV/slang into hoist-resource-variables-with-global-init-function
|
||
// If our call is in global scope (initializer) we should | ||
// hoist the call into the start of every entry-point | ||
if (!m_context->builder->getInsertLoc().getInst()->getParent() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what this is trying to achieve here, but I think this is likely not the right way.
Usually if you have a global variable and you want to initialize it with some code (e.g. a call), you create a IRGlobalVar
, which is a IRGlobalValueWithCode
that can have a body just like a IRFunc
. Inside the body you create a basic block with instructions + a return
instruct that returns the value of the init expr.
Here instead of inserting a call into global scope, we should be creating that IRGlobalVar, and rely on follow up passes to insert the logic in the body into entry points.
…vars that act as alias's
…e spirv/CUDA treat literals as ptrs to literals)
//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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general I think we will have issues when there are global resource variables that is being written to dynamically, so I am not sure if this is something we want to allow...
static const currently is overloaded to also mean compile time constants, maybe we should disallow static consts of resource type as well. Is this test coming from some existing user code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general I think we will have issues when there are global resource variables that is being written to dynamically, so I am not sure if this is something we want to allow...
We will have an issue with dynamic resource variable accesses.
Slang is lacking a compiler pass to manage non-compile-time known resource accesses such as:
RWStructuredBuffer<uint> outputBuffer;
RWStructuredBuffer<uint> inputBuffer1;
RWStructuredBuffer<uint> inputBuffer2;
RWStructuredBuffer<uint> inputBuffer3;
[numthreads(4,4,4)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
RWStructuredBuffer<uint> resource;
if(dispatchThreadID.x == 1)
{
resource = inputBuffer1;
}
else if(dispatchThreadID.x == 2)
{
resource = inputBuffer2;
}
else if(dispatchThreadID.x == 3)
{
resource = inputBuffer3;
}
// This is illegal since resource is not known at compile time.
outputBuffer[0] = resource[1];
}
From what I understand to add this would just piggy-back on our dynamic dispatch system logic:
- Diagnose all non-constexpr resources, assign all resources of equal type a different ID (like with dynamic dispatch).
- Any assignments to a resource assigns an ID (not the actual resource)
- All accesses to a non-compile-time resource specializes all texture intrinsic uses to pick the correct texture using our ID (if/else binary tree)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static const currently is overloaded to also mean compile time constants, maybe we should disallow static consts of resource type as well. Is this test coming from some existing user code?
No test currently uses static const Stuff gStuff = Stuff( inputBuffer, inputBuffer );
.
The problem is that some tests use static const Stuff gStuff = { inputBuffer, inputBuffer }
, which once #4854 is merged will be an issue since we start emitting what is effectively static const Stuff gStuff = Stuff( inputBuffer, inputBuffer );
.
Reading at the code here, it seems clear to me that we need to legalize out the resource typed global variable out of existence before we reach In this case, the global |
I will work on this solution 👍 Edit: As per conversation outside github-comments, work on this PR will halt for now. |
Fixes: #4874
Goal:
Hoist resource variables using a global initializer function in such a way that any global with initializer function has any possible resource variables hoisted into their use location directly (otherwise many targets won't work).
Changes:
[only done for resource variables for simplicity of legalization pass]