-
Notifications
You must be signed in to change notification settings - Fork 30
[Metal] Explicit root signature layout for Metal backend #1061
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
kcloudy0717
wants to merge
11
commits into
llvm:main
Choose a base branch
from
kcloudy0717:kcloudy0717/metal_irconverter
base: main
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
02e455b
Add metal_irconverter includes/libs to third-party
kcloudy0717 c9928b9
Proof of concept of using explicit root signature layout for Metal ba…
kcloudy0717 e495693
Removed XFAIL for unexpectedly passing tests that is expected to pass
kcloudy0717 4841183
Apply LLVM naming conventions
kcloudy0717 83fa0da
Cleanup headers and added more forward declarations
kcloudy0717 254bd53
Cleanup comment and removed dead code path
kcloudy0717 6c1ae76
Removed metalirconverter.dylib, use the one installed on the system
kcloudy0717 08a9170
Removed XFAIL for passing tests
kcloudy0717 3c783b1
Removed metal_irconverter headers, used the one in usr/local/include
kcloudy0717 9211702
Attempt to fix dylib error on CI machines
kcloudy0717 3621a54
Removed XFAIL for passing tests
kcloudy0717 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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include "MTLDescriptorHeap.h" | ||
| #include "MetalIRConverter.h" | ||
|
|
||
| using namespace offloadtest; | ||
|
|
||
| static NS::UInteger getDescriptorHeapBindPoint(MTLDescriptorHeapType Type) { | ||
| switch (Type) { | ||
| case MTLDescriptorHeapType::CBV_SRV_UAV: | ||
| return kIRDescriptorHeapBindPoint; | ||
| case MTLDescriptorHeapType::Sampler: | ||
| return kIRSamplerHeapBindPoint; | ||
| } | ||
| llvm_unreachable("All cases handled."); | ||
| } | ||
|
|
||
| MTLGPUDescriptorHandle & | ||
| MTLGPUDescriptorHandle::Offset(int32_t OffsetInDescriptors) { | ||
| Ptr = MTL::GPUAddress(int64_t(Ptr) + int64_t(OffsetInDescriptors) * | ||
| sizeof(IRDescriptorTableEntry)); | ||
| return *this; | ||
| } | ||
|
|
||
| llvm::Expected<std::unique_ptr<MTLDescriptorHeap>> | ||
| MTLDescriptorHeap::create(MTL::Device *Device, | ||
| const MTLDescriptorHeapDesc &Desc) { | ||
| if (!Device) | ||
| return llvm::createStringError(std::errc::invalid_argument, | ||
| "Invalid MTL::Device pointer."); | ||
|
|
||
| if (Desc.NumDescriptors == 0) | ||
| return llvm::createStringError(std::errc::invalid_argument, | ||
| "Invalid descriptor heap description."); | ||
|
|
||
| MTL::Buffer *Buf = | ||
| Device->newBuffer(Desc.NumDescriptors * sizeof(IRDescriptorTableEntry), | ||
| MTL::ResourceStorageModeShared); | ||
| if (!Buf) | ||
| return llvm::createStringError(std::errc::not_enough_memory, | ||
| "Failed to create MTLDescriptorHeap."); | ||
| return std::make_unique<MTLDescriptorHeap>(Desc, Buf); | ||
| } | ||
|
|
||
| MTLDescriptorHeap::~MTLDescriptorHeap() { | ||
| if (Buffer) | ||
| Buffer->release(); | ||
| } | ||
|
|
||
| MTLGPUDescriptorHandle | ||
| MTLDescriptorHeap::getGPUDescriptorHandleForHeapStart() const { | ||
| return MTLGPUDescriptorHandle{Buffer->gpuAddress()}; | ||
| } | ||
|
|
||
| IRDescriptorTableEntry * | ||
| MTLDescriptorHeap::getEntryHandle(uint32_t Index) const { | ||
| assert(Index < Desc.NumDescriptors && "Descriptor index out of bounds."); | ||
| return static_cast<IRDescriptorTableEntry *>(Buffer->contents()) + Index; | ||
| } | ||
|
|
||
| void MTLDescriptorHeap::bind(MTL::RenderCommandEncoder *Encoder) { | ||
| Encoder->useResource(Buffer, MTL::ResourceUsageRead); | ||
| // Dynamic resource indexing | ||
| const NS::UInteger BindPoint = getDescriptorHeapBindPoint(Desc.Type); | ||
| Encoder->setVertexBuffer(Buffer, 0, BindPoint); | ||
| Encoder->setFragmentBuffer(Buffer, 0, BindPoint); | ||
| } | ||
|
|
||
| void MTLDescriptorHeap::bind(MTL::ComputeCommandEncoder *Encoder) { | ||
| Encoder->useResource(Buffer, MTL::ResourceUsageRead); | ||
| // Dynamic resource indexing | ||
| const NS::UInteger BindPoint = getDescriptorHeapBindPoint(Desc.Type); | ||
| Encoder->setBuffer(Buffer, 0, BindPoint); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| //===- MTLDescriptorHeap.h - Metal Descriptor Heap ------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef OFFLOADTEST_API_MTL_MTLDESCRIPTORHEAP_H | ||
| #define OFFLOADTEST_API_MTL_MTLDESCRIPTORHEAP_H | ||
|
|
||
| #include "llvm/Support/Error.h" | ||
| #include <memory> | ||
|
|
||
| // Forward declarations | ||
| namespace MTL { | ||
| class Device; | ||
| class Buffer; | ||
| class RenderCommandEncoder; | ||
| class ComputeCommandEncoder; | ||
| } // namespace MTL | ||
| struct IRDescriptorTableEntry; | ||
|
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. It's a bit awkward that |
||
|
|
||
| namespace offloadtest { | ||
| struct MTLGPUDescriptorHandle { | ||
| MTLGPUDescriptorHandle &Offset(int32_t OffsetInDescriptors); | ||
|
|
||
| uint64_t Ptr; | ||
| }; | ||
|
|
||
| enum class MTLDescriptorHeapType { | ||
| CBV_SRV_UAV, | ||
| Sampler, | ||
| }; | ||
|
|
||
| struct MTLDescriptorHeapDesc { | ||
| MTLDescriptorHeapType Type; | ||
| uint32_t NumDescriptors; | ||
| }; | ||
|
|
||
| // MTLDescriptorHeap mimics the D3D12 descriptor heap concept, except | ||
| // MTLDescriptorHeap is always shader visible and meant to be used | ||
| // by the argument buffer for shader resource binding with the explicit root | ||
| // signature layout. | ||
| class MTLDescriptorHeap { | ||
| MTLDescriptorHeapDesc Desc; | ||
| MTL::Buffer *Buffer; | ||
|
|
||
| public: | ||
| static llvm::Expected<std::unique_ptr<MTLDescriptorHeap>> | ||
| create(MTL::Device *Device, const MTLDescriptorHeapDesc &Desc); | ||
|
|
||
| MTLDescriptorHeap(const MTLDescriptorHeapDesc &Desc, MTL::Buffer *Buffer) | ||
| : Desc(Desc), Buffer(Buffer) {} | ||
| ~MTLDescriptorHeap(); | ||
|
|
||
| MTLGPUDescriptorHandle getGPUDescriptorHandleForHeapStart() const; | ||
|
|
||
| IRDescriptorTableEntry *getEntryHandle(uint32_t Index) const; | ||
|
|
||
| void bind(MTL::RenderCommandEncoder *Encoder); | ||
| void bind(MTL::ComputeCommandEncoder *Encoder); | ||
| }; | ||
| } // namespace offloadtest | ||
|
|
||
| #endif // OFFLOADTEST_API_MTL_MTLDESCRIPTORHEAP_H | ||
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.
I am not sure if we should expose this here. This is a concept that only makes sense for DX12 and Metal, but not for Vulkan. I do see the value of deduplicating this code between DX and Metal. Perhaps we should put this in some sort of util header file?
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.
This is a really niche case usage for the enum that I don't think it justifies adding a new util header that will be used by multiple backends for it.
Maybe we can come back to the util header once we see more shared code that is needed by multiple backends? Alternatively, I can just remove the deduplication of this code and have the same enum defined in the Metal backend.
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'm okay with cleaning this up later. We are adding an abstraction layer for the graphics APIs, so we'll just take another look at this enum once it becomes relevant.
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.
That sounds good to me. Thanks for the review.