diff.cpp 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021
  1. // Copyright (c) 2022 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/diff/diff.h"
  15. #include "source/diff/lcs.h"
  16. #include "source/disassemble.h"
  17. #include "source/ext_inst.h"
  18. #include "source/latest_version_spirv_header.h"
  19. #include "source/print.h"
  20. #include "spirv-tools/libspirv.hpp"
  21. namespace spvtools {
  22. namespace diff {
  23. namespace {
  24. // A map from an id to the instruction that defines it.
  25. using IdToInstructionMap = std::vector<const opt::Instruction*>;
  26. // A map from an id to the instructions that decorate it, or name it, etc.
  27. using IdToInfoMap = std::vector<std::vector<const opt::Instruction*>>;
  28. // A map from an instruction to another, used for instructions without id.
  29. using InstructionToInstructionMap =
  30. std::unordered_map<const opt::Instruction*, const opt::Instruction*>;
  31. // A flat list of instructions in a function for easier iteration.
  32. using InstructionList = std::vector<const opt::Instruction*>;
  33. // A map from a function to its list of instructions.
  34. using FunctionInstMap = std::map<uint32_t, InstructionList>;
  35. // A list of ids with some similar property, for example functions with the same
  36. // name.
  37. using IdGroup = std::vector<uint32_t>;
  38. // A map of names to ids with the same name. This is an ordered map so
  39. // different implementations produce identical results.
  40. using IdGroupMapByName = std::map<std::string, IdGroup>;
  41. using IdGroupMapByTypeId = std::map<uint32_t, IdGroup>;
  42. using IdGroupMapByOp = std::map<spv::Op, IdGroup>;
  43. using IdGroupMapByStorageClass = std::map<spv::StorageClass, IdGroup>;
  44. // A set of potential id mappings that haven't been resolved yet. Any id in src
  45. // may map in any id in dst. Note that ids are added in the same order as they
  46. // appear in src and dst to facilitate matching dependent instructions. For
  47. // example, this guarantees that when matching OpTypeVector, the basic type of
  48. // the vector is already (potentially) matched.
  49. struct PotentialIdMap {
  50. std::vector<uint32_t> src_ids;
  51. std::vector<uint32_t> dst_ids;
  52. };
  53. void CompactIds(std::vector<uint32_t>& ids) {
  54. size_t write_index = 0;
  55. for (size_t i = 0; i < ids.size(); ++i) {
  56. if (ids[i] != 0) {
  57. ids[write_index++] = ids[i];
  58. }
  59. }
  60. ids.resize(write_index);
  61. }
  62. // A mapping from ids in one module to ids in the other.
  63. //
  64. // Differ contains two of these, for src->dst and dst->src.
  65. class IdMap {
  66. public:
  67. IdMap(size_t id_bound) { id_map_.resize(id_bound, 0); }
  68. void MapIds(uint32_t from, uint32_t to) {
  69. assert(from != 0);
  70. assert(to != 0);
  71. assert(from < id_map_.size());
  72. assert(id_map_[from] == 0);
  73. id_map_[from] = to;
  74. }
  75. uint32_t MappedId(uint32_t from) const {
  76. assert(from != 0);
  77. return from < id_map_.size() ? id_map_[from] : 0;
  78. }
  79. const opt::Instruction* MappedInst(const opt::Instruction* from_inst) const {
  80. assert(from_inst != nullptr);
  81. assert(!from_inst->HasResultId());
  82. auto mapped = inst_map_.find(from_inst);
  83. if (mapped == inst_map_.end()) {
  84. return nullptr;
  85. }
  86. return mapped->second;
  87. }
  88. bool IsMapped(uint32_t from) const {
  89. assert(from != 0);
  90. return from < id_map_.size() && id_map_[from] != 0;
  91. }
  92. bool IsMapped(const opt::Instruction* from_inst) const {
  93. assert(from_inst != nullptr);
  94. assert(!from_inst->HasResultId());
  95. return inst_map_.find(from_inst) != inst_map_.end();
  96. }
  97. // Some instructions don't have result ids. Those are mapped by pointer.
  98. void MapInsts(const opt::Instruction* from_inst,
  99. const opt::Instruction* to_inst) {
  100. assert(from_inst != nullptr);
  101. assert(to_inst != nullptr);
  102. assert(inst_map_.find(from_inst) == inst_map_.end());
  103. inst_map_[from_inst] = to_inst;
  104. }
  105. uint32_t IdBound() const { return static_cast<uint32_t>(id_map_.size()); }
  106. // Generate a fresh id in this mapping's domain.
  107. uint32_t MakeFreshId() {
  108. id_map_.push_back(0);
  109. return static_cast<uint32_t>(id_map_.size()) - 1;
  110. }
  111. private:
  112. // Given an id, returns the corresponding id in the other module, or 0 if not
  113. // matched yet.
  114. std::vector<uint32_t> id_map_;
  115. // Same for instructions that don't have an id.
  116. InstructionToInstructionMap inst_map_;
  117. };
  118. // Two way mapping of ids.
  119. class SrcDstIdMap {
  120. public:
  121. SrcDstIdMap(size_t src_id_bound, size_t dst_id_bound)
  122. : src_to_dst_(src_id_bound), dst_to_src_(dst_id_bound) {}
  123. void MapIds(uint32_t src, uint32_t dst) {
  124. src_to_dst_.MapIds(src, dst);
  125. dst_to_src_.MapIds(dst, src);
  126. }
  127. uint32_t MappedDstId(uint32_t src) {
  128. uint32_t dst = src_to_dst_.MappedId(src);
  129. assert(dst == 0 || dst_to_src_.MappedId(dst) == src);
  130. return dst;
  131. }
  132. uint32_t MappedSrcId(uint32_t dst) {
  133. uint32_t src = dst_to_src_.MappedId(dst);
  134. assert(src == 0 || src_to_dst_.MappedId(src) == dst);
  135. return src;
  136. }
  137. bool IsSrcMapped(uint32_t src) { return src_to_dst_.IsMapped(src); }
  138. bool IsDstMapped(uint32_t dst) { return dst_to_src_.IsMapped(dst); }
  139. bool IsDstMapped(const opt::Instruction* dst_inst) {
  140. return dst_to_src_.IsMapped(dst_inst);
  141. }
  142. // Map any ids in src and dst that have not been mapped to new ids in dst and
  143. // src respectively. Use src_insn_defined and dst_insn_defined to ignore ids
  144. // that are simply never defined. (Since we assume the inputs are valid
  145. // SPIR-V, this implies they are also never used.)
  146. void MapUnmatchedIds(std::function<bool(uint32_t)> src_insn_defined,
  147. std::function<bool(uint32_t)> dst_insn_defined);
  148. // Some instructions don't have result ids. Those are mapped by pointer.
  149. void MapInsts(const opt::Instruction* src_inst,
  150. const opt::Instruction* dst_inst) {
  151. assert(src_inst->HasResultId() == dst_inst->HasResultId());
  152. if (src_inst->HasResultId()) {
  153. MapIds(src_inst->result_id(), dst_inst->result_id());
  154. } else {
  155. src_to_dst_.MapInsts(src_inst, dst_inst);
  156. dst_to_src_.MapInsts(dst_inst, src_inst);
  157. }
  158. }
  159. const IdMap& SrcToDstMap() const { return src_to_dst_; }
  160. const IdMap& DstToSrcMap() const { return dst_to_src_; }
  161. private:
  162. IdMap src_to_dst_;
  163. IdMap dst_to_src_;
  164. };
  165. // Mappings from ids to instructions and metadata, for a single module's ids.
  166. struct IdInstructions {
  167. IdInstructions(const opt::Module* module)
  168. : inst_map_(module->IdBound(), nullptr),
  169. name_map_(module->IdBound()),
  170. decoration_map_(module->IdBound()),
  171. forward_pointer_map_(module->IdBound()) {
  172. // Map ids from all sections to instructions that define them.
  173. MapIdsToInstruction(module->ext_inst_imports());
  174. MapIdsToInstruction(module->debugs1());
  175. MapIdsToInstruction(module->debugs2());
  176. MapIdsToInstruction(module->debugs3());
  177. MapIdsToInstruction(module->ext_inst_debuginfo());
  178. MapIdsToInstruction(module->types_values());
  179. for (const opt::Function& function : *module) {
  180. function.ForEachInst(
  181. [this](const opt::Instruction* inst) {
  182. if (inst->HasResultId()) {
  183. MapIdToInstruction(inst->result_id(), inst);
  184. }
  185. },
  186. true, true);
  187. }
  188. // Gather decorations applied to ids that could be useful in matching them
  189. // between src and dst modules.
  190. MapIdsToInfos(module->debugs2());
  191. MapIdsToInfos(module->annotations());
  192. MapIdsToInfos(module->types_values());
  193. }
  194. void MapIdToInstruction(uint32_t id, const opt::Instruction* inst);
  195. // Return true if id is mapped to any instruction, false otherwise.
  196. bool IsDefined(uint32_t id) {
  197. return id < inst_map_.size() && inst_map_[id] != nullptr;
  198. }
  199. void MapIdsToInstruction(
  200. opt::IteratorRange<opt::Module::const_inst_iterator> section);
  201. void MapIdsToInfos(
  202. opt::IteratorRange<opt::Module::const_inst_iterator> section);
  203. IdToInstructionMap inst_map_;
  204. IdToInfoMap name_map_;
  205. IdToInfoMap decoration_map_;
  206. IdToInstructionMap forward_pointer_map_;
  207. };
  208. class Differ {
  209. public:
  210. Differ(opt::IRContext* src, opt::IRContext* dst, std::ostream& out,
  211. Options options)
  212. : src_context_(src),
  213. dst_context_(dst),
  214. src_(src->module()),
  215. dst_(dst->module()),
  216. options_(options),
  217. out_(out),
  218. src_id_to_(src_),
  219. dst_id_to_(dst_),
  220. id_map_(src_->IdBound(), dst_->IdBound()) {
  221. // Cache function bodies in canonicalization order.
  222. GetFunctionBodies(src_context_, &src_funcs_, &src_func_insts_);
  223. GetFunctionBodies(dst_context_, &dst_funcs_, &dst_func_insts_);
  224. }
  225. // Match ids or instructions of different sections.
  226. void MatchCapabilities();
  227. void MatchExtensions();
  228. void MatchExtInstImportIds();
  229. void MatchMemoryModel();
  230. void MatchEntryPointIds();
  231. void MatchExecutionModes();
  232. void MatchTypeForwardPointers();
  233. void MatchTypeIds();
  234. void MatchConstants();
  235. void MatchVariableIds();
  236. void MatchFunctions();
  237. // Debug info and annotations are matched only after ids are matched.
  238. void MatchDebugs1();
  239. void MatchDebugs2();
  240. void MatchDebugs3();
  241. void MatchExtInstDebugInfo();
  242. void MatchAnnotations();
  243. // Output the diff.
  244. spv_result_t Output();
  245. void DumpIdMap() {
  246. if (!options_.dump_id_map) {
  247. return;
  248. }
  249. out_ << " Src -> Dst\n";
  250. for (uint32_t src_id = 1; src_id < src_->IdBound(); ++src_id) {
  251. uint32_t dst_id = id_map_.MappedDstId(src_id);
  252. if (src_id_to_.inst_map_[src_id] != nullptr && dst_id != 0)
  253. out_ << std::setw(4) << src_id << " -> " << std::setw(4) << dst_id
  254. << " [" << spvOpcodeString(src_id_to_.inst_map_[src_id]->opcode())
  255. << "]\n";
  256. }
  257. }
  258. private:
  259. // Helper functions that match ids between src and dst
  260. void PoolPotentialIds(
  261. opt::IteratorRange<opt::Module::const_inst_iterator> section,
  262. std::vector<uint32_t>& ids, bool is_src,
  263. std::function<bool(const opt::Instruction&)> filter,
  264. std::function<uint32_t(const opt::Instruction&)> get_id);
  265. void MatchIds(
  266. PotentialIdMap& potential,
  267. std::function<bool(const opt::Instruction*, const opt::Instruction*)>
  268. match);
  269. // Helper functions that match id-less instructions between src and dst.
  270. void MatchPreambleInstructions(
  271. opt::IteratorRange<opt::Module::const_inst_iterator> src_insts,
  272. opt::IteratorRange<opt::Module::const_inst_iterator> dst_insts);
  273. InstructionList SortPreambleInstructions(
  274. const opt::Module* module,
  275. opt::IteratorRange<opt::Module::const_inst_iterator> insts);
  276. int ComparePreambleInstructions(const opt::Instruction* a,
  277. const opt::Instruction* b,
  278. const opt::Module* src_inst_module,
  279. const opt::Module* dst_inst_module);
  280. // Helper functions that match debug and annotation instructions of already
  281. // matched ids.
  282. void MatchDebugAndAnnotationInstructions(
  283. opt::IteratorRange<opt::Module::const_inst_iterator> src_insts,
  284. opt::IteratorRange<opt::Module::const_inst_iterator> dst_insts);
  285. // Get various properties from an id. These Helper functions are passed to
  286. // `GroupIds` and `GroupIdsAndMatch` below (as the `get_group` argument).
  287. uint32_t GroupIdsHelperGetTypeId(const IdInstructions& id_to, uint32_t id);
  288. uint32_t GroupIdsHelperGetFunctionTypeId(const IdInstructions& id_to,
  289. uint32_t id);
  290. spv::StorageClass GroupIdsHelperGetTypePointerStorageClass(
  291. const IdInstructions& id_to, uint32_t id);
  292. spv::Op GroupIdsHelperGetTypePointerTypeOp(const IdInstructions& id_to,
  293. uint32_t id);
  294. // Given a list of ids, groups them based on some value. The `get_group`
  295. // function extracts a piece of information corresponding to each id, and the
  296. // ids are bucketed based on that (and output in `groups`). This is useful to
  297. // attempt to match ids between src and dst only when said property is
  298. // identical.
  299. template <typename T>
  300. void GroupIds(const IdGroup& ids, bool is_src, std::map<T, IdGroup>* groups,
  301. T (Differ::*get_group)(const IdInstructions&, uint32_t));
  302. // Calls GroupIds to bucket ids in src and dst based on a property returned by
  303. // `get_group`. This function then calls `match_group` for each bucket (i.e.
  304. // "group") with identical values for said property.
  305. //
  306. // For example, say src and dst ids have the following properties
  307. // correspondingly:
  308. //
  309. // - src ids' properties: {id0: A, id1: A, id2: B, id3: C, id4: B}
  310. // - dst ids' properties: {id0': B, id1': C, id2': B, id3': D, id4': B}
  311. //
  312. // Then `match_group` is called 2 times:
  313. //
  314. // - Once with: ([id2, id4], [id0', id2', id4']) corresponding to B
  315. // - Once with: ([id3], [id2']) corresponding to C
  316. //
  317. // Ids corresponding to A and D cannot match based on this property.
  318. template <typename T>
  319. void GroupIdsAndMatch(
  320. const IdGroup& src_ids, const IdGroup& dst_ids, T invalid_group_key,
  321. T (Differ::*get_group)(const IdInstructions&, uint32_t),
  322. std::function<void(const IdGroup& src_group, const IdGroup& dst_group)>
  323. match_group);
  324. // Bucket `src_ids` and `dst_ids` by the key ids returned by `get_group`, and
  325. // then call `match_group` on pairs of buckets whose key ids are matched with
  326. // each other.
  327. //
  328. // For example, suppose we want to pair up groups of instructions with the
  329. // same type. Naturally, the source instructions refer to their types by their
  330. // ids in the source, and the destination instructions use destination type
  331. // ids, so simply comparing source and destination type ids as integers, as
  332. // `GroupIdsAndMatch` would do, is meaningless. But if a prior call to
  333. // `MatchTypeIds` has established type matches between the two modules, then
  334. // we can consult those to pair source and destination buckets whose types are
  335. // equivalent.
  336. //
  337. // Suppose our input groups are as follows:
  338. //
  339. // - src_ids: { 1 -> 100, 2 -> 300, 3 -> 100, 4 -> 200 }
  340. // - dst_ids: { 5 -> 10, 6 -> 20, 7 -> 10, 8 -> 300 }
  341. //
  342. // Here, `X -> Y` means that the instruction with SPIR-V id `X` is a member of
  343. // the group, and `Y` is the id of its type. If we use
  344. // `Differ::GroupIdsHelperGetTypeId` for `get_group`, then
  345. // `get_group(X) == Y`.
  346. //
  347. // These instructions are bucketed by type as follows:
  348. //
  349. // - source: [1, 3] -> 100
  350. // [4] -> 200
  351. // [2] -> 300
  352. //
  353. // - destination: [5, 7] -> 10
  354. // [6] -> 20
  355. // [8] -> 300
  356. //
  357. // Now suppose that we have previously matched up src type 100 with dst type
  358. // 10, and src type 200 with dst type 20, but no other types are matched.
  359. //
  360. // Then `match_group` is called twice:
  361. // - Once with ([1,3], [5, 7]), corresponding to 100/10
  362. // - Once with ([4],[6]), corresponding to 200/20
  363. //
  364. // The source type 300 isn't matched with anything, so the fact that there's a
  365. // destination type 300 is irrelevant, and thus 2 and 8 are never passed to
  366. // `match_group`.
  367. //
  368. // This function isn't specific to types; it simply buckets by the ids
  369. // returned from `get_group`, and consults existing matches to pair up the
  370. // resulting buckets.
  371. void GroupIdsAndMatchByMappedId(
  372. const IdGroup& src_ids, const IdGroup& dst_ids,
  373. uint32_t (Differ::*get_group)(const IdInstructions&, uint32_t),
  374. std::function<void(const IdGroup& src_group, const IdGroup& dst_group)>
  375. match_group);
  376. // Helper functions that determine if two instructions match
  377. bool DoIdsMatch(uint32_t src_id, uint32_t dst_id);
  378. bool DoesOperandMatch(const opt::Operand& src_operand,
  379. const opt::Operand& dst_operand);
  380. bool DoOperandsMatch(const opt::Instruction* src_inst,
  381. const opt::Instruction* dst_inst,
  382. uint32_t in_operand_index_start,
  383. uint32_t in_operand_count);
  384. bool DoInstructionsMatch(const opt::Instruction* src_inst,
  385. const opt::Instruction* dst_inst);
  386. bool DoIdsMatchFuzzy(uint32_t src_id, uint32_t dst_id);
  387. bool DoesOperandMatchFuzzy(const opt::Operand& src_operand,
  388. const opt::Operand& dst_operand);
  389. bool DoInstructionsMatchFuzzy(const opt::Instruction* src_inst,
  390. const opt::Instruction* dst_inst);
  391. bool AreIdenticalUintConstants(uint32_t src_id, uint32_t dst_id);
  392. bool DoDebugAndAnnotationInstructionsMatch(const opt::Instruction* src_inst,
  393. const opt::Instruction* dst_inst);
  394. bool AreVariablesMatchable(uint32_t src_id, uint32_t dst_id,
  395. uint32_t flexibility);
  396. bool MatchOpTypeStruct(const opt::Instruction* src_inst,
  397. const opt::Instruction* dst_inst,
  398. uint32_t flexibility);
  399. bool MatchOpConstant(const opt::Instruction* src_inst,
  400. const opt::Instruction* dst_inst, uint32_t flexibility);
  401. bool MatchOpSpecConstant(const opt::Instruction* src_inst,
  402. const opt::Instruction* dst_inst);
  403. bool MatchOpVariable(const opt::Instruction* src_inst,
  404. const opt::Instruction* dst_inst, uint32_t flexibility);
  405. bool MatchPerVertexType(uint32_t src_type_id, uint32_t dst_type_id);
  406. bool MatchPerVertexVariable(const opt::Instruction* src_inst,
  407. const opt::Instruction* dst_inst);
  408. // Helper functions for matching OpTypeForwardPointer
  409. void MatchTypeForwardPointersByName(const IdGroup& src, const IdGroup& dst);
  410. void MatchTypeForwardPointersByTypeOp(const IdGroup& src, const IdGroup& dst);
  411. // Helper functions for function matching.
  412. using FunctionMap = std::map<uint32_t, const opt::Function*>;
  413. InstructionList GetFunctionBody(opt::IRContext* context,
  414. opt::Function& function);
  415. InstructionList GetFunctionHeader(const opt::Function& function);
  416. void GetFunctionBodies(opt::IRContext* context, FunctionMap* functions,
  417. FunctionInstMap* function_insts);
  418. void GetFunctionHeaderInstructions(const opt::Module* module,
  419. FunctionInstMap* function_insts);
  420. void BestEffortMatchFunctions(const IdGroup& src_func_ids,
  421. const IdGroup& dst_func_ids,
  422. const FunctionInstMap& src_func_insts,
  423. const FunctionInstMap& dst_func_insts);
  424. // Calculates the diff of two function bodies. Note that the matched
  425. // instructions themselves may not be identical; output of exact matches
  426. // should produce the exact instruction while inexact matches should produce a
  427. // diff as well.
  428. //
  429. // Returns the similarity of the two bodies = 2*N_match / (N_src + N_dst)
  430. void MatchFunctionParamIds(const opt::Function* src_func,
  431. const opt::Function* dst_func);
  432. float MatchFunctionBodies(const InstructionList& src_body,
  433. const InstructionList& dst_body,
  434. DiffMatch* src_match_result,
  435. DiffMatch* dst_match_result);
  436. void MatchIdsInFunctionBodies(const InstructionList& src_body,
  437. const InstructionList& dst_body,
  438. const DiffMatch& src_match_result,
  439. const DiffMatch& dst_match_result,
  440. uint32_t flexibility);
  441. void MatchVariablesUsedByMatchedInstructions(const opt::Instruction* src_inst,
  442. const opt::Instruction* dst_inst,
  443. uint32_t flexibility);
  444. // Helper functions to retrieve information pertaining to an id
  445. const opt::Instruction* GetInst(const IdInstructions& id_to, uint32_t id);
  446. uint32_t GetConstantUint(const IdInstructions& id_to, uint32_t constant_id);
  447. spv::ExecutionModel GetExecutionModel(const opt::Module* module,
  448. uint32_t entry_point_id);
  449. bool HasName(const IdInstructions& id_to, uint32_t id);
  450. // Get the OpName associated with an id
  451. std::string GetName(const IdInstructions& id_to, uint32_t id, bool* has_name);
  452. // Get the OpName associated with an id, with argument types stripped for
  453. // functions. Some tools don't encode function argument types in the OpName
  454. // string, and this improves diff between SPIR-V from those tools and others.
  455. std::string GetSanitizedName(const IdInstructions& id_to, uint32_t id);
  456. uint32_t GetVarTypeId(const IdInstructions& id_to, uint32_t var_id,
  457. spv::StorageClass* storage_class);
  458. bool GetDecorationValue(const IdInstructions& id_to, uint32_t id,
  459. spv::Decoration decoration,
  460. uint32_t* decoration_value);
  461. const opt::Instruction* GetForwardPointerInst(const IdInstructions& id_to,
  462. uint32_t id);
  463. bool IsIntType(const IdInstructions& id_to, uint32_t type_id);
  464. bool IsFloatType(const IdInstructions& id_to, uint32_t type_id);
  465. bool IsConstantUint(const IdInstructions& id_to, uint32_t id);
  466. bool IsVariable(const IdInstructions& id_to, uint32_t pointer_id);
  467. bool IsOp(const IdInstructions& id_to, uint32_t id, spv::Op opcode);
  468. bool IsPerVertexType(const IdInstructions& id_to, uint32_t type_id);
  469. bool IsPerVertexVariable(const IdInstructions& id_to, uint32_t type_id);
  470. spv::StorageClass GetPerVertexStorageClass(const opt::Module* module,
  471. uint32_t type_id);
  472. spv_ext_inst_type_t GetExtInstType(const IdInstructions& id_to,
  473. uint32_t set_id);
  474. spv_number_kind_t GetNumberKind(const IdInstructions& id_to,
  475. const opt::Instruction& inst,
  476. uint32_t operand_index,
  477. uint32_t* number_bit_width);
  478. spv_number_kind_t GetTypeNumberKind(const IdInstructions& id_to, uint32_t id,
  479. uint32_t* number_bit_width);
  480. // Helper functions to output a diff line
  481. const opt::Instruction* MappedDstInst(const opt::Instruction* src_inst);
  482. const opt::Instruction* MappedSrcInst(const opt::Instruction* dst_inst);
  483. const opt::Instruction* MappedInstImpl(const opt::Instruction* inst,
  484. const IdMap& to_other,
  485. const IdInstructions& other_id_to);
  486. void OutputLine(std::function<bool()> are_lines_identical,
  487. std::function<void()> output_src_line,
  488. std::function<void()> output_dst_line);
  489. template <typename InstList>
  490. void OutputSection(
  491. const InstList& src_insts, const InstList& dst_insts,
  492. std::function<void(const opt::Instruction&, const IdInstructions&,
  493. const opt::Instruction&)>
  494. write_inst);
  495. void ToParsedInstruction(const opt::Instruction& inst,
  496. const IdInstructions& id_to,
  497. const opt::Instruction& original_inst,
  498. spv_parsed_instruction_t* parsed_inst,
  499. std::vector<spv_parsed_operand_t>& parsed_operands,
  500. std::vector<uint32_t>& inst_binary);
  501. opt::Instruction ToMappedSrcIds(const opt::Instruction& dst_inst);
  502. void OutputRed() {
  503. if (options_.color_output) out_ << spvtools::clr::red{true};
  504. }
  505. void OutputGreen() {
  506. if (options_.color_output) out_ << spvtools::clr::green{true};
  507. }
  508. void OutputResetColor() {
  509. if (options_.color_output) out_ << spvtools::clr::reset{true};
  510. }
  511. opt::IRContext* src_context_;
  512. opt::IRContext* dst_context_;
  513. const opt::Module* src_;
  514. const opt::Module* dst_;
  515. Options options_;
  516. std::ostream& out_;
  517. // Helpers to look up instructions based on id.
  518. IdInstructions src_id_to_;
  519. IdInstructions dst_id_to_;
  520. // The ids that have been matched between src and dst so far.
  521. SrcDstIdMap id_map_;
  522. // List of instructions in function bodies after canonicalization. Cached
  523. // here to avoid duplicate work. More importantly, some maps use
  524. // opt::Instruction pointers so they need to be unique.
  525. FunctionInstMap src_func_insts_;
  526. FunctionInstMap dst_func_insts_;
  527. FunctionMap src_funcs_;
  528. FunctionMap dst_funcs_;
  529. };
  530. void SrcDstIdMap::MapUnmatchedIds(
  531. std::function<bool(uint32_t)> src_insn_defined,
  532. std::function<bool(uint32_t)> dst_insn_defined) {
  533. const uint32_t src_id_bound = static_cast<uint32_t>(src_to_dst_.IdBound());
  534. const uint32_t dst_id_bound = static_cast<uint32_t>(dst_to_src_.IdBound());
  535. for (uint32_t src_id = 1; src_id < src_id_bound; ++src_id) {
  536. if (!src_to_dst_.IsMapped(src_id) && src_insn_defined(src_id)) {
  537. uint32_t fresh_dst_id = dst_to_src_.MakeFreshId();
  538. MapIds(src_id, fresh_dst_id);
  539. }
  540. }
  541. for (uint32_t dst_id = 1; dst_id < dst_id_bound; ++dst_id) {
  542. if (!dst_to_src_.IsMapped(dst_id) && dst_insn_defined(dst_id)) {
  543. uint32_t fresh_src_id = src_to_dst_.MakeFreshId();
  544. MapIds(fresh_src_id, dst_id);
  545. }
  546. }
  547. }
  548. void IdInstructions::MapIdToInstruction(uint32_t id,
  549. const opt::Instruction* inst) {
  550. assert(id != 0);
  551. assert(id < inst_map_.size());
  552. assert(inst_map_[id] == nullptr);
  553. inst_map_[id] = inst;
  554. }
  555. void IdInstructions::MapIdsToInstruction(
  556. opt::IteratorRange<opt::Module::const_inst_iterator> section) {
  557. for (const opt::Instruction& inst : section) {
  558. uint32_t result_id = inst.result_id();
  559. if (result_id == 0) {
  560. continue;
  561. }
  562. MapIdToInstruction(result_id, &inst);
  563. }
  564. }
  565. void IdInstructions::MapIdsToInfos(
  566. opt::IteratorRange<opt::Module::const_inst_iterator> section) {
  567. for (const opt::Instruction& inst : section) {
  568. IdToInfoMap* info_map = nullptr;
  569. uint32_t id_operand = 0;
  570. switch (inst.opcode()) {
  571. case spv::Op::OpName:
  572. info_map = &name_map_;
  573. break;
  574. case spv::Op::OpMemberName:
  575. info_map = &name_map_;
  576. break;
  577. case spv::Op::OpDecorate:
  578. info_map = &decoration_map_;
  579. break;
  580. case spv::Op::OpMemberDecorate:
  581. info_map = &decoration_map_;
  582. break;
  583. case spv::Op::OpTypeForwardPointer: {
  584. uint32_t id = inst.GetSingleWordOperand(0);
  585. assert(id != 0);
  586. assert(id < forward_pointer_map_.size());
  587. forward_pointer_map_[id] = &inst;
  588. continue;
  589. }
  590. default:
  591. // Currently unsupported instruction, don't attempt to use it for
  592. // matching.
  593. break;
  594. }
  595. if (info_map == nullptr) {
  596. continue;
  597. }
  598. uint32_t id = inst.GetOperand(id_operand).AsId();
  599. assert(id != 0);
  600. assert(id < info_map->size());
  601. assert(std::find((*info_map)[id].begin(), (*info_map)[id].end(), &inst) ==
  602. (*info_map)[id].end());
  603. (*info_map)[id].push_back(&inst);
  604. }
  605. }
  606. void Differ::PoolPotentialIds(
  607. opt::IteratorRange<opt::Module::const_inst_iterator> section,
  608. std::vector<uint32_t>& ids, bool is_src,
  609. std::function<bool(const opt::Instruction&)> filter,
  610. std::function<uint32_t(const opt::Instruction&)> get_id) {
  611. for (const opt::Instruction& inst : section) {
  612. if (!filter(inst)) {
  613. continue;
  614. }
  615. uint32_t result_id = get_id(inst);
  616. assert(result_id != 0);
  617. assert(std::find(ids.begin(), ids.end(), result_id) == ids.end());
  618. // Don't include ids that are already matched, for example through
  619. // OpTypeForwardPointer.
  620. const bool is_matched = is_src ? id_map_.IsSrcMapped(result_id)
  621. : id_map_.IsDstMapped(result_id);
  622. if (is_matched) {
  623. continue;
  624. }
  625. ids.push_back(result_id);
  626. }
  627. }
  628. void Differ::MatchIds(
  629. PotentialIdMap& potential,
  630. std::function<bool(const opt::Instruction*, const opt::Instruction*)>
  631. match) {
  632. for (size_t src_index = 0; src_index < potential.src_ids.size();
  633. ++src_index) {
  634. for (size_t dst_index = 0; dst_index < potential.dst_ids.size();
  635. ++dst_index) {
  636. const uint32_t src_id = potential.src_ids[src_index];
  637. const uint32_t dst_id = potential.dst_ids[dst_index];
  638. if (dst_id == 0) {
  639. // Already matched.
  640. continue;
  641. }
  642. const opt::Instruction* src_inst = src_id_to_.inst_map_[src_id];
  643. const opt::Instruction* dst_inst = dst_id_to_.inst_map_[dst_id];
  644. if (match(src_inst, dst_inst)) {
  645. id_map_.MapIds(src_id, dst_id);
  646. // Remove the ids from the potential list.
  647. potential.src_ids[src_index] = 0;
  648. potential.dst_ids[dst_index] = 0;
  649. // Find a match for the next src id.
  650. break;
  651. }
  652. }
  653. }
  654. // Remove matched ids to make the next iteration faster.
  655. CompactIds(potential.src_ids);
  656. CompactIds(potential.dst_ids);
  657. }
  658. void Differ::MatchPreambleInstructions(
  659. opt::IteratorRange<opt::Module::const_inst_iterator> src_insts,
  660. opt::IteratorRange<opt::Module::const_inst_iterator> dst_insts) {
  661. // First, pool all instructions from each section and sort them.
  662. InstructionList sorted_src_insts = SortPreambleInstructions(src_, src_insts);
  663. InstructionList sorted_dst_insts = SortPreambleInstructions(dst_, dst_insts);
  664. // Then walk and match them.
  665. size_t src_cur = 0;
  666. size_t dst_cur = 0;
  667. while (src_cur < sorted_src_insts.size() &&
  668. dst_cur < sorted_dst_insts.size()) {
  669. const opt::Instruction* src_inst = sorted_src_insts[src_cur];
  670. const opt::Instruction* dst_inst = sorted_dst_insts[dst_cur];
  671. int compare = ComparePreambleInstructions(src_inst, dst_inst, src_, dst_);
  672. if (compare == 0) {
  673. id_map_.MapInsts(src_inst, dst_inst);
  674. }
  675. if (compare <= 0) {
  676. ++src_cur;
  677. }
  678. if (compare >= 0) {
  679. ++dst_cur;
  680. }
  681. }
  682. }
  683. InstructionList Differ::SortPreambleInstructions(
  684. const opt::Module* module,
  685. opt::IteratorRange<opt::Module::const_inst_iterator> insts) {
  686. InstructionList sorted;
  687. for (const opt::Instruction& inst : insts) {
  688. sorted.push_back(&inst);
  689. }
  690. std::sort(
  691. sorted.begin(), sorted.end(),
  692. [this, module](const opt::Instruction* a, const opt::Instruction* b) {
  693. return ComparePreambleInstructions(a, b, module, module) < 0;
  694. });
  695. return sorted;
  696. }
  697. int Differ::ComparePreambleInstructions(const opt::Instruction* a,
  698. const opt::Instruction* b,
  699. const opt::Module* src_inst_module,
  700. const opt::Module* dst_inst_module) {
  701. assert(a->opcode() == b->opcode());
  702. assert(!a->HasResultId());
  703. assert(!a->HasResultType());
  704. const uint32_t a_operand_count = a->NumOperands();
  705. const uint32_t b_operand_count = b->NumOperands();
  706. if (a_operand_count < b_operand_count) {
  707. return -1;
  708. }
  709. if (a_operand_count > b_operand_count) {
  710. return 1;
  711. }
  712. // Instead of comparing OpExecutionMode entry point ids as ids, compare them
  713. // through their corresponding execution model. This simplifies traversing
  714. // the sorted list of instructions between src and dst modules.
  715. if (a->opcode() == spv::Op::OpExecutionMode) {
  716. const spv::ExecutionModel src_model =
  717. GetExecutionModel(src_inst_module, a->GetSingleWordOperand(0));
  718. const spv::ExecutionModel dst_model =
  719. GetExecutionModel(dst_inst_module, b->GetSingleWordOperand(0));
  720. if (src_model < dst_model) {
  721. return -1;
  722. }
  723. if (src_model > dst_model) {
  724. return 1;
  725. }
  726. }
  727. // Match every operand of the instruction.
  728. for (uint32_t operand_index = 0; operand_index < a_operand_count;
  729. ++operand_index) {
  730. const opt::Operand& a_operand = a->GetOperand(operand_index);
  731. const opt::Operand& b_operand = b->GetOperand(operand_index);
  732. if (a_operand.type < b_operand.type) {
  733. return -1;
  734. }
  735. if (a_operand.type > b_operand.type) {
  736. return 1;
  737. }
  738. switch (a_operand.type) {
  739. case SPV_OPERAND_TYPE_ID:
  740. // Don't compare ids, there can't be multiple instances of the
  741. // OpExecutionMode with different ids of the same execution model.
  742. break;
  743. case SPV_OPERAND_TYPE_TYPE_ID:
  744. case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
  745. case SPV_OPERAND_TYPE_SCOPE_ID:
  746. assert(false && "Unreachable");
  747. break;
  748. case SPV_OPERAND_TYPE_LITERAL_STRING: {
  749. int str_compare =
  750. strcmp(a_operand.AsString().c_str(), b_operand.AsString().c_str());
  751. if (str_compare != 0) {
  752. return str_compare;
  753. }
  754. break;
  755. }
  756. default:
  757. // Expect literal values to match.
  758. assert(a_operand.words.size() == 1);
  759. assert(b_operand.words.size() == 1);
  760. if (a_operand.words[0] < b_operand.words[0]) {
  761. return -1;
  762. }
  763. if (a_operand.words[0] > b_operand.words[0]) {
  764. return 1;
  765. }
  766. break;
  767. }
  768. }
  769. return 0;
  770. }
  771. void Differ::MatchDebugAndAnnotationInstructions(
  772. opt::IteratorRange<opt::Module::const_inst_iterator> src_insts,
  773. opt::IteratorRange<opt::Module::const_inst_iterator> dst_insts) {
  774. for (const opt::Instruction& src_inst : src_insts) {
  775. for (const opt::Instruction& dst_inst : dst_insts) {
  776. if (MappedSrcInst(&dst_inst) != nullptr) {
  777. continue;
  778. }
  779. // Map instructions as soon as they match. Debug and annotation
  780. // instructions are matched such that there can't be multiple matches.
  781. if (DoDebugAndAnnotationInstructionsMatch(&src_inst, &dst_inst)) {
  782. id_map_.MapInsts(&src_inst, &dst_inst);
  783. break;
  784. }
  785. }
  786. }
  787. }
  788. uint32_t Differ::GroupIdsHelperGetTypeId(const IdInstructions& id_to,
  789. uint32_t id) {
  790. return GetInst(id_to, id)->type_id();
  791. }
  792. // Return an `OpFunction` instruction's full `OpTypeFunction` type,
  793. // which includes parameter types.
  794. //
  795. // `GroupIdsHelperGetTypeId` applied to an `OpFunction` only gets the
  796. // function's return type, so this is a slightly more precise way to
  797. // match up functions by signature.
  798. uint32_t Differ::GroupIdsHelperGetFunctionTypeId(const IdInstructions& id_to,
  799. uint32_t id) {
  800. return GetInst(id_to, id)->GetSingleWordOperand(3);
  801. }
  802. spv::StorageClass Differ::GroupIdsHelperGetTypePointerStorageClass(
  803. const IdInstructions& id_to, uint32_t id) {
  804. const opt::Instruction* inst = GetInst(id_to, id);
  805. assert(inst && inst->opcode() == spv::Op::OpTypePointer);
  806. return spv::StorageClass(inst->GetSingleWordInOperand(0));
  807. }
  808. spv::Op Differ::GroupIdsHelperGetTypePointerTypeOp(const IdInstructions& id_to,
  809. uint32_t id) {
  810. const opt::Instruction* inst = GetInst(id_to, id);
  811. assert(inst && inst->opcode() == spv::Op::OpTypePointer);
  812. const uint32_t type_id = inst->GetSingleWordInOperand(1);
  813. const opt::Instruction* type_inst = GetInst(id_to, type_id);
  814. assert(type_inst);
  815. return type_inst->opcode();
  816. }
  817. // Group unmatched ids in `ids` according to some characteristic,
  818. // determined by `get_group`.
  819. //
  820. // Using `get_group` to compute some sort of key for each id, set
  821. // `groups` to map each key to all the ids that have that key.
  822. //
  823. // For example, to group ids by name, pass `Differ::GetName` as
  824. // `get_group`. This will fill `groups` with a map from each name to
  825. // all the ids with that name.
  826. //
  827. // Under the assumption that we're trying to establish new pairings,
  828. // ids that are already paired are omitted from `groups`.
  829. //
  830. // The `is_src` parameter indicates whether `ids` are drawn from the
  831. // source module or the destination module.
  832. //
  833. // The template parameter `T` is the key type, like `std::string` or
  834. // `uint32_t`.
  835. template <typename T>
  836. void Differ::GroupIds(const IdGroup& ids, bool is_src,
  837. std::map<T, IdGroup>* groups,
  838. T (Differ::*get_group)(const IdInstructions&, uint32_t)) {
  839. assert(groups->empty());
  840. const IdInstructions& id_to = is_src ? src_id_to_ : dst_id_to_;
  841. for (const uint32_t id : ids) {
  842. // Don't include ids that are already matched, for example through
  843. // OpEntryPoint.
  844. const bool is_matched =
  845. is_src ? id_map_.IsSrcMapped(id) : id_map_.IsDstMapped(id);
  846. if (is_matched) {
  847. continue;
  848. }
  849. T group = (this->*get_group)(id_to, id);
  850. (*groups)[group].push_back(id);
  851. }
  852. }
  853. // Group `src_ids` and `dst_ids` according to `get_group`, and then use
  854. // `match_group` to pair up ids in corresponding groups.
  855. //
  856. // Don't try to pair ids in groups whose key is `invalid_group_key`.
  857. template <typename T>
  858. void Differ::GroupIdsAndMatch(
  859. const IdGroup& src_ids, const IdGroup& dst_ids, T invalid_group_key,
  860. T (Differ::*get_group)(const IdInstructions&, uint32_t),
  861. std::function<void(const IdGroup& src_group, const IdGroup& dst_group)>
  862. match_group) {
  863. // Group the ids based on a key (get_group)
  864. std::map<T, IdGroup> src_groups;
  865. std::map<T, IdGroup> dst_groups;
  866. GroupIds<T>(src_ids, true, &src_groups, get_group);
  867. GroupIds<T>(dst_ids, false, &dst_groups, get_group);
  868. // Iterate over the groups, and match those with identical keys
  869. for (const auto& iter : src_groups) {
  870. const T& key = iter.first;
  871. const IdGroup& src_group = iter.second;
  872. if (key == invalid_group_key) {
  873. continue;
  874. }
  875. const IdGroup& dst_group = dst_groups[key];
  876. // Let the caller match the groups as appropriate.
  877. match_group(src_group, dst_group);
  878. }
  879. }
  880. void Differ::GroupIdsAndMatchByMappedId(
  881. const IdGroup& src_ids, const IdGroup& dst_ids,
  882. uint32_t (Differ::*get_group)(const IdInstructions&, uint32_t),
  883. std::function<void(const IdGroup& src_group, const IdGroup& dst_group)>
  884. match_group) {
  885. // Group the ids based on a key (get_group)
  886. std::map<uint32_t, IdGroup> src_groups;
  887. std::map<uint32_t, IdGroup> dst_groups;
  888. GroupIds<uint32_t>(src_ids, true, &src_groups, get_group);
  889. GroupIds<uint32_t>(dst_ids, false, &dst_groups, get_group);
  890. // Iterate over pairs of groups whose keys map to each other.
  891. for (const auto& iter : src_groups) {
  892. const uint32_t& src_key = iter.first;
  893. const IdGroup& src_group = iter.second;
  894. if (src_key == 0) {
  895. continue;
  896. }
  897. if (id_map_.IsSrcMapped(src_key)) {
  898. const uint32_t& dst_key = id_map_.MappedDstId(src_key);
  899. const IdGroup& dst_group = dst_groups[dst_key];
  900. // Let the caller match the groups as appropriate.
  901. match_group(src_group, dst_group);
  902. }
  903. }
  904. }
  905. bool Differ::DoIdsMatch(uint32_t src_id, uint32_t dst_id) {
  906. assert(dst_id != 0);
  907. return id_map_.MappedDstId(src_id) == dst_id;
  908. }
  909. bool Differ::DoesOperandMatch(const opt::Operand& src_operand,
  910. const opt::Operand& dst_operand) {
  911. assert(src_operand.type == dst_operand.type);
  912. switch (src_operand.type) {
  913. case SPV_OPERAND_TYPE_ID:
  914. case SPV_OPERAND_TYPE_TYPE_ID:
  915. case SPV_OPERAND_TYPE_RESULT_ID:
  916. case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
  917. case SPV_OPERAND_TYPE_SCOPE_ID:
  918. // Match ids only if they are already matched in the id map.
  919. return DoIdsMatch(src_operand.AsId(), dst_operand.AsId());
  920. case SPV_OPERAND_TYPE_LITERAL_STRING:
  921. return src_operand.AsString() == dst_operand.AsString();
  922. default:
  923. // Otherwise expect them to match exactly.
  924. assert(src_operand.type != SPV_OPERAND_TYPE_LITERAL_STRING);
  925. if (src_operand.words.size() != dst_operand.words.size()) {
  926. return false;
  927. }
  928. for (size_t i = 0; i < src_operand.words.size(); ++i) {
  929. if (src_operand.words[i] != dst_operand.words[i]) {
  930. return false;
  931. }
  932. }
  933. return true;
  934. }
  935. }
  936. bool Differ::DoOperandsMatch(const opt::Instruction* src_inst,
  937. const opt::Instruction* dst_inst,
  938. uint32_t in_operand_index_start,
  939. uint32_t in_operand_count) {
  940. // Caller should have returned early for instructions with different opcode.
  941. assert(src_inst->opcode() == dst_inst->opcode());
  942. bool match = true;
  943. for (uint32_t i = 0; i < in_operand_count; ++i) {
  944. const uint32_t in_operand_index = in_operand_index_start + i;
  945. const opt::Operand& src_operand = src_inst->GetInOperand(in_operand_index);
  946. const opt::Operand& dst_operand = dst_inst->GetInOperand(in_operand_index);
  947. match = match && DoesOperandMatch(src_operand, dst_operand);
  948. }
  949. return match;
  950. }
  951. bool Differ::DoInstructionsMatch(const opt::Instruction* src_inst,
  952. const opt::Instruction* dst_inst) {
  953. // Check whether the two instructions are identical, that is the instructions
  954. // themselves are matched, every id is matched, and every other value is
  955. // identical.
  956. if (MappedDstInst(src_inst) != dst_inst) {
  957. return false;
  958. }
  959. assert(src_inst->opcode() == dst_inst->opcode());
  960. if (src_inst->NumOperands() != dst_inst->NumOperands()) {
  961. return false;
  962. }
  963. for (uint32_t operand_index = 0; operand_index < src_inst->NumOperands();
  964. ++operand_index) {
  965. const opt::Operand& src_operand = src_inst->GetOperand(operand_index);
  966. const opt::Operand& dst_operand = dst_inst->GetOperand(operand_index);
  967. if (!DoesOperandMatch(src_operand, dst_operand)) {
  968. return false;
  969. }
  970. }
  971. return true;
  972. }
  973. bool Differ::DoIdsMatchFuzzy(uint32_t src_id, uint32_t dst_id) {
  974. assert(dst_id != 0);
  975. const uint32_t mapped_dst_id = id_map_.MappedDstId(src_id);
  976. // Consider unmatched ids as a match. In function bodies, no result id is
  977. // matched yet and thus they are excluded from instruction matching when used
  978. // as parameters in subsequent instructions.
  979. if (mapped_dst_id == 0 || mapped_dst_id == dst_id) {
  980. return true;
  981. }
  982. // Int and Uint constants are interchangeable, match them in that case.
  983. if (AreIdenticalUintConstants(src_id, dst_id)) {
  984. return true;
  985. }
  986. return false;
  987. }
  988. bool Differ::DoesOperandMatchFuzzy(const opt::Operand& src_operand,
  989. const opt::Operand& dst_operand) {
  990. if (src_operand.type != dst_operand.type) {
  991. return false;
  992. }
  993. assert(src_operand.type != SPV_OPERAND_TYPE_RESULT_ID);
  994. assert(dst_operand.type != SPV_OPERAND_TYPE_RESULT_ID);
  995. switch (src_operand.type) {
  996. case SPV_OPERAND_TYPE_ID:
  997. case SPV_OPERAND_TYPE_TYPE_ID:
  998. case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
  999. case SPV_OPERAND_TYPE_SCOPE_ID:
  1000. // Match id operands only if they are already matched in the id map.
  1001. return DoIdsMatchFuzzy(src_operand.AsId(), dst_operand.AsId());
  1002. default:
  1003. // Otherwise allow everything to match.
  1004. return true;
  1005. }
  1006. }
  1007. bool Differ::DoInstructionsMatchFuzzy(const opt::Instruction* src_inst,
  1008. const opt::Instruction* dst_inst) {
  1009. // Similar to DoOperandsMatch, but only checks that ids that have already been
  1010. // matched are identical. Ids that are unknown are allowed to match, as well
  1011. // as any non-id operand.
  1012. if (src_inst->opcode() != dst_inst->opcode()) {
  1013. return false;
  1014. }
  1015. // For external instructions, make sure the set and opcode of the external
  1016. // instruction matches too.
  1017. if (src_inst->opcode() == spv::Op::OpExtInst) {
  1018. if (!DoOperandsMatch(src_inst, dst_inst, 0, 2)) {
  1019. return false;
  1020. }
  1021. }
  1022. assert(src_inst->HasResultType() == dst_inst->HasResultType());
  1023. if (src_inst->HasResultType() &&
  1024. !DoIdsMatchFuzzy(src_inst->type_id(), dst_inst->type_id())) {
  1025. return false;
  1026. }
  1027. // TODO: allow some instructions to match with different instruction lengths,
  1028. // for example OpImage* with additional operands.
  1029. if (src_inst->NumInOperandWords() != dst_inst->NumInOperandWords()) {
  1030. return false;
  1031. }
  1032. bool match = true;
  1033. for (uint32_t in_operand_index = 0;
  1034. in_operand_index < src_inst->NumInOperandWords(); ++in_operand_index) {
  1035. const opt::Operand& src_operand = src_inst->GetInOperand(in_operand_index);
  1036. const opt::Operand& dst_operand = dst_inst->GetInOperand(in_operand_index);
  1037. match = match && DoesOperandMatchFuzzy(src_operand, dst_operand);
  1038. }
  1039. return match;
  1040. }
  1041. bool Differ::AreIdenticalUintConstants(uint32_t src_id, uint32_t dst_id) {
  1042. return IsConstantUint(src_id_to_, src_id) &&
  1043. IsConstantUint(dst_id_to_, dst_id) &&
  1044. GetConstantUint(src_id_to_, src_id) ==
  1045. GetConstantUint(dst_id_to_, dst_id);
  1046. }
  1047. bool Differ::DoDebugAndAnnotationInstructionsMatch(
  1048. const opt::Instruction* src_inst, const opt::Instruction* dst_inst) {
  1049. if (src_inst->opcode() != dst_inst->opcode()) {
  1050. return false;
  1051. }
  1052. switch (src_inst->opcode()) {
  1053. case spv::Op::OpString:
  1054. case spv::Op::OpSourceExtension:
  1055. case spv::Op::OpModuleProcessed:
  1056. return DoesOperandMatch(src_inst->GetOperand(0), dst_inst->GetOperand(0));
  1057. case spv::Op::OpSource:
  1058. return DoOperandsMatch(src_inst, dst_inst, 0, 2);
  1059. case spv::Op::OpSourceContinued:
  1060. return true;
  1061. case spv::Op::OpName:
  1062. return DoOperandsMatch(src_inst, dst_inst, 0, 1);
  1063. case spv::Op::OpMemberName:
  1064. return DoOperandsMatch(src_inst, dst_inst, 0, 2);
  1065. case spv::Op::OpDecorate:
  1066. return DoOperandsMatch(src_inst, dst_inst, 0, 2);
  1067. case spv::Op::OpMemberDecorate:
  1068. return DoOperandsMatch(src_inst, dst_inst, 0, 3);
  1069. case spv::Op::OpExtInst:
  1070. case spv::Op::OpDecorationGroup:
  1071. case spv::Op::OpGroupDecorate:
  1072. case spv::Op::OpGroupMemberDecorate:
  1073. return false;
  1074. default:
  1075. return false;
  1076. }
  1077. }
  1078. bool Differ::AreVariablesMatchable(uint32_t src_id, uint32_t dst_id,
  1079. uint32_t flexibility) {
  1080. // Variables must match by their built-in decorations.
  1081. uint32_t src_built_in_decoration = 0, dst_built_in_decoration = 0;
  1082. const bool src_is_built_in = GetDecorationValue(
  1083. src_id_to_, src_id, spv::Decoration::BuiltIn, &src_built_in_decoration);
  1084. const bool dst_is_built_in = GetDecorationValue(
  1085. dst_id_to_, dst_id, spv::Decoration::BuiltIn, &dst_built_in_decoration);
  1086. if (src_is_built_in != dst_is_built_in) {
  1087. return false;
  1088. }
  1089. if (src_is_built_in && src_built_in_decoration != dst_built_in_decoration) {
  1090. return false;
  1091. }
  1092. // Check their types and storage classes.
  1093. spv::StorageClass src_storage_class, dst_storage_class;
  1094. const uint32_t src_type_id =
  1095. GetVarTypeId(src_id_to_, src_id, &src_storage_class);
  1096. const uint32_t dst_type_id =
  1097. GetVarTypeId(dst_id_to_, dst_id, &dst_storage_class);
  1098. if (!DoIdsMatch(src_type_id, dst_type_id)) {
  1099. return false;
  1100. }
  1101. switch (flexibility) {
  1102. case 0:
  1103. if (src_storage_class != dst_storage_class) {
  1104. return false;
  1105. }
  1106. break;
  1107. case 1:
  1108. if (src_storage_class != dst_storage_class) {
  1109. // Allow one of the two to be Private while the other is Input or
  1110. // Output, this allows matching in/out variables that have been turned
  1111. // global as part of linking two stages (as done in ANGLE).
  1112. const bool src_is_io = src_storage_class == spv::StorageClass::Input ||
  1113. src_storage_class == spv::StorageClass::Output;
  1114. const bool dst_is_io = dst_storage_class == spv::StorageClass::Input ||
  1115. dst_storage_class == spv::StorageClass::Output;
  1116. const bool src_is_private =
  1117. src_storage_class == spv::StorageClass::Private;
  1118. const bool dst_is_private =
  1119. dst_storage_class == spv::StorageClass::Private;
  1120. if (!((src_is_io && dst_is_private) || (src_is_private && dst_is_io))) {
  1121. return false;
  1122. }
  1123. }
  1124. break;
  1125. default:
  1126. assert(false && "Unreachable");
  1127. return false;
  1128. }
  1129. // TODO: Is there any other way to check compatiblity of the variables? It's
  1130. // easy to tell when the variables definitely don't match, but there's little
  1131. // information that can be used for a definite match.
  1132. return true;
  1133. }
  1134. bool Differ::MatchOpTypeStruct(const opt::Instruction* src_inst,
  1135. const opt::Instruction* dst_inst,
  1136. uint32_t flexibility) {
  1137. const uint32_t src_type_id = src_inst->result_id();
  1138. const uint32_t dst_type_id = dst_inst->result_id();
  1139. bool src_has_name = false, dst_has_name = false;
  1140. std::string src_name = GetName(src_id_to_, src_type_id, &src_has_name);
  1141. std::string dst_name = GetName(dst_id_to_, dst_type_id, &dst_has_name);
  1142. // If debug info is present, always match the structs by name.
  1143. if (src_has_name && dst_has_name) {
  1144. if (src_name != dst_name) {
  1145. return false;
  1146. }
  1147. // For gl_PerVertex, find the type pointer of this type (array) and make
  1148. // sure the storage classes of src and dst match; geometry and tessellation
  1149. // shaders have two instances of gl_PerVertex.
  1150. if (src_name == "gl_PerVertex") {
  1151. return MatchPerVertexType(src_type_id, dst_type_id);
  1152. }
  1153. return true;
  1154. }
  1155. // If debug info is not present, match the structs by their type.
  1156. // For gl_PerVertex, find the type pointer of this type (array) and match by
  1157. // storage class. The gl_PerVertex struct is itself found by the BuiltIn
  1158. // decorations applied to its members.
  1159. const bool src_is_per_vertex = IsPerVertexType(src_id_to_, src_type_id);
  1160. const bool dst_is_per_vertex = IsPerVertexType(dst_id_to_, dst_type_id);
  1161. if (src_is_per_vertex != dst_is_per_vertex) {
  1162. return false;
  1163. }
  1164. if (src_is_per_vertex) {
  1165. return MatchPerVertexType(src_type_id, dst_type_id);
  1166. }
  1167. switch (flexibility) {
  1168. case 0:
  1169. if (src_inst->NumInOperandWords() != dst_inst->NumInOperandWords()) {
  1170. return false;
  1171. }
  1172. return DoOperandsMatch(src_inst, dst_inst, 0,
  1173. src_inst->NumInOperandWords());
  1174. case 1:
  1175. // TODO: match by taking a diff of the fields, and see if there's a >75%
  1176. // match. Need to then make sure OpMemberName, OpMemberDecorate,
  1177. // OpAccessChain etc are aware of the struct field matching.
  1178. return false;
  1179. default:
  1180. assert(false && "Unreachable");
  1181. return false;
  1182. }
  1183. }
  1184. bool Differ::MatchOpConstant(const opt::Instruction* src_inst,
  1185. const opt::Instruction* dst_inst,
  1186. uint32_t flexibility) {
  1187. // The constants' type must match. In flexibility == 1, match constants of
  1188. // int and uint, as they are generally interchangeable.
  1189. switch (flexibility) {
  1190. case 0:
  1191. if (!DoesOperandMatch(src_inst->GetOperand(0), dst_inst->GetOperand(0))) {
  1192. return false;
  1193. }
  1194. break;
  1195. case 1:
  1196. if (!IsIntType(src_id_to_, src_inst->type_id()) ||
  1197. !IsIntType(dst_id_to_, dst_inst->type_id())) {
  1198. return false;
  1199. }
  1200. break;
  1201. default:
  1202. assert(false && "Unreachable");
  1203. return false;
  1204. }
  1205. const opt::Operand& src_value_operand = src_inst->GetOperand(2);
  1206. const opt::Operand& dst_value_operand = dst_inst->GetOperand(2);
  1207. const uint64_t src_value = src_value_operand.AsLiteralUint64();
  1208. const uint64_t dst_value = dst_value_operand.AsLiteralUint64();
  1209. // If values are identical, it's a match.
  1210. if (src_value == dst_value) {
  1211. return true;
  1212. }
  1213. // Otherwise, only allow flexibility for float types.
  1214. if (IsFloatType(src_id_to_, src_inst->type_id()) && flexibility == 1) {
  1215. // Tolerance is:
  1216. //
  1217. // - For float: allow 4 bits of mantissa as error
  1218. // - For double: allow 6 bits of mantissa as error
  1219. //
  1220. // TODO: the above values are arbitrary and a placeholder; investigate the
  1221. // amount of error resulting from using `printf("%f", f)` and `printf("%lf",
  1222. // d)` and having glslang parse them.
  1223. const uint64_t tolerance = src_value_operand.words.size() == 1 ? 16 : 64;
  1224. return src_value - dst_value < tolerance ||
  1225. dst_value - src_value < tolerance;
  1226. }
  1227. return false;
  1228. }
  1229. bool Differ::MatchOpSpecConstant(const opt::Instruction* src_inst,
  1230. const opt::Instruction* dst_inst) {
  1231. const uint32_t src_id = src_inst->result_id();
  1232. const uint32_t dst_id = dst_inst->result_id();
  1233. bool src_has_name = false, dst_has_name = false;
  1234. std::string src_name = GetName(src_id_to_, src_id, &src_has_name);
  1235. std::string dst_name = GetName(dst_id_to_, dst_id, &dst_has_name);
  1236. // If debug info is present, always match the spec consts by name.
  1237. if (src_has_name && dst_has_name) {
  1238. return src_name == dst_name;
  1239. }
  1240. // Otherwise, match them by SpecId.
  1241. uint32_t src_spec_id, dst_spec_id;
  1242. if (GetDecorationValue(src_id_to_, src_id, spv::Decoration::SpecId,
  1243. &src_spec_id) &&
  1244. GetDecorationValue(dst_id_to_, dst_id, spv::Decoration::SpecId,
  1245. &dst_spec_id)) {
  1246. return src_spec_id == dst_spec_id;
  1247. }
  1248. // There is no SpecId decoration, while not practical, still valid.
  1249. // SpecConstantOp don't have SpecId and can be matched by operands
  1250. if (src_inst->opcode() == spv::Op::OpSpecConstantOp) {
  1251. if (src_inst->NumInOperandWords() == dst_inst->NumInOperandWords()) {
  1252. return DoOperandsMatch(src_inst, dst_inst, 0,
  1253. src_inst->NumInOperandWords());
  1254. }
  1255. }
  1256. return false;
  1257. }
  1258. bool Differ::MatchOpVariable(const opt::Instruction* src_inst,
  1259. const opt::Instruction* dst_inst,
  1260. uint32_t flexibility) {
  1261. const uint32_t src_id = src_inst->result_id();
  1262. const uint32_t dst_id = dst_inst->result_id();
  1263. const bool src_is_pervertex = IsPerVertexVariable(src_id_to_, src_id);
  1264. const bool dst_is_pervertex = IsPerVertexVariable(dst_id_to_, dst_id);
  1265. // For gl_PerVertex, make sure the input and output instances are matched
  1266. // correctly.
  1267. if (src_is_pervertex != dst_is_pervertex) {
  1268. return false;
  1269. }
  1270. if (src_is_pervertex) {
  1271. return MatchPerVertexVariable(src_inst, dst_inst);
  1272. }
  1273. bool src_has_name = false, dst_has_name = false;
  1274. std::string src_name = GetName(src_id_to_, src_id, &src_has_name);
  1275. std::string dst_name = GetName(dst_id_to_, dst_id, &dst_has_name);
  1276. // If debug info is present, always match the variables by name.
  1277. if (src_has_name && dst_has_name) {
  1278. return src_name == dst_name;
  1279. }
  1280. // If debug info is not present, see if the variables can be matched by their
  1281. // built-in decorations.
  1282. uint32_t src_built_in_decoration;
  1283. const bool src_is_built_in = GetDecorationValue(
  1284. src_id_to_, src_id, spv::Decoration::BuiltIn, &src_built_in_decoration);
  1285. if (src_is_built_in && AreVariablesMatchable(src_id, dst_id, flexibility)) {
  1286. return true;
  1287. }
  1288. spv::StorageClass src_storage_class, dst_storage_class;
  1289. GetVarTypeId(src_id_to_, src_id, &src_storage_class);
  1290. GetVarTypeId(dst_id_to_, dst_id, &dst_storage_class);
  1291. if (src_storage_class != dst_storage_class) {
  1292. return false;
  1293. }
  1294. // If variables are decorated with set/binding, match by the value of those
  1295. // decorations.
  1296. if (!options_.ignore_set_binding) {
  1297. uint32_t src_set = 0, dst_set = 0;
  1298. uint32_t src_binding = 0, dst_binding = 0;
  1299. const bool src_has_set = GetDecorationValue(
  1300. src_id_to_, src_id, spv::Decoration::DescriptorSet, &src_set);
  1301. const bool dst_has_set = GetDecorationValue(
  1302. dst_id_to_, dst_id, spv::Decoration::DescriptorSet, &dst_set);
  1303. const bool src_has_binding = GetDecorationValue(
  1304. src_id_to_, src_id, spv::Decoration::Binding, &src_set);
  1305. const bool dst_has_binding = GetDecorationValue(
  1306. dst_id_to_, dst_id, spv::Decoration::Binding, &dst_set);
  1307. if (src_has_set && dst_has_set && src_has_binding && dst_has_binding) {
  1308. return src_set == dst_set && src_binding == dst_binding;
  1309. }
  1310. }
  1311. // If variables are decorated with location, match by the value of that
  1312. // decoration.
  1313. if (!options_.ignore_location) {
  1314. uint32_t src_location, dst_location;
  1315. const bool src_has_location = GetDecorationValue(
  1316. src_id_to_, src_id, spv::Decoration::Location, &src_location);
  1317. const bool dst_has_location = GetDecorationValue(
  1318. dst_id_to_, dst_id, spv::Decoration::Location, &dst_location);
  1319. if (src_has_location && dst_has_location) {
  1320. return src_location == dst_location;
  1321. }
  1322. }
  1323. // Currently, there's no other way to match variables.
  1324. return false;
  1325. }
  1326. bool Differ::MatchPerVertexType(uint32_t src_type_id, uint32_t dst_type_id) {
  1327. // For gl_PerVertex, find the type pointer of this type (array) and make sure
  1328. // the storage classes of src and dst match; geometry and tessellation shaders
  1329. // have two instances of gl_PerVertex.
  1330. spv::StorageClass src_storage_class =
  1331. GetPerVertexStorageClass(src_, src_type_id);
  1332. spv::StorageClass dst_storage_class =
  1333. GetPerVertexStorageClass(dst_, dst_type_id);
  1334. assert(src_storage_class == spv::StorageClass::Input ||
  1335. src_storage_class == spv::StorageClass::Output);
  1336. assert(dst_storage_class == spv::StorageClass::Input ||
  1337. dst_storage_class == spv::StorageClass::Output);
  1338. return src_storage_class == dst_storage_class;
  1339. }
  1340. bool Differ::MatchPerVertexVariable(const opt::Instruction* src_inst,
  1341. const opt::Instruction* dst_inst) {
  1342. spv::StorageClass src_storage_class =
  1343. spv::StorageClass(src_inst->GetSingleWordInOperand(0));
  1344. spv::StorageClass dst_storage_class =
  1345. spv::StorageClass(dst_inst->GetSingleWordInOperand(0));
  1346. return src_storage_class == dst_storage_class;
  1347. }
  1348. void Differ::MatchTypeForwardPointersByName(const IdGroup& src,
  1349. const IdGroup& dst) {
  1350. // Given two sets of compatible groups of OpTypeForwardPointer instructions,
  1351. // attempts to match them by name.
  1352. // Group them by debug info and loop over them.
  1353. GroupIdsAndMatch<std::string>(
  1354. src, dst, "", &Differ::GetSanitizedName,
  1355. [this](const IdGroup& src_group, const IdGroup& dst_group) {
  1356. // Match only if there's a unique forward declaration with this debug
  1357. // name.
  1358. if (src_group.size() == 1 && dst_group.size() == 1) {
  1359. id_map_.MapIds(src_group[0], dst_group[0]);
  1360. }
  1361. });
  1362. }
  1363. void Differ::MatchTypeForwardPointersByTypeOp(const IdGroup& src,
  1364. const IdGroup& dst) {
  1365. // Given two sets of compatible groups of OpTypeForwardPointer instructions,
  1366. // attempts to match them by type op. Must be called after
  1367. // MatchTypeForwardPointersByName to match as many as possible by debug info.
  1368. // Remove ids that are matched with debug info in
  1369. // MatchTypeForwardPointersByName.
  1370. IdGroup src_unmatched_ids;
  1371. IdGroup dst_unmatched_ids;
  1372. std::copy_if(src.begin(), src.end(), std::back_inserter(src_unmatched_ids),
  1373. [this](uint32_t id) { return !id_map_.IsSrcMapped(id); });
  1374. std::copy_if(dst.begin(), dst.end(), std::back_inserter(dst_unmatched_ids),
  1375. [this](uint32_t id) { return !id_map_.IsDstMapped(id); });
  1376. // Match only if there's a unique forward declaration with this
  1377. // storage class and type opcode. If both have debug info, they
  1378. // must not have been matchable.
  1379. if (src_unmatched_ids.size() == 1 && dst_unmatched_ids.size() == 1) {
  1380. uint32_t src_id = src_unmatched_ids[0];
  1381. uint32_t dst_id = dst_unmatched_ids[0];
  1382. if (!HasName(src_id_to_, src_id) || !HasName(dst_id_to_, dst_id)) {
  1383. id_map_.MapIds(src_id, dst_id);
  1384. }
  1385. }
  1386. }
  1387. InstructionList Differ::GetFunctionBody(opt::IRContext* context,
  1388. opt::Function& function) {
  1389. // Canonicalize the blocks of the function to produce better diff, for example
  1390. // to not produce any diff if the src and dst have the same switch/case blocks
  1391. // but with the cases simply reordered.
  1392. std::list<opt::BasicBlock*> order;
  1393. context->cfg()->ComputeStructuredOrder(&function, &*function.begin(), &order);
  1394. // Go over the instructions of the function and add the instructions to a flat
  1395. // list to simplify future iterations.
  1396. InstructionList body;
  1397. for (opt::BasicBlock* block : order) {
  1398. block->ForEachInst(
  1399. [&body](const opt::Instruction* inst) { body.push_back(inst); }, true);
  1400. }
  1401. body.push_back(function.EndInst());
  1402. return body;
  1403. }
  1404. InstructionList Differ::GetFunctionHeader(const opt::Function& function) {
  1405. // Go over the instructions of the function and add the header instructions to
  1406. // a flat list to simplify diff generation.
  1407. InstructionList body;
  1408. function.WhileEachInst(
  1409. [&body](const opt::Instruction* inst) {
  1410. if (inst->opcode() == spv::Op::OpLabel) {
  1411. return false;
  1412. }
  1413. body.push_back(inst);
  1414. return true;
  1415. },
  1416. true, true);
  1417. return body;
  1418. }
  1419. void Differ::GetFunctionBodies(opt::IRContext* context, FunctionMap* functions,
  1420. FunctionInstMap* function_insts) {
  1421. for (opt::Function& function : *context->module()) {
  1422. uint32_t id = function.result_id();
  1423. assert(functions->find(id) == functions->end());
  1424. assert(function_insts->find(id) == function_insts->end());
  1425. (*functions)[id] = &function;
  1426. InstructionList body = GetFunctionBody(context, function);
  1427. (*function_insts)[id] = std::move(body);
  1428. }
  1429. }
  1430. void Differ::GetFunctionHeaderInstructions(const opt::Module* module,
  1431. FunctionInstMap* function_insts) {
  1432. for (opt::Function& function : *module) {
  1433. InstructionList body = GetFunctionHeader(function);
  1434. (*function_insts)[function.result_id()] = std::move(body);
  1435. }
  1436. }
  1437. void Differ::BestEffortMatchFunctions(const IdGroup& src_func_ids,
  1438. const IdGroup& dst_func_ids,
  1439. const FunctionInstMap& src_func_insts,
  1440. const FunctionInstMap& dst_func_insts) {
  1441. struct MatchResult {
  1442. uint32_t src_id;
  1443. uint32_t dst_id;
  1444. DiffMatch src_match;
  1445. DiffMatch dst_match;
  1446. float match_rate;
  1447. bool operator<(const MatchResult& other) const {
  1448. return match_rate > other.match_rate;
  1449. }
  1450. };
  1451. std::vector<MatchResult> all_match_results;
  1452. for (const uint32_t src_func_id : src_func_ids) {
  1453. if (id_map_.IsSrcMapped(src_func_id)) {
  1454. continue;
  1455. }
  1456. const std::string src_name = GetSanitizedName(src_id_to_, src_func_id);
  1457. for (const uint32_t dst_func_id : dst_func_ids) {
  1458. if (id_map_.IsDstMapped(dst_func_id)) {
  1459. continue;
  1460. }
  1461. // Don't match functions that are named, but the names are different.
  1462. const std::string dst_name = GetSanitizedName(dst_id_to_, dst_func_id);
  1463. if (src_name != "" && dst_name != "" && src_name != dst_name) {
  1464. continue;
  1465. }
  1466. DiffMatch src_match_result, dst_match_result;
  1467. float match_rate = MatchFunctionBodies(
  1468. src_func_insts.at(src_func_id), dst_func_insts.at(dst_func_id),
  1469. &src_match_result, &dst_match_result);
  1470. // Only consider the functions a match if there's at least 60% match.
  1471. // This is an arbitrary limit that should be tuned.
  1472. constexpr float pass_match_rate = 0.6f;
  1473. if (match_rate >= pass_match_rate) {
  1474. all_match_results.emplace_back(
  1475. MatchResult{src_func_id, dst_func_id, std::move(src_match_result),
  1476. std::move(dst_match_result), match_rate});
  1477. }
  1478. }
  1479. }
  1480. std::sort(all_match_results.begin(), all_match_results.end());
  1481. for (const MatchResult& match_result : all_match_results) {
  1482. if (id_map_.IsSrcMapped(match_result.src_id) ||
  1483. id_map_.IsDstMapped(match_result.dst_id)) {
  1484. continue;
  1485. }
  1486. id_map_.MapIds(match_result.src_id, match_result.dst_id);
  1487. MatchFunctionParamIds(src_funcs_[match_result.src_id],
  1488. dst_funcs_[match_result.dst_id]);
  1489. MatchIdsInFunctionBodies(src_func_insts.at(match_result.src_id),
  1490. dst_func_insts.at(match_result.dst_id),
  1491. match_result.src_match, match_result.dst_match, 0);
  1492. }
  1493. }
  1494. void Differ::MatchFunctionParamIds(const opt::Function* src_func,
  1495. const opt::Function* dst_func) {
  1496. IdGroup src_params;
  1497. IdGroup dst_params;
  1498. src_func->ForEachParam(
  1499. [&src_params](const opt::Instruction* param) {
  1500. src_params.push_back(param->result_id());
  1501. },
  1502. false);
  1503. dst_func->ForEachParam(
  1504. [&dst_params](const opt::Instruction* param) {
  1505. dst_params.push_back(param->result_id());
  1506. },
  1507. false);
  1508. GroupIdsAndMatch<std::string>(
  1509. src_params, dst_params, "", &Differ::GetSanitizedName,
  1510. [this](const IdGroup& src_group, const IdGroup& dst_group) {
  1511. // There shouldn't be two parameters with the same name, so the ids
  1512. // should match. There is nothing restricting the SPIR-V however to have
  1513. // two parameters with the same name, so be resilient against that.
  1514. if (src_group.size() == 1 && dst_group.size() == 1) {
  1515. id_map_.MapIds(src_group[0], dst_group[0]);
  1516. }
  1517. });
  1518. // Then match the parameters by their type. If there are multiple of them,
  1519. // match them by their order.
  1520. GroupIdsAndMatchByMappedId(
  1521. src_params, dst_params, &Differ::GroupIdsHelperGetTypeId,
  1522. [this](const IdGroup& src_group_by_type_id,
  1523. const IdGroup& dst_group_by_type_id) {
  1524. const size_t shared_param_count =
  1525. std::min(src_group_by_type_id.size(), dst_group_by_type_id.size());
  1526. for (size_t param_index = 0; param_index < shared_param_count;
  1527. ++param_index) {
  1528. id_map_.MapIds(src_group_by_type_id[param_index],
  1529. dst_group_by_type_id[param_index]);
  1530. }
  1531. });
  1532. }
  1533. float Differ::MatchFunctionBodies(const InstructionList& src_body,
  1534. const InstructionList& dst_body,
  1535. DiffMatch* src_match_result,
  1536. DiffMatch* dst_match_result) {
  1537. LongestCommonSubsequence<std::vector<const opt::Instruction*>> lcs(src_body,
  1538. dst_body);
  1539. uint32_t best_match_length = lcs.Get<const opt::Instruction*>(
  1540. [this](const opt::Instruction* src_inst,
  1541. const opt::Instruction* dst_inst) {
  1542. return DoInstructionsMatchFuzzy(src_inst, dst_inst);
  1543. },
  1544. src_match_result, dst_match_result);
  1545. // TODO: take the gaps in between matches and match those again with a relaxed
  1546. // instruction-and-type-only comparison. This can produce a better diff for
  1547. // example if an array index is changed, causing the OpAccessChain id to not
  1548. // match and subsequently every operation that's derived from that id.
  1549. // Usually this mismatch cascades until the next OpStore which doesn't produce
  1550. // an id.
  1551. return static_cast<float>(best_match_length) * 2.0f /
  1552. static_cast<float>(src_body.size() + dst_body.size());
  1553. }
  1554. void Differ::MatchIdsInFunctionBodies(const InstructionList& src_body,
  1555. const InstructionList& dst_body,
  1556. const DiffMatch& src_match_result,
  1557. const DiffMatch& dst_match_result,
  1558. uint32_t flexibility) {
  1559. size_t src_cur = 0;
  1560. size_t dst_cur = 0;
  1561. while (src_cur < src_body.size() && dst_cur < dst_body.size()) {
  1562. if (src_match_result[src_cur] && dst_match_result[dst_cur]) {
  1563. // Match instructions the src and dst instructions.
  1564. //
  1565. // TODO: count the matchings between variables discovered this way and
  1566. // choose the "best match" after all functions have been diffed and all
  1567. // instructions analyzed.
  1568. const opt::Instruction* src_inst = src_body[src_cur++];
  1569. const opt::Instruction* dst_inst = dst_body[dst_cur++];
  1570. // Record the matching between the instructions. This is done only once
  1571. // (hence flexibility == 0). Calls with non-zero flexibility values will
  1572. // only deal with matching other ids based on the operands.
  1573. if (flexibility == 0) {
  1574. id_map_.MapInsts(src_inst, dst_inst);
  1575. }
  1576. // Match any unmatched variables referenced by the instructions.
  1577. MatchVariablesUsedByMatchedInstructions(src_inst, dst_inst, flexibility);
  1578. continue;
  1579. }
  1580. if (!src_match_result[src_cur]) {
  1581. ++src_cur;
  1582. }
  1583. if (!dst_match_result[dst_cur]) {
  1584. ++dst_cur;
  1585. }
  1586. }
  1587. }
  1588. void Differ::MatchVariablesUsedByMatchedInstructions(
  1589. const opt::Instruction* src_inst, const opt::Instruction* dst_inst,
  1590. uint32_t flexibility) {
  1591. // For OpAccessChain, OpLoad and OpStore instructions that reference unmatched
  1592. // variables, match them as a best effort.
  1593. assert(src_inst->opcode() == dst_inst->opcode());
  1594. switch (src_inst->opcode()) {
  1595. default:
  1596. // TODO: match functions based on OpFunctionCall?
  1597. break;
  1598. case spv::Op::OpAccessChain:
  1599. case spv::Op::OpInBoundsAccessChain:
  1600. case spv::Op::OpPtrAccessChain:
  1601. case spv::Op::OpInBoundsPtrAccessChain:
  1602. case spv::Op::OpLoad:
  1603. case spv::Op::OpStore:
  1604. const uint32_t src_pointer_id = src_inst->GetSingleWordInOperand(0);
  1605. const uint32_t dst_pointer_id = dst_inst->GetSingleWordInOperand(0);
  1606. if (IsVariable(src_id_to_, src_pointer_id) &&
  1607. IsVariable(dst_id_to_, dst_pointer_id) &&
  1608. !id_map_.IsSrcMapped(src_pointer_id) &&
  1609. !id_map_.IsDstMapped(dst_pointer_id) &&
  1610. AreVariablesMatchable(src_pointer_id, dst_pointer_id, flexibility)) {
  1611. id_map_.MapIds(src_pointer_id, dst_pointer_id);
  1612. }
  1613. break;
  1614. }
  1615. }
  1616. const opt::Instruction* Differ::GetInst(const IdInstructions& id_to,
  1617. uint32_t id) {
  1618. assert(id != 0);
  1619. assert(id < id_to.inst_map_.size());
  1620. const opt::Instruction* inst = id_to.inst_map_[id];
  1621. assert(inst != nullptr);
  1622. return inst;
  1623. }
  1624. uint32_t Differ::GetConstantUint(const IdInstructions& id_to,
  1625. uint32_t constant_id) {
  1626. const opt::Instruction* constant_inst = GetInst(id_to, constant_id);
  1627. assert(constant_inst->opcode() == spv::Op::OpConstant);
  1628. assert(GetInst(id_to, constant_inst->type_id())->opcode() ==
  1629. spv::Op::OpTypeInt);
  1630. return constant_inst->GetSingleWordInOperand(0);
  1631. }
  1632. spv::ExecutionModel Differ::GetExecutionModel(const opt::Module* module,
  1633. uint32_t entry_point_id) {
  1634. for (const opt::Instruction& inst : module->entry_points()) {
  1635. assert(inst.opcode() == spv::Op::OpEntryPoint);
  1636. if (inst.GetSingleWordOperand(1) == entry_point_id) {
  1637. return spv::ExecutionModel(inst.GetSingleWordOperand(0));
  1638. }
  1639. }
  1640. assert(false && "Unreachable");
  1641. return spv::ExecutionModel(0xFFF);
  1642. }
  1643. bool Differ::HasName(const IdInstructions& id_to, uint32_t id) {
  1644. assert(id != 0);
  1645. assert(id < id_to.name_map_.size());
  1646. for (const opt::Instruction* inst : id_to.name_map_[id]) {
  1647. if (inst->opcode() == spv::Op::OpName) {
  1648. return true;
  1649. }
  1650. }
  1651. return false;
  1652. }
  1653. std::string Differ::GetName(const IdInstructions& id_to, uint32_t id,
  1654. bool* has_name) {
  1655. assert(id != 0);
  1656. assert(id < id_to.name_map_.size());
  1657. for (const opt::Instruction* inst : id_to.name_map_[id]) {
  1658. if (inst->opcode() == spv::Op::OpName) {
  1659. *has_name = true;
  1660. return inst->GetOperand(1).AsString();
  1661. }
  1662. }
  1663. *has_name = false;
  1664. return "";
  1665. }
  1666. std::string Differ::GetSanitizedName(const IdInstructions& id_to, uint32_t id) {
  1667. bool has_name = false;
  1668. std::string name = GetName(id_to, id, &has_name);
  1669. if (!has_name) {
  1670. return "";
  1671. }
  1672. // Remove args from the name, in case this is a function name
  1673. return name.substr(0, name.find('('));
  1674. }
  1675. uint32_t Differ::GetVarTypeId(const IdInstructions& id_to, uint32_t var_id,
  1676. spv::StorageClass* storage_class) {
  1677. const opt::Instruction* var_inst = GetInst(id_to, var_id);
  1678. assert(var_inst->opcode() == spv::Op::OpVariable);
  1679. *storage_class = spv::StorageClass(var_inst->GetSingleWordInOperand(0));
  1680. // Get the type pointer from the variable.
  1681. const uint32_t type_pointer_id = var_inst->type_id();
  1682. const opt::Instruction* type_pointer_inst = GetInst(id_to, type_pointer_id);
  1683. // Get the type from the type pointer.
  1684. return type_pointer_inst->GetSingleWordInOperand(1);
  1685. }
  1686. bool Differ::GetDecorationValue(const IdInstructions& id_to, uint32_t id,
  1687. spv::Decoration decoration,
  1688. uint32_t* decoration_value) {
  1689. assert(id != 0);
  1690. assert(id < id_to.decoration_map_.size());
  1691. for (const opt::Instruction* inst : id_to.decoration_map_[id]) {
  1692. if (inst->opcode() == spv::Op::OpDecorate &&
  1693. inst->GetSingleWordOperand(0) == id &&
  1694. spv::Decoration(inst->GetSingleWordOperand(1)) == decoration) {
  1695. *decoration_value = inst->GetSingleWordOperand(2);
  1696. return true;
  1697. }
  1698. }
  1699. return false;
  1700. }
  1701. const opt::Instruction* Differ::GetForwardPointerInst(
  1702. const IdInstructions& id_to, uint32_t id) {
  1703. assert(id != 0);
  1704. assert(id < id_to.forward_pointer_map_.size());
  1705. return id_to.forward_pointer_map_[id];
  1706. }
  1707. bool Differ::IsIntType(const IdInstructions& id_to, uint32_t type_id) {
  1708. return IsOp(id_to, type_id, spv::Op::OpTypeInt);
  1709. }
  1710. bool Differ::IsFloatType(const IdInstructions& id_to, uint32_t type_id) {
  1711. return IsOp(id_to, type_id, spv::Op::OpTypeFloat);
  1712. }
  1713. bool Differ::IsConstantUint(const IdInstructions& id_to, uint32_t id) {
  1714. const opt::Instruction* constant_inst = GetInst(id_to, id);
  1715. if (constant_inst->opcode() != spv::Op::OpConstant) {
  1716. return false;
  1717. }
  1718. const opt::Instruction* type_inst = GetInst(id_to, constant_inst->type_id());
  1719. return type_inst->opcode() == spv::Op::OpTypeInt;
  1720. }
  1721. bool Differ::IsVariable(const IdInstructions& id_to, uint32_t pointer_id) {
  1722. return IsOp(id_to, pointer_id, spv::Op::OpVariable);
  1723. }
  1724. bool Differ::IsOp(const IdInstructions& id_to, uint32_t id, spv::Op op) {
  1725. return GetInst(id_to, id)->opcode() == op;
  1726. }
  1727. bool Differ::IsPerVertexType(const IdInstructions& id_to, uint32_t type_id) {
  1728. assert(type_id != 0);
  1729. assert(type_id < id_to.decoration_map_.size());
  1730. for (const opt::Instruction* inst : id_to.decoration_map_[type_id]) {
  1731. if (inst->opcode() == spv::Op::OpMemberDecorate &&
  1732. inst->GetSingleWordOperand(0) == type_id &&
  1733. spv::Decoration(inst->GetSingleWordOperand(2)) ==
  1734. spv::Decoration::BuiltIn) {
  1735. spv::BuiltIn built_in = spv::BuiltIn(inst->GetSingleWordOperand(3));
  1736. // Only gl_PerVertex can have, and it can only have, the following
  1737. // built-in decorations.
  1738. return built_in == spv::BuiltIn::Position ||
  1739. built_in == spv::BuiltIn::PointSize ||
  1740. built_in == spv::BuiltIn::ClipDistance ||
  1741. built_in == spv::BuiltIn::CullDistance;
  1742. }
  1743. }
  1744. return false;
  1745. }
  1746. bool Differ::IsPerVertexVariable(const IdInstructions& id_to, uint32_t var_id) {
  1747. // Get the type from the type pointer.
  1748. spv::StorageClass storage_class;
  1749. uint32_t type_id = GetVarTypeId(id_to, var_id, &storage_class);
  1750. const opt::Instruction* type_inst = GetInst(id_to, type_id);
  1751. // If array, get the element type.
  1752. if (type_inst->opcode() == spv::Op::OpTypeArray) {
  1753. type_id = type_inst->GetSingleWordInOperand(0);
  1754. }
  1755. // Now check if the type is gl_PerVertex.
  1756. return IsPerVertexType(id_to, type_id);
  1757. }
  1758. spv::StorageClass Differ::GetPerVertexStorageClass(const opt::Module* module,
  1759. uint32_t type_id) {
  1760. for (const opt::Instruction& inst : module->types_values()) {
  1761. switch (inst.opcode()) {
  1762. case spv::Op::OpTypeArray:
  1763. // The gl_PerVertex instance could be an array, look for a variable of
  1764. // the array type instead.
  1765. if (inst.GetSingleWordInOperand(0) == type_id) {
  1766. type_id = inst.result_id();
  1767. }
  1768. break;
  1769. case spv::Op::OpTypePointer:
  1770. // Find the storage class of the pointer to this type.
  1771. if (inst.GetSingleWordInOperand(1) == type_id) {
  1772. return spv::StorageClass(inst.GetSingleWordInOperand(0));
  1773. }
  1774. break;
  1775. default:
  1776. break;
  1777. }
  1778. }
  1779. // gl_PerVertex is declared, but is unused. Return either of Input or Output
  1780. // classes just so it matches one in the other module. This should be highly
  1781. // unlikely, perhaps except for ancient GS-used-to-emulate-CS scenarios.
  1782. return spv::StorageClass::Output;
  1783. }
  1784. spv_ext_inst_type_t Differ::GetExtInstType(const IdInstructions& id_to,
  1785. uint32_t set_id) {
  1786. const opt::Instruction* set_inst = GetInst(id_to, set_id);
  1787. return spvExtInstImportTypeGet(set_inst->GetInOperand(0).AsString().c_str());
  1788. }
  1789. spv_number_kind_t Differ::GetNumberKind(const IdInstructions& id_to,
  1790. const opt::Instruction& inst,
  1791. uint32_t operand_index,
  1792. uint32_t* number_bit_width) {
  1793. const opt::Operand& operand = inst.GetOperand(operand_index);
  1794. *number_bit_width = 0;
  1795. // A very limited version of Parser::parseOperand.
  1796. switch (operand.type) {
  1797. case SPV_OPERAND_TYPE_LITERAL_INTEGER:
  1798. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
  1799. // Always unsigned integers.
  1800. *number_bit_width = 32;
  1801. return SPV_NUMBER_UNSIGNED_INT;
  1802. case SPV_OPERAND_TYPE_LITERAL_FLOAT:
  1803. // Always float.
  1804. *number_bit_width = 32;
  1805. return SPV_NUMBER_FLOATING;
  1806. case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
  1807. case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
  1808. switch (inst.opcode()) {
  1809. case spv::Op::OpSwitch:
  1810. case spv::Op::OpConstant:
  1811. case spv::Op::OpSpecConstant:
  1812. // Same kind of number as the selector (OpSwitch) or the type
  1813. // (Op*Constant).
  1814. return GetTypeNumberKind(id_to, inst.GetSingleWordOperand(0),
  1815. number_bit_width);
  1816. default:
  1817. assert(false && "Unreachable");
  1818. break;
  1819. }
  1820. break;
  1821. default:
  1822. break;
  1823. }
  1824. return SPV_NUMBER_NONE;
  1825. }
  1826. spv_number_kind_t Differ::GetTypeNumberKind(const IdInstructions& id_to,
  1827. uint32_t id,
  1828. uint32_t* number_bit_width) {
  1829. const opt::Instruction* type_inst = GetInst(id_to, id);
  1830. if (!spvOpcodeIsScalarType(type_inst->opcode())) {
  1831. type_inst = GetInst(id_to, type_inst->type_id());
  1832. }
  1833. switch (type_inst->opcode()) {
  1834. case spv::Op::OpTypeInt:
  1835. *number_bit_width = type_inst->GetSingleWordOperand(1);
  1836. return type_inst->GetSingleWordOperand(2) == 0 ? SPV_NUMBER_UNSIGNED_INT
  1837. : SPV_NUMBER_SIGNED_INT;
  1838. break;
  1839. case spv::Op::OpTypeFloat:
  1840. *number_bit_width = type_inst->GetSingleWordOperand(1);
  1841. return SPV_NUMBER_FLOATING;
  1842. default:
  1843. assert(false && "Unreachable");
  1844. return SPV_NUMBER_NONE;
  1845. }
  1846. }
  1847. void Differ::MatchCapabilities() {
  1848. MatchPreambleInstructions(src_->capabilities(), dst_->capabilities());
  1849. }
  1850. void Differ::MatchExtensions() {
  1851. MatchPreambleInstructions(src_->extensions(), dst_->extensions());
  1852. }
  1853. void Differ::MatchExtInstImportIds() {
  1854. // Bunch all of this section's ids as potential matches.
  1855. PotentialIdMap potential_id_map;
  1856. auto get_result_id = [](const opt::Instruction& inst) {
  1857. return inst.result_id();
  1858. };
  1859. auto accept_all = [](const opt::Instruction&) { return true; };
  1860. PoolPotentialIds(src_->ext_inst_imports(), potential_id_map.src_ids, true,
  1861. accept_all, get_result_id);
  1862. PoolPotentialIds(dst_->ext_inst_imports(), potential_id_map.dst_ids, false,
  1863. accept_all, get_result_id);
  1864. // Then match the ids.
  1865. MatchIds(potential_id_map, [](const opt::Instruction* src_inst,
  1866. const opt::Instruction* dst_inst) {
  1867. // Match OpExtInstImport by exact name, which is operand 1
  1868. const opt::Operand& src_name = src_inst->GetOperand(1);
  1869. const opt::Operand& dst_name = dst_inst->GetOperand(1);
  1870. return src_name.AsString() == dst_name.AsString();
  1871. });
  1872. }
  1873. void Differ::MatchMemoryModel() {
  1874. // Always match the memory model instructions, there is always a single one of
  1875. // it.
  1876. id_map_.MapInsts(src_->GetMemoryModel(), dst_->GetMemoryModel());
  1877. }
  1878. void Differ::MatchEntryPointIds() {
  1879. // Match OpEntryPoint ids (at index 1) by ExecutionModel (at index 0) and
  1880. // possibly name (at index 2). OpEntryPoint doesn't produce a result id, so
  1881. // this function doesn't use the helpers the other functions use.
  1882. // Map from execution model to OpEntryPoint instructions of that model.
  1883. using ExecutionModelMap =
  1884. std::unordered_map<uint32_t, std::vector<const opt::Instruction*>>;
  1885. ExecutionModelMap src_entry_points_map;
  1886. ExecutionModelMap dst_entry_points_map;
  1887. std::set<uint32_t> all_execution_models;
  1888. for (const opt::Instruction& src_inst : src_->entry_points()) {
  1889. uint32_t execution_model = src_inst.GetSingleWordOperand(0);
  1890. src_entry_points_map[execution_model].push_back(&src_inst);
  1891. all_execution_models.insert(execution_model);
  1892. }
  1893. for (const opt::Instruction& dst_inst : dst_->entry_points()) {
  1894. uint32_t execution_model = dst_inst.GetSingleWordOperand(0);
  1895. dst_entry_points_map[execution_model].push_back(&dst_inst);
  1896. all_execution_models.insert(execution_model);
  1897. }
  1898. // Go through each model and match the ids.
  1899. for (const uint32_t execution_model : all_execution_models) {
  1900. auto& src_insts = src_entry_points_map[execution_model];
  1901. auto& dst_insts = dst_entry_points_map[execution_model];
  1902. // If there is only one entry point in src and dst with that model, match
  1903. // them unconditionally.
  1904. if (src_insts.size() == 1 && dst_insts.size() == 1) {
  1905. uint32_t src_id = src_insts[0]->GetSingleWordOperand(1);
  1906. uint32_t dst_id = dst_insts[0]->GetSingleWordOperand(1);
  1907. id_map_.MapIds(src_id, dst_id);
  1908. id_map_.MapInsts(src_insts[0], dst_insts[0]);
  1909. continue;
  1910. }
  1911. // Otherwise match them by name.
  1912. for (const opt::Instruction* src_inst : src_insts) {
  1913. for (const opt::Instruction* dst_inst : dst_insts) {
  1914. if (id_map_.IsDstMapped(dst_inst)) continue;
  1915. const opt::Operand& src_name = src_inst->GetOperand(2);
  1916. const opt::Operand& dst_name = dst_inst->GetOperand(2);
  1917. if (src_name.AsString() == dst_name.AsString()) {
  1918. uint32_t src_id = src_inst->GetSingleWordOperand(1);
  1919. uint32_t dst_id = dst_inst->GetSingleWordOperand(1);
  1920. id_map_.MapIds(src_id, dst_id);
  1921. id_map_.MapInsts(src_inst, dst_inst);
  1922. break;
  1923. }
  1924. }
  1925. }
  1926. }
  1927. }
  1928. void Differ::MatchExecutionModes() {
  1929. MatchPreambleInstructions(src_->execution_modes(), dst_->execution_modes());
  1930. }
  1931. void Differ::MatchTypeForwardPointers() {
  1932. // Bunch all of type forward pointers as potential matches.
  1933. PotentialIdMap potential_id_map;
  1934. auto get_pointer_type_id = [](const opt::Instruction& inst) {
  1935. return inst.GetSingleWordOperand(0);
  1936. };
  1937. auto accept_type_forward_pointer_ops = [](const opt::Instruction& inst) {
  1938. return inst.opcode() == spv::Op::OpTypeForwardPointer;
  1939. };
  1940. PoolPotentialIds(src_->types_values(), potential_id_map.src_ids, true,
  1941. accept_type_forward_pointer_ops, get_pointer_type_id);
  1942. PoolPotentialIds(dst_->types_values(), potential_id_map.dst_ids, false,
  1943. accept_type_forward_pointer_ops, get_pointer_type_id);
  1944. // Matching types with cyclical references (i.e. in the style of linked lists)
  1945. // can get very complex. Currently, the diff tool matches types bottom up, so
  1946. // on every instruction it expects to know if its operands are already matched
  1947. // or not. With cyclical references, it cannot know that. Type matching may
  1948. // need significant modifications to be able to support this use case.
  1949. //
  1950. // Currently, forwarded types are only matched by storage class and debug
  1951. // info, with minimal matching of the type being forwarded:
  1952. //
  1953. // - Group by class
  1954. // - Group by OpType being pointed to
  1955. // - Group by debug info
  1956. // - If same name and unique, match
  1957. // - If leftover is unique, match
  1958. // Group forwarded pointers by storage class first and loop over them.
  1959. GroupIdsAndMatch<spv::StorageClass>(
  1960. potential_id_map.src_ids, potential_id_map.dst_ids,
  1961. spv::StorageClass::Max, &Differ::GroupIdsHelperGetTypePointerStorageClass,
  1962. [this](const IdGroup& src_group_by_storage_class,
  1963. const IdGroup& dst_group_by_storage_class) {
  1964. // Group them further by the type they are pointing to and loop over
  1965. // them.
  1966. GroupIdsAndMatch<spv::Op>(
  1967. src_group_by_storage_class, dst_group_by_storage_class,
  1968. spv::Op::Max, &Differ::GroupIdsHelperGetTypePointerTypeOp,
  1969. [this](const IdGroup& src_group_by_type_op,
  1970. const IdGroup& dst_group_by_type_op) {
  1971. // Group them even further by debug info, if possible and match by
  1972. // debug name.
  1973. MatchTypeForwardPointersByName(src_group_by_type_op,
  1974. dst_group_by_type_op);
  1975. // Match the leftovers only if they lack debug info and there is
  1976. // only one instance of them.
  1977. MatchTypeForwardPointersByTypeOp(src_group_by_type_op,
  1978. dst_group_by_type_op);
  1979. });
  1980. });
  1981. // Match the instructions that forward declare the same type themselves
  1982. for (uint32_t src_id : potential_id_map.src_ids) {
  1983. uint32_t dst_id = id_map_.MappedDstId(src_id);
  1984. if (dst_id == 0) continue;
  1985. const opt::Instruction* src_forward_inst =
  1986. GetForwardPointerInst(src_id_to_, src_id);
  1987. const opt::Instruction* dst_forward_inst =
  1988. GetForwardPointerInst(dst_id_to_, dst_id);
  1989. assert(src_forward_inst);
  1990. assert(dst_forward_inst);
  1991. id_map_.MapInsts(src_forward_inst, dst_forward_inst);
  1992. }
  1993. }
  1994. void Differ::MatchTypeIds() {
  1995. // Bunch all of type ids as potential matches.
  1996. PotentialIdMap potential_id_map;
  1997. auto get_result_id = [](const opt::Instruction& inst) {
  1998. return inst.result_id();
  1999. };
  2000. auto accept_type_ops = [](const opt::Instruction& inst) {
  2001. return spvOpcodeGeneratesType(inst.opcode());
  2002. };
  2003. PoolPotentialIds(src_->types_values(), potential_id_map.src_ids, true,
  2004. accept_type_ops, get_result_id);
  2005. PoolPotentialIds(dst_->types_values(), potential_id_map.dst_ids, false,
  2006. accept_type_ops, get_result_id);
  2007. // Then match the ids. Start with exact matches, then match the leftover with
  2008. // gradually loosening degrees of strictness. For example, in the absence of
  2009. // debug info, two block types will be matched if they differ only in a few of
  2010. // the fields.
  2011. for (uint32_t flexibility = 0; flexibility < 2; ++flexibility) {
  2012. MatchIds(potential_id_map, [this, flexibility](
  2013. const opt::Instruction* src_inst,
  2014. const opt::Instruction* dst_inst) {
  2015. const spv::Op src_op = src_inst->opcode();
  2016. const spv::Op dst_op = dst_inst->opcode();
  2017. // Don't match if the opcode is not the same.
  2018. if (src_op != dst_op) {
  2019. return false;
  2020. }
  2021. switch (src_op) {
  2022. case spv::Op::OpTypeVoid:
  2023. case spv::Op::OpTypeBool:
  2024. case spv::Op::OpTypeSampler:
  2025. case spv::Op::OpTypeAccelerationStructureNV:
  2026. case spv::Op::OpTypeRayQueryKHR:
  2027. // the above types have no operands and are unique, match them.
  2028. return true;
  2029. case spv::Op::OpTypeInt:
  2030. case spv::Op::OpTypeFloat:
  2031. case spv::Op::OpTypeVector:
  2032. case spv::Op::OpTypeMatrix:
  2033. case spv::Op::OpTypeSampledImage:
  2034. case spv::Op::OpTypeRuntimeArray:
  2035. case spv::Op::OpTypePointer:
  2036. // Match these instructions when all operands match.
  2037. assert(src_inst->NumInOperandWords() ==
  2038. dst_inst->NumInOperandWords());
  2039. return DoOperandsMatch(src_inst, dst_inst, 0,
  2040. src_inst->NumInOperandWords());
  2041. case spv::Op::OpTypeFunction:
  2042. case spv::Op::OpTypeImage:
  2043. // Match function types only if they have the same number of operands,
  2044. // and they all match.
  2045. // Match image types similarly, expecting the optional final parameter
  2046. // to match (if provided in both)
  2047. if (src_inst->NumInOperandWords() != dst_inst->NumInOperandWords()) {
  2048. return false;
  2049. }
  2050. return DoOperandsMatch(src_inst, dst_inst, 0,
  2051. src_inst->NumInOperandWords());
  2052. case spv::Op::OpTypeArray:
  2053. // Match arrays only if the element type and length match. The length
  2054. // is an id of a constant, so the actual constant it's defining is
  2055. // compared instead.
  2056. if (!DoOperandsMatch(src_inst, dst_inst, 0, 1)) {
  2057. return false;
  2058. }
  2059. if (AreIdenticalUintConstants(src_inst->GetSingleWordInOperand(1),
  2060. dst_inst->GetSingleWordInOperand(1))) {
  2061. return true;
  2062. }
  2063. // If size is not OpConstant, expect the ids to match exactly (for
  2064. // example if a spec contant is used).
  2065. return DoOperandsMatch(src_inst, dst_inst, 1, 1);
  2066. case spv::Op::OpTypeStruct:
  2067. return MatchOpTypeStruct(src_inst, dst_inst, flexibility);
  2068. default:
  2069. return false;
  2070. }
  2071. });
  2072. }
  2073. }
  2074. void Differ::MatchConstants() {
  2075. // Bunch all of constant ids as potential matches.
  2076. PotentialIdMap potential_id_map;
  2077. auto get_result_id = [](const opt::Instruction& inst) {
  2078. return inst.result_id();
  2079. };
  2080. auto accept_type_ops = [](const opt::Instruction& inst) {
  2081. return spvOpcodeIsConstant(inst.opcode());
  2082. };
  2083. PoolPotentialIds(src_->types_values(), potential_id_map.src_ids, true,
  2084. accept_type_ops, get_result_id);
  2085. PoolPotentialIds(dst_->types_values(), potential_id_map.dst_ids, false,
  2086. accept_type_ops, get_result_id);
  2087. // Then match the ids. Constants are matched exactly, except for float types
  2088. // that are first matched exactly, then leftovers are matched with a small
  2089. // error.
  2090. for (uint32_t flexibility = 0; flexibility < 2; ++flexibility) {
  2091. MatchIds(potential_id_map, [this, flexibility](
  2092. const opt::Instruction* src_inst,
  2093. const opt::Instruction* dst_inst) {
  2094. const spv::Op src_op = src_inst->opcode();
  2095. const spv::Op dst_op = dst_inst->opcode();
  2096. // Don't match if the opcode is not the same.
  2097. if (src_op != dst_op) {
  2098. return false;
  2099. }
  2100. switch (src_op) {
  2101. case spv::Op::OpConstantTrue:
  2102. case spv::Op::OpConstantFalse:
  2103. // true and false are unique, match them.
  2104. return true;
  2105. case spv::Op::OpConstant:
  2106. return MatchOpConstant(src_inst, dst_inst, flexibility);
  2107. case spv::Op::OpConstantComposite:
  2108. case spv::Op::OpSpecConstantComposite:
  2109. // Composite constants must match in type and value.
  2110. //
  2111. // TODO: match OpConstantNull with OpConstantComposite with all zeros
  2112. // at flexibility == 1
  2113. // TODO: match constants from structs that have been flexibly-matched.
  2114. if (src_inst->NumInOperandWords() != dst_inst->NumInOperandWords()) {
  2115. return false;
  2116. }
  2117. return DoesOperandMatch(src_inst->GetOperand(0),
  2118. dst_inst->GetOperand(0)) &&
  2119. DoOperandsMatch(src_inst, dst_inst, 0,
  2120. src_inst->NumInOperandWords());
  2121. case spv::Op::OpConstantSampler:
  2122. // Match sampler constants exactly.
  2123. // TODO: Allow flexibility in parameters to better diff shaders where
  2124. // the sampler param has changed.
  2125. assert(src_inst->NumInOperandWords() ==
  2126. dst_inst->NumInOperandWords());
  2127. return DoOperandsMatch(src_inst, dst_inst, 0,
  2128. src_inst->NumInOperandWords());
  2129. case spv::Op::OpConstantNull:
  2130. // Match null constants as long as the type matches.
  2131. return DoesOperandMatch(src_inst->GetOperand(0),
  2132. dst_inst->GetOperand(0));
  2133. case spv::Op::OpSpecConstantTrue:
  2134. case spv::Op::OpSpecConstantFalse:
  2135. case spv::Op::OpSpecConstant:
  2136. case spv::Op::OpSpecConstantOp:
  2137. // Match spec constants by name if available, then by the SpecId
  2138. // decoration.
  2139. return MatchOpSpecConstant(src_inst, dst_inst);
  2140. default:
  2141. return false;
  2142. }
  2143. });
  2144. }
  2145. }
  2146. void Differ::MatchVariableIds() {
  2147. // Bunch all of variable ids as potential matches.
  2148. PotentialIdMap potential_id_map;
  2149. auto get_result_id = [](const opt::Instruction& inst) {
  2150. return inst.result_id();
  2151. };
  2152. auto accept_type_ops = [](const opt::Instruction& inst) {
  2153. return inst.opcode() == spv::Op::OpVariable;
  2154. };
  2155. PoolPotentialIds(src_->types_values(), potential_id_map.src_ids, true,
  2156. accept_type_ops, get_result_id);
  2157. PoolPotentialIds(dst_->types_values(), potential_id_map.dst_ids, false,
  2158. accept_type_ops, get_result_id);
  2159. // Then match the ids. Start with exact matches, then match the leftover with
  2160. // gradually loosening degrees of strictness. For example, in the absence of
  2161. // debug info, two otherwise identical variables will be matched if one of
  2162. // them has a Private storage class and the other doesn't.
  2163. for (uint32_t flexibility = 0; flexibility < 2; ++flexibility) {
  2164. MatchIds(potential_id_map,
  2165. [this, flexibility](const opt::Instruction* src_inst,
  2166. const opt::Instruction* dst_inst) {
  2167. assert(src_inst->opcode() == spv::Op::OpVariable);
  2168. assert(dst_inst->opcode() == spv::Op::OpVariable);
  2169. return MatchOpVariable(src_inst, dst_inst, flexibility);
  2170. });
  2171. }
  2172. }
  2173. void Differ::MatchFunctions() {
  2174. IdGroup src_func_ids;
  2175. IdGroup dst_func_ids;
  2176. for (const auto& func : src_funcs_) {
  2177. src_func_ids.push_back(func.first);
  2178. }
  2179. for (const auto& func : dst_funcs_) {
  2180. dst_func_ids.push_back(func.first);
  2181. }
  2182. // Base the matching of functions on debug info when available.
  2183. GroupIdsAndMatch<std::string>(
  2184. src_func_ids, dst_func_ids, "", &Differ::GetSanitizedName,
  2185. [this](const IdGroup& src_group, const IdGroup& dst_group) {
  2186. // If there is a single function with this name in src and dst, it's a
  2187. // definite match.
  2188. if (src_group.size() == 1 && dst_group.size() == 1) {
  2189. id_map_.MapIds(src_group[0], dst_group[0]);
  2190. return;
  2191. }
  2192. // If there are multiple functions with the same name, group them by
  2193. // type, and match only if the types match (and are unique).
  2194. GroupIdsAndMatchByMappedId(src_group, dst_group,
  2195. &Differ::GroupIdsHelperGetTypeId,
  2196. [this](const IdGroup& src_group_by_type_id,
  2197. const IdGroup& dst_group_by_type_id) {
  2198. if (src_group_by_type_id.size() == 1 &&
  2199. dst_group_by_type_id.size() == 1) {
  2200. id_map_.MapIds(src_group_by_type_id[0],
  2201. dst_group_by_type_id[0]);
  2202. }
  2203. });
  2204. });
  2205. // Any functions that are left are pooled together and matched as if unnamed,
  2206. // with the only exception that two functions with mismatching names are not
  2207. // matched.
  2208. //
  2209. // Before that however, the diff of the functions that are matched are taken
  2210. // and processed, so that more of the global variables can be matched before
  2211. // attempting to match the rest of the functions. They can contribute to the
  2212. // precision of the diff of those functions.
  2213. for (const uint32_t src_func_id : src_func_ids) {
  2214. const uint32_t dst_func_id = id_map_.MappedDstId(src_func_id);
  2215. if (dst_func_id == 0) {
  2216. continue;
  2217. }
  2218. // Since these functions are definite matches, match their parameters for a
  2219. // better diff.
  2220. MatchFunctionParamIds(src_funcs_[src_func_id], dst_funcs_[dst_func_id]);
  2221. // Take the diff of the two functions.
  2222. DiffMatch src_match_result, dst_match_result;
  2223. MatchFunctionBodies(src_func_insts_[src_func_id],
  2224. dst_func_insts_[dst_func_id], &src_match_result,
  2225. &dst_match_result);
  2226. // Match ids between the two function bodies; which can also result in
  2227. // global variables getting matched.
  2228. MatchIdsInFunctionBodies(src_func_insts_[src_func_id],
  2229. dst_func_insts_[dst_func_id], src_match_result,
  2230. dst_match_result, 0);
  2231. }
  2232. // Best effort match functions with matching return and argument types.
  2233. GroupIdsAndMatchByMappedId(
  2234. src_func_ids, dst_func_ids, &Differ::GroupIdsHelperGetFunctionTypeId,
  2235. [this](const IdGroup& src_group_by_func_type_id,
  2236. const IdGroup& dst_group_by_func_type_id) {
  2237. BestEffortMatchFunctions(src_group_by_func_type_id,
  2238. dst_group_by_func_type_id, src_func_insts_,
  2239. dst_func_insts_);
  2240. });
  2241. // Best effort match functions with matching return types.
  2242. GroupIdsAndMatchByMappedId(
  2243. src_func_ids, dst_func_ids, &Differ::GroupIdsHelperGetTypeId,
  2244. [this](const IdGroup& src_group_by_type_id,
  2245. const IdGroup& dst_group_by_type_id) {
  2246. BestEffortMatchFunctions(src_group_by_type_id, dst_group_by_type_id,
  2247. src_func_insts_, dst_func_insts_);
  2248. });
  2249. // Any function that's left, best effort match them.
  2250. BestEffortMatchFunctions(src_func_ids, dst_func_ids, src_func_insts_,
  2251. dst_func_insts_);
  2252. }
  2253. void Differ::MatchDebugs1() {
  2254. // This section in cludes: OpString, OpSourceExtension, OpSource,
  2255. // OpSourceContinued
  2256. MatchDebugAndAnnotationInstructions(src_->debugs1(), dst_->debugs1());
  2257. }
  2258. void Differ::MatchDebugs2() {
  2259. // This section includes: OpName, OpMemberName
  2260. MatchDebugAndAnnotationInstructions(src_->debugs2(), dst_->debugs2());
  2261. }
  2262. void Differ::MatchDebugs3() {
  2263. // This section includes: OpModuleProcessed
  2264. MatchDebugAndAnnotationInstructions(src_->debugs3(), dst_->debugs3());
  2265. }
  2266. void Differ::MatchExtInstDebugInfo() {
  2267. // This section includes OpExtInst for DebugInfo extension
  2268. MatchDebugAndAnnotationInstructions(src_->ext_inst_debuginfo(),
  2269. dst_->ext_inst_debuginfo());
  2270. }
  2271. void Differ::MatchAnnotations() {
  2272. // This section includes OpDecorate and family.
  2273. MatchDebugAndAnnotationInstructions(src_->annotations(), dst_->annotations());
  2274. }
  2275. const opt::Instruction* Differ::MappedDstInst(
  2276. const opt::Instruction* src_inst) {
  2277. return MappedInstImpl(src_inst, id_map_.SrcToDstMap(), dst_id_to_);
  2278. }
  2279. const opt::Instruction* Differ::MappedSrcInst(
  2280. const opt::Instruction* dst_inst) {
  2281. return MappedInstImpl(dst_inst, id_map_.DstToSrcMap(), src_id_to_);
  2282. }
  2283. const opt::Instruction* Differ::MappedInstImpl(
  2284. const opt::Instruction* inst, const IdMap& to_other,
  2285. const IdInstructions& other_id_to) {
  2286. if (inst->HasResultId()) {
  2287. if (to_other.IsMapped(inst->result_id())) {
  2288. const uint32_t other_result_id = to_other.MappedId(inst->result_id());
  2289. assert(other_result_id < other_id_to.inst_map_.size());
  2290. return other_id_to.inst_map_[other_result_id];
  2291. }
  2292. return nullptr;
  2293. }
  2294. return to_other.MappedInst(inst);
  2295. }
  2296. void Differ::OutputLine(std::function<bool()> are_lines_identical,
  2297. std::function<void()> output_src_line,
  2298. std::function<void()> output_dst_line) {
  2299. if (are_lines_identical()) {
  2300. out_ << " ";
  2301. output_src_line();
  2302. } else {
  2303. OutputRed();
  2304. out_ << "-";
  2305. output_src_line();
  2306. OutputGreen();
  2307. out_ << "+";
  2308. output_dst_line();
  2309. OutputResetColor();
  2310. }
  2311. }
  2312. const opt::Instruction* IterInst(opt::Module::const_inst_iterator& iter) {
  2313. return &*iter;
  2314. }
  2315. const opt::Instruction* IterInst(InstructionList::const_iterator& iter) {
  2316. return *iter;
  2317. }
  2318. template <typename InstList>
  2319. void Differ::OutputSection(
  2320. const InstList& src_insts, const InstList& dst_insts,
  2321. std::function<void(const opt::Instruction&, const IdInstructions&,
  2322. const opt::Instruction&)>
  2323. write_inst) {
  2324. auto src_iter = src_insts.begin();
  2325. auto dst_iter = dst_insts.begin();
  2326. // - While src_inst doesn't have a match, output it with -
  2327. // - While dst_inst doesn't have a match, output it with +
  2328. // - Now src_inst and dst_inst both have matches; might not match each other!
  2329. // * If section is unordered, just process src_inst and its match (dst_inst
  2330. // or not),
  2331. // dst_inst will eventually be processed when its match is seen.
  2332. // * If section is ordered, also just process src_inst and its match. Its
  2333. // match must
  2334. // necessarily be dst_inst.
  2335. while (src_iter != src_insts.end() || dst_iter != dst_insts.end()) {
  2336. OutputRed();
  2337. while (src_iter != src_insts.end() &&
  2338. MappedDstInst(IterInst(src_iter)) == nullptr) {
  2339. out_ << "-";
  2340. write_inst(*IterInst(src_iter), src_id_to_, *IterInst(src_iter));
  2341. ++src_iter;
  2342. }
  2343. OutputGreen();
  2344. while (dst_iter != dst_insts.end() &&
  2345. MappedSrcInst(IterInst(dst_iter)) == nullptr) {
  2346. out_ << "+";
  2347. write_inst(ToMappedSrcIds(*IterInst(dst_iter)), dst_id_to_,
  2348. *IterInst(dst_iter));
  2349. ++dst_iter;
  2350. }
  2351. OutputResetColor();
  2352. if (src_iter != src_insts.end() && dst_iter != dst_insts.end()) {
  2353. const opt::Instruction* src_inst = IterInst(src_iter);
  2354. const opt::Instruction* matched_dst_inst = MappedDstInst(src_inst);
  2355. assert(matched_dst_inst != nullptr);
  2356. assert(MappedSrcInst(IterInst(dst_iter)) != nullptr);
  2357. OutputLine(
  2358. [this, src_inst, matched_dst_inst]() {
  2359. return DoInstructionsMatch(src_inst, matched_dst_inst);
  2360. },
  2361. [this, src_inst, &write_inst]() {
  2362. write_inst(*src_inst, src_id_to_, *src_inst);
  2363. },
  2364. [this, matched_dst_inst, &write_inst]() {
  2365. write_inst(ToMappedSrcIds(*matched_dst_inst), dst_id_to_,
  2366. *matched_dst_inst);
  2367. });
  2368. ++src_iter;
  2369. ++dst_iter;
  2370. }
  2371. }
  2372. }
  2373. void Differ::ToParsedInstruction(
  2374. const opt::Instruction& inst, const IdInstructions& id_to,
  2375. const opt::Instruction& original_inst,
  2376. spv_parsed_instruction_t* parsed_inst,
  2377. std::vector<spv_parsed_operand_t>& parsed_operands,
  2378. std::vector<uint32_t>& inst_binary) {
  2379. inst.ToBinaryWithoutAttachedDebugInsts(&inst_binary);
  2380. parsed_operands.resize(inst.NumOperands());
  2381. parsed_inst->words = inst_binary.data();
  2382. parsed_inst->num_words = static_cast<uint16_t>(inst_binary.size());
  2383. parsed_inst->opcode = static_cast<uint16_t>(inst.opcode());
  2384. parsed_inst->ext_inst_type =
  2385. inst.opcode() == spv::Op::OpExtInst
  2386. ? GetExtInstType(id_to, original_inst.GetSingleWordInOperand(0))
  2387. : SPV_EXT_INST_TYPE_NONE;
  2388. parsed_inst->type_id =
  2389. inst.HasResultType() ? inst.GetSingleWordOperand(0) : 0;
  2390. parsed_inst->result_id = inst.HasResultId() ? inst.result_id() : 0;
  2391. parsed_inst->operands = parsed_operands.data();
  2392. parsed_inst->num_operands = static_cast<uint16_t>(parsed_operands.size());
  2393. // Word 0 is always op and num_words, so operands start at offset 1.
  2394. uint32_t offset = 1;
  2395. for (uint16_t operand_index = 0; operand_index < parsed_inst->num_operands;
  2396. ++operand_index) {
  2397. const opt::Operand& operand = inst.GetOperand(operand_index);
  2398. spv_parsed_operand_t& parsed_operand = parsed_operands[operand_index];
  2399. parsed_operand.offset = static_cast<uint16_t>(offset);
  2400. parsed_operand.num_words = static_cast<uint16_t>(operand.words.size());
  2401. parsed_operand.type = operand.type;
  2402. parsed_operand.number_kind = GetNumberKind(
  2403. id_to, original_inst, operand_index, &parsed_operand.number_bit_width);
  2404. offset += parsed_operand.num_words;
  2405. }
  2406. }
  2407. opt::Instruction Differ::ToMappedSrcIds(const opt::Instruction& dst_inst) {
  2408. // Create an identical instruction to dst_inst, except ids are changed to the
  2409. // mapped one.
  2410. opt::Instruction mapped_inst = dst_inst;
  2411. for (uint32_t operand_index = 0; operand_index < mapped_inst.NumOperands();
  2412. ++operand_index) {
  2413. opt::Operand& operand = mapped_inst.GetOperand(operand_index);
  2414. if (spvIsIdType(operand.type)) {
  2415. assert(id_map_.IsDstMapped(operand.AsId()));
  2416. operand.words[0] = id_map_.MappedSrcId(operand.AsId());
  2417. }
  2418. }
  2419. return mapped_inst;
  2420. }
  2421. spv_result_t Differ::Output() {
  2422. id_map_.MapUnmatchedIds(
  2423. [this](uint32_t src_id) { return src_id_to_.IsDefined(src_id); },
  2424. [this](uint32_t dst_id) { return dst_id_to_.IsDefined(dst_id); });
  2425. src_id_to_.inst_map_.resize(id_map_.SrcToDstMap().IdBound(), nullptr);
  2426. dst_id_to_.inst_map_.resize(id_map_.DstToSrcMap().IdBound(), nullptr);
  2427. const spv_target_env target_env = SPV_ENV_UNIVERSAL_1_6;
  2428. spv_opcode_table opcode_table;
  2429. spv_operand_table operand_table;
  2430. spv_ext_inst_table ext_inst_table;
  2431. spv_result_t result;
  2432. result = spvOpcodeTableGet(&opcode_table, target_env);
  2433. if (result != SPV_SUCCESS) return result;
  2434. result = spvOperandTableGet(&operand_table, target_env);
  2435. if (result != SPV_SUCCESS) return result;
  2436. result = spvExtInstTableGet(&ext_inst_table, target_env);
  2437. if (result != SPV_SUCCESS) return result;
  2438. spv_context_t context{
  2439. target_env,
  2440. opcode_table,
  2441. operand_table,
  2442. ext_inst_table,
  2443. };
  2444. const AssemblyGrammar grammar(&context);
  2445. if (!grammar.isValid()) return SPV_ERROR_INVALID_TABLE;
  2446. uint32_t disassembly_options = SPV_BINARY_TO_TEXT_OPTION_PRINT;
  2447. if (options_.indent) {
  2448. disassembly_options |= SPV_BINARY_TO_TEXT_OPTION_INDENT;
  2449. }
  2450. NameMapper name_mapper = GetTrivialNameMapper();
  2451. disassemble::InstructionDisassembler dis(grammar, out_, disassembly_options,
  2452. name_mapper);
  2453. if (!options_.no_header) {
  2454. // Output the header
  2455. // TODO: when using diff with text, the assembler overrides the version and
  2456. // generator, so these aren't reflected correctly in the output. Could
  2457. // potentially extract this info from the header comment.
  2458. OutputLine([]() { return true; }, [&dis]() { dis.EmitHeaderSpirv(); },
  2459. []() { assert(false && "Unreachable"); });
  2460. OutputLine([this]() { return src_->version() == dst_->version(); },
  2461. [this, &dis]() { dis.EmitHeaderVersion(src_->version()); },
  2462. [this, &dis]() { dis.EmitHeaderVersion(dst_->version()); });
  2463. OutputLine([this]() { return src_->generator() == dst_->generator(); },
  2464. [this, &dis]() { dis.EmitHeaderGenerator(src_->generator()); },
  2465. [this, &dis]() { dis.EmitHeaderGenerator(dst_->generator()); });
  2466. OutputLine(
  2467. [this]() { return src_->IdBound() == id_map_.SrcToDstMap().IdBound(); },
  2468. [this, &dis]() { dis.EmitHeaderIdBound(src_->IdBound()); },
  2469. [this, &dis]() {
  2470. dis.EmitHeaderIdBound(id_map_.SrcToDstMap().IdBound());
  2471. });
  2472. OutputLine([this]() { return src_->schema() == dst_->schema(); },
  2473. [this, &dis]() { dis.EmitHeaderSchema(src_->schema()); },
  2474. [this, &dis]() { dis.EmitHeaderSchema(dst_->schema()); });
  2475. }
  2476. // For each section, iterate both modules and output the disassembly.
  2477. auto write_inst = [this, &dis](const opt::Instruction& inst,
  2478. const IdInstructions& id_to,
  2479. const opt::Instruction& original_inst) {
  2480. spv_parsed_instruction_t parsed_inst;
  2481. std::vector<spv_parsed_operand_t> parsed_operands;
  2482. std::vector<uint32_t> inst_binary;
  2483. ToParsedInstruction(inst, id_to, original_inst, &parsed_inst,
  2484. parsed_operands, inst_binary);
  2485. dis.EmitInstruction(parsed_inst, 0);
  2486. };
  2487. OutputSection(src_->capabilities(), dst_->capabilities(), write_inst);
  2488. OutputSection(src_->extensions(), dst_->extensions(), write_inst);
  2489. OutputSection(src_->ext_inst_imports(), dst_->ext_inst_imports(), write_inst);
  2490. // There is only one memory model.
  2491. OutputLine(
  2492. [this]() {
  2493. return DoInstructionsMatch(src_->GetMemoryModel(),
  2494. dst_->GetMemoryModel());
  2495. },
  2496. [this, &write_inst]() {
  2497. write_inst(*src_->GetMemoryModel(), src_id_to_,
  2498. *src_->GetMemoryModel());
  2499. },
  2500. [this, &write_inst]() {
  2501. write_inst(*dst_->GetMemoryModel(), dst_id_to_,
  2502. *dst_->GetMemoryModel());
  2503. });
  2504. OutputSection(src_->entry_points(), dst_->entry_points(), write_inst);
  2505. OutputSection(src_->execution_modes(), dst_->execution_modes(), write_inst);
  2506. OutputSection(src_->debugs1(), dst_->debugs1(), write_inst);
  2507. OutputSection(src_->debugs2(), dst_->debugs2(), write_inst);
  2508. OutputSection(src_->debugs3(), dst_->debugs3(), write_inst);
  2509. OutputSection(src_->ext_inst_debuginfo(), dst_->ext_inst_debuginfo(),
  2510. write_inst);
  2511. OutputSection(src_->annotations(), dst_->annotations(), write_inst);
  2512. OutputSection(src_->types_values(), dst_->types_values(), write_inst);
  2513. // Get the body of all the functions.
  2514. FunctionInstMap src_func_header_insts;
  2515. FunctionInstMap dst_func_header_insts;
  2516. GetFunctionHeaderInstructions(src_, &src_func_header_insts);
  2517. GetFunctionHeaderInstructions(dst_, &dst_func_header_insts);
  2518. for (const auto& src_func : src_func_insts_) {
  2519. const uint32_t src_func_id = src_func.first;
  2520. const InstructionList& src_insts = src_func.second;
  2521. const InstructionList& src_header_insts =
  2522. src_func_header_insts[src_func_id];
  2523. const uint32_t dst_func_id = id_map_.MappedDstId(src_func_id);
  2524. if (dst_func_insts_.find(dst_func_id) == dst_func_insts_.end()) {
  2525. OutputSection(src_header_insts, InstructionList(), write_inst);
  2526. OutputSection(src_insts, InstructionList(), write_inst);
  2527. continue;
  2528. }
  2529. const InstructionList& dst_insts = dst_func_insts_[dst_func_id];
  2530. const InstructionList& dst_header_insts =
  2531. dst_func_header_insts[dst_func_id];
  2532. OutputSection(src_header_insts, dst_header_insts, write_inst);
  2533. OutputSection(src_insts, dst_insts, write_inst);
  2534. }
  2535. for (const auto& dst_func : dst_func_insts_) {
  2536. const uint32_t dst_func_id = dst_func.first;
  2537. const InstructionList& dst_insts = dst_func.second;
  2538. const InstructionList& dst_header_insts =
  2539. dst_func_header_insts[dst_func_id];
  2540. const uint32_t src_func_id = id_map_.MappedSrcId(dst_func_id);
  2541. if (src_func_insts_.find(src_func_id) == src_func_insts_.end()) {
  2542. OutputSection(InstructionList(), dst_header_insts, write_inst);
  2543. OutputSection(InstructionList(), dst_insts, write_inst);
  2544. }
  2545. }
  2546. out_ << std::flush;
  2547. return SPV_SUCCESS;
  2548. }
  2549. } // anonymous namespace
  2550. spv_result_t Diff(opt::IRContext* src, opt::IRContext* dst, std::ostream& out,
  2551. Options options) {
  2552. // High level algorithm:
  2553. //
  2554. // - Some sections of SPIR-V don't deal with ids; instructions in those
  2555. // sections are matched identically. For example OpCapability instructions.
  2556. // - Some sections produce ids, and they can be trivially matched by their
  2557. // parameters. For example OpExtInstImport instructions.
  2558. // - Some sections annotate ids. These are matched at the end, after the ids
  2559. // themselves are matched. For example OpName or OpDecorate instructions.
  2560. // - Some sections produce ids that depend on other ids and they can be
  2561. // recursively matched. For example OpType* instructions.
  2562. // - Some sections produce ids that are not trivially matched. For these ids,
  2563. // the debug info is used when possible, or a best guess (such as through
  2564. // decorations) is used. For example OpVariable instructions.
  2565. // - Matching functions is done with multiple attempts:
  2566. // * Functions with identical debug names are matched if there are no
  2567. // overloads.
  2568. // * Otherwise, functions with identical debug names and types are matched.
  2569. // * The rest of the functions are best-effort matched, first in groups of
  2570. // identical type, then any with any.
  2571. // * The best-effort matching takes the diff of every pair of functions in
  2572. // a group and selects the top matches that also meet a similarity
  2573. // index.
  2574. // * Once a pair of functions are matched, the fuzzy diff of the
  2575. // instructions is used to match the instructions in the function body.
  2576. // The fuzzy diff makes sure that sufficiently similar instructions are
  2577. // matched and that yet-to-be-matched result ids don't result in a larger
  2578. // diff.
  2579. //
  2580. // Once the instructions are matched between the src and dst SPIR-V, the src
  2581. // is traversed and its disassembly is output. In the process, any unmatched
  2582. // instruction is prefixed with -, and any unmatched instruction in dst in the
  2583. // same section is output prefixed with +. To avoid confusion, the
  2584. // instructions in dst are output with matching ids in src so the output
  2585. // assembly is consistent.
  2586. Differ differ(src, dst, out, options);
  2587. // First, match instructions between the different non-annotation sections of
  2588. // the SPIR-V.
  2589. differ.MatchCapabilities();
  2590. differ.MatchExtensions();
  2591. differ.MatchExtInstImportIds();
  2592. differ.MatchMemoryModel();
  2593. differ.MatchEntryPointIds();
  2594. differ.MatchExecutionModes();
  2595. differ.MatchTypeForwardPointers();
  2596. differ.MatchTypeIds();
  2597. differ.MatchConstants();
  2598. differ.MatchVariableIds();
  2599. differ.MatchFunctions();
  2600. // Match instructions that annotate previously-matched ids.
  2601. differ.MatchDebugs1();
  2602. differ.MatchDebugs2();
  2603. differ.MatchDebugs3();
  2604. differ.MatchExtInstDebugInfo();
  2605. differ.MatchAnnotations();
  2606. // Show the disassembly with the diff.
  2607. //
  2608. // TODO: Based on an option, output either based on src or dst, i.e. the diff
  2609. // can show the ids and instruction/function order either from src or dst.
  2610. spv_result_t result = differ.Output();
  2611. differ.DumpIdMap();
  2612. return result;
  2613. }
  2614. } // namespace diff
  2615. } // namespace spvtools