transformation_set_selection_control.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. protobufs::TransformationSetSelectionControl message)
  19. : message_(std::move(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* ir_context, const TransformationContext& /*unused*/) const {
  27. assert((spv::SelectionControlMask(message_.selection_control()) ==
  28. spv::SelectionControlMask::MaskNone ||
  29. spv::SelectionControlMask(message_.selection_control()) ==
  30. spv::SelectionControlMask::Flatten ||
  31. spv::SelectionControlMask(message_.selection_control()) ==
  32. spv::SelectionControlMask::DontFlatten) &&
  33. "Selection control should never be set to something other than "
  34. "'None', 'Flatten' or 'DontFlatten'");
  35. if (auto block = ir_context->get_instr_block(message_.block_id())) {
  36. if (auto merge_inst = block->GetMergeInst()) {
  37. return merge_inst->opcode() == spv::Op::OpSelectionMerge;
  38. }
  39. }
  40. // Either the block did not exit, or did not end with OpSelectionMerge.
  41. return false;
  42. }
  43. void TransformationSetSelectionControl::Apply(
  44. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  45. ir_context->get_instr_block(message_.block_id())
  46. ->GetMergeInst()
  47. ->SetInOperand(1, {message_.selection_control()});
  48. }
  49. protobufs::Transformation TransformationSetSelectionControl::ToMessage() const {
  50. protobufs::Transformation result;
  51. *result.mutable_set_selection_control() = message_;
  52. return result;
  53. }
  54. std::unordered_set<uint32_t> TransformationSetSelectionControl::GetFreshIds()
  55. const {
  56. return std::unordered_set<uint32_t>();
  57. }
  58. } // namespace fuzz
  59. } // namespace spvtools