spirv-fuzz: Minor refactoring (#3507)
diff --git a/source/fuzz/fuzzer_pass_add_parameters.cpp b/source/fuzz/fuzzer_pass_add_parameters.cpp
index 9273452..c03b4f4 100644
--- a/source/fuzz/fuzzer_pass_add_parameters.cpp
+++ b/source/fuzz/fuzzer_pass_add_parameters.cpp
@@ -32,7 +32,16 @@
FuzzerPassAddParameters::~FuzzerPassAddParameters() = default;
void FuzzerPassAddParameters::Apply() {
- const auto& type_candidates = ComputeTypeCandidates();
+ // Compute type candidates for the new parameter.
+ std::vector<uint32_t> type_candidates;
+ for (const auto& type_inst : GetIRContext()->module()->GetTypes()) {
+ const auto* type =
+ GetIRContext()->get_type_mgr()->GetType(type_inst->result_id());
+ assert(type && "Type instruction is not registered in the type manager");
+ if (TransformationAddParameter::IsParameterTypeSupported(*type)) {
+ type_candidates.push_back(type_inst->result_id());
+ }
+ }
if (type_candidates.empty()) {
// The module contains no suitable types to use in new parameters.
@@ -71,32 +80,6 @@
}
}
-std::vector<uint32_t> FuzzerPassAddParameters::ComputeTypeCandidates() const {
- std::vector<uint32_t> result;
-
- for (const auto* type_inst : GetIRContext()->module()->GetTypes()) {
- // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
- // the number of types we support here is limited by the number of types
- // supported by |FindOrCreateZeroConstant|.
- switch (type_inst->opcode()) {
- case SpvOpTypeBool:
- case SpvOpTypeInt:
- case SpvOpTypeFloat:
- case SpvOpTypeArray:
- case SpvOpTypeMatrix:
- case SpvOpTypeVector:
- case SpvOpTypeStruct: {
- result.push_back(type_inst->result_id());
- } break;
- default:
- // Ignore other types.
- break;
- }
- }
-
- return result;
-}
-
uint32_t FuzzerPassAddParameters::GetNumberOfParameters(
const opt::Function& function) const {
const auto* type = GetIRContext()->get_type_mgr()->GetType(
diff --git a/source/fuzz/fuzzer_pass_add_parameters.h b/source/fuzz/fuzzer_pass_add_parameters.h
index feb009f..f1261ae 100644
--- a/source/fuzz/fuzzer_pass_add_parameters.h
+++ b/source/fuzz/fuzzer_pass_add_parameters.h
@@ -37,10 +37,6 @@
void Apply() override;
private:
- // Uses types, defined in the module, to compute a vector of their ids, which
- // will be used as type ids of new parameters.
- std::vector<uint32_t> ComputeTypeCandidates() const;
-
// Returns number of parameters of |function|.
uint32_t GetNumberOfParameters(const opt::Function& function) const;
};
diff --git a/source/fuzz/fuzzer_pass_replace_parameter_with_global.cpp b/source/fuzz/fuzzer_pass_replace_parameter_with_global.cpp
index 87b2b5d..907c5b4 100644
--- a/source/fuzz/fuzzer_pass_replace_parameter_with_global.cpp
+++ b/source/fuzz/fuzzer_pass_replace_parameter_with_global.cpp
@@ -58,7 +58,7 @@
param->type_id());
assert(param_type && "Parameter has invalid type");
return TransformationReplaceParameterWithGlobal::
- CanReplaceFunctionParameterType(*param_type);
+ IsParameterTypeSupported(*param_type);
})) {
continue;
}
@@ -71,8 +71,9 @@
param_type =
GetIRContext()->get_type_mgr()->GetType(replaced_param->type_id());
assert(param_type && "Parameter has invalid type");
- } while (!TransformationReplaceParameterWithGlobal::
- CanReplaceFunctionParameterType(*param_type));
+ } while (
+ !TransformationReplaceParameterWithGlobal::IsParameterTypeSupported(
+ *param_type));
assert(replaced_param && "Unable to find a parameter to replace");
diff --git a/source/fuzz/transformation_add_parameter.cpp b/source/fuzz/transformation_add_parameter.cpp
index b2dd7be..b64c7e5 100644
--- a/source/fuzz/transformation_add_parameter.cpp
+++ b/source/fuzz/transformation_add_parameter.cpp
@@ -56,7 +56,7 @@
const auto* initializer_type =
ir_context->get_type_mgr()->GetType(initializer_inst->type_id());
- if (!initializer_type || initializer_type->AsVoid()) {
+ if (!initializer_type || !IsParameterTypeSupported(*initializer_type)) {
return false;
}
@@ -139,5 +139,28 @@
return result;
}
+bool TransformationAddParameter::IsParameterTypeSupported(
+ const opt::analysis::Type& type) {
+ // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
+ // Think about other type instructions we can add here.
+ switch (type.kind()) {
+ case opt::analysis::Type::kBool:
+ case opt::analysis::Type::kInteger:
+ case opt::analysis::Type::kFloat:
+ case opt::analysis::Type::kArray:
+ case opt::analysis::Type::kMatrix:
+ case opt::analysis::Type::kVector:
+ return true;
+ case opt::analysis::Type::kStruct:
+ return std::all_of(type.AsStruct()->element_types().begin(),
+ type.AsStruct()->element_types().end(),
+ [](const opt::analysis::Type* element_type) {
+ return IsParameterTypeSupported(*element_type);
+ });
+ default:
+ return false;
+ }
+}
+
} // namespace fuzz
} // namespace spvtools
diff --git a/source/fuzz/transformation_add_parameter.h b/source/fuzz/transformation_add_parameter.h
index 42b5fbb..e6b9019 100644
--- a/source/fuzz/transformation_add_parameter.h
+++ b/source/fuzz/transformation_add_parameter.h
@@ -35,7 +35,8 @@
// - |function_id| must be a valid result id of some non-entry-point function
// in the module.
// - |initializer_id| must be a valid result id of some instruction in the
- // module. Instruction's type may not be OpTypeVoid.
+ // module. Instruction's type must be supported by this transformation
+ // as specified by IsParameterTypeSupported function.
// - |parameter_fresh_id| and |function_type_fresh_id| are fresh ids and are
// not equal.
bool IsApplicable(
@@ -52,6 +53,10 @@
protobufs::Transformation ToMessage() const override;
+ // Returns true if the type of the parameter is supported by this
+ // transformation.
+ static bool IsParameterTypeSupported(const opt::analysis::Type& type);
+
private:
protobufs::TransformationAddParameter message_;
};
diff --git a/source/fuzz/transformation_replace_parameter_with_global.cpp b/source/fuzz/transformation_replace_parameter_with_global.cpp
index 2ce7cfe..61c7f10 100644
--- a/source/fuzz/transformation_replace_parameter_with_global.cpp
+++ b/source/fuzz/transformation_replace_parameter_with_global.cpp
@@ -76,7 +76,7 @@
const auto* param_type =
ir_context->get_type_mgr()->GetType(param_inst->type_id());
assert(param_type && "Parameter has invalid type");
- if (!CanReplaceFunctionParameterType(*param_type)) {
+ if (!IsParameterTypeSupported(*param_type)) {
return false;
}
@@ -217,7 +217,7 @@
return result;
}
-bool TransformationReplaceParameterWithGlobal::CanReplaceFunctionParameterType(
+bool TransformationReplaceParameterWithGlobal::IsParameterTypeSupported(
const opt::analysis::Type& type) {
// TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
// Think about other type instructions we can add here.
@@ -230,12 +230,11 @@
case opt::analysis::Type::kVector:
return true;
case opt::analysis::Type::kStruct:
- return std::all_of(
- type.AsStruct()->element_types().begin(),
- type.AsStruct()->element_types().end(),
- [](const opt::analysis::Type* element_type) {
- return CanReplaceFunctionParameterType(*element_type);
- });
+ return std::all_of(type.AsStruct()->element_types().begin(),
+ type.AsStruct()->element_types().end(),
+ [](const opt::analysis::Type* element_type) {
+ return IsParameterTypeSupported(*element_type);
+ });
default:
return false;
}
diff --git a/source/fuzz/transformation_replace_parameter_with_global.h b/source/fuzz/transformation_replace_parameter_with_global.h
index cd379b0..49e7585 100644
--- a/source/fuzz/transformation_replace_parameter_with_global.h
+++ b/source/fuzz/transformation_replace_parameter_with_global.h
@@ -55,7 +55,7 @@
// Returns true if the type of the parameter is supported by this
// transformation.
- static bool CanReplaceFunctionParameterType(const opt::analysis::Type& type);
+ static bool IsParameterTypeSupported(const opt::analysis::Type& type);
private:
protobufs::TransformationReplaceParameterWithGlobal message_;