[wgsl-reader] Add support for read only storage buffers.

This Cl adds the necessary infrastructure to parse the access decoration
for storage buffers.

This CL incorporates changes from bclayton@ from
https://dawn-review.googlesource.com/c/tint/+/33202 and
https://dawn-review.googlesource.com/c/tint/+/33201

Bug: tint:287
Change-Id: I7479f2cf7ab794b24c682b9927c4c68f6d325839
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33161
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Auto-Submit: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair
2020-11-19 18:55:01 +00:00
committed by Commit Bot service account
parent d2f73226bc
commit 7214f407dc
15 changed files with 573 additions and 30 deletions

View File

@@ -0,0 +1,41 @@
// Copyright 2020 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/ast/access_decoration.h"
namespace tint {
namespace ast {
constexpr const DecorationKind AccessDecoration::Kind;
AccessDecoration::AccessDecoration(AccessControl val, const Source& source)
: TypeDecoration(Kind, source), value_(val) {}
AccessDecoration::~AccessDecoration() = default;
bool AccessDecoration::IsKind(DecorationKind kind) const {
return kind == Kind || TypeDecoration::IsKind(kind);
}
bool AccessDecoration::IsAccess() const {
return true;
}
void AccessDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "AccessDecoration{" << value_ << "}" << std::endl;
}
} // namespace ast
} // namespace tint

View File

@@ -0,0 +1,61 @@
// Copyright 2020 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_AST_ACCESS_DECORATION_H_
#define SRC_AST_ACCESS_DECORATION_H_
#include <stddef.h>
#include "src/ast/access_control.h"
#include "src/ast/type_decoration.h"
namespace tint {
namespace ast {
/// An access decoration
class AccessDecoration : public TypeDecoration {
public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind = DecorationKind::kAccess;
/// constructor
/// @param value the access value
/// @param source the source of this decoration
explicit AccessDecoration(AccessControl value, const Source& source);
~AccessDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is an access decoration
bool IsAccess() const override;
/// @returns the access control value
AccessControl value() const { return value_; }
/// Outputs the decoration to the given stream
/// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private:
AccessControl value_ = AccessControl::kReadWrite;
};
} // namespace ast
} // namespace tint
#endif // SRC_AST_ACCESS_DECORATION_H_

View File

@@ -0,0 +1,47 @@
// Copyright 2020 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/ast/access_decoration.h"
#include <sstream>
#include "src/ast/test_helper.h"
namespace tint {
namespace ast {
namespace {
using AccessDecorationTest = TestHelper;
TEST_F(AccessDecorationTest, Creation) {
AccessDecoration d{AccessControl::kWriteOnly, Source{}};
EXPECT_EQ(AccessControl::kWriteOnly, d.value());
}
TEST_F(AccessDecorationTest, Is) {
AccessDecoration d{AccessControl::kReadWrite, Source{}};
EXPECT_FALSE(d.IsAccess());
}
TEST_F(AccessDecorationTest, ToStr) {
AccessDecoration d{AccessControl::kReadOnly, Source{}};
std::ostringstream out;
d.to_str(out, 0);
EXPECT_EQ(out.str(), R"(AccessDecoration{read}
)");
}
} // namespace
} // namespace ast
} // namespace tint

View File

