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 "source/opt/inline_opaque_pass.h"
18
19 #include <utility>
20
21 namespace spvtools {
22 namespace opt {
23 namespace {
24 constexpr uint32_t kTypePointerTypeIdInIdx = 1;
25 } // namespace
26
IsOpaqueType(uint32_t typeId)27 bool InlineOpaquePass::IsOpaqueType(uint32_t typeId) {
28 const Instruction* typeInst = get_def_use_mgr()->GetDef(typeId);
29 switch (typeInst->opcode()) {
30 case spv::Op::OpTypeSampler:
31 case spv::Op::OpTypeImage:
32 case spv::Op::OpTypeSampledImage:
33 return true;
34 case spv::Op::OpTypePointer:
35 return IsOpaqueType(
36 typeInst->GetSingleWordInOperand(kTypePointerTypeIdInIdx));
37 default:
38 break;
39 }
40 // TODO(greg-lunarg): Handle arrays containing opaque type
41 if (typeInst->opcode() != spv::Op::OpTypeStruct) return false;
42 // Return true if any member is opaque
43 return !typeInst->WhileEachInId([this](const uint32_t* tid) {
44 if (IsOpaqueType(*tid)) return false;
45 return true;
46 });
47 }
48
HasOpaqueArgsOrReturn(const Instruction * callInst)49 bool InlineOpaquePass::HasOpaqueArgsOrReturn(const Instruction* callInst) {
50 // Check return type
51 if (IsOpaqueType(callInst->type_id())) return true;
52 // Check args
53 int icnt = 0;
54 return !callInst->WhileEachInId([&icnt, this](const uint32_t* iid) {
55 if (icnt > 0) {
56 const Instruction* argInst = get_def_use_mgr()->GetDef(*iid);
57 if (IsOpaqueType(argInst->type_id())) return false;
58 }
59 ++icnt;
60 return true;
61 });
62 }
63
InlineOpaque(Function * func)64 Pass::Status InlineOpaquePass::InlineOpaque(Function* func) {
65 bool modified = false;
66 // Using block iterators here because of block erasures and insertions.
67 for (auto bi = func->begin(); bi != func->end(); ++bi) {
68 for (auto ii = bi->begin(); ii != bi->end();) {
69 if (IsInlinableFunctionCall(&*ii) && HasOpaqueArgsOrReturn(&*ii)) {
70 // Inline call.
71 std::vector<std::unique_ptr<BasicBlock>> newBlocks;
72 std::vector<std::unique_ptr<Instruction>> newVars;
73 if (!GenInlineCode(&newBlocks, &newVars, ii, bi)) {
74 return Status::Failure;
75 }
76
77 // If call block is replaced with more than one block, point
78 // succeeding phis at new last block.
79 if (newBlocks.size() > 1) UpdateSucceedingPhis(newBlocks);
80 // Replace old calling block with new block(s).
81 bi = bi.Erase();
82 bi = bi.InsertBefore(&newBlocks);
83 // Insert new function variables.
84 if (newVars.size() > 0)
85 func->begin()->begin().InsertBefore(std::move(newVars));
86 // Restart inlining at beginning of calling block.
87 ii = bi->begin();
88 modified = true;
89 } else {
90 ++ii;
91 }
92 }
93 }
94 return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
95 }
96
Initialize()97 void InlineOpaquePass::Initialize() { InitializeInline(); }
98
ProcessImpl()99 Pass::Status InlineOpaquePass::ProcessImpl() {
100 Status status = Status::SuccessWithoutChange;
101 // Do opaque inlining on each function in entry point call tree
102 ProcessFunction pfn = [&status, this](Function* fp) {
103 status = CombineStatus(status, InlineOpaque(fp));
104 return false;
105 };
106 context()->ProcessReachableCallTree(pfn);
107 return status;
108 }
109
110 InlineOpaquePass::InlineOpaquePass() = default;
111
Process()112 Pass::Status InlineOpaquePass::Process() {
113 Initialize();
114 return ProcessImpl();
115 }
116
117 } // namespace opt
118 } // namespace spvtools
119