SPIRV-Tools/test/opt/redundancy_elimination_test.cpp
Alan Baker 867451f49e Add scalar replacement
Adds a scalar replacement pass. The pass considers all function scope
variables of composite type. If there are accesses to individual
elements (and it is legal) the pass replaces the variable with a
variable for each composite element and updates all the uses.

Added the pass to -O
Added NumUses and NumUsers to DefUseManager
Added some helper methods for the inst to block mapping in context
Added some helper methods for specific constant types

No longer generate duplicate pointer types.

* Now searches for an existing pointer of the appropriate type instead
of failing validation
* Fixed spec constant extracts
* Addressed changes for review
* Changed RunSinglePassAndMatch to be able to run validation
 * current users do not enable it

Added handling of acceptable decorations.

* Decorations are also transfered where appropriate

Refactored extension checking into FeatureManager

* Context now owns a feature manager
 * consciously NOT an analysis
 * added some test
* fixed some minor issues related to decorates
* added some decorate related tests for scalar replacement
2017-12-11 10:51:13 -05:00

277 lines
8.8 KiB
C++

// Copyright (c) 2017 Google Inc.
//
// 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 "opt/value_number_table.h"
#include "assembly_builder.h"
#include "gmock/gmock.h"
#include "opt/build_module.h"
#include "pass_fixture.h"
#include "pass_utils.h"
namespace {
using namespace spvtools;
using ::testing::HasSubstr;
using ::testing::MatchesRegex;
using RedundancyEliminationTest = PassTest<::testing::Test>;
#ifdef SPIRV_EFFCEE
// Test that it can get a simple case of local redundancy elimination.
// The rest of the test check for extra functionality.
TEST_F(RedundancyEliminationTest, RemoveRedundantLocalAdd) {
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %2 "main"
OpExecutionMode %2 OriginUpperLeft
OpSource GLSL 430
%3 = OpTypeVoid
%4 = OpTypeFunction %3
%5 = OpTypeFloat 32
%6 = OpTypePointer Function %5
%2 = OpFunction %3 None %4
%7 = OpLabel
%8 = OpVariable %6 Function
%9 = OpLoad %5 %8
%10 = OpFAdd %5 %9 %9
; CHECK: OpFAdd
; CHECK-NOT: OpFAdd
%11 = OpFAdd %5 %9 %9
OpReturn
OpFunctionEnd
)";
SinglePassRunAndMatch<opt::RedundancyEliminationPass>(text, false);
}
// Remove a redundant add across basic blocks.
TEST_F(RedundancyEliminationTest, RemoveRedundantAdd) {
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %2 "main"
OpExecutionMode %2 OriginUpperLeft
OpSource GLSL 430
%3 = OpTypeVoid
%4 = OpTypeFunction %3
%5 = OpTypeFloat 32
%6 = OpTypePointer Function %5
%2 = OpFunction %3 None %4
%7 = OpLabel
%8 = OpVariable %6 Function
%9 = OpLoad %5 %8
%10 = OpFAdd %5 %9 %9
OpBranch %11
%11 = OpLabel
; CHECK: OpFAdd
; CHECK-NOT: OpFAdd
%12 = OpFAdd %5 %9 %9
OpReturn
OpFunctionEnd
)";
SinglePassRunAndMatch<opt::RedundancyEliminationPass>(text, false);
}
// Remove a redundant add going through a multiple basic blocks.
TEST_F(RedundancyEliminationTest, RemoveRedundantAddDiamond) {
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %2 "main"
OpExecutionMode %2 OriginUpperLeft
OpSource GLSL 430
%3 = OpTypeVoid
%4 = OpTypeFunction %3
%5 = OpTypeFloat 32
%6 = OpTypePointer Function %5
%7 = OpTypeBool
%8 = OpConstantTrue %7
%2 = OpFunction %3 None %4
%9 = OpLabel
%10 = OpVariable %6 Function
%11 = OpLoad %5 %10
%12 = OpFAdd %5 %11 %11
; CHECK: OpFAdd
; CHECK-NOT: OpFAdd
OpBranchConditional %8 %13 %14
%13 = OpLabel
OpBranch %15
%14 = OpLabel
OpBranch %15
%15 = OpLabel
%16 = OpFAdd %5 %11 %11
OpReturn
OpFunctionEnd
)";
SinglePassRunAndMatch<opt::RedundancyEliminationPass>(text, false);
}
// Remove a redundant add in a side node.
TEST_F(RedundancyEliminationTest, RemoveRedundantAddInSideNode) {
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %2 "main"
OpExecutionMode %2 OriginUpperLeft
OpSource GLSL 430
%3 = OpTypeVoid
%4 = OpTypeFunction %3
%5 = OpTypeFloat 32
%6 = OpTypePointer Function %5
%7 = OpTypeBool
%8 = OpConstantTrue %7
%2 = OpFunction %3 None %4
%9 = OpLabel
%10 = OpVariable %6 Function
%11 = OpLoad %5 %10
%12 = OpFAdd %5 %11 %11
; CHECK: OpFAdd
; CHECK-NOT: OpFAdd
OpBranchConditional %8 %13 %14
%13 = OpLabel
OpBranch %15
%14 = OpLabel
%16 = OpFAdd %5 %11 %11
OpBranch %15
%15 = OpLabel
OpReturn
OpFunctionEnd
)";
SinglePassRunAndMatch<opt::RedundancyEliminationPass>(text, false);
}
// Remove a redundant add whose value is in the result of a phi node.
TEST_F(RedundancyEliminationTest, RemoveRedundantAddWithPhi) {
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %2 "main"
OpExecutionMode %2 OriginUpperLeft
OpSource GLSL 430
%3 = OpTypeVoid
%4 = OpTypeFunction %3
%5 = OpTypeFloat 32
%6 = OpTypePointer Function %5
%7 = OpTypeBool
%8 = OpConstantTrue %7
%2 = OpFunction %3 None %4
%9 = OpLabel
%10 = OpVariable %6 Function
%11 = OpLoad %5 %10
OpBranchConditional %8 %13 %14
%13 = OpLabel
%add1 = OpFAdd %5 %11 %11
; CHECK: OpFAdd
OpBranch %15
%14 = OpLabel
%add2 = OpFAdd %5 %11 %11
; CHECK: OpFAdd
OpBranch %15
%15 = OpLabel
; CHECK: OpPhi
%phi = OpPhi %5 %add1 %13 %add2 %14
; CHECK-NOT: OpFAdd
%16 = OpFAdd %5 %11 %11
OpReturn
OpFunctionEnd
)";
SinglePassRunAndMatch<opt::RedundancyEliminationPass>(text, false);
}
// Keep the add because it is redundant on some paths, but not all paths.
TEST_F(RedundancyEliminationTest, KeepPartiallyRedundantAdd) {
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %2 "main"
OpExecutionMode %2 OriginUpperLeft
OpSource GLSL 430
%3 = OpTypeVoid
%4 = OpTypeFunction %3
%5 = OpTypeFloat 32
%6 = OpTypePointer Function %5
%7 = OpTypeBool
%8 = OpConstantTrue %7
%2 = OpFunction %3 None %4
%9 = OpLabel
%10 = OpVariable %6 Function
%11 = OpLoad %5 %10
OpBranchConditional %8 %13 %14
%13 = OpLabel
%add = OpFAdd %5 %11 %11
OpBranch %15
%14 = OpLabel
OpBranch %15
%15 = OpLabel
%16 = OpFAdd %5 %11 %11
OpReturn
OpFunctionEnd
)";
auto result = SinglePassRunAndDisassemble<opt::RedundancyEliminationPass>(
text, /* skip_nop = */ true, /* do_validation = */ false);
EXPECT_EQ(opt::Pass::Status::SuccessWithoutChange, std::get<1>(result));
}
// Keep the add. Even if it is redundant on all paths, there is no single id
// whose definition dominates the add and contains the same value.
TEST_F(RedundancyEliminationTest, KeepRedundantAddWithoutPhi) {
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %2 "main"
OpExecutionMode %2 OriginUpperLeft
OpSource GLSL 430
%3 = OpTypeVoid
%4 = OpTypeFunction %3
%5 = OpTypeFloat 32
%6 = OpTypePointer Function %5
%7 = OpTypeBool
%8 = OpConstantTrue %7
%2 = OpFunction %3 None %4
%9 = OpLabel
%10 = OpVariable %6 Function
%11 = OpLoad %5 %10
OpBranchConditional %8 %13 %14
%13 = OpLabel
%add1 = OpFAdd %5 %11 %11
OpBranch %15
%14 = OpLabel
%add2 = OpFAdd %5 %11 %11
OpBranch %15
%15 = OpLabel
%16 = OpFAdd %5 %11 %11
OpReturn
OpFunctionEnd
)";
auto result = SinglePassRunAndDisassemble<opt::RedundancyEliminationPass>(
text, /* skip_nop = */ true, /* do_validation = */ false);
EXPECT_EQ(opt::Pass::Status::SuccessWithoutChange, std::get<1>(result));
}
#endif
} // anonymous namespace