blob: 17c19db6f41c62ab7325306cc58b8d5528e49eef [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();
89 FunctionType *CalleeTy = F.getFunctionType();
David Neto22f144c2017-06-12 14:26:21 -040090
SJW2c317da2020-03-23 07:39:13 -050091 // 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 Neto22f144c2017-06-12 14:26:21 -0400101 }
102
SJW2c317da2020-03-23 07:39:13 -0500103 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
116void 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 Neto22f144c2017-06-12 14:26:21 -0400135}
136
137bool SplatArgPass::runOnModule(Module &M) {
SJW2c317da2020-03-23 07:39:13 -0500138 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 &param_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 Neto22f144c2017-06-12 14:26:21 -0400163 }
SJW2c317da2020-03-23 07:39:13 -0500164 func_list.push_front(&F);
David Neto22f144c2017-06-12 14:26:21 -0400165 }
SJW2c317da2020-03-23 07:39:13 -0500166 break;
167 }
168 default:
169 break;
David Neto22f144c2017-06-12 14:26:21 -0400170 }
171 }
172 }
SJW2c317da2020-03-23 07:39:13 -0500173 if (func_list.size() != 0) {
174 // remove dead
175 for (auto *F : func_list) {
176 if (F->use_empty()) {
177 F->eraseFromParent();
David Neto22f144c2017-06-12 14:26:21 -0400178 }
179 }
SJW2c317da2020-03-23 07:39:13 -0500180 return true;
David Neto22f144c2017-06-12 14:26:21 -0400181 }
SJW2c317da2020-03-23 07:39:13 -0500182 return false;
David Neto22f144c2017-06-12 14:26:21 -0400183}