<NME> fuzzer_pass_replace_linear_algebra_instructions.cpp
<BEF> // Copyright (c) 2020 André Perez Maselco
//
// 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_replace_linear_algebra_instructions.h"

#include "source/fuzz/fuzzer_util.h"
#include "source/fuzz/instruction_descriptor.h"
#include "source/fuzz/transformation_replace_linear_algebra_instruction.h"

namespace spvtools {
namespace fuzz {

FuzzerPassReplaceLinearAlgebraInstructions::
    FuzzerPassReplaceLinearAlgebraInstructions(
        opt::IRContext* ir_context,
        TransformationContext* transformation_context,
        FuzzerContext* fuzzer_context,
        protobufs::TransformationSequence* transformations,
        bool ignore_inapplicable_transformations)
    : FuzzerPass(ir_context, transformation_context, fuzzer_context,
                 transformations, ignore_inapplicable_transformations) {}

void FuzzerPassReplaceLinearAlgebraInstructions::Apply() {
  // For each instruction, checks whether it is a linear algebra instruction. In
void FuzzerPassReplaceLinearAlgebraInstructions::Apply() {
  // For each instruction, checks whether it is a linear algebra instruction. In
  // this case, the transformation is randomly applied.
  GetIRContext()->module()->ForEachInst([this](opt::Instruction* instruction) {
    if (!spvOpcodeIsLinearAlgebra(instruction->opcode())) {
      return;
    }

    if (!GetFuzzerContext()->ChoosePercentage(
            GetFuzzerContext()
                ->GetChanceOfReplacingLinearAlgebraInstructions())) {
      return;
    }

    ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(
        GetFuzzerContext()->GetFreshIds(
            TransformationReplaceLinearAlgebraInstruction::
                GetRequiredFreshIdCount(GetIRContext(), instruction)),
        MakeInstructionDescriptor(GetIRContext(), instruction)));
  });
}

}  // namespace fuzz

<MSG> spirv-fuzz: iterate over blocks in replace linear algebra pass (#3654)

This PR changes the way FuzzerPassReplaceLinearAlgebraInstructions iterates over the module instructions. It avoids iterating over non-relevant instructions (globals, types, etc.).
<DFF> @@ -36,23 +36,27 @@ FuzzerPassReplaceLinearAlgebraInstructions::
 void FuzzerPassReplaceLinearAlgebraInstructions::Apply() {
   // For each instruction, checks whether it is a linear algebra instruction. In
   // this case, the transformation is randomly applied.
-  GetIRContext()->module()->ForEachInst([this](opt::Instruction* instruction) {
-    if (!spvOpcodeIsLinearAlgebra(instruction->opcode())) {
-      return;
+  for (auto& function : *GetIRContext()->module()) {
+    for (auto& block : function) {
+      for (auto& instruction : block) {
+        if (!spvOpcodeIsLinearAlgebra(instruction.opcode())) {
+          continue;
+        }
+
+        if (!GetFuzzerContext()->ChoosePercentage(
+                GetFuzzerContext()
+                    ->GetChanceOfReplacingLinearAlgebraInstructions())) {
+          continue;
+        }
+
+        ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(
+            GetFuzzerContext()->GetFreshIds(
+                TransformationReplaceLinearAlgebraInstruction::
+                    GetRequiredFreshIdCount(GetIRContext(), &instruction)),
+            MakeInstructionDescriptor(GetIRContext(), &instruction)));
+      }
     }
-
-    if (!GetFuzzerContext()->ChoosePercentage(
-            GetFuzzerContext()
-                ->GetChanceOfReplacingLinearAlgebraInstructions())) {
-      return;
-    }
-
-    ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(
-        GetFuzzerContext()->GetFreshIds(
-            TransformationReplaceLinearAlgebraInstruction::
-                GetRequiredFreshIdCount(GetIRContext(), instruction)),
-        MakeInstructionDescriptor(GetIRContext(), instruction)));
-  });
+  }
 }
 
 }  // namespace fuzz

