2016-06-28 14:23:13 +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 14:23:13 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-28 14:23:13 +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 14:23:13 +00:00
|
|
|
|
|
|
|
#ifndef SPIRV_TOOLS_LIBSPIRV_HPP_
|
|
|
|
#define SPIRV_TOOLS_LIBSPIRV_HPP_
|
|
|
|
|
2016-09-16 20:12:04 +00:00
|
|
|
#include <functional>
|
2016-09-09 14:46:23 +00:00
|
|
|
#include <memory>
|
2016-06-28 14:23:13 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "spirv-tools/libspirv.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
|
2016-09-16 20:12:04 +00:00
|
|
|
// Message consumer. The C strings for source and message are only alive for the
|
|
|
|
// specific invocation.
|
|
|
|
using MessageConsumer = std::function<void(
|
|
|
|
spv_message_level_t /* level */, const char* /* source */,
|
|
|
|
const spv_position_t& /* position */, const char* /* message */
|
|
|
|
)>;
|
|
|
|
|
2017-03-16 19:06:12 +00:00
|
|
|
// A RAII wrapper around a validator options object.
|
|
|
|
class ValidatorOptions {
|
|
|
|
public:
|
|
|
|
ValidatorOptions() : options_(spvValidatorOptionsCreate()) {}
|
|
|
|
~ValidatorOptions() { spvValidatorOptionsDestroy(options_); }
|
|
|
|
// Allow implicit conversion to the underlying object.
|
|
|
|
operator spv_validator_options() const { return options_; }
|
|
|
|
|
|
|
|
// Sets a limit.
|
|
|
|
void SetUniversalLimit(spv_validator_limit limit_type, uint32_t limit) {
|
|
|
|
spvValidatorOptionsSetUniversalLimit(options_, limit_type, limit);
|
|
|
|
}
|
|
|
|
|
2017-10-24 19:13:13 +00:00
|
|
|
void SetRelaxStructStore(bool val) {
|
|
|
|
spvValidatorOptionsSetRelaxStoreStruct(options_, val);
|
|
|
|
}
|
|
|
|
|
2017-03-16 19:06:12 +00:00
|
|
|
private:
|
|
|
|
spv_validator_options options_;
|
|
|
|
};
|
|
|
|
|
2016-06-28 14:23:13 +00:00
|
|
|
// C++ interface for SPIRV-Tools functionalities. It wraps the context
|
|
|
|
// (including target environment and the corresponding SPIR-V grammar) and
|
2016-09-09 14:46:23 +00:00
|
|
|
// provides methods for assembling, disassembling, and validating.
|
2016-06-28 14:23:13 +00:00
|
|
|
//
|
2016-09-09 14:46:23 +00:00
|
|
|
// Instances of this class provide basic thread-safety guarantee.
|
2016-09-16 19:56:30 +00:00
|
|
|
class SpirvTools {
|
2016-06-28 14:23:13 +00:00
|
|
|
public:
|
2016-09-09 14:46:23 +00:00
|
|
|
enum {
|
2017-04-11 23:46:15 +00:00
|
|
|
// Default assembling option used by assemble():
|
|
|
|
kDefaultAssembleOption = SPV_TEXT_TO_BINARY_OPTION_NONE,
|
|
|
|
|
2016-09-09 14:46:23 +00:00
|
|
|
// Default disassembling option used by Disassemble():
|
|
|
|
// * Avoid prefix comments from decoding the SPIR-V module header, and
|
|
|
|
// * Use friendly names for variables.
|
|
|
|
kDefaultDisassembleOption = SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES
|
|
|
|
};
|
2016-06-28 14:23:13 +00:00
|
|
|
|
2016-09-09 14:46:23 +00:00
|
|
|
// Constructs an instance targeting the given environment |env|.
|
|
|
|
//
|
|
|
|
// 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.
|
2016-09-16 19:56:30 +00:00
|
|
|
explicit SpirvTools(spv_target_env env);
|
2016-06-28 14:23:13 +00:00
|
|
|
|
2016-09-09 14:46:23 +00:00
|
|
|
// Disables copy/move constructor/assignment operations.
|
2016-09-16 19:56:30 +00:00
|
|
|
SpirvTools(const SpirvTools&) = delete;
|
|
|
|
SpirvTools(SpirvTools&&) = delete;
|
|
|
|
SpirvTools& operator=(const SpirvTools&) = delete;
|
|
|
|
SpirvTools& operator=(SpirvTools&&) = delete;
|
2016-09-09 14:46:23 +00:00
|
|
|
|
|
|
|
// Destructs this instance.
|
2016-09-16 19:56:30 +00:00
|
|
|
~SpirvTools();
|
2016-09-09 14:46:23 +00:00
|
|
|
|
|
|
|
// Sets the message consumer to the given |consumer|. The |consumer| will be
|
|
|
|
// invoked once for each message communicated from the library.
|
2016-09-02 22:06:18 +00:00
|
|
|
void SetMessageConsumer(MessageConsumer consumer);
|
2016-06-28 14:23:13 +00:00
|
|
|
|
|
|
|
// Assembles the given assembly |text| and writes the result to |binary|.
|
2016-09-09 14:46:23 +00:00
|
|
|
// Returns true on successful assembling. |binary| will be kept untouched if
|
|
|
|
// assembling is unsuccessful.
|
2017-04-11 23:46:15 +00:00
|
|
|
bool Assemble(const std::string& text, std::vector<uint32_t>* binary,
|
|
|
|
uint32_t options = kDefaultAssembleOption) const;
|
2016-09-20 22:03:37 +00:00
|
|
|
// |text_size| specifies the number of bytes in |text|. A terminating null
|
|
|
|
// character is not required to present in |text| as long as |text| is valid.
|
|
|
|
bool Assemble(const char* text, size_t text_size,
|
2017-04-11 23:46:15 +00:00
|
|
|
std::vector<uint32_t>* binary,
|
|
|
|
uint32_t options = kDefaultAssembleOption) const;
|
2016-09-09 14:46:23 +00:00
|
|
|
|
|
|
|
// Disassembles the given SPIR-V |binary| with the given |options| and writes
|
|
|
|
// the assembly to |text|. Returns ture on successful disassembling. |text|
|
|
|
|
// will be kept untouched if diassembling is unsuccessful.
|
|
|
|
bool Disassemble(const std::vector<uint32_t>& binary, std::string* text,
|
|
|
|
uint32_t options = kDefaultDisassembleOption) const;
|
2016-09-20 22:03:37 +00:00
|
|
|
// |binary_size| specifies the number of words in |binary|.
|
|
|
|
bool Disassemble(const uint32_t* binary, size_t binary_size,
|
|
|
|
std::string* text,
|
|
|
|
uint32_t options = kDefaultDisassembleOption) const;
|
2016-09-09 14:46:23 +00:00
|
|
|
|
|
|
|
// Validates the given SPIR-V |binary|. Returns true if no issues are found.
|
|
|
|
// Otherwise, returns false and communicates issues via the message consumer
|
|
|
|
// registered.
|
|
|
|
bool Validate(const std::vector<uint32_t>& binary) const;
|
2016-09-20 22:03:37 +00:00
|
|
|
// |binary_size| specifies the number of words in |binary|.
|
|
|
|
bool Validate(const uint32_t* binary, size_t binary_size) const;
|
2017-03-16 19:06:12 +00:00
|
|
|
// Like the previous overload, but takes an options object.
|
2017-04-11 23:46:15 +00:00
|
|
|
bool Validate(const uint32_t* binary, size_t binary_size,
|
|
|
|
const ValidatorOptions& options) const;
|
2016-06-28 14:23:13 +00:00
|
|
|
|
|
|
|
private:
|
2016-09-09 14:46:23 +00:00
|
|
|
struct Impl; // Opaque struct for holding the data fields used by this class.
|
|
|
|
std::unique_ptr<Impl> impl_; // Unique pointer to implementation data.
|
2016-06-28 14:23:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace spvtools
|
|
|
|
|
|
|
|
#endif // SPIRV_TOOLS_LIBSPIRV_HPP_
|