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 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 17 | #include "source/opt/inline_pass.h" |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 18 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 19 | #include <unordered_set> |
| 20 | #include <utility> |
| 21 | |
| 22 | #include "source/cfa.h" |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 23 | #include "source/opt/reflect.h" |
dan sinclair | 1963a2d | 2018-08-14 15:01:50 -0400 | [diff] [blame] | 24 | #include "source/util/make_unique.h" |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 25 | |
| 26 | // Indices of operands in SPIR-V instructions |
| 27 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 28 | static const int kSpvFunctionCallFunctionId = 2; |
| 29 | static const int kSpvFunctionCallArgumentId = 3; |
| 30 | static const int kSpvReturnValueId = 0; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 31 | |
| 32 | namespace spvtools { |
| 33 | namespace opt { |
| 34 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 35 | uint32_t InlinePass::AddPointerToType(uint32_t type_id, |
| 36 | SpvStorageClass storage_class) { |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 37 | uint32_t resultId = context()->TakeNextId(); |
| 38 | if (resultId == 0) { |
| 39 | return resultId; |
| 40 | } |
| 41 | |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 42 | std::unique_ptr<Instruction> type_inst( |
| 43 | new Instruction(context(), SpvOpTypePointer, 0, resultId, |
| 44 | {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS, |
| 45 | {uint32_t(storage_class)}}, |
| 46 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {type_id}}})); |
Steven Perron | 476cae6 | 2017-10-30 11:13:24 -0400 | [diff] [blame] | 47 | context()->AddType(std::move(type_inst)); |
Alan Baker | 6169085 | 2017-12-08 15:33:19 -0500 | [diff] [blame] | 48 | analysis::Type* pointeeTy; |
| 49 | std::unique_ptr<analysis::Pointer> pointerTy; |
| 50 | std::tie(pointeeTy, pointerTy) = |
| 51 | context()->get_type_mgr()->GetTypeAndPointerType(type_id, |
| 52 | SpvStorageClassFunction); |
| 53 | context()->get_type_mgr()->RegisterType(resultId, *pointerTy); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 54 | return resultId; |
| 55 | } |
| 56 | |
| 57 | void InlinePass::AddBranch(uint32_t label_id, |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 58 | std::unique_ptr<BasicBlock>* block_ptr) { |
| 59 | std::unique_ptr<Instruction> newBranch( |
| 60 | new Instruction(context(), SpvOpBranch, 0, 0, |
| 61 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {label_id}}})); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 62 | (*block_ptr)->AddInstruction(std::move(newBranch)); |
| 63 | } |
| 64 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 65 | void InlinePass::AddBranchCond(uint32_t cond_id, uint32_t true_id, |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 66 | uint32_t false_id, |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 67 | std::unique_ptr<BasicBlock>* block_ptr) { |
| 68 | std::unique_ptr<Instruction> newBranch( |
| 69 | new Instruction(context(), SpvOpBranchConditional, 0, 0, |
| 70 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {cond_id}}, |
| 71 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {true_id}}, |
| 72 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {false_id}}})); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 73 | (*block_ptr)->AddInstruction(std::move(newBranch)); |
| 74 | } |
| 75 | |
| 76 | void InlinePass::AddLoopMerge(uint32_t merge_id, uint32_t continue_id, |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 77 | std::unique_ptr<BasicBlock>* block_ptr) { |
| 78 | std::unique_ptr<Instruction> newLoopMerge(new Instruction( |
Alan Baker | a771713 | 2017-11-14 14:11:50 -0500 | [diff] [blame] | 79 | context(), SpvOpLoopMerge, 0, 0, |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 80 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {merge_id}}, |
| 81 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {continue_id}}, |
| 82 | {spv_operand_type_t::SPV_OPERAND_TYPE_LOOP_CONTROL, {0}}})); |
| 83 | (*block_ptr)->AddInstruction(std::move(newLoopMerge)); |
| 84 | } |
| 85 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 86 | void InlinePass::AddStore(uint32_t ptr_id, uint32_t val_id, |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 87 | std::unique_ptr<BasicBlock>* block_ptr, |
| 88 | const Instruction* line_inst, |
| 89 | const DebugScope& dbg_scope) { |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 90 | std::unique_ptr<Instruction> newStore( |
| 91 | new Instruction(context(), SpvOpStore, 0, 0, |
| 92 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}}, |
| 93 | {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {val_id}}})); |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 94 | if (line_inst != nullptr) { |
| 95 | newStore->dbg_line_insts().push_back(*line_inst); |
| 96 | } |
| 97 | newStore->SetDebugScope(dbg_scope); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 98 | (*block_ptr)->AddInstruction(std::move(newStore)); |
| 99 | } |
| 100 | |
| 101 | void InlinePass::AddLoad(uint32_t type_id, uint32_t resultId, uint32_t ptr_id, |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 102 | std::unique_ptr<BasicBlock>* block_ptr, |
| 103 | const Instruction* line_inst, |
| 104 | const DebugScope& dbg_scope) { |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 105 | std::unique_ptr<Instruction> newLoad( |
| 106 | new Instruction(context(), SpvOpLoad, type_id, resultId, |
| 107 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}}})); |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 108 | if (line_inst != nullptr) { |
| 109 | newLoad->dbg_line_insts().push_back(*line_inst); |
| 110 | } |
| 111 | newLoad->SetDebugScope(dbg_scope); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 112 | (*block_ptr)->AddInstruction(std::move(newLoad)); |
| 113 | } |
| 114 | |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 115 | std::unique_ptr<Instruction> InlinePass::NewLabel(uint32_t label_id) { |
| 116 | std::unique_ptr<Instruction> newLabel( |
| 117 | new Instruction(context(), SpvOpLabel, 0, label_id, {})); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 118 | return newLabel; |
| 119 | } |
| 120 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 121 | uint32_t InlinePass::GetFalseId() { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 122 | if (false_id_ != 0) return false_id_; |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 123 | false_id_ = get_module()->GetGlobalValue(SpvOpConstantFalse); |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 124 | if (false_id_ != 0) return false_id_; |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 125 | uint32_t boolId = get_module()->GetGlobalValue(SpvOpTypeBool); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 126 | if (boolId == 0) { |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 127 | boolId = context()->TakeNextId(); |
| 128 | if (boolId == 0) { |
| 129 | return 0; |
| 130 | } |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 131 | get_module()->AddGlobalValue(SpvOpTypeBool, boolId, 0); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 132 | } |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 133 | false_id_ = context()->TakeNextId(); |
| 134 | if (false_id_ == 0) { |
| 135 | return 0; |
| 136 | } |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 137 | get_module()->AddGlobalValue(SpvOpConstantFalse, false_id_, boolId); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 138 | return false_id_; |
| 139 | } |
| 140 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 141 | void InlinePass::MapParams( |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 142 | Function* calleeFn, BasicBlock::iterator call_inst_itr, |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 143 | std::unordered_map<uint32_t, uint32_t>* callee2caller) { |
| 144 | int param_idx = 0; |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 145 | calleeFn->ForEachParam( |
| 146 | [&call_inst_itr, ¶m_idx, &callee2caller](const Instruction* cpi) { |
| 147 | const uint32_t pid = cpi->result_id(); |
| 148 | (*callee2caller)[pid] = call_inst_itr->GetSingleWordOperand( |
| 149 | kSpvFunctionCallArgumentId + param_idx); |
| 150 | ++param_idx; |
| 151 | }); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 154 | bool InlinePass::CloneAndMapLocals( |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 155 | Function* calleeFn, std::vector<std::unique_ptr<Instruction>>* new_vars, |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 156 | std::unordered_map<uint32_t, uint32_t>* callee2caller, |
| 157 | analysis::DebugInlinedAtContext* inlined_at_ctx) { |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 158 | auto callee_block_itr = calleeFn->begin(); |
| 159 | auto callee_var_itr = callee_block_itr->begin(); |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 160 | while (callee_var_itr->opcode() == SpvOp::SpvOpVariable || |
Greg Fischer | d9f8925 | 2021-07-21 10:04:38 -0600 | [diff] [blame] | 161 | callee_var_itr->GetCommonDebugOpcode() == |
| 162 | CommonDebugInfoDebugDeclare) { |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 163 | if (callee_var_itr->opcode() != SpvOp::SpvOpVariable) { |
| 164 | ++callee_var_itr; |
| 165 | continue; |
| 166 | } |
| 167 | |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 168 | std::unique_ptr<Instruction> var_inst(callee_var_itr->Clone(context())); |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 169 | uint32_t newId = context()->TakeNextId(); |
| 170 | if (newId == 0) { |
| 171 | return false; |
| 172 | } |
Pierre Moreau | 5bd55f1 | 2018-02-20 19:19:57 +0100 | [diff] [blame] | 173 | get_decoration_mgr()->CloneDecorations(callee_var_itr->result_id(), newId); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 174 | var_inst->SetResultId(newId); |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 175 | var_inst->UpdateDebugInlinedAt( |
| 176 | context()->get_debug_info_mgr()->BuildDebugInlinedAtChain( |
| 177 | callee_var_itr->GetDebugInlinedAt(), inlined_at_ctx)); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 178 | (*callee2caller)[callee_var_itr->result_id()] = newId; |
| 179 | new_vars->push_back(std::move(var_inst)); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 180 | ++callee_var_itr; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 181 | } |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 182 | return true; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | uint32_t InlinePass::CreateReturnVar( |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 186 | Function* calleeFn, std::vector<std::unique_ptr<Instruction>>* new_vars) { |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 187 | uint32_t returnVarId = 0; |
| 188 | const uint32_t calleeTypeId = calleeFn->type_id(); |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 189 | analysis::TypeManager* type_mgr = context()->get_type_mgr(); |
| 190 | assert(type_mgr->GetType(calleeTypeId)->AsVoid() == nullptr && |
| 191 | "Cannot create a return variable of type void."); |
| 192 | // Find or create ptr to callee return type. |
| 193 | uint32_t returnVarTypeId = |
| 194 | type_mgr->FindPointerToType(calleeTypeId, SpvStorageClassFunction); |
| 195 | |
| 196 | if (returnVarTypeId == 0) { |
| 197 | returnVarTypeId = AddPointerToType(calleeTypeId, SpvStorageClassFunction); |
| 198 | if (returnVarTypeId == 0) { |
| 199 | return 0; |
| 200 | } |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 201 | } |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 202 | |
| 203 | // Add return var to new function scope variables. |
| 204 | returnVarId = context()->TakeNextId(); |
| 205 | if (returnVarId == 0) { |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | std::unique_ptr<Instruction> var_inst( |
| 210 | new Instruction(context(), SpvOpVariable, returnVarTypeId, returnVarId, |
| 211 | {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS, |
| 212 | {SpvStorageClassFunction}}})); |
| 213 | new_vars->push_back(std::move(var_inst)); |
Pierre Moreau | 5bd55f1 | 2018-02-20 19:19:57 +0100 | [diff] [blame] | 214 | get_decoration_mgr()->CloneDecorations(calleeFn->result_id(), returnVarId); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 215 | return returnVarId; |
| 216 | } |
| 217 | |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 218 | bool InlinePass::IsSameBlockOp(const Instruction* inst) const { |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 219 | return inst->opcode() == SpvOpSampledImage || inst->opcode() == SpvOpImage; |
| 220 | } |
| 221 | |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 222 | bool InlinePass::CloneSameBlockOps( |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 223 | std::unique_ptr<Instruction>* inst, |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 224 | std::unordered_map<uint32_t, uint32_t>* postCallSB, |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 225 | std::unordered_map<uint32_t, Instruction*>* preCallSB, |
| 226 | std::unique_ptr<BasicBlock>* block_ptr) { |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 227 | return (*inst)->WhileEachInId([&postCallSB, &preCallSB, &block_ptr, |
| 228 | this](uint32_t* iid) { |
| 229 | const auto mapItr = (*postCallSB).find(*iid); |
| 230 | if (mapItr == (*postCallSB).end()) { |
| 231 | const auto mapItr2 = (*preCallSB).find(*iid); |
| 232 | if (mapItr2 != (*preCallSB).end()) { |
| 233 | // Clone pre-call same-block ops, map result id. |
| 234 | const Instruction* inInst = mapItr2->second; |
| 235 | std::unique_ptr<Instruction> sb_inst(inInst->Clone(context())); |
| 236 | if (!CloneSameBlockOps(&sb_inst, postCallSB, preCallSB, block_ptr)) { |
| 237 | return false; |
Pierre Moreau | 5bd55f1 | 2018-02-20 19:19:57 +0100 | [diff] [blame] | 238 | } |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 239 | |
| 240 | const uint32_t rid = sb_inst->result_id(); |
| 241 | const uint32_t nid = context()->TakeNextId(); |
| 242 | if (nid == 0) { |
| 243 | return false; |
| 244 | } |
| 245 | get_decoration_mgr()->CloneDecorations(rid, nid); |
| 246 | sb_inst->SetResultId(nid); |
| 247 | (*postCallSB)[rid] = nid; |
| 248 | *iid = nid; |
| 249 | (*block_ptr)->AddInstruction(std::move(sb_inst)); |
| 250 | } |
| 251 | } else { |
| 252 | // Reset same-block op operand. |
| 253 | *iid = mapItr->second; |
| 254 | } |
| 255 | return true; |
| 256 | }); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 259 | void InlinePass::MoveInstsBeforeEntryBlock( |
| 260 | std::unordered_map<uint32_t, Instruction*>* preCallSB, |
| 261 | BasicBlock* new_blk_ptr, BasicBlock::iterator call_inst_itr, |
| 262 | UptrVectorIterator<BasicBlock> call_block_itr) { |
| 263 | for (auto cii = call_block_itr->begin(); cii != call_inst_itr; |
| 264 | cii = call_block_itr->begin()) { |
| 265 | Instruction* inst = &*cii; |
| 266 | inst->RemoveFromList(); |
| 267 | std::unique_ptr<Instruction> cp_inst(inst); |
| 268 | // Remember same-block ops for possible regeneration. |
| 269 | if (IsSameBlockOp(&*cp_inst)) { |
| 270 | auto* sb_inst_ptr = cp_inst.get(); |
| 271 | (*preCallSB)[cp_inst->result_id()] = sb_inst_ptr; |
| 272 | } |
| 273 | new_blk_ptr->AddInstruction(std::move(cp_inst)); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | std::unique_ptr<BasicBlock> InlinePass::AddGuardBlock( |
| 278 | std::vector<std::unique_ptr<BasicBlock>>* new_blocks, |
| 279 | std::unordered_map<uint32_t, uint32_t>* callee2caller, |
| 280 | std::unique_ptr<BasicBlock> new_blk_ptr, uint32_t entry_blk_label_id) { |
| 281 | const auto guard_block_id = context()->TakeNextId(); |
| 282 | if (guard_block_id == 0) { |
| 283 | return nullptr; |
| 284 | } |
| 285 | AddBranch(guard_block_id, &new_blk_ptr); |
| 286 | new_blocks->push_back(std::move(new_blk_ptr)); |
| 287 | // Start the next block. |
| 288 | new_blk_ptr = MakeUnique<BasicBlock>(NewLabel(guard_block_id)); |
| 289 | // Reset the mapping of the callee's entry block to point to |
| 290 | // the guard block. Do this so we can fix up phis later on to |
| 291 | // satisfy dominance. |
| 292 | (*callee2caller)[entry_blk_label_id] = guard_block_id; |
| 293 | return new_blk_ptr; |
| 294 | } |
| 295 | |
| 296 | InstructionList::iterator InlinePass::AddStoresForVariableInitializers( |
| 297 | const std::unordered_map<uint32_t, uint32_t>& callee2caller, |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 298 | analysis::DebugInlinedAtContext* inlined_at_ctx, |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 299 | std::unique_ptr<BasicBlock>* new_blk_ptr, |
| 300 | UptrVectorIterator<BasicBlock> callee_first_block_itr) { |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 301 | auto callee_itr = callee_first_block_itr->begin(); |
| 302 | while (callee_itr->opcode() == SpvOp::SpvOpVariable || |
Greg Fischer | d9f8925 | 2021-07-21 10:04:38 -0600 | [diff] [blame] | 303 | callee_itr->GetCommonDebugOpcode() == CommonDebugInfoDebugDeclare) { |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 304 | if (callee_itr->opcode() == SpvOp::SpvOpVariable && |
| 305 | callee_itr->NumInOperands() == 2) { |
| 306 | assert(callee2caller.count(callee_itr->result_id()) && |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 307 | "Expected the variable to have already been mapped."); |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 308 | uint32_t new_var_id = callee2caller.at(callee_itr->result_id()); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 309 | |
| 310 | // The initializer must be a constant or global value. No mapped |
| 311 | // should be used. |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 312 | uint32_t val_id = callee_itr->GetSingleWordInOperand(1); |
| 313 | AddStore(new_var_id, val_id, new_blk_ptr, callee_itr->dbg_line_inst(), |
| 314 | context()->get_debug_info_mgr()->BuildDebugScope( |
| 315 | callee_itr->GetDebugScope(), inlined_at_ctx)); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 316 | } |
Greg Fischer | d9f8925 | 2021-07-21 10:04:38 -0600 | [diff] [blame] | 317 | if (callee_itr->GetCommonDebugOpcode() == CommonDebugInfoDebugDeclare) { |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 318 | InlineSingleInstruction( |
| 319 | callee2caller, new_blk_ptr->get(), &*callee_itr, |
| 320 | context()->get_debug_info_mgr()->BuildDebugInlinedAtChain( |
| 321 | callee_itr->GetDebugScope().GetInlinedAt(), inlined_at_ctx)); |
| 322 | } |
| 323 | ++callee_itr; |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 324 | } |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 325 | return callee_itr; |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 328 | bool InlinePass::InlineSingleInstruction( |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 329 | const std::unordered_map<uint32_t, uint32_t>& callee2caller, |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 330 | BasicBlock* new_blk_ptr, const Instruction* inst, uint32_t dbg_inlined_at) { |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 331 | // If we have return, it must be at the end of the callee. We will handle |
| 332 | // it at the end. |
| 333 | if (inst->opcode() == SpvOpReturnValue || inst->opcode() == SpvOpReturn) |
| 334 | return true; |
| 335 | |
| 336 | // Copy callee instruction and remap all input Ids. |
| 337 | std::unique_ptr<Instruction> cp_inst(inst->Clone(context())); |
| 338 | cp_inst->ForEachInId([&callee2caller](uint32_t* iid) { |
| 339 | const auto mapItr = callee2caller.find(*iid); |
| 340 | if (mapItr != callee2caller.end()) { |
| 341 | *iid = mapItr->second; |
| 342 | } |
| 343 | }); |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 344 | |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 345 | // If result id is non-zero, remap it. |
| 346 | const uint32_t rid = cp_inst->result_id(); |
| 347 | if (rid != 0) { |
| 348 | const auto mapItr = callee2caller.find(rid); |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 349 | if (mapItr == callee2caller.end()) { |
| 350 | return false; |
| 351 | } |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 352 | uint32_t nid = mapItr->second; |
| 353 | cp_inst->SetResultId(nid); |
| 354 | get_decoration_mgr()->CloneDecorations(rid, nid); |
| 355 | } |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 356 | |
| 357 | cp_inst->UpdateDebugInlinedAt(dbg_inlined_at); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 358 | new_blk_ptr->AddInstruction(std::move(cp_inst)); |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | std::unique_ptr<BasicBlock> InlinePass::InlineReturn( |
| 363 | const std::unordered_map<uint32_t, uint32_t>& callee2caller, |
| 364 | std::vector<std::unique_ptr<BasicBlock>>* new_blocks, |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 365 | std::unique_ptr<BasicBlock> new_blk_ptr, |
| 366 | analysis::DebugInlinedAtContext* inlined_at_ctx, Function* calleeFn, |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 367 | const Instruction* inst, uint32_t returnVarId) { |
| 368 | // Store return value to return variable. |
| 369 | if (inst->opcode() == SpvOpReturnValue) { |
| 370 | assert(returnVarId != 0); |
| 371 | uint32_t valId = inst->GetInOperand(kSpvReturnValueId).words[0]; |
| 372 | const auto mapItr = callee2caller.find(valId); |
| 373 | if (mapItr != callee2caller.end()) { |
| 374 | valId = mapItr->second; |
| 375 | } |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 376 | AddStore(returnVarId, valId, &new_blk_ptr, inst->dbg_line_inst(), |
| 377 | context()->get_debug_info_mgr()->BuildDebugScope( |
| 378 | inst->GetDebugScope(), inlined_at_ctx)); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | uint32_t returnLabelId = 0; |
| 382 | for (auto callee_block_itr = calleeFn->begin(); |
| 383 | callee_block_itr != calleeFn->end(); ++callee_block_itr) { |
Ehsan | cd05078 | 2020-12-07 09:26:05 -0600 | [diff] [blame] | 384 | if (spvOpcodeIsAbort(callee_block_itr->tail()->opcode())) { |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 385 | returnLabelId = context()->TakeNextId(); |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | if (returnLabelId == 0) return new_blk_ptr; |
| 390 | |
| 391 | if (inst->opcode() == SpvOpReturn || inst->opcode() == SpvOpReturnValue) |
| 392 | AddBranch(returnLabelId, &new_blk_ptr); |
| 393 | new_blocks->push_back(std::move(new_blk_ptr)); |
| 394 | return MakeUnique<BasicBlock>(NewLabel(returnLabelId)); |
| 395 | } |
| 396 | |
| 397 | bool InlinePass::InlineEntryBlock( |
| 398 | const std::unordered_map<uint32_t, uint32_t>& callee2caller, |
| 399 | std::unique_ptr<BasicBlock>* new_blk_ptr, |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 400 | UptrVectorIterator<BasicBlock> callee_first_block, |
| 401 | analysis::DebugInlinedAtContext* inlined_at_ctx) { |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 402 | auto callee_inst_itr = AddStoresForVariableInitializers( |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 403 | callee2caller, inlined_at_ctx, new_blk_ptr, callee_first_block); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 404 | |
| 405 | while (callee_inst_itr != callee_first_block->end()) { |
Greg Fischer | 983ee23 | 2021-07-28 19:35:32 -0600 | [diff] [blame^] | 406 | // Don't inline function definition links, the calling function is not a |
| 407 | // definition. |
| 408 | if (callee_inst_itr->GetVulkan100DebugOpcode() == |
| 409 | NonSemanticVulkanDebugInfo100DebugFunctionDefinition) { |
| 410 | ++callee_inst_itr; |
| 411 | continue; |
| 412 | } |
| 413 | |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 414 | if (!InlineSingleInstruction( |
| 415 | callee2caller, new_blk_ptr->get(), &*callee_inst_itr, |
| 416 | context()->get_debug_info_mgr()->BuildDebugInlinedAtChain( |
| 417 | callee_inst_itr->GetDebugScope().GetInlinedAt(), |
| 418 | inlined_at_ctx))) { |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | ++callee_inst_itr; |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | std::unique_ptr<BasicBlock> InlinePass::InlineBasicBlocks( |
| 427 | std::vector<std::unique_ptr<BasicBlock>>* new_blocks, |
| 428 | const std::unordered_map<uint32_t, uint32_t>& callee2caller, |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 429 | std::unique_ptr<BasicBlock> new_blk_ptr, |
| 430 | analysis::DebugInlinedAtContext* inlined_at_ctx, Function* calleeFn) { |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 431 | auto callee_block_itr = calleeFn->begin(); |
| 432 | ++callee_block_itr; |
| 433 | |
| 434 | while (callee_block_itr != calleeFn->end()) { |
| 435 | new_blocks->push_back(std::move(new_blk_ptr)); |
| 436 | const auto mapItr = |
| 437 | callee2caller.find(callee_block_itr->GetLabelInst()->result_id()); |
| 438 | if (mapItr == callee2caller.end()) return nullptr; |
| 439 | new_blk_ptr = MakeUnique<BasicBlock>(NewLabel(mapItr->second)); |
| 440 | |
| 441 | auto tail_inst_itr = callee_block_itr->end(); |
| 442 | for (auto inst_itr = callee_block_itr->begin(); inst_itr != tail_inst_itr; |
| 443 | ++inst_itr) { |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 444 | if (!InlineSingleInstruction( |
| 445 | callee2caller, new_blk_ptr.get(), &*inst_itr, |
| 446 | context()->get_debug_info_mgr()->BuildDebugInlinedAtChain( |
| 447 | inst_itr->GetDebugScope().GetInlinedAt(), inlined_at_ctx))) { |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 448 | return nullptr; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | ++callee_block_itr; |
| 453 | } |
| 454 | return new_blk_ptr; |
| 455 | } |
| 456 | |
| 457 | bool InlinePass::MoveCallerInstsAfterFunctionCall( |
| 458 | std::unordered_map<uint32_t, Instruction*>* preCallSB, |
| 459 | std::unordered_map<uint32_t, uint32_t>* postCallSB, |
| 460 | std::unique_ptr<BasicBlock>* new_blk_ptr, |
| 461 | BasicBlock::iterator call_inst_itr, bool multiBlocks) { |
| 462 | // Copy remaining instructions from caller block. |
| 463 | for (Instruction* inst = call_inst_itr->NextNode(); inst; |
| 464 | inst = call_inst_itr->NextNode()) { |
| 465 | inst->RemoveFromList(); |
| 466 | std::unique_ptr<Instruction> cp_inst(inst); |
| 467 | // If multiple blocks generated, regenerate any same-block |
| 468 | // instruction that has not been seen in this last block. |
| 469 | if (multiBlocks) { |
| 470 | if (!CloneSameBlockOps(&cp_inst, postCallSB, preCallSB, new_blk_ptr)) { |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | // Remember same-block ops in this block. |
| 475 | if (IsSameBlockOp(&*cp_inst)) { |
| 476 | const uint32_t rid = cp_inst->result_id(); |
| 477 | (*postCallSB)[rid] = rid; |
| 478 | } |
| 479 | } |
| 480 | new_blk_ptr->get()->AddInstruction(std::move(cp_inst)); |
| 481 | } |
| 482 | |
| 483 | return true; |
| 484 | } |
| 485 | |
| 486 | void InlinePass::MoveLoopMergeInstToFirstBlock( |
| 487 | std::vector<std::unique_ptr<BasicBlock>>* new_blocks) { |
| 488 | // Move the OpLoopMerge from the last block back to the first, where |
| 489 | // it belongs. |
| 490 | auto& first = new_blocks->front(); |
| 491 | auto& last = new_blocks->back(); |
| 492 | assert(first != last); |
| 493 | |
| 494 | // Insert a modified copy of the loop merge into the first block. |
| 495 | auto loop_merge_itr = last->tail(); |
| 496 | --loop_merge_itr; |
| 497 | assert(loop_merge_itr->opcode() == SpvOpLoopMerge); |
| 498 | std::unique_ptr<Instruction> cp_inst(loop_merge_itr->Clone(context())); |
| 499 | first->tail().InsertBefore(std::move(cp_inst)); |
| 500 | |
| 501 | // Remove the loop merge from the last block. |
| 502 | loop_merge_itr->RemoveFromList(); |
| 503 | delete &*loop_merge_itr; |
| 504 | } |
| 505 | |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 506 | bool InlinePass::GenInlineCode( |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 507 | std::vector<std::unique_ptr<BasicBlock>>* new_blocks, |
| 508 | std::vector<std::unique_ptr<Instruction>>* new_vars, |
| 509 | BasicBlock::iterator call_inst_itr, |
| 510 | UptrVectorIterator<BasicBlock> call_block_itr) { |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 511 | // Map from all ids in the callee to their equivalent id in the caller |
| 512 | // as callee instructions are copied into caller. |
| 513 | std::unordered_map<uint32_t, uint32_t> callee2caller; |
| 514 | // Pre-call same-block insts |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 515 | std::unordered_map<uint32_t, Instruction*> preCallSB; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 516 | // Post-call same-block op ids |
| 517 | std::unordered_map<uint32_t, uint32_t> postCallSB; |
| 518 | |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 519 | analysis::DebugInlinedAtContext inlined_at_ctx(&*call_inst_itr); |
| 520 | |
Steven Perron | b3daa93 | 2018-03-06 11:20:28 -0500 | [diff] [blame] | 521 | // Invalidate the def-use chains. They are not kept up to date while |
| 522 | // inlining. However, certain calls try to keep them up-to-date if they are |
| 523 | // valid. These operations can fail. |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 524 | context()->InvalidateAnalyses(IRContext::kAnalysisDefUse); |
Steven Perron | b3daa93 | 2018-03-06 11:20:28 -0500 | [diff] [blame] | 525 | |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 526 | // If the caller is a loop header and the callee has multiple blocks, then the |
| 527 | // normal inlining logic will place the OpLoopMerge in the last of several |
| 528 | // blocks in the loop. Instead, it should be placed at the end of the first |
| 529 | // block. We'll wait to move the OpLoopMerge until the end of the regular |
| 530 | // inlining logic, and only if necessary. |
| 531 | bool caller_is_loop_header = call_block_itr->GetLoopMergeInst() != nullptr; |
| 532 | |
| 533 | // Single-trip loop continue block |
| 534 | std::unique_ptr<BasicBlock> single_trip_loop_cont_blk; |
| 535 | |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 536 | Function* calleeFn = id2function_[call_inst_itr->GetSingleWordOperand( |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 537 | kSpvFunctionCallFunctionId)]; |
| 538 | |
| 539 | // Map parameters to actual arguments. |
| 540 | MapParams(calleeFn, call_inst_itr, &callee2caller); |
| 541 | |
| 542 | // Define caller local variables for all callee variables and create map to |
| 543 | // them. |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 544 | if (!CloneAndMapLocals(calleeFn, new_vars, &callee2caller, &inlined_at_ctx)) { |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 545 | return false; |
| 546 | } |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 547 | |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 548 | // First block needs to use label of original block |
| 549 | // but map callee label in case of phi reference. |
| 550 | uint32_t entry_blk_label_id = calleeFn->begin()->GetLabelInst()->result_id(); |
| 551 | callee2caller[entry_blk_label_id] = call_block_itr->id(); |
| 552 | std::unique_ptr<BasicBlock> new_blk_ptr = |
| 553 | MakeUnique<BasicBlock>(NewLabel(call_block_itr->id())); |
| 554 | |
| 555 | // Move instructions of original caller block up to call instruction. |
| 556 | MoveInstsBeforeEntryBlock(&preCallSB, new_blk_ptr.get(), call_inst_itr, |
| 557 | call_block_itr); |
| 558 | |
| 559 | if (caller_is_loop_header && |
| 560 | (*(calleeFn->begin())).GetMergeInst() != nullptr) { |
| 561 | // We can't place both the caller's merge instruction and |
| 562 | // another merge instruction in the same block. So split the |
| 563 | // calling block. Insert an unconditional branch to a new guard |
| 564 | // block. Later, once we know the ID of the last block, we |
| 565 | // will move the caller's OpLoopMerge from the last generated |
| 566 | // block into the first block. We also wait to avoid |
| 567 | // invalidating various iterators. |
| 568 | new_blk_ptr = AddGuardBlock(new_blocks, &callee2caller, |
| 569 | std::move(new_blk_ptr), entry_blk_label_id); |
| 570 | if (new_blk_ptr == nullptr) return false; |
| 571 | } |
| 572 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 573 | // Create return var if needed. |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 574 | const uint32_t calleeTypeId = calleeFn->type_id(); |
| 575 | uint32_t returnVarId = 0; |
| 576 | analysis::Type* calleeType = context()->get_type_mgr()->GetType(calleeTypeId); |
| 577 | if (calleeType->AsVoid() == nullptr) { |
| 578 | returnVarId = CreateReturnVar(calleeFn, new_vars); |
| 579 | if (returnVarId == 0) { |
| 580 | return false; |
| 581 | } |
| 582 | } |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 583 | |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 584 | calleeFn->WhileEachInst([&callee2caller, this](const Instruction* cpi) { |
| 585 | // Create set of callee result ids. Used to detect forward references |
GregF | a699d1a | 2017-08-29 18:35:05 -0600 | [diff] [blame] | 586 | const uint32_t rid = cpi->result_id(); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 587 | if (rid != 0 && callee2caller.find(rid) == callee2caller.end()) { |
| 588 | const uint32_t nid = context()->TakeNextId(); |
| 589 | if (nid == 0) return false; |
| 590 | callee2caller[rid] = nid; |
| 591 | } |
| 592 | return true; |
GregF | a699d1a | 2017-08-29 18:35:05 -0600 | [diff] [blame] | 593 | }); |
| 594 | |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 595 | // Inline DebugClare instructions in the callee's header. |
| 596 | calleeFn->ForEachDebugInstructionsInHeader( |
| 597 | [&new_blk_ptr, &callee2caller, &inlined_at_ctx, this](Instruction* inst) { |
| 598 | InlineSingleInstruction( |
| 599 | callee2caller, new_blk_ptr.get(), inst, |
| 600 | context()->get_debug_info_mgr()->BuildDebugInlinedAtChain( |
| 601 | inst->GetDebugScope().GetInlinedAt(), &inlined_at_ctx)); |
| 602 | }); |
| 603 | |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 604 | // Inline the entry block of the callee function. |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 605 | if (!InlineEntryBlock(callee2caller, &new_blk_ptr, calleeFn->begin(), |
| 606 | &inlined_at_ctx)) { |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 607 | return false; |
| 608 | } |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 609 | |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 610 | // Inline blocks of the callee function other than the entry block. |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 611 | new_blk_ptr = |
| 612 | InlineBasicBlocks(new_blocks, callee2caller, std::move(new_blk_ptr), |
| 613 | &inlined_at_ctx, calleeFn); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 614 | if (new_blk_ptr == nullptr) return false; |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 615 | |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 616 | new_blk_ptr = InlineReturn(callee2caller, new_blocks, std::move(new_blk_ptr), |
| 617 | &inlined_at_ctx, calleeFn, |
| 618 | &*(calleeFn->tail()->tail()), returnVarId); |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 619 | |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 620 | // Load return value into result id of call, if it exists. |
| 621 | if (returnVarId != 0) { |
| 622 | const uint32_t resId = call_inst_itr->result_id(); |
| 623 | assert(resId != 0); |
Jaebaek Seo | 50b1557 | 2020-05-21 13:09:43 -0400 | [diff] [blame] | 624 | AddLoad(calleeTypeId, resId, returnVarId, &new_blk_ptr, |
| 625 | call_inst_itr->dbg_line_inst(), call_inst_itr->GetDebugScope()); |
David Neto | efff5fa | 2017-08-31 15:47:31 -0400 | [diff] [blame] | 626 | } |
| 627 | |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 628 | // Move instructions of original caller block after call instruction. |
| 629 | if (!MoveCallerInstsAfterFunctionCall(&preCallSB, &postCallSB, &new_blk_ptr, |
| 630 | call_inst_itr, |
| 631 | calleeFn->begin() != calleeFn->end())) |
| 632 | return false; |
| 633 | |
| 634 | // Finalize inline code. |
| 635 | new_blocks->push_back(std::move(new_blk_ptr)); |
| 636 | |
| 637 | if (caller_is_loop_header && (new_blocks->size() > 1)) |
| 638 | MoveLoopMergeInstToFirstBlock(new_blocks); |
| 639 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 640 | // Update block map given replacement blocks. |
| 641 | for (auto& blk : *new_blocks) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 642 | id2block_[blk->id()] = &*blk; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 643 | } |
Alastair Donaldson | 1299436 | 2020-11-27 16:31:04 +0000 | [diff] [blame] | 644 | |
| 645 | // We need to kill the name and decorations for the call, which will be |
| 646 | // deleted. |
| 647 | context()->KillNamesAndDecorates(&*call_inst_itr); |
| 648 | |
Steven Perron | acd2781 | 2018-12-18 19:34:03 +0000 | [diff] [blame] | 649 | return true; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Steven Perron | aa9e8f5 | 2019-07-17 14:59:05 -0400 | [diff] [blame] | 652 | bool InlinePass::IsInlinableFunctionCall(const Instruction* inst) { |
David Neto | ceb1d4f | 2017-03-31 10:36:58 -0400 | [diff] [blame] | 653 | if (inst->opcode() != SpvOp::SpvOpFunctionCall) return false; |
GregF | a107d34 | 2017-04-25 13:57:20 -0600 | [diff] [blame] | 654 | const uint32_t calleeFnId = |
| 655 | inst->GetSingleWordOperand(kSpvFunctionCallFunctionId); |
| 656 | const auto ci = inlinable_.find(calleeFnId); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 657 | if (ci == inlinable_.cend()) return false; |
| 658 | |
| 659 | if (early_return_funcs_.find(calleeFnId) != early_return_funcs_.end()) { |
| 660 | // We rely on the merge-return pass to handle the early return case |
| 661 | // in advance. |
| 662 | std::string message = |
| 663 | "The function '" + id2function_[calleeFnId]->DefInst().PrettyPrint() + |
| 664 | "' could not be inlined because the return instruction " |
| 665 | "is not at the end of the function. This could be fixed by " |
| 666 | "running merge-return before inlining."; |
| 667 | consumer()(SPV_MSG_WARNING, "", {0, 0, 0}, message.c_str()); |
| 668 | return false; |
| 669 | } |
| 670 | |
| 671 | return true; |
David Neto | ceb1d4f | 2017-03-31 10:36:58 -0400 | [diff] [blame] | 672 | } |
| 673 | |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 674 | void InlinePass::UpdateSucceedingPhis( |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 675 | std::vector<std::unique_ptr<BasicBlock>>& new_blocks) { |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 676 | const auto firstBlk = new_blocks.begin(); |
| 677 | const auto lastBlk = new_blocks.end() - 1; |
| 678 | const uint32_t firstId = (*firstBlk)->id(); |
| 679 | const uint32_t lastId = (*lastBlk)->id(); |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 680 | const BasicBlock& const_last_block = *lastBlk->get(); |
David Neto | 87f9cfa | 2018-02-02 14:17:42 -0800 | [diff] [blame] | 681 | const_last_block.ForEachSuccessorLabel( |
| 682 | [&firstId, &lastId, this](const uint32_t succ) { |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 683 | BasicBlock* sbp = this->id2block_[succ]; |
| 684 | sbp->ForEachPhiInst([&firstId, &lastId](Instruction* phi) { |
David Neto | 87f9cfa | 2018-02-02 14:17:42 -0800 | [diff] [blame] | 685 | phi->ForEachInId([&firstId, &lastId](uint32_t* id) { |
| 686 | if (*id == firstId) *id = lastId; |
| 687 | }); |
| 688 | }); |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 689 | }); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 690 | } |
| 691 | |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 692 | bool InlinePass::HasNoReturnInLoop(Function* func) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 693 | // If control not structured, do not do loop/return analysis |
| 694 | // TODO: Analyze returns in non-structured control flow |
Steven Perron | 756b277 | 2017-12-19 14:18:13 -0500 | [diff] [blame] | 695 | if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader)) |
| 696 | return false; |
greg-lunarg | 6721478 | 2018-11-08 07:11:20 -0700 | [diff] [blame] | 697 | const auto structured_analysis = context()->GetStructuredCFGAnalysis(); |
| 698 | // Search for returns in structured construct. |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 699 | bool return_in_loop = false; |
greg-lunarg | 6721478 | 2018-11-08 07:11:20 -0700 | [diff] [blame] | 700 | for (auto& blk : *func) { |
| 701 | auto terminal_ii = blk.cend(); |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 702 | --terminal_ii; |
greg-lunarg | 6721478 | 2018-11-08 07:11:20 -0700 | [diff] [blame] | 703 | if (spvOpcodeIsReturn(terminal_ii->opcode()) && |
| 704 | structured_analysis->ContainingLoop(blk.id()) != 0) { |
| 705 | return_in_loop = true; |
| 706 | break; |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | return !return_in_loop; |
| 710 | } |
| 711 | |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 712 | void InlinePass::AnalyzeReturns(Function* func) { |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 713 | // Analyze functions without a return in loop. |
greg-lunarg | 6721478 | 2018-11-08 07:11:20 -0700 | [diff] [blame] | 714 | if (HasNoReturnInLoop(func)) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 715 | no_return_in_loop_.insert(func->result_id()); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 716 | } |
| 717 | // Analyze functions with a return before its tail basic block. |
| 718 | for (auto& blk : *func) { |
| 719 | auto terminal_ii = blk.cend(); |
| 720 | --terminal_ii; |
| 721 | if (spvOpcodeIsReturn(terminal_ii->opcode()) && &blk != func->tail()) { |
greg-lunarg | 6721478 | 2018-11-08 07:11:20 -0700 | [diff] [blame] | 722 | early_return_funcs_.insert(func->result_id()); |
Steven Perron | bd0a2da | 2020-05-14 10:55:47 -0400 | [diff] [blame] | 723 | break; |
| 724 | } |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 725 | } |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 726 | } |
| 727 | |
dan sinclair | c7da51a | 2018-07-12 15:14:43 -0400 | [diff] [blame] | 728 | bool InlinePass::IsInlinableFunction(Function* func) { |
GregF | a107d34 | 2017-04-25 13:57:20 -0600 | [diff] [blame] | 729 | // We can only inline a function if it has blocks. |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 730 | if (func->cbegin() == func->cend()) return false; |
Junda Liu | 82b378d | 2020-10-30 04:38:56 +0800 | [diff] [blame] | 731 | |
| 732 | // Do not inline functions with DontInline flag. |
| 733 | if (func->control_mask() & SpvFunctionControlDontInlineMask) { |
| 734 | return false; |
| 735 | } |
| 736 | |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 737 | // Do not inline functions with returns in loops. Currently early return |
| 738 | // functions are inlined by wrapping them in a one trip loop and implementing |
| 739 | // the returns as a branch to the loop's merge block. However, this can only |
| 740 | // done validly if the return was not in a loop in the original function. |
| 741 | // Also remember functions with multiple (early) returns. |
| 742 | AnalyzeReturns(func); |
Steven Perron | 2d2a512 | 2018-11-29 14:24:58 -0500 | [diff] [blame] | 743 | if (no_return_in_loop_.find(func->result_id()) == no_return_in_loop_.cend()) { |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | if (func->IsRecursive()) { |
| 748 | return false; |
| 749 | } |
| 750 | |
Steven Perron | c18c9ff | 2019-10-04 13:05:32 -0400 | [diff] [blame] | 751 | // Do not inline functions with an OpKill if they are called from a continue |
| 752 | // construct. If it is inlined into a continue construct it will generate |
| 753 | // invalid code. |
| 754 | bool func_is_called_from_continue = |
| 755 | funcs_called_from_continue_.count(func->result_id()) != 0; |
Steven Perron | c7a39bc | 2019-09-11 13:26:55 -0400 | [diff] [blame] | 756 | |
alan-baker | f3cec93 | 2020-07-22 11:45:02 -0400 | [diff] [blame] | 757 | if (func_is_called_from_continue && ContainsKillOrTerminateInvocation(func)) { |
Steven Perron | c7a39bc | 2019-09-11 13:26:55 -0400 | [diff] [blame] | 758 | return false; |
| 759 | } |
| 760 | |
Steven Perron | 2d2a512 | 2018-11-29 14:24:58 -0500 | [diff] [blame] | 761 | return true; |
GregF | a107d34 | 2017-04-25 13:57:20 -0600 | [diff] [blame] | 762 | } |
| 763 | |
alan-baker | f3cec93 | 2020-07-22 11:45:02 -0400 | [diff] [blame] | 764 | bool InlinePass::ContainsKillOrTerminateInvocation(Function* func) const { |
| 765 | return !func->WhileEachInst([](Instruction* inst) { |
Ehsan | cd05078 | 2020-12-07 09:26:05 -0600 | [diff] [blame] | 766 | return !spvOpcodeTerminatesExecution(inst->opcode()); |
alan-baker | f3cec93 | 2020-07-22 11:45:02 -0400 | [diff] [blame] | 767 | }); |
Steven Perron | c18c9ff | 2019-10-04 13:05:32 -0400 | [diff] [blame] | 768 | } |
| 769 | |
dan sinclair | f96b7f1 | 2018-07-12 09:08:45 -0400 | [diff] [blame] | 770 | void InlinePass::InitializeInline() { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 771 | false_id_ = 0; |
| 772 | |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 773 | // clear collections |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 774 | id2function_.clear(); |
| 775 | id2block_.clear(); |
GregF | a107d34 | 2017-04-25 13:57:20 -0600 | [diff] [blame] | 776 | inlinable_.clear(); |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 777 | no_return_in_loop_.clear(); |
greg-lunarg | 6721478 | 2018-11-08 07:11:20 -0700 | [diff] [blame] | 778 | early_return_funcs_.clear(); |
Steven Perron | c18c9ff | 2019-10-04 13:05:32 -0400 | [diff] [blame] | 779 | funcs_called_from_continue_ = |
| 780 | context()->GetStructuredCFGAnalysis()->FindFuncsCalledFromContinue(); |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 781 | |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 782 | for (auto& fn : *get_module()) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 783 | // Initialize function and block maps. |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 784 | id2function_[fn.result_id()] = &fn; |
| 785 | for (auto& blk : fn) { |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 786 | id2block_[blk.id()] = &blk; |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 787 | } |
Greg Fischer | bba812f | 2017-05-04 20:55:53 -0600 | [diff] [blame] | 788 | // Compute inlinability |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 789 | if (IsInlinableFunction(&fn)) inlinable_.insert(fn.result_id()); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 790 | } |
Eleni Maria Stea | 045cc8f | 2018-03-21 11:15:56 +0200 | [diff] [blame] | 791 | } |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 792 | |
Diego Novillo | 1040a95 | 2017-10-25 13:26:25 -0400 | [diff] [blame] | 793 | InlinePass::InlinePass() {} |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 794 | |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 795 | } // namespace opt |
| 796 | } // namespace spvtools |