ccp_pass.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. // This file implements conditional constant propagation as described in
  15. //
  16. // Constant propagation with conditional branches,
  17. // Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
  18. #include "source/opt/ccp_pass.h"
  19. #include <algorithm>
  20. #include <limits>
  21. #include "source/opt/fold.h"
  22. #include "source/opt/function.h"
  23. #include "source/opt/module.h"
  24. #include "source/opt/propagator.h"
  25. namespace spvtools {
  26. namespace opt {
  27. namespace {
  28. // This SSA id is never defined nor referenced in the IR. It is a special ID
  29. // which represents varying values. When an ID is found to have a varying
  30. // value, its entry in the |values_| table maps to kVaryingSSAId.
  31. constexpr uint32_t kVaryingSSAId = std::numeric_limits<uint32_t>::max();
  32. } // namespace
  33. bool CCPPass::IsVaryingValue(uint32_t id) const { return id == kVaryingSSAId; }
  34. SSAPropagator::PropStatus CCPPass::MarkInstructionVarying(Instruction* instr) {
  35. assert(instr->result_id() != 0 &&
  36. "Instructions with no result cannot be marked varying.");
  37. values_[instr->result_id()] = kVaryingSSAId;
  38. return SSAPropagator::kVarying;
  39. }
  40. SSAPropagator::PropStatus CCPPass::VisitPhi(Instruction* phi) {
  41. uint32_t meet_val_id = 0;
  42. // Implement the lattice meet operation. The result of this Phi instruction is
  43. // interesting only if the meet operation over arguments coming through
  44. // executable edges yields the same constant value.
  45. for (uint32_t i = 2; i < phi->NumOperands(); i += 2) {
  46. if (!propagator_->IsPhiArgExecutable(phi, i)) {
  47. // Ignore arguments coming through non-executable edges.
  48. continue;
  49. }
  50. uint32_t phi_arg_id = phi->GetSingleWordOperand(i);
  51. auto it = values_.find(phi_arg_id);
  52. if (it != values_.end()) {
  53. // We found an argument with a constant value. Apply the meet operation
  54. // with the previous arguments.
  55. if (it->second == kVaryingSSAId) {
  56. // The "constant" value is actually a placeholder for varying. Return
  57. // varying for this phi.
  58. return MarkInstructionVarying(phi);
  59. } else if (meet_val_id == 0) {
  60. // This is the first argument we find. Initialize the result to its
  61. // constant value id.
  62. meet_val_id = it->second;
  63. } else if (it->second == meet_val_id) {
  64. // The argument is the same constant value already computed. Continue
  65. // looking.
  66. continue;
  67. } else {
  68. // We either found a varying value, or another constant value different
  69. // from the previous computed meet value. This Phi will never be
  70. // constant.
  71. return MarkInstructionVarying(phi);
  72. }
  73. } else {
  74. // The incoming value has no recorded value and is therefore not
  75. // interesting. A not interesting value joined with any other value is the
  76. // other value.
  77. continue;
  78. }
  79. }
  80. // If there are no incoming executable edges, the meet ID will still be 0. In
  81. // that case, return not interesting to evaluate the Phi node again.
  82. if (meet_val_id == 0) {
  83. return SSAPropagator::kNotInteresting;
  84. }
  85. // All the operands have the same constant value represented by |meet_val_id|.
  86. // Set the Phi's result to that value and declare it interesting.
  87. values_[phi->result_id()] = meet_val_id;
  88. return SSAPropagator::kInteresting;
  89. }
  90. uint32_t CCPPass::ComputeLatticeMeet(Instruction* instr, uint32_t val2) {
  91. // Given two values val1 and val2, the meet operation in the constant
  92. // lattice uses the following rules:
  93. //
  94. // meet(val1, UNDEFINED) = val1
  95. // meet(val1, VARYING) = VARYING
  96. // meet(val1, val2) = val1 if val1 == val2
  97. // meet(val1, val2) = VARYING if val1 != val2
  98. //
  99. // When two different values meet, the result is always varying because CCP
  100. // does not allow lateral transitions in the lattice. This prevents
  101. // infinite cycles during propagation.
  102. auto val1_it = values_.find(instr->result_id());
  103. if (val1_it == values_.end()) {
  104. return val2;
  105. }
  106. uint32_t val1 = val1_it->second;
  107. if (IsVaryingValue(val1)) {
  108. return val1;
  109. } else if (IsVaryingValue(val2)) {
  110. return val2;
  111. } else if (val1 != val2) {
  112. return kVaryingSSAId;
  113. }
  114. return val2;
  115. }
  116. SSAPropagator::PropStatus CCPPass::VisitAssignment(Instruction* instr) {
  117. assert(instr->result_id() != 0 &&
  118. "Expecting an instruction that produces a result");
  119. // If this is a copy operation, and the RHS is a known constant, assign its
  120. // value to the LHS.
  121. if (instr->opcode() == spv::Op::OpCopyObject) {
  122. uint32_t rhs_id = instr->GetSingleWordInOperand(0);
  123. auto it = values_.find(rhs_id);
  124. if (it != values_.end()) {
  125. if (IsVaryingValue(it->second)) {
  126. return MarkInstructionVarying(instr);
  127. } else {
  128. uint32_t new_val = ComputeLatticeMeet(instr, it->second);
  129. values_[instr->result_id()] = new_val;
  130. return IsVaryingValue(new_val) ? SSAPropagator::kVarying
  131. : SSAPropagator::kInteresting;
  132. }
  133. }
  134. return SSAPropagator::kNotInteresting;
  135. }
  136. // Instructions with a RHS that cannot produce a constant are always varying.
  137. if (!instr->IsFoldable()) {
  138. return MarkInstructionVarying(instr);
  139. }
  140. // See if the RHS of the assignment folds into a constant value.
  141. auto map_func = [this](uint32_t id) {
  142. auto it = values_.find(id);
  143. if (it == values_.end() || IsVaryingValue(it->second)) {
  144. return id;
  145. }
  146. return it->second;
  147. };
  148. Instruction* folded_inst =
  149. context()->get_instruction_folder().FoldInstructionToConstant(instr,
  150. map_func);
  151. if (folded_inst != nullptr) {
  152. // We do not want to change the body of the function by adding new
  153. // instructions. When folding we can only generate new constants.
  154. assert((folded_inst->IsConstant() ||
  155. IsSpecConstantInst(folded_inst->opcode())) &&
  156. "CCP is only interested in constant values.");
  157. uint32_t new_val = ComputeLatticeMeet(instr, folded_inst->result_id());
  158. values_[instr->result_id()] = new_val;
  159. return IsVaryingValue(new_val) ? SSAPropagator::kVarying
  160. : SSAPropagator::kInteresting;
  161. }
  162. // Conservatively mark this instruction as varying if any input id is varying.
  163. if (!instr->WhileEachInId([this](uint32_t* op_id) {
  164. auto iter = values_.find(*op_id);
  165. if (iter != values_.end() && IsVaryingValue(iter->second)) return false;
  166. return true;
  167. })) {
  168. return MarkInstructionVarying(instr);
  169. }
  170. // If not, see if there is a least one unknown operand to the instruction. If
  171. // so, we might be able to fold it later.
  172. if (!instr->WhileEachInId([this](uint32_t* op_id) {
  173. auto it = values_.find(*op_id);
  174. if (it == values_.end()) return false;
  175. return true;
  176. })) {
  177. return SSAPropagator::kNotInteresting;
  178. }
  179. // Otherwise, we will never be able to fold this instruction, so mark it
  180. // varying.
  181. return MarkInstructionVarying(instr);
  182. }
  183. SSAPropagator::PropStatus CCPPass::VisitBranch(Instruction* instr,
  184. BasicBlock** dest_bb) const {
  185. assert(instr->IsBranch() && "Expected a branch instruction.");
  186. *dest_bb = nullptr;
  187. uint32_t dest_label = 0;
  188. if (instr->opcode() == spv::Op::OpBranch) {
  189. // An unconditional jump always goes to its unique destination.
  190. dest_label = instr->GetSingleWordInOperand(0);
  191. } else if (instr->opcode() == spv::Op::OpBranchConditional) {
  192. // For a conditional branch, determine whether the predicate selector has a
  193. // known value in |values_|. If it does, set the destination block
  194. // according to the selector's boolean value.
  195. uint32_t pred_id = instr->GetSingleWordOperand(0);
  196. auto it = values_.find(pred_id);
  197. if (it == values_.end() || IsVaryingValue(it->second)) {
  198. // The predicate has an unknown value, either branch could be taken.
  199. return SSAPropagator::kVarying;
  200. }
  201. // Get the constant value for the predicate selector from the value table.
  202. // Use it to decide which branch will be taken.
  203. uint32_t pred_val_id = it->second;
  204. const analysis::Constant* c = const_mgr_->FindDeclaredConstant(pred_val_id);
  205. assert(c && "Expected to find a constant declaration for a known value.");
  206. // Undef values should have returned as varying above.
  207. assert(c->AsBoolConstant() || c->AsNullConstant());
  208. if (c->AsNullConstant()) {
  209. dest_label = instr->GetSingleWordOperand(2u);
  210. } else {
  211. const analysis::BoolConstant* val = c->AsBoolConstant();
  212. dest_label = val->value() ? instr->GetSingleWordOperand(1)
  213. : instr->GetSingleWordOperand(2);
  214. }
  215. } else {
  216. // For an OpSwitch, extract the value taken by the switch selector and check
  217. // which of the target literals it matches. The branch associated with that
  218. // literal is the taken branch.
  219. assert(instr->opcode() == spv::Op::OpSwitch);
  220. if (instr->GetOperand(0).words.size() != 1) {
  221. // If the selector is wider than 32-bits, return varying. TODO(dnovillo):
  222. // Add support for wider constants.
  223. return SSAPropagator::kVarying;
  224. }
  225. uint32_t select_id = instr->GetSingleWordOperand(0);
  226. auto it = values_.find(select_id);
  227. if (it == values_.end() || IsVaryingValue(it->second)) {
  228. // The selector has an unknown value, any of the branches could be taken.
  229. return SSAPropagator::kVarying;
  230. }
  231. // Get the constant value for the selector from the value table. Use it to
  232. // decide which branch will be taken.
  233. uint32_t select_val_id = it->second;
  234. const analysis::Constant* c =
  235. const_mgr_->FindDeclaredConstant(select_val_id);
  236. assert(c && "Expected to find a constant declaration for a known value.");
  237. // TODO: support 64-bit integer switches.
  238. uint32_t constant_cond = 0;
  239. if (const analysis::IntConstant* val = c->AsIntConstant()) {
  240. constant_cond = val->words()[0];
  241. } else {
  242. // Undef values should have returned varying above.
  243. assert(c->AsNullConstant());
  244. constant_cond = 0;
  245. }
  246. // Start assuming that the selector will take the default value;
  247. dest_label = instr->GetSingleWordOperand(1);
  248. for (uint32_t i = 2; i < instr->NumOperands(); i += 2) {
  249. if (constant_cond == instr->GetSingleWordOperand(i)) {
  250. dest_label = instr->GetSingleWordOperand(i + 1);
  251. break;
  252. }
  253. }
  254. }
  255. assert(dest_label && "Destination label should be set at this point.");
  256. *dest_bb = context()->cfg()->block(dest_label);
  257. return SSAPropagator::kInteresting;
  258. }
  259. SSAPropagator::PropStatus CCPPass::VisitInstruction(Instruction* instr,
  260. BasicBlock** dest_bb) {
  261. *dest_bb = nullptr;
  262. if (instr->opcode() == spv::Op::OpPhi) {
  263. return VisitPhi(instr);
  264. } else if (instr->IsBranch()) {
  265. return VisitBranch(instr, dest_bb);
  266. } else if (instr->result_id()) {
  267. return VisitAssignment(instr);
  268. }
  269. return SSAPropagator::kVarying;
  270. }
  271. bool CCPPass::ReplaceValues() {
  272. // Even if we make no changes to the function's IR, propagation may have
  273. // created new constants. Even if those constants cannot be replaced in
  274. // the IR, the constant definition itself is a change. To reflect this,
  275. // we check whether the next ID to be given by the module is different than
  276. // the original bound ID. If that happens, new instructions were added to the
  277. // module during propagation.
  278. //
  279. // See https://github.com/KhronosGroup/SPIRV-Tools/issues/3636 and
  280. // https://github.com/KhronosGroup/SPIRV-Tools/issues/3991 for details.
  281. bool changed_ir = (context()->module()->IdBound() > original_id_bound_);
  282. for (const auto& it : values_) {
  283. uint32_t id = it.first;
  284. uint32_t cst_id = it.second;
  285. if (!IsVaryingValue(cst_id) && id != cst_id) {
  286. context()->KillNamesAndDecorates(id);
  287. changed_ir |= context()->ReplaceAllUsesWith(id, cst_id);
  288. }
  289. }
  290. return changed_ir;
  291. }
  292. bool CCPPass::PropagateConstants(Function* fp) {
  293. if (fp->IsDeclaration()) {
  294. return false;
  295. }
  296. // Mark function parameters as varying.
  297. fp->ForEachParam([this](const Instruction* inst) {
  298. values_[inst->result_id()] = kVaryingSSAId;
  299. });
  300. const auto visit_fn = [this](Instruction* instr, BasicBlock** dest_bb) {
  301. return VisitInstruction(instr, dest_bb);
  302. };
  303. propagator_ =
  304. std::unique_ptr<SSAPropagator>(new SSAPropagator(context(), visit_fn));
  305. if (propagator_->Run(fp)) {
  306. return ReplaceValues();
  307. }
  308. return false;
  309. }
  310. void CCPPass::Initialize() {
  311. const_mgr_ = context()->get_constant_mgr();
  312. // Populate the constant table with values from constant declarations in the
  313. // module. The values of each OpConstant declaration is the identity
  314. // assignment (i.e., each constant is its own value).
  315. for (const auto& inst : get_module()->types_values()) {
  316. // Record compile time constant ids. Treat all other global values as
  317. // varying.
  318. if (inst.IsConstant()) {
  319. values_[inst.result_id()] = inst.result_id();
  320. } else {
  321. values_[inst.result_id()] = kVaryingSSAId;
  322. }
  323. }
  324. original_id_bound_ = context()->module()->IdBound();
  325. }
  326. Pass::Status CCPPass::Process() {
  327. Initialize();
  328. // Process all entry point functions.
  329. ProcessFunction pfn = [this](Function* fp) { return PropagateConstants(fp); };
  330. bool modified = context()->ProcessReachableCallTree(pfn);
  331. return modified ? Pass::Status::SuccessWithChange
  332. : Pass::Status::SuccessWithoutChange;
  333. }
  334. } // namespace opt
  335. } // namespace spvtools