blob: a6b2268be5c2d126ae11572b9e3fd98c628a68d7 [file] [log] [blame]
David Neto3ac99392020-03-17 16:14:10 +00001// Copyright 2020 The Tint Authors
2//
3// 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
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// 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.
14
David Neto4e7f7da2020-03-17 20:07:41 +000015#include "src/reader/spirv/spirv_tools_helpers_test.h"
David Neto3ac99392020-03-17 16:14:10 +000016
David Neto3ac99392020-03-17 16:14:10 +000017#include "gtest/gtest.h"
18#include "spirv-tools/libspirv.hpp"
19
20namespace tint {
21namespace reader {
David Neto4df5d402020-03-17 20:08:06 +000022namespace spirv {
David Neto3ac99392020-03-17 16:14:10 +000023namespace test {
24
David Neto3ac99392020-03-17 16:14:10 +000025std::vector<uint32_t> Assemble(const std::string& spirv_assembly) {
26 // TODO(dneto): Use ScopedTrace?
27
28 // (The target environment doesn't affect assembly.
29 spvtools::SpirvTools tools(SPV_ENV_UNIVERSAL_1_0);
30 std::stringstream errors;
31 std::vector<uint32_t> result;
32 tools.SetMessageConsumer([&errors](spv_message_level_t, const char*,
33 const spv_position_t& position,
34 const char* message) {
35 errors << "assembly error:" << position.line << ":" << position.column
36 << ": " << message;
37 });
38
39 const auto success = tools.Assemble(
40 spirv_assembly, &result, SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
41 EXPECT_TRUE(success) << errors.str();
42
43 return result;
44}
45
David Neto7e5e02f2020-05-20 20:36:18 +000046std::string AssembleFailure(const std::string& spirv_assembly) {
47 // TODO(dneto): Use ScopedTrace?
48
49 // (The target environment doesn't affect assembly.
50 spvtools::SpirvTools tools(SPV_ENV_UNIVERSAL_1_0);
51 std::stringstream errors;
52 std::vector<uint32_t> result;
53 tools.SetMessageConsumer([&errors](spv_message_level_t, const char*,
54 const spv_position_t& position,
55 const char* message) {
56 errors << position.line << ":" << position.column << ": " << message;
57 });
58
59 const auto success = tools.Assemble(
60 spirv_assembly, &result, SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
61 EXPECT_FALSE(success);
62
63 return errors.str();
64}
65
David Neto1b543c62020-11-24 15:17:46 +000066std::string Disassemble(const std::vector<uint32_t>& spirv_module) {
67 spvtools::SpirvTools tools(SPV_ENV_UNIVERSAL_1_0);
68 std::stringstream errors;
69 tools.SetMessageConsumer([&errors](spv_message_level_t, const char*,
70 const spv_position_t& position,
71 const char* message) {
72 errors << "disassmbly error:" << position.line << ":" << position.column
73 << ": " << message;
74 });
75
76 std::string result;
David Netodf48b952021-05-04 20:17:41 +000077 const auto success = tools.Disassemble(
David Neto58a36242021-05-05 09:46:31 +000078 spirv_module, &result, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
David Neto1b543c62020-11-24 15:17:46 +000079 EXPECT_TRUE(success) << errors.str();
80
81 return result;
82}
83
David Neto3ac99392020-03-17 16:14:10 +000084} // namespace test
David Neto4df5d402020-03-17 20:08:06 +000085} // namespace spirv
David Neto3ac99392020-03-17 16:14:10 +000086} // namespace reader
87} // namespace tint