aboutsummaryrefslogtreecommitdiff
path: root/source/val
diff options
context:
space:
mode:
authorEhsan Nasiri <ehsann@google.com>2017-01-11 10:51:23 -0500
committerDavid Neto <dneto@google.com>2017-01-16 16:52:08 -0500
commitfcf7df069d9ed00c42e00ae2717107f9772cb426 (patch)
tree129fdacbe55d94c50d44e16304f63b98e564a171 /source/val
parent1c11c8690f6b8dd3a396447bca8a180e468156a7 (diff)
downloadspirv-tools-fcf7df069d9ed00c42e00ae2717107f9772cb426.tar.gz
Adding decoration class and tests.
* Added the decoration class as well as the code that registers the decorations for each <id> and also decorations for struct members. * Added unit tests for decorations in ValidationState as well as decoration id tests.
Diffstat (limited to 'source/val')
-rw-r--r--source/val/decoration.h84
-rw-r--r--source/val/validation_state.h35
2 files changed, 119 insertions, 0 deletions
diff --git a/source/val/decoration.h b/source/val/decoration.h
new file mode 100644
index 00000000..161dada5
--- /dev/null
+++ b/source/val/decoration.h
@@ -0,0 +1,84 @@
+// Copyright (c) 2017 Google Inc.
+//
+// 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 LIBSPIRV_VAL_DECORATION_H_
+#define LIBSPIRV_VAL_DECORATION_H_
+
+#include <unordered_map>
+#include <vector>
+
+namespace libspirv {
+
+// An object of this class represents a specific decoration including its
+// parameters (if any). Decorations are used by OpDecorate and OpMemberDecorate,
+// and they describe certain properties that can be assigned to one or several
+// <id>s.
+//
+// A Decoration object contains the decoration type (an enum), associated
+// literal parameters, and struct member index. If the decoration does not apply
+// to a struct member, then the index is kInvalidIndex. A Decoration object does
+// not store the target Id, i.e. the Id to which it applies. It is
+// possible for the same decoration to be applied to several <id>s (and they
+// might be assigned using separate SPIR-V instructions, possibly using an
+// assignment through GroupDecorate).
+//
+// Example 1: Decoration for an object<id> with no parameters:
+// OpDecorate %obj Flat
+// dec_type_ = SpvDecorationFlat
+// params_ = empty vector
+// struct_member_index_ = kInvalidMember
+//
+// Example 2: Decoration for an object<id> with two parameters:
+// OpDecorate %obj LinkageAttributes "link" Import
+// dec_type_ = SpvDecorationLinkageAttributes
+// params_ = vector { link, Import }
+// struct_member_index_ = kInvalidMember
+//
+// Example 3: Decoration for a member of a structure with one parameter:
+// OpMemberDecorate %struct 2 Offset 2
+// dec_type_ = SpvDecorationOffset
+// params_ = vector { 2 }
+// struct_member_index_ = 2
+//
+class Decoration {
+ public:
+ enum { kInvalidMember = -1 };
+ Decoration(SpvDecoration t,
+ const std::vector<uint32_t>& parameters = std::vector<uint32_t>(),
+ uint32_t member_index = kInvalidMember)
+ : dec_type_(t), params_(parameters), struct_member_index_(member_index) {}
+
+ void set_struct_member_index(uint32_t index) { struct_member_index_ = index; }
+ int struct_member_index() { return struct_member_index_; }
+ SpvDecoration dec_type() { return dec_type_; }
+ std::vector<uint32_t>& params() { return params_; }
+
+ inline bool operator==(const Decoration& rhs) const {
+ return (dec_type_ == rhs.dec_type_ && params_ == rhs.params_ &&
+ struct_member_index_ == rhs.struct_member_index_);
+ }
+
+ private:
+ SpvDecoration dec_type_;
+ std::vector<uint32_t> params_;
+
+ // If the decoration applies to a member of a structure type, then the index
+ // of the member is stored here. Otherwise, this is kInvalidIndex.
+ int struct_member_index_;
+};
+
+} // namespace libspirv
+
+#endif /// LIBSPIRV_VAL_DECORATION_H_
+
diff --git a/source/val/validation_state.h b/source/val/validation_state.h
index a4a41406..5bdcac22 100644
--- a/source/val/validation_state.h
+++ b/source/val/validation_state.h
@@ -22,6 +22,7 @@
#include <vector>
#include "assembly_grammar.h"
+#include "decoration.h"
#include "diagnostic.h"
#include "enum_set.h"
#include "spirv-tools/libspirv.h"
@@ -177,6 +178,37 @@ class ValidationState_t {
/// Registers the instruction
void RegisterInstruction(const spv_parsed_instruction_t& inst);
+ /// Registers the decoration for the given <id>
+ void RegisterDecorationForId(uint32_t id, const Decoration& dec) {
+ id_decorations_[id].push_back(dec);
+ }
+
+ /// Registers the list of decorations for the given <id>
+ template <class InputIt>
+ void RegisterDecorationsForId(uint32_t id, InputIt begin, InputIt end) {
+ std::vector<Decoration>& cur_decs = id_decorations_[id];
+ cur_decs.insert(cur_decs.end(), begin, end);
+ }
+
+ /// Registers the list of decorations for the given member of the given
+ /// structure.
+ template <class InputIt>
+ void RegisterDecorationsForStructMember(uint32_t struct_id,
+ uint32_t member_index, InputIt begin,
+ InputIt end) {
+ RegisterDecorationsForId(struct_id, begin, end);
+ for (auto& decoration : id_decorations_[struct_id]) {
+ decoration.set_struct_member_index(member_index);
+ }
+ }
+
+ /// Returns all the decorations for the given <id>. If no decorations exist
+ /// for the <id>, it registers an empty vector for it in the map and
+ /// returns the empty vector.
+ std::vector<Decoration>& id_decorations(uint32_t id) {
+ return id_decorations_[id];
+ }
+
/// Finds id's def, if it exists. If found, returns the definition otherwise
/// nullptr
const Instruction* FindDef(uint32_t id) const;
@@ -280,6 +312,9 @@ class ValidationState_t {
/// Structure Nesting Depth
std::unordered_map<uint32_t, uint32_t> struct_nesting_depth_;
+ /// Stores the list of decorations for a given <id>
+ std::unordered_map<uint32_t, std::vector<Decoration>> id_decorations_;
+
AssemblyGrammar grammar_;
SpvAddressingModel addressing_model_;