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 | |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 15 | #include <math.h> |
| 16 | #include <string> |
| 17 | #include <tuple> |
| 18 | |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringSwitch.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 20 | #include "llvm/IR/Constants.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 21 | #include "llvm/IR/IRBuilder.h" |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 22 | #include "llvm/IR/Instructions.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 24 | #include "llvm/IR/ValueSymbolTable.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
| 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include "llvm/Transforms/Utils/Cloning.h" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 29 | |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 30 | #include "spirv/1.0/spirv.hpp" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 31 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 32 | #include "clspv/Option.h" |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 33 | |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 34 | #include "Passes.h" |
| 35 | #include "SPIRVOp.h" |
| 36 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
| 39 | #define DEBUG_TYPE "ReplaceOpenCLBuiltin" |
| 40 | |
| 41 | namespace { |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 42 | |
| 43 | struct ArgTypeInfo { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 44 | enum class SignedNess { None, Unsigned, Signed }; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 45 | SignedNess signedness; |
| 46 | }; |
| 47 | |
| 48 | struct FunctionInfo { |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 49 | StringRef name; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 50 | std::vector<ArgTypeInfo> argTypeInfos; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 51 | |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 52 | bool isArgSigned(size_t arg) const { |
| 53 | assert(argTypeInfos.size() > arg); |
| 54 | return argTypeInfos[arg].signedness == ArgTypeInfo::SignedNess::Signed; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 57 | static FunctionInfo getFromMangledName(StringRef name) { |
| 58 | FunctionInfo fi; |
| 59 | if (!getFromMangledNameCheck(name, &fi)) { |
| 60 | llvm_unreachable("Can't parse mangled function name!"); |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 61 | } |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 62 | return fi; |
| 63 | } |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 64 | |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 65 | static bool getFromMangledNameCheck(StringRef name, FunctionInfo *finfo) { |
| 66 | if (!name.consume_front("_Z")) { |
| 67 | return false; |
| 68 | } |
| 69 | size_t nameLen; |
| 70 | if (name.consumeInteger(10, nameLen)) { |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 74 | finfo->name = name.take_front(nameLen); |
| 75 | name = name.drop_front(nameLen); |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 76 | |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 77 | ArgTypeInfo prev_ti; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 78 | |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 79 | while (name.size() != 0) { |
| 80 | |
| 81 | ArgTypeInfo ti; |
| 82 | |
| 83 | // Try parsing a vector prefix |
| 84 | if (name.consume_front("Dv")) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 85 | int numElems; |
| 86 | if (name.consumeInteger(10, numElems)) { |
| 87 | return false; |
| 88 | } |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 89 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 90 | if (!name.consume_front("_")) { |
| 91 | return false; |
| 92 | } |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Parse the base type |
| 96 | char typeCode = name.front(); |
| 97 | name = name.drop_front(1); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 98 | switch (typeCode) { |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 99 | case 'c': // char |
| 100 | case 'a': // signed char |
| 101 | case 's': // short |
| 102 | case 'i': // int |
| 103 | case 'l': // long |
| 104 | ti.signedness = ArgTypeInfo::SignedNess::Signed; |
| 105 | break; |
| 106 | case 'h': // unsigned char |
| 107 | case 't': // unsigned short |
| 108 | case 'j': // unsigned int |
| 109 | case 'm': // unsigned long |
| 110 | ti.signedness = ArgTypeInfo::SignedNess::Unsigned; |
| 111 | break; |
| 112 | case 'f': |
| 113 | ti.signedness = ArgTypeInfo::SignedNess::None; |
| 114 | break; |
| 115 | case 'S': |
| 116 | ti = prev_ti; |
| 117 | if (!name.consume_front("_")) { |
| 118 | return false; |
| 119 | } |
| 120 | break; |
| 121 | default: |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | finfo->argTypeInfos.push_back(ti); |
| 126 | |
| 127 | prev_ti = ti; |
| 128 | } |
| 129 | |
| 130 | return true; |
| 131 | }; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 134 | uint32_t clz(uint32_t v) { |
| 135 | uint32_t r; |
| 136 | uint32_t shift; |
| 137 | |
| 138 | r = (v > 0xFFFF) << 4; |
| 139 | v >>= r; |
| 140 | shift = (v > 0xFF) << 3; |
| 141 | v >>= shift; |
| 142 | r |= shift; |
| 143 | shift = (v > 0xF) << 2; |
| 144 | v >>= shift; |
| 145 | r |= shift; |
| 146 | shift = (v > 0x3) << 1; |
| 147 | v >>= shift; |
| 148 | r |= shift; |
| 149 | r |= (v >> 1); |
| 150 | |
| 151 | return r; |
| 152 | } |
| 153 | |
| 154 | Type *getBoolOrBoolVectorTy(LLVMContext &C, unsigned elements) { |
| 155 | if (1 == elements) { |
| 156 | return Type::getInt1Ty(C); |
| 157 | } else { |
| 158 | return VectorType::get(Type::getInt1Ty(C), elements); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | struct ReplaceOpenCLBuiltinPass final : public ModulePass { |
| 163 | static char ID; |
| 164 | ReplaceOpenCLBuiltinPass() : ModulePass(ID) {} |
| 165 | |
| 166 | bool runOnModule(Module &M) override; |
Kévin Petit | 2444e9b | 2018-11-09 14:14:37 +0000 | [diff] [blame] | 167 | bool replaceAbs(Module &M); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 168 | bool replaceAbsDiff(Module &M); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 169 | bool replaceCopysign(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 170 | bool replaceRecip(Module &M); |
| 171 | bool replaceDivide(Module &M); |
| 172 | bool replaceExp10(Module &M); |
| 173 | bool replaceLog10(Module &M); |
| 174 | bool replaceBarrier(Module &M); |
| 175 | bool replaceMemFence(Module &M); |
| 176 | bool replaceRelational(Module &M); |
| 177 | bool replaceIsInfAndIsNan(Module &M); |
| 178 | bool replaceAllAndAny(Module &M); |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 179 | bool replaceUpsample(Module &M); |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 180 | bool replaceRotate(Module &M); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 181 | bool replaceConvert(Module &M); |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 182 | bool replaceMulHiMadHi(Module &M); |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 183 | bool replaceSelect(Module &M); |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 184 | bool replaceBitSelect(Module &M); |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 185 | bool replaceStepSmoothStep(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 186 | bool replaceSignbit(Module &M); |
| 187 | bool replaceMadandMad24andMul24(Module &M); |
| 188 | bool replaceVloadHalf(Module &M); |
| 189 | bool replaceVloadHalf2(Module &M); |
| 190 | bool replaceVloadHalf4(Module &M); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 191 | bool replaceClspvVloadaHalf2(Module &M); |
| 192 | bool replaceClspvVloadaHalf4(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 193 | bool replaceVstoreHalf(Module &M); |
| 194 | bool replaceVstoreHalf2(Module &M); |
| 195 | bool replaceVstoreHalf4(Module &M); |
| 196 | bool replaceReadImageF(Module &M); |
| 197 | bool replaceAtomics(Module &M); |
| 198 | bool replaceCross(Module &M); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 199 | bool replaceFract(Module &M); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 200 | bool replaceVload(Module &M); |
| 201 | bool replaceVstore(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 202 | }; |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 203 | } // namespace |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 204 | |
| 205 | char ReplaceOpenCLBuiltinPass::ID = 0; |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 206 | INITIALIZE_PASS(ReplaceOpenCLBuiltinPass, "ReplaceOpenCLBuiltin", |
| 207 | "Replace OpenCL Builtins Pass", false, false) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 208 | |
| 209 | namespace clspv { |
| 210 | ModulePass *createReplaceOpenCLBuiltinPass() { |
| 211 | return new ReplaceOpenCLBuiltinPass(); |
| 212 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 213 | } // namespace clspv |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 214 | |
| 215 | bool ReplaceOpenCLBuiltinPass::runOnModule(Module &M) { |
| 216 | bool Changed = false; |
| 217 | |
Kévin Petit | 2444e9b | 2018-11-09 14:14:37 +0000 | [diff] [blame] | 218 | Changed |= replaceAbs(M); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 219 | Changed |= replaceAbsDiff(M); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 220 | Changed |= replaceCopysign(M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 221 | Changed |= replaceRecip(M); |
| 222 | Changed |= replaceDivide(M); |
| 223 | Changed |= replaceExp10(M); |
| 224 | Changed |= replaceLog10(M); |
| 225 | Changed |= replaceBarrier(M); |
| 226 | Changed |= replaceMemFence(M); |
| 227 | Changed |= replaceRelational(M); |
| 228 | Changed |= replaceIsInfAndIsNan(M); |
| 229 | Changed |= replaceAllAndAny(M); |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 230 | Changed |= replaceUpsample(M); |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 231 | Changed |= replaceRotate(M); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 232 | Changed |= replaceConvert(M); |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 233 | Changed |= replaceMulHiMadHi(M); |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 234 | Changed |= replaceSelect(M); |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 235 | Changed |= replaceBitSelect(M); |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 236 | Changed |= replaceStepSmoothStep(M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 237 | Changed |= replaceSignbit(M); |
| 238 | Changed |= replaceMadandMad24andMul24(M); |
| 239 | Changed |= replaceVloadHalf(M); |
| 240 | Changed |= replaceVloadHalf2(M); |
| 241 | Changed |= replaceVloadHalf4(M); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 242 | Changed |= replaceClspvVloadaHalf2(M); |
| 243 | Changed |= replaceClspvVloadaHalf4(M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 244 | Changed |= replaceVstoreHalf(M); |
| 245 | Changed |= replaceVstoreHalf2(M); |
| 246 | Changed |= replaceVstoreHalf4(M); |
| 247 | Changed |= replaceReadImageF(M); |
| 248 | Changed |= replaceAtomics(M); |
| 249 | Changed |= replaceCross(M); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 250 | Changed |= replaceFract(M); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 251 | Changed |= replaceVload(M); |
| 252 | Changed |= replaceVstore(M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 253 | |
| 254 | return Changed; |
| 255 | } |
| 256 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 257 | bool replaceCallsWithValue(Module &M, std::vector<const char *> Names, |
| 258 | std::function<Value *(CallInst *)> Replacer) { |
Kévin Petit | 2444e9b | 2018-11-09 14:14:37 +0000 | [diff] [blame] | 259 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 260 | bool Changed = false; |
Kévin Petit | 2444e9b | 2018-11-09 14:14:37 +0000 | [diff] [blame] | 261 | |
| 262 | for (auto Name : Names) { |
| 263 | // If we find a function with the matching name. |
| 264 | if (auto F = M.getFunction(Name)) { |
| 265 | SmallVector<Instruction *, 4> ToRemoves; |
| 266 | |
| 267 | // Walk the users of the function. |
| 268 | for (auto &U : F->uses()) { |
| 269 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
Kévin Petit | 2444e9b | 2018-11-09 14:14:37 +0000 | [diff] [blame] | 270 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 271 | auto NewValue = Replacer(CI); |
| 272 | |
| 273 | if (NewValue != nullptr) { |
| 274 | CI->replaceAllUsesWith(NewValue); |
| 275 | } |
Kévin Petit | 2444e9b | 2018-11-09 14:14:37 +0000 | [diff] [blame] | 276 | |
| 277 | // Lastly, remember to remove the user. |
| 278 | ToRemoves.push_back(CI); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | Changed = !ToRemoves.empty(); |
| 283 | |
| 284 | // And cleanup the calls we don't use anymore. |
| 285 | for (auto V : ToRemoves) { |
| 286 | V->eraseFromParent(); |
| 287 | } |
| 288 | |
| 289 | // And remove the function we don't need either too. |
| 290 | F->eraseFromParent(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return Changed; |
| 295 | } |
| 296 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 297 | bool ReplaceOpenCLBuiltinPass::replaceAbs(Module &M) { |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 298 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 299 | std::vector<const char *> Names = { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 300 | "_Z3absh", "_Z3absDv2_h", "_Z3absDv3_h", "_Z3absDv4_h", |
| 301 | "_Z3abst", "_Z3absDv2_t", "_Z3absDv3_t", "_Z3absDv4_t", |
| 302 | "_Z3absj", "_Z3absDv2_j", "_Z3absDv3_j", "_Z3absDv4_j", |
| 303 | "_Z3absm", "_Z3absDv2_m", "_Z3absDv3_m", "_Z3absDv4_m", |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 304 | }; |
| 305 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 306 | return replaceCallsWithValue(M, Names, |
| 307 | [](CallInst *CI) { return CI->getOperand(0); }); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | bool ReplaceOpenCLBuiltinPass::replaceAbsDiff(Module &M) { |
| 311 | |
| 312 | std::vector<const char *> Names = { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 313 | "_Z8abs_diffcc", "_Z8abs_diffDv2_cS_", "_Z8abs_diffDv3_cS_", |
| 314 | "_Z8abs_diffDv4_cS_", "_Z8abs_diffhh", "_Z8abs_diffDv2_hS_", |
| 315 | "_Z8abs_diffDv3_hS_", "_Z8abs_diffDv4_hS_", "_Z8abs_diffss", |
| 316 | "_Z8abs_diffDv2_sS_", "_Z8abs_diffDv3_sS_", "_Z8abs_diffDv4_sS_", |
| 317 | "_Z8abs_difftt", "_Z8abs_diffDv2_tS_", "_Z8abs_diffDv3_tS_", |
| 318 | "_Z8abs_diffDv4_tS_", "_Z8abs_diffii", "_Z8abs_diffDv2_iS_", |
| 319 | "_Z8abs_diffDv3_iS_", "_Z8abs_diffDv4_iS_", "_Z8abs_diffjj", |
| 320 | "_Z8abs_diffDv2_jS_", "_Z8abs_diffDv3_jS_", "_Z8abs_diffDv4_jS_", |
| 321 | "_Z8abs_diffll", "_Z8abs_diffDv2_lS_", "_Z8abs_diffDv3_lS_", |
| 322 | "_Z8abs_diffDv4_lS_", "_Z8abs_diffmm", "_Z8abs_diffDv2_mS_", |
| 323 | "_Z8abs_diffDv3_mS_", "_Z8abs_diffDv4_mS_", |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 324 | }; |
| 325 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 326 | return replaceCallsWithValue(M, Names, [](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 327 | auto XValue = CI->getOperand(0); |
| 328 | auto YValue = CI->getOperand(1); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 329 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 330 | IRBuilder<> Builder(CI); |
| 331 | auto XmY = Builder.CreateSub(XValue, YValue); |
| 332 | auto YmX = Builder.CreateSub(YValue, XValue); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 333 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 334 | Value *Cmp; |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 335 | auto F = CI->getCalledFunction(); |
| 336 | auto finfo = FunctionInfo::getFromMangledName(F->getName()); |
| 337 | if (finfo.isArgSigned(0)) { |
| 338 | Cmp = Builder.CreateICmpSGT(YValue, XValue); |
| 339 | } else { |
| 340 | Cmp = Builder.CreateICmpUGT(YValue, XValue); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 341 | } |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 342 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 343 | return Builder.CreateSelect(Cmp, YmX, XmY); |
| 344 | }); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 347 | bool ReplaceOpenCLBuiltinPass::replaceCopysign(Module &M) { |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 348 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 349 | std::vector<const char *> Names = { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 350 | "_Z8copysignff", |
| 351 | "_Z8copysignDv2_fS_", |
| 352 | "_Z8copysignDv3_fS_", |
| 353 | "_Z8copysignDv4_fS_", |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 354 | }; |
| 355 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 356 | return replaceCallsWithValue(M, Names, [&M](CallInst *CI) { |
| 357 | auto XValue = CI->getOperand(0); |
| 358 | auto YValue = CI->getOperand(1); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 359 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 360 | auto Ty = XValue->getType(); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 361 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 362 | Type *IntTy = Type::getIntNTy(M.getContext(), Ty->getScalarSizeInBits()); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 363 | if (Ty->isVectorTy()) { |
| 364 | IntTy = VectorType::get(IntTy, Ty->getVectorNumElements()); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 365 | } |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 366 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 367 | // Return X with the sign of Y |
| 368 | |
| 369 | // Sign bit masks |
| 370 | auto SignBit = IntTy->getScalarSizeInBits() - 1; |
| 371 | auto SignBitMask = 1 << SignBit; |
| 372 | auto SignBitMaskValue = ConstantInt::get(IntTy, SignBitMask); |
| 373 | auto NotSignBitMaskValue = ConstantInt::get(IntTy, ~SignBitMask); |
| 374 | |
| 375 | IRBuilder<> Builder(CI); |
| 376 | |
| 377 | // Extract sign of Y |
| 378 | auto YInt = Builder.CreateBitCast(YValue, IntTy); |
| 379 | auto YSign = Builder.CreateAnd(YInt, SignBitMaskValue); |
| 380 | |
| 381 | // Clear sign bit in X |
| 382 | auto XInt = Builder.CreateBitCast(XValue, IntTy); |
| 383 | XInt = Builder.CreateAnd(XInt, NotSignBitMaskValue); |
| 384 | |
| 385 | // Insert sign bit of Y into X |
| 386 | auto NewXInt = Builder.CreateOr(XInt, YSign); |
| 387 | |
| 388 | // And cast back to floating-point |
| 389 | return Builder.CreateBitCast(NewXInt, Ty); |
| 390 | }); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 391 | } |
| 392 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 393 | bool ReplaceOpenCLBuiltinPass::replaceRecip(Module &M) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 394 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 395 | std::vector<const char *> Names = { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 396 | "_Z10half_recipf", "_Z12native_recipf", "_Z10half_recipDv2_f", |
| 397 | "_Z12native_recipDv2_f", "_Z10half_recipDv3_f", "_Z12native_recipDv3_f", |
| 398 | "_Z10half_recipDv4_f", "_Z12native_recipDv4_f", |
| 399 | }; |
| 400 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 401 | return replaceCallsWithValue(M, Names, [](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 402 | // Recip has one arg. |
| 403 | auto Arg = CI->getOperand(0); |
| 404 | auto Cst1 = ConstantFP::get(Arg->getType(), 1.0); |
| 405 | return BinaryOperator::Create(Instruction::FDiv, Cst1, Arg, "", CI); |
| 406 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | bool ReplaceOpenCLBuiltinPass::replaceDivide(Module &M) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 410 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 411 | std::vector<const char *> Names = { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 412 | "_Z11half_divideff", "_Z13native_divideff", |
| 413 | "_Z11half_divideDv2_fS_", "_Z13native_divideDv2_fS_", |
| 414 | "_Z11half_divideDv3_fS_", "_Z13native_divideDv3_fS_", |
| 415 | "_Z11half_divideDv4_fS_", "_Z13native_divideDv4_fS_", |
| 416 | }; |
| 417 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 418 | return replaceCallsWithValue(M, Names, [](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 419 | auto Op0 = CI->getOperand(0); |
| 420 | auto Op1 = CI->getOperand(1); |
| 421 | return BinaryOperator::Create(Instruction::FDiv, Op0, Op1, "", CI); |
| 422 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | bool ReplaceOpenCLBuiltinPass::replaceExp10(Module &M) { |
| 426 | bool Changed = false; |
| 427 | |
| 428 | const std::map<const char *, const char *> Map = { |
| 429 | {"_Z5exp10f", "_Z3expf"}, |
| 430 | {"_Z10half_exp10f", "_Z8half_expf"}, |
| 431 | {"_Z12native_exp10f", "_Z10native_expf"}, |
| 432 | {"_Z5exp10Dv2_f", "_Z3expDv2_f"}, |
| 433 | {"_Z10half_exp10Dv2_f", "_Z8half_expDv2_f"}, |
| 434 | {"_Z12native_exp10Dv2_f", "_Z10native_expDv2_f"}, |
| 435 | {"_Z5exp10Dv3_f", "_Z3expDv3_f"}, |
| 436 | {"_Z10half_exp10Dv3_f", "_Z8half_expDv3_f"}, |
| 437 | {"_Z12native_exp10Dv3_f", "_Z10native_expDv3_f"}, |
| 438 | {"_Z5exp10Dv4_f", "_Z3expDv4_f"}, |
| 439 | {"_Z10half_exp10Dv4_f", "_Z8half_expDv4_f"}, |
| 440 | {"_Z12native_exp10Dv4_f", "_Z10native_expDv4_f"}}; |
| 441 | |
| 442 | for (auto Pair : Map) { |
| 443 | // If we find a function with the matching name. |
| 444 | if (auto F = M.getFunction(Pair.first)) { |
| 445 | SmallVector<Instruction *, 4> ToRemoves; |
| 446 | |
| 447 | // Walk the users of the function. |
| 448 | for (auto &U : F->uses()) { |
| 449 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 450 | auto NewF = M.getOrInsertFunction(Pair.second, F->getFunctionType()); |
| 451 | |
| 452 | auto Arg = CI->getOperand(0); |
| 453 | |
| 454 | // Constant of the natural log of 10 (ln(10)). |
| 455 | const double Ln10 = |
| 456 | 2.302585092994045684017991454684364207601101488628772976033; |
| 457 | |
| 458 | auto Mul = BinaryOperator::Create( |
| 459 | Instruction::FMul, ConstantFP::get(Arg->getType(), Ln10), Arg, "", |
| 460 | CI); |
| 461 | |
| 462 | auto NewCI = CallInst::Create(NewF, Mul, "", CI); |
| 463 | |
| 464 | CI->replaceAllUsesWith(NewCI); |
| 465 | |
| 466 | // Lastly, remember to remove the user. |
| 467 | ToRemoves.push_back(CI); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | Changed = !ToRemoves.empty(); |
| 472 | |
| 473 | // And cleanup the calls we don't use anymore. |
| 474 | for (auto V : ToRemoves) { |
| 475 | V->eraseFromParent(); |
| 476 | } |
| 477 | |
| 478 | // And remove the function we don't need either too. |
| 479 | F->eraseFromParent(); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return Changed; |
| 484 | } |
| 485 | |
| 486 | bool ReplaceOpenCLBuiltinPass::replaceLog10(Module &M) { |
| 487 | bool Changed = false; |
| 488 | |
| 489 | const std::map<const char *, const char *> Map = { |
| 490 | {"_Z5log10f", "_Z3logf"}, |
| 491 | {"_Z10half_log10f", "_Z8half_logf"}, |
| 492 | {"_Z12native_log10f", "_Z10native_logf"}, |
| 493 | {"_Z5log10Dv2_f", "_Z3logDv2_f"}, |
| 494 | {"_Z10half_log10Dv2_f", "_Z8half_logDv2_f"}, |
| 495 | {"_Z12native_log10Dv2_f", "_Z10native_logDv2_f"}, |
| 496 | {"_Z5log10Dv3_f", "_Z3logDv3_f"}, |
| 497 | {"_Z10half_log10Dv3_f", "_Z8half_logDv3_f"}, |
| 498 | {"_Z12native_log10Dv3_f", "_Z10native_logDv3_f"}, |
| 499 | {"_Z5log10Dv4_f", "_Z3logDv4_f"}, |
| 500 | {"_Z10half_log10Dv4_f", "_Z8half_logDv4_f"}, |
| 501 | {"_Z12native_log10Dv4_f", "_Z10native_logDv4_f"}}; |
| 502 | |
| 503 | for (auto Pair : Map) { |
| 504 | // If we find a function with the matching name. |
| 505 | if (auto F = M.getFunction(Pair.first)) { |
| 506 | SmallVector<Instruction *, 4> ToRemoves; |
| 507 | |
| 508 | // Walk the users of the function. |
| 509 | for (auto &U : F->uses()) { |
| 510 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 511 | auto NewF = M.getOrInsertFunction(Pair.second, F->getFunctionType()); |
| 512 | |
| 513 | auto Arg = CI->getOperand(0); |
| 514 | |
| 515 | // Constant of the reciprocal of the natural log of 10 (ln(10)). |
| 516 | const double Ln10 = |
| 517 | 0.434294481903251827651128918916605082294397005803666566114; |
| 518 | |
| 519 | auto NewCI = CallInst::Create(NewF, Arg, "", CI); |
| 520 | |
| 521 | auto Mul = BinaryOperator::Create( |
| 522 | Instruction::FMul, ConstantFP::get(Arg->getType(), Ln10), NewCI, |
| 523 | "", CI); |
| 524 | |
| 525 | CI->replaceAllUsesWith(Mul); |
| 526 | |
| 527 | // Lastly, remember to remove the user. |
| 528 | ToRemoves.push_back(CI); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | Changed = !ToRemoves.empty(); |
| 533 | |
| 534 | // And cleanup the calls we don't use anymore. |
| 535 | for (auto V : ToRemoves) { |
| 536 | V->eraseFromParent(); |
| 537 | } |
| 538 | |
| 539 | // And remove the function we don't need either too. |
| 540 | F->eraseFromParent(); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | return Changed; |
| 545 | } |
| 546 | |
| 547 | bool ReplaceOpenCLBuiltinPass::replaceBarrier(Module &M) { |
| 548 | bool Changed = false; |
| 549 | |
| 550 | enum { CLK_LOCAL_MEM_FENCE = 0x01, CLK_GLOBAL_MEM_FENCE = 0x02 }; |
| 551 | |
| 552 | const std::map<const char *, const char *> Map = { |
| 553 | {"_Z7barrierj", "__spirv_control_barrier"}}; |
| 554 | |
| 555 | for (auto Pair : Map) { |
| 556 | // If we find a function with the matching name. |
| 557 | if (auto F = M.getFunction(Pair.first)) { |
| 558 | SmallVector<Instruction *, 4> ToRemoves; |
| 559 | |
| 560 | // Walk the users of the function. |
| 561 | for (auto &U : F->uses()) { |
| 562 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 563 | auto FType = F->getFunctionType(); |
| 564 | SmallVector<Type *, 3> Params; |
| 565 | for (unsigned i = 0; i < 3; i++) { |
| 566 | Params.push_back(FType->getParamType(0)); |
| 567 | } |
| 568 | auto NewFType = |
| 569 | FunctionType::get(FType->getReturnType(), Params, false); |
| 570 | auto NewF = M.getOrInsertFunction(Pair.second, NewFType); |
alan-baker | b37f973 | 2019-06-05 01:28:00 -0400 | [diff] [blame] | 571 | cast<Function>(NewF.getCallee())->setCannotDuplicate(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 572 | |
| 573 | auto Arg = CI->getOperand(0); |
| 574 | |
| 575 | // We need to map the OpenCL constants to the SPIR-V equivalents. |
| 576 | const auto LocalMemFence = |
| 577 | ConstantInt::get(Arg->getType(), CLK_LOCAL_MEM_FENCE); |
| 578 | const auto GlobalMemFence = |
| 579 | ConstantInt::get(Arg->getType(), CLK_GLOBAL_MEM_FENCE); |
| 580 | const auto ConstantSequentiallyConsistent = ConstantInt::get( |
| 581 | Arg->getType(), spv::MemorySemanticsSequentiallyConsistentMask); |
| 582 | const auto ConstantScopeDevice = |
| 583 | ConstantInt::get(Arg->getType(), spv::ScopeDevice); |
| 584 | const auto ConstantScopeWorkgroup = |
| 585 | ConstantInt::get(Arg->getType(), spv::ScopeWorkgroup); |
| 586 | |
| 587 | // Map CLK_LOCAL_MEM_FENCE to MemorySemanticsWorkgroupMemoryMask. |
| 588 | const auto LocalMemFenceMask = BinaryOperator::Create( |
| 589 | Instruction::And, LocalMemFence, Arg, "", CI); |
| 590 | const auto WorkgroupShiftAmount = |
| 591 | clz(spv::MemorySemanticsWorkgroupMemoryMask) - |
| 592 | clz(CLK_LOCAL_MEM_FENCE); |
| 593 | const auto MemorySemanticsWorkgroup = BinaryOperator::Create( |
| 594 | Instruction::Shl, LocalMemFenceMask, |
| 595 | ConstantInt::get(Arg->getType(), WorkgroupShiftAmount), "", CI); |
| 596 | |
| 597 | // Map CLK_GLOBAL_MEM_FENCE to MemorySemanticsUniformMemoryMask. |
| 598 | const auto GlobalMemFenceMask = BinaryOperator::Create( |
| 599 | Instruction::And, GlobalMemFence, Arg, "", CI); |
| 600 | const auto UniformShiftAmount = |
| 601 | clz(spv::MemorySemanticsUniformMemoryMask) - |
| 602 | clz(CLK_GLOBAL_MEM_FENCE); |
| 603 | const auto MemorySemanticsUniform = BinaryOperator::Create( |
| 604 | Instruction::Shl, GlobalMemFenceMask, |
| 605 | ConstantInt::get(Arg->getType(), UniformShiftAmount), "", CI); |
| 606 | |
| 607 | // And combine the above together, also adding in |
| 608 | // MemorySemanticsSequentiallyConsistentMask. |
| 609 | auto MemorySemantics = |
| 610 | BinaryOperator::Create(Instruction::Or, MemorySemanticsWorkgroup, |
| 611 | ConstantSequentiallyConsistent, "", CI); |
| 612 | MemorySemantics = BinaryOperator::Create( |
| 613 | Instruction::Or, MemorySemantics, MemorySemanticsUniform, "", CI); |
| 614 | |
| 615 | // For Memory Scope if we used CLK_GLOBAL_MEM_FENCE, we need to use |
| 616 | // Device Scope, otherwise Workgroup Scope. |
| 617 | const auto Cmp = |
| 618 | CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, |
| 619 | GlobalMemFenceMask, GlobalMemFence, "", CI); |
| 620 | const auto MemoryScope = SelectInst::Create( |
| 621 | Cmp, ConstantScopeDevice, ConstantScopeWorkgroup, "", CI); |
| 622 | |
| 623 | // Lastly, the Execution Scope is always Workgroup Scope. |
| 624 | const auto ExecutionScope = ConstantScopeWorkgroup; |
| 625 | |
| 626 | auto NewCI = CallInst::Create( |
| 627 | NewF, {ExecutionScope, MemoryScope, MemorySemantics}, "", CI); |
| 628 | |
| 629 | CI->replaceAllUsesWith(NewCI); |
| 630 | |
| 631 | // Lastly, remember to remove the user. |
| 632 | ToRemoves.push_back(CI); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | Changed = !ToRemoves.empty(); |
| 637 | |
| 638 | // And cleanup the calls we don't use anymore. |
| 639 | for (auto V : ToRemoves) { |
| 640 | V->eraseFromParent(); |
| 641 | } |
| 642 | |
| 643 | // And remove the function we don't need either too. |
| 644 | F->eraseFromParent(); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return Changed; |
| 649 | } |
| 650 | |
| 651 | bool ReplaceOpenCLBuiltinPass::replaceMemFence(Module &M) { |
| 652 | bool Changed = false; |
| 653 | |
| 654 | enum { CLK_LOCAL_MEM_FENCE = 0x01, CLK_GLOBAL_MEM_FENCE = 0x02 }; |
| 655 | |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 656 | using Tuple = std::tuple<const char *, unsigned>; |
| 657 | const std::map<const char *, Tuple> Map = { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 658 | {"_Z9mem_fencej", Tuple("__spirv_memory_barrier", |
| 659 | spv::MemorySemanticsSequentiallyConsistentMask)}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 660 | {"_Z14read_mem_fencej", |
| 661 | Tuple("__spirv_memory_barrier", spv::MemorySemanticsAcquireMask)}, |
| 662 | {"_Z15write_mem_fencej", |
| 663 | Tuple("__spirv_memory_barrier", spv::MemorySemanticsReleaseMask)}}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 664 | |
| 665 | for (auto Pair : Map) { |
| 666 | // If we find a function with the matching name. |
| 667 | if (auto F = M.getFunction(Pair.first)) { |
| 668 | SmallVector<Instruction *, 4> ToRemoves; |
| 669 | |
| 670 | // Walk the users of the function. |
| 671 | for (auto &U : F->uses()) { |
| 672 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 673 | auto FType = F->getFunctionType(); |
| 674 | SmallVector<Type *, 2> Params; |
| 675 | for (unsigned i = 0; i < 2; i++) { |
| 676 | Params.push_back(FType->getParamType(0)); |
| 677 | } |
| 678 | auto NewFType = |
| 679 | FunctionType::get(FType->getReturnType(), Params, false); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 680 | auto NewF = M.getOrInsertFunction(std::get<0>(Pair.second), NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 681 | |
| 682 | auto Arg = CI->getOperand(0); |
| 683 | |
| 684 | // We need to map the OpenCL constants to the SPIR-V equivalents. |
| 685 | const auto LocalMemFence = |
| 686 | ConstantInt::get(Arg->getType(), CLK_LOCAL_MEM_FENCE); |
| 687 | const auto GlobalMemFence = |
| 688 | ConstantInt::get(Arg->getType(), CLK_GLOBAL_MEM_FENCE); |
| 689 | const auto ConstantMemorySemantics = |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 690 | ConstantInt::get(Arg->getType(), std::get<1>(Pair.second)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 691 | const auto ConstantScopeDevice = |
| 692 | ConstantInt::get(Arg->getType(), spv::ScopeDevice); |
| 693 | |
| 694 | // Map CLK_LOCAL_MEM_FENCE to MemorySemanticsWorkgroupMemoryMask. |
| 695 | const auto LocalMemFenceMask = BinaryOperator::Create( |
| 696 | Instruction::And, LocalMemFence, Arg, "", CI); |
| 697 | const auto WorkgroupShiftAmount = |
| 698 | clz(spv::MemorySemanticsWorkgroupMemoryMask) - |
| 699 | clz(CLK_LOCAL_MEM_FENCE); |
| 700 | const auto MemorySemanticsWorkgroup = BinaryOperator::Create( |
| 701 | Instruction::Shl, LocalMemFenceMask, |
| 702 | ConstantInt::get(Arg->getType(), WorkgroupShiftAmount), "", CI); |
| 703 | |
| 704 | // Map CLK_GLOBAL_MEM_FENCE to MemorySemanticsUniformMemoryMask. |
| 705 | const auto GlobalMemFenceMask = BinaryOperator::Create( |
| 706 | Instruction::And, GlobalMemFence, Arg, "", CI); |
| 707 | const auto UniformShiftAmount = |
| 708 | clz(spv::MemorySemanticsUniformMemoryMask) - |
| 709 | clz(CLK_GLOBAL_MEM_FENCE); |
| 710 | const auto MemorySemanticsUniform = BinaryOperator::Create( |
| 711 | Instruction::Shl, GlobalMemFenceMask, |
| 712 | ConstantInt::get(Arg->getType(), UniformShiftAmount), "", CI); |
| 713 | |
| 714 | // And combine the above together, also adding in |
| 715 | // MemorySemanticsSequentiallyConsistentMask. |
| 716 | auto MemorySemantics = |
| 717 | BinaryOperator::Create(Instruction::Or, MemorySemanticsWorkgroup, |
| 718 | ConstantMemorySemantics, "", CI); |
| 719 | MemorySemantics = BinaryOperator::Create( |
| 720 | Instruction::Or, MemorySemantics, MemorySemanticsUniform, "", CI); |
| 721 | |
| 722 | // Memory Scope is always device. |
| 723 | const auto MemoryScope = ConstantScopeDevice; |
| 724 | |
| 725 | auto NewCI = |
| 726 | CallInst::Create(NewF, {MemoryScope, MemorySemantics}, "", CI); |
| 727 | |
| 728 | CI->replaceAllUsesWith(NewCI); |
| 729 | |
| 730 | // Lastly, remember to remove the user. |
| 731 | ToRemoves.push_back(CI); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | Changed = !ToRemoves.empty(); |
| 736 | |
| 737 | // And cleanup the calls we don't use anymore. |
| 738 | for (auto V : ToRemoves) { |
| 739 | V->eraseFromParent(); |
| 740 | } |
| 741 | |
| 742 | // And remove the function we don't need either too. |
| 743 | F->eraseFromParent(); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | return Changed; |
| 748 | } |
| 749 | |
| 750 | bool ReplaceOpenCLBuiltinPass::replaceRelational(Module &M) { |
| 751 | bool Changed = false; |
| 752 | |
| 753 | const std::map<const char *, std::pair<CmpInst::Predicate, int32_t>> Map = { |
| 754 | {"_Z7isequalff", {CmpInst::FCMP_OEQ, 1}}, |
| 755 | {"_Z7isequalDv2_fS_", {CmpInst::FCMP_OEQ, -1}}, |
| 756 | {"_Z7isequalDv3_fS_", {CmpInst::FCMP_OEQ, -1}}, |
| 757 | {"_Z7isequalDv4_fS_", {CmpInst::FCMP_OEQ, -1}}, |
| 758 | {"_Z9isgreaterff", {CmpInst::FCMP_OGT, 1}}, |
| 759 | {"_Z9isgreaterDv2_fS_", {CmpInst::FCMP_OGT, -1}}, |
| 760 | {"_Z9isgreaterDv3_fS_", {CmpInst::FCMP_OGT, -1}}, |
| 761 | {"_Z9isgreaterDv4_fS_", {CmpInst::FCMP_OGT, -1}}, |
| 762 | {"_Z14isgreaterequalff", {CmpInst::FCMP_OGE, 1}}, |
| 763 | {"_Z14isgreaterequalDv2_fS_", {CmpInst::FCMP_OGE, -1}}, |
| 764 | {"_Z14isgreaterequalDv3_fS_", {CmpInst::FCMP_OGE, -1}}, |
| 765 | {"_Z14isgreaterequalDv4_fS_", {CmpInst::FCMP_OGE, -1}}, |
| 766 | {"_Z6islessff", {CmpInst::FCMP_OLT, 1}}, |
| 767 | {"_Z6islessDv2_fS_", {CmpInst::FCMP_OLT, -1}}, |
| 768 | {"_Z6islessDv3_fS_", {CmpInst::FCMP_OLT, -1}}, |
| 769 | {"_Z6islessDv4_fS_", {CmpInst::FCMP_OLT, -1}}, |
| 770 | {"_Z11islessequalff", {CmpInst::FCMP_OLE, 1}}, |
| 771 | {"_Z11islessequalDv2_fS_", {CmpInst::FCMP_OLE, -1}}, |
| 772 | {"_Z11islessequalDv3_fS_", {CmpInst::FCMP_OLE, -1}}, |
| 773 | {"_Z11islessequalDv4_fS_", {CmpInst::FCMP_OLE, -1}}, |
| 774 | {"_Z10isnotequalff", {CmpInst::FCMP_ONE, 1}}, |
| 775 | {"_Z10isnotequalDv2_fS_", {CmpInst::FCMP_ONE, -1}}, |
| 776 | {"_Z10isnotequalDv3_fS_", {CmpInst::FCMP_ONE, -1}}, |
| 777 | {"_Z10isnotequalDv4_fS_", {CmpInst::FCMP_ONE, -1}}, |
| 778 | }; |
| 779 | |
| 780 | for (auto Pair : Map) { |
| 781 | // If we find a function with the matching name. |
| 782 | if (auto F = M.getFunction(Pair.first)) { |
| 783 | SmallVector<Instruction *, 4> ToRemoves; |
| 784 | |
| 785 | // Walk the users of the function. |
| 786 | for (auto &U : F->uses()) { |
| 787 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 788 | // The predicate to use in the CmpInst. |
| 789 | auto Predicate = Pair.second.first; |
| 790 | |
| 791 | // The value to return for true. |
| 792 | auto TrueValue = |
| 793 | ConstantInt::getSigned(CI->getType(), Pair.second.second); |
| 794 | |
| 795 | // The value to return for false. |
| 796 | auto FalseValue = Constant::getNullValue(CI->getType()); |
| 797 | |
| 798 | auto Arg1 = CI->getOperand(0); |
| 799 | auto Arg2 = CI->getOperand(1); |
| 800 | |
| 801 | const auto Cmp = |
| 802 | CmpInst::Create(Instruction::FCmp, Predicate, Arg1, Arg2, "", CI); |
| 803 | |
| 804 | const auto Select = |
| 805 | SelectInst::Create(Cmp, TrueValue, FalseValue, "", CI); |
| 806 | |
| 807 | CI->replaceAllUsesWith(Select); |
| 808 | |
| 809 | // Lastly, remember to remove the user. |
| 810 | ToRemoves.push_back(CI); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | Changed = !ToRemoves.empty(); |
| 815 | |
| 816 | // And cleanup the calls we don't use anymore. |
| 817 | for (auto V : ToRemoves) { |
| 818 | V->eraseFromParent(); |
| 819 | } |
| 820 | |
| 821 | // And remove the function we don't need either too. |
| 822 | F->eraseFromParent(); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | return Changed; |
| 827 | } |
| 828 | |
| 829 | bool ReplaceOpenCLBuiltinPass::replaceIsInfAndIsNan(Module &M) { |
| 830 | bool Changed = false; |
| 831 | |
| 832 | const std::map<const char *, std::pair<const char *, int32_t>> Map = { |
| 833 | {"_Z5isinff", {"__spirv_isinff", 1}}, |
| 834 | {"_Z5isinfDv2_f", {"__spirv_isinfDv2_f", -1}}, |
| 835 | {"_Z5isinfDv3_f", {"__spirv_isinfDv3_f", -1}}, |
| 836 | {"_Z5isinfDv4_f", {"__spirv_isinfDv4_f", -1}}, |
| 837 | {"_Z5isnanf", {"__spirv_isnanf", 1}}, |
| 838 | {"_Z5isnanDv2_f", {"__spirv_isnanDv2_f", -1}}, |
| 839 | {"_Z5isnanDv3_f", {"__spirv_isnanDv3_f", -1}}, |
| 840 | {"_Z5isnanDv4_f", {"__spirv_isnanDv4_f", -1}}, |
| 841 | }; |
| 842 | |
| 843 | for (auto Pair : Map) { |
| 844 | // If we find a function with the matching name. |
| 845 | if (auto F = M.getFunction(Pair.first)) { |
| 846 | SmallVector<Instruction *, 4> ToRemoves; |
| 847 | |
| 848 | // Walk the users of the function. |
| 849 | for (auto &U : F->uses()) { |
| 850 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 851 | const auto CITy = CI->getType(); |
| 852 | |
| 853 | // The fake SPIR-V intrinsic to generate. |
| 854 | auto SPIRVIntrinsic = Pair.second.first; |
| 855 | |
| 856 | // The value to return for true. |
| 857 | auto TrueValue = ConstantInt::getSigned(CITy, Pair.second.second); |
| 858 | |
| 859 | // The value to return for false. |
| 860 | auto FalseValue = Constant::getNullValue(CITy); |
| 861 | |
| 862 | const auto CorrespondingBoolTy = getBoolOrBoolVectorTy( |
| 863 | M.getContext(), |
| 864 | CITy->isVectorTy() ? CITy->getVectorNumElements() : 1); |
| 865 | |
| 866 | auto NewFType = |
| 867 | FunctionType::get(CorrespondingBoolTy, |
| 868 | F->getFunctionType()->getParamType(0), false); |
| 869 | |
| 870 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 871 | |
| 872 | auto Arg = CI->getOperand(0); |
| 873 | |
| 874 | auto NewCI = CallInst::Create(NewF, Arg, "", CI); |
| 875 | |
| 876 | const auto Select = |
| 877 | SelectInst::Create(NewCI, TrueValue, FalseValue, "", CI); |
| 878 | |
| 879 | CI->replaceAllUsesWith(Select); |
| 880 | |
| 881 | // Lastly, remember to remove the user. |
| 882 | ToRemoves.push_back(CI); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | Changed = !ToRemoves.empty(); |
| 887 | |
| 888 | // And cleanup the calls we don't use anymore. |
| 889 | for (auto V : ToRemoves) { |
| 890 | V->eraseFromParent(); |
| 891 | } |
| 892 | |
| 893 | // And remove the function we don't need either too. |
| 894 | F->eraseFromParent(); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | return Changed; |
| 899 | } |
| 900 | |
| 901 | bool ReplaceOpenCLBuiltinPass::replaceAllAndAny(Module &M) { |
| 902 | bool Changed = false; |
| 903 | |
| 904 | const std::map<const char *, const char *> Map = { |
Kévin Petit | fd27cca | 2018-10-31 13:00:17 +0000 | [diff] [blame] | 905 | // all |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 906 | {"_Z3allc", ""}, |
| 907 | {"_Z3allDv2_c", "__spirv_allDv2_c"}, |
| 908 | {"_Z3allDv3_c", "__spirv_allDv3_c"}, |
| 909 | {"_Z3allDv4_c", "__spirv_allDv4_c"}, |
Kévin Petit | fd27cca | 2018-10-31 13:00:17 +0000 | [diff] [blame] | 910 | {"_Z3alls", ""}, |
| 911 | {"_Z3allDv2_s", "__spirv_allDv2_s"}, |
| 912 | {"_Z3allDv3_s", "__spirv_allDv3_s"}, |
| 913 | {"_Z3allDv4_s", "__spirv_allDv4_s"}, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 914 | {"_Z3alli", ""}, |
| 915 | {"_Z3allDv2_i", "__spirv_allDv2_i"}, |
| 916 | {"_Z3allDv3_i", "__spirv_allDv3_i"}, |
| 917 | {"_Z3allDv4_i", "__spirv_allDv4_i"}, |
Kévin Petit | fd27cca | 2018-10-31 13:00:17 +0000 | [diff] [blame] | 918 | {"_Z3alll", ""}, |
| 919 | {"_Z3allDv2_l", "__spirv_allDv2_l"}, |
| 920 | {"_Z3allDv3_l", "__spirv_allDv3_l"}, |
| 921 | {"_Z3allDv4_l", "__spirv_allDv4_l"}, |
| 922 | |
| 923 | // any |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 924 | {"_Z3anyc", ""}, |
| 925 | {"_Z3anyDv2_c", "__spirv_anyDv2_c"}, |
| 926 | {"_Z3anyDv3_c", "__spirv_anyDv3_c"}, |
| 927 | {"_Z3anyDv4_c", "__spirv_anyDv4_c"}, |
Kévin Petit | fd27cca | 2018-10-31 13:00:17 +0000 | [diff] [blame] | 928 | {"_Z3anys", ""}, |
| 929 | {"_Z3anyDv2_s", "__spirv_anyDv2_s"}, |
| 930 | {"_Z3anyDv3_s", "__spirv_anyDv3_s"}, |
| 931 | {"_Z3anyDv4_s", "__spirv_anyDv4_s"}, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 932 | {"_Z3anyi", ""}, |
| 933 | {"_Z3anyDv2_i", "__spirv_anyDv2_i"}, |
| 934 | {"_Z3anyDv3_i", "__spirv_anyDv3_i"}, |
| 935 | {"_Z3anyDv4_i", "__spirv_anyDv4_i"}, |
Kévin Petit | fd27cca | 2018-10-31 13:00:17 +0000 | [diff] [blame] | 936 | {"_Z3anyl", ""}, |
| 937 | {"_Z3anyDv2_l", "__spirv_anyDv2_l"}, |
| 938 | {"_Z3anyDv3_l", "__spirv_anyDv3_l"}, |
| 939 | {"_Z3anyDv4_l", "__spirv_anyDv4_l"}, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 940 | }; |
| 941 | |
| 942 | for (auto Pair : Map) { |
| 943 | // If we find a function with the matching name. |
| 944 | if (auto F = M.getFunction(Pair.first)) { |
| 945 | SmallVector<Instruction *, 4> ToRemoves; |
| 946 | |
| 947 | // Walk the users of the function. |
| 948 | for (auto &U : F->uses()) { |
| 949 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 950 | // The fake SPIR-V intrinsic to generate. |
| 951 | auto SPIRVIntrinsic = Pair.second; |
| 952 | |
| 953 | auto Arg = CI->getOperand(0); |
| 954 | |
| 955 | Value *V; |
| 956 | |
Kévin Petit | fd27cca | 2018-10-31 13:00:17 +0000 | [diff] [blame] | 957 | // If the argument is a 32-bit int, just use a shift |
| 958 | if (Arg->getType() == Type::getInt32Ty(M.getContext())) { |
| 959 | V = BinaryOperator::Create(Instruction::LShr, Arg, |
| 960 | ConstantInt::get(Arg->getType(), 31), "", |
| 961 | CI); |
| 962 | } else { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 963 | // The value for zero to compare against. |
| 964 | const auto ZeroValue = Constant::getNullValue(Arg->getType()); |
| 965 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 966 | // The value to return for true. |
| 967 | const auto TrueValue = ConstantInt::get(CI->getType(), 1); |
| 968 | |
| 969 | // The value to return for false. |
| 970 | const auto FalseValue = Constant::getNullValue(CI->getType()); |
| 971 | |
Kévin Petit | fd27cca | 2018-10-31 13:00:17 +0000 | [diff] [blame] | 972 | const auto Cmp = CmpInst::Create( |
| 973 | Instruction::ICmp, CmpInst::ICMP_SLT, Arg, ZeroValue, "", CI); |
| 974 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 975 | Value *SelectSource; |
Kévin Petit | fd27cca | 2018-10-31 13:00:17 +0000 | [diff] [blame] | 976 | |
| 977 | // If we have a function to call, call it! |
| 978 | if (0 < strlen(SPIRVIntrinsic)) { |
| 979 | |
| 980 | const auto NewFType = FunctionType::get( |
| 981 | Type::getInt1Ty(M.getContext()), Cmp->getType(), false); |
| 982 | |
| 983 | const auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 984 | |
| 985 | const auto NewCI = CallInst::Create(NewF, Cmp, "", CI); |
| 986 | |
| 987 | SelectSource = NewCI; |
| 988 | |
| 989 | } else { |
| 990 | SelectSource = Cmp; |
| 991 | } |
| 992 | |
| 993 | V = SelectInst::Create(SelectSource, TrueValue, FalseValue, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | CI->replaceAllUsesWith(V); |
| 997 | |
| 998 | // Lastly, remember to remove the user. |
| 999 | ToRemoves.push_back(CI); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | Changed = !ToRemoves.empty(); |
| 1004 | |
| 1005 | // And cleanup the calls we don't use anymore. |
| 1006 | for (auto V : ToRemoves) { |
| 1007 | V->eraseFromParent(); |
| 1008 | } |
| 1009 | |
| 1010 | // And remove the function we don't need either too. |
| 1011 | F->eraseFromParent(); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | return Changed; |
| 1016 | } |
| 1017 | |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 1018 | bool ReplaceOpenCLBuiltinPass::replaceUpsample(Module &M) { |
| 1019 | bool Changed = false; |
| 1020 | |
| 1021 | for (auto const &SymVal : M.getValueSymbolTable()) { |
| 1022 | // Skip symbols whose name doesn't match |
| 1023 | if (!SymVal.getKey().startswith("_Z8upsample")) { |
| 1024 | continue; |
| 1025 | } |
| 1026 | // Is there a function going by that name? |
| 1027 | if (auto F = dyn_cast<Function>(SymVal.getValue())) { |
| 1028 | |
| 1029 | SmallVector<Instruction *, 4> ToRemoves; |
| 1030 | |
| 1031 | // Walk the users of the function. |
| 1032 | for (auto &U : F->uses()) { |
| 1033 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1034 | |
| 1035 | // Get arguments |
| 1036 | auto HiValue = CI->getOperand(0); |
| 1037 | auto LoValue = CI->getOperand(1); |
| 1038 | |
| 1039 | // Don't touch overloads that aren't in OpenCL C |
| 1040 | auto HiType = HiValue->getType(); |
| 1041 | auto LoType = LoValue->getType(); |
| 1042 | |
| 1043 | if (HiType != LoType) { |
| 1044 | continue; |
| 1045 | } |
| 1046 | |
| 1047 | if (!HiType->isIntOrIntVectorTy()) { |
| 1048 | continue; |
| 1049 | } |
| 1050 | |
| 1051 | if (HiType->getScalarSizeInBits() * 2 != |
| 1052 | CI->getType()->getScalarSizeInBits()) { |
| 1053 | continue; |
| 1054 | } |
| 1055 | |
| 1056 | if ((HiType->getScalarSizeInBits() != 8) && |
| 1057 | (HiType->getScalarSizeInBits() != 16) && |
| 1058 | (HiType->getScalarSizeInBits() != 32)) { |
| 1059 | continue; |
| 1060 | } |
| 1061 | |
| 1062 | if (HiType->isVectorTy()) { |
| 1063 | if ((HiType->getVectorNumElements() != 2) && |
| 1064 | (HiType->getVectorNumElements() != 3) && |
| 1065 | (HiType->getVectorNumElements() != 4) && |
| 1066 | (HiType->getVectorNumElements() != 8) && |
| 1067 | (HiType->getVectorNumElements() != 16)) { |
| 1068 | continue; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | // Convert both operands to the result type |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1073 | auto HiCast = |
| 1074 | CastInst::CreateZExtOrBitCast(HiValue, CI->getType(), "", CI); |
| 1075 | auto LoCast = |
| 1076 | CastInst::CreateZExtOrBitCast(LoValue, CI->getType(), "", CI); |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 1077 | |
| 1078 | // Shift high operand |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1079 | auto ShiftAmount = |
| 1080 | ConstantInt::get(CI->getType(), HiType->getScalarSizeInBits()); |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 1081 | auto HiShifted = BinaryOperator::Create(Instruction::Shl, HiCast, |
| 1082 | ShiftAmount, "", CI); |
| 1083 | |
| 1084 | // OR both results |
| 1085 | Value *V = BinaryOperator::Create(Instruction::Or, HiShifted, LoCast, |
| 1086 | "", CI); |
| 1087 | |
| 1088 | // Replace call with the expression |
| 1089 | CI->replaceAllUsesWith(V); |
| 1090 | |
| 1091 | // Lastly, remember to remove the user. |
| 1092 | ToRemoves.push_back(CI); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | Changed = !ToRemoves.empty(); |
| 1097 | |
| 1098 | // And cleanup the calls we don't use anymore. |
| 1099 | for (auto V : ToRemoves) { |
| 1100 | V->eraseFromParent(); |
| 1101 | } |
| 1102 | |
| 1103 | // And remove the function we don't need either too. |
| 1104 | F->eraseFromParent(); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | return Changed; |
| 1109 | } |
| 1110 | |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 1111 | bool ReplaceOpenCLBuiltinPass::replaceRotate(Module &M) { |
| 1112 | bool Changed = false; |
| 1113 | |
| 1114 | for (auto const &SymVal : M.getValueSymbolTable()) { |
| 1115 | // Skip symbols whose name doesn't match |
| 1116 | if (!SymVal.getKey().startswith("_Z6rotate")) { |
| 1117 | continue; |
| 1118 | } |
| 1119 | // Is there a function going by that name? |
| 1120 | if (auto F = dyn_cast<Function>(SymVal.getValue())) { |
| 1121 | |
| 1122 | SmallVector<Instruction *, 4> ToRemoves; |
| 1123 | |
| 1124 | // Walk the users of the function. |
| 1125 | for (auto &U : F->uses()) { |
| 1126 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1127 | |
| 1128 | // Get arguments |
| 1129 | auto SrcValue = CI->getOperand(0); |
| 1130 | auto RotAmount = CI->getOperand(1); |
| 1131 | |
| 1132 | // Don't touch overloads that aren't in OpenCL C |
| 1133 | auto SrcType = SrcValue->getType(); |
| 1134 | auto RotType = RotAmount->getType(); |
| 1135 | |
| 1136 | if ((SrcType != RotType) || (CI->getType() != SrcType)) { |
| 1137 | continue; |
| 1138 | } |
| 1139 | |
| 1140 | if (!SrcType->isIntOrIntVectorTy()) { |
| 1141 | continue; |
| 1142 | } |
| 1143 | |
| 1144 | if ((SrcType->getScalarSizeInBits() != 8) && |
| 1145 | (SrcType->getScalarSizeInBits() != 16) && |
| 1146 | (SrcType->getScalarSizeInBits() != 32) && |
| 1147 | (SrcType->getScalarSizeInBits() != 64)) { |
| 1148 | continue; |
| 1149 | } |
| 1150 | |
| 1151 | if (SrcType->isVectorTy()) { |
| 1152 | if ((SrcType->getVectorNumElements() != 2) && |
| 1153 | (SrcType->getVectorNumElements() != 3) && |
| 1154 | (SrcType->getVectorNumElements() != 4) && |
| 1155 | (SrcType->getVectorNumElements() != 8) && |
| 1156 | (SrcType->getVectorNumElements() != 16)) { |
| 1157 | continue; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | // The approach used is to shift the top bits down, the bottom bits up |
| 1162 | // and OR the two shifted values. |
| 1163 | |
| 1164 | // The rotation amount is to be treated modulo the element size. |
| 1165 | // Since SPIR-V shift ops don't support this, let's apply the |
| 1166 | // modulo ahead of shifting. The element size is always a power of |
| 1167 | // two so we can just AND with a mask. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1168 | auto ModMask = |
| 1169 | ConstantInt::get(SrcType, SrcType->getScalarSizeInBits() - 1); |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 1170 | RotAmount = BinaryOperator::Create(Instruction::And, RotAmount, |
| 1171 | ModMask, "", CI); |
| 1172 | |
| 1173 | // Let's calc the amount by which to shift top bits down |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1174 | auto ScalarSize = |
| 1175 | ConstantInt::get(SrcType, SrcType->getScalarSizeInBits()); |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 1176 | auto DownAmount = BinaryOperator::Create(Instruction::Sub, ScalarSize, |
| 1177 | RotAmount, "", CI); |
| 1178 | |
| 1179 | // Now shift the bottom bits up and the top bits down |
| 1180 | auto LoRotated = BinaryOperator::Create(Instruction::Shl, SrcValue, |
| 1181 | RotAmount, "", CI); |
| 1182 | auto HiRotated = BinaryOperator::Create(Instruction::LShr, SrcValue, |
| 1183 | DownAmount, "", CI); |
| 1184 | |
| 1185 | // Finally OR the two shifted values |
| 1186 | Value *V = BinaryOperator::Create(Instruction::Or, LoRotated, |
| 1187 | HiRotated, "", CI); |
| 1188 | |
| 1189 | // Replace call with the expression |
| 1190 | CI->replaceAllUsesWith(V); |
| 1191 | |
| 1192 | // Lastly, remember to remove the user. |
| 1193 | ToRemoves.push_back(CI); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | Changed = !ToRemoves.empty(); |
| 1198 | |
| 1199 | // And cleanup the calls we don't use anymore. |
| 1200 | for (auto V : ToRemoves) { |
| 1201 | V->eraseFromParent(); |
| 1202 | } |
| 1203 | |
| 1204 | // And remove the function we don't need either too. |
| 1205 | F->eraseFromParent(); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | return Changed; |
| 1210 | } |
| 1211 | |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1212 | bool ReplaceOpenCLBuiltinPass::replaceConvert(Module &M) { |
| 1213 | bool Changed = false; |
| 1214 | |
| 1215 | for (auto const &SymVal : M.getValueSymbolTable()) { |
| 1216 | |
| 1217 | // Skip symbols whose name obviously doesn't match |
| 1218 | if (!SymVal.getKey().contains("convert_")) { |
| 1219 | continue; |
| 1220 | } |
| 1221 | |
| 1222 | // Is there a function going by that name? |
| 1223 | if (auto F = dyn_cast<Function>(SymVal.getValue())) { |
| 1224 | |
| 1225 | // Get info from the mangled name |
| 1226 | FunctionInfo finfo; |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 1227 | bool parsed = FunctionInfo::getFromMangledNameCheck(F->getName(), &finfo); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1228 | |
| 1229 | // All functions of interest are handled by our mangled name parser |
| 1230 | if (!parsed) { |
| 1231 | continue; |
| 1232 | } |
| 1233 | |
| 1234 | // Move on if this isn't a call to convert_ |
| 1235 | if (!finfo.name.startswith("convert_")) { |
| 1236 | continue; |
| 1237 | } |
| 1238 | |
| 1239 | // Extract the destination type from the function name |
| 1240 | StringRef DstTypeName = finfo.name; |
| 1241 | DstTypeName.consume_front("convert_"); |
| 1242 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1243 | auto DstSignedNess = |
| 1244 | StringSwitch<ArgTypeInfo::SignedNess>(DstTypeName) |
| 1245 | .StartsWith("char", ArgTypeInfo::SignedNess::Signed) |
| 1246 | .StartsWith("short", ArgTypeInfo::SignedNess::Signed) |
| 1247 | .StartsWith("int", ArgTypeInfo::SignedNess::Signed) |
| 1248 | .StartsWith("long", ArgTypeInfo::SignedNess::Signed) |
| 1249 | .StartsWith("uchar", ArgTypeInfo::SignedNess::Unsigned) |
| 1250 | .StartsWith("ushort", ArgTypeInfo::SignedNess::Unsigned) |
| 1251 | .StartsWith("uint", ArgTypeInfo::SignedNess::Unsigned) |
| 1252 | .StartsWith("ulong", ArgTypeInfo::SignedNess::Unsigned) |
| 1253 | .Default(ArgTypeInfo::SignedNess::None); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1254 | |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1255 | bool DstIsSigned = DstSignedNess == ArgTypeInfo::SignedNess::Signed; |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 1256 | bool SrcIsSigned = finfo.isArgSigned(0); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1257 | |
| 1258 | SmallVector<Instruction *, 4> ToRemoves; |
| 1259 | |
| 1260 | // Walk the users of the function. |
| 1261 | for (auto &U : F->uses()) { |
| 1262 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1263 | |
| 1264 | // Get arguments |
| 1265 | auto SrcValue = CI->getOperand(0); |
| 1266 | |
| 1267 | // Don't touch overloads that aren't in OpenCL C |
| 1268 | auto SrcType = SrcValue->getType(); |
| 1269 | auto DstType = CI->getType(); |
| 1270 | |
| 1271 | if ((SrcType->isVectorTy() && !DstType->isVectorTy()) || |
| 1272 | (!SrcType->isVectorTy() && DstType->isVectorTy())) { |
| 1273 | continue; |
| 1274 | } |
| 1275 | |
| 1276 | if (SrcType->isVectorTy()) { |
| 1277 | |
| 1278 | if (SrcType->getVectorNumElements() != |
| 1279 | DstType->getVectorNumElements()) { |
| 1280 | continue; |
| 1281 | } |
| 1282 | |
| 1283 | if ((SrcType->getVectorNumElements() != 2) && |
| 1284 | (SrcType->getVectorNumElements() != 3) && |
| 1285 | (SrcType->getVectorNumElements() != 4) && |
| 1286 | (SrcType->getVectorNumElements() != 8) && |
| 1287 | (SrcType->getVectorNumElements() != 16)) { |
| 1288 | continue; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | bool SrcIsFloat = SrcType->getScalarType()->isFloatingPointTy(); |
| 1293 | bool DstIsFloat = DstType->getScalarType()->isFloatingPointTy(); |
| 1294 | |
| 1295 | bool SrcIsInt = SrcType->isIntOrIntVectorTy(); |
| 1296 | bool DstIsInt = DstType->isIntOrIntVectorTy(); |
| 1297 | |
| 1298 | Value *V; |
| 1299 | if (SrcIsFloat && DstIsFloat) { |
| 1300 | V = CastInst::CreateFPCast(SrcValue, DstType, "", CI); |
| 1301 | } else if (SrcIsFloat && DstIsInt) { |
| 1302 | if (DstIsSigned) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1303 | V = CastInst::Create(Instruction::FPToSI, SrcValue, DstType, "", |
| 1304 | CI); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1305 | } else { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1306 | V = CastInst::Create(Instruction::FPToUI, SrcValue, DstType, "", |
| 1307 | CI); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1308 | } |
| 1309 | } else if (SrcIsInt && DstIsFloat) { |
| 1310 | if (SrcIsSigned) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1311 | V = CastInst::Create(Instruction::SIToFP, SrcValue, DstType, "", |
| 1312 | CI); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1313 | } else { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1314 | V = CastInst::Create(Instruction::UIToFP, SrcValue, DstType, "", |
| 1315 | CI); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1316 | } |
| 1317 | } else if (SrcIsInt && DstIsInt) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1318 | V = CastInst::CreateIntegerCast(SrcValue, DstType, SrcIsSigned, "", |
| 1319 | CI); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1320 | } else { |
| 1321 | // Not something we're supposed to handle, just move on |
| 1322 | continue; |
| 1323 | } |
| 1324 | |
| 1325 | // Replace call with the expression |
| 1326 | CI->replaceAllUsesWith(V); |
| 1327 | |
| 1328 | // Lastly, remember to remove the user. |
| 1329 | ToRemoves.push_back(CI); |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | Changed = !ToRemoves.empty(); |
| 1334 | |
| 1335 | // And cleanup the calls we don't use anymore. |
| 1336 | for (auto V : ToRemoves) { |
| 1337 | V->eraseFromParent(); |
| 1338 | } |
| 1339 | |
| 1340 | // And remove the function we don't need either too. |
| 1341 | F->eraseFromParent(); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | return Changed; |
| 1346 | } |
| 1347 | |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1348 | bool ReplaceOpenCLBuiltinPass::replaceMulHiMadHi(Module &M) { |
| 1349 | bool Changed = false; |
| 1350 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1351 | SmallVector<Function *, 4> FnWorklist; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1352 | |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1353 | for (auto const &SymVal : M.getValueSymbolTable()) { |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1354 | bool isMad = SymVal.getKey().startswith("_Z6mad_hi"); |
| 1355 | bool isMul = SymVal.getKey().startswith("_Z6mul_hi"); |
| 1356 | |
| 1357 | // Skip symbols whose name doesn't match |
| 1358 | if (!isMad && !isMul) { |
| 1359 | continue; |
| 1360 | } |
| 1361 | |
| 1362 | // Is there a function going by that name? |
| 1363 | if (auto F = dyn_cast<Function>(SymVal.getValue())) { |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1364 | FnWorklist.push_back(F); |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1368 | for (auto F : FnWorklist) { |
| 1369 | SmallVector<Instruction *, 4> ToRemoves; |
| 1370 | |
| 1371 | bool isMad = F->getName().startswith("_Z6mad_hi"); |
| 1372 | // Walk the users of the function. |
| 1373 | for (auto &U : F->uses()) { |
| 1374 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1375 | |
| 1376 | // Get arguments |
| 1377 | auto AValue = CI->getOperand(0); |
| 1378 | auto BValue = CI->getOperand(1); |
| 1379 | auto CValue = CI->getOperand(2); |
| 1380 | |
| 1381 | // Don't touch overloads that aren't in OpenCL C |
| 1382 | auto AType = AValue->getType(); |
| 1383 | auto BType = BValue->getType(); |
| 1384 | auto CType = CValue->getType(); |
| 1385 | |
| 1386 | if ((AType != BType) || (CI->getType() != AType) || |
| 1387 | (isMad && (AType != CType))) { |
| 1388 | continue; |
| 1389 | } |
| 1390 | |
| 1391 | if (!AType->isIntOrIntVectorTy()) { |
| 1392 | continue; |
| 1393 | } |
| 1394 | |
| 1395 | if ((AType->getScalarSizeInBits() != 8) && |
| 1396 | (AType->getScalarSizeInBits() != 16) && |
| 1397 | (AType->getScalarSizeInBits() != 32) && |
| 1398 | (AType->getScalarSizeInBits() != 64)) { |
| 1399 | continue; |
| 1400 | } |
| 1401 | |
| 1402 | if (AType->isVectorTy()) { |
| 1403 | if ((AType->getVectorNumElements() != 2) && |
| 1404 | (AType->getVectorNumElements() != 3) && |
| 1405 | (AType->getVectorNumElements() != 4) && |
| 1406 | (AType->getVectorNumElements() != 8) && |
| 1407 | (AType->getVectorNumElements() != 16)) { |
| 1408 | continue; |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | // Get infos from the mangled OpenCL built-in function name |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 1413 | auto finfo = FunctionInfo::getFromMangledName(F->getName()); |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1414 | |
| 1415 | // Select the appropriate signed/unsigned SPIR-V op |
| 1416 | spv::Op opcode; |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 1417 | if (finfo.isArgSigned(0)) { |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1418 | opcode = spv::OpSMulExtended; |
| 1419 | } else { |
| 1420 | opcode = spv::OpUMulExtended; |
| 1421 | } |
| 1422 | |
| 1423 | // Our SPIR-V op returns a struct, create a type for it |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1424 | SmallVector<Type *, 2> TwoValueType = {AType, AType}; |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1425 | auto ExMulRetType = StructType::create(TwoValueType); |
| 1426 | |
| 1427 | // Call the SPIR-V op |
| 1428 | auto Call = clspv::InsertSPIRVOp(CI, opcode, {Attribute::ReadNone}, |
| 1429 | ExMulRetType, {AValue, BValue}); |
| 1430 | |
| 1431 | // Get the high part of the result |
| 1432 | unsigned Idxs[] = {1}; |
| 1433 | Value *V = ExtractValueInst::Create(Call, Idxs, "", CI); |
| 1434 | |
| 1435 | // If we're handling a mad_hi, add the third argument to the result |
| 1436 | if (isMad) { |
| 1437 | V = BinaryOperator::Create(Instruction::Add, V, CValue, "", CI); |
| 1438 | } |
| 1439 | |
| 1440 | // Replace call with the expression |
| 1441 | CI->replaceAllUsesWith(V); |
| 1442 | |
| 1443 | // Lastly, remember to remove the user. |
| 1444 | ToRemoves.push_back(CI); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | Changed = !ToRemoves.empty(); |
| 1449 | |
| 1450 | // And cleanup the calls we don't use anymore. |
| 1451 | for (auto V : ToRemoves) { |
| 1452 | V->eraseFromParent(); |
| 1453 | } |
| 1454 | |
| 1455 | // And remove the function we don't need either too. |
| 1456 | F->eraseFromParent(); |
| 1457 | } |
| 1458 | |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1459 | return Changed; |
| 1460 | } |
| 1461 | |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1462 | bool ReplaceOpenCLBuiltinPass::replaceSelect(Module &M) { |
| 1463 | bool Changed = false; |
| 1464 | |
| 1465 | for (auto const &SymVal : M.getValueSymbolTable()) { |
| 1466 | // Skip symbols whose name doesn't match |
| 1467 | if (!SymVal.getKey().startswith("_Z6select")) { |
| 1468 | continue; |
| 1469 | } |
| 1470 | // Is there a function going by that name? |
| 1471 | if (auto F = dyn_cast<Function>(SymVal.getValue())) { |
| 1472 | |
| 1473 | SmallVector<Instruction *, 4> ToRemoves; |
| 1474 | |
| 1475 | // Walk the users of the function. |
| 1476 | for (auto &U : F->uses()) { |
| 1477 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1478 | |
| 1479 | // Get arguments |
| 1480 | auto FalseValue = CI->getOperand(0); |
| 1481 | auto TrueValue = CI->getOperand(1); |
| 1482 | auto PredicateValue = CI->getOperand(2); |
| 1483 | |
| 1484 | // Don't touch overloads that aren't in OpenCL C |
| 1485 | auto FalseType = FalseValue->getType(); |
| 1486 | auto TrueType = TrueValue->getType(); |
| 1487 | auto PredicateType = PredicateValue->getType(); |
| 1488 | |
| 1489 | if (FalseType != TrueType) { |
| 1490 | continue; |
| 1491 | } |
| 1492 | |
| 1493 | if (!PredicateType->isIntOrIntVectorTy()) { |
| 1494 | continue; |
| 1495 | } |
| 1496 | |
| 1497 | if (!FalseType->isIntOrIntVectorTy() && |
| 1498 | !FalseType->getScalarType()->isFloatingPointTy()) { |
| 1499 | continue; |
| 1500 | } |
| 1501 | |
| 1502 | if (FalseType->isVectorTy() && !PredicateType->isVectorTy()) { |
| 1503 | continue; |
| 1504 | } |
| 1505 | |
| 1506 | if (FalseType->getScalarSizeInBits() != |
| 1507 | PredicateType->getScalarSizeInBits()) { |
| 1508 | continue; |
| 1509 | } |
| 1510 | |
| 1511 | if (FalseType->isVectorTy()) { |
| 1512 | if (FalseType->getVectorNumElements() != |
| 1513 | PredicateType->getVectorNumElements()) { |
| 1514 | continue; |
| 1515 | } |
| 1516 | |
| 1517 | if ((FalseType->getVectorNumElements() != 2) && |
| 1518 | (FalseType->getVectorNumElements() != 3) && |
| 1519 | (FalseType->getVectorNumElements() != 4) && |
| 1520 | (FalseType->getVectorNumElements() != 8) && |
| 1521 | (FalseType->getVectorNumElements() != 16)) { |
| 1522 | continue; |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | // Create constant |
| 1527 | const auto ZeroValue = Constant::getNullValue(PredicateType); |
| 1528 | |
| 1529 | // Scalar and vector are to be treated differently |
| 1530 | CmpInst::Predicate Pred; |
| 1531 | if (PredicateType->isVectorTy()) { |
| 1532 | Pred = CmpInst::ICMP_SLT; |
| 1533 | } else { |
| 1534 | Pred = CmpInst::ICMP_NE; |
| 1535 | } |
| 1536 | |
| 1537 | // Create comparison instruction |
| 1538 | auto Cmp = CmpInst::Create(Instruction::ICmp, Pred, PredicateValue, |
| 1539 | ZeroValue, "", CI); |
| 1540 | |
| 1541 | // Create select |
| 1542 | Value *V = SelectInst::Create(Cmp, TrueValue, FalseValue, "", CI); |
| 1543 | |
| 1544 | // Replace call with the selection |
| 1545 | CI->replaceAllUsesWith(V); |
| 1546 | |
| 1547 | // Lastly, remember to remove the user. |
| 1548 | ToRemoves.push_back(CI); |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | Changed = !ToRemoves.empty(); |
| 1553 | |
| 1554 | // And cleanup the calls we don't use anymore. |
| 1555 | for (auto V : ToRemoves) { |
| 1556 | V->eraseFromParent(); |
| 1557 | } |
| 1558 | |
| 1559 | // And remove the function we don't need either too. |
| 1560 | F->eraseFromParent(); |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | return Changed; |
| 1565 | } |
| 1566 | |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1567 | bool ReplaceOpenCLBuiltinPass::replaceBitSelect(Module &M) { |
| 1568 | bool Changed = false; |
| 1569 | |
| 1570 | for (auto const &SymVal : M.getValueSymbolTable()) { |
| 1571 | // Skip symbols whose name doesn't match |
| 1572 | if (!SymVal.getKey().startswith("_Z9bitselect")) { |
| 1573 | continue; |
| 1574 | } |
| 1575 | // Is there a function going by that name? |
| 1576 | if (auto F = dyn_cast<Function>(SymVal.getValue())) { |
| 1577 | |
| 1578 | SmallVector<Instruction *, 4> ToRemoves; |
| 1579 | |
| 1580 | // Walk the users of the function. |
| 1581 | for (auto &U : F->uses()) { |
| 1582 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1583 | |
| 1584 | if (CI->getNumOperands() != 4) { |
| 1585 | continue; |
| 1586 | } |
| 1587 | |
| 1588 | // Get arguments |
| 1589 | auto FalseValue = CI->getOperand(0); |
| 1590 | auto TrueValue = CI->getOperand(1); |
| 1591 | auto PredicateValue = CI->getOperand(2); |
| 1592 | |
| 1593 | // Don't touch overloads that aren't in OpenCL C |
| 1594 | auto FalseType = FalseValue->getType(); |
| 1595 | auto TrueType = TrueValue->getType(); |
| 1596 | auto PredicateType = PredicateValue->getType(); |
| 1597 | |
| 1598 | if ((FalseType != TrueType) || (PredicateType != TrueType)) { |
| 1599 | continue; |
| 1600 | } |
| 1601 | |
| 1602 | if (TrueType->isVectorTy()) { |
| 1603 | if (!TrueType->getScalarType()->isFloatingPointTy() && |
| 1604 | !TrueType->getScalarType()->isIntegerTy()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1605 | continue; |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1606 | } |
| 1607 | if ((TrueType->getVectorNumElements() != 2) && |
| 1608 | (TrueType->getVectorNumElements() != 3) && |
| 1609 | (TrueType->getVectorNumElements() != 4) && |
| 1610 | (TrueType->getVectorNumElements() != 8) && |
| 1611 | (TrueType->getVectorNumElements() != 16)) { |
| 1612 | continue; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | // Remember the type of the operands |
| 1617 | auto OpType = TrueType; |
| 1618 | |
| 1619 | // The actual bit selection will always be done on an integer type, |
| 1620 | // declare it here |
| 1621 | Type *BitType; |
| 1622 | |
| 1623 | // If the operands are float, then bitcast them to int |
| 1624 | if (OpType->getScalarType()->isFloatingPointTy()) { |
| 1625 | |
| 1626 | // First create the new type |
| 1627 | auto ScalarSize = OpType->getScalarType()->getPrimitiveSizeInBits(); |
| 1628 | BitType = Type::getIntNTy(M.getContext(), ScalarSize); |
| 1629 | if (OpType->isVectorTy()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1630 | BitType = |
| 1631 | VectorType::get(BitType, OpType->getVectorNumElements()); |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | // Then bitcast all operands |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1635 | PredicateValue = |
| 1636 | CastInst::CreateZExtOrBitCast(PredicateValue, BitType, "", CI); |
| 1637 | FalseValue = |
| 1638 | CastInst::CreateZExtOrBitCast(FalseValue, BitType, "", CI); |
| 1639 | TrueValue = |
| 1640 | CastInst::CreateZExtOrBitCast(TrueValue, BitType, "", CI); |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1641 | |
| 1642 | } else { |
| 1643 | // The operands have an integer type, use it directly |
| 1644 | BitType = OpType; |
| 1645 | } |
| 1646 | |
| 1647 | // All the operands are now always integers |
| 1648 | // implement as (c & b) | (~c & a) |
| 1649 | |
| 1650 | // Create our negated predicate value |
| 1651 | auto AllOnes = Constant::getAllOnesValue(BitType); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1652 | auto NotPredicateValue = BinaryOperator::Create( |
| 1653 | Instruction::Xor, PredicateValue, AllOnes, "", CI); |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1654 | |
| 1655 | // Then put everything together |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1656 | auto BitsFalse = BinaryOperator::Create( |
| 1657 | Instruction::And, NotPredicateValue, FalseValue, "", CI); |
| 1658 | auto BitsTrue = BinaryOperator::Create( |
| 1659 | Instruction::And, PredicateValue, TrueValue, "", CI); |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1660 | |
| 1661 | Value *V = BinaryOperator::Create(Instruction::Or, BitsFalse, |
| 1662 | BitsTrue, "", CI); |
| 1663 | |
| 1664 | // If we were dealing with a floating point type, we must bitcast |
| 1665 | // the result back to that |
| 1666 | if (OpType->getScalarType()->isFloatingPointTy()) { |
| 1667 | V = CastInst::CreateZExtOrBitCast(V, OpType, "", CI); |
| 1668 | } |
| 1669 | |
| 1670 | // Replace call with our new code |
| 1671 | CI->replaceAllUsesWith(V); |
| 1672 | |
| 1673 | // Lastly, remember to remove the user. |
| 1674 | ToRemoves.push_back(CI); |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | Changed = !ToRemoves.empty(); |
| 1679 | |
| 1680 | // And cleanup the calls we don't use anymore. |
| 1681 | for (auto V : ToRemoves) { |
| 1682 | V->eraseFromParent(); |
| 1683 | } |
| 1684 | |
| 1685 | // And remove the function we don't need either too. |
| 1686 | F->eraseFromParent(); |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | return Changed; |
| 1691 | } |
| 1692 | |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1693 | bool ReplaceOpenCLBuiltinPass::replaceStepSmoothStep(Module &M) { |
| 1694 | bool Changed = false; |
| 1695 | |
| 1696 | const std::map<const char *, const char *> Map = { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1697 | {"_Z4stepfDv2_f", "_Z4stepDv2_fS_"}, |
| 1698 | {"_Z4stepfDv3_f", "_Z4stepDv3_fS_"}, |
| 1699 | {"_Z4stepfDv4_f", "_Z4stepDv4_fS_"}, |
| 1700 | {"_Z10smoothstepffDv2_f", "_Z10smoothstepDv2_fS_S_"}, |
| 1701 | {"_Z10smoothstepffDv3_f", "_Z10smoothstepDv3_fS_S_"}, |
| 1702 | {"_Z10smoothstepffDv4_f", "_Z10smoothstepDv4_fS_S_"}, |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1703 | }; |
| 1704 | |
| 1705 | for (auto Pair : Map) { |
| 1706 | // If we find a function with the matching name. |
| 1707 | if (auto F = M.getFunction(Pair.first)) { |
| 1708 | SmallVector<Instruction *, 4> ToRemoves; |
| 1709 | |
| 1710 | // Walk the users of the function. |
| 1711 | for (auto &U : F->uses()) { |
| 1712 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1713 | |
| 1714 | auto ReplacementFn = Pair.second; |
| 1715 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1716 | SmallVector<Value *, 2> ArgsToSplat = {CI->getOperand(0)}; |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1717 | Value *VectorArg; |
| 1718 | |
| 1719 | // First figure out which function we're dealing with |
| 1720 | if (F->getName().startswith("_Z10smoothstep")) { |
| 1721 | ArgsToSplat.push_back(CI->getOperand(1)); |
| 1722 | VectorArg = CI->getOperand(2); |
| 1723 | } else { |
| 1724 | VectorArg = CI->getOperand(1); |
| 1725 | } |
| 1726 | |
| 1727 | // Splat arguments that need to be |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1728 | SmallVector<Value *, 2> SplatArgs; |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1729 | auto VecType = VectorArg->getType(); |
| 1730 | |
| 1731 | for (auto arg : ArgsToSplat) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1732 | Value *NewVectorArg = UndefValue::get(VecType); |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1733 | for (auto i = 0; i < VecType->getVectorNumElements(); i++) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1734 | auto index = |
| 1735 | ConstantInt::get(Type::getInt32Ty(M.getContext()), i); |
| 1736 | NewVectorArg = |
| 1737 | InsertElementInst::Create(NewVectorArg, arg, index, "", CI); |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1738 | } |
| 1739 | SplatArgs.push_back(NewVectorArg); |
| 1740 | } |
| 1741 | |
| 1742 | // Replace the call with the vector/vector flavour |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1743 | SmallVector<Type *, 3> NewArgTypes(ArgsToSplat.size() + 1, VecType); |
| 1744 | const auto NewFType = |
| 1745 | FunctionType::get(CI->getType(), NewArgTypes, false); |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1746 | |
| 1747 | const auto NewF = M.getOrInsertFunction(ReplacementFn, NewFType); |
| 1748 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1749 | SmallVector<Value *, 3> NewArgs; |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1750 | for (auto arg : SplatArgs) { |
| 1751 | NewArgs.push_back(arg); |
| 1752 | } |
| 1753 | NewArgs.push_back(VectorArg); |
| 1754 | |
| 1755 | const auto NewCI = CallInst::Create(NewF, NewArgs, "", CI); |
| 1756 | |
| 1757 | CI->replaceAllUsesWith(NewCI); |
| 1758 | |
| 1759 | // Lastly, remember to remove the user. |
| 1760 | ToRemoves.push_back(CI); |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | Changed = !ToRemoves.empty(); |
| 1765 | |
| 1766 | // And cleanup the calls we don't use anymore. |
| 1767 | for (auto V : ToRemoves) { |
| 1768 | V->eraseFromParent(); |
| 1769 | } |
| 1770 | |
| 1771 | // And remove the function we don't need either too. |
| 1772 | F->eraseFromParent(); |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | return Changed; |
| 1777 | } |
| 1778 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1779 | bool ReplaceOpenCLBuiltinPass::replaceSignbit(Module &M) { |
| 1780 | bool Changed = false; |
| 1781 | |
| 1782 | const std::map<const char *, Instruction::BinaryOps> Map = { |
| 1783 | {"_Z7signbitf", Instruction::LShr}, |
| 1784 | {"_Z7signbitDv2_f", Instruction::AShr}, |
| 1785 | {"_Z7signbitDv3_f", Instruction::AShr}, |
| 1786 | {"_Z7signbitDv4_f", Instruction::AShr}, |
| 1787 | }; |
| 1788 | |
| 1789 | for (auto Pair : Map) { |
| 1790 | // If we find a function with the matching name. |
| 1791 | if (auto F = M.getFunction(Pair.first)) { |
| 1792 | SmallVector<Instruction *, 4> ToRemoves; |
| 1793 | |
| 1794 | // Walk the users of the function. |
| 1795 | for (auto &U : F->uses()) { |
| 1796 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1797 | auto Arg = CI->getOperand(0); |
| 1798 | |
| 1799 | auto Bitcast = |
| 1800 | CastInst::CreateZExtOrBitCast(Arg, CI->getType(), "", CI); |
| 1801 | |
| 1802 | auto Shr = BinaryOperator::Create(Pair.second, Bitcast, |
| 1803 | ConstantInt::get(CI->getType(), 31), |
| 1804 | "", CI); |
| 1805 | |
| 1806 | CI->replaceAllUsesWith(Shr); |
| 1807 | |
| 1808 | // Lastly, remember to remove the user. |
| 1809 | ToRemoves.push_back(CI); |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | Changed = !ToRemoves.empty(); |
| 1814 | |
| 1815 | // And cleanup the calls we don't use anymore. |
| 1816 | for (auto V : ToRemoves) { |
| 1817 | V->eraseFromParent(); |
| 1818 | } |
| 1819 | |
| 1820 | // And remove the function we don't need either too. |
| 1821 | F->eraseFromParent(); |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | return Changed; |
| 1826 | } |
| 1827 | |
| 1828 | bool ReplaceOpenCLBuiltinPass::replaceMadandMad24andMul24(Module &M) { |
| 1829 | bool Changed = false; |
| 1830 | |
| 1831 | const std::map<const char *, |
| 1832 | std::pair<Instruction::BinaryOps, Instruction::BinaryOps>> |
| 1833 | Map = { |
| 1834 | {"_Z3madfff", {Instruction::FMul, Instruction::FAdd}}, |
| 1835 | {"_Z3madDv2_fS_S_", {Instruction::FMul, Instruction::FAdd}}, |
| 1836 | {"_Z3madDv3_fS_S_", {Instruction::FMul, Instruction::FAdd}}, |
| 1837 | {"_Z3madDv4_fS_S_", {Instruction::FMul, Instruction::FAdd}}, |
| 1838 | {"_Z5mad24iii", {Instruction::Mul, Instruction::Add}}, |
| 1839 | {"_Z5mad24Dv2_iS_S_", {Instruction::Mul, Instruction::Add}}, |
| 1840 | {"_Z5mad24Dv3_iS_S_", {Instruction::Mul, Instruction::Add}}, |
| 1841 | {"_Z5mad24Dv4_iS_S_", {Instruction::Mul, Instruction::Add}}, |
| 1842 | {"_Z5mad24jjj", {Instruction::Mul, Instruction::Add}}, |
| 1843 | {"_Z5mad24Dv2_jS_S_", {Instruction::Mul, Instruction::Add}}, |
| 1844 | {"_Z5mad24Dv3_jS_S_", {Instruction::Mul, Instruction::Add}}, |
| 1845 | {"_Z5mad24Dv4_jS_S_", {Instruction::Mul, Instruction::Add}}, |
| 1846 | {"_Z5mul24ii", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 1847 | {"_Z5mul24Dv2_iS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 1848 | {"_Z5mul24Dv3_iS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 1849 | {"_Z5mul24Dv4_iS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 1850 | {"_Z5mul24jj", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 1851 | {"_Z5mul24Dv2_jS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 1852 | {"_Z5mul24Dv3_jS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 1853 | {"_Z5mul24Dv4_jS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 1854 | }; |
| 1855 | |
| 1856 | for (auto Pair : Map) { |
| 1857 | // If we find a function with the matching name. |
| 1858 | if (auto F = M.getFunction(Pair.first)) { |
| 1859 | SmallVector<Instruction *, 4> ToRemoves; |
| 1860 | |
| 1861 | // Walk the users of the function. |
| 1862 | for (auto &U : F->uses()) { |
| 1863 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1864 | // The multiply instruction to use. |
| 1865 | auto MulInst = Pair.second.first; |
| 1866 | |
| 1867 | // The add instruction to use. |
| 1868 | auto AddInst = Pair.second.second; |
| 1869 | |
| 1870 | SmallVector<Value *, 8> Args(CI->arg_begin(), CI->arg_end()); |
| 1871 | |
| 1872 | auto I = BinaryOperator::Create(MulInst, CI->getArgOperand(0), |
| 1873 | CI->getArgOperand(1), "", CI); |
| 1874 | |
| 1875 | if (Instruction::BinaryOpsEnd != AddInst) { |
| 1876 | I = BinaryOperator::Create(AddInst, I, CI->getArgOperand(2), "", |
| 1877 | CI); |
| 1878 | } |
| 1879 | |
| 1880 | CI->replaceAllUsesWith(I); |
| 1881 | |
| 1882 | // Lastly, remember to remove the user. |
| 1883 | ToRemoves.push_back(CI); |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | Changed = !ToRemoves.empty(); |
| 1888 | |
| 1889 | // And cleanup the calls we don't use anymore. |
| 1890 | for (auto V : ToRemoves) { |
| 1891 | V->eraseFromParent(); |
| 1892 | } |
| 1893 | |
| 1894 | // And remove the function we don't need either too. |
| 1895 | F->eraseFromParent(); |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | return Changed; |
| 1900 | } |
| 1901 | |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1902 | bool ReplaceOpenCLBuiltinPass::replaceVstore(Module &M) { |
| 1903 | bool Changed = false; |
| 1904 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1905 | for (auto const &SymVal : M.getValueSymbolTable()) { |
| 1906 | if (!SymVal.getKey().contains("vstore")) |
| 1907 | continue; |
| 1908 | if (SymVal.getKey().contains("vstore_")) |
| 1909 | continue; |
| 1910 | if (SymVal.getKey().contains("vstorea")) |
| 1911 | continue; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1912 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1913 | if (auto F = dyn_cast<Function>(SymVal.getValue())) { |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1914 | SmallVector<Instruction *, 4> ToRemoves; |
| 1915 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1916 | auto fname = F->getName(); |
| 1917 | if (!fname.consume_front("_Z")) |
| 1918 | continue; |
| 1919 | size_t name_len; |
| 1920 | if (fname.consumeInteger(10, name_len)) |
| 1921 | continue; |
| 1922 | std::string name = fname.take_front(name_len); |
| 1923 | |
| 1924 | bool ok = StringSwitch<bool>(name) |
| 1925 | .Case("vstore2", true) |
| 1926 | .Case("vstore3", true) |
| 1927 | .Case("vstore4", true) |
| 1928 | .Case("vstore8", true) |
| 1929 | .Case("vstore16", true) |
| 1930 | .Default(false); |
| 1931 | if (!ok) |
| 1932 | continue; |
| 1933 | |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1934 | for (auto &U : F->uses()) { |
| 1935 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1936 | auto data = CI->getOperand(0); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1937 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1938 | auto data_type = data->getType(); |
| 1939 | if (!data_type->isVectorTy()) |
| 1940 | continue; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1941 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1942 | auto elems = data_type->getVectorNumElements(); |
| 1943 | if (elems != 2 && elems != 3 && elems != 4 && elems != 8 && |
| 1944 | elems != 16) |
| 1945 | continue; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1946 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1947 | auto offset = CI->getOperand(1); |
| 1948 | auto ptr = CI->getOperand(2); |
| 1949 | auto ptr_type = ptr->getType(); |
| 1950 | auto pointee_type = ptr_type->getPointerElementType(); |
| 1951 | if (pointee_type != data_type->getVectorElementType()) |
| 1952 | continue; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1953 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1954 | // Avoid pointer casts. Instead generate the correct number of stores |
| 1955 | // and rely on drivers to coalesce appropriately. |
| 1956 | IRBuilder<> builder(CI); |
| 1957 | auto elems_const = builder.getInt32(elems); |
| 1958 | auto adjust = builder.CreateMul(offset, elems_const); |
| 1959 | for (auto i = 0; i < elems; ++i) { |
| 1960 | auto idx = builder.getInt32(i); |
| 1961 | auto add = builder.CreateAdd(adjust, idx); |
| 1962 | auto gep = builder.CreateGEP(ptr, add); |
| 1963 | auto extract = builder.CreateExtractElement(data, i); |
| 1964 | auto store = builder.CreateStore(extract, gep); |
| 1965 | } |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1966 | |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1967 | ToRemoves.push_back(CI); |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | Changed = !ToRemoves.empty(); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1972 | for (auto V : ToRemoves) { |
| 1973 | V->eraseFromParent(); |
| 1974 | } |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1975 | F->eraseFromParent(); |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | return Changed; |
| 1980 | } |
| 1981 | |
| 1982 | bool ReplaceOpenCLBuiltinPass::replaceVload(Module &M) { |
| 1983 | bool Changed = false; |
| 1984 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1985 | for (auto const &SymVal : M.getValueSymbolTable()) { |
| 1986 | if (!SymVal.getKey().contains("vload")) |
| 1987 | continue; |
| 1988 | if (SymVal.getKey().contains("vload_")) |
| 1989 | continue; |
| 1990 | if (SymVal.getKey().contains("vloada")) |
| 1991 | continue; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1992 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1993 | if (auto F = dyn_cast<Function>(SymVal.getValue())) { |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1994 | SmallVector<Instruction *, 4> ToRemoves; |
| 1995 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 1996 | auto fname = F->getName(); |
| 1997 | if (!fname.consume_front("_Z")) |
| 1998 | continue; |
| 1999 | size_t name_len; |
| 2000 | if (fname.consumeInteger(10, name_len)) |
| 2001 | continue; |
| 2002 | std::string name = fname.take_front(name_len); |
| 2003 | |
| 2004 | bool ok = StringSwitch<bool>(name) |
| 2005 | .Case("vload2", true) |
| 2006 | .Case("vload3", true) |
| 2007 | .Case("vload4", true) |
| 2008 | .Case("vload8", true) |
| 2009 | .Case("vload16", true) |
| 2010 | .Default(false); |
| 2011 | if (!ok) |
| 2012 | continue; |
| 2013 | |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 2014 | for (auto &U : F->uses()) { |
| 2015 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 2016 | auto ret_type = F->getReturnType(); |
| 2017 | if (!ret_type->isVectorTy()) |
| 2018 | continue; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 2019 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 2020 | auto elems = ret_type->getVectorNumElements(); |
| 2021 | if (elems != 2 && elems != 3 && elems != 4 && elems != 8 && |
| 2022 | elems != 16) |
| 2023 | continue; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 2024 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 2025 | auto offset = CI->getOperand(0); |
| 2026 | auto ptr = CI->getOperand(1); |
| 2027 | auto ptr_type = ptr->getType(); |
| 2028 | auto pointee_type = ptr_type->getPointerElementType(); |
| 2029 | if (pointee_type != ret_type->getVectorElementType()) |
| 2030 | continue; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 2031 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 2032 | // Avoid pointer casts. Instead generate the correct number of loads |
| 2033 | // and rely on drivers to coalesce appropriately. |
| 2034 | IRBuilder<> builder(CI); |
| 2035 | auto elems_const = builder.getInt32(elems); |
| 2036 | Value *insert = UndefValue::get(ret_type); |
| 2037 | auto adjust = builder.CreateMul(offset, elems_const); |
| 2038 | for (auto i = 0; i < elems; ++i) { |
| 2039 | auto idx = builder.getInt32(i); |
| 2040 | auto add = builder.CreateAdd(adjust, idx); |
| 2041 | auto gep = builder.CreateGEP(ptr, add); |
| 2042 | auto load = builder.CreateLoad(gep); |
| 2043 | insert = builder.CreateInsertElement(insert, load, i); |
| 2044 | } |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 2045 | |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame^] | 2046 | CI->replaceAllUsesWith(insert); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 2047 | ToRemoves.push_back(CI); |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | Changed = !ToRemoves.empty(); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 2052 | for (auto V : ToRemoves) { |
| 2053 | V->eraseFromParent(); |
| 2054 | } |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 2055 | F->eraseFromParent(); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | return Changed; |
| 2060 | } |
| 2061 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2062 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf(Module &M) { |
| 2063 | bool Changed = false; |
| 2064 | |
| 2065 | const std::vector<const char *> Map = {"_Z10vload_halfjPU3AS1KDh", |
| 2066 | "_Z10vload_halfjPU3AS2KDh"}; |
| 2067 | |
| 2068 | for (auto Name : Map) { |
| 2069 | // If we find a function with the matching name. |
| 2070 | if (auto F = M.getFunction(Name)) { |
| 2071 | SmallVector<Instruction *, 4> ToRemoves; |
| 2072 | |
| 2073 | // Walk the users of the function. |
| 2074 | for (auto &U : F->uses()) { |
| 2075 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 2076 | // The index argument from vload_half. |
| 2077 | auto Arg0 = CI->getOperand(0); |
| 2078 | |
| 2079 | // The pointer argument from vload_half. |
| 2080 | auto Arg1 = CI->getOperand(1); |
| 2081 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2082 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2083 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2084 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
| 2085 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2086 | // Our intrinsic to unpack a float2 from an int. |
| 2087 | auto SPIRVIntrinsic = "spirv.unpack.v2f16"; |
| 2088 | |
| 2089 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 2090 | |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 2091 | if (clspv::Option::F16BitStorage()) { |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 2092 | auto ShortTy = Type::getInt16Ty(M.getContext()); |
| 2093 | auto ShortPointerTy = PointerType::get( |
| 2094 | ShortTy, Arg1->getType()->getPointerAddressSpace()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2095 | |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 2096 | // Cast the half* pointer to short*. |
| 2097 | auto Cast = |
| 2098 | CastInst::CreatePointerCast(Arg1, ShortPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2099 | |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 2100 | // Index into the correct address of the casted pointer. |
| 2101 | auto Index = GetElementPtrInst::Create(ShortTy, Cast, Arg0, "", CI); |
| 2102 | |
| 2103 | // Load from the short* we casted to. |
| 2104 | auto Load = new LoadInst(Index, "", CI); |
| 2105 | |
| 2106 | // ZExt the short -> int. |
| 2107 | auto ZExt = CastInst::CreateZExtOrBitCast(Load, IntTy, "", CI); |
| 2108 | |
| 2109 | // Get our float2. |
| 2110 | auto Call = CallInst::Create(NewF, ZExt, "", CI); |
| 2111 | |
| 2112 | // Extract out the bottom element which is our float result. |
| 2113 | auto Extract = ExtractElementInst::Create( |
| 2114 | Call, ConstantInt::get(IntTy, 0), "", CI); |
| 2115 | |
| 2116 | CI->replaceAllUsesWith(Extract); |
| 2117 | } else { |
| 2118 | // Assume the pointer argument points to storage aligned to 32bits |
| 2119 | // or more. |
| 2120 | // TODO(dneto): Do more analysis to make sure this is true? |
| 2121 | // |
| 2122 | // Replace call vstore_half(i32 %index, half addrspace(1) %base) |
| 2123 | // with: |
| 2124 | // |
| 2125 | // %base_i32_ptr = bitcast half addrspace(1)* %base to i32 |
| 2126 | // addrspace(1)* %index_is_odd32 = and i32 %index, 1 %index_i32 = |
| 2127 | // lshr i32 %index, 1 %in_ptr = getlementptr i32, i32 |
| 2128 | // addrspace(1)* %base_i32_ptr, %index_i32 %value_i32 = load i32, |
| 2129 | // i32 addrspace(1)* %in_ptr %converted = call <2 x float> |
| 2130 | // @spirv.unpack.v2f16(i32 %value_i32) %value = extractelement <2 |
| 2131 | // x float> %converted, %index_is_odd32 |
| 2132 | |
| 2133 | auto IntPointerTy = PointerType::get( |
| 2134 | IntTy, Arg1->getType()->getPointerAddressSpace()); |
| 2135 | |
David Neto | 973e6a8 | 2017-05-30 13:48:18 -0400 | [diff] [blame] | 2136 | // Cast the base pointer to int*. |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 2137 | // In a valid call (according to assumptions), this should get |
David Neto | 973e6a8 | 2017-05-30 13:48:18 -0400 | [diff] [blame] | 2138 | // optimized away in the simplify GEP pass. |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 2139 | auto Cast = CastInst::CreatePointerCast(Arg1, IntPointerTy, "", CI); |
| 2140 | |
| 2141 | auto One = ConstantInt::get(IntTy, 1); |
| 2142 | auto IndexIsOdd = BinaryOperator::CreateAnd(Arg0, One, "", CI); |
| 2143 | auto IndexIntoI32 = BinaryOperator::CreateLShr(Arg0, One, "", CI); |
| 2144 | |
| 2145 | // Index into the correct address of the casted pointer. |
| 2146 | auto Ptr = |
| 2147 | GetElementPtrInst::Create(IntTy, Cast, IndexIntoI32, "", CI); |
| 2148 | |
| 2149 | // Load from the int* we casted to. |
| 2150 | auto Load = new LoadInst(Ptr, "", CI); |
| 2151 | |
| 2152 | // Get our float2. |
| 2153 | auto Call = CallInst::Create(NewF, Load, "", CI); |
| 2154 | |
| 2155 | // Extract out the float result, where the element number is |
| 2156 | // determined by whether the original index was even or odd. |
| 2157 | auto Extract = ExtractElementInst::Create(Call, IndexIsOdd, "", CI); |
| 2158 | |
| 2159 | CI->replaceAllUsesWith(Extract); |
| 2160 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2161 | |
| 2162 | // Lastly, remember to remove the user. |
| 2163 | ToRemoves.push_back(CI); |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | Changed = !ToRemoves.empty(); |
| 2168 | |
| 2169 | // And cleanup the calls we don't use anymore. |
| 2170 | for (auto V : ToRemoves) { |
| 2171 | V->eraseFromParent(); |
| 2172 | } |
| 2173 | |
| 2174 | // And remove the function we don't need either too. |
| 2175 | F->eraseFromParent(); |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | return Changed; |
| 2180 | } |
| 2181 | |
| 2182 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf2(Module &M) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2183 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2184 | const std::vector<const char *> Names = { |
David Neto | 556c7e6 | 2018-06-08 13:45:55 -0700 | [diff] [blame] | 2185 | "_Z11vload_half2jPU3AS1KDh", |
| 2186 | "_Z12vloada_half2jPU3AS1KDh", // vloada_half2 global |
| 2187 | "_Z11vload_half2jPU3AS2KDh", |
| 2188 | "_Z12vloada_half2jPU3AS2KDh", // vloada_half2 constant |
| 2189 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2190 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2191 | return replaceCallsWithValue(M, Names, [&M](CallInst *CI) { |
| 2192 | // The index argument from vload_half. |
| 2193 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2194 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2195 | // The pointer argument from vload_half. |
| 2196 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2197 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2198 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2199 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2200 | auto NewPointerTy = |
| 2201 | PointerType::get(IntTy, Arg1->getType()->getPointerAddressSpace()); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2202 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2203 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2204 | // Cast the half* pointer to int*. |
| 2205 | auto Cast = CastInst::CreatePointerCast(Arg1, NewPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2206 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2207 | // Index into the correct address of the casted pointer. |
| 2208 | auto Index = GetElementPtrInst::Create(IntTy, Cast, Arg0, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2209 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2210 | // Load from the int* we casted to. |
| 2211 | auto Load = new LoadInst(Index, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2212 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2213 | // Our intrinsic to unpack a float2 from an int. |
| 2214 | auto SPIRVIntrinsic = "spirv.unpack.v2f16"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2215 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2216 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2217 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2218 | // Get our float2. |
| 2219 | return CallInst::Create(NewF, Load, "", CI); |
| 2220 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf4(Module &M) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2224 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2225 | const std::vector<const char *> Names = { |
David Neto | 556c7e6 | 2018-06-08 13:45:55 -0700 | [diff] [blame] | 2226 | "_Z11vload_half4jPU3AS1KDh", |
| 2227 | "_Z12vloada_half4jPU3AS1KDh", |
| 2228 | "_Z11vload_half4jPU3AS2KDh", |
| 2229 | "_Z12vloada_half4jPU3AS2KDh", |
| 2230 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2231 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2232 | return replaceCallsWithValue(M, Names, [&M](CallInst *CI) { |
| 2233 | // The index argument from vload_half. |
| 2234 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2235 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2236 | // The pointer argument from vload_half. |
| 2237 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2238 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2239 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2240 | auto Int2Ty = VectorType::get(IntTy, 2); |
| 2241 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2242 | auto NewPointerTy = |
| 2243 | PointerType::get(Int2Ty, Arg1->getType()->getPointerAddressSpace()); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2244 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2245 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2246 | // Cast the half* pointer to int2*. |
| 2247 | auto Cast = CastInst::CreatePointerCast(Arg1, NewPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2248 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2249 | // Index into the correct address of the casted pointer. |
| 2250 | auto Index = GetElementPtrInst::Create(Int2Ty, Cast, Arg0, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2251 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2252 | // Load from the int2* we casted to. |
| 2253 | auto Load = new LoadInst(Index, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2254 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2255 | // Extract each element from the loaded int2. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2256 | auto X = |
| 2257 | ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 0), "", CI); |
| 2258 | auto Y = |
| 2259 | ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 1), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2260 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2261 | // Our intrinsic to unpack a float2 from an int. |
| 2262 | auto SPIRVIntrinsic = "spirv.unpack.v2f16"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2263 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2264 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2265 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2266 | // Get the lower (x & y) components of our final float4. |
| 2267 | auto Lo = CallInst::Create(NewF, X, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2268 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2269 | // Get the higher (z & w) components of our final float4. |
| 2270 | auto Hi = CallInst::Create(NewF, Y, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2271 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2272 | Constant *ShuffleMask[4] = { |
| 2273 | ConstantInt::get(IntTy, 0), ConstantInt::get(IntTy, 1), |
| 2274 | ConstantInt::get(IntTy, 2), ConstantInt::get(IntTy, 3)}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2275 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2276 | // Combine our two float2's into one float4. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2277 | return new ShuffleVectorInst(Lo, Hi, ConstantVector::get(ShuffleMask), "", |
| 2278 | CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2279 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2280 | } |
| 2281 | |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2282 | bool ReplaceOpenCLBuiltinPass::replaceClspvVloadaHalf2(Module &M) { |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2283 | |
| 2284 | // Replace __clspv_vloada_half2(uint Index, global uint* Ptr) with: |
| 2285 | // |
| 2286 | // %u = load i32 %ptr |
| 2287 | // %fxy = call <2 x float> Unpack2xHalf(u) |
| 2288 | // %result = shufflevector %fxy %fzw <4 x i32> <0, 1, 2, 3> |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2289 | const std::vector<const char *> Names = { |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2290 | "_Z20__clspv_vloada_half2jPU3AS1Kj", // global |
| 2291 | "_Z20__clspv_vloada_half2jPU3AS3Kj", // local |
| 2292 | "_Z20__clspv_vloada_half2jPKj", // private |
| 2293 | }; |
| 2294 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2295 | return replaceCallsWithValue(M, Names, [&M](CallInst *CI) { |
| 2296 | auto Index = CI->getOperand(0); |
| 2297 | auto Ptr = CI->getOperand(1); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2298 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2299 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2300 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
| 2301 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2302 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2303 | auto IndexedPtr = GetElementPtrInst::Create(IntTy, Ptr, Index, "", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2304 | auto Load = new LoadInst(IndexedPtr, "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2305 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2306 | // Our intrinsic to unpack a float2 from an int. |
| 2307 | auto SPIRVIntrinsic = "spirv.unpack.v2f16"; |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2308 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2309 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2310 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2311 | // Get our final float2. |
| 2312 | return CallInst::Create(NewF, Load, "", CI); |
| 2313 | }); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | bool ReplaceOpenCLBuiltinPass::replaceClspvVloadaHalf4(Module &M) { |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2317 | |
| 2318 | // Replace __clspv_vloada_half4(uint Index, global uint2* Ptr) with: |
| 2319 | // |
| 2320 | // %u2 = load <2 x i32> %ptr |
| 2321 | // %u2xy = extractelement %u2, 0 |
| 2322 | // %u2zw = extractelement %u2, 1 |
| 2323 | // %fxy = call <2 x float> Unpack2xHalf(uint) |
| 2324 | // %fzw = call <2 x float> Unpack2xHalf(uint) |
| 2325 | // %result = shufflevector %fxy %fzw <4 x i32> <0, 1, 2, 3> |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2326 | const std::vector<const char *> Names = { |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2327 | "_Z20__clspv_vloada_half4jPU3AS1KDv2_j", // global |
| 2328 | "_Z20__clspv_vloada_half4jPU3AS3KDv2_j", // local |
| 2329 | "_Z20__clspv_vloada_half4jPKDv2_j", // private |
| 2330 | }; |
| 2331 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2332 | return replaceCallsWithValue(M, Names, [&M](CallInst *CI) { |
| 2333 | auto Index = CI->getOperand(0); |
| 2334 | auto Ptr = CI->getOperand(1); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2335 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2336 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2337 | auto Int2Ty = VectorType::get(IntTy, 2); |
| 2338 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
| 2339 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2340 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2341 | auto IndexedPtr = GetElementPtrInst::Create(Int2Ty, Ptr, Index, "", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2342 | auto Load = new LoadInst(IndexedPtr, "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2343 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2344 | // Extract each element from the loaded int2. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2345 | auto X = |
| 2346 | ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 0), "", CI); |
| 2347 | auto Y = |
| 2348 | ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 1), "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2349 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2350 | // Our intrinsic to unpack a float2 from an int. |
| 2351 | auto SPIRVIntrinsic = "spirv.unpack.v2f16"; |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2352 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2353 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2354 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2355 | // Get the lower (x & y) components of our final float4. |
| 2356 | auto Lo = CallInst::Create(NewF, X, "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2357 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2358 | // Get the higher (z & w) components of our final float4. |
| 2359 | auto Hi = CallInst::Create(NewF, Y, "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2360 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2361 | Constant *ShuffleMask[4] = { |
| 2362 | ConstantInt::get(IntTy, 0), ConstantInt::get(IntTy, 1), |
| 2363 | ConstantInt::get(IntTy, 2), ConstantInt::get(IntTy, 3)}; |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2364 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2365 | // Combine our two float2's into one float4. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2366 | return new ShuffleVectorInst(Lo, Hi, ConstantVector::get(ShuffleMask), "", |
| 2367 | CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2368 | }); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2371 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf(Module &M) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2372 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2373 | const std::vector<const char *> Names = {"_Z11vstore_halffjPU3AS1Dh", |
| 2374 | "_Z15vstore_half_rtefjPU3AS1Dh", |
| 2375 | "_Z15vstore_half_rtzfjPU3AS1Dh"}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2376 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2377 | return replaceCallsWithValue(M, Names, [&M](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2378 | // The value to store. |
| 2379 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2380 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2381 | // The index argument from vstore_half. |
| 2382 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2383 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2384 | // The pointer argument from vstore_half. |
| 2385 | auto Arg2 = CI->getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2386 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2387 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2388 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
| 2389 | auto NewFType = FunctionType::get(IntTy, Float2Ty, false); |
| 2390 | auto One = ConstantInt::get(IntTy, 1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2391 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2392 | // Our intrinsic to pack a float2 to an int. |
| 2393 | auto SPIRVIntrinsic = "spirv.pack.v2f16"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2394 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2395 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2396 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2397 | // Insert our value into a float2 so that we can pack it. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2398 | auto TempVec = InsertElementInst::Create( |
| 2399 | UndefValue::get(Float2Ty), Arg0, ConstantInt::get(IntTy, 0), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2400 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2401 | // Pack the float2 -> half2 (in an int). |
| 2402 | auto X = CallInst::Create(NewF, TempVec, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2403 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2404 | Value *Ret; |
| 2405 | if (clspv::Option::F16BitStorage()) { |
| 2406 | auto ShortTy = Type::getInt16Ty(M.getContext()); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2407 | auto ShortPointerTy = |
| 2408 | PointerType::get(ShortTy, Arg2->getType()->getPointerAddressSpace()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2409 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2410 | // Truncate our i32 to an i16. |
| 2411 | auto Trunc = CastInst::CreateTruncOrBitCast(X, ShortTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2412 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2413 | // Cast the half* pointer to short*. |
| 2414 | auto Cast = CastInst::CreatePointerCast(Arg2, ShortPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2415 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2416 | // Index into the correct address of the casted pointer. |
| 2417 | auto Index = GetElementPtrInst::Create(ShortTy, Cast, Arg1, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2418 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2419 | // Store to the int* we casted to. |
| 2420 | Ret = new StoreInst(Trunc, Index, CI); |
| 2421 | } else { |
| 2422 | // We can only write to 32-bit aligned words. |
| 2423 | // |
| 2424 | // Assuming base is aligned to 32-bits, replace the equivalent of |
| 2425 | // vstore_half(value, index, base) |
| 2426 | // with: |
| 2427 | // uint32_t* target_ptr = (uint32_t*)(base) + index / 2; |
| 2428 | // uint32_t write_to_upper_half = index & 1u; |
| 2429 | // uint32_t shift = write_to_upper_half << 4; |
| 2430 | // |
| 2431 | // // Pack the float value as a half number in bottom 16 bits |
| 2432 | // // of an i32. |
| 2433 | // uint32_t packed = spirv.pack.v2f16((float2)(value, undef)); |
| 2434 | // |
| 2435 | // uint32_t xor_value = (*target_ptr & (0xffff << shift)) |
| 2436 | // ^ ((packed & 0xffff) << shift) |
| 2437 | // // We only need relaxed consistency, but OpenCL 1.2 only has |
| 2438 | // // sequentially consistent atomics. |
| 2439 | // // TODO(dneto): Use relaxed consistency. |
| 2440 | // atomic_xor(target_ptr, xor_value) |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2441 | auto IntPointerTy = |
| 2442 | PointerType::get(IntTy, Arg2->getType()->getPointerAddressSpace()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2443 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2444 | auto Four = ConstantInt::get(IntTy, 4); |
| 2445 | auto FFFF = ConstantInt::get(IntTy, 0xffff); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2446 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2447 | auto IndexIsOdd = |
| 2448 | BinaryOperator::CreateAnd(Arg1, One, "index_is_odd_i32", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2449 | // Compute index / 2 |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2450 | auto IndexIntoI32 = |
| 2451 | BinaryOperator::CreateLShr(Arg1, One, "index_into_i32", CI); |
| 2452 | auto BaseI32Ptr = |
| 2453 | CastInst::CreatePointerCast(Arg2, IntPointerTy, "base_i32_ptr", CI); |
| 2454 | auto OutPtr = GetElementPtrInst::Create(IntTy, BaseI32Ptr, IndexIntoI32, |
| 2455 | "base_i32_ptr", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2456 | auto CurrentValue = new LoadInst(OutPtr, "current_value", CI); |
| 2457 | auto Shift = BinaryOperator::CreateShl(IndexIsOdd, Four, "shift", CI); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2458 | auto MaskBitsToWrite = |
| 2459 | BinaryOperator::CreateShl(FFFF, Shift, "mask_bits_to_write", CI); |
| 2460 | auto MaskedCurrent = BinaryOperator::CreateAnd( |
| 2461 | MaskBitsToWrite, CurrentValue, "masked_current", CI); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2462 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2463 | auto XLowerBits = |
| 2464 | BinaryOperator::CreateAnd(X, FFFF, "lower_bits_of_packed", CI); |
| 2465 | auto NewBitsToWrite = |
| 2466 | BinaryOperator::CreateShl(XLowerBits, Shift, "new_bits_to_write", CI); |
| 2467 | auto ValueToXor = BinaryOperator::CreateXor(MaskedCurrent, NewBitsToWrite, |
| 2468 | "value_to_xor", CI); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2469 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2470 | // Generate the call to atomi_xor. |
| 2471 | SmallVector<Type *, 5> ParamTypes; |
| 2472 | // The pointer type. |
| 2473 | ParamTypes.push_back(IntPointerTy); |
| 2474 | // The Types for memory scope, semantics, and value. |
| 2475 | ParamTypes.push_back(IntTy); |
| 2476 | ParamTypes.push_back(IntTy); |
| 2477 | ParamTypes.push_back(IntTy); |
| 2478 | auto NewFType = FunctionType::get(IntTy, ParamTypes, false); |
| 2479 | auto NewF = M.getOrInsertFunction("spirv.atomic_xor", NewFType); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2480 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2481 | const auto ConstantScopeDevice = |
| 2482 | ConstantInt::get(IntTy, spv::ScopeDevice); |
| 2483 | // Assume the pointee is in OpenCL global (SPIR-V Uniform) or local |
| 2484 | // (SPIR-V Workgroup). |
| 2485 | const auto AddrSpaceSemanticsBits = |
| 2486 | IntPointerTy->getPointerAddressSpace() == 1 |
| 2487 | ? spv::MemorySemanticsUniformMemoryMask |
| 2488 | : spv::MemorySemanticsWorkgroupMemoryMask; |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2489 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2490 | // We're using relaxed consistency here. |
| 2491 | const auto ConstantMemorySemantics = |
| 2492 | ConstantInt::get(IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 2493 | AddrSpaceSemanticsBits); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2494 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2495 | SmallVector<Value *, 5> Params{OutPtr, ConstantScopeDevice, |
| 2496 | ConstantMemorySemantics, ValueToXor}; |
| 2497 | CallInst::Create(NewF, Params, "store_halfword_xor_trick", CI); |
| 2498 | Ret = nullptr; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2499 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2500 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2501 | return Ret; |
| 2502 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf2(Module &M) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2506 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2507 | const std::vector<const char *> Names = { |
David Neto | e287152 | 2018-06-08 11:09:54 -0700 | [diff] [blame] | 2508 | "_Z12vstore_half2Dv2_fjPU3AS1Dh", |
| 2509 | "_Z13vstorea_half2Dv2_fjPU3AS1Dh", // vstorea global |
| 2510 | "_Z13vstorea_half2Dv2_fjPU3AS3Dh", // vstorea local |
| 2511 | "_Z13vstorea_half2Dv2_fjPDh", // vstorea private |
| 2512 | "_Z16vstore_half2_rteDv2_fjPU3AS1Dh", |
| 2513 | "_Z17vstorea_half2_rteDv2_fjPU3AS1Dh", // vstorea global |
| 2514 | "_Z17vstorea_half2_rteDv2_fjPU3AS3Dh", // vstorea local |
| 2515 | "_Z17vstorea_half2_rteDv2_fjPDh", // vstorea private |
| 2516 | "_Z16vstore_half2_rtzDv2_fjPU3AS1Dh", |
| 2517 | "_Z17vstorea_half2_rtzDv2_fjPU3AS1Dh", // vstorea global |
| 2518 | "_Z17vstorea_half2_rtzDv2_fjPU3AS3Dh", // vstorea local |
| 2519 | "_Z17vstorea_half2_rtzDv2_fjPDh", // vstorea private |
| 2520 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2521 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2522 | return replaceCallsWithValue(M, Names, [&M](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2523 | // The value to store. |
| 2524 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2525 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2526 | // The index argument from vstore_half. |
| 2527 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2528 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2529 | // The pointer argument from vstore_half. |
| 2530 | auto Arg2 = CI->getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2531 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2532 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2533 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2534 | auto NewPointerTy = |
| 2535 | PointerType::get(IntTy, Arg2->getType()->getPointerAddressSpace()); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2536 | auto NewFType = FunctionType::get(IntTy, Float2Ty, false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2537 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2538 | // Our intrinsic to pack a float2 to an int. |
| 2539 | auto SPIRVIntrinsic = "spirv.pack.v2f16"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2540 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2541 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2542 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2543 | // Turn the packed x & y into the final packing. |
| 2544 | auto X = CallInst::Create(NewF, Arg0, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2545 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2546 | // Cast the half* pointer to int*. |
| 2547 | auto Cast = CastInst::CreatePointerCast(Arg2, NewPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2548 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2549 | // Index into the correct address of the casted pointer. |
| 2550 | auto Index = GetElementPtrInst::Create(IntTy, Cast, Arg1, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2551 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2552 | // Store to the int* we casted to. |
| 2553 | return new StoreInst(X, Index, CI); |
| 2554 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf4(Module &M) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2558 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2559 | const std::vector<const char *> Names = { |
David Neto | e287152 | 2018-06-08 11:09:54 -0700 | [diff] [blame] | 2560 | "_Z12vstore_half4Dv4_fjPU3AS1Dh", |
| 2561 | "_Z13vstorea_half4Dv4_fjPU3AS1Dh", // global |
| 2562 | "_Z13vstorea_half4Dv4_fjPU3AS3Dh", // local |
| 2563 | "_Z13vstorea_half4Dv4_fjPDh", // private |
| 2564 | "_Z16vstore_half4_rteDv4_fjPU3AS1Dh", |
| 2565 | "_Z17vstorea_half4_rteDv4_fjPU3AS1Dh", // global |
| 2566 | "_Z17vstorea_half4_rteDv4_fjPU3AS3Dh", // local |
| 2567 | "_Z17vstorea_half4_rteDv4_fjPDh", // private |
| 2568 | "_Z16vstore_half4_rtzDv4_fjPU3AS1Dh", |
| 2569 | "_Z17vstorea_half4_rtzDv4_fjPU3AS1Dh", // global |
| 2570 | "_Z17vstorea_half4_rtzDv4_fjPU3AS3Dh", // local |
| 2571 | "_Z17vstorea_half4_rtzDv4_fjPDh", // private |
| 2572 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2573 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2574 | return replaceCallsWithValue(M, Names, [&M](CallInst *CI) { |
| 2575 | // The value to store. |
| 2576 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2577 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2578 | // The index argument from vstore_half. |
| 2579 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2580 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2581 | // The pointer argument from vstore_half. |
| 2582 | auto Arg2 = CI->getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2583 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2584 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2585 | auto Int2Ty = VectorType::get(IntTy, 2); |
| 2586 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2587 | auto NewPointerTy = |
| 2588 | PointerType::get(Int2Ty, Arg2->getType()->getPointerAddressSpace()); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2589 | auto NewFType = FunctionType::get(IntTy, Float2Ty, false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2590 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2591 | Constant *LoShuffleMask[2] = {ConstantInt::get(IntTy, 0), |
| 2592 | ConstantInt::get(IntTy, 1)}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2593 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2594 | // Extract out the x & y components of our to store value. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2595 | auto Lo = new ShuffleVectorInst(Arg0, UndefValue::get(Arg0->getType()), |
| 2596 | ConstantVector::get(LoShuffleMask), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2597 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2598 | Constant *HiShuffleMask[2] = {ConstantInt::get(IntTy, 2), |
| 2599 | ConstantInt::get(IntTy, 3)}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2600 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2601 | // Extract out the z & w components of our to store value. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2602 | auto Hi = new ShuffleVectorInst(Arg0, UndefValue::get(Arg0->getType()), |
| 2603 | ConstantVector::get(HiShuffleMask), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2604 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2605 | // Our intrinsic to pack a float2 to an int. |
| 2606 | auto SPIRVIntrinsic = "spirv.pack.v2f16"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2607 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2608 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2609 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2610 | // Turn the packed x & y into the final component of our int2. |
| 2611 | auto X = CallInst::Create(NewF, Lo, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2612 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2613 | // Turn the packed z & w into the final component of our int2. |
| 2614 | auto Y = CallInst::Create(NewF, Hi, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2615 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2616 | auto Combine = InsertElementInst::Create( |
| 2617 | UndefValue::get(Int2Ty), X, ConstantInt::get(IntTy, 0), "", CI); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2618 | Combine = InsertElementInst::Create(Combine, Y, ConstantInt::get(IntTy, 1), |
| 2619 | "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2620 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2621 | // Cast the half* pointer to int2*. |
| 2622 | auto Cast = CastInst::CreatePointerCast(Arg2, NewPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2623 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2624 | // Index into the correct address of the casted pointer. |
| 2625 | auto Index = GetElementPtrInst::Create(Int2Ty, Cast, Arg1, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2626 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2627 | // Store to the int2* we casted to. |
| 2628 | return new StoreInst(Combine, Index, CI); |
| 2629 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | bool ReplaceOpenCLBuiltinPass::replaceReadImageF(Module &M) { |
| 2633 | bool Changed = false; |
| 2634 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2635 | const std::map<const char *, const char *> Map = { |
| 2636 | {"_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_i", |
| 2637 | "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f"}, |
| 2638 | {"_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv4_i", |
| 2639 | "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv4_f"}}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2640 | |
| 2641 | for (auto Pair : Map) { |
| 2642 | // If we find a function with the matching name. |
| 2643 | if (auto F = M.getFunction(Pair.first)) { |
| 2644 | SmallVector<Instruction *, 4> ToRemoves; |
| 2645 | |
| 2646 | // Walk the users of the function. |
| 2647 | for (auto &U : F->uses()) { |
| 2648 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 2649 | // The image. |
| 2650 | auto Arg0 = CI->getOperand(0); |
| 2651 | |
| 2652 | // The sampler. |
| 2653 | auto Arg1 = CI->getOperand(1); |
| 2654 | |
| 2655 | // The coordinate (integer type that we can't handle). |
| 2656 | auto Arg2 = CI->getOperand(2); |
| 2657 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2658 | auto FloatVecTy = |
| 2659 | VectorType::get(Type::getFloatTy(M.getContext()), |
| 2660 | Arg2->getType()->getVectorNumElements()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2661 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2662 | auto NewFType = FunctionType::get( |
| 2663 | CI->getType(), {Arg0->getType(), Arg1->getType(), FloatVecTy}, |
| 2664 | false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2665 | |
| 2666 | auto NewF = M.getOrInsertFunction(Pair.second, NewFType); |
| 2667 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2668 | auto Cast = |
| 2669 | CastInst::Create(Instruction::SIToFP, Arg2, FloatVecTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2670 | |
| 2671 | auto NewCI = CallInst::Create(NewF, {Arg0, Arg1, Cast}, "", CI); |
| 2672 | |
| 2673 | CI->replaceAllUsesWith(NewCI); |
| 2674 | |
| 2675 | // Lastly, remember to remove the user. |
| 2676 | ToRemoves.push_back(CI); |
| 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | Changed = !ToRemoves.empty(); |
| 2681 | |
| 2682 | // And cleanup the calls we don't use anymore. |
| 2683 | for (auto V : ToRemoves) { |
| 2684 | V->eraseFromParent(); |
| 2685 | } |
| 2686 | |
| 2687 | // And remove the function we don't need either too. |
| 2688 | F->eraseFromParent(); |
| 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | return Changed; |
| 2693 | } |
| 2694 | |
| 2695 | bool ReplaceOpenCLBuiltinPass::replaceAtomics(Module &M) { |
| 2696 | bool Changed = false; |
| 2697 | |
| 2698 | const std::map<const char *, const char *> Map = { |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2699 | {"_Z8atom_incPU3AS1Vi", "spirv.atomic_inc"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2700 | {"_Z8atom_incPU3AS3Vi", "spirv.atomic_inc"}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2701 | {"_Z8atom_incPU3AS1Vj", "spirv.atomic_inc"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2702 | {"_Z8atom_incPU3AS3Vj", "spirv.atomic_inc"}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2703 | {"_Z8atom_decPU3AS1Vi", "spirv.atomic_dec"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2704 | {"_Z8atom_decPU3AS3Vi", "spirv.atomic_dec"}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2705 | {"_Z8atom_decPU3AS1Vj", "spirv.atomic_dec"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2706 | {"_Z8atom_decPU3AS3Vj", "spirv.atomic_dec"}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2707 | {"_Z12atom_cmpxchgPU3AS1Viii", "spirv.atomic_compare_exchange"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2708 | {"_Z12atom_cmpxchgPU3AS3Viii", "spirv.atomic_compare_exchange"}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2709 | {"_Z12atom_cmpxchgPU3AS1Vjjj", "spirv.atomic_compare_exchange"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2710 | {"_Z12atom_cmpxchgPU3AS3Vjjj", "spirv.atomic_compare_exchange"}, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2711 | {"_Z10atomic_incPU3AS1Vi", "spirv.atomic_inc"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2712 | {"_Z10atomic_incPU3AS3Vi", "spirv.atomic_inc"}, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2713 | {"_Z10atomic_incPU3AS1Vj", "spirv.atomic_inc"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2714 | {"_Z10atomic_incPU3AS3Vj", "spirv.atomic_inc"}, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2715 | {"_Z10atomic_decPU3AS1Vi", "spirv.atomic_dec"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2716 | {"_Z10atomic_decPU3AS3Vi", "spirv.atomic_dec"}, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2717 | {"_Z10atomic_decPU3AS1Vj", "spirv.atomic_dec"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2718 | {"_Z10atomic_decPU3AS3Vj", "spirv.atomic_dec"}, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2719 | {"_Z14atomic_cmpxchgPU3AS1Viii", "spirv.atomic_compare_exchange"}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2720 | {"_Z14atomic_cmpxchgPU3AS3Viii", "spirv.atomic_compare_exchange"}, |
| 2721 | {"_Z14atomic_cmpxchgPU3AS1Vjjj", "spirv.atomic_compare_exchange"}, |
| 2722 | {"_Z14atomic_cmpxchgPU3AS3Vjjj", "spirv.atomic_compare_exchange"}}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2723 | |
| 2724 | for (auto Pair : Map) { |
| 2725 | // If we find a function with the matching name. |
| 2726 | if (auto F = M.getFunction(Pair.first)) { |
| 2727 | SmallVector<Instruction *, 4> ToRemoves; |
| 2728 | |
| 2729 | // Walk the users of the function. |
| 2730 | for (auto &U : F->uses()) { |
| 2731 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 2732 | auto FType = F->getFunctionType(); |
| 2733 | SmallVector<Type *, 5> ParamTypes; |
| 2734 | |
| 2735 | // The pointer type. |
| 2736 | ParamTypes.push_back(FType->getParamType(0)); |
| 2737 | |
| 2738 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2739 | |
| 2740 | // The memory scope type. |
| 2741 | ParamTypes.push_back(IntTy); |
| 2742 | |
| 2743 | // The memory semantics type. |
| 2744 | ParamTypes.push_back(IntTy); |
| 2745 | |
| 2746 | if (2 < CI->getNumArgOperands()) { |
| 2747 | // The unequal memory semantics type. |
| 2748 | ParamTypes.push_back(IntTy); |
| 2749 | |
| 2750 | // The value type. |
| 2751 | ParamTypes.push_back(FType->getParamType(2)); |
| 2752 | |
| 2753 | // The comparator type. |
| 2754 | ParamTypes.push_back(FType->getParamType(1)); |
| 2755 | } else if (1 < CI->getNumArgOperands()) { |
| 2756 | // The value type. |
| 2757 | ParamTypes.push_back(FType->getParamType(1)); |
| 2758 | } |
| 2759 | |
| 2760 | auto NewFType = |
| 2761 | FunctionType::get(FType->getReturnType(), ParamTypes, false); |
| 2762 | auto NewF = M.getOrInsertFunction(Pair.second, NewFType); |
| 2763 | |
| 2764 | // We need to map the OpenCL constants to the SPIR-V equivalents. |
| 2765 | const auto ConstantScopeDevice = |
| 2766 | ConstantInt::get(IntTy, spv::ScopeDevice); |
| 2767 | const auto ConstantMemorySemantics = ConstantInt::get( |
| 2768 | IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 2769 | spv::MemorySemanticsSequentiallyConsistentMask); |
| 2770 | |
| 2771 | SmallVector<Value *, 5> Params; |
| 2772 | |
| 2773 | // The pointer. |
| 2774 | Params.push_back(CI->getArgOperand(0)); |
| 2775 | |
| 2776 | // The memory scope. |
| 2777 | Params.push_back(ConstantScopeDevice); |
| 2778 | |
| 2779 | // The memory semantics. |
| 2780 | Params.push_back(ConstantMemorySemantics); |
| 2781 | |
| 2782 | if (2 < CI->getNumArgOperands()) { |
| 2783 | // The unequal memory semantics. |
| 2784 | Params.push_back(ConstantMemorySemantics); |
| 2785 | |
| 2786 | // The value. |
| 2787 | Params.push_back(CI->getArgOperand(2)); |
| 2788 | |
| 2789 | // The comparator. |
| 2790 | Params.push_back(CI->getArgOperand(1)); |
| 2791 | } else if (1 < CI->getNumArgOperands()) { |
| 2792 | // The value. |
| 2793 | Params.push_back(CI->getArgOperand(1)); |
| 2794 | } |
| 2795 | |
| 2796 | auto NewCI = CallInst::Create(NewF, Params, "", CI); |
| 2797 | |
| 2798 | CI->replaceAllUsesWith(NewCI); |
| 2799 | |
| 2800 | // Lastly, remember to remove the user. |
| 2801 | ToRemoves.push_back(CI); |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | Changed = !ToRemoves.empty(); |
| 2806 | |
| 2807 | // And cleanup the calls we don't use anymore. |
| 2808 | for (auto V : ToRemoves) { |
| 2809 | V->eraseFromParent(); |
| 2810 | } |
| 2811 | |
| 2812 | // And remove the function we don't need either too. |
| 2813 | F->eraseFromParent(); |
| 2814 | } |
| 2815 | } |
| 2816 | |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2817 | const std::map<const char *, llvm::AtomicRMWInst::BinOp> Map2 = { |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2818 | {"_Z8atom_addPU3AS1Vii", llvm::AtomicRMWInst::Add}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2819 | {"_Z8atom_addPU3AS3Vii", llvm::AtomicRMWInst::Add}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2820 | {"_Z8atom_addPU3AS1Vjj", llvm::AtomicRMWInst::Add}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2821 | {"_Z8atom_addPU3AS3Vjj", llvm::AtomicRMWInst::Add}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2822 | {"_Z8atom_subPU3AS1Vii", llvm::AtomicRMWInst::Sub}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2823 | {"_Z8atom_subPU3AS3Vii", llvm::AtomicRMWInst::Sub}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2824 | {"_Z8atom_subPU3AS1Vjj", llvm::AtomicRMWInst::Sub}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2825 | {"_Z8atom_subPU3AS3Vjj", llvm::AtomicRMWInst::Sub}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2826 | {"_Z9atom_xchgPU3AS1Vii", llvm::AtomicRMWInst::Xchg}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2827 | {"_Z9atom_xchgPU3AS3Vii", llvm::AtomicRMWInst::Xchg}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2828 | {"_Z9atom_xchgPU3AS1Vjj", llvm::AtomicRMWInst::Xchg}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2829 | {"_Z9atom_xchgPU3AS3Vjj", llvm::AtomicRMWInst::Xchg}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2830 | {"_Z8atom_minPU3AS1Vii", llvm::AtomicRMWInst::Min}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2831 | {"_Z8atom_minPU3AS3Vii", llvm::AtomicRMWInst::Min}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2832 | {"_Z8atom_minPU3AS1Vjj", llvm::AtomicRMWInst::UMin}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2833 | {"_Z8atom_minPU3AS3Vjj", llvm::AtomicRMWInst::UMin}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2834 | {"_Z8atom_maxPU3AS1Vii", llvm::AtomicRMWInst::Max}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2835 | {"_Z8atom_maxPU3AS3Vii", llvm::AtomicRMWInst::Max}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2836 | {"_Z8atom_maxPU3AS1Vjj", llvm::AtomicRMWInst::UMax}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2837 | {"_Z8atom_maxPU3AS3Vjj", llvm::AtomicRMWInst::UMax}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2838 | {"_Z8atom_andPU3AS1Vii", llvm::AtomicRMWInst::And}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2839 | {"_Z8atom_andPU3AS3Vii", llvm::AtomicRMWInst::And}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2840 | {"_Z8atom_andPU3AS1Vjj", llvm::AtomicRMWInst::And}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2841 | {"_Z8atom_andPU3AS3Vjj", llvm::AtomicRMWInst::And}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2842 | {"_Z7atom_orPU3AS1Vii", llvm::AtomicRMWInst::Or}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2843 | {"_Z7atom_orPU3AS3Vii", llvm::AtomicRMWInst::Or}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2844 | {"_Z7atom_orPU3AS1Vjj", llvm::AtomicRMWInst::Or}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2845 | {"_Z7atom_orPU3AS3Vjj", llvm::AtomicRMWInst::Or}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2846 | {"_Z8atom_xorPU3AS1Vii", llvm::AtomicRMWInst::Xor}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2847 | {"_Z8atom_xorPU3AS3Vii", llvm::AtomicRMWInst::Xor}, |
Kévin Petit | 4f6c6b0 | 2018-10-25 18:56:55 +0000 | [diff] [blame] | 2848 | {"_Z8atom_xorPU3AS1Vjj", llvm::AtomicRMWInst::Xor}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2849 | {"_Z8atom_xorPU3AS3Vjj", llvm::AtomicRMWInst::Xor}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2850 | {"_Z10atomic_addPU3AS1Vii", llvm::AtomicRMWInst::Add}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2851 | {"_Z10atomic_addPU3AS3Vii", llvm::AtomicRMWInst::Add}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2852 | {"_Z10atomic_addPU3AS1Vjj", llvm::AtomicRMWInst::Add}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2853 | {"_Z10atomic_addPU3AS3Vjj", llvm::AtomicRMWInst::Add}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2854 | {"_Z10atomic_subPU3AS1Vii", llvm::AtomicRMWInst::Sub}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2855 | {"_Z10atomic_subPU3AS3Vii", llvm::AtomicRMWInst::Sub}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2856 | {"_Z10atomic_subPU3AS1Vjj", llvm::AtomicRMWInst::Sub}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2857 | {"_Z10atomic_subPU3AS3Vjj", llvm::AtomicRMWInst::Sub}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2858 | {"_Z11atomic_xchgPU3AS1Vii", llvm::AtomicRMWInst::Xchg}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2859 | {"_Z11atomic_xchgPU3AS3Vii", llvm::AtomicRMWInst::Xchg}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2860 | {"_Z11atomic_xchgPU3AS1Vjj", llvm::AtomicRMWInst::Xchg}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2861 | {"_Z11atomic_xchgPU3AS3Vjj", llvm::AtomicRMWInst::Xchg}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2862 | {"_Z10atomic_minPU3AS1Vii", llvm::AtomicRMWInst::Min}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2863 | {"_Z10atomic_minPU3AS3Vii", llvm::AtomicRMWInst::Min}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2864 | {"_Z10atomic_minPU3AS1Vjj", llvm::AtomicRMWInst::UMin}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2865 | {"_Z10atomic_minPU3AS3Vjj", llvm::AtomicRMWInst::UMin}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2866 | {"_Z10atomic_maxPU3AS1Vii", llvm::AtomicRMWInst::Max}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2867 | {"_Z10atomic_maxPU3AS3Vii", llvm::AtomicRMWInst::Max}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2868 | {"_Z10atomic_maxPU3AS1Vjj", llvm::AtomicRMWInst::UMax}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2869 | {"_Z10atomic_maxPU3AS3Vjj", llvm::AtomicRMWInst::UMax}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2870 | {"_Z10atomic_andPU3AS1Vii", llvm::AtomicRMWInst::And}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2871 | {"_Z10atomic_andPU3AS3Vii", llvm::AtomicRMWInst::And}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2872 | {"_Z10atomic_andPU3AS1Vjj", llvm::AtomicRMWInst::And}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2873 | {"_Z10atomic_andPU3AS3Vjj", llvm::AtomicRMWInst::And}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2874 | {"_Z9atomic_orPU3AS1Vii", llvm::AtomicRMWInst::Or}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2875 | {"_Z9atomic_orPU3AS3Vii", llvm::AtomicRMWInst::Or}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2876 | {"_Z9atomic_orPU3AS1Vjj", llvm::AtomicRMWInst::Or}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2877 | {"_Z9atomic_orPU3AS3Vjj", llvm::AtomicRMWInst::Or}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2878 | {"_Z10atomic_xorPU3AS1Vii", llvm::AtomicRMWInst::Xor}, |
Kévin Petit | a303dc6 | 2019-03-26 21:40:35 +0000 | [diff] [blame] | 2879 | {"_Z10atomic_xorPU3AS3Vii", llvm::AtomicRMWInst::Xor}, |
| 2880 | {"_Z10atomic_xorPU3AS1Vjj", llvm::AtomicRMWInst::Xor}, |
| 2881 | {"_Z10atomic_xorPU3AS3Vjj", llvm::AtomicRMWInst::Xor}}; |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2882 | |
| 2883 | for (auto Pair : Map2) { |
| 2884 | // If we find a function with the matching name. |
| 2885 | if (auto F = M.getFunction(Pair.first)) { |
| 2886 | SmallVector<Instruction *, 4> ToRemoves; |
| 2887 | |
| 2888 | // Walk the users of the function. |
| 2889 | for (auto &U : F->uses()) { |
| 2890 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 2891 | auto AtomicOp = new AtomicRMWInst( |
| 2892 | Pair.second, CI->getArgOperand(0), CI->getArgOperand(1), |
| 2893 | AtomicOrdering::SequentiallyConsistent, SyncScope::System, CI); |
| 2894 | |
| 2895 | CI->replaceAllUsesWith(AtomicOp); |
| 2896 | |
| 2897 | // Lastly, remember to remove the user. |
| 2898 | ToRemoves.push_back(CI); |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | Changed = !ToRemoves.empty(); |
| 2903 | |
| 2904 | // And cleanup the calls we don't use anymore. |
| 2905 | for (auto V : ToRemoves) { |
| 2906 | V->eraseFromParent(); |
| 2907 | } |
| 2908 | |
| 2909 | // And remove the function we don't need either too. |
| 2910 | F->eraseFromParent(); |
| 2911 | } |
| 2912 | } |
| 2913 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2914 | return Changed; |
| 2915 | } |
| 2916 | |
| 2917 | bool ReplaceOpenCLBuiltinPass::replaceCross(Module &M) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2918 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2919 | std::vector<const char *> Names = { |
| 2920 | "_Z5crossDv4_fS_", |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2921 | }; |
| 2922 | |
| 2923 | return replaceCallsWithValue(M, Names, [&M](CallInst *CI) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2924 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2925 | auto FloatTy = Type::getFloatTy(M.getContext()); |
| 2926 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2927 | Constant *DownShuffleMask[3] = {ConstantInt::get(IntTy, 0), |
| 2928 | ConstantInt::get(IntTy, 1), |
| 2929 | ConstantInt::get(IntTy, 2)}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2930 | |
| 2931 | Constant *UpShuffleMask[4] = { |
| 2932 | ConstantInt::get(IntTy, 0), ConstantInt::get(IntTy, 1), |
| 2933 | ConstantInt::get(IntTy, 2), ConstantInt::get(IntTy, 3)}; |
| 2934 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2935 | Constant *FloatVec[3] = {ConstantFP::get(FloatTy, 0.0f), |
| 2936 | UndefValue::get(FloatTy), |
| 2937 | UndefValue::get(FloatTy)}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2938 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2939 | auto Vec4Ty = CI->getArgOperand(0)->getType(); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2940 | auto Arg0 = |
| 2941 | new ShuffleVectorInst(CI->getArgOperand(0), UndefValue::get(Vec4Ty), |
| 2942 | ConstantVector::get(DownShuffleMask), "", CI); |
| 2943 | auto Arg1 = |
| 2944 | new ShuffleVectorInst(CI->getArgOperand(1), UndefValue::get(Vec4Ty), |
| 2945 | ConstantVector::get(DownShuffleMask), "", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2946 | auto Vec3Ty = Arg0->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2947 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2948 | auto NewFType = FunctionType::get(Vec3Ty, {Vec3Ty, Vec3Ty}, false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2949 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2950 | auto Cross3Func = M.getOrInsertFunction("_Z5crossDv3_fS_", NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2951 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2952 | auto DownResult = CallInst::Create(Cross3Func, {Arg0, Arg1}, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2953 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2954 | return new ShuffleVectorInst(DownResult, ConstantVector::get(FloatVec), |
| 2955 | ConstantVector::get(UpShuffleMask), "", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2956 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2957 | } |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2958 | |
| 2959 | bool ReplaceOpenCLBuiltinPass::replaceFract(Module &M) { |
| 2960 | bool Changed = false; |
| 2961 | |
| 2962 | // OpenCL's float result = fract(float x, float* ptr) |
| 2963 | // |
| 2964 | // In the LLVM domain: |
| 2965 | // |
| 2966 | // %floor_result = call spir_func float @floor(float %x) |
| 2967 | // store float %floor_result, float * %ptr |
| 2968 | // %fract_intermediate = call spir_func float @clspv.fract(float %x) |
| 2969 | // %result = call spir_func float |
| 2970 | // @fmin(float %fract_intermediate, float 0x1.fffffep-1f) |
| 2971 | // |
| 2972 | // Becomes in the SPIR-V domain, where translations of floor, fmin, |
| 2973 | // and clspv.fract occur in the SPIR-V generator pass: |
| 2974 | // |
| 2975 | // %glsl_ext = OpExtInstImport "GLSL.std.450" |
| 2976 | // %just_under_1 = OpConstant %float 0x1.fffffep-1f |
| 2977 | // ... |
| 2978 | // %floor_result = OpExtInst %float %glsl_ext Floor %x |
| 2979 | // OpStore %ptr %floor_result |
| 2980 | // %fract_intermediate = OpExtInst %float %glsl_ext Fract %x |
| 2981 | // %fract_result = OpExtInst %float |
| 2982 | // %glsl_ext Fmin %fract_intermediate %just_under_1 |
| 2983 | |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2984 | using std::string; |
| 2985 | |
| 2986 | // Mapping from the fract builtin to the floor, fmin, and clspv.fract builtins |
| 2987 | // we need. The clspv.fract builtin is the same as GLSL.std.450 Fract. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2988 | using QuadType = |
| 2989 | std::tuple<const char *, const char *, const char *, const char *>; |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2990 | auto make_quad = [](const char *a, const char *b, const char *c, |
| 2991 | const char *d) { |
| 2992 | return std::tuple<const char *, const char *, const char *, const char *>( |
| 2993 | a, b, c, d); |
| 2994 | }; |
| 2995 | const std::vector<QuadType> Functions = { |
| 2996 | make_quad("_Z5fractfPf", "_Z5floorff", "_Z4fminff", "clspv.fract.f"), |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2997 | make_quad("_Z5fractDv2_fPS_", "_Z5floorDv2_f", "_Z4fminDv2_ff", |
| 2998 | "clspv.fract.v2f"), |
| 2999 | make_quad("_Z5fractDv3_fPS_", "_Z5floorDv3_f", "_Z4fminDv3_ff", |
| 3000 | "clspv.fract.v3f"), |
| 3001 | make_quad("_Z5fractDv4_fPS_", "_Z5floorDv4_f", "_Z4fminDv4_ff", |
| 3002 | "clspv.fract.v4f"), |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3003 | }; |
| 3004 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3005 | for (auto &quad : Functions) { |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3006 | const StringRef fract_name(std::get<0>(quad)); |
| 3007 | |
| 3008 | // If we find a function with the matching name. |
| 3009 | if (auto F = M.getFunction(fract_name)) { |
| 3010 | if (F->use_begin() == F->use_end()) |
| 3011 | continue; |
| 3012 | |
| 3013 | // We have some uses. |
| 3014 | Changed = true; |
| 3015 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3016 | auto &Context = M.getContext(); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3017 | |
| 3018 | const StringRef floor_name(std::get<1>(quad)); |
| 3019 | const StringRef fmin_name(std::get<2>(quad)); |
| 3020 | const StringRef clspv_fract_name(std::get<3>(quad)); |
| 3021 | |
| 3022 | // This is either float or a float vector. All the float-like |
| 3023 | // types are this type. |
| 3024 | auto result_ty = F->getReturnType(); |
| 3025 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3026 | Function *fmin_fn = M.getFunction(fmin_name); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3027 | if (!fmin_fn) { |
| 3028 | // Make the fmin function. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3029 | FunctionType *fn_ty = |
| 3030 | FunctionType::get(result_ty, {result_ty, result_ty}, false); |
alan-baker | bccf62c | 2019-03-29 10:32:41 -0400 | [diff] [blame] | 3031 | fmin_fn = |
| 3032 | cast<Function>(M.getOrInsertFunction(fmin_name, fn_ty).getCallee()); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3033 | fmin_fn->addFnAttr(Attribute::ReadNone); |
| 3034 | fmin_fn->setCallingConv(CallingConv::SPIR_FUNC); |
| 3035 | } |
| 3036 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3037 | Function *floor_fn = M.getFunction(floor_name); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3038 | if (!floor_fn) { |
| 3039 | // Make the floor function. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3040 | FunctionType *fn_ty = FunctionType::get(result_ty, {result_ty}, false); |
alan-baker | bccf62c | 2019-03-29 10:32:41 -0400 | [diff] [blame] | 3041 | floor_fn = cast<Function>( |
| 3042 | M.getOrInsertFunction(floor_name, fn_ty).getCallee()); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3043 | floor_fn->addFnAttr(Attribute::ReadNone); |
| 3044 | floor_fn->setCallingConv(CallingConv::SPIR_FUNC); |
| 3045 | } |
| 3046 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3047 | Function *clspv_fract_fn = M.getFunction(clspv_fract_name); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3048 | if (!clspv_fract_fn) { |
| 3049 | // Make the clspv_fract function. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3050 | FunctionType *fn_ty = FunctionType::get(result_ty, {result_ty}, false); |
alan-baker | bccf62c | 2019-03-29 10:32:41 -0400 | [diff] [blame] | 3051 | clspv_fract_fn = cast<Function>( |
| 3052 | M.getOrInsertFunction(clspv_fract_name, fn_ty).getCallee()); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3053 | clspv_fract_fn->addFnAttr(Attribute::ReadNone); |
| 3054 | clspv_fract_fn->setCallingConv(CallingConv::SPIR_FUNC); |
| 3055 | } |
| 3056 | |
| 3057 | // Number of significant significand bits, whether represented or not. |
| 3058 | unsigned num_significand_bits; |
| 3059 | switch (result_ty->getScalarType()->getTypeID()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3060 | case Type::HalfTyID: |
| 3061 | num_significand_bits = 11; |
| 3062 | break; |
| 3063 | case Type::FloatTyID: |
| 3064 | num_significand_bits = 24; |
| 3065 | break; |
| 3066 | case Type::DoubleTyID: |
| 3067 | num_significand_bits = 53; |
| 3068 | break; |
| 3069 | default: |
| 3070 | assert(false && "Unhandled float type when processing fract builtin"); |
| 3071 | break; |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3072 | } |
| 3073 | // Beware that the disassembler displays this value as |
| 3074 | // OpConstant %float 1 |
| 3075 | // which is not quite right. |
| 3076 | const double kJustUnderOneScalar = |
| 3077 | ldexp(double((1 << num_significand_bits) - 1), -num_significand_bits); |
| 3078 | |
| 3079 | Constant *just_under_one = |
| 3080 | ConstantFP::get(result_ty->getScalarType(), kJustUnderOneScalar); |
| 3081 | if (result_ty->isVectorTy()) { |
| 3082 | just_under_one = ConstantVector::getSplat( |
| 3083 | result_ty->getVectorNumElements(), just_under_one); |
| 3084 | } |
| 3085 | |
| 3086 | IRBuilder<> Builder(Context); |
| 3087 | |
| 3088 | SmallVector<Instruction *, 4> ToRemoves; |
| 3089 | |
| 3090 | // Walk the users of the function. |
| 3091 | for (auto &U : F->uses()) { |
| 3092 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 3093 | |
| 3094 | Builder.SetInsertPoint(CI); |
| 3095 | auto arg = CI->getArgOperand(0); |
| 3096 | auto ptr = CI->getArgOperand(1); |
| 3097 | |
| 3098 | // Compute floor result and store it. |
| 3099 | auto floor = Builder.CreateCall(floor_fn, {arg}); |
| 3100 | Builder.CreateStore(floor, ptr); |
| 3101 | |
| 3102 | auto fract_intermediate = Builder.CreateCall(clspv_fract_fn, arg); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3103 | auto fract_result = |
| 3104 | Builder.CreateCall(fmin_fn, {fract_intermediate, just_under_one}); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 3105 | |
| 3106 | CI->replaceAllUsesWith(fract_result); |
| 3107 | |
| 3108 | // Lastly, remember to remove the user. |
| 3109 | ToRemoves.push_back(CI); |
| 3110 | } |
| 3111 | } |
| 3112 | |
| 3113 | // And cleanup the calls we don't use anymore. |
| 3114 | for (auto V : ToRemoves) { |
| 3115 | V->eraseFromParent(); |
| 3116 | } |
| 3117 | |
| 3118 | // And remove the function we don't need either too. |
| 3119 | F->eraseFromParent(); |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | return Changed; |
| 3124 | } |