Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 1 | // Copyright (c) 2017 The Khronos Group Inc. |
| 2 | // Copyright (c) 2017 Valve Corporation |
| 3 | // Copyright (c) 2017 LunarG Inc. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | #include "inline_pass.h" |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 18 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 19 | #include "cfa.h" |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 20 | |
| 21 | // Indices of operands in SPIR-V instructions |
| 22 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 23 | static const int kSpvFunctionCallFunctionId = 2; |
| 24 | static const int kSpvFunctionCallArgumentId = 3; |
| 25 | static const int kSpvReturnValueId = 0; |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 26 | static const int kSpvLoopMergeMergeBlockId = 0; |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 27 | static const int kSpvLoopMergeContinueTargetIdInIdx = 1; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 28 | |
| 29 | namespace spvtools { |
| 30 | namespace opt { |
| 31 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 32 | uint32_t InlinePass::AddPointerToType(uint32_t type_id, |
| 33 | SpvStorageClass storage_class) { |
| 34 | uint32_t resultId = TakeNextId(); |
| 35 | std::unique_ptr<ir::Instruction> type_inst(new ir::Instruction( |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 36 | context(), SpvOpTypePointer, 0, resultId, |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 37 | {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS, |
| 38 | {uint32_t(storage_class)}}, |
| 39 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {type_id}}})); |
Steven Perron | 476cae6 | 2017-10-30 11:13:24 -0400 | [diff] [blame] | 40 | context()->AddType(std::move(type_inst)); |
Alan Baker | 6169085 | 2017-12-08 15:33:19 -0500 | [diff] [blame] | 41 | analysis::Type* pointeeTy; |
| 42 | std::unique_ptr<analysis::Pointer> pointerTy; |
| 43 | std::tie(pointeeTy, pointerTy) = |
| 44 | context()->get_type_mgr()->GetTypeAndPointerType(type_id, |
| 45 | SpvStorageClassFunction); |
| 46 | context()->get_type_mgr()->RegisterType(resultId, *pointerTy); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 47 | return resultId; |
| 48 | } |
| 49 | |
| 50 | void InlinePass::AddBranch(uint32_t label_id, |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 51 | std::unique_ptr<ir::BasicBlock>* block_ptr) { |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 52 | std::unique_ptr<ir::Instruction> newBranch(new ir::Instruction( |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 53 | context(), SpvOpBranch, 0, 0, |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 54 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {label_id}}})); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 55 | (*block_ptr)->AddInstruction(std::move(newBranch)); |
| 56 | } |
| 57 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 58 | void InlinePass::AddBranchCond(uint32_t cond_id, uint32_t true_id, |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 59 | uint32_t false_id, |
| 60 | std::unique_ptr<ir::BasicBlock>* block_ptr) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 61 | std::unique_ptr<ir::Instruction> newBranch(new ir::Instruction( |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 62 | context(), SpvOpBranchConditional, 0, 0, |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 63 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {cond_id}}, |
| 64 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {true_id}}, |
| 65 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {false_id}}})); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 66 | (*block_ptr)->AddInstruction(std::move(newBranch)); |
| 67 | } |
| 68 | |
| 69 | void InlinePass::AddLoopMerge(uint32_t merge_id, uint32_t continue_id, |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 70 | std::unique_ptr<ir::BasicBlock>* block_ptr) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 71 | std::unique_ptr<ir::Instruction> newLoopMerge(new ir::Instruction( |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 72 | context(), SpvOpLoopMerge, 0, 0, |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 73 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {merge_id}}, |
| 74 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {continue_id}}, |
| 75 | {spv_operand_type_t::SPV_OPERAND_TYPE_LOOP_CONTROL, {0}}})); |
| 76 | (*block_ptr)->AddInstruction(std::move(newLoopMerge)); |
| 77 | } |
| 78 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 79 | void InlinePass::AddStore(uint32_t ptr_id, uint32_t val_id, |
| 80 | std::unique_ptr<ir::BasicBlock>* block_ptr) { |
| 81 | std::unique_ptr<ir::Instruction> newStore(new ir::Instruction( |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 82 | context(), SpvOpStore, 0, 0, |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 83 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}}, |
| 84 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {val_id}}})); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 85 | (*block_ptr)->AddInstruction(std::move(newStore)); |
| 86 | } |
| 87 | |
| 88 | void InlinePass::AddLoad(uint32_t type_id, uint32_t resultId, uint32_t ptr_id, |
| 89 | std::unique_ptr<ir::BasicBlock>* block_ptr) { |
| 90 | std::unique_ptr<ir::Instruction> newLoad(new ir::Instruction( |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 91 | context(), SpvOpLoad, type_id, resultId, |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 92 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}}})); |
| 93 | (*block_ptr)->AddInstruction(std::move(newLoad)); |
| 94 | } |
| 95 | |
| 96 | std::unique_ptr<ir::Instruction> InlinePass::NewLabel(uint32_t label_id) { |
| 97 | std::unique_ptr<ir::Instruction> newLabel( |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 98 | new ir::Instruction(context(), SpvOpLabel, 0, label_id, {})); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 99 | return newLabel; |
| 100 | } |
| 101 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 102 | uint32_t InlinePass::GetFalseId() { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 103 | if (false_id_ != 0) return false_id_; |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 104 | false_id_ = get_module()->GetGlobalValue(SpvOpConstantFalse); |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 105 | if (false_id_ != 0) return false_id_; |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 106 | uint32_t boolId = get_module()->GetGlobalValue(SpvOpTypeBool); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 107 | if (boolId == 0) { |
| 108 | boolId = TakeNextId(); |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 109 | get_module()->AddGlobalValue(SpvOpTypeBool, boolId, 0); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 110 | } |
| 111 | false_id_ = TakeNextId(); |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 112 | get_module()->AddGlobalValue(SpvOpConstantFalse, false_id_, boolId); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 113 | return false_id_; |
| 114 | } |
| 115 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 116 | void InlinePass::MapParams( |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 117 | ir::Function* calleeFn, ir::BasicBlock::iterator call_inst_itr, |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 118 | std::unordered_map<uint32_t, uint32_t>* callee2caller) { |
| 119 | int param_idx = 0; |
| 120 | calleeFn->ForEachParam( |
| 121 | [&call_inst_itr, ¶m_idx, &callee2caller](const ir::Instruction* cpi) { |
| 122 | const uint32_t pid = cpi->result_id(); |
| 123 | (*callee2caller)[pid] = call_inst_itr->GetSingleWordOperand( |
| 124 | kSpvFunctionCallArgumentId + param_idx); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 125 | ++param_idx; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 126 | }); |
| 127 | } |
| 128 | |
| 129 | void InlinePass::CloneAndMapLocals( |
| 130 | ir::Function* calleeFn, |
| 131 | std::vector<std::unique_ptr<ir::Instruction>>* new_vars, |
| 132 | std::unordered_map<uint32_t, uint32_t>* callee2caller) { |
| 133 | auto callee_block_itr = calleeFn->begin(); |
| 134 | auto callee_var_itr = callee_block_itr->begin(); |
| 135 | while (callee_var_itr->opcode() == SpvOp::SpvOpVariable) { |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 136 | std::unique_ptr<ir::Instruction> var_inst( |
| 137 | callee_var_itr->Clone(callee_var_itr->context())); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 138 | uint32_t newId = TakeNextId(); |
Diego Novillo | 8322813 | 2017-11-27 10:16:41 -0500 | [diff] [blame] | 139 | get_decoration_mgr()->CloneDecorations(callee_var_itr->result_id(), newId, |
| 140 | update_def_use_mgr_); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 141 | var_inst->SetResultId(newId); |
| 142 | (*callee2caller)[callee_var_itr->result_id()] = newId; |
| 143 | new_vars->push_back(std::move(var_inst)); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 144 | ++callee_var_itr; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | uint32_t InlinePass::CreateReturnVar( |
| 149 | ir::Function* calleeFn, |
| 150 | std::vector<std::unique_ptr<ir::Instruction>>* new_vars) { |
| 151 | uint32_t returnVarId = 0; |
| 152 | const uint32_t calleeTypeId = calleeFn->type_id(); |
Steven Perron | 06b437d | 2018-02-09 20:33:06 -0500 | [diff] [blame^] | 153 | analysis::Type* calleeType = context()->get_type_mgr()->GetType(calleeTypeId); |
| 154 | if (calleeType->AsVoid() == nullptr) { |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 155 | // Find or create ptr to callee return type. |
Steven Perron | b86eb68 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 156 | uint32_t returnVarTypeId = context()->get_type_mgr()->FindPointerToType( |
| 157 | calleeTypeId, SpvStorageClassFunction); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 158 | if (returnVarTypeId == 0) |
| 159 | returnVarTypeId = AddPointerToType(calleeTypeId, SpvStorageClassFunction); |
| 160 | // Add return var to new function scope variables. |
| 161 | returnVarId = TakeNextId(); |
| 162 | std::unique_ptr<ir::Instruction> var_inst(new ir::Instruction( |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 163 | context(), SpvOpVariable, returnVarTypeId, returnVarId, |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 164 | {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS, |
| 165 | {SpvStorageClassFunction}}})); |
| 166 | new_vars->push_back(std::move(var_inst)); |
| 167 | } |
Diego Novillo | 8322813 | 2017-11-27 10:16:41 -0500 | [diff] [blame] | 168 | get_decoration_mgr()->CloneDecorations(calleeFn->result_id(), returnVarId, |
| 169 | update_def_use_mgr_); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 170 | return returnVarId; |
| 171 | } |
| 172 | |
| 173 | bool InlinePass::IsSameBlockOp(const ir::Instruction* inst) const { |
| 174 | return inst->opcode() == SpvOpSampledImage || inst->opcode() == SpvOpImage; |
| 175 | } |
| 176 | |
| 177 | void InlinePass::CloneSameBlockOps( |
| 178 | std::unique_ptr<ir::Instruction>* inst, |
| 179 | std::unordered_map<uint32_t, uint32_t>* postCallSB, |
| 180 | std::unordered_map<uint32_t, ir::Instruction*>* preCallSB, |
| 181 | std::unique_ptr<ir::BasicBlock>* block_ptr) { |
Diego Novillo | 8322813 | 2017-11-27 10:16:41 -0500 | [diff] [blame] | 182 | (*inst)->ForEachInId([&postCallSB, &preCallSB, &block_ptr, |
| 183 | this](uint32_t* iid) { |
| 184 | const auto mapItr = (*postCallSB).find(*iid); |
| 185 | if (mapItr == (*postCallSB).end()) { |
| 186 | const auto mapItr2 = (*preCallSB).find(*iid); |
| 187 | if (mapItr2 != (*preCallSB).end()) { |
| 188 | // Clone pre-call same-block ops, map result id. |
| 189 | const ir::Instruction* inInst = mapItr2->second; |
| 190 | std::unique_ptr<ir::Instruction> sb_inst( |
| 191 | inInst->Clone(inInst->context())); |
| 192 | CloneSameBlockOps(&sb_inst, postCallSB, preCallSB, block_ptr); |
| 193 | const uint32_t rid = sb_inst->result_id(); |
| 194 | const uint32_t nid = this->TakeNextId(); |
| 195 | get_decoration_mgr()->CloneDecorations(rid, nid, update_def_use_mgr_); |
| 196 | sb_inst->SetResultId(nid); |
| 197 | (*postCallSB)[rid] = nid; |
| 198 | *iid = nid; |
| 199 | (*block_ptr)->AddInstruction(std::move(sb_inst)); |
| 200 | } |
| 201 | } else { |
| 202 | // Reset same-block op operand. |
| 203 | *iid = mapItr->second; |
| 204 | } |
| 205 | }); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void InlinePass::GenInlineCode( |
| 209 | std::vector<std::unique_ptr<ir::BasicBlock>>* new_blocks, |
| 210 | std::vector<std::unique_ptr<ir::Instruction>>* new_vars, |
Steven Perron | bb7802b | 2017-10-13 14:25:21 -0400 | [diff] [blame] | 211 | ir::BasicBlock::iterator call_inst_itr, |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 212 | ir::UptrVectorIterator<ir::BasicBlock> call_block_itr) { |
| 213 | // Map from all ids in the callee to their equivalent id in the caller |
| 214 | // as callee instructions are copied into caller. |
| 215 | std::unordered_map<uint32_t, uint32_t> callee2caller; |
| 216 | // Pre-call same-block insts |
| 217 | std::unordered_map<uint32_t, ir::Instruction*> preCallSB; |
| 218 | // Post-call same-block op ids |
| 219 | std::unordered_map<uint32_t, uint32_t> postCallSB; |
| 220 | |
| 221 | ir::Function* calleeFn = id2function_[call_inst_itr->GetSingleWordOperand( |
| 222 | kSpvFunctionCallFunctionId)]; |
| 223 | |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 224 | // Check for multiple returns in the callee. |
| 225 | auto fi = multi_return_funcs_.find(calleeFn->result_id()); |
| 226 | const bool multiReturn = fi != multi_return_funcs_.end(); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 227 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 228 | // Map parameters to actual arguments. |
| 229 | MapParams(calleeFn, call_inst_itr, &callee2caller); |
| 230 | |
| 231 | // Define caller local variables for all callee variables and create map to |
| 232 | // them. |
| 233 | CloneAndMapLocals(calleeFn, new_vars, &callee2caller); |
| 234 | |
| 235 | // Create return var if needed. |
| 236 | uint32_t returnVarId = CreateReturnVar(calleeFn, new_vars); |
| 237 | |
GregF | a699d1a | 2017-08-29 18:35:05 -0600 | [diff] [blame] | 238 | // Create set of callee result ids. Used to detect forward references |
| 239 | std::unordered_set<uint32_t> callee_result_ids; |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 240 | calleeFn->ForEachInst([&callee_result_ids](const ir::Instruction* cpi) { |
GregF | a699d1a | 2017-08-29 18:35:05 -0600 | [diff] [blame] | 241 | const uint32_t rid = cpi->result_id(); |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 242 | if (rid != 0) callee_result_ids.insert(rid); |
GregF | a699d1a | 2017-08-29 18:35:05 -0600 | [diff] [blame] | 243 | }); |
| 244 | |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 245 | // If the caller is in a single-block loop, and the callee has multiple |
| 246 | // blocks, then the normal inlining logic will place the OpLoopMerge in |
| 247 | // the last of several blocks in the loop. Instead, it should be placed |
| 248 | // at the end of the first block. First determine if the caller is in a |
| 249 | // single block loop. We'll wait to move the OpLoopMerge until the end |
| 250 | // of the regular inlining logic, and only if necessary. |
| 251 | bool caller_is_single_block_loop = false; |
David Neto | 25ddfec | 2017-09-02 19:01:03 -0400 | [diff] [blame] | 252 | bool caller_is_loop_header = false; |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 253 | if (auto* loop_merge = call_block_itr->GetLoopMergeInst()) { |
David Neto | 25ddfec | 2017-09-02 19:01:03 -0400 | [diff] [blame] | 254 | caller_is_loop_header = true; |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 255 | caller_is_single_block_loop = |
| 256 | call_block_itr->id() == |
| 257 | loop_merge->GetSingleWordInOperand(kSpvLoopMergeContinueTargetIdInIdx); |
| 258 | } |
| 259 | |
| 260 | bool callee_begins_with_structured_header = |
| 261 | (*(calleeFn->begin())).GetMergeInst() != nullptr; |
| 262 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 263 | // Clone and map callee code. Copy caller block code to beginning of |
| 264 | // first block and end of last block. |
| 265 | bool prevInstWasReturn = false; |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 266 | uint32_t singleTripLoopHeaderId = 0; |
| 267 | uint32_t singleTripLoopContinueId = 0; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 268 | uint32_t returnLabelId = 0; |
| 269 | bool multiBlocks = false; |
| 270 | const uint32_t calleeTypeId = calleeFn->type_id(); |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 271 | // new_blk_ptr is a new basic block in the caller. New instructions are |
| 272 | // written to it. It is created when we encounter the OpLabel |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 273 | // of the first callee block. It is appended to new_blocks only when |
| 274 | // it is complete. |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 275 | std::unique_ptr<ir::BasicBlock> new_blk_ptr; |
| 276 | calleeFn->ForEachInst([&new_blocks, &callee2caller, &call_block_itr, |
| 277 | &call_inst_itr, &new_blk_ptr, &prevInstWasReturn, |
David Neto | 25ddfec | 2017-09-02 19:01:03 -0400 | [diff] [blame] | 278 | &returnLabelId, &returnVarId, caller_is_loop_header, |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 279 | callee_begins_with_structured_header, &calleeTypeId, |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 280 | &multiBlocks, &postCallSB, &preCallSB, multiReturn, |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 281 | &singleTripLoopHeaderId, &singleTripLoopContinueId, |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 282 | &callee_result_ids, this](const ir::Instruction* cpi) { |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 283 | switch (cpi->opcode()) { |
| 284 | case SpvOpFunction: |
| 285 | case SpvOpFunctionParameter: |
| 286 | case SpvOpVariable: |
| 287 | // Already processed |
| 288 | break; |
| 289 | case SpvOpLabel: { |
| 290 | // If previous instruction was early return, insert branch |
| 291 | // instruction to return block. |
| 292 | if (prevInstWasReturn) { |
| 293 | if (returnLabelId == 0) returnLabelId = this->TakeNextId(); |
| 294 | AddBranch(returnLabelId, &new_blk_ptr); |
| 295 | prevInstWasReturn = false; |
| 296 | } |
| 297 | // Finish current block (if it exists) and get label for next block. |
| 298 | uint32_t labelId; |
| 299 | bool firstBlock = false; |
| 300 | if (new_blk_ptr != nullptr) { |
| 301 | new_blocks->push_back(std::move(new_blk_ptr)); |
| 302 | // If result id is already mapped, use it, otherwise get a new |
| 303 | // one. |
| 304 | const uint32_t rid = cpi->result_id(); |
| 305 | const auto mapItr = callee2caller.find(rid); |
| 306 | labelId = (mapItr != callee2caller.end()) ? mapItr->second |
| 307 | : this->TakeNextId(); |
| 308 | } else { |
| 309 | // First block needs to use label of original block |
| 310 | // but map callee label in case of phi reference. |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 311 | labelId = call_block_itr->id(); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 312 | callee2caller[cpi->result_id()] = labelId; |
| 313 | firstBlock = true; |
| 314 | } |
| 315 | // Create first/next block. |
| 316 | new_blk_ptr.reset(new ir::BasicBlock(NewLabel(labelId))); |
| 317 | if (firstBlock) { |
| 318 | // Copy contents of original caller block up to call instruction. |
| 319 | for (auto cii = call_block_itr->begin(); cii != call_inst_itr; |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 320 | ++cii) { |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 321 | std::unique_ptr<ir::Instruction> cp_inst(cii->Clone(context())); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 322 | // Remember same-block ops for possible regeneration. |
| 323 | if (IsSameBlockOp(&*cp_inst)) { |
| 324 | auto* sb_inst_ptr = cp_inst.get(); |
| 325 | preCallSB[cp_inst->result_id()] = sb_inst_ptr; |
| 326 | } |
| 327 | new_blk_ptr->AddInstruction(std::move(cp_inst)); |
| 328 | } |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 329 | if (caller_is_loop_header && callee_begins_with_structured_header) { |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 330 | // We can't place both the caller's merge instruction and another |
| 331 | // merge instruction in the same block. So split the calling block. |
| 332 | // Insert an unconditional branch to a new guard block. Later, |
| 333 | // once we know the ID of the last block, we will move the caller's |
| 334 | // OpLoopMerge from the last generated block into the first block. |
| 335 | // We also wait to avoid invalidating various iterators. |
| 336 | const auto guard_block_id = this->TakeNextId(); |
| 337 | AddBranch(guard_block_id, &new_blk_ptr); |
| 338 | new_blocks->push_back(std::move(new_blk_ptr)); |
| 339 | // Start the next block. |
| 340 | new_blk_ptr.reset(new ir::BasicBlock(NewLabel(guard_block_id))); |
| 341 | // Reset the mapping of the callee's entry block to point to |
| 342 | // the guard block. Do this so we can fix up phis later on to |
| 343 | // satisfy dominance. |
| 344 | callee2caller[cpi->result_id()] = guard_block_id; |
| 345 | } |
| 346 | // If callee has multiple returns, insert a header block for |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 347 | // single-trip loop that will encompass callee code. Start postheader |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 348 | // block. |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 349 | // |
| 350 | // Note: Consider the following combination: |
| 351 | // - the caller is a single block loop |
| 352 | // - the callee does not begin with a structure header |
| 353 | // - the callee has multiple returns. |
| 354 | // We still need to split the caller block and insert a guard block. |
| 355 | // But we only need to do it once. We haven't done it yet, but the |
| 356 | // single-trip loop header will serve the same purpose. |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 357 | if (multiReturn) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 358 | singleTripLoopHeaderId = this->TakeNextId(); |
| 359 | AddBranch(singleTripLoopHeaderId, &new_blk_ptr); |
| 360 | new_blocks->push_back(std::move(new_blk_ptr)); |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 361 | new_blk_ptr.reset( |
| 362 | new ir::BasicBlock(NewLabel(singleTripLoopHeaderId))); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 363 | returnLabelId = this->TakeNextId(); |
| 364 | singleTripLoopContinueId = this->TakeNextId(); |
| 365 | AddLoopMerge(returnLabelId, singleTripLoopContinueId, &new_blk_ptr); |
| 366 | uint32_t postHeaderId = this->TakeNextId(); |
| 367 | AddBranch(postHeaderId, &new_blk_ptr); |
| 368 | new_blocks->push_back(std::move(new_blk_ptr)); |
| 369 | new_blk_ptr.reset(new ir::BasicBlock(NewLabel(postHeaderId))); |
| 370 | multiBlocks = true; |
David Neto | 860c419 | 2017-08-31 17:33:44 -0400 | [diff] [blame] | 371 | // Reset the mapping of the callee's entry block to point to |
| 372 | // the post-header block. Do this so we can fix up phis later |
| 373 | // on to satisfy dominance. |
| 374 | callee2caller[cpi->result_id()] = postHeaderId; |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 375 | } |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 376 | } else { |
| 377 | multiBlocks = true; |
| 378 | } |
| 379 | } break; |
| 380 | case SpvOpReturnValue: { |
| 381 | // Store return value to return variable. |
| 382 | assert(returnVarId != 0); |
| 383 | uint32_t valId = cpi->GetInOperand(kSpvReturnValueId).words[0]; |
| 384 | const auto mapItr = callee2caller.find(valId); |
| 385 | if (mapItr != callee2caller.end()) { |
| 386 | valId = mapItr->second; |
| 387 | } |
| 388 | AddStore(returnVarId, valId, &new_blk_ptr); |
| 389 | |
| 390 | // Remember we saw a return; if followed by a label, will need to |
| 391 | // insert branch. |
| 392 | prevInstWasReturn = true; |
| 393 | } break; |
| 394 | case SpvOpReturn: { |
| 395 | // Remember we saw a return; if followed by a label, will need to |
| 396 | // insert branch. |
| 397 | prevInstWasReturn = true; |
| 398 | } break; |
| 399 | case SpvOpFunctionEnd: { |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 400 | // If there was an early return, we generated a return label id |
| 401 | // for it. Now we have to generate the return block with that Id. |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 402 | if (returnLabelId != 0) { |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 403 | // If previous instruction was return, insert branch instruction |
| 404 | // to return block. |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 405 | if (prevInstWasReturn) AddBranch(returnLabelId, &new_blk_ptr); |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 406 | if (multiReturn) { |
| 407 | // If we generated a loop header to for the single-trip loop |
| 408 | // to accommodate multiple returns, insert the continue |
| 409 | // target block now, with a false branch back to the loop header. |
| 410 | new_blocks->push_back(std::move(new_blk_ptr)); |
| 411 | new_blk_ptr.reset( |
| 412 | new ir::BasicBlock(NewLabel(singleTripLoopContinueId))); |
| 413 | AddBranchCond(GetFalseId(), singleTripLoopHeaderId, returnLabelId, |
| 414 | &new_blk_ptr); |
| 415 | } |
| 416 | // Generate the return block. |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 417 | new_blocks->push_back(std::move(new_blk_ptr)); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 418 | new_blk_ptr.reset(new ir::BasicBlock(NewLabel(returnLabelId))); |
| 419 | multiBlocks = true; |
| 420 | } |
| 421 | // Load return value into result id of call, if it exists. |
| 422 | if (returnVarId != 0) { |
| 423 | const uint32_t resId = call_inst_itr->result_id(); |
| 424 | assert(resId != 0); |
| 425 | AddLoad(calleeTypeId, resId, returnVarId, &new_blk_ptr); |
| 426 | } |
| 427 | // Copy remaining instructions from caller block. |
| 428 | auto cii = call_inst_itr; |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 429 | for (++cii; cii != call_block_itr->end(); ++cii) { |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 430 | std::unique_ptr<ir::Instruction> cp_inst(cii->Clone(context())); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 431 | // If multiple blocks generated, regenerate any same-block |
| 432 | // instruction that has not been seen in this last block. |
| 433 | if (multiBlocks) { |
| 434 | CloneSameBlockOps(&cp_inst, &postCallSB, &preCallSB, &new_blk_ptr); |
| 435 | // Remember same-block ops in this block. |
| 436 | if (IsSameBlockOp(&*cp_inst)) { |
| 437 | const uint32_t rid = cp_inst->result_id(); |
| 438 | postCallSB[rid] = rid; |
| 439 | } |
| 440 | } |
| 441 | new_blk_ptr->AddInstruction(std::move(cp_inst)); |
| 442 | } |
| 443 | // Finalize inline code. |
| 444 | new_blocks->push_back(std::move(new_blk_ptr)); |
| 445 | } break; |
| 446 | default: { |
| 447 | // Copy callee instruction and remap all input Ids. |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 448 | std::unique_ptr<ir::Instruction> cp_inst(cpi->Clone(context())); |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 449 | cp_inst->ForEachInId([&callee2caller, &callee_result_ids, |
| 450 | this](uint32_t* iid) { |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 451 | const auto mapItr = callee2caller.find(*iid); |
| 452 | if (mapItr != callee2caller.end()) { |
| 453 | *iid = mapItr->second; |
GregF | a699d1a | 2017-08-29 18:35:05 -0600 | [diff] [blame] | 454 | } else if (callee_result_ids.find(*iid) != callee_result_ids.end()) { |
| 455 | // Forward reference. Allocate a new id, map it, |
| 456 | // use it and check for it when remapping result ids |
| 457 | const uint32_t nid = this->TakeNextId(); |
| 458 | callee2caller[*iid] = nid; |
| 459 | *iid = nid; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 460 | } |
| 461 | }); |
GregF | a699d1a | 2017-08-29 18:35:05 -0600 | [diff] [blame] | 462 | // If result id is non-zero, remap it. If already mapped, use mapped |
| 463 | // value, else use next id. |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 464 | const uint32_t rid = cp_inst->result_id(); |
| 465 | if (rid != 0) { |
GregF | a699d1a | 2017-08-29 18:35:05 -0600 | [diff] [blame] | 466 | const auto mapItr = callee2caller.find(rid); |
| 467 | uint32_t nid; |
| 468 | if (mapItr != callee2caller.end()) { |
| 469 | nid = mapItr->second; |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 470 | } else { |
GregF | a699d1a | 2017-08-29 18:35:05 -0600 | [diff] [blame] | 471 | nid = this->TakeNextId(); |
| 472 | callee2caller[rid] = nid; |
| 473 | } |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 474 | cp_inst->SetResultId(nid); |
Steven Perron | eb4653a | 2017-11-13 15:31:43 -0500 | [diff] [blame] | 475 | get_decoration_mgr()->CloneDecorations(rid, nid, update_def_use_mgr_); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 476 | } |
| 477 | new_blk_ptr->AddInstruction(std::move(cp_inst)); |
| 478 | } break; |
| 479 | } |
| 480 | }); |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 481 | |
David Neto | 25ddfec | 2017-09-02 19:01:03 -0400 | [diff] [blame] | 482 | if (caller_is_loop_header && (new_blocks->size() > 1)) { |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 483 | // Move the OpLoopMerge from the last block back to the first, where |
David Neto | 25ddfec | 2017-09-02 19:01:03 -0400 | [diff] [blame] | 484 | // it belongs. |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 485 | auto& first = new_blocks->front(); |
| 486 | auto& last = new_blocks->back(); |
| 487 | assert(first != last); |
| 488 | |
| 489 | // Insert a modified copy of the loop merge into the first block. |
| 490 | auto loop_merge_itr = last->tail(); |
| 491 | --loop_merge_itr; |
| 492 | assert(loop_merge_itr->opcode() == SpvOpLoopMerge); |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 493 | std::unique_ptr<ir::Instruction> cp_inst(loop_merge_itr->Clone(context())); |
David Neto | 25ddfec | 2017-09-02 19:01:03 -0400 | [diff] [blame] | 494 | if (caller_is_single_block_loop) { |
| 495 | // Also, update its continue target to point to the last block. |
| 496 | cp_inst->SetInOperand(kSpvLoopMergeContinueTargetIdInIdx, {last->id()}); |
| 497 | } |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 498 | first->tail().InsertBefore(std::move(cp_inst)); |
| 499 | |
| 500 | // Remove the loop merge from the last block. |
Steven Perron | bb7802b | 2017-10-13 14:25:21 -0400 | [diff] [blame] | 501 | loop_merge_itr->RemoveFromList(); |
| 502 | delete &*loop_merge_itr; |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 503 | } |
| 504 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 505 | // Update block map given replacement blocks. |
| 506 | for (auto& blk : *new_blocks) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 507 | id2block_[blk->id()] = &*blk; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
David Neto | ceb1d4f | 2017-03-31 10:36:58 -0400 | [diff] [blame] | 511 | bool InlinePass::IsInlinableFunctionCall(const ir::Instruction* inst) { |
| 512 | if (inst->opcode() != SpvOp::SpvOpFunctionCall) return false; |
GregF | a107d34 | 2017-04-25 13:57:20 -0600 | [diff] [blame] | 513 | const uint32_t calleeFnId = |
| 514 | inst->GetSingleWordOperand(kSpvFunctionCallFunctionId); |
| 515 | const auto ci = inlinable_.find(calleeFnId); |
| 516 | return ci != inlinable_.cend(); |
David Neto | ceb1d4f | 2017-03-31 10:36:58 -0400 | [diff] [blame] | 517 | } |
| 518 | |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 519 | void InlinePass::UpdateSucceedingPhis( |
| 520 | std::vector<std::unique_ptr<ir::BasicBlock>>& new_blocks) { |
| 521 | const auto firstBlk = new_blocks.begin(); |
| 522 | const auto lastBlk = new_blocks.end() - 1; |
| 523 | const uint32_t firstId = (*firstBlk)->id(); |
| 524 | const uint32_t lastId = (*lastBlk)->id(); |
David Neto | 87f9cfa | 2018-02-02 14:17:42 -0800 | [diff] [blame] | 525 | const ir::BasicBlock& const_last_block = *lastBlk->get(); |
| 526 | const_last_block.ForEachSuccessorLabel( |
| 527 | [&firstId, &lastId, this](const uint32_t succ) { |
| 528 | ir::BasicBlock* sbp = this->id2block_[succ]; |
| 529 | sbp->ForEachPhiInst([&firstId, &lastId](ir::Instruction* phi) { |
| 530 | phi->ForEachInId([&firstId, &lastId](uint32_t* id) { |
| 531 | if (*id == firstId) *id = lastId; |
| 532 | }); |
| 533 | }); |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 534 | }); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 537 | bool InlinePass::HasMultipleReturns(ir::Function* func) { |
| 538 | bool seenReturn = false; |
| 539 | bool multipleReturns = false; |
| 540 | for (auto& blk : *func) { |
| 541 | auto terminal_ii = blk.cend(); |
| 542 | --terminal_ii; |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 543 | if (terminal_ii->opcode() == SpvOpReturn || |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 544 | terminal_ii->opcode() == SpvOpReturnValue) { |
| 545 | if (seenReturn) { |
| 546 | multipleReturns = true; |
| 547 | break; |
| 548 | } |
| 549 | seenReturn = true; |
| 550 | } |
| 551 | } |
| 552 | return multipleReturns; |
| 553 | } |
| 554 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 555 | void InlinePass::ComputeStructuredSuccessors(ir::Function* func) { |
| 556 | // If header, make merge block first successor. |
| 557 | for (auto& blk : *func) { |
Diego Novillo | fef669f | 2017-10-30 17:42:26 -0400 | [diff] [blame] | 558 | uint32_t mbid = blk.MergeBlockIdIfAny(); |
| 559 | if (mbid != 0) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 560 | block2structured_succs_[&blk].push_back(id2block_[mbid]); |
Diego Novillo | fef669f | 2017-10-30 17:42:26 -0400 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | // Add true successors. |
David Neto | 87f9cfa | 2018-02-02 14:17:42 -0800 | [diff] [blame] | 564 | const auto& const_blk = blk; |
| 565 | const_blk.ForEachSuccessorLabel([&blk, this](const uint32_t sbid) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 566 | block2structured_succs_[&blk].push_back(id2block_[sbid]); |
| 567 | }); |
| 568 | } |
| 569 | } |
Diego Novillo | fef669f | 2017-10-30 17:42:26 -0400 | [diff] [blame] | 570 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 571 | InlinePass::GetBlocksFunction InlinePass::StructuredSuccessorsFunction() { |
| 572 | return [this](const ir::BasicBlock* block) { |
| 573 | return &(block2structured_succs_[block]); |
| 574 | }; |
| 575 | } |
| 576 | |
| 577 | bool InlinePass::HasNoReturnInLoop(ir::Function* func) { |
| 578 | // If control not structured, do not do loop/return analysis |
| 579 | // TODO: Analyze returns in non-structured control flow |
Steven Perron | 756b277 | 2017-12-19 14:18:13 -0500 | [diff] [blame] | 580 | if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader)) |
| 581 | return false; |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 582 | // Compute structured block order. This order has the property |
| 583 | // that dominators are before all blocks they dominate and merge blocks |
| 584 | // are after all blocks that are in the control constructs of their header. |
| 585 | ComputeStructuredSuccessors(func); |
| 586 | auto ignore_block = [](cbb_ptr) {}; |
| 587 | auto ignore_edge = [](cbb_ptr, cbb_ptr) {}; |
| 588 | std::list<const ir::BasicBlock*> structuredOrder; |
| 589 | spvtools::CFA<ir::BasicBlock>::DepthFirstTraversal( |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 590 | &*func->begin(), StructuredSuccessorsFunction(), ignore_block, |
| 591 | [&](cbb_ptr b) { structuredOrder.push_front(b); }, ignore_edge); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 592 | // Search for returns in loops. Only need to track outermost loop |
| 593 | bool return_in_loop = false; |
| 594 | uint32_t outerLoopMergeId = 0; |
| 595 | for (auto& blk : structuredOrder) { |
| 596 | // Exiting current outer loop |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 597 | if (blk->id() == outerLoopMergeId) outerLoopMergeId = 0; |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 598 | // Return block |
| 599 | auto terminal_ii = blk->cend(); |
| 600 | --terminal_ii; |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 601 | if (terminal_ii->opcode() == SpvOpReturn || |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 602 | terminal_ii->opcode() == SpvOpReturnValue) { |
| 603 | if (outerLoopMergeId != 0) { |
| 604 | return_in_loop = true; |
| 605 | break; |
| 606 | } |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 607 | } else if (terminal_ii != blk->cbegin()) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 608 | auto merge_ii = terminal_ii; |
| 609 | --merge_ii; |
| 610 | // Entering outermost loop |
| 611 | if (merge_ii->opcode() == SpvOpLoopMerge && outerLoopMergeId == 0) |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 612 | outerLoopMergeId = |
| 613 | merge_ii->GetSingleWordOperand(kSpvLoopMergeMergeBlockId); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | return !return_in_loop; |
| 617 | } |
| 618 | |
| 619 | void InlinePass::AnalyzeReturns(ir::Function* func) { |
| 620 | // Look for multiple returns |
| 621 | if (!HasMultipleReturns(func)) { |
| 622 | no_return_in_loop_.insert(func->result_id()); |
| 623 | return; |
| 624 | } |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 625 | multi_return_funcs_.insert(func->result_id()); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 626 | // If multiple returns, see if any are in a loop |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 627 | if (HasNoReturnInLoop(func)) no_return_in_loop_.insert(func->result_id()); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | bool InlinePass::IsInlinableFunction(ir::Function* func) { |
GregF | a107d34 | 2017-04-25 13:57:20 -0600 | [diff] [blame] | 631 | // We can only inline a function if it has blocks. |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 632 | if (func->cbegin() == func->cend()) return false; |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 633 | // Do not inline functions with returns in loops. Currently early return |
| 634 | // functions are inlined by wrapping them in a one trip loop and implementing |
| 635 | // the returns as a branch to the loop's merge block. However, this can only |
| 636 | // done validly if the return was not in a loop in the original function. |
| 637 | // Also remember functions with multiple (early) returns. |
| 638 | AnalyzeReturns(func); |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 639 | return no_return_in_loop_.find(func->result_id()) != |
| 640 | no_return_in_loop_.cend(); |
GregF | a107d34 | 2017-04-25 13:57:20 -0600 | [diff] [blame] | 641 | } |
| 642 | |
Steven Perron | 476cae6 | 2017-10-30 11:13:24 -0400 | [diff] [blame] | 643 | void InlinePass::InitializeInline(ir::IRContext* c) { |
| 644 | InitializeProcessing(c); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 645 | |
Alan Baker | 746bfd2 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 646 | // Don't bother updating the DefUseManger |
Pierre Moreau | 6904396 | 2017-11-30 22:32:44 +0100 | [diff] [blame] | 647 | update_def_use_mgr_ = [](ir::Instruction&, bool) {}; |
Daniel Schürmann | a76d097 | 2017-10-24 18:28:18 +0200 | [diff] [blame] | 648 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 649 | false_id_ = 0; |
| 650 | |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 651 | // clear collections |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 652 | id2function_.clear(); |
| 653 | id2block_.clear(); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 654 | block2structured_succs_.clear(); |
GregF | a107d34 | 2017-04-25 13:57:20 -0600 | [diff] [blame] | 655 | inlinable_.clear(); |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 656 | no_return_in_loop_.clear(); |
David Neto | 2a1014b | 2017-08-09 14:59:04 -0400 | [diff] [blame] | 657 | multi_return_funcs_.clear(); |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 658 | |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 659 | for (auto& fn : *get_module()) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 660 | // Initialize function and block maps. |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 661 | id2function_[fn.result_id()] = &fn; |
| 662 | for (auto& blk : fn) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 663 | id2block_[blk.id()] = &blk; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 664 | } |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 665 | // Compute inlinability |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 666 | if (IsInlinableFunction(&fn)) inlinable_.insert(fn.result_id()); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 667 | } |
| 668 | }; |
| 669 | |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 670 | InlinePass::InlinePass() {} |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 671 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 672 | } // namespace opt |
| 673 | } // namespace spvtools |