1 // Copyright (c) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/opt/wrap_opkill.h"
16
17 #include "ir_builder.h"
18
19 namespace spvtools {
20 namespace opt {
21
Process()22 Pass::Status WrapOpKill::Process() {
23 bool modified = false;
24
25 auto func_to_process =
26 context()->GetStructuredCFGAnalysis()->FindFuncsCalledFromContinue();
27 for (uint32_t func_id : func_to_process) {
28 Function* func = context()->GetFunction(func_id);
29 bool successful = func->WhileEachInst([this, &modified](Instruction* inst) {
30 const auto opcode = inst->opcode();
31 if ((opcode == SpvOpKill) || (opcode == SpvOpTerminateInvocation)) {
32 modified = true;
33 if (!ReplaceWithFunctionCall(inst)) {
34 return false;
35 }
36 }
37 return true;
38 });
39
40 if (!successful) {
41 return Status::Failure;
42 }
43 }
44
45 if (opkill_function_ != nullptr) {
46 assert(modified &&
47 "The function should only be generated if something was modified.");
48 context()->AddFunction(std::move(opkill_function_));
49 }
50 if (opterminateinvocation_function_ != nullptr) {
51 assert(modified &&
52 "The function should only be generated if something was modified.");
53 context()->AddFunction(std::move(opterminateinvocation_function_));
54 }
55 return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
56 }
57
ReplaceWithFunctionCall(Instruction * inst)58 bool WrapOpKill::ReplaceWithFunctionCall(Instruction* inst) {
59 assert((inst->opcode() == SpvOpKill ||
60 inst->opcode() == SpvOpTerminateInvocation) &&
61 "|inst| must be an OpKill or OpTerminateInvocation instruction.");
62 InstructionBuilder ir_builder(
63 context(), inst,
64 IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
65 uint32_t func_id = GetKillingFuncId(inst->opcode());
66 if (func_id == 0) {
67 return false;
68 }
69 Instruction* call_inst =
70 ir_builder.AddFunctionCall(GetVoidTypeId(), func_id, {});
71 if (call_inst == nullptr) {
72 return false;
73 }
74 call_inst->UpdateDebugInfoFrom(inst);
75
76 Instruction* return_inst = nullptr;
77 uint32_t return_type_id = GetOwningFunctionsReturnType(inst);
78 if (return_type_id != GetVoidTypeId()) {
79 Instruction* undef = ir_builder.AddNullaryOp(return_type_id, SpvOpUndef);
80 if (undef == nullptr) {
81 return false;
82 }
83 return_inst =
84 ir_builder.AddUnaryOp(0, SpvOpReturnValue, undef->result_id());
85 } else {
86 return_inst = ir_builder.AddNullaryOp(0, SpvOpReturn);
87 }
88
89 if (return_inst == nullptr) {
90 return false;
91 }
92
93 context()->KillInst(inst);
94 return true;
95 }
96
GetVoidTypeId()97 uint32_t WrapOpKill::GetVoidTypeId() {
98 if (void_type_id_ != 0) {
99 return void_type_id_;
100 }
101
102 analysis::TypeManager* type_mgr = context()->get_type_mgr();
103 analysis::Void void_type;
104 void_type_id_ = type_mgr->GetTypeInstruction(&void_type);
105 return void_type_id_;
106 }
107
GetVoidFunctionTypeId()108 uint32_t WrapOpKill::GetVoidFunctionTypeId() {
109 analysis::TypeManager* type_mgr = context()->get_type_mgr();
110 analysis::Void void_type;
111 const analysis::Type* registered_void_type =
112 type_mgr->GetRegisteredType(&void_type);
113
114 analysis::Function func_type(registered_void_type, {});
115 return type_mgr->GetTypeInstruction(&func_type);
116 }
117
GetKillingFuncId(SpvOp opcode)118 uint32_t WrapOpKill::GetKillingFuncId(SpvOp opcode) {
119 // Parameterize by opcode
120 assert(opcode == SpvOpKill || opcode == SpvOpTerminateInvocation);
121
122 std::unique_ptr<Function>* const killing_func =
123 (opcode == SpvOpKill) ? &opkill_function_
124 : &opterminateinvocation_function_;
125
126 if (*killing_func != nullptr) {
127 return (*killing_func)->result_id();
128 }
129
130 uint32_t killing_func_id = TakeNextId();
131 if (killing_func_id == 0) {
132 return 0;
133 }
134
135 uint32_t void_type_id = GetVoidTypeId();
136 if (void_type_id == 0) {
137 return 0;
138 }
139
140 // Generate the function start instruction
141 std::unique_ptr<Instruction> func_start(new Instruction(
142 context(), SpvOpFunction, void_type_id, killing_func_id, {}));
143 func_start->AddOperand({SPV_OPERAND_TYPE_FUNCTION_CONTROL, {0}});
144 func_start->AddOperand({SPV_OPERAND_TYPE_ID, {GetVoidFunctionTypeId()}});
145 (*killing_func).reset(new Function(std::move(func_start)));
146
147 // Generate the function end instruction
148 std::unique_ptr<Instruction> func_end(
149 new Instruction(context(), SpvOpFunctionEnd, 0, 0, {}));
150 (*killing_func)->SetFunctionEnd(std::move(func_end));
151
152 // Create the one basic block for the function.
153 uint32_t lab_id = TakeNextId();
154 if (lab_id == 0) {
155 return 0;
156 }
157 std::unique_ptr<Instruction> label_inst(
158 new Instruction(context(), SpvOpLabel, 0, lab_id, {}));
159 std::unique_ptr<BasicBlock> bb(new BasicBlock(std::move(label_inst)));
160
161 // Add the OpKill to the basic block
162 std::unique_ptr<Instruction> kill_inst(
163 new Instruction(context(), opcode, 0, 0, {}));
164 bb->AddInstruction(std::move(kill_inst));
165
166 // Add the bb to the function
167 (*killing_func)->AddBasicBlock(std::move(bb));
168
169 // Add the function to the module.
170 if (context()->AreAnalysesValid(IRContext::kAnalysisDefUse)) {
171 (*killing_func)->ForEachInst([this](Instruction* inst) {
172 context()->AnalyzeDefUse(inst);
173 });
174 }
175
176 if (context()->AreAnalysesValid(IRContext::kAnalysisInstrToBlockMapping)) {
177 for (BasicBlock& basic_block : *(*killing_func)) {
178 context()->set_instr_block(basic_block.GetLabelInst(), &basic_block);
179 for (Instruction& inst : basic_block) {
180 context()->set_instr_block(&inst, &basic_block);
181 }
182 }
183 }
184
185 return (*killing_func)->result_id();
186 }
187
GetOwningFunctionsReturnType(Instruction * inst)188 uint32_t WrapOpKill::GetOwningFunctionsReturnType(Instruction* inst) {
189 BasicBlock* bb = context()->get_instr_block(inst);
190 if (bb == nullptr) {
191 return 0;
192 }
193
194 Function* func = bb->GetParent();
195 return func->type_id();
196 }
197
198 } // namespace opt
199 } // namespace spvtools
200