blob: 620945341025ca4e70369cc5fbe85e2e941b9b27 [file] [log] [blame]
David Neto22f144c2017-06-12 14:26:21 -04001// 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
SJW2c317da2020-03-23 07:39:13 -050015#include <list>
16
David Neto118188e2018-08-24 11:27:54 -040017#include "llvm/IR/IRBuilder.h"
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040018#include "llvm/IR/Instructions.h"
David Neto118188e2018-08-24 11:27:54 -040019#include "llvm/IR/Module.h"
20#include "llvm/Pass.h"
21#include "llvm/Support/raw_ostream.h"
David Neto22f144c2017-06-12 14:26:21 -040022
SJW2c317da2020-03-23 07:39:13 -050023#include "Builtins.h"
Diego Novilloa4c44fa2019-04-11 10:56:15 -040024#include "Passes.h"
25
SJW2c317da2020-03-23 07:39:13 -050026using namespace clspv;
David Neto22f144c2017-06-12 14:26:21 -040027using namespace llvm;
28
29#define DEBUG_TYPE "splatarg"
30
31namespace {
SJW2c317da2020-03-23 07:39:13 -050032
David Neto22f144c2017-06-12 14:26:21 -040033struct SplatArgPass : public ModulePass {
34 static char ID;
35 SplatArgPass() : ModulePass(ID) {}
36
SJW2c317da2020-03-23 07:39:13 -050037 std::string getSplatName(const Builtins::FunctionInfo &func_info,
38 const Builtins::ParamTypeInfo &param_info,
39 bool three_params);
40 Function *getReplacementFunction(Function &F, const std::string &NewCallName);
41 void replaceCall(Function *NewCallee, CallInst *Call);
42
David Neto22f144c2017-06-12 14:26:21 -040043 bool runOnModule(Module &M) override;
44};
SJW2c317da2020-03-23 07:39:13 -050045
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040046} // namespace
David Neto22f144c2017-06-12 14:26:21 -040047
48char SplatArgPass::ID = 0;
Diego Novilloa4c44fa2019-04-11 10:56:15 -040049INITIALIZE_PASS(SplatArgPass, "SplatArg", "Splat Argument Pass", false, false)
David Neto22f144c2017-06-12 14:26:21 -040050
51namespace clspv {
52llvm::ModulePass *createSplatArgPass() { return new SplatArgPass(); }
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040053} // namespace clspv
David Neto22f144c2017-06-12 14:26:21 -040054
SJW2c317da2020-03-23 07:39:13 -050055// Programmatically convert mangled_name to vectorized version
56std::string
57SplatArgPass::getSplatName(const Builtins::FunctionInfo &func_info,
58 const Builtins::ParamTypeInfo &param_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 Neto22f144c2017-06-12 14:26:21 -040084
SJW2c317da2020-03-23 07:39:13 -050085// Create replacement function once
86Function *SplatArgPass::getReplacementFunction(Function &F,
87 const std::string &NewCallName) {
88 Module *M = F.getParent();
David Neto22f144c2017-06-12 14:26:21 -040089
SJW2c317da2020-03-23 07:39:13 -050090 // Create new callee function type with vector type.
91 Type *VectorType = F.getArg(0)->getType();
92 SmallVector<Type *, 4> NewCalleeParamTys;
93 for (auto ai = F.arg_begin(); ai != F.arg_end(); ++ai) {
94 auto &Arg = *ai;
95 if (Arg.getType()->isVectorTy()) {
96 NewCalleeParamTys.push_back(Arg.getType());
97 } else {
98 NewCalleeParamTys.push_back(VectorType);
99 }
David Neto22f144c2017-06-12 14:26:21 -0400100 }
101
SJW2c317da2020-03-23 07:39:13 -0500102 FunctionType *NewCalleeTy =
103 FunctionType::get(F.getReturnType(), NewCalleeParamTys, false);
104
105 // Create new callee function declaration with new function type.
106 Function *NewCallee = cast<Function>(
107 M->getOrInsertFunction(NewCallName, NewCalleeTy).getCallee());
108 NewCallee->setCallingConv(CallingConv::SPIR_FUNC);
109
110 return NewCallee;
111}
112
113// Replace each callee to vectorized version
114// - also vectorize parameters
115void SplatArgPass::replaceCall(Function *NewCallee, CallInst *Call) {
116 Function *Callee = Call->getCalledFunction();
117 FunctionType *CalleeTy = Callee->getFunctionType();
118 VectorType *VTy = cast<VectorType>(Call->getType());
119
120 // Change target of call instruction.
121 Call->setCalledFunction(NewCallee);
122
123 // Change operands of call instruction.
124 IRBuilder<> Builder(Call);
125 for (unsigned i = 0; i < CalleeTy->getNumParams(); i++) {
126 if (!CalleeTy->getParamType(i)->isVectorTy()) {
alan-baker5a8c3be2020-09-09 13:44:26 -0400127 Value *NewArg =
128 Builder.CreateVectorSplat(VTy->getElementCount().getKnownMinValue(),
129 Call->getArgOperand(i), "arg_splat");
SJW2c317da2020-03-23 07:39:13 -0500130 Call->setArgOperand(i, NewArg);
131 }
132 }
133
134 Call->setCallingConv(CallingConv::SPIR_FUNC);
David Neto22f144c2017-06-12 14:26:21 -0400135}
136
137bool SplatArgPass::runOnModule(Module &M) {
SJWf4b01582020-03-31 19:19:17 -0500138 std::list<std::pair<Function *, const Builtins::FunctionInfo &>> func_list;
139 // Collect candidates
140 for (auto &F : M.getFunctionList()) {
SJW2c317da2020-03-23 07:39:13 -0500141 // process only function declarations
SJWf4b01582020-03-31 19:19:17 -0500142 if (F.isDeclaration() && !F.use_empty()) {
SJW2c317da2020-03-23 07:39:13 -0500143 auto &func_info = Builtins::Lookup(&F);
144 auto func_type = func_info.getType();
145 switch (func_type) {
146 case Builtins::kClamp:
147 case Builtins::kMix:
148 case Builtins::kMax:
149 case Builtins::kFmax:
150 case Builtins::kMin:
151 case Builtins::kFmin: {
152 auto &param_info = func_info.getParameter(0);
153 uint32_t vec_size = param_info.vector_size;
154 bool last_is_scalar = func_info.getLastParameter().vector_size == 0;
155 if (vec_size != 0 && last_is_scalar) {
SJWf4b01582020-03-31 19:19:17 -0500156 func_list.push_back({&F, func_info});
David Neto22f144c2017-06-12 14:26:21 -0400157 }
SJW2c317da2020-03-23 07:39:13 -0500158 break;
159 }
160 default:
161 break;
David Neto22f144c2017-06-12 14:26:21 -0400162 }
163 }
164 }
SJWf4b01582020-03-31 19:19:17 -0500165 // Replace with vectorized version
166 for (auto FI : func_list) {
167 auto *F = FI.first;
168 auto &func_info = FI.second;
169 auto func_type = func_info.getType();
170 auto &param_info = func_info.getParameter(0);
171 bool has_3_params =
172 func_type == Builtins::kClamp || func_type == Builtins::kMix;
173 std::string NewFName = getSplatName(func_info, param_info, has_3_params);
174 Function *NewCallee = getReplacementFunction(*F, NewFName);
175 // Replace the users of the function.
176 while (!F->use_empty()) {
177 User *U = F->user_back();
178 replaceCall(NewCallee, dyn_cast<CallInst>(U));
David Neto22f144c2017-06-12 14:26:21 -0400179 }
SJWf4b01582020-03-31 19:19:17 -0500180 // Remove if dead
181 if (F->use_empty()) {
182 F->eraseFromParent();
183 }
David Neto22f144c2017-06-12 14:26:21 -0400184 }
SJWf4b01582020-03-31 19:19:17 -0500185 return func_list.size() != 0;
David Neto22f144c2017-06-12 14:26:21 -0400186}