Implement Default Struct Layout

Implements https://github.com/gpuweb/gpuweb/pull/1447

SPIR-V Reader is still TODO, but continues to function as the offset
decoration is still supported.

Bug: tint:626
Bug: tint:629
Change-Id: Id574eb3a5c6729559382812de37b23f0c68fd406
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/43640
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton
2021-03-15 10:43:11 +00:00
committed by Commit Bot service account
parent 717fbbf183
commit d614dd5d12
107 changed files with 2401 additions and 2038 deletions

69
src/semantic/array.h Normal file
View File

@@ -0,0 +1,69 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_SEMANTIC_ARRAY_H_
#define SRC_SEMANTIC_ARRAY_H_
#include <stdint.h>
#include "src/semantic/node.h"
namespace tint {
// Forward declarations
namespace type {
class Array;
} // namespace type
namespace semantic {
/// Array holds the semantic information for Array nodes.
class Array : public Castable<Array, Node> {
public:
/// Constructor
/// @param type the Array type
/// @param align the byte alignment of the structure
/// @param size the byte size of the structure
/// @param stride the number of bytes from the start of one element of the
/// array to the start of the next element
Array(type::Array* type, uint32_t align, uint32_t size, uint32_t stride);
/// @return the resolved type of the Array
type::Array* Type() const { return type_; }
/// @returns the byte alignment of the array
/// @note this may differ from the alignment of a structure member of this
/// array type, if the member is annotated with the `[[align(n)]]` decoration.
uint32_t Align() const { return align_; }
/// @returns the byte size of the array
/// @note this may differ from the size of a structure member of this array
/// type, if the member is annotated with the `[[size(n)]]` decoration.
uint32_t Size() const { return size_; }
/// @returns the number of bytes from the start of one element of the
/// array to the start of the next element
uint32_t Stride() const { return stride_; }
private:
type::Array* const type_;
uint32_t const align_;
uint32_t const size_;
uint32_t const stride_;
};
} // namespace semantic
} // namespace tint
#endif // SRC_SEMANTIC_ARRAY_H_

View File

