| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- // Copyright (c) 2019 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #include "source/fuzz/transformation_add_constant_scalar.h"
- #include "gtest/gtest.h"
- #include "source/fuzz/fuzzer_util.h"
- #include "test/fuzz/fuzz_test_util.h"
- namespace spvtools {
- namespace fuzz {
- namespace {
- TEST(TransformationAddConstantScalarTest, IsApplicable) {
- std::string reference_shader = R"(
- OpCapability Shader
- OpCapability Int64
- OpCapability Float64
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %17 "main"
- ; Types
- ; 32-bit types
- %2 = OpTypeInt 32 0
- %3 = OpTypeInt 32 1
- %4 = OpTypeFloat 32
- ; 64-bit types
- %5 = OpTypeInt 64 0
- %6 = OpTypeInt 64 1
- %7 = OpTypeFloat 64
- %8 = OpTypePointer Private %2
- %9 = OpTypeVoid
- %10 = OpTypeFunction %9
- ; Constants
- ; 32-bit constants
- %11 = OpConstant %2 1
- %12 = OpConstant %3 2
- %13 = OpConstant %4 3
- ; 64-bit constants
- %14 = OpConstant %5 1
- %15 = OpConstant %6 2
- %16 = OpConstant %7 3
- ; main function
- %17 = OpFunction %9 None %10
- %18 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context =
- BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Tests |fresh_id| being non-fresh.
- auto transformation = TransformationAddConstantScalar(18, 2, {0}, false);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- // Tests undefined |type_id|.
- transformation = TransformationAddConstantScalar(19, 20, {0}, false);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- // Tests |type_id| not representing a type instruction.
- transformation = TransformationAddConstantScalar(19, 11, {0}, false);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- // Tests |type_id| representing an OpTypePointer instruction.
- transformation = TransformationAddConstantScalar(19, 8, {0}, false);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- // Tests |type_id| representing an OpTypeVoid instruction.
- transformation = TransformationAddConstantScalar(19, 9, {0}, false);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- // Tests |words| having no words.
- transformation = TransformationAddConstantScalar(19, 2, {}, false);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- // Tests |words| having 2 words for a 32-bit type.
- transformation = TransformationAddConstantScalar(19, 2, {0, 1}, false);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- // Tests |words| having 3 words for a 64-bit type.
- transformation = TransformationAddConstantScalar(19, 5, {0, 1, 2}, false);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- // Tests |words| having 2 words for a 32-bit float type.
- transformation = TransformationAddConstantScalar(19, 4, {0, 1}, false);
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- }
- TEST(TransformationAddConstantScalarTest, Apply) {
- std::string reference_shader = R"(
- OpCapability Shader
- OpCapability Int64
- OpCapability Float64
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %17 "main"
- ; Types
- ; 32-bit types
- %2 = OpTypeInt 32 0
- %3 = OpTypeInt 32 1
- %4 = OpTypeFloat 32
- ; 64-bit types
- %5 = OpTypeInt 64 0
- %6 = OpTypeInt 64 1
- %7 = OpTypeFloat 64
- %8 = OpTypePointer Private %2
- %9 = OpTypeVoid
- %10 = OpTypeFunction %9
- ; Constants
- ; 32-bit constants
- %11 = OpConstant %2 1
- %12 = OpConstant %3 2
- %13 = OpConstant %4 3
- ; 64-bit constants
- %14 = OpConstant %5 1
- %15 = OpConstant %6 2
- %16 = OpConstant %7 3
- ; main function
- %17 = OpFunction %9 None %10
- %18 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context =
- BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Adds 32-bit unsigned integer (1 logical operand with 1 word).
- auto transformation = TransformationAddConstantScalar(19, 2, {4}, false);
- ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(19));
- ASSERT_EQ(nullptr, context->get_constant_mgr()->FindDeclaredConstant(19));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_EQ(spv::Op::OpConstant,
- context->get_def_use_mgr()->GetDef(19)->opcode());
- ASSERT_EQ(4, context->get_constant_mgr()->FindDeclaredConstant(19)->GetU32());
- auto* constant_instruction = context->get_def_use_mgr()->GetDef(19);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds 32-bit signed integer (1 logical operand with 1 word).
- transformation = TransformationAddConstantScalar(20, 3, {5}, false);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(20);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds 32-bit float (1 logical operand with 1 word).
- transformation = TransformationAddConstantScalar(
- 21, 4, {0b01000000110000000000000000000000}, false);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(21);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds 64-bit unsigned integer (1 logical operand with 2 words).
- transformation = TransformationAddConstantScalar(22, 5, {7, 0}, false);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(22);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds 64-bit signed integer (1 logical operand with 2 words).
- transformation = TransformationAddConstantScalar(23, 6, {8, 0}, false);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(23);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds 64-bit float (1 logical operand with 2 words).
- transformation = TransformationAddConstantScalar(
- 24, 7, {0, 0b01000000001000100000000000000000}, false);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(24);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds irrelevant 32-bit unsigned integer (1 logical operand with 1 word).
- transformation = TransformationAddConstantScalar(25, 2, {10}, true);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(25);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds irrelevant 32-bit signed integer (1 logical operand with 1 word).
- transformation = TransformationAddConstantScalar(26, 3, {11}, true);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(26);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds irrelevant 32-bit float (1 logical operand with 1 word).
- transformation = TransformationAddConstantScalar(
- 27, 4, {0b01000001010000000000000000000000}, true);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(27);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds irrelevant 64-bit unsigned integer (1 logical operand with 2 words).
- transformation = TransformationAddConstantScalar(28, 5, {13, 0}, true);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(28);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds irrelevant 64-bit signed integer (1 logical operand with 2 words).
- transformation = TransformationAddConstantScalar(29, 6, {14, 0}, true);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(29);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- // Adds irrelevant 64-bit float (1 logical operand with 2 words).
- transformation = TransformationAddConstantScalar(
- 30, 7, {0, 0b01000000001011100000000000000000}, true);
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- constant_instruction = context->get_def_use_mgr()->GetDef(30);
- EXPECT_EQ(constant_instruction->NumInOperands(), 1);
- EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- for (uint32_t result_id = 19; result_id <= 24; ++result_id) {
- ASSERT_FALSE(
- transformation_context.GetFactManager()->IdIsIrrelevant(result_id));
- }
- for (uint32_t result_id = 25; result_id <= 30; ++result_id) {
- ASSERT_TRUE(
- transformation_context.GetFactManager()->IdIsIrrelevant(result_id));
- }
- std::string variant_shader = R"(
- OpCapability Shader
- OpCapability Int64
- OpCapability Float64
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %17 "main"
- ; Types
- ; 32-bit types
- %2 = OpTypeInt 32 0
- %3 = OpTypeInt 32 1
- %4 = OpTypeFloat 32
- ; 64-bit types
- %5 = OpTypeInt 64 0
- %6 = OpTypeInt 64 1
- %7 = OpTypeFloat 64
- %8 = OpTypePointer Private %2
- %9 = OpTypeVoid
- %10 = OpTypeFunction %9
- ; Constants
- ; 32-bit constants
- %11 = OpConstant %2 1
- %12 = OpConstant %3 2
- %13 = OpConstant %4 3
- ; 64-bit constants
- %14 = OpConstant %5 1
- %15 = OpConstant %6 2
- %16 = OpConstant %7 3
- ; added constants
- %19 = OpConstant %2 4
- %20 = OpConstant %3 5
- %21 = OpConstant %4 6
- %22 = OpConstant %5 7
- %23 = OpConstant %6 8
- %24 = OpConstant %7 9
- %25 = OpConstant %2 10
- %26 = OpConstant %3 11
- %27 = OpConstant %4 12
- %28 = OpConstant %5 13
- %29 = OpConstant %6 14
- %30 = OpConstant %7 15
- ; main function
- %17 = OpFunction %9 None %10
- %18 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, variant_shader, context.get()));
- }
- } // namespace
- } // namespace fuzz
- } // namespace spvtools
|