transformation_replace_id_with_synonym.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2019 Google LLC
  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. #include "source/fuzz/transformation_replace_id_with_synonym.h"
  15. #include <algorithm>
  16. #include "source/fuzz/data_descriptor.h"
  17. #include "source/fuzz/fuzzer_util.h"
  18. #include "source/fuzz/id_use_descriptor.h"
  19. #include "source/opt/types.h"
  20. #include "source/util/make_unique.h"
  21. namespace spvtools {
  22. namespace fuzz {
  23. TransformationReplaceIdWithSynonym::TransformationReplaceIdWithSynonym(
  24. protobufs::TransformationReplaceIdWithSynonym message)
  25. : message_(std::move(message)) {}
  26. TransformationReplaceIdWithSynonym::TransformationReplaceIdWithSynonym(
  27. protobufs::IdUseDescriptor id_use_descriptor, uint32_t synonymous_id) {
  28. *message_.mutable_id_use_descriptor() = std::move(id_use_descriptor);
  29. message_.set_synonymous_id(synonymous_id);
  30. }
  31. bool TransformationReplaceIdWithSynonym::IsApplicable(
  32. opt::IRContext* ir_context,
  33. const TransformationContext& transformation_context) const {
  34. auto id_of_interest = message_.id_use_descriptor().id_of_interest();
  35. // Does the fact manager know about the synonym?
  36. auto data_descriptor_for_synonymous_id =
  37. MakeDataDescriptor(message_.synonymous_id(), {});
  38. if (!transformation_context.GetFactManager()->IsSynonymous(
  39. MakeDataDescriptor(id_of_interest, {}),
  40. data_descriptor_for_synonymous_id)) {
  41. return false;
  42. }
  43. // Does the id use descriptor in the transformation identify an instruction?
  44. auto use_instruction =
  45. FindInstructionContainingUse(message_.id_use_descriptor(), ir_context);
  46. if (!use_instruction) {
  47. return false;
  48. }
  49. uint32_t type_id_of_interest =
  50. ir_context->get_def_use_mgr()->GetDef(id_of_interest)->type_id();
  51. uint32_t type_id_synonym = ir_context->get_def_use_mgr()
  52. ->GetDef(message_.synonymous_id())
  53. ->type_id();
  54. // If the id of interest and the synonym are scalar or vector integer
  55. // constants with different signedness, their use can only be swapped if the
  56. // instruction is agnostic to the signedness of the operand.
  57. if (!fuzzerutil::TypesAreCompatible(
  58. ir_context, use_instruction->opcode(),
  59. message_.id_use_descriptor().in_operand_index(), type_id_of_interest,
  60. type_id_synonym)) {
  61. return false;
  62. }
  63. // Is the use suitable for being replaced in principle?
  64. if (!fuzzerutil::IdUseCanBeReplaced(
  65. ir_context, transformation_context, use_instruction,
  66. message_.id_use_descriptor().in_operand_index())) {
  67. return false;
  68. }
  69. // The transformation is applicable if the synonymous id is available at the
  70. // use point.
  71. return fuzzerutil::IdIsAvailableAtUse(
  72. ir_context, use_instruction,
  73. message_.id_use_descriptor().in_operand_index(),
  74. message_.synonymous_id());
  75. }
  76. void TransformationReplaceIdWithSynonym::Apply(
  77. spvtools::opt::IRContext* ir_context,
  78. TransformationContext* /*unused*/) const {
  79. auto instruction_to_change =
  80. FindInstructionContainingUse(message_.id_use_descriptor(), ir_context);
  81. instruction_to_change->SetInOperand(
  82. message_.id_use_descriptor().in_operand_index(),
  83. {message_.synonymous_id()});
  84. ir_context->get_def_use_mgr()->EraseUseRecordsOfOperandIds(
  85. instruction_to_change);
  86. ir_context->get_def_use_mgr()->AnalyzeInstUse(instruction_to_change);
  87. // No analyses need to be invalidated, since the transformation is local to a
  88. // block, and the def-use analysis has been updated.
  89. }
  90. protobufs::Transformation TransformationReplaceIdWithSynonym::ToMessage()
  91. const {
  92. protobufs::Transformation result;
  93. *result.mutable_replace_id_with_synonym() = message_;
  94. return result;
  95. }
  96. std::unordered_set<uint32_t> TransformationReplaceIdWithSynonym::GetFreshIds()
  97. const {
  98. return std::unordered_set<uint32_t>();
  99. }
  100. } // namespace fuzz
  101. } // namespace spvtools