-
Notifications
You must be signed in to change notification settings - Fork 428
Support loadAligned on CUDA backend. #10098
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
Open
csyonghe
wants to merge
9
commits into
shader-slang:master
Choose a base branch
from
csyonghe:loadaligned-cuda
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.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2c9c08d
Support loadAligned on CUDA backend.
csyonghe 9cae4dd
Address review comments.
csyonghe 87c161f
Address comment.
csyonghe c2c6efa
format code
slangbot 08c9d5e
address review.
csyonghe 83035b2
Merge pull request #45 from slangbot/format-10098-loadaligned-cuda
csyonghe b1d860e
Update source/slang/slang-ir-cuda-immutable-load.cpp
csyonghe cc4d549
format code
slangbot 6e7e3ff
Merge pull request #46 from slangbot/format-10098-loadaligned-cuda
csyonghe 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -60,6 +60,52 @@ struct LoadMethod | |
| struct ImmutableBufferLoadLoweringContext : InstPassBase | ||
| { | ||
| Dictionary<IRType*, LoadMethod> loadFuncs; | ||
|
|
||
| struct AlignedTypeWrapperKey | ||
| { | ||
| IRType* innerType; | ||
| IRIntegerValue alignment; | ||
| HashCode getHashCode() const | ||
| { | ||
| return combineHash(Slang::getHashCode(innerType), Slang::getHashCode(alignment)); | ||
| } | ||
| bool operator==(const AlignedTypeWrapperKey& other) const | ||
| { | ||
| return innerType == other.innerType && alignment == other.alignment; | ||
| } | ||
| }; | ||
|
|
||
| Dictionary<AlignedTypeWrapperKey, IRType*> alignedWrapperTypes; | ||
|
|
||
| IRType* getOrCreateAlignedWrapper(IRType* innerType, IRIntegerValue alignment) | ||
| { | ||
| IRSizeAndAlignment naturalSizeAlignment; | ||
| getNaturalSizeAndAlignment(targetProgram->getTargetReq(), innerType, &naturalSizeAlignment); | ||
| if (naturalSizeAlignment.alignment >= alignment) | ||
| return innerType; | ||
|
|
||
| auto key = AlignedTypeWrapperKey{innerType, alignment}; | ||
| if (auto* wrappedType = alignedWrapperTypes.tryGetValue(key)) | ||
| return *wrappedType; | ||
|
|
||
| IRBuilder builder(innerType); | ||
| builder.setInsertAfter(innerType); | ||
|
|
||
| auto structType = builder.createStructType(); | ||
| StringBuilder nameSb; | ||
| getTypeNameHint(nameSb, innerType); | ||
| nameSb << "_aligned" << alignment; | ||
| builder.addNameHintDecoration(structType, nameSb.getUnownedSlice()); | ||
|
|
||
| auto fieldKey = builder.createStructKey(); | ||
| builder.addNameHintDecoration(fieldKey, toSlice("val")); | ||
| builder.createStructField(structType, fieldKey, innerType); | ||
| builder.addAlignmentDecoration(structType, alignment); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we re-use IRAlignedAttr as the decoration? |
||
|
|
||
| alignedWrapperTypes[key] = structType; | ||
| return structType; | ||
| } | ||
|
|
||
| TargetProgram* targetProgram; | ||
|
|
||
| IRFunc* createLoadFunc(IRBuilder& builder, IRType* valueType, IRParam*& outParam) | ||
|
|
@@ -300,16 +346,63 @@ struct ImmutableBufferLoadLoweringContext : InstPassBase | |
| case kIROp_Load: | ||
| { | ||
| auto load = as<IRLoad>(inst); | ||
| if (isPointerToImmutableLocation(getRootAddr(load->getPtr()))) | ||
| IRInst* loadedValue = load; | ||
|
|
||
| IRBuilder builder(load); | ||
| builder.setInsertBefore(load); | ||
| auto rootAddr = getRootAddr(load->getPtr()); | ||
| auto alignmentAttr = load->findAttr<IRAlignedAttr>(); | ||
| bool needUnwrap = false; | ||
| if (alignmentAttr) | ||
| { | ||
| auto wrappedType = getOrCreateAlignedWrapper( | ||
| load->getDataType(), | ||
| getIntVal(alignmentAttr->getAlignment())); | ||
| if (wrappedType != load->getDataType()) | ||
| { | ||
| auto newPtr = builder.emitBitCast( | ||
| builder.getPtrType( | ||
| kIROp_PtrType, | ||
| wrappedType, | ||
| as<IRPtrTypeBase>(load->getPtr()->getDataType())), | ||
| load->getPtr()); | ||
| builder.replaceOperand(load->getPtrOperand(), newPtr); | ||
| load->setFullType(wrappedType); | ||
| needUnwrap = true; | ||
| } | ||
| } | ||
| if (isPointerToImmutableLocation(rootAddr)) | ||
| { | ||
| if (auto immutableLoad = emitImmutableLoad(builder, load->getPtr())) | ||
| { | ||
| loadedValue = immutableLoad; | ||
| } | ||
| } | ||
|
|
||
| if (needUnwrap) | ||
| { | ||
| IRBuilder builder(load); | ||
| builder.setInsertBefore(load); | ||
| if (auto newLoad = emitImmutableLoad(builder, load->getPtr())) | ||
| List<IRUse*> uses; | ||
| for (auto use = inst->firstUse; use; use = use->nextUse) | ||
| { | ||
| uses.add(use); | ||
| } | ||
| auto wrappedStruct = as<IRStructType>(loadedValue->getDataType()); | ||
| SLANG_ASSERT(wrappedStruct); | ||
| auto firstField = wrappedStruct->getFields().getFirst(); | ||
| SLANG_ASSERT(firstField); | ||
| auto fieldKey = firstField->getKey(); | ||
| builder.setInsertAfter(loadedValue); | ||
| loadedValue = builder.emitFieldExtract(loadedValue, fieldKey); | ||
| for (auto use : uses) | ||
| { | ||
| load->replaceUsesWith(newLoad); | ||
| load->removeAndDeallocate(); | ||
| builder.replaceOperand(use, loadedValue); | ||
| } | ||
| } | ||
csyonghe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else if (loadedValue != inst) | ||
| { | ||
| inst->replaceUsesWith(loadedValue); | ||
| inst->removeAndDeallocate(); | ||
| } | ||
csyonghe marked this conversation as resolved.
Show resolved
Hide resolved
csyonghe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| break; | ||
| case kIROp_StructuredBufferLoad: | ||
|
|
@@ -365,7 +458,7 @@ struct ImmutableBufferLoadLoweringContext : InstPassBase | |
| } | ||
| }; | ||
|
|
||
| void lowerImmutableBufferLoadForCUDA(IRModule* module, TargetProgram* targetProgram) | ||
| void lowerImmutableOrAlignedBufferLoadForCUDA(IRModule* module, TargetProgram* targetProgram) | ||
| { | ||
| ImmutableBufferLoadLoweringContext context(module); | ||
| context.targetProgram = targetProgram; | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.