| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- // Copyright (c) 2023 Nintendo
- //
- // 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 <string>
- #include <iostream>
- #include "gtest/gtest.h"
- #include "spirv-tools/libspirv.h"
- namespace spvtools {
- namespace {
- TEST(OptimizerCInterface, DefaultConsumerWithValidationNoPassesForInvalidInput) {
- const uint32_t spirv[] = {
- 0xDEADFEED, // Invalid Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x01000000, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001 // GLSL450
- };
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- // Do not register any passes
- auto options = spvOptimizerOptionsCreate();
- ASSERT_NE(options, nullptr);
- spvOptimizerOptionsSetRunValidator(options, true);
- spv_binary binary = nullptr;
- EXPECT_NE(SPV_SUCCESS,
- spvOptimizerRun(optimizer, spirv, sizeof(spirv) / sizeof(uint32_t),
- &binary, options));
- ASSERT_EQ(binary, nullptr);
- spvOptimizerOptionsDestroy(options);
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, SpecifyConsumerWithValidationNoPassesForInvalidInput) {
- const uint32_t spirv[] = {
- 0xDEADFEED, // Invalid Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x01000000, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001 // GLSL450
- };
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- spvOptimizerSetMessageConsumer(
- optimizer,
- [](spv_message_level_t, const char*, const spv_position_t*,
- const char* message) {
- std::cout << message << std::endl;
- });
- // Do not register any passes
- auto options = spvOptimizerOptionsCreate();
- ASSERT_NE(options, nullptr);
- spvOptimizerOptionsSetRunValidator(options, true);
- testing::internal::CaptureStdout();
- spv_binary binary = nullptr;
- EXPECT_NE(SPV_SUCCESS,
- spvOptimizerRun(optimizer, spirv, sizeof(spirv) / sizeof(uint32_t),
- &binary, options));
- ASSERT_EQ(binary, nullptr);
- auto output = testing::internal::GetCapturedStdout();
- EXPECT_STRNE(output.c_str(), "");
- spvOptimizerOptionsDestroy(options);
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, DefaultConsumerWithValidationNoPassesForValidInput) {
- const uint32_t spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000001, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001 // GLSL450
- };
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- // Do not register any passes
- auto options = spvOptimizerOptionsCreate();
- ASSERT_NE(options, nullptr);
- spvOptimizerOptionsSetRunValidator(options, true);
- spv_binary binary = nullptr;
- EXPECT_EQ(SPV_SUCCESS,
- spvOptimizerRun(optimizer, spirv, sizeof(spirv) / sizeof(uint32_t),
- &binary, options));
- ASSERT_NE(binary, nullptr);
- spvOptimizerOptionsDestroy(options);
- // Should remain unchanged
- EXPECT_EQ(binary->wordCount, sizeof(spirv) / sizeof(uint32_t));
- EXPECT_EQ(memcmp(binary->code, spirv, sizeof(spirv) / sizeof(uint32_t)), 0);
- spvBinaryDestroy(binary);
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, DefaultConsumerNoPassesForValidInput) {
- const uint32_t spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000003, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001, // GLSL450
- 0x00040015, // OpTypeInt
- 0x00000001, // %1
- 0x00000020, // 32 Bits
- 0x00000000, // Unsigned
- 0x0004002B, // OpConstant
- 0x00000001, // %1
- 0x00000002, // %2
- 0x00000001 // 1
- };
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- // Do not register any passes
- auto options = spvOptimizerOptionsCreate();
- ASSERT_NE(options, nullptr);
- spvOptimizerOptionsSetRunValidator(options, true);
- spv_binary binary = nullptr;
- EXPECT_EQ(SPV_SUCCESS,
- spvOptimizerRun(optimizer, spirv, sizeof(spirv) / sizeof(uint32_t),
- &binary, options));
- ASSERT_NE(binary, nullptr);
- spvOptimizerOptionsDestroy(options);
- // Should remain unchanged
- EXPECT_EQ(binary->wordCount, sizeof(spirv) / sizeof(uint32_t));
- EXPECT_EQ(memcmp(binary->code, spirv, sizeof(spirv) / sizeof(uint32_t)), 0);
- spvBinaryDestroy(binary);
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, DefaultConsumerLegalizationPassesForValidInput) {
- const uint32_t spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000003, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001, // GLSL450
- 0x00040015, // OpTypeInt
- 0x00000001, // %1
- 0x00000020, // 32 Bits
- 0x00000000, // Unsigned
- 0x0004002B, // OpConstant
- 0x00000001, // %1
- 0x00000002, // %2
- 0x00000001 // 1
- };
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- spvOptimizerRegisterLegalizationPasses(optimizer);
- auto options = spvOptimizerOptionsCreate();
- ASSERT_NE(options, nullptr);
- spvOptimizerOptionsSetRunValidator(options, false);
- spv_binary binary = nullptr;
- EXPECT_EQ(SPV_SUCCESS,
- spvOptimizerRun(optimizer, spirv, sizeof(spirv) / sizeof(uint32_t),
- &binary, options));
- ASSERT_NE(binary, nullptr);
- spvOptimizerOptionsDestroy(options);
- // Only check that SPV_SUCCESS is returned, do not verify output
- spvBinaryDestroy(binary);
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, DefaultConsumerPerformancePassesForValidInput) {
- const uint32_t spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000003, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001, // GLSL450
- 0x00040015, // OpTypeInt
- 0x00000001, // %1
- 0x00000020, // 32 Bits
- 0x00000000, // Unsigned
- 0x0004002B, // OpConstant
- 0x00000001, // %1
- 0x00000002, // %2
- 0x00000001 // 1
- };
- const uint32_t expected_spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000001, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001 // GLSL450
- };
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- spvOptimizerRegisterPerformancePasses(optimizer);
- auto options = spvOptimizerOptionsCreate();
- ASSERT_NE(options, nullptr);
- spvOptimizerOptionsSetRunValidator(options, false);
- spv_binary binary = nullptr;
- EXPECT_EQ(SPV_SUCCESS,
- spvOptimizerRun(optimizer, spirv, sizeof(spirv) / sizeof(uint32_t),
- &binary, options));
- ASSERT_NE(binary, nullptr);
- spvOptimizerOptionsDestroy(options);
- // Unreferenced OpTypeInt and OpConstant should be removed
- EXPECT_EQ(binary->wordCount, sizeof(expected_spirv) / sizeof(uint32_t));
- EXPECT_EQ(memcmp(binary->code, expected_spirv,
- sizeof(expected_spirv) / sizeof(uint32_t)), 0);
- spvBinaryDestroy(binary);
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, DefaultConsumerSizePassesForValidInput) {
- const uint32_t spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000003, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001, // GLSL450
- 0x00040015, // OpTypeInt
- 0x00000001, // %1
- 0x00000020, // 32 Bits
- 0x00000000, // Unsigned
- 0x0004002B, // OpConstant
- 0x00000001, // %1
- 0x00000002, // %2
- 0x00000001 // 1
- };
- const uint32_t expected_spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000001, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001 // GLSL450
- };
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- spvOptimizerRegisterSizePasses(optimizer);
- auto options = spvOptimizerOptionsCreate();
- ASSERT_NE(options, nullptr);
- spvOptimizerOptionsSetRunValidator(options, false);
- spv_binary binary = nullptr;
- EXPECT_EQ(SPV_SUCCESS,
- spvOptimizerRun(optimizer, spirv, sizeof(spirv) / sizeof(uint32_t),
- &binary, options));
- ASSERT_NE(binary, nullptr);
- spvOptimizerOptionsDestroy(options);
- // Unreferenced OpTypeInt and OpConstant should be removed
- EXPECT_EQ(binary->wordCount, sizeof(expected_spirv) / sizeof(uint32_t));
- EXPECT_EQ(memcmp(binary->code, expected_spirv,
- sizeof(expected_spirv) / sizeof(uint32_t)), 0);
- spvBinaryDestroy(binary);
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, DefaultConsumerPassFromFlagForValidInput) {
- const uint32_t spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000003, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001, // GLSL450
- 0x00040015, // OpTypeInt
- 0x00000001, // %1
- 0x00000020, // 32 Bits
- 0x00000000, // Unsigned
- 0x0004002B, // OpConstant
- 0x00000001, // %1
- 0x00000002, // %2
- 0x00000001 // 1
- };
- const uint32_t expected_spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000001, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001 // GLSL450
- };
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- EXPECT_TRUE(spvOptimizerRegisterPassFromFlag(
- optimizer, "--eliminate-dead-code-aggressive"));
- auto options = spvOptimizerOptionsCreate();
- ASSERT_NE(options, nullptr);
- spvOptimizerOptionsSetRunValidator(options, false);
- spv_binary binary = nullptr;
- EXPECT_EQ(SPV_SUCCESS,
- spvOptimizerRun(optimizer, spirv, sizeof(spirv) / sizeof(uint32_t),
- &binary, options));
- ASSERT_NE(binary, nullptr);
- spvOptimizerOptionsDestroy(options);
- // Unreferenced OpTypeInt and OpConstant should be removed
- EXPECT_EQ(binary->wordCount, sizeof(expected_spirv) / sizeof(uint32_t));
- EXPECT_EQ(memcmp(binary->code, expected_spirv,
- sizeof(expected_spirv) / sizeof(uint32_t)), 0);
- spvBinaryDestroy(binary);
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, DefaultConsumerPassesFromFlagsForValidInput) {
- const uint32_t spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000003, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001, // GLSL450
- 0x00040015, // OpTypeInt
- 0x00000001, // %1
- 0x00000020, // 32 Bits
- 0x00000000, // Unsigned
- 0x0004002B, // OpConstant
- 0x00000001, // %1
- 0x00000002, // %2
- 0x00000001 // 1
- };
- const uint32_t expected_spirv[] = {
- 0x07230203, // Magic
- 0x00010100, // Version 1.1
- 0x00000000, // No Generator
- 0x00000001, // Bound
- 0x00000000, // Schema
- 0x00020011, // OpCapability
- 0x00000001, // Shader
- 0x00020011, // OpCapability
- 0x00000005, // Linkage
- 0x0003000E, // OpMemoryModel
- 0x00000000, // Logical
- 0x00000001 // GLSL450
- };
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- const char* flags[2] = {
- "--eliminate-dead-const",
- "--eliminate-dead-code-aggressive"
- };
- EXPECT_TRUE(spvOptimizerRegisterPassesFromFlags(
- optimizer, flags, sizeof(flags) / sizeof(const char*)));
- auto options = spvOptimizerOptionsCreate();
- ASSERT_NE(options, nullptr);
- spvOptimizerOptionsSetRunValidator(options, false);
- spv_binary binary = nullptr;
- EXPECT_EQ(SPV_SUCCESS,
- spvOptimizerRun(optimizer, spirv, sizeof(spirv) / sizeof(uint32_t),
- &binary, options));
- ASSERT_NE(binary, nullptr);
- spvOptimizerOptionsDestroy(options);
- // Unreferenced OpTypeInt and OpConstant should be removed
- EXPECT_EQ(binary->wordCount, sizeof(expected_spirv) / sizeof(uint32_t));
- EXPECT_EQ(memcmp(binary->code, expected_spirv,
- sizeof(expected_spirv) / sizeof(uint32_t)), 0);
- spvBinaryDestroy(binary);
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, DefaultConsumerInvalidPassFromFlag) {
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- EXPECT_FALSE(spvOptimizerRegisterPassFromFlag(
- optimizer, "--this-is-not-a-valid-pass"));
- spvOptimizerDestroy(optimizer);
- }
- TEST(OptimizerCInterface, DefaultConsumerInvalidPassesFromFlags) {
- auto optimizer = spvOptimizerCreate(SPV_ENV_UNIVERSAL_1_1);
- ASSERT_NE(optimizer, nullptr);
- const char* flags[2] = {
- "--eliminate-dead-const",
- "--this-is-not-a-valid-pass"
- };
- EXPECT_FALSE(spvOptimizerRegisterPassesFromFlags(
- optimizer, flags, sizeof(flags) / sizeof(const char*)));
- spvOptimizerDestroy(optimizer);
- }
- } // namespace
- } // namespace spvtools
|