std::unique_ptr<ir::Module> BuildModule(spv_target_env env,
MessageConsumer consumer,
const std::string& text) {
- SpvTools t(env);
+ SpirvTools t(env);
t.SetMessageConsumer(consumer);
std::vector<uint32_t> binary;
if (!t.Assemble(text, &binary)) return nullptr;
namespace spvtools {
// Structs for holding the data members for SpvTools.
-struct SpvTools::Impl {
+struct SpirvTools::Impl {
explicit Impl(spv_target_env env) : context(spvContextCreate(env)) {
// The default consumer in spv_context_t is a null consumer, which provides
// equivalent functionality (from the user's perspective) as a real consumer
spv_context context; // C interface context object.
};
-SpvTools::SpvTools(spv_target_env env) : impl_(new Impl(env)) {}
+SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {}
-SpvTools::~SpvTools() {}
+SpirvTools::~SpirvTools() {}
-void SpvTools::SetMessageConsumer(MessageConsumer consumer) {
+void SpirvTools::SetMessageConsumer(MessageConsumer consumer) {
SetContextMessageConsumer(impl_->context, std::move(consumer));
}
-bool SpvTools::Assemble(const std::string& text,
- std::vector<uint32_t>* binary) const {
+bool SpirvTools::Assemble(const std::string& text,
+ std::vector<uint32_t>* binary) const {
spv_binary spvbinary = nullptr;
spv_result_t status = spvTextToBinary(impl_->context, text.data(),
text.size(), &spvbinary, nullptr);
return status == SPV_SUCCESS;
}
-bool SpvTools::Disassemble(const std::vector<uint32_t>& binary,
- std::string* text, uint32_t options) const {
+bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary,
+ std::string* text, uint32_t options) const {
spv_text spvtext = nullptr;
spv_result_t status = spvBinaryToText(
impl_->context, binary.data(), binary.size(), options, &spvtext, nullptr);
return status == SPV_SUCCESS;
}
-bool SpvTools::Validate(const std::vector<uint32_t>& binary) const {
+bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const {
spv_const_binary_t b = {binary.data(), binary.size()};
return spvValidate(impl_->context, &b, nullptr) == SPV_SUCCESS;
}
// provides methods for assembling, disassembling, and validating.
//
// Instances of this class provide basic thread-safety guarantee.
-class SpvTools {
+class SpirvTools {
public:
enum {
// Default disassembling option used by Disassemble():
// The constructed instance will have an empty message consumer, which just
// ignores all messages from the library. Use SetMessageConsumer() to supply
// one if messages are of concern.
- explicit SpvTools(spv_target_env env);
+ explicit SpirvTools(spv_target_env env);
// Disables copy/move constructor/assignment operations.
- SpvTools(const SpvTools&) = delete;
- SpvTools(SpvTools&&) = delete;
- SpvTools& operator=(const SpvTools&) = delete;
- SpvTools& operator=(SpvTools&&) = delete;
+ SpirvTools(const SpirvTools&) = delete;
+ SpirvTools(SpirvTools&&) = delete;
+ SpirvTools& operator=(const SpirvTools&) = delete;
+ SpirvTools& operator=(SpirvTools&&) = delete;
// Destructs this instance.
- ~SpvTools();
+ ~SpirvTools();
// Sets the message consumer to the given |consumer|. The |consumer| will be
// invoked once for each message communicated from the library.
TEST(CppInterface, SuccessfulRoundTrip) {
const std::string input_text = "%2 = OpSizeOf %1 %3\n";
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
std::vector<uint32_t> binary;
EXPECT_TRUE(t.Assemble(input_text, &binary));
TEST(CppInterface, AssembleEmptyModule) {
std::vector<uint32_t> binary(10, 42);
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
EXPECT_TRUE(t.Assemble("", &binary));
// We only have the header.
EXPECT_EQ(5u, binary.size());
TEST(CppInterface, AssembleWithWrongTargetEnv) {
const std::string input_text = "%r = OpSizeOf %type %pointer";
- SpvTools t(SPV_ENV_UNIVERSAL_1_0);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_0);
int invocation_count = 0;
t.SetMessageConsumer(
[&invocation_count](spv_message_level_t level, const char* source,
TEST(CppInterface, DisassembleEmptyModule) {
std::string text(10, 'x');
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
int invocation_count = 0;
t.SetMessageConsumer(
[&invocation_count](spv_message_level_t level, const char* source,
TEST(CppInterface, DisassembleWithWrongTargetEnv) {
const std::string input_text = "%r = OpSizeOf %type %pointer";
- SpvTools t11(SPV_ENV_UNIVERSAL_1_1);
- SpvTools t10(SPV_ENV_UNIVERSAL_1_0);
+ SpirvTools t11(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t10(SPV_ENV_UNIVERSAL_1_0);
int invocation_count = 0;
t10.SetMessageConsumer(
[&invocation_count](spv_message_level_t level, const char* source,
TEST(CppInterface, SuccessfulValidation) {
const std::string input_text =
"OpCapability Shader\nOpMemoryModel Logical GLSL450";
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
int invocation_count = 0;
t.SetMessageConsumer([&invocation_count](spv_message_level_t, const char*,
const spv_position_t&, const char*) {
}
TEST(CppInterface, ValidateEmptyModule) {
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
int invocation_count = 0;
t.SetMessageConsumer(
[&invocation_count](spv_message_level_t level, const char* source,
// source code, we can get the given |optimized| source code.
void CheckOptimization(const char* original, const char* optimized,
const Optimizer& opt) {
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
std::vector<uint32_t> original_binary;
ASSERT_TRUE(t.Assemble(original, &original_binary));
}
TEST(CppInterface, OptimizeEmptyModule) {
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
std::vector<uint32_t> binary;
EXPECT_TRUE(t.Assemble("", &binary));
}
TEST(CppInterface, OptimizeSameAddressForOriginalOptimizedBinary) {
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
std::vector<uint32_t> binary;
ASSERT_TRUE(t.Assemble("OpSource GLSL 450", &binary));
private:
MessageConsumer consumer_; // Message consumer.
- SpvTools tools_; // An instance for calling SPIRV-Tools functionalities.
+ SpirvTools tools_; // An instance for calling SPIRV-Tools functionalities.
std::unique_ptr<opt::PassManager> manager_; // The pass manager.
};
// Disassembles the given |inst| and returns the disassembly.
std::string DisassembleInst(ir::Instruction* inst) {
- SpvTools tools(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools tools(SPV_ENV_UNIVERSAL_1_1);
std::vector<uint32_t> binary;
// We need this to generate the necessary header in the binary.
// Disassembles the given |module| and returns the disassembly.
std::string DisassembleModule(ir::Module* module) {
- SpvTools tools(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools tools(SPV_ENV_UNIVERSAL_1_1);
std::vector<uint32_t> binary;
module->ToBinary(&binary, /* skip_nop = */ false);
using namespace spvtools;
void DoRoundTripCheck(const std::string& text) {
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
std::unique_ptr<ir::Module> module =
BuildModule(SPV_ENV_UNIVERSAL_1_1, IgnoreMessage, text);
ASSERT_NE(nullptr, module) << "Failed to assemble\n" << text;
"%double = OpTypeFloat 64\n";
// clang-format on
- SpvTools t(SPV_ENV_UNIVERSAL_1_1);
+ SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
std::unique_ptr<ir::Module> module =
BuildModule(SPV_ENV_UNIVERSAL_1_1, IgnoreMessage, text);
ASSERT_NE(nullptr, module);