David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1 | // Copyright 2017 The Clspv Authors. All rights reserved. |
| 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 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 15 | #include <list> |
| 16 | |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 17 | #include "llvm/IR/IRBuilder.h" |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 18 | #include "llvm/IR/Instructions.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
| 20 | #include "llvm/Pass.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 22 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 23 | #include "Builtins.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 24 | #include "Passes.h" |
| 25 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 26 | using namespace clspv; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | #define DEBUG_TYPE "splatarg" |
| 30 | |
| 31 | namespace { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 32 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 33 | struct SplatArgPass : public ModulePass { |
| 34 | static char ID; |
| 35 | SplatArgPass() : ModulePass(ID) {} |
| 36 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 37 | std::string getSplatName(const Builtins::FunctionInfo &func_info, |
| 38 | const Builtins::ParamTypeInfo ¶m_info, |
| 39 | bool three_params); |
| 40 | Function *getReplacementFunction(Function &F, const std::string &NewCallName); |
| 41 | void replaceCall(Function *NewCallee, CallInst *Call); |
| 42 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 43 | bool runOnModule(Module &M) override; |
| 44 | }; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 45 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 46 | } // namespace |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 47 | |
| 48 | char SplatArgPass::ID = 0; |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 49 | INITIALIZE_PASS(SplatArgPass, "SplatArg", "Splat Argument Pass", false, false) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 50 | |
| 51 | namespace clspv { |
| 52 | llvm::ModulePass *createSplatArgPass() { return new SplatArgPass(); } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 53 | } // namespace clspv |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 54 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 55 | // Programmatically convert mangled_name to vectorized version |
| 56 | std::string |
| 57 | SplatArgPass::getSplatName(const Builtins::FunctionInfo &func_info, |
| 58 | const Builtins::ParamTypeInfo ¶m_info, |
| 59 | bool three_params) { |
| 60 | const char *type_code = "f"; |
| 61 | uint32_t index = Log2_32(param_info.byte_len); |
| 62 | assert(index <= 3); |
| 63 | const char *signed_int_type_tbl[] = {"c", "s", "i", "l"}; |
| 64 | const char *unsigned_int_type_tbl[] = {"h", "t", "j", "m"}; |
| 65 | const char *float_type_tbl[] = {"", "Dh", "f", "d"}; |
| 66 | switch (param_info.type_id) { |
| 67 | case Type::IntegerTyID: |
| 68 | type_code = param_info.is_signed ? signed_int_type_tbl[index] |
| 69 | : unsigned_int_type_tbl[index]; |
| 70 | break; |
| 71 | case Type::FloatTyID: |
| 72 | if (index == 0) |
| 73 | llvm_unreachable("Unsupported float type"); |
| 74 | type_code = float_type_tbl[index]; |
| 75 | break; |
| 76 | default: |
| 77 | llvm_unreachable("Unsupported type"); |
| 78 | } |
| 79 | const auto &func_name = func_info.getName(); |
| 80 | return "_Z" + std::to_string(func_name.size()) + func_name + "Dv" + |
| 81 | std::to_string(param_info.vector_size) + "_" + type_code + |
| 82 | (three_params ? "S_S_" : "S_"); |
| 83 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 84 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 85 | // Create replacement function once |
| 86 | Function *SplatArgPass::getReplacementFunction(Function &F, |
| 87 | const std::string &NewCallName) { |
| 88 | Module *M = F.getParent(); |
| 89 | FunctionType *CalleeTy = F.getFunctionType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 90 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 91 | // Create new callee function type with vector type. |
| 92 | Type *VectorType = F.getArg(0)->getType(); |
| 93 | SmallVector<Type *, 4> NewCalleeParamTys; |
| 94 | for (auto ai = F.arg_begin(); ai != F.arg_end(); ++ai) { |
| 95 | auto &Arg = *ai; |
| 96 | if (Arg.getType()->isVectorTy()) { |
| 97 | NewCalleeParamTys.push_back(Arg.getType()); |
| 98 | } else { |
| 99 | NewCalleeParamTys.push_back(VectorType); |
| 100 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 101 | } |
| 102 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 103 | FunctionType *NewCalleeTy = |
| 104 | FunctionType::get(F.getReturnType(), NewCalleeParamTys, false); |
| 105 | |
| 106 | // Create new callee function declaration with new function type. |
| 107 | Function *NewCallee = cast<Function>( |
| 108 | M->getOrInsertFunction(NewCallName, NewCalleeTy).getCallee()); |
| 109 | NewCallee->setCallingConv(CallingConv::SPIR_FUNC); |
| 110 | |
| 111 | return NewCallee; |
| 112 | } |
| 113 | |
| 114 | // Replace each callee to vectorized version |
| 115 | // - also vectorize parameters |
| 116 | void SplatArgPass::replaceCall(Function *NewCallee, CallInst *Call) { |
| 117 | Function *Callee = Call->getCalledFunction(); |
| 118 | FunctionType *CalleeTy = Callee->getFunctionType(); |
| 119 | VectorType *VTy = cast<VectorType>(Call->getType()); |
| 120 | |
| 121 | // Change target of call instruction. |
| 122 | Call->setCalledFunction(NewCallee); |
| 123 | |
| 124 | // Change operands of call instruction. |
| 125 | IRBuilder<> Builder(Call); |
| 126 | for (unsigned i = 0; i < CalleeTy->getNumParams(); i++) { |
| 127 | if (!CalleeTy->getParamType(i)->isVectorTy()) { |
| 128 | Value *NewArg = Builder.CreateVectorSplat( |
| 129 | VTy->getNumElements(), Call->getArgOperand(i), "arg_splat"); |
| 130 | Call->setArgOperand(i, NewArg); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | Call->setCallingConv(CallingConv::SPIR_FUNC); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool SplatArgPass::runOnModule(Module &M) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 138 | std::list<Function *> func_list; |
| 139 | for (auto &F : M) { |
| 140 | // process only function declarations |
| 141 | if (F.empty() && !F.use_empty()) { |
| 142 | auto &func_info = Builtins::Lookup(&F); |
| 143 | auto func_type = func_info.getType(); |
| 144 | switch (func_type) { |
| 145 | case Builtins::kClamp: |
| 146 | case Builtins::kMix: |
| 147 | case Builtins::kMax: |
| 148 | case Builtins::kFmax: |
| 149 | case Builtins::kMin: |
| 150 | case Builtins::kFmin: { |
| 151 | auto ¶m_info = func_info.getParameter(0); |
| 152 | uint32_t vec_size = param_info.vector_size; |
| 153 | bool last_is_scalar = func_info.getLastParameter().vector_size == 0; |
| 154 | if (vec_size != 0 && last_is_scalar) { |
| 155 | bool has_3_params = |
| 156 | func_type == Builtins::kClamp || func_type == Builtins::kMix; |
| 157 | std::string NewFName = |
| 158 | getSplatName(func_info, param_info, has_3_params); |
| 159 | Function *NewCallee = getReplacementFunction(F, NewFName); |
| 160 | // Replace the users of the function. |
| 161 | for (User *U : F.users()) { |
| 162 | replaceCall(NewCallee, dyn_cast<CallInst>(U)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 163 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 164 | func_list.push_front(&F); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 165 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | default: |
| 169 | break; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 173 | if (func_list.size() != 0) { |
| 174 | // remove dead |
| 175 | for (auto *F : func_list) { |
| 176 | if (F->use_empty()) { |
| 177 | F->eraseFromParent(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 180 | return true; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 181 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 182 | return false; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 183 | } |