blob: 3c874a7ef00942fa17a53807aac8808b1d41bd23 [file] [log] [blame]
Greg Fischer04fcc662016-11-10 10:11:50 -07001// 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 sinclaireda2cfb2018-08-03 15:06:09 -040017#include "source/opt/inline_pass.h"
GregFe28bd392017-08-01 17:20:13 -060018
dan sinclaireda2cfb2018-08-03 15:06:09 -040019#include <unordered_set>
20#include <utility>
21
22#include "source/cfa.h"
dan sinclair1963a2d2018-08-14 15:01:50 -040023#include "source/util/make_unique.h"
Greg Fischer04fcc662016-11-10 10:11:50 -070024
25// Indices of operands in SPIR-V instructions
26
Greg Fischer04fcc662016-11-10 10:11:50 -070027static const int kSpvFunctionCallFunctionId = 2;
28static const int kSpvFunctionCallArgumentId = 3;
29static const int kSpvReturnValueId = 0;
Greg Fischer04fcc662016-11-10 10:11:50 -070030
31namespace spvtools {
32namespace opt {
33
Greg Fischer04fcc662016-11-10 10:11:50 -070034uint32_t InlinePass::AddPointerToType(uint32_t type_id,
35 SpvStorageClass storage_class) {
Steven Perronacd27812018-12-18 19:34:03 +000036 uint32_t resultId = context()->TakeNextId();
37 if (resultId == 0) {
38 return resultId;
39 }
40
dan sinclairc7da51a2018-07-12 15:14:43 -040041 std::unique_ptr<Instruction> type_inst(
42 new Instruction(context(), SpvOpTypePointer, 0, resultId,
43 {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS,
44 {uint32_t(storage_class)}},
45 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {type_id}}}));
Steven Perron476cae62017-10-30 11:13:24 -040046 context()->AddType(std::move(type_inst));
Alan Baker61690852017-12-08 15:33:19 -050047 analysis::Type* pointeeTy;
48 std::unique_ptr<analysis::Pointer> pointerTy;
49 std::tie(pointeeTy, pointerTy) =
50 context()->get_type_mgr()->GetTypeAndPointerType(type_id,
51 SpvStorageClassFunction);
52 context()->get_type_mgr()->RegisterType(resultId, *pointerTy);
Greg Fischer04fcc662016-11-10 10:11:50 -070053 return resultId;
54}
55
56void InlinePass::AddBranch(uint32_t label_id,
dan sinclairc7da51a2018-07-12 15:14:43 -040057 std::unique_ptr<BasicBlock>* block_ptr) {
58 std::unique_ptr<Instruction> newBranch(
59 new Instruction(context(), SpvOpBranch, 0, 0,
60 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {label_id}}}));
Greg Fischer04fcc662016-11-10 10:11:50 -070061 (*block_ptr)->AddInstruction(std::move(newBranch));
62}
63
Greg Fischerbba812f2017-05-04 20:55:53 -060064void InlinePass::AddBranchCond(uint32_t cond_id, uint32_t true_id,
Diego Novillod2938e42017-11-08 12:40:02 -050065 uint32_t false_id,
dan sinclairc7da51a2018-07-12 15:14:43 -040066 std::unique_ptr<BasicBlock>* block_ptr) {
67 std::unique_ptr<Instruction> newBranch(
68 new Instruction(context(), SpvOpBranchConditional, 0, 0,
69 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {cond_id}},
70 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {true_id}},
71 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {false_id}}}));
Greg Fischerbba812f2017-05-04 20:55:53 -060072 (*block_ptr)->AddInstruction(std::move(newBranch));
73}
74
75void InlinePass::AddLoopMerge(uint32_t merge_id, uint32_t continue_id,
dan sinclairc7da51a2018-07-12 15:14:43 -040076 std::unique_ptr<BasicBlock>* block_ptr) {
77 std::unique_ptr<Instruction> newLoopMerge(new Instruction(
Alan Bakera7717132017-11-14 14:11:50 -050078 context(), SpvOpLoopMerge, 0, 0,
Greg Fischerbba812f2017-05-04 20:55:53 -060079 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {merge_id}},
80 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {continue_id}},
81 {spv_operand_type_t::SPV_OPERAND_TYPE_LOOP_CONTROL, {0}}}));
82 (*block_ptr)->AddInstruction(std::move(newLoopMerge));
83}
84
Greg Fischer04fcc662016-11-10 10:11:50 -070085void InlinePass::AddStore(uint32_t ptr_id, uint32_t val_id,
dan sinclairc7da51a2018-07-12 15:14:43 -040086 std::unique_ptr<BasicBlock>* block_ptr) {
87 std::unique_ptr<Instruction> newStore(
88 new Instruction(context(), SpvOpStore, 0, 0,
89 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}},
90 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {val_id}}}));
Greg Fischer04fcc662016-11-10 10:11:50 -070091 (*block_ptr)->AddInstruction(std::move(newStore));
92}
93
94void InlinePass::AddLoad(uint32_t type_id, uint32_t resultId, uint32_t ptr_id,
dan sinclairc7da51a2018-07-12 15:14:43 -040095 std::unique_ptr<BasicBlock>* block_ptr) {
96 std::unique_ptr<Instruction> newLoad(
97 new Instruction(context(), SpvOpLoad, type_id, resultId,
98 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ptr_id}}}));
Greg Fischer04fcc662016-11-10 10:11:50 -070099 (*block_ptr)->AddInstruction(std::move(newLoad));
100}
101
dan sinclairc7da51a2018-07-12 15:14:43 -0400102std::unique_ptr<Instruction> InlinePass::NewLabel(uint32_t label_id) {
103 std::unique_ptr<Instruction> newLabel(
104 new Instruction(context(), SpvOpLabel, 0, label_id, {}));
Greg Fischer04fcc662016-11-10 10:11:50 -0700105 return newLabel;
106}
107
Greg Fischerbba812f2017-05-04 20:55:53 -0600108uint32_t InlinePass::GetFalseId() {
Diego Novillod2938e42017-11-08 12:40:02 -0500109 if (false_id_ != 0) return false_id_;
Diego Novillo1040a952017-10-25 13:26:25 -0400110 false_id_ = get_module()->GetGlobalValue(SpvOpConstantFalse);
Diego Novillod2938e42017-11-08 12:40:02 -0500111 if (false_id_ != 0) return false_id_;
Diego Novillo1040a952017-10-25 13:26:25 -0400112 uint32_t boolId = get_module()->GetGlobalValue(SpvOpTypeBool);
Greg Fischerbba812f2017-05-04 20:55:53 -0600113 if (boolId == 0) {
Steven Perronacd27812018-12-18 19:34:03 +0000114 boolId = context()->TakeNextId();
115 if (boolId == 0) {
116 return 0;
117 }
Diego Novillo1040a952017-10-25 13:26:25 -0400118 get_module()->AddGlobalValue(SpvOpTypeBool, boolId, 0);
Greg Fischerbba812f2017-05-04 20:55:53 -0600119 }
Steven Perronacd27812018-12-18 19:34:03 +0000120 false_id_ = context()->TakeNextId();
121 if (false_id_ == 0) {
122 return 0;
123 }
Diego Novillo1040a952017-10-25 13:26:25 -0400124 get_module()->AddGlobalValue(SpvOpConstantFalse, false_id_, boolId);
Greg Fischerbba812f2017-05-04 20:55:53 -0600125 return false_id_;
126}
127
Greg Fischer04fcc662016-11-10 10:11:50 -0700128void InlinePass::MapParams(
dan sinclairc7da51a2018-07-12 15:14:43 -0400129 Function* calleeFn, BasicBlock::iterator call_inst_itr,
Greg Fischer04fcc662016-11-10 10:11:50 -0700130 std::unordered_map<uint32_t, uint32_t>* callee2caller) {
131 int param_idx = 0;
Steven Perronacd27812018-12-18 19:34:03 +0000132 calleeFn->ForEachParam(
133 [&call_inst_itr, &param_idx, &callee2caller](const Instruction* cpi) {
134 const uint32_t pid = cpi->result_id();
135 (*callee2caller)[pid] = call_inst_itr->GetSingleWordOperand(
136 kSpvFunctionCallArgumentId + param_idx);
137 ++param_idx;
138 });
Greg Fischer04fcc662016-11-10 10:11:50 -0700139}
140
Steven Perronacd27812018-12-18 19:34:03 +0000141bool InlinePass::CloneAndMapLocals(
dan sinclairc7da51a2018-07-12 15:14:43 -0400142 Function* calleeFn, std::vector<std::unique_ptr<Instruction>>* new_vars,
Greg Fischer04fcc662016-11-10 10:11:50 -0700143 std::unordered_map<uint32_t, uint32_t>* callee2caller) {
144 auto callee_block_itr = calleeFn->begin();
145 auto callee_var_itr = callee_block_itr->begin();
146 while (callee_var_itr->opcode() == SpvOp::SpvOpVariable) {
dan sinclairc7da51a2018-07-12 15:14:43 -0400147 std::unique_ptr<Instruction> var_inst(callee_var_itr->Clone(context()));
Steven Perronacd27812018-12-18 19:34:03 +0000148 uint32_t newId = context()->TakeNextId();
149 if (newId == 0) {
150 return false;
151 }
Pierre Moreau5bd55f12018-02-20 19:19:57 +0100152 get_decoration_mgr()->CloneDecorations(callee_var_itr->result_id(), newId);
Greg Fischer04fcc662016-11-10 10:11:50 -0700153 var_inst->SetResultId(newId);
154 (*callee2caller)[callee_var_itr->result_id()] = newId;
155 new_vars->push_back(std::move(var_inst));
Greg Fischerbba812f2017-05-04 20:55:53 -0600156 ++callee_var_itr;
Greg Fischer04fcc662016-11-10 10:11:50 -0700157 }
Steven Perronacd27812018-12-18 19:34:03 +0000158 return true;
Greg Fischer04fcc662016-11-10 10:11:50 -0700159}
160
161uint32_t InlinePass::CreateReturnVar(
dan sinclairc7da51a2018-07-12 15:14:43 -0400162 Function* calleeFn, std::vector<std::unique_ptr<Instruction>>* new_vars) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700163 uint32_t returnVarId = 0;
164 const uint32_t calleeTypeId = calleeFn->type_id();
Steven Perronacd27812018-12-18 19:34:03 +0000165 analysis::TypeManager* type_mgr = context()->get_type_mgr();
166 assert(type_mgr->GetType(calleeTypeId)->AsVoid() == nullptr &&
167 "Cannot create a return variable of type void.");
168 // Find or create ptr to callee return type.
169 uint32_t returnVarTypeId =
170 type_mgr->FindPointerToType(calleeTypeId, SpvStorageClassFunction);
171
172 if (returnVarTypeId == 0) {
173 returnVarTypeId = AddPointerToType(calleeTypeId, SpvStorageClassFunction);
174 if (returnVarTypeId == 0) {
175 return 0;
176 }
Greg Fischer04fcc662016-11-10 10:11:50 -0700177 }
Steven Perronacd27812018-12-18 19:34:03 +0000178
179 // Add return var to new function scope variables.
180 returnVarId = context()->TakeNextId();
181 if (returnVarId == 0) {
182 return 0;
183 }
184
185 std::unique_ptr<Instruction> var_inst(
186 new Instruction(context(), SpvOpVariable, returnVarTypeId, returnVarId,
187 {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS,
188 {SpvStorageClassFunction}}}));
189 new_vars->push_back(std::move(var_inst));
Pierre Moreau5bd55f12018-02-20 19:19:57 +0100190 get_decoration_mgr()->CloneDecorations(calleeFn->result_id(), returnVarId);
Greg Fischer04fcc662016-11-10 10:11:50 -0700191 return returnVarId;
192}
193
dan sinclairc7da51a2018-07-12 15:14:43 -0400194bool InlinePass::IsSameBlockOp(const Instruction* inst) const {
Greg Fischer04fcc662016-11-10 10:11:50 -0700195 return inst->opcode() == SpvOpSampledImage || inst->opcode() == SpvOpImage;
196}
197
Steven Perronacd27812018-12-18 19:34:03 +0000198bool InlinePass::CloneSameBlockOps(
dan sinclairc7da51a2018-07-12 15:14:43 -0400199 std::unique_ptr<Instruction>* inst,
Greg Fischer04fcc662016-11-10 10:11:50 -0700200 std::unordered_map<uint32_t, uint32_t>* postCallSB,
dan sinclairc7da51a2018-07-12 15:14:43 -0400201 std::unordered_map<uint32_t, Instruction*>* preCallSB,
202 std::unique_ptr<BasicBlock>* block_ptr) {
Steven Perronacd27812018-12-18 19:34:03 +0000203 return (*inst)->WhileEachInId([&postCallSB, &preCallSB, &block_ptr,
204 this](uint32_t* iid) {
205 const auto mapItr = (*postCallSB).find(*iid);
206 if (mapItr == (*postCallSB).end()) {
207 const auto mapItr2 = (*preCallSB).find(*iid);
208 if (mapItr2 != (*preCallSB).end()) {
209 // Clone pre-call same-block ops, map result id.
210 const Instruction* inInst = mapItr2->second;
211 std::unique_ptr<Instruction> sb_inst(inInst->Clone(context()));
212 if (!CloneSameBlockOps(&sb_inst, postCallSB, preCallSB, block_ptr)) {
213 return false;
Pierre Moreau5bd55f12018-02-20 19:19:57 +0100214 }
Steven Perronacd27812018-12-18 19:34:03 +0000215
216 const uint32_t rid = sb_inst->result_id();
217 const uint32_t nid = context()->TakeNextId();
218 if (nid == 0) {
219 return false;
220 }
221 get_decoration_mgr()->CloneDecorations(rid, nid);
222 sb_inst->SetResultId(nid);
223 (*postCallSB)[rid] = nid;
224 *iid = nid;
225 (*block_ptr)->AddInstruction(std::move(sb_inst));
226 }
227 } else {
228 // Reset same-block op operand.
229 *iid = mapItr->second;
230 }
231 return true;
232 });
Greg Fischer04fcc662016-11-10 10:11:50 -0700233}
234
Steven Perronacd27812018-12-18 19:34:03 +0000235bool InlinePass::GenInlineCode(
dan sinclairc7da51a2018-07-12 15:14:43 -0400236 std::vector<std::unique_ptr<BasicBlock>>* new_blocks,
237 std::vector<std::unique_ptr<Instruction>>* new_vars,
238 BasicBlock::iterator call_inst_itr,
239 UptrVectorIterator<BasicBlock> call_block_itr) {
Greg Fischer04fcc662016-11-10 10:11:50 -0700240 // Map from all ids in the callee to their equivalent id in the caller
241 // as callee instructions are copied into caller.
242 std::unordered_map<uint32_t, uint32_t> callee2caller;
243 // Pre-call same-block insts
dan sinclairc7da51a2018-07-12 15:14:43 -0400244 std::unordered_map<uint32_t, Instruction*> preCallSB;
Greg Fischer04fcc662016-11-10 10:11:50 -0700245 // Post-call same-block op ids
246 std::unordered_map<uint32_t, uint32_t> postCallSB;
247
Steven Perronb3daa932018-03-06 11:20:28 -0500248 // Invalidate the def-use chains. They are not kept up to date while
249 // inlining. However, certain calls try to keep them up-to-date if they are
250 // valid. These operations can fail.
dan sinclairc7da51a2018-07-12 15:14:43 -0400251 context()->InvalidateAnalyses(IRContext::kAnalysisDefUse);
Steven Perronb3daa932018-03-06 11:20:28 -0500252
dan sinclairc7da51a2018-07-12 15:14:43 -0400253 Function* calleeFn = id2function_[call_inst_itr->GetSingleWordOperand(
Greg Fischer04fcc662016-11-10 10:11:50 -0700254 kSpvFunctionCallFunctionId)];
255
Steven Perrond4fac342020-05-13 23:44:19 -0400256 // Check for multiple returns in the callee.
257 auto fi = early_return_funcs_.find(calleeFn->result_id());
258 const bool earlyReturn = fi != early_return_funcs_.end();
259
Greg Fischer04fcc662016-11-10 10:11:50 -0700260 // Map parameters to actual arguments.
261 MapParams(calleeFn, call_inst_itr, &callee2caller);
262
263 // Define caller local variables for all callee variables and create map to
264 // them.
Steven Perronacd27812018-12-18 19:34:03 +0000265 if (!CloneAndMapLocals(calleeFn, new_vars, &callee2caller)) {
266 return false;
267 }
Greg Fischer04fcc662016-11-10 10:11:50 -0700268
269 // Create return var if needed.
Steven Perronacd27812018-12-18 19:34:03 +0000270 const uint32_t calleeTypeId = calleeFn->type_id();
271 uint32_t returnVarId = 0;
272 analysis::Type* calleeType = context()->get_type_mgr()->GetType(calleeTypeId);
273 if (calleeType->AsVoid() == nullptr) {
274 returnVarId = CreateReturnVar(calleeFn, new_vars);
275 if (returnVarId == 0) {
276 return false;
277 }
278 }
Greg Fischer04fcc662016-11-10 10:11:50 -0700279
Steven Perrond4fac342020-05-13 23:44:19 -0400280 // Create set of callee result ids. Used to detect forward references
281 std::unordered_set<uint32_t> callee_result_ids;
282 calleeFn->ForEachInst([&callee_result_ids](const Instruction* cpi) {
GregFa699d1a2017-08-29 18:35:05 -0600283 const uint32_t rid = cpi->result_id();
Steven Perrond4fac342020-05-13 23:44:19 -0400284 if (rid != 0) callee_result_ids.insert(rid);
GregFa699d1a2017-08-29 18:35:05 -0600285 });
286
Steven Perrond4fac342020-05-13 23:44:19 -0400287 // If the caller is a loop header and the callee has multiple blocks, then the
288 // normal inlining logic will place the OpLoopMerge in the last of several
289 // blocks in the loop. Instead, it should be placed at the end of the first
290 // block. We'll wait to move the OpLoopMerge until the end of the regular
291 // inlining logic, and only if necessary.
292 bool caller_is_loop_header = false;
293 if (call_block_itr->GetLoopMergeInst()) {
294 caller_is_loop_header = true;
295 }
296
297 bool callee_begins_with_structured_header =
298 (*(calleeFn->begin())).GetMergeInst() != nullptr;
299
300 // Clone and map callee code. Copy caller block code to beginning of
301 // first block and end of last block.
302 bool prevInstWasReturn = false;
303 uint32_t singleTripLoopHeaderId = 0;
304 uint32_t singleTripLoopContinueId = 0;
305 uint32_t returnLabelId = 0;
306 bool multiBlocks = false;
307 // new_blk_ptr is a new basic block in the caller. New instructions are
308 // written to it. It is created when we encounter the OpLabel
309 // of the first callee block. It is appended to new_blocks only when
310 // it is complete.
311 std::unique_ptr<BasicBlock> new_blk_ptr;
312 bool successful = calleeFn->WhileEachInst(
313 [&new_blocks, &callee2caller, &call_block_itr, &call_inst_itr,
314 &new_blk_ptr, &prevInstWasReturn, &returnLabelId, &returnVarId,
315 caller_is_loop_header, callee_begins_with_structured_header,
316 &calleeTypeId, &multiBlocks, &postCallSB, &preCallSB, earlyReturn,
317 &singleTripLoopHeaderId, &singleTripLoopContinueId, &callee_result_ids,
318 this](const Instruction* cpi) {
319 switch (cpi->opcode()) {
320 case SpvOpFunction:
321 case SpvOpFunctionParameter:
322 // Already processed
323 break;
324 case SpvOpVariable:
325 if (cpi->NumInOperands() == 2) {
326 assert(callee2caller.count(cpi->result_id()) &&
327 "Expected the variable to have already been mapped.");
328 uint32_t new_var_id = callee2caller.at(cpi->result_id());
329
330 // The initializer must be a constant or global value. No mapped
331 // should be used.
332 uint32_t val_id = cpi->GetSingleWordInOperand(1);
333 AddStore(new_var_id, val_id, &new_blk_ptr);
334 }
335 break;
336 case SpvOpUnreachable:
337 case SpvOpKill: {
338 // Generate a return label so that we split the block with the
339 // function call. Copy the terminator into the new block.
340 if (returnLabelId == 0) {
341 returnLabelId = context()->TakeNextId();
342 if (returnLabelId == 0) {
343 return false;
344 }
345 }
346 std::unique_ptr<Instruction> terminator(
347 new Instruction(context(), cpi->opcode(), 0, 0, {}));
348 new_blk_ptr->AddInstruction(std::move(terminator));
349 break;
350 }
351 case SpvOpLabel: {
352 // If previous instruction was early return, insert branch
353 // instruction to return block.
354 if (prevInstWasReturn) {
355 if (returnLabelId == 0) {
356 returnLabelId = context()->TakeNextId();
357 if (returnLabelId == 0) {
358 return false;
359 }
360 }
361 AddBranch(returnLabelId, &new_blk_ptr);
362 prevInstWasReturn = false;
363 }
364 // Finish current block (if it exists) and get label for next block.
365 uint32_t labelId;
366 bool firstBlock = false;
367 if (new_blk_ptr != nullptr) {
368 new_blocks->push_back(std::move(new_blk_ptr));
369 // If result id is already mapped, use it, otherwise get a new
370 // one.
371 const uint32_t rid = cpi->result_id();
372 const auto mapItr = callee2caller.find(rid);
373 labelId = (mapItr != callee2caller.end())
374 ? mapItr->second
375 : context()->TakeNextId();
376 if (labelId == 0) {
377 return false;
378 }
379 } else {
380 // First block needs to use label of original block
381 // but map callee label in case of phi reference.
382 labelId = call_block_itr->id();
383 callee2caller[cpi->result_id()] = labelId;
384 firstBlock = true;
385 }
386 // Create first/next block.
387 new_blk_ptr = MakeUnique<BasicBlock>(NewLabel(labelId));
388 if (firstBlock) {
389 // Copy contents of original caller block up to call instruction.
390 for (auto cii = call_block_itr->begin(); cii != call_inst_itr;
391 cii = call_block_itr->begin()) {
392 Instruction* inst = &*cii;
393 inst->RemoveFromList();
394 std::unique_ptr<Instruction> cp_inst(inst);
395 // Remember same-block ops for possible regeneration.
396 if (IsSameBlockOp(&*cp_inst)) {
397 auto* sb_inst_ptr = cp_inst.get();
398 preCallSB[cp_inst->result_id()] = sb_inst_ptr;
399 }
400 new_blk_ptr->AddInstruction(std::move(cp_inst));
401 }
402 if (caller_is_loop_header &&
403 callee_begins_with_structured_header) {
404 // We can't place both the caller's merge instruction and
405 // another merge instruction in the same block. So split the
406 // calling block. Insert an unconditional branch to a new guard
407 // block. Later, once we know the ID of the last block, we
408 // will move the caller's OpLoopMerge from the last generated
409 // block into the first block. We also wait to avoid
410 // invalidating various iterators.
411 const auto guard_block_id = context()->TakeNextId();
412 if (guard_block_id == 0) {
413 return false;
414 }
415 AddBranch(guard_block_id, &new_blk_ptr);
416 new_blocks->push_back(std::move(new_blk_ptr));
417 // Start the next block.
418 new_blk_ptr = MakeUnique<BasicBlock>(NewLabel(guard_block_id));
419 // Reset the mapping of the callee's entry block to point to
420 // the guard block. Do this so we can fix up phis later on to
421 // satisfy dominance.
422 callee2caller[cpi->result_id()] = guard_block_id;
423 }
424 // If callee has early return, insert a header block for
425 // single-trip loop that will encompass callee code. Start
426 // postheader block.
427 //
428 // Note: Consider the following combination:
429 // - the caller is a single block loop
430 // - the callee does not begin with a structure header
431 // - the callee has multiple returns.
432 // We still need to split the caller block and insert a guard
433 // block. But we only need to do it once. We haven't done it yet,
434 // but the single-trip loop header will serve the same purpose.
435 if (earlyReturn) {
436 singleTripLoopHeaderId = context()->TakeNextId();
437 if (singleTripLoopHeaderId == 0) {
438 return false;
439 }
440 AddBranch(singleTripLoopHeaderId, &new_blk_ptr);
441 new_blocks->push_back(std::move(new_blk_ptr));
442 new_blk_ptr =
443 MakeUnique<BasicBlock>(NewLabel(singleTripLoopHeaderId));
444 returnLabelId = context()->TakeNextId();
445 singleTripLoopContinueId = context()->TakeNextId();
446 if (returnLabelId == 0 || singleTripLoopContinueId == 0) {
447 return false;
448 }
449 AddLoopMerge(returnLabelId, singleTripLoopContinueId,
450 &new_blk_ptr);
451 uint32_t postHeaderId = context()->TakeNextId();
452 if (postHeaderId == 0) {
453 return false;
454 }
455 AddBranch(postHeaderId, &new_blk_ptr);
456 new_blocks->push_back(std::move(new_blk_ptr));
457 new_blk_ptr = MakeUnique<BasicBlock>(NewLabel(postHeaderId));
458 multiBlocks = true;
459 // Reset the mapping of the callee's entry block to point to
460 // the post-header block. Do this so we can fix up phis later
461 // on to satisfy dominance.
462 callee2caller[cpi->result_id()] = postHeaderId;
463 }
464 } else {
465 multiBlocks = true;
466 }
467 } break;
468 case SpvOpReturnValue: {
469 // Store return value to return variable.
470 assert(returnVarId != 0);
471 uint32_t valId = cpi->GetInOperand(kSpvReturnValueId).words[0];
472 const auto mapItr = callee2caller.find(valId);
473 if (mapItr != callee2caller.end()) {
474 valId = mapItr->second;
475 }
476 AddStore(returnVarId, valId, &new_blk_ptr);
477
478 // Remember we saw a return; if followed by a label, will need to
479 // insert branch.
480 prevInstWasReturn = true;
481 } break;
482 case SpvOpReturn: {
483 // Remember we saw a return; if followed by a label, will need to
484 // insert branch.
485 prevInstWasReturn = true;
486 } break;
487 case SpvOpFunctionEnd: {
488 // If there was an early return, we generated a return label id
489 // for it. Now we have to generate the return block with that Id.
490 if (returnLabelId != 0) {
491 // If previous instruction was return, insert branch instruction
492 // to return block.
493 if (prevInstWasReturn) AddBranch(returnLabelId, &new_blk_ptr);
494 if (earlyReturn) {
495 // If we generated a loop header for the single-trip loop
496 // to accommodate early returns, insert the continue
497 // target block now, with a false branch back to the loop
498 // header.
499 new_blocks->push_back(std::move(new_blk_ptr));
500 new_blk_ptr =
501 MakeUnique<BasicBlock>(NewLabel(singleTripLoopContinueId));
502 uint32_t false_id = GetFalseId();
503 if (false_id == 0) {
504 return false;
505 }
506 AddBranchCond(false_id, singleTripLoopHeaderId, returnLabelId,
507 &new_blk_ptr);
508 }
509 // Generate the return block.
510 new_blocks->push_back(std::move(new_blk_ptr));
511 new_blk_ptr = MakeUnique<BasicBlock>(NewLabel(returnLabelId));
512 multiBlocks = true;
513 }
514 // Load return value into result id of call, if it exists.
515 if (returnVarId != 0) {
516 const uint32_t resId = call_inst_itr->result_id();
517 assert(resId != 0);
518 AddLoad(calleeTypeId, resId, returnVarId, &new_blk_ptr);
519 }
520 // Copy remaining instructions from caller block.
521 for (Instruction* inst = call_inst_itr->NextNode(); inst;
522 inst = call_inst_itr->NextNode()) {
523 inst->RemoveFromList();
524 std::unique_ptr<Instruction> cp_inst(inst);
525 // If multiple blocks generated, regenerate any same-block
526 // instruction that has not been seen in this last block.
527 if (multiBlocks) {
528 if (!CloneSameBlockOps(&cp_inst, &postCallSB, &preCallSB,
529 &new_blk_ptr)) {
530 return false;
531 }
532
533 // Remember same-block ops in this block.
534 if (IsSameBlockOp(&*cp_inst)) {
535 const uint32_t rid = cp_inst->result_id();
536 postCallSB[rid] = rid;
537 }
538 }
539 new_blk_ptr->AddInstruction(std::move(cp_inst));
540 }
541 // Finalize inline code.
542 new_blocks->push_back(std::move(new_blk_ptr));
543 } break;
544 default: {
545 // Copy callee instruction and remap all input Ids.
546 std::unique_ptr<Instruction> cp_inst(cpi->Clone(context()));
547 bool succeeded = cp_inst->WhileEachInId(
548 [&callee2caller, &callee_result_ids, this](uint32_t* iid) {
549 const auto mapItr = callee2caller.find(*iid);
550 if (mapItr != callee2caller.end()) {
551 *iid = mapItr->second;
552 } else if (callee_result_ids.find(*iid) !=
553 callee_result_ids.end()) {
554 // Forward reference. Allocate a new id, map it,
555 // use it and check for it when remapping result ids
556 const uint32_t nid = context()->TakeNextId();
557 if (nid == 0) {
558 return false;
559 }
560 callee2caller[*iid] = nid;
561 *iid = nid;
562 }
563 return true;
564 });
565 if (!succeeded) {
566 return false;
567 }
568 // If result id is non-zero, remap it. If already mapped, use mapped
569 // value, else use next id.
570 const uint32_t rid = cp_inst->result_id();
571 if (rid != 0) {
572 const auto mapItr = callee2caller.find(rid);
573 uint32_t nid;
574 if (mapItr != callee2caller.end()) {
575 nid = mapItr->second;
576 } else {
577 nid = context()->TakeNextId();
578 if (nid == 0) {
579 return false;
580 }
581 callee2caller[rid] = nid;
582 }
583 cp_inst->SetResultId(nid);
584 get_decoration_mgr()->CloneDecorations(rid, nid);
585 }
586 new_blk_ptr->AddInstruction(std::move(cp_inst));
587 } break;
588 }
589 return true;
590 });
591
592 if (!successful) {
Steven Perronacd27812018-12-18 19:34:03 +0000593 return false;
594 }
David Netoefff5fa2017-08-31 15:47:31 -0400595
Steven Perrond4fac342020-05-13 23:44:19 -0400596 if (caller_is_loop_header && (new_blocks->size() > 1)) {
597 // Move the OpLoopMerge from the last block back to the first, where
598 // it belongs.
599 auto& first = new_blocks->front();
600 auto& last = new_blocks->back();
601 assert(first != last);
David Netoefff5fa2017-08-31 15:47:31 -0400602
Steven Perrond4fac342020-05-13 23:44:19 -0400603 // Insert a modified copy of the loop merge into the first block.
604 auto loop_merge_itr = last->tail();
605 --loop_merge_itr;
606 assert(loop_merge_itr->opcode() == SpvOpLoopMerge);
607 std::unique_ptr<Instruction> cp_inst(loop_merge_itr->Clone(context()));
608 first->tail().InsertBefore(std::move(cp_inst));
David Netoefff5fa2017-08-31 15:47:31 -0400609
Steven Perrond4fac342020-05-13 23:44:19 -0400610 // Remove the loop merge from the last block.
611 loop_merge_itr->RemoveFromList();
612 delete &*loop_merge_itr;
David Netoefff5fa2017-08-31 15:47:31 -0400613 }
614
Greg Fischer04fcc662016-11-10 10:11:50 -0700615 // Update block map given replacement blocks.
616 for (auto& blk : *new_blocks) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600617 id2block_[blk->id()] = &*blk;
Greg Fischer04fcc662016-11-10 10:11:50 -0700618 }
Steven Perronacd27812018-12-18 19:34:03 +0000619 return true;
Greg Fischer04fcc662016-11-10 10:11:50 -0700620}
621
Steven Perronaa9e8f52019-07-17 14:59:05 -0400622bool InlinePass::IsInlinableFunctionCall(const Instruction* inst) {
David Netoceb1d4f2017-03-31 10:36:58 -0400623 if (inst->opcode() != SpvOp::SpvOpFunctionCall) return false;
GregFa107d342017-04-25 13:57:20 -0600624 const uint32_t calleeFnId =
625 inst->GetSingleWordOperand(kSpvFunctionCallFunctionId);
626 const auto ci = inlinable_.find(calleeFnId);
Steven Perrond4fac342020-05-13 23:44:19 -0400627 return ci != inlinable_.cend();
David Netoceb1d4f2017-03-31 10:36:58 -0400628}
629
GregFe28bd392017-08-01 17:20:13 -0600630void InlinePass::UpdateSucceedingPhis(
dan sinclairc7da51a2018-07-12 15:14:43 -0400631 std::vector<std::unique_ptr<BasicBlock>>& new_blocks) {
GregFe28bd392017-08-01 17:20:13 -0600632 const auto firstBlk = new_blocks.begin();
633 const auto lastBlk = new_blocks.end() - 1;
634 const uint32_t firstId = (*firstBlk)->id();
635 const uint32_t lastId = (*lastBlk)->id();
dan sinclairc7da51a2018-07-12 15:14:43 -0400636 const BasicBlock& const_last_block = *lastBlk->get();
David Neto87f9cfa2018-02-02 14:17:42 -0800637 const_last_block.ForEachSuccessorLabel(
638 [&firstId, &lastId, this](const uint32_t succ) {
dan sinclairc7da51a2018-07-12 15:14:43 -0400639 BasicBlock* sbp = this->id2block_[succ];
640 sbp->ForEachPhiInst([&firstId, &lastId](Instruction* phi) {
David Neto87f9cfa2018-02-02 14:17:42 -0800641 phi->ForEachInId([&firstId, &lastId](uint32_t* id) {
642 if (*id == firstId) *id = lastId;
643 });
644 });
GregFe28bd392017-08-01 17:20:13 -0600645 });
Greg Fischer04fcc662016-11-10 10:11:50 -0700646}
647
Steven Perrond4fac342020-05-13 23:44:19 -0400648bool InlinePass::HasNoReturnInStructuredConstruct(Function* func) {
649 // If control not structured, do not do loop/return analysis
650 // TODO: Analyze returns in non-structured control flow
651 if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader))
652 return false;
653 const auto structured_analysis = context()->GetStructuredCFGAnalysis();
654 // Search for returns in structured construct.
655 bool return_in_construct = false;
656 for (auto& blk : *func) {
657 auto terminal_ii = blk.cend();
658 --terminal_ii;
659 if (spvOpcodeIsReturn(terminal_ii->opcode()) &&
660 structured_analysis->ContainingConstruct(blk.id()) != 0) {
661 return_in_construct = true;
662 break;
663 }
664 }
665 return !return_in_construct;
666}
667
dan sinclairc7da51a2018-07-12 15:14:43 -0400668bool InlinePass::HasNoReturnInLoop(Function* func) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600669 // If control not structured, do not do loop/return analysis
670 // TODO: Analyze returns in non-structured control flow
Steven Perron756b2772017-12-19 14:18:13 -0500671 if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader))
672 return false;
greg-lunarg67214782018-11-08 07:11:20 -0700673 const auto structured_analysis = context()->GetStructuredCFGAnalysis();
674 // Search for returns in structured construct.
Greg Fischerbba812f2017-05-04 20:55:53 -0600675 bool return_in_loop = false;
greg-lunarg67214782018-11-08 07:11:20 -0700676 for (auto& blk : *func) {
677 auto terminal_ii = blk.cend();
Greg Fischerbba812f2017-05-04 20:55:53 -0600678 --terminal_ii;
greg-lunarg67214782018-11-08 07:11:20 -0700679 if (spvOpcodeIsReturn(terminal_ii->opcode()) &&
680 structured_analysis->ContainingLoop(blk.id()) != 0) {
681 return_in_loop = true;
682 break;
Greg Fischerbba812f2017-05-04 20:55:53 -0600683 }
684 }
685 return !return_in_loop;
686}
687
dan sinclairc7da51a2018-07-12 15:14:43 -0400688void InlinePass::AnalyzeReturns(Function* func) {
greg-lunarg67214782018-11-08 07:11:20 -0700689 if (HasNoReturnInLoop(func)) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600690 no_return_in_loop_.insert(func->result_id());
Steven Perrond4fac342020-05-13 23:44:19 -0400691 if (!HasNoReturnInStructuredConstruct(func))
greg-lunarg67214782018-11-08 07:11:20 -0700692 early_return_funcs_.insert(func->result_id());
Greg Fischerbba812f2017-05-04 20:55:53 -0600693 }
Greg Fischerbba812f2017-05-04 20:55:53 -0600694}
695
dan sinclairc7da51a2018-07-12 15:14:43 -0400696bool InlinePass::IsInlinableFunction(Function* func) {
GregFa107d342017-04-25 13:57:20 -0600697 // We can only inline a function if it has blocks.
Diego Novillod2938e42017-11-08 12:40:02 -0500698 if (func->cbegin() == func->cend()) return false;
Greg Fischerbba812f2017-05-04 20:55:53 -0600699 // Do not inline functions with returns in loops. Currently early return
700 // functions are inlined by wrapping them in a one trip loop and implementing
701 // the returns as a branch to the loop's merge block. However, this can only
702 // done validly if the return was not in a loop in the original function.
703 // Also remember functions with multiple (early) returns.
704 AnalyzeReturns(func);
Steven Perron2d2a5122018-11-29 14:24:58 -0500705 if (no_return_in_loop_.find(func->result_id()) == no_return_in_loop_.cend()) {
706 return false;
707 }
708
709 if (func->IsRecursive()) {
710 return false;
711 }
712
Steven Perronc18c9ff2019-10-04 13:05:32 -0400713 // Do not inline functions with an OpKill if they are called from a continue
714 // construct. If it is inlined into a continue construct it will generate
715 // invalid code.
716 bool func_is_called_from_continue =
717 funcs_called_from_continue_.count(func->result_id()) != 0;
Steven Perronc7a39bc2019-09-11 13:26:55 -0400718
Steven Perronc18c9ff2019-10-04 13:05:32 -0400719 if (func_is_called_from_continue && ContainsKill(func)) {
Steven Perronc7a39bc2019-09-11 13:26:55 -0400720 return false;
721 }
722
Steven Perron2d2a5122018-11-29 14:24:58 -0500723 return true;
GregFa107d342017-04-25 13:57:20 -0600724}
725
Steven Perronc18c9ff2019-10-04 13:05:32 -0400726bool InlinePass::ContainsKill(Function* func) const {
727 return !func->WhileEachInst(
728 [](Instruction* inst) { return inst->opcode() != SpvOpKill; });
729}
730
dan sinclairf96b7f12018-07-12 09:08:45 -0400731void InlinePass::InitializeInline() {
Greg Fischerbba812f2017-05-04 20:55:53 -0600732 false_id_ = 0;
733
GregFe28bd392017-08-01 17:20:13 -0600734 // clear collections
Greg Fischer04fcc662016-11-10 10:11:50 -0700735 id2function_.clear();
736 id2block_.clear();
GregFa107d342017-04-25 13:57:20 -0600737 inlinable_.clear();
GregFe28bd392017-08-01 17:20:13 -0600738 no_return_in_loop_.clear();
greg-lunarg67214782018-11-08 07:11:20 -0700739 early_return_funcs_.clear();
Steven Perronc18c9ff2019-10-04 13:05:32 -0400740 funcs_called_from_continue_ =
741 context()->GetStructuredCFGAnalysis()->FindFuncsCalledFromContinue();
GregFe28bd392017-08-01 17:20:13 -0600742
Diego Novillo1040a952017-10-25 13:26:25 -0400743 for (auto& fn : *get_module()) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600744 // Initialize function and block maps.
Greg Fischer04fcc662016-11-10 10:11:50 -0700745 id2function_[fn.result_id()] = &fn;
746 for (auto& blk : fn) {
Greg Fischerbba812f2017-05-04 20:55:53 -0600747 id2block_[blk.id()] = &blk;
Greg Fischer04fcc662016-11-10 10:11:50 -0700748 }
Greg Fischerbba812f2017-05-04 20:55:53 -0600749 // Compute inlinability
Diego Novillod2938e42017-11-08 12:40:02 -0500750 if (IsInlinableFunction(&fn)) inlinable_.insert(fn.result_id());
Greg Fischer04fcc662016-11-10 10:11:50 -0700751 }
Eleni Maria Stea045cc8f2018-03-21 11:15:56 +0200752}
Greg Fischer04fcc662016-11-10 10:11:50 -0700753
Diego Novillo1040a952017-10-25 13:26:25 -0400754InlinePass::InlinePass() {}
Greg Fischer04fcc662016-11-10 10:11:50 -0700755
Greg Fischer04fcc662016-11-10 10:11:50 -0700756} // namespace opt
757} // namespace spvtools