2016-06-28 18:58:17 +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-06-28 18:58:17 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-28 18:58:17 +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-06-28 18:58:17 +00:00
|
|
|
|
2016-08-25 22:36:03 +00:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
|
2016-08-31 16:44:49 +00:00
|
|
|
#include <initializer_list>
|
|
|
|
|
2016-08-25 22:36:03 +00:00
|
|
|
#include "module_utils.h"
|
2016-08-19 03:16:21 +00:00
|
|
|
#include "opt/make_unique.h"
|
2016-08-25 22:36:03 +00:00
|
|
|
#include "pass_fixture.h"
|
2016-06-28 18:58:17 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace spvtools;
|
2016-08-25 22:36:03 +00:00
|
|
|
using spvtest::GetIdBound;
|
|
|
|
using ::testing::Eq;
|
2016-06-28 18:58:17 +00:00
|
|
|
|
2016-08-31 16:44:49 +00:00
|
|
|
// A null pass whose construtors accept arguments
|
|
|
|
class NullPassWithArgs : public opt::NullPass {
|
|
|
|
public:
|
2016-09-20 20:48:00 +00:00
|
|
|
NullPassWithArgs(uint32_t) {}
|
|
|
|
NullPassWithArgs(std::string) {}
|
|
|
|
NullPassWithArgs(const std::vector<int>&) {}
|
|
|
|
NullPassWithArgs(const std::vector<int>&, uint32_t) {}
|
2016-08-31 16:44:49 +00:00
|
|
|
|
|
|
|
const char* name() const override { return "null-with-args"; }
|
|
|
|
};
|
|
|
|
|
2016-06-28 18:58:17 +00:00
|
|
|
TEST(PassManager, Interface) {
|
2016-09-20 20:48:00 +00:00
|
|
|
opt::PassManager manager;
|
2016-06-28 18:58:17 +00:00
|
|
|
EXPECT_EQ(0u, manager.NumPasses());
|
|
|
|
|
|
|
|
manager.AddPass<opt::StripDebugInfoPass>();
|
|
|
|
EXPECT_EQ(1u, manager.NumPasses());
|
2016-07-26 16:11:03 +00:00
|
|
|
EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
|
2016-06-28 18:58:17 +00:00
|
|
|
|
2016-09-20 20:48:00 +00:00
|
|
|
manager.AddPass(MakeUnique<opt::NullPass>());
|
2016-06-28 18:58:17 +00:00
|
|
|
EXPECT_EQ(2u, manager.NumPasses());
|
2016-07-26 16:11:03 +00:00
|
|
|
EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
|
2016-07-28 19:30:37 +00:00
|
|
|
EXPECT_STREQ("null", manager.GetPass(1)->name());
|
2016-06-28 18:58:17 +00:00
|
|
|
|
|
|
|
manager.AddPass<opt::StripDebugInfoPass>();
|
|
|
|
EXPECT_EQ(3u, manager.NumPasses());
|
2016-07-26 16:11:03 +00:00
|
|
|
EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
|
2016-07-28 19:30:37 +00:00
|
|
|
EXPECT_STREQ("null", manager.GetPass(1)->name());
|
2016-07-26 16:11:03 +00:00
|
|
|
EXPECT_STREQ("strip-debug", manager.GetPass(2)->name());
|
2016-08-31 16:44:49 +00:00
|
|
|
|
|
|
|
manager.AddPass<NullPassWithArgs>(1u);
|
|
|
|
manager.AddPass<NullPassWithArgs>("null pass args");
|
|
|
|
manager.AddPass<NullPassWithArgs>(std::initializer_list<int>{1, 2});
|
|
|
|
manager.AddPass<NullPassWithArgs>(std::initializer_list<int>{1, 2}, 3);
|
|
|
|
EXPECT_EQ(7u, manager.NumPasses());
|
|
|
|
EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
|
|
|
|
EXPECT_STREQ("null", manager.GetPass(1)->name());
|
|
|
|
EXPECT_STREQ("strip-debug", manager.GetPass(2)->name());
|
|
|
|
EXPECT_STREQ("null-with-args", manager.GetPass(3)->name());
|
|
|
|
EXPECT_STREQ("null-with-args", manager.GetPass(4)->name());
|
|
|
|
EXPECT_STREQ("null-with-args", manager.GetPass(5)->name());
|
|
|
|
EXPECT_STREQ("null-with-args", manager.GetPass(6)->name());
|
2016-06-28 18:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A pass that appends an OpNop instruction to the debug section.
|
|
|
|
class AppendOpNopPass : public opt::Pass {
|
2016-08-20 03:17:44 +00:00
|
|
|
public:
|
2016-06-28 18:58:17 +00:00
|
|
|
const char* name() const override { return "AppendOpNop"; }
|
2016-09-12 16:39:44 +00:00
|
|
|
Status Process(ir::Module* module) override {
|
|
|
|
module->AddDebugInst(MakeUnique<ir::Instruction>());
|
|
|
|
return Status::SuccessWithChange;
|
2016-06-28 18:58:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-31 16:44:49 +00:00
|
|
|
// A pass that appends specified number of OpNop instructions to the debug
|
|
|
|
// section.
|
|
|
|
class AppendMultipleOpNopPass : public opt::Pass {
|
|
|
|
public:
|
2016-09-20 20:48:00 +00:00
|
|
|
explicit AppendMultipleOpNopPass(uint32_t num_nop) : num_nop_(num_nop) {}
|
2016-08-20 03:17:44 +00:00
|
|
|
|
2016-08-31 16:44:49 +00:00
|
|
|
const char* name() const override { return "AppendOpNop"; }
|
2016-09-12 16:39:44 +00:00
|
|
|
Status Process(ir::Module* module) override {
|
2016-08-31 16:44:49 +00:00
|
|
|
for (uint32_t i = 0; i < num_nop_; i++) {
|
2016-09-12 16:39:44 +00:00
|
|
|
module->AddDebugInst(MakeUnique<ir::Instruction>());
|
2016-08-31 16:44:49 +00:00
|
|
|
}
|
2016-09-12 16:39:44 +00:00
|
|
|
return Status::SuccessWithChange;
|
2016-08-31 16:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t num_nop_;
|
|
|
|
};
|
|
|
|
|
2016-06-28 18:58:17 +00:00
|
|
|
// A pass that duplicates the last instruction in the debug section.
|
|
|
|
class DuplicateInstPass : public opt::Pass {
|
2016-08-20 03:17:44 +00:00
|
|
|
public:
|
2016-06-28 18:58:17 +00:00
|
|
|
const char* name() const override { return "DuplicateInst"; }
|
2016-09-12 16:39:44 +00:00
|
|
|
Status Process(ir::Module* module) override {
|
2016-08-19 03:16:21 +00:00
|
|
|
auto inst = MakeUnique<ir::Instruction>(*(--module->debug_end()));
|
2016-06-28 18:58:17 +00:00
|
|
|
module->AddDebugInst(std::move(inst));
|
2016-09-12 16:39:44 +00:00
|
|
|
return Status::SuccessWithChange;
|
2016-06-28 18:58:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using PassManagerTest = PassTest<::testing::Test>;
|
|
|
|
|
|
|
|
TEST_F(PassManagerTest, Run) {
|
|
|
|
const std::string text = "OpMemoryModel Logical GLSL450\nOpSource ESSL 310\n";
|
|
|
|
|
|
|
|
AddPass<AppendOpNopPass>();
|
|
|
|
AddPass<AppendOpNopPass>();
|
|
|
|
RunAndCheck(text.c_str(), (text + "OpNop\nOpNop\n").c_str());
|
|
|
|
|
|
|
|
RenewPassManger();
|
|
|
|
AddPass<AppendOpNopPass>();
|
|
|
|
AddPass<DuplicateInstPass>();
|
|
|
|
RunAndCheck(text.c_str(), (text + "OpNop\nOpNop\n").c_str());
|
|
|
|
|
|
|
|
RenewPassManger();
|
|
|
|
AddPass<DuplicateInstPass>();
|
|
|
|
AddPass<AppendOpNopPass>();
|
|
|
|
RunAndCheck(text.c_str(), (text + "OpSource ESSL 310\nOpNop\n").c_str());
|
2016-08-31 16:44:49 +00:00
|
|
|
|
|
|
|
RenewPassManger();
|
|
|
|
AddPass<AppendMultipleOpNopPass>(3);
|
|
|
|
RunAndCheck(text.c_str(), (text + "OpNop\nOpNop\nOpNop\n").c_str());
|
2016-06-28 18:58:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 22:36:03 +00:00
|
|
|
// A pass that appends an OpTypeVoid instruction that uses a given id.
|
|
|
|
class AppendTypeVoidInstPass : public opt::Pass {
|
|
|
|
public:
|
2016-09-20 20:48:00 +00:00
|
|
|
explicit AppendTypeVoidInstPass(uint32_t result_id) : result_id_(result_id) {}
|
2016-08-20 03:17:44 +00:00
|
|
|
|
2016-08-25 22:36:03 +00:00
|
|
|
const char* name() const override { return "AppendTypeVoidInstPass"; }
|
2016-09-12 16:39:44 +00:00
|
|
|
Status Process(ir::Module* module) override {
|
2016-08-25 22:36:03 +00:00
|
|
|
auto inst = MakeUnique<ir::Instruction>(SpvOpTypeVoid, 0, result_id_,
|
|
|
|
std::vector<ir::Operand>{});
|
|
|
|
module->AddType(std::move(inst));
|
2016-09-12 16:39:44 +00:00
|
|
|
return Status::SuccessWithChange;
|
2016-08-25 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t result_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(PassManager, RecomputeIdBoundAutomatically) {
|
|
|
|
ir::Module module;
|
|
|
|
EXPECT_THAT(GetIdBound(module), Eq(0u));
|
|
|
|
|
2016-09-20 20:48:00 +00:00
|
|
|
opt::PassManager manager;
|
2016-08-25 22:36:03 +00:00
|
|
|
manager.Run(&module);
|
|
|
|
manager.AddPass<AppendOpNopPass>();
|
|
|
|
// With no ID changes, the ID bound does not change.
|
|
|
|
EXPECT_THAT(GetIdBound(module), Eq(0u));
|
|
|
|
|
|
|
|
// Now we force an Id of 100 to be used.
|
2016-09-20 20:48:00 +00:00
|
|
|
manager.AddPass(MakeUnique<AppendTypeVoidInstPass>(100));
|
2016-08-25 22:36:03 +00:00
|
|
|
EXPECT_THAT(GetIdBound(module), Eq(0u));
|
|
|
|
manager.Run(&module);
|
|
|
|
// The Id has been updated automatically, even though the pass
|
|
|
|
// did not update it.
|
|
|
|
EXPECT_THAT(GetIdBound(module), Eq(101u));
|
|
|
|
|
|
|
|
// Try one more time!
|
2016-09-20 20:48:00 +00:00
|
|
|
manager.AddPass(MakeUnique<AppendTypeVoidInstPass>(200));
|
2016-08-25 22:36:03 +00:00
|
|
|
manager.Run(&module);
|
|
|
|
EXPECT_THAT(GetIdBound(module), Eq(201u));
|
|
|
|
|
|
|
|
// Add another pass, but which uses a lower Id.
|
2016-09-20 20:48:00 +00:00
|
|
|
manager.AddPass(MakeUnique<AppendTypeVoidInstPass>(10));
|
2016-08-25 22:36:03 +00:00
|
|
|
manager.Run(&module);
|
|
|
|
// The Id stays high.
|
|
|
|
EXPECT_THAT(GetIdBound(module), Eq(201u));
|
|
|
|
}
|
|
|
|
|
2016-06-28 18:58:17 +00:00
|
|
|
} // anonymous namespace
|