diff.cpp 105 KB

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