| 1 |
// Copyright (c) 2018 Google LLC |
| 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 "test/reduce/reduce_test_util.h" |
| 16 |
|
| 17 |
#include <iostream> |
| 18 |
|
| 19 |
#include "tools/io.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace reduce { |
| 23 |
|
| 24 |
void CheckEqual(const spv_target_env env, |
| 25 |
const std::vector<uint32_t>& expected_binary, |
| 26 |
const std::vector<uint32_t>& actual_binary) { |
| 27 |
if (expected_binary != actual_binary) { |
| 28 |
SpirvTools t(env); |
| 29 |
std::string expected_disassembled; |
| 30 |
std::string actual_disassembled; |
| 31 |
ASSERT_TRUE(t.Disassemble(expected_binary, &expected_disassembled, |
| 32 |
kReduceDisassembleOption)); |
| 33 |
ASSERT_TRUE(t.Disassemble(actual_binary, &actual_disassembled, |
| 34 |
kReduceDisassembleOption)); |
| 35 |
ASSERT_EQ(expected_disassembled, actual_disassembled); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
void CheckEqual(const spv_target_env env, const std::string& expected_text, |
| 40 |
const std::vector<uint32_t>& actual_binary) { |
| 41 |
std::vector<uint32_t> expected_binary; |
| 42 |
SpirvTools t(env); |
| 43 |
ASSERT_TRUE( |
| 44 |
t.Assemble(expected_text, &expected_binary, kReduceAssembleOption)); |
| 45 |
CheckEqual(env, expected_binary, actual_binary); |
| 46 |
} |
| 47 |
|
| 48 |
void CheckEqual(const spv_target_env env, const std::string& expected_text, |
| 49 |
const opt::IRContext* actual_ir) { |
| 50 |
std::vector<uint32_t> actual_binary; |
| 51 |
actual_ir->module()->ToBinary(&actual_binary, false); |
| 52 |
CheckEqual(env, expected_text, actual_binary); |
| 53 |
} |
| 54 |
|
| 55 |
void CheckValid(spv_target_env env, const opt::IRContext* ir) { |
| 56 |
std::vector<uint32_t> binary; |
| 57 |
ir->module()->ToBinary(&binary, false); |
| 58 |
SpirvTools t(env); |
| 59 |
ASSERT_TRUE(t.Validate(binary)); |
| 60 |
} |
| 61 |
|
| 62 |
std::string ToString(spv_target_env env, const opt::IRContext* ir) { |
| 63 |
std::vector<uint32_t> binary; |
| 64 |
ir->module()->ToBinary(&binary, false); |
| 65 |
SpirvTools t(env); |
| 66 |
std::string result; |
| 67 |
t.Disassemble(binary, &result, kReduceDisassembleOption); |
| 68 |
return result; |
| 69 |
} |
| 70 |
|
| 71 |
void NopDiagnostic(spv_message_level_t /*level*/, const char* /*source*/, |
| 72 |
const spv_position_t& /*position*/, |
| 73 |
const char* /*message*/) {} |
| 74 |
|
| 75 |
void CLIMessageConsumer(spv_message_level_t level, const char*, |
| 76 |
const spv_position_t& position, const char* message) { |
| 77 |
switch (level) { |
| 78 |
case SPV_MSG_FATAL: |
| 79 |
case SPV_MSG_INTERNAL_ERROR: |
| 80 |
case SPV_MSG_ERROR: |
| 81 |
std::cerr << "error: line " << position.index << ": " << message |
| 82 |
<< std::endl; |
| 83 |
break; |
| 84 |
case SPV_MSG_WARNING: |
| 85 |
std::cout << "warning: line " << position.index << ": " << message |
| 86 |
<< std::endl; |
| 87 |
break; |
| 88 |
case SPV_MSG_INFO: |
| 89 |
std::cout << "info: line " << position.index << ": " << message |
| 90 |
<< std::endl; |
| 91 |
break; |
| 92 |
default: |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void DumpShader(opt::IRContext* context, const char* filename) { |
| 98 |
std::vector<uint32_t> binary; |
| 99 |
context->module()->ToBinary(&binary, false); |
| 100 |
DumpShader(binary, filename); |
| 101 |
} |
| 102 |
|
| 103 |
void DumpShader(const std::vector<uint32_t>& binary, const char* filename) { |
| 104 |
auto write_file_succeeded = |
| 105 |
WriteFile(filename, "wb", &binary[0], binary.size()); |
| 106 |
if (!write_file_succeeded) { |
| 107 |
std::cerr << "Failed to dump shader" << std::endl; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
} // namespace reduce |
| 112 |
} // namespace spvtools |