SPV compression: Remove file/path manipulation stuff, setting up for that to be a separate tool. Added copyright messages as well.

git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@31201 e7fa87d3-cd2b-0410-9028-fcbf551c1848
This commit is contained in:
John Kessenich 2015-05-15 16:02:07 +00:00
parent ccc7acc969
commit 2aa7f3a671
3 changed files with 473 additions and 542 deletions

View File

@ -1,29 +1,50 @@
//
//Copyright (C) 2015 LunarG, Inc.
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//
#include "SPVRemapper.h" #include "SPVRemapper.h"
#include "doc.h" #include "doc.h"
/* -*-mode:c++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 3 -*- */ /* -*-mode:c++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 3 -*- */
// Poor man's basename: given a complete path, return file portion. #if !defined (use_cpp11)
// E.g: // ... not supported before C++11
// Linux: /foo/bar/test -> test #else // defined (use_cpp11)
// Win: c:\foo\bar\test -> test
// It's not very efficient, but that doesn't matter for our minimal-duty use. #include <algorithm>
// Using boost::filesystem would be better in many ways, but want to avoid that dependency. #include <cassert>
const std::string spv::spirvbin_base_t::basename(const std::string& filename)
{
const size_t sepLoc = filename.find_last_of(path_sep_char());
return (sepLoc == filename.npos) ? filename : filename.substr(sepLoc+1);
}
#if !defined (use_cpp11)
// ... not supported before C++11
#else // defined (use_cpp11)
#include <fstream>
#include <algorithm>
#include <cassert>
namespace spv { namespace spv {
// By default, just abort on error. Can be overridden via RegisterErrorHandler // By default, just abort on error. Can be overridden via RegisterErrorHandler
@ -201,20 +222,20 @@ spv::Id spirvbin_t::localId(spv::Id id, spv::Id newId)
if (id >= idMapL.size()) if (id >= idMapL.size())
idMapL.resize(id+1, unused); idMapL.resize(id+1, unused);
if (newId != unmapped && newId != unused) { if (newId != unmapped && newId != unused) {
if (isOldIdUnused(id)) if (isOldIdUnused(id))
ferror(std::string("ID unused in module: ") + std::to_string(id)); error(std::string("ID unused in module: ") + std::to_string(id));
if (!isOldIdUnmapped(id)) if (!isOldIdUnmapped(id))
ferror(std::string("ID already mapped: ") + std::to_string(id) + " -> " error(std::string("ID already mapped: ") + std::to_string(id) + " -> "
+ std::to_string(localId(id))); + std::to_string(localId(id)));
if (isNewIdMapped(newId)) if (isNewIdMapped(newId))
ferror(std::string("ID already used in module: ") + std::to_string(newId)); error(std::string("ID already used in module: ") + std::to_string(newId));
msg(4, 4, std::string("map: ") + std::to_string(id) + " -> " + std::to_string(newId)); msg(4, 4, std::string("map: ") + std::to_string(id) + " -> " + std::to_string(newId));
setMapped(newId); setMapped(newId);
largestNewId = std::max(largestNewId, newId); largestNewId = std::max(largestNewId, newId);
} }
@ -235,45 +256,15 @@ std::string spirvbin_t::literalString(int word) const
literal += *bytes++; literal += *bytes++;
return literal; return literal;
} }
// Write word stream to disk, in outputDir, with same filename used to read it. void spirvbin_t::applyMap()
void spirvbin_t::write(const std::string& outputDir) const {
{ msg(3, 2, std::string("Applying map: "));
if (filename.empty())
error("missing filename"); // Map local IDs through the ID map
process(inst_fn_nop, // ignore instructions
if (outputDir.empty())
error("missing output directory");
const std::string outfile = outputDir + path_sep_char() + basename(filename);
std::ofstream fp;
msg(2, 2, std::string("writing: ") + outfile);
fp.open(outfile, std::fstream::out | std::fstream::binary);
if (fp.fail())
error(std::string("error opening file for write: ") + outfile);
for (auto word : spv) {
fp.write((char *)&word, sizeof(word));
if (fp.fail())
error(std::string("error writing file: ") + outfile);
}
// file is closed by destructor
}
void spirvbin_t::applyMap()
{
msg(3, 2, std::string("Applying map: ") + basename(filename));
// Map local IDs through the ID map
process(inst_fn_nop, // ignore instructions
[this](spv::Id& id) { [this](spv::Id& id) {
id = localId(id); id = localId(id);
assert(id != unused && id != unmapped); assert(id != unused && id != unmapped);
@ -282,13 +273,13 @@ void spirvbin_t::applyMap()
} }
// Find free IDs for anything we haven't mapped // Find free IDs for anything we haven't mapped
void spirvbin_t::mapRemainder() void spirvbin_t::mapRemainder()
{ {
msg(3, 2, std::string("Remapping remainder: ") + basename(filename)); msg(3, 2, std::string("Remapping remainder: "));
spv::Id unusedId = 1; // can't use 0: that's NoResult spv::Id unusedId = 1; // can't use 0: that's NoResult
spirword_t maxBound = 0; spirword_t maxBound = 0;
for (spv::Id id = 0; id < idMapL.size(); ++id) { for (spv::Id id = 0; id < idMapL.size(); ++id) {
if (isOldIdUnused(id)) if (isOldIdUnused(id))
@ -296,13 +287,13 @@ void spirvbin_t::mapRemainder()
// Find a new mapping for any used but unmapped IDs // Find a new mapping for any used but unmapped IDs
if (isOldIdUnmapped(id)) if (isOldIdUnmapped(id))
localId(id, unusedId = nextUnusedId(unusedId)); localId(id, unusedId = nextUnusedId(unusedId));
if (isOldIdUnmapped(id)) if (isOldIdUnmapped(id))
ferror(std::string("old ID not mapped: ") + std::to_string(id)); error(std::string("old ID not mapped: ") + std::to_string(id));
// Track max bound // Track max bound
maxBound = std::max(maxBound, localId(id) + 1); maxBound = std::max(maxBound, localId(id) + 1);
} }
bound(maxBound); // reset header ID bound to as big as it now needs to be bound(maxBound); // reset header ID bound to as big as it now needs to be
@ -323,13 +314,13 @@ void spirvbin_t::stripDebug()
}, },
op_fn_nop); op_fn_nop);
} }
void spirvbin_t::buildLocalMaps() void spirvbin_t::buildLocalMaps()
{ {
msg(2, 2, std::string("build local maps: ") + filename); msg(2, 2, std::string("build local maps: "));
mapped.clear(); mapped.clear();
idMapL.clear(); idMapL.clear();
nameMap.clear(); nameMap.clear();
fnPos.clear(); fnPos.clear();
fnPosDCE.clear(); fnPosDCE.clear();
@ -359,19 +350,19 @@ void spirvbin_t::buildLocalMaps()
} else if (opCode == spv::Op::OpFunctionCall) { } else if (opCode == spv::Op::OpFunctionCall) {
++fnCalls[asId(start + 3)]; ++fnCalls[asId(start + 3)];
} else if (opCode == spv::Op::OpEntryPoint) { } else if (opCode == spv::Op::OpEntryPoint) {
entryPoint = asId(start + 2); entryPoint = asId(start + 2);
} else if (opCode == spv::Op::OpFunction) { } else if (opCode == spv::Op::OpFunction) {
if (fnStart != 0) if (fnStart != 0)
ferror("nested function found"); error("nested function found");
fnStart = start; fnStart = start;
fnRes = asId(start + 2); fnRes = asId(start + 2);
} else if (opCode == spv::Op::OpFunctionEnd) { } else if (opCode == spv::Op::OpFunctionEnd) {
assert(fnRes != spv::NoResult); assert(fnRes != spv::NoResult);
if (fnStart == 0) if (fnStart == 0)
ferror("function end without function start"); error("function end without function start");
fnPos[fnRes] = {fnStart, start + asWordCount(start)}; fnPos[fnRes] = {fnStart, start + asWordCount(start)};
fnStart = 0; fnStart = 0;
} else if (isConstOp(opCode)) { } else if (isConstOp(opCode)) {
assert(asId(start + 2) != spv::NoResult); assert(asId(start + 2) != spv::NoResult);
typeConstPos.insert(start); typeConstPos.insert(start);
typeConstPosR[asId(start + 2)] = start; typeConstPosR[asId(start + 2)] = start;
@ -385,79 +376,45 @@ void spirvbin_t::buildLocalMaps()
}, },
[this](spv::Id& id) { localId(id, unmapped); } [this](spv::Id& id) { localId(id, unmapped); }
); );
} }
// Read word stream from disk // Validate the SPIR header
void spirvbin_t::read(const std::string& inFilename) void spirvbin_t::validate() const
{ {
std::ifstream fp; msg(2, 2, std::string("validating: "));
filename = inFilename;
if (spv.size() < header_size)
msg(2, 2, std::string("reading: ") + filename); error("file too short: ");
spv.clear(); if (magic() != spv::MagicNumber)
fp.open(filename, std::fstream::in | std::fstream::binary); error("bad magic number");
if (fp.fail()) // field 1 = version
ferror("error opening file for read: "); // field 2 = generator magic
// field 3 = result <id> bound
// Reserve space (for efficiency, not for correctness)
fp.seekg(0, fp.end); if (schemaNum() != 0)
spv.reserve(size_t(fp.tellg()) / sizeof(spirword_t)); error("bad schema, must be 0");
fp.seekg(0, fp.beg); }
while (!fp.eof()) {
spirword_t inWord;
fp.read((char *)&inWord, sizeof(inWord));
if (!fp.eof()) {
spv.push_back(inWord);
if (fp.fail())
ferror("error reading file: ");
}
}
}
// Validate the SPIR header
void spirvbin_t::validate() const
{
msg(2, 2, std::string("validating: ") + filename);
if (spv.size() < header_size)
ferror("file too short: ");
if (magic() != spv::MagicNumber)
ferror("bad magic number");
// 1 = version: TODO: print for verbose output
// 2 = generator magic: TODO: print for verbose output
// 3 = result <id> bound: TODO: print for verbose output
if (schemaNum() != 0)
ferror("bad schema, must be 0");
}
int spirvbin_t::processInstruction(int word, instfn_t instFn, idfn_t idFn) int spirvbin_t::processInstruction(int word, instfn_t instFn, idfn_t idFn)
{ {
const auto instructionStart = word; const auto instructionStart = word;
const unsigned wordCount = asWordCount(instructionStart); const unsigned wordCount = asWordCount(instructionStart);
const spv::Op opCode = asOpCode(instructionStart); const spv::Op opCode = asOpCode(instructionStart);
const int nextInst = word++ + wordCount; const int nextInst = word++ + wordCount;
if (nextInst > int(spv.size())) if (nextInst > int(spv.size()))
ferror("spir instruction terminated too early"); error("spir instruction terminated too early");
// Base for computing number of operands; will be updated as more is learned // Base for computing number of operands; will be updated as more is learned
unsigned numOperands = wordCount - 1; unsigned numOperands = wordCount - 1;
// msg(5, 4, std::string("opcode: ") + spv::InstructionDesc[opCode].opName); if (instFn(opCode, instructionStart))
return nextInst;
if (instFn(opCode, instructionStart))
return nextInst;
// Read type and result ID from instruction desc table // Read type and result ID from instruction desc table
if (spv::InstructionDesc[opCode].hasType()) { if (spv::InstructionDesc[opCode].hasType()) {
idFn(asId(word++)); idFn(asId(word++));
@ -903,13 +860,13 @@ void spirvbin_t::optLoadStore()
buildLocalMaps(); // rebuild ID mapping data buildLocalMaps(); // rebuild ID mapping data
} }
// remove bodies of uncalled functions // remove bodies of uncalled functions
void spirvbin_t::dceFuncs() void spirvbin_t::dceFuncs()
{ {
msg(3, 2, std::string("Removing Dead Functions: ") + filename); msg(3, 2, std::string("Removing Dead Functions: "));
// TODO: There are more efficient ways to do this. // TODO: There are more efficient ways to do this.
bool changed = true; bool changed = true;
while (changed) { while (changed) {
changed = false; changed = false;
@ -920,13 +877,12 @@ void spirvbin_t::dceFuncs()
continue; continue;
} }
const auto call_it = fnCalls.find(fn->first); const auto call_it = fnCalls.find(fn->first);
if (call_it == fnCalls.end() || call_it->second == 0) { if (call_it == fnCalls.end() || call_it->second == 0) {
// msg(3, 4, std::string("removing dead function: ") + std::to_string(fn->first)); changed = true;
changed = true; stripRange.push_back(fn->second);
stripRange.push_back(fn->second); fnPosDCE.insert(*fn);
fnPosDCE.insert(*fn);
// decrease counts of called functions // decrease counts of called functions
process( process(
@ -951,13 +907,13 @@ void spirvbin_t::dceFuncs()
} }
} }
// remove unused function variables + decorations // remove unused function variables + decorations
void spirvbin_t::dceVars() void spirvbin_t::dceVars()
{ {
msg(3, 2, std::string("DCE Vars: ") + basename(filename)); msg(3, 2, std::string("DCE Vars: "));
std::unordered_map<spv::Id, int> varUseCount; std::unordered_map<spv::Id, int> varUseCount;
// Count function variable use // Count function variable use
process( process(
[&](spv::Op opCode, int start) { [&](spv::Op opCode, int start) {
@ -1088,13 +1044,13 @@ spv::Id spirvbin_t::findType(const spirvbin_t::globaltypes_t& globalTypes, spv::
// Return start position in SPV of given type. error if not found. // Return start position in SPV of given type. error if not found.
int spirvbin_t::typePos(spv::Id id) const int spirvbin_t::typePos(spv::Id id) const
{ {
const auto tid_it = typeConstPosR.find(id); const auto tid_it = typeConstPosR.find(id);
if (tid_it == typeConstPosR.end()) if (tid_it == typeConstPosR.end())
ferror("type ID not found"); error("type ID not found");
return tid_it->second; return tid_it->second;
} }
// Hash types to canonical values. This can return ID collisions (it's a bit // Hash types to canonical values. This can return ID collisions (it's a bit
// inevitable): it's up to the caller to handle that gracefully. // inevitable): it's up to the caller to handle that gracefully.
@ -1168,22 +1124,22 @@ std::uint32_t spirvbin_t::hashType(int typeStart) const
for (unsigned w=3; w < wordCount; ++w) for (unsigned w=3; w < wordCount; ++w)
hash += w * spv[typeStart+w]; hash += w * spv[typeStart+w];
return hash; return hash;
} }
default: default:
ferror("unknown type opcode"); error("unknown type opcode");
return 0; return 0;
} }
} }
void spirvbin_t::mapTypeConst() void spirvbin_t::mapTypeConst()
{ {
globaltypes_t globalTypeMap; globaltypes_t globalTypeMap;
msg(3, 2, std::string("Remapping Consts & Types: ") + basename(filename)); msg(3, 2, std::string("Remapping Consts & Types: "));
static const std::uint32_t softTypeIdLimit = 3011; // small prime. TODO: get from options static const std::uint32_t softTypeIdLimit = 3011; // small prime. TODO: get from options
static const std::uint32_t firstMappedID = 8; // offset into ID space static const std::uint32_t firstMappedID = 8; // offset into ID space
for (auto& typeStart : typeConstPos) { for (auto& typeStart : typeConstPos) {
const spv::Id resId = asTypeConstId(typeStart); const spv::Id resId = asTypeConstId(typeStart);
@ -1257,19 +1213,10 @@ void spirvbin_t::remap(std::vector<std::uint32_t>& in_spv, std::uint32_t opts)
{ {
spv.swap(in_spv); spv.swap(in_spv);
remap(opts); remap(opts);
spv.swap(in_spv); spv.swap(in_spv);
} }
// remap from a disk file } // namespace SPV
void spirvbin_t::remap(const std::string& file, const std::string& outputDir,
std::uint32_t opts) #endif // defined (use_cpp11)
{
read(file);
remap(opts);
write(outputDir);
}
} // namespace SPV
#endif // defined (use_cpp11)

View File

@ -1,281 +1,286 @@
//
#ifndef SPIRVREMAPPER_H //Copyright (C) 2015 LunarG, Inc.
#define SPIRVREMAPPER_H //
//All rights reserved.
#include <string> //
#include <vector> //Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
namespace spv { //are met:
//
// MSVC defines __cplusplus as an older value, even when it supports almost all of 11. // Redistributions of source code must retain the above copyright
// We handle that here by making our own symbol. // notice, this list of conditions and the following disclaimer.
#if __cplusplus >= 201103L || _MSC_VER >= 1800 //
# define use_cpp11 1 // Redistributions in binary form must reproduce the above
#endif // copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
class spirvbin_base_t // with the distribution.
{ //
public: // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
enum Options { // contributors may be used to endorse or promote products derived
NONE = 0, // from this software without specific prior written permission.
STRIP = (1<<0), //
MAP_TYPES = (1<<1), //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
MAP_NAMES = (1<<2), //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
MAP_FUNCS = (1<<3), //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
DCE_FUNCS = (1<<4), //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
DCE_VARS = (1<<5), //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
DCE_TYPES = (1<<6), //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
OPT_LOADSTORE = (1<<7), //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
OPT_FWD_LS = (1<<8), // EXPERIMENTAL: PRODUCES INVALID SCHEMA-0 SPIRV //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
MAP_ALL = (MAP_TYPES | MAP_NAMES | MAP_FUNCS), //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
DCE_ALL = (DCE_FUNCS | DCE_VARS | DCE_TYPES), //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
OPT_ALL = (OPT_LOADSTORE), //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
ALL_BUT_STRIP = (MAP_ALL | DCE_ALL | OPT_ALL), //
DO_EVERYTHING = (STRIP | ALL_BUT_STRIP)
}; #ifndef SPIRVREMAPPER_H
#define SPIRVREMAPPER_H
// OS dependent path separator (avoiding boost::filesystem dependency)
#if defined(_WIN32) #include <string>
static const char path_sep_char() { return '\\'; } #include <vector>
#else
static const char path_sep_char() { return '/'; } namespace spv {
#endif
// MSVC defines __cplusplus as an older value, even when it supports almost all of 11.
// Poor man's basename, to avoid external dependencies // We handle that here by making our own symbol.
static const std::string basename(const std::string& filename); #if __cplusplus >= 201103L || _MSC_VER >= 1800
}; # define use_cpp11 1
#endif
} // namespace SPV
class spirvbin_base_t
#if !defined (use_cpp11) {
#include <stdio.h> public:
enum Options {
namespace spv { NONE = 0,
STRIP = (1<<0),
class spirvbin_t : public spirvbin_base_t MAP_TYPES = (1<<1),
{ MAP_NAMES = (1<<2),
public: MAP_FUNCS = (1<<3),
spirvbin_t(int verbose = 0) { } DCE_FUNCS = (1<<4),
DCE_VARS = (1<<5),
void remap(std::vector<unsigned int>& spv, unsigned int opts = 0) DCE_TYPES = (1<<6),
{ OPT_LOADSTORE = (1<<7),
printf("Tool not compiled for C++11, which is required for SPIR-V remapping.\n"); OPT_FWD_LS = (1<<8), // EXPERIMENTAL: PRODUCES INVALID SCHEMA-0 SPIRV
} MAP_ALL = (MAP_TYPES | MAP_NAMES | MAP_FUNCS),
DCE_ALL = (DCE_FUNCS | DCE_VARS | DCE_TYPES),
void remap(const std::string& filename, const std::string& outputDir, unsigned int opts = 0) OPT_ALL = (OPT_LOADSTORE),
{
printf("Tool not compiled for C++11, which is required for SPIR-V remapping.\n"); ALL_BUT_STRIP = (MAP_ALL | DCE_ALL | OPT_ALL),
} DO_EVERYTHING = (STRIP | ALL_BUT_STRIP)
}; };
};
} // namespace SPV
} // namespace SPV
#else // defined (use_cpp11)
#if !defined (use_cpp11)
#include <functional> #include <stdio.h>
#include <cstdint>
#include <unordered_map> namespace spv {
#include <unordered_set> class spirvbin_t : public spirvbin_base_t
#include <map> {
#include <set> public:
#include <cassert> spirvbin_t(int verbose = 0) { }
#include "../../glslang/SPIRV/spirv.h" void remap(std::vector<unsigned int>& spv, unsigned int opts = 0)
#include "../../glslang/SPIRV/spvIR.h" {
printf("Tool not compiled for C++11, which is required for SPIR-V remapping.\n");
namespace spv { }
};
// class to hold SPIRV binary data for remapping, DCE, and debug stripping
class spirvbin_t : public spirvbin_base_t } // namespace SPV
{
public: #else // defined (use_cpp11)
spirvbin_t(int verbose = 0) : entryPoint(spv::NoResult), largestNewId(0), verbose(verbose) { }
#include <functional>
// remap on an existing binary in memory #include <cstdint>
void remap(std::vector<std::uint32_t>& spv, std::uint32_t opts = Options::DO_EVERYTHING); #include <unordered_map>
#include <unordered_set>
// load binary from disk file, and remap that. #include <map>
void remap(const std::string& filename, const std::string& outputDir, #include <set>
std::uint32_t opts = Options::DO_EVERYTHING); #include <cassert>
// Type for error/log handler functions #include "../../glslang/SPIRV/spirv.h"
typedef std::function<void(const std::string&)> errorfn_t; #include "../../glslang/SPIRV/spvIR.h"
typedef std::function<void(const std::string&)> logfn_t;
namespace spv {
// Register error/log handling functions (can be lambda fn / functor / etc)
static void registerErrorHandler(errorfn_t handler) { errorHandler = handler; } // class to hold SPIRV binary data for remapping, DCE, and debug stripping
static void registerLogHandler(logfn_t handler) { logHandler = handler; } class spirvbin_t : public spirvbin_base_t
{
protected: public:
// This can be overridden to provide other message behavior if needed spirvbin_t(int verbose = 0) : entryPoint(spv::NoResult), largestNewId(0), verbose(verbose) { }
virtual void msg(int minVerbosity, int indent, const std::string& txt) const;
// remap on an existing binary in memory
private: void remap(std::vector<std::uint32_t>& spv, std::uint32_t opts = Options::DO_EVERYTHING);
// write SPV to given directory using filename passed to remap(filename...)
void write(const std::string& outputDir) const; // Type for error/log handler functions
typedef std::function<void(const std::string&)> errorfn_t;
// Local to global, or global to local ID map typedef std::function<void(const std::string&)> logfn_t;
typedef std::unordered_map<spv::Id, spv::Id> idmap_t;
typedef std::unordered_set<spv::Id> idset_t; // Register error/log handling functions (can be lambda fn / functor / etc)
static void registerErrorHandler(errorfn_t handler) { errorHandler = handler; }
void read(const std::string& filename); // read SPV from disk file static void registerLogHandler(logfn_t handler) { logHandler = handler; }
void remap(std::uint32_t opts = Options::DO_EVERYTHING);
protected:
// Map of names to IDs // This can be overridden to provide other message behavior if needed
typedef std::unordered_map<std::string, spv::Id> namemap_t; virtual void msg(int minVerbosity, int indent, const std::string& txt) const;
typedef std::uint32_t spirword_t; private:
// Local to global, or global to local ID map
typedef std::pair<int, int> range_t; typedef std::unordered_map<spv::Id, spv::Id> idmap_t;
typedef std::function<void(spv::Id&)> idfn_t; typedef std::unordered_set<spv::Id> idset_t;
typedef std::function<bool(spv::Op, int start)> instfn_t;
void remap(std::uint32_t opts = Options::DO_EVERYTHING);
// Special Values for ID map:
static const spv::Id unmapped; // unchanged from default value // Map of names to IDs
static const spv::Id unused; // unused ID typedef std::unordered_map<std::string, spv::Id> namemap_t;
static const int header_size; // SPIR header = 5 words
typedef std::uint32_t spirword_t;
class id_iterator_t;
typedef std::pair<int, int> range_t;
// For mapping type entries between different shaders typedef std::function<void(spv::Id&)> idfn_t;
typedef std::vector<spirword_t> typeentry_t; typedef std::function<bool(spv::Op, int start)> instfn_t;
typedef std::map<spv::Id, typeentry_t> globaltypes_t;
// Special Values for ID map:
// A set that preserves position order, and a reverse map static const spv::Id unmapped; // unchanged from default value
typedef std::set<int> posmap_t; static const spv::Id unused; // unused ID
typedef std::unordered_map<spv::Id, int> posmap_rev_t; static const int header_size; // SPIR header = 5 words
// handle error class id_iterator_t;
void error(const std::string& txt) const { errorHandler(txt); }
// handle error with our filename appended to the string // For mapping type entries between different shaders
void ferror(const std::string& txt) const { typedef std::vector<spirword_t> typeentry_t;
error(std::string("\nERROR processing file ") + filename + ":\n" + txt); typedef std::map<spv::Id, typeentry_t> globaltypes_t;
}
// A set that preserves position order, and a reverse map
bool isConstOp(spv::Op opCode) const; typedef std::set<int> posmap_t;
bool isTypeOp(spv::Op opCode) const; typedef std::unordered_map<spv::Id, int> posmap_rev_t;
bool isStripOp(spv::Op opCode) const;
bool isFlowCtrlOpen(spv::Op opCode) const; // handle error
bool isFlowCtrlClose(spv::Op opCode) const; void error(const std::string& txt) const { errorHandler(txt); }
range_t literalRange(spv::Op opCode) const;
range_t typeRange(spv::Op opCode) const; bool isConstOp(spv::Op opCode) const;
range_t constRange(spv::Op opCode) const; bool isTypeOp(spv::Op opCode) const;
bool isStripOp(spv::Op opCode) const;
spv::Id& asId(int word) { return spv[word]; } bool isFlowCtrlOpen(spv::Op opCode) const;
const spv::Id& asId(int word) const { return spv[word]; } bool isFlowCtrlClose(spv::Op opCode) const;
spv::Op asOpCode(int word) const { return opOpCode(spv[word]); } range_t literalRange(spv::Op opCode) const;
std::uint32_t asOpCodeHash(int word); range_t typeRange(spv::Op opCode) const;
spv::Decoration asDecoration(int word) const { return spv::Decoration(spv[word]); } range_t constRange(spv::Op opCode) const;
unsigned asWordCount(int word) const { return opWordCount(spv[word]); }
spv::Id asTypeConstId(int word) const { return asId(word + (isTypeOp(asOpCode(word)) ? 1 : 2)); } spv::Id& asId(int word) { return spv[word]; }
int typePos(spv::Id id) const; const spv::Id& asId(int word) const { return spv[word]; }
spv::Op asOpCode(int word) const { return opOpCode(spv[word]); }
static unsigned opWordCount(spirword_t data) { return data >> spv::WordCountShift; } std::uint32_t asOpCodeHash(int word);
static spv::Op opOpCode(spirword_t data) { return spv::Op(data & spv::OpCodeMask); } spv::Decoration asDecoration(int word) const { return spv::Decoration(spv[word]); }
unsigned asWordCount(int word) const { return opWordCount(spv[word]); }
// Header access & set methods spv::Id asTypeConstId(int word) const { return asId(word + (isTypeOp(asOpCode(word)) ? 1 : 2)); }
spirword_t magic() const { return spv[0]; } // return magic number int typePos(spv::Id id) const;
spirword_t bound() const { return spv[3]; } // return Id bound from header
spirword_t bound(spirword_t b) { return spv[3] = b; }; static unsigned opWordCount(spirword_t data) { return data >> spv::WordCountShift; }
spirword_t genmagic() const { return spv[2]; } // generator magic static spv::Op opOpCode(spirword_t data) { return spv::Op(data & spv::OpCodeMask); }
spirword_t genmagic(spirword_t m) { return spv[2] = m; }
spirword_t schemaNum() const { return spv[4]; } // schema number from header // Header access & set methods
spirword_t magic() const { return spv[0]; } // return magic number
// Mapping fns: get spirword_t bound() const { return spv[3]; } // return Id bound from header
spv::Id localId(spv::Id id) const { return idMapL[id]; } spirword_t bound(spirword_t b) { return spv[3] = b; };
spirword_t genmagic() const { return spv[2]; } // generator magic
// Mapping fns: set spirword_t genmagic(spirword_t m) { return spv[2] = m; }
inline spv::Id localId(spv::Id id, spv::Id newId); spirword_t schemaNum() const { return spv[4]; } // schema number from header
void countIds(spv::Id id);
// Mapping fns: get
// Return next unused new local ID. spv::Id localId(spv::Id id) const { return idMapL[id]; }
// NOTE: boost::dynamic_bitset would be more efficient due to find_next(),
// which std::vector<bool> doens't have. // Mapping fns: set
inline spv::Id nextUnusedId(spv::Id id); inline spv::Id localId(spv::Id id, spv::Id newId);
void countIds(spv::Id id);
void buildLocalMaps();
std::string literalString(int word) const; // Return literal as a std::string // Return next unused new local ID.
int literalStringWords(const std::string& str) const { return (int(str.size())+4)/4; } // NOTE: boost::dynamic_bitset would be more efficient due to find_next(),
// which std::vector<bool> doens't have.
bool isNewIdMapped(spv::Id newId) const { return isMapped(newId); } inline spv::Id nextUnusedId(spv::Id id);
bool isOldIdUnmapped(spv::Id oldId) const { return localId(oldId) == unmapped; }
bool isOldIdUnused(spv::Id oldId) const { return localId(oldId) == unused; } void buildLocalMaps();
bool isOldIdMapped(spv::Id oldId) const { return !isOldIdUnused(oldId) && !isOldIdUnmapped(oldId); } std::string literalString(int word) const; // Return literal as a std::string
bool isFunction(spv::Id oldId) const { return fnPos.find(oldId) != fnPos.end(); } int literalStringWords(const std::string& str) const { return (int(str.size())+4)/4; }
// bool matchType(const globaltypes_t& globalTypes, spv::Id lt, spv::Id gt) const; bool isNewIdMapped(spv::Id newId) const { return isMapped(newId); }
// spv::Id findType(const globaltypes_t& globalTypes, spv::Id lt) const; bool isOldIdUnmapped(spv::Id oldId) const { return localId(oldId) == unmapped; }
std::uint32_t hashType(int typeStart) const; bool isOldIdUnused(spv::Id oldId) const { return localId(oldId) == unused; }
bool isOldIdMapped(spv::Id oldId) const { return !isOldIdUnused(oldId) && !isOldIdUnmapped(oldId); }
spirvbin_t& process(instfn_t, idfn_t, int begin = 0, int end = 0); bool isFunction(spv::Id oldId) const { return fnPos.find(oldId) != fnPos.end(); }
int processInstruction(int word, instfn_t, idfn_t);
// bool matchType(const globaltypes_t& globalTypes, spv::Id lt, spv::Id gt) const;
void validate() const; // spv::Id findType(const globaltypes_t& globalTypes, spv::Id lt) const;
void mapTypeConst(); std::uint32_t hashType(int typeStart) const;
void mapFnBodies();
void optLoadStore(); spirvbin_t& process(instfn_t, idfn_t, int begin = 0, int end = 0);
void dceFuncs(); int processInstruction(int word, instfn_t, idfn_t);
void dceVars();
void dceTypes(); void validate() const;
void mapNames(); void mapTypeConst();
void foldIds(); // fold IDs to smallest space void mapFnBodies();
void forwardLoadStores(); // load store forwarding (EXPERIMENTAL) void optLoadStore();
void offsetIds(); // create relative offset IDs void dceFuncs();
void dceVars();
void applyMap(); // remap per local name map void dceTypes();
void mapRemainder(); // map any IDs we haven't touched yet void mapNames();
void stripDebug(); // strip debug info void foldIds(); // fold IDs to smallest space
void strip(); // remove debug symbols void forwardLoadStores(); // load store forwarding (EXPERIMENTAL)
void offsetIds(); // create relative offset IDs
std::vector<spirword_t> spv; // SPIR words
std::string filename; // the file this came from void applyMap(); // remap per local name map
void mapRemainder(); // map any IDs we haven't touched yet
namemap_t nameMap; // ID names from OpName void stripDebug(); // strip debug info
void strip(); // remove debug symbols
// Since we want to also do binary ops, we can't use std::vector<bool>. we could use
// boost::dynamic_bitset, but we're trying to avoid a boost dependency. std::vector<spirword_t> spv; // SPIR words
typedef std::uint64_t bits_t;
std::vector<bits_t> mapped; // which new IDs have been mapped namemap_t nameMap; // ID names from OpName
static const int mBits = sizeof(bits_t) * 4;
// Since we want to also do binary ops, we can't use std::vector<bool>. we could use
bool isMapped(spv::Id id) const { return id < maxMappedId() && ((mapped[id/mBits] & (1LL<<(id%mBits))) != 0); } // boost::dynamic_bitset, but we're trying to avoid a boost dependency.
void setMapped(spv::Id id) { resizeMapped(id); mapped[id/mBits] |= (1LL<<(id%mBits)); } typedef std::uint64_t bits_t;
void resizeMapped(spv::Id id) { if (id >= maxMappedId()) mapped.resize(id/mBits+1, 0); } std::vector<bits_t> mapped; // which new IDs have been mapped
size_t maxMappedId() const { return mapped.size() * mBits; } static const int mBits = sizeof(bits_t) * 4;
// Add a strip range for a given instruction starting at 'start' bool isMapped(spv::Id id) const { return id < maxMappedId() && ((mapped[id/mBits] & (1LL<<(id%mBits))) != 0); }
// Note: avoiding brace initializers to please older versions os MSVC. void setMapped(spv::Id id) { resizeMapped(id); mapped[id/mBits] |= (1LL<<(id%mBits)); }
void stripInst(int start) { stripRange.push_back(std::pair<unsigned, unsigned>(start, start + asWordCount(start))); } void resizeMapped(spv::Id id) { if (id >= maxMappedId()) mapped.resize(id/mBits+1, 0); }
size_t maxMappedId() const { return mapped.size() * mBits; }
// Function start and end. use unordered_map because we'll have
// many fewer functions than IDs. // Add a strip range for a given instruction starting at 'start'
std::unordered_map<spv::Id, std::pair<int, int>> fnPos; // Note: avoiding brace initializers to please older versions os MSVC.
std::unordered_map<spv::Id, std::pair<int, int>> fnPosDCE; // deleted functions void stripInst(int start) { stripRange.push_back(std::pair<unsigned, unsigned>(start, start + asWordCount(start))); }
// Which functions are called, anywhere in the module, with a call count // Function start and end. use unordered_map because we'll have
std::unordered_map<spv::Id, int> fnCalls; // many fewer functions than IDs.
std::unordered_map<spv::Id, std::pair<int, int>> fnPos;
posmap_t typeConstPos; // word positions that define types & consts (ordered) std::unordered_map<spv::Id, std::pair<int, int>> fnPosDCE; // deleted functions
posmap_rev_t typeConstPosR; // reverse map from IDs to positions
// Which functions are called, anywhere in the module, with a call count
std::vector<spv::Id> idMapL; // ID {M}ap from {L}ocal to {G}lobal IDs std::unordered_map<spv::Id, int> fnCalls;
spv::Id entryPoint; // module entry point posmap_t typeConstPos; // word positions that define types & consts (ordered)
spv::Id largestNewId; // biggest new ID we have mapped anything to posmap_rev_t typeConstPosR; // reverse map from IDs to positions
// Sections of the binary to strip, given as [begin,end) std::vector<spv::Id> idMapL; // ID {M}ap from {L}ocal to {G}lobal IDs
std::vector<std::pair<unsigned, unsigned>> stripRange;
spv::Id entryPoint; // module entry point
// processing options: spv::Id largestNewId; // biggest new ID we have mapped anything to
std::uint32_t options;
int verbose; // verbosity level // Sections of the binary to strip, given as [begin,end)
std::vector<std::pair<unsigned, unsigned>> stripRange;
static errorfn_t errorHandler;
static logfn_t logHandler; // processing options:
}; std::uint32_t options;
int verbose; // verbosity level
} // namespace SPV
static errorfn_t errorHandler;
#endif // defined (use_cpp11) static logfn_t logHandler;
#endif // SPIRVREMAPPER_H };
} // namespace SPV
#endif // defined (use_cpp11)
#endif // SPIRVREMAPPER_H

View File

@ -44,7 +44,6 @@
#include "../SPIRV/GLSL450Lib.h" #include "../SPIRV/GLSL450Lib.h"
#include "../SPIRV/doc.h" #include "../SPIRV/doc.h"
#include "../SPIRV/disassemble.h" #include "../SPIRV/disassemble.h"
#include "../SPIRV/SPVRemapper.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
@ -72,8 +71,6 @@ enum TOptions {
EOptionSpv = 0x0800, EOptionSpv = 0x0800,
EOptionHumanReadableSpv = 0x1000, EOptionHumanReadableSpv = 0x1000,
EOptionDefaultDesktop = 0x2000, EOptionDefaultDesktop = 0x2000,
EOptionCanonicalizeSpv = 0x4000,
EOptionStripSpv = 0x8000,
}; };
// //
@ -484,17 +481,11 @@ bool ProcessArguments(int argc, char* argv[])
for (; argc >= 1; argc--, argv++) { for (; argc >= 1; argc--, argv++) {
Work[argc] = 0; Work[argc] = 0;
if (argv[0][0] == '-') { if (argv[0][0] == '-') {
const char optLetter = argv[0][1]; switch (argv[0][1]) {
case 'H':
switch (optLetter) { Options |= EOptionHumanReadableSpv;
case 'S': // fall through to -V // fall through to -V
case 'C': // fall through to -V
case 'H': // fall through to -V
case 'V': case 'V':
if (optLetter == 'H') Options |= EOptionHumanReadableSpv;
if (optLetter == 'S') Options |= EOptionStripSpv;
if (optLetter == 'C') Options |= EOptionCanonicalizeSpv;
Options |= EOptionSpv; Options |= EOptionSpv;
Options |= EOptionLinkProgram; Options |= EOptionLinkProgram;
break; break;
@ -669,17 +660,7 @@ void CompileAndLinkShaders()
case EShLangCompute: name = "comp"; break; case EShLangCompute: name = "comp"; break;
default: name = "unknown"; break; default: name = "unknown"; break;
} }
if (Options & (EOptionCanonicalizeSpv | EOptionStripSpv)) {
const unsigned int remapOpts =
((Options & EOptionCanonicalizeSpv) ? (spv::spirvbin_t::ALL_BUT_STRIP) : 0) |
((Options & EOptionStripSpv) ? (spv::spirvbin_t::STRIP) : 0);
spv::Parameterize();
spv::spirvbin_t().remap(spirv, remapOpts);
}
glslang::OutputSpv(spirv, name); glslang::OutputSpv(spirv, name);
if (Options & EOptionHumanReadableSpv) { if (Options & EOptionHumanReadableSpv) {
spv::Parameterize(); spv::Parameterize();
GLSL_STD_450::GetDebugNames(GlslStd450DebugNames); GLSL_STD_450::GetDebugNames(GlslStd450DebugNames);
@ -886,8 +867,6 @@ void usage()
"To get other information, use one of the following options:\n" "To get other information, use one of the following options:\n"
"(Each option must be specified separately, but can go anywhere in the command line.)\n" "(Each option must be specified separately, but can go anywhere in the command line.)\n"
" -V create SPIR-V in file <stage>.spv\n" " -V create SPIR-V in file <stage>.spv\n"
" -C canonicalize generated SPIR-V: turns on -V\n"
" -S debug-strip SPIR-V: turns on -V\n"
" -H print human readable form of SPIR-V; turns on -V\n" " -H print human readable form of SPIR-V; turns on -V\n"
" -c configuration dump; use to create default configuration file (redirect to a .conf file)\n" " -c configuration dump; use to create default configuration file (redirect to a .conf file)\n"
" -d default to desktop (#version 110) when there is no version in the shader (default is ES version 100)\n" " -d default to desktop (#version 110) when there is no version in the shader (default is ES version 100)\n"