File: extract_source_test.cpp

package info (click to toggle)
spirv-tools 2025.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,588 kB
  • sloc: cpp: 470,407; javascript: 5,893; python: 3,326; ansic: 488; sh: 450; ruby: 88; makefile: 18; lisp: 9
file content (265 lines) | stat: -rw-r--r-- 7,952 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2023 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "tools/objdump/extract_source.h"

#include <gtest/gtest.h>

#include <string>

#include "source/opt/build_module.h"
#include "source/opt/ir_context.h"
#include "spirv-tools/libspirv.hpp"
#include "tools/util/cli_consumer.h"

namespace {

constexpr auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_6;

std::pair<bool, std::unordered_map<std::string, std::string>> ExtractSource(
    const std::string& spv_source) {
  std::unique_ptr<spvtools::opt::IRContext> ctx = spvtools::BuildModule(
      kDefaultEnvironment, spvtools::utils::CLIMessageConsumer, spv_source,
      spvtools::SpirvTools::kDefaultAssembleOption |
          SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  std::vector<uint32_t> binary;
  ctx->module()->ToBinary(&binary, /* skip_nop = */ false);
  std::unordered_map<std::string, std::string> output;
  bool result = ExtractSourceFromModule(binary, &output);
  return std::make_pair(result, std::move(output));
}

}  // namespace

TEST(ExtractSourceTest, no_debug) {
  std::string source = R"(
           OpCapability Shader
           OpCapability Linkage
           OpMemoryModel Logical GLSL450
   %void = OpTypeVoid
      %2 = OpTypeFunction %void
   %bool = OpTypeBool
      %4 = OpUndef %bool
      %5 = OpFunction %void None %2
      %6 = OpLabel
           OpReturn
           OpFunctionEnd
  )";

  auto[success, result] = ExtractSource(source);
  ASSERT_TRUE(success);
  ASSERT_TRUE(result.size() == 0);
}

TEST(ExtractSourceTest, SimpleSource) {
  std::string source = R"(
      OpCapability Shader
      OpMemoryModel Logical GLSL450
      OpEntryPoint GLCompute %1 "compute_1"
      OpExecutionMode %1 LocalSize 1 1 1
 %2 = OpString "compute.hlsl"
      OpSource HLSL 660 %2 "[numthreads(1, 1, 1)] void compute_1(){ }"
      OpName %1 "compute_1"
 %3 = OpTypeVoid
 %4 = OpTypeFunction %3
 %1 = OpFunction %3 None %4
 %5 = OpLabel
      OpLine %2 1 41
      OpReturn
      OpFunctionEnd
  )";

  auto[success, result] = ExtractSource(source);
  ASSERT_TRUE(success);
  ASSERT_TRUE(result.size() == 1);
  ASSERT_TRUE(result["compute.hlsl"] ==
              "[numthreads(1, 1, 1)] void compute_1(){ }");
}

TEST(ExtractSourceTest, SourceContinued) {
  std::string source = R"(
      OpCapability Shader
      OpMemoryModel Logical GLSL450
      OpEntryPoint GLCompute %1 "compute_1"
      OpExecutionMode %1 LocalSize 1 1 1
 %2 = OpString "compute.hlsl"
      OpSource HLSL 660 %2 "[numthreads(1, 1, 1)] "
      OpSourceContinued "void compute_1(){ }"
      OpName %1 "compute_1"
 %3 = OpTypeVoid
 %4 = OpTypeFunction %3
 %1 = OpFunction %3 None %4
 %5 = OpLabel
      OpLine %2 1 41
      OpReturn
      OpFunctionEnd
  )";

  auto[success, result] = ExtractSource(source);
  ASSERT_TRUE(success);
  ASSERT_TRUE(result.size() == 1);
  ASSERT_TRUE(result["compute.hlsl"] ==
              "[numthreads(1, 1, 1)] void compute_1(){ }");
}

TEST(ExtractSourceTest, OnlyFilename) {
  std::string source = R"(
      OpCapability Shader
      OpMemoryModel Logical GLSL450
      OpEntryPoint GLCompute %1 "compute_1"
      OpExecutionMode %1 LocalSize 1 1 1
 %2 = OpString "compute.hlsl"
      OpSource HLSL 660 %2
      OpName %1 "compute_1"
 %3 = OpTypeVoid
 %4 = OpTypeFunction %3
 %1 = OpFunction %3 None %4
 %5 = OpLabel
      OpLine %2 1 41
      OpReturn
      OpFunctionEnd
  )";

  auto[success, result] = ExtractSource(source);
  ASSERT_TRUE(success);
  ASSERT_TRUE(result.size() == 1);
  ASSERT_TRUE(result["compute.hlsl"] == "");
}

