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#ifndef TEST_REDUCE_REDUCE_TEST_UTIL_H_
16#define TEST_REDUCE_REDUCE_TEST_UTIL_H_
17
18#include "gtest/gtest.h"
19#include "source/opt/ir_context.h"
20#include "source/reduce/reduction_opportunity.h"
21#include "spirv-tools/libspirv.h"
22
23namespace spvtools {
24namespace reduce {
25
26// Checks whether the given binaries are bit-wise equal.
27void CheckEqual(spv_target_env env,
28                const std::vector<uint32_t>& expected_binary,
29                const std::vector<uint32_t>& actual_binary);
30
31// Assembles the given text and check whether the resulting binary is bit-wise
32// equal to the given binary.
33void CheckEqual(spv_target_env env, const std::string& expected_text,
34                const std::vector<uint32_t>& actual_binary);
35
36// Assembles the given text and turns the given IR into binary, then checks
37// whether the resulting binaries are bit-wise equal.
38void CheckEqual(spv_target_env env, const std::string& expected_text,
39                const opt::IRContext* actual_ir);
40
41// Assembles the given IR context and checks whether the resulting binary is
42// valid.
43void CheckValid(spv_target_env env, const opt::IRContext* ir);
44
45// Assembles the given IR context, then returns its disassembly as a string.
46// Useful for debugging.
47std::string ToString(spv_target_env env, const opt::IRContext* ir);
48
49// Assembly options for writing reduction tests.  It simplifies matters if
50// numeric ids do not change.
51const uint32_t kReduceAssembleOption =
52    SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS;
53// Disassembly options for writing reduction tests.
54const uint32_t kReduceDisassembleOption =
55    SPV_BINARY_TO_TEXT_OPTION_NO_HEADER | SPV_BINARY_TO_TEXT_OPTION_INDENT;
56
57// Don't print reducer info during testing.
58void NopDiagnostic(spv_message_level_t /*level*/, const char* /*source*/,
59                   const spv_position_t& /*position*/, const char* /*message*/);
60
61// Prints reducer messages (for debugging).
62void CLIMessageConsumer(spv_message_level_t level, const char*,
63                        const spv_position_t& position, const char* message);
64
65// Dumps the SPIRV-V module in |context| to file |filename|. Useful for
66// interactive debugging.
67void DumpShader(opt::IRContext* context, const char* filename);
68
69// Dumps |binary| to file |filename|. Useful for interactive debugging.
70void DumpShader(const std::vector<uint32_t>& binary, const char* filename);
71
72}  // namespace reduce
73}  // namespace spvtools
74
75#endif  // TEST_REDUCE_REDUCE_TEST_UTIL_H_
76