Explorar el Código

Updated spirv-tools.

Бранимир Караџић hace 6 años
padre
commit
ace4142b94
Se han modificado 37 ficheros con 2761 adiciones y 2419 borrados
  1. 14 1
      3rdparty/spirv-tools/CHANGES
  2. 1 1
      3rdparty/spirv-tools/DEPS
  3. 1 1
      3rdparty/spirv-tools/include/generated/build-version.inc
  4. 5 0
      3rdparty/spirv-tools/source/fuzz/CMakeLists.txt
  5. 13 1
      3rdparty/spirv-tools/source/fuzz/data_descriptor.cpp
  6. 7 1
      3rdparty/spirv-tools/source/fuzz/data_descriptor.h
  7. 215 0
      3rdparty/spirv-tools/source/fuzz/equivalence_relation.h
  8. 59 64
      3rdparty/spirv-tools/source/fuzz/fact_manager.cpp
  9. 14 7
      3rdparty/spirv-tools/source/fuzz/fact_manager.h
  10. 9 5
      3rdparty/spirv-tools/source/fuzz/fuzzer.cpp
  11. 4 0
      3rdparty/spirv-tools/source/fuzz/fuzzer_context.cpp
  12. 4 0
      3rdparty/spirv-tools/source/fuzz/fuzzer_context.h
  13. 113 0
      3rdparty/spirv-tools/source/fuzz/fuzzer_pass_adjust_memory_operands_masks.cpp
  14. 40 0
      3rdparty/spirv-tools/source/fuzz/fuzzer_pass_adjust_memory_operands_masks.h
  15. 1 1
      3rdparty/spirv-tools/source/fuzz/fuzzer_pass_apply_id_synonyms.cpp
  16. 51 16
      3rdparty/spirv-tools/source/fuzz/fuzzer_pass_construct_composites.cpp
  17. 48 0
      3rdparty/spirv-tools/source/fuzz/fuzzer_util.cpp
  18. 8 0
      3rdparty/spirv-tools/source/fuzz/fuzzer_util.h
  19. 35 0
      3rdparty/spirv-tools/source/fuzz/instruction_descriptor.cpp
  20. 9 0
      3rdparty/spirv-tools/source/fuzz/instruction_descriptor.h
  21. 32 11
      3rdparty/spirv-tools/source/fuzz/protobufs/spvtoolsfuzz.proto
  22. 4 0
      3rdparty/spirv-tools/source/fuzz/transformation.cpp
  23. 14 9
      3rdparty/spirv-tools/source/fuzz/transformation_construct_composite.cpp
  24. 4 5
      3rdparty/spirv-tools/source/fuzz/transformation_copy_object.cpp
  25. 2 2
      3rdparty/spirv-tools/source/fuzz/transformation_replace_id_with_synonym.cpp
  26. 201 0
      3rdparty/spirv-tools/source/fuzz/transformation_set_memory_operands_mask.cpp
  27. 76 0
      3rdparty/spirv-tools/source/fuzz/transformation_set_memory_operands_mask.h
  28. 30 23
      3rdparty/spirv-tools/source/opt/instrument_pass.cpp
  29. 3 1
      3rdparty/spirv-tools/test/fuzz/CMakeLists.txt
  30. 98 0
      3rdparty/spirv-tools/test/fuzz/equivalence_relation_test.cpp
  31. 667 18
      3rdparty/spirv-tools/test/fuzz/fuzzer_replayer_test.cpp
  32. 69 0
      3rdparty/spirv-tools/test/fuzz/instruction_descriptor_test.cpp
  33. 4 3
      3rdparty/spirv-tools/test/fuzz/transformation_construct_composite_test.cpp
  34. 65 55
      3rdparty/spirv-tools/test/fuzz/transformation_copy_object_test.cpp
  35. 0 2194
      3rdparty/spirv-tools/test/fuzz/transformation_replace_id_with_synonym_test.cpp
  36. 432 0
      3rdparty/spirv-tools/test/fuzz/transformation_set_memory_operands_mask_test.cpp
  37. 409 0
      3rdparty/spirv-tools/test/opt/inst_bindless_check_test.cpp

+ 14 - 1
3rdparty/spirv-tools/CHANGES

@@ -1,11 +1,17 @@
 Revision history for SPIRV-Tools
 
