Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WGSL] Use arena allocation for AST nodes
https://bugs.webkit.org/show_bug.cgi?id=255917 rdar://108496324 Reviewed by Myles C. Maxfield. The AST was originally implemented using only unique references/pointers, which made it really difficult to transform the AST. Currently we have a mix of Ref(Ptr) and unique references/pointers, which is unnecessary since all the nodes have the same lifecycle. To simplify things, we switch to an arena allocator, inspired by the JSC's ParserArena. I converted only a single leaf node, StructureMember, to use the new AST builder as a proof of concept, but more nodes can be converted incrementally later. * Source/WebGPU/WGSL/AST/ASTBuilder.cpp: Added. (WGSL::AST::Builder::arena): (WGSL::AST::Builder::~Builder): (WGSL::AST::Builder::allocateArena): * Source/WebGPU/WGSL/AST/ASTBuilder.h: Added. (WGSL::AST::Builder::construct): (WGSL::AST::Builder::alignSize): * Source/WebGPU/WGSL/AST/ASTStringDumper.cpp: (WGSL::AST::StringDumper::visitPointerVector): (WGSL::AST::StringDumper::visit): * Source/WebGPU/WGSL/AST/ASTStringDumper.h: * Source/WebGPU/WGSL/AST/ASTStructureMember.h: * Source/WebGPU/WGSL/AST/ASTVisitor.cpp: (WGSL::AST::Visitor::visit): * Source/WebGPU/WGSL/EntryPointRewriter.cpp: (WGSL::EntryPointRewriter::constructInputStruct): (WGSL::EntryPointRewriter::visit): * Source/WebGPU/WGSL/GlobalVariableRewriter.cpp: (WGSL::RewriteGlobalVariables::insertStructs): * Source/WebGPU/WGSL/MangleNames.cpp: (WGSL::NameManglerVisitor::visit): * Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp: (WGSL::Metal::FunctionDefinitionWriter::visit): * Source/WebGPU/WGSL/Parser.cpp: (WGSL::Parser<Lexer>::parseStructure): (WGSL::Parser<Lexer>::parseStructureMember): Deleted. * Source/WebGPU/WGSL/ParserPrivate.h: (WGSL::Parser::Parser): * Source/WebGPU/WGSL/TypeCheck.cpp: (WGSL::TypeChecker::visitStructMembers): * Source/WebGPU/WGSL/WGSLShaderModule.h: (WGSL::ShaderModule::astBuilder): * Source/WebGPU/WebGPU.xcodeproj/project.pbxproj: Canonical link: https://commits.webkit.org/263452@main
- Loading branch information
1 parent
3257310
commit af2e0ab
Showing
13 changed files
with
191 additions
and
23 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2023 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include "config.h" | ||
#include "ASTBuilder.h" | ||
|
||
namespace WGSL::AST { | ||
|
||
DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(WGSLAST); | ||
|
||
Builder::Builder(Builder&& other) | ||
{ | ||
m_arena = std::exchange(other.m_arena, nullptr); | ||
m_arenaEnd = std::exchange(other.m_arenaEnd, nullptr); | ||
m_arenas = WTFMove(other.m_arenas); | ||
m_nodes = WTFMove(other.m_nodes); | ||
} | ||
|
||
inline uint8_t* Builder::arena() | ||
{ | ||
ASSERT(m_arenaEnd); | ||
return m_arenaEnd - arenaSize; | ||
} | ||
|
||
Builder::~Builder() | ||
{ | ||
size_t size = m_nodes.size(); | ||
for (size_t i = 0; i < size; ++i) | ||
m_nodes[i]->~Node(); | ||
} | ||
|
||
void Builder::allocateArena() | ||
{ | ||
m_arenas.append(MallocPtr<uint8_t, WGSLASTMalloc>::malloc(arenaSize)); | ||
m_arena = m_arenas.last().get(); | ||
m_arenaEnd = m_arena + arenaSize; | ||
ASSERT(arena() == m_arena); | ||
} | ||
|
||
} // namespace WGSL::AST |
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,83 @@ | ||
/* | ||
* Copyright (c) 2023 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "ASTNode.h" | ||
#include <wtf/MallocPtr.h> | ||
#include <wtf/Noncopyable.h> | ||
#include <wtf/Nonmovable.h> | ||
#include <wtf/Vector.h> | ||
|
||
#define WGSL_AST_BUILDER_NODE(Node) \ | ||
WTF_MAKE_NONCOPYABLE(Node); \ | ||
WTF_MAKE_NONMOVABLE(Node); \ | ||
friend class Builder; \ | ||
|
||
namespace WGSL::AST { | ||
|
||
DECLARE_ALLOCATOR_WITH_HEAP_IDENTIFIER(WGSLAST); | ||
|
||
class Builder { | ||
WTF_MAKE_NONCOPYABLE(Builder); | ||
|
||
public: | ||
static constexpr size_t arenaSize = 0x4000; | ||
|
||
Builder() = default; | ||
Builder(Builder&&); | ||
~Builder(); | ||
|
||
template<typename T, typename... Arguments, typename = std::enable_if_t<std::is_base_of_v<Node, T>>> | ||
T& construct(Arguments&&... arguments) | ||
{ | ||
constexpr size_t size = sizeof(T); | ||
constexpr size_t alignedSize = alignSize(size); | ||
static_assert(alignedSize <= arenaSize); | ||
if (UNLIKELY(static_cast<size_t>(m_arenaEnd - m_arena) < alignedSize)) | ||
allocateArena(); | ||
|
||
auto* node = new (m_arena) T(std::forward<Arguments>(arguments)...); | ||
m_arena += alignedSize; | ||
m_nodes.append(node); | ||
return *node; | ||
} | ||
|
||
private: | ||
static constexpr size_t alignSize(size_t size) | ||
{ | ||
return (size + sizeof(WTF::AllocAlignmentInteger) - 1) & ~(sizeof(WTF::AllocAlignmentInteger) - 1); | ||
} | ||
|
||
uint8_t* arena(); | ||
void allocateArena(); | ||
|
||
uint8_t* m_arena { nullptr }; | ||
uint8_t* m_arenaEnd { nullptr }; | ||
Vector<MallocPtr<uint8_t, WGSLASTMalloc>> m_arenas; | ||
Vector<Node*> m_nodes; | ||
}; | ||
|
||
} // namespace WGSL::AST |
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
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
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
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
Oops, something went wrong.