val_conversion_test.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. // Copyright (c) 2017 Google 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. // Tests for unique type declaration rules validator.
  15. #include <string>
  16. #include "gmock/gmock.h"
  17. #include "test/unit_spirv.h"
  18. #include "test/val/val_code_generator.h"
  19. #include "test/val/val_fixtures.h"
  20. namespace spvtools {
  21. namespace val {
  22. namespace {
  23. using ::testing::HasSubstr;
  24. using ::testing::Not;
  25. using ::testing::Values;
  26. using ValidateConversion = spvtest::ValidateBase<bool>;
  27. std::string GenerateShaderCode(
  28. const std::string& body,
  29. const std::string& capabilities_and_extensions = "",
  30. const std::string& decorations = "", const std::string& types = "",
  31. const std::string& variables = "") {
  32. const std::string capabilities =
  33. R"(
  34. OpCapability Shader
  35. OpCapability Int64
  36. OpCapability Float64)";
  37. const std::string after_extension_before_decorations =
  38. R"(
  39. OpMemoryModel Logical GLSL450
  40. OpEntryPoint Fragment %main "main"
  41. OpExecutionMode %main OriginUpperLeft)";
  42. const std::string after_decorations_before_types =
  43. R"(
  44. %void = OpTypeVoid
  45. %func = OpTypeFunction %void
  46. %bool = OpTypeBool
  47. %f32 = OpTypeFloat 32
  48. %u32 = OpTypeInt 32 0
  49. %s32 = OpTypeInt 32 1
  50. %f64 = OpTypeFloat 64
  51. %u64 = OpTypeInt 64 0
  52. %s64 = OpTypeInt 64 1
  53. %boolvec2 = OpTypeVector %bool 2
  54. %s32vec2 = OpTypeVector %s32 2
  55. %u32vec2 = OpTypeVector %u32 2
  56. %u64vec2 = OpTypeVector %u64 2
  57. %f32vec2 = OpTypeVector %f32 2
  58. %f64vec2 = OpTypeVector %f64 2
  59. %boolvec3 = OpTypeVector %bool 3
  60. %u32vec3 = OpTypeVector %u32 3
  61. %u64vec3 = OpTypeVector %u64 3
  62. %s32vec3 = OpTypeVector %s32 3
  63. %f32vec3 = OpTypeVector %f32 3
  64. %f64vec3 = OpTypeVector %f64 3
  65. %boolvec4 = OpTypeVector %bool 4
  66. %u32vec4 = OpTypeVector %u32 4
  67. %u64vec4 = OpTypeVector %u64 4
  68. %s32vec4 = OpTypeVector %s32 4
  69. %f32vec4 = OpTypeVector %f32 4
  70. %f64vec4 = OpTypeVector %f64 4
  71. %f32_0 = OpConstant %f32 0
  72. %f32_1 = OpConstant %f32 1
  73. %f32_2 = OpConstant %f32 2
  74. %f32_3 = OpConstant %f32 3
  75. %f32_4 = OpConstant %f32 4
  76. %s32_0 = OpConstant %s32 0
  77. %s32_1 = OpConstant %s32 1
  78. %s32_2 = OpConstant %s32 2
  79. %s32_3 = OpConstant %s32 3
  80. %s32_4 = OpConstant %s32 4
  81. %s32_m1 = OpConstant %s32 -1
  82. %u32_0 = OpConstant %u32 0
  83. %u32_1 = OpConstant %u32 1
  84. %u32_2 = OpConstant %u32 2
  85. %u32_3 = OpConstant %u32 3
  86. %u32_4 = OpConstant %u32 4
  87. %f64_0 = OpConstant %f64 0
  88. %f64_1 = OpConstant %f64 1
  89. %f64_2 = OpConstant %f64 2
  90. %f64_3 = OpConstant %f64 3
  91. %f64_4 = OpConstant %f64 4
  92. %s64_0 = OpConstant %s64 0
  93. %s64_1 = OpConstant %s64 1
  94. %s64_2 = OpConstant %s64 2
  95. %s64_3 = OpConstant %s64 3
  96. %s64_4 = OpConstant %s64 4
  97. %s64_m1 = OpConstant %s64 -1
  98. %u64_0 = OpConstant %u64 0
  99. %u64_1 = OpConstant %u64 1
  100. %u64_2 = OpConstant %u64 2
  101. %u64_3 = OpConstant %u64 3
  102. %u64_4 = OpConstant %u64 4
  103. %u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
  104. %u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
  105. %u32vec3_012 = OpConstantComposite %u32vec3 %u32_0 %u32_1 %u32_2
  106. %u32vec3_123 = OpConstantComposite %u32vec3 %u32_1 %u32_2 %u32_3
  107. %u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
  108. %u32vec4_1234 = OpConstantComposite %u32vec4 %u32_1 %u32_2 %u32_3 %u32_4
  109. %s32vec2_01 = OpConstantComposite %s32vec2 %s32_0 %s32_1
  110. %s32vec2_12 = OpConstantComposite %s32vec2 %s32_1 %s32_2
  111. %s32vec3_012 = OpConstantComposite %s32vec3 %s32_0 %s32_1 %s32_2
  112. %s32vec3_123 = OpConstantComposite %s32vec3 %s32_1 %s32_2 %s32_3
  113. %s32vec4_0123 = OpConstantComposite %s32vec4 %s32_0 %s32_1 %s32_2 %s32_3
  114. %s32vec4_1234 = OpConstantComposite %s32vec4 %s32_1 %s32_2 %s32_3 %s32_4
  115. %f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
  116. %f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
  117. %f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
  118. %f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
  119. %f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
  120. %f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
  121. %f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
  122. %f64vec2_12 = OpConstantComposite %f64vec2 %f64_1 %f64_2
  123. %f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
  124. %f64vec3_123 = OpConstantComposite %f64vec3 %f64_1 %f64_2 %f64_3
  125. %f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
  126. %f64vec4_1234 = OpConstantComposite %f64vec4 %f64_1 %f64_2 %f64_3 %f64_4
  127. %true = OpConstantTrue %bool
  128. %false = OpConstantFalse %bool
  129. %f32ptr_func = OpTypePointer Function %f32)";
  130. const std::string after_variables_before_body =
  131. R"(
  132. %main = OpFunction %void None %func
  133. %main_entry = OpLabel)";
  134. const std::string after_body =
  135. R"(
  136. OpReturn
  137. OpFunctionEnd)";
  138. return capabilities + capabilities_and_extensions +
  139. after_extension_before_decorations + decorations +
  140. after_decorations_before_types + types + variables +
  141. after_variables_before_body + body + after_body;
  142. }
  143. std::string GenerateKernelCode(
  144. const std::string& body,
  145. const std::string& capabilities_and_extensions = "") {
  146. const std::string capabilities =
  147. R"(
  148. OpCapability Addresses
  149. OpCapability Kernel
  150. OpCapability Linkage
  151. OpCapability GenericPointer
  152. OpCapability Int64
  153. OpCapability Float64)";
  154. const std::string after_extension_before_body =
  155. R"(
  156. OpMemoryModel Physical32 OpenCL
  157. %void = OpTypeVoid
  158. %func = OpTypeFunction %void
  159. %bool = OpTypeBool
  160. %f32 = OpTypeFloat 32
  161. %u32 = OpTypeInt 32 0
  162. %f64 = OpTypeFloat 64
  163. %u64 = OpTypeInt 64 0
  164. %boolvec2 = OpTypeVector %bool 2
  165. %u32vec2 = OpTypeVector %u32 2
  166. %u64vec2 = OpTypeVector %u64 2
  167. %f32vec2 = OpTypeVector %f32 2
  168. %f64vec2 = OpTypeVector %f64 2
  169. %boolvec3 = OpTypeVector %bool 3
  170. %u32vec3 = OpTypeVector %u32 3
  171. %u64vec3 = OpTypeVector %u64 3
  172. %f32vec3 = OpTypeVector %f32 3
  173. %f64vec3 = OpTypeVector %f64 3
  174. %boolvec4 = OpTypeVector %bool 4
  175. %u32vec4 = OpTypeVector %u32 4
  176. %u64vec4 = OpTypeVector %u64 4
  177. %f32vec4 = OpTypeVector %f32 4
  178. %f64vec4 = OpTypeVector %f64 4
  179. %f32_0 = OpConstant %f32 0
  180. %f32_1 = OpConstant %f32 1
  181. %f32_2 = OpConstant %f32 2
  182. %f32_3 = OpConstant %f32 3
  183. %f32_4 = OpConstant %f32 4
  184. %u32_0 = OpConstant %u32 0
  185. %u32_1 = OpConstant %u32 1
  186. %u32_2 = OpConstant %u32 2
  187. %u32_3 = OpConstant %u32 3
  188. %u32_4 = OpConstant %u32 4
  189. %f64_0 = OpConstant %f64 0
  190. %f64_1 = OpConstant %f64 1
  191. %f64_2 = OpConstant %f64 2
  192. %f64_3 = OpConstant %f64 3
  193. %f64_4 = OpConstant %f64 4
  194. %u64_0 = OpConstant %u64 0
  195. %u64_1 = OpConstant %u64 1
  196. %u64_2 = OpConstant %u64 2
  197. %u64_3 = OpConstant %u64 3
  198. %u64_4 = OpConstant %u64 4
  199. %u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
  200. %u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
  201. %u32vec3_012 = OpConstantComposite %u32vec3 %u32_0 %u32_1 %u32_2
  202. %u32vec3_123 = OpConstantComposite %u32vec3 %u32_1 %u32_2 %u32_3
  203. %u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
  204. %u32vec4_1234 = OpConstantComposite %u32vec4 %u32_1 %u32_2 %u32_3 %u32_4
  205. %f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
  206. %f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
  207. %f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
  208. %f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
  209. %f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
  210. %f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
  211. %f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
  212. %f64vec2_12 = OpConstantComposite %f64vec2 %f64_1 %f64_2
  213. %f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
  214. %f64vec3_123 = OpConstantComposite %f64vec3 %f64_1 %f64_2 %f64_3
  215. %f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
  216. %f64vec4_1234 = OpConstantComposite %f64vec4 %f64_1 %f64_2 %f64_3 %f64_4
  217. %u64vec2_01 = OpConstantComposite %u64vec2 %u64_0 %u64_1
  218. %true = OpConstantTrue %bool
  219. %false = OpConstantFalse %bool
  220. %f32ptr_func = OpTypePointer Function %f32
  221. %u32ptr_func = OpTypePointer Function %u32
  222. %f32ptr_gen = OpTypePointer Generic %f32
  223. %f32ptr_inp = OpTypePointer Input %f32
  224. %f32ptr_wg = OpTypePointer Workgroup %f32
  225. %f32ptr_cwg = OpTypePointer CrossWorkgroup %f32
  226. %f32inp = OpVariable %f32ptr_inp Input
  227. %main = OpFunction %void None %func
  228. %main_entry = OpLabel)";
  229. const std::string after_body =
  230. R"(
  231. OpReturn
  232. OpFunctionEnd)";
  233. return capabilities + capabilities_and_extensions +
  234. after_extension_before_body + body + after_body;
  235. }
  236. TEST_F(ValidateConversion, ConvertFToUSuccess) {
  237. const std::string body = R"(
  238. %val1 = OpConvertFToU %u32 %f32_1
  239. %val2 = OpConvertFToU %u32 %f64_0
  240. %val3 = OpConvertFToU %u32vec2 %f32vec2_01
  241. )";
  242. CompileSuccessfully(GenerateShaderCode(body).c_str());
  243. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  244. }
  245. TEST_F(ValidateConversion, ConvertFToUWrongResultType) {
  246. const std::string body = R"(
  247. %val = OpConvertFToU %s32 %f32_1
  248. )";
  249. CompileSuccessfully(GenerateShaderCode(body).c_str());
  250. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  251. EXPECT_THAT(getDiagnosticString(),
  252. HasSubstr("Expected unsigned int scalar or vector type as Result "
  253. "Type: ConvertFToU"));
  254. }
  255. TEST_F(ValidateConversion, ConvertFToUWrongInputType) {
  256. const std::string body = R"(
  257. %val = OpConvertFToU %u32 %u32_1
  258. )";
  259. CompileSuccessfully(GenerateShaderCode(body).c_str());
  260. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  261. EXPECT_THAT(
  262. getDiagnosticString(),
  263. HasSubstr("Expected input to be float scalar or vector: ConvertFToU"));
  264. }
  265. TEST_F(ValidateConversion, ConvertFToUDifferentDimension) {
  266. const std::string body = R"(
  267. %val = OpConvertFToU %u32 %f32vec2_01
  268. )";
  269. CompileSuccessfully(GenerateShaderCode(body).c_str());
  270. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  271. EXPECT_THAT(getDiagnosticString(),
  272. HasSubstr("Expected input to have the same dimension as Result "
  273. "Type: ConvertFToU"));
  274. }
  275. TEST_F(ValidateConversion, ConvertFToSSuccess) {
  276. const std::string body = R"(
  277. %val1 = OpConvertFToS %s32 %f32_1
  278. %val2 = OpConvertFToS %u32 %f64_0
  279. %val3 = OpConvertFToS %s32vec2 %f32vec2_01
  280. )";
  281. CompileSuccessfully(GenerateShaderCode(body).c_str());
  282. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  283. }
  284. TEST_F(ValidateConversion, ConvertFToSWrongResultType) {
  285. const std::string body = R"(
  286. %val = OpConvertFToS %bool %f32_1
  287. )";
  288. CompileSuccessfully(GenerateShaderCode(body).c_str());
  289. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  290. EXPECT_THAT(
  291. getDiagnosticString(),
  292. HasSubstr(
  293. "Expected int scalar or vector type as Result Type: ConvertFToS"));
  294. }
  295. TEST_F(ValidateConversion, ConvertFToSWrongInputType) {
  296. const std::string body = R"(
  297. %val = OpConvertFToS %s32 %u32_1
  298. )";
  299. CompileSuccessfully(GenerateShaderCode(body).c_str());
  300. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  301. EXPECT_THAT(
  302. getDiagnosticString(),
  303. HasSubstr("Expected input to be float scalar or vector: ConvertFToS"));
  304. }
  305. TEST_F(ValidateConversion, ConvertFToSDifferentDimension) {
  306. const std::string body = R"(
  307. %val = OpConvertFToS %u32 %f32vec2_01
  308. )";
  309. CompileSuccessfully(GenerateShaderCode(body).c_str());
  310. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  311. EXPECT_THAT(getDiagnosticString(),
  312. HasSubstr("Expected input to have the same dimension as Result "
  313. "Type: ConvertFToS"));
  314. }
  315. TEST_F(ValidateConversion, ConvertSToFSuccess) {
  316. const std::string body = R"(
  317. %val1 = OpConvertSToF %f32 %u32_1
  318. %val2 = OpConvertSToF %f32 %s64_0
  319. %val3 = OpConvertSToF %f32vec2 %s32vec2_01
  320. )";
  321. CompileSuccessfully(GenerateShaderCode(body).c_str());
  322. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  323. }
  324. TEST_F(ValidateConversion, ConvertSToFWrongResultType) {
  325. const std::string body = R"(
  326. %val = OpConvertSToF %u32 %s32_1
  327. )";
  328. CompileSuccessfully(GenerateShaderCode(body).c_str());
  329. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  330. EXPECT_THAT(
  331. getDiagnosticString(),
  332. HasSubstr(
  333. "Expected float scalar or vector type as Result Type: ConvertSToF"));
  334. }
  335. TEST_F(ValidateConversion, ConvertSToFWrongInputType) {
  336. const std::string body = R"(
  337. %val = OpConvertSToF %f32 %f32_1
  338. )";
  339. CompileSuccessfully(GenerateShaderCode(body).c_str());
  340. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  341. EXPECT_THAT(
  342. getDiagnosticString(),
  343. HasSubstr("Expected input to be int scalar or vector: ConvertSToF"));
  344. }
  345. TEST_F(ValidateConversion, ConvertSToFDifferentDimension) {
  346. const std::string body = R"(
  347. %val = OpConvertSToF %f32 %u32vec2_01
  348. )";
  349. CompileSuccessfully(GenerateShaderCode(body).c_str());
  350. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  351. EXPECT_THAT(getDiagnosticString(),
  352. HasSubstr("Expected input to have the same dimension as Result "
  353. "Type: ConvertSToF"));
  354. }
  355. TEST_F(ValidateConversion, UConvertSuccess) {
  356. const std::string body = R"(
  357. %val1 = OpUConvert %u32 %u64_1
  358. %val2 = OpUConvert %u64 %s32_0
  359. %val3 = OpUConvert %u64vec2 %s32vec2_01
  360. )";
  361. CompileSuccessfully(GenerateShaderCode(body).c_str());
  362. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  363. }
  364. TEST_F(ValidateConversion, UConvertWrongResultType) {
  365. const std::string body = R"(
  366. %val = OpUConvert %s32 %s32_1
  367. )";
  368. CompileSuccessfully(GenerateShaderCode(body).c_str());
  369. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  370. EXPECT_THAT(getDiagnosticString(),
  371. HasSubstr("Expected unsigned int scalar or vector type as Result "
  372. "Type: UConvert"));
  373. }
  374. TEST_F(ValidateConversion, UConvertWrongInputType) {
  375. const std::string body = R"(
  376. %val = OpUConvert %u32 %f32_1
  377. )";
  378. CompileSuccessfully(GenerateShaderCode(body).c_str());
  379. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  380. EXPECT_THAT(getDiagnosticString(),
  381. HasSubstr("Expected input to be int scalar or vector: UConvert"));
  382. }
  383. TEST_F(ValidateConversion, UConvertDifferentDimension) {
  384. const std::string body = R"(
  385. %val = OpUConvert %u32 %u32vec2_01
  386. )";
  387. CompileSuccessfully(GenerateShaderCode(body).c_str());
  388. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  389. EXPECT_THAT(getDiagnosticString(),
  390. HasSubstr("Expected input to have the same dimension as Result "
  391. "Type: UConvert"));
  392. }
  393. TEST_F(ValidateConversion, UConvertSameBitWidth) {
  394. const std::string body = R"(
  395. %val = OpUConvert %u32 %s32_1
  396. )";
  397. CompileSuccessfully(GenerateShaderCode(body).c_str());
  398. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  399. EXPECT_THAT(getDiagnosticString(),
  400. HasSubstr("Expected input to have different bit width from "
  401. "Result Type: UConvert"));
  402. }
  403. TEST_F(ValidateConversion, SConvertSuccess) {
  404. const std::string body = R"(
  405. %val1 = OpSConvert %s32 %u64_1
  406. %val2 = OpSConvert %s64 %s32_0
  407. %val3 = OpSConvert %u64vec2 %s32vec2_01
  408. )";
  409. CompileSuccessfully(GenerateShaderCode(body).c_str());
  410. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  411. }
  412. TEST_F(ValidateConversion, SConvertWrongResultType) {
  413. const std::string body = R"(
  414. %val = OpSConvert %f32 %s32_1
  415. )";
  416. CompileSuccessfully(GenerateShaderCode(body).c_str());
  417. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  418. EXPECT_THAT(
  419. getDiagnosticString(),
  420. HasSubstr("Expected int scalar or vector type as Result Type: SConvert"));
  421. }
  422. TEST_F(ValidateConversion, SConvertWrongInputType) {
  423. const std::string body = R"(
  424. %val = OpSConvert %u32 %f32_1
  425. )";
  426. CompileSuccessfully(GenerateShaderCode(body).c_str());
  427. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  428. EXPECT_THAT(getDiagnosticString(),
  429. HasSubstr("Expected input to be int scalar or vector: SConvert"));
  430. }
  431. TEST_F(ValidateConversion, SConvertDifferentDimension) {
  432. const std::string body = R"(
  433. %val = OpSConvert %s32 %u32vec2_01
  434. )";
  435. CompileSuccessfully(GenerateShaderCode(body).c_str());
  436. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  437. EXPECT_THAT(getDiagnosticString(),
  438. HasSubstr("Expected input to have the same dimension as Result "
  439. "Type: SConvert"));
  440. }
  441. TEST_F(ValidateConversion, SConvertSameBitWidth) {
  442. const std::string body = R"(
  443. %val = OpSConvert %u32 %s32_1
  444. )";
  445. CompileSuccessfully(GenerateShaderCode(body).c_str());
  446. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  447. EXPECT_THAT(getDiagnosticString(),
  448. HasSubstr("Expected input to have different bit width from "
  449. "Result Type: SConvert"));
  450. }
  451. TEST_F(ValidateConversion, FConvertSuccess) {
  452. const std::string body = R"(
  453. %val1 = OpFConvert %f32 %f64_1
  454. %val2 = OpFConvert %f64 %f32_0
  455. %val3 = OpFConvert %f64vec2 %f32vec2_01
  456. )";
  457. CompileSuccessfully(GenerateShaderCode(body).c_str());
  458. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  459. }
  460. TEST_F(ValidateConversion, FConvertWrongResultType) {
  461. const std::string body = R"(
  462. %val = OpFConvert %u32 %f32_1
  463. )";
  464. CompileSuccessfully(GenerateShaderCode(body).c_str());
  465. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  466. EXPECT_THAT(
  467. getDiagnosticString(),
  468. HasSubstr(
  469. "Expected float scalar or vector type as Result Type: FConvert"));
  470. }
  471. TEST_F(ValidateConversion, FConvertWrongInputType) {
  472. const std::string body = R"(
  473. %val = OpFConvert %f32 %u64_1
  474. )";
  475. CompileSuccessfully(GenerateShaderCode(body).c_str());
  476. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  477. EXPECT_THAT(
  478. getDiagnosticString(),
  479. HasSubstr("Expected input to be float scalar or vector: FConvert"));
  480. }
  481. TEST_F(ValidateConversion, FConvertDifferentDimension) {
  482. const std::string body = R"(
  483. %val = OpFConvert %f64 %f32vec2_01
  484. )";
  485. CompileSuccessfully(GenerateShaderCode(body).c_str());
  486. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  487. EXPECT_THAT(getDiagnosticString(),
  488. HasSubstr("Expected input to have the same dimension as Result "
  489. "Type: FConvert"));
  490. }
  491. TEST_F(ValidateConversion, FConvertSameBitWidth) {
  492. const std::string body = R"(
  493. %val = OpFConvert %f32 %f32_1
  494. )";
  495. CompileSuccessfully(GenerateShaderCode(body).c_str());
  496. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  497. EXPECT_THAT(getDiagnosticString(),
  498. HasSubstr("Expected input to have different bit width from "
  499. "Result Type: FConvert"));
  500. }
  501. TEST_F(ValidateConversion, QuantizeToF16Success) {
  502. const std::string body = R"(
  503. %val1 = OpQuantizeToF16 %f32 %f32_1
  504. %val2 = OpQuantizeToF16 %f32 %f32_0
  505. %val3 = OpQuantizeToF16 %f32vec2 %f32vec2_01
  506. )";
  507. CompileSuccessfully(GenerateShaderCode(body).c_str());
  508. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  509. }
  510. TEST_F(ValidateConversion, QuantizeToF16WrongResultType) {
  511. const std::string body = R"(
  512. %val = OpQuantizeToF16 %u32 %f32_1
  513. )";
  514. CompileSuccessfully(GenerateShaderCode(body).c_str());
  515. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  516. EXPECT_THAT(
  517. getDiagnosticString(),
  518. HasSubstr("Expected 32-bit float scalar or vector type as Result Type: "
  519. "QuantizeToF16"));
  520. }
  521. TEST_F(ValidateConversion, QuantizeToF16WrongResultTypeBitWidth) {
  522. const std::string body = R"(
  523. %val = OpQuantizeToF16 %u64 %f64_1
  524. )";
  525. CompileSuccessfully(GenerateShaderCode(body).c_str());
  526. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  527. EXPECT_THAT(
  528. getDiagnosticString(),
  529. HasSubstr("Expected 32-bit float scalar or vector type as Result Type: "
  530. "QuantizeToF16"));
  531. }
  532. TEST_F(ValidateConversion, QuantizeToF16WrongInputType) {
  533. const std::string body = R"(
  534. %val = OpQuantizeToF16 %f32 %f64_1
  535. )";
  536. CompileSuccessfully(GenerateShaderCode(body).c_str());
  537. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  538. EXPECT_THAT(
  539. getDiagnosticString(),
  540. HasSubstr(
  541. "Expected input type to be equal to Result Type: QuantizeToF16"));
  542. }
  543. TEST_F(ValidateConversion, ConvertPtrToUSuccess) {
  544. const std::string body = R"(
  545. %ptr = OpVariable %f32ptr_func Function
  546. %val1 = OpConvertPtrToU %u32 %ptr
  547. %val2 = OpConvertPtrToU %u64 %ptr
  548. )";
  549. CompileSuccessfully(GenerateKernelCode(body).c_str());
  550. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  551. }
  552. TEST_F(ValidateConversion, ConvertPtrToUWrongResultType) {
  553. const std::string body = R"(
  554. %ptr = OpVariable %f32ptr_func Function
  555. %val = OpConvertPtrToU %f32 %ptr
  556. )";
  557. CompileSuccessfully(GenerateKernelCode(body).c_str());
  558. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  559. EXPECT_THAT(getDiagnosticString(),
  560. HasSubstr("Expected unsigned int scalar type as Result Type: "
  561. "ConvertPtrToU"));
  562. }
  563. TEST_F(ValidateConversion, ConvertPtrToUNotPointer) {
  564. const std::string body = R"(
  565. %val = OpConvertPtrToU %u32 %f32_1
  566. )";
  567. CompileSuccessfully(GenerateKernelCode(body).c_str());
  568. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  569. EXPECT_THAT(getDiagnosticString(),
  570. HasSubstr("Expected input to be a pointer: ConvertPtrToU"));
  571. }
  572. TEST_F(ValidateConversion, SatConvertSToUSuccess) {
  573. const std::string body = R"(
  574. %val1 = OpSatConvertSToU %u32 %u64_2
  575. %val2 = OpSatConvertSToU %u64 %u32_1
  576. %val3 = OpSatConvertSToU %u64vec2 %u32vec2_12
  577. )";
  578. CompileSuccessfully(GenerateKernelCode(body).c_str());
  579. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  580. }
  581. TEST_F(ValidateConversion, SatConvertSToUWrongResultType) {
  582. const std::string body = R"(
  583. %val = OpSatConvertSToU %f32 %u32_1
  584. )";
  585. CompileSuccessfully(GenerateKernelCode(body).c_str());
  586. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  587. EXPECT_THAT(getDiagnosticString(),
  588. HasSubstr("Expected int scalar or vector type as Result Type: "
  589. "SatConvertSToU"));
  590. }
  591. TEST_F(ValidateConversion, SatConvertSToUWrongInputType) {
  592. const std::string body = R"(
  593. %val = OpSatConvertSToU %u32 %f32_1
  594. )";
  595. CompileSuccessfully(GenerateKernelCode(body).c_str());
  596. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  597. EXPECT_THAT(
  598. getDiagnosticString(),
  599. HasSubstr("Expected int scalar or vector as input: SatConvertSToU"));
  600. }
  601. TEST_F(ValidateConversion, SatConvertSToUDifferentDimension) {
  602. const std::string body = R"(
  603. %val = OpSatConvertSToU %u32 %u32vec2_12
  604. )";
  605. CompileSuccessfully(GenerateKernelCode(body).c_str());
  606. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  607. EXPECT_THAT(
  608. getDiagnosticString(),
  609. HasSubstr("Expected input to have the same dimension as Result Type: "
  610. "SatConvertSToU"));
  611. }
  612. TEST_F(ValidateConversion, ConvertUToPtrSuccess) {
  613. const std::string body = R"(
  614. %val1 = OpConvertUToPtr %f32ptr_func %u32_1
  615. %val2 = OpConvertUToPtr %f32ptr_func %u64_1
  616. )";
  617. CompileSuccessfully(GenerateKernelCode(body).c_str());
  618. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  619. }
  620. TEST_F(ValidateConversion, ConvertUToPtrWrongResultType) {
  621. const std::string body = R"(
  622. %val = OpConvertUToPtr %f32 %u32_1
  623. )";
  624. CompileSuccessfully(GenerateKernelCode(body).c_str());
  625. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  626. EXPECT_THAT(getDiagnosticString(),
  627. HasSubstr("Expected Result Type to be a pointer: ConvertUToPtr"));
  628. }
  629. TEST_F(ValidateConversion, ConvertUToPtrNotInt) {
  630. const std::string body = R"(
  631. %val = OpConvertUToPtr %f32ptr_func %f32_1
  632. )";
  633. CompileSuccessfully(GenerateKernelCode(body).c_str());
  634. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  635. EXPECT_THAT(getDiagnosticString(),
  636. HasSubstr("Expected int scalar as input: ConvertUToPtr"));
  637. }
  638. TEST_F(ValidateConversion, ConvertUToPtrNotIntScalar) {
  639. const std::string body = R"(
  640. %val = OpConvertUToPtr %f32ptr_func %u32vec2_12
  641. )";
  642. CompileSuccessfully(GenerateKernelCode(body).c_str());
  643. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  644. EXPECT_THAT(getDiagnosticString(),
  645. HasSubstr("Expected int scalar as input: ConvertUToPtr"));
  646. }
  647. TEST_F(ValidateConversion, PtrCastToGenericSuccess) {
  648. const std::string body = R"(
  649. %ptr_func = OpVariable %f32ptr_func Function
  650. %val = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  651. )";
  652. CompileSuccessfully(GenerateKernelCode(body).c_str());
  653. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  654. }
  655. TEST_F(ValidateConversion, PtrCastToGenericWrongResultType) {
  656. const std::string body = R"(
  657. %ptr_func = OpVariable %f32ptr_func Function
  658. %val = OpPtrCastToGeneric %f32 %ptr_func
  659. )";
  660. CompileSuccessfully(GenerateKernelCode(body).c_str());
  661. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  662. EXPECT_THAT(
  663. getDiagnosticString(),
  664. HasSubstr("Expected Result Type to be a pointer: PtrCastToGeneric"));
  665. }
  666. TEST_F(ValidateConversion, PtrCastToGenericWrongResultStorageClass) {
  667. const std::string body = R"(
  668. %ptr_func = OpVariable %f32ptr_func Function
  669. %val = OpPtrCastToGeneric %f32ptr_func %ptr_func
  670. )";
  671. CompileSuccessfully(GenerateKernelCode(body).c_str());
  672. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  673. EXPECT_THAT(getDiagnosticString(),
  674. HasSubstr("Expected Result Type to have storage class Generic: "
  675. "PtrCastToGeneric"));
  676. }
  677. TEST_F(ValidateConversion, PtrCastToGenericWrongInputType) {
  678. const std::string body = R"(
  679. %ptr_func = OpVariable %f32ptr_func Function
  680. %val = OpPtrCastToGeneric %f32ptr_gen %f32
  681. )";
  682. CompileSuccessfully(GenerateKernelCode(body).c_str());
  683. ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  684. EXPECT_THAT(getDiagnosticString(), HasSubstr("Operand 4[%float] cannot be a "
  685. "type"));
  686. }
  687. TEST_F(ValidateConversion, PtrCastToGenericWrongInputStorageClass) {
  688. const std::string body = R"(
  689. %val = OpPtrCastToGeneric %f32ptr_gen %f32inp
  690. )";
  691. CompileSuccessfully(GenerateKernelCode(body).c_str());
  692. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  693. EXPECT_THAT(getDiagnosticString(),
  694. HasSubstr("Expected input to have storage class Workgroup, "
  695. "CrossWorkgroup or Function: PtrCastToGeneric"));
  696. }
  697. TEST_F(ValidateConversion, PtrCastToGenericPointToDifferentType) {
  698. const std::string body = R"(
  699. %ptr_func = OpVariable %u32ptr_func Function
  700. %val = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  701. )";
  702. CompileSuccessfully(GenerateKernelCode(body).c_str());
  703. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  704. EXPECT_THAT(
  705. getDiagnosticString(),
  706. HasSubstr("Expected input and Result Type to point to the same type: "
  707. "PtrCastToGeneric"));
  708. }
  709. TEST_F(ValidateConversion, GenericCastToPtrSuccess) {
  710. const std::string body = R"(
  711. %ptr_func = OpVariable %f32ptr_func Function
  712. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  713. %ptr_func2 = OpGenericCastToPtr %f32ptr_func %ptr_gen
  714. %ptr_wg = OpGenericCastToPtr %f32ptr_wg %ptr_gen
  715. %ptr_cwg = OpGenericCastToPtr %f32ptr_cwg %ptr_gen
  716. )";
  717. CompileSuccessfully(GenerateKernelCode(body).c_str());
  718. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  719. }
  720. TEST_F(ValidateConversion, GenericCastToPtrWrongResultType) {
  721. const std::string body = R"(
  722. %ptr_func = OpVariable %f32ptr_func Function
  723. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  724. %ptr_func2 = OpGenericCastToPtr %f32 %ptr_gen
  725. )";
  726. CompileSuccessfully(GenerateKernelCode(body).c_str());
  727. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  728. EXPECT_THAT(
  729. getDiagnosticString(),
  730. HasSubstr("Expected Result Type to be a pointer: GenericCastToPtr"));
  731. }
  732. TEST_F(ValidateConversion, GenericCastToPtrWrongResultStorageClass) {
  733. const std::string body = R"(
  734. %ptr_func = OpVariable %f32ptr_func Function
  735. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  736. %ptr_func2 = OpGenericCastToPtr %f32ptr_gen %ptr_gen
  737. )";
  738. CompileSuccessfully(GenerateKernelCode(body).c_str());
  739. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  740. EXPECT_THAT(getDiagnosticString(),
  741. HasSubstr("Expected Result Type to have storage class Workgroup, "
  742. "CrossWorkgroup or Function: GenericCastToPtr"));
  743. }
  744. TEST_F(ValidateConversion, GenericCastToPtrWrongInputType) {
  745. const std::string body = R"(
  746. %ptr_func = OpVariable %f32ptr_func Function
  747. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  748. %ptr_func2 = OpGenericCastToPtr %f32ptr_func %f32_1
  749. )";
  750. CompileSuccessfully(GenerateKernelCode(body).c_str());
  751. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  752. EXPECT_THAT(getDiagnosticString(),
  753. HasSubstr("Expected input to be a pointer: GenericCastToPtr"));
  754. }
  755. TEST_F(ValidateConversion, GenericCastToPtrWrongInputStorageClass) {
  756. const std::string body = R"(
  757. %ptr_func = OpVariable %f32ptr_func Function
  758. %ptr_func2 = OpGenericCastToPtr %f32ptr_func %ptr_func
  759. )";
  760. CompileSuccessfully(GenerateKernelCode(body).c_str());
  761. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  762. EXPECT_THAT(getDiagnosticString(),
  763. HasSubstr("Expected input to have storage class Generic: "
  764. "GenericCastToPtr"));
  765. }
  766. TEST_F(ValidateConversion, GenericCastToPtrPointToDifferentType) {
  767. const std::string body = R"(
  768. %ptr_func = OpVariable %f32ptr_func Function
  769. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  770. %ptr_func2 = OpGenericCastToPtr %u32ptr_func %ptr_gen
  771. )";
  772. CompileSuccessfully(GenerateKernelCode(body).c_str());
  773. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  774. EXPECT_THAT(
  775. getDiagnosticString(),
  776. HasSubstr("Expected input and Result Type to point to the same type: "
  777. "GenericCastToPtr"));
  778. }
  779. TEST_F(ValidateConversion, GenericCastToPtrExplicitSuccess) {
  780. const std::string body = R"(
  781. %ptr_func = OpVariable %f32ptr_func Function
  782. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  783. %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_func %ptr_gen Function
  784. %ptr_wg = OpGenericCastToPtrExplicit %f32ptr_wg %ptr_gen Workgroup
  785. %ptr_cwg = OpGenericCastToPtrExplicit %f32ptr_cwg %ptr_gen CrossWorkgroup
  786. )";
  787. CompileSuccessfully(GenerateKernelCode(body).c_str());
  788. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  789. }
  790. TEST_F(ValidateConversion, GenericCastToPtrExplicitWrongResultType) {
  791. const std::string body = R"(
  792. %ptr_func = OpVariable %f32ptr_func Function
  793. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  794. %ptr_func2 = OpGenericCastToPtrExplicit %f32 %ptr_gen Function
  795. )";
  796. CompileSuccessfully(GenerateKernelCode(body).c_str());
  797. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  798. EXPECT_THAT(
  799. getDiagnosticString(),
  800. HasSubstr(
  801. "Expected Result Type to be a pointer: GenericCastToPtrExplicit"));
  802. }
  803. TEST_F(ValidateConversion, GenericCastToPtrExplicitResultStorageClassDiffers) {
  804. const std::string body = R"(
  805. %ptr_func = OpVariable %f32ptr_func Function
  806. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  807. %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_func %ptr_gen Workgroup
  808. )";
  809. CompileSuccessfully(GenerateKernelCode(body).c_str());
  810. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  811. EXPECT_THAT(getDiagnosticString(),
  812. HasSubstr("Expected Result Type to be of target storage class: "
  813. "GenericCastToPtrExplicit"));
  814. }
  815. TEST_F(ValidateConversion, GenericCastToPtrExplicitWrongResultStorageClass) {
  816. const std::string body = R"(
  817. %ptr_func = OpVariable %f32ptr_func Function
  818. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  819. %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_gen %ptr_gen Generic
  820. )";
  821. CompileSuccessfully(GenerateKernelCode(body).c_str());
  822. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  823. EXPECT_THAT(
  824. getDiagnosticString(),
  825. HasSubstr("Expected target storage class to be Workgroup, "
  826. "CrossWorkgroup or Function: GenericCastToPtrExplicit"));
  827. }
  828. TEST_F(ValidateConversion, GenericCastToPtrExplicitWrongInputType) {
  829. const std::string body = R"(
  830. %ptr_func = OpVariable %f32ptr_func Function
  831. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  832. %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_func %f32_1 Function
  833. )";
  834. CompileSuccessfully(GenerateKernelCode(body).c_str());
  835. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  836. EXPECT_THAT(
  837. getDiagnosticString(),
  838. HasSubstr("Expected input to be a pointer: GenericCastToPtrExplicit"));
  839. }
  840. TEST_F(ValidateConversion, GenericCastToPtrExplicitWrongInputStorageClass) {
  841. const std::string body = R"(
  842. %ptr_func = OpVariable %f32ptr_func Function
  843. %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_func %ptr_func Function
  844. )";
  845. CompileSuccessfully(GenerateKernelCode(body).c_str());
  846. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  847. EXPECT_THAT(getDiagnosticString(),
  848. HasSubstr("Expected input to have storage class Generic: "
  849. "GenericCastToPtrExplicit"));
  850. }
  851. TEST_F(ValidateConversion, GenericCastToPtrExplicitPointToDifferentType) {
  852. const std::string body = R"(
  853. %ptr_func = OpVariable %f32ptr_func Function
  854. %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
  855. %ptr_func2 = OpGenericCastToPtrExplicit %u32ptr_func %ptr_gen Function
  856. )";
  857. CompileSuccessfully(GenerateKernelCode(body).c_str());
  858. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  859. EXPECT_THAT(
  860. getDiagnosticString(),
  861. HasSubstr("Expected input and Result Type to point to the same type: "
  862. "GenericCastToPtrExplicit"));
  863. }
  864. TEST_F(ValidateConversion, CoopMatConversionSuccess) {
  865. const std::string body =
  866. R"(
  867. OpCapability Shader
  868. OpCapability Float16
  869. OpCapability Int16
  870. OpCapability CooperativeMatrixNV
  871. OpExtension "SPV_NV_cooperative_matrix"
  872. OpMemoryModel Logical GLSL450
  873. OpEntryPoint GLCompute %main "main"
  874. %void = OpTypeVoid
  875. %func = OpTypeFunction %void
  876. %bool = OpTypeBool
  877. %f16 = OpTypeFloat 16
  878. %f32 = OpTypeFloat 32
  879. %u16 = OpTypeInt 16 0
  880. %u32 = OpTypeInt 32 0
  881. %s16 = OpTypeInt 16 1
  882. %s32 = OpTypeInt 32 1
  883. %u32_8 = OpConstant %u32 8
  884. %subgroup = OpConstant %u32 3
  885. %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
  886. %f32mat = OpTypeCooperativeMatrixNV %f32 %subgroup %u32_8 %u32_8
  887. %u16mat = OpTypeCooperativeMatrixNV %u16 %subgroup %u32_8 %u32_8
  888. %u32mat = OpTypeCooperativeMatrixNV %u32 %subgroup %u32_8 %u32_8
  889. %s16mat = OpTypeCooperativeMatrixNV %s16 %subgroup %u32_8 %u32_8
  890. %s32mat = OpTypeCooperativeMatrixNV %s32 %subgroup %u32_8 %u32_8
  891. %f16_1 = OpConstant %f16 1
  892. %f32_1 = OpConstant %f32 1
  893. %u16_1 = OpConstant %u16 1
  894. %u32_1 = OpConstant %u32 1
  895. %s16_1 = OpConstant %s16 1
  896. %s32_1 = OpConstant %s32 1
  897. %f16mat_1 = OpConstantComposite %f16mat %f16_1
  898. %f32mat_1 = OpConstantComposite %f32mat %f32_1
  899. %u16mat_1 = OpConstantComposite %u16mat %u16_1
  900. %u32mat_1 = OpConstantComposite %u32mat %u32_1
  901. %s16mat_1 = OpConstantComposite %s16mat %s16_1
  902. %s32mat_1 = OpConstantComposite %s32mat %s32_1
  903. %main = OpFunction %void None %func
  904. %main_entry = OpLabel
  905. %val11 = OpConvertFToU %u16mat %f16mat_1
  906. %val12 = OpConvertFToU %u32mat %f16mat_1
  907. %val13 = OpConvertFToS %s16mat %f16mat_1
  908. %val14 = OpConvertFToS %s32mat %f16mat_1
  909. %val15 = OpFConvert %f32mat %f16mat_1
  910. %val21 = OpConvertFToU %u16mat %f32mat_1
  911. %val22 = OpConvertFToU %u32mat %f32mat_1
  912. %val23 = OpConvertFToS %s16mat %f32mat_1
  913. %val24 = OpConvertFToS %s32mat %f32mat_1
  914. %val25 = OpFConvert %f16mat %f32mat_1
  915. %val31 = OpConvertUToF %f16mat %u16mat_1
  916. %val32 = OpConvertUToF %f32mat %u16mat_1
  917. %val33 = OpUConvert %u32mat %u16mat_1
  918. %val34 = OpSConvert %s32mat %u16mat_1
  919. %val41 = OpConvertSToF %f16mat %s16mat_1
  920. %val42 = OpConvertSToF %f32mat %s16mat_1
  921. %val43 = OpUConvert %u32mat %s16mat_1
  922. %val44 = OpSConvert %s32mat %s16mat_1
  923. OpReturn
  924. OpFunctionEnd)";
  925. CompileSuccessfully(body.c_str());
  926. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  927. }
  928. TEST_F(ValidateConversion, CoopMatConversionShapesMismatchFail) {
  929. const std::string body =
  930. R"(
  931. OpCapability Shader
  932. OpCapability Float16
  933. OpCapability Int16
  934. OpCapability CooperativeMatrixNV
  935. OpExtension "SPV_NV_cooperative_matrix"
  936. OpMemoryModel Logical GLSL450
  937. OpEntryPoint GLCompute %main "main"
  938. %void = OpTypeVoid
  939. %func = OpTypeFunction %void
  940. %bool = OpTypeBool
  941. %f16 = OpTypeFloat 16
  942. %f32 = OpTypeFloat 32
  943. %u16 = OpTypeInt 16 0
  944. %u32 = OpTypeInt 32 0
  945. %s16 = OpTypeInt 16 1
  946. %s32 = OpTypeInt 32 1
  947. %u32_8 = OpConstant %u32 8
  948. %u32_4 = OpConstant %u32 4
  949. %subgroup = OpConstant %u32 3
  950. %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
  951. %f32mat = OpTypeCooperativeMatrixNV %f32 %subgroup %u32_4 %u32_4
  952. %f16_1 = OpConstant %f16 1
  953. %f16mat_1 = OpConstantComposite %f16mat %f16_1
  954. %main = OpFunction %void None %func
  955. %main_entry = OpLabel
  956. %val15 = OpFConvert %f32mat %f16mat_1
  957. OpReturn
  958. OpFunctionEnd)";
  959. CompileSuccessfully(body.c_str());
  960. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  961. EXPECT_THAT(
  962. getDiagnosticString(),
  963. HasSubstr(
  964. "Expected rows of Matrix type and Result Type to be identical"));
  965. }
  966. TEST_F(ValidateConversion, CoopMatConversionShapesMismatchPass) {
  967. const std::string body =
  968. R"(
  969. OpCapability Shader
  970. OpCapability Float16
  971. OpCapability Int16
  972. OpCapability CooperativeMatrixNV
  973. OpExtension "SPV_NV_cooperative_matrix"
  974. OpMemoryModel Logical GLSL450
  975. OpEntryPoint GLCompute %main "main"
  976. %void = OpTypeVoid
  977. %func = OpTypeFunction %void
  978. %bool = OpTypeBool
  979. %f16 = OpTypeFloat 16
  980. %f32 = OpTypeFloat 32
  981. %u16 = OpTypeInt 16 0
  982. %u32 = OpTypeInt 32 0
  983. %s16 = OpTypeInt 16 1
  984. %s32 = OpTypeInt 32 1
  985. %u32_8 = OpConstant %u32 8
  986. %u32_4 = OpSpecConstant %u32 4
  987. %subgroup = OpConstant %u32 3
  988. %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
  989. %f32mat = OpTypeCooperativeMatrixNV %f32 %subgroup %u32_4 %u32_4
  990. %f16_1 = OpConstant %f16 1
  991. %f16mat_1 = OpConstantComposite %f16mat %f16_1
  992. %main = OpFunction %void None %func
  993. %main_entry = OpLabel
  994. %val15 = OpFConvert %f32mat %f16mat_1
  995. OpReturn
  996. OpFunctionEnd)";
  997. CompileSuccessfully(body.c_str());
  998. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  999. }
  1000. TEST_F(ValidateConversion, BitcastSuccess) {
  1001. const std::string body = R"(
  1002. %ptr = OpVariable %f32ptr_func Function
  1003. %val1 = OpBitcast %u32 %ptr
  1004. %val2 = OpBitcast %u64 %ptr
  1005. %val3 = OpBitcast %f32ptr_func %u32_1
  1006. %val4 = OpBitcast %f32ptr_wg %u64_1
  1007. %val5 = OpBitcast %f32 %u32_1
  1008. %val6 = OpBitcast %f32vec2 %u32vec2_12
  1009. %val7 = OpBitcast %f32vec2 %u64_1
  1010. %val8 = OpBitcast %f64 %u32vec2_12
  1011. %val9 = OpBitcast %f32vec4 %f64vec2_12
  1012. )";
  1013. CompileSuccessfully(GenerateKernelCode(body).c_str());
  1014. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  1015. }
  1016. TEST_F(ValidateConversion, BitcastSuccessSPV1p5) {
  1017. const std::string body = R"(
  1018. %ptr = OpVariable %f32ptr_func Function
  1019. %val1 = OpBitcast %u32 %ptr
  1020. %val2 = OpBitcast %u64 %ptr
  1021. %val3 = OpBitcast %f32ptr_func %u32_1
  1022. %val4 = OpBitcast %f32ptr_wg %u64_1
  1023. %val5 = OpBitcast %f32 %u32_1
  1024. %val6 = OpBitcast %f32vec2 %u32vec2_12
  1025. %val7 = OpBitcast %f32vec2 %u64_1
  1026. %val8 = OpBitcast %f64 %u32vec2_12
  1027. %val9 = OpBitcast %f32vec4 %f64vec2_12
  1028. %val10 = OpBitcast %u32ptr_func %u32vec2_01
  1029. %val11 = OpBitcast %u32vec2 %ptr
  1030. )";
  1031. CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
  1032. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
  1033. }
  1034. TEST_F(ValidateConversion, BitcastSuccessPhysicalStorageBufferKHR) {
  1035. const std::string body = R"(
  1036. %ptr = OpVariable %f32ptr_func Function
  1037. %val1 = OpBitcast %u32 %ptr
  1038. %val2 = OpBitcast %u64 %ptr
  1039. %val3 = OpBitcast %f32ptr_func %u32_1
  1040. %val4 = OpBitcast %f32ptr_wg %u64_1
  1041. %val5 = OpBitcast %f32 %u32_1
  1042. %val6 = OpBitcast %f32vec2 %u32vec2_12
  1043. %val7 = OpBitcast %f32vec2 %u64_1
  1044. %val8 = OpBitcast %f64 %u32vec2_12
  1045. %val9 = OpBitcast %f32vec4 %f64vec2_12
  1046. %val10 = OpBitcast %u32ptr_func %u32vec2_01
  1047. %val11 = OpBitcast %u32vec2 %ptr
  1048. )";
  1049. CompileSuccessfully(
  1050. GenerateKernelCode(body,
  1051. "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
  1052. .c_str());
  1053. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  1054. }
  1055. TEST_F(ValidateConversion, BitcastInputHasNoType) {
  1056. const std::string body = R"(
  1057. %val = OpBitcast %u32 %f32
  1058. )";
  1059. CompileSuccessfully(GenerateKernelCode(body).c_str());
  1060. ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  1061. EXPECT_THAT(getDiagnosticString(), HasSubstr("Operand 4[%float] cannot be a "
  1062. "type"));
  1063. }
  1064. TEST_F(ValidateConversion, BitcastWrongResultType) {
  1065. const std::string body = R"(
  1066. %val = OpBitcast %bool %f32_1
  1067. )";
  1068. CompileSuccessfully(GenerateKernelCode(body).c_str());
  1069. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1070. EXPECT_THAT(
  1071. getDiagnosticString(),
  1072. HasSubstr("Expected Result Type to be a pointer or int or float vector "
  1073. "or scalar type: Bitcast"));
  1074. }
  1075. TEST_F(ValidateConversion, BitcastWrongInputType) {
  1076. const std::string body = R"(
  1077. %val = OpBitcast %u32 %true
  1078. )";
  1079. CompileSuccessfully(GenerateKernelCode(body).c_str());
  1080. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1081. EXPECT_THAT(getDiagnosticString(),
  1082. HasSubstr("Expected input to be a pointer or int or float vector "
  1083. "or scalar: Bitcast"));
  1084. }
  1085. TEST_F(ValidateConversion, BitcastPtrWrongInputType) {
  1086. const std::string body = R"(
  1087. %val = OpBitcast %u32ptr_func %f32_1
  1088. )";
  1089. CompileSuccessfully(GenerateKernelCode(body).c_str());
  1090. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1091. EXPECT_THAT(getDiagnosticString(),
  1092. HasSubstr("Expected input to be a pointer or int scalar if "
  1093. "Result Type is pointer: Bitcast"));
  1094. }
  1095. TEST_F(ValidateConversion, BitcastPtrWrongInputTypeSPV1p5) {
  1096. const std::string body = R"(
  1097. %val = OpBitcast %u32ptr_func %f32_1
  1098. )";
  1099. CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
  1100. ASSERT_EQ(SPV_ERROR_INVALID_DATA,
  1101. ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
  1102. EXPECT_THAT(getDiagnosticString(),
  1103. HasSubstr("Expected input to be a pointer, int scalar or 32-bit "
  1104. "int vector if Result Type is pointer: Bitcast"));
  1105. }
  1106. TEST_F(ValidateConversion, BitcastPtrWrongInputTypePhysicalStorageBufferKHR) {
  1107. const std::string body = R"(
  1108. %val = OpBitcast %u32ptr_func %f32_1
  1109. )";
  1110. CompileSuccessfully(
  1111. GenerateKernelCode(body,
  1112. "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
  1113. .c_str());
  1114. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1115. EXPECT_THAT(getDiagnosticString(),
  1116. HasSubstr("Expected input to be a pointer, int scalar or 32-bit "
  1117. "int vector if Result Type is pointer: Bitcast"));
  1118. }
  1119. TEST_F(ValidateConversion, BitcastPtrWrongInputTypeIntVectorSPV1p5) {
  1120. const std::string body = R"(
  1121. %val = OpBitcast %u32ptr_func %u64vec2_01
  1122. )";
  1123. CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
  1124. ASSERT_EQ(SPV_ERROR_INVALID_DATA,
  1125. ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
  1126. EXPECT_THAT(getDiagnosticString(),
  1127. HasSubstr("Expected input to be a pointer, int scalar or 32-bit "
  1128. "int vector if Result Type is pointer: Bitcast"));
  1129. }
  1130. TEST_F(ValidateConversion,
  1131. BitcastPtrWrongInputTypeIntVectorPhysicalStorageBufferKHR) {
  1132. const std::string body = R"(
  1133. %val = OpBitcast %u32ptr_func %u64vec2_01
  1134. )";
  1135. CompileSuccessfully(
  1136. GenerateKernelCode(body,
  1137. "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
  1138. .c_str());
  1139. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1140. EXPECT_THAT(getDiagnosticString(),
  1141. HasSubstr("Expected input to be a pointer, int scalar or 32-bit "
  1142. "int vector if Result Type is pointer: Bitcast"));
  1143. }
  1144. TEST_F(ValidateConversion, BitcastPtrWrongResultType) {
  1145. const std::string body = R"(
  1146. %val = OpBitcast %f32 %f32inp
  1147. )";
  1148. CompileSuccessfully(GenerateKernelCode(body).c_str());
  1149. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1150. EXPECT_THAT(getDiagnosticString(),
  1151. HasSubstr("Pointer can only be converted to another pointer or "
  1152. "int scalar: Bitcast"));
  1153. }
  1154. TEST_F(ValidateConversion, BitcastPtrWrongResultTypeSPV1p5) {
  1155. const std::string body = R"(
  1156. %val = OpBitcast %f32 %f32inp
  1157. )";
  1158. CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
  1159. ASSERT_EQ(SPV_ERROR_INVALID_DATA,
  1160. ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
  1161. EXPECT_THAT(getDiagnosticString(),
  1162. HasSubstr("Pointer can only be converted to another pointer, int "
  1163. "scalar or 32-bit int vector: Bitcast"));
  1164. }
  1165. TEST_F(ValidateConversion, BitcastPtrWrongResultTypePhysicalStorageBufferKHR) {
  1166. const std::string body = R"(
  1167. %val = OpBitcast %f32 %f32inp
  1168. )";
  1169. CompileSuccessfully(
  1170. GenerateKernelCode(body,
  1171. "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
  1172. .c_str());
  1173. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1174. EXPECT_THAT(getDiagnosticString(),
  1175. HasSubstr("Pointer can only be converted to another pointer, int "
  1176. "scalar or 32-bit int vector: Bitcast"));
  1177. }
  1178. TEST_F(ValidateConversion, BitcastPtrWrongResultTypeIntVectorSPV1p5) {
  1179. const std::string body = R"(
  1180. %val = OpBitcast %u64vec2 %f32inp
  1181. )";
  1182. CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
  1183. ASSERT_EQ(SPV_ERROR_INVALID_DATA,
  1184. ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
  1185. EXPECT_THAT(getDiagnosticString(),
  1186. HasSubstr("Pointer can only be converted to another pointer, int "
  1187. "scalar or 32-bit int vector: Bitcast"));
  1188. }
  1189. TEST_F(ValidateConversion,
  1190. BitcastPtrWrongResultTypeIntVectorPhysicalStorageBufferKHR) {
  1191. const std::string body = R"(
  1192. %val = OpBitcast %u64vec2 %f32inp
  1193. )";
  1194. CompileSuccessfully(
  1195. GenerateKernelCode(body,
  1196. "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
  1197. .c_str());
  1198. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1199. EXPECT_THAT(getDiagnosticString(),
  1200. HasSubstr("Pointer can only be converted to another pointer, int "
  1201. "scalar or 32-bit int vector: Bitcast"));
  1202. }
  1203. TEST_F(ValidateConversion, BitcastDifferentTotalBitWidth) {
  1204. const std::string body = R"(
  1205. %val = OpBitcast %f32 %u64_1
  1206. )";
  1207. CompileSuccessfully(GenerateKernelCode(body).c_str());
  1208. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1209. EXPECT_THAT(
  1210. getDiagnosticString(),
  1211. HasSubstr(
  1212. "Expected input to have the same total bit width as Result Type: "
  1213. "Bitcast"));
  1214. }
  1215. TEST_F(ValidateConversion, ConvertUToPtrInputIsAType) {
  1216. const std::string spirv = R"(
  1217. OpCapability Addresses
  1218. OpCapability Shader
  1219. OpCapability Linkage
  1220. OpMemoryModel Logical GLSL450
  1221. %int = OpTypeInt 32 0
  1222. %ptr_int = OpTypePointer Function %int
  1223. %void = OpTypeVoid
  1224. %voidfn = OpTypeFunction %void
  1225. %func = OpFunction %void None %voidfn
  1226. %entry = OpLabel
  1227. %1 = OpConvertUToPtr %ptr_int %int
  1228. OpReturn
  1229. OpFunctionEnd
  1230. )";
  1231. CompileSuccessfully(spirv);
  1232. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  1233. EXPECT_THAT(getDiagnosticString(), HasSubstr("Operand 1[%uint] cannot be a "
  1234. "type"));
  1235. }
  1236. TEST_F(ValidateConversion, ConvertUToPtrPSBSuccess) {
  1237. const std::string body = R"(
  1238. OpCapability PhysicalStorageBufferAddressesEXT
  1239. OpCapability Int64
  1240. OpCapability Shader
  1241. OpExtension "SPV_EXT_physical_storage_buffer"
  1242. OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
  1243. OpEntryPoint Fragment %main "main"
  1244. OpExecutionMode %main OriginUpperLeft
  1245. %uint64 = OpTypeInt 64 0
  1246. %u64_1 = OpConstant %uint64 1
  1247. %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
  1248. %void = OpTypeVoid
  1249. %voidfn = OpTypeFunction %void
  1250. %main = OpFunction %void None %voidfn
  1251. %entry = OpLabel
  1252. %val1 = OpConvertUToPtr %ptr %u64_1
  1253. OpReturn
  1254. OpFunctionEnd
  1255. )";
  1256. CompileSuccessfully(body.c_str());
  1257. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  1258. }
  1259. TEST_F(ValidateConversion, ConvertUToPtrPSBStorageClass) {
  1260. const std::string body = R"(
  1261. OpCapability PhysicalStorageBufferAddressesEXT
  1262. OpCapability Int64
  1263. OpCapability Shader
  1264. OpExtension "SPV_EXT_physical_storage_buffer"
  1265. OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
  1266. OpEntryPoint Fragment %main "main"
  1267. OpExecutionMode %main OriginUpperLeft
  1268. %uint64 = OpTypeInt 64 0
  1269. %u64_1 = OpConstant %uint64 1
  1270. %ptr = OpTypePointer Function %uint64
  1271. %void = OpTypeVoid
  1272. %voidfn = OpTypeFunction %void
  1273. %main = OpFunction %void None %voidfn
  1274. %entry = OpLabel
  1275. %val1 = OpConvertUToPtr %ptr %u64_1
  1276. %val2 = OpConvertPtrToU %uint64 %val1
  1277. OpReturn
  1278. OpFunctionEnd
  1279. )";
  1280. CompileSuccessfully(body.c_str());
  1281. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1282. EXPECT_THAT(getDiagnosticString(),
  1283. HasSubstr("Pointer storage class must be "
  1284. "PhysicalStorageBufferEXT: ConvertUToPtr"));
  1285. }
  1286. TEST_F(ValidateConversion, ConvertPtrToUPSBSuccess) {
  1287. const std::string body = R"(
  1288. OpCapability PhysicalStorageBufferAddressesEXT
  1289. OpCapability Int64
  1290. OpCapability Shader
  1291. OpExtension "SPV_EXT_physical_storage_buffer"
  1292. OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
  1293. OpEntryPoint Fragment %main "main"
  1294. OpExecutionMode %main OriginUpperLeft
  1295. OpDecorate %val1 RestrictPointerEXT
  1296. %uint64 = OpTypeInt 64 0
  1297. %u64_1 = OpConstant %uint64 1
  1298. %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
  1299. %pptr_f = OpTypePointer Function %ptr
  1300. %void = OpTypeVoid
  1301. %voidfn = OpTypeFunction %void
  1302. %main = OpFunction %void None %voidfn
  1303. %entry = OpLabel
  1304. %val1 = OpVariable %pptr_f Function
  1305. %val2 = OpLoad %ptr %val1
  1306. %val3 = OpConvertPtrToU %uint64 %val2
  1307. OpReturn
  1308. OpFunctionEnd
  1309. )";
  1310. CompileSuccessfully(body.c_str());
  1311. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  1312. }
  1313. TEST_F(ValidateConversion, ConvertPtrToUPSBStorageClass) {
  1314. const std::string body = R"(
  1315. OpCapability PhysicalStorageBufferAddressesEXT
  1316. OpCapability Int64
  1317. OpCapability Shader
  1318. OpExtension "SPV_EXT_physical_storage_buffer"
  1319. OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
  1320. OpEntryPoint Fragment %main "main"
  1321. OpExecutionMode %main OriginUpperLeft
  1322. %uint64 = OpTypeInt 64 0
  1323. %u64_1 = OpConstant %uint64 1
  1324. %ptr = OpTypePointer Function %uint64
  1325. %void = OpTypeVoid
  1326. %voidfn = OpTypeFunction %void
  1327. %main = OpFunction %void None %voidfn
  1328. %entry = OpLabel
  1329. %val1 = OpVariable %ptr Function
  1330. %val2 = OpConvertPtrToU %uint64 %val1
  1331. OpReturn
  1332. OpFunctionEnd
  1333. )";
  1334. CompileSuccessfully(body.c_str());
  1335. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  1336. EXPECT_THAT(getDiagnosticString(),
  1337. HasSubstr("Pointer storage class must be "
  1338. "PhysicalStorageBufferEXT: ConvertPtrToU"));
  1339. }
  1340. using ValidateSmallConversions = spvtest::ValidateBase<std::string>;
  1341. CodeGenerator GetSmallConversionsCodeGenerator() {
  1342. CodeGenerator generator;
  1343. generator.capabilities_ = R"(
  1344. OpCapability Shader
  1345. OpCapability Linkage
  1346. OpCapability UniformAndStorageBuffer16BitAccess
  1347. OpCapability UniformAndStorageBuffer8BitAccess
  1348. )";
  1349. generator.extensions_ = R"(
  1350. OpExtension "SPV_KHR_16bit_storage"
  1351. OpExtension "SPV_KHR_8bit_storage"
  1352. )";
  1353. generator.memory_model_ = "OpMemoryModel Logical GLSL450\n";
  1354. generator.before_types_ = R"(
  1355. OpDecorate %char_block Block
  1356. OpMemberDecorate %char_block 0 Offset 0
  1357. OpDecorate %short_block Block
  1358. OpMemberDecorate %short_block 0 Offset 0
  1359. OpDecorate %half_block Block
  1360. OpMemberDecorate %half_block 0 Offset 0
  1361. OpDecorate %int_block Block
  1362. OpMemberDecorate %int_block 0 Offset 0
  1363. OpDecorate %float_block Block
  1364. OpMemberDecorate %float_block 0 Offset 0
  1365. )";
  1366. generator.types_ = R"(
  1367. %void = OpTypeVoid
  1368. %int = OpTypeInt 32 0
  1369. %int_0 = OpConstant %int 0
  1370. %int_1 = OpConstant %int 1
  1371. %int2 = OpTypeVector %int 2
  1372. %float = OpTypeFloat 32
  1373. %float_0 = OpConstant %float 0
  1374. %float2 = OpTypeVector %float 2
  1375. %char = OpTypeInt 8 0
  1376. %char2 = OpTypeVector %char 2
  1377. %short = OpTypeInt 16 0
  1378. %short2 = OpTypeVector %short 2
  1379. %half = OpTypeFloat 16
  1380. %half2 = OpTypeVector %half 2
  1381. %char_block = OpTypeStruct %char2
  1382. %short_block = OpTypeStruct %short2
  1383. %half_block = OpTypeStruct %half2
  1384. %int_block = OpTypeStruct %int2
  1385. %float_block = OpTypeStruct %float2
  1386. %ptr_ssbo_char_block = OpTypePointer StorageBuffer %char_block
  1387. %ptr_ssbo_char2 = OpTypePointer StorageBuffer %char2
  1388. %ptr_ssbo_char = OpTypePointer StorageBuffer %char
  1389. %ptr_ssbo_short_block = OpTypePointer StorageBuffer %short_block
  1390. %ptr_ssbo_short2 = OpTypePointer StorageBuffer %short2
  1391. %ptr_ssbo_short = OpTypePointer StorageBuffer %short
  1392. %ptr_ssbo_half_block = OpTypePointer StorageBuffer %half_block
  1393. %ptr_ssbo_half2 = OpTypePointer StorageBuffer %half2
  1394. %ptr_ssbo_half = OpTypePointer StorageBuffer %half
  1395. %ptr_ssbo_int_block = OpTypePointer StorageBuffer %int_block
  1396. %ptr_ssbo_int2 = OpTypePointer StorageBuffer %int2
  1397. %ptr_ssbo_int = OpTypePointer StorageBuffer %int
  1398. %ptr_ssbo_float_block = OpTypePointer StorageBuffer %float_block
  1399. %ptr_ssbo_float2 = OpTypePointer StorageBuffer %float2
  1400. %ptr_ssbo_float = OpTypePointer StorageBuffer %float
  1401. %void_fn = OpTypeFunction %void
  1402. %char_var = OpVariable %ptr_ssbo_char_block StorageBuffer
  1403. %short_var = OpVariable %ptr_ssbo_short_block StorageBuffer
  1404. %half_var = OpVariable %ptr_ssbo_half_block StorageBuffer
  1405. %int_var = OpVariable %ptr_ssbo_int_block StorageBuffer
  1406. %float_var = OpVariable %ptr_ssbo_float_block StorageBuffer
  1407. )";
  1408. generator.after_types_ = R"(
  1409. %func = OpFunction %void None %void_fn
  1410. %entry = OpLabel
  1411. %char2_gep = OpAccessChain %ptr_ssbo_char2 %char_var %int_0
  1412. %ld_char2 = OpLoad %char2 %char2_gep
  1413. %char_gep = OpAccessChain %ptr_ssbo_char %char_var %int_0 %int_0
  1414. %ld_char = OpLoad %char %char_gep
  1415. %short2_gep = OpAccessChain %ptr_ssbo_short2 %short_var %int_0
  1416. %ld_short2 = OpLoad %short2 %short2_gep
  1417. %short_gep = OpAccessChain %ptr_ssbo_short %short_var %int_0 %int_0
  1418. %ld_short = OpLoad %short %short_gep
  1419. %half2_gep = OpAccessChain %ptr_ssbo_half2 %half_var %int_0
  1420. %ld_half2 = OpLoad %half2 %half2_gep
  1421. %half_gep = OpAccessChain %ptr_ssbo_half %half_var %int_0 %int_0
  1422. %ld_half = OpLoad %half %half_gep
  1423. %int2_gep = OpAccessChain %ptr_ssbo_int2 %int_var %int_0
  1424. %ld_int2 = OpLoad %int2 %int2_gep
  1425. %int_gep = OpAccessChain %ptr_ssbo_int %int_var %int_0 %int_0
  1426. %ld_int = OpLoad %int %int_gep
  1427. %float2_gep = OpAccessChain %ptr_ssbo_float2 %float_var %int_0
  1428. %ld_float2 = OpLoad %float2 %float2_gep
  1429. %float_gep = OpAccessChain %ptr_ssbo_float %float_var %int_0 %int_0
  1430. %ld_float = OpLoad %float %float_gep
  1431. )";
  1432. generator.add_at_the_end_ = R"(
  1433. OpReturn
  1434. OpFunctionEnd
  1435. )";
  1436. return generator;
  1437. }
  1438. TEST_P(ValidateSmallConversions, Instruction) {
  1439. CodeGenerator generator = GetSmallConversionsCodeGenerator();
  1440. generator.after_types_ += GetParam() + "\n";
  1441. CompileSuccessfully(generator.Build(), SPV_ENV_UNIVERSAL_1_3);
  1442. EXPECT_EQ(SPV_ERROR_INVALID_DATA,
  1443. ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
  1444. EXPECT_THAT(
  1445. getDiagnosticString(),
  1446. HasSubstr(
  1447. "8- or 16-bit types can only be used with width-only conversions"));
  1448. }
  1449. INSTANTIATE_TEST_SUITE_P(SmallConversionInstructions, ValidateSmallConversions,
  1450. Values("%inst = OpConvertFToU %char %ld_float",
  1451. "%inst = OpConvertFToU %char2 %ld_float2",
  1452. "%inst = OpConvertFToU %short %ld_float",
  1453. "%inst = OpConvertFToU %short2 %ld_float2",
  1454. "%inst = OpConvertFToU %int %ld_half",
  1455. "%inst = OpConvertFToU %int2 %ld_half2",
  1456. "%inst = OpConvertFToS %char %ld_float",
  1457. "%inst = OpConvertFToS %char2 %ld_float2",
  1458. "%inst = OpConvertFToS %short %ld_float",
  1459. "%inst = OpConvertFToS %short2 %ld_float2",
  1460. "%inst = OpConvertFToS %int %ld_half",
  1461. "%inst = OpConvertFToS %int2 %ld_half2",
  1462. "%inst = OpConvertSToF %float %ld_char",
  1463. "%inst = OpConvertSToF %float2 %ld_char2",
  1464. "%inst = OpConvertSToF %float %ld_short",
  1465. "%inst = OpConvertSToF %float2 %ld_short2",
  1466. "%inst = OpConvertSToF %half %ld_int",
  1467. "%inst = OpConvertSToF %half2 %ld_int2",
  1468. "%inst = OpConvertUToF %float %ld_char",
  1469. "%inst = OpConvertUToF %float2 %ld_char2",
  1470. "%inst = OpConvertUToF %float %ld_short",
  1471. "%inst = OpConvertUToF %float2 %ld_short2",
  1472. "%inst = OpConvertUToF %half %ld_int",
  1473. "%inst = OpConvertUToF %half2 %ld_int2",
  1474. "%inst = OpBitcast %half %ld_short",
  1475. "%inst = OpBitcast %half2 %ld_short2",
  1476. "%inst = OpBitcast %short %ld_half",
  1477. "%inst = OpBitcast %short2 %ld_half2"));
  1478. } // namespace
  1479. } // namespace val
  1480. } // namespace spvtools