Promote ir namespace and create draft libspirv.{h|c}pp.

This commit is contained in:
Lei Zhang 2016-06-28 10:23:13 -04:00
parent 8590f9cc81
commit abf8f6413c
16 changed files with 138 additions and 122 deletions

View File

@ -3,12 +3,14 @@ add_library(SPIRV-Tools-opt
function.h
instruction.h
ir_loader.h
libspirv.hpp
module.h
reflect.h
function.cpp
instruction.cpp
ir_loader.cpp
libspirv.cpp
module.cpp
)

View File

@ -37,7 +37,6 @@
#include "instruction.h"
namespace spvtools {
namespace opt {
namespace ir {
class Function;
@ -81,7 +80,6 @@ inline void BasicBlock::ToBinary(std::vector<uint32_t>* binary,
}
} // namespace ir
} // namespace opt
} // namespace spvtools
#endif // LIBSPIRV_OPT_BASIC_BLOCK_H_

View File

@ -27,7 +27,6 @@
#include "function.h"
namespace spvtools {
namespace opt {
namespace ir {
void Function::ForEachInst(const std::function<void(Instruction*)>& f) {
@ -45,5 +44,4 @@ void Function::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
}
} // namespace ir
} // namespace opt
} // namespace spvtools

View File

@ -35,7 +35,6 @@
#include "instruction.h"
namespace spvtools {
namespace opt {
namespace ir {
class Module;
@ -74,7 +73,6 @@ class Function {
};
} // namespace ir
} // namespace opt
} // namespace spvtools
#endif // LIBSPIRV_OPT_CONSTRUCTS_H_

View File

@ -31,7 +31,6 @@
#include "reflect.h"
namespace spvtools {
namespace opt {
namespace ir {
Instruction::Instruction(const spv_parsed_instruction_t& inst,
@ -78,5 +77,4 @@ void Instruction::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
}
} // namespace ir
} // namespace opt
} // namespace spvtools

View File

@ -36,7 +36,6 @@
#include "spirv/1.1/spirv.h"
namespace spvtools {
namespace opt {
namespace ir {
class Function;
@ -185,7 +184,6 @@ inline void Instruction::ForEachInst(
}
} // namespace ir
} // namespace opt
} // namespace spvtools
#endif // LIBSPIRV_OPT_INSTRUCTION_H_

View File

