ssa_rewrite_pass.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. // Copyright (c) 2018 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. // This file implements the SSA rewriting algorithm proposed in
  15. //
  16. // Simple and Efficient Construction of Static Single Assignment Form.
  17. // Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
  18. // In: Jhala R., De Bosschere K. (eds)
  19. // Compiler Construction. CC 2013.
  20. // Lecture Notes in Computer Science, vol 7791.
  21. // Springer, Berlin, Heidelberg
  22. //
  23. // https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
  24. //
  25. // In contrast to common eager algorithms based on dominance and dominance
  26. // frontier information, this algorithm works backwards from load operations.
  27. //
  28. // When a target variable is loaded, it queries the variable's reaching
  29. // definition. If the reaching definition is unknown at the current location,
  30. // it searches backwards in the CFG, inserting Phi instructions at join points
  31. // in the CFG along the way until it finds the desired store instruction.
  32. //
  33. // The algorithm avoids repeated lookups using memoization.
  34. //
  35. // For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
  36. // this algorithm is proven to produce minimal SSA. That is, it inserts the
  37. // minimal number of Phi instructions required to ensure the SSA property, but
  38. // some Phi instructions may be dead
  39. // (https://en.wikipedia.org/wiki/Static_single_assignment_form).
  40. #include "source/opt/ssa_rewrite_pass.h"
  41. #include <memory>
  42. #include <sstream>
  43. #include "source/opcode.h"
  44. #include "source/opt/cfg.h"
  45. #include "source/opt/mem_pass.h"
  46. #include "source/opt/types.h"
  47. #include "source/util/make_unique.h"
  48. // Debug logging (0: Off, 1-N: Verbosity level). Replace this with the
  49. // implementation done for
  50. // https://github.com/KhronosGroup/SPIRV-Tools/issues/1351
  51. // #define SSA_REWRITE_DEBUGGING_LEVEL 3
  52. #ifdef SSA_REWRITE_DEBUGGING_LEVEL
  53. #include <ostream>
  54. #else
  55. #define SSA_REWRITE_DEBUGGING_LEVEL 0
  56. #endif
  57. namespace spvtools {
  58. namespace opt {
  59. namespace {
  60. const uint32_t kStoreValIdInIdx = 1;
  61. const uint32_t kVariableInitIdInIdx = 1;
  62. const uint32_t kDebugDeclareOperandVariableIdx = 5;
  63. } // namespace
  64. std::string SSARewriter::PhiCandidate::PrettyPrint(const CFG* cfg) const {
  65. std::ostringstream str;
  66. str << "%" << result_id_ << " = Phi[%" << var_id_ << ", BB %" << bb_->id()
  67. << "](";
  68. if (phi_args_.size() > 0) {
  69. uint32_t arg_ix = 0;
  70. for (uint32_t pred_label : cfg->preds(bb_->id())) {
  71. uint32_t arg_id = phi_args_[arg_ix++];
  72. str << "[%" << arg_id << ", bb(%" << pred_label << ")] ";
  73. }
  74. }
  75. str << ")";
  76. if (copy_of_ != 0) {
  77. str << " [COPY OF " << copy_of_ << "]";
  78. }
  79. str << ((is_complete_) ? " [COMPLETE]" : " [INCOMPLETE]");
  80. return str.str();
  81. }
  82. SSARewriter::PhiCandidate& SSARewriter::CreatePhiCandidate(uint32_t var_id,
  83. BasicBlock* bb) {
  84. // TODO(1841): Handle id overflow.
  85. uint32_t phi_result_id = pass_->context()->TakeNextId();
  86. auto result = phi_candidates_.emplace(
  87. phi_result_id, PhiCandidate(var_id, phi_result_id, bb));
  88. PhiCandidate& phi_candidate = result.first->second;
  89. return phi_candidate;
  90. }
  91. void SSARewriter::ReplacePhiUsersWith(const PhiCandidate& phi_to_remove,
  92. uint32_t repl_id) {
  93. for (uint32_t user_id : phi_to_remove.users()) {
  94. PhiCandidate* user_phi = GetPhiCandidate(user_id);
  95. BasicBlock* bb = pass_->context()->get_instr_block(user_id);
  96. if (user_phi) {
  97. // If the user is a Phi candidate, replace all arguments that refer to
  98. // |phi_to_remove.result_id()| with |repl_id|.
  99. for (uint32_t& arg : user_phi->phi_args()) {
  100. if (arg == phi_to_remove.result_id()) {
  101. arg = repl_id;
  102. }
  103. }
  104. } else if (bb->id() == user_id) {
  105. // The phi candidate is the definition of the variable at basic block
  106. // |bb|. We must change this to the replacement.
  107. WriteVariable(phi_to_remove.var_id(), bb, repl_id);
  108. } else {
  109. // For regular loads, traverse the |load_replacement_| table looking for
  110. // instances of |phi_to_remove|.
  111. for (auto& it : load_replacement_) {
  112. if (it.second == phi_to_remove.result_id()) {
  113. it.second = repl_id;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. uint32_t SSARewriter::TryRemoveTrivialPhi(PhiCandidate* phi_candidate) {
  120. uint32_t same_id = 0;
  121. for (uint32_t arg_id : phi_candidate->phi_args()) {
  122. if (arg_id == same_id || arg_id == phi_candidate->result_id()) {
  123. // This is a self-reference operand or a reference to the same value ID.
  124. continue;
  125. }
  126. if (same_id != 0) {
  127. // This Phi candidate merges at least two values. Therefore, it is not
  128. // trivial.
  129. assert(phi_candidate->copy_of() == 0 &&
  130. "Phi candidate transitioning from copy to non-copy.");
  131. return phi_candidate->result_id();
  132. }
  133. same_id = arg_id;
  134. }
  135. // The previous logic has determined that this Phi candidate |phi_candidate|
  136. // is trivial. It is essentially the copy operation phi_candidate->phi_result
  137. // = Phi(same, same, same, ...). Since it is not necessary, we can re-route
  138. // all the users of |phi_candidate->phi_result| to all its users, and remove
  139. // |phi_candidate|.
  140. // Mark the Phi candidate as a trivial copy of |same_id|, so it won't be
  141. // generated.
  142. phi_candidate->MarkCopyOf(same_id);
  143. assert(same_id != 0 && "Completed Phis cannot have %0 in their arguments");
  144. // Since |phi_candidate| always produces |same_id|, replace all the users of
  145. // |phi_candidate| with |same_id|.
  146. ReplacePhiUsersWith(*phi_candidate, same_id);
  147. return same_id;
  148. }
  149. uint32_t SSARewriter::AddPhiOperands(PhiCandidate* phi_candidate) {
  150. assert(phi_candidate->phi_args().size() == 0 &&
  151. "Phi candidate already has arguments");
  152. bool found_0_arg = false;
  153. for (uint32_t pred : pass_->cfg()->preds(phi_candidate->bb()->id())) {
  154. BasicBlock* pred_bb = pass_->cfg()->block(pred);
  155. // If |pred_bb| is not sealed, use %0 to indicate that
  156. // |phi_candidate| needs to be completed after the whole CFG has
  157. // been processed.
  158. //
  159. // Note that we cannot call GetReachingDef() in these cases
  160. // because this would generate an empty Phi candidate in
  161. // |pred_bb|. When |pred_bb| is later processed, a new definition
  162. // for |phi_candidate->var_id_| will be lost because
  163. // |phi_candidate| will still be reached by the empty Phi.
  164. //
  165. // Consider:
  166. //
  167. // BB %23:
  168. // %38 = Phi[%i](%int_0[%1], %39[%25])
  169. //
  170. // ...
  171. //
  172. // BB %25: [Starts unsealed]
  173. // %39 = Phi[%i]()
  174. // %34 = ...
  175. // OpStore %i %34 -> Currdef(%i) at %25 is %34
  176. // OpBranch %23
  177. //
  178. // When we first create the Phi in %38, we add an operandless Phi in
  179. // %39 to hold the unknown reaching def for %i.
  180. //
  181. // But then, when we go to complete %39 at the end. The reaching def
  182. // for %i in %25's predecessor is %38 itself. So we miss the fact
  183. // that %25 has a def for %i that should be used.
  184. //
  185. // By making the argument %0, we make |phi_candidate| incomplete,
  186. // which will cause it to be completed after the whole CFG has
  187. // been scanned.
  188. uint32_t arg_id = IsBlockSealed(pred_bb)
  189. ? GetReachingDef(phi_candidate->var_id(), pred_bb)
  190. : 0;
  191. phi_candidate->phi_args().push_back(arg_id);
  192. if (arg_id == 0) {
  193. found_0_arg = true;
  194. } else {
  195. // If this argument is another Phi candidate, add |phi_candidate| to the
  196. // list of users for the defining Phi.
  197. PhiCandidate* defining_phi = GetPhiCandidate(arg_id);
  198. if (defining_phi && defining_phi != phi_candidate) {
  199. defining_phi->AddUser(phi_candidate->result_id());
  200. }
  201. }
  202. }
  203. // If we could not fill-in all the arguments of this Phi, mark it incomplete
  204. // so it gets completed after the whole CFG has been processed.
  205. if (found_0_arg) {
  206. phi_candidate->MarkIncomplete();
  207. incomplete_phis_.push(phi_candidate);
  208. return phi_candidate->result_id();
  209. }
  210. // Try to remove |phi_candidate|, if it's trivial.
  211. uint32_t repl_id = TryRemoveTrivialPhi(phi_candidate);
  212. if (repl_id == phi_candidate->result_id()) {
  213. // |phi_candidate| is complete and not trivial. Add it to the
  214. // list of Phi candidates to generate.
  215. phi_candidate->MarkComplete();
  216. phis_to_generate_.push_back(phi_candidate);
  217. }
  218. return repl_id;
  219. }
  220. uint32_t SSARewriter::GetValueAtBlock(uint32_t var_id, BasicBlock* bb) {
  221. assert(bb != nullptr);
  222. const auto& bb_it = defs_at_block_.find(bb);
  223. if (bb_it != defs_at_block_.end()) {
  224. const auto& current_defs = bb_it->second;
  225. const auto& var_it = current_defs.find(var_id);
  226. if (var_it != current_defs.end()) {
  227. return var_it->second;
  228. }
  229. }
  230. return 0;
  231. }
  232. uint32_t SSARewriter::GetReachingDef(uint32_t var_id, BasicBlock* bb) {
  233. // If |var_id| has a definition in |bb|, return it.
  234. uint32_t val_id = GetValueAtBlock(var_id, bb);
  235. if (val_id != 0) return val_id;
  236. // Otherwise, look up the value for |var_id| in |bb|'s predecessors.
  237. auto& predecessors = pass_->cfg()->preds(bb->id());
  238. if (predecessors.size() == 1) {
  239. // If |bb| has exactly one predecessor, we look for |var_id|'s definition
  240. // there.
  241. val_id = GetReachingDef(var_id, pass_->cfg()->block(predecessors[0]));
  242. } else if (predecessors.size() > 1) {
  243. // If there is more than one predecessor, this is a join block which may
  244. // require a Phi instruction. This will act as |var_id|'s current
  245. // definition to break potential cycles.
  246. PhiCandidate& phi_candidate = CreatePhiCandidate(var_id, bb);
  247. // Set the value for |bb| to avoid an infinite recursion.
  248. WriteVariable(var_id, bb, phi_candidate.result_id());
  249. val_id = AddPhiOperands(&phi_candidate);
  250. }
  251. // If we could not find a store for this variable in the path from the root
  252. // of the CFG, the variable is not defined, so we use undef.
  253. if (val_id == 0) {
  254. val_id = pass_->GetUndefVal(var_id);
  255. if (val_id == 0) {
  256. return 0;
  257. }
  258. }
  259. WriteVariable(var_id, bb, val_id);
  260. return val_id;
  261. }
  262. void SSARewriter::SealBlock(BasicBlock* bb) {
  263. auto result = sealed_blocks_.insert(bb);
  264. (void)result;
  265. assert(result.second == true &&
  266. "Tried to seal the same basic block more than once.");
  267. }
  268. void SSARewriter::ProcessStore(Instruction* inst, BasicBlock* bb) {
  269. auto opcode = inst->opcode();
  270. assert((opcode == SpvOpStore || opcode == SpvOpVariable) &&
  271. "Expecting a store or a variable definition instruction.");
  272. uint32_t var_id = 0;
  273. uint32_t val_id = 0;
  274. if (opcode == SpvOpStore) {
  275. (void)pass_->GetPtr(inst, &var_id);
  276. val_id = inst->GetSingleWordInOperand(kStoreValIdInIdx);
  277. } else if (inst->NumInOperands() >= 2) {
  278. var_id = inst->result_id();
  279. val_id = inst->GetSingleWordInOperand(kVariableInitIdInIdx);
  280. }
  281. if (pass_->IsTargetVar(var_id)) {
  282. WriteVariable(var_id, bb, val_id);
  283. pass_->context()->get_debug_info_mgr()->AddDebugValueIfVarDeclIsVisible(
  284. inst, var_id, val_id, inst, &decls_invisible_to_value_assignment_);
  285. #if SSA_REWRITE_DEBUGGING_LEVEL > 1
  286. std::cerr << "\tFound store '%" << var_id << " = %" << val_id << "': "
  287. << inst->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
  288. << "\n";
  289. #endif
  290. }
  291. }
  292. bool SSARewriter::ProcessLoad(Instruction* inst, BasicBlock* bb) {
  293. // Get the pointer that we are using to load from.
  294. uint32_t var_id = 0;
  295. (void)pass_->GetPtr(inst, &var_id);
  296. // Get the immediate reaching definition for |var_id|.
  297. //
  298. // In the presence of variable pointers, the reaching definition may be
  299. // another pointer. For example, the following fragment:
  300. //
  301. // %2 = OpVariable %_ptr_Input_float Input
  302. // %11 = OpVariable %_ptr_Function__ptr_Input_float Function
  303. // OpStore %11 %2
  304. // %12 = OpLoad %_ptr_Input_float %11
  305. // %13 = OpLoad %float %12
  306. //
  307. // corresponds to the pseudo-code:
  308. //
  309. // layout(location = 0) in flat float *%2
  310. // float %13;
  311. // float *%12;
  312. // float **%11;
  313. // *%11 = %2;
  314. // %12 = *%11;
  315. // %13 = *%12;
  316. //
  317. // which ultimately, should correspond to:
  318. //
  319. // %13 = *%2;
  320. //
  321. // During rewriting, the pointer %12 is found to be replaceable by %2 (i.e.,
  322. // load_replacement_[12] is 2). However, when processing the load
  323. // %13 = *%12, the type of %12's reaching definition is another float
  324. // pointer (%2), instead of a float value.
  325. //
  326. // When this happens, we need to continue looking up the reaching definition
  327. // chain until we get to a float value or a non-target var (i.e. a variable
  328. // that cannot be SSA replaced, like %2 in this case since it is a function
  329. // argument).
  330. analysis::DefUseManager* def_use_mgr = pass_->context()->get_def_use_mgr();
  331. analysis::TypeManager* type_mgr = pass_->context()->get_type_mgr();
  332. analysis::Type* load_type = type_mgr->GetType(inst->type_id());
  333. uint32_t val_id = 0;
  334. bool found_reaching_def = false;
  335. while (!found_reaching_def) {
  336. if (!pass_->IsTargetVar(var_id)) {
  337. // If the variable we are loading from is not an SSA target (globals,
  338. // function parameters), do nothing.
  339. return true;
  340. }
  341. val_id = GetReachingDef(var_id, bb);
  342. if (val_id == 0) {
  343. return false;
  344. }
  345. // If the reaching definition is a pointer type different than the type of
  346. // the instruction we are analyzing, then it must be a reference to another
  347. // pointer (otherwise, this would be invalid SPIRV). We continue
  348. // de-referencing it by making |val_id| be |var_id|.
  349. //
  350. // NOTE: if there is no reaching definition instruction, it means |val_id|
  351. // is an undef.
  352. Instruction* reaching_def_inst = def_use_mgr->GetDef(val_id);
  353. if (reaching_def_inst &&
  354. !type_mgr->GetType(reaching_def_inst->type_id())->IsSame(load_type)) {
  355. var_id = val_id;
  356. } else {
  357. found_reaching_def = true;
  358. }
  359. }
  360. // Schedule a replacement for the result of this load instruction with
  361. // |val_id|. After all the rewriting decisions are made, every use of
  362. // this load will be replaced with |val_id|.
  363. uint32_t load_id = inst->result_id();
  364. assert(load_replacement_.count(load_id) == 0);
  365. load_replacement_[load_id] = val_id;
  366. PhiCandidate* defining_phi = GetPhiCandidate(val_id);
  367. if (defining_phi) {
  368. defining_phi->AddUser(load_id);
  369. }
  370. #if SSA_REWRITE_DEBUGGING_LEVEL > 1
  371. std::cerr << "\tFound load: "
  372. << inst->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
  373. << " (replacement for %" << load_id << " is %" << val_id << ")\n";
  374. #endif
  375. return true;
  376. }
  377. void SSARewriter::PrintPhiCandidates() const {
  378. std::cerr << "\nPhi candidates:\n";
  379. for (const auto& phi_it : phi_candidates_) {
  380. std::cerr << "\tBB %" << phi_it.second.bb()->id() << ": "
  381. << phi_it.second.PrettyPrint(pass_->cfg()) << "\n";
  382. }
  383. std::cerr << "\n";
  384. }
  385. void SSARewriter::PrintReplacementTable() const {
  386. std::cerr << "\nLoad replacement table\n";
  387. for (const auto& it : load_replacement_) {
  388. std::cerr << "\t%" << it.first << " -> %" << it.second << "\n";
  389. }
  390. std::cerr << "\n";
  391. }
  392. bool SSARewriter::GenerateSSAReplacements(BasicBlock* bb) {
  393. #if SSA_REWRITE_DEBUGGING_LEVEL > 1
  394. std::cerr << "Generating SSA replacements for block: " << bb->id() << "\n";
  395. std::cerr << bb->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
  396. << "\n";
  397. #endif
  398. for (auto& inst : *bb) {
  399. auto opcode = inst.opcode();
  400. if (opcode == SpvOpStore || opcode == SpvOpVariable) {
  401. ProcessStore(&inst, bb);
  402. } else if (inst.opcode() == SpvOpLoad) {
  403. if (!ProcessLoad(&inst, bb)) {
  404. return false;
  405. }
  406. }
  407. }
  408. // Seal |bb|. This means that all the stores in it have been scanned and
  409. // it's ready to feed them into its successors.
  410. SealBlock(bb);
  411. #if SSA_REWRITE_DEBUGGING_LEVEL > 1
  412. PrintPhiCandidates();
  413. PrintReplacementTable();
  414. std::cerr << "\n\n";
  415. #endif
  416. return true;
  417. }
  418. uint32_t SSARewriter::GetReplacement(std::pair<uint32_t, uint32_t> repl) {
  419. uint32_t val_id = repl.second;
  420. auto it = load_replacement_.find(val_id);
  421. while (it != load_replacement_.end()) {
  422. val_id = it->second;
  423. it = load_replacement_.find(val_id);
  424. }
  425. return val_id;
  426. }
  427. uint32_t SSARewriter::GetPhiArgument(const PhiCandidate* phi_candidate,
  428. uint32_t ix) {
  429. assert(phi_candidate->IsReady() &&
  430. "Tried to get the final argument from an incomplete/trivial Phi");
  431. uint32_t arg_id = phi_candidate->phi_args()[ix];
  432. while (arg_id != 0) {
  433. PhiCandidate* phi_user = GetPhiCandidate(arg_id);
  434. if (phi_user == nullptr || phi_user->IsReady()) {
  435. // If the argument is not a Phi or it's a Phi candidate ready to be
  436. // emitted, return it.
  437. return arg_id;
  438. }
  439. arg_id = phi_user->copy_of();
  440. }
  441. assert(false &&
  442. "No Phi candidates in the copy-of chain are ready to be generated");
  443. return 0;
  444. }
  445. bool SSARewriter::ApplyReplacements() {
  446. bool modified = false;
  447. #if SSA_REWRITE_DEBUGGING_LEVEL > 2
  448. std::cerr << "\n\nApplying replacement decisions to IR\n\n";
  449. PrintPhiCandidates();
  450. PrintReplacementTable();
  451. std::cerr << "\n\n";
  452. #endif
  453. // Add Phi instructions from completed Phi candidates.
  454. std::vector<Instruction*> generated_phis;
  455. for (const PhiCandidate* phi_candidate : phis_to_generate_) {
  456. #if SSA_REWRITE_DEBUGGING_LEVEL > 2
  457. std::cerr << "Phi candidate: " << phi_candidate->PrettyPrint(pass_->cfg())
  458. << "\n";
  459. #endif
  460. assert(phi_candidate->is_complete() &&
  461. "Tried to instantiate a Phi instruction from an incomplete Phi "
  462. "candidate");
  463. auto* local_var = pass_->get_def_use_mgr()->GetDef(phi_candidate->var_id());
  464. // Build the vector of operands for the new OpPhi instruction.
  465. uint32_t type_id = pass_->GetPointeeTypeId(local_var);
  466. std::vector<Operand> phi_operands;
  467. uint32_t arg_ix = 0;
  468. std::unordered_map<uint32_t, uint32_t> already_seen;
  469. for (uint32_t pred_label : pass_->cfg()->preds(phi_candidate->bb()->id())) {
  470. uint32_t op_val_id = GetPhiArgument(phi_candidate, arg_ix++);
  471. if (already_seen.count(pred_label) == 0) {
  472. phi_operands.push_back(
  473. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {op_val_id}});
  474. phi_operands.push_back(
  475. {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {pred_label}});
  476. already_seen[pred_label] = op_val_id;
  477. } else {
  478. // It is possible that there are two edges from the same parent block.
  479. // Since the OpPhi can have only one entry for each parent, we have to
  480. // make sure the two edges are consistent with each other.
  481. assert(already_seen[pred_label] == op_val_id &&
  482. "Inconsistent value for duplicate edges.");
  483. }
  484. }
  485. // Generate a new OpPhi instruction and insert it in its basic
  486. // block.
  487. std::unique_ptr<Instruction> phi_inst(
  488. new Instruction(pass_->context(), SpvOpPhi, type_id,
  489. phi_candidate->result_id(), phi_operands));
  490. generated_phis.push_back(phi_inst.get());
  491. pass_->get_def_use_mgr()->AnalyzeInstDef(&*phi_inst);
  492. pass_->context()->set_instr_block(&*phi_inst, phi_candidate->bb());
  493. auto insert_it = phi_candidate->bb()->begin();
  494. insert_it = insert_it.InsertBefore(std::move(phi_inst));
  495. pass_->context()->get_decoration_mgr()->CloneDecorations(
  496. phi_candidate->var_id(), phi_candidate->result_id(),
  497. {SpvDecorationRelaxedPrecision});
  498. // Add DebugValue for the new OpPhi instruction.
  499. insert_it->SetDebugScope(local_var->GetDebugScope());
  500. pass_->context()->get_debug_info_mgr()->AddDebugValueIfVarDeclIsVisible(
  501. &*insert_it, phi_candidate->var_id(), phi_candidate->result_id(),
  502. &*insert_it, &decls_invisible_to_value_assignment_);
  503. modified = true;
  504. }
  505. // Scan uses for all inserted Phi instructions. Do this separately from the
  506. // registration of the Phi instruction itself to avoid trying to analyze
  507. // uses of Phi instructions that have not been registered yet.
  508. for (Instruction* phi_inst : generated_phis) {
  509. pass_->get_def_use_mgr()->AnalyzeInstUse(&*phi_inst);
  510. }
  511. #if SSA_REWRITE_DEBUGGING_LEVEL > 1
  512. std::cerr << "\n\nReplacing the result of load instructions with the "
  513. "corresponding SSA id\n\n";
  514. #endif
  515. // Apply replacements from the load replacement table.
  516. for (auto& repl : load_replacement_) {
  517. uint32_t load_id = repl.first;
  518. uint32_t val_id = GetReplacement(repl);
  519. Instruction* load_inst =
  520. pass_->context()->get_def_use_mgr()->GetDef(load_id);
  521. #if SSA_REWRITE_DEBUGGING_LEVEL > 2
  522. std::cerr << "\t"
  523. << load_inst->PrettyPrint(
  524. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
  525. << " (%" << load_id << " -> %" << val_id << ")\n";
  526. #endif
  527. // Remove the load instruction and replace all the uses of this load's
  528. // result with |val_id|. Kill any names or decorates using the load's
  529. // result before replacing to prevent incorrect replacement in those
  530. // instructions.
  531. pass_->context()->KillNamesAndDecorates(load_id);
  532. pass_->context()->ReplaceAllUsesWith(load_id, val_id);
  533. pass_->context()->KillInst(load_inst);
  534. modified = true;
  535. }
  536. return modified;
  537. }
  538. void SSARewriter::FinalizePhiCandidate(PhiCandidate* phi_candidate) {
  539. assert(phi_candidate->phi_args().size() > 0 &&
  540. "Phi candidate should have arguments");
  541. uint32_t ix = 0;
  542. for (uint32_t pred : pass_->cfg()->preds(phi_candidate->bb()->id())) {
  543. BasicBlock* pred_bb = pass_->cfg()->block(pred);
  544. uint32_t& arg_id = phi_candidate->phi_args()[ix++];
  545. if (arg_id == 0) {
  546. // If |pred_bb| is still not sealed, it means it's unreachable. In this
  547. // case, we just use Undef as an argument.
  548. arg_id = IsBlockSealed(pred_bb)
  549. ? GetReachingDef(phi_candidate->var_id(), pred_bb)
  550. : pass_->GetUndefVal(phi_candidate->var_id());
  551. }
  552. }
  553. // This candidate is now completed.
  554. phi_candidate->MarkComplete();
  555. // If |phi_candidate| is not trivial, add it to the list of Phis to
  556. // generate.
  557. if (TryRemoveTrivialPhi(phi_candidate) == phi_candidate->result_id()) {
  558. // If we could not remove |phi_candidate|, it means that it is complete
  559. // and not trivial. Add it to the list of Phis to generate.
  560. assert(!phi_candidate->copy_of() && "A completed Phi cannot be trivial.");
  561. phis_to_generate_.push_back(phi_candidate);
  562. }
  563. }
  564. void SSARewriter::FinalizePhiCandidates() {
  565. #if SSA_REWRITE_DEBUGGING_LEVEL > 1
  566. std::cerr << "Finalizing Phi candidates:\n\n";
  567. PrintPhiCandidates();
  568. std::cerr << "\n";
  569. #endif
  570. // Now, complete the collected candidates.
  571. while (incomplete_phis_.size() > 0) {
  572. PhiCandidate* phi_candidate = incomplete_phis_.front();
  573. incomplete_phis_.pop();
  574. FinalizePhiCandidate(phi_candidate);
  575. }
  576. }
  577. Pass::Status SSARewriter::AddDebugValuesForInvisibleDebugDecls(Function* fp) {
  578. // For the cases the value assignment is invisible to DebugDeclare e.g.,
  579. // the argument passing for an inlined function.
  580. //
  581. // Before inlining foo(int x):
  582. // a = 3;
  583. // foo(3);
  584. // After inlining:
  585. // a = 3;
  586. // foo and x disappeared but we want to specify "DebugValue: %x = %int_3".
  587. //
  588. // We want to specify the value for the variable using |defs_at_block_[bb]|,
  589. // where |bb| is the basic block contains the decl.
  590. DominatorAnalysis* dom_tree = pass_->context()->GetDominatorAnalysis(fp);
  591. Pass::Status status = Pass::Status::SuccessWithoutChange;
  592. for (auto* decl : decls_invisible_to_value_assignment_) {
  593. uint32_t var_id =
  594. decl->GetSingleWordOperand(kDebugDeclareOperandVariableIdx);
  595. auto* var = pass_->get_def_use_mgr()->GetDef(var_id);
  596. if (var->opcode() == SpvOpFunctionParameter) continue;
  597. auto* bb = pass_->context()->get_instr_block(decl);
  598. uint32_t value_id = GetValueAtBlock(var_id, bb);
  599. Instruction* value = nullptr;
  600. if (value_id) value = pass_->get_def_use_mgr()->GetDef(value_id);
  601. // If |value| is defined before the function body, it dominates |decl|.
  602. // If |value| dominates |decl|, we can set it as DebugValue.
  603. if (value && (pass_->context()->get_instr_block(value) == nullptr ||
  604. dom_tree->Dominates(value, decl))) {
  605. if (pass_->context()->get_debug_info_mgr()->AddDebugValueForDecl(
  606. decl, value->result_id(), decl, value) == nullptr) {
  607. return Pass::Status::Failure;
  608. }
  609. } else {
  610. // If |value| in the same basic block does not dominate |decl|, we can
  611. // assign the value in the immediate dominator.
  612. value_id = GetValueAtBlock(var_id, dom_tree->ImmediateDominator(bb));
  613. if (value_id) value = pass_->get_def_use_mgr()->GetDef(value_id);
  614. if (value_id &&
  615. pass_->context()->get_debug_info_mgr()->AddDebugValueForDecl(
  616. decl, value_id, decl, value) == nullptr) {
  617. return Pass::Status::Failure;
  618. }
  619. }
  620. // DebugDeclares of target variables will be removed by
  621. // SSARewritePass::Process().
  622. if (!pass_->IsTargetVar(var_id)) {
  623. pass_->context()->get_debug_info_mgr()->KillDebugDeclares(var_id);
  624. }
  625. status = Pass::Status::SuccessWithChange;
  626. }
  627. return status;
  628. }
  629. Pass::Status SSARewriter::RewriteFunctionIntoSSA(Function* fp) {
  630. #if SSA_REWRITE_DEBUGGING_LEVEL > 0
  631. std::cerr << "Function before SSA rewrite:\n"
  632. << fp->PrettyPrint(0) << "\n\n\n";
  633. #endif
  634. // Collect variables that can be converted into SSA IDs.
  635. pass_->CollectTargetVars(fp);
  636. // Generate all the SSA replacements and Phi candidates. This will
  637. // generate incomplete and trivial Phis.
  638. bool succeeded = pass_->cfg()->WhileEachBlockInReversePostOrder(
  639. fp->entry().get(), [this](BasicBlock* bb) {
  640. if (!GenerateSSAReplacements(bb)) {
  641. return false;
  642. }
  643. return true;
  644. });
  645. if (!succeeded) {
  646. return Pass::Status::Failure;
  647. }
  648. // Remove trivial Phis and add arguments to incomplete Phis.
  649. FinalizePhiCandidates();
  650. // Finally, apply all the replacements in the IR.
  651. bool modified = ApplyReplacements();
  652. auto status = AddDebugValuesForInvisibleDebugDecls(fp);
  653. if (status == Pass::Status::SuccessWithChange ||
  654. status == Pass::Status::Failure) {
  655. return status;
  656. }
  657. #if SSA_REWRITE_DEBUGGING_LEVEL > 0
  658. std::cerr << "\n\n\nFunction after SSA rewrite:\n"
  659. << fp->PrettyPrint(0) << "\n";
  660. #endif
  661. return modified ? Pass::Status::SuccessWithChange
  662. : Pass::Status::SuccessWithoutChange;
  663. }
  664. Pass::Status SSARewritePass::Process() {
  665. Status status = Status::SuccessWithoutChange;
  666. for (auto& fn : *get_module()) {
  667. if (fn.IsDeclaration()) {
  668. continue;
  669. }
  670. status =
  671. CombineStatus(status, SSARewriter(this).RewriteFunctionIntoSSA(&fn));
  672. // Kill DebugDeclares for target variables.
  673. for (auto var_id : seen_target_vars_) {
  674. context()->get_debug_info_mgr()->KillDebugDeclares(var_id);
  675. }
  676. if (status == Status::Failure) {
  677. break;
  678. }
  679. }
  680. return status;
  681. }
  682. } // namespace opt
  683. } // namespace spvtools