binary_to_text_test.cpp 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  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 <sstream>
  15. #include <string>
  16. #include <tuple>
  17. #include <vector>
  18. #include "gmock/gmock.h"
  19. #include "source/spirv_constant.h"
  20. #include "test/test_fixture.h"
  21. #include "test/unit_spirv.h"
  22. namespace spvtools {
  23. namespace {
  24. using spvtest::AutoText;
  25. using spvtest::ScopedContext;
  26. using spvtest::TextToBinaryTest;
  27. using ::testing::Combine;
  28. using ::testing::Eq;
  29. using ::testing::HasSubstr;
  30. class BinaryToText : public ::testing::Test {
  31. public:
  32. BinaryToText()
  33. : context(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)), binary(nullptr) {}
  34. ~BinaryToText() override {
  35. spvBinaryDestroy(binary);
  36. spvContextDestroy(context);
  37. }
  38. void SetUp() override {
  39. const char* textStr = R"(
  40. OpSource OpenCL_C 12
  41. OpMemoryModel Physical64 OpenCL
  42. OpSourceExtension "PlaceholderExtensionName"
  43. OpEntryPoint Kernel %1 "foo"
  44. OpExecutionMode %1 LocalSizeHint 1 1 1
  45. %2 = OpTypeVoid
  46. %3 = OpTypeBool
  47. %4 = OpTypeInt 8 0
  48. %5 = OpTypeInt 8 1
  49. %6 = OpTypeInt 16 0
  50. %7 = OpTypeInt 16 1
  51. %8 = OpTypeInt 32 0
  52. %9 = OpTypeInt 32 1
  53. %10 = OpTypeInt 64 0
  54. %11 = OpTypeInt 64 1
  55. %12 = OpTypeFloat 16
  56. %13 = OpTypeFloat 32
  57. %14 = OpTypeFloat 64
  58. %15 = OpTypeVector %4 2
  59. )";
  60. spv_text_t text = {textStr, strlen(textStr)};
  61. spv_diagnostic diagnostic = nullptr;
  62. spv_result_t error =
  63. spvTextToBinary(context, text.str, text.length, &binary, &diagnostic);
  64. spvDiagnosticPrint(diagnostic);
  65. spvDiagnosticDestroy(diagnostic);
  66. ASSERT_EQ(SPV_SUCCESS, error);
  67. }
  68. void TearDown() override {
  69. spvBinaryDestroy(binary);
  70. binary = nullptr;
  71. }
  72. // Compiles the given assembly text, and saves it into 'binary'.
  73. void CompileSuccessfully(std::string text) {
  74. spvBinaryDestroy(binary);
  75. binary = nullptr;
  76. spv_diagnostic diagnostic = nullptr;
  77. EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(context, text.c_str(), text.size(),
  78. &binary, &diagnostic));
  79. }
  80. spv_context context;
  81. spv_binary binary;
  82. };
  83. TEST_F(BinaryToText, Default) {
  84. spv_text text = nullptr;
  85. spv_diagnostic diagnostic = nullptr;
  86. ASSERT_EQ(
  87. SPV_SUCCESS,
  88. spvBinaryToText(context, binary->code, binary->wordCount,
  89. SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
  90. printf("%s", text->str);
  91. spvTextDestroy(text);
  92. }
  93. TEST_F(BinaryToText, Print) {
  94. spv_text text = nullptr;
  95. spv_diagnostic diagnostic = nullptr;
  96. ASSERT_EQ(
  97. SPV_SUCCESS,
  98. spvBinaryToText(context, binary->code, binary->wordCount,
  99. SPV_BINARY_TO_TEXT_OPTION_PRINT, &text, &diagnostic));
  100. ASSERT_EQ(text, nullptr);
  101. spvTextDestroy(text);
  102. }
  103. TEST_F(BinaryToText, MissingModule) {
  104. spv_text text;
  105. spv_diagnostic diagnostic = nullptr;
  106. EXPECT_EQ(
  107. SPV_ERROR_INVALID_BINARY,
  108. spvBinaryToText(context, nullptr, 42, SPV_BINARY_TO_TEXT_OPTION_NONE,
  109. &text, &diagnostic));
  110. EXPECT_THAT(diagnostic->error, Eq(std::string("Missing module.")));
  111. if (diagnostic) {
  112. spvDiagnosticPrint(diagnostic);
  113. spvDiagnosticDestroy(diagnostic);
  114. }
  115. }
  116. TEST_F(BinaryToText, TruncatedModule) {
  117. // Make a valid module with zero instructions.
  118. CompileSuccessfully("");
  119. EXPECT_EQ(SPV_INDEX_INSTRUCTION, binary->wordCount);
  120. for (size_t length = 0; length < SPV_INDEX_INSTRUCTION; length++) {
  121. spv_text text = nullptr;
  122. spv_diagnostic diagnostic = nullptr;
  123. EXPECT_EQ(
  124. SPV_ERROR_INVALID_BINARY,
  125. spvBinaryToText(context, binary->code, length,
  126. SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
  127. ASSERT_NE(nullptr, diagnostic);
  128. std::stringstream expected;
  129. expected << "Module has incomplete header: only " << length
  130. << " words instead of " << SPV_INDEX_INSTRUCTION;
  131. EXPECT_THAT(diagnostic->error, Eq(expected.str()));
  132. spvDiagnosticDestroy(diagnostic);
  133. }
  134. }
  135. TEST_F(BinaryToText, InvalidMagicNumber) {
  136. CompileSuccessfully("");
  137. std::vector<uint32_t> damaged_binary(binary->code,
  138. binary->code + binary->wordCount);
  139. damaged_binary[SPV_INDEX_MAGIC_NUMBER] ^= 123;
  140. spv_diagnostic diagnostic = nullptr;
  141. spv_text text;
  142. EXPECT_EQ(
  143. SPV_ERROR_INVALID_BINARY,
  144. spvBinaryToText(context, damaged_binary.data(), damaged_binary.size(),
  145. SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
  146. ASSERT_NE(nullptr, diagnostic);
  147. std::stringstream expected;
  148. expected << "Invalid SPIR-V magic number '" << std::hex
  149. << damaged_binary[SPV_INDEX_MAGIC_NUMBER] << "'.";
  150. EXPECT_THAT(diagnostic->error, Eq(expected.str()));
  151. spvDiagnosticDestroy(diagnostic);
  152. }
  153. struct FailedDecodeCase {
  154. std::string source_text;
  155. std::vector<uint32_t> appended_instruction;
  156. std::string expected_error_message;
  157. };
  158. using BinaryToTextFail =
  159. spvtest::TextToBinaryTestBase<::testing::TestWithParam<FailedDecodeCase>>;
  160. TEST_P(BinaryToTextFail, EncodeSuccessfullyDecodeFailed) {
  161. EXPECT_THAT(EncodeSuccessfullyDecodeFailed(GetParam().source_text,
  162. GetParam().appended_instruction),
  163. Eq(GetParam().expected_error_message));
  164. }
  165. INSTANTIATE_TEST_SUITE_P(
  166. InvalidIds, BinaryToTextFail,
  167. ::testing::ValuesIn(std::vector<FailedDecodeCase>{
  168. {"", spvtest::MakeInstruction(spv::Op::OpTypeVoid, {0}),
  169. "Error: Result Id is 0"},
  170. {"", spvtest::MakeInstruction(spv::Op::OpConstant, {0, 1, 42}),
  171. "Error: Type Id is 0"},
  172. {"%1 = OpTypeVoid", spvtest::MakeInstruction(spv::Op::OpTypeVoid, {1}),
  173. "Id 1 is defined more than once"},
  174. {"%1 = OpTypeVoid\n"
  175. "%2 = OpNot %1 %foo",
  176. spvtest::MakeInstruction(spv::Op::OpNot, {1, 2, 3}),
  177. "Id 2 is defined more than once"},
  178. {"%1 = OpTypeVoid\n"
  179. "%2 = OpNot %1 %foo",
  180. spvtest::MakeInstruction(spv::Op::OpNot, {1, 1, 3}),
  181. "Id 1 is defined more than once"},
  182. // The following are the two failure cases for
  183. // Parser::setNumericTypeInfoForType.
  184. {"", spvtest::MakeInstruction(spv::Op::OpConstant, {500, 1, 42}),
  185. "Type Id 500 is not a type"},
  186. {"%1 = OpTypeInt 32 0\n"
  187. "%2 = OpTypeVector %1 4",
  188. spvtest::MakeInstruction(spv::Op::OpConstant, {2, 3, 999}),
  189. "Type Id 2 is not a scalar numeric type"},
  190. }));
  191. INSTANTIATE_TEST_SUITE_P(
  192. InvalidIdsCheckedDuringLiteralCaseParsing, BinaryToTextFail,
  193. ::testing::ValuesIn(std::vector<FailedDecodeCase>{
  194. {"", spvtest::MakeInstruction(spv::Op::OpSwitch, {1, 2, 3, 4}),
  195. "Invalid OpSwitch: selector id 1 has no type"},
  196. {"%1 = OpTypeVoid\n",
  197. spvtest::MakeInstruction(spv::Op::OpSwitch, {1, 2, 3, 4}),
  198. "Invalid OpSwitch: selector id 1 is a type, not a value"},
  199. {"%1 = OpConstantTrue !500",
  200. spvtest::MakeInstruction(spv::Op::OpSwitch, {1, 2, 3, 4}),
  201. "Type Id 500 is not a type"},
  202. {"%1 = OpTypeFloat 32\n%2 = OpConstant %1 1.5",
  203. spvtest::MakeInstruction(spv::Op::OpSwitch, {2, 3, 4, 5}),
  204. "Invalid OpSwitch: selector id 2 is not a scalar integer"},
  205. }));
  206. TEST_F(TextToBinaryTest, OneInstruction) {
  207. const std::string input = "OpSource OpenCL_C 12\n";
  208. EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
  209. }
  210. // Exercise the case where an operand itself has operands.
  211. // This could detect problems in updating the expected-set-of-operands
  212. // list.
  213. TEST_F(TextToBinaryTest, OperandWithOperands) {
  214. const std::string input = R"(OpEntryPoint Kernel %1 "foo"
  215. OpExecutionMode %1 LocalSizeHint 100 200 300
  216. %2 = OpTypeVoid
  217. %3 = OpTypeFunction %2
  218. %1 = OpFunction %1 None %3
  219. )";
  220. EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
  221. }
  222. using RoundTripInstructionsTest = spvtest::TextToBinaryTestBase<
  223. ::testing::TestWithParam<std::tuple<spv_target_env, std::string>>>;
  224. TEST_P(RoundTripInstructionsTest, Sample) {
  225. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  226. std::get<1>(GetParam()), SPV_BINARY_TO_TEXT_OPTION_NONE,
  227. SPV_TEXT_TO_BINARY_OPTION_NONE, std::get<0>(GetParam())),
  228. Eq(std::get<1>(GetParam())));
  229. }
  230. // clang-format off
  231. INSTANTIATE_TEST_SUITE_P(
  232. NumericLiterals, RoundTripInstructionsTest,
  233. // This test is independent of environment, so just test the one.
  234. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
  235. SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
  236. ::testing::ValuesIn(std::vector<std::string>{
  237. "%1 = OpTypeInt 12 0\n%2 = OpConstant %1 1867\n",
  238. "%1 = OpTypeInt 12 1\n%2 = OpConstant %1 1867\n",
  239. "%1 = OpTypeInt 12 1\n%2 = OpConstant %1 -1867\n",
  240. "%1 = OpTypeInt 32 0\n%2 = OpConstant %1 1867\n",
  241. "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 1867\n",
  242. "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 -1867\n",
  243. "%1 = OpTypeInt 64 0\n%2 = OpConstant %1 18446744073709551615\n",
  244. "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 9223372036854775807\n",
  245. "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 -9223372036854775808\n",
  246. // 16-bit floats print as hex floats.
  247. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ff4p+16\n",
  248. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.d2cp-10\n",
  249. // 32-bit floats
  250. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -3.125\n",
  251. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1.8p+128\n", // NaN
  252. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.0002p+128\n", // NaN
  253. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1p+128\n", // Inf
  254. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1p+128\n", // -Inf
  255. // 64-bit floats
  256. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -3.125\n",
  257. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.ffffffffffffap-1023\n", // small normal
  258. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.ffffffffffffap-1023\n",
  259. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.8p+1024\n", // NaN
  260. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.0002p+1024\n", // NaN
  261. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1p+1024\n", // Inf
  262. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1p+1024\n", // -Inf
  263. })));
  264. // clang-format on
  265. INSTANTIATE_TEST_SUITE_P(
  266. MemoryAccessMasks, RoundTripInstructionsTest,
  267. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
  268. SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
  269. ::testing::ValuesIn(std::vector<std::string>{
  270. "OpStore %1 %2\n", // 3 words long.
  271. "OpStore %1 %2 None\n", // 4 words long, explicit final 0.
  272. "OpStore %1 %2 Volatile\n",
  273. "OpStore %1 %2 Aligned 8\n",
  274. "OpStore %1 %2 Nontemporal\n",
  275. // Combinations show the names from LSB to MSB
  276. "OpStore %1 %2 Volatile|Aligned 16\n",
  277. "OpStore %1 %2 Volatile|Nontemporal\n",
  278. "OpStore %1 %2 Volatile|Aligned|Nontemporal 32\n",
  279. })));
  280. INSTANTIATE_TEST_SUITE_P(
  281. FPFastMathModeMasks, RoundTripInstructionsTest,
  282. Combine(
  283. ::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
  284. SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
  285. ::testing::ValuesIn(std::vector<std::string>{
  286. "OpDecorate %1 FPFastMathMode None\n",
  287. "OpDecorate %1 FPFastMathMode NotNaN\n",
  288. "OpDecorate %1 FPFastMathMode NotInf\n",
  289. "OpDecorate %1 FPFastMathMode NSZ\n",
  290. "OpDecorate %1 FPFastMathMode AllowRecip\n",
  291. "OpDecorate %1 FPFastMathMode Fast\n",
  292. // Combinations show the names from LSB to MSB
  293. "OpDecorate %1 FPFastMathMode NotNaN|NotInf\n",
  294. "OpDecorate %1 FPFastMathMode NSZ|AllowRecip\n",
  295. "OpDecorate %1 FPFastMathMode NotNaN|NotInf|NSZ|AllowRecip|Fast\n",
  296. })));
  297. INSTANTIATE_TEST_SUITE_P(
  298. LoopControlMasks, RoundTripInstructionsTest,
  299. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
  300. SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_2),
  301. ::testing::ValuesIn(std::vector<std::string>{
  302. "OpLoopMerge %1 %2 None\n",
  303. "OpLoopMerge %1 %2 Unroll\n",
  304. "OpLoopMerge %1 %2 DontUnroll\n",
  305. "OpLoopMerge %1 %2 Unroll|DontUnroll\n",
  306. })));
  307. INSTANTIATE_TEST_SUITE_P(LoopControlMasksV11, RoundTripInstructionsTest,
  308. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_1,
  309. SPV_ENV_UNIVERSAL_1_2,
  310. SPV_ENV_UNIVERSAL_1_3),
  311. ::testing::ValuesIn(std::vector<std::string>{
  312. "OpLoopMerge %1 %2 DependencyInfinite\n",
  313. "OpLoopMerge %1 %2 DependencyLength 8\n",
  314. })));
  315. INSTANTIATE_TEST_SUITE_P(
  316. SelectionControlMasks, RoundTripInstructionsTest,
  317. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
  318. SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_2),
  319. ::testing::ValuesIn(std::vector<std::string>{
  320. "OpSelectionMerge %1 None\n",
  321. "OpSelectionMerge %1 Flatten\n",
  322. "OpSelectionMerge %1 DontFlatten\n",
  323. "OpSelectionMerge %1 Flatten|DontFlatten\n",
  324. })));
  325. INSTANTIATE_TEST_SUITE_P(
  326. FunctionControlMasks, RoundTripInstructionsTest,
  327. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
  328. SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
  329. ::testing::ValuesIn(std::vector<std::string>{
  330. "%2 = OpFunction %1 None %3\n",
  331. "%2 = OpFunction %1 Inline %3\n",
  332. "%2 = OpFunction %1 DontInline %3\n",
  333. "%2 = OpFunction %1 Pure %3\n",
  334. "%2 = OpFunction %1 Const %3\n",
  335. "%2 = OpFunction %1 Inline|Pure|Const %3\n",
  336. "%2 = OpFunction %1 DontInline|Const %3\n",
  337. })));
  338. INSTANTIATE_TEST_SUITE_P(
  339. ImageMasks, RoundTripInstructionsTest,
  340. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
  341. SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
  342. ::testing::ValuesIn(std::vector<std::string>{
  343. "%2 = OpImageFetch %1 %3 %4\n",
  344. "%2 = OpImageFetch %1 %3 %4 None\n",
  345. "%2 = OpImageFetch %1 %3 %4 Bias %5\n",
  346. "%2 = OpImageFetch %1 %3 %4 Lod %5\n",
  347. "%2 = OpImageFetch %1 %3 %4 Grad %5 %6\n",
  348. "%2 = OpImageFetch %1 %3 %4 ConstOffset %5\n",
  349. "%2 = OpImageFetch %1 %3 %4 Offset %5\n",
  350. "%2 = OpImageFetch %1 %3 %4 ConstOffsets %5\n",
  351. "%2 = OpImageFetch %1 %3 %4 Sample %5\n",
  352. "%2 = OpImageFetch %1 %3 %4 MinLod %5\n",
  353. "%2 = OpImageFetch %1 %3 %4 Bias|Lod|Grad %5 %6 %7 %8\n",
  354. "%2 = OpImageFetch %1 %3 %4 ConstOffset|Offset|ConstOffsets"
  355. " %5 %6 %7\n",
  356. "%2 = OpImageFetch %1 %3 %4 Sample|MinLod %5 %6\n",
  357. "%2 = OpImageFetch %1 %3 %4"
  358. " Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample|MinLod"
  359. " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"})));
  360. INSTANTIATE_TEST_SUITE_P(
  361. NewInstructionsInSPIRV1_2, RoundTripInstructionsTest,
  362. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
  363. ::testing::ValuesIn(std::vector<std::string>{
  364. "OpExecutionModeId %1 SubgroupsPerWorkgroupId %2\n",
  365. "OpExecutionModeId %1 LocalSizeId %2 %3 %4\n",
  366. "OpExecutionModeId %1 LocalSizeHintId %2 %3 %4\n",
  367. "OpDecorateId %1 AlignmentId %2\n",
  368. "OpDecorateId %1 MaxByteOffsetId %2\n",
  369. })));
  370. INSTANTIATE_TEST_SUITE_P(
  371. CacheControlsINTEL, RoundTripInstructionsTest,
  372. Combine(
  373. ::testing::Values(SPV_ENV_UNIVERSAL_1_0),
  374. ::testing::ValuesIn(std::vector<std::string>{
  375. "OpDecorate %1 CacheControlLoadINTEL 0 UncachedINTEL\n",
  376. "OpDecorate %1 CacheControlLoadINTEL 1 CachedINTEL\n",
  377. "OpDecorate %1 CacheControlLoadINTEL 2 StreamingINTEL\n",
  378. "OpDecorate %1 CacheControlLoadINTEL 3 InvalidateAfterReadINTEL\n",
  379. "OpDecorate %1 CacheControlLoadINTEL 4 ConstCachedINTEL\n",
  380. "OpDecorate %1 CacheControlStoreINTEL 0 UncachedINTEL\n",
  381. "OpDecorate %1 CacheControlStoreINTEL 1 WriteThroughINTEL\n",
  382. "OpDecorate %1 CacheControlStoreINTEL 2 WriteBackINTEL\n",
  383. "OpDecorate %1 CacheControlStoreINTEL 3 StreamingINTEL\n",
  384. })));
  385. INSTANTIATE_TEST_SUITE_P(
  386. HostAccessINTEL, RoundTripInstructionsTest,
  387. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0),
  388. ::testing::ValuesIn(std::vector<std::string>{
  389. "OpDecorate %1 HostAccessINTEL NoneINTEL \"none\"\n",
  390. "OpDecorate %1 HostAccessINTEL ReadINTEL \"read\"\n",
  391. "OpDecorate %1 HostAccessINTEL WriteINTEL \"write\"\n",
  392. "OpDecorate %1 HostAccessINTEL ReadWriteINTEL \"readwrite\"\n",
  393. })));
  394. // clang-format off
  395. INSTANTIATE_TEST_SUITE_P(
  396. MatrixMultiplyAccumulateOperands, RoundTripInstructionsTest,
  397. Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0),
  398. ::testing::ValuesIn(std::vector<std::string>{
  399. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6\n",
  400. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 None\n",
  401. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixASignedComponentsINTEL\n",
  402. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBSignedComponentsINTEL\n",
  403. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixCBFloat16INTEL\n",
  404. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixResultBFloat16INTEL\n",
  405. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixAPackedInt8INTEL\n",
  406. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBPackedInt8INTEL\n",
  407. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixAPackedInt4INTEL\n",
  408. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBPackedInt4INTEL\n",
  409. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixATF32INTEL\n",
  410. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBTF32INTEL\n",
  411. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixCBFloat16INTEL\n",
  412. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixAPackedFloat16INTEL\n",
  413. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBPackedFloat16INTEL\n",
  414. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixAPackedBFloat16INTEL\n",
  415. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBPackedBFloat16INTEL\n",
  416. "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 "
  417. "MatrixASignedComponentsINTEL|MatrixBSignedComponentsINTEL|MatrixAPackedInt8INTEL|MatrixBPackedInt8INTEL\n",
  418. })));
  419. // clang-format on
  420. using MaskSorting = TextToBinaryTest;
  421. TEST_F(MaskSorting, MasksAreSortedFromLSBToMSB) {
  422. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  423. "OpStore %1 %2 Nontemporal|Aligned|Volatile 32"),
  424. Eq("OpStore %1 %2 Volatile|Aligned|Nontemporal 32\n"));
  425. EXPECT_THAT(
  426. EncodeAndDecodeSuccessfully(
  427. "OpDecorate %1 FPFastMathMode NotInf|Fast|AllowRecip|NotNaN|NSZ"),
  428. Eq("OpDecorate %1 FPFastMathMode NotNaN|NotInf|NSZ|AllowRecip|Fast\n"));
  429. EXPECT_THAT(
  430. EncodeAndDecodeSuccessfully("OpLoopMerge %1 %2 DontUnroll|Unroll"),
  431. Eq("OpLoopMerge %1 %2 Unroll|DontUnroll\n"));
  432. EXPECT_THAT(
  433. EncodeAndDecodeSuccessfully("OpSelectionMerge %1 DontFlatten|Flatten"),
  434. Eq("OpSelectionMerge %1 Flatten|DontFlatten\n"));
  435. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  436. "%2 = OpFunction %1 DontInline|Const|Pure|Inline %3"),
  437. Eq("%2 = OpFunction %1 Inline|DontInline|Pure|Const %3\n"));
  438. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  439. "%2 = OpImageFetch %1 %3 %4"
  440. " MinLod|Sample|Offset|Lod|Grad|ConstOffsets|ConstOffset|Bias"
  441. " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"),
  442. Eq("%2 = OpImageFetch %1 %3 %4"
  443. " Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample|MinLod"
  444. " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"));
  445. }
  446. using OperandTypeTest = TextToBinaryTest;
  447. TEST_F(OperandTypeTest, OptionalTypedLiteralNumber) {
  448. const std::string input =
  449. "%1 = OpTypeInt 32 0\n"
  450. "%2 = OpConstant %1 42\n"
  451. "OpSwitch %2 %3 100 %4\n";
  452. EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
  453. }
  454. using IndentTest = spvtest::TextToBinaryTest;
  455. TEST_F(IndentTest, Sample) {
  456. const std::string input = R"(
  457. OpCapability Shader
  458. OpMemoryModel Logical GLSL450
  459. %1 = OpTypeInt 32 0
  460. %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10 ; force IDs into double digits
  461. %11 = OpConstant %1 42
  462. OpStore %2 %3 Aligned|Volatile 4 ; bogus, but not indented
  463. )";
  464. const std::string expected =
  465. R"( OpCapability Shader
  466. OpMemoryModel Logical GLSL450
  467. %1 = OpTypeInt 32 0
  468. %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10
  469. %11 = OpConstant %1 42
  470. OpStore %2 %3 Volatile|Aligned 4
  471. )";
  472. EXPECT_THAT(
  473. EncodeAndDecodeSuccessfully(input, SPV_BINARY_TO_TEXT_OPTION_INDENT),
  474. expected);
  475. }
  476. TEST_F(IndentTest, NestedIf) {
  477. const std::string input = R"(
  478. OpCapability Shader
  479. OpMemoryModel Logical Simple
  480. OpEntryPoint Fragment %100 "main"
  481. OpExecutionMode %100 OriginUpperLeft
  482. OpName %var "var"
  483. %void = OpTypeVoid
  484. %3 = OpTypeFunction %void
  485. %bool = OpTypeBool
  486. %5 = OpConstantNull %bool
  487. %true = OpConstantTrue %bool
  488. %false = OpConstantFalse %bool
  489. %uint = OpTypeInt 32 0
  490. %int = OpTypeInt 32 1
  491. %uint_42 = OpConstant %uint 42
  492. %int_42 = OpConstant %int 42
  493. %13 = OpTypeFunction %uint
  494. %uint_0 = OpConstant %uint 0
  495. %uint_1 = OpConstant %uint 1
  496. %uint_2 = OpConstant %uint 2
  497. %uint_3 = OpConstant %uint 3
  498. %uint_4 = OpConstant %uint 4
  499. %uint_5 = OpConstant %uint 5
  500. %uint_6 = OpConstant %uint 6
  501. %uint_7 = OpConstant %uint 7
  502. %uint_8 = OpConstant %uint 8
  503. %uint_10 = OpConstant %uint 10
  504. %uint_20 = OpConstant %uint 20
  505. %uint_30 = OpConstant %uint 30
  506. %uint_40 = OpConstant %uint 40
  507. %uint_50 = OpConstant %uint 50
  508. %uint_90 = OpConstant %uint 90
  509. %uint_99 = OpConstant %uint 99
  510. %_ptr_Private_uint = OpTypePointer Private %uint
  511. %var = OpVariable %_ptr_Private_uint Private
  512. %uint_999 = OpConstant %uint 999
  513. %100 = OpFunction %void None %3
  514. %10 = OpLabel
  515. OpStore %var %uint_0
  516. OpSelectionMerge %99 None
  517. OpBranchConditional %5 %30 %40
  518. %30 = OpLabel
  519. OpStore %var %uint_1
  520. OpBranch %99
  521. %40 = OpLabel
  522. OpStore %var %uint_2
  523. OpBranch %99
  524. %99 = OpLabel
  525. OpStore %var %uint_999
  526. OpReturn
  527. OpFunctionEnd
  528. )";
  529. const std::string expected =
  530. R"( OpCapability Shader
  531. OpMemoryModel Logical Simple
  532. OpEntryPoint Fragment %100 "main"
  533. OpExecutionMode %100 OriginUpperLeft
  534. OpName %1 "var"
  535. %2 = OpTypeVoid
  536. %3 = OpTypeFunction %2
  537. %4 = OpTypeBool
  538. %5 = OpConstantNull %4
  539. %6 = OpConstantTrue %4
  540. %7 = OpConstantFalse %4
  541. %8 = OpTypeInt 32 0
  542. %9 = OpTypeInt 32 1
  543. %11 = OpConstant %8 42
  544. %12 = OpConstant %9 42
  545. %13 = OpTypeFunction %8
  546. %14 = OpConstant %8 0
  547. %15 = OpConstant %8 1
  548. %16 = OpConstant %8 2
  549. %17 = OpConstant %8 3
  550. %18 = OpConstant %8 4
  551. %19 = OpConstant %8 5
  552. %20 = OpConstant %8 6
  553. %21 = OpConstant %8 7
  554. %22 = OpConstant %8 8
  555. %23 = OpConstant %8 10
  556. %24 = OpConstant %8 20
  557. %25 = OpConstant %8 30
  558. %26 = OpConstant %8 40
  559. %27 = OpConstant %8 50
  560. %28 = OpConstant %8 90
  561. %29 = OpConstant %8 99
  562. %31 = OpTypePointer Private %8
  563. %1 = OpVariable %31 Private
  564. %32 = OpConstant %8 999
  565. %100 = OpFunction %2 None %3
  566. %10 = OpLabel
  567. OpStore %1 %14
  568. OpSelectionMerge %99 None
  569. OpBranchConditional %5 %30 %40
  570. %30 = OpLabel
  571. OpStore %1 %15
  572. OpBranch %99
  573. %40 = OpLabel
  574. OpStore %1 %16
  575. OpBranch %99
  576. %99 = OpLabel
  577. OpStore %1 %32
  578. OpReturn
  579. OpFunctionEnd
  580. )";
  581. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  582. input,
  583. SPV_BINARY_TO_TEXT_OPTION_INDENT |
  584. SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
  585. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
  586. expected);
  587. }
  588. TEST_F(IndentTest, NestedWhile) {
  589. const std::string input = R"(
  590. OpCapability Shader
  591. OpMemoryModel Logical Simple
  592. OpEntryPoint Fragment %100 "main"
  593. OpExecutionMode %100 OriginUpperLeft
  594. OpName %var "var"
  595. %void = OpTypeVoid
  596. %3 = OpTypeFunction %void
  597. %bool = OpTypeBool
  598. %5 = OpConstantNull %bool
  599. %true = OpConstantTrue %bool
  600. %false = OpConstantFalse %bool
  601. %uint = OpTypeInt 32 0
  602. %int = OpTypeInt 32 1
  603. %uint_42 = OpConstant %uint 42
  604. %int_42 = OpConstant %int 42
  605. %13 = OpTypeFunction %uint
  606. %uint_0 = OpConstant %uint 0
  607. %uint_1 = OpConstant %uint 1
  608. %uint_2 = OpConstant %uint 2
  609. %uint_3 = OpConstant %uint 3
  610. %uint_4 = OpConstant %uint 4
  611. %uint_5 = OpConstant %uint 5
  612. %uint_6 = OpConstant %uint 6
  613. %uint_7 = OpConstant %uint 7
  614. %uint_8 = OpConstant %uint 8
  615. %uint_10 = OpConstant %uint 10
  616. %uint_20 = OpConstant %uint 20
  617. %uint_30 = OpConstant %uint 30
  618. %uint_40 = OpConstant %uint 40
  619. %uint_50 = OpConstant %uint 50
  620. %uint_90 = OpConstant %uint 90
  621. %uint_99 = OpConstant %uint 99
  622. %_ptr_Private_uint = OpTypePointer Private %uint
  623. %var = OpVariable %_ptr_Private_uint Private
  624. %uint_999 = OpConstant %uint 999
  625. %100 = OpFunction %void None %3
  626. %10 = OpLabel
  627. OpStore %var %uint_0
  628. OpBranch %20
  629. %20 = OpLabel
  630. OpStore %var %uint_1
  631. OpLoopMerge %99 %20 None
  632. OpBranch %80
  633. %80 = OpLabel
  634. OpStore %var %uint_2
  635. OpBranchConditional %5 %99 %20
  636. %99 = OpLabel
  637. OpStore %var %uint_3
  638. OpReturn
  639. OpFunctionEnd
  640. )";
  641. const std::string expected =
  642. R"( OpCapability Shader
  643. OpMemoryModel Logical Simple
  644. OpEntryPoint Fragment %100 "main"
  645. OpExecutionMode %100 OriginUpperLeft
  646. OpName %1 "var"
  647. %2 = OpTypeVoid
  648. %3 = OpTypeFunction %2
  649. %4 = OpTypeBool
  650. %5 = OpConstantNull %4
  651. %6 = OpConstantTrue %4
  652. %7 = OpConstantFalse %4
  653. %8 = OpTypeInt 32 0
  654. %9 = OpTypeInt 32 1
  655. %11 = OpConstant %8 42
  656. %12 = OpConstant %9 42
  657. %13 = OpTypeFunction %8
  658. %14 = OpConstant %8 0
  659. %15 = OpConstant %8 1
  660. %16 = OpConstant %8 2
  661. %17 = OpConstant %8 3
  662. %18 = OpConstant %8 4
  663. %19 = OpConstant %8 5
  664. %21 = OpConstant %8 6
  665. %22 = OpConstant %8 7
  666. %23 = OpConstant %8 8
  667. %24 = OpConstant %8 10
  668. %25 = OpConstant %8 20
  669. %26 = OpConstant %8 30
  670. %27 = OpConstant %8 40
  671. %28 = OpConstant %8 50
  672. %29 = OpConstant %8 90
  673. %30 = OpConstant %8 99
  674. %31 = OpTypePointer Private %8
  675. %1 = OpVariable %31 Private
  676. %32 = OpConstant %8 999
  677. %100 = OpFunction %2 None %3
  678. %10 = OpLabel
  679. OpStore %1 %14
  680. OpBranch %20
  681. %20 = OpLabel
  682. OpStore %1 %15
  683. OpLoopMerge %99 %20 None
  684. OpBranch %80
  685. %80 = OpLabel
  686. OpStore %1 %16
  687. OpBranchConditional %5 %99 %20
  688. %99 = OpLabel
  689. OpStore %1 %17
  690. OpReturn
  691. OpFunctionEnd
  692. )";
  693. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  694. input,
  695. SPV_BINARY_TO_TEXT_OPTION_INDENT |
  696. SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
  697. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
  698. expected);
  699. }
  700. TEST_F(IndentTest, NestedLoopInLoop) {
  701. const std::string input = R"(
  702. OpCapability Shader
  703. OpMemoryModel Logical Simple
  704. OpEntryPoint Fragment %100 "main"
  705. OpExecutionMode %100 OriginUpperLeft
  706. OpName %var "var"
  707. %void = OpTypeVoid
  708. %3 = OpTypeFunction %void
  709. %bool = OpTypeBool
  710. %5 = OpConstantNull %bool
  711. %true = OpConstantTrue %bool
  712. %false = OpConstantFalse %bool
  713. %uint = OpTypeInt 32 0
  714. %int = OpTypeInt 32 1
  715. %uint_42 = OpConstant %uint 42
  716. %int_42 = OpConstant %int 42
  717. %13 = OpTypeFunction %uint
  718. %uint_0 = OpConstant %uint 0
  719. %uint_1 = OpConstant %uint 1
  720. %uint_2 = OpConstant %uint 2
  721. %uint_3 = OpConstant %uint 3
  722. %uint_4 = OpConstant %uint 4
  723. %uint_5 = OpConstant %uint 5
  724. %uint_6 = OpConstant %uint 6
  725. %uint_7 = OpConstant %uint 7
  726. %uint_8 = OpConstant %uint 8
  727. %uint_10 = OpConstant %uint 10
  728. %uint_20 = OpConstant %uint 20
  729. %uint_30 = OpConstant %uint 30
  730. %uint_40 = OpConstant %uint 40
  731. %uint_50 = OpConstant %uint 50
  732. %uint_90 = OpConstant %uint 90
  733. %uint_99 = OpConstant %uint 99
  734. %_ptr_Private_uint = OpTypePointer Private %uint
  735. %var = OpVariable %_ptr_Private_uint Private
  736. %uint_999 = OpConstant %uint 999
  737. %100 = OpFunction %void None %3
  738. %10 = OpLabel
  739. OpBranch %20
  740. %20 = OpLabel
  741. OpLoopMerge %99 %50 None
  742. OpBranchConditional %5 %30 %99
  743. %30 = OpLabel
  744. OpLoopMerge %49 %40 None
  745. OpBranchConditional %true %35 %49
  746. %35 = OpLabel
  747. OpBranch %37
  748. %37 = OpLabel
  749. OpBranch %40
  750. %40 = OpLabel
  751. OpBranch %30
  752. %49 = OpLabel
  753. OpBranch %50
  754. %50 = OpLabel
  755. OpBranch %20
  756. %99 = OpLabel
  757. OpReturn
  758. OpFunctionEnd
  759. )";
  760. const std::string expected =
  761. R"( OpCapability Shader
  762. OpMemoryModel Logical Simple
  763. OpEntryPoint Fragment %100 "main"
  764. OpExecutionMode %100 OriginUpperLeft
  765. OpName %1 "var"
  766. %2 = OpTypeVoid
  767. %3 = OpTypeFunction %2
  768. %4 = OpTypeBool
  769. %5 = OpConstantNull %4
  770. %6 = OpConstantTrue %4
  771. %7 = OpConstantFalse %4
  772. %8 = OpTypeInt 32 0
  773. %9 = OpTypeInt 32 1
  774. %11 = OpConstant %8 42
  775. %12 = OpConstant %9 42
  776. %13 = OpTypeFunction %8
  777. %14 = OpConstant %8 0
  778. %15 = OpConstant %8 1
  779. %16 = OpConstant %8 2
  780. %17 = OpConstant %8 3
  781. %18 = OpConstant %8 4
  782. %19 = OpConstant %8 5
  783. %21 = OpConstant %8 6
  784. %22 = OpConstant %8 7
  785. %23 = OpConstant %8 8
  786. %24 = OpConstant %8 10
  787. %25 = OpConstant %8 20
  788. %26 = OpConstant %8 30
  789. %27 = OpConstant %8 40
  790. %28 = OpConstant %8 50
  791. %29 = OpConstant %8 90
  792. %31 = OpConstant %8 99
  793. %32 = OpTypePointer Private %8
  794. %1 = OpVariable %32 Private
  795. %33 = OpConstant %8 999
  796. %100 = OpFunction %2 None %3
  797. %10 = OpLabel
  798. OpBranch %20
  799. %20 = OpLabel
  800. OpLoopMerge %99 %50 None
  801. OpBranchConditional %5 %30 %99
  802. %30 = OpLabel
  803. OpLoopMerge %49 %40 None
  804. OpBranchConditional %6 %35 %49
  805. %35 = OpLabel
  806. OpBranch %37
  807. %37 = OpLabel
  808. OpBranch %40
  809. %40 = OpLabel
  810. OpBranch %30
  811. %49 = OpLabel
  812. OpBranch %50
  813. %50 = OpLabel
  814. OpBranch %20
  815. %99 = OpLabel
  816. OpReturn
  817. OpFunctionEnd
  818. )";
  819. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  820. input,
  821. SPV_BINARY_TO_TEXT_OPTION_INDENT |
  822. SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
  823. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
  824. expected);
  825. }
  826. TEST_F(IndentTest, NestedSwitch) {
  827. const std::string input = R"(
  828. OpCapability Shader
  829. OpMemoryModel Logical Simple
  830. OpEntryPoint Fragment %100 "main"
  831. OpExecutionMode %100 OriginUpperLeft
  832. OpName %var "var"
  833. %void = OpTypeVoid
  834. %3 = OpTypeFunction %void
  835. %bool = OpTypeBool
  836. %5 = OpConstantNull %bool
  837. %true = OpConstantTrue %bool
  838. %false = OpConstantFalse %bool
  839. %uint = OpTypeInt 32 0
  840. %int = OpTypeInt 32 1
  841. %uint_42 = OpConstant %uint 42
  842. %int_42 = OpConstant %int 42
  843. %13 = OpTypeFunction %uint
  844. %uint_0 = OpConstant %uint 0
  845. %uint_1 = OpConstant %uint 1
  846. %uint_2 = OpConstant %uint 2
  847. %uint_3 = OpConstant %uint 3
  848. %uint_4 = OpConstant %uint 4
  849. %uint_5 = OpConstant %uint 5
  850. %uint_6 = OpConstant %uint 6
  851. %uint_7 = OpConstant %uint 7
  852. %uint_8 = OpConstant %uint 8
  853. %uint_10 = OpConstant %uint 10
  854. %uint_20 = OpConstant %uint 20
  855. %uint_30 = OpConstant %uint 30
  856. %uint_40 = OpConstant %uint 40
  857. %uint_50 = OpConstant %uint 50
  858. %uint_90 = OpConstant %uint 90
  859. %uint_99 = OpConstant %uint 99
  860. %_ptr_Private_uint = OpTypePointer Private %uint
  861. %var = OpVariable %_ptr_Private_uint Private
  862. %uint_999 = OpConstant %uint 999
  863. %100 = OpFunction %void None %3
  864. %10 = OpLabel
  865. OpSelectionMerge %99 None
  866. OpSwitch %uint_42 %80 20 %20 30 %30
  867. %20 = OpLabel
  868. OpBranch %80
  869. %80 = OpLabel
  870. OpBranch %30
  871. %30 = OpLabel
  872. OpBranch %99
  873. %99 = OpLabel
  874. OpReturn
  875. OpFunctionEnd
  876. )";
  877. const std::string expected =
  878. R"( OpCapability Shader
  879. OpMemoryModel Logical Simple
  880. OpEntryPoint Fragment %100 "main"
  881. OpExecutionMode %100 OriginUpperLeft
  882. OpName %1 "var"
  883. %2 = OpTypeVoid
  884. %3 = OpTypeFunction %2
  885. %4 = OpTypeBool
  886. %5 = OpConstantNull %4
  887. %6 = OpConstantTrue %4
  888. %7 = OpConstantFalse %4
  889. %8 = OpTypeInt 32 0
  890. %9 = OpTypeInt 32 1
  891. %11 = OpConstant %8 42
  892. %12 = OpConstant %9 42
  893. %13 = OpTypeFunction %8
  894. %14 = OpConstant %8 0
  895. %15 = OpConstant %8 1
  896. %16 = OpConstant %8 2
  897. %17 = OpConstant %8 3
  898. %18 = OpConstant %8 4
  899. %19 = OpConstant %8 5
  900. %21 = OpConstant %8 6
  901. %22 = OpConstant %8 7
  902. %23 = OpConstant %8 8
  903. %24 = OpConstant %8 10
  904. %25 = OpConstant %8 20
  905. %26 = OpConstant %8 30
  906. %27 = OpConstant %8 40
  907. %28 = OpConstant %8 50
  908. %29 = OpConstant %8 90
  909. %31 = OpConstant %8 99
  910. %32 = OpTypePointer Private %8
  911. %1 = OpVariable %32 Private
  912. %33 = OpConstant %8 999
  913. %100 = OpFunction %2 None %3
  914. %10 = OpLabel
  915. OpSelectionMerge %99 None
  916. OpSwitch %11 %80 20 %20 30 %30
  917. %20 = OpLabel
  918. OpBranch %80
  919. %80 = OpLabel
  920. OpBranch %30
  921. %30 = OpLabel
  922. OpBranch %99
  923. %99 = OpLabel
  924. OpReturn
  925. OpFunctionEnd
  926. )";
  927. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  928. input,
  929. SPV_BINARY_TO_TEXT_OPTION_INDENT |
  930. SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
  931. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
  932. expected);
  933. }
  934. TEST_F(IndentTest, ReorderedIf) {
  935. const std::string input = R"(
  936. OpCapability Shader
  937. OpMemoryModel Logical Simple
  938. OpEntryPoint Fragment %100 "main"
  939. OpExecutionMode %100 OriginUpperLeft
  940. OpName %1 "var"
  941. %2 = OpTypeVoid
  942. %3 = OpTypeFunction %2
  943. %4 = OpTypeBool
  944. %5 = OpConstantNull %4
  945. %6 = OpConstantTrue %4
  946. %7 = OpConstantFalse %4
  947. %8 = OpTypeInt 32 0
  948. %9 = OpTypeInt 32 1
  949. %11 = OpConstant %8 42
  950. %12 = OpConstant %9 42
  951. %13 = OpTypeFunction %8
  952. %14 = OpConstant %8 0
  953. %15 = OpConstant %8 1
  954. %16 = OpConstant %8 2
  955. %17 = OpConstant %8 3
  956. %18 = OpConstant %8 4
  957. %19 = OpConstant %8 5
  958. %21 = OpConstant %8 6
  959. %22 = OpConstant %8 7
  960. %23 = OpConstant %8 8
  961. %24 = OpConstant %8 10
  962. %25 = OpConstant %8 20
  963. %26 = OpConstant %8 30
  964. %27 = OpConstant %8 40
  965. %28 = OpConstant %8 50
  966. %29 = OpConstant %8 90
  967. %31 = OpConstant %8 99
  968. %32 = OpTypePointer Private %8
  969. %1 = OpVariable %32 Private
  970. %33 = OpConstant %8 999
  971. %100 = OpFunction %2 None %3
  972. %10 = OpLabel
  973. OpSelectionMerge %99 None
  974. OpBranchConditional %5 %20 %50
  975. %99 = OpLabel
  976. OpReturn
  977. %20 = OpLabel
  978. OpSelectionMerge %49 None
  979. OpBranchConditional %5 %30 %40
  980. %49 = OpLabel
  981. OpBranch %99
  982. %40 = OpLabel
  983. OpBranch %49
  984. %30 = OpLabel
  985. OpBranch %49
  986. %50 = OpLabel
  987. OpSelectionMerge %79 None
  988. OpBranchConditional %5 %60 %70
  989. %79 = OpLabel
  990. OpBranch %99
  991. %60 = OpLabel
  992. OpBranch %79
  993. %70 = OpLabel
  994. OpBranch %79
  995. OpFunctionEnd
  996. )";
  997. const std::string expected =
  998. R"( OpCapability Shader
  999. OpMemoryModel Logical Simple
  1000. OpEntryPoint Fragment %100 "main"
  1001. OpExecutionMode %100 OriginUpperLeft
  1002. OpName %1 "var"
  1003. %2 = OpTypeVoid
  1004. %3 = OpTypeFunction %2
  1005. %4 = OpTypeBool
  1006. %5 = OpConstantNull %4
  1007. %6 = OpConstantTrue %4
  1008. %7 = OpConstantFalse %4
  1009. %8 = OpTypeInt 32 0
  1010. %9 = OpTypeInt 32 1
  1011. %11 = OpConstant %8 42
  1012. %12 = OpConstant %9 42
  1013. %13 = OpTypeFunction %8
  1014. %14 = OpConstant %8 0
  1015. %15 = OpConstant %8 1
  1016. %16 = OpConstant %8 2
  1017. %17 = OpConstant %8 3
  1018. %18 = OpConstant %8 4
  1019. %19 = OpConstant %8 5
  1020. %21 = OpConstant %8 6
  1021. %22 = OpConstant %8 7
  1022. %23 = OpConstant %8 8
  1023. %24 = OpConstant %8 10
  1024. %25 = OpConstant %8 20
  1025. %26 = OpConstant %8 30
  1026. %27 = OpConstant %8 40
  1027. %28 = OpConstant %8 50
  1028. %29 = OpConstant %8 90
  1029. %31 = OpConstant %8 99
  1030. %32 = OpTypePointer Private %8
  1031. %1 = OpVariable %32 Private
  1032. %33 = OpConstant %8 999
  1033. %100 = OpFunction %2 None %3
  1034. %10 = OpLabel
  1035. OpSelectionMerge %99 None
  1036. OpBranchConditional %5 %20 %50
  1037. %20 = OpLabel
  1038. OpSelectionMerge %49 None
  1039. OpBranchConditional %5 %30 %40
  1040. %30 = OpLabel
  1041. OpBranch %49
  1042. %40 = OpLabel
  1043. OpBranch %49
  1044. %49 = OpLabel
  1045. OpBranch %99
  1046. %50 = OpLabel
  1047. OpSelectionMerge %79 None
  1048. OpBranchConditional %5 %60 %70
  1049. %60 = OpLabel
  1050. OpBranch %79
  1051. %70 = OpLabel
  1052. OpBranch %79
  1053. %79 = OpLabel
  1054. OpBranch %99
  1055. %99 = OpLabel
  1056. OpReturn
  1057. OpFunctionEnd
  1058. )";
  1059. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  1060. input,
  1061. SPV_BINARY_TO_TEXT_OPTION_INDENT |
  1062. SPV_BINARY_TO_TEXT_OPTION_REORDER_BLOCKS,
  1063. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
  1064. expected);
  1065. }
  1066. TEST_F(IndentTest, ReorderedFallThroughInSwitch) {
  1067. const std::string input = R"(
  1068. OpCapability Shader
  1069. OpMemoryModel Logical Simple
  1070. OpEntryPoint Fragment %100 "main"
  1071. OpExecutionMode %100 OriginUpperLeft
  1072. OpName %1 "var"
  1073. %2 = OpTypeVoid
  1074. %3 = OpTypeFunction %2
  1075. %4 = OpTypeBool
  1076. %5 = OpConstantNull %4
  1077. %6 = OpConstantTrue %4
  1078. %7 = OpConstantFalse %4
  1079. %8 = OpTypeInt 32 0
  1080. %9 = OpTypeInt 32 1
  1081. %11 = OpConstant %8 42
  1082. %12 = OpConstant %9 42
  1083. %13 = OpTypeFunction %8
  1084. %14 = OpConstant %8 0
  1085. %15 = OpConstant %8 1
  1086. %16 = OpConstant %8 2
  1087. %17 = OpConstant %8 3
  1088. %18 = OpConstant %8 4
  1089. %19 = OpConstant %8 5
  1090. %21 = OpConstant %8 6
  1091. %22 = OpConstant %8 7
  1092. %23 = OpConstant %8 8
  1093. %24 = OpConstant %8 10
  1094. %25 = OpConstant %8 20
  1095. %26 = OpConstant %8 30
  1096. %27 = OpConstant %8 40
  1097. %28 = OpConstant %8 50
  1098. %29 = OpConstant %8 90
  1099. %31 = OpConstant %8 99
  1100. %32 = OpTypePointer Private %8
  1101. %1 = OpVariable %32 Private
  1102. %33 = OpConstant %8 999
  1103. %100 = OpFunction %2 None %3
  1104. %10 = OpLabel
  1105. OpSelectionMerge %99 None
  1106. OpSwitch %11 %50 20 %20 50 %50
  1107. %99 = OpLabel
  1108. OpReturn
  1109. %20 = OpLabel
  1110. OpSelectionMerge %49 None
  1111. OpBranchConditional %5 %30 %40
  1112. %49 = OpLabel
  1113. OpBranchConditional %5 %99 %50
  1114. %30 = OpLabel
  1115. OpBranch %49
  1116. %40 = OpLabel
  1117. OpBranch %49
  1118. %50 = OpLabel
  1119. OpSelectionMerge %79 None
  1120. OpBranchConditional %5 %60 %70
  1121. %79 = OpLabel
  1122. OpBranch %99
  1123. %60 = OpLabel
  1124. OpBranch %79
  1125. %70 = OpLabel
  1126. OpBranch %79
  1127. OpFunctionEnd
  1128. )";
  1129. const std::string expected =
  1130. R"( OpCapability Shader
  1131. OpMemoryModel Logical Simple
  1132. OpEntryPoint Fragment %100 "main"
  1133. OpExecutionMode %100 OriginUpperLeft
  1134. OpName %1 "var"
  1135. %2 = OpTypeVoid
  1136. %3 = OpTypeFunction %2
  1137. %4 = OpTypeBool
  1138. %5 = OpConstantNull %4
  1139. %6 = OpConstantTrue %4
  1140. %7 = OpConstantFalse %4
  1141. %8 = OpTypeInt 32 0
  1142. %9 = OpTypeInt 32 1
  1143. %11 = OpConstant %8 42
  1144. %12 = OpConstant %9 42
  1145. %13 = OpTypeFunction %8
  1146. %14 = OpConstant %8 0
  1147. %15 = OpConstant %8 1
  1148. %16 = OpConstant %8 2
  1149. %17 = OpConstant %8 3
  1150. %18 = OpConstant %8 4
  1151. %19 = OpConstant %8 5
  1152. %21 = OpConstant %8 6
  1153. %22 = OpConstant %8 7
  1154. %23 = OpConstant %8 8
  1155. %24 = OpConstant %8 10
  1156. %25 = OpConstant %8 20
  1157. %26 = OpConstant %8 30
  1158. %27 = OpConstant %8 40
  1159. %28 = OpConstant %8 50
  1160. %29 = OpConstant %8 90
  1161. %31 = OpConstant %8 99
  1162. %32 = OpTypePointer Private %8
  1163. %1 = OpVariable %32 Private
  1164. %33 = OpConstant %8 999
  1165. %100 = OpFunction %2 None %3
  1166. %10 = OpLabel
  1167. OpSelectionMerge %99 None
  1168. OpSwitch %11 %50 20 %20 50 %50
  1169. %20 = OpLabel
  1170. OpSelectionMerge %49 None
  1171. OpBranchConditional %5 %30 %40
  1172. %30 = OpLabel
  1173. OpBranch %49
  1174. %40 = OpLabel
  1175. OpBranch %49
  1176. %49 = OpLabel
  1177. OpBranchConditional %5 %99 %50
  1178. %50 = OpLabel
  1179. OpSelectionMerge %79 None
  1180. OpBranchConditional %5 %60 %70
  1181. %60 = OpLabel
  1182. OpBranch %79
  1183. %70 = OpLabel
  1184. OpBranch %79
  1185. %79 = OpLabel
  1186. OpBranch %99
  1187. %99 = OpLabel
  1188. OpReturn
  1189. OpFunctionEnd
  1190. )";
  1191. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  1192. input,
  1193. SPV_BINARY_TO_TEXT_OPTION_INDENT |
  1194. SPV_BINARY_TO_TEXT_OPTION_REORDER_BLOCKS,
  1195. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
  1196. expected);
  1197. }
  1198. TEST_F(IndentTest, ReorderedNested) {
  1199. const std::string input = R"(
  1200. OpCapability Shader
  1201. %1 = OpExtInstImport "GLSL.std.450"
  1202. OpMemoryModel Logical GLSL450
  1203. OpEntryPoint Fragment %4 "main" %204
  1204. OpExecutionMode %4 OriginUpperLeft
  1205. OpSource GLSL 450
  1206. OpName %4 "main"
  1207. OpName %16 "ff(vf2;f1;"
  1208. OpName %14 "g"
  1209. OpName %15 "f"
  1210. OpName %19 "vg"
  1211. OpName %20 "Block140"
  1212. OpMemberName %20 0 "a"
  1213. OpMemberName %20 1 "b"
  1214. OpName %22 "b140"
  1215. OpName %35 "sv"
  1216. OpName %39 "s"
  1217. OpName %46 "f"
  1218. OpName %51 "g"
  1219. OpName %57 "x"
  1220. OpName %69 "param"
  1221. OpName %75 "i"
  1222. OpName %80 "vc"
  1223. OpName %88 "j"
  1224. OpName %95 "size"
  1225. OpName %174 "v"
  1226. OpName %187 "i"
  1227. OpName %204 "o_color"
  1228. OpMemberDecorate %20 0 Offset 0
  1229. OpMemberDecorate %20 1 Offset 16
  1230. OpDecorate %20 Block
  1231. OpDecorate %22 DescriptorSet 1
  1232. OpDecorate %22 Binding 0
  1233. OpDecorate %39 DescriptorSet 0
  1234. OpDecorate %39 Binding 1
  1235. OpDecorate %95 SpecId 20
  1236. OpDecorate %204 Location 2
  1237. %2 = OpTypeVoid
  1238. %3 = OpTypeFunction %2
  1239. %6 = OpTypeFloat 32
  1240. %7 = OpTypeVector %6 2
  1241. %8 = OpTypePointer Function %7
  1242. %9 = OpTypeVector %6 4
  1243. %10 = OpTypeInt 32 0
  1244. %11 = OpConstant %10 2
  1245. %12 = OpTypeArray %9 %11
  1246. %13 = OpTypeFunction %12 %8 %6
  1247. %18 = OpTypePointer Private %9
  1248. %19 = OpVariable %18 Private
  1249. %20 = OpTypeStruct %6 %9
  1250. %21 = OpTypePointer Uniform %20
  1251. %22 = OpVariable %21 Uniform
  1252. %23 = OpTypeInt 32 1
  1253. %24 = OpConstant %23 1
  1254. %25 = OpTypePointer Uniform %9
  1255. %28 = OpConstant %6 0
  1256. %29 = OpConstantComposite %9 %28 %28 %28 %28
  1257. %34 = OpTypePointer Function %9
  1258. %36 = OpTypeImage %6 2D 0 0 0 1 Unknown
  1259. %37 = OpTypeSampledImage %36
  1260. %38 = OpTypePointer UniformConstant %37
  1261. %39 = OpVariable %38 UniformConstant
  1262. %41 = OpConstantComposite %7 %28 %28
  1263. %45 = OpTypePointer Function %6
  1264. %47 = OpConstant %23 0
  1265. %48 = OpTypePointer Uniform %6
  1266. %53 = OpConstant %6 1
  1267. %55 = OpTypeBool
  1268. %56 = OpTypePointer Function %55
  1269. %58 = OpConstant %10 0
  1270. %59 = OpTypePointer Private %6
  1271. %74 = OpTypePointer Function %23
  1272. %87 = OpTypePointer Function %10
  1273. %95 = OpSpecConstant %10 2
  1274. %100 = OpConstant %10 1
  1275. %109 = OpConstantComposite %9 %53 %53 %53 %53
  1276. %127 = OpConstant %23 10
  1277. %139 = OpConstant %6 2
  1278. %143 = OpConstant %6 3
  1279. %158 = OpConstant %6 4
  1280. %177 = OpConstant %6 0.5
  1281. %195 = OpConstant %23 100
  1282. %202 = OpTypeVector %10 4
  1283. %203 = OpTypePointer Output %202
  1284. %204 = OpVariable %203 Output
  1285. %4 = OpFunction %2 None %3
  1286. %5 = OpLabel
  1287. %35 = OpVariable %34 Function
  1288. %46 = OpVariable %45 Function
  1289. %51 = OpVariable %45 Function
  1290. %57 = OpVariable %56 Function
  1291. %69 = OpVariable %8 Function
  1292. %75 = OpVariable %74 Function
  1293. %80 = OpVariable %34 Function
  1294. %88 = OpVariable %87 Function
  1295. %174 = OpVariable %45 Function
  1296. %187 = OpVariable %74 Function
  1297. %26 = OpAccessChain %25 %22 %24
  1298. %27 = OpLoad %9 %26
  1299. OpStore %19 %27
  1300. %40 = OpLoad %37 %39
  1301. %42 = OpImageSampleImplicitLod %9 %40 %41
  1302. %43 = OpLoad %9 %19
  1303. %44 = OpFAdd %9 %42 %43
  1304. OpStore %35 %44
  1305. %49 = OpAccessChain %48 %22 %47
  1306. %50 = OpLoad %6 %49
  1307. OpStore %46 %50
  1308. %52 = OpLoad %6 %46
  1309. %54 = OpFAdd %6 %52 %53
  1310. OpStore %51 %54
  1311. %60 = OpAccessChain %59 %19 %58
  1312. %61 = OpLoad %6 %60
  1313. %62 = OpFOrdGreaterThan %55 %61 %28
  1314. OpSelectionMerge %64 None
  1315. OpBranchConditional %62 %63 %64
  1316. %64 = OpLabel
  1317. %73 = OpPhi %55 %62 %5 %72 %63
  1318. OpStore %57 %73
  1319. OpStore %75 %47
  1320. OpBranch %76
  1321. %197 = OpLabel
  1322. OpBranch %190
  1323. %63 = OpLabel
  1324. %65 = OpLoad %6 %46
  1325. %66 = OpLoad %6 %51
  1326. %67 = OpCompositeConstruct %7 %65 %66
  1327. %68 = OpLoad %6 %51
  1328. OpStore %69 %67
  1329. %70 = OpFunctionCall %12 %16 %69 %68
  1330. %71 = OpCompositeExtract %6 %70 0 0
  1331. %72 = OpFOrdGreaterThan %55 %71 %28
  1332. OpBranch %64
  1333. %77 = OpLabel
  1334. %81 = OpLoad %9 %19
  1335. OpStore %80 %81
  1336. %82 = OpAccessChain %45 %80 %58
  1337. %83 = OpLoad %6 %82
  1338. %84 = OpFOrdGreaterThan %55 %83 %28
  1339. OpSelectionMerge %86 None
  1340. OpBranchConditional %84 %85 %113
  1341. %85 = OpLabel
  1342. OpStore %88 %58
  1343. OpBranch %89
  1344. %89 = OpLabel
  1345. OpLoopMerge %91 %92 None
  1346. OpBranch %93
  1347. %93 = OpLabel
  1348. %94 = OpLoad %10 %88
  1349. %96 = OpULessThan %55 %94 %95
  1350. OpBranchConditional %96 %90 %91
  1351. %105 = OpLabel
  1352. OpBranch %92
  1353. %198 = OpLabel
  1354. OpBranch %191
  1355. %163 = OpLabel
  1356. OpBranch %136
  1357. %104 = OpLabel
  1358. OpBranch %91
  1359. %76 = OpLabel
  1360. OpLoopMerge %78 %79 None
  1361. OpBranch %77
  1362. %92 = OpLabel
  1363. %107 = OpLoad %10 %88
  1364. %108 = OpIAdd %10 %107 %24
  1365. OpStore %88 %108
  1366. OpBranch %89
  1367. %91 = OpLabel
  1368. %110 = OpLoad %9 %80
  1369. %111 = OpFAdd %9 %110 %109
  1370. OpStore %80 %111
  1371. OpBranch %79
  1372. %113 = OpLabel
  1373. %114 = OpLoad %9 %80
  1374. %115 = OpFSub %9 %114 %109
  1375. OpStore %80 %115
  1376. OpBranch %86
  1377. %132 = OpLabel
  1378. %137 = OpLoad %6 %51
  1379. %138 = OpFAdd %6 %137 %53
  1380. OpStore %51 %138
  1381. OpBranch %133
  1382. %86 = OpLabel
  1383. %116 = OpAccessChain %45 %80 %100
  1384. %117 = OpLoad %6 %116
  1385. %118 = OpFOrdGreaterThan %55 %117 %28
  1386. OpSelectionMerge %120 None
  1387. OpBranchConditional %118 %119 %120
  1388. %119 = OpLabel
  1389. OpBranch %78
  1390. %120 = OpLabel
  1391. %122 = OpAccessChain %45 %80 %11
  1392. %123 = OpLoad %6 %122
  1393. %124 = OpFAdd %6 %123 %53
  1394. %125 = OpAccessChain %45 %80 %11
  1395. OpStore %125 %124
  1396. OpBranch %79
  1397. %79 = OpLabel
  1398. %126 = OpLoad %23 %75
  1399. %128 = OpSLessThan %55 %126 %127
  1400. OpBranchConditional %128 %76 %78
  1401. %78 = OpLabel
  1402. %129 = OpAccessChain %48 %22 %47
  1403. %130 = OpLoad %6 %129
  1404. %131 = OpConvertFToS %23 %130
  1405. OpSelectionMerge %136 None
  1406. OpSwitch %131 %135 0 %132 1 %132 2 %132 3 %133 4 %134
  1407. %90 = OpLabel
  1408. %97 = OpLoad %9 %19
  1409. %98 = OpLoad %9 %80
  1410. %99 = OpFAdd %9 %98 %97
  1411. OpStore %80 %99
  1412. %101 = OpAccessChain %45 %80 %100
  1413. %102 = OpLoad %6 %101
  1414. %103 = OpFOrdLessThan %55 %102 %28
  1415. OpSelectionMerge %105 None
  1416. OpBranchConditional %103 %104 %105
  1417. %161 = OpLabel
  1418. OpLoopMerge %163 %164 None
  1419. OpBranch %165
  1420. %165 = OpLabel
  1421. %166 = OpLoad %6 %51
  1422. %167 = OpFOrdLessThan %55 %166 %139
  1423. OpBranchConditional %167 %162 %163
  1424. %164 = OpLabel
  1425. OpBranch %161
  1426. %162 = OpLabel
  1427. %168 = OpLoad %6 %46
  1428. %169 = OpFOrdLessThan %55 %168 %53
  1429. OpSelectionMerge %171 None
  1430. OpBranchConditional %169 %170 %171
  1431. %135 = OpLabel
  1432. %159 = OpLoad %6 %51
  1433. %160 = OpFAdd %6 %159 %158
  1434. OpStore %51 %160
  1435. OpBranch %161
  1436. %133 = OpLabel
  1437. %140 = OpLoad %6 %51
  1438. %141 = OpFAdd %6 %140 %139
  1439. OpStore %51 %141
  1440. OpBranch %136
  1441. %134 = OpLabel
  1442. %144 = OpLoad %6 %51
  1443. %145 = OpFAdd %6 %144 %143
  1444. OpStore %51 %145
  1445. OpBranch %146
  1446. %146 = OpLabel
  1447. OpLoopMerge %148 %149 None
  1448. OpBranch %150
  1449. %150 = OpLabel
  1450. %151 = OpLoad %6 %51
  1451. %152 = OpFOrdLessThan %55 %151 %139
  1452. OpBranchConditional %152 %147 %148
  1453. %147 = OpLabel
  1454. %153 = OpLoad %6 %46
  1455. %154 = OpFOrdLessThan %55 %153 %53
  1456. OpSelectionMerge %156 None
  1457. OpBranchConditional %154 %155 %156
  1458. %155 = OpLabel
  1459. OpBranch %148
  1460. %156 = OpLabel
  1461. OpBranch %149
  1462. %149 = OpLabel
  1463. OpBranch %146
  1464. %148 = OpLabel
  1465. OpBranch %135
  1466. %136 = OpLabel
  1467. OpStore %174 %53
  1468. %175 = OpAccessChain %45 %35 %58
  1469. %176 = OpLoad %6 %175
  1470. %178 = OpFOrdLessThanEqual %55 %176 %177
  1471. OpSelectionMerge %180 None
  1472. OpBranchConditional %178 %179 %181
  1473. %179 = OpLabel
  1474. OpStore %174 %28
  1475. OpBranch %180
  1476. %185 = OpLabel
  1477. OpStore %174 %139
  1478. OpBranch %186
  1479. %181 = OpLabel
  1480. %182 = OpAccessChain %45 %35 %58
  1481. %183 = OpLoad %6 %182
  1482. %184 = OpFOrdGreaterThanEqual %55 %183 %177
  1483. OpSelectionMerge %186 None
  1484. OpBranchConditional %184 %185 %186
  1485. %170 = OpLabel
  1486. OpBranch %163
  1487. %171 = OpLabel
  1488. OpBranch %164
  1489. %186 = OpLabel
  1490. OpBranch %180
  1491. %188 = OpLabel
  1492. OpLoopMerge %190 %191 None
  1493. OpBranch %189
  1494. %189 = OpLabel
  1495. %192 = OpLoad %9 %19
  1496. %193 = OpFAdd %9 %192 %109
  1497. OpStore %19 %193
  1498. %194 = OpLoad %23 %187
  1499. %196 = OpSGreaterThan %55 %194 %195
  1500. OpSelectionMerge %198 None
  1501. OpBranchConditional %196 %197 %198
  1502. %180 = OpLabel
  1503. OpStore %187 %47
  1504. OpBranch %188
  1505. %191 = OpLabel
  1506. %200 = OpLoad %23 %187
  1507. %201 = OpIAdd %23 %200 %24
  1508. OpStore %187 %201
  1509. OpBranch %188
  1510. %190 = OpLabel
  1511. OpReturn
  1512. OpFunctionEnd
  1513. %16 = OpFunction %12 None %13
  1514. %14 = OpFunctionParameter %8
  1515. %15 = OpFunctionParameter %6
  1516. %17 = OpLabel
  1517. %30 = OpCompositeConstruct %9 %15 %15 %15 %15
  1518. %31 = OpCompositeConstruct %12 %29 %30
  1519. OpReturnValue %31
  1520. OpFunctionEnd
  1521. )";
  1522. const std::string expected =
  1523. R"( OpCapability Shader
  1524. %1 = OpExtInstImport "GLSL.std.450"
  1525. OpMemoryModel Logical GLSL450
  1526. OpEntryPoint Fragment %4 "main" %204
  1527. OpExecutionMode %4 OriginUpperLeft
  1528. OpSource GLSL 450
  1529. OpName %4 "main"
  1530. OpName %16 "ff(vf2;f1;"
  1531. OpName %14 "g"
  1532. OpName %15 "f"
  1533. OpName %19 "vg"
  1534. OpName %20 "Block140"
  1535. OpMemberName %20 0 "a"
  1536. OpMemberName %20 1 "b"
  1537. OpName %22 "b140"
  1538. OpName %35 "sv"
  1539. OpName %39 "s"
  1540. OpName %46 "f"
  1541. OpName %51 "g"
  1542. OpName %57 "x"
  1543. OpName %69 "param"
  1544. OpName %75 "i"
  1545. OpName %80 "vc"
  1546. OpName %88 "j"
  1547. OpName %95 "size"
  1548. OpName %174 "v"
  1549. OpName %187 "i"
  1550. OpName %204 "o_color"
  1551. OpMemberDecorate %20 0 Offset 0
  1552. OpMemberDecorate %20 1 Offset 16
  1553. OpDecorate %20 Block
  1554. OpDecorate %22 DescriptorSet 1
  1555. OpDecorate %22 Binding 0
  1556. OpDecorate %39 DescriptorSet 0
  1557. OpDecorate %39 Binding 1
  1558. OpDecorate %95 SpecId 20
  1559. OpDecorate %204 Location 2
  1560. %2 = OpTypeVoid
  1561. %3 = OpTypeFunction %2
  1562. %6 = OpTypeFloat 32
  1563. %7 = OpTypeVector %6 2
  1564. %8 = OpTypePointer Function %7
  1565. %9 = OpTypeVector %6 4
  1566. %10 = OpTypeInt 32 0
  1567. %11 = OpConstant %10 2
  1568. %12 = OpTypeArray %9 %11
  1569. %13 = OpTypeFunction %12 %8 %6
  1570. %18 = OpTypePointer Private %9
  1571. %19 = OpVariable %18 Private
  1572. %20 = OpTypeStruct %6 %9
  1573. %21 = OpTypePointer Uniform %20
  1574. %22 = OpVariable %21 Uniform
  1575. %23 = OpTypeInt 32 1
  1576. %24 = OpConstant %23 1
  1577. %25 = OpTypePointer Uniform %9
  1578. %28 = OpConstant %6 0
  1579. %29 = OpConstantComposite %9 %28 %28 %28 %28
  1580. %34 = OpTypePointer Function %9
  1581. %36 = OpTypeImage %6 2D 0 0 0 1 Unknown
  1582. %37 = OpTypeSampledImage %36
  1583. %38 = OpTypePointer UniformConstant %37
  1584. %39 = OpVariable %38 UniformConstant
  1585. %41 = OpConstantComposite %7 %28 %28
  1586. %45 = OpTypePointer Function %6
  1587. %47 = OpConstant %23 0
  1588. %48 = OpTypePointer Uniform %6
  1589. %53 = OpConstant %6 1
  1590. %55 = OpTypeBool
  1591. %56 = OpTypePointer Function %55
  1592. %58 = OpConstant %10 0
  1593. %59 = OpTypePointer Private %6
  1594. %74 = OpTypePointer Function %23
  1595. %87 = OpTypePointer Function %10
  1596. %95 = OpSpecConstant %10 2
  1597. %100 = OpConstant %10 1
  1598. %109 = OpConstantComposite %9 %53 %53 %53 %53
  1599. %127 = OpConstant %23 10
  1600. %139 = OpConstant %6 2
  1601. %143 = OpConstant %6 3
  1602. %158 = OpConstant %6 4
  1603. %177 = OpConstant %6 0.5
  1604. %195 = OpConstant %23 100
  1605. %202 = OpTypeVector %10 4
  1606. %203 = OpTypePointer Output %202
  1607. %204 = OpVariable %203 Output
  1608. %4 = OpFunction %2 None %3
  1609. %5 = OpLabel
  1610. %35 = OpVariable %34 Function
  1611. %46 = OpVariable %45 Function
  1612. %51 = OpVariable %45 Function
  1613. %57 = OpVariable %56 Function
  1614. %69 = OpVariable %8 Function
  1615. %75 = OpVariable %74 Function
  1616. %80 = OpVariable %34 Function
  1617. %88 = OpVariable %87 Function
  1618. %174 = OpVariable %45 Function
  1619. %187 = OpVariable %74 Function
  1620. %26 = OpAccessChain %25 %22 %24
  1621. %27 = OpLoad %9 %26
  1622. OpStore %19 %27
  1623. %40 = OpLoad %37 %39
  1624. %42 = OpImageSampleImplicitLod %9 %40 %41
  1625. %43 = OpLoad %9 %19
  1626. %44 = OpFAdd %9 %42 %43
  1627. OpStore %35 %44
  1628. %49 = OpAccessChain %48 %22 %47
  1629. %50 = OpLoad %6 %49
  1630. OpStore %46 %50
  1631. %52 = OpLoad %6 %46
  1632. %54 = OpFAdd %6 %52 %53
  1633. OpStore %51 %54
  1634. %60 = OpAccessChain %59 %19 %58
  1635. %61 = OpLoad %6 %60
  1636. %62 = OpFOrdGreaterThan %55 %61 %28
  1637. OpSelectionMerge %64 None
  1638. OpBranchConditional %62 %63 %64
  1639. %63 = OpLabel
  1640. %65 = OpLoad %6 %46
  1641. %66 = OpLoad %6 %51
  1642. %67 = OpCompositeConstruct %7 %65 %66
  1643. %68 = OpLoad %6 %51
  1644. OpStore %69 %67
  1645. %70 = OpFunctionCall %12 %16 %69 %68
  1646. %71 = OpCompositeExtract %6 %70 0 0
  1647. %72 = OpFOrdGreaterThan %55 %71 %28
  1648. OpBranch %64
  1649. %64 = OpLabel
  1650. %73 = OpPhi %55 %62 %5 %72 %63
  1651. OpStore %57 %73
  1652. OpStore %75 %47
  1653. OpBranch %76
  1654. %76 = OpLabel
  1655. OpLoopMerge %78 %79 None
  1656. OpBranch %77
  1657. %77 = OpLabel
  1658. %81 = OpLoad %9 %19
  1659. OpStore %80 %81
  1660. %82 = OpAccessChain %45 %80 %58
  1661. %83 = OpLoad %6 %82
  1662. %84 = OpFOrdGreaterThan %55 %83 %28
  1663. OpSelectionMerge %86 None
  1664. OpBranchConditional %84 %85 %113
  1665. %85 = OpLabel
  1666. OpStore %88 %58
  1667. OpBranch %89
  1668. %89 = OpLabel
  1669. OpLoopMerge %91 %92 None
  1670. OpBranch %93
  1671. %93 = OpLabel
  1672. %94 = OpLoad %10 %88
  1673. %96 = OpULessThan %55 %94 %95
  1674. OpBranchConditional %96 %90 %91
  1675. %90 = OpLabel
  1676. %97 = OpLoad %9 %19
  1677. %98 = OpLoad %9 %80
  1678. %99 = OpFAdd %9 %98 %97
  1679. OpStore %80 %99
  1680. %101 = OpAccessChain %45 %80 %100
  1681. %102 = OpLoad %6 %101
  1682. %103 = OpFOrdLessThan %55 %102 %28
  1683. OpSelectionMerge %105 None
  1684. OpBranchConditional %103 %104 %105
  1685. %104 = OpLabel
  1686. OpBranch %91
  1687. %105 = OpLabel
  1688. OpBranch %92
  1689. %92 = OpLabel
  1690. %107 = OpLoad %10 %88
  1691. %108 = OpIAdd %10 %107 %24
  1692. OpStore %88 %108
  1693. OpBranch %89
  1694. %91 = OpLabel
  1695. %110 = OpLoad %9 %80
  1696. %111 = OpFAdd %9 %110 %109
  1697. OpStore %80 %111
  1698. OpBranch %79
  1699. %113 = OpLabel
  1700. %114 = OpLoad %9 %80
  1701. %115 = OpFSub %9 %114 %109
  1702. OpStore %80 %115
  1703. OpBranch %86
  1704. %86 = OpLabel
  1705. %116 = OpAccessChain %45 %80 %100
  1706. %117 = OpLoad %6 %116
  1707. %118 = OpFOrdGreaterThan %55 %117 %28
  1708. OpSelectionMerge %120 None
  1709. OpBranchConditional %118 %119 %120
  1710. %119 = OpLabel
  1711. OpBranch %78
  1712. %120 = OpLabel
  1713. %122 = OpAccessChain %45 %80 %11
  1714. %123 = OpLoad %6 %122
  1715. %124 = OpFAdd %6 %123 %53
  1716. %125 = OpAccessChain %45 %80 %11
  1717. OpStore %125 %124
  1718. OpBranch %79
  1719. %79 = OpLabel
  1720. %126 = OpLoad %23 %75
  1721. %128 = OpSLessThan %55 %126 %127
  1722. OpBranchConditional %128 %76 %78
  1723. %78 = OpLabel
  1724. %129 = OpAccessChain %48 %22 %47
  1725. %130 = OpLoad %6 %129
  1726. %131 = OpConvertFToS %23 %130
  1727. OpSelectionMerge %136 None
  1728. OpSwitch %131 %135 0 %132 1 %132 2 %132 3 %133 4 %134
  1729. %132 = OpLabel
  1730. %137 = OpLoad %6 %51
  1731. %138 = OpFAdd %6 %137 %53
  1732. OpStore %51 %138
  1733. OpBranch %133
  1734. %133 = OpLabel
  1735. %140 = OpLoad %6 %51
  1736. %141 = OpFAdd %6 %140 %139
  1737. OpStore %51 %141
  1738. OpBranch %136
  1739. %134 = OpLabel
  1740. %144 = OpLoad %6 %51
  1741. %145 = OpFAdd %6 %144 %143
  1742. OpStore %51 %145
  1743. OpBranch %146
  1744. %146 = OpLabel
  1745. OpLoopMerge %148 %149 None
  1746. OpBranch %150
  1747. %150 = OpLabel
  1748. %151 = OpLoad %6 %51
  1749. %152 = OpFOrdLessThan %55 %151 %139
  1750. OpBranchConditional %152 %147 %148
  1751. %147 = OpLabel
  1752. %153 = OpLoad %6 %46
  1753. %154 = OpFOrdLessThan %55 %153 %53
  1754. OpSelectionMerge %156 None
  1755. OpBranchConditional %154 %155 %156
  1756. %155 = OpLabel
  1757. OpBranch %148
  1758. %156 = OpLabel
  1759. OpBranch %149
  1760. %149 = OpLabel
  1761. OpBranch %146
  1762. %148 = OpLabel
  1763. OpBranch %135
  1764. %135 = OpLabel
  1765. %159 = OpLoad %6 %51
  1766. %160 = OpFAdd %6 %159 %158
  1767. OpStore %51 %160
  1768. OpBranch %161
  1769. %161 = OpLabel
  1770. OpLoopMerge %163 %164 None
  1771. OpBranch %165
  1772. %165 = OpLabel
  1773. %166 = OpLoad %6 %51
  1774. %167 = OpFOrdLessThan %55 %166 %139
  1775. OpBranchConditional %167 %162 %163
  1776. %162 = OpLabel
  1777. %168 = OpLoad %6 %46
  1778. %169 = OpFOrdLessThan %55 %168 %53
  1779. OpSelectionMerge %171 None
  1780. OpBranchConditional %169 %170 %171
  1781. %170 = OpLabel
  1782. OpBranch %163
  1783. %171 = OpLabel
  1784. OpBranch %164
  1785. %164 = OpLabel
  1786. OpBranch %161
  1787. %163 = OpLabel
  1788. OpBranch %136
  1789. %136 = OpLabel
  1790. OpStore %174 %53
  1791. %175 = OpAccessChain %45 %35 %58
  1792. %176 = OpLoad %6 %175
  1793. %178 = OpFOrdLessThanEqual %55 %176 %177
  1794. OpSelectionMerge %180 None
  1795. OpBranchConditional %178 %179 %181
  1796. %179 = OpLabel
  1797. OpStore %174 %28
  1798. OpBranch %180
  1799. %181 = OpLabel
  1800. %182 = OpAccessChain %45 %35 %58
  1801. %183 = OpLoad %6 %182
  1802. %184 = OpFOrdGreaterThanEqual %55 %183 %177
  1803. OpSelectionMerge %186 None
  1804. OpBranchConditional %184 %185 %186
  1805. %185 = OpLabel
  1806. OpStore %174 %139
  1807. OpBranch %186
  1808. %186 = OpLabel
  1809. OpBranch %180
  1810. %180 = OpLabel
  1811. OpStore %187 %47
  1812. OpBranch %188
  1813. %188 = OpLabel
  1814. OpLoopMerge %190 %191 None
  1815. OpBranch %189
  1816. %189 = OpLabel
  1817. %192 = OpLoad %9 %19
  1818. %193 = OpFAdd %9 %192 %109
  1819. OpStore %19 %193
  1820. %194 = OpLoad %23 %187
  1821. %196 = OpSGreaterThan %55 %194 %195
  1822. OpSelectionMerge %198 None
  1823. OpBranchConditional %196 %197 %198
  1824. %197 = OpLabel
  1825. OpBranch %190
  1826. %198 = OpLabel
  1827. OpBranch %191
  1828. %191 = OpLabel
  1829. %200 = OpLoad %23 %187
  1830. %201 = OpIAdd %23 %200 %24
  1831. OpStore %187 %201
  1832. OpBranch %188
  1833. %190 = OpLabel
  1834. OpReturn
  1835. OpFunctionEnd
  1836. %16 = OpFunction %12 None %13
  1837. %14 = OpFunctionParameter %8
  1838. %15 = OpFunctionParameter %6
  1839. %17 = OpLabel
  1840. %30 = OpCompositeConstruct %9 %15 %15 %15 %15
  1841. %31 = OpCompositeConstruct %12 %29 %30
  1842. OpReturnValue %31
  1843. OpFunctionEnd
  1844. )";
  1845. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  1846. input,
  1847. SPV_BINARY_TO_TEXT_OPTION_INDENT |
  1848. SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT |
  1849. SPV_BINARY_TO_TEXT_OPTION_REORDER_BLOCKS,
  1850. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
  1851. expected);
  1852. }
  1853. using FriendlyNameDisassemblyTest = spvtest::TextToBinaryTest;
  1854. TEST_F(FriendlyNameDisassemblyTest, Sample) {
  1855. const std::string input = R"(
  1856. OpCapability Shader
  1857. OpMemoryModel Logical GLSL450
  1858. %1 = OpTypeInt 32 0
  1859. %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10 ; force IDs into double digits
  1860. %11 = OpConstant %1 42
  1861. )";
  1862. const std::string expected =
  1863. R"(OpCapability Shader
  1864. OpMemoryModel Logical GLSL450
  1865. %uint = OpTypeInt 32 0
  1866. %_struct_2 = OpTypeStruct %uint %3 %4 %5 %6 %7 %8 %9 %10
  1867. %uint_42 = OpConstant %uint 42
  1868. )";
  1869. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  1870. input, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES),
  1871. expected);
  1872. }
  1873. TEST_F(TextToBinaryTest, ShowByteOffsetsWhenRequested) {
  1874. const std::string input = R"(
  1875. OpCapability Shader
  1876. OpMemoryModel Logical GLSL450
  1877. %1 = OpTypeInt 32 0
  1878. %2 = OpTypeVoid
  1879. )";
  1880. const std::string expected =
  1881. R"(OpCapability Shader ; 0x00000014
  1882. OpMemoryModel Logical GLSL450 ; 0x0000001c
  1883. %1 = OpTypeInt 32 0 ; 0x00000028
  1884. %2 = OpTypeVoid ; 0x00000038
  1885. )";
  1886. EXPECT_THAT(EncodeAndDecodeSuccessfully(
  1887. input, SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET),
  1888. expected);
  1889. }
  1890. TEST_F(TextToBinaryTest, Comments) {
  1891. const std::string input = R"(OpCapability Shader
  1892. OpMemoryModel Logical GLSL450
  1893. OpEntryPoint Fragment %63 "main" %4 %22
  1894. OpExecutionMode %63 OriginUpperLeft
  1895. OpSource GLSL 450
  1896. OpName %4 "_ue"
  1897. OpName %8 "_uf"
  1898. OpName %11 "_ug"
  1899. OpName %12 "_uA"
  1900. OpMemberName %12 0 "_ux"
  1901. OpName %14 "_uc"
  1902. OpName %15 "_uB"
  1903. OpMemberName %15 0 "_ux"
  1904. OpName %20 "_ud"
  1905. OpName %22 "_ucol"
  1906. OpName %26 "ANGLEDepthRangeParams"
  1907. OpMemberName %26 0 "near"
  1908. OpMemberName %26 1 "far"
  1909. OpMemberName %26 2 "diff"
  1910. OpMemberName %26 3 "reserved"
  1911. OpName %27 "ANGLEUniformBlock"
  1912. OpMemberName %27 0 "viewport"
  1913. OpMemberName %27 1 "clipDistancesEnabled"
  1914. OpMemberName %27 2 "xfbActiveUnpaused"
  1915. OpMemberName %27 3 "xfbVerticesPerInstance"
  1916. OpMemberName %27 4 "numSamples"
  1917. OpMemberName %27 5 "xfbBufferOffsets"
  1918. OpMemberName %27 6 "acbBufferOffsets"
  1919. OpMemberName %27 7 "depthRange"
  1920. OpName %29 "ANGLEUniforms"
  1921. OpName %33 "_uc"
  1922. OpName %32 "_uh"
  1923. OpName %49 "_ux"
  1924. OpName %50 "_uy"
  1925. OpName %48 "_ui"
  1926. OpName %63 "main"
  1927. OpName %65 "param"
  1928. OpName %68 "param"
  1929. OpName %73 "param"
  1930. OpDecorate %4 Location 0
  1931. OpDecorate %8 RelaxedPrecision
  1932. OpDecorate %8 DescriptorSet 0
  1933. OpDecorate %8 Binding 0
  1934. OpDecorate %11 DescriptorSet 0
  1935. OpDecorate %11 Binding 1
  1936. OpMemberDecorate %12 0 Offset 0
  1937. OpMemberDecorate %12 0 RelaxedPrecision
  1938. OpDecorate %12 Block
  1939. OpDecorate %14 DescriptorSet 0
  1940. OpDecorate %14 Binding 2
  1941. OpMemberDecorate %15 0 Offset 0
  1942. OpMemberDecorate %15 0 RelaxedPrecision
  1943. OpDecorate %15 BufferBlock
  1944. OpDecorate %20 DescriptorSet 0
  1945. OpDecorate %20 Binding 3
  1946. OpDecorate %22 RelaxedPrecision
  1947. OpDecorate %22 Location 0
  1948. OpMemberDecorate %26 0 Offset 0
  1949. OpMemberDecorate %26 1 Offset 4
  1950. OpMemberDecorate %26 2 Offset 8
  1951. OpMemberDecorate %26 3 Offset 12
  1952. OpMemberDecorate %27 0 Offset 0
  1953. OpMemberDecorate %27 1 Offset 16
  1954. OpMemberDecorate %27 2 Offset 20
  1955. OpMemberDecorate %27 3 Offset 24
  1956. OpMemberDecorate %27 4 Offset 28
  1957. OpMemberDecorate %27 5 Offset 32
  1958. OpMemberDecorate %27 6 Offset 48
  1959. OpMemberDecorate %27 7 Offset 64
  1960. OpMemberDecorate %27 2 RelaxedPrecision
  1961. OpMemberDecorate %27 4 RelaxedPrecision
  1962. OpDecorate %27 Block
  1963. OpDecorate %29 DescriptorSet 0
  1964. OpDecorate %29 Binding 4
  1965. OpDecorate %32 RelaxedPrecision
  1966. OpDecorate %33 RelaxedPrecision
  1967. OpDecorate %36 RelaxedPrecision
  1968. OpDecorate %37 RelaxedPrecision
  1969. OpDecorate %38 RelaxedPrecision
  1970. OpDecorate %39 RelaxedPrecision
  1971. OpDecorate %41 RelaxedPrecision
  1972. OpDecorate %42 RelaxedPrecision
  1973. OpDecorate %43 RelaxedPrecision
  1974. OpDecorate %48 RelaxedPrecision
  1975. OpDecorate %49 RelaxedPrecision
  1976. OpDecorate %50 RelaxedPrecision
  1977. OpDecorate %52 RelaxedPrecision
  1978. OpDecorate %53 RelaxedPrecision
  1979. OpDecorate %54 RelaxedPrecision
  1980. OpDecorate %55 RelaxedPrecision
  1981. OpDecorate %56 RelaxedPrecision
  1982. OpDecorate %57 RelaxedPrecision
  1983. OpDecorate %58 RelaxedPrecision
  1984. OpDecorate %59 RelaxedPrecision
  1985. OpDecorate %60 RelaxedPrecision
  1986. OpDecorate %67 RelaxedPrecision
  1987. OpDecorate %68 RelaxedPrecision
  1988. OpDecorate %72 RelaxedPrecision
  1989. OpDecorate %73 RelaxedPrecision
  1990. OpDecorate %75 RelaxedPrecision
  1991. OpDecorate %76 RelaxedPrecision
  1992. OpDecorate %77 RelaxedPrecision
  1993. OpDecorate %80 RelaxedPrecision
  1994. OpDecorate %81 RelaxedPrecision
  1995. %1 = OpTypeFloat 32
  1996. %2 = OpTypeVector %1 4
  1997. %5 = OpTypeImage %1 2D 0 0 0 1 Unknown
  1998. %6 = OpTypeSampledImage %5
  1999. %9 = OpTypeImage %1 2D 0 0 0 2 Rgba8
  2000. %12 = OpTypeStruct %2
  2001. %15 = OpTypeStruct %2
  2002. %16 = OpTypeInt 32 0
  2003. %17 = OpConstant %16 2
  2004. %18 = OpTypeArray %15 %17
  2005. %23 = OpTypeInt 32 1
  2006. %24 = OpTypeVector %23 4
  2007. %25 = OpTypeVector %16 4
  2008. %26 = OpTypeStruct %1 %1 %1 %1
  2009. %27 = OpTypeStruct %2 %16 %16 %23 %23 %24 %25 %26
  2010. %35 = OpTypeVector %1 2
  2011. %40 = OpTypeVector %23 2
  2012. %61 = OpTypeVoid
  2013. %69 = OpConstant %16 0
  2014. %78 = OpConstant %16 1
  2015. %3 = OpTypePointer Input %2
  2016. %7 = OpTypePointer UniformConstant %6
  2017. %10 = OpTypePointer UniformConstant %9
  2018. %13 = OpTypePointer Uniform %12
  2019. %19 = OpTypePointer Uniform %18
  2020. %21 = OpTypePointer Output %2
  2021. %28 = OpTypePointer Uniform %27
  2022. %30 = OpTypePointer Function %2
  2023. %70 = OpTypePointer Uniform %2
  2024. %31 = OpTypeFunction %2 %30
  2025. %47 = OpTypeFunction %2 %30 %30
  2026. %62 = OpTypeFunction %61
  2027. %4 = OpVariable %3 Input
  2028. %8 = OpVariable %7 UniformConstant
  2029. %11 = OpVariable %10 UniformConstant
  2030. %14 = OpVariable %13 Uniform
  2031. %20 = OpVariable %19 Uniform
  2032. %22 = OpVariable %21 Output
  2033. %29 = OpVariable %28 Uniform
  2034. %32 = OpFunction %2 None %31
  2035. %33 = OpFunctionParameter %30
  2036. %34 = OpLabel
  2037. %36 = OpLoad %6 %8
  2038. %37 = OpLoad %2 %33
  2039. %38 = OpVectorShuffle %35 %37 %37 0 1
  2040. %39 = OpImageSampleImplicitLod %2 %36 %38
  2041. %41 = OpLoad %2 %33
  2042. %42 = OpVectorShuffle %35 %41 %41 2 3
  2043. %43 = OpConvertFToS %40 %42
  2044. %44 = OpLoad %9 %11
  2045. %45 = OpImageRead %2 %44 %43
  2046. %46 = OpFAdd %2 %39 %45
  2047. OpReturnValue %46
  2048. OpFunctionEnd
  2049. %48 = OpFunction %2 None %47
  2050. %49 = OpFunctionParameter %30
  2051. %50 = OpFunctionParameter %30
  2052. %51 = OpLabel
  2053. %52 = OpLoad %2 %49
  2054. %53 = OpVectorShuffle %35 %52 %52 0 1
  2055. %54 = OpLoad %2 %50
  2056. %55 = OpVectorShuffle %35 %54 %54 2 3
  2057. %56 = OpCompositeExtract %1 %53 0
  2058. %57 = OpCompositeExtract %1 %53 1
  2059. %58 = OpCompositeExtract %1 %55 0
  2060. %59 = OpCompositeExtract %1 %55 1
  2061. %60 = OpCompositeConstruct %2 %56 %57 %58 %59
  2062. OpReturnValue %60
  2063. OpFunctionEnd
  2064. %63 = OpFunction %61 None %62
  2065. %64 = OpLabel
  2066. %65 = OpVariable %30 Function
  2067. %68 = OpVariable %30 Function
  2068. %73 = OpVariable %30 Function
  2069. %66 = OpLoad %2 %4
  2070. OpStore %65 %66
  2071. %67 = OpFunctionCall %2 %32 %65
  2072. %71 = OpAccessChain %70 %14 %69
  2073. %72 = OpLoad %2 %71
  2074. OpStore %68 %72
  2075. %74 = OpAccessChain %70 %20 %69 %69
  2076. %75 = OpLoad %2 %74
  2077. OpStore %73 %75
  2078. %76 = OpFunctionCall %2 %48 %68 %73
  2079. %77 = OpFAdd %2 %67 %76
  2080. %79 = OpAccessChain %70 %20 %78 %69
  2081. %80 = OpLoad %2 %79
  2082. %81 = OpFAdd %2 %77 %80
  2083. OpStore %22 %81
  2084. OpReturn
  2085. OpFunctionEnd
  2086. )";
  2087. const std::string expected = R"( OpCapability Shader
  2088. OpMemoryModel Logical GLSL450
  2089. OpEntryPoint Fragment %63 "main" %4 %22
  2090. OpExecutionMode %63 OriginUpperLeft
  2091. ; Debug Information
  2092. OpSource GLSL 450
  2093. OpName %4 "_ue" ; id %4
  2094. OpName %8 "_uf" ; id %8
  2095. OpName %11 "_ug" ; id %11
  2096. OpName %12 "_uA" ; id %12
  2097. OpMemberName %12 0 "_ux"
  2098. OpName %14 "_uc" ; id %14
  2099. OpName %15 "_uB" ; id %15
  2100. OpMemberName %15 0 "_ux"
  2101. OpName %20 "_ud" ; id %20
  2102. OpName %22 "_ucol" ; id %22
  2103. OpName %26 "ANGLEDepthRangeParams" ; id %26
  2104. OpMemberName %26 0 "near"
  2105. OpMemberName %26 1 "far"
  2106. OpMemberName %26 2 "diff"
  2107. OpMemberName %26 3 "reserved"
  2108. OpName %27 "ANGLEUniformBlock" ; id %27
  2109. OpMemberName %27 0 "viewport"
  2110. OpMemberName %27 1 "clipDistancesEnabled"
  2111. OpMemberName %27 2 "xfbActiveUnpaused"
  2112. OpMemberName %27 3 "xfbVerticesPerInstance"
  2113. OpMemberName %27 4 "numSamples"
  2114. OpMemberName %27 5 "xfbBufferOffsets"
  2115. OpMemberName %27 6 "acbBufferOffsets"
  2116. OpMemberName %27 7 "depthRange"
  2117. OpName %29 "ANGLEUniforms" ; id %29
  2118. OpName %33 "_uc" ; id %33
  2119. OpName %32 "_uh" ; id %32
  2120. OpName %49 "_ux" ; id %49
  2121. OpName %50 "_uy" ; id %50
  2122. OpName %48 "_ui" ; id %48
  2123. OpName %63 "main" ; id %63
  2124. OpName %65 "param" ; id %65
  2125. OpName %68 "param" ; id %68
  2126. OpName %73 "param" ; id %73
  2127. ; Annotations
  2128. OpDecorate %4 Location 0
  2129. OpDecorate %8 RelaxedPrecision
  2130. OpDecorate %8 DescriptorSet 0
  2131. OpDecorate %8 Binding 0
  2132. OpDecorate %11 DescriptorSet 0
  2133. OpDecorate %11 Binding 1
  2134. OpMemberDecorate %12 0 Offset 0
  2135. OpMemberDecorate %12 0 RelaxedPrecision
  2136. OpDecorate %12 Block
  2137. OpDecorate %14 DescriptorSet 0
  2138. OpDecorate %14 Binding 2
  2139. OpMemberDecorate %15 0 Offset 0
  2140. OpMemberDecorate %15 0 RelaxedPrecision
  2141. OpDecorate %15 BufferBlock
  2142. OpDecorate %20 DescriptorSet 0
  2143. OpDecorate %20 Binding 3
  2144. OpDecorate %22 RelaxedPrecision
  2145. OpDecorate %22 Location 0
  2146. OpMemberDecorate %26 0 Offset 0
  2147. OpMemberDecorate %26 1 Offset 4
  2148. OpMemberDecorate %26 2 Offset 8
  2149. OpMemberDecorate %26 3 Offset 12
  2150. OpMemberDecorate %27 0 Offset 0
  2151. OpMemberDecorate %27 1 Offset 16
  2152. OpMemberDecorate %27 2 Offset 20
  2153. OpMemberDecorate %27 3 Offset 24
  2154. OpMemberDecorate %27 4 Offset 28
  2155. OpMemberDecorate %27 5 Offset 32
  2156. OpMemberDecorate %27 6 Offset 48
  2157. OpMemberDecorate %27 7 Offset 64
  2158. OpMemberDecorate %27 2 RelaxedPrecision
  2159. OpMemberDecorate %27 4 RelaxedPrecision
  2160. OpDecorate %27 Block
  2161. OpDecorate %29 DescriptorSet 0
  2162. OpDecorate %29 Binding 4
  2163. OpDecorate %32 RelaxedPrecision
  2164. OpDecorate %33 RelaxedPrecision
  2165. OpDecorate %36 RelaxedPrecision
  2166. OpDecorate %37 RelaxedPrecision
  2167. OpDecorate %38 RelaxedPrecision
  2168. OpDecorate %39 RelaxedPrecision
  2169. OpDecorate %41 RelaxedPrecision
  2170. OpDecorate %42 RelaxedPrecision
  2171. OpDecorate %43 RelaxedPrecision
  2172. OpDecorate %48 RelaxedPrecision
  2173. OpDecorate %49 RelaxedPrecision
  2174. OpDecorate %50 RelaxedPrecision
  2175. OpDecorate %52 RelaxedPrecision
  2176. OpDecorate %53 RelaxedPrecision
  2177. OpDecorate %54 RelaxedPrecision
  2178. OpDecorate %55 RelaxedPrecision
  2179. OpDecorate %56 RelaxedPrecision
  2180. OpDecorate %57 RelaxedPrecision
  2181. OpDecorate %58 RelaxedPrecision
  2182. OpDecorate %59 RelaxedPrecision
  2183. OpDecorate %60 RelaxedPrecision
  2184. OpDecorate %67 RelaxedPrecision
  2185. OpDecorate %68 RelaxedPrecision
  2186. OpDecorate %72 RelaxedPrecision
  2187. OpDecorate %73 RelaxedPrecision
  2188. OpDecorate %75 RelaxedPrecision
  2189. OpDecorate %76 RelaxedPrecision
  2190. OpDecorate %77 RelaxedPrecision
  2191. OpDecorate %80 RelaxedPrecision
  2192. OpDecorate %81 RelaxedPrecision
  2193. ; Types, variables and constants
  2194. %1 = OpTypeFloat 32
  2195. %2 = OpTypeVector %1 4
  2196. %5 = OpTypeImage %1 2D 0 0 0 1 Unknown
  2197. %6 = OpTypeSampledImage %5
  2198. %9 = OpTypeImage %1 2D 0 0 0 2 Rgba8
  2199. %12 = OpTypeStruct %2 ; Block
  2200. %15 = OpTypeStruct %2 ; BufferBlock
  2201. %16 = OpTypeInt 32 0
  2202. %17 = OpConstant %16 2
  2203. %18 = OpTypeArray %15 %17
  2204. %23 = OpTypeInt 32 1
  2205. %24 = OpTypeVector %23 4
  2206. %25 = OpTypeVector %16 4
  2207. %26 = OpTypeStruct %1 %1 %1 %1
  2208. %27 = OpTypeStruct %2 %16 %16 %23 %23 %24 %25 %26 ; Block
  2209. %35 = OpTypeVector %1 2
  2210. %40 = OpTypeVector %23 2
  2211. %61 = OpTypeVoid
  2212. %69 = OpConstant %16 0
  2213. %78 = OpConstant %16 1
  2214. %3 = OpTypePointer Input %2
  2215. %7 = OpTypePointer UniformConstant %6
  2216. %10 = OpTypePointer UniformConstant %9
  2217. %13 = OpTypePointer Uniform %12
  2218. %19 = OpTypePointer Uniform %18
  2219. %21 = OpTypePointer Output %2
  2220. %28 = OpTypePointer Uniform %27
  2221. %30 = OpTypePointer Function %2
  2222. %70 = OpTypePointer Uniform %2
  2223. %31 = OpTypeFunction %2 %30
  2224. %47 = OpTypeFunction %2 %30 %30
  2225. %62 = OpTypeFunction %61
  2226. %4 = OpVariable %3 Input ; Location 0
  2227. %8 = OpVariable %7 UniformConstant ; RelaxedPrecision, DescriptorSet 0, Binding 0
  2228. %11 = OpVariable %10 UniformConstant ; DescriptorSet 0, Binding 1
  2229. %14 = OpVariable %13 Uniform ; DescriptorSet 0, Binding 2
  2230. %20 = OpVariable %19 Uniform ; DescriptorSet 0, Binding 3
  2231. %22 = OpVariable %21 Output ; RelaxedPrecision, Location 0
  2232. %29 = OpVariable %28 Uniform ; DescriptorSet 0, Binding 4
  2233. ; Function 32
  2234. %32 = OpFunction %2 None %31 ; RelaxedPrecision
  2235. %33 = OpFunctionParameter %30 ; RelaxedPrecision
  2236. %34 = OpLabel
  2237. %36 = OpLoad %6 %8 ; RelaxedPrecision
  2238. %37 = OpLoad %2 %33 ; RelaxedPrecision
  2239. %38 = OpVectorShuffle %35 %37 %37 0 1 ; RelaxedPrecision
  2240. %39 = OpImageSampleImplicitLod %2 %36 %38 ; RelaxedPrecision
  2241. %41 = OpLoad %2 %33 ; RelaxedPrecision
  2242. %42 = OpVectorShuffle %35 %41 %41 2 3 ; RelaxedPrecision
  2243. %43 = OpConvertFToS %40 %42 ; RelaxedPrecision
  2244. %44 = OpLoad %9 %11
  2245. %45 = OpImageRead %2 %44 %43
  2246. %46 = OpFAdd %2 %39 %45
  2247. OpReturnValue %46
  2248. OpFunctionEnd
  2249. ; Function 48
  2250. %48 = OpFunction %2 None %47 ; RelaxedPrecision
  2251. %49 = OpFunctionParameter %30 ; RelaxedPrecision
  2252. %50 = OpFunctionParameter %30 ; RelaxedPrecision
  2253. %51 = OpLabel
  2254. %52 = OpLoad %2 %49 ; RelaxedPrecision
  2255. %53 = OpVectorShuffle %35 %52 %52 0 1 ; RelaxedPrecision
  2256. %54 = OpLoad %2 %50 ; RelaxedPrecision
  2257. %55 = OpVectorShuffle %35 %54 %54 2 3 ; RelaxedPrecision
  2258. %56 = OpCompositeExtract %1 %53 0 ; RelaxedPrecision
  2259. %57 = OpCompositeExtract %1 %53 1 ; RelaxedPrecision
  2260. %58 = OpCompositeExtract %1 %55 0 ; RelaxedPrecision
  2261. %59 = OpCompositeExtract %1 %55 1 ; RelaxedPrecision
  2262. %60 = OpCompositeConstruct %2 %56 %57 %58 %59 ; RelaxedPrecision
  2263. OpReturnValue %60
  2264. OpFunctionEnd
  2265. ; Function 63
  2266. %63 = OpFunction %61 None %62
  2267. %64 = OpLabel
  2268. %65 = OpVariable %30 Function
  2269. %68 = OpVariable %30 Function ; RelaxedPrecision
  2270. %73 = OpVariable %30 Function ; RelaxedPrecision
  2271. %66 = OpLoad %2 %4
  2272. OpStore %65 %66
  2273. %67 = OpFunctionCall %2 %32 %65 ; RelaxedPrecision
  2274. %71 = OpAccessChain %70 %14 %69
  2275. %72 = OpLoad %2 %71 ; RelaxedPrecision
  2276. OpStore %68 %72
  2277. %74 = OpAccessChain %70 %20 %69 %69
  2278. %75 = OpLoad %2 %74 ; RelaxedPrecision
  2279. OpStore %73 %75
  2280. %76 = OpFunctionCall %2 %48 %68 %73 ; RelaxedPrecision
  2281. %77 = OpFAdd %2 %67 %76 ; RelaxedPrecision
  2282. %79 = OpAccessChain %70 %20 %78 %69
  2283. %80 = OpLoad %2 %79 ; RelaxedPrecision
  2284. %81 = OpFAdd %2 %77 %80 ; RelaxedPrecision
  2285. OpStore %22 %81
  2286. OpReturn
  2287. OpFunctionEnd
  2288. )";
  2289. EXPECT_THAT(
  2290. EncodeAndDecodeSuccessfully(
  2291. input,
  2292. SPV_BINARY_TO_TEXT_OPTION_COMMENT | SPV_BINARY_TO_TEXT_OPTION_INDENT,
  2293. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
  2294. expected);
  2295. }
  2296. TEST_F(TextToBinaryTest, NestedWithComments) {
  2297. const std::string input = R"(OpCapability Shader
  2298. %1 = OpExtInstImport "GLSL.std.450"
  2299. OpMemoryModel Logical GLSL450
  2300. OpEntryPoint Fragment %4 "main" %8 %44
  2301. OpExecutionMode %4 OriginUpperLeft
  2302. OpSource ESSL 310
  2303. OpName %4 "main"
  2304. OpName %8 "v"
  2305. OpName %44 "color"
  2306. OpDecorate %8 RelaxedPrecision
  2307. OpDecorate %8 Location 0
  2308. OpDecorate %9 RelaxedPrecision
  2309. OpDecorate %18 RelaxedPrecision
  2310. OpDecorate %19 RelaxedPrecision
  2311. OpDecorate %20 RelaxedPrecision
  2312. OpDecorate %23 RelaxedPrecision
  2313. OpDecorate %24 RelaxedPrecision
  2314. OpDecorate %25 RelaxedPrecision
  2315. OpDecorate %26 RelaxedPrecision
  2316. OpDecorate %27 RelaxedPrecision
  2317. OpDecorate %28 RelaxedPrecision
  2318. OpDecorate %29 RelaxedPrecision
  2319. OpDecorate %30 RelaxedPrecision
  2320. OpDecorate %31 RelaxedPrecision
  2321. OpDecorate %33 RelaxedPrecision
  2322. OpDecorate %34 RelaxedPrecision
  2323. OpDecorate %35 RelaxedPrecision
  2324. OpDecorate %36 RelaxedPrecision
  2325. OpDecorate %37 RelaxedPrecision
  2326. OpDecorate %39 RelaxedPrecision
  2327. OpDecorate %40 RelaxedPrecision
  2328. OpDecorate %41 RelaxedPrecision
  2329. OpDecorate %42 RelaxedPrecision
  2330. OpDecorate %44 RelaxedPrecision
  2331. OpDecorate %44 Location 0
  2332. OpDecorate %45 RelaxedPrecision
  2333. %2 = OpTypeVoid
  2334. %3 = OpTypeFunction %2
  2335. %6 = OpTypeFloat 32
  2336. %7 = OpTypePointer Input %6
  2337. %8 = OpVariable %7 Input
  2338. %10 = OpConstant %6 0
  2339. %11 = OpTypeBool
  2340. %15 = OpTypeVector %6 4
  2341. %16 = OpTypePointer Function %15
  2342. %21 = OpConstant %6 -0.5
  2343. %22 = OpConstant %6 -0.300000012
  2344. %38 = OpConstant %6 0.5
  2345. %43 = OpTypePointer Output %15
  2346. %44 = OpVariable %43 Output
  2347. %4 = OpFunction %2 None %3
  2348. %5 = OpLabel
  2349. %9 = OpLoad %6 %8
  2350. %12 = OpFOrdLessThanEqual %11 %9 %10
  2351. OpSelectionMerge %14 None
  2352. OpBranchConditional %12 %13 %32
  2353. %13 = OpLabel
  2354. %18 = OpLoad %6 %8
  2355. %19 = OpExtInst %6 %1 Log %18
  2356. %20 = OpLoad %6 %8
  2357. %23 = OpExtInst %6 %1 FClamp %20 %21 %22
  2358. %24 = OpFMul %6 %19 %23
  2359. %25 = OpLoad %6 %8
  2360. %26 = OpExtInst %6 %1 Sin %25
  2361. %27 = OpLoad %6 %8
  2362. %28 = OpExtInst %6 %1 Cos %27
  2363. %29 = OpLoad %6 %8
  2364. %30 = OpExtInst %6 %1 Exp %29
  2365. %31 = OpCompositeConstruct %15 %24 %26 %28 %30
  2366. OpBranch %14
  2367. %32 = OpLabel
  2368. %33 = OpLoad %6 %8
  2369. %34 = OpExtInst %6 %1 Sqrt %33
  2370. %35 = OpLoad %6 %8
  2371. %36 = OpExtInst %6 %1 FSign %35
  2372. %37 = OpLoad %6 %8
  2373. %39 = OpExtInst %6 %1 FMax %37 %38
  2374. %40 = OpLoad %6 %8
  2375. %41 = OpExtInst %6 %1 Floor %40
  2376. %42 = OpCompositeConstruct %15 %34 %36 %39 %41
  2377. OpBranch %14
  2378. %14 = OpLabel
  2379. %45 = OpPhi %15 %31 %13 %42 %32
  2380. OpStore %44 %45
  2381. OpReturn
  2382. OpFunctionEnd
  2383. )";
  2384. const std::string expected = R"( OpCapability Shader
  2385. %1 = OpExtInstImport "GLSL.std.450"
  2386. OpMemoryModel Logical GLSL450
  2387. OpEntryPoint Fragment %4 "main" %8 %44
  2388. OpExecutionMode %4 OriginUpperLeft
  2389. ; Debug Information
  2390. OpSource ESSL 310
  2391. OpName %4 "main" ; id %4
  2392. OpName %8 "v" ; id %8
  2393. OpName %44 "color" ; id %44
  2394. ; Annotations
  2395. OpDecorate %8 RelaxedPrecision
  2396. OpDecorate %8 Location 0
  2397. OpDecorate %9 RelaxedPrecision
  2398. OpDecorate %18 RelaxedPrecision
  2399. OpDecorate %19 RelaxedPrecision
  2400. OpDecorate %20 RelaxedPrecision
  2401. OpDecorate %23 RelaxedPrecision
  2402. OpDecorate %24 RelaxedPrecision
  2403. OpDecorate %25 RelaxedPrecision
  2404. OpDecorate %26 RelaxedPrecision
  2405. OpDecorate %27 RelaxedPrecision
  2406. OpDecorate %28 RelaxedPrecision
  2407. OpDecorate %29 RelaxedPrecision
  2408. OpDecorate %30 RelaxedPrecision
  2409. OpDecorate %31 RelaxedPrecision
  2410. OpDecorate %33 RelaxedPrecision
  2411. OpDecorate %34 RelaxedPrecision
  2412. OpDecorate %35 RelaxedPrecision
  2413. OpDecorate %36 RelaxedPrecision
  2414. OpDecorate %37 RelaxedPrecision
  2415. OpDecorate %39 RelaxedPrecision
  2416. OpDecorate %40 RelaxedPrecision
  2417. OpDecorate %41 RelaxedPrecision
  2418. OpDecorate %42 RelaxedPrecision
  2419. OpDecorate %44 RelaxedPrecision
  2420. OpDecorate %44 Location 0
  2421. OpDecorate %45 RelaxedPrecision
  2422. ; Types, variables and constants
  2423. %2 = OpTypeVoid
  2424. %3 = OpTypeFunction %2
  2425. %6 = OpTypeFloat 32
  2426. %7 = OpTypePointer Input %6
  2427. %8 = OpVariable %7 Input ; RelaxedPrecision, Location 0
  2428. %10 = OpConstant %6 0
  2429. %11 = OpTypeBool
  2430. %15 = OpTypeVector %6 4
  2431. %16 = OpTypePointer Function %15
  2432. %21 = OpConstant %6 -0.5
  2433. %22 = OpConstant %6 -0.300000012
  2434. %38 = OpConstant %6 0.5
  2435. %43 = OpTypePointer Output %15
  2436. %44 = OpVariable %43 Output ; RelaxedPrecision, Location 0
  2437. ; Function 4
  2438. %4 = OpFunction %2 None %3
  2439. %5 = OpLabel
  2440. %9 = OpLoad %6 %8 ; RelaxedPrecision
  2441. %12 = OpFOrdLessThanEqual %11 %9 %10
  2442. OpSelectionMerge %14 None
  2443. OpBranchConditional %12 %13 %32
  2444. %13 = OpLabel
  2445. %18 = OpLoad %6 %8 ; RelaxedPrecision
  2446. %19 = OpExtInst %6 %1 Log %18 ; RelaxedPrecision
  2447. %20 = OpLoad %6 %8 ; RelaxedPrecision
  2448. %23 = OpExtInst %6 %1 FClamp %20 %21 %22 ; RelaxedPrecision
  2449. %24 = OpFMul %6 %19 %23 ; RelaxedPrecision
  2450. %25 = OpLoad %6 %8 ; RelaxedPrecision
  2451. %26 = OpExtInst %6 %1 Sin %25 ; RelaxedPrecision
  2452. %27 = OpLoad %6 %8 ; RelaxedPrecision
  2453. %28 = OpExtInst %6 %1 Cos %27 ; RelaxedPrecision
  2454. %29 = OpLoad %6 %8 ; RelaxedPrecision
  2455. %30 = OpExtInst %6 %1 Exp %29 ; RelaxedPrecision
  2456. %31 = OpCompositeConstruct %15 %24 %26 %28 %30 ; RelaxedPrecision
  2457. OpBranch %14
  2458. %32 = OpLabel
  2459. %33 = OpLoad %6 %8 ; RelaxedPrecision
  2460. %34 = OpExtInst %6 %1 Sqrt %33 ; RelaxedPrecision
  2461. %35 = OpLoad %6 %8 ; RelaxedPrecision
  2462. %36 = OpExtInst %6 %1 FSign %35 ; RelaxedPrecision
  2463. %37 = OpLoad %6 %8 ; RelaxedPrecision
  2464. %39 = OpExtInst %6 %1 FMax %37 %38 ; RelaxedPrecision
  2465. %40 = OpLoad %6 %8 ; RelaxedPrecision
  2466. %41 = OpExtInst %6 %1 Floor %40 ; RelaxedPrecision
  2467. %42 = OpCompositeConstruct %15 %34 %36 %39 %41 ; RelaxedPrecision
  2468. OpBranch %14
  2469. %14 = OpLabel
  2470. %45 = OpPhi %15 %31 %13 %42 %32 ; RelaxedPrecision
  2471. OpStore %44 %45
  2472. OpReturn
  2473. OpFunctionEnd
  2474. )";
  2475. EXPECT_THAT(
  2476. EncodeAndDecodeSuccessfully(
  2477. input,
  2478. SPV_BINARY_TO_TEXT_OPTION_COMMENT | SPV_BINARY_TO_TEXT_OPTION_INDENT |
  2479. SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
  2480. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
  2481. expected);
  2482. }
  2483. // Test version string.
  2484. TEST_F(TextToBinaryTest, VersionString) {
  2485. auto words = CompileSuccessfully("");
  2486. spv_text decoded_text = nullptr;
  2487. EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
  2488. words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
  2489. &decoded_text, &diagnostic),
  2490. Eq(SPV_SUCCESS));
  2491. EXPECT_EQ(nullptr, diagnostic);
  2492. EXPECT_THAT(decoded_text->str, HasSubstr("Version: 1.0\n"))
  2493. << EncodeAndDecodeSuccessfully("");
  2494. spvTextDestroy(decoded_text);
  2495. }
  2496. // Test generator string.
  2497. // A test case for the generator string. This allows us to
  2498. // test both of the 16-bit components of the generator word.
  2499. struct GeneratorStringCase {
  2500. uint16_t generator;
  2501. uint16_t misc;
  2502. std::string expected;
  2503. };
  2504. using GeneratorStringTest = spvtest::TextToBinaryTestBase<
  2505. ::testing::TestWithParam<GeneratorStringCase>>;
  2506. TEST_P(GeneratorStringTest, Sample) {
  2507. auto words = CompileSuccessfully("");
  2508. EXPECT_EQ(2u, SPV_INDEX_GENERATOR_NUMBER);
  2509. words[SPV_INDEX_GENERATOR_NUMBER] =
  2510. SPV_GENERATOR_WORD(GetParam().generator, GetParam().misc);
  2511. spv_text decoded_text = nullptr;
  2512. EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
  2513. words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
  2514. &decoded_text, &diagnostic),
  2515. Eq(SPV_SUCCESS));
  2516. EXPECT_THAT(diagnostic, Eq(nullptr));
  2517. EXPECT_THAT(std::string(decoded_text->str), HasSubstr(GetParam().expected));
  2518. spvTextDestroy(decoded_text);
  2519. }
  2520. INSTANTIATE_TEST_SUITE_P(GeneratorStrings, GeneratorStringTest,
  2521. ::testing::ValuesIn(std::vector<GeneratorStringCase>{
  2522. {SPV_GENERATOR_KHRONOS, 12, "Khronos; 12"},
  2523. {SPV_GENERATOR_LUNARG, 99, "LunarG; 99"},
  2524. {SPV_GENERATOR_VALVE, 1, "Valve; 1"},
  2525. {SPV_GENERATOR_CODEPLAY, 65535, "Codeplay; 65535"},
  2526. {SPV_GENERATOR_NVIDIA, 19, "NVIDIA; 19"},
  2527. {SPV_GENERATOR_ARM, 1000, "ARM; 1000"},
  2528. {SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR, 38,
  2529. "Khronos LLVM/SPIR-V Translator; 38"},
  2530. {SPV_GENERATOR_KHRONOS_ASSEMBLER, 2,
  2531. "Khronos SPIR-V Tools Assembler; 2"},
  2532. {SPV_GENERATOR_KHRONOS_GLSLANG, 1,
  2533. "Khronos Glslang Reference Front End; 1"},
  2534. {1000, 18, "Unknown(1000); 18"},
  2535. {65535, 32767, "Unknown(65535); 32767"},
  2536. }));
  2537. // TODO(dneto): Test new instructions and enums in SPIR-V 1.3
  2538. } // namespace
  2539. } // namespace spvtools