mirror of
https://github.com/KhronosGroup/SPIRV-Tools
synced 2024-12-24 00:40:14 +00:00
Add a home brewed make_unique to ease creating unique_ptrs.
This commit is contained in:
parent
6f13c73229
commit
f51d82338e
@ -27,6 +27,7 @@
|
||||
#include "libspirv.hpp"
|
||||
|
||||
#include "ir_loader.h"
|
||||
#include "make_unique.h"
|
||||
|
||||
namespace spvtools {
|
||||
|
||||
@ -36,8 +37,8 @@ namespace {
|
||||
spv_result_t SetSpvHeader(void* builder, spv_endianness_t, uint32_t magic,
|
||||
uint32_t version, uint32_t generator,
|
||||
uint32_t id_bound, uint32_t reserved) {
|
||||
reinterpret_cast<ir::IrLoader*>(builder)->SetModuleHeader(
|
||||
magic, version, generator, id_bound, reserved);
|
||||
reinterpret_cast<ir::IrLoader*>(builder)
|
||||
->SetModuleHeader(magic, version, generator, id_bound, reserved);
|
||||
return SPV_SUCCESS;
|
||||
};
|
||||
|
||||
@ -88,7 +89,7 @@ std::unique_ptr<ir::Module> SpvTools::BuildModule(
|
||||
const std::vector<uint32_t>& binary) {
|
||||
spv_diagnostic diagnostic = nullptr;
|
||||
|
||||
std::unique_ptr<ir::Module> module(new ir::Module);
|
||||
auto module = MakeUnique<ir::Module>();
|
||||
ir::IrLoader loader(module.get());
|
||||
|
||||
spv_result_t status =
|
||||
|
42
source/opt/make_unique.h
Normal file
42
source/opt/make_unique.h
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2016 Google Inc.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and/or associated documentation files (the
|
||||
// "Materials"), to deal in the Materials without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Materials, and to
|
||||
// permit persons to whom the Materials are furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Materials.
|
||||
//
|
||||
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
|
||||
// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
|
||||
// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
|
||||
// https://www.khronos.org/registry/
|
||||
//
|
||||
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
||||
|
||||
#ifndef LIBSPIRV_OPT_MAKE_UNIQUE_H_
|
||||
#define LIBSPIRV_OPT_MAKE_UNIQUE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
namespace spvtools {
|
||||
|
||||
template <typename T, typename... Args>
|
||||
std::unique_ptr<T> MakeUnique(Args&&... args) {
|
||||
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
} // namespace spvtools
|
||||
|
||||
#endif // LIBSPIRV_OPT_MAKE_UNIQUE_H_
|
@ -27,7 +27,6 @@
|
||||
#ifndef LIBSPIRV_TEST_OPT_PASS_FIXTURE_H_
|
||||
#define LIBSPIRV_TEST_OPT_PASS_FIXTURE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
@ -37,6 +36,7 @@
|
||||
#include "opt/libspirv.hpp"
|
||||
#include "opt/pass_manager.h"
|
||||
#include "opt/passes.h"
|
||||
#include "opt/make_unique.h"
|
||||
|
||||
namespace spvtools {
|
||||
|
||||
@ -59,8 +59,8 @@ class PassTest : public TestT {
|
||||
std::tuple<std::string, bool> OptimizeAndDisassemble(
|
||||
opt::Pass* pass, const std::string& original, bool skip_nop = false) {
|
||||
std::unique_ptr<ir::Module> module = tools_.BuildModule(original);
|
||||
EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
|
||||
<< original << std::endl;
|
||||
EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n" << original
|
||||
<< std::endl;
|
||||
if (!module) {
|
||||
return std::make_tuple(std::string(), false);
|
||||
}
|
||||
@ -71,8 +71,7 @@ class PassTest : public TestT {
|
||||
module->ToBinary(&binary, skip_nop);
|
||||
std::string optimized;
|
||||
EXPECT_EQ(SPV_SUCCESS, tools_.Disassemble(binary, &optimized))
|
||||
<< "Disassembling failed for shader:\n"
|
||||
<< original << std::endl;
|
||||
<< "Disassembling failed for shader:\n" << original << std::endl;
|
||||
return std::make_tuple(optimized, modified);
|
||||
}
|
||||
|
||||
@ -82,7 +81,7 @@ class PassTest : public TestT {
|
||||
template <typename PassT>
|
||||
std::tuple<std::string, bool> SinglePassRunAndDisassemble(
|
||||
const std::string& assembly, bool skip_nop = false) {
|
||||
auto pass = std::unique_ptr<PassT>(new PassT);
|
||||
auto pass = MakeUnique<PassT>();
|
||||
return OptimizeAndDisassemble(pass.get(), assembly, skip_nop);
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "gmock/gmock.h"
|
||||
|
||||
#include "opt/iterator.h"
|
||||
#include "opt/make_unique.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@ -148,7 +149,7 @@ TEST(Iterator, InsertBeginEnd) {
|
||||
// Insert at the beginning
|
||||
expected.insert(expected.begin(), -100);
|
||||
ir::UptrVectorIterator<int> begin(&data, data.begin());
|
||||
auto insert_point = begin.InsertBefore(std::unique_ptr<int>(new int(-100)));
|
||||
auto insert_point = begin.InsertBefore(MakeUnique<int>(-100));
|
||||
for (int i = 0; i < count + 1; ++i) {
|
||||
actual.push_back(*(insert_point++));
|
||||
}
|
||||
@ -159,9 +160,9 @@ TEST(Iterator, InsertBeginEnd) {
|
||||
expected.push_back(-36);
|
||||
expected.push_back(-77);
|
||||
ir::UptrVectorIterator<int> end(&data, data.end());
|
||||
end = end.InsertBefore(std::unique_ptr<int>(new int(-77)));
|
||||
end = end.InsertBefore(std::unique_ptr<int>(new int(-36)));
|
||||
end = end.InsertBefore(std::unique_ptr<int>(new int(-42)));
|
||||
end = end.InsertBefore(MakeUnique<int>(-77));
|
||||
end = end.InsertBefore(MakeUnique<int>(-36));
|
||||
end = end.InsertBefore(MakeUnique<int>(-42));
|
||||
|
||||
actual.clear();
|
||||
begin = ir::UptrVectorIterator<int>(&data, data.begin());
|
||||
@ -189,8 +190,8 @@ TEST(Iterator, InsertMiddle) {
|
||||
|
||||
ir::UptrVectorIterator<int> it(&data, data.begin());
|
||||
for (int i = 0; i < insert_pos; ++i) ++it;
|
||||
it = it.InsertBefore(std::unique_ptr<int>(new int(-100)));
|
||||
it = it.InsertBefore(std::unique_ptr<int>(new int(-42)));
|
||||
it = it.InsertBefore(MakeUnique<int>(-100));
|
||||
it = it.InsertBefore(MakeUnique<int>(-42));
|
||||
auto begin = ir::UptrVectorIterator<int>(&data, data.begin());
|
||||
for (int i = 0; i < count + 2; ++i) {
|
||||
actual.push_back(*(begin++));
|
||||
|
@ -25,6 +25,7 @@
|
||||
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
||||
|
||||
#include "pass_fixture.h"
|
||||
#include "opt/make_unique.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@ -38,7 +39,7 @@ TEST(PassManager, Interface) {
|
||||
EXPECT_EQ(1u, manager.NumPasses());
|
||||
EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
|
||||
|
||||
manager.AddPass(std::unique_ptr<opt::NullPass>(new opt::NullPass));
|
||||
manager.AddPass(MakeUnique<opt::NullPass>());
|
||||
EXPECT_EQ(2u, manager.NumPasses());
|
||||
EXPECT_STREQ("strip-debug", manager.GetPass(0)->name());
|
||||
EXPECT_STREQ("null", manager.GetPass(1)->name());
|
||||
@ -54,7 +55,7 @@ TEST(PassManager, Interface) {
|
||||
class AppendOpNopPass : public opt::Pass {
|
||||
const char* name() const override { return "AppendOpNop"; }
|
||||
bool Process(ir::Module* module) override {
|
||||
std::unique_ptr<ir::Instruction> inst(new ir::Instruction());
|
||||
auto inst = MakeUnique<ir::Instruction>();
|
||||
module->AddDebugInst(std::move(inst));
|
||||
return true;
|
||||
}
|
||||
@ -64,8 +65,7 @@ class AppendOpNopPass : public opt::Pass {
|
||||
class DuplicateInstPass : public opt::Pass {
|
||||
const char* name() const override { return "DuplicateInst"; }
|
||||
bool Process(ir::Module* module) override {
|
||||
std::unique_ptr<ir::Instruction> inst(
|
||||
new ir::Instruction(*(--module->debug_end())));
|
||||
auto inst = MakeUnique<ir::Instruction>(*(--module->debug_end()));
|
||||
module->AddDebugInst(std::move(inst));
|
||||
return true;
|
||||
}
|
||||
|
@ -29,11 +29,13 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "source/opt/types.h"
|
||||
#include "opt/make_unique.h"
|
||||
#include "opt/types.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace spvtools::opt::analysis;
|
||||
using spvtools::MakeUnique;
|
||||
|
||||
// Fixture class providing some element types.
|
||||
class SameTypeTest : public ::testing::Test {
|
||||
@ -241,18 +243,18 @@ TEST(Types, FloatWidth) {
|
||||
}
|
||||
|
||||
TEST(Types, VectorElementCount) {
|
||||
auto s32 = std::unique_ptr<Integer>(new Integer(32, true));
|
||||
auto s32 = MakeUnique<Integer>(32, true);
|
||||
for (uint32_t c : {2, 3, 4}) {
|
||||
auto s32v = std::unique_ptr<Vector>(new Vector(s32.get(), c));
|
||||
auto s32v = MakeUnique<Vector>(s32.get(), c);
|
||||
EXPECT_EQ(c, s32v->element_count());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Types, MatrixElementCount) {
|
||||
auto s32 = std::unique_ptr<Integer>(new Integer(32, true));
|
||||
auto s32v4 = std::unique_ptr<Vector>(new Vector(s32.get(), 4));
|
||||
auto s32 = MakeUnique<Integer>(32, true);
|
||||
auto s32v4 = MakeUnique<Vector>(s32.get(), 4);
|
||||
for (uint32_t c : {1, 2, 3, 4, 10, 100}) {
|
||||
auto s32m = std::unique_ptr<Matrix>(new Matrix(s32v4.get(), c));
|
||||
auto s32m = MakeUnique<Matrix>(s32v4.get(), c);
|
||||
EXPECT_EQ(c, s32m->element_count());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user