-v2019.5-dev 2019-10-09
+v2019.5-dev 2019-10-21
  - General:
    - Export SPIRV-Tools targets on installation
    - SPIRV-Tools support for SPIR-V 1.5 (#2865)
    - Add WebGPU SPIR-V Assembler in JavaScript. (#2876)
    - Add Bazel build configuration. (#2891)
+   - Add support for building with emscripten (#2948)
+   - Update SPIR-V binary header test for SPIR-V 1.5 (#2967)
+   - Add fuzzer for spirv-as call path (#2976)
+   - Improved CMake install step. (#2963)
+   - Add fuzzer for spirv-dis call path (#2977)
+   - Ensure timestamp does not vary with timezone. (#2982)
  - Optimizer
    - Add descriptor array scalar replacement (#2742)
    - Add pass to wrap OpKill in a function call (#2790)
@@ -17,6 +23,7 @@ v2019.5-dev 2019-10-09
    - Fold Min, Max, and Clamp instructions. (#2836)
    - Better handling of OpKill in continues (#2842,#2922,#2933)
    - Enable OpTypeCooperativeMatrix specialization (#2927)
+   - Support constant-folding UConvert and SConvert (#2960)
    Fixes:
    - Instrument: Fix version 2 output record write for tess eval shaders. (#2782)
    - Instrument: Add support for Buffer Device Address extension (#2792)
@@ -31,6 +38,9 @@ v2019.5-dev 2019-10-09
    - Relaxed bitcast with pointers (#2878)
    - Validate physical storage buffer restrictions (#2930)
    - Add SPV_KHR_shader_clock validation (#2879)
+   - Validate that selections are structured (#2962)
+   - Disallow use of OpCompositeExtract/OpCompositeInsert with no indices (#2980)
+   - Check that derivatives operate on 32-bit values (#2983)
    Fixes:
    - Fix validation of constant matrices (#2794)
    - Update "remquor" validation
@@ -50,6 +60,9 @@ v2019.5-dev 2019-10-09
    - option to convert shader into a form that renders red (#2934)
    - Add fuzzer pass to change selection controls (#2944)
    - add transformation and pass to construct composites (#2941)
+   - Add fuzzer pass to change loop controls (#2949)
+   - Add fuzzer pass to change function controls (#2951)
+   - Add fuzzer pass to add NoContraction decorations (#2950)
 
 
 v2019.4 2019-08-08

+ 1 - 1
3rdparty/spirv-tools/DEPS

@@ -6,7 +6,7 @@ vars = {
   'effcee_revision': 'cd25ec17e9382f99a895b9ef53ff3c277464d07d',
   'googletest_revision': 'f2fb48c3b3d79a75a88a99fba6576b25d42ec528',
   're2_revision': '5bd613749fd530b576b890283bfb6bc6ea6246cb',
-  'spirv_headers_revision': '842ec90674627ed2ffef609e3cd79d1562eded01',
+  'spirv_headers_revision': 'af64a9e826bf5bb5fcd2434dd71be1e41e922563',
 }
 
 deps = {

+ 1 - 1
3rdparty/spirv-tools/include/generated/build-version.inc

@@ -1 +1 @@
-"v2019.5-dev", "SPIRV-Tools v2019.5-dev v2019.4-133-ge8c3f9b0"
+"v2019.5-dev", "SPIRV-Tools v2019.5-dev v2019.4-140-g0dbd4e35"

+ 5 - 0
3rdparty/spirv-tools/source/fuzz/CMakeLists.txt

@@ -30,6 +30,7 @@ if(SPIRV_BUILD_FUZZER)
 
   set(SPIRV_TOOLS_FUZZ_SOURCES
         data_descriptor.h
+        equivalence_relation.h
         fact_manager.h
         force_render_red.h
         fuzzer.h
@@ -41,6 +42,7 @@ if(SPIRV_BUILD_FUZZER)
         fuzzer_pass_add_useful_constructs.h
         fuzzer_pass_adjust_function_controls.h
         fuzzer_pass_adjust_loop_controls.h
+        fuzzer_pass_adjust_memory_operands_masks.h
         fuzzer_pass_adjust_selection_controls.h
         fuzzer_pass_apply_id_synonyms.h
         fuzzer_pass_construct_composites.h
@@ -74,6 +76,7 @@ if(SPIRV_BUILD_FUZZER)
         transformation_replace_id_with_synonym.h
         transformation_set_function_control.h
         transformation_set_loop_control.h
+        transformation_set_memory_operands_mask.h
         transformation_set_selection_control.h
         transformation_split_block.h
         uniform_buffer_element_descriptor.h
@@ -91,6 +94,7 @@ if(SPIRV_BUILD_FUZZER)
         fuzzer_pass_add_useful_constructs.cpp
         fuzzer_pass_adjust_function_controls.cpp
         fuzzer_pass_adjust_loop_controls.cpp
+        fuzzer_pass_adjust_memory_operands_masks.cpp
         fuzzer_pass_adjust_selection_controls.cpp
         fuzzer_pass_apply_id_synonyms.cpp
         fuzzer_pass_construct_composites.cpp
@@ -123,6 +127,7 @@ if(SPIRV_BUILD_FUZZER)
         transformation_replace_id_with_synonym.cpp
         transformation_set_function_control.cpp
         transformation_set_loop_control.cpp
+        transformation_set_memory_operands_mask.cpp
         transformation_set_selection_control.cpp
         transformation_split_block.cpp
         uniform_buffer_element_descriptor.cpp

+ 13 - 1
3rdparty/spirv-tools/source/fuzz/data_descriptor.cpp

@@ -20,15 +20,27 @@ namespace spvtools {
 namespace fuzz {
 
 protobufs::DataDescriptor MakeDataDescriptor(uint32_t object,
-                                             std::vector<uint32_t>&& indices) {
+                                             std::vector<uint32_t>&& indices,
+                                             uint32_t num_contiguous_elements) {
   protobufs::DataDescriptor result;
   result.set_object(object);
   for (auto index : indices) {
     result.add_index(index);
   }
+  result.set_num_contiguous_elements(num_contiguous_elements);
   return result;
 }
 
+size_t DataDescriptorHash::operator()(
+    const protobufs::DataDescriptor* data_descriptor) const {
+  std::u32string hash;
+  hash.push_back(data_descriptor->object());
+  for (auto an_index : data_descriptor->index()) {
+    hash.push_back(an_index);
+  }
+  return std::hash<std::u32string>()(hash);
+}
+
 bool DataDescriptorEquals::operator()(
     const protobufs::DataDescriptor* first,
     const protobufs::DataDescriptor* second) const {

+ 7 - 1
3rdparty/spirv-tools/source/fuzz/data_descriptor.h

@@ -25,7 +25,13 @@ namespace fuzz {
 // Factory method to create a data descriptor message from an object id and a
 // list of indices.
 protobufs::DataDescriptor MakeDataDescriptor(uint32_t object,
-                                             std::vector<uint32_t>&& indices);
+                                             std::vector<uint32_t>&& indices,
+                                             uint32_t num_contiguous_elements);
+
+// Hash function for data descriptors.
+struct DataDescriptorHash {
+  size_t operator()(const protobufs::DataDescriptor* data_descriptor) const;
+};
 
 // Equality function for data descriptors.
 struct DataDescriptorEquals {

+ 215 - 0
3rdparty/spirv-tools/source/fuzz/equivalence_relation.h

@@ -0,0 +1,215 @@
+// Copyright (c) 2019 Google LLC
+//
+// 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 SOURCE_FUZZ_EQUIVALENCE_RELATION_H_
+#define SOURCE_FUZZ_EQUIVALENCE_RELATION_H_
+
+#include <memory>
+#include <unordered_map>
+#include <unordered_set>
+#include <vector>
+
+#include "source/util/make_unique.h"
+
+namespace spvtools {
+namespace fuzz {
+
+// A class for representing an equivalence relation on objects of type |T|,
+// which should be a value type.  The type |T| is required to have a copy
+// constructor, and |PointerHashT| and |PointerEqualsT| must be functors
+// providing hashing and equality testing functionality for pointers to objects
+// of type |T|.
+//
+// A disjoint-set (a.k.a. union-find or merge-find) data structure is used to
+// represent the equivalence relation.  Path compression is used.  Union by
+// rank/size is not used.
+//
+// Each disjoint set is represented as a tree, rooted at the representative
+// of the set.
+//
+// Getting the representative of a value simply requires chasing parent pointers
+// from the value until you reach the root.
+//
+// Checking equivalence of two elements requires checking that the
+// representatives are equal.
+//
+// Traversing the tree rooted at a value's representative visits the value's
+// equivalence class.
+//
+// |PointerHashT| and |PointerEqualsT| are used to define *equality* between
+// values, and otherwise are *not* used to define the equivalence relation
+// (except that equal values are equivalent).  The equivalence relation is
+// constructed by repeatedly adding pairs of (typically non-equal) values that
+// are deemed to be equivalent.
+//
+// For example in an equivalence relation on integers, 1 and 5 might be added
+// as equivalent, so that IsEquivalent(1, 5) holds, because they represent
+// IDs in a SPIR-V binary that are known to contain the same value at run time,
+// but clearly 1 != 5.  Since 1 and 1 are equal, IsEquivalent(1, 1) will also
+// hold.
+//
+// Each unique (up to equality) value added to the relation is copied into
+// |owned_values_|, so there is one canonical memory address per unique value.
+// Uniqueness is ensured by storing (and checking) a set of pointers to these
+// values in |value_set_|, which uses |PointerHashT| and |PointerEqualsT|.
+//
+// |parent_| and |children_| encode the equivalence relation, i.e., the trees.
+template <typename T, typename PointerHashT, typename PointerEqualsT>
+class EquivalenceRelation {
+ public:
+  using ValueSet = std::unordered_set<const T*, PointerHashT, PointerEqualsT>;
+
+  // Merges the equivalence classes associated with |value1| and |value2|.
+  // If any of these values was not previously in the equivalence relation, it
+  // is added to the pool of values known to be in the relation.
+  void MakeEquivalent(const T& value1, const T& value2) {
+    // Register each value if necessary.
+    for (auto value : {value1, value2}) {
+      if (!Exists(value)) {
+        // Register the value in the equivalence relation.  This relies on
+        // T having a copy constructor.
+        auto unique_pointer_to_value = MakeUnique<T>(value);
+        auto pointer_to_value = unique_pointer_to_value.get();
+        owned_values_.push_back(std::move(unique_pointer_to_value));
+        value_set_.insert(pointer_to_value);
+
+        // Initially say that the value is its own parent and that it has no
+        // children.
+        parent_[pointer_to_value] = pointer_to_value;
+        children_[pointer_to_value] = std::unordered_set<const T*>();
+      }
+    }
+
+    // Look up canonical pointers to each of the values in the value pool.
+    const T* value1_ptr = *value_set_.find(&value1);
+    const T* value2_ptr = *value_set_.find(&value2);
+
+    // If the values turn out to be identical, they are already in the same
+    // equivalence class so there is nothing to do.
+    if (value1_ptr == value2_ptr) {
+      return;
+    }
+
+    // Find the representative for each value's equivalence class, and if they
+    // are not already in the same class, make one the parent of the other.
+    const T* representative1 = Find(value1_ptr);
+    const T* representative2 = Find(value2_ptr);
+    if (representative1 != representative2) {
+      parent_[representative1] = representative2;
+      children_[representative2].insert(representative1);
+    }
+  }
+
+  // Returns pointers to all values in the equivalence class of |value|, which
+  // must already be part of the equivalence relation.
+  ValueSet GetEquivalenceClass(const T& value) const {
+    assert(Exists(value));
+
+    ValueSet result;
+
+    // Traverse the tree of values rooted at the representative of the
+    // equivalence class to which |value| belongs, and collect up all the values
+    // that are encountered.  This constitutes the whole equivalence class.
+    std::vector<const T*> stack;
+    stack.push_back(Find(*value_set_.find(&value)));
+    while (!stack.empty()) {
+      const T* item = stack.back();
+      result.insert(item);
+      stack.pop_back();
+      for (auto child : children_[item]) {
+        stack.push_back(child);
+      }
+    }
+    return result;
+  }
+
+  // Returns true if and only if |value1| and |value2| are in the same
+  // equivalence class.  Both values must already be known to the equivalence
+  // relation.
+  bool IsEquivalent(const T& value1, const T& value2) const {
+    return Find(&value1) == Find(&value2);
+  }
+
+  // Returns the set of all values known to be part of the equivalence relation.
+  ValueSet GetAllKnownValues() const {
+    ValueSet result;
+    for (auto& value : owned_values_) {
+      result.insert(value.get());
+    }
+    return result;
+  }
+
+ private:
+  // Returns true if and only if |value| is known to be part of the equivalence
+  // relation.
+  bool Exists(const T& value) const {
+    return value_set_.find(&value) != value_set_.end();
+  }
+
+  // Returns the representative of the equivalence class of |value|, which must
+  // already be known to the equivalence relation.  This is the 'Find' operation
+  // in a classic union-find data structure.
+  const T* Find(const T* value) const {
+    assert(Exists(*value));
+
+    // Compute the result by chasing parents until we find a value that is its
+    // own parent.
+    const T* result = value;
+    while (parent_[result] != result) {
+      result = parent_[result];
+    }
+
+    // At this point, |result| is the representative of the equivalence class.
+    // Now perform the 'path compression' optimization by doing another pass up
+    // the parent chain, setting the parent of each node to be the
+    // representative, and rewriting children correspondingly.
+    const T* current = value;
+    while (parent_[current] != result) {
+      const T* next = parent_[current];
+      parent_[current] = result;
+      children_[result].insert(current);
+      children_[next].erase(current);
+      current = next;
+    }
+    return result;
+  }
+
+  // Maps every value to a parent.  The representative of an equivalence class
+  // is its own parent.  A value's representative can be found by walking its
+  // chain of ancestors.
+  //
+  // Mutable because the intuitively const method, 'Find', performs path
+  // compression.
+  mutable std::unordered_map<const T*, const T*> parent_;
+
+  // Stores the children of each value.  This allows the equivalence class of
+  // a value to be calculated by traversing all descendents of the class's
+  // representative.
+  //
+  // Mutable because the intuitively const method, 'Find', performs path
+  // compression.
+  mutable std::unordered_map<const T*, std::unordered_set<const T*>> children_;
+
+  // The values known to the equivalence relation are alloacated in
+  // |owned_values_|, and |value_pool_| provides (via |PointerHashT| and
+  // |PointerEqualsT|) a means for mapping a value of interest to a pointer
+  // into an equivalent value in |owned_values_|.
+  ValueSet value_set_;
+  std::vector<std::unique_ptr<T>> owned_values_;
+};
+
+}  // namespace fuzz
+}  // namespace spvtools
+
+#endif  // SOURCE_FUZZ_EQUIVALENCE_RELATION_H_

+ 59 - 64
3rdparty/spirv-tools/source/fuzz/fact_manager.cpp

@@ -14,8 +14,12 @@
 
 #include "source/fuzz/fact_manager.h"
 
+#include <map>
 #include <sstream>
+#include <unordered_set>
 
+#include "source/fuzz/equivalence_relation.h"
+#include "source/fuzz/fuzzer_util.h"
 #include "source/fuzz/uniform_buffer_element_descriptor.h"
 #include "source/opt/ir_context.h"
 
@@ -276,42 +280,20 @@ bool FactManager::ConstantUniformFacts::AddFact(
   }
   auto should_be_uniform_pointer_instruction =
       context->get_def_use_mgr()->GetDef(uniform_variable->type_id());
-  auto element_type =
+  auto composite_type =
       should_be_uniform_pointer_instruction->GetSingleWordInOperand(1);
 
-  for (auto index : fact.uniform_buffer_element_descriptor().index()) {
-    auto should_be_composite_type =
-        context->get_def_use_mgr()->GetDef(element_type);
-    if (SpvOpTypeStruct == should_be_composite_type->opcode()) {
-      if (index >= should_be_composite_type->NumInOperands()) {
-        return false;
-      }
-      element_type = should_be_composite_type->GetSingleWordInOperand(index);
-    } else if (SpvOpTypeArray == should_be_composite_type->opcode()) {
-      auto array_length_constant =
-          context->get_constant_mgr()
-              ->GetConstantFromInst(context->get_def_use_mgr()->GetDef(
-                  should_be_composite_type->GetSingleWordInOperand(1)))
-              ->AsIntConstant();
-      if (array_length_constant->words().size() != 1) {
-        return false;
-      }
-      auto array_length = array_length_constant->GetU32();
-      if (index >= array_length) {
-        return false;
-      }
-      element_type = should_be_composite_type->GetSingleWordInOperand(0);
-    } else if (SpvOpTypeVector == should_be_composite_type->opcode()) {
-      auto vector_length = should_be_composite_type->GetSingleWordInOperand(1);
-      if (index >= vector_length) {
-        return false;
-      }
-      element_type = should_be_composite_type->GetSingleWordInOperand(0);
-    } else {
-      return false;
-    }
+  auto final_element_type_id = fuzzerutil::WalkCompositeIndices(
+      context, composite_type,
+      fact.uniform_buffer_element_descriptor().index());
+  if (!final_element_type_id) {
+    return false;
   }
-  auto final_element_type = context->get_type_mgr()->GetType(element_type);
+  auto final_element_type =
+      context->get_type_mgr()->GetType(final_element_type_id);
+  assert(final_element_type &&
+         "There should be a type corresponding to this id.");
+
   if (!(final_element_type->AsFloat() || final_element_type->AsInteger())) {
     return false;
   }
@@ -329,7 +311,8 @@ bool FactManager::ConstantUniformFacts::AddFact(
     return false;
   }
   facts_and_type_ids.emplace_back(
-      std::pair<protobufs::FactConstantUniform, uint32_t>(fact, element_type));
+      std::pair<protobufs::FactConstantUniform, uint32_t>(
+          fact, final_element_type_id));
   return true;
 }
 
@@ -337,39 +320,30 @@ bool FactManager::ConstantUniformFacts::AddFact(
 //==============================
 
 //==============================
-// Id synonym facts
+// Data synonym facts
 
 // The purpose of this struct is to group the fields and data used to represent
-// facts about id synonyms.
-struct FactManager::IdSynonymFacts {
+// facts about data synonyms.
+struct FactManager::DataSynonymFacts {
   // See method in FactManager which delegates to this method.
-  void AddFact(const protobufs::FactIdSynonym& fact);
-
-  // A record of all the synonyms that are available.
-  std::map<uint32_t, std::vector<protobufs::DataDescriptor>> synonyms;
+  void AddFact(const protobufs::FactDataSynonym& fact);
 
-  // The set of keys to the above map; useful if you just want to know which ids
-  // have synonyms.
-  std::set<uint32_t> ids_with_synonyms;
+  EquivalenceRelation<protobufs::DataDescriptor, DataDescriptorHash,
+                      DataDescriptorEquals>
+      synonymous;
 };
 
-void FactManager::IdSynonymFacts::AddFact(
-    const protobufs::FactIdSynonym& fact) {
-  if (synonyms.count(fact.id()) == 0) {
-    assert(ids_with_synonyms.count(fact.id()) == 0);
-    ids_with_synonyms.insert(fact.id());
-    synonyms[fact.id()] = std::vector<protobufs::DataDescriptor>();
-  }
-  assert(ids_with_synonyms.count(fact.id()) == 1);
-  synonyms[fact.id()].push_back(fact.data_descriptor());
+void FactManager::DataSynonymFacts::AddFact(
+    const protobufs::FactDataSynonym& fact) {
+  synonymous.MakeEquivalent(fact.data1(), fact.data2());
 }
 
-// End of id synonym facts
+// End of data synonym facts
 //==============================
 
 FactManager::FactManager()
     : uniform_constant_facts_(MakeUnique<ConstantUniformFacts>()),
-      id_synonym_facts_(MakeUnique<IdSynonymFacts>()) {}
+      data_synonym_facts_(MakeUnique<DataSynonymFacts>()) {}
 
 FactManager::~FactManager() = default;
 
@@ -385,14 +359,14 @@ void FactManager::AddFacts(const MessageConsumer& message_consumer,
   }
 }
 
-bool FactManager::AddFact(const spvtools::fuzz::protobufs::Fact& fact,
-                          spvtools::opt::IRContext* context) {
+bool FactManager::AddFact(const fuzz::protobufs::Fact& fact,
+                          opt::IRContext* context) {
   switch (fact.fact_case()) {
     case protobufs::Fact::kConstantUniformFact:
       return uniform_constant_facts_->AddFact(fact.constant_uniform_fact(),
                                               context);
-    case protobufs::Fact::kIdSynonymFact:
-      id_synonym_facts_->AddFact(fact.id_synonym_fact());
+    case protobufs::Fact::kDataSynonymFact:
+      data_synonym_facts_->AddFact(fact.data_synonym_fact());
       return true;
     default:
       assert(false && "Unknown fact type.");
@@ -400,6 +374,14 @@ bool FactManager::AddFact(const spvtools::fuzz::protobufs::Fact& fact,
   }
 }
 
+void FactManager::AddFactDataSynonym(const protobufs::DataDescriptor& data1,
+                                     const protobufs::DataDescriptor& data2) {
+  protobufs::FactDataSynonym fact;
+  *fact.mutable_data1() = data1;
+  *fact.mutable_data2() = data2;
+  data_synonym_facts_->AddFact(fact);
+}
+
 std::vector<uint32_t> FactManager::GetConstantsAvailableFromUniformsForType(
     opt::IRContext* ir_context, uint32_t type_id) const {
   return uniform_constant_facts_->GetConstantsAvailableFromUniformsForType(
@@ -430,13 +412,26 @@ FactManager::GetConstantUniformFactsAndTypes() const {
   return uniform_constant_facts_->facts_and_type_ids;
 }
 
-const std::set<uint32_t>& FactManager::GetIdsForWhichSynonymsAreKnown() const {
-  return id_synonym_facts_->ids_with_synonyms;
+std::set<uint32_t> FactManager::GetIdsForWhichSynonymsAreKnown() const {
+  std::set<uint32_t> result;
+  for (auto& data_descriptor :
+       data_synonym_facts_->synonymous.GetAllKnownValues()) {
+    if (data_descriptor->index().empty()) {
+      assert(data_descriptor->num_contiguous_elements() == 1 &&
+             "Multiple contiguous elements are only allowed for data "
+             "descriptors that "
+             "are indices into vectors.");
+      result.insert(data_descriptor->object());
+    }
+  }
+  return result;
 }
 
-const std::vector<protobufs::DataDescriptor>& FactManager::GetSynonymsForId(
-    uint32_t id) const {
-  return id_synonym_facts_->synonyms.at(id);
+std::unordered_set<const protobufs::DataDescriptor*, DataDescriptorHash,
+                   DataDescriptorEquals>
+FactManager::GetSynonymsForId(uint32_t id) const {
+  return data_synonym_facts_->synonymous.GetEquivalenceClass(
+      MakeDataDescriptor(id, {}, 1));
 }
 
 }  // namespace fuzz

+ 14 - 7
3rdparty/spirv-tools/source/fuzz/fact_manager.h

@@ -20,6 +20,7 @@
 #include <utility>
 #include <vector>
 
+#include "source/fuzz/data_descriptor.h"
 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
 #include "source/opt/constants.h"
 
@@ -52,6 +53,10 @@ class FactManager {
   // fact manager.
   bool AddFact(const protobufs::Fact& fact, opt::IRContext* context);
 
+  // Record the fact that |data1| and |data2| are synonymous.
+  void AddFactDataSynonym(const protobufs::DataDescriptor& data1,
+                          const protobufs::DataDescriptor& data2);
+
   // The fact manager is responsible for managing a few distinct categories of
   // facts. In principle there could be different fact managers for each kind
   // of fact, but in practice providing one 'go to' place for facts is
@@ -101,12 +106,13 @@ class FactManager {
 
   // Returns every id for which a fact of the form "this id is synonymous
   // with this piece of data" is known.
-  const std::set<uint32_t>& GetIdsForWhichSynonymsAreKnown() const;
+  std::set<uint32_t> GetIdsForWhichSynonymsAreKnown() const;
 
   // Requires that at least one synonym for |id| is known, and returns the
-  // sequence of all known synonyms.
-  const std::vector<protobufs::DataDescriptor>& GetSynonymsForId(
-      uint32_t id) const;
+  // equivalence class of all known synonyms.
+  std::unordered_set<const protobufs::DataDescriptor*, DataDescriptorHash,
+                     DataDescriptorEquals>
+  GetSynonymsForId(uint32_t id) const;
 
   // End of id synonym facts
   //==============================
@@ -120,9 +126,10 @@ class FactManager {
   std::unique_ptr<ConstantUniformFacts>
       uniform_constant_facts_;  // Unique pointer to internal data.
 
-  struct IdSynonymFacts;  // Opaque struct for holding data about id synonyms.
-  std::unique_ptr<IdSynonymFacts>
-      id_synonym_facts_;  // Unique pointer to internal data.
+  struct DataSynonymFacts;  // Opaque struct for holding data about data
+                            // synonyms.
+  std::unique_ptr<DataSynonymFacts>
+      data_synonym_facts_;  // Unique pointer to internal data.
 };
 
 }  // namespace fuzz

+ 9 - 5
3rdparty/spirv-tools/source/fuzz/fuzzer.cpp

@@ -18,6 +18,7 @@
 #include <memory>
 #include <sstream>
 
+#include "fuzzer_pass_adjust_memory_operands_masks.h"
 #include "source/fuzz/fact_manager.h"
 #include "source/fuzz/fuzzer_context.h"
 #include "source/fuzz/fuzzer_pass_add_dead_breaks.h"
@@ -173,14 +174,17 @@ Fuzzer::FuzzerResultStatus Fuzzer::Run(
   // Now apply some passes that it does not make sense to apply repeatedly,
   // as they do not unlock other passes.
   std::vector<std::unique_ptr<FuzzerPass>> final_passes;
-  MaybeAddPass<FuzzerPassAdjustFunctionControls>(&passes, ir_context.get(),
-                                                 &fact_manager, &fuzzer_context,
-                                                 transformation_sequence_out);
-  MaybeAddPass<FuzzerPassAdjustLoopControls>(&passes, ir_context.get(),
+  MaybeAddPass<FuzzerPassAdjustFunctionControls>(
+      &final_passes, ir_context.get(), &fact_manager, &fuzzer_context,
+      transformation_sequence_out);
+  MaybeAddPass<FuzzerPassAdjustLoopControls>(&final_passes, ir_context.get(),
                                              &fact_manager, &fuzzer_context,
                                              transformation_sequence_out);
+  MaybeAddPass<FuzzerPassAdjustMemoryOperandsMasks>(
+      &final_passes, ir_context.get(), &fact_manager, &fuzzer_context,
+      transformation_sequence_out);
   MaybeAddPass<FuzzerPassAdjustSelectionControls>(
-      &passes, ir_context.get(), &fact_manager, &fuzzer_context,
+      &final_passes, ir_context.get(), &fact_manager, &fuzzer_context,
       transformation_sequence_out);
   for (auto& pass : final_passes) {
     pass->Apply();

+ 4 - 0
3rdparty/spirv-tools/source/fuzz/fuzzer_context.cpp

@@ -30,6 +30,8 @@ const std::pair<uint32_t, uint32_t> kChanceOfAddingNoContractionDecoration = {
 const std::pair<uint32_t, uint32_t> kChanceOfAdjustingFunctionControl = {20,
                                                                          70};
 const std::pair<uint32_t, uint32_t> kChanceOfAdjustingLoopControl = {20, 90};
+const std::pair<uint32_t, uint32_t> kChanceOfAdjustingMemoryOperandsMask = {20,
+                                                                            90};
 const std::pair<uint32_t, uint32_t> kChanceOfAdjustingSelectionControl = {20,
                                                                           90};
 const std::pair<uint32_t, uint32_t> kChanceOfCopyingObject = {20, 50};
@@ -72,6 +74,8 @@ FuzzerContext::FuzzerContext(RandomGenerator* random_generator,
       ChooseBetweenMinAndMax(kChanceOfAdjustingFunctionControl);
   chance_of_adjusting_loop_control_ =
       ChooseBetweenMinAndMax(kChanceOfAdjustingLoopControl);
+  chance_of_adjusting_memory_operands_mask_ =
+      ChooseBetweenMinAndMax(kChanceOfAdjustingMemoryOperandsMask);
   chance_of_adjusting_selection_control_ =
       ChooseBetweenMinAndMax(kChanceOfAdjustingSelectionControl);
   chance_of_constructing_composite_ =

+ 4 - 0
3rdparty/spirv-tools/source/fuzz/fuzzer_context.h

@@ -71,6 +71,9 @@ class FuzzerContext {
   uint32_t GetChanceOfAdjustingLoopControl() {
     return chance_of_adjusting_loop_control_;
   }
+  uint32_t GetChanceOfAdjustingMemoryOperandsMask() {
+    return chance_of_adjusting_memory_operands_mask_;
+  }
   uint32_t GetChanceOfAdjustingSelectionControl() {
     return chance_of_adjusting_selection_control_;
   }
@@ -112,6 +115,7 @@ class FuzzerContext {
   uint32_t chance_of_adding_no_contraction_decoration_;
   uint32_t chance_of_adjusting_function_control_;
   uint32_t chance_of_adjusting_loop_control_;
+  uint32_t chance_of_adjusting_memory_operands_mask_;
   uint32_t chance_of_adjusting_selection_control_;
   uint32_t chance_of_constructing_composite_;
   uint32_t chance_of_copying_object_;

+ 113 - 0
3rdparty/spirv-tools/source/fuzz/fuzzer_pass_adjust_memory_operands_masks.cpp

@@ -0,0 +1,113 @@
+// Copyright (c) 2019 Google LLC
+//
+// 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 "source/fuzz/fuzzer_pass_adjust_memory_operands_masks.h"
+
+#include "source/fuzz/instruction_descriptor.h"
+#include "source/fuzz/transformation_set_memory_operands_mask.h"
+
+namespace spvtools {
+namespace fuzz {
+
+FuzzerPassAdjustMemoryOperandsMasks::FuzzerPassAdjustMemoryOperandsMasks(
+    opt::IRContext* ir_context, FactManager* fact_manager,
+    FuzzerContext* fuzzer_context,
+    protobufs::TransformationSequence* transformations)
+    : FuzzerPass(ir_context, fact_manager, fuzzer_context, transformations) {}
+
+FuzzerPassAdjustMemoryOperandsMasks::~FuzzerPassAdjustMemoryOperandsMasks() =
+    default;
+
+void FuzzerPassAdjustMemoryOperandsMasks::Apply() {
+  // Consider every block in every function.
+  for (auto& function : *GetIRContext()->module()) {
+    for (auto& block : function) {
+      // Consider every instruction in this block, using an explicit iterator so
+      // that when we find an instruction of interest we can search backwards to
+      // create an id descriptor for it.
+      for (auto inst_it = block.cbegin(); inst_it != block.cend(); ++inst_it) {
+        if (!TransformationSetMemoryOperandsMask::IsMemoryAccess(*inst_it)) {
+          // We are only interested in memory access instructions.
+          continue;
+        }
+
+        std::vector<uint32_t> indices_of_available_masks_to_adjust;
+        // All memory instructions have at least one memory operands mask.
+        indices_of_available_masks_to_adjust.push_back(0);
+        // From SPIR-V 1.4 onwards, OpCopyMemory and OpCopyMemorySized have a
+        // second mask.
+        switch (inst_it->opcode()) {
+          case SpvOpCopyMemory:
+          case SpvOpCopyMemorySized:
+            if (TransformationSetMemoryOperandsMask::
+                    MultipleMemoryOperandMasksAreSupported(GetIRContext())) {
+              indices_of_available_masks_to_adjust.push_back(1);
+            }
+            break;
+          default:
+            break;
+        }
+
+        // Consider the available masks
+        for (auto mask_index : indices_of_available_masks_to_adjust) {
+          // Randomly decide whether to adjust this mask.
+          if (!GetFuzzerContext()->ChoosePercentage(
+                  GetFuzzerContext()
+                      ->GetChanceOfAdjustingMemoryOperandsMask())) {
+            continue;
+          }
+          // Get the existing mask, using None if there was no mask present at
+          // all.
+          auto existing_mask_in_operand_index =
+              TransformationSetMemoryOperandsMask::GetInOperandIndexForMask(
+                  *inst_it, mask_index);
+          auto existing_mask =
+              existing_mask_in_operand_index < inst_it->NumInOperands()
+                  ? inst_it->GetSingleWordOperand(
+                        existing_mask_in_operand_index)
+                  : static_cast<uint32_t>(SpvMemoryAccessMaskNone);
+
+          // There are two things we can do to a mask:
+          // - add Volatile if not already present
+          // - toggle Nontemporal
+          // The following ensures that we do at least one of these
+          bool add_volatile = !(existing_mask & SpvMemoryAccessVolatileMask) &&
+                              GetFuzzerContext()->ChooseEven();
+          bool toggle_nontemporal =
+              !add_volatile || GetFuzzerContext()->ChooseEven();
+
+          // These bitwise operations use '|' to add Volatile if desired, and
+          // '^' to toggle Nontemporal if desired.
+          uint32_t new_mask =
+              (existing_mask | (add_volatile ? SpvMemoryAccessVolatileMask
+                                             : SpvMemoryAccessMaskNone)) ^
+              (toggle_nontemporal ? SpvMemoryAccessNontemporalMask
+                                  : SpvMemoryAccessMaskNone);
+
+          TransformationSetMemoryOperandsMask transformation(
+              MakeInstructionDescriptor(block, inst_it), new_mask, mask_index);
+          assert(
+              transformation.IsApplicable(GetIRContext(), *GetFactManager()) &&
+              "Transformation should be applicable by construction.");
+          transformation.Apply(GetIRContext(), GetFactManager());
+          *GetTransformations()->add_transformation() =
+              transformation.ToMessage();
+        }
+      }
+    }
+  }
+}
+
+}  // namespace fuzz
+}  // namespace spvtools

+ 40 - 0
3rdparty/spirv-tools/source/fuzz/fuzzer_pass_adjust_memory_operands_masks.h

@@ -0,0 +1,40 @@
+// Copyright (c) 2019 Google LLC
+//
+// 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 SOURCE_FUZZ_FUZZER_PASS_ADJUST_MEMORY_OPERANDS_MASKS_H_
+#define SOURCE_FUZZ_FUZZER_PASS_ADJUST_MEMORY_OPERANDS_MASKS_H_
+
+#include "source/fuzz/fuzzer_pass.h"
+
+namespace spvtools {
+namespace fuzz {
+
+// A fuzzer pass to adjust the memory operand masks in memory access
+// instructions.
+class FuzzerPassAdjustMemoryOperandsMasks : public FuzzerPass {
+ public:
+  FuzzerPassAdjustMemoryOperandsMasks(
+      opt::IRContext* ir_context, FactManager* fact_manager,
+      FuzzerContext* fuzzer_context,
+      protobufs::TransformationSequence* transformations);
+
+  ~FuzzerPassAdjustMemoryOperandsMasks();
+
+  void Apply() override;
+};
+
+}  // namespace fuzz
+}  // namespace spvtools
+
+#endif  // SOURCE_FUZZ_FUZZER_PASS_ADJUST_MEMORY_OPERANDS_MASKS_H_

+ 1 - 1
3rdparty/spirv-tools/source/fuzz/fuzzer_pass_apply_id_synonyms.cpp

@@ -56,7 +56,7 @@ void FuzzerPassApplyIdSynonyms::Apply() {
           std::vector<const protobufs::DataDescriptor*> synonyms_to_try;
           for (auto& data_descriptor :
                GetFactManager()->GetSynonymsForId(id_with_known_synonyms)) {
-            synonyms_to_try.push_back(&data_descriptor);
+            synonyms_to_try.push_back(data_descriptor);
           }
           while (!synonyms_to_try.empty()) {
             auto synonym_index =

+ 51 - 16
3rdparty/spirv-tools/source/fuzz/fuzzer_pass_construct_composites.cpp

@@ -165,18 +165,26 @@ std::unique_ptr<std::vector<uint32_t>>
 FuzzerPassConstructComposites::TryConstructingArrayComposite(
     const opt::analysis::Array& array_type,
     const TypeIdToInstructions& type_id_to_available_instructions) {
-  // TODO make these be true by construction
+  // At present we assume arrays have a constant size.
   assert(array_type.length_info().words.size() == 2);
   assert(array_type.length_info().words[0] ==
          opt::analysis::Array::LengthInfo::kConstant);
 
   auto result = MakeUnique<std::vector<uint32_t>>();
+
+  // Get the element type for the array.
   auto element_type_id =
       GetIRContext()->get_type_mgr()->GetId(array_type.element_type());
+
+  // Get all instructions at our disposal that compute something of this element
+  // type.
   auto available_instructions =
       type_id_to_available_instructions.find(element_type_id);
+
   if (available_instructions == type_id_to_available_instructions.cend()) {
-    // TODO comment infeasible
+    // If there are not any instructions available that compute the element type
+    // of the array then we are not in a position to construct a composite with
+    // this array type.
     return nullptr;
   }
   for (uint32_t index = 0; index < array_type.length_info().words[1]; index++) {
@@ -192,10 +200,30 @@ std::unique_ptr<std::vector<uint32_t>>
 FuzzerPassConstructComposites::TryConstructingMatrixComposite(
     const opt::analysis::Matrix& matrix_type,
     const TypeIdToInstructions& type_id_to_available_instructions) {
-  (void)(matrix_type);
-  (void)(type_id_to_available_instructions);
-  assert(false);
-  return nullptr;
+  auto result = MakeUnique<std::vector<uint32_t>>();
+
+  // Get the element type for the matrix.
+  auto element_type_id =
+      GetIRContext()->get_type_mgr()->GetId(matrix_type.element_type());
+
+  // Get all instructions at our disposal that compute something of this element
+  // type.
+  auto available_instructions =
+      type_id_to_available_instructions.find(element_type_id);
+
+  if (available_instructions == type_id_to_available_instructions.cend()) {
+    // If there are not any instructions available that compute the element type
+    // of the matrix then we are not in a position to construct a composite with
+    // this matrix type.
+    return nullptr;
+  }
+  for (uint32_t index = 0; index < matrix_type.element_count(); index++) {
+    result->push_back(available_instructions
+                          ->second[GetFuzzerContext()->RandomIndex(
+                              available_instructions->second)]
+                          ->result_id());
+  }
+  return result;
 }
 
 std::unique_ptr<std::vector<uint32_t>>
@@ -203,12 +231,16 @@ FuzzerPassConstructComposites::TryConstructingStructComposite(
     const opt::analysis::Struct& struct_type,
     const TypeIdToInstructions& type_id_to_available_instructions) {
   auto result = MakeUnique<std::vector<uint32_t>>();
+  // Consider the type of each field of the struct.
   for (auto element_type : struct_type.element_types()) {
     auto element_type_id = GetIRContext()->get_type_mgr()->GetId(element_type);
+    // Find the instructions at our disposal that compute something of the field
+    // type.
     auto available_instructions =
         type_id_to_available_instructions.find(element_type_id);
     if (available_instructions == type_id_to_available_instructions.cend()) {
-      // TODO comment infeasible
+      // If there are no such instructions, we cannot construct a composite of
+      // this struct type.
       return nullptr;
     }
     result->push_back(available_instructions
@@ -230,17 +262,14 @@ FuzzerPassConstructComposites::TryConstructingVectorComposite(
 
   // Collect a mapping, from type id to width, for scalar/vector types that are
   // smaller in width than |vector_type|, but that have the same underlying
-  // type.  For example, if |vector_type| is vec4, the mapping will be { float
-  // -> 1, vec2 -> 2, vec3 -> 3 }.  The mapping will have missing entries if
-  // some of these types do not exist.
+  // type.  For example, if |vector_type| is vec4, the mapping will be:
+  //   { float -> 1, vec2 -> 2, vec3 -> 3 }
+  // The mapping will have missing entries if some of these types do not exist.
 
-  // TODO comment why we have the list as well.
-  std::vector<uint32_t> smaller_vector_type_ids;
   std::map<uint32_t, uint32_t> smaller_vector_type_id_to_width;
   // Add the underlying type.  This id must exist, in order for |vector_type| to
   // exist.
   auto scalar_type_id = GetIRContext()->get_type_mgr()->GetId(element_type);
-  smaller_vector_type_ids.push_back(scalar_type_id);
   smaller_vector_type_id_to_width[scalar_type_id] = 1;
 
   // Now add every vector type with width at least 2, and less than the width of
@@ -250,9 +279,10 @@ FuzzerPassConstructComposites::TryConstructingVectorComposite(
                                               width);
     auto smaller_vector_type_id =
         GetIRContext()->get_type_mgr()->GetId(&smaller_vector_type);
-    // TODO recap why it might be 0
+    // We might find that there is no declared type of this smaller width.
+    // For example, a module can declare vec4 without having declared vec2 or
+    // vec3.
     if (smaller_vector_type_id) {
-      smaller_vector_type_ids.push_back(smaller_vector_type_id);
       smaller_vector_type_id_to_width[smaller_vector_type_id] = width;
     }
   }
@@ -291,7 +321,12 @@ FuzzerPassConstructComposites::TryConstructingVectorComposite(
                                          available_instructions->second.end());
     }
     if (instructions_to_choose_from.empty()) {
-      // TODO comment - like fuzzed into a corner
+      // We may get unlucky and find that there are not any instructions to
+      // choose from.  In this case we give up constructing a composite of this
+      // vector type.  It might be that we could construct the composite in
+      // another manner, so we could opt to retry a few times here, but it is
+      // simpler to just give up on the basis that this will not happen
+      // frequently.
       return nullptr;
     }
     auto instruction_to_use =

+ 48 - 0
3rdparty/spirv-tools/source/fuzz/fuzzer_util.cpp

@@ -330,6 +330,54 @@ bool IsCompositeType(const opt::analysis::Type* type) {
                   type->AsVector());
 }
 
+uint32_t WalkCompositeIndices(
+    opt::IRContext* context, uint32_t base_object_type_id,
+    const google::protobuf::RepeatedField<google::protobuf::uint32>& indices) {
+  uint32_t sub_object_type_id = base_object_type_id;
+  for (auto index : indices) {
+    auto should_be_composite_type =
+        context->get_def_use_mgr()->GetDef(sub_object_type_id);
+    assert(should_be_composite_type && "The type should exist.");
+    if (SpvOpTypeStruct == should_be_composite_type->opcode()) {
+      if (index >= should_be_composite_type->NumInOperands()) {
+        return 0;
+      }
+      sub_object_type_id =
+          should_be_composite_type->GetSingleWordInOperand(index);
+    } else if (SpvOpTypeArray == should_be_composite_type->opcode()) {
+      auto array_length_constant =
+          context->get_constant_mgr()
+              ->GetConstantFromInst(context->get_def_use_mgr()->GetDef(
+                  should_be_composite_type->GetSingleWordInOperand(1)))
+              ->AsIntConstant();
+      if (array_length_constant->words().size() != 1) {
+        return 0;
+      }
+      auto array_length = array_length_constant->GetU32();
+      if (index >= array_length) {
+        return 0;
+      }
+      sub_object_type_id = should_be_composite_type->GetSingleWordInOperand(0);
+    } else if (SpvOpTypeVector == should_be_composite_type->opcode()) {
+      auto vector_length = should_be_composite_type->GetSingleWordInOperand(1);
+      if (index >= vector_length) {
+        return 0;
+      }
+      sub_object_type_id = should_be_composite_type->GetSingleWordInOperand(0);
+    } else if (SpvOpTypeMatrix == should_be_composite_type->opcode()) {
+      auto matrix_column_count =
+          should_be_composite_type->GetSingleWordInOperand(1);
+      if (index >= matrix_column_count) {
+        return 0;
+      }
+      sub_object_type_id = should_be_composite_type->GetSingleWordInOperand(0);
+    } else {
+      return 0;
+    }
+  }
+  return sub_object_type_id;
+}
+
 }  // namespace fuzzerutil
 
 }  // namespace fuzz

+ 8 - 0
3rdparty/spirv-tools/source/fuzz/fuzzer_util.h

@@ -94,6 +94,14 @@ bool CanMakeSynonymOf(opt::IRContext* ir_context, opt::Instruction* inst);
 // struct or vector.
 bool IsCompositeType(const opt::analysis::Type* type);
 
+// Given a type id, |base_object_type_id|, checks that the given sequence of
+// |indices| is suitable for indexing into this type.  Returns the id of the
+// type of the final sub-object reached via the indices if they are valid, and
+// 0 otherwise.
+uint32_t WalkCompositeIndices(
+    opt::IRContext* context, uint32_t base_object_type_id,
+    const google::protobuf::RepeatedField<google::protobuf::uint32>& indices);
+
 }  // namespace fuzzerutil
 
 }  // namespace fuzz

+ 35 - 0
3rdparty/spirv-tools/source/fuzz/instruction_descriptor.cpp

@@ -66,5 +66,40 @@ protobufs::InstructionDescriptor MakeInstructionDescriptor(
   return result;
 }
 
+protobufs::InstructionDescriptor MakeInstructionDescriptor(
+    const opt::BasicBlock& block,
+    const opt::BasicBlock::const_iterator& inst_it) {
+  const SpvOp opcode =
+      inst_it->opcode();    // The opcode of the instruction being described.
+  uint32_t skip_count = 0;  // The number of these opcodes we have skipped when
+                            // searching backwards.
+
+  // Consider instructions in the block in reverse order, starting from
+  // |inst_it|.
+  for (opt::BasicBlock::const_iterator backwards_iterator = inst_it;;
+       --backwards_iterator) {
+    if (backwards_iterator->HasResultId()) {
+      // As soon as we find an instruction with a result id, we can return a
+      // descriptor for |inst_it|.
+      return MakeInstructionDescriptor(backwards_iterator->result_id(), opcode,
+                                       skip_count);
+    }
+    if (backwards_iterator != inst_it &&
+        backwards_iterator->opcode() == opcode) {
+      // We are skipping over an instruction with the same opcode as |inst_it|;
+      // we increase our skip count to reflect this.
+      skip_count++;
+    }
+    if (backwards_iterator == block.begin()) {
+      // We exit the loop when we reach the start of the block, but only after
+      // we have processed the first instruction in the block.
+      break;
+    }
+  }
+  // We did not find an instruction inside the block with a result id, so we use
+  // the block's label's id.
+  return MakeInstructionDescriptor(block.id(), opcode, skip_count);
+}
+
 }  // namespace fuzz
 }  // namespace spvtools

+ 9 - 0
3rdparty/spirv-tools/source/fuzz/instruction_descriptor.h

@@ -16,6 +16,7 @@
 #define SOURCE_FUZZ_INSTRUCTION_DESCRIPTOR_H_
 
 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
+#include "source/opt/basic_block.h"
 #include "source/opt/ir_context.h"
 
 namespace spvtools {
@@ -34,6 +35,14 @@ protobufs::InstructionDescriptor MakeInstructionDescriptor(
     uint32_t base_instruction_result_id, SpvOp target_instruction_opcode,
     uint32_t num_opcodes_to_ignore);
 
+// Returns an instruction descriptor that describing the instruction at
+// |inst_it|, which must be inside |block|.  The descriptor will be with
+// respect to the first instruction at or before |inst_it| that has a result
+// id.
+protobufs::InstructionDescriptor MakeInstructionDescriptor(
+    const opt::BasicBlock& block,
+    const opt::BasicBlock::const_iterator& inst_it);
+
 }  // namespace fuzz
 }  // namespace spvtools
 

+ 32 - 11
3rdparty/spirv-tools/source/fuzz/protobufs/spvtoolsfuzz.proto

@@ -82,6 +82,10 @@ message DataDescriptor {
   // 0 or more indices, used to index into a composite object
   repeated uint32 index = 2;
 
+  // The number of contiguous elements.  This will typically be 1, but e.g. 2 or
+  // 3 can be used to describe the 'xy' or 'xyz' portion of a vec4.
+  uint32 num_contiguous_elements = 3;
+
 }
 
 message UniformBufferElementDescriptor {
@@ -124,7 +128,7 @@ message Fact {
   oneof fact {
     // Order the fact options by numeric id (rather than alphabetically).
     FactConstantUniform constant_uniform_fact = 1;
-    FactIdSynonym id_synonym_fact = 2;
+    FactDataSynonym data_synonym_fact = 2;
   }
 }
 
@@ -146,19 +150,16 @@ message FactConstantUniform {
 
 }
 
-message FactIdSynonym {
+message FactDataSynonym {
 
-  // Records the fact that the data held in an id is guaranteed to be equal to
-  // the data held in a data descriptor.  spirv-fuzz can use this to replace
-  // uses of the id with references to the data described by the data
-  // descriptor.
+  // Records the fact that the data held in two data descriptors are guaranteed
+  // to be equal.  spirv-fuzz can use this to replace uses of one piece of data
+  // with a known-to-be-equal piece of data.
 
-  // An id
-  uint32 id = 1;
+  // Data descriptors guaranteed to hold identical data.
+  DataDescriptor data1 = 1;
 
-  // A data descriptor guaranteed to hold a value identical to that held by the
-  // id
-  DataDescriptor data_descriptor = 2;
+  DataDescriptor data2 = 2;
 
 }
 
@@ -190,6 +191,7 @@ message Transformation {
     TransformationSetLoopControl set_loop_control = 17;
     TransformationSetFunctionControl set_function_control = 18;
     TransformationAddNoContractionDecoration add_no_contraction_decoration = 19;
+    TransformationSetMemoryOperandsMask set_memory_operands_mask = 20;
     // Add additional option using the next available number.
   }
 }
@@ -464,6 +466,25 @@ message TransformationSetLoopControl {
 
 }
 
+message TransformationSetMemoryOperandsMask {
+
+  // A transformation that sets the memory operands mask of a memory access
+  // instruction.
+
+  // A descriptor for a memory access instruction, e.g. an OpLoad
+  InstructionDescriptor memory_access_instruction = 1;
+
+  // A mask of memory operands to be applied to the instruction.  It must be the
+  // same as the original mask, except that Volatile can be added, and
+  // Nontemporal can be added or removed.
+  uint32 memory_operands_mask = 2;
+
+  // Some memory access instructions allow more than one mask to be specified;
+  // this field indicates which mask should be set
+  uint32 memory_operands_mask_index = 3;
+
+}
+
 message TransformationSetSelectionControl {
 
   // A transformation that sets the selection control operand of an

+ 4 - 0
3rdparty/spirv-tools/source/fuzz/transformation.cpp

@@ -33,6 +33,7 @@
 #include "source/fuzz/transformation_replace_id_with_synonym.h"
 #include "source/fuzz/transformation_set_function_control.h"
 #include "source/fuzz/transformation_set_loop_control.h"
+#include "source/fuzz/transformation_set_memory_operands_mask.h"
 #include "source/fuzz/transformation_set_selection_control.h"
 #include "source/fuzz/transformation_split_block.h"
 #include "source/util/make_unique.h"
@@ -94,6 +95,9 @@ std::unique_ptr<Transformation> Transformation::FromMessage(
     case protobufs::Transformation::TransformationCase::kSetLoopControl:
       return MakeUnique<TransformationSetLoopControl>(
           message.set_loop_control());
+    case protobufs::Transformation::TransformationCase::kSetMemoryOperandsMask:
+      return MakeUnique<TransformationSetMemoryOperandsMask>(
+          message.set_memory_operands_mask());
     case protobufs::Transformation::TransformationCase::kSetSelectionControl:
       return MakeUnique<TransformationSetSelectionControl>(
           message.set_selection_control());

+ 14 - 9
3rdparty/spirv-tools/source/fuzz/transformation_construct_composite.cpp

@@ -14,6 +14,7 @@
 
 #include "source/fuzz/transformation_construct_composite.h"
 
+#include "source/fuzz/data_descriptor.h"
 #include "source/fuzz/fuzzer_util.h"
 #include "source/fuzz/instruction_descriptor.h"
 #include "source/opt/instruction.h"
@@ -138,12 +139,10 @@ void TransformationConstructComposite::Apply(opt::IRContext* context,
       context->get_type_mgr()->GetType(message_.composite_type_id());
   uint32_t index = 0;
   for (auto component : message_.component()) {
-    protobufs::Fact fact;
-    fact.mutable_id_synonym_fact()->set_id(component);
-    fact.mutable_id_synonym_fact()->mutable_data_descriptor()->set_object(
-        message_.fresh_id());
-    fact.mutable_id_synonym_fact()->mutable_data_descriptor()->add_index(index);
-    fact_manager->AddFact(fact, context);
+    // Decide how many contiguous composite elements are represented by the
+    // components.  This is always 1, except in the case of a vector that is
+    // constructed by smaller vectors.
+    uint32_t num_contiguous_elements;
     if (composite_type->AsVector()) {
       // The vector case is a bit fiddly, because one argument to a vector
       // constructor can cover more than one element.
@@ -152,17 +151,23 @@ void TransformationConstructComposite::Apply(opt::IRContext* context,
       if (component_type->AsVector()) {
         assert(component_type->AsVector()->element_type() ==
                composite_type->AsVector()->element_type());
-        index += component_type->AsVector()->element_count();
+        num_contiguous_elements = component_type->AsVector()->element_count();
       } else {
         assert(component_type == composite_type->AsVector()->element_type());
-        index++;
+        num_contiguous_elements = 1;
       }
     } else {
       // The non-vector cases are all easy: the constructor has exactly the same
       // number of arguments as the number of sub-components, so we can just
       // increment the index.
-      index++;
+      num_contiguous_elements = 1;
     }
+
+    fact_manager->AddFactDataSynonym(
+        MakeDataDescriptor(component, {}, 1),
+        MakeDataDescriptor(message_.fresh_id(), {index},
+                           num_contiguous_elements));
+    index += num_contiguous_elements;
   }
 
   fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());

+ 4 - 5
3rdparty/spirv-tools/source/fuzz/transformation_copy_object.cpp

@@ -14,6 +14,7 @@
 
 #include "source/fuzz/transformation_copy_object.h"
 
+#include "source/fuzz/data_descriptor.h"
 #include "source/fuzz/fuzzer_util.h"
 #include "source/fuzz/instruction_descriptor.h"
 #include "source/opt/instruction.h"
@@ -101,11 +102,9 @@ void TransformationCopyObject::Apply(opt::IRContext* context,
   fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());
   context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
 
-  protobufs::Fact fact;
-  fact.mutable_id_synonym_fact()->set_id(message_.object());
-  fact.mutable_id_synonym_fact()->mutable_data_descriptor()->set_object(
-      message_.fresh_id());
-  fact_manager->AddFact(fact, context);
+  fact_manager->AddFactDataSynonym(
+      MakeDataDescriptor(message_.object(), {}, 1),
+      MakeDataDescriptor(message_.fresh_id(), {}, 1));
 }
 
 protobufs::Transformation TransformationCopyObject::ToMessage() const {

+ 2 - 2
3rdparty/spirv-tools/source/fuzz/transformation_replace_id_with_synonym.cpp

@@ -53,8 +53,8 @@ bool TransformationReplaceIdWithSynonym::IsApplicable(
 
   auto available_synonyms = fact_manager.GetSynonymsForId(id_of_interest);
   if (std::find_if(available_synonyms.begin(), available_synonyms.end(),
-                   [this](protobufs::DataDescriptor dd) -> bool {
-                     return DataDescriptorEquals()(&dd,
+                   [this](const protobufs::DataDescriptor* dd) -> bool {
+                     return DataDescriptorEquals()(dd,
                                                    &message_.data_descriptor());
                    }) == available_synonyms.end()) {
     return false;

+ 201 - 0
3rdparty/spirv-tools/source/fuzz/transformation_set_memory_operands_mask.cpp

@@ -0,0 +1,201 @@
+// Copyright (c) 2019 Google LLC
+//
+// 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 "source/fuzz/transformation_set_memory_operands_mask.h"
+
+#include "source/fuzz/instruction_descriptor.h"
+
+namespace spvtools {
+namespace fuzz {
+
+namespace {
+
+const uint32_t kOpLoadMemoryOperandsMaskIndex = 1;
+const uint32_t kOpStoreMemoryOperandsMaskIndex = 2;
+const uint32_t kOpCopyMemoryFirstMemoryOperandsMaskIndex = 2;
+const uint32_t kOpCopyMemorySizedFirstMemoryOperandsMaskIndex = 3;
+
+}  // namespace
+
+TransformationSetMemoryOperandsMask::TransformationSetMemoryOperandsMask(
+    const spvtools::fuzz::protobufs::TransformationSetMemoryOperandsMask&
+        message)
+    : message_(message) {}
+
+TransformationSetMemoryOperandsMask::TransformationSetMemoryOperandsMask(
+    const protobufs::InstructionDescriptor& memory_access_instruction,
+    uint32_t memory_operands_mask, uint32_t memory_operands_mask_index) {
+  *message_.mutable_memory_access_instruction() = memory_access_instruction;
+  message_.set_memory_operands_mask(memory_operands_mask);
+  message_.set_memory_operands_mask_index(memory_operands_mask_index);
+}
+
+bool TransformationSetMemoryOperandsMask::IsApplicable(
+    opt::IRContext* context,
+    const spvtools::fuzz::FactManager& /*unused*/) const {
+  if (message_.memory_operands_mask_index() != 0) {
+    // The following conditions should never be violated, even if
+    // transformations end up being replayed in a different way to the manner in
+    // which they were applied during fuzzing, hence why these are assertions
+    // rather than applicability checks.
+    assert(message_.memory_operands_mask_index() == 1);
+    assert(message_.memory_access_instruction().target_instruction_opcode() ==
+               SpvOpCopyMemory ||
+           message_.memory_access_instruction().target_instruction_opcode() ==
+               SpvOpCopyMemorySized);
+    assert(MultipleMemoryOperandMasksAreSupported(context));
+  }
+
+  auto instruction =
+      FindInstruction(message_.memory_access_instruction(), context);
+  if (!instruction) {
+    return false;
+  }
+  if (!IsMemoryAccess(*instruction)) {
+    return false;
+  }
+
+  auto original_mask_in_operand_index = GetInOperandIndexForMask(
+      *instruction, message_.memory_operands_mask_index());
+  assert(original_mask_in_operand_index != 0 &&
+         "The given mask index is not valid.");
+  uint32_t original_mask =
+      original_mask_in_operand_index < instruction->NumInOperands()
+          ? instruction->GetSingleWordInOperand(original_mask_in_operand_index)
+          : static_cast<uint32_t>(SpvMemoryAccessMaskNone);
+  uint32_t new_mask = message_.memory_operands_mask();
+
+  // Volatile must not be removed
+  if ((original_mask & SpvMemoryAccessVolatileMask) &&
+      !(new_mask & SpvMemoryAccessVolatileMask)) {
+    return false;
+  }
+
+  // Nontemporal can be added or removed, and no other flag is allowed to
+  // change.  We do this by checking that the masks are equal once we set
+  // their Volatile and Nontemporal flags to the same value (this works
+  // because valid manipulation of Volatile is checked above, and the manner
+  // in which Nontemporal is manipulated does not matter).
+  return (original_mask | SpvMemoryAccessVolatileMask |
+          SpvMemoryAccessNontemporalMask) ==
+         (new_mask | SpvMemoryAccessVolatileMask |
+          SpvMemoryAccessNontemporalMask);
+}
+
+void TransformationSetMemoryOperandsMask::Apply(
+    opt::IRContext* context, spvtools::fuzz::FactManager* /*unused*/) const {
+  auto instruction =
+      FindInstruction(message_.memory_access_instruction(), context);
+  auto original_mask_in_operand_index = GetInOperandIndexForMask(
+      *instruction, message_.memory_operands_mask_index());
+  // Either add a new operand, if no mask operand was already present, or
+  // replace an existing mask operand.
+  if (original_mask_in_operand_index >= instruction->NumInOperands()) {
+    instruction->AddOperand(
+        {SPV_OPERAND_TYPE_MEMORY_ACCESS, {message_.memory_operands_mask()}});
+
+  } else {
+    instruction->SetInOperand(original_mask_in_operand_index,
+                              {message_.memory_operands_mask()});
+  }
+}
+
+protobufs::Transformation TransformationSetMemoryOperandsMask::ToMessage()
+    const {
+  protobufs::Transformation result;
+  *result.mutable_set_memory_operands_mask() = message_;
+  return result;
+}
+
+bool TransformationSetMemoryOperandsMask::IsMemoryAccess(
+    const opt::Instruction& instruction) {
+  switch (instruction.opcode()) {
+    case SpvOpLoad:
+    case SpvOpStore:
+    case SpvOpCopyMemory:
+    case SpvOpCopyMemorySized:
+      return true;
+    default:
+      return false;
+  }
+}
+
+uint32_t TransformationSetMemoryOperandsMask::GetInOperandIndexForMask(
+    const opt::Instruction& instruction, uint32_t mask_index) {
+  // Get the input operand index associated with the first memory operands mask
+  // for the instruction.
+  uint32_t first_mask_in_operand_index = 0;
+  switch (instruction.opcode()) {
+    case SpvOpLoad:
+      first_mask_in_operand_index = kOpLoadMemoryOperandsMaskIndex;
+      break;
+    case SpvOpStore:
+      first_mask_in_operand_index = kOpStoreMemoryOperandsMaskIndex;
+      break;
+    case SpvOpCopyMemory:
+      first_mask_in_operand_index = kOpCopyMemoryFirstMemoryOperandsMaskIndex;
+      break;
+    case SpvOpCopyMemorySized:
+      first_mask_in_operand_index =
+          kOpCopyMemorySizedFirstMemoryOperandsMaskIndex;
+      break;
+    default:
+      assert(false && "Unknown memory instruction.");
+      break;
+  }
+  // If we are looking for the input operand index of the first mask, return it.
+  if (mask_index == 0) {
+    return first_mask_in_operand_index;
+  }
+  assert(mask_index == 1 && "Memory operands mask index must be 0 or 1.");
+
+  // We are looking for the input operand index of the second mask.  This is a
+  // little complicated because, depending on the contents of the first mask,
+  // there may be some input operands separating the two masks.
+  uint32_t first_mask =
+      instruction.GetSingleWordInOperand(first_mask_in_operand_index);
+
+  // Consider each bit that might have an associated extra input operand, and
+  // count how many there are expected to be.
+  uint32_t first_mask_extra_operand_count = 0;
+  for (auto mask_bit :
+       {SpvMemoryAccessAlignedMask, SpvMemoryAccessMakePointerAvailableMask,
+        SpvMemoryAccessMakePointerAvailableKHRMask,
+        SpvMemoryAccessMakePointerVisibleMask,
+        SpvMemoryAccessMakePointerVisibleKHRMask}) {
+    if (first_mask & mask_bit) {
+      first_mask_extra_operand_count++;
+    }
+  }
+  return first_mask_in_operand_index + first_mask_extra_operand_count + 1;
+}
+
+bool TransformationSetMemoryOperandsMask::
+    MultipleMemoryOperandMasksAreSupported(opt::IRContext* context) {
+  // TODO(afd): We capture the universal environments for which this loop
+  //  control is definitely not supported.  The check should be refined on
+  //  demand for other target environments.
+  switch (context->grammar().target_env()) {
+    case SPV_ENV_UNIVERSAL_1_0:
+    case SPV_ENV_UNIVERSAL_1_1:
+    case SPV_ENV_UNIVERSAL_1_2:
+    case SPV_ENV_UNIVERSAL_1_3:
+      return false;
+    default:
+      return true;
+  }
+}
+
+}  // namespace fuzz
+}  // namespace spvtools

+ 76 - 0
3rdparty/spirv-tools/source/fuzz/transformation_set_memory_operands_mask.h

@@ -0,0 +1,76 @@
+// Copyright (c) 2019 Google LLC
+//
+// 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 SOURCE_FUZZ_TRANSFORMATION_SET_MEMORY_OPERANDS_MASK_H_
+#define SOURCE_FUZZ_TRANSFORMATION_SET_MEMORY_OPERANDS_MASK_H_
+
+#include "source/fuzz/fact_manager.h"
+#include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
+#include "source/fuzz/transformation.h"
+#include "source/opt/ir_context.h"
+
+namespace spvtools {
+namespace fuzz {
+
+class TransformationSetMemoryOperandsMask : public Transformation {
+ public:
+  explicit TransformationSetMemoryOperandsMask(
+      const protobufs::TransformationSetMemoryOperandsMask& message);
+
+  TransformationSetMemoryOperandsMask(
+      const protobufs::InstructionDescriptor& memory_access_instruction,
+      uint32_t memory_operands_mask, uint32_t memory_operands_mask_index);
+
+  // - |message_.memory_access_instruction| must describe a memory access
+  //   instruction.
+  // - |message_.memory_operands_mask_index| must be suitable for this memory
+  //   access instruction, e.g. it must be 0 in the case of OpLoad, and may be
+  //   1 in the case of OpCopyMemory if the SPIR-V version is 1.4 or higher.
+  // - |message_.memory_operands_mask| must be identical to the original memory
+  //   operands mask, except that Volatile may be added, and Nontemporal may be
+  //   toggled.
+  bool IsApplicable(opt::IRContext* context,
+                    const FactManager& fact_manager) const override;
+
+  // Replaces the operands mask identified by
+  // |message_.memory_operands_mask_index| in the instruction described by
+  // |message_.memory_access_instruction| with |message_.memory_operands_mask|,
+  // creating an input operand for the mask if no such operand was present.
+  void Apply(opt::IRContext* context, FactManager* fact_manager) const override;
+
+  protobufs::Transformation ToMessage() const override;
+
+  // Helper function that determines whether |instruction| is a memory
+  // instruction (e.g. OpLoad).
+  static bool IsMemoryAccess(const opt::Instruction& instruction);
+
+  // Does the version of SPIR-V being used support multiple memory operand
+  // masks on relevant memory access instructions?
+  static bool MultipleMemoryOperandMasksAreSupported(opt::IRContext* context);
+
+  // Helper function to get the input operand index associated with mask number
+  // |mask_index|. This is a bit tricky if there are multiple masks, because the
+  // index associated with the second mask depends on whether the first mask
+  // includes any flags such as Aligned that have corresponding operands.
+  static uint32_t GetInOperandIndexForMask(const opt::Instruction& instruction,
+                                           uint32_t mask_index);
+
+ private:
+  protobufs::TransformationSetMemoryOperandsMask message_;
+};
+
+}  // namespace fuzz
+}  // namespace spvtools
+
+#endif  // SOURCE_FUZZ_TRANSFORMATION_SET_MEMORY_OPERANDS_MASK_H_

+ 30 - 23
3rdparty/spirv-tools/source/opt/instrument_pass.cpp

@@ -325,29 +325,36 @@ void InstrumentPass::CloneSameBlockOps(
     std::unordered_map<uint32_t, uint32_t>* same_blk_post,
     std::unordered_map<uint32_t, Instruction*>* same_blk_pre,
     BasicBlock* block_ptr) {
-  (*inst)->ForEachInId(
-      [&same_blk_post, &same_blk_pre, &block_ptr, this](uint32_t* iid) {
-        const auto map_itr = (*same_blk_post).find(*iid);
-        if (map_itr == (*same_blk_post).end()) {
-          const auto map_itr2 = (*same_blk_pre).find(*iid);
-          if (map_itr2 != (*same_blk_pre).end()) {
-            // Clone pre-call same-block ops, map result id.
-            const Instruction* in_inst = map_itr2->second;
-            std::unique_ptr<Instruction> sb_inst(in_inst->Clone(context()));
-            CloneSameBlockOps(&sb_inst, same_blk_post, same_blk_pre, block_ptr);
-            const uint32_t rid = sb_inst->result_id();
-            const uint32_t nid = this->TakeNextId();
-            get_decoration_mgr()->CloneDecorations(rid, nid);
-            sb_inst->SetResultId(nid);
-            (*same_blk_post)[rid] = nid;
-            *iid = nid;
-            block_ptr->AddInstruction(std::move(sb_inst));
-          }
-        } else {
-          // Reset same-block op operand.
-          *iid = map_itr->second;
-        }
-      });
+  bool changed = false;
+  (*inst)->ForEachInId([&same_blk_post, &same_blk_pre, &block_ptr, &changed,
+                        this](uint32_t* iid) {
+    const auto map_itr = (*same_blk_post).find(*iid);
+    if (map_itr == (*same_blk_post).end()) {
+      const auto map_itr2 = (*same_blk_pre).find(*iid);
+      if (map_itr2 != (*same_blk_pre).end()) {
+        // Clone pre-call same-block ops, map result id.
+        const Instruction* in_inst = map_itr2->second;
+        std::unique_ptr<Instruction> sb_inst(in_inst->Clone(context()));
+        const uint32_t rid = sb_inst->result_id();
+        const uint32_t nid = this->TakeNextId();
+        get_decoration_mgr()->CloneDecorations(rid, nid);
+        sb_inst->SetResultId(nid);
+        get_def_use_mgr()->AnalyzeInstDefUse(&*sb_inst);
+        (*same_blk_post)[rid] = nid;
+        *iid = nid;
+        changed = true;
+        CloneSameBlockOps(&sb_inst, same_blk_post, same_blk_pre, block_ptr);
+        block_ptr->AddInstruction(std::move(sb_inst));
+      }
+    } else {
+      // Reset same-block op operand if necessary
+      if (*iid != map_itr->second) {
+        *iid = map_itr->second;
+        changed = true;
+      }
+    }
+  });
+  if (changed) get_def_use_mgr()->AnalyzeInstUse(&**inst);
 }
 
 void InstrumentPass::UpdateSucceedingPhis(

+ 3 - 1
3rdparty/spirv-tools/test/fuzz/CMakeLists.txt

@@ -17,9 +17,11 @@ if (${SPIRV_BUILD_FUZZER})
   set(SOURCES
           fuzz_test_util.h
 
+          equivalence_relation_test.cpp
           fact_manager_test.cpp
           fuzz_test_util.cpp
           fuzzer_pass_add_useful_constructs_test.cpp
+          instruction_descriptor_test.cpp
           transformation_add_constant_boolean_test.cpp
           transformation_add_constant_scalar_test.cpp
           transformation_add_dead_break_test.cpp
@@ -34,9 +36,9 @@ if (${SPIRV_BUILD_FUZZER})
           transformation_move_block_down_test.cpp
           transformation_replace_boolean_constant_with_constant_binary_test.cpp
           transformation_replace_constant_with_uniform_test.cpp
-          transformation_replace_id_with_synonym_test.cpp
           transformation_set_function_control_test.cpp
           transformation_set_loop_control_test.cpp
+          transformation_set_memory_operands_mask_test.cpp
           transformation_set_selection_control_test.cpp
           transformation_split_block_test.cpp
           uniform_buffer_element_descriptor_test.cpp)

+ 98 - 0
3rdparty/spirv-tools/test/fuzz/equivalence_relation_test.cpp

@@ -0,0 +1,98 @@
+// Copyright (c) 2019 Google LLC
+//
+// 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 <set>
+
+#include "gtest/gtest.h"
+#include "source/fuzz/equivalence_relation.h"
+
+namespace spvtools {
+namespace fuzz {
+namespace {
+
+struct UInt32Equals {
+  bool operator()(const uint32_t* first, const uint32_t* second) const {
+    return *first == *second;
+  }
+};
+
+struct UInt32Hash {
+  size_t operator()(const uint32_t* element) const {
+    return static_cast<size_t>(*element);
+  }
+};
+
+std::set<uint32_t> ToUIntSet(
+    EquivalenceRelation<uint32_t, UInt32Hash, UInt32Equals>::ValueSet
+        pointers) {
+  std::set<uint32_t> result;
+  for (auto pointer : pointers) {
+    result.insert(*pointer);
+  }
+  return result;
+}
+
+TEST(EquivalenceRelationTest, BasicTest) {
+  EquivalenceRelation<uint32_t, UInt32Hash, UInt32Equals> relation;
+  ASSERT_TRUE(relation.GetAllKnownValues().empty());
+
+  for (uint32_t element = 2; element < 80; element += 2) {
+    relation.MakeEquivalent(0, element);
+    relation.MakeEquivalent(element - 1, element + 1);
+  }
+
+  for (uint32_t element = 82; element < 100; element += 2) {
+    relation.MakeEquivalent(80, element);
+    relation.MakeEquivalent(element - 1, element + 1);
+  }
+
+  relation.MakeEquivalent(78, 80);
+
+  std::set<uint32_t> class1;
+  for (uint32_t element = 0; element < 98; element += 2) {
+    ASSERT_TRUE(relation.IsEquivalent(0, element));
+    ASSERT_TRUE(relation.IsEquivalent(element, element + 2));
+    class1.insert(element);
+  }
+  class1.insert(98);
+  ASSERT_TRUE(class1 == ToUIntSet(relation.GetEquivalenceClass(0)));
+  ASSERT_TRUE(class1 == ToUIntSet(relation.GetEquivalenceClass(4)));
+  ASSERT_TRUE(class1 == ToUIntSet(relation.GetEquivalenceClass(40)));
+
+  std::set<uint32_t> class2;
+  for (uint32_t element = 1; element < 79; element += 2) {
+    ASSERT_TRUE(relation.IsEquivalent(1, element));
+    ASSERT_TRUE(relation.IsEquivalent(element, element + 2));
+    class2.insert(element);
+  }
+  class2.insert(79);
+  ASSERT_TRUE(class2 == ToUIntSet(relation.GetEquivalenceClass(1)));
+  ASSERT_TRUE(class2 == ToUIntSet(relation.GetEquivalenceClass(11)));
+  ASSERT_TRUE(class2 == ToUIntSet(relation.GetEquivalenceClass(31)));
+
+  std::set<uint32_t> class3;
+  for (uint32_t element = 81; element < 99; element += 2) {
+    ASSERT_TRUE(relation.IsEquivalent(81, element));
+    ASSERT_TRUE(relation.IsEquivalent(element, element + 2));
+    class3.insert(element);
+  }
+  class3.insert(99);
+  ASSERT_TRUE(class3 == ToUIntSet(relation.GetEquivalenceClass(81)));
+  ASSERT_TRUE(class3 == ToUIntSet(relation.GetEquivalenceClass(91)));
+  ASSERT_TRUE(class3 == ToUIntSet(relation.GetEquivalenceClass(99)));
+}
+
+}  // namespace
+}  // namespace fuzz
+}  // namespace spvtools

+ 667 - 18
3rdparty/spirv-tools/test/fuzz/fuzzer_replayer_test.cpp

@@ -23,6 +23,26 @@ namespace {
 
 const uint32_t kNumFuzzerRuns = 20;
 
+void AddConstantUniformFact(protobufs::FactSequence* facts,
+                            uint32_t descriptor_set, uint32_t binding,
+                            std::vector<uint32_t>&& indices, uint32_t value) {
+  protobufs::FactConstantUniform fact;
+  *fact.mutable_uniform_buffer_element_descriptor() =
+      MakeUniformBufferElementDescriptor(descriptor_set, binding,
+                                         std::move(indices));
+  *fact.mutable_constant_word()->Add() = value;
+  protobufs::Fact temp;
+  *temp.mutable_constant_uniform_fact() = fact;
+  *facts->mutable_fact()->Add() = temp;
+}
+
+// Reinterpret the bits of |value| as a 32-bit unsigned int
+uint32_t FloatBitsAsUint(float value) {
+  uint32_t result;
+  memcpy(&result, &value, sizeof(float));
+  return result;
+}
+
 // Assembles the given |shader| text, and then runs the fuzzer |num_runs|
 // times, using successive seeds starting from |initial_seed|.  Checks that
 // the binary produced after each fuzzer run is valid, and that replaying
@@ -953,30 +973,659 @@ TEST(FuzzerReplayerTest, Miscellaneous3) {
 
   // Add the facts "resolution.x == 250" and "resolution.y == 100".
   protobufs::FactSequence facts;
-  {
-    protobufs::FactConstantUniform resolution_x_eq_250;
-    *resolution_x_eq_250.mutable_uniform_buffer_element_descriptor() =
-        MakeUniformBufferElementDescriptor(0, 0, {0, 0});
-    *resolution_x_eq_250.mutable_constant_word()->Add() = 250;
-    protobufs::Fact temp;
-    *temp.mutable_constant_uniform_fact() = resolution_x_eq_250;
-    *facts.mutable_fact()->Add() = temp;
-  }
-  {
-    protobufs::FactConstantUniform resolution_y_eq_100;
-    *resolution_y_eq_100.mutable_uniform_buffer_element_descriptor() =
-        MakeUniformBufferElementDescriptor(0, 0, {0, 1});
-    *resolution_y_eq_100.mutable_constant_word()->Add() = 100;
-    protobufs::Fact temp;
-    *temp.mutable_constant_uniform_fact() = resolution_y_eq_100;
-    *facts.mutable_fact()->Add() = temp;
-  }
+  AddConstantUniformFact(&facts, 0, 0, {0, 0}, 250);
+  AddConstantUniformFact(&facts, 0, 0, {0, 1}, 100);
 
   // Do some fuzzer runs, starting from an initial seed of 94 (seed value chosen
   // arbitrarily).
   RunFuzzerAndReplayer(shader, facts, 94, kNumFuzzerRuns);
 }
 
+TEST(FuzzerReplayerTest, Miscellaneous4) {
+  // The SPIR-V comes from the 'matrices_smart_loops' GLSL shader that ships
+  // with GraphicsFuzz.
+
+  std::string shader = R"(
+               OpCapability Shader
+          %1 = OpExtInstImport "GLSL.std.450"
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint Fragment %4 "main" %327 %363 %65 %70 %80 %90 %99 %108 %117 %126 %135 %144 %333
+               OpExecutionMode %4 OriginUpperLeft
+               OpSource ESSL 310
+               OpName %4 "main"
+               OpName %8 "matrix_number"
+               OpName %12 "cols"
+               OpName %23 "rows"
+               OpName %31 "c"
+               OpName %41 "r"
+               OpName %65 "m22"
+               OpName %68 "buf0"
+               OpMemberName %68 0 "one"
+               OpName %70 ""
+               OpName %80 "m23"
+               OpName %90 "m24"
+               OpName %99 "m32"
+               OpName %108 "m33"
+               OpName %117 "m34"
+               OpName %126 "m42"
+               OpName %135 "m43"
+               OpName %144 "m44"
+               OpName %164 "sum_index"
+               OpName %165 "cols"
+               OpName %173 "rows"
+               OpName %184 "sums"
+               OpName %189 "c"
+               OpName %198 "r"
+               OpName %325 "region_x"
+               OpName %327 "gl_FragCoord"
+               OpName %331 "buf1"
+               OpMemberName %331 0 "resolution"
+               OpName %333 ""
+               OpName %340 "region_y"
+               OpName %348 "overall_region"
+               OpName %363 "_GLF_color"
+               OpDecorate %8 RelaxedPrecision
+               OpDecorate %12 RelaxedPrecision
+               OpDecorate %19 RelaxedPrecision
+               OpDecorate %23 RelaxedPrecision
+               OpDecorate %29 RelaxedPrecision
+               OpDecorate %31 RelaxedPrecision
+               OpDecorate %38 RelaxedPrecision
+               OpDecorate %39 RelaxedPrecision
+               OpDecorate %41 RelaxedPrecision
+               OpDecorate %47 RelaxedPrecision
+               OpDecorate %48 RelaxedPrecision
+               OpDecorate %50 RelaxedPrecision
+               OpDecorate %66 RelaxedPrecision
+               OpDecorate %67 RelaxedPrecision
+               OpMemberDecorate %68 0 Offset 0
+               OpDecorate %68 Block
+               OpDecorate %70 DescriptorSet 0
+               OpDecorate %70 Binding 0
+               OpDecorate %81 RelaxedPrecision
+               OpDecorate %82 RelaxedPrecision
+               OpDecorate %91 RelaxedPrecision
+               OpDecorate %92 RelaxedPrecision
+               OpDecorate %100 RelaxedPrecision
+               OpDecorate %101 RelaxedPrecision
+               OpDecorate %109 RelaxedPrecision
+               OpDecorate %110 RelaxedPrecision
+               OpDecorate %118 RelaxedPrecision
+               OpDecorate %119 RelaxedPrecision
+               OpDecorate %127 RelaxedPrecision
+               OpDecorate %128 RelaxedPrecision
+               OpDecorate %136 RelaxedPrecision
+               OpDecorate %137 RelaxedPrecision
+               OpDecorate %145 RelaxedPrecision
+               OpDecorate %146 RelaxedPrecision
+               OpDecorate %152 RelaxedPrecision
+               OpDecorate %154 RelaxedPrecision
+               OpDecorate %155 RelaxedPrecision
+               OpDecorate %156 RelaxedPrecision
+               OpDecorate %157 RelaxedPrecision
+               OpDecorate %159 RelaxedPrecision
+               OpDecorate %160 RelaxedPrecision
+               OpDecorate %161 RelaxedPrecision
+               OpDecorate %162 RelaxedPrecision
+               OpDecorate %163 RelaxedPrecision
+               OpDecorate %164 RelaxedPrecision
+               OpDecorate %165 RelaxedPrecision
+               OpDecorate %171 RelaxedPrecision
+               OpDecorate %173 RelaxedPrecision
+               OpDecorate %179 RelaxedPrecision
+               OpDecorate %185 RelaxedPrecision
+               OpDecorate %189 RelaxedPrecision
+               OpDecorate %195 RelaxedPrecision
+               OpDecorate %196 RelaxedPrecision
+               OpDecorate %198 RelaxedPrecision
+               OpDecorate %204 RelaxedPrecision
+               OpDecorate %205 RelaxedPrecision
+               OpDecorate %207 RelaxedPrecision
+               OpDecorate %218 RelaxedPrecision
+               OpDecorate %219 RelaxedPrecision
+               OpDecorate %220 RelaxedPrecision
+               OpDecorate %228 RelaxedPrecision
+               OpDecorate %229 RelaxedPrecision
+               OpDecorate %230 RelaxedPrecision
+               OpDecorate %238 RelaxedPrecision
+               OpDecorate %239 RelaxedPrecision
+               OpDecorate %240 RelaxedPrecision
+               OpDecorate %248 RelaxedPrecision
+               OpDecorate %249 RelaxedPrecision
+               OpDecorate %250 RelaxedPrecision
+               OpDecorate %258 RelaxedPrecision
+               OpDecorate %259 RelaxedPrecision
+               OpDecorate %260 RelaxedPrecision
+               OpDecorate %268 RelaxedPrecision
+               OpDecorate %269 RelaxedPrecision
+               OpDecorate %270 RelaxedPrecision
+               OpDecorate %278 RelaxedPrecision
+               OpDecorate %279 RelaxedPrecision
+               OpDecorate %280 RelaxedPrecision
+               OpDecorate %288 RelaxedPrecision
+               OpDecorate %289 RelaxedPrecision
+               OpDecorate %290 RelaxedPrecision
+               OpDecorate %298 RelaxedPrecision
+               OpDecorate %299 RelaxedPrecision
+               OpDecorate %300 RelaxedPrecision
+               OpDecorate %309 RelaxedPrecision
+               OpDecorate %310 RelaxedPrecision
+               OpDecorate %311 RelaxedPrecision
+               OpDecorate %312 RelaxedPrecision
+               OpDecorate %313 RelaxedPrecision
+               OpDecorate %319 RelaxedPrecision
+               OpDecorate %320 RelaxedPrecision
+               OpDecorate %321 RelaxedPrecision
+               OpDecorate %322 RelaxedPrecision
+               OpDecorate %323 RelaxedPrecision
+               OpDecorate %324 RelaxedPrecision
+               OpDecorate %325 RelaxedPrecision
+               OpDecorate %327 BuiltIn FragCoord
+               OpMemberDecorate %331 0 Offset 0
+               OpDecorate %331 Block
+               OpDecorate %333 DescriptorSet 0
+               OpDecorate %333 Binding 1
+               OpDecorate %339 RelaxedPrecision
+               OpDecorate %340 RelaxedPrecision
+               OpDecorate %347 RelaxedPrecision
+               OpDecorate %348 RelaxedPrecision
+               OpDecorate %349 RelaxedPrecision
+               OpDecorate %351 RelaxedPrecision
+               OpDecorate %352 RelaxedPrecision
+               OpDecorate %353 RelaxedPrecision
+               OpDecorate %354 RelaxedPrecision
+               OpDecorate %356 RelaxedPrecision
+               OpDecorate %363 Location 0
+               OpDecorate %364 RelaxedPrecision
+          %2 = OpTypeVoid
+          %3 = OpTypeFunction %2
+          %6 = OpTypeInt 32 0
+          %7 = OpTypePointer Function %6
+          %9 = OpConstant %6 0
+         %10 = OpTypeInt 32 1
+         %11 = OpTypePointer Function %10
+         %13 = OpConstant %10 2
+         %20 = OpConstant %10 4
+         %21 = OpTypeBool
+         %32 = OpConstant %10 0
+         %61 = OpTypeFloat 32
+         %62 = OpTypeVector %61 2
+         %63 = OpTypeMatrix %62 2
+         %64 = OpTypePointer Private %63
+         %65 = OpVariable %64 Private
+         %68 = OpTypeStruct %61
+         %69 = OpTypePointer Uniform %68
+         %70 = OpVariable %69 Uniform
+         %71 = OpTypePointer Uniform %61
+         %74 = OpTypePointer Private %61
+         %77 = OpTypeVector %61 3
+         %78 = OpTypeMatrix %77 2
+         %79 = OpTypePointer Private %78
+         %80 = OpVariable %79 Private
+         %87 = OpTypeVector %61 4
+         %88 = OpTypeMatrix %87 2
+         %89 = OpTypePointer Private %88
+         %90 = OpVariable %89 Private
+         %97 = OpTypeMatrix %62 3
+         %98 = OpTypePointer Private %97
+         %99 = OpVariable %98 Private
+        %106 = OpTypeMatrix %77 3
+        %107 = OpTypePointer Private %106
+        %108 = OpVariable %107 Private
+        %115 = OpTypeMatrix %87 3
+        %116 = OpTypePointer Private %115
+        %117 = OpVariable %116 Private
+        %124 = OpTypeMatrix %62 4
+        %125 = OpTypePointer Private %124
+        %126 = OpVariable %125 Private
+        %133 = OpTypeMatrix %77 4
+        %134 = OpTypePointer Private %133
+        %135 = OpVariable %134 Private
+        %142 = OpTypeMatrix %87 4
+        %143 = OpTypePointer Private %142
+        %144 = OpVariable %143 Private
+        %153 = OpConstant %10 1
+        %158 = OpConstant %6 1
+        %181 = OpConstant %6 9
+        %182 = OpTypeArray %61 %181
+        %183 = OpTypePointer Function %182
+        %186 = OpConstant %61 0
+        %187 = OpTypePointer Function %61
+        %314 = OpConstant %61 16
+        %326 = OpTypePointer Input %87
+        %327 = OpVariable %326 Input
+        %328 = OpTypePointer Input %61
+        %331 = OpTypeStruct %62
+        %332 = OpTypePointer Uniform %331
+        %333 = OpVariable %332 Uniform
+        %336 = OpConstant %61 3
+        %350 = OpConstant %10 3
+        %357 = OpConstant %10 9
+        %362 = OpTypePointer Output %87
+        %363 = OpVariable %362 Output
+        %368 = OpConstant %61 1
+        %374 = OpConstantComposite %87 %186 %186 %186 %368
+          %4 = OpFunction %2 None %3
+          %5 = OpLabel
+          %8 = OpVariable %7 Function
+         %12 = OpVariable %11 Function
+         %23 = OpVariable %11 Function
+         %31 = OpVariable %11 Function
+         %41 = OpVariable %11 Function
+        %164 = OpVariable %11 Function
+        %165 = OpVariable %11 Function
+        %173 = OpVariable %11 Function
+        %184 = OpVariable %183 Function
+        %189 = OpVariable %11 Function
+        %198 = OpVariable %11 Function
+        %325 = OpVariable %11 Function
+        %340 = OpVariable %11 Function
+        %348 = OpVariable %11 Function
+               OpStore %8 %9
+               OpStore %12 %13
+               OpBranch %14
+         %14 = OpLabel
+               OpLoopMerge %16 %17 None
+               OpBranch %18
+         %18 = OpLabel
+         %19 = OpLoad %10 %12
+         %22 = OpSLessThanEqual %21 %19 %20
+               OpBranchConditional %22 %15 %16
+         %15 = OpLabel
+               OpStore %23 %13
+               OpBranch %24
+         %24 = OpLabel
+               OpLoopMerge %26 %27 None
+               OpBranch %28
+         %28 = OpLabel
+         %29 = OpLoad %10 %23
+         %30 = OpSLessThanEqual %21 %29 %20
+               OpBranchConditional %30 %25 %26
+         %25 = OpLabel
+               OpStore %31 %32
+               OpBranch %33
+         %33 = OpLabel
+               OpLoopMerge %35 %36 None
+               OpBranch %37
+         %37 = OpLabel
+         %38 = OpLoad %10 %31
+         %39 = OpLoad %10 %12
+         %40 = OpSLessThan %21 %38 %39
+               OpBranchConditional %40 %34 %35
+         %34 = OpLabel
+               OpStore %41 %32
+               OpBranch %42
+         %42 = OpLabel
+               OpLoopMerge %44 %45 None
+               OpBranch %46
+         %46 = OpLabel
+         %47 = OpLoad %10 %41
+         %48 = OpLoad %10 %23
+         %49 = OpSLessThan %21 %47 %48
+               OpBranchConditional %49 %43 %44
+         %43 = OpLabel
+         %50 = OpLoad %6 %8
+               OpSelectionMerge %60 None
+               OpSwitch %50 %60 0 %51 1 %52 2 %53 3 %54 4 %55 5 %56 6 %57 7 %58 8 %59
+         %51 = OpLabel
+         %66 = OpLoad %10 %31
+         %67 = OpLoad %10 %41
+         %72 = OpAccessChain %71 %70 %32
+         %73 = OpLoad %61 %72
+         %75 = OpAccessChain %74 %65 %66 %67
+               OpStore %75 %73
+               OpBranch %60
+         %52 = OpLabel
+         %81 = OpLoad %10 %31
+         %82 = OpLoad %10 %41
+         %83 = OpAccessChain %71 %70 %32
+         %84 = OpLoad %61 %83
+         %85 = OpAccessChain %74 %80 %81 %82
+               OpStore %85 %84
+               OpBranch %60
+         %53 = OpLabel
+         %91 = OpLoad %10 %31
+         %92 = OpLoad %10 %41
+         %93 = OpAccessChain %71 %70 %32
+         %94 = OpLoad %61 %93
+         %95 = OpAccessChain %74 %90 %91 %92
+               OpStore %95 %94
+               OpBranch %60
+         %54 = OpLabel
+        %100 = OpLoad %10 %31
+        %101 = OpLoad %10 %41
+        %102 = OpAccessChain %71 %70 %32
+        %103 = OpLoad %61 %102
+        %104 = OpAccessChain %74 %99 %100 %101
+               OpStore %104 %103
+               OpBranch %60
+         %55 = OpLabel
+        %109 = OpLoad %10 %31
+        %110 = OpLoad %10 %41
+        %111 = OpAccessChain %71 %70 %32
+        %112 = OpLoad %61 %111
+        %113 = OpAccessChain %74 %108 %109 %110
+               OpStore %113 %112
+               OpBranch %60
+         %56 = OpLabel
+        %118 = OpLoad %10 %31
+        %119 = OpLoad %10 %41
+        %120 = OpAccessChain %71 %70 %32
+        %121 = OpLoad %61 %120
+        %122 = OpAccessChain %74 %117 %118 %119
+               OpStore %122 %121
+               OpBranch %60
+         %57 = OpLabel
+        %127 = OpLoad %10 %31
+        %128 = OpLoad %10 %41
+        %129 = OpAccessChain %71 %70 %32
+        %130 = OpLoad %61 %129
+        %131 = OpAccessChain %74 %126 %127 %128
+               OpStore %131 %130
+               OpBranch %60
+         %58 = OpLabel
+        %136 = OpLoad %10 %31
+        %137 = OpLoad %10 %41
+        %138 = OpAccessChain %71 %70 %32
+        %139 = OpLoad %61 %138
+        %140 = OpAccessChain %74 %135 %136 %137
+               OpStore %140 %139
+               OpBranch %60
+         %59 = OpLabel
+        %145 = OpLoad %10 %31
+        %146 = OpLoad %10 %41
+        %147 = OpAccessChain %71 %70 %32
+        %148 = OpLoad %61 %147
+        %149 = OpAccessChain %74 %144 %145 %146
+               OpStore %149 %148
+               OpBranch %60
+         %60 = OpLabel
+               OpBranch %45
+         %45 = OpLabel
+        %152 = OpLoad %10 %41
+        %154 = OpIAdd %10 %152 %153
+               OpStore %41 %154
+               OpBranch %42
+         %44 = OpLabel
+               OpBranch %36
+         %36 = OpLabel
+        %155 = OpLoad %10 %31
+        %156 = OpIAdd %10 %155 %153
+               OpStore %31 %156
+               OpBranch %33
+         %35 = OpLabel
+        %157 = OpLoad %6 %8
+        %159 = OpIAdd %6 %157 %158
+               OpStore %8 %159
+               OpBranch %27
+         %27 = OpLabel
+        %160 = OpLoad %10 %23
+        %161 = OpIAdd %10 %160 %153
+               OpStore %23 %161
+               OpBranch %24
+         %26 = OpLabel
+               OpBranch %17
+         %17 = OpLabel
+        %162 = OpLoad %10 %12
+        %163 = OpIAdd %10 %162 %153
+               OpStore %12 %163
+               OpBranch %14
+         %16 = OpLabel
+               OpStore %164 %32
+               OpStore %165 %13
+               OpBranch %166
+        %166 = OpLabel
+               OpLoopMerge %168 %169 None
+               OpBranch %170
+        %170 = OpLabel
+        %171 = OpLoad %10 %165
+        %172 = OpSLessThanEqual %21 %171 %20
+               OpBranchConditional %172 %167 %168
+        %167 = OpLabel
+               OpStore %173 %13
+               OpBranch %174
+        %174 = OpLabel
+               OpLoopMerge %176 %177 None
+               OpBranch %178
+        %178 = OpLabel
+        %179 = OpLoad %10 %173
+        %180 = OpSLessThanEqual %21 %179 %20
+               OpBranchConditional %180 %175 %176
+        %175 = OpLabel
+        %185 = OpLoad %10 %164
+        %188 = OpAccessChain %187 %184 %185
+               OpStore %188 %186
+               OpStore %189 %32
+               OpBranch %190
+        %190 = OpLabel
+               OpLoopMerge %192 %193 None
+               OpBranch %194
+        %194 = OpLabel
+        %195 = OpLoad %10 %189
+        %196 = OpLoad %10 %165
+        %197 = OpSLessThan %21 %195 %196
+               OpBranchConditional %197 %191 %192
+        %191 = OpLabel
+               OpStore %198 %32
+               OpBranch %199
+        %199 = OpLabel
+               OpLoopMerge %201 %202 None
+               OpBranch %203
+        %203 = OpLabel
+        %204 = OpLoad %10 %198
+        %205 = OpLoad %10 %173
+        %206 = OpSLessThan %21 %204 %205
+               OpBranchConditional %206 %200 %201
+        %200 = OpLabel
+        %207 = OpLoad %10 %164
+               OpSelectionMerge %217 None
+               OpSwitch %207 %217 0 %208 1 %209 2 %210 3 %211 4 %212 5 %213 6 %214 7 %215 8 %216
+        %208 = OpLabel
+        %218 = OpLoad %10 %164
+        %219 = OpLoad %10 %189
+        %220 = OpLoad %10 %198
+        %221 = OpAccessChain %74 %65 %219 %220
+        %222 = OpLoad %61 %221
+        %223 = OpAccessChain %187 %184 %218
+        %224 = OpLoad %61 %223
+        %225 = OpFAdd %61 %224 %222
+        %226 = OpAccessChain %187 %184 %218
+               OpStore %226 %225
+               OpBranch %217
+        %209 = OpLabel
+        %228 = OpLoad %10 %164
+        %229 = OpLoad %10 %189
+        %230 = OpLoad %10 %198
+        %231 = OpAccessChain %74 %80 %229 %230
+        %232 = OpLoad %61 %231
+        %233 = OpAccessChain %187 %184 %228
+        %234 = OpLoad %61 %233
+        %235 = OpFAdd %61 %234 %232
+        %236 = OpAccessChain %187 %184 %228
+               OpStore %236 %235
+               OpBranch %217
+        %210 = OpLabel
+        %238 = OpLoad %10 %164
+        %239 = OpLoad %10 %189
+        %240 = OpLoad %10 %198
+        %241 = OpAccessChain %74 %90 %239 %240
+        %242 = OpLoad %61 %241
+        %243 = OpAccessChain %187 %184 %238
+        %244 = OpLoad %61 %243
+        %245 = OpFAdd %61 %244 %242
+        %246 = OpAccessChain %187 %184 %238
+               OpStore %246 %245
+               OpBranch %217
+        %211 = OpLabel
+        %248 = OpLoad %10 %164
+        %249 = OpLoad %10 %189
+        %250 = OpLoad %10 %198
+        %251 = OpAccessChain %74 %99 %249 %250
+        %252 = OpLoad %61 %251
+        %253 = OpAccessChain %187 %184 %248
+        %254 = OpLoad %61 %253
+        %255 = OpFAdd %61 %254 %252
+        %256 = OpAccessChain %187 %184 %248
+               OpStore %256 %255
+               OpBranch %217
+        %212 = OpLabel
+        %258 = OpLoad %10 %164
+        %259 = OpLoad %10 %189
+        %260 = OpLoad %10 %198
+        %261 = OpAccessChain %74 %108 %259 %260
+        %262 = OpLoad %61 %261
+        %263 = OpAccessChain %187 %184 %258
+        %264 = OpLoad %61 %263
+        %265 = OpFAdd %61 %264 %262
+        %266 = OpAccessChain %187 %184 %258
+               OpStore %266 %265
+               OpBranch %217
+        %213 = OpLabel
+        %268 = OpLoad %10 %164
+        %269 = OpLoad %10 %189
+        %270 = OpLoad %10 %198
+        %271 = OpAccessChain %74 %117 %269 %270
+        %272 = OpLoad %61 %271
+        %273 = OpAccessChain %187 %184 %268
+        %274 = OpLoad %61 %273
+        %275 = OpFAdd %61 %274 %272
+        %276 = OpAccessChain %187 %184 %268
+               OpStore %276 %275
+               OpBranch %217
+        %214 = OpLabel
+        %278 = OpLoad %10 %164
+        %279 = OpLoad %10 %189
+        %280 = OpLoad %10 %198
+        %281 = OpAccessChain %74 %126 %279 %280
+        %282 = OpLoad %61 %281
+        %283 = OpAccessChain %187 %184 %278
+        %284 = OpLoad %61 %283
+        %285 = OpFAdd %61 %284 %282
+        %286 = OpAccessChain %187 %184 %278
+               OpStore %286 %285
+               OpBranch %217
+        %215 = OpLabel
+        %288 = OpLoad %10 %164
+        %289 = OpLoad %10 %189
+        %290 = OpLoad %10 %198
+        %291 = OpAccessChain %74 %135 %289 %290
+        %292 = OpLoad %61 %291
+        %293 = OpAccessChain %187 %184 %288
+        %294 = OpLoad %61 %293
+        %295 = OpFAdd %61 %294 %292
+        %296 = OpAccessChain %187 %184 %288
+               OpStore %296 %295
+               OpBranch %217
+        %216 = OpLabel
+        %298 = OpLoad %10 %164
+        %299 = OpLoad %10 %189
+        %300 = OpLoad %10 %198
+        %301 = OpAccessChain %74 %144 %299 %300
+        %302 = OpLoad %61 %301
+        %303 = OpAccessChain %187 %184 %298
+        %304 = OpLoad %61 %303
+        %305 = OpFAdd %61 %304 %302
+        %306 = OpAccessChain %187 %184 %298
+               OpStore %306 %305
+               OpBranch %217
+        %217 = OpLabel
+               OpBranch %202
+        %202 = OpLabel
+        %309 = OpLoad %10 %198
+        %310 = OpIAdd %10 %309 %153
+               OpStore %198 %310
+               OpBranch %199
+        %201 = OpLabel
+               OpBranch %193
+        %193 = OpLabel
+        %311 = OpLoad %10 %189
+        %312 = OpIAdd %10 %311 %153
+               OpStore %189 %312
+               OpBranch %190
+        %192 = OpLabel
+        %313 = OpLoad %10 %164
+        %315 = OpAccessChain %187 %184 %313
+        %316 = OpLoad %61 %315
+        %317 = OpFDiv %61 %316 %314
+        %318 = OpAccessChain %187 %184 %313
+               OpStore %318 %317
+        %319 = OpLoad %10 %164
+        %320 = OpIAdd %10 %319 %153
+               OpStore %164 %320
+               OpBranch %177
+        %177 = OpLabel
+        %321 = OpLoad %10 %173
+        %322 = OpIAdd %10 %321 %153
+               OpStore %173 %322
+               OpBranch %174
+        %176 = OpLabel
+               OpBranch %169
+        %169 = OpLabel
+        %323 = OpLoad %10 %165
+        %324 = OpIAdd %10 %323 %153
+               OpStore %165 %324
+               OpBranch %166
+        %168 = OpLabel
+        %329 = OpAccessChain %328 %327 %9
+        %330 = OpLoad %61 %329
+        %334 = OpAccessChain %71 %333 %32 %9
+        %335 = OpLoad %61 %334
+        %337 = OpFDiv %61 %335 %336
+        %338 = OpFDiv %61 %330 %337
+        %339 = OpConvertFToS %10 %338
+               OpStore %325 %339
+        %341 = OpAccessChain %328 %327 %158
+        %342 = OpLoad %61 %341
+        %343 = OpAccessChain %71 %333 %32 %9
+        %344 = OpLoad %61 %343
+        %345 = OpFDiv %61 %344 %336
+        %346 = OpFDiv %61 %342 %345
+        %347 = OpConvertFToS %10 %346
+               OpStore %340 %347
+        %349 = OpLoad %10 %340
+        %351 = OpIMul %10 %349 %350
+        %352 = OpLoad %10 %325
+        %353 = OpIAdd %10 %351 %352
+               OpStore %348 %353
+        %354 = OpLoad %10 %348
+        %355 = OpSGreaterThan %21 %354 %32
+        %356 = OpLoad %10 %348
+        %358 = OpSLessThan %21 %356 %357
+        %359 = OpLogicalAnd %21 %355 %358
+               OpSelectionMerge %361 None
+               OpBranchConditional %359 %360 %373
+        %360 = OpLabel
+        %364 = OpLoad %10 %348
+        %365 = OpAccessChain %187 %184 %364
+        %366 = OpLoad %61 %365
+        %367 = OpCompositeConstruct %77 %366 %366 %366
+        %369 = OpCompositeExtract %61 %367 0
+        %370 = OpCompositeExtract %61 %367 1
+        %371 = OpCompositeExtract %61 %367 2
+        %372 = OpCompositeConstruct %87 %369 %370 %371 %368
+               OpStore %363 %372
+               OpBranch %361
+        %373 = OpLabel
+               OpStore %363 %374
+               OpBranch %361
+        %361 = OpLabel
+               OpReturn
+               OpFunctionEnd
+  )";
+
+  // Add the facts:
+  //  - "one == 1.0"
+  //  - "resolution.y == 256.0",
+  protobufs::FactSequence facts;
+  AddConstantUniformFact(&facts, 0, 0, {0}, FloatBitsAsUint(1.0));
+  AddConstantUniformFact(&facts, 0, 1, {0, 0}, FloatBitsAsUint(256.0));
+  AddConstantUniformFact(&facts, 0, 1, {0, 1}, FloatBitsAsUint(256.0));
+
+  // Do some fuzzer runs, starting from an initial seed of 14 (seed value chosen
+  // arbitrarily).
+  RunFuzzerAndReplayer(shader, facts, 14, kNumFuzzerRuns);
+}
+
 }  // namespace
 }  // namespace fuzz
 }  // namespace spvtools

+ 69 - 0
3rdparty/spirv-tools/test/fuzz/instruction_descriptor_test.cpp

@@ -0,0 +1,69 @@
+// Copyright (c) 2019 Google LLC
+//
+// 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 "source/fuzz/instruction_descriptor.h"
+#include "test/fuzz/fuzz_test_util.h"
+
+namespace spvtools {
+namespace fuzz {
+namespace {
+
+TEST(InstructionDescriptorTest, BasicTest) {
+  std::string shader = R"(
+               OpCapability Shader
+          %1 = OpExtInstImport "GLSL.std.450"
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint Fragment %4 "main"
+               OpExecutionMode %4 OriginUpperLeft
+               OpSource ESSL 310
+          %2 = OpTypeVoid
+          %3 = OpTypeFunction %2
+          %6 = OpTypeInt 32 0
+          %7 = OpTypePointer Function %6
+          %9 = OpConstant %6 0
+         %10 = OpTypeInt 32 1
+         %11 = OpTypePointer Function %10
+         %13 = OpConstant %10 2
+         %32 = OpConstant %10 0
+          %4 = OpFunction %2 None %3
+          %5 = OpLabel
+        %164 = OpVariable %11 Function
+        %165 = OpVariable %11 Function
+               OpBranch %16
+         %16 = OpLabel
+               OpStore %164 %32
+               OpStore %165 %13
+               OpReturn
+               OpFunctionEnd
+  )";
+
+  const auto env = SPV_ENV_UNIVERSAL_1_4;
+  const auto consumer = nullptr;
+  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
+  ASSERT_TRUE(IsValid(env, context.get()));
+
+  for (auto& function : *context->module()) {
+    for (auto& block : function) {
+      for (auto inst_it = block.cbegin(); inst_it != block.cend(); ++inst_it) {
+        ASSERT_EQ(&*inst_it,
+                  FindInstruction(MakeInstructionDescriptor(block, inst_it),
+                                  context.get()));
+      }
+    }
+  }
+}
+
+}  // namespace
+}  // namespace fuzz
+}  // namespace spvtools

+ 4 - 3
3rdparty/spirv-tools/test/fuzz/transformation_construct_composite_test.cpp

@@ -28,10 +28,11 @@ bool SynonymFactHolds(const FactManager& fact_manager, uint32_t id,
     return false;
   }
   auto synonyms = fact_manager.GetSynonymsForId(id);
-  auto temp = MakeDataDescriptor(synonym_base_id, std::move(synonym_indices));
+  auto temp =
+      MakeDataDescriptor(synonym_base_id, std::move(synonym_indices), 1);
   return std::find_if(synonyms.begin(), synonyms.end(),
-                      [&temp](protobufs::DataDescriptor dd) -> bool {
-                        return DataDescriptorEquals()(&dd, &temp);
+                      [&temp](const protobufs::DataDescriptor* dd) -> bool {
+                        return DataDescriptorEquals()(dd, &temp);
                       }) != synonyms.end();
 }
 

+ 65 - 55
3rdparty/spirv-tools/test/fuzz/transformation_copy_object_test.cpp

@@ -12,9 +12,13 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "source/fuzz/transformation_copy_object.h"
+#include <algorithm>
+#include <set>
+#include <unordered_set>
+
 #include "source/fuzz/data_descriptor.h"
 #include "source/fuzz/instruction_descriptor.h"
+#include "source/fuzz/transformation_copy_object.h"
 #include "test/fuzz/fuzz_test_util.h"
 
 namespace spvtools {
@@ -50,60 +54,66 @@ TEST(TransformationCopyObjectTest, CopyBooleanConstants) {
 
   ASSERT_EQ(0, fact_manager.GetIdsForWhichSynonymsAreKnown().size());
 
-  TransformationCopyObject copy_true(
-      7, MakeInstructionDescriptor(5, SpvOpReturn, 0), 100);
-  ASSERT_TRUE(copy_true.IsApplicable(context.get(), fact_manager));
-  copy_true.Apply(context.get(), &fact_manager);
-
-  const std::set<uint32_t>& ids_for_which_synonyms_are_known =
-      fact_manager.GetIdsForWhichSynonymsAreKnown();
-  ASSERT_EQ(1, ids_for_which_synonyms_are_known.size());
-  ASSERT_TRUE(ids_for_which_synonyms_are_known.find(7) !=
-              ids_for_which_synonyms_are_known.end());
-  ASSERT_EQ(1, fact_manager.GetSynonymsForId(7).size());
-  protobufs::DataDescriptor descriptor_100 = MakeDataDescriptor(100, {});
-  ASSERT_TRUE(DataDescriptorEquals()(&descriptor_100,
-                                     &fact_manager.GetSynonymsForId(7)[0]));
-
-  TransformationCopyObject copy_false(
-      8, MakeInstructionDescriptor(100, SpvOpReturn, 0), 101);
-  ASSERT_TRUE(copy_false.IsApplicable(context.get(), fact_manager));
-  copy_false.Apply(context.get(), &fact_manager);
-  ASSERT_EQ(2, ids_for_which_synonyms_are_known.size());
-  ASSERT_TRUE(ids_for_which_synonyms_are_known.find(8) !=
-              ids_for_which_synonyms_are_known.end());
-  ASSERT_EQ(1, fact_manager.GetSynonymsForId(8).size());
-  protobufs::DataDescriptor descriptor_101 = MakeDataDescriptor(101, {});
-  ASSERT_TRUE(DataDescriptorEquals()(&descriptor_101,
-                                     &fact_manager.GetSynonymsForId(8)[0]));
-
-  TransformationCopyObject copy_false_again(
-      101, MakeInstructionDescriptor(5, SpvOpReturn, 0), 102);
-  ASSERT_TRUE(copy_false_again.IsApplicable(context.get(), fact_manager));
-  copy_false_again.Apply(context.get(), &fact_manager);
-  ASSERT_EQ(3, ids_for_which_synonyms_are_known.size());
-  ASSERT_TRUE(ids_for_which_synonyms_are_known.find(101) !=
-              ids_for_which_synonyms_are_known.end());
-  ASSERT_EQ(1, fact_manager.GetSynonymsForId(101).size());
-  protobufs::DataDescriptor descriptor_102 = MakeDataDescriptor(102, {});
-  ASSERT_TRUE(DataDescriptorEquals()(&descriptor_102,
-                                     &fact_manager.GetSynonymsForId(101)[0]));
-
-  TransformationCopyObject copy_true_again(
-      7, MakeInstructionDescriptor(102, SpvOpReturn, 0), 103);
-  ASSERT_TRUE(copy_true_again.IsApplicable(context.get(), fact_manager));
-  copy_true_again.Apply(context.get(), &fact_manager);
-  // This does re-uses an id for which synonyms are already known, so the count
-  // of such ids does not change.
-  ASSERT_EQ(3, ids_for_which_synonyms_are_known.size());
-  ASSERT_TRUE(ids_for_which_synonyms_are_known.find(7) !=
-              ids_for_which_synonyms_are_known.end());
-  ASSERT_EQ(2, fact_manager.GetSynonymsForId(7).size());
-  protobufs::DataDescriptor descriptor_103 = MakeDataDescriptor(103, {});
-  ASSERT_TRUE(DataDescriptorEquals()(&descriptor_103,
-                                     &fact_manager.GetSynonymsForId(7)[0]) ||
-              DataDescriptorEquals()(&descriptor_103,
-                                     &fact_manager.GetSynonymsForId(7)[1]));
+  {
+    TransformationCopyObject copy_true(
+        7, MakeInstructionDescriptor(5, SpvOpReturn, 0), 100);
+    ASSERT_TRUE(copy_true.IsApplicable(context.get(), fact_manager));
+    copy_true.Apply(context.get(), &fact_manager);
+
+    std::set<uint32_t> ids_for_which_synonyms_are_known =
+        fact_manager.GetIdsForWhichSynonymsAreKnown();
+    ASSERT_EQ(2, ids_for_which_synonyms_are_known.size());
+    ASSERT_TRUE(ids_for_which_synonyms_are_known.find(7) !=
+                ids_for_which_synonyms_are_known.end());
+    ASSERT_EQ(2, fact_manager.GetSynonymsForId(7).size());
+    protobufs::DataDescriptor descriptor_100 = MakeDataDescriptor(100, {}, 1);
+    ASSERT_TRUE(fact_manager.GetSynonymsForId(7).count(&descriptor_100) > 0);
+  }
+
+  {
+    TransformationCopyObject copy_false(
+        8, MakeInstructionDescriptor(100, SpvOpReturn, 0), 101);
+    ASSERT_TRUE(copy_false.IsApplicable(context.get(), fact_manager));
+    copy_false.Apply(context.get(), &fact_manager);
+    std::set<uint32_t> ids_for_which_synonyms_are_known =
+        fact_manager.GetIdsForWhichSynonymsAreKnown();
+    ASSERT_EQ(4, ids_for_which_synonyms_are_known.size());
+    ASSERT_TRUE(ids_for_which_synonyms_are_known.find(8) !=
+                ids_for_which_synonyms_are_known.end());
+    ASSERT_EQ(2, fact_manager.GetSynonymsForId(8).size());
+    protobufs::DataDescriptor descriptor_101 = MakeDataDescriptor(101, {}, 1);
+    ASSERT_TRUE(fact_manager.GetSynonymsForId(8).count(&descriptor_101) > 0);
+  }
+
+  {
+    TransformationCopyObject copy_false_again(
+        101, MakeInstructionDescriptor(5, SpvOpReturn, 0), 102);
+    ASSERT_TRUE(copy_false_again.IsApplicable(context.get(), fact_manager));
+    copy_false_again.Apply(context.get(), &fact_manager);
+    std::set<uint32_t> ids_for_which_synonyms_are_known =
+        fact_manager.GetIdsForWhichSynonymsAreKnown();
+    ASSERT_EQ(5, ids_for_which_synonyms_are_known.size());
+    ASSERT_TRUE(ids_for_which_synonyms_are_known.find(101) !=
+                ids_for_which_synonyms_are_known.end());
+    ASSERT_EQ(3, fact_manager.GetSynonymsForId(101).size());
+    protobufs::DataDescriptor descriptor_102 = MakeDataDescriptor(102, {}, 1);
+    ASSERT_TRUE(fact_manager.GetSynonymsForId(101).count(&descriptor_102) > 0);
+  }
+
+  {
+    TransformationCopyObject copy_true_again(
+        7, MakeInstructionDescriptor(102, SpvOpReturn, 0), 103);
+    ASSERT_TRUE(copy_true_again.IsApplicable(context.get(), fact_manager));
+    copy_true_again.Apply(context.get(), &fact_manager);
+    std::set<uint32_t> ids_for_which_synonyms_are_known =
+        fact_manager.GetIdsForWhichSynonymsAreKnown();
+    ASSERT_EQ(6, ids_for_which_synonyms_are_known.size());
+    ASSERT_TRUE(ids_for_which_synonyms_are_known.find(7) !=
+                ids_for_which_synonyms_are_known.end());
+    ASSERT_EQ(3, fact_manager.GetSynonymsForId(7).size());
+    protobufs::DataDescriptor descriptor_103 = MakeDataDescriptor(103, {}, 1);
+    ASSERT_TRUE(fact_manager.GetSynonymsForId(7).count(&descriptor_103) > 0);
+  }
 
   std::string after_transformation = R"(
                OpCapability Shader

+ 0 - 2194
3rdparty/spirv-tools/test/fuzz/transformation_replace_id_with_synonym_test.cpp

@@ -1,2194 +0,0 @@
-// Copyright (c) 2019 Google LLC
-//
-// 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 "source/fuzz/transformation_replace_id_with_synonym.h"
-#include "source/fuzz/data_descriptor.h"
-#include "source/fuzz/id_use_descriptor.h"
-#include "source/fuzz/instruction_descriptor.h"
-#include "test/fuzz/fuzz_test_util.h"
-
-namespace spvtools {
-namespace fuzz {
-namespace {
-
-// The following shader was obtained from this GLSL, which was then optimized
-// with spirv-opt -O and manually edited to include some uses of OpCopyObject
-// (to introduce id synonyms).
-//
-// #version 310 es
-//
-// precision highp int;
-// precision highp float;
-//
-// layout(set = 0, binding = 0) uniform buf {
-//   int a;
-//   int b;
-//   int c;
-// };
-//
-// layout(location = 0) out vec4 color;
-//
-// void main() {
-//   int x = a;
-//   float f = 0.0;
-//   while (x < b) {
-//     switch(x % 4) {
-//       case 0:
-//         color[0] = f;
-//         break;
-//       case 1:
-//         color[1] = f;
-//         break;
-//       case 2:
-//         color[2] = f;
-//         break;
-//       case 3:
-//         color[3] = f;
-//         break;
-//       default:
-//         break;
-//     }
-//     if (x > c) {
-//       x++;
-//     } else {
-//       x += 2;
-//     }
-//   }
-//   color[0] += color[1] + float(x);
-// }
-const std::string kComplexShader = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main" %42
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %9 "buf"
-               OpMemberName %9 0 "a"
-               OpMemberName %9 1 "b"
-               OpMemberName %9 2 "c"
-               OpName %11 ""
-               OpName %42 "color"
-               OpMemberDecorate %9 0 Offset 0
-               OpMemberDecorate %9 1 Offset 4
-               OpMemberDecorate %9 2 Offset 8
-               OpDecorate %9 Block
-               OpDecorate %11 DescriptorSet 0
-               OpDecorate %11 Binding 0
-               OpDecorate %42 Location 0
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %9 = OpTypeStruct %6 %6 %6
-         %10 = OpTypePointer Uniform %9
-         %11 = OpVariable %10 Uniform
-         %12 = OpConstant %6 0
-         %13 = OpTypePointer Uniform %6
-         %16 = OpTypeFloat 32
-         %19 = OpConstant %16 0
-         %26 = OpConstant %6 1
-         %29 = OpTypeBool
-         %32 = OpConstant %6 4
-         %40 = OpTypeVector %16 4
-         %41 = OpTypePointer Output %40
-         %42 = OpVariable %41 Output
-         %44 = OpTypeInt 32 0
-         %45 = OpConstant %44 0
-         %46 = OpTypePointer Output %16
-         %50 = OpConstant %44 1
-         %54 = OpConstant %44 2
-         %58 = OpConstant %44 3
-         %64 = OpConstant %6 2
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-        %209 = OpCopyObject %6 %12
-         %14 = OpAccessChain %13 %11 %12
-         %15 = OpLoad %6 %14
-        %200 = OpCopyObject %6 %15
-               OpBranch %20
-         %20 = OpLabel
-         %84 = OpPhi %6 %15 %5 %86 %69
-         %27 = OpAccessChain %13 %11 %26
-         %28 = OpLoad %6 %27
-        %207 = OpCopyObject %6 %84
-        %201 = OpCopyObject %6 %15
-         %30 = OpSLessThan %29 %84 %28
-               OpLoopMerge %22 %69 None
-               OpBranchConditional %30 %21 %22
-         %21 = OpLabel
-         %33 = OpSMod %6 %84 %32
-        %208 = OpCopyObject %6 %33
-               OpSelectionMerge %39 None
-               OpSwitch %33 %38 0 %34 1 %35 2 %36 3 %37
-         %38 = OpLabel
-        %202 = OpCopyObject %6 %15
-               OpBranch %39
-         %34 = OpLabel
-        %210 = OpCopyObject %16 %19
-         %47 = OpAccessChain %46 %42 %45
-               OpStore %47 %19
-               OpBranch %39
-         %35 = OpLabel
-         %51 = OpAccessChain %46 %42 %50
-               OpStore %51 %19
-               OpBranch %39
-         %36 = OpLabel
-        %204 = OpCopyObject %44 %54
-         %55 = OpAccessChain %46 %42 %54
-        %203 = OpCopyObject %46 %55
-               OpStore %55 %19
-               OpBranch %39
-         %37 = OpLabel
-         %59 = OpAccessChain %46 %42 %58
-               OpStore %59 %19
-               OpBranch %39
-         %39 = OpLabel
-        %300 = OpIAdd %6 %15 %15
-         %65 = OpAccessChain %13 %11 %64
-         %66 = OpLoad %6 %65
-         %67 = OpSGreaterThan %29 %84 %66
-               OpSelectionMerge %69 None
-               OpBranchConditional %67 %68 %72
-         %68 = OpLabel
-         %71 = OpIAdd %6 %84 %26
-               OpBranch %69
-         %72 = OpLabel
-         %74 = OpIAdd %6 %84 %64
-        %205 = OpCopyObject %6 %74
-               OpBranch %69
-         %69 = OpLabel
-         %86 = OpPhi %6 %71 %68 %74 %72
-        %301 = OpPhi %6 %71 %68 %15 %72
-               OpBranch %20
-         %22 = OpLabel
-         %75 = OpAccessChain %46 %42 %50
-         %76 = OpLoad %16 %75
-         %78 = OpConvertSToF %16 %84
-         %80 = OpAccessChain %46 %42 %45
-        %206 = OpCopyObject %16 %78
-         %81 = OpLoad %16 %80
-         %79 = OpFAdd %16 %76 %78
-         %82 = OpFAdd %16 %81 %79
-               OpStore %80 %82
-               OpReturn
-               OpFunctionEnd
-)";
-
-protobufs::Fact MakeSynonymFact(uint32_t id, uint32_t synonym_object,
-                                std::vector<uint32_t> indices = {}) {
-  protobufs::FactIdSynonym id_synonym_fact;
-  id_synonym_fact.set_id(id);
-  id_synonym_fact.mutable_data_descriptor()->set_object(synonym_object);
-  for (auto index : indices) {
-    id_synonym_fact.mutable_data_descriptor()->add_index(index);
-  }
-  protobufs::Fact result;
-  *result.mutable_id_synonym_fact() = id_synonym_fact;
-  return result;
-}
-
-// Equips the fact manager with synonym facts for the above shader.
-void SetUpIdSynonyms(FactManager* fact_manager, opt::IRContext* context) {
-  fact_manager->AddFact(MakeSynonymFact(15, 200), context);
-  fact_manager->AddFact(MakeSynonymFact(15, 201), context);
-  fact_manager->AddFact(MakeSynonymFact(15, 202), context);
-  fact_manager->AddFact(MakeSynonymFact(55, 203), context);
-  fact_manager->AddFact(MakeSynonymFact(54, 204), context);
-  fact_manager->AddFact(MakeSynonymFact(74, 205), context);
-  fact_manager->AddFact(MakeSynonymFact(78, 206), context);
-  fact_manager->AddFact(MakeSynonymFact(84, 207), context);
-  fact_manager->AddFact(MakeSynonymFact(33, 208), context);
-  fact_manager->AddFact(MakeSynonymFact(12, 209), context);
-  fact_manager->AddFact(MakeSynonymFact(19, 210), context);
-}
-
-TEST(TransformationReplaceIdWithSynonymTest, IllegalTransformations) {
-  const auto env = SPV_ENV_UNIVERSAL_1_3;
-  const auto consumer = nullptr;
-  const auto context =
-      BuildModule(env, consumer, kComplexShader, kFuzzAssembleOption);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  FactManager fact_manager;
-  SetUpIdSynonyms(&fact_manager, context.get());
-
-  // %202 cannot replace %15 as in-operand 0 of %300, since %202 does not
-  // dominate %300.
-  auto synonym_does_not_dominate_use = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(15, MakeInstructionDescriptor(300, SpvOpIAdd, 0), 0),
-      MakeDataDescriptor(202, {}), 0);
-  ASSERT_FALSE(
-      synonym_does_not_dominate_use.IsApplicable(context.get(), fact_manager));
-
-  // %202 cannot replace %15 as in-operand 2 of %301, since this is the OpPhi's
-  // incoming value for block %72, and %202 does not dominate %72.
-  auto synonym_does_not_dominate_use_op_phi =
-      TransformationReplaceIdWithSynonym(
-          MakeIdUseDescriptor(15, MakeInstructionDescriptor(301, SpvOpPhi, 0),
-                              2),
-          MakeDataDescriptor(202, {}), 0);
-  ASSERT_FALSE(synonym_does_not_dominate_use_op_phi.IsApplicable(context.get(),
-                                                                 fact_manager));
-
-  // %200 is not a synonym for %84
-  auto id_in_use_is_not_synonymous = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          84, MakeInstructionDescriptor(67, SpvOpSGreaterThan, 0), 0),
-      MakeDataDescriptor(200, {}), 0);
-  ASSERT_FALSE(
-      id_in_use_is_not_synonymous.IsApplicable(context.get(), fact_manager));
-
-  // %86 is not a synonym for anything (and in particular not for %74)
-  auto id_has_no_synonyms = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(86, MakeInstructionDescriptor(84, SpvOpPhi, 0), 2),
-      MakeDataDescriptor(74, {}), 0);
-  ASSERT_FALSE(id_has_no_synonyms.IsApplicable(context.get(), fact_manager));
-
-  // This would lead to %207 = 'OpCopyObject %type %207' if it were allowed
-  auto synonym_use_is_in_synonym_definition =
-      TransformationReplaceIdWithSynonym(
-          MakeIdUseDescriptor(
-              84, MakeInstructionDescriptor(207, SpvOpCopyObject, 0), 0),
-          MakeDataDescriptor(207, {}), 0);
-  ASSERT_FALSE(synonym_use_is_in_synonym_definition.IsApplicable(context.get(),
-                                                                 fact_manager));
-
-  // The id use descriptor does not lead to a use (%84 is not used in the
-  // definition of %207)
-  auto bad_id_use_descriptor = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          84, MakeInstructionDescriptor(200, SpvOpCopyObject, 0), 0),
-      MakeDataDescriptor(207, {}), 0);
-  ASSERT_FALSE(bad_id_use_descriptor.IsApplicable(context.get(), fact_manager));
-
-  // This replacement would lead to an access chain into a struct using a
-  // non-constant index.
-  auto bad_access_chain = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          12, MakeInstructionDescriptor(14, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(209, {}), 0);
-  ASSERT_FALSE(bad_access_chain.IsApplicable(context.get(), fact_manager));
-}
-
-TEST(TransformationReplaceIdWithSynonymTest, LegalTransformations) {
-  const auto env = SPV_ENV_UNIVERSAL_1_3;
-  const auto consumer = nullptr;
-  const auto context =
-      BuildModule(env, consumer, kComplexShader, kFuzzAssembleOption);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  FactManager fact_manager;
-  SetUpIdSynonyms(&fact_manager, context.get());
-
-  auto global_constant_synonym = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(19, MakeInstructionDescriptor(47, SpvOpStore, 0), 1),
-      MakeDataDescriptor(210, {}), 0);
-  ASSERT_TRUE(
-      global_constant_synonym.IsApplicable(context.get(), fact_manager));
-  global_constant_synonym.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  auto replace_vector_access_chain_index = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          54, MakeInstructionDescriptor(55, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(204, {}), 0);
-  ASSERT_TRUE(replace_vector_access_chain_index.IsApplicable(context.get(),
-                                                             fact_manager));
-  replace_vector_access_chain_index.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // This is an interesting case because it replaces something that is being
-  // copied with something that is already a synonym.
-  auto regular_replacement = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          15, MakeInstructionDescriptor(202, SpvOpCopyObject, 0), 0),
-      MakeDataDescriptor(201, {}), 0);
-  ASSERT_TRUE(regular_replacement.IsApplicable(context.get(), fact_manager));
-  regular_replacement.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  auto regular_replacement2 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(55, MakeInstructionDescriptor(203, SpvOpStore, 0), 0),
-      MakeDataDescriptor(203, {}), 0);
-  ASSERT_TRUE(regular_replacement2.IsApplicable(context.get(), fact_manager));
-  regular_replacement2.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  auto good_op_phi = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(74, MakeInstructionDescriptor(86, SpvOpPhi, 0), 2),
-      MakeDataDescriptor(205, {}), 0);
-  ASSERT_TRUE(good_op_phi.IsApplicable(context.get(), fact_manager));
-  good_op_phi.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  const std::string after_transformation = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main" %42
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %9 "buf"
-               OpMemberName %9 0 "a"
-               OpMemberName %9 1 "b"
-               OpMemberName %9 2 "c"
-               OpName %11 ""
-               OpName %42 "color"
-               OpMemberDecorate %9 0 Offset 0
-               OpMemberDecorate %9 1 Offset 4
-               OpMemberDecorate %9 2 Offset 8
-               OpDecorate %9 Block
-               OpDecorate %11 DescriptorSet 0
-               OpDecorate %11 Binding 0
-               OpDecorate %42 Location 0
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %9 = OpTypeStruct %6 %6 %6
-         %10 = OpTypePointer Uniform %9
-         %11 = OpVariable %10 Uniform
-         %12 = OpConstant %6 0
-         %13 = OpTypePointer Uniform %6
-         %16 = OpTypeFloat 32
-         %19 = OpConstant %16 0
-         %26 = OpConstant %6 1
-         %29 = OpTypeBool
-         %32 = OpConstant %6 4
-         %40 = OpTypeVector %16 4
-         %41 = OpTypePointer Output %40
-         %42 = OpVariable %41 Output
-         %44 = OpTypeInt 32 0
-         %45 = OpConstant %44 0
-         %46 = OpTypePointer Output %16
-         %50 = OpConstant %44 1
-         %54 = OpConstant %44 2
-         %58 = OpConstant %44 3
-         %64 = OpConstant %6 2
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-        %209 = OpCopyObject %6 %12
-         %14 = OpAccessChain %13 %11 %12
-         %15 = OpLoad %6 %14
-        %200 = OpCopyObject %6 %15
-               OpBranch %20
-         %20 = OpLabel
-         %84 = OpPhi %6 %15 %5 %86 %69
-         %27 = OpAccessChain %13 %11 %26
-         %28 = OpLoad %6 %27
-        %207 = OpCopyObject %6 %84
-        %201 = OpCopyObject %6 %15
-         %30 = OpSLessThan %29 %84 %28
-               OpLoopMerge %22 %69 None
-               OpBranchConditional %30 %21 %22
-         %21 = OpLabel
-         %33 = OpSMod %6 %84 %32
-        %208 = OpCopyObject %6 %33
-               OpSelectionMerge %39 None
-               OpSwitch %33 %38 0 %34 1 %35 2 %36 3 %37
-         %38 = OpLabel
-        %202 = OpCopyObject %6 %201
-               OpBranch %39
-         %34 = OpLabel
-        %210 = OpCopyObject %16 %19
-         %47 = OpAccessChain %46 %42 %45
-               OpStore %47 %210
-               OpBranch %39
-         %35 = OpLabel
-         %51 = OpAccessChain %46 %42 %50
-               OpStore %51 %19
-               OpBranch %39
-         %36 = OpLabel
-        %204 = OpCopyObject %44 %54
-         %55 = OpAccessChain %46 %42 %204
-        %203 = OpCopyObject %46 %55
-               OpStore %203 %19
-               OpBranch %39
-         %37 = OpLabel
-         %59 = OpAccessChain %46 %42 %58
-               OpStore %59 %19
-               OpBranch %39
-         %39 = OpLabel
-        %300 = OpIAdd %6 %15 %15
-         %65 = OpAccessChain %13 %11 %64
-         %66 = OpLoad %6 %65
-         %67 = OpSGreaterThan %29 %84 %66
-               OpSelectionMerge %69 None
-               OpBranchConditional %67 %68 %72
-         %68 = OpLabel
-         %71 = OpIAdd %6 %84 %26
-               OpBranch %69
-         %72 = OpLabel
-         %74 = OpIAdd %6 %84 %64
-        %205 = OpCopyObject %6 %74
-               OpBranch %69
-         %69 = OpLabel
-         %86 = OpPhi %6 %71 %68 %205 %72
-        %301 = OpPhi %6 %71 %68 %15 %72
-               OpBranch %20
-         %22 = OpLabel
-         %75 = OpAccessChain %46 %42 %50
-         %76 = OpLoad %16 %75
-         %78 = OpConvertSToF %16 %84
-         %80 = OpAccessChain %46 %42 %45
-        %206 = OpCopyObject %16 %78
-         %81 = OpLoad %16 %80
-         %79 = OpFAdd %16 %76 %78
-         %82 = OpFAdd %16 %81 %79
-               OpStore %80 %82
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
-}
-
-TEST(TransformationReplaceIdWithSynonymTest, SynonymsOfVariables) {
-  // The following SPIR-V comes from this GLSL, with object copies added:
-  //
-  // #version 310 es
-  //
-  // precision highp int;
-  //
-  // int g;
-  //
-  // void main() {
-  //   int l;
-  //   l = g;
-  //   g = l;
-  // }
-  const std::string shader = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %8 "l"
-               OpName %10 "g"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %7 = OpTypePointer Function %6
-          %9 = OpTypePointer Private %6
-         %10 = OpVariable %9 Private
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-          %8 = OpVariable %7 Function
-        %100 = OpCopyObject %9 %10
-        %101 = OpCopyObject %7 %8
-         %11 = OpLoad %6 %10
-               OpStore %8 %11
-         %12 = OpLoad %6 %8
-               OpStore %10 %12
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  const auto env = SPV_ENV_UNIVERSAL_1_3;
-  const auto consumer = nullptr;
-  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  FactManager fact_manager;
-
-  fact_manager.AddFact(MakeSynonymFact(10, 100), context.get());
-  fact_manager.AddFact(MakeSynonymFact(8, 101), context.get());
-
-  // Replace %10 with %100 in:
-  // %11 = OpLoad %6 %10
-  auto replacement1 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(10, MakeInstructionDescriptor(11, SpvOpLoad, 0), 0),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_TRUE(replacement1.IsApplicable(context.get(), fact_manager));
-  replacement1.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %8 with %101 in:
-  // OpStore %8 %11
-  auto replacement2 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(8, MakeInstructionDescriptor(11, SpvOpStore, 0), 0),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_TRUE(replacement2.IsApplicable(context.get(), fact_manager));
-  replacement2.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %8 with %101 in:
-  // %12 = OpLoad %6 %8
-  auto replacement3 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(8, MakeInstructionDescriptor(12, SpvOpLoad, 0), 0),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_TRUE(replacement3.IsApplicable(context.get(), fact_manager));
-  replacement3.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %10 with %100 in:
-  // OpStore %10 %12
-  auto replacement4 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(10, MakeInstructionDescriptor(12, SpvOpStore, 0), 0),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_TRUE(replacement4.IsApplicable(context.get(), fact_manager));
-  replacement4.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  const std::string after_transformation = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %8 "l"
-               OpName %10 "g"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %7 = OpTypePointer Function %6
-          %9 = OpTypePointer Private %6
-         %10 = OpVariable %9 Private
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-          %8 = OpVariable %7 Function
-        %100 = OpCopyObject %9 %10
-        %101 = OpCopyObject %7 %8
-         %11 = OpLoad %6 %100
-               OpStore %101 %11
-         %12 = OpLoad %6 %101
-               OpStore %100 %12
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
-}
-
-TEST(TransformationReplaceIdWithSynonymTest,
-     SynonymOfVariableNoGoodInFunctionCall) {
-  // The following SPIR-V comes from this GLSL, with an object copy added:
-  //
-  // #version 310 es
-  //
-  // precision highp int;
-  //
-  // void foo(int x) { }
-  //
-  // void main() {
-  //   int a;
-  //   a = 2;
-  //   foo(a);
-  // }
-  const std::string shader = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %10 "foo(i1;"
-               OpName %9 "x"
-               OpName %12 "a"
-               OpName %14 "param"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %7 = OpTypePointer Function %6
-          %8 = OpTypeFunction %2 %7
-         %13 = OpConstant %6 2
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-         %12 = OpVariable %7 Function
-         %14 = OpVariable %7 Function
-               OpStore %12 %13
-         %15 = OpLoad %6 %12
-               OpStore %14 %15
-        %100 = OpCopyObject %7 %14
-         %16 = OpFunctionCall %2 %10 %14
-               OpReturn
-               OpFunctionEnd
-         %10 = OpFunction %2 None %8
-          %9 = OpFunctionParameter %7
-         %11 = OpLabel
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  const auto env = SPV_ENV_UNIVERSAL_1_3;
-  const auto consumer = nullptr;
-  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  FactManager fact_manager;
-
-  fact_manager.AddFact(MakeSynonymFact(14, 100), context.get());
-
-  // Replace %14 with %100 in:
-  // %16 = OpFunctionCall %2 %10 %14
-  auto replacement = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          14, MakeInstructionDescriptor(16, SpvOpFunctionCall, 0), 1),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_FALSE(replacement.IsApplicable(context.get(), fact_manager));
-}
-
-TEST(TransformationReplaceIdWithSynonymTest, SynonymsOfAccessChainIndices) {
-  // The following SPIR-V comes from this GLSL, with object copies added:
-  //
-  // #version 310 es
-  //
-  // precision highp float;
-  // precision highp int;
-  //
-  // struct S {
-  //   int[3] a;
-  //   vec4 b;
-  //   bool c;
-  // } d;
-  //
-  // float[20] e;
-  //
-  // struct T {
-  //   float f;
-  //   S g;
-  // } h;
-  //
-  // T[4] i;
-  //
-  // void main() {
-  //   d.a[2] = 10;
-  //   d.b[3] = 11.0;
-  //   d.c = false;
-  //   e[17] = 12.0;
-  //   h.f = 13.0;
-  //   h.g.a[1] = 14;
-  //   h.g.b[0] = 15.0;
-  //   h.g.c = true;
-  //   i[0].f = 16.0;
-  //   i[1].g.a[0] = 17;
-  //   i[2].g.b[1] = 18.0;
-  //   i[3].g.c = true;
-  // }
-  const std::string shader = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %13 "S"
-               OpMemberName %13 0 "a"
-               OpMemberName %13 1 "b"
-               OpMemberName %13 2 "c"
-               OpName %15 "d"
-               OpName %31 "e"
-               OpName %35 "T"
-               OpMemberName %35 0 "f"
-               OpMemberName %35 1 "g"
-               OpName %37 "h"
-               OpName %50 "i"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %7 = OpTypeInt 32 0
-          %8 = OpConstant %7 3
-          %9 = OpTypeArray %6 %8
-         %10 = OpTypeFloat 32
-         %11 = OpTypeVector %10 4
-         %12 = OpTypeBool
-         %13 = OpTypeStruct %9 %11 %12
-         %14 = OpTypePointer Private %13
-         %15 = OpVariable %14 Private
-         %16 = OpConstant %6 0
-         %17 = OpConstant %6 2
-         %18 = OpConstant %6 10
-         %19 = OpTypePointer Private %6
-         %21 = OpConstant %6 1
-         %22 = OpConstant %10 11
-         %23 = OpTypePointer Private %10
-         %25 = OpConstantFalse %12
-         %26 = OpTypePointer Private %12
-         %28 = OpConstant %7 20
-         %29 = OpTypeArray %10 %28
-         %30 = OpTypePointer Private %29
-         %31 = OpVariable %30 Private
-         %32 = OpConstant %6 17
-         %33 = OpConstant %10 12
-         %35 = OpTypeStruct %10 %13
-         %36 = OpTypePointer Private %35
-         %37 = OpVariable %36 Private
-         %38 = OpConstant %10 13
-         %40 = OpConstant %6 14
-         %42 = OpConstant %10 15
-         %43 = OpConstant %7 0
-         %45 = OpConstantTrue %12
-         %47 = OpConstant %7 4
-         %48 = OpTypeArray %35 %47
-         %49 = OpTypePointer Private %48
-         %50 = OpVariable %49 Private
-         %51 = OpConstant %10 16
-         %54 = OpConstant %10 18
-         %55 = OpConstant %7 1
-         %57 = OpConstant %6 3
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-
-         %100 = OpCopyObject %6 %16 ; 0
-         %101 = OpCopyObject %6 %21 ; 1
-         %102 = OpCopyObject %6 %17 ; 2
-         %103 = OpCopyObject %6 %57 ; 3
-         %104 = OpCopyObject %6 %18 ; 10
-         %105 = OpCopyObject %6 %40 ; 14
-         %106 = OpCopyObject %6 %32 ; 17
-         %107 = OpCopyObject %7 %43 ; 0
-         %108 = OpCopyObject %7 %55 ; 1
-         %109 = OpCopyObject %7  %8 ; 3
-         %110 = OpCopyObject %7 %47 ; 4
-         %111 = OpCopyObject %7 %28 ; 20
-         %112 = OpCopyObject %12 %45 ; true
-
-         %20 = OpAccessChain %19 %15 %16 %17
-               OpStore %20 %18
-         %24 = OpAccessChain %23 %15 %21 %8
-               OpStore %24 %22
-         %27 = OpAccessChain %26 %15 %17
-               OpStore %27 %25
-         %34 = OpAccessChain %23 %31 %32
-               OpStore %34 %33
-         %39 = OpAccessChain %23 %37 %16
-               OpStore %39 %38
-         %41 = OpAccessChain %19 %37 %21 %16 %21
-               OpStore %41 %40
-         %44 = OpAccessChain %23 %37 %21 %21 %43
-               OpStore %44 %42
-         %46 = OpAccessChain %26 %37 %21 %17
-               OpStore %46 %45
-         %52 = OpAccessChain %23 %50 %16 %16
-               OpStore %52 %51
-         %53 = OpAccessChain %19 %50 %21 %21 %16 %16
-               OpStore %53 %32
-         %56 = OpAccessChain %23 %50 %17 %21 %21 %55
-               OpStore %56 %54
-         %58 = OpAccessChain %26 %50 %57 %21 %17
-               OpStore %58 %45
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  const auto env = SPV_ENV_UNIVERSAL_1_3;
-  const auto consumer = nullptr;
-  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  FactManager fact_manager;
-
-  // Add synonym facts corresponding to the OpCopyObject operations that have
-  // been applied to all constants in the module.
-  fact_manager.AddFact(MakeSynonymFact(16, 100), context.get());
-  fact_manager.AddFact(MakeSynonymFact(21, 101), context.get());
-  fact_manager.AddFact(MakeSynonymFact(17, 102), context.get());
-  fact_manager.AddFact(MakeSynonymFact(57, 103), context.get());
-  fact_manager.AddFact(MakeSynonymFact(18, 104), context.get());
-  fact_manager.AddFact(MakeSynonymFact(40, 105), context.get());
-  fact_manager.AddFact(MakeSynonymFact(32, 106), context.get());
-  fact_manager.AddFact(MakeSynonymFact(43, 107), context.get());
-  fact_manager.AddFact(MakeSynonymFact(55, 108), context.get());
-  fact_manager.AddFact(MakeSynonymFact(8, 109), context.get());
-  fact_manager.AddFact(MakeSynonymFact(47, 110), context.get());
-  fact_manager.AddFact(MakeSynonymFact(28, 111), context.get());
-  fact_manager.AddFact(MakeSynonymFact(45, 112), context.get());
-
-  // Replacements of the form %16 -> %100
-
-  // %20 = OpAccessChain %19 %15 *%16* %17
-  // Corresponds to d.*a*[2]
-  // The index %16 used for a cannot be replaced
-  auto replacement1 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          16, MakeInstructionDescriptor(20, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_FALSE(replacement1.IsApplicable(context.get(), fact_manager));
-
-  // %39 = OpAccessChain %23 %37 *%16*
-  // Corresponds to h.*f*
-  // The index %16 used for f cannot be replaced
-  auto replacement2 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          16, MakeInstructionDescriptor(39, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_FALSE(replacement2.IsApplicable(context.get(), fact_manager));
-
-  // %41 = OpAccessChain %19 %37 %21 *%16* %21
-  // Corresponds to h.g.*a*[1]
-  // The index %16 used for a cannot be replaced
-  auto replacement3 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          16, MakeInstructionDescriptor(41, SpvOpAccessChain, 0), 2),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_FALSE(replacement3.IsApplicable(context.get(), fact_manager));
-
-  // %52 = OpAccessChain %23 %50 *%16* %16
-  // Corresponds to i[*0*].f
-  // The index %16 used for 0 *can* be replaced
-  auto replacement4 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          16, MakeInstructionDescriptor(52, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_TRUE(replacement4.IsApplicable(context.get(), fact_manager));
-  replacement4.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // %52 = OpAccessChain %23 %50 %16 *%16*
-  // Corresponds to i[0].*f*
-  // The index %16 used for f cannot be replaced
-  auto replacement5 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          16, MakeInstructionDescriptor(52, SpvOpAccessChain, 0), 2),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_FALSE(replacement5.IsApplicable(context.get(), fact_manager));
-
-  // %53 = OpAccessChain %19 %50 %21 %21 *%16* %16
-  // Corresponds to i[1].g.*a*[0]
-  // The index %16 used for a cannot be replaced
-  auto replacement6 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          16, MakeInstructionDescriptor(53, SpvOpAccessChain, 0), 3),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_FALSE(replacement6.IsApplicable(context.get(), fact_manager));
-
-  // %53 = OpAccessChain %19 %50 %21 %21 %16 *%16*
-  // Corresponds to i[1].g.a[*0*]
-  // The index %16 used for 0 *can* be replaced
-  auto replacement7 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          16, MakeInstructionDescriptor(53, SpvOpAccessChain, 0), 4),
-      MakeDataDescriptor(100, {}), 0);
-  ASSERT_TRUE(replacement7.IsApplicable(context.get(), fact_manager));
-  replacement7.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replacements of the form %21 -> %101
-
-  // %24 = OpAccessChain %23 %15 *%21* %8
-  // Corresponds to d.*b*[3]
-  // The index %24 used for b cannot be replaced
-  auto replacement8 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(24, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_FALSE(replacement8.IsApplicable(context.get(), fact_manager));
-
-  // %41 = OpAccessChain %19 %37 *%21* %16 %21
-  // Corresponds to h.*g*.a[1]
-  // The index %24 used for g cannot be replaced
-  auto replacement9 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(41, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_FALSE(replacement9.IsApplicable(context.get(), fact_manager));
-
-  // %41 = OpAccessChain %19 %37 %21 %16 *%21*
-  // Corresponds to h.g.a[*1*]
-  // The index %24 used for 1 *can* be replaced
-  auto replacement10 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(41, SpvOpAccessChain, 0), 3),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_TRUE(replacement10.IsApplicable(context.get(), fact_manager));
-  replacement10.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // %44 = OpAccessChain %23 %37 *%21* %21 %43
-  // Corresponds to h.*g*.b[0]
-  // The index %24 used for g cannot be replaced
-  auto replacement11 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(44, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_FALSE(replacement11.IsApplicable(context.get(), fact_manager));
-
-  // %44 = OpAccessChain %23 %37 %21 *%21* %43
-  // Corresponds to h.g.*b*[0]
-  // The index %24 used for b cannot be replaced
-  auto replacement12 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(44, SpvOpAccessChain, 0), 2),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_FALSE(replacement12.IsApplicable(context.get(), fact_manager));
-
-  // %46 = OpAccessChain %26 %37 *%21* %17
-  // Corresponds to h.*g*.c
-  // The index %24 used for g cannot be replaced
-  auto replacement13 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(46, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_FALSE(replacement13.IsApplicable(context.get(), fact_manager));
-
-  // %53 = OpAccessChain %19 %50 *%21* %21 %16 %16
-  // Corresponds to i[*1*].g.a[0]
-  // The index %24 used for 1 *can* be replaced
-  auto replacement14 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(53, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_TRUE(replacement14.IsApplicable(context.get(), fact_manager));
-  replacement14.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // %53 = OpAccessChain %19 %50 %21 *%21* %16 %16
-  // Corresponds to i[1].*g*.a[0]
-  // The index %24 used for g cannot be replaced
-  auto replacement15 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(53, SpvOpAccessChain, 0), 2),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_FALSE(replacement15.IsApplicable(context.get(), fact_manager));
-
-  // %56 = OpAccessChain %23 %50 %17 *%21* %21 %55
-  // Corresponds to i[2].*g*.b[1]
-  // The index %24 used for g cannot be replaced
-  auto replacement16 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(56, SpvOpAccessChain, 0), 2),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_FALSE(replacement16.IsApplicable(context.get(), fact_manager));
-
-  // %56 = OpAccessChain %23 %50 %17 %21 *%21* %55
-  // Corresponds to i[2].g.*b*[1]
-  // The index %24 used for b cannot be replaced
-  auto replacement17 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(56, SpvOpAccessChain, 0), 3),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_FALSE(replacement17.IsApplicable(context.get(), fact_manager));
-
-  // %58 = OpAccessChain %26 %50 %57 *%21* %17
-  // Corresponds to i[3].*g*.c
-  // The index %24 used for g cannot be replaced
-  auto replacement18 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          21, MakeInstructionDescriptor(58, SpvOpAccessChain, 0), 2),
-      MakeDataDescriptor(101, {}), 0);
-  ASSERT_FALSE(replacement18.IsApplicable(context.get(), fact_manager));
-
-  // Replacements of the form %17 -> %102
-
-  // %20 = OpAccessChain %19 %15 %16 %17
-  // Corresponds to d.a[*2*]
-  // The index %17 used for 2 *can* be replaced
-  auto replacement19 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          17, MakeInstructionDescriptor(20, SpvOpAccessChain, 0), 2),
-      MakeDataDescriptor(102, {}), 0);
-  ASSERT_TRUE(replacement19.IsApplicable(context.get(), fact_manager));
-  replacement19.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // %27 = OpAccessChain %26 %15 %17
-  // Corresponds to d.c
-  // The index %17 used for c cannot be replaced
-  auto replacement20 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          17, MakeInstructionDescriptor(27, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(102, {}), 0);
-  ASSERT_FALSE(replacement20.IsApplicable(context.get(), fact_manager));
-
-  // %46 = OpAccessChain %26 %37 %21 %17
-  // Corresponds to h.g.*c*
-  // The index %17 used for c cannot be replaced
-  auto replacement21 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          17, MakeInstructionDescriptor(46, SpvOpAccessChain, 0), 2),
-      MakeDataDescriptor(102, {}), 0);
-  ASSERT_FALSE(replacement21.IsApplicable(context.get(), fact_manager));
-
-  // %56 = OpAccessChain %23 %50 %17 %21 %21 %55
-  // Corresponds to i[*2*].g.b[1]
-  // The index %17 used for 2 *can* be replaced
-  auto replacement22 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          17, MakeInstructionDescriptor(56, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(102, {}), 0);
-  ASSERT_TRUE(replacement22.IsApplicable(context.get(), fact_manager));
-  replacement22.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // %58 = OpAccessChain %26 %50 %57 %21 %17
-  // Corresponds to i[3].g.*c*
-  // The index %17 used for c cannot be replaced
-  auto replacement23 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          17, MakeInstructionDescriptor(58, SpvOpAccessChain, 0), 3),
-      MakeDataDescriptor(102, {}), 0);
-  ASSERT_FALSE(replacement23.IsApplicable(context.get(), fact_manager));
-
-  // Replacements of the form %57 -> %103
-
-  // %58 = OpAccessChain %26 %50 *%57* %21 %17
-  // Corresponds to i[*3*].g.c
-  // The index %57 used for 3 *can* be replaced
-  auto replacement24 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          57, MakeInstructionDescriptor(58, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(103, {}), 0);
-  ASSERT_TRUE(replacement24.IsApplicable(context.get(), fact_manager));
-  replacement24.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replacements of the form %32 -> %106
-
-  // %34 = OpAccessChain %23 %31 *%32*
-  // Corresponds to e[*17*]
-  // The index %32 used for 17 *can* be replaced
-  auto replacement25 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          32, MakeInstructionDescriptor(34, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(106, {}), 0);
-  ASSERT_TRUE(replacement25.IsApplicable(context.get(), fact_manager));
-  replacement25.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replacements of the form %43 -> %107
-
-  // %44 = OpAccessChain %23 %37 %21 %21 *%43*
-  // Corresponds to h.g.b[*0*]
-  // The index %43 used for 0 *can* be replaced
-  auto replacement26 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          43, MakeInstructionDescriptor(44, SpvOpAccessChain, 0), 3),
-      MakeDataDescriptor(107, {}), 0);
-  ASSERT_TRUE(replacement26.IsApplicable(context.get(), fact_manager));
-  replacement26.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replacements of the form %55 -> %108
-
-  // %56 = OpAccessChain %23 %50 %17 %21 %21 *%55*
-  // Corresponds to i[2].g.b[*1*]
-  // The index %55 used for 1 *can* be replaced
-  auto replacement27 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          55, MakeInstructionDescriptor(56, SpvOpAccessChain, 0), 4),
-      MakeDataDescriptor(108, {}), 0);
-  ASSERT_TRUE(replacement27.IsApplicable(context.get(), fact_manager));
-  replacement27.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replacements of the form %8 -> %109
-
-  // %24 = OpAccessChain %23 %15 %21 *%8*
-  // Corresponds to d.b[*3*]
-  // The index %8 used for 3 *can* be replaced
-  auto replacement28 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(8, MakeInstructionDescriptor(24, SpvOpAccessChain, 0),
-                          2),
-      MakeDataDescriptor(109, {}), 0);
-  ASSERT_TRUE(replacement28.IsApplicable(context.get(), fact_manager));
-  replacement28.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  const std::string after_transformation = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %13 "S"
-               OpMemberName %13 0 "a"
-               OpMemberName %13 1 "b"
-               OpMemberName %13 2 "c"
-               OpName %15 "d"
-               OpName %31 "e"
-               OpName %35 "T"
-               OpMemberName %35 0 "f"
-               OpMemberName %35 1 "g"
-               OpName %37 "h"
-               OpName %50 "i"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %7 = OpTypeInt 32 0
-          %8 = OpConstant %7 3
-          %9 = OpTypeArray %6 %8
-         %10 = OpTypeFloat 32
-         %11 = OpTypeVector %10 4
-         %12 = OpTypeBool
-         %13 = OpTypeStruct %9 %11 %12
-         %14 = OpTypePointer Private %13
-         %15 = OpVariable %14 Private
-         %16 = OpConstant %6 0
-         %17 = OpConstant %6 2
-         %18 = OpConstant %6 10
-         %19 = OpTypePointer Private %6
-         %21 = OpConstant %6 1
-         %22 = OpConstant %10 11
-         %23 = OpTypePointer Private %10
-         %25 = OpConstantFalse %12
-         %26 = OpTypePointer Private %12
-         %28 = OpConstant %7 20
-         %29 = OpTypeArray %10 %28
-         %30 = OpTypePointer Private %29
-         %31 = OpVariable %30 Private
-         %32 = OpConstant %6 17
-         %33 = OpConstant %10 12
-         %35 = OpTypeStruct %10 %13
-         %36 = OpTypePointer Private %35
-         %37 = OpVariable %36 Private
-         %38 = OpConstant %10 13
-         %40 = OpConstant %6 14
-         %42 = OpConstant %10 15
-         %43 = OpConstant %7 0
-         %45 = OpConstantTrue %12
-         %47 = OpConstant %7 4
-         %48 = OpTypeArray %35 %47
-         %49 = OpTypePointer Private %48
-         %50 = OpVariable %49 Private
-         %51 = OpConstant %10 16
-         %54 = OpConstant %10 18
-         %55 = OpConstant %7 1
-         %57 = OpConstant %6 3
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-
-         %100 = OpCopyObject %6 %16 ; 0
-         %101 = OpCopyObject %6 %21 ; 1
-         %102 = OpCopyObject %6 %17 ; 2
-         %103 = OpCopyObject %6 %57 ; 3
-         %104 = OpCopyObject %6 %18 ; 10
-         %105 = OpCopyObject %6 %40 ; 14
-         %106 = OpCopyObject %6 %32 ; 17
-         %107 = OpCopyObject %7 %43 ; 0
-         %108 = OpCopyObject %7 %55 ; 1
-         %109 = OpCopyObject %7  %8 ; 3
-         %110 = OpCopyObject %7 %47 ; 4
-         %111 = OpCopyObject %7 %28 ; 20
-         %112 = OpCopyObject %12 %45 ; true
-
-         %20 = OpAccessChain %19 %15 %16 %102
-               OpStore %20 %18
-         %24 = OpAccessChain %23 %15 %21 %109
-               OpStore %24 %22
-         %27 = OpAccessChain %26 %15 %17
-               OpStore %27 %25
-         %34 = OpAccessChain %23 %31 %106
-               OpStore %34 %33
-         %39 = OpAccessChain %23 %37 %16
-               OpStore %39 %38
-         %41 = OpAccessChain %19 %37 %21 %16 %101
-               OpStore %41 %40
-         %44 = OpAccessChain %23 %37 %21 %21 %107
-               OpStore %44 %42
-         %46 = OpAccessChain %26 %37 %21 %17
-               OpStore %46 %45
-         %52 = OpAccessChain %23 %50 %100 %16
-               OpStore %52 %51
-         %53 = OpAccessChain %19 %50 %101 %21 %16 %100
-               OpStore %53 %32
-         %56 = OpAccessChain %23 %50 %102 %21 %21 %108
-               OpStore %56 %54
-         %58 = OpAccessChain %26 %50 %103 %21 %17
-               OpStore %58 %45
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
-}
-
-TEST(TransformationReplaceIdWithSynonymTest, ArrayCompositeSynonyms) {
-  std::string shader = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %11 "A"
-               OpName %20 "B"
-               OpName %31 "g"
-               OpName %35 "h"
-               OpDecorate %11 RelaxedPrecision
-               OpDecorate %22 RelaxedPrecision
-               OpDecorate %27 RelaxedPrecision
-               OpDecorate %35 RelaxedPrecision
-               OpDecorate %36 RelaxedPrecision
-               OpDecorate %40 RelaxedPrecision
-               OpDecorate %41 RelaxedPrecision
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %7 = OpTypeInt 32 0
-          %8 = OpConstant %7 3
-          %9 = OpTypeArray %6 %8
-         %10 = OpTypePointer Function %9
-         %12 = OpConstant %6 0
-         %13 = OpConstant %6 3
-         %14 = OpTypePointer Function %6
-         %16 = OpTypeFloat 32
-         %17 = OpConstant %7 4
-         %18 = OpTypeArray %16 %17
-         %19 = OpTypePointer Function %18
-         %24 = OpTypePointer Function %16
-         %28 = OpConstant %16 42
-         %30 = OpConstant %6 2
-         %34 = OpConstant %6 1
-         %38 = OpConstant %6 42
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-         %11 = OpVariable %10 Function
-         %20 = OpVariable %19 Function
-         %31 = OpVariable %24 Function
-         %35 = OpVariable %14 Function
-         %15 = OpAccessChain %14 %11 %12
-         %21 = OpAccessChain %14 %11 %12
-         %22 = OpLoad %6 %21
-        %100 = OpCompositeConstruct %9 %12 %13 %22
-               OpStore %15 %13
-         %23 = OpConvertSToF %16 %22
-         %25 = OpAccessChain %24 %20 %12
-               OpStore %25 %23
-         %26 = OpAccessChain %14 %11 %12
-         %27 = OpLoad %6 %26
-         %29 = OpAccessChain %24 %20 %27
-               OpStore %29 %28
-         %32 = OpLoad %16 %31
-        %101 = OpCompositeConstruct %18 %28 %23 %32 %23
-         %50 = OpCopyObject %16 %23
-         %51 = OpCopyObject %16 %23
-         %33 = OpAccessChain %24 %20 %30
-               OpStore %33 %28
-               OpStore %33 %32
-         %36 = OpLoad %6 %35
-         %37 = OpAccessChain %14 %11 %34
-               OpStore %37 %36
-         %39 = OpAccessChain %14 %11 %12
-         %40 = OpLoad %6 %39
-         %41 = OpIAdd %6 %38 %40
-         %42 = OpAccessChain %14 %11 %30
-               OpStore %42 %41
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  const auto env = SPV_ENV_UNIVERSAL_1_3;
-  const auto consumer = nullptr;
-  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  FactManager fact_manager;
-  fact_manager.AddFact(MakeSynonymFact(12, 100, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(13, 100, {1}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(22, 100, {2}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(28, 101, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(23, 101, {1}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(32, 101, {2}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(23, 101, {3}), context.get());
-
-  // Replace %12 with %100[0] in '%25 = OpAccessChain %24 %20 %12'
-  auto good_replacement_1 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          12, MakeInstructionDescriptor(25, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(100, {0}), 102);
-  // Bad: id already in use
-  auto bad_replacement_1 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          12, MakeInstructionDescriptor(25, SpvOpAccessChain, 0), 1),
-      MakeDataDescriptor(100, {0}), 25);
-  ASSERT_TRUE(good_replacement_1.IsApplicable(context.get(), fact_manager));
-  ASSERT_FALSE(bad_replacement_1.IsApplicable(context.get(), fact_manager));
-  good_replacement_1.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %13 with %100[1] in 'OpStore %15 %13'
-  auto good_replacement_2 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(13, MakeInstructionDescriptor(100, SpvOpStore, 0), 1),
-      MakeDataDescriptor(100, {1}), 103);
-  // Bad: too many indices
-  auto bad_replacement_2 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(13, MakeInstructionDescriptor(100, SpvOpStore, 0), 1),
-      MakeDataDescriptor(100, {1, 0}), 103);
-  ASSERT_TRUE(good_replacement_2.IsApplicable(context.get(), fact_manager));
-  ASSERT_FALSE(bad_replacement_2.IsApplicable(context.get(), fact_manager));
-  good_replacement_2.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %22 with %100[2] in '%23 = OpConvertSToF %16 %22'
-  auto good_replacement_3 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          22, MakeInstructionDescriptor(23, SpvOpConvertSToF, 0), 0),
-      MakeDataDescriptor(100, {2}), 104);
-  // Bad: wrong input operand index
-  auto bad_replacement_3 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          22, MakeInstructionDescriptor(23, SpvOpConvertSToF, 0), 1),
-      MakeDataDescriptor(100, {2}), 104);
-  ASSERT_TRUE(good_replacement_3.IsApplicable(context.get(), fact_manager));
-  ASSERT_FALSE(bad_replacement_3.IsApplicable(context.get(), fact_manager));
-  good_replacement_3.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %28 with %101[0] in 'OpStore %33 %28'
-  auto good_replacement_4 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(28, MakeInstructionDescriptor(33, SpvOpStore, 0), 1),
-      MakeDataDescriptor(101, {0}), 105);
-  // Bad: id use descriptor does not identify an appropriate instruction
-  auto bad_replacement_4 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(28, MakeInstructionDescriptor(33, SpvOpCopyObject, 0),
-                          1),
-      MakeDataDescriptor(101, {0}), 105);
-  ASSERT_TRUE(good_replacement_4.IsApplicable(context.get(), fact_manager));
-  ASSERT_FALSE(bad_replacement_4.IsApplicable(context.get(), fact_manager));
-  good_replacement_4.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %23 with %101[1] in '%50 = OpCopyObject %16 %23'
-  auto good_replacement_5 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(23, MakeInstructionDescriptor(50, SpvOpCopyObject, 0),
-                          0),
-      MakeDataDescriptor(101, {1}), 106);
-  // Bad: wrong synonym fact being used
-  auto bad_replacement_5 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(23, MakeInstructionDescriptor(50, SpvOpCopyObject, 0),
-                          0),
-      MakeDataDescriptor(101, {0}), 106);
-  ASSERT_TRUE(good_replacement_5.IsApplicable(context.get(), fact_manager));
-  ASSERT_FALSE(bad_replacement_5.IsApplicable(context.get(), fact_manager));
-  good_replacement_5.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %32 with %101[2] in 'OpStore %33 %32'
-  auto good_replacement_6 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(32, MakeInstructionDescriptor(33, SpvOpStore, 1), 1),
-      MakeDataDescriptor(101, {2}), 107);
-  // Bad: id 1001 does not exist
-  auto bad_replacement_6 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(32, MakeInstructionDescriptor(33, SpvOpStore, 1), 1),
-      MakeDataDescriptor(1001, {2}), 107);
-  ASSERT_TRUE(good_replacement_6.IsApplicable(context.get(), fact_manager));
-  ASSERT_FALSE(bad_replacement_6.IsApplicable(context.get(), fact_manager));
-  good_replacement_6.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %23 with %101[3] in '%51 = OpCopyObject %16 %23'
-  auto good_replacement_7 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(23, MakeInstructionDescriptor(51, SpvOpCopyObject, 0),
-                          0),
-      MakeDataDescriptor(101, {3}), 108);
-  // Bad: id 0 is invalid
-  auto bad_replacement_7 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(0, MakeInstructionDescriptor(51, SpvOpCopyObject, 0),
-                          0),
-      MakeDataDescriptor(101, {3}), 108);
-  ASSERT_TRUE(good_replacement_7.IsApplicable(context.get(), fact_manager));
-  ASSERT_FALSE(bad_replacement_7.IsApplicable(context.get(), fact_manager));
-  good_replacement_7.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  const std::string after_transformation = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %11 "A"
-               OpName %20 "B"
-               OpName %31 "g"
-               OpName %35 "h"
-               OpDecorate %11 RelaxedPrecision
-               OpDecorate %22 RelaxedPrecision
-               OpDecorate %27 RelaxedPrecision
-               OpDecorate %35 RelaxedPrecision
-               OpDecorate %36 RelaxedPrecision
-               OpDecorate %40 RelaxedPrecision
-               OpDecorate %41 RelaxedPrecision
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %7 = OpTypeInt 32 0
-          %8 = OpConstant %7 3
-          %9 = OpTypeArray %6 %8
-         %10 = OpTypePointer Function %9
-         %12 = OpConstant %6 0
-         %13 = OpConstant %6 3
-         %14 = OpTypePointer Function %6
-         %16 = OpTypeFloat 32
-         %17 = OpConstant %7 4
-         %18 = OpTypeArray %16 %17
-         %19 = OpTypePointer Function %18
-         %24 = OpTypePointer Function %16
-         %28 = OpConstant %16 42
-         %30 = OpConstant %6 2
-         %34 = OpConstant %6 1
-         %38 = OpConstant %6 42
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-         %11 = OpVariable %10 Function
-         %20 = OpVariable %19 Function
-         %31 = OpVariable %24 Function
-         %35 = OpVariable %14 Function
-         %15 = OpAccessChain %14 %11 %12
-         %21 = OpAccessChain %14 %11 %12
-         %22 = OpLoad %6 %21
-        %100 = OpCompositeConstruct %9 %12 %13 %22
-        %103 = OpCompositeExtract %6 %100 1
-               OpStore %15 %103
-        %104 = OpCompositeExtract %6 %100 2
-         %23 = OpConvertSToF %16 %104
-        %102 = OpCompositeExtract %6 %100 0
-         %25 = OpAccessChain %24 %20 %102
-               OpStore %25 %23
-         %26 = OpAccessChain %14 %11 %12
-         %27 = OpLoad %6 %26
-         %29 = OpAccessChain %24 %20 %27
-               OpStore %29 %28
-         %32 = OpLoad %16 %31
-        %101 = OpCompositeConstruct %18 %28 %23 %32 %23
-        %106 = OpCompositeExtract %16 %101 1
-         %50 = OpCopyObject %16 %106
-        %108 = OpCompositeExtract %16 %101 3
-         %51 = OpCopyObject %16 %108
-         %33 = OpAccessChain %24 %20 %30
-        %105 = OpCompositeExtract %16 %101 0
-               OpStore %33 %105
-        %107 = OpCompositeExtract %16 %101 2
-               OpStore %33 %107
-         %36 = OpLoad %6 %35
-         %37 = OpAccessChain %14 %11 %34
-               OpStore %37 %36
-         %39 = OpAccessChain %14 %11 %12
-         %40 = OpLoad %6 %39
-         %41 = OpIAdd %6 %38 %40
-         %42 = OpAccessChain %14 %11 %30
-               OpStore %42 %41
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
-}
-
-TEST(TransformationReplaceIdWithSynonymTest, MatrixCompositeSynonyms) {
-  std::string shader = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %10 "m"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeFloat 32
-          %7 = OpTypeVector %6 4
-         %50 = OpUndef %7
-          %8 = OpTypeMatrix %7 3
-          %9 = OpTypePointer Function %8
-         %11 = OpTypeInt 32 1
-         %12 = OpConstant %11 0
-         %13 = OpConstant %6 1
-         %14 = OpConstantComposite %7 %13 %13 %13 %13
-         %15 = OpTypePointer Function %7
-         %17 = OpConstant %11 1
-         %18 = OpConstant %6 2
-         %19 = OpConstantComposite %7 %18 %18 %18 %18
-         %21 = OpConstant %11 2
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-         %10 = OpVariable %9 Function
-         %16 = OpAccessChain %15 %10 %12
-               OpStore %16 %14
-         %20 = OpAccessChain %15 %10 %17
-               OpStore %20 %19
-         %22 = OpAccessChain %15 %10 %12
-         %23 = OpLoad %7 %22
-         %24 = OpAccessChain %15 %10 %17
-         %25 = OpLoad %7 %24
-        %100 = OpCompositeConstruct %8 %23 %25 %50
-         %26 = OpFAdd %7 %23 %25
-         %27 = OpAccessChain %15 %10 %21
-               OpStore %27 %26
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  const auto env = SPV_ENV_UNIVERSAL_1_3;
-  const auto consumer = nullptr;
-  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  FactManager fact_manager;
-  fact_manager.AddFact(MakeSynonymFact(23, 100, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(25, 100, {1}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(50, 100, {2}), context.get());
-
-  // Replace %23 with %100[0] in '%26 = OpFAdd %7 %23 %25'
-  auto replacement_1 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(23, MakeInstructionDescriptor(26, SpvOpFAdd, 0), 0),
-      MakeDataDescriptor(100, {0}), 101);
-  ASSERT_TRUE(replacement_1.IsApplicable(context.get(), fact_manager));
-  replacement_1.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %25 with %100[1] in '%26 = OpFAdd %7 %23 %25'
-  auto replacement_2 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(25, MakeInstructionDescriptor(26, SpvOpFAdd, 0), 1),
-      MakeDataDescriptor(100, {1}), 102);
-  ASSERT_TRUE(replacement_2.IsApplicable(context.get(), fact_manager));
-  replacement_2.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  const std::string after_transformation = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %10 "m"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeFloat 32
-          %7 = OpTypeVector %6 4
-         %50 = OpUndef %7
-          %8 = OpTypeMatrix %7 3
-          %9 = OpTypePointer Function %8
-         %11 = OpTypeInt 32 1
-         %12 = OpConstant %11 0
-         %13 = OpConstant %6 1
-         %14 = OpConstantComposite %7 %13 %13 %13 %13
-         %15 = OpTypePointer Function %7
-         %17 = OpConstant %11 1
-         %18 = OpConstant %6 2
-         %19 = OpConstantComposite %7 %18 %18 %18 %18
-         %21 = OpConstant %11 2
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-         %10 = OpVariable %9 Function
-         %16 = OpAccessChain %15 %10 %12
-               OpStore %16 %14
-         %20 = OpAccessChain %15 %10 %17
-               OpStore %20 %19
-         %22 = OpAccessChain %15 %10 %12
-         %23 = OpLoad %7 %22
-         %24 = OpAccessChain %15 %10 %17
-         %25 = OpLoad %7 %24
-        %100 = OpCompositeConstruct %8 %23 %25 %50
-        %101 = OpCompositeExtract %7 %100 0
-        %102 = OpCompositeExtract %7 %100 1
-         %26 = OpFAdd %7 %101 %102
-         %27 = OpAccessChain %15 %10 %21
-               OpStore %27 %26
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
-}
-
-TEST(TransformationReplaceIdWithSynonymTest, StructCompositeSynonyms) {
-  std::string shader = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %9 "Inner"
-               OpMemberName %9 0 "a"
-               OpMemberName %9 1 "b"
-               OpName %11 "i1"
-               OpName %17 "i2"
-               OpName %31 "Point"
-               OpMemberName %31 0 "x"
-               OpMemberName %31 1 "y"
-               OpMemberName %31 2 "z"
-               OpName %32 "Outer"
-               OpMemberName %32 0 "c"
-               OpMemberName %32 1 "d"
-               OpName %34 "o1"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %7 = OpTypeFloat 32
-          %8 = OpTypeVector %7 2
-          %9 = OpTypeStruct %6 %8
-         %10 = OpTypePointer Function %9
-         %12 = OpConstant %6 1
-         %13 = OpConstant %7 2
-         %14 = OpConstant %7 3
-         %15 = OpConstantComposite %8 %13 %14
-         %16 = OpConstantComposite %9 %12 %15
-         %18 = OpConstant %6 0
-         %19 = OpTypePointer Function %6
-         %24 = OpTypePointer Function %8
-         %27 = OpConstant %7 4
-         %31 = OpTypeStruct %7 %7 %7
-         %32 = OpTypeStruct %9 %31
-         %33 = OpTypePointer Function %32
-         %36 = OpConstant %7 10
-         %37 = OpTypeInt 32 0
-         %38 = OpConstant %37 0
-         %39 = OpTypePointer Function %7
-         %42 = OpConstant %37 1
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-         %11 = OpVariable %10 Function
-         %17 = OpVariable %10 Function
-         %34 = OpVariable %33 Function
-        %101 = OpCompositeConstruct %31 %27 %36 %27
-               OpStore %11 %16
-         %20 = OpAccessChain %19 %11 %18
-         %21 = OpLoad %6 %20
-         %22 = OpIAdd %6 %21 %12
-        %102 = OpCompositeConstruct %9 %22 %15
-         %23 = OpAccessChain %19 %17 %18
-               OpStore %23 %22
-         %25 = OpAccessChain %24 %17 %12
-         %26 = OpLoad %8 %25
-         %28 = OpCompositeConstruct %8 %27 %27
-         %29 = OpFAdd %8 %26 %28
-         %30 = OpAccessChain %24 %17 %12
-               OpStore %30 %29
-         %35 = OpLoad %9 %11
-         %40 = OpAccessChain %39 %11 %12 %38
-         %41 = OpLoad %7 %40
-         %43 = OpAccessChain %39 %11 %12 %42
-         %44 = OpLoad %7 %43
-         %45 = OpCompositeConstruct %31 %36 %41 %44
-        %100 = OpCompositeConstruct %32 %16 %45
-         %46 = OpCompositeConstruct %32 %35 %45
-               OpStore %34 %46
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  const auto env = SPV_ENV_UNIVERSAL_1_3;
-  const auto consumer = nullptr;
-  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  FactManager fact_manager;
-
-  fact_manager.AddFact(MakeSynonymFact(16, 100, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(45, 100, {1}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(27, 101, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(36, 101, {1}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(27, 101, {2}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(22, 102, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(15, 102, {1}), context.get());
-
-  // Replace %45 with %100[1] in '%46 = OpCompositeConstruct %32 %35 %45'
-  auto replacement_1 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          45, MakeInstructionDescriptor(46, SpvOpCompositeConstruct, 0), 1),
-      MakeDataDescriptor(100, {1}), 201);
-  ASSERT_TRUE(replacement_1.IsApplicable(context.get(), fact_manager));
-  replacement_1.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace second occurrence of %27 with %101[0] in '%28 =
-  // OpCompositeConstruct %8 %27 %27'
-  auto replacement_2 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          27, MakeInstructionDescriptor(28, SpvOpCompositeConstruct, 0), 1),
-      MakeDataDescriptor(101, {0}), 202);
-  ASSERT_TRUE(replacement_2.IsApplicable(context.get(), fact_manager));
-  replacement_2.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %36 with %101[1] in '%45 = OpCompositeConstruct %31 %36 %41 %44'
-  auto replacement_3 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          36, MakeInstructionDescriptor(45, SpvOpCompositeConstruct, 0), 0),
-      MakeDataDescriptor(101, {1}), 203);
-  ASSERT_TRUE(replacement_3.IsApplicable(context.get(), fact_manager));
-  replacement_3.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace first occurrence of %27 with %101[2] in '%28 = OpCompositeConstruct
-  // %8 %27 %27'
-  auto replacement_4 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          27, MakeInstructionDescriptor(28, SpvOpCompositeConstruct, 0), 0),
-      MakeDataDescriptor(101, {2}), 204);
-  ASSERT_TRUE(replacement_4.IsApplicable(context.get(), fact_manager));
-  replacement_4.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %22 with %102[0] in 'OpStore %23 %22'
-  auto replacement_5 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(22, MakeInstructionDescriptor(23, SpvOpStore, 0), 1),
-      MakeDataDescriptor(102, {0}), 205);
-  ASSERT_TRUE(replacement_5.IsApplicable(context.get(), fact_manager));
-  replacement_5.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  const std::string after_transformation = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %9 "Inner"
-               OpMemberName %9 0 "a"
-               OpMemberName %9 1 "b"
-               OpName %11 "i1"
-               OpName %17 "i2"
-               OpName %31 "Point"
-               OpMemberName %31 0 "x"
-               OpMemberName %31 1 "y"
-               OpMemberName %31 2 "z"
-               OpName %32 "Outer"
-               OpMemberName %32 0 "c"
-               OpMemberName %32 1 "d"
-               OpName %34 "o1"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeInt 32 1
-          %7 = OpTypeFloat 32
-          %8 = OpTypeVector %7 2
-          %9 = OpTypeStruct %6 %8
-         %10 = OpTypePointer Function %9
-         %12 = OpConstant %6 1
-         %13 = OpConstant %7 2
-         %14 = OpConstant %7 3
-         %15 = OpConstantComposite %8 %13 %14
-         %16 = OpConstantComposite %9 %12 %15
-         %18 = OpConstant %6 0
-         %19 = OpTypePointer Function %6
-         %24 = OpTypePointer Function %8
-         %27 = OpConstant %7 4
-         %31 = OpTypeStruct %7 %7 %7
-         %32 = OpTypeStruct %9 %31
-         %33 = OpTypePointer Function %32
-         %36 = OpConstant %7 10
-         %37 = OpTypeInt 32 0
-         %38 = OpConstant %37 0
-         %39 = OpTypePointer Function %7
-         %42 = OpConstant %37 1
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-         %11 = OpVariable %10 Function
-         %17 = OpVariable %10 Function
-         %34 = OpVariable %33 Function
-        %101 = OpCompositeConstruct %31 %27 %36 %27
-               OpStore %11 %16
-         %20 = OpAccessChain %19 %11 %18
-         %21 = OpLoad %6 %20
-         %22 = OpIAdd %6 %21 %12
-        %102 = OpCompositeConstruct %9 %22 %15
-         %23 = OpAccessChain %19 %17 %18
-        %205 = OpCompositeExtract %6 %102 0
-               OpStore %23 %205
-         %25 = OpAccessChain %24 %17 %12
-         %26 = OpLoad %8 %25
-        %202 = OpCompositeExtract %7 %101 0
-        %204 = OpCompositeExtract %7 %101 2
-         %28 = OpCompositeConstruct %8 %204 %202
-         %29 = OpFAdd %8 %26 %28
-         %30 = OpAccessChain %24 %17 %12
-               OpStore %30 %29
-         %35 = OpLoad %9 %11
-         %40 = OpAccessChain %39 %11 %12 %38
-         %41 = OpLoad %7 %40
-         %43 = OpAccessChain %39 %11 %12 %42
-         %44 = OpLoad %7 %43
-        %203 = OpCompositeExtract %7 %101 1
-         %45 = OpCompositeConstruct %31 %203 %41 %44
-        %100 = OpCompositeConstruct %32 %16 %45
-        %201 = OpCompositeExtract %31 %100 1
-         %46 = OpCompositeConstruct %32 %35 %201
-               OpStore %34 %46
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
-}
-
-TEST(TransformationReplaceIdWithSynonymTest, VectorCompositeSynonyms) {
-  std::string shader = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %8 "f"
-               OpName %12 "v2"
-               OpName %18 "v3"
-               OpName %23 "v4"
-               OpName %32 "b"
-               OpName %36 "bv2"
-               OpName %41 "bv3"
-               OpName %50 "bv4"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeFloat 32
-          %7 = OpTypePointer Function %6
-          %9 = OpConstant %6 42
-         %10 = OpTypeVector %6 2
-         %11 = OpTypePointer Function %10
-         %16 = OpTypeVector %6 3
-         %17 = OpTypePointer Function %16
-         %21 = OpTypeVector %6 4
-         %22 = OpTypePointer Function %21
-         %30 = OpTypeBool
-         %31 = OpTypePointer Function %30
-         %33 = OpConstantFalse %30
-         %34 = OpTypeVector %30 2
-         %35 = OpTypePointer Function %34
-         %37 = OpConstantTrue %30
-         %38 = OpConstantComposite %34 %37 %37
-         %39 = OpTypeVector %30 3
-         %40 = OpTypePointer Function %39
-         %48 = OpTypeVector %30 4
-         %49 = OpTypePointer Function %48
-         %51 = OpTypeInt 32 0
-         %52 = OpConstant %51 2
-         %55 = OpConstant %6 0
-         %57 = OpConstant %51 1
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-          %8 = OpVariable %7 Function
-         %12 = OpVariable %11 Function
-         %18 = OpVariable %17 Function
-         %23 = OpVariable %22 Function
-         %32 = OpVariable %31 Function
-         %36 = OpVariable %35 Function
-         %41 = OpVariable %40 Function
-         %50 = OpVariable %49 Function
-               OpStore %8 %9
-         %13 = OpLoad %6 %8
-         %14 = OpLoad %6 %8
-         %15 = OpCompositeConstruct %10 %13 %14
-               OpStore %12 %15
-         %19 = OpLoad %10 %12
-         %20 = OpVectorShuffle %16 %19 %19 0 0 1
-               OpStore %18 %20
-         %24 = OpLoad %16 %18
-         %25 = OpLoad %6 %8
-         %26 = OpCompositeExtract %6 %24 0
-         %27 = OpCompositeExtract %6 %24 1
-         %28 = OpCompositeExtract %6 %24 2
-         %29 = OpCompositeConstruct %21 %26 %27 %28 %25
-               OpStore %23 %29
-               OpStore %32 %33
-               OpStore %36 %38
-         %42 = OpLoad %30 %32
-         %43 = OpLoad %34 %36
-         %44 = OpVectorShuffle %34 %43 %43 0 0
-         %45 = OpCompositeExtract %30 %44 0
-         %46 = OpCompositeExtract %30 %44 1
-         %47 = OpCompositeConstruct %39 %42 %45 %46
-               OpStore %41 %47
-         %53 = OpAccessChain %7 %23 %52
-         %54 = OpLoad %6 %53
-
-        %100 = OpCompositeConstruct %21 %20 %54
-        %101 = OpCompositeConstruct %21 %15 %19
-        %102 = OpCompositeConstruct %16 %27 %15
-        %103 = OpCompositeConstruct %48 %33 %47
-        %104 = OpCompositeConstruct %34 %42 %45
-        %105 = OpCompositeConstruct %39 %38 %46
-
-         %86 = OpCopyObject %30 %33
-         %56 = OpFOrdNotEqual %30 %54 %55
-         %80 = OpCopyObject %16 %20
-         %58 = OpAccessChain %7 %18 %57
-         %59 = OpLoad %6 %58
-         %60 = OpFOrdNotEqual %30 %59 %55
-         %61 = OpLoad %34 %36
-         %62 = OpLogicalAnd %30 %45 %46
-         %63 = OpLogicalOr %30 %45 %46
-         %64 = OpCompositeConstruct %48 %56 %60 %62 %63
-               OpStore %12 %15
-         %81 = OpVectorShuffle %16 %19 %19 0 0 1
-         %82 = OpCompositeConstruct %21 %26 %27 %28 %25
-         %83 = OpCopyObject %10 %15
-         %84 = OpCopyObject %39 %47
-               OpStore %50 %64
-         %85 = OpCopyObject %30 %42
-               OpStore %36 %38
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  const auto env = SPV_ENV_UNIVERSAL_1_3;
-  const auto consumer = nullptr;
-  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  FactManager fact_manager;
-  fact_manager.AddFact(MakeSynonymFact(20, 100, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(54, 100, {3}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(15, 101, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(19, 101, {2}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(27, 102, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(15, 102, {1}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(33, 103, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(47, 103, {1}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(42, 104, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(45, 104, {1}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(38, 105, {0}), context.get());
-  fact_manager.AddFact(MakeSynonymFact(46, 105, {2}), context.get());
-
-  // Replace %20 with %100[0] in '%80 = OpCopyObject %16 %20'
-  auto replacement_1 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(20, MakeInstructionDescriptor(80, SpvOpCopyObject, 0),
-                          0),
-      MakeDataDescriptor(100, {0}), 200);
-  ASSERT_TRUE(replacement_1.IsApplicable(context.get(), fact_manager));
-  replacement_1.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %54 with %100[3] in '%56 = OpFOrdNotEqual %30 %54 %55'
-  auto replacement_2 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          54, MakeInstructionDescriptor(56, SpvOpFOrdNotEqual, 0), 0),
-      MakeDataDescriptor(100, {3}), 201);
-  ASSERT_TRUE(replacement_2.IsApplicable(context.get(), fact_manager));
-  replacement_2.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %15 with %101[0] in 'OpStore %12 %15'
-  auto replacement_3 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(15, MakeInstructionDescriptor(64, SpvOpStore, 0), 1),
-      MakeDataDescriptor(101, {0}), 202);
-  ASSERT_TRUE(replacement_3.IsApplicable(context.get(), fact_manager));
-  replacement_3.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %19 with %101[2] in '%81 = OpVectorShuffle %16 %19 %19 0 0 1'
-  auto replacement_4 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          19, MakeInstructionDescriptor(81, SpvOpVectorShuffle, 0), 0),
-      MakeDataDescriptor(101, {2}), 203);
-  ASSERT_TRUE(replacement_4.IsApplicable(context.get(), fact_manager));
-  replacement_4.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %27 with %102[0] in '%82 = OpCompositeConstruct %21 %26 %27 %28
-  // %25'
-  auto replacement_5 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(
-          27, MakeInstructionDescriptor(82, SpvOpCompositeConstruct, 0), 1),
-      MakeDataDescriptor(102, {0}), 204);
-  ASSERT_TRUE(replacement_5.IsApplicable(context.get(), fact_manager));
-  replacement_5.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %15 with %102[1] in '%83 = OpCopyObject %10 %15'
-  auto replacement_6 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(15, MakeInstructionDescriptor(83, SpvOpCopyObject, 0),
-                          0),
-      MakeDataDescriptor(102, {1}), 205);
-  ASSERT_TRUE(replacement_6.IsApplicable(context.get(), fact_manager));
-  replacement_6.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %33 with %103[0] in '%86 = OpCopyObject %30 %33'
-  auto replacement_7 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(33, MakeInstructionDescriptor(86, SpvOpCopyObject, 0),
-                          0),
-      MakeDataDescriptor(103, {0}), 206);
-  ASSERT_TRUE(replacement_7.IsApplicable(context.get(), fact_manager));
-  replacement_7.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %47 with %103[1] in '%84 = OpCopyObject %39 %47'
-  auto replacement_8 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(47, MakeInstructionDescriptor(84, SpvOpCopyObject, 0),
-                          0),
-      MakeDataDescriptor(103, {1}), 207);
-  ASSERT_TRUE(replacement_8.IsApplicable(context.get(), fact_manager));
-  replacement_8.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %42 with %104[0] in '%85 = OpCopyObject %30 %42'
-  auto replacement_9 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(42, MakeInstructionDescriptor(85, SpvOpCopyObject, 0),
-                          0),
-      MakeDataDescriptor(104, {0}), 208);
-  ASSERT_TRUE(replacement_9.IsApplicable(context.get(), fact_manager));
-  replacement_9.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %45 with %104[1] in '%63 = OpLogicalOr %30 %45 %46'
-  auto replacement_10 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(45, MakeInstructionDescriptor(63, SpvOpLogicalOr, 0),
-                          0),
-      MakeDataDescriptor(104, {1}), 209);
-  ASSERT_TRUE(replacement_10.IsApplicable(context.get(), fact_manager));
-  replacement_10.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %38 with %105[0] in 'OpStore %36 %38'
-  auto replacement_11 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(38, MakeInstructionDescriptor(85, SpvOpStore, 0), 1),
-      MakeDataDescriptor(105, {0}), 210);
-  ASSERT_TRUE(replacement_11.IsApplicable(context.get(), fact_manager));
-  replacement_11.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  // Replace %46 with %105[2] in '%62 = OpLogicalAnd %30 %45 %46'
-  auto replacement_12 = TransformationReplaceIdWithSynonym(
-      MakeIdUseDescriptor(46, MakeInstructionDescriptor(62, SpvOpLogicalAnd, 0),
-                          1),
-      MakeDataDescriptor(105, {2}), 211);
-  ASSERT_TRUE(replacement_12.IsApplicable(context.get(), fact_manager));
-  replacement_12.Apply(context.get(), &fact_manager);
-  ASSERT_TRUE(IsValid(env, context.get()));
-
-  const std::string after_transformation = R"(
-               OpCapability Shader
-          %1 = OpExtInstImport "GLSL.std.450"
-               OpMemoryModel Logical GLSL450
-               OpEntryPoint Fragment %4 "main"
-               OpExecutionMode %4 OriginUpperLeft
-               OpSource ESSL 310
-               OpName %4 "main"
-               OpName %8 "f"
-               OpName %12 "v2"
-               OpName %18 "v3"
-               OpName %23 "v4"
-               OpName %32 "b"
-               OpName %36 "bv2"
-               OpName %41 "bv3"
-               OpName %50 "bv4"
-          %2 = OpTypeVoid
-          %3 = OpTypeFunction %2
-          %6 = OpTypeFloat 32
-          %7 = OpTypePointer Function %6
-          %9 = OpConstant %6 42
-         %10 = OpTypeVector %6 2
-         %11 = OpTypePointer Function %10
-         %16 = OpTypeVector %6 3
-         %17 = OpTypePointer Function %16
-         %21 = OpTypeVector %6 4
-         %22 = OpTypePointer Function %21
-         %30 = OpTypeBool
-         %31 = OpTypePointer Function %30
-         %33 = OpConstantFalse %30
-         %34 = OpTypeVector %30 2
-         %35 = OpTypePointer Function %34
-         %37 = OpConstantTrue %30
-         %38 = OpConstantComposite %34 %37 %37
-         %39 = OpTypeVector %30 3
-         %40 = OpTypePointer Function %39
-         %48 = OpTypeVector %30 4
-         %49 = OpTypePointer Function %48
-         %51 = OpTypeInt 32 0
-         %52 = OpConstant %51 2
-         %55 = OpConstant %6 0
-         %57 = OpConstant %51 1
-          %4 = OpFunction %2 None %3
-          %5 = OpLabel
-          %8 = OpVariable %7 Function
-         %12 = OpVariable %11 Function
-         %18 = OpVariable %17 Function
-         %23 = OpVariable %22 Function
-         %32 = OpVariable %31 Function
-         %36 = OpVariable %35 Function
-         %41 = OpVariable %40 Function
-         %50 = OpVariable %49 Function
-               OpStore %8 %9
-         %13 = OpLoad %6 %8
-         %14 = OpLoad %6 %8
-         %15 = OpCompositeConstruct %10 %13 %14
-               OpStore %12 %15
-         %19 = OpLoad %10 %12
-         %20 = OpVectorShuffle %16 %19 %19 0 0 1
-               OpStore %18 %20
-         %24 = OpLoad %16 %18
-         %25 = OpLoad %6 %8
-         %26 = OpCompositeExtract %6 %24 0
-         %27 = OpCompositeExtract %6 %24 1
-         %28 = OpCompositeExtract %6 %24 2
-         %29 = OpCompositeConstruct %21 %26 %27 %28 %25
-               OpStore %23 %29
-               OpStore %32 %33
-               OpStore %36 %38
-         %42 = OpLoad %30 %32
-         %43 = OpLoad %34 %36
-         %44 = OpVectorShuffle %34 %43 %43 0 0
-         %45 = OpCompositeExtract %30 %44 0
-         %46 = OpCompositeExtract %30 %44 1
-         %47 = OpCompositeConstruct %39 %42 %45 %46
-               OpStore %41 %47
-         %53 = OpAccessChain %7 %23 %52
-         %54 = OpLoad %6 %53
-
-        %100 = OpCompositeConstruct %21 %20 %54
-        %101 = OpCompositeConstruct %21 %15 %19
-        %102 = OpCompositeConstruct %16 %27 %15
-        %103 = OpCompositeConstruct %48 %33 %47
-        %104 = OpCompositeConstruct %34 %42 %45
-        %105 = OpCompositeConstruct %39 %38 %46
-
-        %206 = OpCompositeExtract %30 %103 0
-         %86 = OpCopyObject %30 %206
-        %201 = OpCompositeExtract %6 %100 3
-         %56 = OpFOrdNotEqual %30 %201 %55
-        %200 = OpVectorShuffle %16 %100 %100 0 1 2
-         %80 = OpCopyObject %16 %200
-         %58 = OpAccessChain %7 %18 %57
-         %59 = OpLoad %6 %58
-         %60 = OpFOrdNotEqual %30 %59 %55
-         %61 = OpLoad %34 %36
-        %211 = OpCompositeExtract %30 %105 2
-         %62 = OpLogicalAnd %30 %45 %211
-        %209 = OpCompositeExtract %30 %104 1
-         %63 = OpLogicalOr %30 %209 %46
-         %64 = OpCompositeConstruct %48 %56 %60 %62 %63
-        %202 = OpVectorShuffle %10 %101 %101 0 1
-               OpStore %12 %202
-        %203 = OpVectorShuffle %10 %101 %101 2 3
-         %81 = OpVectorShuffle %16 %203 %19 0 0 1
-        %204 = OpCompositeExtract %6 %102 0
-         %82 = OpCompositeConstruct %21 %26 %204 %28 %25
-        %205 = OpVectorShuffle %10 %102 %102 1 2
-         %83 = OpCopyObject %10 %205
-        %207 = OpVectorShuffle %39 %103 %103 1 2 3
-         %84 = OpCopyObject %39 %207
-               OpStore %50 %64
-        %208 = OpCompositeExtract %30 %104 0
-         %85 = OpCopyObject %30 %208
-        %210 = OpVectorShuffle %34 %105 %105 0 1
-               OpStore %36 %210
-               OpReturn
-               OpFunctionEnd
-  )";
-
-  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
-}
-
-}  // namespace
-}  // namespace fuzz
-}  // namespace spvtools

+ 432 - 0
3rdparty/spirv-tools/test/fuzz/transformation_set_memory_operands_mask_test.cpp

@@ -0,0 +1,432 @@
+// Copyright (c) 2019 Google LLC
+//
+// 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 "source/fuzz/transformation_set_memory_operands_mask.h"
+#include "source/fuzz/instruction_descriptor.h"
+#include "test/fuzz/fuzz_test_util.h"
+
+namespace spvtools {
+namespace fuzz {
+namespace {
+
+TEST(TransformationSetMemoryOperandsMaskTest, PreSpirv14) {
+  std::string shader = R"(
+               OpCapability Shader
+          %1 = OpExtInstImport "GLSL.std.450"
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint Fragment %4 "main"
+               OpExecutionMode %4 OriginUpperLeft
+               OpSource ESSL 310
+               OpName %4 "main"
+               OpName %7 "Point3D"
+               OpMemberName %7 0 "x"
+               OpMemberName %7 1 "y"
+               OpMemberName %7 2 "z"
+               OpName %12 "global_points"
+               OpName %15 "block"
+               OpMemberName %15 0 "in_points"
+               OpMemberName %15 1 "in_point"
+               OpName %17 ""
+               OpName %133 "local_points"
+               OpMemberDecorate %7 0 Offset 0
+               OpMemberDecorate %7 1 Offset 4
+               OpMemberDecorate %7 2 Offset 8
+               OpDecorate %10 ArrayStride 16
+               OpMemberDecorate %15 0 Offset 0
+               OpMemberDecorate %15 1 Offset 192
+               OpDecorate %15 Block
+               OpDecorate %17 DescriptorSet 0
+               OpDecorate %17 Binding 0
+          %2 = OpTypeVoid
+          %3 = OpTypeFunction %2
+          %6 = OpTypeFloat 32
+          %7 = OpTypeStruct %6 %6 %6
+          %8 = OpTypeInt 32 0
+          %9 = OpConstant %8 12
+         %10 = OpTypeArray %7 %9
+         %11 = OpTypePointer Private %10
+         %12 = OpVariable %11 Private
+         %15 = OpTypeStruct %10 %7
+         %16 = OpTypePointer Uniform %15
+         %17 = OpVariable %16 Uniform
+         %18 = OpTypeInt 32 1
+         %19 = OpConstant %18 0
+         %20 = OpTypePointer Uniform %10
+         %24 = OpTypePointer Private %7
+         %27 = OpTypePointer Private %6
+         %30 = OpConstant %18 1
+        %132 = OpTypePointer Function %10
+        %135 = OpTypePointer Uniform %7
+        %145 = OpTypePointer Function %7
+          %4 = OpFunction %2 None %3
+          %5 = OpLabel
+        %133 = OpVariable %132 Function
+         %21 = OpAccessChain %20 %17 %19
+               OpCopyMemory %12 %21 Aligned 16
+               OpCopyMemory %133 %12 Volatile
+        %136 = OpAccessChain %135 %17 %30
+        %138 = OpAccessChain %24 %12 %19
+               OpCopyMemory %138 %136 None
+        %146 = OpAccessChain %145 %133 %30
+        %147 = OpLoad %7 %146 Volatile|Nontemporal|Aligned 16
+        %148 = OpAccessChain %24 %12 %19
+               OpStore %148 %147 Nontemporal
+               OpReturn
+               OpFunctionEnd
+  )";
+
+  const auto env = SPV_ENV_UNIVERSAL_1_3;
+  const auto consumer = nullptr;
+  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
+  ASSERT_TRUE(IsValid(env, context.get()));
+
+  FactManager fact_manager;
+
+  // Not OK: the instruction is not a memory access.
+  ASSERT_FALSE(TransformationSetMemoryOperandsMask(
+                   MakeInstructionDescriptor(21, SpvOpAccessChain, 0),
+                   SpvMemoryAccessMaskNone, 0)
+                   .IsApplicable(context.get(), fact_manager));
+
+  // Not OK to remove Aligned
+  ASSERT_FALSE(TransformationSetMemoryOperandsMask(
+                   MakeInstructionDescriptor(147, SpvOpLoad, 0),
+                   SpvMemoryAccessVolatileMask | SpvMemoryAccessNontemporalMask,
+                   0)
+                   .IsApplicable(context.get(), fact_manager));
+
+  TransformationSetMemoryOperandsMask transformation1(
+      MakeInstructionDescriptor(147, SpvOpLoad, 0),
+      SpvMemoryAccessAlignedMask | SpvMemoryAccessVolatileMask, 0);
+  ASSERT_TRUE(transformation1.IsApplicable(context.get(), fact_manager));
+  transformation1.Apply(context.get(), &fact_manager);
+
+  // Not OK to remove Aligned
+  ASSERT_FALSE(TransformationSetMemoryOperandsMask(
+                   MakeInstructionDescriptor(21, SpvOpCopyMemory, 0),
+                   SpvMemoryAccessMaskNone, 0)
+                   .IsApplicable(context.get(), fact_manager));
+
+  // OK: leaves the mask as is
+  ASSERT_TRUE(TransformationSetMemoryOperandsMask(
+                  MakeInstructionDescriptor(21, SpvOpCopyMemory, 0),
+                  SpvMemoryAccessAlignedMask, 0)
+                  .IsApplicable(context.get(), fact_manager));
+
+  // OK: adds Nontemporal and Volatile
+  TransformationSetMemoryOperandsMask transformation2(
+      MakeInstructionDescriptor(21, SpvOpCopyMemory, 0),
+      SpvMemoryAccessAlignedMask | SpvMemoryAccessNontemporalMask |
+          SpvMemoryAccessVolatileMask,
+      0);
+  ASSERT_TRUE(transformation2.IsApplicable(context.get(), fact_manager));
+  transformation2.Apply(context.get(), &fact_manager);
+
+  // Not OK to remove Volatile
+  ASSERT_FALSE(TransformationSetMemoryOperandsMask(
+                   MakeInstructionDescriptor(21, SpvOpCopyMemory, 1),
+                   SpvMemoryAccessNontemporalMask, 0)
+                   .IsApplicable(context.get(), fact_manager));
+
+  // Not OK to add Aligned
+  ASSERT_FALSE(TransformationSetMemoryOperandsMask(
+                   MakeInstructionDescriptor(21, SpvOpCopyMemory, 1),
+                   SpvMemoryAccessAlignedMask | SpvMemoryAccessVolatileMask, 0)
+                   .IsApplicable(context.get(), fact_manager));
+
+  // OK: adds Nontemporal
+  TransformationSetMemoryOperandsMask transformation3(
+      MakeInstructionDescriptor(21, SpvOpCopyMemory, 1),
+      SpvMemoryAccessNontemporalMask | SpvMemoryAccessVolatileMask, 0);
+  ASSERT_TRUE(transformation3.IsApplicable(context.get(), fact_manager));
+  transformation3.Apply(context.get(), &fact_manager);
+
+  // OK: adds Nontemporal and Volatile
+  TransformationSetMemoryOperandsMask transformation4(
+      MakeInstructionDescriptor(138, SpvOpCopyMemory, 0),
+      SpvMemoryAccessNontemporalMask | SpvMemoryAccessVolatileMask, 0);
+  ASSERT_TRUE(transformation4.IsApplicable(context.get(), fact_manager));
+  transformation4.Apply(context.get(), &fact_manager);
+
+  // OK: removes Nontemporal, adds Volatile
+  TransformationSetMemoryOperandsMask transformation5(
+      MakeInstructionDescriptor(148, SpvOpStore, 0),
+      SpvMemoryAccessVolatileMask, 0);
+  ASSERT_TRUE(transformation5.IsApplicable(context.get(), fact_manager));
+  transformation5.Apply(context.get(), &fact_manager);
+
+  std::string after_transformation = R"(
+               OpCapability Shader
+          %1 = OpExtInstImport "GLSL.std.450"
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint Fragment %4 "main"
+               OpExecutionMode %4 OriginUpperLeft
+               OpSource ESSL 310
+               OpName %4 "main"
+               OpName %7 "Point3D"
+               OpMemberName %7 0 "x"
+               OpMemberName %7 1 "y"
+               OpMemberName %7 2 "z"
+               OpName %12 "global_points"
+               OpName %15 "block"
+               OpMemberName %15 0 "in_points"
+               OpMemberName %15 1 "in_point"
+               OpName %17 ""
+               OpName %133 "local_points"
+               OpMemberDecorate %7 0 Offset 0
+               OpMemberDecorate %7 1 Offset 4
+               OpMemberDecorate %7 2 Offset 8
+               OpDecorate %10 ArrayStride 16
+               OpMemberDecorate %15 0 Offset 0
+               OpMemberDecorate %15 1 Offset 192
+               OpDecorate %15 Block
+               OpDecorate %17 DescriptorSet 0
+               OpDecorate %17 Binding 0
+          %2 = OpTypeVoid
+          %3 = OpTypeFunction %2
+          %6 = OpTypeFloat 32
+          %7 = OpTypeStruct %6 %6 %6
+          %8 = OpTypeInt 32 0
+          %9 = OpConstant %8 12
+         %10 = OpTypeArray %7 %9
+         %11 = OpTypePointer Private %10
+         %12 = OpVariable %11 Private
+         %15 = OpTypeStruct %10 %7
+         %16 = OpTypePointer Uniform %15
+         %17 = OpVariable %16 Uniform
+         %18 = OpTypeInt 32 1
+         %19 = OpConstant %18 0
+         %20 = OpTypePointer Uniform %10
+         %24 = OpTypePointer Private %7
+         %27 = OpTypePointer Private %6
+         %30 = OpConstant %18 1
+        %132 = OpTypePointer Function %10
+        %135 = OpTypePointer Uniform %7
+        %145 = OpTypePointer Function %7
+          %4 = OpFunction %2 None %3
+          %5 = OpLabel
+        %133 = OpVariable %132 Function
+         %21 = OpAccessChain %20 %17 %19
+               OpCopyMemory %12 %21 Aligned|Nontemporal|Volatile 16
+               OpCopyMemory %133 %12 Nontemporal|Volatile
+        %136 = OpAccessChain %135 %17 %30
+        %138 = OpAccessChain %24 %12 %19
+               OpCopyMemory %138 %136 Nontemporal|Volatile
+        %146 = OpAccessChain %145 %133 %30
+        %147 = OpLoad %7 %146 Aligned|Volatile 16
+        %148 = OpAccessChain %24 %12 %19
+               OpStore %148 %147 Volatile
+               OpReturn
+               OpFunctionEnd
+  )";
+  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
+}
+
+TEST(TransformationSetMemoryOperandsMaskTest, Spirv14) {
+  std::string shader = R"(
+               OpCapability Shader
+          %1 = OpExtInstImport "GLSL.std.450"
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint Fragment %4 "main" %12 %17
+               OpExecutionMode %4 OriginUpperLeft
+               OpSource ESSL 310
+               OpName %4 "main"
+               OpName %7 "Point3D"
+               OpMemberName %7 0 "x"
+               OpMemberName %7 1 "y"
+               OpMemberName %7 2 "z"
+               OpName %12 "global_points"
+               OpName %15 "block"
+               OpMemberName %15 0 "in_points"
+               OpMemberName %15 1 "in_point"
+               OpName %17 ""
+               OpName %133 "local_points"
+               OpMemberDecorate %7 0 Offset 0
+               OpMemberDecorate %7 1 Offset 4
+               OpMemberDecorate %7 2 Offset 8
+               OpDecorate %10 ArrayStride 16
+               OpMemberDecorate %15 0 Offset 0
+               OpMemberDecorate %15 1 Offset 192
+               OpDecorate %15 Block
+               OpDecorate %17 DescriptorSet 0
+               OpDecorate %17 Binding 0
+          %2 = OpTypeVoid
+          %3 = OpTypeFunction %2
+          %6 = OpTypeFloat 32
+          %7 = OpTypeStruct %6 %6 %6
+          %8 = OpTypeInt 32 0
+          %9 = OpConstant %8 12
+         %10 = OpTypeArray %7 %9
+         %11 = OpTypePointer Private %10
+         %12 = OpVariable %11 Private
+         %15 = OpTypeStruct %10 %7
+         %16 = OpTypePointer Uniform %15
+         %17 = OpVariable %16 Uniform
+         %18 = OpTypeInt 32 1
+         %19 = OpConstant %18 0
+         %20 = OpTypePointer Uniform %10
+         %24 = OpTypePointer Private %7
+         %27 = OpTypePointer Private %6
+         %30 = OpConstant %18 1
+        %132 = OpTypePointer Function %10
+        %135 = OpTypePointer Uniform %7
+        %145 = OpTypePointer Function %7
+          %4 = OpFunction %2 None %3
+          %5 = OpLabel
+        %133 = OpVariable %132 Function
+         %21 = OpAccessChain %20 %17 %19
+               OpCopyMemory %12 %21 Aligned 16 Nontemporal|Aligned 16
+               OpCopyMemory %133 %12 Volatile
+        %136 = OpAccessChain %135 %17 %30
+        %138 = OpAccessChain %24 %12 %19
+               OpCopyMemory %138 %136 None Aligned 16
+               OpCopyMemory %138 %136 Aligned 16
+        %146 = OpAccessChain %145 %133 %30
+        %147 = OpLoad %7 %146 Volatile|Nontemporal|Aligned 16
+        %148 = OpAccessChain %24 %12 %19
+               OpStore %148 %147 Nontemporal
+               OpReturn
+               OpFunctionEnd
+  )";
+
+  const auto env = SPV_ENV_UNIVERSAL_1_4;
+  const auto consumer = nullptr;
+  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
+  ASSERT_TRUE(IsValid(env, context.get()));
+
+  FactManager fact_manager;
+
+  TransformationSetMemoryOperandsMask transformation1(
+      MakeInstructionDescriptor(21, SpvOpCopyMemory, 0),
+      SpvMemoryAccessAlignedMask | SpvMemoryAccessVolatileMask, 1);
+  // Bad: cannot remove aligned
+  ASSERT_FALSE(TransformationSetMemoryOperandsMask(
+                   MakeInstructionDescriptor(21, SpvOpCopyMemory, 0),
+                   SpvMemoryAccessVolatileMask, 1)
+                   .IsApplicable(context.get(), fact_manager));
+  ASSERT_TRUE(transformation1.IsApplicable(context.get(), fact_manager));
+  transformation1.Apply(context.get(), &fact_manager);
+
+  TransformationSetMemoryOperandsMask transformation2(
+      MakeInstructionDescriptor(21, SpvOpCopyMemory, 1),
+      SpvMemoryAccessNontemporalMask | SpvMemoryAccessVolatileMask, 1);
+  // Bad: cannot remove volatile
+  ASSERT_FALSE(TransformationSetMemoryOperandsMask(
+                   MakeInstructionDescriptor(21, SpvOpCopyMemory, 1),
+                   SpvMemoryAccessNontemporalMask, 0)
+                   .IsApplicable(context.get(), fact_manager));
+  ASSERT_TRUE(transformation2.IsApplicable(context.get(), fact_manager));
+  transformation2.Apply(context.get(), &fact_manager);
+
+  TransformationSetMemoryOperandsMask transformation3(
+      MakeInstructionDescriptor(138, SpvOpCopyMemory, 0),
+      SpvMemoryAccessAlignedMask | SpvMemoryAccessNontemporalMask, 1);
+  // Bad: the first mask is None, so Aligned cannot be added to it.
+  ASSERT_FALSE(TransformationSetMemoryOperandsMask(
+                   MakeInstructionDescriptor(138, SpvOpCopyMemory, 0),
+                   SpvMemoryAccessAlignedMask | SpvMemoryAccessNontemporalMask,
+                   0)
+                   .IsApplicable(context.get(), fact_manager));
+  ASSERT_TRUE(transformation3.IsApplicable(context.get(), fact_manager));
+  transformation3.Apply(context.get(), &fact_manager);
+
+  TransformationSetMemoryOperandsMask transformation4(
+      MakeInstructionDescriptor(138, SpvOpCopyMemory, 1),
+      SpvMemoryAccessVolatileMask, 1);
+  ASSERT_TRUE(transformation4.IsApplicable(context.get(), fact_manager));
+  transformation4.Apply(context.get(), &fact_manager);
+
+  TransformationSetMemoryOperandsMask transformation5(
+      MakeInstructionDescriptor(147, SpvOpLoad, 0),
+      SpvMemoryAccessVolatileMask | SpvMemoryAccessAlignedMask, 0);
+  ASSERT_TRUE(transformation5.IsApplicable(context.get(), fact_manager));
+  transformation5.Apply(context.get(), &fact_manager);
+
+  TransformationSetMemoryOperandsMask transformation6(
+      MakeInstructionDescriptor(148, SpvOpStore, 0), SpvMemoryAccessMaskNone,
+      0);
+  ASSERT_TRUE(transformation6.IsApplicable(context.get(), fact_manager));
+  transformation6.Apply(context.get(), &fact_manager);
+
+  std::string after_transformation = R"(
+               OpCapability Shader
+          %1 = OpExtInstImport "GLSL.std.450"
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint Fragment %4 "main" %12 %17
+               OpExecutionMode %4 OriginUpperLeft
+               OpSource ESSL 310
+               OpName %4 "main"
+               OpName %7 "Point3D"
+               OpMemberName %7 0 "x"
+               OpMemberName %7 1 "y"
+               OpMemberName %7 2 "z"
+               OpName %12 "global_points"
+               OpName %15 "block"
+               OpMemberName %15 0 "in_points"
+               OpMemberName %15 1 "in_point"
+               OpName %17 ""
+               OpName %133 "local_points"
+               OpMemberDecorate %7 0 Offset 0
+               OpMemberDecorate %7 1 Offset 4
+               OpMemberDecorate %7 2 Offset 8
+               OpDecorate %10 ArrayStride 16
+               OpMemberDecorate %15 0 Offset 0
+               OpMemberDecorate %15 1 Offset 192
+               OpDecorate %15 Block
+               OpDecorate %17 DescriptorSet 0
+               OpDecorate %17 Binding 0
+          %2 = OpTypeVoid
+          %3 = OpTypeFunction %2
+          %6 = OpTypeFloat 32
+          %7 = OpTypeStruct %6 %6 %6
+          %8 = OpTypeInt 32 0
+          %9 = OpConstant %8 12
+         %10 = OpTypeArray %7 %9
+         %11 = OpTypePointer Private %10
+         %12 = OpVariable %11 Private
+         %15 = OpTypeStruct %10 %7
+         %16 = OpTypePointer Uniform %15
+         %17 = OpVariable %16 Uniform
+         %18 = OpTypeInt 32 1
+         %19 = OpConstant %18 0
+         %20 = OpTypePointer Uniform %10
+         %24 = OpTypePointer Private %7
+         %27 = OpTypePointer Private %6
+         %30 = OpConstant %18 1
+        %132 = OpTypePointer Function %10
+        %135 = OpTypePointer Uniform %7
+        %145 = OpTypePointer Function %7
+          %4 = OpFunction %2 None %3
+          %5 = OpLabel
+        %133 = OpVariable %132 Function
+         %21 = OpAccessChain %20 %17 %19
+               OpCopyMemory %12 %21 Aligned 16 Aligned|Volatile 16
+               OpCopyMemory %133 %12 Volatile Nontemporal|Volatile
+        %136 = OpAccessChain %135 %17 %30
+        %138 = OpAccessChain %24 %12 %19
+               OpCopyMemory %138 %136 None Aligned|Nontemporal 16
+               OpCopyMemory %138 %136 Aligned 16 Volatile
+        %146 = OpAccessChain %145 %133 %30
+        %147 = OpLoad %7 %146 Volatile|Aligned 16
+        %148 = OpAccessChain %24 %12 %19
+               OpStore %148 %147 None
+               OpReturn
+               OpFunctionEnd
+  )";
+  ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
+}
+
+}  // namespace
+}  // namespace fuzz
+}  // namespace spvtools

+ 409 - 0
3rdparty/spirv-tools/test/opt/inst_bindless_check_test.cpp

@@ -10820,6 +10820,415 @@ OpFunctionEnd
       true, 7u, 23u, true, true, 2u);
 }
 
+TEST_F(InstBindlessTest, InstBoundsInitSameBlockOpReplication) {
+  // Test that same block ops like OpSampledImage are replicated properly
+  // where needed.
+  //
+  // clang-format off
+  //
+  // #version 450 core
+  // #extension GL_EXT_nonuniform_qualifier : enable
+  //
+  // layout(location = 0) in vec2 inTexcoord;
+  // layout(location = 0) out vec4 outColor;
+  //
+  // layout(set = 0, binding = 0) uniform Uniforms {
+  //   vec2 var0;
+  // } uniforms;
+  //
+  // layout(set = 0, binding = 1) uniform sampler uniformSampler;
+  // layout(set = 0, binding = 2) uniform texture2D uniformTex;
+  // layout(set = 0, binding = 3) uniform texture2D uniformTexArr[8];
+  //
+  // void main() {
+  //   int index = 0;
+  //   float x = texture(sampler2D(uniformTexArr[nonuniformEXT(index)], uniformSampler), inTexcoord.xy).x;
+  //   float y = texture(sampler2D(uniformTex, uniformSampler), inTexcoord.xy * uniforms.var0.xy).x;
+  //   outColor = vec4(x, y, 0.0, 0.0);
+  // }
+  //
+  // clang-format on
+
+  const std::string defs_before =
+      R"(OpCapability Shader
+OpCapability ShaderNonUniformEXT
+OpCapability SampledImageArrayNonUniformIndexingEXT
+OpExtension "SPV_EXT_descriptor_indexing"
+%1 = OpExtInstImport "GLSL.std.450"
+OpMemoryModel Logical GLSL450
+OpEntryPoint Fragment %main "main" %inTexcoord %outColor
+OpExecutionMode %main OriginUpperLeft
+OpSource GLSL 450
+OpSourceExtension "GL_EXT_nonuniform_qualifier"
+OpName %main "main"
+OpName %index "index"
+OpName %x "x"
+OpName %uniformTexArr "uniformTexArr"
+OpName %uniformSampler "uniformSampler"
+OpName %inTexcoord "inTexcoord"
+OpName %y "y"
+OpName %uniformTex "uniformTex"
+OpName %Uniforms "Uniforms"
+OpMemberName %Uniforms 0 "var0"
+OpName %uniforms "uniforms"
+OpName %outColor "outColor"
+OpDecorate %uniformTexArr DescriptorSet 0
+OpDecorate %uniformTexArr Binding 3
+OpDecorate %19 NonUniformEXT
+OpDecorate %22 NonUniformEXT
+OpDecorate %uniformSampler DescriptorSet 0
+OpDecorate %uniformSampler Binding 1
+OpDecorate %inTexcoord Location 0
+OpDecorate %uniformTex DescriptorSet 0
+OpDecorate %uniformTex Binding 2
+OpMemberDecorate %Uniforms 0 Offset 0
+OpDecorate %Uniforms Block
+OpDecorate %uniforms DescriptorSet 0
+OpDecorate %uniforms Binding 0
+OpDecorate %outColor Location 0
+%void = OpTypeVoid
+%3 = OpTypeFunction %void
+%int = OpTypeInt 32 1
+%_ptr_Function_int = OpTypePointer Function %int
+%int_0 = OpConstant %int 0
+%float = OpTypeFloat 32
+%_ptr_Function_float = OpTypePointer Function %float
+%13 = OpTypeImage %float 2D 0 0 0 1 Unknown
+%uint = OpTypeInt 32 0
+%uint_8 = OpConstant %uint 8
+%_arr_13_uint_8 = OpTypeArray %13 %uint_8
+%_ptr_UniformConstant__arr_13_uint_8 = OpTypePointer UniformConstant %_arr_13_uint_8
+%uniformTexArr = OpVariable %_ptr_UniformConstant__arr_13_uint_8 UniformConstant
+%_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
+%23 = OpTypeSampler
+%_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
+%uniformSampler = OpVariable %_ptr_UniformConstant_23 UniformConstant
+%27 = OpTypeSampledImage %13
+%v2float = OpTypeVector %float 2
+%_ptr_Input_v2float = OpTypePointer Input %v2float
+%inTexcoord = OpVariable %_ptr_Input_v2float Input
+%v4float = OpTypeVector %float 4
+%uint_0 = OpConstant %uint 0
+%uniformTex = OpVariable %_ptr_UniformConstant_13 UniformConstant
+%Uniforms = OpTypeStruct %v2float
+%_ptr_Uniform_Uniforms = OpTypePointer Uniform %Uniforms
+%uniforms = OpVariable %_ptr_Uniform_Uniforms Uniform
+%_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
+%_ptr_Output_v4float = OpTypePointer Output %v4float
+%outColor = OpVariable %_ptr_Output_v4float Output
+%float_0 = OpConstant %float 0
+)";
+
+  const std::string defs_after =
+      R"(OpCapability Shader
+OpCapability ShaderNonUniform
+OpCapability SampledImageArrayNonUniformIndexing
+OpExtension "SPV_EXT_descriptor_indexing"
+OpExtension "SPV_KHR_storage_buffer_storage_class"
+%1 = OpExtInstImport "GLSL.std.450"
+OpMemoryModel Logical GLSL450
+OpEntryPoint Fragment %main "main" %inTexcoord %outColor %gl_FragCoord
+OpExecutionMode %main OriginUpperLeft
+OpSource GLSL 450
+OpSourceExtension "GL_EXT_nonuniform_qualifier"
+OpName %main "main"
+OpName %index "index"
+OpName %x "x"
+OpName %uniformTexArr "uniformTexArr"
+OpName %uniformSampler "uniformSampler"
+OpName %inTexcoord "inTexcoord"
+OpName %y "y"
+OpName %uniformTex "uniformTex"
+OpName %Uniforms "Uniforms"
+OpMemberName %Uniforms 0 "var0"
+OpName %uniforms "uniforms"
+OpName %outColor "outColor"
+OpDecorate %uniformTexArr DescriptorSet 0
+OpDecorate %uniformTexArr Binding 3
+OpDecorate %19 NonUniform
+OpDecorate %22 NonUniform
+OpDecorate %uniformSampler DescriptorSet 0
+OpDecorate %uniformSampler Binding 1
+OpDecorate %inTexcoord Location 0
+OpDecorate %uniformTex DescriptorSet 0
+OpDecorate %uniformTex Binding 2
+OpMemberDecorate %Uniforms 0 Offset 0
+OpDecorate %Uniforms Block
+OpDecorate %uniforms DescriptorSet 0
+OpDecorate %uniforms Binding 0
+OpDecorate %outColor Location 0
+OpDecorate %63 NonUniform
+OpDecorate %_runtimearr_uint ArrayStride 4
+OpDecorate %_struct_75 Block
+OpMemberDecorate %_struct_75 0 Offset 0
+OpMemberDecorate %_struct_75 1 Offset 4
+OpDecorate %77 DescriptorSet 7
+OpDecorate %77 Binding 0
+OpDecorate %gl_FragCoord BuiltIn FragCoord
+OpDecorate %_struct_132 Block
+OpMemberDecorate %_struct_132 0 Offset 0
+OpDecorate %134 DescriptorSet 7
+OpDecorate %134 Binding 1
+OpDecorate %151 NonUniform
+%void = OpTypeVoid
+%3 = OpTypeFunction %void
+%int = OpTypeInt 32 1
+%_ptr_Function_int = OpTypePointer Function %int
+%int_0 = OpConstant %int 0
+%float = OpTypeFloat 32
+%_ptr_Function_float = OpTypePointer Function %float
+%13 = OpTypeImage %float 2D 0 0 0 1 Unknown
+%uint = OpTypeInt 32 0
+%uint_8 = OpConstant %uint 8
+%_arr_13_uint_8 = OpTypeArray %13 %uint_8
+%_ptr_UniformConstant__arr_13_uint_8 = OpTypePointer UniformConstant %_arr_13_uint_8
+%uniformTexArr = OpVariable %_ptr_UniformConstant__arr_13_uint_8 UniformConstant
+%_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
+%23 = OpTypeSampler
+%_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
+%uniformSampler = OpVariable %_ptr_UniformConstant_23 UniformConstant
+%27 = OpTypeSampledImage %13
+%v2float = OpTypeVector %float 2
+%_ptr_Input_v2float = OpTypePointer Input %v2float
+%inTexcoord = OpVariable %_ptr_Input_v2float Input
+%v4float = OpTypeVector %float 4
+%uint_0 = OpConstant %uint 0
+%uniformTex = OpVariable %_ptr_UniformConstant_13 UniformConstant
+%Uniforms = OpTypeStruct %v2float
+%_ptr_Uniform_Uniforms = OpTypePointer Uniform %Uniforms
+%uniforms = OpVariable %_ptr_Uniform_Uniforms Uniform
+%_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
+%_ptr_Output_v4float = OpTypePointer Output %v4float
+%outColor = OpVariable %_ptr_Output_v4float Output
+%float_0 = OpConstant %float 0
+%bool = OpTypeBool
+%68 = OpTypeFunction %void %uint %uint %uint %uint
+%_runtimearr_uint = OpTypeRuntimeArray %uint
+%_struct_75 = OpTypeStruct %uint %_runtimearr_uint
+%_ptr_StorageBuffer__struct_75 = OpTypePointer StorageBuffer %_struct_75
+%77 = OpVariable %_ptr_StorageBuffer__struct_75 StorageBuffer
+%_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
+%uint_10 = OpConstant %uint 10
+%uint_4 = OpConstant %uint 4
+%uint_1 = OpConstant %uint 1
+%uint_23 = OpConstant %uint 23
+%uint_2 = OpConstant %uint 2
+%uint_3 = OpConstant %uint 3
+%_ptr_Input_v4float = OpTypePointer Input %v4float
+%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
+%v4uint = OpTypeVector %uint 4
+%uint_5 = OpConstant %uint 5
+%uint_7 = OpConstant %uint 7
+%uint_9 = OpConstant %uint 9
+%uint_79 = OpConstant %uint 79
+%122 = OpConstantNull %v4float
+%126 = OpTypeFunction %uint %uint %uint %uint %uint
+%_struct_132 = OpTypeStruct %_runtimearr_uint
+%_ptr_StorageBuffer__struct_132 = OpTypePointer StorageBuffer %_struct_132
+%134 = OpVariable %_ptr_StorageBuffer__struct_132 StorageBuffer
+%uint_87 = OpConstant %uint 87
+%165 = OpConstantNull %v2float
+%uint_89 = OpConstant %uint 89
+)";
+
+  const std::string func_before =
+      R"(%main = OpFunction %void None %3
+%5 = OpLabel
+%index = OpVariable %_ptr_Function_int Function
+%x = OpVariable %_ptr_Function_float Function
+%y = OpVariable %_ptr_Function_float Function
+OpStore %index %int_0
+%19 = OpLoad %int %index
+%21 = OpAccessChain %_ptr_UniformConstant_13 %uniformTexArr %19
+%22 = OpLoad %13 %21
+%26 = OpLoad %23 %uniformSampler
+%28 = OpSampledImage %27 %22 %26
+%32 = OpLoad %v2float %inTexcoord
+%34 = OpImageSampleImplicitLod %v4float %28 %32
+%36 = OpCompositeExtract %float %34 0
+OpStore %x %36
+%39 = OpLoad %13 %uniformTex
+%40 = OpLoad %23 %uniformSampler
+%41 = OpSampledImage %27 %39 %40
+%42 = OpLoad %v2float %inTexcoord
+%47 = OpAccessChain %_ptr_Uniform_v2float %uniforms %int_0
+%48 = OpLoad %v2float %47
+%49 = OpFMul %v2float %42 %48
+%50 = OpImageSampleImplicitLod %v4float %41 %49
+%51 = OpCompositeExtract %float %50 0
+OpStore %y %51
+%54 = OpLoad %float %x
+%55 = OpLoad %float %y
+%57 = OpCompositeConstruct %v4float %54 %55 %float_0 %float_0
+OpStore %outColor %57
+OpReturn
+OpFunctionEnd
+)";
+
+  const std::string func_after =
+      R"(%main = OpFunction %void None %3
+%5 = OpLabel
+%index = OpVariable %_ptr_Function_int Function
+%x = OpVariable %_ptr_Function_float Function
+%y = OpVariable %_ptr_Function_float Function
+OpStore %index %int_0
+%19 = OpLoad %int %index
+%21 = OpAccessChain %_ptr_UniformConstant_13 %uniformTexArr %19
+%22 = OpLoad %13 %21
+%26 = OpLoad %23 %uniformSampler
+%28 = OpSampledImage %27 %22 %26
+%32 = OpLoad %v2float %inTexcoord
+%59 = OpULessThan %bool %19 %uint_8
+OpSelectionMerge %60 None
+OpBranchConditional %59 %61 %62
+%61 = OpLabel
+%63 = OpLoad %13 %21
+%64 = OpSampledImage %27 %63 %26
+%124 = OpBitcast %uint %19
+%146 = OpFunctionCall %uint %125 %uint_0 %uint_0 %uint_3 %124
+%147 = OpINotEqual %bool %146 %uint_0
+OpSelectionMerge %148 None
+OpBranchConditional %147 %149 %150
+%149 = OpLabel
+%151 = OpLoad %13 %21
+%152 = OpSampledImage %27 %151 %26
+%153 = OpImageSampleImplicitLod %v4float %152 %32
+OpBranch %148
+%150 = OpLabel
+%154 = OpBitcast %uint %19
+%155 = OpFunctionCall %void %67 %uint_79 %uint_1 %154 %uint_0
+OpBranch %148
+%148 = OpLabel
+%156 = OpPhi %v4float %153 %149 %122 %150
+OpBranch %60
+%62 = OpLabel
+%66 = OpBitcast %uint %19
+%121 = OpFunctionCall %void %67 %uint_79 %uint_0 %66 %uint_8
+OpBranch %60
+%60 = OpLabel
+%123 = OpPhi %v4float %156 %148 %122 %62
+%36 = OpCompositeExtract %float %123 0
+OpStore %x %36
+%39 = OpLoad %13 %uniformTex
+%40 = OpLoad %23 %uniformSampler
+%41 = OpSampledImage %27 %39 %40
+%42 = OpLoad %v2float %inTexcoord
+%47 = OpAccessChain %_ptr_Uniform_v2float %uniforms %int_0
+%157 = OpFunctionCall %uint %125 %uint_0 %uint_0 %uint_0 %uint_0
+%158 = OpINotEqual %bool %157 %uint_0
+OpSelectionMerge %159 None
+OpBranchConditional %158 %160 %161
+%160 = OpLabel
+%162 = OpLoad %v2float %47
+OpBranch %159
+%161 = OpLabel
+%164 = OpFunctionCall %void %67 %uint_87 %uint_1 %uint_0 %uint_0
+OpBranch %159
+%159 = OpLabel
+%166 = OpPhi %v2float %162 %160 %165 %161
+%49 = OpFMul %v2float %42 %166
+%167 = OpSampledImage %27 %39 %40
+%168 = OpFunctionCall %uint %125 %uint_0 %uint_0 %uint_2 %uint_0
+%169 = OpINotEqual %bool %168 %uint_0
+OpSelectionMerge %170 None
+OpBranchConditional %169 %171 %172
+%171 = OpLabel
+%173 = OpLoad %13 %uniformTex
+%174 = OpSampledImage %27 %173 %40
+%175 = OpImageSampleImplicitLod %v4float %174 %49
+OpBranch %170
+%172 = OpLabel
+%177 = OpFunctionCall %void %67 %uint_89 %uint_1 %uint_0 %uint_0
+OpBranch %170
+%170 = OpLabel
+%178 = OpPhi %v4float %175 %171 %122 %172
+%51 = OpCompositeExtract %float %178 0
+OpStore %y %51
+%54 = OpLoad %float %x
+%55 = OpLoad %float %y
+%57 = OpCompositeConstruct %v4float %54 %55 %float_0 %float_0
+OpStore %outColor %57
+OpReturn
+OpFunctionEnd
+)";
+
+  const std::string new_funcs =
+      R"(%67 = OpFunction %void None %68
+%69 = OpFunctionParameter %uint
+%70 = OpFunctionParameter %uint
+%71 = OpFunctionParameter %uint
+%72 = OpFunctionParameter %uint
+%73 = OpLabel
+%79 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_0
+%82 = OpAtomicIAdd %uint %79 %uint_4 %uint_0 %uint_10
+%83 = OpIAdd %uint %82 %uint_10
+%84 = OpArrayLength %uint %77 1
+%85 = OpULessThanEqual %bool %83 %84
+OpSelectionMerge %86 None
+OpBranchConditional %85 %87 %86
+%87 = OpLabel
+%88 = OpIAdd %uint %82 %uint_0
+%90 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %88
+OpStore %90 %uint_10
+%92 = OpIAdd %uint %82 %uint_1
+%93 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %92
+OpStore %93 %uint_23
+%95 = OpIAdd %uint %82 %uint_2
+%96 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %95
+OpStore %96 %69
+%98 = OpIAdd %uint %82 %uint_3
+%99 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %98
+OpStore %99 %uint_4
+%102 = OpLoad %v4float %gl_FragCoord
+%104 = OpBitcast %v4uint %102
+%105 = OpCompositeExtract %uint %104 0
+%106 = OpIAdd %uint %82 %uint_4
+%107 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %106
+OpStore %107 %105
+%108 = OpCompositeExtract %uint %104 1
+%110 = OpIAdd %uint %82 %uint_5
+%111 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %110
+OpStore %111 %108
+%113 = OpIAdd %uint %82 %uint_7
+%114 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %113
+OpStore %114 %70
+%115 = OpIAdd %uint %82 %uint_8
+%116 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %115
+OpStore %116 %71
+%118 = OpIAdd %uint %82 %uint_9
+%119 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %118
+OpStore %119 %72
+OpBranch %86
+%86 = OpLabel
+OpReturn
+OpFunctionEnd
+%125 = OpFunction %uint None %126
+%127 = OpFunctionParameter %uint
+%128 = OpFunctionParameter %uint
+%129 = OpFunctionParameter %uint
+%130 = OpFunctionParameter %uint
+%131 = OpLabel
+%135 = OpAccessChain %_ptr_StorageBuffer_uint %134 %uint_0 %127
+%136 = OpLoad %uint %135
+%137 = OpIAdd %uint %136 %128
+%138 = OpAccessChain %_ptr_StorageBuffer_uint %134 %uint_0 %137
+%139 = OpLoad %uint %138
+%140 = OpIAdd %uint %139 %129
+%141 = OpAccessChain %_ptr_StorageBuffer_uint %134 %uint_0 %140
+%142 = OpLoad %uint %141
+%143 = OpIAdd %uint %142 %130
+%144 = OpAccessChain %_ptr_StorageBuffer_uint %134 %uint_0 %143
+%145 = OpLoad %uint %144
+OpReturnValue %145
+OpFunctionEnd
+)";
+
+  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
+  SinglePassRunAndCheck<InstBindlessCheckPass>(
+      defs_before + func_before, defs_after + func_after + new_funcs, true,
+      true, 7u, 23u, true, true, 2u);
+}
+
 // TODO(greg-lunarg): Add tests to verify handling of these cases:
 //
 //   Compute shader