mirror of
https://github.com/KhronosGroup/SPIRV-Tools
synced 2024-12-29 19:21:09 +00:00
867451f49e
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
159 lines
5.2 KiB
C++
159 lines
5.2 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 LocalRedundancyEliminationTest = PassTest<::testing::Test>;
|
|
|
|
#ifdef SPIRV_EFFCEE
|
|
// Remove an instruction when it was already computed.
|
|
TEST_F(LocalRedundancyEliminationTest, 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
|
|
; CHECK: OpFAdd
|
|
; CHECK-NOT: OpFAdd
|
|
%11 = OpFAdd %5 %9 %9
|
|
OpReturn
|
|
OpFunctionEnd
|
|
)";
|
|
SinglePassRunAndMatch<opt::LocalRedundancyEliminationPass>(text, false);
|
|
}
|
|
|
|
// Make sure we keep instruction that are different, but look similar.
|
|
TEST_F(LocalRedundancyEliminationTest, KeepDifferentAdd) {
|
|
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
|
|
OpStore %8 %10
|
|
%11 = OpLoad %5 %8
|
|
; CHECK: %11 = OpLoad
|
|
%12 = OpFAdd %5 %11 %11
|
|
; CHECK: OpFAdd [[:%\w+]] %11 %11
|
|
OpReturn
|
|
OpFunctionEnd
|
|
)";
|
|
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
|
|
SinglePassRunAndMatch<opt::LocalRedundancyEliminationPass>(text, false);
|
|
}
|
|
|
|
// This test is check that the values are being propagated properly, and that
|
|
// we are able to identify sequences of instruction that are not needed.
|
|
TEST_F(LocalRedundancyEliminationTest, RemoveMultipleInstructions) {
|
|
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 Uniform %5
|
|
%8 = OpVariable %6 Uniform
|
|
%2 = OpFunction %3 None %4
|
|
%7 = OpLabel
|
|
; CHECK: [[r1:%\w+]] = OpLoad
|
|
%9 = OpLoad %5 %8
|
|
; CHECK-NEXT: [[r2:%\w+]] = OpFAdd [[:%\w+]] [[r1]] [[r1]]
|
|
%10 = OpFAdd %5 %9 %9
|
|
; CHECK-NEXT: [[r3:%\w+]] = OpFMul [[:%\w+]] [[r2]] [[r1]]
|
|
%11 = OpFMul %5 %10 %9
|
|
; CHECK-NOT: OpLoad
|
|
%12 = OpLoad %5 %8
|
|
; CHECK-NOT: OpFAdd [[:\w+]] %12 %12
|
|
%13 = OpFAdd %5 %12 %12
|
|
; CHECK-NOT: OpFMul
|
|
%14 = OpFMul %5 %13 %12
|
|
; CHECK-NEXT: [[:%\w+]] = OpFAdd [[:%\w+]] [[r3]] [[r3]]
|
|
%15 = OpFAdd %5 %14 %11
|
|
OpReturn
|
|
OpFunctionEnd
|
|
)";
|
|
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
|
|
SinglePassRunAndMatch<opt::LocalRedundancyEliminationPass>(text, false);
|
|
}
|
|
|
|
// Redundant instructions in different blocks should be kept.
|
|
TEST_F(LocalRedundancyEliminationTest, KeepInstructionsInDifferentBlocks) {
|
|
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
|
|
%bb1 = OpLabel
|
|
%8 = OpVariable %6 Function
|
|
%9 = OpLoad %5 %8
|
|
%10 = OpFAdd %5 %9 %9
|
|
; CHECK: OpFAdd
|
|
OpBranch %bb2
|
|
%bb2 = OpLabel
|
|
; CHECK: OpFAdd
|
|
%11 = OpFAdd %5 %9 %9
|
|
OpReturn
|
|
OpFunctionEnd
|
|
)";
|
|
SinglePassRunAndMatch<opt::LocalRedundancyEliminationPass>(text, false);
|
|
}
|
|
#endif
|
|
} // anonymous namespace
|