1 // 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
15 #include "src/reader/spirv/spirv_tools_helpers_test.h"
16
17 #include "gtest/gtest.h"
18 #include "spirv-tools/libspirv.hpp"
19
20 namespace tint {
21 namespace reader {
22 namespace spirv {
23 namespace test {
24
Assemble(const std::string & spirv_assembly)25 std::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
AssembleFailure(const std::string & spirv_assembly)46 std::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
Disassemble(const std::vector<uint32_t> & spirv_module)66 std::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;
77 const auto success = tools.Disassemble(
78 spirv_module, &result, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
79 EXPECT_TRUE(success) << errors.str();
80
81 return result;
82 }
83
84 } // namespace test
85 } // namespace spirv
86 } // namespace reader
87 } // namespace tint
88