spirv-fuzz: Fix regression (#3396)

Fixes #3392.
This commit is contained in:
Vasyl Teliman 2020-06-05 14:28:07 +03:00 committed by GitHub
parent 74130f2d3a
commit 94808bd0f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 38 deletions

View File

@ -550,6 +550,39 @@ bool IsNullConstantSupported(const opt::analysis::Type& type) {
type.AsDeviceEvent() || type.AsReserveId() || type.AsQueue();
}
bool GlobalVariablesMustBeDeclaredInEntryPointInterfaces(
const opt::IRContext* ir_context) {
// TODO(afd): We capture the universal environments for which this requirement
// holds. The check should be refined on demand for other target
// environments.
switch (ir_context->grammar().target_env()) {
case SPV_ENV_UNIVERSAL_1_0:
case SPV_ENV_UNIVERSAL_1_1:
case SPV_ENV_UNIVERSAL_1_2:
case SPV_ENV_UNIVERSAL_1_3:
return false;
default:
return true;
}
}
void AddVariableIdToEntryPointInterfaces(opt::IRContext* context, uint32_t id) {
if (GlobalVariablesMustBeDeclaredInEntryPointInterfaces(context)) {
// Conservatively add this global to the interface of every entry point in
// the module. This means that the global is available for other
// transformations to use.
//
// A downside of this is that the global will be in the interface even if it
// ends up never being used.
//
// TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3111) revisit
// this if a more thorough approach to entry point interfaces is taken.
for (auto& entry_point : context->module()->entry_points()) {
entry_point.AddOperand({SPV_OPERAND_TYPE_ID, {id}});
}
}
}
} // namespace fuzzerutil
} // namespace fuzz

View File

@ -215,6 +215,17 @@ uint32_t MaybeGetPointerType(opt::IRContext* context, uint32_t pointee_type_id,
// to have an OpConstantNull value.
bool IsNullConstantSupported(const opt::analysis::Type& type);
// Returns true if and only if the SPIR-V version being used requires that
// global variables accessed in the static call graph of an entry point need
// to be listed in that entry point's interface.
bool GlobalVariablesMustBeDeclaredInEntryPointInterfaces(
const opt::IRContext* context);
// Adds |id| into the interface of every entry point of the shader.
// Does nothing if SPIR-V doesn't require global variables, that are accessed
// from an entry point function, to be listed in that function's interface.
void AddVariableIdToEntryPointInterfaces(opt::IRContext* context, uint32_t id);
} // namespace fuzzerutil
} // namespace fuzz

View File

@ -105,20 +105,8 @@ void TransformationAddGlobalVariable::Apply(
input_operands));
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
if (GlobalVariablesMustBeDeclaredInEntryPointInterfaces(ir_context)) {
// Conservatively add this global to the interface of every entry point in
// the module. This means that the global is available for other
// transformations to use.
//
// A downside of this is that the global will be in the interface even if it
// ends up never being used.
//
// TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3111) revisit
// this if a more thorough approach to entry point interfaces is taken.
for (auto& entry_point : ir_context->module()->entry_points()) {
entry_point.AddOperand({SPV_OPERAND_TYPE_ID, {message_.fresh_id()}});
}
}
fuzzerutil::AddVariableIdToEntryPointInterfaces(ir_context,
message_.fresh_id());
if (message_.value_is_irrelevant()) {
transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
@ -137,22 +125,5 @@ protobufs::Transformation TransformationAddGlobalVariable::ToMessage() const {
return result;
}
bool TransformationAddGlobalVariable::
GlobalVariablesMustBeDeclaredInEntryPointInterfaces(
opt::IRContext* ir_context) {
// TODO(afd): We capture the universal environments for which this requirement
// holds. The check should be refined on demand for other target
// environments.
switch (ir_context->grammar().target_env()) {
case SPV_ENV_UNIVERSAL_1_0:
case SPV_ENV_UNIVERSAL_1_1:
case SPV_ENV_UNIVERSAL_1_2:
case SPV_ENV_UNIVERSAL_1_3:
return false;
default:
return true;
}
}
} // namespace fuzz
} // namespace spvtools

View File

@ -58,12 +58,6 @@ class TransformationAddGlobalVariable : public Transformation {
protobufs::Transformation ToMessage() const override;
private:
// Returns true if and only if the SPIR-V version being used requires that
// global variables accessed in the static call graph of an entry point need
// to be listed in that entry point's interface.
static bool GlobalVariablesMustBeDeclaredInEntryPointInterfaces(
opt::IRContext* ir_context);
protobufs::TransformationAddGlobalVariable message_;
};

View File

@ -109,6 +109,9 @@ void TransformationPushIdThroughVariable::Apply(
ir_context, SpvOpVariable, pointer_type_id, message_.variable_id(),
opt::Instruction::OperandList(
{{SPV_OPERAND_TYPE_STORAGE_CLASS, {SpvStorageClassPrivate}}})));
fuzzerutil::AddVariableIdToEntryPointInterfaces(ir_context,
message_.variable_id());
} else {
ir_context
->get_instr_block(

View File

@ -350,7 +350,7 @@ TEST(TransformationPushIdThroughVariableTest, Apply) {
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %4 "main" %92 %52 %53
OpEntryPoint Fragment %4 "main" %92 %52 %53 %109
OpExecutionMode %4 OriginUpperLeft
OpSource ESSL 310
OpDecorate %92 BuiltIn FragCoord