File: extract_source.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 (216 lines) | stat: -rw-r--r-- 7,775 bytes parent folder | download | duplicates (7)
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
// 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 "extract_source.h"

#include <cassert>
#include <string>
#include <unordered_map>
#include <vector>

#include "source/latest_version_spirv_header.h"
#include "source/opt/log.h"
#include "spirv-tools/libspirv.hpp"
#include "tools/util/cli_consumer.h"

namespace {

constexpr auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_6;

// Extract a string literal from a given range.
// Copies all the characters from `begin` to the first '\0' it encounters, while
// removing escape patterns.
// Not finding a '\0' before reaching `end` fails the extraction.
//
// Returns `true` if the extraction succeeded.
// `output` value is undefined if false is returned.
spv_result_t ExtractStringLiteral(const spv_position_t& loc, const char* begin,
                                  const char* end, std::string* output) {
  size_t sourceLength = std::distance(begin, end);
  std::string escapedString;
  escapedString.resize(sourceLength);

  size_t writeIndex = 0;
  size_t readIndex = 0;
  for (; readIndex < sourceLength; writeIndex++, readIndex++) {
    const char read = begin[readIndex];
    if (read == '\0') {
      escapedString.resize(writeIndex);
      output->append(escapedString);
      return SPV_SUCCESS;
    }

    if (read == '\\') {
      ++readIndex;
    }
    escapedString[writeIndex] = begin[readIndex];
  }

  spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc,
                  "Missing NULL terminator for literal string.");
  return SPV_ERROR_INVALID_BINARY;
}

spv_result_t extractOpString(const spv_position_t& loc,
                             const spv_parsed_instruction_t& instruction,
                             std::string* output) {
  assert(output != nullptr);
  assert(instruction.opcode == static_cast<unsigned>(spv::Op::OpString));
  if (instruction.num_operands != 2) {
    spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc,
                    "Missing operands for OpString.");
    return SPV_ERROR_INVALID_BINARY;
  }

  const auto& operand = instruction.operands[1];
  const char* stringBegin =
      reinterpret_cast<const char*>(instruction.words + operand.offset);
  const char* stringEnd = reinterpret_cast<const char*>(
      instruction.words + operand.offset + operand.num_words);
  return ExtractStringLiteral(loc, stringBegin, stringEnd, output);
}

spv_result_t extractOpSourceContinued(
    const spv_position_t& loc, const spv_parsed_instruction_t& instruction,
    std::string* output) {
  assert(output != nullptr);
  assert(instruction.opcode ==
         static_cast<unsigned>(spv::Op::OpSourceContinued));
  if (instruction.num_operands != 1) {
    spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc,
                    "Missing operands for OpSourceContinued.");
    return SPV_ERROR_INVALID_BINARY;
  }

  const auto& operand = instruction.operands[0];
  const char* stringBegin =
      reinterpret_cast<const char*>(instruction.words + operand.offset);
  const char* stringEnd = reinterpret_cast<const char*>(
      instruction.words + operand.offset + operand.num_words);
  return ExtractStringLiteral(loc, stringBegin, stringEnd, output);
}

spv_result_t extractOpSource(const spv_position_t& loc,
                             const spv_parsed_instruction_t& instruction,
                             spv::Id* filename, std::string* code) {
  assert(filename != nullptr && code != nullptr);
  assert(instruction.opcode == static_cast<unsigned>(spv::Op::OpSource));
  // OpCode [ Source Language | Version | File (optional) | Source (optional) ]
  if (instruction.num_words < 3) {
    spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc,
                    "Missing operands for OpSource.");
    return SPV_ERROR_INVALID_BINARY;
  }

  *filename = 0;
  *code = "";
  if (instruction.num_words < 4) {
    return SPV_SUCCESS;
  }
  *filename = instruction.words[3];

  if (instruction.num_words < 5) {
    return SPV_SUCCESS;
  }

  const char* stringBegin =
      reinterpret_cast<const char*>(instruction.words + 4);
  const char* stringEnd =
      reinterpret_cast<const char*>(instruction.words + instruction.num_words);
  return ExtractStringLiteral(loc, stringBegin, stringEnd, code);
}

}  // namespace

bool ExtractSourceFromModule(
    const std::vector<uint32_t>& binary,
    std::unordered_map<std::string, std::string>* output) {
  auto context = spvtools::SpirvTools(kDefaultEnvironment);
  context.SetMessageConsumer(spvtools::utils::CLIMessageConsumer);

  // There is nothing valuable in the header.
  spvtools::HeaderParser headerParser = [](const spv_endianness_t,
                                           const spv_parsed_header_t&) {
    return SPV_SUCCESS;
  };

  std::unordered_map<uint32_t, std::string> stringMap;
  std::vector<std::pair<spv::Id, std::string>> sources;
  spv::Op lastOpcode = spv::Op::Max;
  size_t instructionIndex = 0;

  spvtools::InstructionParser instructionParser =
      [&stringMap, &sources, &lastOpcode,
       &instructionIndex](const spv_parsed_instruction_t& instruction) {
        const spv_position_t loc = {0, 0, instructionIndex + 1};
        spv_result_t result = SPV_SUCCESS;

        if (instruction.opcode == static_cast<unsigned>(spv::Op::OpString)) {
          std::string content;
          result = extractOpString(loc, instruction, &content);
          if (result == SPV_SUCCESS) {
            stringMap.emplace(instruction.result_id, std::move(content));
          }
        } else if (instruction.opcode ==
                   static_cast<unsigned>(spv::Op::OpSource)) {
          spv::Id filenameId;
          std::string code;
          result = extractOpSource(loc, instruction, &filenameId, &code);
          if (result == SPV_SUCCESS) {
            sources.emplace_back(std::make_pair(filenameId, std::move(code)));
          }
        } else if (instruction.opcode ==
                   static_cast<unsigned>(spv::Op::OpSourceContinued)) {
          if (lastOpcode != spv::Op::OpSource) {
            spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc,
                            "OpSourceContinued MUST follow an OpSource.");
            return SPV_ERROR_INVALID_BINARY;
          }

          assert(sources.size() > 0);
          result = extractOpSourceContinued(loc, instruction,
                                            &sources.back().second);
        }

        ++instructionIndex;
        lastOpcode = static_cast<spv::Op>(instruction.opcode);
        return result;
      };

  if (!context.Parse(binary, headerParser, instructionParser)) {
    return false;
  }

  std::string defaultName = "unnamed-";
  size_t unnamedCount = 0;
  for (auto & [ id, code ] : sources) {
    std::string filename;
    const auto it = stringMap.find(id);
    if (it == stringMap.cend() || it->second.empty()) {
      filename = "unnamed-" + std::to_string(unnamedCount) + ".hlsl";
      ++unnamedCount;
    } else {
      filename = it->second;
    }

    if (output->count(filename) != 0) {
      spvtools::Error(spvtools::utils::CLIMessageConsumer, "", {},
                      "Source file name conflict.");
      return false;
    }
    output->insert({filename, code});
  }

  return true;
}