transformation_set_selection_control.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_set_selection_control.h"
  15. namespace spvtools {
  16. namespace fuzz {
  17. TransformationSetSelectionControl::TransformationSetSelectionControl(
  18. const spvtools::fuzz::protobufs::TransformationSetSelectionControl& message)
  19. : message_(message) {}
  20. TransformationSetSelectionControl::TransformationSetSelectionControl(
  21. uint32_t block_id, uint32_t selection_control) {
  22. message_.set_block_id(block_id);
  23. message_.set_selection_control(selection_control);
  24. }
  25. bool TransformationSetSelectionControl::IsApplicable(
  26. opt::IRContext* context, const FactManager& /*unused*/) const {
  27. assert((message_.selection_control() == SpvSelectionControlMaskNone ||
  28. message_.selection_control() == SpvSelectionControlFlattenMask ||
  29. message_.selection_control() == SpvSelectionControlDontFlattenMask) &&
  30. "Selection control should never be set to something other than "
  31. "'None', 'Flatten' or 'DontFlatten'");
  32. if (auto block = context->get_instr_block(message_.block_id())) {
  33. if (auto merge_inst = block->GetMergeInst()) {
  34. return merge_inst->opcode() == SpvOpSelectionMerge;
  35. }
  36. }
  37. // Either the block did not exit, or did not end with OpSelectionMerge.
  38. return false;
  39. }
  40. void TransformationSetSelectionControl::Apply(opt::IRContext* context,
  41. FactManager* /*unused*/) const {
  42. context->get_instr_block(message_.block_id())
  43. ->GetMergeInst()
  44. ->SetInOperand(1, {message_.selection_control()});
  45. }
  46. protobufs::Transformation TransformationSetSelectionControl::ToMessage() const {
  47. protobufs::Transformation result;
  48. *result.mutable_set_selection_control() = message_;
  49. return result;
  50. }
  51. } // namespace fuzz
  52. } // namespace spvtools