diff.cpp 106 KB

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