1fd4e5da5Sopenharmony_ci// Copyright (c) 2017 Pierre Moreau
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci#include <string>
16fd4e5da5Sopenharmony_ci
17fd4e5da5Sopenharmony_ci#include "gmock/gmock.h"
18fd4e5da5Sopenharmony_ci#include "test/link/linker_fixture.h"
19fd4e5da5Sopenharmony_ci
20fd4e5da5Sopenharmony_cinamespace spvtools {
21fd4e5da5Sopenharmony_cinamespace {
22fd4e5da5Sopenharmony_ci
23fd4e5da5Sopenharmony_ciusing ::testing::HasSubstr;
24fd4e5da5Sopenharmony_ciusing BinaryVersion = spvtest::LinkerTest;
25fd4e5da5Sopenharmony_ci
26fd4e5da5Sopenharmony_cispvtest::Binary CreateBinary(uint32_t version) {
27fd4e5da5Sopenharmony_ci  return {
28fd4e5da5Sopenharmony_ci      // clang-format off
29fd4e5da5Sopenharmony_ci      // Header
30fd4e5da5Sopenharmony_ci      static_cast<uint32_t>(spv::MagicNumber),
31fd4e5da5Sopenharmony_ci      version,
32fd4e5da5Sopenharmony_ci      SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS, 0),
33fd4e5da5Sopenharmony_ci      1u,  // NOTE: Bound
34fd4e5da5Sopenharmony_ci      0u,  // NOTE: Schema; reserved
35fd4e5da5Sopenharmony_ci
36fd4e5da5Sopenharmony_ci      // OpCapability Shader
37fd4e5da5Sopenharmony_ci      static_cast<uint32_t>(spv::Op::OpCapability) | 2u << spv::WordCountShift,
38fd4e5da5Sopenharmony_ci      static_cast<uint32_t>(spv::Capability::Shader),
39fd4e5da5Sopenharmony_ci
40fd4e5da5Sopenharmony_ci      // OpMemoryModel Logical Simple
41fd4e5da5Sopenharmony_ci      static_cast<uint32_t>(spv::Op::OpMemoryModel) | 3u << spv::WordCountShift,
42fd4e5da5Sopenharmony_ci      static_cast<uint32_t>(spv::AddressingModel::Logical),
43fd4e5da5Sopenharmony_ci      static_cast<uint32_t>(spv::MemoryModel::Simple)
44fd4e5da5Sopenharmony_ci      // clang-format on
45fd4e5da5Sopenharmony_ci  };
46fd4e5da5Sopenharmony_ci}
47fd4e5da5Sopenharmony_ci
48fd4e5da5Sopenharmony_ciTEST_F(BinaryVersion, Match) {
49fd4e5da5Sopenharmony_ci  // clang-format off
50fd4e5da5Sopenharmony_ci  spvtest::Binaries binaries = {
51fd4e5da5Sopenharmony_ci      CreateBinary(SPV_SPIRV_VERSION_WORD(1, 3)),
52fd4e5da5Sopenharmony_ci      CreateBinary(SPV_SPIRV_VERSION_WORD(1, 3)),
53fd4e5da5Sopenharmony_ci  };
54fd4e5da5Sopenharmony_ci  // clang-format on
55fd4e5da5Sopenharmony_ci  spvtest::Binary linked_binary;
56fd4e5da5Sopenharmony_ci  ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary)) << GetErrorMessage();
57fd4e5da5Sopenharmony_ci  EXPECT_THAT(GetErrorMessage(), std::string());
58fd4e5da5Sopenharmony_ci  EXPECT_EQ(SPV_SPIRV_VERSION_WORD(1, 3), linked_binary[1]);
59fd4e5da5Sopenharmony_ci}
60fd4e5da5Sopenharmony_ci
61fd4e5da5Sopenharmony_ciTEST_F(BinaryVersion, Mismatch) {
62fd4e5da5Sopenharmony_ci  // clang-format off
63fd4e5da5Sopenharmony_ci  spvtest::Binaries binaries = {
64fd4e5da5Sopenharmony_ci      CreateBinary(SPV_SPIRV_VERSION_WORD(1, 3)),
65fd4e5da5Sopenharmony_ci      CreateBinary(SPV_SPIRV_VERSION_WORD(1, 5)),
66fd4e5da5Sopenharmony_ci  };
67fd4e5da5Sopenharmony_ci  // clang-format on
68fd4e5da5Sopenharmony_ci  spvtest::Binary linked_binary;
69fd4e5da5Sopenharmony_ci  ASSERT_EQ(SPV_ERROR_INTERNAL, Link(binaries, &linked_binary))
70fd4e5da5Sopenharmony_ci      << GetErrorMessage();
71fd4e5da5Sopenharmony_ci  EXPECT_THAT(GetErrorMessage(),
72fd4e5da5Sopenharmony_ci              HasSubstr("Conflicting SPIR-V versions: 1.3 (input modules 1 "
73fd4e5da5Sopenharmony_ci                        "through 1) vs 1.5 (input module 2)."));
74fd4e5da5Sopenharmony_ci}
75fd4e5da5Sopenharmony_ci
76fd4e5da5Sopenharmony_ciTEST_F(BinaryVersion, UseHighest) {
77fd4e5da5Sopenharmony_ci  // clang-format off
78fd4e5da5Sopenharmony_ci  spvtest::Binaries binaries = {
79fd4e5da5Sopenharmony_ci      CreateBinary(SPV_SPIRV_VERSION_WORD(1, 3)),
80fd4e5da5Sopenharmony_ci      CreateBinary(SPV_SPIRV_VERSION_WORD(1, 5)),
81fd4e5da5Sopenharmony_ci  };
82fd4e5da5Sopenharmony_ci  // clang-format on
83fd4e5da5Sopenharmony_ci  LinkerOptions options;
84fd4e5da5Sopenharmony_ci  options.SetUseHighestVersion(true);
85fd4e5da5Sopenharmony_ci  spvtest::Binary linked_binary;
86fd4e5da5Sopenharmony_ci  ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary, options))
87fd4e5da5Sopenharmony_ci      << GetErrorMessage();
88fd4e5da5Sopenharmony_ci  EXPECT_THAT(GetErrorMessage(), std::string());
89fd4e5da5Sopenharmony_ci  EXPECT_EQ(SPV_SPIRV_VERSION_WORD(1, 5), linked_binary[1]);
90fd4e5da5Sopenharmony_ci}
91fd4e5da5Sopenharmony_ci
92fd4e5da5Sopenharmony_ci}  // namespace
93fd4e5da5Sopenharmony_ci}  // namespace spvtools
94