@@ -29,9 +29,9 @@ class Call : public Castable<Call, Expression> {
/// @param declaration the AST node
/// @param target the call target
/// @param statement the statement that owns this expression
explicit Call(ast::Expression* declaration,
const CallTarget* target,
Statement* statement);
Call(ast::Expression* declaration,
const CallTarget* target,
Statement* statement);
/// Destructor
~Call() override;

View File

@@ -23,12 +23,6 @@
#include "src/semantic/type_mappings.h"
namespace tint {
// Forward declarations
namespace ast {
class Node;
} // namespace ast
namespace semantic {
/// Info holds all the resolved semantic information for a Program.
@@ -48,26 +42,29 @@ class Info {
/// @return this Program
Info& operator=(Info&& rhs);
/// Get looks up the semantic information for the AST node `ast_node`.
/// @param ast_node the AST node
/// Get looks up the semantic information for the AST or type node `node`.
/// @param node the AST or type node
/// @returns a pointer to the semantic node if found, otherwise nullptr
template <typename AST, typename SEM = SemanticNodeTypeFor<AST>>
const SEM* Get(const AST* ast_node) const {
auto it = ast_to_sem_.find(ast_node);
if (it == ast_to_sem_.end()) {
template <typename AST_OR_TYPE,
typename SEM = SemanticNodeTypeFor<AST_OR_TYPE>>
const SEM* Get(const AST_OR_TYPE* node) const {
auto it = map.find(node);
if (it == map.end()) {
return nullptr;
}
return it->second->template As<SEM>();
}
/// Add registers the semantic node `sem_node` for the AST node `ast_node`.
/// @param ast_node the AST node
/// Add registers the semantic node `sem_node` for the AST or type node
/// `node`.
/// @param node the AST or type node
/// @param sem_node the semantic node
template <typename AST>
void Add(const AST* ast_node, const SemanticNodeTypeFor<AST>* sem_node) {
template <typename AST_OR_TYPE>
void Add(const AST_OR_TYPE* node,
const SemanticNodeTypeFor<AST_OR_TYPE>* sem_node) {
// Check there's no semantic info already existing for the node
assert(Get(ast_node) == nullptr);
ast_to_sem_.emplace(ast_node, sem_node);
assert(Get(node) == nullptr);
map.emplace(node, sem_node);
}
/// Wrap returns a new Info created with the contents of `inner`.
@@ -79,12 +76,12 @@ class Info {
/// @return the Info that wraps `inner`
static Info Wrap(const Info& inner) {
Info out;
out.ast_to_sem_ = inner.ast_to_sem_;
out.map = inner.map;
return out;
}
private:
std::unordered_map<const ast::Node*, const semantic::Node*> ast_to_sem_;
std::unordered_map<const CastableBase*, const semantic::Node*> map;
};
} // namespace semantic

26
src/semantic/sem_array.cc Normal file
View File

@@ -0,0 +1,26 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/semantic/array.h"
TINT_INSTANTIATE_TYPEINFO(tint::semantic::Array);
namespace tint {
namespace semantic {
Array::Array(type::Array* type, uint32_t align, uint32_t size, uint32_t stride)
: type_(type), align_(align), size_(size), stride_(stride) {}
} // namespace semantic
} // namespace tint

View File

@@ -0,0 +1,39 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/semantic/struct.h"
TINT_INSTANTIATE_TYPEINFO(tint::semantic::Struct);
TINT_INSTANTIATE_TYPEINFO(tint::semantic::StructMember);
namespace tint {
namespace semantic {
Struct::Struct(type::Struct* type,
StructMemberList members,
uint32_t align,
uint32_t size)
: type_(type), members_(std::move(members)), align_(align), size_(size) {}
Struct::~Struct() = default;
StructMember::StructMember(ast::StructMember* declaration,
uint32_t offset,
uint32_t size)
: declaration_(declaration), offset_(offset), size_(size) {}
StructMember::~StructMember() = default;
} // namespace semantic
} // namespace tint

112
src/semantic/struct.h Normal file
View File

@@ -0,0 +1,112 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0(the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_SEMANTIC_STRUCT_H_
#define SRC_SEMANTIC_STRUCT_H_
#include <stdint.h>
#include <vector>
#include "src/semantic/node.h"
namespace tint {
// Forward declarations
namespace ast {
class StructMember;
} // namespace ast
namespace type {
class Struct;
} // namespace type
namespace semantic {
class StructMember;
/// A vector of StructMember pointers.
using StructMemberList = std::vector<StructMember*>;
/// Struct holds the semantic information for structures.
class Struct : public Castable<Struct, Node> {
public:
/// Constructor
/// @param type the structure type
/// @param members the structure members
/// @param align the byte alignment of the structure
/// @param size the byte size of the structure
Struct(type::Struct* type,
StructMemberList members,
uint32_t align,
uint32_t size);
/// Destructor
~Struct() override;
/// @returns the structure type
type::Struct* Type() const { return type_; }
/// @returns the members of the structure
const StructMemberList& Members() const { return members_; }
/// @returns the byte alignment of the structure
/// @note this may differ from the alignment of a structure member of this
/// structure type, if the member is annotated with the `[[align(n)]]`
/// decoration.
uint32_t Align() const { return align_; }
/// @returns the byte size of the structure
/// @note this may differ from the size of a structure member of this
/// structure type, if the member is annotated with the `[[size(n)]]`
/// decoration.
uint32_t Size() const { return size_; }
private:
type::Struct* const type_;
StructMemberList const members_;
uint32_t const align_;
uint32_t const size_;
};
/// StructMember holds the semantic information for structure members.
class StructMember : public Castable<StructMember, Node> {
public:
/// Constructor
/// @param declaration the AST declaration node
/// @param offset the byte offset from the base of the structure
/// @param size the byte size
StructMember(ast::StructMember* declaration, uint32_t offset, uint32_t size);
/// Destructor
~StructMember() override;
/// @returns the AST declaration node
ast::StructMember* Declaration() const { return declaration_; }
/// @returns byte offset from base of structure
uint32_t Offset() const { return offset_; }
/// @returns byte size
uint32_t Size() const { return size_; }
private:
ast::StructMember* const declaration_;
uint32_t const offset_; // Byte offset from base of structure
uint32_t const size_; // Byte size
};
} // namespace semantic
} // namespace tint
#endif // SRC_SEMANTIC_STRUCT_H_

View File

@@ -19,44 +19,52 @@ namespace tint {
// Forward declarations
namespace ast {
class CallExpression;
class Expression;
class Function;
class MemberAccessorExpression;
class StructMember;
class Variable;
} // namespace ast
namespace type {
class Array;
class Struct;
} // namespace type
namespace semantic {
// Forward declarations
class Array;
class Call;
class Expression;
class Function;
class MemberAccessorExpression;
class Struct;
class StructMember;
class Variable;
/// TypeMappings is a struct that holds dummy `operator()` methods that's used
/// by SemanticNodeTypeFor to map AST node types to their corresponding semantic
/// node types.
/// The standard operator overload resolving rules will be used to infer the
/// return type based on the argument type.
/// by SemanticNodeTypeFor to map AST / type node types to their corresponding
/// semantic node types. The standard operator overload resolving rules will be
/// used to infer the return type based on the argument type.
struct TypeMappings {
//! @cond Doxygen_Suppress
semantic::Expression* operator()(ast::Expression*);
semantic::Function* operator()(ast::Function*);
semantic::Variable* operator()(ast::Variable*);
semantic::Call* operator()(ast::CallExpression*);
semantic::MemberAccessorExpression* operator()(
ast::MemberAccessorExpression*);
Array* operator()(type::Array*);
Call* operator()(ast::CallExpression*);
Expression* operator()(ast::Expression*);
Function* operator()(ast::Function*);
MemberAccessorExpression* operator()(ast::MemberAccessorExpression*);
Struct* operator()(type::Struct*);
StructMember* operator()(ast::StructMember*);
Variable* operator()(ast::Variable*);
//! @endcond
};
/// SemanticNodeTypeFor resolves to the appropriate semantic::Node type for the
/// AST node type `AST`.
template <typename AST>
/// AST or type node `AST_OR_TYPE`.
template <typename AST_OR_TYPE>
using SemanticNodeTypeFor = typename std::remove_pointer<decltype(
TypeMappings()(std::declval<AST*>()))>::type;
TypeMappings()(std::declval<AST_OR_TYPE*>()))>::type;
} // namespace semantic
} // namespace tint