@@ -37,6 +37,10 @@ std::ostream& operator<<(std::ostream& out, DecorationKind data) {
return out << "struct member";
case DecorationKind::kStructMemberOffset:
return out << "offset";
case DecorationKind::kType:
return out << "type";
case DecorationKind::kAccess:
return out << "access";
case DecorationKind::kVariable:
return out << "variable";
case DecorationKind::kBinding:

View File

@@ -35,6 +35,8 @@ enum class DecorationKind {
kStruct,
kStructMember,
/*|*/ kStructMemberOffset,
kType,
/*|*/ kAccess,
kVariable,
/*|*/ kBinding,
/*|*/ kBuiltin,

View File

@@ -19,6 +19,7 @@
#include <unordered_set>
#include <utility>
#include "src/ast/access_decoration.h"
#include "src/ast/array_decoration.h"
#include "src/ast/binding_decoration.h"
#include "src/ast/builtin_decoration.h"
@@ -31,6 +32,7 @@
#include "src/ast/struct_member_decoration.h"
#include "src/ast/struct_member_offset_decoration.h"
#include "src/ast/test_helper.h"
#include "src/ast/type_decoration.h"
#include "src/ast/variable_decoration.h"
#include "src/ast/workgroup_decoration.h"
@@ -70,6 +72,8 @@ TEST_F(DecorationTest, Kinds) {
EXPECT_EQ(StructMemberDecoration::Kind, DecorationKind::kStructMember);
EXPECT_EQ(StructMemberOffsetDecoration::Kind,
DecorationKind::kStructMemberOffset);
EXPECT_EQ(TypeDecoration::Kind, DecorationKind::kType);
EXPECT_EQ(AccessDecoration::Kind, DecorationKind::kAccess);
EXPECT_EQ(VariableDecoration::Kind, DecorationKind::kVariable);
EXPECT_EQ(BindingDecoration::Kind, DecorationKind::kBinding);
EXPECT_EQ(BuiltinDecoration::Kind, DecorationKind::kBuiltin);
@@ -83,6 +87,7 @@ TEST_F(DecorationTest, IsKind) {
DecorationKind::kFunction, DecorationKind::kStage,
DecorationKind::kWorkgroup, DecorationKind::kStruct,
DecorationKind::kStructMember, DecorationKind::kStructMemberOffset,
DecorationKind::kType, DecorationKind::kAccess,
DecorationKind::kVariable, DecorationKind::kBinding,
DecorationKind::kBuiltin, DecorationKind::kConstantId,
DecorationKind::kLocation,
@@ -101,6 +106,8 @@ TEST_F(DecorationTest, IsKind) {
// kStruct
// kStructMember
// | kStructMemberOffset
// kType
// | kAccess
// kVariable
// | kBinding
// | kBuiltin
@@ -129,6 +136,10 @@ TEST_F(DecorationTest, IsKind) {
{DecorationKind::kStructMember,
DecorationKind::kStructMemberOffset},
},
{
DecorationKind::kAccess,
{DecorationKind::kType, DecorationKind::kAccess},
},
{
DecorationKind::kBinding,
{DecorationKind::kVariable, DecorationKind::kBinding},
@@ -159,6 +170,7 @@ TEST_F(DecorationTest, IsKind) {
StageDecoration stage(PipelineStage::kNone, {});
WorkgroupDecoration workgroup(0, {});
StructMemberOffsetDecoration struct_member_offset(0, {});
AccessDecoration access(AccessControl::kReadOnly, {});
BindingDecoration binding(0, {});
BuiltinDecoration builtin(Builtin::kNone, {});
ConstantIdDecoration constant_id(0, {});
@@ -168,6 +180,7 @@ TEST_F(DecorationTest, IsKind) {
check(&stage);
check(&workgroup);
check(&struct_member_offset);
check(&access);
check(&binding);
check(&builtin);
check(&constant_id);

View File

@@ -0,0 +1,45 @@
// Copyright 2020 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/ast/type_decoration.h"
#include <assert.h>
#include "src/ast/access_decoration.h"
namespace tint {
namespace ast {
constexpr const DecorationKind TypeDecoration::Kind;
TypeDecoration::TypeDecoration(DecorationKind kind, const Source& source)
: Decoration(kind, source) {}
TypeDecoration::~TypeDecoration() = default;
bool TypeDecoration::IsKind(DecorationKind kind) const {
return kind == Kind;
}
bool TypeDecoration::IsAccess() const {
return false;
}
AccessDecoration* TypeDecoration::AsAccess() {
assert(IsAccess());
return static_cast<AccessDecoration*>(this);
}
} // namespace ast
} // namespace tint

62
src/ast/type_decoration.h Normal file
View File

@@ -0,0 +1,62 @@
// Copyright 2020 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_AST_TYPE_DECORATION_H_
#define SRC_AST_TYPE_DECORATION_H_
#include <memory>
#include <ostream>
#include <string>
#include <vector>
#include "src/ast/decoration.h"
namespace tint {
namespace ast {
class AccessDecoration;
/// A decoration attached to a type
class TypeDecoration : public Decoration {
public:
/// The kind of decoration that this type represents
static constexpr const DecorationKind Kind = DecorationKind::kType;
~TypeDecoration() override;
/// @param kind the decoration kind
/// @return true if this Decoration is of the (or derives from) the given
/// kind.
bool IsKind(DecorationKind kind) const override;
/// @returns true if this is an access decoration
virtual bool IsAccess() const;
/// @returns the decoration as an access decoration
AccessDecoration* AsAccess();
protected:
/// Constructor
/// @param kind the decoration kind
/// @param source the source of this decoration
explicit TypeDecoration(DecorationKind kind, const Source& source);
};
/// A list of type decorations
using TypeDecorationList = std::vector<TypeDecoration*>;
} // namespace ast
} // namespace tint
#endif // SRC_AST_TYPE_DECORATION_H_