2016-07-26 16:11:03 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2016-07-26 16:11:03 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-07-26 16:11:03 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-07-26 16:11:03 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
2016-07-26 16:11:03 +00:00
|
|
|
#include <tuple>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <utility>
|
2016-07-26 16:11:03 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "test/opt/pass_fixture.h"
|
|
|
|
#include "test/opt/pass_utils.h"
|
|
|
|
|
2018-07-11 13:24:49 +00:00
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
2016-07-26 16:11:03 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct FreezeSpecConstantValueTypeTestCase {
|
|
|
|
const char* type_decl;
|
|
|
|
const char* spec_const;
|
|
|
|
const char* expected_frozen_const;
|
|
|
|
};
|
|
|
|
|
|
|
|
using FreezeSpecConstantValueTypeTest =
|
|
|
|
PassTest<::testing::TestWithParam<FreezeSpecConstantValueTypeTestCase>>;
|
|
|
|
|
|
|
|
TEST_P(FreezeSpecConstantValueTypeTest, PrimaryType) {
|
|
|
|
auto& test_case = GetParam();
|
|
|
|
std::vector<const char*> text = {"OpCapability Shader",
|
|
|
|
"OpMemoryModel Logical GLSL450",
|
|
|
|
test_case.type_decl, test_case.spec_const};
|
|
|
|
std::vector<const char*> expected = {
|
|
|
|
"OpCapability Shader", "OpMemoryModel Logical GLSL450",
|
|
|
|
test_case.type_decl, test_case.expected_frozen_const};
|
2018-07-11 13:24:49 +00:00
|
|
|
SinglePassRunAndCheck<FreezeSpecConstantValuePass>(
|
2016-08-31 16:44:49 +00:00
|
|
|
JoinAllInsts(text), JoinAllInsts(expected), /* skip_nop = */ false);
|
2016-07-26 16:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test each primary type.
|
2019-01-29 23:56:52 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2016-07-26 16:11:03 +00:00
|
|
|
PrimaryTypeSpecConst, FreezeSpecConstantValueTypeTest,
|
|
|
|
::testing::ValuesIn(std::vector<FreezeSpecConstantValueTypeTestCase>({
|
|
|
|
// Type declaration, original spec constant definition, expected frozen
|
|
|
|
// spec constants.
|
|
|
|
{"%int = OpTypeInt 32 1", "%2 = OpSpecConstant %int 1",
|
2016-10-13 20:22:04 +00:00
|
|
|
"%int_1 = OpConstant %int 1"},
|
2016-07-26 16:11:03 +00:00
|
|
|
{"%uint = OpTypeInt 32 0", "%2 = OpSpecConstant %uint 1",
|
2016-10-13 20:22:04 +00:00
|
|
|
"%uint_1 = OpConstant %uint 1"},
|
2018-03-30 23:35:45 +00:00
|
|
|
{"%float = OpTypeFloat 32", "%2 = OpSpecConstant %float 3.1415",
|
|
|
|
"%float_3_1415 = OpConstant %float 3.1415"},
|
|
|
|
{"%double = OpTypeFloat 64", "%2 = OpSpecConstant %double 3.141592653",
|
|
|
|
"%double_3_141592653 = OpConstant %double 3.141592653"},
|
2016-07-26 16:11:03 +00:00
|
|
|
{"%bool = OpTypeBool", "%2 = OpSpecConstantTrue %bool",
|
2017-01-11 21:12:50 +00:00
|
|
|
"%true = OpConstantTrue %bool"},
|
2016-07-26 16:11:03 +00:00
|
|
|
{"%bool = OpTypeBool", "%2 = OpSpecConstantFalse %bool",
|
2017-01-11 21:12:50 +00:00
|
|
|
"%false = OpConstantFalse %bool"},
|
2016-07-26 16:11:03 +00:00
|
|
|
})));
|
|
|
|
|
|
|
|
using FreezeSpecConstantValueRemoveDecorationTest = PassTest<::testing::Test>;
|
|
|
|
|
|
|
|
TEST_F(FreezeSpecConstantValueRemoveDecorationTest,
|
|
|
|
RemoveDecorationInstWithSpecId) {
|
|
|
|
std::vector<const char*> text = {
|
|
|
|
// clang-format off
|
|
|
|
"OpCapability Shader",
|
|
|
|
"OpCapability Float64",
|
|
|
|
"%1 = OpExtInstImport \"GLSL.std.450\"",
|
|
|
|
"OpMemoryModel Logical GLSL450",
|
|
|
|
"OpEntryPoint Vertex %main \"main\"",
|
|
|
|
"OpSource GLSL 450",
|
|
|
|
"OpSourceExtension \"GL_GOOGLE_cpp_style_line_directive\"",
|
|
|
|
"OpSourceExtension \"GL_GOOGLE_include_directive\"",
|
|
|
|
"OpName %main \"main\"",
|
|
|
|
"OpDecorate %3 SpecId 200",
|
|
|
|
"OpDecorate %4 SpecId 201",
|
|
|
|
"OpDecorate %5 SpecId 202",
|
|
|
|
"OpDecorate %6 SpecId 203",
|
|
|
|
"%void = OpTypeVoid",
|
|
|
|
"%8 = OpTypeFunction %void",
|
|
|
|
"%int = OpTypeInt 32 1",
|
|
|
|
"%3 = OpSpecConstant %int 3",
|
|
|
|
"%float = OpTypeFloat 32",
|
2018-03-30 23:35:45 +00:00
|
|
|
"%4 = OpSpecConstant %float 3.1415",
|
2016-07-26 16:11:03 +00:00
|
|
|
"%double = OpTypeFloat 64",
|
|
|
|
"%5 = OpSpecConstant %double 3.14159265358979",
|
|
|
|
"%bool = OpTypeBool",
|
|
|
|
"%6 = OpSpecConstantTrue %bool",
|
|
|
|
"%13 = OpSpecConstantFalse %bool",
|
|
|
|
"%main = OpFunction %void None %8",
|
|
|
|
"%14 = OpLabel",
|
|
|
|
"OpReturn",
|
|
|
|
"OpFunctionEnd",
|
|
|
|
// clang-format on
|
|
|
|
};
|
|
|
|
std::string expected_disassembly = SelectiveJoin(text, [](const char* line) {
|
|
|
|
return std::string(line).find("SpecId") != std::string::npos;
|
|
|
|
});
|
2016-10-13 20:22:04 +00:00
|
|
|
std::vector<std::pair<const char*, const char*>> replacement_pairs = {
|
|
|
|
{"%3 = OpSpecConstant %int 3", "%int_3 = OpConstant %int 3"},
|
2018-03-30 23:35:45 +00:00
|
|
|
{"%4 = OpSpecConstant %float 3.1415",
|
|
|
|
"%float_3_1415 = OpConstant %float 3.1415"},
|
2016-10-13 20:22:04 +00:00
|
|
|
{"%5 = OpSpecConstant %double 3.14159265358979",
|
|
|
|
"%double_3_14159265358979 = OpConstant %double 3.14159265358979"},
|
2017-01-11 21:12:50 +00:00
|
|
|
{"%6 = OpSpecConstantTrue ", "%true = OpConstantTrue "},
|
|
|
|
{"%13 = OpSpecConstantFalse ", "%false = OpConstantFalse "},
|
2016-07-26 16:11:03 +00:00
|
|
|
};
|
2016-10-13 20:22:04 +00:00
|
|
|
for (auto& p : replacement_pairs) {
|
2016-07-26 16:11:03 +00:00
|
|
|
EXPECT_TRUE(FindAndReplace(&expected_disassembly, p.first, p.second))
|
|
|
|
<< "text:\n"
|
|
|
|
<< expected_disassembly << "\n"
|
|
|
|
<< "find_str:\n"
|
|
|
|
<< p.first << "\n"
|
|
|
|
<< "replace_str:\n"
|
|
|
|
<< p.second << "\n";
|
|
|
|
}
|
2018-07-11 13:24:49 +00:00
|
|
|
SinglePassRunAndCheck<FreezeSpecConstantValuePass>(JoinAllInsts(text),
|
|
|
|
expected_disassembly,
|
|
|
|
/* skip_nop = */ true);
|
2016-07-26 16:11:03 +00:00
|
|
|
}
|
2018-07-11 13:24:49 +00:00
|
|
|
|
2022-02-14 14:33:29 +00:00
|
|
|
TEST_F(FreezeSpecConstantValueRemoveDecorationTest,
|
|
|
|
RemoveDecorationForLocalSizeIdWithSpecId) {
|
|
|
|
std::vector<const char*> text = {
|
|
|
|
// clang-format off
|
|
|
|
"OpCapability Shader",
|
|
|
|
"%1 = OpExtInstImport \"GLSL.std.450\"",
|
|
|
|
"OpMemoryModel Logical GLSL450",
|
|
|
|
"OpEntryPoint GLCompute %2 \"main\"",
|
|
|
|
"OpExecutionModeId %2 LocalSizeId %uint_32 %uint_1 %uint_1_0",
|
|
|
|
"OpSource GLSL 450",
|
|
|
|
"OpDecorate %3 SpecId 18",
|
|
|
|
"OpDecorate %5 SpecId 19",
|
|
|
|
"%void = OpTypeVoid",
|
|
|
|
"%9 = OpTypeFunction %void",
|
|
|
|
"%uint = OpTypeInt 32 0",
|
|
|
|
"%uint_32 = OpSpecConstant %uint 32",
|
|
|
|
"%uint_1 = OpConstant %uint 1",
|
|
|
|
"%uint_1_0 = OpSpecConstant %uint 1",
|
|
|
|
"%2 = OpFunction %void None %9",
|
|
|
|
"%11 = OpLabel",
|
|
|
|
"OpReturn",
|
|
|
|
"OpFunctionEnd",
|
|
|
|
// clang-format on
|
|
|
|
};
|
|
|
|
std::string expected_disassembly = SelectiveJoin(text, [](const char* line) {
|
|
|
|
return std::string(line).find("SpecId") != std::string::npos;
|
|
|
|
});
|
|
|
|
std::vector<std::pair<const char*, const char*>> replacement_pairs = {
|
|
|
|
{"%uint_32 = OpSpecConstant %uint 32", "%uint_32 = OpConstant %uint 32"},
|
|
|
|
{"%uint_1_0 = OpSpecConstant %uint 1", "%uint_1_0 = OpConstant %uint 1"},
|
|
|
|
};
|
|
|
|
for (auto& p : replacement_pairs) {
|
|
|
|
EXPECT_TRUE(FindAndReplace(&expected_disassembly, p.first, p.second))
|
|
|
|
<< "text:\n"
|
|
|
|
<< expected_disassembly << "\n"
|
|
|
|
<< "find_str:\n"
|
|
|
|
<< p.first << "\n"
|
|
|
|
<< "replace_str:\n"
|
|
|
|
<< p.second << "\n";
|
|
|
|
}
|
|
|
|
SinglePassRunAndCheck<FreezeSpecConstantValuePass>(JoinAllInsts(text),
|
|
|
|
expected_disassembly,
|
|
|
|
/* skip_nop = */ true);
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:24:49 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|