@ -31,7 +31,6 @@
#include "reflect.h"
namespace spvtools {
namespace opt {
namespace ir {
void IrLoader::AddInstruction(const spv_parsed_instruction_t* inst) {
@ -119,5 +118,4 @@ void IrLoader::EndModule() {
}
} // namespace ir
} // namespace opt
} // namespace spvtools

View File

@ -35,7 +35,6 @@
#include "spirv-tools/libspirv.h"
namespace spvtools {
namespace opt {
namespace ir {
// Loader class for constructing SPIR-V in-memory IR representation. Methods in
@ -77,7 +76,6 @@ class IrLoader {
};
} // namespace ir
} // namespace opt
} // namespace spvtools
#endif // LIBSPIRV_OPT_IR_LOADER_H_

View File

@ -24,15 +24,11 @@
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
#include "opt_test_common.h"
#include "libspirv.hpp"
#include <gtest/gtest.h>
#include "source/opt/ir_loader.h"
#include "spirv-tools/libspirv.h"
#include "ir_loader.h"
namespace spvtools {
namespace opt {
namespace {
@ -54,63 +50,64 @@ spv_result_t SetSpvInst(void* builder, const spv_parsed_instruction_t* inst) {
} // annoymous namespace
// Assembles the given assembly |text| and returns the binary.
std::vector<uint32_t> Assemble(const std::string& text) {
spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
spv_binary binary = nullptr;
spv_result_t SpvTools::Assemble(const std::string& text,
std::vector<uint32_t>* binary) {
spv_binary spvbinary = nullptr;
spv_diagnostic diagnostic = nullptr;
spv_result_t status =
spvTextToBinary(context, text.data(), text.size(), &binary, &diagnostic);
EXPECT_EQ(SPV_SUCCESS, status) << "assemble text to binary failed";
std::vector<uint32_t> result(binary->code, binary->code + binary->wordCount);
spv_result_t status = spvTextToBinary(context_, text.data(), text.size(),
&spvbinary, &diagnostic);
if (status == SPV_SUCCESS) {
binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount);
}
spvDiagnosticDestroy(diagnostic);
spvBinaryDestroy(binary);
spvContextDestroy(context);
spvBinaryDestroy(spvbinary);
return result;
return status;
}
// Disassembles the given SPIR-V |binary| and returns the assembly.
std::string Disassemble(const std::vector<uint32_t>& binary) {
spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
spv_text text = nullptr;
spv_result_t SpvTools::Disassemble(const std::vector<uint32_t>& binary,
std::string* text) {
spv_text spvtext = nullptr;
spv_diagnostic diagnostic = nullptr;
spv_result_t status =
spvBinaryToText(context, binary.data(), binary.size(),
SPV_BINARY_TO_TEXT_OPTION_NO_HEADER, &text, &diagnostic);
EXPECT_EQ(SPV_SUCCESS, status) << "disassemble binary to text failed";
std::string result(text->str, text->str + text->length);
spv_result_t status = spvBinaryToText(context_, binary.data(), binary.size(),
SPV_BINARY_TO_TEXT_OPTION_NO_HEADER,
&spvtext, &diagnostic);
if (status == SPV_SUCCESS) {
text->assign(spvtext->str, spvtext->str + spvtext->length);
}
spvDiagnosticDestroy(diagnostic);
spvTextDestroy(text);
spvContextDestroy(context);
spvTextDestroy(spvtext);
return result;
return status;
}
// Builds and returns a Module for the given SPIR-V |binary|.
std::unique_ptr<ir::Module> BuildModule(const std::vector<uint32_t>& binary) {
spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
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);
ir::IrLoader builder(module.get());
ir::IrLoader loader(module.get());
spv_result_t status =
spvBinaryParse(context, &builder, binary.data(), binary.size(),
spvBinaryParse(context_, &loader, binary.data(), binary.size(),
SetSpvHeader, SetSpvInst, &diagnostic);
EXPECT_EQ(SPV_SUCCESS, status) << "build ir::Module from binary failed";
spvDiagnosticDestroy(diagnostic);
spvContextDestroy(context);
builder.EndModule();
loader.EndModule();
return module;
if (status == SPV_SUCCESS) return module;
return nullptr;
}
std::unique_ptr<ir::Module> SpvTools::BuildModule(const std::string& text) {
std::vector<uint32_t> binary;
if (Assemble(text, &binary) != SPV_SUCCESS) return nullptr;
return BuildModule(binary);
}
} // namespace opt
} // namespace spvtools

76
source/opt/libspirv.hpp Normal file
View File

@ -0,0 +1,76 @@
// 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 SPIRV_TOOLS_LIBSPIRV_HPP_
#define SPIRV_TOOLS_LIBSPIRV_HPP_
#include <memory>
#include <string>
#include <vector>
#include "module.h"
#include "spirv-tools/libspirv.h"
namespace spvtools {
// C++ interface for SPIRV-Tools functionalities. It wraps the context
// (including target environment and the corresponding SPIR-V grammar) and
// provides methods for assembling, disassembling, validating, and optimizing.
//
// Instances of this class are thread-safe.
class SpvTools {
public:
// Creates an instance targeting the given environment |env|.
SpvTools(spv_target_env env) : context_(spvContextCreate(env)) {}
~SpvTools() { spvContextDestroy(context_); }
// TODO(antiagainst): handle error message in the following APIs.
// Assembles the given assembly |text| and writes the result to |binary|.
// Returns SPV_SUCCESS on successful assembling.
spv_result_t Assemble(const std::string& text, std::vector<uint32_t>* binary);
// Disassembles the given SPIR-V |binary| and returns the assembly. Returns
// SPV_SUCCESS on successful disassembling.
spv_result_t Disassemble(const std::vector<uint32_t>& binary,
std::string* text);
// Builds and returns a Module from the given SPIR-V |binary|.
std::unique_ptr<ir::Module> BuildModule(const std::vector<uint32_t>& binary);
// Builds and returns a Module from the given SPIR-V assembly |text|.
std::unique_ptr<ir::Module> BuildModule(const std::string& text);
private:
// Context for the current invocation. Thread-safety of this class depends on
// the constness of this field.
spv_context context_;
};
} // namespace spvtools
#endif // SPIRV_TOOLS_LIBSPIRV_HPP_

View File

@ -28,7 +28,6 @@
#include "reflect.h"
namespace spvtools {
namespace opt {
namespace ir {
std::vector<Instruction*> Module::types() {
@ -75,5 +74,4 @@ void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
}
} // namespace ir
} // namespace opt
} // namespace spvtools

View File

@ -35,7 +35,6 @@
#include "instruction.h"
namespace spvtools {
namespace opt {
namespace ir {
// A struct for containing the module header information.
@ -125,7 +124,6 @@ class Module {
};
} // namespace ir
} // namespace opt
} // namespace spvtools
#endif // LIBSPIRV_OPT_MODULE_H_

View File

@ -30,7 +30,6 @@
#include "spirv/1.1/spirv.h"
namespace spvtools {
namespace opt {
namespace ir {
// Note that as SPIR-V evolves over time, new opcodes may appear. So the
@ -59,7 +58,6 @@ inline bool IsTerminatorInst(SpvOp opcode) {
}
} // namespace ir
} // namespace opt
} // namespace spvtools
#endif // LIBSPIRV_OPT_REFLECT_H_

View File

@ -25,6 +25,6 @@
# MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
add_spvtools_unittest(TARGET ir_loader
SRCS test_ir_loader.cpp opt_test_common.cpp
SRCS test_ir_loader.cpp
LIBS SPIRV-Tools-opt ${SPIRV_TOOLS}
)

View File

@ -1,54 +0,0 @@
// 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_TEST_OPT_OPT_TEST_COMMON_H_
#define LIBSPIRV_TEST_OPT_OPT_TEST_COMMON_H_
#include <memory>
#include <string>
#include <vector>
#include "source/opt/module.h"
namespace spvtools {
namespace opt {
// TODO(antiagainst): expand and export these functions as the C++ interface in
// libspirv.hpp.
// Assembles the given assembly |text| and returns the binary.
std::vector<uint32_t> Assemble(const std::string& text);
// Disassembles the given SPIR-V |binary| and returns the assembly.
std::string Disassemble(const std::vector<uint32_t>& binary);
// Builds and returns a Module for the given SPIR-V |binary|.
std::unique_ptr<ir::Module> BuildModule(const std::vector<uint32_t>& binary);
} // namespace opt
} // namespace spvtools
#endif // LIBSPIRV_TEST_OPT_OPT_TEST_COMMON_H_

View File

@ -26,11 +26,11 @@
#include <gtest/gtest.h>
#include "opt_test_common.h"
#include "opt/libspirv.hpp"
namespace {
using namespace spvtools::opt;
using namespace spvtools;
TEST(IrBuilder, RoundTrip) {
// #version 310 es
@ -76,11 +76,16 @@ TEST(IrBuilder, RoundTrip) {
"OpReturnValue %20\n"
"OpFunctionEnd\n";
std::unique_ptr<ir::Module> module = BuildModule(Assemble(text));
SpvTools t(SPV_ENV_UNIVERSAL_1_1);
std::unique_ptr<ir::Module> module = t.BuildModule(text);
ASSERT_NE(nullptr, module);
std::vector<uint32_t> binary;
module->ToBinary(&binary, /* skip_nop = */ false);
EXPECT_EQ(text, Disassemble(binary));
std::string disassembled_text;
EXPECT_EQ(SPV_SUCCESS, t.Disassemble(binary, &disassembled_text));
EXPECT_EQ(text, disassembled_text);
}
TEST(IrBuilder, KeepLineDebugInfo) {
@ -109,11 +114,16 @@ TEST(IrBuilder, KeepLineDebugInfo) {
"OpReturn\n"
"OpFunctionEnd\n";
std::unique_ptr<ir::Module> module = BuildModule(Assemble(text));
SpvTools t(SPV_ENV_UNIVERSAL_1_1);
std::unique_ptr<ir::Module> module = t.BuildModule(text);
ASSERT_NE(nullptr, module);
std::vector<uint32_t> binary;
module->ToBinary(&binary, /* skip_nop = */ false);
EXPECT_EQ(text, Disassemble(binary));
std::string disassembled_text;
EXPECT_EQ(SPV_SUCCESS, t.Disassemble(binary, &disassembled_text));
EXPECT_EQ(text, disassembled_text);
}
TEST(IrBuilder, LocalGlobalVariables) {
@ -183,11 +193,16 @@ TEST(IrBuilder, LocalGlobalVariables) {
"OpReturnValue %30\n"
"OpFunctionEnd\n";
std::unique_ptr<ir::Module> module = BuildModule(Assemble(text));
SpvTools t(SPV_ENV_UNIVERSAL_1_1);
std::unique_ptr<ir::Module> module = t.BuildModule(text);
ASSERT_NE(nullptr, module);
std::vector<uint32_t> binary;
module->ToBinary(&binary, /* skip_nop = */ false);
EXPECT_EQ(text, Disassemble(binary));
std::string disassembled_text;
EXPECT_EQ(SPV_SUCCESS, t.Disassemble(binary, &disassembled_text));
EXPECT_EQ(text, disassembled_text);
}
} // anonymous namespace