TEST(ExtractSourceTest, MultipleFiles) {
  std::string source = R"(
      OpCapability Shader
      OpMemoryModel Logical GLSL450
      OpEntryPoint GLCompute %1 "compute_1"
      OpExecutionMode %1 LocalSize 1 1 1
 %2 = OpString "compute1.hlsl"
 %3 = OpString "compute2.hlsl"
      OpSource HLSL 660 %2 "some instruction"
      OpSource HLSL 660 %3 "some other instruction"
      OpName %1 "compute_1"
 %4 = OpTypeVoid
 %5 = OpTypeFunction %4
 %1 = OpFunction %4 None %5
 %6 = OpLabel
      OpLine %2 1 41
      OpReturn
      OpFunctionEnd
  )";

  auto[success, result] = ExtractSource(source);
  ASSERT_TRUE(success);
  ASSERT_TRUE(result.size() == 2);
  ASSERT_TRUE(result["compute1.hlsl"] == "some instruction");
  ASSERT_TRUE(result["compute2.hlsl"] == "some other instruction");
}

TEST(ExtractSourceTest, MultilineCode) {
  std::string source = R"(
               OpCapability Shader
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %1 "compute_1"
               OpExecutionMode %1 LocalSize 1 1 1
          %2 = OpString "compute.hlsl"
               OpSource HLSL 660 %2 "[numthreads(1, 1, 1)]
void compute_1() {
}
"
               OpName %1 "compute_1"
          %3 = OpTypeVoid
          %4 = OpTypeFunction %3
          %1 = OpFunction %3 None %4
          %5 = OpLabel
               OpLine %2 3 1
               OpReturn
               OpFunctionEnd
  )";

  auto[success, result] = ExtractSource(source);
  ASSERT_TRUE(success);
  ASSERT_TRUE(result.size() == 1);
  ASSERT_TRUE(result["compute.hlsl"] ==
              "[numthreads(1, 1, 1)]\nvoid compute_1() {\n}\n");
}

TEST(ExtractSourceTest, EmptyFilename) {
  std::string source = R"(
               OpCapability Shader
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %1 "compute_1"
               OpExecutionMode %1 LocalSize 1 1 1
          %2 = OpString ""
               OpSource HLSL 660 %2 "void compute(){}"
               OpName %1 "compute_1"
          %3 = OpTypeVoid
          %4 = OpTypeFunction %3
          %1 = OpFunction %3 None %4
          %5 = OpLabel
               OpLine %2 3 1
               OpReturn
               OpFunctionEnd
  )";

  auto[success, result] = ExtractSource(source);
  ASSERT_TRUE(success);
  ASSERT_TRUE(result.size() == 1);
  ASSERT_TRUE(result["unnamed-0.hlsl"] == "void compute(){}");
}

TEST(ExtractSourceTest, EscapeEscaped) {
  std::string source = R"(
               OpCapability Shader
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %1 "compute"
               OpExecutionMode %1 LocalSize 1 1 1
          %2 = OpString "compute.hlsl"
               OpSource HLSL 660 %2 "// check \" escape removed"
               OpName %1 "compute"
          %3 = OpTypeVoid
          %4 = OpTypeFunction %3
          %1 = OpFunction %3 None %4
          %5 = OpLabel
               OpLine %2 6 1
               OpReturn
               OpFunctionEnd
  )";

  auto[success, result] = ExtractSource(source);
  ASSERT_TRUE(success);
  ASSERT_TRUE(result.size() == 1);
  ASSERT_TRUE(result["compute.hlsl"] == "// check \" escape removed");
}

TEST(ExtractSourceTest, OpSourceWithNoSource) {
  std::string source = R"(
               OpCapability Shader
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %1 "compute"
               OpExecutionMode %1 LocalSize 1 1 1
          %2 = OpString "compute.hlsl"
               OpSource HLSL 660 %2
               OpName %1 "compute"
          %3 = OpTypeVoid
          %4 = OpTypeFunction %3
          %1 = OpFunction %3 None %4
          %5 = OpLabel
               OpLine %2 6 1
               OpReturn
               OpFunctionEnd
  )";

  auto[success, result] = ExtractSource(source);
  ASSERT_TRUE(success);
  ASSERT_TRUE(result.size() == 1);
  ASSERT_TRUE(result["compute.hlsl"] == "");
}