diff --git a/SPIRV/SPVRemapper.cpp b/SPIRV/SPVRemapper.cpp index fdfbeb90c..61937f5ba 100644 --- a/SPIRV/SPVRemapper.cpp +++ b/SPIRV/SPVRemapper.cpp @@ -160,15 +160,30 @@ namespace spv { } // Is this an opcode we should remove when using --strip? - bool spirvbin_t::isStripOp(spv::Op opCode) const + bool spirvbin_t::isStripOp(spv::Op opCode, unsigned start) const { switch (opCode) { case spv::OpSource: case spv::OpSourceExtension: case spv::OpName: case spv::OpMemberName: - case spv::OpLine: return true; - default: return false; + case spv::OpLine : + { + const spv::Id target = asId(start + 1); + const std::string name = literalString(start + 2); + + std::vector::const_iterator it; + for (it = stripWhiteList.begin(); it < stripWhiteList.end(); it++) + { + if (name.find(*it) != std::string::npos) { + return false; + } + } + + return true; + } + default : + return false; } } @@ -372,7 +387,7 @@ namespace spv { process( [&](spv::Op opCode, unsigned start) { // remember opcodes we want to strip later - if (isStripOp(opCode)) + if (isStripOp(opCode, start)) stripInst(start); return true; }, @@ -1494,8 +1509,10 @@ namespace spv { } // remap from a memory image - void spirvbin_t::remap(std::vector& in_spv, std::uint32_t opts) + void spirvbin_t::remap(std::vector& in_spv, const std::vector& whiteListStrings, + std::uint32_t opts) { + stripWhiteList = whiteListStrings; spv.swap(in_spv); remap(opts); spv.swap(in_spv); diff --git a/SPIRV/SPVRemapper.h b/SPIRV/SPVRemapper.h index d6b9c346d..2dfacd733 100644 --- a/SPIRV/SPVRemapper.h +++ b/SPIRV/SPVRemapper.h @@ -118,7 +118,8 @@ public: virtual ~spirvbin_t() { } // remap on an existing binary in memory - void remap(std::vector& spv, std::uint32_t opts = DO_EVERYTHING); + void remap(std::vector& spv, const std::vector& whiteListStrings, + std::uint32_t opts = DO_EVERYTHING); // Type for error/log handler functions typedef std::function errorfn_t; @@ -180,6 +181,8 @@ private: unsigned typeSizeInWords(spv::Id id) const; unsigned idTypeSizeInWords(spv::Id id) const; + bool isStripOp(spv::Op opCode, unsigned start) const; + spv::Id& asId(unsigned word) { return spv[word]; } const spv::Id& asId(unsigned word) const { return spv[word]; } spv::Op asOpCode(unsigned word) const { return opOpCode(spv[word]); } @@ -249,6 +252,8 @@ private: std::vector spv; // SPIR words + std::vector stripWhiteList; + namemap_t nameMap; // ID names from OpName // Since we want to also do binary ops, we can't use std::vector. we could use diff --git a/StandAlone/spirv-remap.cpp b/StandAlone/spirv-remap.cpp index 48878c3af..15c3ac513 100644 --- a/StandAlone/spirv-remap.cpp +++ b/StandAlone/spirv-remap.cpp @@ -105,6 +105,32 @@ namespace { } } + // Read strings from a file + void read(std::vector& strings, const std::string& inFilename, int verbosity) + { + std::ifstream fp; + + if (verbosity > 0) + logHandler(std::string(" reading: ") + inFilename); + + strings.clear(); + fp.open(inFilename, std::fstream::in); + + if (fp.fail()) + errHandler("error opening file for read: "); + + std::string line; + while (std::getline(fp, line)) + { + // Ignore empty lines and lines starting with the comment marker '#'. + if (line.length() == 0 || line[0] == '#') { + continue; + } + + strings.push_back(line); + } + } + void write(std::vector& spv, const std::string& outFile, int verbosity) { if (outFile.empty()) @@ -144,6 +170,7 @@ namespace { << " [--dce (all|types|funcs)]" << " [--opt (all|loadstore)]" << " [--strip-all | --strip all | -s]" + << " [--strip-white-list]" << " [--do-everything]" << " --input | -i file1 [file2...] --output|-o DESTDIR" << std::endl; @@ -156,16 +183,18 @@ namespace { // grind through each SPIR in turn void execute(const std::vector& inputFile, const std::string& outputDir, - int opts, int verbosity) + const std::string& whiteListFile, int opts, int verbosity) { + std::vector whiteListStrings; + if(!whiteListFile.empty()) + read(whiteListStrings, whiteListFile, verbosity); + for (auto it = inputFile.cbegin(); it != inputFile.cend(); ++it) { const std::string &filename = *it; std::vector spv; read(spv, filename, verbosity); - spv::spirvbin_t(verbosity).remap(spv, opts); - + spv::spirvbin_t(verbosity).remap(spv, whiteListStrings, opts); const std::string outfile = outputDir + path_sep_char() + basename(filename); - write(spv, outfile, verbosity); } @@ -176,6 +205,7 @@ namespace { // Parse command line options void parseCmdLine(int argc, char** argv, std::vector& inputFile, std::string& outputDir, + std::string& stripWhiteListFile, int& options, int& verbosity) { @@ -245,6 +275,9 @@ namespace { options = options | spv::spirvbin_t::STRIP; ++a; } + } else if (arg == "--strip-white-list") { + ++a; + stripWhiteListFile = argv[a++]; } else if (arg == "--dce") { // Parse comma (or colon, etc) separated list of things to dce ++a; @@ -315,6 +348,7 @@ int main(int argc, char** argv) { std::vector inputFile; std::string outputDir; + std::string whiteListFile; int opts; int verbosity; @@ -329,13 +363,13 @@ int main(int argc, char** argv) if (argc < 2) usage(argv[0]); - parseCmdLine(argc, argv, inputFile, outputDir, opts, verbosity); + parseCmdLine(argc, argv, inputFile, outputDir, whiteListFile, opts, verbosity); if (outputDir.empty()) usage(argv[0], "Output directory required"); // Main operations: read, remap, and write. - execute(inputFile, outputDir, opts, verbosity); + execute(inputFile, outputDir, whiteListFile, opts, verbosity); // If we get here, everything went OK! Nothing more to be done. } diff --git a/gtests/TestFixture.h b/gtests/TestFixture.h index 2b057dcb0..5b66dece7 100644 --- a/gtests/TestFixture.h +++ b/gtests/TestFixture.h @@ -367,11 +367,12 @@ public: if (success && (controls & EShMsgSpvRules)) { spv::SpvBuildLogger logger; + std::vector whiteListStrings; std::vector spirv_binary; glslang::GlslangToSpv(*program.getIntermediate(stage), spirv_binary, &logger, &options()); - spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, remapOptions); + spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, whiteListStrings, remapOptions); std::ostringstream disassembly_stream; spv::Parameterize(); @@ -394,9 +395,9 @@ public: { if ((controls & EShMsgSpvRules)) { std::vector spirv_binary(code); // scratch copy + std::vector whiteListStrings; + spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, whiteListStrings, remapOptions); - spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, remapOptions); - std::ostringstream disassembly_stream; spv::Parameterize(); spv::Disassemble(disassembly_stream, spirv_binary);