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 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 19 | #include <llvm/IR/Constants.h> |
| 20 | #include <llvm/IR/Instructions.h> |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 21 | #include <llvm/IR/IRBuilder.h> |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 22 | #include <llvm/IR/Module.h> |
| 23 | #include <llvm/Pass.h> |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 24 | #include <llvm/Support/CommandLine.h> |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 25 | #include <llvm/Support/raw_ostream.h> |
| 26 | #include <llvm/Transforms/Utils/Cloning.h> |
| 27 | |
| 28 | #include <spirv/1.0/spirv.hpp> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | #define DEBUG_TYPE "ReplaceOpenCLBuiltin" |
| 33 | |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 34 | // TODO(dneto): As per Neil's suggestion, might not need this if you can |
| 35 | // trace the pointer back far enough to see that it's 32-bit aligned. |
| 36 | // However, even in the vstore_half case, you'll probably get better |
| 37 | // performance if you can rely on SPV_KHR_16bit_storage since in the |
| 38 | // alternate case you're using a (relaxed) atomic, and therefore have |
| 39 | // to write through to the cache. |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 40 | static llvm::cl::opt<bool> f16bit_storage( |
| 41 | "f16bit_storage", llvm::cl::init(false), |
| 42 | llvm::cl::desc("Assume the target supports SPV_KHR_16bit_storage")); |
| 43 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 44 | namespace { |
| 45 | uint32_t clz(uint32_t v) { |
| 46 | uint32_t r; |
| 47 | uint32_t shift; |
| 48 | |
| 49 | r = (v > 0xFFFF) << 4; |
| 50 | v >>= r; |
| 51 | shift = (v > 0xFF) << 3; |
| 52 | v >>= shift; |
| 53 | r |= shift; |
| 54 | shift = (v > 0xF) << 2; |
| 55 | v >>= shift; |
| 56 | r |= shift; |
| 57 | shift = (v > 0x3) << 1; |
| 58 | v >>= shift; |
| 59 | r |= shift; |
| 60 | r |= (v >> 1); |
| 61 | |
| 62 | return r; |
| 63 | } |
| 64 | |
| 65 | Type *getBoolOrBoolVectorTy(LLVMContext &C, unsigned elements) { |
| 66 | if (1 == elements) { |
| 67 | return Type::getInt1Ty(C); |
| 68 | } else { |
| 69 | return VectorType::get(Type::getInt1Ty(C), elements); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | struct ReplaceOpenCLBuiltinPass final : public ModulePass { |
| 74 | static char ID; |
| 75 | ReplaceOpenCLBuiltinPass() : ModulePass(ID) {} |
| 76 | |
| 77 | bool runOnModule(Module &M) override; |
| 78 | bool replaceRecip(Module &M); |
| 79 | bool replaceDivide(Module &M); |
| 80 | bool replaceExp10(Module &M); |
| 81 | bool replaceLog10(Module &M); |
| 82 | bool replaceBarrier(Module &M); |
| 83 | bool replaceMemFence(Module &M); |
| 84 | bool replaceRelational(Module &M); |
| 85 | bool replaceIsInfAndIsNan(Module &M); |
| 86 | bool replaceAllAndAny(Module &M); |
| 87 | bool replaceSignbit(Module &M); |
| 88 | bool replaceMadandMad24andMul24(Module &M); |
| 89 | bool replaceVloadHalf(Module &M); |
| 90 | bool replaceVloadHalf2(Module &M); |
| 91 | bool replaceVloadHalf4(Module &M); |
| 92 | bool replaceVstoreHalf(Module &M); |
| 93 | bool replaceVstoreHalf2(Module &M); |
| 94 | bool replaceVstoreHalf4(Module &M); |
| 95 | bool replaceReadImageF(Module &M); |
| 96 | bool replaceAtomics(Module &M); |
| 97 | bool replaceCross(Module &M); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 98 | bool replaceFract(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 99 | }; |
| 100 | } |
| 101 | |
| 102 | char ReplaceOpenCLBuiltinPass::ID = 0; |
| 103 | static RegisterPass<ReplaceOpenCLBuiltinPass> X("ReplaceOpenCLBuiltin", |
| 104 | "Replace OpenCL Builtins Pass"); |
| 105 | |
| 106 | namespace clspv { |
| 107 | ModulePass *createReplaceOpenCLBuiltinPass() { |
| 108 | return new ReplaceOpenCLBuiltinPass(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | bool ReplaceOpenCLBuiltinPass::runOnModule(Module &M) { |
| 113 | bool Changed = false; |
| 114 | |
| 115 | Changed |= replaceRecip(M); |
| 116 | Changed |= replaceDivide(M); |
| 117 | Changed |= replaceExp10(M); |
| 118 | Changed |= replaceLog10(M); |
| 119 | Changed |= replaceBarrier(M); |
| 120 | Changed |= replaceMemFence(M); |
| 121 | Changed |= replaceRelational(M); |
| 122 | Changed |= replaceIsInfAndIsNan(M); |
| 123 | Changed |= replaceAllAndAny(M); |
| 124 | Changed |= replaceSignbit(M); |
| 125 | Changed |= replaceMadandMad24andMul24(M); |
| 126 | Changed |= replaceVloadHalf(M); |
| 127 | Changed |= replaceVloadHalf2(M); |
| 128 | Changed |= replaceVloadHalf4(M); |
| 129 | Changed |= replaceVstoreHalf(M); |
| 130 | Changed |= replaceVstoreHalf2(M); |
| 131 | Changed |= replaceVstoreHalf4(M); |
| 132 | Changed |= replaceReadImageF(M); |
| 133 | Changed |= replaceAtomics(M); |
| 134 | Changed |= replaceCross(M); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 135 | Changed |= replaceFract(M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 136 | |
| 137 | return Changed; |
| 138 | } |
| 139 | |
| 140 | bool ReplaceOpenCLBuiltinPass::replaceRecip(Module &M) { |
| 141 | bool Changed = false; |
| 142 | |
| 143 | const char *Names[] = { |
| 144 | "_Z10half_recipf", "_Z12native_recipf", "_Z10half_recipDv2_f", |
| 145 | "_Z12native_recipDv2_f", "_Z10half_recipDv3_f", "_Z12native_recipDv3_f", |
| 146 | "_Z10half_recipDv4_f", "_Z12native_recipDv4_f", |
| 147 | }; |
| 148 | |
| 149 | for (auto Name : Names) { |
| 150 | // If we find a function with the matching name. |
| 151 | if (auto F = M.getFunction(Name)) { |
| 152 | SmallVector<Instruction *, 4> ToRemoves; |
| 153 | |
| 154 | // Walk the users of the function. |
| 155 | for (auto &U : F->uses()) { |
| 156 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 157 | // Recip has one arg. |
| 158 | auto Arg = CI->getOperand(0); |
| 159 | |
| 160 | auto Div = BinaryOperator::Create( |
| 161 | Instruction::FDiv, ConstantFP::get(Arg->getType(), 1.0), Arg, "", |
| 162 | CI); |
| 163 | |
| 164 | CI->replaceAllUsesWith(Div); |
| 165 | |
| 166 | // Lastly, remember to remove the user. |
| 167 | ToRemoves.push_back(CI); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | Changed = !ToRemoves.empty(); |
| 172 | |
| 173 | // And cleanup the calls we don't use anymore. |
| 174 | for (auto V : ToRemoves) { |
| 175 | V->eraseFromParent(); |
| 176 | } |
| 177 | |
| 178 | // And remove the function we don't need either too. |
| 179 | F->eraseFromParent(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return Changed; |
| 184 | } |
| 185 | |
| 186 | bool ReplaceOpenCLBuiltinPass::replaceDivide(Module &M) { |
| 187 | bool Changed = false; |
| 188 | |
| 189 | const char *Names[] = { |
| 190 | "_Z11half_divideff", "_Z13native_divideff", |
| 191 | "_Z11half_divideDv2_fS_", "_Z13native_divideDv2_fS_", |
| 192 | "_Z11half_divideDv3_fS_", "_Z13native_divideDv3_fS_", |
| 193 | "_Z11half_divideDv4_fS_", "_Z13native_divideDv4_fS_", |
| 194 | }; |
| 195 | |
| 196 | for (auto Name : Names) { |
| 197 | // If we find a function with the matching name. |
| 198 | if (auto F = M.getFunction(Name)) { |
| 199 | SmallVector<Instruction *, 4> ToRemoves; |
| 200 | |
| 201 | // Walk the users of the function. |
| 202 | for (auto &U : F->uses()) { |
| 203 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 204 | auto Div = BinaryOperator::Create( |
| 205 | Instruction::FDiv, CI->getOperand(0), CI->getOperand(1), "", CI); |
| 206 | |
| 207 | CI->replaceAllUsesWith(Div); |
| 208 | |
| 209 | // Lastly, remember to remove the user. |
| 210 | ToRemoves.push_back(CI); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | Changed = !ToRemoves.empty(); |
| 215 | |
| 216 | // And cleanup the calls we don't use anymore. |
| 217 | for (auto V : ToRemoves) { |
| 218 | V->eraseFromParent(); |
| 219 | } |
| 220 | |
| 221 | // And remove the function we don't need either too. |
| 222 | F->eraseFromParent(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return Changed; |
| 227 | } |
| 228 | |
| 229 | bool ReplaceOpenCLBuiltinPass::replaceExp10(Module &M) { |
| 230 | bool Changed = false; |
| 231 | |
| 232 | const std::map<const char *, const char *> Map = { |
| 233 | {"_Z5exp10f", "_Z3expf"}, |
| 234 | {"_Z10half_exp10f", "_Z8half_expf"}, |
| 235 | {"_Z12native_exp10f", "_Z10native_expf"}, |
| 236 | {"_Z5exp10Dv2_f", "_Z3expDv2_f"}, |
| 237 | {"_Z10half_exp10Dv2_f", "_Z8half_expDv2_f"}, |
| 238 | {"_Z12native_exp10Dv2_f", "_Z10native_expDv2_f"}, |
| 239 | {"_Z5exp10Dv3_f", "_Z3expDv3_f"}, |
| 240 | {"_Z10half_exp10Dv3_f", "_Z8half_expDv3_f"}, |
| 241 | {"_Z12native_exp10Dv3_f", "_Z10native_expDv3_f"}, |
| 242 | {"_Z5exp10Dv4_f", "_Z3expDv4_f"}, |
| 243 | {"_Z10half_exp10Dv4_f", "_Z8half_expDv4_f"}, |
| 244 | {"_Z12native_exp10Dv4_f", "_Z10native_expDv4_f"}}; |
| 245 | |
| 246 | for (auto Pair : Map) { |
| 247 | // If we find a function with the matching name. |
| 248 | if (auto F = M.getFunction(Pair.first)) { |
| 249 | SmallVector<Instruction *, 4> ToRemoves; |
| 250 | |
| 251 | // Walk the users of the function. |
| 252 | for (auto &U : F->uses()) { |
| 253 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 254 | auto NewF = M.getOrInsertFunction(Pair.second, F->getFunctionType()); |
| 255 | |
| 256 | auto Arg = CI->getOperand(0); |
| 257 | |
| 258 | // Constant of the natural log of 10 (ln(10)). |
| 259 | const double Ln10 = |
| 260 | 2.302585092994045684017991454684364207601101488628772976033; |
| 261 | |
| 262 | auto Mul = BinaryOperator::Create( |
| 263 | Instruction::FMul, ConstantFP::get(Arg->getType(), Ln10), Arg, "", |
| 264 | CI); |
| 265 | |
| 266 | auto NewCI = CallInst::Create(NewF, Mul, "", CI); |
| 267 | |
| 268 | CI->replaceAllUsesWith(NewCI); |
| 269 | |
| 270 | // Lastly, remember to remove the user. |
| 271 | ToRemoves.push_back(CI); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | Changed = !ToRemoves.empty(); |
| 276 | |
| 277 | // And cleanup the calls we don't use anymore. |
| 278 | for (auto V : ToRemoves) { |
| 279 | V->eraseFromParent(); |
| 280 | } |
| 281 | |
| 282 | // And remove the function we don't need either too. |
| 283 | F->eraseFromParent(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return Changed; |
| 288 | } |
| 289 | |
| 290 | bool ReplaceOpenCLBuiltinPass::replaceLog10(Module &M) { |
| 291 | bool Changed = false; |
| 292 | |
| 293 | const std::map<const char *, const char *> Map = { |
| 294 | {"_Z5log10f", "_Z3logf"}, |
| 295 | {"_Z10half_log10f", "_Z8half_logf"}, |
| 296 | {"_Z12native_log10f", "_Z10native_logf"}, |
| 297 | {"_Z5log10Dv2_f", "_Z3logDv2_f"}, |
| 298 | {"_Z10half_log10Dv2_f", "_Z8half_logDv2_f"}, |
| 299 | {"_Z12native_log10Dv2_f", "_Z10native_logDv2_f"}, |
| 300 | {"_Z5log10Dv3_f", "_Z3logDv3_f"}, |
| 301 | {"_Z10half_log10Dv3_f", "_Z8half_logDv3_f"}, |
| 302 | {"_Z12native_log10Dv3_f", "_Z10native_logDv3_f"}, |
| 303 | {"_Z5log10Dv4_f", "_Z3logDv4_f"}, |
| 304 | {"_Z10half_log10Dv4_f", "_Z8half_logDv4_f"}, |
| 305 | {"_Z12native_log10Dv4_f", "_Z10native_logDv4_f"}}; |
| 306 | |
| 307 | for (auto Pair : Map) { |
| 308 | // If we find a function with the matching name. |
| 309 | if (auto F = M.getFunction(Pair.first)) { |
| 310 | SmallVector<Instruction *, 4> ToRemoves; |
| 311 | |
| 312 | // Walk the users of the function. |
| 313 | for (auto &U : F->uses()) { |
| 314 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 315 | auto NewF = M.getOrInsertFunction(Pair.second, F->getFunctionType()); |
| 316 | |
| 317 | auto Arg = CI->getOperand(0); |
| 318 | |
| 319 | // Constant of the reciprocal of the natural log of 10 (ln(10)). |
| 320 | const double Ln10 = |
| 321 | 0.434294481903251827651128918916605082294397005803666566114; |
| 322 | |
| 323 | auto NewCI = CallInst::Create(NewF, Arg, "", CI); |
| 324 | |
| 325 | auto Mul = BinaryOperator::Create( |
| 326 | Instruction::FMul, ConstantFP::get(Arg->getType(), Ln10), NewCI, |
| 327 | "", CI); |
| 328 | |
| 329 | CI->replaceAllUsesWith(Mul); |
| 330 | |
| 331 | // Lastly, remember to remove the user. |
| 332 | ToRemoves.push_back(CI); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | Changed = !ToRemoves.empty(); |
| 337 | |
| 338 | // And cleanup the calls we don't use anymore. |
| 339 | for (auto V : ToRemoves) { |
| 340 | V->eraseFromParent(); |
| 341 | } |
| 342 | |
| 343 | // And remove the function we don't need either too. |
| 344 | F->eraseFromParent(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return Changed; |
| 349 | } |
| 350 | |
| 351 | bool ReplaceOpenCLBuiltinPass::replaceBarrier(Module &M) { |
| 352 | bool Changed = false; |
| 353 | |
| 354 | enum { CLK_LOCAL_MEM_FENCE = 0x01, CLK_GLOBAL_MEM_FENCE = 0x02 }; |
| 355 | |
| 356 | const std::map<const char *, const char *> Map = { |
| 357 | {"_Z7barrierj", "__spirv_control_barrier"}}; |
| 358 | |
| 359 | for (auto Pair : Map) { |
| 360 | // If we find a function with the matching name. |
| 361 | if (auto F = M.getFunction(Pair.first)) { |
| 362 | SmallVector<Instruction *, 4> ToRemoves; |
| 363 | |
| 364 | // Walk the users of the function. |
| 365 | for (auto &U : F->uses()) { |
| 366 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 367 | auto FType = F->getFunctionType(); |
| 368 | SmallVector<Type *, 3> Params; |
| 369 | for (unsigned i = 0; i < 3; i++) { |
| 370 | Params.push_back(FType->getParamType(0)); |
| 371 | } |
| 372 | auto NewFType = |
| 373 | FunctionType::get(FType->getReturnType(), Params, false); |
| 374 | auto NewF = M.getOrInsertFunction(Pair.second, NewFType); |
| 375 | |
| 376 | auto Arg = CI->getOperand(0); |
| 377 | |
| 378 | // We need to map the OpenCL constants to the SPIR-V equivalents. |
| 379 | const auto LocalMemFence = |
| 380 | ConstantInt::get(Arg->getType(), CLK_LOCAL_MEM_FENCE); |
| 381 | const auto GlobalMemFence = |
| 382 | ConstantInt::get(Arg->getType(), CLK_GLOBAL_MEM_FENCE); |
| 383 | const auto ConstantSequentiallyConsistent = ConstantInt::get( |
| 384 | Arg->getType(), spv::MemorySemanticsSequentiallyConsistentMask); |
| 385 | const auto ConstantScopeDevice = |
| 386 | ConstantInt::get(Arg->getType(), spv::ScopeDevice); |
| 387 | const auto ConstantScopeWorkgroup = |
| 388 | ConstantInt::get(Arg->getType(), spv::ScopeWorkgroup); |
| 389 | |
| 390 | // Map CLK_LOCAL_MEM_FENCE to MemorySemanticsWorkgroupMemoryMask. |
| 391 | const auto LocalMemFenceMask = BinaryOperator::Create( |
| 392 | Instruction::And, LocalMemFence, Arg, "", CI); |
| 393 | const auto WorkgroupShiftAmount = |
| 394 | clz(spv::MemorySemanticsWorkgroupMemoryMask) - |
| 395 | clz(CLK_LOCAL_MEM_FENCE); |
| 396 | const auto MemorySemanticsWorkgroup = BinaryOperator::Create( |
| 397 | Instruction::Shl, LocalMemFenceMask, |
| 398 | ConstantInt::get(Arg->getType(), WorkgroupShiftAmount), "", CI); |
| 399 | |
| 400 | // Map CLK_GLOBAL_MEM_FENCE to MemorySemanticsUniformMemoryMask. |
| 401 | const auto GlobalMemFenceMask = BinaryOperator::Create( |
| 402 | Instruction::And, GlobalMemFence, Arg, "", CI); |
| 403 | const auto UniformShiftAmount = |
| 404 | clz(spv::MemorySemanticsUniformMemoryMask) - |
| 405 | clz(CLK_GLOBAL_MEM_FENCE); |
| 406 | const auto MemorySemanticsUniform = BinaryOperator::Create( |
| 407 | Instruction::Shl, GlobalMemFenceMask, |
| 408 | ConstantInt::get(Arg->getType(), UniformShiftAmount), "", CI); |
| 409 | |
| 410 | // And combine the above together, also adding in |
| 411 | // MemorySemanticsSequentiallyConsistentMask. |
| 412 | auto MemorySemantics = |
| 413 | BinaryOperator::Create(Instruction::Or, MemorySemanticsWorkgroup, |
| 414 | ConstantSequentiallyConsistent, "", CI); |
| 415 | MemorySemantics = BinaryOperator::Create( |
| 416 | Instruction::Or, MemorySemantics, MemorySemanticsUniform, "", CI); |
| 417 | |
| 418 | // For Memory Scope if we used CLK_GLOBAL_MEM_FENCE, we need to use |
| 419 | // Device Scope, otherwise Workgroup Scope. |
| 420 | const auto Cmp = |
| 421 | CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, |
| 422 | GlobalMemFenceMask, GlobalMemFence, "", CI); |
| 423 | const auto MemoryScope = SelectInst::Create( |
| 424 | Cmp, ConstantScopeDevice, ConstantScopeWorkgroup, "", CI); |
| 425 | |
| 426 | // Lastly, the Execution Scope is always Workgroup Scope. |
| 427 | const auto ExecutionScope = ConstantScopeWorkgroup; |
| 428 | |
| 429 | auto NewCI = CallInst::Create( |
| 430 | NewF, {ExecutionScope, MemoryScope, MemorySemantics}, "", CI); |
| 431 | |
| 432 | CI->replaceAllUsesWith(NewCI); |
| 433 | |
| 434 | // Lastly, remember to remove the user. |
| 435 | ToRemoves.push_back(CI); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | Changed = !ToRemoves.empty(); |
| 440 | |
| 441 | // And cleanup the calls we don't use anymore. |
| 442 | for (auto V : ToRemoves) { |
| 443 | V->eraseFromParent(); |
| 444 | } |
| 445 | |
| 446 | // And remove the function we don't need either too. |
| 447 | F->eraseFromParent(); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | return Changed; |
| 452 | } |
| 453 | |
| 454 | bool ReplaceOpenCLBuiltinPass::replaceMemFence(Module &M) { |
| 455 | bool Changed = false; |
| 456 | |
| 457 | enum { CLK_LOCAL_MEM_FENCE = 0x01, CLK_GLOBAL_MEM_FENCE = 0x02 }; |
| 458 | |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 459 | using Tuple = std::tuple<const char *, unsigned>; |
| 460 | const std::map<const char *, Tuple> Map = { |
| 461 | {"_Z9mem_fencej", |
| 462 | Tuple("__spirv_memory_barrier", |
| 463 | spv::MemorySemanticsSequentiallyConsistentMask)}, |
| 464 | {"_Z14read_mem_fencej", |
| 465 | Tuple("__spirv_memory_barrier", spv::MemorySemanticsAcquireMask)}, |
| 466 | {"_Z15write_mem_fencej", |
| 467 | Tuple("__spirv_memory_barrier", spv::MemorySemanticsReleaseMask)}}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 468 | |
| 469 | for (auto Pair : Map) { |
| 470 | // If we find a function with the matching name. |
| 471 | if (auto F = M.getFunction(Pair.first)) { |
| 472 | SmallVector<Instruction *, 4> ToRemoves; |
| 473 | |
| 474 | // Walk the users of the function. |
| 475 | for (auto &U : F->uses()) { |
| 476 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 477 | auto FType = F->getFunctionType(); |
| 478 | SmallVector<Type *, 2> Params; |
| 479 | for (unsigned i = 0; i < 2; i++) { |
| 480 | Params.push_back(FType->getParamType(0)); |
| 481 | } |
| 482 | auto NewFType = |
| 483 | FunctionType::get(FType->getReturnType(), Params, false); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 484 | auto NewF = M.getOrInsertFunction(std::get<0>(Pair.second), NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 485 | |
| 486 | auto Arg = CI->getOperand(0); |
| 487 | |
| 488 | // We need to map the OpenCL constants to the SPIR-V equivalents. |
| 489 | const auto LocalMemFence = |
| 490 | ConstantInt::get(Arg->getType(), CLK_LOCAL_MEM_FENCE); |
| 491 | const auto GlobalMemFence = |
| 492 | ConstantInt::get(Arg->getType(), CLK_GLOBAL_MEM_FENCE); |
| 493 | const auto ConstantMemorySemantics = |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 494 | ConstantInt::get(Arg->getType(), std::get<1>(Pair.second)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 495 | const auto ConstantScopeDevice = |
| 496 | ConstantInt::get(Arg->getType(), spv::ScopeDevice); |
| 497 | |
| 498 | // Map CLK_LOCAL_MEM_FENCE to MemorySemanticsWorkgroupMemoryMask. |
| 499 | const auto LocalMemFenceMask = BinaryOperator::Create( |
| 500 | Instruction::And, LocalMemFence, Arg, "", CI); |
| 501 | const auto WorkgroupShiftAmount = |
| 502 | clz(spv::MemorySemanticsWorkgroupMemoryMask) - |
| 503 | clz(CLK_LOCAL_MEM_FENCE); |
| 504 | const auto MemorySemanticsWorkgroup = BinaryOperator::Create( |
| 505 | Instruction::Shl, LocalMemFenceMask, |
| 506 | ConstantInt::get(Arg->getType(), WorkgroupShiftAmount), "", CI); |
| 507 | |
| 508 | // Map CLK_GLOBAL_MEM_FENCE to MemorySemanticsUniformMemoryMask. |
| 509 | const auto GlobalMemFenceMask = BinaryOperator::Create( |
| 510 | Instruction::And, GlobalMemFence, Arg, "", CI); |
| 511 | const auto UniformShiftAmount = |
| 512 | clz(spv::MemorySemanticsUniformMemoryMask) - |
| 513 | clz(CLK_GLOBAL_MEM_FENCE); |
| 514 | const auto MemorySemanticsUniform = BinaryOperator::Create( |
| 515 | Instruction::Shl, GlobalMemFenceMask, |
| 516 | ConstantInt::get(Arg->getType(), UniformShiftAmount), "", CI); |
| 517 | |
| 518 | // And combine the above together, also adding in |
| 519 | // MemorySemanticsSequentiallyConsistentMask. |
| 520 | auto MemorySemantics = |
| 521 | BinaryOperator::Create(Instruction::Or, MemorySemanticsWorkgroup, |
| 522 | ConstantMemorySemantics, "", CI); |
| 523 | MemorySemantics = BinaryOperator::Create( |
| 524 | Instruction::Or, MemorySemantics, MemorySemanticsUniform, "", CI); |
| 525 | |
| 526 | // Memory Scope is always device. |
| 527 | const auto MemoryScope = ConstantScopeDevice; |
| 528 | |
| 529 | auto NewCI = |
| 530 | CallInst::Create(NewF, {MemoryScope, MemorySemantics}, "", CI); |
| 531 | |
| 532 | CI->replaceAllUsesWith(NewCI); |
| 533 | |
| 534 | // Lastly, remember to remove the user. |
| 535 | ToRemoves.push_back(CI); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | Changed = !ToRemoves.empty(); |
| 540 | |
| 541 | // And cleanup the calls we don't use anymore. |
| 542 | for (auto V : ToRemoves) { |
| 543 | V->eraseFromParent(); |
| 544 | } |
| 545 | |
| 546 | // And remove the function we don't need either too. |
| 547 | F->eraseFromParent(); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return Changed; |
| 552 | } |
| 553 | |
| 554 | bool ReplaceOpenCLBuiltinPass::replaceRelational(Module &M) { |
| 555 | bool Changed = false; |
| 556 | |
| 557 | const std::map<const char *, std::pair<CmpInst::Predicate, int32_t>> Map = { |
| 558 | {"_Z7isequalff", {CmpInst::FCMP_OEQ, 1}}, |
| 559 | {"_Z7isequalDv2_fS_", {CmpInst::FCMP_OEQ, -1}}, |
| 560 | {"_Z7isequalDv3_fS_", {CmpInst::FCMP_OEQ, -1}}, |
| 561 | {"_Z7isequalDv4_fS_", {CmpInst::FCMP_OEQ, -1}}, |
| 562 | {"_Z9isgreaterff", {CmpInst::FCMP_OGT, 1}}, |
| 563 | {"_Z9isgreaterDv2_fS_", {CmpInst::FCMP_OGT, -1}}, |
| 564 | {"_Z9isgreaterDv3_fS_", {CmpInst::FCMP_OGT, -1}}, |
| 565 | {"_Z9isgreaterDv4_fS_", {CmpInst::FCMP_OGT, -1}}, |
| 566 | {"_Z14isgreaterequalff", {CmpInst::FCMP_OGE, 1}}, |
| 567 | {"_Z14isgreaterequalDv2_fS_", {CmpInst::FCMP_OGE, -1}}, |
| 568 | {"_Z14isgreaterequalDv3_fS_", {CmpInst::FCMP_OGE, -1}}, |
| 569 | {"_Z14isgreaterequalDv4_fS_", {CmpInst::FCMP_OGE, -1}}, |
| 570 | {"_Z6islessff", {CmpInst::FCMP_OLT, 1}}, |
| 571 | {"_Z6islessDv2_fS_", {CmpInst::FCMP_OLT, -1}}, |
| 572 | {"_Z6islessDv3_fS_", {CmpInst::FCMP_OLT, -1}}, |
| 573 | {"_Z6islessDv4_fS_", {CmpInst::FCMP_OLT, -1}}, |
| 574 | {"_Z11islessequalff", {CmpInst::FCMP_OLE, 1}}, |
| 575 | {"_Z11islessequalDv2_fS_", {CmpInst::FCMP_OLE, -1}}, |
| 576 | {"_Z11islessequalDv3_fS_", {CmpInst::FCMP_OLE, -1}}, |
| 577 | {"_Z11islessequalDv4_fS_", {CmpInst::FCMP_OLE, -1}}, |
| 578 | {"_Z10isnotequalff", {CmpInst::FCMP_ONE, 1}}, |
| 579 | {"_Z10isnotequalDv2_fS_", {CmpInst::FCMP_ONE, -1}}, |
| 580 | {"_Z10isnotequalDv3_fS_", {CmpInst::FCMP_ONE, -1}}, |
| 581 | {"_Z10isnotequalDv4_fS_", {CmpInst::FCMP_ONE, -1}}, |
| 582 | }; |
| 583 | |
| 584 | for (auto Pair : Map) { |
| 585 | // If we find a function with the matching name. |
| 586 | if (auto F = M.getFunction(Pair.first)) { |
| 587 | SmallVector<Instruction *, 4> ToRemoves; |
| 588 | |
| 589 | // Walk the users of the function. |
| 590 | for (auto &U : F->uses()) { |
| 591 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 592 | // The predicate to use in the CmpInst. |
| 593 | auto Predicate = Pair.second.first; |
| 594 | |
| 595 | // The value to return for true. |
| 596 | auto TrueValue = |
| 597 | ConstantInt::getSigned(CI->getType(), Pair.second.second); |
| 598 | |
| 599 | // The value to return for false. |
| 600 | auto FalseValue = Constant::getNullValue(CI->getType()); |
| 601 | |
| 602 | auto Arg1 = CI->getOperand(0); |
| 603 | auto Arg2 = CI->getOperand(1); |
| 604 | |
| 605 | const auto Cmp = |
| 606 | CmpInst::Create(Instruction::FCmp, Predicate, Arg1, Arg2, "", CI); |
| 607 | |
| 608 | const auto Select = |
| 609 | SelectInst::Create(Cmp, TrueValue, FalseValue, "", CI); |
| 610 | |
| 611 | CI->replaceAllUsesWith(Select); |
| 612 | |
| 613 | // Lastly, remember to remove the user. |
| 614 | ToRemoves.push_back(CI); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | Changed = !ToRemoves.empty(); |
| 619 | |
| 620 | // And cleanup the calls we don't use anymore. |
| 621 | for (auto V : ToRemoves) { |
| 622 | V->eraseFromParent(); |
| 623 | } |
| 624 | |
| 625 | // And remove the function we don't need either too. |
| 626 | F->eraseFromParent(); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | return Changed; |
| 631 | } |
| 632 | |
| 633 | bool ReplaceOpenCLBuiltinPass::replaceIsInfAndIsNan(Module &M) { |
| 634 | bool Changed = false; |
| 635 | |
| 636 | const std::map<const char *, std::pair<const char *, int32_t>> Map = { |
| 637 | {"_Z5isinff", {"__spirv_isinff", 1}}, |
| 638 | {"_Z5isinfDv2_f", {"__spirv_isinfDv2_f", -1}}, |
| 639 | {"_Z5isinfDv3_f", {"__spirv_isinfDv3_f", -1}}, |
| 640 | {"_Z5isinfDv4_f", {"__spirv_isinfDv4_f", -1}}, |
| 641 | {"_Z5isnanf", {"__spirv_isnanf", 1}}, |
| 642 | {"_Z5isnanDv2_f", {"__spirv_isnanDv2_f", -1}}, |
| 643 | {"_Z5isnanDv3_f", {"__spirv_isnanDv3_f", -1}}, |
| 644 | {"_Z5isnanDv4_f", {"__spirv_isnanDv4_f", -1}}, |
| 645 | }; |
| 646 | |
| 647 | for (auto Pair : Map) { |
| 648 | // If we find a function with the matching name. |
| 649 | if (auto F = M.getFunction(Pair.first)) { |
| 650 | SmallVector<Instruction *, 4> ToRemoves; |
| 651 | |
| 652 | // Walk the users of the function. |
| 653 | for (auto &U : F->uses()) { |
| 654 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 655 | const auto CITy = CI->getType(); |
| 656 | |
| 657 | // The fake SPIR-V intrinsic to generate. |
| 658 | auto SPIRVIntrinsic = Pair.second.first; |
| 659 | |
| 660 | // The value to return for true. |
| 661 | auto TrueValue = ConstantInt::getSigned(CITy, Pair.second.second); |
| 662 | |
| 663 | // The value to return for false. |
| 664 | auto FalseValue = Constant::getNullValue(CITy); |
| 665 | |
| 666 | const auto CorrespondingBoolTy = getBoolOrBoolVectorTy( |
| 667 | M.getContext(), |
| 668 | CITy->isVectorTy() ? CITy->getVectorNumElements() : 1); |
| 669 | |
| 670 | auto NewFType = |
| 671 | FunctionType::get(CorrespondingBoolTy, |
| 672 | F->getFunctionType()->getParamType(0), false); |
| 673 | |
| 674 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 675 | |
| 676 | auto Arg = CI->getOperand(0); |
| 677 | |
| 678 | auto NewCI = CallInst::Create(NewF, Arg, "", CI); |
| 679 | |
| 680 | const auto Select = |
| 681 | SelectInst::Create(NewCI, TrueValue, FalseValue, "", CI); |
| 682 | |
| 683 | CI->replaceAllUsesWith(Select); |
| 684 | |
| 685 | // Lastly, remember to remove the user. |
| 686 | ToRemoves.push_back(CI); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | Changed = !ToRemoves.empty(); |
| 691 | |
| 692 | // And cleanup the calls we don't use anymore. |
| 693 | for (auto V : ToRemoves) { |
| 694 | V->eraseFromParent(); |
| 695 | } |
| 696 | |
| 697 | // And remove the function we don't need either too. |
| 698 | F->eraseFromParent(); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return Changed; |
| 703 | } |
| 704 | |
| 705 | bool ReplaceOpenCLBuiltinPass::replaceAllAndAny(Module &M) { |
| 706 | bool Changed = false; |
| 707 | |
| 708 | const std::map<const char *, const char *> Map = { |
| 709 | {"_Z3alli", ""}, |
| 710 | {"_Z3allDv2_i", "__spirv_allDv2_i"}, |
| 711 | {"_Z3allDv3_i", "__spirv_allDv3_i"}, |
| 712 | {"_Z3allDv4_i", "__spirv_allDv4_i"}, |
| 713 | {"_Z3anyi", ""}, |
| 714 | {"_Z3anyDv2_i", "__spirv_anyDv2_i"}, |
| 715 | {"_Z3anyDv3_i", "__spirv_anyDv3_i"}, |
| 716 | {"_Z3anyDv4_i", "__spirv_anyDv4_i"}, |
| 717 | }; |
| 718 | |
| 719 | for (auto Pair : Map) { |
| 720 | // If we find a function with the matching name. |
| 721 | if (auto F = M.getFunction(Pair.first)) { |
| 722 | SmallVector<Instruction *, 4> ToRemoves; |
| 723 | |
| 724 | // Walk the users of the function. |
| 725 | for (auto &U : F->uses()) { |
| 726 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 727 | // The fake SPIR-V intrinsic to generate. |
| 728 | auto SPIRVIntrinsic = Pair.second; |
| 729 | |
| 730 | auto Arg = CI->getOperand(0); |
| 731 | |
| 732 | Value *V; |
| 733 | |
| 734 | // If we have a function to call, call it! |
| 735 | if (0 < strlen(SPIRVIntrinsic)) { |
| 736 | // The value for zero to compare against. |
| 737 | const auto ZeroValue = Constant::getNullValue(Arg->getType()); |
| 738 | |
| 739 | const auto Cmp = CmpInst::Create( |
| 740 | Instruction::ICmp, CmpInst::ICMP_SLT, Arg, ZeroValue, "", CI); |
| 741 | const auto NewFType = FunctionType::get( |
| 742 | Type::getInt1Ty(M.getContext()), Cmp->getType(), false); |
| 743 | |
| 744 | const auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 745 | |
| 746 | const auto NewCI = CallInst::Create(NewF, Cmp, "", CI); |
| 747 | |
| 748 | // The value to return for true. |
| 749 | const auto TrueValue = ConstantInt::get(CI->getType(), 1); |
| 750 | |
| 751 | // The value to return for false. |
| 752 | const auto FalseValue = Constant::getNullValue(CI->getType()); |
| 753 | |
| 754 | V = SelectInst::Create(NewCI, TrueValue, FalseValue, "", CI); |
| 755 | } else { |
| 756 | V = BinaryOperator::Create(Instruction::LShr, Arg, |
| 757 | ConstantInt::get(CI->getType(), 31), "", |
| 758 | CI); |
| 759 | } |
| 760 | |
| 761 | CI->replaceAllUsesWith(V); |
| 762 | |
| 763 | // Lastly, remember to remove the user. |
| 764 | ToRemoves.push_back(CI); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | Changed = !ToRemoves.empty(); |
| 769 | |
| 770 | // And cleanup the calls we don't use anymore. |
| 771 | for (auto V : ToRemoves) { |
| 772 | V->eraseFromParent(); |
| 773 | } |
| 774 | |
| 775 | // And remove the function we don't need either too. |
| 776 | F->eraseFromParent(); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | return Changed; |
| 781 | } |
| 782 | |
| 783 | bool ReplaceOpenCLBuiltinPass::replaceSignbit(Module &M) { |
| 784 | bool Changed = false; |
| 785 | |
| 786 | const std::map<const char *, Instruction::BinaryOps> Map = { |
| 787 | {"_Z7signbitf", Instruction::LShr}, |
| 788 | {"_Z7signbitDv2_f", Instruction::AShr}, |
| 789 | {"_Z7signbitDv3_f", Instruction::AShr}, |
| 790 | {"_Z7signbitDv4_f", Instruction::AShr}, |
| 791 | }; |
| 792 | |
| 793 | for (auto Pair : Map) { |
| 794 | // If we find a function with the matching name. |
| 795 | if (auto F = M.getFunction(Pair.first)) { |
| 796 | SmallVector<Instruction *, 4> ToRemoves; |
| 797 | |
| 798 | // Walk the users of the function. |
| 799 | for (auto &U : F->uses()) { |
| 800 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 801 | auto Arg = CI->getOperand(0); |
| 802 | |
| 803 | auto Bitcast = |
| 804 | CastInst::CreateZExtOrBitCast(Arg, CI->getType(), "", CI); |
| 805 | |
| 806 | auto Shr = BinaryOperator::Create(Pair.second, Bitcast, |
| 807 | ConstantInt::get(CI->getType(), 31), |
| 808 | "", CI); |
| 809 | |
| 810 | CI->replaceAllUsesWith(Shr); |
| 811 | |
| 812 | // Lastly, remember to remove the user. |
| 813 | ToRemoves.push_back(CI); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | Changed = !ToRemoves.empty(); |
| 818 | |
| 819 | // And cleanup the calls we don't use anymore. |
| 820 | for (auto V : ToRemoves) { |
| 821 | V->eraseFromParent(); |
| 822 | } |
| 823 | |
| 824 | // And remove the function we don't need either too. |
| 825 | F->eraseFromParent(); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return Changed; |
| 830 | } |
| 831 | |
| 832 | bool ReplaceOpenCLBuiltinPass::replaceMadandMad24andMul24(Module &M) { |
| 833 | bool Changed = false; |
| 834 | |
| 835 | const std::map<const char *, |
| 836 | std::pair<Instruction::BinaryOps, Instruction::BinaryOps>> |
| 837 | Map = { |
| 838 | {"_Z3madfff", {Instruction::FMul, Instruction::FAdd}}, |
| 839 | {"_Z3madDv2_fS_S_", {Instruction::FMul, Instruction::FAdd}}, |
| 840 | {"_Z3madDv3_fS_S_", {Instruction::FMul, Instruction::FAdd}}, |
| 841 | {"_Z3madDv4_fS_S_", {Instruction::FMul, Instruction::FAdd}}, |
| 842 | {"_Z5mad24iii", {Instruction::Mul, Instruction::Add}}, |
| 843 | {"_Z5mad24Dv2_iS_S_", {Instruction::Mul, Instruction::Add}}, |
| 844 | {"_Z5mad24Dv3_iS_S_", {Instruction::Mul, Instruction::Add}}, |
| 845 | {"_Z5mad24Dv4_iS_S_", {Instruction::Mul, Instruction::Add}}, |
| 846 | {"_Z5mad24jjj", {Instruction::Mul, Instruction::Add}}, |
| 847 | {"_Z5mad24Dv2_jS_S_", {Instruction::Mul, Instruction::Add}}, |
| 848 | {"_Z5mad24Dv3_jS_S_", {Instruction::Mul, Instruction::Add}}, |
| 849 | {"_Z5mad24Dv4_jS_S_", {Instruction::Mul, Instruction::Add}}, |
| 850 | {"_Z5mul24ii", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 851 | {"_Z5mul24Dv2_iS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 852 | {"_Z5mul24Dv3_iS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 853 | {"_Z5mul24Dv4_iS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 854 | {"_Z5mul24jj", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 855 | {"_Z5mul24Dv2_jS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 856 | {"_Z5mul24Dv3_jS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 857 | {"_Z5mul24Dv4_jS_", {Instruction::Mul, Instruction::BinaryOpsEnd}}, |
| 858 | }; |
| 859 | |
| 860 | for (auto Pair : Map) { |
| 861 | // If we find a function with the matching name. |
| 862 | if (auto F = M.getFunction(Pair.first)) { |
| 863 | SmallVector<Instruction *, 4> ToRemoves; |
| 864 | |
| 865 | // Walk the users of the function. |
| 866 | for (auto &U : F->uses()) { |
| 867 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 868 | // The multiply instruction to use. |
| 869 | auto MulInst = Pair.second.first; |
| 870 | |
| 871 | // The add instruction to use. |
| 872 | auto AddInst = Pair.second.second; |
| 873 | |
| 874 | SmallVector<Value *, 8> Args(CI->arg_begin(), CI->arg_end()); |
| 875 | |
| 876 | auto I = BinaryOperator::Create(MulInst, CI->getArgOperand(0), |
| 877 | CI->getArgOperand(1), "", CI); |
| 878 | |
| 879 | if (Instruction::BinaryOpsEnd != AddInst) { |
| 880 | I = BinaryOperator::Create(AddInst, I, CI->getArgOperand(2), "", |
| 881 | CI); |
| 882 | } |
| 883 | |
| 884 | CI->replaceAllUsesWith(I); |
| 885 | |
| 886 | // Lastly, remember to remove the user. |
| 887 | ToRemoves.push_back(CI); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | Changed = !ToRemoves.empty(); |
| 892 | |
| 893 | // And cleanup the calls we don't use anymore. |
| 894 | for (auto V : ToRemoves) { |
| 895 | V->eraseFromParent(); |
| 896 | } |
| 897 | |
| 898 | // And remove the function we don't need either too. |
| 899 | F->eraseFromParent(); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | return Changed; |
| 904 | } |
| 905 | |
| 906 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf(Module &M) { |
| 907 | bool Changed = false; |
| 908 | |
| 909 | const std::vector<const char *> Map = {"_Z10vload_halfjPU3AS1KDh", |
| 910 | "_Z10vload_halfjPU3AS2KDh"}; |
| 911 | |
| 912 | for (auto Name : Map) { |
| 913 | // If we find a function with the matching name. |
| 914 | if (auto F = M.getFunction(Name)) { |
| 915 | SmallVector<Instruction *, 4> ToRemoves; |
| 916 | |
| 917 | // Walk the users of the function. |
| 918 | for (auto &U : F->uses()) { |
| 919 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 920 | // The index argument from vload_half. |
| 921 | auto Arg0 = CI->getOperand(0); |
| 922 | |
| 923 | // The pointer argument from vload_half. |
| 924 | auto Arg1 = CI->getOperand(1); |
| 925 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 926 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 927 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 928 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
| 929 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 930 | // Our intrinsic to unpack a float2 from an int. |
| 931 | auto SPIRVIntrinsic = "spirv.unpack.v2f16"; |
| 932 | |
| 933 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 934 | |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 935 | if (f16bit_storage) { |
| 936 | auto ShortTy = Type::getInt16Ty(M.getContext()); |
| 937 | auto ShortPointerTy = PointerType::get( |
| 938 | ShortTy, Arg1->getType()->getPointerAddressSpace()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 939 | |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 940 | // Cast the half* pointer to short*. |
| 941 | auto Cast = |
| 942 | CastInst::CreatePointerCast(Arg1, ShortPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 943 | |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 944 | // Index into the correct address of the casted pointer. |
| 945 | auto Index = GetElementPtrInst::Create(ShortTy, Cast, Arg0, "", CI); |
| 946 | |
| 947 | // Load from the short* we casted to. |
| 948 | auto Load = new LoadInst(Index, "", CI); |
| 949 | |
| 950 | // ZExt the short -> int. |
| 951 | auto ZExt = CastInst::CreateZExtOrBitCast(Load, IntTy, "", CI); |
| 952 | |
| 953 | // Get our float2. |
| 954 | auto Call = CallInst::Create(NewF, ZExt, "", CI); |
| 955 | |
| 956 | // Extract out the bottom element which is our float result. |
| 957 | auto Extract = ExtractElementInst::Create( |
| 958 | Call, ConstantInt::get(IntTy, 0), "", CI); |
| 959 | |
| 960 | CI->replaceAllUsesWith(Extract); |
| 961 | } else { |
| 962 | // Assume the pointer argument points to storage aligned to 32bits |
| 963 | // or more. |
| 964 | // TODO(dneto): Do more analysis to make sure this is true? |
| 965 | // |
| 966 | // Replace call vstore_half(i32 %index, half addrspace(1) %base) |
| 967 | // with: |
| 968 | // |
| 969 | // %base_i32_ptr = bitcast half addrspace(1)* %base to i32 |
| 970 | // addrspace(1)* %index_is_odd32 = and i32 %index, 1 %index_i32 = |
| 971 | // lshr i32 %index, 1 %in_ptr = getlementptr i32, i32 |
| 972 | // addrspace(1)* %base_i32_ptr, %index_i32 %value_i32 = load i32, |
| 973 | // i32 addrspace(1)* %in_ptr %converted = call <2 x float> |
| 974 | // @spirv.unpack.v2f16(i32 %value_i32) %value = extractelement <2 |
| 975 | // x float> %converted, %index_is_odd32 |
| 976 | |
| 977 | auto IntPointerTy = PointerType::get( |
| 978 | IntTy, Arg1->getType()->getPointerAddressSpace()); |
| 979 | |
David Neto | 973e6a8 | 2017-05-30 13:48:18 -0400 | [diff] [blame] | 980 | // Cast the base pointer to int*. |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 981 | // In a valid call (according to assumptions), this should get |
David Neto | 973e6a8 | 2017-05-30 13:48:18 -0400 | [diff] [blame] | 982 | // optimized away in the simplify GEP pass. |
David Neto | ac825b8 | 2017-05-30 12:49:01 -0400 | [diff] [blame] | 983 | auto Cast = CastInst::CreatePointerCast(Arg1, IntPointerTy, "", CI); |
| 984 | |
| 985 | auto One = ConstantInt::get(IntTy, 1); |
| 986 | auto IndexIsOdd = BinaryOperator::CreateAnd(Arg0, One, "", CI); |
| 987 | auto IndexIntoI32 = BinaryOperator::CreateLShr(Arg0, One, "", CI); |
| 988 | |
| 989 | // Index into the correct address of the casted pointer. |
| 990 | auto Ptr = |
| 991 | GetElementPtrInst::Create(IntTy, Cast, IndexIntoI32, "", CI); |
| 992 | |
| 993 | // Load from the int* we casted to. |
| 994 | auto Load = new LoadInst(Ptr, "", CI); |
| 995 | |
| 996 | // Get our float2. |
| 997 | auto Call = CallInst::Create(NewF, Load, "", CI); |
| 998 | |
| 999 | // Extract out the float result, where the element number is |
| 1000 | // determined by whether the original index was even or odd. |
| 1001 | auto Extract = ExtractElementInst::Create(Call, IndexIsOdd, "", CI); |
| 1002 | |
| 1003 | CI->replaceAllUsesWith(Extract); |
| 1004 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1005 | |
| 1006 | // Lastly, remember to remove the user. |
| 1007 | ToRemoves.push_back(CI); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | Changed = !ToRemoves.empty(); |
| 1012 | |
| 1013 | // And cleanup the calls we don't use anymore. |
| 1014 | for (auto V : ToRemoves) { |
| 1015 | V->eraseFromParent(); |
| 1016 | } |
| 1017 | |
| 1018 | // And remove the function we don't need either too. |
| 1019 | F->eraseFromParent(); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | return Changed; |
| 1024 | } |
| 1025 | |
| 1026 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf2(Module &M) { |
| 1027 | bool Changed = false; |
| 1028 | |
| 1029 | const std::vector<const char *> Map = {"_Z11vload_half2jPU3AS1KDh", |
| 1030 | "_Z11vload_half2jPU3AS2KDh"}; |
| 1031 | |
| 1032 | for (auto Name : Map) { |
| 1033 | // If we find a function with the matching name. |
| 1034 | if (auto F = M.getFunction(Name)) { |
| 1035 | SmallVector<Instruction *, 4> ToRemoves; |
| 1036 | |
| 1037 | // Walk the users of the function. |
| 1038 | for (auto &U : F->uses()) { |
| 1039 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1040 | // The index argument from vload_half. |
| 1041 | auto Arg0 = CI->getOperand(0); |
| 1042 | |
| 1043 | // The pointer argument from vload_half. |
| 1044 | auto Arg1 = CI->getOperand(1); |
| 1045 | |
| 1046 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 1047 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
| 1048 | auto NewPointerTy = PointerType::get( |
| 1049 | IntTy, Arg1->getType()->getPointerAddressSpace()); |
| 1050 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
| 1051 | |
| 1052 | // Cast the half* pointer to int*. |
| 1053 | auto Cast = CastInst::CreatePointerCast(Arg1, NewPointerTy, "", CI); |
| 1054 | |
| 1055 | // Index into the correct address of the casted pointer. |
| 1056 | auto Index = GetElementPtrInst::Create(IntTy, Cast, Arg0, "", CI); |
| 1057 | |
| 1058 | // Load from the int* we casted to. |
| 1059 | auto Load = new LoadInst(Index, "", CI); |
| 1060 | |
| 1061 | // Our intrinsic to unpack a float2 from an int. |
| 1062 | auto SPIRVIntrinsic = "spirv.unpack.v2f16"; |
| 1063 | |
| 1064 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 1065 | |
| 1066 | // Get our float2. |
| 1067 | auto Call = CallInst::Create(NewF, Load, "", CI); |
| 1068 | |
| 1069 | CI->replaceAllUsesWith(Call); |
| 1070 | |
| 1071 | // Lastly, remember to remove the user. |
| 1072 | ToRemoves.push_back(CI); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | Changed = !ToRemoves.empty(); |
| 1077 | |
| 1078 | // And cleanup the calls we don't use anymore. |
| 1079 | for (auto V : ToRemoves) { |
| 1080 | V->eraseFromParent(); |
| 1081 | } |
| 1082 | |
| 1083 | // And remove the function we don't need either too. |
| 1084 | F->eraseFromParent(); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | return Changed; |
| 1089 | } |
| 1090 | |
| 1091 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf4(Module &M) { |
| 1092 | bool Changed = false; |
| 1093 | |
| 1094 | const std::vector<const char *> Map = {"_Z11vload_half4jPU3AS1KDh", |
| 1095 | "_Z11vload_half4jPU3AS2KDh"}; |
| 1096 | |
| 1097 | for (auto Name : Map) { |
| 1098 | // If we find a function with the matching name. |
| 1099 | if (auto F = M.getFunction(Name)) { |
| 1100 | SmallVector<Instruction *, 4> ToRemoves; |
| 1101 | |
| 1102 | // Walk the users of the function. |
| 1103 | for (auto &U : F->uses()) { |
| 1104 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1105 | // The index argument from vload_half. |
| 1106 | auto Arg0 = CI->getOperand(0); |
| 1107 | |
| 1108 | // The pointer argument from vload_half. |
| 1109 | auto Arg1 = CI->getOperand(1); |
| 1110 | |
| 1111 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 1112 | auto Int2Ty = VectorType::get(IntTy, 2); |
| 1113 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
| 1114 | auto NewPointerTy = PointerType::get( |
| 1115 | Int2Ty, Arg1->getType()->getPointerAddressSpace()); |
| 1116 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
| 1117 | |
| 1118 | // Cast the half* pointer to int2*. |
| 1119 | auto Cast = CastInst::CreatePointerCast(Arg1, NewPointerTy, "", CI); |
| 1120 | |
| 1121 | // Index into the correct address of the casted pointer. |
| 1122 | auto Index = GetElementPtrInst::Create(Int2Ty, Cast, Arg0, "", CI); |
| 1123 | |
| 1124 | // Load from the int2* we casted to. |
| 1125 | auto Load = new LoadInst(Index, "", CI); |
| 1126 | |
| 1127 | // Extract each element from the loaded int2. |
| 1128 | auto X = ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 0), |
| 1129 | "", CI); |
| 1130 | auto Y = ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 1), |
| 1131 | "", CI); |
| 1132 | |
| 1133 | // Our intrinsic to unpack a float2 from an int. |
| 1134 | auto SPIRVIntrinsic = "spirv.unpack.v2f16"; |
| 1135 | |
| 1136 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 1137 | |
| 1138 | // Get the lower (x & y) components of our final float4. |
| 1139 | auto Lo = CallInst::Create(NewF, X, "", CI); |
| 1140 | |
| 1141 | // Get the higher (z & w) components of our final float4. |
| 1142 | auto Hi = CallInst::Create(NewF, Y, "", CI); |
| 1143 | |
| 1144 | Constant *ShuffleMask[4] = { |
| 1145 | ConstantInt::get(IntTy, 0), ConstantInt::get(IntTy, 1), |
| 1146 | ConstantInt::get(IntTy, 2), ConstantInt::get(IntTy, 3)}; |
| 1147 | |
| 1148 | // Combine our two float2's into one float4. |
| 1149 | auto Combine = new ShuffleVectorInst( |
| 1150 | Lo, Hi, ConstantVector::get(ShuffleMask), "", CI); |
| 1151 | |
| 1152 | CI->replaceAllUsesWith(Combine); |
| 1153 | |
| 1154 | // Lastly, remember to remove the user. |
| 1155 | ToRemoves.push_back(CI); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | Changed = !ToRemoves.empty(); |
| 1160 | |
| 1161 | // And cleanup the calls we don't use anymore. |
| 1162 | for (auto V : ToRemoves) { |
| 1163 | V->eraseFromParent(); |
| 1164 | } |
| 1165 | |
| 1166 | // And remove the function we don't need either too. |
| 1167 | F->eraseFromParent(); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | return Changed; |
| 1172 | } |
| 1173 | |
| 1174 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf(Module &M) { |
| 1175 | bool Changed = false; |
| 1176 | |
| 1177 | const std::vector<const char *> Map = {"_Z11vstore_halffjPU3AS1Dh", |
| 1178 | "_Z15vstore_half_rtefjPU3AS1Dh", |
| 1179 | "_Z15vstore_half_rtzfjPU3AS1Dh"}; |
| 1180 | |
| 1181 | for (auto Name : Map) { |
| 1182 | // If we find a function with the matching name. |
| 1183 | if (auto F = M.getFunction(Name)) { |
| 1184 | SmallVector<Instruction *, 4> ToRemoves; |
| 1185 | |
| 1186 | // Walk the users of the function. |
| 1187 | for (auto &U : F->uses()) { |
| 1188 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1189 | // The value to store. |
| 1190 | auto Arg0 = CI->getOperand(0); |
| 1191 | |
| 1192 | // The index argument from vstore_half. |
| 1193 | auto Arg1 = CI->getOperand(1); |
| 1194 | |
| 1195 | // The pointer argument from vstore_half. |
| 1196 | auto Arg2 = CI->getOperand(2); |
| 1197 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1198 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 1199 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1200 | auto NewFType = FunctionType::get(IntTy, Float2Ty, false); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 1201 | auto One = ConstantInt::get(IntTy, 1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1202 | |
| 1203 | // Our intrinsic to pack a float2 to an int. |
| 1204 | auto SPIRVIntrinsic = "spirv.pack.v2f16"; |
| 1205 | |
| 1206 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 1207 | |
| 1208 | // Insert our value into a float2 so that we can pack it. |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 1209 | auto TempVec = |
| 1210 | InsertElementInst::Create(UndefValue::get(Float2Ty), Arg0, |
| 1211 | ConstantInt::get(IntTy, 0), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1212 | |
| 1213 | // Pack the float2 -> half2 (in an int). |
| 1214 | auto X = CallInst::Create(NewF, TempVec, "", CI); |
| 1215 | |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 1216 | if (f16bit_storage) { |
| 1217 | auto ShortTy = Type::getInt16Ty(M.getContext()); |
| 1218 | auto ShortPointerTy = PointerType::get( |
| 1219 | ShortTy, Arg2->getType()->getPointerAddressSpace()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1220 | |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 1221 | // Truncate our i32 to an i16. |
| 1222 | auto Trunc = CastInst::CreateTruncOrBitCast(X, ShortTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1223 | |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 1224 | // Cast the half* pointer to short*. |
| 1225 | auto Cast = CastInst::CreatePointerCast(Arg2, ShortPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1226 | |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 1227 | // Index into the correct address of the casted pointer. |
| 1228 | auto Index = GetElementPtrInst::Create(ShortTy, Cast, Arg1, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1229 | |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 1230 | // Store to the int* we casted to. |
| 1231 | auto Store = new StoreInst(Trunc, Index, CI); |
| 1232 | |
| 1233 | CI->replaceAllUsesWith(Store); |
| 1234 | } else { |
| 1235 | // We can only write to 32-bit aligned words. |
| 1236 | // |
| 1237 | // Assuming base is aligned to 32-bits, replace the equivalent of |
| 1238 | // vstore_half(value, index, base) |
| 1239 | // with: |
| 1240 | // uint32_t* target_ptr = (uint32_t*)(base) + index / 2; |
| 1241 | // uint32_t write_to_upper_half = index & 1u; |
| 1242 | // uint32_t shift = write_to_upper_half << 4; |
| 1243 | // |
| 1244 | // // Pack the float value as a half number in bottom 16 bits |
| 1245 | // // of an i32. |
| 1246 | // uint32_t packed = spirv.pack.v2f16((float2)(value, undef)); |
| 1247 | // |
| 1248 | // uint32_t xor_value = (*target_ptr & (0xffff << shift)) |
| 1249 | // ^ ((packed & 0xffff) << shift) |
| 1250 | // // We only need relaxed consistency, but OpenCL 1.2 only has |
| 1251 | // // sequentially consistent atomics. |
| 1252 | // // TODO(dneto): Use relaxed consistency. |
| 1253 | // atomic_xor(target_ptr, xor_value) |
| 1254 | auto IntPointerTy = PointerType::get( |
| 1255 | IntTy, Arg2->getType()->getPointerAddressSpace()); |
| 1256 | |
| 1257 | auto Four = ConstantInt::get(IntTy, 4); |
| 1258 | auto FFFF = ConstantInt::get(IntTy, 0xffff); |
| 1259 | |
| 1260 | auto IndexIsOdd = BinaryOperator::CreateAnd(Arg1, One, "index_is_odd_i32", CI); |
| 1261 | // Compute index / 2 |
| 1262 | auto IndexIntoI32 = BinaryOperator::CreateLShr(Arg1, One, "index_into_i32", CI); |
| 1263 | auto BaseI32Ptr = CastInst::CreatePointerCast(Arg2, IntPointerTy, "base_i32_ptr", CI); |
| 1264 | auto OutPtr = GetElementPtrInst::Create(IntTy, BaseI32Ptr, IndexIntoI32, "base_i32_ptr", CI); |
| 1265 | auto CurrentValue = new LoadInst(OutPtr, "current_value", CI); |
| 1266 | auto Shift = BinaryOperator::CreateShl(IndexIsOdd, Four, "shift", CI); |
| 1267 | auto MaskBitsToWrite = BinaryOperator::CreateShl(FFFF, Shift, "mask_bits_to_write", CI); |
| 1268 | auto MaskedCurrent = BinaryOperator::CreateAnd(MaskBitsToWrite, CurrentValue, "masked_current", CI); |
| 1269 | |
| 1270 | auto XLowerBits = BinaryOperator::CreateAnd(X, FFFF, "lower_bits_of_packed", CI); |
| 1271 | auto NewBitsToWrite = BinaryOperator::CreateShl(XLowerBits, Shift, "new_bits_to_write", CI); |
| 1272 | auto ValueToXor = BinaryOperator::CreateXor(MaskedCurrent, NewBitsToWrite, "value_to_xor", CI); |
| 1273 | |
| 1274 | // Generate the call to atomi_xor. |
| 1275 | SmallVector<Type *, 5> ParamTypes; |
| 1276 | // The pointer type. |
| 1277 | ParamTypes.push_back(IntPointerTy); |
| 1278 | // The Types for memory scope, semantics, and value. |
| 1279 | ParamTypes.push_back(IntTy); |
| 1280 | ParamTypes.push_back(IntTy); |
| 1281 | ParamTypes.push_back(IntTy); |
| 1282 | auto NewFType = FunctionType::get(IntTy, ParamTypes, false); |
| 1283 | auto NewF = M.getOrInsertFunction("spirv.atomic_xor", NewFType); |
| 1284 | |
| 1285 | const auto ConstantScopeDevice = |
| 1286 | ConstantInt::get(IntTy, spv::ScopeDevice); |
| 1287 | // Assume the pointee is in OpenCL global (SPIR-V Uniform) or local |
| 1288 | // (SPIR-V Workgroup). |
| 1289 | const auto AddrSpaceSemanticsBits = |
| 1290 | IntPointerTy->getPointerAddressSpace() == 1 |
| 1291 | ? spv::MemorySemanticsUniformMemoryMask |
| 1292 | : spv::MemorySemanticsWorkgroupMemoryMask; |
| 1293 | |
| 1294 | // We're using relaxed consistency here. |
| 1295 | const auto ConstantMemorySemantics = |
| 1296 | ConstantInt::get(IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 1297 | AddrSpaceSemanticsBits); |
| 1298 | |
| 1299 | SmallVector<Value *, 5> Params{OutPtr, ConstantScopeDevice, |
| 1300 | ConstantMemorySemantics, ValueToXor}; |
| 1301 | CallInst::Create(NewF, Params, "store_halfword_xor_trick", CI); |
| 1302 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1303 | |
| 1304 | // Lastly, remember to remove the user. |
| 1305 | ToRemoves.push_back(CI); |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | Changed = !ToRemoves.empty(); |
| 1310 | |
| 1311 | // And cleanup the calls we don't use anymore. |
| 1312 | for (auto V : ToRemoves) { |
| 1313 | V->eraseFromParent(); |
| 1314 | } |
| 1315 | |
| 1316 | // And remove the function we don't need either too. |
| 1317 | F->eraseFromParent(); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | return Changed; |
| 1322 | } |
| 1323 | |
| 1324 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf2(Module &M) { |
| 1325 | bool Changed = false; |
| 1326 | |
| 1327 | const std::vector<const char *> Map = {"_Z12vstore_half2Dv2_fjPU3AS1Dh", |
| 1328 | "_Z16vstore_half2_rteDv2_fjPU3AS1Dh", |
| 1329 | "_Z16vstore_half2_rtzDv2_fjPU3AS1Dh"}; |
| 1330 | |
| 1331 | for (auto Name : Map) { |
| 1332 | // If we find a function with the matching name. |
| 1333 | if (auto F = M.getFunction(Name)) { |
| 1334 | SmallVector<Instruction *, 4> ToRemoves; |
| 1335 | |
| 1336 | // Walk the users of the function. |
| 1337 | for (auto &U : F->uses()) { |
| 1338 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1339 | // The value to store. |
| 1340 | auto Arg0 = CI->getOperand(0); |
| 1341 | |
| 1342 | // The index argument from vstore_half. |
| 1343 | auto Arg1 = CI->getOperand(1); |
| 1344 | |
| 1345 | // The pointer argument from vstore_half. |
| 1346 | auto Arg2 = CI->getOperand(2); |
| 1347 | |
| 1348 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 1349 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
| 1350 | auto NewPointerTy = PointerType::get( |
| 1351 | IntTy, Arg2->getType()->getPointerAddressSpace()); |
| 1352 | auto NewFType = FunctionType::get(IntTy, Float2Ty, false); |
| 1353 | |
| 1354 | // Our intrinsic to pack a float2 to an int. |
| 1355 | auto SPIRVIntrinsic = "spirv.pack.v2f16"; |
| 1356 | |
| 1357 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 1358 | |
| 1359 | // Turn the packed x & y into the final packing. |
| 1360 | auto X = CallInst::Create(NewF, Arg0, "", CI); |
| 1361 | |
| 1362 | // Cast the half* pointer to int*. |
| 1363 | auto Cast = CastInst::CreatePointerCast(Arg2, NewPointerTy, "", CI); |
| 1364 | |
| 1365 | // Index into the correct address of the casted pointer. |
| 1366 | auto Index = GetElementPtrInst::Create(IntTy, Cast, Arg1, "", CI); |
| 1367 | |
| 1368 | // Store to the int* we casted to. |
| 1369 | auto Store = new StoreInst(X, Index, CI); |
| 1370 | |
| 1371 | CI->replaceAllUsesWith(Store); |
| 1372 | |
| 1373 | // Lastly, remember to remove the user. |
| 1374 | ToRemoves.push_back(CI); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | Changed = !ToRemoves.empty(); |
| 1379 | |
| 1380 | // And cleanup the calls we don't use anymore. |
| 1381 | for (auto V : ToRemoves) { |
| 1382 | V->eraseFromParent(); |
| 1383 | } |
| 1384 | |
| 1385 | // And remove the function we don't need either too. |
| 1386 | F->eraseFromParent(); |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | return Changed; |
| 1391 | } |
| 1392 | |
| 1393 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf4(Module &M) { |
| 1394 | bool Changed = false; |
| 1395 | |
| 1396 | const std::vector<const char *> Map = {"_Z12vstore_half4Dv4_fjPU3AS1Dh", |
| 1397 | "_Z16vstore_half4_rteDv4_fjPU3AS1Dh", |
| 1398 | "_Z16vstore_half4_rtzDv4_fjPU3AS1Dh"}; |
| 1399 | |
| 1400 | for (auto Name : Map) { |
| 1401 | // If we find a function with the matching name. |
| 1402 | if (auto F = M.getFunction(Name)) { |
| 1403 | SmallVector<Instruction *, 4> ToRemoves; |
| 1404 | |
| 1405 | // Walk the users of the function. |
| 1406 | for (auto &U : F->uses()) { |
| 1407 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1408 | // The value to store. |
| 1409 | auto Arg0 = CI->getOperand(0); |
| 1410 | |
| 1411 | // The index argument from vstore_half. |
| 1412 | auto Arg1 = CI->getOperand(1); |
| 1413 | |
| 1414 | // The pointer argument from vstore_half. |
| 1415 | auto Arg2 = CI->getOperand(2); |
| 1416 | |
| 1417 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 1418 | auto Int2Ty = VectorType::get(IntTy, 2); |
| 1419 | auto Float2Ty = VectorType::get(Type::getFloatTy(M.getContext()), 2); |
| 1420 | auto NewPointerTy = PointerType::get( |
| 1421 | Int2Ty, Arg2->getType()->getPointerAddressSpace()); |
| 1422 | auto NewFType = FunctionType::get(IntTy, Float2Ty, false); |
| 1423 | |
| 1424 | Constant *LoShuffleMask[2] = {ConstantInt::get(IntTy, 0), |
| 1425 | ConstantInt::get(IntTy, 1)}; |
| 1426 | |
| 1427 | // Extract out the x & y components of our to store value. |
| 1428 | auto Lo = |
| 1429 | new ShuffleVectorInst(Arg0, UndefValue::get(Arg0->getType()), |
| 1430 | ConstantVector::get(LoShuffleMask), "", CI); |
| 1431 | |
| 1432 | Constant *HiShuffleMask[2] = {ConstantInt::get(IntTy, 2), |
| 1433 | ConstantInt::get(IntTy, 3)}; |
| 1434 | |
| 1435 | // Extract out the z & w components of our to store value. |
| 1436 | auto Hi = |
| 1437 | new ShuffleVectorInst(Arg0, UndefValue::get(Arg0->getType()), |
| 1438 | ConstantVector::get(HiShuffleMask), "", CI); |
| 1439 | |
| 1440 | // Our intrinsic to pack a float2 to an int. |
| 1441 | auto SPIRVIntrinsic = "spirv.pack.v2f16"; |
| 1442 | |
| 1443 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 1444 | |
| 1445 | // Turn the packed x & y into the final component of our int2. |
| 1446 | auto X = CallInst::Create(NewF, Lo, "", CI); |
| 1447 | |
| 1448 | // Turn the packed z & w into the final component of our int2. |
| 1449 | auto Y = CallInst::Create(NewF, Hi, "", CI); |
| 1450 | |
| 1451 | auto Combine = InsertElementInst::Create( |
| 1452 | UndefValue::get(Int2Ty), X, ConstantInt::get(IntTy, 0), "", CI); |
| 1453 | Combine = InsertElementInst::Create( |
| 1454 | Combine, Y, ConstantInt::get(IntTy, 1), "", CI); |
| 1455 | |
| 1456 | // Cast the half* pointer to int2*. |
| 1457 | auto Cast = CastInst::CreatePointerCast(Arg2, NewPointerTy, "", CI); |
| 1458 | |
| 1459 | // Index into the correct address of the casted pointer. |
| 1460 | auto Index = GetElementPtrInst::Create(Int2Ty, Cast, Arg1, "", CI); |
| 1461 | |
| 1462 | // Store to the int2* we casted to. |
| 1463 | auto Store = new StoreInst(Combine, Index, CI); |
| 1464 | |
| 1465 | CI->replaceAllUsesWith(Store); |
| 1466 | |
| 1467 | // Lastly, remember to remove the user. |
| 1468 | ToRemoves.push_back(CI); |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | Changed = !ToRemoves.empty(); |
| 1473 | |
| 1474 | // And cleanup the calls we don't use anymore. |
| 1475 | for (auto V : ToRemoves) { |
| 1476 | V->eraseFromParent(); |
| 1477 | } |
| 1478 | |
| 1479 | // And remove the function we don't need either too. |
| 1480 | F->eraseFromParent(); |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | return Changed; |
| 1485 | } |
| 1486 | |
| 1487 | bool ReplaceOpenCLBuiltinPass::replaceReadImageF(Module &M) { |
| 1488 | bool Changed = false; |
| 1489 | |
| 1490 | const std::map<const char *, const char*> Map = { |
| 1491 | { "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_i", "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f" }, |
| 1492 | { "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv4_i", "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv4_f" } |
| 1493 | }; |
| 1494 | |
| 1495 | for (auto Pair : Map) { |
| 1496 | // If we find a function with the matching name. |
| 1497 | if (auto F = M.getFunction(Pair.first)) { |
| 1498 | SmallVector<Instruction *, 4> ToRemoves; |
| 1499 | |
| 1500 | // Walk the users of the function. |
| 1501 | for (auto &U : F->uses()) { |
| 1502 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1503 | // The image. |
| 1504 | auto Arg0 = CI->getOperand(0); |
| 1505 | |
| 1506 | // The sampler. |
| 1507 | auto Arg1 = CI->getOperand(1); |
| 1508 | |
| 1509 | // The coordinate (integer type that we can't handle). |
| 1510 | auto Arg2 = CI->getOperand(2); |
| 1511 | |
| 1512 | auto FloatVecTy = VectorType::get(Type::getFloatTy(M.getContext()), Arg2->getType()->getVectorNumElements()); |
| 1513 | |
| 1514 | auto NewFType = FunctionType::get(CI->getType(), {Arg0->getType(), Arg1->getType(), FloatVecTy}, false); |
| 1515 | |
| 1516 | auto NewF = M.getOrInsertFunction(Pair.second, NewFType); |
| 1517 | |
| 1518 | auto Cast = CastInst::Create(Instruction::SIToFP, Arg2, FloatVecTy, "", CI); |
| 1519 | |
| 1520 | auto NewCI = CallInst::Create(NewF, {Arg0, Arg1, Cast}, "", CI); |
| 1521 | |
| 1522 | CI->replaceAllUsesWith(NewCI); |
| 1523 | |
| 1524 | // Lastly, remember to remove the user. |
| 1525 | ToRemoves.push_back(CI); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | Changed = !ToRemoves.empty(); |
| 1530 | |
| 1531 | // And cleanup the calls we don't use anymore. |
| 1532 | for (auto V : ToRemoves) { |
| 1533 | V->eraseFromParent(); |
| 1534 | } |
| 1535 | |
| 1536 | // And remove the function we don't need either too. |
| 1537 | F->eraseFromParent(); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | return Changed; |
| 1542 | } |
| 1543 | |
| 1544 | bool ReplaceOpenCLBuiltinPass::replaceAtomics(Module &M) { |
| 1545 | bool Changed = false; |
| 1546 | |
| 1547 | const std::map<const char *, const char *> Map = { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1548 | {"_Z10atomic_incPU3AS1Vi", "spirv.atomic_inc"}, |
| 1549 | {"_Z10atomic_incPU3AS1Vj", "spirv.atomic_inc"}, |
| 1550 | {"_Z10atomic_decPU3AS1Vi", "spirv.atomic_dec"}, |
| 1551 | {"_Z10atomic_decPU3AS1Vj", "spirv.atomic_dec"}, |
| 1552 | {"_Z14atomic_cmpxchgPU3AS1Viii", "spirv.atomic_compare_exchange"}, |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 1553 | {"_Z14atomic_cmpxchgPU3AS1Vjjj", "spirv.atomic_compare_exchange"}}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1554 | |
| 1555 | for (auto Pair : Map) { |
| 1556 | // If we find a function with the matching name. |
| 1557 | if (auto F = M.getFunction(Pair.first)) { |
| 1558 | SmallVector<Instruction *, 4> ToRemoves; |
| 1559 | |
| 1560 | // Walk the users of the function. |
| 1561 | for (auto &U : F->uses()) { |
| 1562 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1563 | auto FType = F->getFunctionType(); |
| 1564 | SmallVector<Type *, 5> ParamTypes; |
| 1565 | |
| 1566 | // The pointer type. |
| 1567 | ParamTypes.push_back(FType->getParamType(0)); |
| 1568 | |
| 1569 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 1570 | |
| 1571 | // The memory scope type. |
| 1572 | ParamTypes.push_back(IntTy); |
| 1573 | |
| 1574 | // The memory semantics type. |
| 1575 | ParamTypes.push_back(IntTy); |
| 1576 | |
| 1577 | if (2 < CI->getNumArgOperands()) { |
| 1578 | // The unequal memory semantics type. |
| 1579 | ParamTypes.push_back(IntTy); |
| 1580 | |
| 1581 | // The value type. |
| 1582 | ParamTypes.push_back(FType->getParamType(2)); |
| 1583 | |
| 1584 | // The comparator type. |
| 1585 | ParamTypes.push_back(FType->getParamType(1)); |
| 1586 | } else if (1 < CI->getNumArgOperands()) { |
| 1587 | // The value type. |
| 1588 | ParamTypes.push_back(FType->getParamType(1)); |
| 1589 | } |
| 1590 | |
| 1591 | auto NewFType = |
| 1592 | FunctionType::get(FType->getReturnType(), ParamTypes, false); |
| 1593 | auto NewF = M.getOrInsertFunction(Pair.second, NewFType); |
| 1594 | |
| 1595 | // We need to map the OpenCL constants to the SPIR-V equivalents. |
| 1596 | const auto ConstantScopeDevice = |
| 1597 | ConstantInt::get(IntTy, spv::ScopeDevice); |
| 1598 | const auto ConstantMemorySemantics = ConstantInt::get( |
| 1599 | IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 1600 | spv::MemorySemanticsSequentiallyConsistentMask); |
| 1601 | |
| 1602 | SmallVector<Value *, 5> Params; |
| 1603 | |
| 1604 | // The pointer. |
| 1605 | Params.push_back(CI->getArgOperand(0)); |
| 1606 | |
| 1607 | // The memory scope. |
| 1608 | Params.push_back(ConstantScopeDevice); |
| 1609 | |
| 1610 | // The memory semantics. |
| 1611 | Params.push_back(ConstantMemorySemantics); |
| 1612 | |
| 1613 | if (2 < CI->getNumArgOperands()) { |
| 1614 | // The unequal memory semantics. |
| 1615 | Params.push_back(ConstantMemorySemantics); |
| 1616 | |
| 1617 | // The value. |
| 1618 | Params.push_back(CI->getArgOperand(2)); |
| 1619 | |
| 1620 | // The comparator. |
| 1621 | Params.push_back(CI->getArgOperand(1)); |
| 1622 | } else if (1 < CI->getNumArgOperands()) { |
| 1623 | // The value. |
| 1624 | Params.push_back(CI->getArgOperand(1)); |
| 1625 | } |
| 1626 | |
| 1627 | auto NewCI = CallInst::Create(NewF, Params, "", CI); |
| 1628 | |
| 1629 | CI->replaceAllUsesWith(NewCI); |
| 1630 | |
| 1631 | // Lastly, remember to remove the user. |
| 1632 | ToRemoves.push_back(CI); |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | Changed = !ToRemoves.empty(); |
| 1637 | |
| 1638 | // And cleanup the calls we don't use anymore. |
| 1639 | for (auto V : ToRemoves) { |
| 1640 | V->eraseFromParent(); |
| 1641 | } |
| 1642 | |
| 1643 | // And remove the function we don't need either too. |
| 1644 | F->eraseFromParent(); |
| 1645 | } |
| 1646 | } |
| 1647 | |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 1648 | const std::map<const char *, llvm::AtomicRMWInst::BinOp> Map2 = { |
| 1649 | {"_Z10atomic_addPU3AS1Vii", llvm::AtomicRMWInst::Add}, |
| 1650 | {"_Z10atomic_addPU3AS1Vjj", llvm::AtomicRMWInst::Add}, |
| 1651 | {"_Z10atomic_subPU3AS1Vii", llvm::AtomicRMWInst::Sub}, |
| 1652 | {"_Z10atomic_subPU3AS1Vjj", llvm::AtomicRMWInst::Sub}, |
| 1653 | {"_Z11atomic_xchgPU3AS1Vii", llvm::AtomicRMWInst::Xchg}, |
| 1654 | {"_Z11atomic_xchgPU3AS1Vjj", llvm::AtomicRMWInst::Xchg}, |
| 1655 | {"_Z10atomic_minPU3AS1Vii", llvm::AtomicRMWInst::Min}, |
| 1656 | {"_Z10atomic_minPU3AS1Vjj", llvm::AtomicRMWInst::UMin}, |
| 1657 | {"_Z10atomic_maxPU3AS1Vii", llvm::AtomicRMWInst::Max}, |
| 1658 | {"_Z10atomic_maxPU3AS1Vjj", llvm::AtomicRMWInst::UMax}, |
| 1659 | {"_Z10atomic_andPU3AS1Vii", llvm::AtomicRMWInst::And}, |
| 1660 | {"_Z10atomic_andPU3AS1Vjj", llvm::AtomicRMWInst::And}, |
| 1661 | {"_Z9atomic_orPU3AS1Vii", llvm::AtomicRMWInst::Or}, |
| 1662 | {"_Z9atomic_orPU3AS1Vjj", llvm::AtomicRMWInst::Or}, |
| 1663 | {"_Z10atomic_xorPU3AS1Vii", llvm::AtomicRMWInst::Xor}, |
| 1664 | {"_Z10atomic_xorPU3AS1Vjj", llvm::AtomicRMWInst::Xor}}; |
| 1665 | |
| 1666 | for (auto Pair : Map2) { |
| 1667 | // If we find a function with the matching name. |
| 1668 | if (auto F = M.getFunction(Pair.first)) { |
| 1669 | SmallVector<Instruction *, 4> ToRemoves; |
| 1670 | |
| 1671 | // Walk the users of the function. |
| 1672 | for (auto &U : F->uses()) { |
| 1673 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1674 | auto AtomicOp = new AtomicRMWInst( |
| 1675 | Pair.second, CI->getArgOperand(0), CI->getArgOperand(1), |
| 1676 | AtomicOrdering::SequentiallyConsistent, SyncScope::System, CI); |
| 1677 | |
| 1678 | CI->replaceAllUsesWith(AtomicOp); |
| 1679 | |
| 1680 | // Lastly, remember to remove the user. |
| 1681 | ToRemoves.push_back(CI); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | Changed = !ToRemoves.empty(); |
| 1686 | |
| 1687 | // And cleanup the calls we don't use anymore. |
| 1688 | for (auto V : ToRemoves) { |
| 1689 | V->eraseFromParent(); |
| 1690 | } |
| 1691 | |
| 1692 | // And remove the function we don't need either too. |
| 1693 | F->eraseFromParent(); |
| 1694 | } |
| 1695 | } |
| 1696 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1697 | return Changed; |
| 1698 | } |
| 1699 | |
| 1700 | bool ReplaceOpenCLBuiltinPass::replaceCross(Module &M) { |
| 1701 | bool Changed = false; |
| 1702 | |
| 1703 | // If we find a function with the matching name. |
| 1704 | if (auto F = M.getFunction("_Z5crossDv4_fS_")) { |
| 1705 | SmallVector<Instruction *, 4> ToRemoves; |
| 1706 | |
| 1707 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 1708 | auto FloatTy = Type::getFloatTy(M.getContext()); |
| 1709 | |
| 1710 | Constant *DownShuffleMask[3] = { |
| 1711 | ConstantInt::get(IntTy, 0), ConstantInt::get(IntTy, 1), |
| 1712 | ConstantInt::get(IntTy, 2)}; |
| 1713 | |
| 1714 | Constant *UpShuffleMask[4] = { |
| 1715 | ConstantInt::get(IntTy, 0), ConstantInt::get(IntTy, 1), |
| 1716 | ConstantInt::get(IntTy, 2), ConstantInt::get(IntTy, 3)}; |
| 1717 | |
| 1718 | Constant *FloatVec[3] = { |
| 1719 | ConstantFP::get(FloatTy, 0.0f), UndefValue::get(FloatTy), UndefValue::get(FloatTy) |
| 1720 | }; |
| 1721 | |
| 1722 | // Walk the users of the function. |
| 1723 | for (auto &U : F->uses()) { |
| 1724 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1725 | auto Vec4Ty = CI->getArgOperand(0)->getType(); |
| 1726 | auto Arg0 = new ShuffleVectorInst(CI->getArgOperand(0), UndefValue::get(Vec4Ty), ConstantVector::get(DownShuffleMask), "", CI); |
| 1727 | auto Arg1 = new ShuffleVectorInst(CI->getArgOperand(1), UndefValue::get(Vec4Ty), ConstantVector::get(DownShuffleMask), "", CI); |
| 1728 | auto Vec3Ty = Arg0->getType(); |
| 1729 | |
| 1730 | auto NewFType = |
| 1731 | FunctionType::get(Vec3Ty, {Vec3Ty, Vec3Ty}, false); |
| 1732 | |
| 1733 | auto Cross3Func = M.getOrInsertFunction("_Z5crossDv3_fS_", NewFType); |
| 1734 | |
| 1735 | auto DownResult = CallInst::Create(Cross3Func, {Arg0, Arg1}, "", CI); |
| 1736 | |
| 1737 | auto Result = new ShuffleVectorInst(DownResult, ConstantVector::get(FloatVec), ConstantVector::get(UpShuffleMask), "", CI); |
| 1738 | |
| 1739 | CI->replaceAllUsesWith(Result); |
| 1740 | |
| 1741 | // Lastly, remember to remove the user. |
| 1742 | ToRemoves.push_back(CI); |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | Changed = !ToRemoves.empty(); |
| 1747 | |
| 1748 | // And cleanup the calls we don't use anymore. |
| 1749 | for (auto V : ToRemoves) { |
| 1750 | V->eraseFromParent(); |
| 1751 | } |
| 1752 | |
| 1753 | // And remove the function we don't need either too. |
| 1754 | F->eraseFromParent(); |
| 1755 | } |
| 1756 | |
| 1757 | return Changed; |
| 1758 | } |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 1759 | |
| 1760 | bool ReplaceOpenCLBuiltinPass::replaceFract(Module &M) { |
| 1761 | bool Changed = false; |
| 1762 | |
| 1763 | // OpenCL's float result = fract(float x, float* ptr) |
| 1764 | // |
| 1765 | // In the LLVM domain: |
| 1766 | // |
| 1767 | // %floor_result = call spir_func float @floor(float %x) |
| 1768 | // store float %floor_result, float * %ptr |
| 1769 | // %fract_intermediate = call spir_func float @clspv.fract(float %x) |
| 1770 | // %result = call spir_func float |
| 1771 | // @fmin(float %fract_intermediate, float 0x1.fffffep-1f) |
| 1772 | // |
| 1773 | // Becomes in the SPIR-V domain, where translations of floor, fmin, |
| 1774 | // and clspv.fract occur in the SPIR-V generator pass: |
| 1775 | // |
| 1776 | // %glsl_ext = OpExtInstImport "GLSL.std.450" |
| 1777 | // %just_under_1 = OpConstant %float 0x1.fffffep-1f |
| 1778 | // ... |
| 1779 | // %floor_result = OpExtInst %float %glsl_ext Floor %x |
| 1780 | // OpStore %ptr %floor_result |
| 1781 | // %fract_intermediate = OpExtInst %float %glsl_ext Fract %x |
| 1782 | // %fract_result = OpExtInst %float |
| 1783 | // %glsl_ext Fmin %fract_intermediate %just_under_1 |
| 1784 | |
| 1785 | |
| 1786 | using std::string; |
| 1787 | |
| 1788 | // Mapping from the fract builtin to the floor, fmin, and clspv.fract builtins |
| 1789 | // we need. The clspv.fract builtin is the same as GLSL.std.450 Fract. |
| 1790 | using QuadType = std::tuple<const char *, const char *, const char *, const char *>; |
| 1791 | auto make_quad = [](const char *a, const char *b, const char *c, |
| 1792 | const char *d) { |
| 1793 | return std::tuple<const char *, const char *, const char *, const char *>( |
| 1794 | a, b, c, d); |
| 1795 | }; |
| 1796 | const std::vector<QuadType> Functions = { |
| 1797 | make_quad("_Z5fractfPf", "_Z5floorff", "_Z4fminff", "clspv.fract.f"), |
| 1798 | make_quad("_Z5fractDv2_fPS_", "_Z5floorDv2_f", "_Z4fminDv2_ff", "clspv.fract.v2f"), |
| 1799 | make_quad("_Z5fractDv3_fPS_", "_Z5floorDv3_f", "_Z4fminDv3_ff", "clspv.fract.v3f"), |
| 1800 | make_quad("_Z5fractDv4_fPS_", "_Z5floorDv4_f", "_Z4fminDv4_ff", "clspv.fract.v4f"), |
| 1801 | }; |
| 1802 | |
| 1803 | for (auto& quad : Functions) { |
| 1804 | const StringRef fract_name(std::get<0>(quad)); |
| 1805 | |
| 1806 | // If we find a function with the matching name. |
| 1807 | if (auto F = M.getFunction(fract_name)) { |
| 1808 | if (F->use_begin() == F->use_end()) |
| 1809 | continue; |
| 1810 | |
| 1811 | // We have some uses. |
| 1812 | Changed = true; |
| 1813 | |
| 1814 | auto& Context = M.getContext(); |
| 1815 | |
| 1816 | const StringRef floor_name(std::get<1>(quad)); |
| 1817 | const StringRef fmin_name(std::get<2>(quad)); |
| 1818 | const StringRef clspv_fract_name(std::get<3>(quad)); |
| 1819 | |
| 1820 | // This is either float or a float vector. All the float-like |
| 1821 | // types are this type. |
| 1822 | auto result_ty = F->getReturnType(); |
| 1823 | |
| 1824 | Function* fmin_fn = M.getFunction(fmin_name); |
| 1825 | if (!fmin_fn) { |
| 1826 | // Make the fmin function. |
| 1827 | FunctionType* fn_ty = FunctionType::get(result_ty, {result_ty, result_ty}, false); |
| 1828 | fmin_fn = cast<Function>(M.getOrInsertFunction(fmin_name, fn_ty)); |
| 1829 | fmin_fn->addFnAttr(Attribute::ReadOnly); |
| 1830 | fmin_fn->addFnAttr(Attribute::ReadNone); |
| 1831 | fmin_fn->setCallingConv(CallingConv::SPIR_FUNC); |
| 1832 | } |
| 1833 | |
| 1834 | Function* floor_fn = M.getFunction(floor_name); |
| 1835 | if (!floor_fn) { |
| 1836 | // Make the floor function. |
| 1837 | FunctionType* fn_ty = FunctionType::get(result_ty, {result_ty}, false); |
| 1838 | floor_fn = cast<Function>(M.getOrInsertFunction(floor_name, fn_ty)); |
| 1839 | floor_fn->addFnAttr(Attribute::ReadOnly); |
| 1840 | floor_fn->addFnAttr(Attribute::ReadNone); |
| 1841 | floor_fn->setCallingConv(CallingConv::SPIR_FUNC); |
| 1842 | } |
| 1843 | |
| 1844 | Function* clspv_fract_fn = M.getFunction(clspv_fract_name); |
| 1845 | if (!clspv_fract_fn) { |
| 1846 | // Make the clspv_fract function. |
| 1847 | FunctionType* fn_ty = FunctionType::get(result_ty, {result_ty}, false); |
| 1848 | clspv_fract_fn = cast<Function>(M.getOrInsertFunction(clspv_fract_name, fn_ty)); |
| 1849 | clspv_fract_fn->addFnAttr(Attribute::ReadOnly); |
| 1850 | clspv_fract_fn->addFnAttr(Attribute::ReadNone); |
| 1851 | clspv_fract_fn->setCallingConv(CallingConv::SPIR_FUNC); |
| 1852 | } |
| 1853 | |
| 1854 | // Number of significant significand bits, whether represented or not. |
| 1855 | unsigned num_significand_bits; |
| 1856 | switch (result_ty->getScalarType()->getTypeID()) { |
| 1857 | case Type::HalfTyID: |
| 1858 | num_significand_bits = 11; |
| 1859 | break; |
| 1860 | case Type::FloatTyID: |
| 1861 | num_significand_bits = 24; |
| 1862 | break; |
| 1863 | case Type::DoubleTyID: |
| 1864 | num_significand_bits = 53; |
| 1865 | break; |
| 1866 | default: |
| 1867 | assert(false && "Unhandled float type when processing fract builtin"); |
| 1868 | break; |
| 1869 | } |
| 1870 | // Beware that the disassembler displays this value as |
| 1871 | // OpConstant %float 1 |
| 1872 | // which is not quite right. |
| 1873 | const double kJustUnderOneScalar = |
| 1874 | ldexp(double((1 << num_significand_bits) - 1), -num_significand_bits); |
| 1875 | |
| 1876 | Constant *just_under_one = |
| 1877 | ConstantFP::get(result_ty->getScalarType(), kJustUnderOneScalar); |
| 1878 | if (result_ty->isVectorTy()) { |
| 1879 | just_under_one = ConstantVector::getSplat( |
| 1880 | result_ty->getVectorNumElements(), just_under_one); |
| 1881 | } |
| 1882 | |
| 1883 | IRBuilder<> Builder(Context); |
| 1884 | |
| 1885 | SmallVector<Instruction *, 4> ToRemoves; |
| 1886 | |
| 1887 | // Walk the users of the function. |
| 1888 | for (auto &U : F->uses()) { |
| 1889 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 1890 | |
| 1891 | Builder.SetInsertPoint(CI); |
| 1892 | auto arg = CI->getArgOperand(0); |
| 1893 | auto ptr = CI->getArgOperand(1); |
| 1894 | |
| 1895 | // Compute floor result and store it. |
| 1896 | auto floor = Builder.CreateCall(floor_fn, {arg}); |
| 1897 | Builder.CreateStore(floor, ptr); |
| 1898 | |
| 1899 | auto fract_intermediate = Builder.CreateCall(clspv_fract_fn, arg); |
| 1900 | auto fract_result = Builder.CreateCall(fmin_fn, {fract_intermediate, just_under_one}); |
| 1901 | |
| 1902 | CI->replaceAllUsesWith(fract_result); |
| 1903 | |
| 1904 | // Lastly, remember to remove the user. |
| 1905 | ToRemoves.push_back(CI); |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | // And cleanup the calls we don't use anymore. |
| 1910 | for (auto V : ToRemoves) { |
| 1911 | V->eraseFromParent(); |
| 1912 | } |
| 1913 | |
| 1914 | // And remove the function we don't need either too. |
| 1915 | F->eraseFromParent(); |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | return Changed; |
| 1920 | } |