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