Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spirv-fuzz: Fix width in FuzzerPassAddEquationInstructions #3685

Merged
merged 3 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 52 additions & 14 deletions source/fuzz/fuzzer_pass_add_equation_instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,8 @@ void FuzzerPassAddEquationInstructions::Apply() {
return;
}
case SpvOpBitcast: {
std::vector<const opt::Instruction*> candidate_instructions;
for (const auto* inst : available_instructions) {
const auto* type =
GetIRContext()->get_type_mgr()->GetType(inst->type_id());
assert(type && "Instruction has invalid type");
if ((type->AsVector() &&
(type->AsVector()->element_type()->AsInteger() ||
type->AsVector()->element_type()->AsFloat())) ||
type->AsInteger() || type->AsFloat()) {
// We support OpBitcast for only scalars or vectors of
// numerical type.
candidate_instructions.push_back(inst);
}
}
const auto candidate_instructions =
GetNumericalInstructions(available_instructions);

if (!candidate_instructions.empty()) {
const auto* operand_inst =
Expand Down Expand Up @@ -356,5 +344,55 @@ FuzzerPassAddEquationInstructions::RestrictToElementBitWidth(
return result;
}

std::vector<opt::Instruction*>
FuzzerPassAddEquationInstructions::GetNumericalInstructions(
const std::vector<opt::Instruction*>& instructions) const {
std::vector<opt::Instruction*> result;

for (auto* inst : instructions) {
const auto* type = GetIRContext()->get_type_mgr()->GetType(inst->type_id());
assert(type && "Instruction has invalid type");

if (const auto* vector_type = type->AsVector()) {
type = vector_type->element_type();
}

if (!type->AsInteger() && !type->AsFloat()) {
// Only numerical scalars or vectors of numerical components are
// supported.
continue;
}

switch (type->AsInteger() ? type->AsInteger()->width()
: type->AsFloat()->width()) {
case 32:
break;
case 64:
if (!GetIRContext()->get_feature_mgr()->HasCapability(
SpvCapabilityFloat64) ||
!GetIRContext()->get_feature_mgr()->HasCapability(
SpvCapabilityInt64)) {
continue;
}
break;
case 16:
if (!GetIRContext()->get_feature_mgr()->HasCapability(
SpvCapabilityFloat16) ||
!GetIRContext()->get_feature_mgr()->HasCapability(
SpvCapabilityInt16)) {
continue;
}
break;
default:
// |element_width| is not supported.
continue;
}

result.push_back(inst);
}

return result;
}

} // namespace fuzz
} // namespace spvtools
8 changes: 8 additions & 0 deletions source/fuzz/fuzzer_pass_add_equation_instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class FuzzerPassAddEquationInstructions : public FuzzerPass {
std::vector<opt::Instruction*> GetBooleanInstructions(
const std::vector<opt::Instruction*>& instructions) const;

// Yields those instructions in |instructions| that have a scalar numerical or
// a vector of numerical components type. Only 16, 32 and 64-bit numericals
// are supported if both OpTypeInt and OpTypeFloat instructions can be created
// with the specified width (e.g. for 16-bit types both Float16 and Int16
// capabilities must be present).
std::vector<opt::Instruction*> GetNumericalInstructions(
const std::vector<opt::Instruction*>& instructions) const;

// Requires that |instructions| are scalars or vectors of some type. Returns
// only those instructions whose width is |width|. If |width| is 1 this means
// the scalars.
Expand Down