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