aboutsummaryrefslogtreecommitdiff
path: root/source/opt/wrap_opkill.cpp
blob: 51432a73b2dc5fa9ce687f19541c101fe84b0aba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "source/opt/wrap_opkill.h"

#include "ir_builder.h"

namespace spvtools {
namespace opt {

Pass::Status WrapOpKill::Process() {
  bool modified = false;

  auto func_to_process =
      context()->GetStructuredCFGAnalysis()->FindFuncsCalledFromContinue();
  for (uint32_t func_id : func_to_process) {
    Function* func = context()->GetFunction(func_id);
    bool successful = func->WhileEachInst([this, &modified](Instruction* inst) {
      const auto opcode = inst->opcode();
      if ((opcode == SpvOpKill) || (opcode == SpvOpTerminateInvocation)) {
        modified = true;
        if (!ReplaceWithFunctionCall(inst)) {
          return false;
        }
      }
      return true;
    });

    if (!successful) {
      return Status::Failure;
    }
  }

  if (opkill_function_ != nullptr) {
    assert(modified &&
           "The function should only be generated if something was modified.");
    context()->AddFunction(std::move(opkill_function_));
  }
  if (opterminateinvocation_function_ != nullptr) {
    assert(modified &&
           "The function should only be generated if something was modified.");
    context()->AddFunction(std::move(opterminateinvocation_function_));
  }
  return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
}

bool WrapOpKill::ReplaceWithFunctionCall(Instruction* inst) {
  assert((inst->opcode() == SpvOpKill ||
          inst->opcode() == SpvOpTerminateInvocation) &&
         "|inst| must be an OpKill or OpTerminateInvocation instruction.");
  InstructionBuilder ir_builder(
      context(), inst,
      IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  uint32_t func_id = GetKillingFuncId(inst->opcode());
  if (func_id == 0) {
    return false;
  }
  Instruction* call_inst =
      ir_builder.AddFunctionCall(GetVoidTypeId(), func_id, {});
  if (call_inst == nullptr) {
    return false;
  }
  call_inst->UpdateDebugInfoFrom(inst);

  Instruction* return_inst = nullptr;
  uint32_t return_type_id = GetOwningFunctionsReturnType(inst);
  if (return_type_id != GetVoidTypeId()) {
    Instruction* undef = ir_builder.AddNullaryOp(return_type_id, SpvOpUndef);
    if (undef == nullptr) {
      return false;
    }
    return_inst =
        ir_builder.AddUnaryOp(0, SpvOpReturnValue, undef->result_id());
  } else {
    return_inst = ir_builder.AddNullaryOp(0, SpvOpReturn);
  }

  if (return_inst == nullptr) {
    return false;
  }

  context()->KillInst(inst);
  return true;
}

uint32_t WrapOpKill::GetVoidTypeId() {
  if (void_type_id_ != 0) {
    return void_type_id_;
  }

  analysis::TypeManager* type_mgr = context()->get_type_mgr();
  analysis::Void void_type;
  void_type_id_ = type_mgr->GetTypeInstruction(&void_type);
  return void_type_id_;
}

uint32_t WrapOpKill::GetVoidFunctionTypeId() {
  analysis::TypeManager* type_mgr = context()->get_type_mgr();
  analysis::Void void_type;
  const analysis::Type* registered_void_type =
      type_mgr->GetRegisteredType(&void_type);

  analysis::Function func_type(registered_void_type, {});
  return type_mgr->GetTypeInstruction(&func_type);
}

uint32_t WrapOpKill::GetKillingFuncId(SpvOp opcode) {
  //  Parameterize by opcode
  assert(opcode == SpvOpKill || opcode == SpvOpTerminateInvocation);

  std::unique_ptr<Function>* const killing_func =
      (opcode == SpvOpKill) ? &opkill_function_
                            : &opterminateinvocation_function_;

  if (*killing_func != nullptr) {
    return (*killing_func)->result_id();
  }

  uint32_t killing_func_id = TakeNextId();
  if (killing_func_id == 0) {
    return 0;
  }

  uint32_t void_type_id = GetVoidTypeId();
  if (void_type_id == 0) {
    return 0;
  }

  // Generate the function start instruction
  std::unique_ptr<Instruction> func_start(new Instruction(
      context(), SpvOpFunction, void_type_id, killing_func_id, {}));
  func_start->AddOperand({SPV_OPERAND_TYPE_FUNCTION_CONTROL, {0}});
  func_start->AddOperand({SPV_OPERAND_TYPE_ID, {GetVoidFunctionTypeId()}});
  (*killing_func).reset(new Function(std::move(func_start)));

  // Generate the function end instruction
  std::unique_ptr<Instruction> func_end(
      new Instruction(context(), SpvOpFunctionEnd, 0, 0, {}));
  (*killing_func)->SetFunctionEnd(std::move(func_end));

  // Create the one basic block for the function.
  uint32_t lab_id = TakeNextId();
  if (lab_id == 0) {
    return 0;
  }
  std::unique_ptr<Instruction> label_inst(
      new Instruction(context(), SpvOpLabel, 0, lab_id, {}));
  std::unique_ptr<BasicBlock> bb(new BasicBlock(std::move(label_inst)));

  // Add the OpKill to the basic block
  std::unique_ptr<Instruction> kill_inst(
      new Instruction(context(), opcode, 0, 0, {}));
  bb->AddInstruction(std::move(kill_inst));

  // Add the bb to the function
  (*killing_func)->AddBasicBlock(std::move(bb));

  // Add the function to the module.
  if (context()->AreAnalysesValid(IRContext::kAnalysisDefUse)) {
    (*killing_func)->ForEachInst([this](Instruction* inst) {
      context()->AnalyzeDefUse(inst);
    });
  }

  if (context()->AreAnalysesValid(IRContext::kAnalysisInstrToBlockMapping)) {
    for (BasicBlock& basic_block : *(*killing_func)) {
      context()->set_instr_block(basic_block.GetLabelInst(), &basic_block);
      for (Instruction& inst : basic_block) {
        context()->set_instr_block(&inst, &basic_block);
      }
    }
  }

  return (*killing_func)->result_id();
}

uint32_t WrapOpKill::GetOwningFunctionsReturnType(Instruction* inst) {
  BasicBlock* bb = context()->get_instr_block(inst);
  if (bb == nullptr) {
    return 0;
  }

  Function* func = bb->GetParent();
  return func->type_id();
}

}  // namespace opt
}  // namespace spvtools