id_use_descriptor.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/id_use_descriptor.h"
  15. #include "source/fuzz/instruction_descriptor.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. opt::Instruction* FindInstructionContainingUse(
  19. const protobufs::IdUseDescriptor& id_use_descriptor,
  20. opt::IRContext* context) {
  21. auto result =
  22. FindInstruction(id_use_descriptor.enclosing_instruction(), context);
  23. if (!result) {
  24. return nullptr;
  25. }
  26. if (id_use_descriptor.in_operand_index() >= result->NumInOperands()) {
  27. return nullptr;
  28. }
  29. if (result->GetSingleWordInOperand(id_use_descriptor.in_operand_index()) !=
  30. id_use_descriptor.id_of_interest()) {
  31. return nullptr;
  32. }
  33. return result;
  34. }
  35. protobufs::IdUseDescriptor MakeIdUseDescriptor(
  36. uint32_t id_of_interest,
  37. const protobufs::InstructionDescriptor& enclosing_instruction,
  38. uint32_t in_operand_index) {
  39. protobufs::IdUseDescriptor result;
  40. result.set_id_of_interest(id_of_interest);
  41. *result.mutable_enclosing_instruction() = enclosing_instruction;
  42. result.set_in_operand_index(in_operand_index);
  43. return result;
  44. }
  45. protobufs::IdUseDescriptor MakeIdUseDescriptorFromUse(
  46. opt::IRContext* context, opt::Instruction* inst,
  47. uint32_t in_operand_index) {
  48. const auto& in_operand = inst->GetInOperand(in_operand_index);
  49. assert(spvIsInIdType(in_operand.type));
  50. return MakeIdUseDescriptor(in_operand.words[0],
  51. MakeInstructionDescriptor(context, inst),
  52. in_operand_index);
  53. }
  54. } // namespace fuzz
  55. } // namespace spvtools