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 == spv::Op::OpKill) ||
32 (opcode == spv::Op::OpTerminateInvocation)) {
33 modified = true;
34 if (!ReplaceWithFunctionCall(inst)) {
35 return false;
36 }
37 }
38 return true;
39 });
40
41 if (!successful) {
42 return Status::Failure;
43 }
44 }
45
46 if (opkill_function_ != nullptr) {
47 assert(modified &&
48 "The function should only be generated if something was modified.");
49 context()->AddFunction(std::move(opkill_function_));
50 }
51 if (opterminateinvocation_function_ != nullptr) {
52 assert(modified &&
53 "The function should only be generated if something was modified.");
54 context()->AddFunction(std::move(opterminateinvocation_function_));
55 }
56 return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
57 }
58
ReplaceWithFunctionCall(Instruction * inst)59 bool WrapOpKill::ReplaceWithFunctionCall(Instruction* inst) {
60 assert((inst->opcode() == spv::Op::OpKill ||
61 inst->opcode() == spv::Op::OpTerminateInvocation) &&
62 "|inst| must be an OpKill or OpTerminateInvocation instruction.");
63 InstructionBuilder ir_builder(
64 context(), inst,
65 IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
66 uint32_t func_id = GetKillingFuncId(inst->opcode());
67 if (func_id == 0) {
68 return false;
69 }
70 Instruction* call_inst =
71 ir_builder.AddFunctionCall(GetVoidTypeId(), func_id, {});
72 if (call_inst == nullptr) {
73 return false;
74 }
75 call_inst->UpdateDebugInfoFrom(inst);
76
77 Instruction* return_inst = nullptr;
78 uint32_t return_type_id = GetOwningFunctionsReturnType(inst);
79 if (return_type_id != GetVoidTypeId()) {
80 Instruction* undef =
81 ir_builder.AddNullaryOp(return_type_id, spv::Op::OpUndef);
82 if (undef == nullptr) {
83 return false;
84 }
85 return_inst =
86 ir_builder.AddUnaryOp(0, spv::Op::OpReturnValue, undef->result_id());
87 } else {
88 return_inst = ir_builder.AddNullaryOp(0, spv::Op::OpReturn);
89 }
90
91 if (return_inst == nullptr) {
92 return false;
93 }
94
95 context()->KillInst(inst);
96 return true;
97 }
98
GetVoidTypeId()99 uint32_t WrapOpKill::GetVoidTypeId() {
100 if (void_type_id_ != 0) {
101 return void_type_id_;
102 }
103
104 analysis::TypeManager* type_mgr = context()->get_type_mgr();
105 analysis::Void void_type;
106 void_type_id_ = type_mgr->GetTypeInstruction(&void_type);
107 return void_type_id_;
108 }
109
GetVoidFunctionTypeId()110 uint32_t WrapOpKill::GetVoidFunctionTypeId() {
111 analysis::TypeManager* type_mgr = context()->get_type_mgr();
112 analysis::Void void_type;
113 const analysis::Type* registered_void_type =
114 type_mgr->GetRegisteredType(&void_type);
115
116 analysis::Function func_type(registered_void_type, {});
117 return type_mgr->GetTypeInstruction(&func_type);
118 }
119
GetKillingFuncId(spv::Op opcode)120 uint32_t WrapOpKill::GetKillingFuncId(spv::Op opcode) {
121 // Parameterize by opcode
122 assert(opcode == spv::Op::OpKill || opcode == spv::Op::OpTerminateInvocation);
123
124 std::unique_ptr<Function>* const killing_func =
125 (opcode == spv::Op::OpKill) ? &opkill_function_
126 : &opterminateinvocation_function_;
127
128 if (*killing_func != nullptr) {
129 return (*killing_func)->result_id();
130 }
131
132 uint32_t killing_func_id = TakeNextId();
133 if (killing_func_id == 0) {
134 return 0;
135 }
136
137 uint32_t void_type_id = GetVoidTypeId();
138 if (void_type_id == 0) {
139 return 0;
140 }
141
142 // Generate the function start instruction
143 std::unique_ptr<Instruction> func_start(new Instruction(
144 context(), spv::Op::OpFunction, void_type_id, killing_func_id, {}));
145 func_start->AddOperand({SPV_OPERAND_TYPE_FUNCTION_CONTROL, {0}});
146 func_start->AddOperand({SPV_OPERAND_TYPE_ID, {GetVoidFunctionTypeId()}});
147 (*killing_func).reset(new Function(std::move(func_start)));
148
149 // Generate the function end instruction
150 std::unique_ptr<Instruction> func_end(
151 new Instruction(context(), spv::Op::OpFunctionEnd, 0, 0, {}));
152 (*killing_func)->SetFunctionEnd(std::move(func_end));
153
154 // Create the one basic block for the function.
155 uint32_t lab_id = TakeNextId();
156 if (lab_id == 0) {
157 return 0;
158 }
159 std::unique_ptr<Instruction> label_inst(
160 new Instruction(context(), spv::Op::OpLabel, 0, lab_id, {}));
161 std::unique_ptr<BasicBlock> bb(new BasicBlock(std::move(label_inst)));
162
163 // Add the OpKill to the basic block
164 std::unique_ptr<Instruction> kill_inst(
165 new Instruction(context(), opcode, 0, 0, {}));
166 bb->AddInstruction(std::move(kill_inst));
167
168 // Add the bb to the function
169 (*killing_func)->AddBasicBlock(std::move(bb));
170
171 // Add the function to the module.
172 if (context()->AreAnalysesValid(IRContext::kAnalysisDefUse)) {
173 (*killing_func)->ForEachInst([this](Instruction* inst) {
174 context()->AnalyzeDefUse(inst);
175 });
176 }
177
178 if (context()->AreAnalysesValid(IRContext::kAnalysisInstrToBlockMapping)) {
179 for (BasicBlock& basic_block : *(*killing_func)) {
180 context()->set_instr_block(basic_block.GetLabelInst(), &basic_block);
181 for (Instruction& inst : basic_block) {
182 context()->set_instr_block(&inst, &basic_block);
183 }
184 }
185 }
186
187 return (*killing_func)->result_id();
188 }
189
GetOwningFunctionsReturnType(Instruction * inst)190 uint32_t WrapOpKill::GetOwningFunctionsReturnType(Instruction* inst) {
191 BasicBlock* bb = context()->get_instr_block(inst);
192 if (bb == nullptr) {
193 return 0;
194 }
195
196 Function* func = bb->GetParent();
197 return func->type_id();
198 }
199
200 } // namespace opt
201 } // namespace spvtools
202