{'commit': 'diff --git a/source/fuzz/fuzzer_pass_replace_linear_algebra_instructions.cpp b/source/fuzz/fuzzer_pass_replace_linear_algebra_instructions.cpp\nindex 984b7c34..c3e65789 100644\n--- a/source/fuzz/fuzzer_pass_replace_linear_algebra_instructions.cpp\n+++ b/source/fuzz/fuzzer_pass_replace_linear_algebra_instructions.cpp\n@@ -36,23 +36,27 @@ FuzzerPassReplaceLinearAlgebraInstructions::\n void FuzzerPassReplaceLinearAlgebraInstructions::Apply() {\n   // For each instruction, checks whether it is a linear algebra instruction. In\n   // this case, the transformation is randomly applied.\n-  GetIRContext()->module()->ForEachInst([this](opt::Instruction* instruction) {\n-    if (!spvOpcodeIsLinearAlgebra(instruction->opcode())) {\n-      return;\n+  for (auto& function : *GetIRContext()->module()) {\n+    for (auto& block : function) {\n+      for (auto& instruction : block) {\n+        if (!spvOpcodeIsLinearAlgebra(instruction.opcode())) {\n+          continue;\n+        }\n+\n+        if (!GetFuzzerContext()->ChoosePercentage(\n+                GetFuzzerContext()\n+                    ->GetChanceOfReplacingLinearAlgebraInstructions())) {\n+          continue;\n+        }\n+\n+        ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(\n+            GetFuzzerContext()->GetFreshIds(\n+                TransformationReplaceLinearAlgebraInstruction::\n+                    GetRequiredFreshIdCount(GetIRContext(), &instruction)),\n+            MakeInstructionDescriptor(GetIRContext(), &instruction)));\n+      }\n     }\n-\n-    if (!GetFuzzerContext()->ChoosePercentage(\n-            GetFuzzerContext()\n-                ->GetChanceOfReplacingLinearAlgebraInstructions())) {\n-      return;\n-    }\n-\n-    ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(\n-        GetFuzzerContext()->GetFreshIds(\n-            TransformationReplaceLinearAlgebraInstruction::\n-                GetRequiredFreshIdCount(GetIRContext(), instruction)),\n-        MakeInstructionDescriptor(GetIRContext(), instruction)));\n-  });\n+  }\n }\n \n }  // namespace fuzz\n', 'commit_subject': 'spirv-fuzz: iterate over blocks in replace linear algebra pass (#3654)', 'repo_name': 'KhronosGroup/SPIRV-Tools', 'committer': {'name': 'GitHub', 'email': '1505422b2465e9a84f6fdfaa161078890c593f06@github.com', 'time_sec': '1596715217', 'tz_offset': '60', 'date': {'seconds': '1596715217'}}, 'commit_tree': '7a2565acca7be1d5a98f41bce26cc4c2f228f0fb', 'commit_message': 'spirv-fuzz: iterate over blocks in replace linear algebra pass (#3654)\n\nThis PR changes the way FuzzerPassReplaceLinearAlgebraInstructions iterates over the module instructions. It avoids iterating over non-relevant instructions (globals, types, etc.).', 'language_name': 'C++', 'license': 'apache-2.0', 'hunks': '@@ -36,23 +36,27 @@ FuzzerPassReplaceLinearAlgebraInstructions::\n void FuzzerPassReplaceLinearAlgebraInstructions::Apply() {\n   // For each instruction, checks whether it is a linear algebra instruction. In\n   // this case, the transformation is randomly applied.\n-  GetIRContext()->module()->ForEachInst([this](opt::Instruction* instruction) {\n-    if (!spvOpcodeIsLinearAlgebra(instruction->opcode())) {\n-      return;\n+  for (auto& function : *GetIRContext()->module()) {\n+    for (auto& block : function) {\n+      for (auto& instruction : block) {\n+        if (!spvOpcodeIsLinearAlgebra(instruction.opcode())) {\n+          continue;\n+        }\n+\n+        if (!GetFuzzerContext()->ChoosePercentage(\n+                GetFuzzerContext()\n+                    ->GetChanceOfReplacingLinearAlgebraInstructions())) {\n+          continue;\n+        }\n+\n+        ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(\n+            GetFuzzerContext()->GetFreshIds(\n+                TransformationReplaceLinearAlgebraInstruction::\n+                    GetRequiredFreshIdCount(GetIRContext(), &instruction)),\n+            MakeInstructionDescriptor(GetIRContext(), &instruction)));\n+      }\n     }\n-\n-    if (!GetFuzzerContext()->ChoosePercentage(\n-            GetFuzzerContext()\n-                ->GetChanceOfReplacingLinearAlgebraInstructions())) {\n-      return;\n-    }\n-\n-    ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(\n-        GetFuzzerContext()->GetFreshIds(\n-            TransformationReplaceLinearAlgebraInstruction::\n-                GetRequiredFreshIdCount(GetIRContext(), instruction)),\n-        MakeInstructionDescriptor(GetIRContext(), instruction)));\n-  });\n+  }\n }\n \n }  // namespace fuzz\n', 'addition_count': 20, 'deletion_count': 16, 'src_file': 'a/source/fuzz/fuzzer_pass_replace_linear_algebra_instructions.cpp', 'tgt_file': 'b/source/fuzz/fuzzer_pass_replace_linear_algebra_instructions.cpp', 'file_extension': '.cpp', 'before_file': '// Copyright (c) 2020 André Perez Maselco\n//\n// Licensed under the Apache License, Version 2.0 (the "License");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n//     http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an "AS IS" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n#include "source/fuzz/fuzzer_pass_replace_linear_algebra_instructions.h"\n\n#include "source/fuzz/fuzzer_util.h"\n#include "source/fuzz/instruction_descriptor.h"\n#include "source/fuzz/transformation_replace_linear_algebra_instruction.h"\n\nnamespace spvtools {\nnamespace fuzz {\n\nFuzzerPassReplaceLinearAlgebraInstructions::\n    FuzzerPassReplaceLinearAlgebraInstructions(\n        opt::IRContext* ir_context,\n        TransformationContext* transformation_context,\n        FuzzerContext* fuzzer_context,\n        protobufs::TransformationSequence* transformations,\n        bool ignore_inapplicable_transformations)\n    : FuzzerPass(ir_context, transformation_context, fuzzer_context,\n                 transformations, ignore_inapplicable_transformations) {}\n\nvoid FuzzerPassReplaceLinearAlgebraInstructions::Apply() {\n  // For each instruction, checks whether it is a linear algebra instruction. In\nvoid FuzzerPassReplaceLinearAlgebraInstructions::Apply() {\n  // For each instruction, checks whether it is a linear algebra instruction. In\n  // this case, the transformation is randomly applied.\n  GetIRContext()->module()->ForEachInst([this](opt::Instruction* instruction) {\n    if (!spvOpcodeIsLinearAlgebra(instruction->opcode())) {\n      return;\n    }\n\n    if (!GetFuzzerContext()->ChoosePercentage(\n            GetFuzzerContext()\n                ->GetChanceOfReplacingLinearAlgebraInstructions())) {\n      return;\n    }\n\n    ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(\n        GetFuzzerContext()->GetFreshIds(\n            TransformationReplaceLinearAlgebraInstruction::\n                GetRequiredFreshIdCount(GetIRContext(), instruction)),\n        MakeInstructionDescriptor(GetIRContext(), instruction)));\n  });\n}\n\n}  // namespace fuzz\n', 'message': 'spirv-fuzz: iterate over blocks in replace linear algebra pass (#3654)\n\nThis PR changes the way FuzzerPassReplaceLinearAlgebraInstructions iterates over the module instructions. It avoids iterating over non-relevant instructions (globals, types, etc.).', 'author_name': 'GitHub', 'lang': 'cpp'}