transformation_add_bit_instruction_synonym.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2020 André Perez Maselco
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_FUZZ_TRANSFORMATION_ADD_BIT_INSTRUCTION_SYNONYM_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_ADD_BIT_INSTRUCTION_SYNONYM_H_
  16. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  17. #include "source/fuzz/transformation.h"
  18. #include "source/fuzz/transformation_context.h"
  19. #include "source/opt/ir_context.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. // clang-format off
  23. // SPIR-V code to help understand the transformation.
  24. //
  25. // ----------------------------------------------------------------------------------------------------------------
  26. // | Reference shader | Variant shader |
  27. // ----------------------------------------------------------------------------------------------------------------
  28. // | OpCapability Shader | OpCapability Shader |
  29. // | OpCapability Int8 | OpCapability Int8 |
  30. // | %1 = OpExtInstImport "GLSL.std.450" | %1 = OpExtInstImport "GLSL.std.450" |
  31. // | OpMemoryModel Logical GLSL450 | OpMemoryModel Logical GLSL450 |
  32. // | OpEntryPoint Vertex %7 "main" | OpEntryPoint Vertex %7 "main" |
  33. // | | |
  34. // | ; Types | ; Types |
  35. // | %2 = OpTypeInt 8 0 | %2 = OpTypeInt 8 0 |
  36. // | %3 = OpTypeVoid | %3 = OpTypeVoid |
  37. // | %4 = OpTypeFunction %3 | %4 = OpTypeFunction %3 |
  38. // | | |
  39. // | ; Constants | ; Constants |
  40. // | %5 = OpConstant %2 0 | %5 = OpConstant %2 0 |
  41. // | %6 = OpConstant %2 1 | %6 = OpConstant %2 1 |
  42. // | | %10 = OpConstant %2 2 |
  43. // | ; main function | %11 = OpConstant %2 3 |
  44. // | %7 = OpFunction %3 None %4 | %12 = OpConstant %2 4 |
  45. // | %8 = OpLabel | %13 = OpConstant %2 5 |
  46. // | %9 = OpBitwiseOr %2 %5 %6 ; bit instruction | %14 = OpConstant %2 6 |
  47. // | OpReturn | %15 = OpConstant %2 7 |
  48. // | OpFunctionEnd | |
  49. // | | ; main function |
  50. // | | %7 = OpFunction %3 None %4 |
  51. // | | %8 = OpLabel |
  52. // | | |
  53. // | | %16 = OpBitFieldUExtract %2 %5 %5 %6 ; extracts bit 0 from %5 |
  54. // | | %17 = OpBitFieldUExtract %2 %6 %5 %6 ; extracts bit 0 from %6 |
  55. // | | %18 = OpBitwiseOr %2 %16 %17 |
  56. // | | |
  57. // | | %19 = OpBitFieldUExtract %2 %5 %6 %6 ; extracts bit 1 from %5 |
  58. // | | %20 = OpBitFieldUExtract %2 %6 %6 %6 ; extracts bit 1 from %6 |
  59. // | | %21 = OpBitwiseOr %2 %19 %20 |
  60. // | | |
  61. // | | %22 = OpBitFieldUExtract %2 %5 %10 %6 ; extracts bit 2 from %5 |
  62. // | | %23 = OpBitFieldUExtract %2 %6 %10 %6 ; extracts bit 2 from %6 |
  63. // | | %24 = OpBitwiseOr %2 %22 %23 |
  64. // | | |
  65. // | | %25 = OpBitFieldUExtract %2 %5 %11 %6 ; extracts bit 3 from %5 |
  66. // | | %26 = OpBitFieldUExtract %2 %6 %11 %6 ; extracts bit 3 from %6 |
  67. // | | %27 = OpBitwiseOr %2 %25 %26 |
  68. // | | |
  69. // | | %28 = OpBitFieldUExtract %2 %5 %12 %6 ; extracts bit 4 from %5 |
  70. // | | %29 = OpBitFieldUExtract %2 %6 %12 %6 ; extracts bit 4 from %6 |
  71. // | | %30 = OpBitwiseOr %2 %28 %29 |
  72. // | | |
  73. // | | %31 = OpBitFieldUExtract %2 %5 %13 %6 ; extracts bit 5 from %5 |
  74. // | | %32 = OpBitFieldUExtract %2 %6 %13 %6 ; extracts bit 5 from %6 |
  75. // | | %33 = OpBitwiseOr %2 %31 %32 |
  76. // | | |
  77. // | | %34 = OpBitFieldUExtract %2 %5 %14 %6 ; extracts bit 6 from %5 |
  78. // | | %35 = OpBitFieldUExtract %2 %6 %14 %6 ; extracts bit 6 from %6 |
  79. // | | %36 = OpBitwiseOr %2 %34 %35 |
  80. // | | |
  81. // | | %37 = OpBitFieldUExtract %2 %5 %15 %6 ; extracts bit 7 from %5 |
  82. // | | %38 = OpBitFieldUExtract %2 %6 %15 %6 ; extracts bit 7 from %6 |
  83. // | | %39 = OpBitwiseOr %2 %37 %38 |
  84. // | | |
  85. // | | %40 = OpBitFieldInsert %2 %18 %21 %6 %6 ; inserts bit 1 |
  86. // | | %41 = OpBitFieldInsert %2 %40 %24 %10 %6 ; inserts bit 2 |
  87. // | | %42 = OpBitFieldInsert %2 %41 %27 %11 %6 ; inserts bit 3 |
  88. // | | %43 = OpBitFieldInsert %2 %42 %30 %12 %6 ; inserts bit 4 |
  89. // | | %44 = OpBitFieldInsert %2 %43 %33 %13 %6 ; inserts bit 5 |
  90. // | | %45 = OpBitFieldInsert %2 %44 %36 %14 %6 ; inserts bit 6 |
  91. // | | %46 = OpBitFieldInsert %2 %45 %39 %15 %6 ; inserts bit 7 |
  92. // | | %9 = OpBitwiseOr %2 %5 %6 ; bit instruction |
  93. // | | OpReturn |
  94. // | | OpFunctionEnd |
  95. // ----------------------------------------------------------------------------------------------------------------
  96. //
  97. // After the transformation, %9 and %46 will be synonymous.
  98. // clang-format on
  99. class TransformationAddBitInstructionSynonym : public Transformation {
  100. public:
  101. explicit TransformationAddBitInstructionSynonym(
  102. protobufs::TransformationAddBitInstructionSynonym message);
  103. TransformationAddBitInstructionSynonym(
  104. const uint32_t instruction_result_id,
  105. const std::vector<uint32_t>& fresh_ids);
  106. // - |message_.instruction_result_id| must be a bit instruction.
  107. // - |message_.fresh_ids| must be fresh ids needed to apply the
  108. // transformation.
  109. bool IsApplicable(
  110. opt::IRContext* ir_context,
  111. const TransformationContext& transformation_context) const override;
  112. // Adds a bit instruction synonym.
  113. void Apply(opt::IRContext* ir_context,
  114. TransformationContext* transformation_context) const override;
  115. std::unordered_set<uint32_t> GetFreshIds() const override;
  116. protobufs::Transformation ToMessage() const override;
  117. // Returns the number of fresh ids required to apply the transformation.
  118. static uint32_t GetRequiredFreshIdCount(opt::IRContext* ir_context,
  119. opt::Instruction* bit_instruction);
  120. // Returns true if:
  121. // - A |bit_instruction| is one of OpBitwiseOr, OpBitwiseAnd, OpBitwiseXor or
  122. // OpNot.
  123. // - |bit_instruction|'s operands are scalars.
  124. // - The operands have the same signedness.
  125. static bool IsInstructionSupported(opt::IRContext* ir_context,
  126. opt::Instruction* instruction);
  127. private:
  128. protobufs::TransformationAddBitInstructionSynonym message_;
  129. // Adds OpBitwise* or OpNot synonym.
  130. void AddOpBitwiseOrOpNotSynonym(opt::IRContext* ir_context,
  131. TransformationContext* transformation_context,
  132. opt::Instruction* bitwise_instruction) const;
  133. };
  134. } // namespace fuzz
  135. } // namespace spvtools
  136. #endif // SOURCE_FUZZ_TRANSFORMATION_ADD_BIT_INSTRUCTION_SYNONYM_H_