2017-11-02 18:25:48 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-11-17 21:33:32 +00:00
|
|
|
#include <gmock/gmock.h>
|
2017-11-02 18:25:48 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "opt/ir_context.h"
|
|
|
|
#include "opt/pass.h"
|
|
|
|
#include "pass_fixture.h"
|
|
|
|
#include "pass_utils.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace spvtools;
|
|
|
|
using ir::IRContext;
|
|
|
|
using Analysis = IRContext::Analysis;
|
2017-11-13 20:31:43 +00:00
|
|
|
using ::testing::Each;
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
class DummyPassPreservesNothing : public opt::Pass {
|
|
|
|
public:
|
|
|
|
DummyPassPreservesNothing(Status s) : opt::Pass(), status_to_return_(s) {}
|
|
|
|
const char* name() const override { return "dummy-pass"; }
|
|
|
|
Status Process(IRContext*) override { return status_to_return_; }
|
|
|
|
Status status_to_return_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DummyPassPreservesAll : public opt::Pass {
|
|
|
|
public:
|
|
|
|
DummyPassPreservesAll(Status s) : opt::Pass(), status_to_return_(s) {}
|
|
|
|
const char* name() const override { return "dummy-pass"; }
|
|
|
|
Status Process(IRContext*) override { return status_to_return_; }
|
|
|
|
Status status_to_return_;
|
|
|
|
virtual Analysis GetPreservedAnalyses() override {
|
|
|
|
return Analysis(IRContext::kAnalysisEnd - 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DummyPassPreservesFirst : public opt::Pass {
|
|
|
|
public:
|
|
|
|
DummyPassPreservesFirst(Status s) : opt::Pass(), status_to_return_(s) {}
|
|
|
|
const char* name() const override { return "dummy-pass"; }
|
|
|
|
Status Process(IRContext*) override { return status_to_return_; }
|
|
|
|
Status status_to_return_;
|
|
|
|
virtual Analysis GetPreservedAnalyses() override {
|
|
|
|
return IRContext::kAnalysisBegin;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using IRContextTest = PassTest<::testing::Test>;
|
|
|
|
|
|
|
|
TEST_F(IRContextTest, IndividualValidAfterBuild) {
|
|
|
|
std::unique_ptr<ir::Module> module(new ir::Module());
|
2017-12-19 19:18:13 +00:00
|
|
|
IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
|
|
|
|
spvtools::MessageConsumer());
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
|
|
|
|
i <<= 1) {
|
2017-11-14 19:11:50 +00:00
|
|
|
localContext.BuildInvalidAnalyses(i);
|
|
|
|
EXPECT_TRUE(localContext.AreAnalysesValid(i));
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(IRContextTest, AllValidAfterBuild) {
|
|
|
|
std::unique_ptr<ir::Module> module = MakeUnique<ir::Module>();
|
2017-12-19 19:18:13 +00:00
|
|
|
IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
|
|
|
|
spvtools::MessageConsumer());
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
Analysis built_analyses = IRContext::kAnalysisNone;
|
|
|
|
for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
|
|
|
|
i <<= 1) {
|
2017-11-14 19:11:50 +00:00
|
|
|
localContext.BuildInvalidAnalyses(i);
|
2017-11-02 18:25:48 +00:00
|
|
|
built_analyses |= i;
|
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
EXPECT_TRUE(localContext.AreAnalysesValid(built_analyses));
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(IRContextTest, AllValidAfterPassNoChange) {
|
|
|
|
std::unique_ptr<ir::Module> module = MakeUnique<ir::Module>();
|
2017-12-19 19:18:13 +00:00
|
|
|
IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
|
|
|
|
spvtools::MessageConsumer());
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
Analysis built_analyses = IRContext::kAnalysisNone;
|
|
|
|
for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
|
|
|
|
i <<= 1) {
|
2017-11-14 19:11:50 +00:00
|
|
|
localContext.BuildInvalidAnalyses(i);
|
2017-11-02 18:25:48 +00:00
|
|
|
built_analyses |= i;
|
|
|
|
}
|
|
|
|
|
|
|
|
DummyPassPreservesNothing pass(opt::Pass::Status::SuccessWithoutChange);
|
2017-11-14 19:11:50 +00:00
|
|
|
opt::Pass::Status s = pass.Run(&localContext);
|
2017-11-02 18:25:48 +00:00
|
|
|
EXPECT_EQ(s, opt::Pass::Status::SuccessWithoutChange);
|
2017-11-14 19:11:50 +00:00
|
|
|
EXPECT_TRUE(localContext.AreAnalysesValid(built_analyses));
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(IRContextTest, NoneValidAfterPassWithChange) {
|
|
|
|
std::unique_ptr<ir::Module> module = MakeUnique<ir::Module>();
|
2017-12-19 19:18:13 +00:00
|
|
|
IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
|
|
|
|
spvtools::MessageConsumer());
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
|
|
|
|
i <<= 1) {
|
2017-11-14 19:11:50 +00:00
|
|
|
localContext.BuildInvalidAnalyses(i);
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DummyPassPreservesNothing pass(opt::Pass::Status::SuccessWithChange);
|
2017-11-14 19:11:50 +00:00
|
|
|
opt::Pass::Status s = pass.Run(&localContext);
|
2017-11-02 18:25:48 +00:00
|
|
|
EXPECT_EQ(s, opt::Pass::Status::SuccessWithChange);
|
|
|
|
for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
|
|
|
|
i <<= 1) {
|
2017-11-14 19:11:50 +00:00
|
|
|
EXPECT_FALSE(localContext.AreAnalysesValid(i));
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(IRContextTest, AllPreservedAfterPassWithChange) {
|
|
|
|
std::unique_ptr<ir::Module> module = MakeUnique<ir::Module>();
|
2017-12-19 19:18:13 +00:00
|
|
|
IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
|
|
|
|
spvtools::MessageConsumer());
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
|
|
|
|
i <<= 1) {
|
2017-11-14 19:11:50 +00:00
|
|
|
localContext.BuildInvalidAnalyses(i);
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DummyPassPreservesAll pass(opt::Pass::Status::SuccessWithChange);
|
2017-11-14 19:11:50 +00:00
|
|
|
opt::Pass::Status s = pass.Run(&localContext);
|
2017-11-02 18:25:48 +00:00
|
|
|
EXPECT_EQ(s, opt::Pass::Status::SuccessWithChange);
|
|
|
|
for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
|
|
|
|
i <<= 1) {
|
2017-11-14 19:11:50 +00:00
|
|
|
EXPECT_TRUE(localContext.AreAnalysesValid(i));
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:31:43 +00:00
|
|
|
TEST_F(IRContextTest, PreserveFirstOnlyAfterPassWithChange) {
|
2017-11-02 18:25:48 +00:00
|
|
|
std::unique_ptr<ir::Module> module = MakeUnique<ir::Module>();
|
2017-12-19 19:18:13 +00:00
|
|
|
IRContext localContext(SPV_ENV_UNIVERSAL_1_2, std::move(module),
|
|
|
|
spvtools::MessageConsumer());
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
for (Analysis i = IRContext::kAnalysisBegin; i < IRContext::kAnalysisEnd;
|
|
|
|
i <<= 1) {
|
2017-11-14 19:11:50 +00:00
|
|
|
localContext.BuildInvalidAnalyses(i);
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 14:39:00 +00:00
|
|
|
DummyPassPreservesFirst pass(opt::Pass::Status::SuccessWithChange);
|
2017-11-14 19:11:50 +00:00
|
|
|
opt::Pass::Status s = pass.Run(&localContext);
|
2017-11-02 18:25:48 +00:00
|
|
|
EXPECT_EQ(s, opt::Pass::Status::SuccessWithChange);
|
2017-11-14 19:11:50 +00:00
|
|
|
EXPECT_TRUE(localContext.AreAnalysesValid(IRContext::kAnalysisBegin));
|
2017-11-02 18:25:48 +00:00
|
|
|
for (Analysis i = IRContext::kAnalysisBegin << 1; i < IRContext::kAnalysisEnd;
|
|
|
|
i <<= 1) {
|
2017-11-14 19:11:50 +00:00
|
|
|
EXPECT_FALSE(localContext.AreAnalysesValid(i));
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-13 20:31:43 +00:00
|
|
|
|
|
|
|
TEST_F(IRContextTest, KillMemberName) {
|
|
|
|
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
|
|
|
|
OpName %3 "stuff"
|
|
|
|
OpMemberName %3 0 "refZ"
|
|
|
|
OpMemberDecorate %3 0 Offset 0
|
|
|
|
OpDecorate %3 Block
|
|
|
|
%4 = OpTypeFloat 32
|
|
|
|
%3 = OpTypeStruct %4
|
|
|
|
%5 = OpTypeVoid
|
|
|
|
%6 = OpTypeFunction %5
|
|
|
|
%2 = OpFunction %5 None %6
|
|
|
|
%7 = OpLabel
|
|
|
|
OpReturn
|
|
|
|
OpFunctionEnd
|
|
|
|
)";
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
std::unique_ptr<ir::IRContext> context =
|
2017-11-13 20:31:43 +00:00
|
|
|
BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
|
|
|
|
|
|
|
|
// Build the decoration manager.
|
2017-11-14 19:11:50 +00:00
|
|
|
context->get_decoration_mgr();
|
2017-11-13 20:31:43 +00:00
|
|
|
|
|
|
|
// Delete the OpTypeStruct. Should delete the OpName, OpMemberName, and
|
|
|
|
// OpMemberDecorate associated with it.
|
2017-11-14 19:11:50 +00:00
|
|
|
context->KillDef(3);
|
2017-11-13 20:31:43 +00:00
|
|
|
|
|
|
|
// Make sure all of the name are removed.
|
2017-11-14 19:11:50 +00:00
|
|
|
for (auto& inst : context->debugs2()) {
|
2017-11-13 20:31:43 +00:00
|
|
|
EXPECT_EQ(inst.opcode(), SpvOpNop);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure all of the decorations are removed.
|
2017-11-14 19:11:50 +00:00
|
|
|
for (auto& inst : context->annotations()) {
|
2017-11-13 20:31:43 +00:00
|
|
|
EXPECT_EQ(inst.opcode(), SpvOpNop);
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
|
|
|
|
TEST_F(IRContextTest, TakeNextUniqueIdIncrementing) {
|
|
|
|
const uint32_t NUM_TESTS = 1000;
|
2017-12-19 19:18:13 +00:00
|
|
|
IRContext localContext(SPV_ENV_UNIVERSAL_1_2, nullptr);
|
2017-11-14 19:11:50 +00:00
|
|
|
for (uint32_t i = 1; i < NUM_TESTS; ++i)
|
|
|
|
EXPECT_EQ(i, localContext.TakeNextUniqueId());
|
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
|
2017-11-02 18:25:48 +00:00
|
|
|
} // anonymous namespace
|