David Neto | 3ac9939 | 2020-03-17 16:14:10 +0000 | [diff] [blame] | 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 | |
David Neto | 4e7f7da | 2020-03-17 20:07:41 +0000 | [diff] [blame] | 15 | #include "src/reader/spirv/spirv_tools_helpers_test.h" |
David Neto | 3ac9939 | 2020-03-17 16:14:10 +0000 | [diff] [blame] | 16 | |
David Neto | 3ac9939 | 2020-03-17 16:14:10 +0000 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
| 18 | #include "spirv-tools/libspirv.hpp" |
| 19 | |
| 20 | namespace tint { |
| 21 | namespace reader { |
David Neto | 4df5d40 | 2020-03-17 20:08:06 +0000 | [diff] [blame] | 22 | namespace spirv { |
David Neto | 3ac9939 | 2020-03-17 16:14:10 +0000 | [diff] [blame] | 23 | namespace test { |
| 24 | |
David Neto | 3ac9939 | 2020-03-17 16:14:10 +0000 | [diff] [blame] | 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 | |
David Neto | 7e5e02f | 2020-05-20 20:36:18 +0000 | [diff] [blame] | 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 | |
David Neto | 1b543c6 | 2020-11-24 15:17:46 +0000 | [diff] [blame] | 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; |
David Neto | df48b95 | 2021-05-04 20:17:41 +0000 | [diff] [blame] | 77 | const auto success = tools.Disassemble( |
David Neto | 58a3624 | 2021-05-05 09:46:31 +0000 | [diff] [blame] | 78 | spirv_module, &result, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES); |
David Neto | 1b543c6 | 2020-11-24 15:17:46 +0000 | [diff] [blame] | 79 | EXPECT_TRUE(success) << errors.str(); |
| 80 | |
| 81 | return result; |
| 82 | } |
| 83 | |
David Neto | 3ac9939 | 2020-03-17 16:14:10 +0000 | [diff] [blame] | 84 | } // namespace test |
David Neto | 4df5d40 | 2020-03-17 20:08:06 +0000 | [diff] [blame] | 85 | } // namespace spirv |
David Neto | 3ac9939 | 2020-03-17 16:14:10 +0000 | [diff] [blame] | 86 | } // namespace reader |
| 87 | } // namespace tint |