debug_info_manager_test.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // Copyright (c) 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/debug_info_manager.h"
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "effcee/effcee.h"
  19. #include "gmock/gmock.h"
  20. #include "gtest/gtest.h"
  21. #include "source/opt/build_module.h"
  22. #include "source/opt/instruction.h"
  23. #include "spirv-tools/libspirv.hpp"
  24. // Constants for OpenCL.DebugInfo.100 extension instructions.
  25. static const uint32_t kDebugFunctionOperandFunctionIndex = 13;
  26. static const uint32_t kDebugInlinedAtOperandLineIndex = 4;
  27. static const uint32_t kDebugInlinedAtOperandScopeIndex = 5;
  28. static const uint32_t kDebugInlinedAtOperandInlinedIndex = 6;
  29. namespace spvtools {
  30. namespace opt {
  31. namespace analysis {
  32. namespace {
  33. TEST(DebugInfoManager, GetDebugInlinedAt) {
  34. const std::string text = R"(
  35. OpCapability Shader
  36. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  37. OpMemoryModel Logical GLSL450
  38. OpEntryPoint Fragment %main "main" %in_var_COLOR
  39. OpExecutionMode %main OriginUpperLeft
  40. %5 = OpString "ps.hlsl"
  41. %14 = OpString "#line 1 \"ps.hlsl\"
  42. void main(float in_var_color : COLOR) {
  43. float color = in_var_color;
  44. }
  45. "
  46. %17 = OpString "float"
  47. %21 = OpString "main"
  48. %24 = OpString "color"
  49. OpName %in_var_COLOR "in.var.COLOR"
  50. OpName %main "main"
  51. OpDecorate %in_var_COLOR Location 0
  52. %uint = OpTypeInt 32 0
  53. %uint_32 = OpConstant %uint 32
  54. %float = OpTypeFloat 32
  55. %_ptr_Input_float = OpTypePointer Input %float
  56. %void = OpTypeVoid
  57. %27 = OpTypeFunction %void
  58. %_ptr_Function_float = OpTypePointer Function %float
  59. %in_var_COLOR = OpVariable %_ptr_Input_float Input
  60. %13 = OpExtInst %void %1 DebugExpression
  61. %15 = OpExtInst %void %1 DebugSource %5 %14
  62. %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
  63. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
  64. %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
  65. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
  66. %100 = OpExtInst %void %1 DebugInlinedAt 7 %22
  67. %main = OpFunction %void None %27
  68. %28 = OpLabel
  69. %31 = OpLoad %float %in_var_COLOR
  70. OpStore %100 %31
  71. OpReturn
  72. OpFunctionEnd
  73. )";
  74. std::unique_ptr<IRContext> context =
  75. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  76. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  77. DebugInfoManager manager(context.get());
  78. EXPECT_EQ(manager.GetDebugInlinedAt(150), nullptr);
  79. EXPECT_EQ(manager.GetDebugInlinedAt(31), nullptr);
  80. EXPECT_EQ(manager.GetDebugInlinedAt(22), nullptr);
  81. auto* inst = manager.GetDebugInlinedAt(100);
  82. EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex), 7);
  83. EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex), 22);
  84. }
  85. TEST(DebugInfoManager, CreateDebugInlinedAt) {
  86. const std::string text = R"(
  87. OpCapability Shader
  88. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  89. OpMemoryModel Logical GLSL450
  90. OpEntryPoint Fragment %main "main" %in_var_COLOR
  91. OpExecutionMode %main OriginUpperLeft
  92. %5 = OpString "ps.hlsl"
  93. %14 = OpString "#line 1 \"ps.hlsl\"
  94. void main(float in_var_color : COLOR) {
  95. float color = in_var_color;
  96. }
  97. "
  98. %17 = OpString "float"
  99. %21 = OpString "main"
  100. %24 = OpString "color"
  101. OpName %in_var_COLOR "in.var.COLOR"
  102. OpName %main "main"
  103. OpDecorate %in_var_COLOR Location 0
  104. %uint = OpTypeInt 32 0
  105. %uint_32 = OpConstant %uint 32
  106. %float = OpTypeFloat 32
  107. %_ptr_Input_float = OpTypePointer Input %float
  108. %void = OpTypeVoid
  109. %27 = OpTypeFunction %void
  110. %_ptr_Function_float = OpTypePointer Function %float
  111. %in_var_COLOR = OpVariable %_ptr_Input_float Input
  112. %13 = OpExtInst %void %1 DebugExpression
  113. %15 = OpExtInst %void %1 DebugSource %5 %14
  114. %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
  115. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
  116. %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
  117. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
  118. %100 = OpExtInst %void %1 DebugInlinedAt 7 %22
  119. %main = OpFunction %void None %27
  120. %28 = OpLabel
  121. %31 = OpLoad %float %in_var_COLOR
  122. OpStore %100 %31
  123. OpReturn
  124. OpFunctionEnd
  125. )";
  126. DebugScope scope(22U, 0U);
  127. std::unique_ptr<IRContext> context =
  128. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  129. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  130. DebugInfoManager manager(context.get());
  131. uint32_t inlined_at_id = manager.CreateDebugInlinedAt(nullptr, scope);
  132. auto* inlined_at = manager.GetDebugInlinedAt(inlined_at_id);
  133. EXPECT_NE(inlined_at, nullptr);
  134. EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex),
  135. 1);
  136. EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex),
  137. 22);
  138. EXPECT_EQ(inlined_at->NumOperands(), kDebugInlinedAtOperandScopeIndex + 1);
  139. const uint32_t line_number = 77U;
  140. Instruction line(context.get(), SpvOpLine);
  141. line.SetInOperands({
  142. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {5U}},
  143. {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {line_number}},
  144. {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {0U}},
  145. });
  146. inlined_at_id = manager.CreateDebugInlinedAt(&line, scope);
  147. inlined_at = manager.GetDebugInlinedAt(inlined_at_id);
  148. EXPECT_NE(inlined_at, nullptr);
  149. EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex),
  150. line_number);
  151. EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex),
  152. 22);
  153. EXPECT_EQ(inlined_at->NumOperands(), kDebugInlinedAtOperandScopeIndex + 1);
  154. scope.SetInlinedAt(100U);
  155. inlined_at_id = manager.CreateDebugInlinedAt(&line, scope);
  156. inlined_at = manager.GetDebugInlinedAt(inlined_at_id);
  157. EXPECT_NE(inlined_at, nullptr);
  158. EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex),
  159. line_number);
  160. EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex),
  161. 22);
  162. EXPECT_EQ(inlined_at->NumOperands(), kDebugInlinedAtOperandInlinedIndex + 1);
  163. EXPECT_EQ(
  164. inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandInlinedIndex),
  165. 100U);
  166. }
  167. TEST(DebugInfoManager, GetDebugInfoNone) {
  168. const std::string text = R"(
  169. OpCapability Shader
  170. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  171. OpMemoryModel Logical GLSL450
  172. OpEntryPoint Fragment %main "main" %in_var_COLOR
  173. OpExecutionMode %main OriginUpperLeft
  174. %5 = OpString "ps.hlsl"
  175. %14 = OpString "#line 1 \"ps.hlsl\"
  176. void main(float in_var_color : COLOR) {
  177. float color = in_var_color;
  178. }
  179. "
  180. %17 = OpString "float"
  181. %21 = OpString "main"
  182. %24 = OpString "color"
  183. OpName %in_var_COLOR "in.var.COLOR"
  184. OpName %main "main"
  185. OpDecorate %in_var_COLOR Location 0
  186. %uint = OpTypeInt 32 0
  187. %uint_32 = OpConstant %uint 32
  188. %float = OpTypeFloat 32
  189. %_ptr_Input_float = OpTypePointer Input %float
  190. %void = OpTypeVoid
  191. %27 = OpTypeFunction %void
  192. %_ptr_Function_float = OpTypePointer Function %float
  193. %in_var_COLOR = OpVariable %_ptr_Input_float Input
  194. %13 = OpExtInst %void %1 DebugExpression
  195. %15 = OpExtInst %void %1 DebugSource %5 %14
  196. %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
  197. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
  198. %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
  199. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
  200. %12 = OpExtInst %void %1 DebugInfoNone
  201. %25 = OpExtInst %void %1 DebugLocalVariable %24 %18 %15 1 20 %22 FlagIsLocal 0
  202. %main = OpFunction %void None %27
  203. %28 = OpLabel
  204. %100 = OpVariable %_ptr_Function_float Function
  205. %31 = OpLoad %float %in_var_COLOR
  206. OpStore %100 %31
  207. %36 = OpExtInst %void %1 DebugDeclare %25 %100 %13
  208. OpReturn
  209. OpFunctionEnd
  210. )";
  211. std::unique_ptr<IRContext> context =
  212. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  213. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  214. DebugInfoManager manager(context.get());
  215. Instruction* debug_info_none_inst = manager.GetDebugInfoNone();
  216. EXPECT_NE(debug_info_none_inst, nullptr);
  217. EXPECT_EQ(debug_info_none_inst->GetOpenCL100DebugOpcode(),
  218. OpenCLDebugInfo100DebugInfoNone);
  219. EXPECT_EQ(debug_info_none_inst->PreviousNode(), nullptr);
  220. }
  221. TEST(DebugInfoManager, CreateDebugInfoNone) {
  222. const std::string text = R"(
  223. OpCapability Shader
  224. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  225. OpMemoryModel Logical GLSL450
  226. OpEntryPoint Fragment %main "main" %in_var_COLOR
  227. OpExecutionMode %main OriginUpperLeft
  228. %5 = OpString "ps.hlsl"
  229. %14 = OpString "#line 1 \"ps.hlsl\"
  230. void main(float in_var_color : COLOR) {
  231. float color = in_var_color;
  232. }
  233. "
  234. %17 = OpString "float"
  235. %21 = OpString "main"
  236. %24 = OpString "color"
  237. OpName %in_var_COLOR "in.var.COLOR"
  238. OpName %main "main"
  239. OpDecorate %in_var_COLOR Location 0
  240. %uint = OpTypeInt 32 0
  241. %uint_32 = OpConstant %uint 32
  242. %float = OpTypeFloat 32
  243. %_ptr_Input_float = OpTypePointer Input %float
  244. %void = OpTypeVoid
  245. %27 = OpTypeFunction %void
  246. %_ptr_Function_float = OpTypePointer Function %float
  247. %in_var_COLOR = OpVariable %_ptr_Input_float Input
  248. %13 = OpExtInst %void %1 DebugExpression
  249. %15 = OpExtInst %void %1 DebugSource %5 %14
  250. %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
  251. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
  252. %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
  253. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
  254. %25 = OpExtInst %void %1 DebugLocalVariable %24 %18 %15 1 20 %22 FlagIsLocal 0
  255. %main = OpFunction %void None %27
  256. %28 = OpLabel
  257. %100 = OpVariable %_ptr_Function_float Function
  258. %31 = OpLoad %float %in_var_COLOR
  259. OpStore %100 %31
  260. %36 = OpExtInst %void %1 DebugDeclare %25 %100 %13
  261. OpReturn
  262. OpFunctionEnd
  263. )";
  264. std::unique_ptr<IRContext> context =
  265. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  266. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  267. DebugInfoManager manager(context.get());
  268. Instruction* debug_info_none_inst = manager.GetDebugInfoNone();
  269. EXPECT_NE(debug_info_none_inst, nullptr);
  270. EXPECT_EQ(debug_info_none_inst->GetOpenCL100DebugOpcode(),
  271. OpenCLDebugInfo100DebugInfoNone);
  272. EXPECT_EQ(debug_info_none_inst->PreviousNode(), nullptr);
  273. }
  274. TEST(DebugInfoManager, GetDebugFunction) {
  275. const std::string text = R"(
  276. OpCapability Shader
  277. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  278. OpMemoryModel Logical GLSL450
  279. OpEntryPoint Fragment %200 "200" %in_var_COLOR
  280. OpExecutionMode %200 OriginUpperLeft
  281. %5 = OpString "ps.hlsl"
  282. %14 = OpString "#line 1 \"ps.hlsl\"
  283. void 200(float in_var_color : COLOR) {
  284. float color = in_var_color;
  285. }
  286. "
  287. %17 = OpString "float"
  288. %21 = OpString "200"
  289. %24 = OpString "color"
  290. OpName %in_var_COLOR "in.var.COLOR"
  291. OpName %200 "200"
  292. OpDecorate %in_var_COLOR Location 0
  293. %uint = OpTypeInt 32 0
  294. %uint_32 = OpConstant %uint 32
  295. %float = OpTypeFloat 32
  296. %_ptr_Input_float = OpTypePointer Input %float
  297. %void = OpTypeVoid
  298. %27 = OpTypeFunction %void
  299. %_ptr_Function_float = OpTypePointer Function %float
  300. %in_var_COLOR = OpVariable %_ptr_Input_float Input
  301. %13 = OpExtInst %void %1 DebugExpression
  302. %15 = OpExtInst %void %1 DebugSource %5 %14
  303. %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
  304. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
  305. %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
  306. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %200
  307. %25 = OpExtInst %void %1 DebugLocalVariable %24 %18 %15 1 20 %22 FlagIsLocal 0
  308. %200 = OpFunction %void None %27
  309. %28 = OpLabel
  310. %100 = OpVariable %_ptr_Function_float Function
  311. %31 = OpLoad %float %in_var_COLOR
  312. OpStore %100 %31
  313. %36 = OpExtInst %void %1 DebugDeclare %25 %100 %13
  314. OpReturn
  315. OpFunctionEnd
  316. )";
  317. std::unique_ptr<IRContext> context =
  318. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  319. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  320. DebugInfoManager manager(context.get());
  321. EXPECT_EQ(manager.GetDebugFunction(100), nullptr);
  322. EXPECT_EQ(manager.GetDebugFunction(150), nullptr);
  323. Instruction* dbg_fn = manager.GetDebugFunction(200);
  324. EXPECT_EQ(dbg_fn->GetOpenCL100DebugOpcode(), OpenCLDebugInfo100DebugFunction);
  325. EXPECT_EQ(dbg_fn->GetSingleWordOperand(kDebugFunctionOperandFunctionIndex),
  326. 200);
  327. }
  328. TEST(DebugInfoManager, GetDebugFunction_InlinedAway) {
  329. // struct PS_INPUT
  330. // {
  331. // float4 iColor : COLOR;
  332. // };
  333. //
  334. // struct PS_OUTPUT
  335. // {
  336. // float4 oColor : SV_Target0;
  337. // };
  338. //
  339. // float4 foo(float4 ic)
  340. // {
  341. // float4 c = ic / 2.0;
  342. // return c;
  343. // }
  344. //
  345. // PS_OUTPUT MainPs(PS_INPUT i)
  346. // {
  347. // PS_OUTPUT ps_output;
  348. // float4 ic = i.iColor;
  349. // ps_output.oColor = foo(ic);
  350. // return ps_output;
  351. // }
  352. const std::string text = R"(
  353. OpCapability Shader
  354. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  355. OpMemoryModel Logical GLSL450
  356. OpEntryPoint Fragment %MainPs "MainPs" %in_var_COLOR %out_var_SV_Target0
  357. OpExecutionMode %MainPs OriginUpperLeft
  358. %15 = OpString "foo2.frag"
  359. %19 = OpString "PS_OUTPUT"
  360. %23 = OpString "float"
  361. %26 = OpString "oColor"
  362. %28 = OpString "PS_INPUT"
  363. %31 = OpString "iColor"
  364. %33 = OpString "foo"
  365. %37 = OpString "c"
  366. %39 = OpString "ic"
  367. %42 = OpString "src.MainPs"
  368. %47 = OpString "ps_output"
  369. %50 = OpString "i"
  370. OpName %in_var_COLOR "in.var.COLOR"
  371. OpName %out_var_SV_Target0 "out.var.SV_Target0"
  372. OpName %MainPs "MainPs"
  373. OpDecorate %in_var_COLOR Location 0
  374. OpDecorate %out_var_SV_Target0 Location 0
  375. %int = OpTypeInt 32 1
  376. %int_0 = OpConstant %int 0
  377. %float = OpTypeFloat 32
  378. %v4float = OpTypeVector %float 4
  379. %uint = OpTypeInt 32 0
  380. %uint_32 = OpConstant %uint 32
  381. %_ptr_Input_v4float = OpTypePointer Input %v4float
  382. %_ptr_Output_v4float = OpTypePointer Output %v4float
  383. %void = OpTypeVoid
  384. %uint_128 = OpConstant %uint 128
  385. %uint_0 = OpConstant %uint 0
  386. %52 = OpTypeFunction %void
  387. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  388. %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
  389. %float_0_5 = OpConstant %float 0.5
  390. %130 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
  391. %115 = OpExtInst %void %1 DebugInfoNone
  392. %49 = OpExtInst %void %1 DebugExpression
  393. %17 = OpExtInst %void %1 DebugSource %15
  394. %18 = OpExtInst %void %1 DebugCompilationUnit 1 4 %17 HLSL
  395. %21 = OpExtInst %void %1 DebugTypeComposite %19 Structure %17 6 1 %18 %19 %uint_128 FlagIsProtected|FlagIsPrivate %22
  396. %24 = OpExtInst %void %1 DebugTypeBasic %23 %uint_32 Float
  397. %25 = OpExtInst %void %1 DebugTypeVector %24 4
  398. %22 = OpExtInst %void %1 DebugTypeMember %26 %25 %17 8 5 %21 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
  399. %29 = OpExtInst %void %1 DebugTypeComposite %28 Structure %17 1 1 %18 %28 %uint_128 FlagIsProtected|FlagIsPrivate %30
  400. %30 = OpExtInst %void %1 DebugTypeMember %31 %25 %17 3 5 %29 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
  401. %32 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %25 %25
  402. %34 = OpExtInst %void %1 DebugFunction %33 %32 %17 11 1 %18 %33 FlagIsProtected|FlagIsPrivate 12 %115
  403. %36 = OpExtInst %void %1 DebugLexicalBlock %17 12 1 %34
  404. %38 = OpExtInst %void %1 DebugLocalVariable %37 %25 %17 13 12 %36 FlagIsLocal
  405. %41 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %21 %29
  406. %43 = OpExtInst %void %1 DebugFunction %42 %41 %17 17 1 %18 %42 FlagIsProtected|FlagIsPrivate 18 %115
  407. %45 = OpExtInst %void %1 DebugLexicalBlock %17 18 1 %43
  408. %46 = OpExtInst %void %1 DebugLocalVariable %39 %25 %17 20 12 %45 FlagIsLocal
  409. %48 = OpExtInst %void %1 DebugLocalVariable %47 %21 %17 19 15 %45 FlagIsLocal
  410. %107 = OpExtInst %void %1 DebugInlinedAt 21 %45
  411. %MainPs = OpFunction %void None %52
  412. %53 = OpLabel
  413. %57 = OpLoad %v4float %in_var_COLOR
  414. %131 = OpExtInst %void %1 DebugScope %45
  415. OpLine %15 20 12
  416. %117 = OpExtInst %void %1 DebugValue %46 %57 %49
  417. %132 = OpExtInst %void %1 DebugScope %36 %107
  418. OpLine %15 13 19
  419. %112 = OpFMul %v4float %57 %130
  420. OpLine %15 13 12
  421. %116 = OpExtInst %void %1 DebugValue %38 %112 %49
  422. %133 = OpExtInst %void %1 DebugScope %45
  423. %128 = OpExtInst %void %1 DebugValue %48 %112 %49 %int_0
  424. %134 = OpExtInst %void %1 DebugNoScope
  425. OpStore %out_var_SV_Target0 %112
  426. OpReturn
  427. OpFunctionEnd
  428. )";
  429. std::unique_ptr<IRContext> context =
  430. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  431. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  432. DebugInfoManager manager(context.get());
  433. EXPECT_EQ(manager.GetDebugFunction(115), nullptr);
  434. }
  435. TEST(DebugInfoManager, CloneDebugInlinedAt) {
  436. const std::string text = R"(
  437. OpCapability Shader
  438. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  439. OpMemoryModel Logical GLSL450
  440. OpEntryPoint Fragment %main "main" %in_var_COLOR
  441. OpExecutionMode %main OriginUpperLeft
  442. %5 = OpString "ps.hlsl"
  443. %14 = OpString "#line 1 \"ps.hlsl\"
  444. void main(float in_var_color : COLOR) {
  445. float color = in_var_color;
  446. }
  447. "
  448. %17 = OpString "float"
  449. %21 = OpString "main"
  450. %24 = OpString "color"
  451. OpName %in_var_COLOR "in.var.COLOR"
  452. OpName %main "main"
  453. OpDecorate %in_var_COLOR Location 0
  454. %uint = OpTypeInt 32 0
  455. %uint_32 = OpConstant %uint 32
  456. %float = OpTypeFloat 32
  457. %_ptr_Input_float = OpTypePointer Input %float
  458. %void = OpTypeVoid
  459. %27 = OpTypeFunction %void
  460. %_ptr_Function_float = OpTypePointer Function %float
  461. %in_var_COLOR = OpVariable %_ptr_Input_float Input
  462. %13 = OpExtInst %void %1 DebugExpression
  463. %15 = OpExtInst %void %1 DebugSource %5 %14
  464. %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
  465. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
  466. %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
  467. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
  468. %100 = OpExtInst %void %1 DebugInlinedAt 7 %22
  469. %main = OpFunction %void None %27
  470. %28 = OpLabel
  471. %31 = OpLoad %float %in_var_COLOR
  472. OpStore %100 %31
  473. OpReturn
  474. OpFunctionEnd
  475. )";
  476. std::unique_ptr<IRContext> context =
  477. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  478. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  479. DebugInfoManager manager(context.get());
  480. EXPECT_EQ(manager.CloneDebugInlinedAt(150), nullptr);
  481. EXPECT_EQ(manager.CloneDebugInlinedAt(22), nullptr);
  482. auto* inst = manager.CloneDebugInlinedAt(100);
  483. EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex), 7);
  484. EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex), 22);
  485. EXPECT_EQ(inst->NumOperands(), kDebugInlinedAtOperandScopeIndex + 1);
  486. Instruction* before_100 = nullptr;
  487. for (auto it = context->module()->ext_inst_debuginfo_begin();
  488. it != context->module()->ext_inst_debuginfo_end(); ++it) {
  489. if (it->result_id() == 100) break;
  490. before_100 = &*it;
  491. }
  492. EXPECT_NE(inst, before_100);
  493. inst = manager.CloneDebugInlinedAt(100, manager.GetDebugInlinedAt(100));
  494. EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex), 7);
  495. EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex), 22);
  496. EXPECT_EQ(inst->NumOperands(), kDebugInlinedAtOperandScopeIndex + 1);
  497. before_100 = nullptr;
  498. for (auto it = context->module()->ext_inst_debuginfo_begin();
  499. it != context->module()->ext_inst_debuginfo_end(); ++it) {
  500. if (it->result_id() == 100) break;
  501. before_100 = &*it;
  502. }
  503. EXPECT_EQ(inst, before_100);
  504. }
  505. TEST(DebugInfoManager, KillDebugDeclares) {
  506. const std::string text = R"(
  507. OpCapability Shader
  508. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  509. OpMemoryModel Logical GLSL450
  510. OpEntryPoint Fragment %main "main" %in_var_COLOR
  511. OpExecutionMode %main OriginUpperLeft
  512. %5 = OpString "ps.hlsl"
  513. %14 = OpString "#line 1 \"ps.hlsl\"
  514. void main(float in_var_color : COLOR) {
  515. float color = in_var_color;
  516. }
  517. "
  518. %17 = OpString "float"
  519. %21 = OpString "main"
  520. %24 = OpString "color"
  521. OpName %in_var_COLOR "in.var.COLOR"
  522. OpName %main "main"
  523. OpDecorate %in_var_COLOR Location 0
  524. %uint = OpTypeInt 32 0
  525. %uint_32 = OpConstant %uint 32
  526. %float = OpTypeFloat 32
  527. %_ptr_Input_float = OpTypePointer Input %float
  528. %void = OpTypeVoid
  529. %27 = OpTypeFunction %void
  530. %_ptr_Function_float = OpTypePointer Function %float
  531. %in_var_COLOR = OpVariable %_ptr_Input_float Input
  532. %13 = OpExtInst %void %1 DebugExpression
  533. %15 = OpExtInst %void %1 DebugSource %5 %14
  534. %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
  535. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
  536. %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
  537. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
  538. %12 = OpExtInst %void %1 DebugInfoNone
  539. %25 = OpExtInst %void %1 DebugLocalVariable %24 %18 %15 1 20 %22 FlagIsLocal 0
  540. %main = OpFunction %void None %27
  541. %28 = OpLabel
  542. %100 = OpVariable %_ptr_Function_float Function
  543. %31 = OpLoad %float %in_var_COLOR
  544. OpStore %100 %31
  545. %36 = OpExtInst %void %1 DebugDeclare %25 %100 %13
  546. %37 = OpExtInst %void %1 DebugDeclare %25 %100 %13
  547. %38 = OpExtInst %void %1 DebugDeclare %25 %100 %13
  548. OpReturn
  549. OpFunctionEnd
  550. )";
  551. std::unique_ptr<IRContext> context =
  552. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  553. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  554. auto* dbg_info_mgr = context->get_debug_info_mgr();
  555. auto* def_use_mgr = context->get_def_use_mgr();
  556. EXPECT_TRUE(dbg_info_mgr->IsVariableDebugDeclared(100));
  557. EXPECT_EQ(def_use_mgr->GetDef(36)->GetOpenCL100DebugOpcode(),
  558. OpenCLDebugInfo100DebugDeclare);
  559. EXPECT_EQ(def_use_mgr->GetDef(37)->GetOpenCL100DebugOpcode(),
  560. OpenCLDebugInfo100DebugDeclare);
  561. EXPECT_EQ(def_use_mgr->GetDef(38)->GetOpenCL100DebugOpcode(),
  562. OpenCLDebugInfo100DebugDeclare);
  563. dbg_info_mgr->KillDebugDeclares(100);
  564. EXPECT_EQ(def_use_mgr->GetDef(36), nullptr);
  565. EXPECT_EQ(def_use_mgr->GetDef(37), nullptr);
  566. EXPECT_EQ(def_use_mgr->GetDef(38), nullptr);
  567. EXPECT_FALSE(dbg_info_mgr->IsVariableDebugDeclared(100));
  568. }
  569. } // namespace
  570. } // namespace analysis
  571. } // namespace opt
  572. } // namespace spvtools