transformation_replace_parameter_with_global.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (c) 2020 Vasyl Teliman
  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_parameter_with_global.h"
  15. #include <vector>
  16. #include "source/fuzz/fuzzer_util.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationReplaceParameterWithGlobal::
  20. TransformationReplaceParameterWithGlobal(
  21. protobufs::TransformationReplaceParameterWithGlobal message)
  22. : message_(std::move(message)) {}
  23. TransformationReplaceParameterWithGlobal::
  24. TransformationReplaceParameterWithGlobal(
  25. uint32_t function_type_fresh_id, uint32_t parameter_id,
  26. uint32_t global_variable_fresh_id) {
  27. message_.set_function_type_fresh_id(function_type_fresh_id);
  28. message_.set_parameter_id(parameter_id);
  29. message_.set_global_variable_fresh_id(global_variable_fresh_id);
  30. }
  31. bool TransformationReplaceParameterWithGlobal::IsApplicable(
  32. opt::IRContext* ir_context,
  33. const TransformationContext& transformation_context) const {
  34. // Check that |parameter_id| is valid.
  35. const auto* param_inst =
  36. ir_context->get_def_use_mgr()->GetDef(message_.parameter_id());
  37. if (!param_inst || param_inst->opcode() != spv::Op::OpFunctionParameter) {
  38. return false;
  39. }
  40. // Check that function exists and is not an entry point.
  41. const auto* function = fuzzerutil::GetFunctionFromParameterId(
  42. ir_context, message_.parameter_id());
  43. if (!function ||
  44. fuzzerutil::FunctionIsEntryPoint(ir_context, function->result_id())) {
  45. return false;
  46. }
  47. // We already know that the function has at least one parameter -
  48. // |parameter_id|.
  49. // Check that replaced parameter has valid type.
  50. if (!IsParameterTypeSupported(ir_context, param_inst->type_id())) {
  51. return false;
  52. }
  53. // Check that initializer for the global variable exists in the module.
  54. if (fuzzerutil::MaybeGetZeroConstant(ir_context, transformation_context,
  55. param_inst->type_id(), false) == 0) {
  56. return false;
  57. }
  58. // Check that pointer type for the global variable exists in the module.
  59. if (!fuzzerutil::MaybeGetPointerType(ir_context, param_inst->type_id(),
  60. spv::StorageClass::Private)) {
  61. return false;
  62. }
  63. return fuzzerutil::IsFreshId(ir_context, message_.function_type_fresh_id()) &&
  64. fuzzerutil::IsFreshId(ir_context,
  65. message_.global_variable_fresh_id()) &&
  66. message_.function_type_fresh_id() !=
  67. message_.global_variable_fresh_id();
  68. }
  69. void TransformationReplaceParameterWithGlobal::Apply(
  70. opt::IRContext* ir_context,
  71. TransformationContext* transformation_context) const {
  72. const auto* param_inst =
  73. ir_context->get_def_use_mgr()->GetDef(message_.parameter_id());
  74. assert(param_inst && "Parameter must exist");
  75. // Create global variable to store parameter's value.
  76. fuzzerutil::AddGlobalVariable(
  77. ir_context, message_.global_variable_fresh_id(),
  78. fuzzerutil::MaybeGetPointerType(ir_context, param_inst->type_id(),
  79. spv::StorageClass::Private),
  80. spv::StorageClass::Private,
  81. fuzzerutil::MaybeGetZeroConstant(ir_context, *transformation_context,
  82. param_inst->type_id(), false));
  83. auto* function = fuzzerutil::GetFunctionFromParameterId(
  84. ir_context, message_.parameter_id());
  85. assert(function && "Function must exist");
  86. // Insert an OpLoad instruction right after OpVariable instructions.
  87. auto it = function->begin()->begin();
  88. while (it != function->begin()->end() &&
  89. !fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpLoad, it)) {
  90. ++it;
  91. }
  92. assert(fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpLoad, it) &&
  93. "Can't insert OpLoad or OpCopyMemory into the first basic block of "
  94. "the function");
  95. it.InsertBefore(MakeUnique<opt::Instruction>(
  96. ir_context, spv::Op::OpLoad, param_inst->type_id(),
  97. param_inst->result_id(),
  98. opt::Instruction::OperandList{
  99. {SPV_OPERAND_TYPE_ID, {message_.global_variable_fresh_id()}}}));
  100. // Calculate the index of the replaced parameter (we need to know this to
  101. // remove operands from the OpFunctionCall).
  102. auto params = fuzzerutil::GetParameters(ir_context, function->result_id());
  103. auto parameter_index = static_cast<uint32_t>(params.size());
  104. for (uint32_t i = 0, n = static_cast<uint32_t>(params.size()); i < n; ++i) {
  105. if (params[i]->result_id() == message_.parameter_id()) {
  106. parameter_index = i;
  107. break;
  108. }
  109. }
  110. assert(parameter_index != params.size() &&
  111. "Parameter must exist in the function");
  112. // Update all relevant OpFunctionCall instructions.
  113. for (auto* inst : fuzzerutil::GetCallers(ir_context, function->result_id())) {
  114. assert(
  115. fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpStore, inst) &&
  116. "Can't insert OpStore right before the function call");
  117. // Insert an OpStore before the OpFunctionCall. +1 since the first
  118. // operand of OpFunctionCall is an id of the function.
  119. inst->InsertBefore(MakeUnique<opt::Instruction>(
  120. ir_context, spv::Op::OpStore, 0, 0,
  121. opt::Instruction::OperandList{
  122. {SPV_OPERAND_TYPE_ID, {message_.global_variable_fresh_id()}},
  123. {SPV_OPERAND_TYPE_ID,
  124. {inst->GetSingleWordInOperand(parameter_index + 1)}}}));
  125. // +1 since the first operand of OpFunctionCall is an id of the
  126. // function.
  127. inst->RemoveInOperand(parameter_index + 1);
  128. }
  129. // Remove the parameter from the function.
  130. fuzzerutil::RemoveParameter(ir_context, message_.parameter_id());
  131. // Update function's type.
  132. {
  133. // We use a separate scope here since |old_function_type| might become a
  134. // dangling pointer after the call to the fuzzerutil::UpdateFunctionType.
  135. auto* old_function_type = fuzzerutil::GetFunctionType(ir_context, function);
  136. assert(old_function_type && "Function has invalid type");
  137. // +1 and -1 since the first operand is the return type id.
  138. std::vector<uint32_t> parameter_type_ids;
  139. for (uint32_t i = 1; i < old_function_type->NumInOperands(); ++i) {
  140. if (i - 1 != parameter_index) {
  141. parameter_type_ids.push_back(
  142. old_function_type->GetSingleWordInOperand(i));
  143. }
  144. }
  145. fuzzerutil::UpdateFunctionType(
  146. ir_context, function->result_id(), message_.function_type_fresh_id(),
  147. old_function_type->GetSingleWordInOperand(0), parameter_type_ids);
  148. }
  149. // Make sure our changes are analyzed
  150. ir_context->InvalidateAnalysesExceptFor(
  151. opt::IRContext::Analysis::kAnalysisNone);
  152. // Mark the pointee of the global variable storing the parameter's value as
  153. // irrelevant if replaced parameter is irrelevant.
  154. if (transformation_context->GetFactManager()->IdIsIrrelevant(
  155. message_.parameter_id())) {
  156. transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  157. message_.global_variable_fresh_id());
  158. }
  159. }
  160. protobufs::Transformation TransformationReplaceParameterWithGlobal::ToMessage()
  161. const {
  162. protobufs::Transformation result;
  163. *result.mutable_replace_parameter_with_global() = message_;
  164. return result;
  165. }
  166. bool TransformationReplaceParameterWithGlobal::IsParameterTypeSupported(
  167. opt::IRContext* ir_context, uint32_t param_type_id) {
  168. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
  169. // Think about other type instructions we can add here.
  170. return fuzzerutil::CanCreateConstant(ir_context, param_type_id);
  171. }
  172. std::unordered_set<uint32_t>
  173. TransformationReplaceParameterWithGlobal::GetFreshIds() const {
  174. return {message_.function_type_fresh_id(),
  175. message_.global_variable_fresh_id()};
  176. }
  177. } // namespace fuzz
  178. } // namespace spvtools