-
Notifications
You must be signed in to change notification settings - Fork 236
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
Draft
ArielG-NV
wants to merge
10
commits into
shader-slang:master
Choose a base branch
from
ArielG-NV:hoist-resource-variables-with-global-init-function
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
38c84bb
Hoist resource variables using a global initializer function correctly
ArielG-NV 1764468
Merge branch 'master' into hoist-resource-variables-with-global-init-…
ArielG-NV ba467a5
reorganize code
ArielG-NV 0d9d601
Merge branch 'hoist-resource-variables-with-global-init-function' of …
ArielG-NV b521d9b
Merge branch 'master' into hoist-resource-variables-with-global-init-…
ArielG-NV bf97d6b
redo the approach to legalization to just remove all indirect global …
ArielG-NV ea5399d
cleanup and fix warning
ArielG-NV dbc7ae2
don't simplify ptr to non resource types (need an indirect value sinc…
ArielG-NV dc2b47a
fix metal by including it in more compile passes
ArielG-NV d0b23e4
Merge branch 'master' into hoist-resource-variables-with-global-init-…
ArielG-NV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// slang-ir-explicit-global-init.cpp | ||
|
||
#include "slang-ir-simplify-global-vars.h" | ||
#include "slang-ir-insts.h" | ||
#include "slang-ir.h" | ||
#include "slang-ir-util.h" | ||
|
||
namespace Slang | ||
{ | ||
void tryToSimplifyUseOfGlobalVar(IRInst* user, List<IRInst*>& toDestroy) | ||
{ | ||
switch (user->getOp()) | ||
{ | ||
case kIROp_Store: | ||
{ | ||
auto store = as<IRStore>(user); | ||
if (store->getParent() && store->getParent()->getOp() == kIROp_Module) | ||
{ | ||
// Store in Global scope into a GlobalVar is equal to 'insertReturn(global->GetFirstBlock(), Store->GetVal())' | ||
if (auto globalVal = as<IRGlobalValueWithCode>(store->getPtr())) | ||
{ | ||
IRBuilder builder(store); | ||
IRBlock* block = globalVal->getFirstBlock(); | ||
if (!block) | ||
{ | ||
builder.setInsertInto(globalVal); | ||
block = builder.emitBlock(); | ||
} | ||
builder.setInsertInto(block); | ||
builder.emitReturn(store->getVal()); | ||
toDestroy.add(user); | ||
} | ||
} | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void simplifyGlobalVars(IRModule* module) | ||
{ | ||
IRBuilder builder(module); | ||
for (auto inst : module->getGlobalInsts()) | ||
{ | ||
auto globalVar = as<IRGlobalVar>(inst); | ||
if (!globalVar) | ||
continue; | ||
|
||
// Only simplify resource types since otherwise we need additional logic | ||
// not yet implemented | ||
auto globalVarType = globalVar->getDataType(); | ||
auto globalPtrType = as<IRPtrTypeBase>(globalVarType); | ||
if (!globalPtrType | ||
|| !isResourceType(globalPtrType->getValueType())) | ||
continue; | ||
|
||
// Find any trivial use which is supposed-to-be part of the | ||
// init block, hoist these. | ||
// Do some extra auixillary checks. | ||
List<IRInst*> toDestroy; | ||
bool onlyGlobalSideEffects = true; | ||
traverseUses(globalVar, [&](IRUse* use) | ||
{ | ||
auto user = use->getUser(); | ||
tryToSimplifyUseOfGlobalVar(user, toDestroy); | ||
if (user->getParent()->getOp() != kIROp_Module | ||
&& user->mightHaveSideEffects()) | ||
onlyGlobalSideEffects = false; | ||
}); | ||
for (auto i : toDestroy) | ||
i->removeAndDeallocate(); | ||
|
||
// Check if our IRGlobalVar is an alias for another variable, if so, simplify | ||
if (!onlyGlobalSideEffects) | ||
continue; | ||
|
||
auto firstBlock = globalVar->getFirstBlock(); | ||
if (!firstBlock) | ||
continue; | ||
|
||
// For an alias we must encounter a return op first | ||
bool fail = false; | ||
IRReturn* firstReturn = nullptr; | ||
|
||
for (auto localInst : firstBlock->getChildren()) | ||
{ | ||
if (localInst->getOp() != kIROp_Return) | ||
{ | ||
fail = true; | ||
break; | ||
} | ||
else if (localInst->getOp() == kIROp_Return) | ||
{ | ||
firstReturn = as<IRReturn>(localInst); | ||
break; | ||
} | ||
} | ||
|
||
if (fail || !firstReturn) | ||
continue; | ||
|
||
globalVar->replaceUsesWith(firstReturn->getVal()); | ||
firstBlock->removeAndDeallocateAllDecorationsAndChildren(); | ||
firstBlock->removeAndDeallocate(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// slang-ir-explicit-global-init.h | ||
#pragma once | ||
|
||
namespace Slang | ||
{ | ||
struct IRModule; | ||
|
||
/// Simplify IRGlobalVars and possibley remove if an alias. | ||
void simplifyGlobalVars(IRModule* module); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/compute/type-legalize-global-with-init-function.slang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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(filecheck-buffer=BUF): -shaderobj | ||
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUF): -vk -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> inA, RWStructuredBuffer<uint> inB) | ||
{ | ||
a = inA; | ||
b = inB; | ||
} | ||
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; | ||
} | ||
//BUF: 321 | ||
//BUF: 432 | ||
//BUF: 543 | ||
//BUF: 654 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
tests/compute/type-legalize-global-with-init.slang.expected.txt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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:
From what I understand to add this would just piggy-back on our dynamic dispatch system logic:
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.
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 effectivelystatic const Stuff gStuff = Stuff( inputBuffer, inputBuffer );
.