blob: be76caaa8b3cdd7473248e5dfc806ce6fcb23a9e [file] [log] [blame]
Lei Zhangabf8f642016-06-28 10:23:13 -04001// Copyright (c) 2016 Google Inc.
2//
David Neto9fc86582016-09-01 15:33:59 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
Lei Zhangabf8f642016-06-28 10:23:13 -04006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Lei Zhangabf8f642016-06-28 10:23:13 -04008//
David Neto9fc86582016-09-01 15:33:59 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Lei Zhangabf8f642016-06-28 10:23:13 -040014
Lei Zhang620f05e2016-09-16 16:12:04 -040015#include "spirv-tools/libspirv.hpp"
Lei Zhangabf8f642016-06-28 10:23:13 -040016
Ryan Harrison9150cd42021-01-14 16:45:18 -050017#include <cassert>
Steven Perron5c8b4f52018-08-08 11:16:19 -040018#include <iostream>
dan sinclaireda2cfb2018-08-03 15:06:09 -040019#include <string>
20#include <utility>
21#include <vector>
22
23#include "source/table.h"
Lei Zhangabf8f642016-06-28 10:23:13 -040024
25namespace spvtools {
26
Pierre Moreau7183ad52018-01-03 01:54:55 +010027Context::Context(spv_target_env env) : context_(spvContextCreate(env)) {}
28
29Context::Context(Context&& other) : context_(other.context_) {
30 other.context_ = nullptr;
31}
32
33Context& Context::operator=(Context&& other) {
34 spvContextDestroy(context_);
35 context_ = other.context_;
36 other.context_ = nullptr;
37
38 return *this;
39}
40
41Context::~Context() { spvContextDestroy(context_); }
42
43void Context::SetMessageConsumer(MessageConsumer consumer) {
dan sinclair3dad1cd2018-07-07 09:38:00 -040044 SetContextMessageConsumer(context_, std::move(consumer));
Pierre Moreau7183ad52018-01-03 01:54:55 +010045}
46
47spv_context& Context::CContext() { return context_; }
48
49const spv_context& Context::CContext() const { return context_; }
50
Lei Zhang8654caa2016-09-09 10:46:23 -040051// Structs for holding the data members for SpvTools.
Lei Zhang92411ca2016-09-16 15:56:30 -040052struct SpirvTools::Impl {
Lei Zhang8654caa2016-09-09 10:46:23 -040053 explicit Impl(spv_target_env env) : context(spvContextCreate(env)) {
54 // The default consumer in spv_context_t is a null consumer, which provides
55 // equivalent functionality (from the user's perspective) as a real consumer
56 // does nothing.
57 }
58 ~Impl() { spvContextDestroy(context); }
59
60 spv_context context; // C interface context object.
61};
62
Ryan Harrison9150cd42021-01-14 16:45:18 -050063SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {
64 assert(env != SPV_ENV_WEBGPU_0);
65}
Lei Zhang8654caa2016-09-09 10:46:23 -040066
Lei Zhang92411ca2016-09-16 15:56:30 -040067SpirvTools::~SpirvTools() {}
Lei Zhang8654caa2016-09-09 10:46:23 -040068
Lei Zhang92411ca2016-09-16 15:56:30 -040069void SpirvTools::SetMessageConsumer(MessageConsumer consumer) {
dan sinclair3dad1cd2018-07-07 09:38:00 -040070 SetContextMessageConsumer(impl_->context, std::move(consumer));
Lei Zhang755f97f2016-09-02 18:06:18 -040071}
72
Lei Zhang92411ca2016-09-16 15:56:30 -040073bool SpirvTools::Assemble(const std::string& text,
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040074 std::vector<uint32_t>* binary,
75 uint32_t options) const {
76 return Assemble(text.data(), text.size(), binary, options);
Lei Zhang5edf0542016-09-20 18:03:37 -040077}
78
79bool SpirvTools::Assemble(const char* text, const size_t text_size,
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040080 std::vector<uint32_t>* binary,
81 uint32_t options) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040082 spv_binary spvbinary = nullptr;
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040083 spv_result_t status = spvTextToBinaryWithOptions(
84 impl_->context, text, text_size, options, &spvbinary, nullptr);
Lei Zhangabf8f642016-06-28 10:23:13 -040085 if (status == SPV_SUCCESS) {
86 binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount);
87 }
Lei Zhangabf8f642016-06-28 10:23:13 -040088 spvBinaryDestroy(spvbinary);
Lei Zhang8654caa2016-09-09 10:46:23 -040089 return status == SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -040090}
91
Lei Zhang92411ca2016-09-16 15:56:30 -040092bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary,
93 std::string* text, uint32_t options) const {
Lei Zhang5edf0542016-09-20 18:03:37 -040094 return Disassemble(binary.data(), binary.size(), text, options);
95}
96
97bool SpirvTools::Disassemble(const uint32_t* binary, const size_t binary_size,
98 std::string* text, uint32_t options) const {
Lei Zhangabf8f642016-06-28 10:23:13 -040099 spv_text spvtext = nullptr;
Lei Zhang5edf0542016-09-20 18:03:37 -0400100 spv_result_t status = spvBinaryToText(impl_->context, binary, binary_size,
101 options, &spvtext, nullptr);
Mike0b824322022-06-30 00:05:00 +0800102 if (status == SPV_SUCCESS &&
103 (options & SPV_BINARY_TO_TEXT_OPTION_PRINT) == 0) {
104 assert(spvtext);
Lei Zhangabf8f642016-06-28 10:23:13 -0400105 text->assign(spvtext->str, spvtext->str + spvtext->length);
106 }
Lei Zhangabf8f642016-06-28 10:23:13 -0400107 spvTextDestroy(spvtext);
Lei Zhang8654caa2016-09-09 10:46:23 -0400108 return status == SPV_SUCCESS;
109}
Lei Zhangabf8f642016-06-28 10:23:13 -0400110
Lei Zhang92411ca2016-09-16 15:56:30 -0400111bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const {
Lei Zhang5edf0542016-09-20 18:03:37 -0400112 return Validate(binary.data(), binary.size());
113}
114
115bool SpirvTools::Validate(const uint32_t* binary,
116 const size_t binary_size) const {
117 return spvValidateBinary(impl_->context, binary, binary_size, nullptr) ==
118 SPV_SUCCESS;
Lei Zhangabf8f642016-06-28 10:23:13 -0400119}
120
David Neto0066a362017-03-16 15:06:12 -0400121bool SpirvTools::Validate(const uint32_t* binary, const size_t binary_size,
Steven Perron75c1bf22018-09-10 11:49:41 -0400122 spv_validator_options options) const {
David Neto0066a362017-03-16 15:06:12 -0400123 spv_const_binary_t the_binary{binary, binary_size};
Steven Perron5c8b4f52018-08-08 11:16:19 -0400124 spv_diagnostic diagnostic = nullptr;
125 bool valid = spvValidateWithOptions(impl_->context, options, &the_binary,
126 &diagnostic) == SPV_SUCCESS;
127 if (!valid && impl_->context->consumer) {
128 impl_->context->consumer.operator()(
129 SPV_MSG_ERROR, nullptr, diagnostic->position, diagnostic->error);
130 }
131 spvDiagnosticDestroy(diagnostic);
132 return valid;
David Neto0066a362017-03-16 15:06:12 -0400133}
134
fjhenigman20b2e2b2019-01-24 09:45:09 -0500135bool SpirvTools::IsValid() const { return impl_->context != nullptr; }
136
Lei Zhangabf8f642016-06-28 10:23:13 -0400137} // namespace spvtools