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" |
alan-baker | 4986eff | 2020-10-29 13:38:00 -0400 | [diff] [blame] | 24 | #include "llvm/IR/Operator.h" |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 25 | #include "llvm/IR/ValueSymbolTable.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
alan-baker | 4986eff | 2020-10-29 13:38:00 -0400 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/Cloning.h" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 31 | |
alan-baker | e090260 | 2020-03-23 08:43:40 -0400 | [diff] [blame] | 32 | #include "spirv/unified1/spirv.hpp" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 33 | |
alan-baker | 931d18a | 2019-12-12 08:21:32 -0500 | [diff] [blame] | 34 | #include "clspv/AddressSpace.h" |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 35 | #include "clspv/Option.h" |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 36 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 37 | #include "Builtins.h" |
alan-baker | 931d18a | 2019-12-12 08:21:32 -0500 | [diff] [blame] | 38 | #include "Constants.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 39 | #include "Passes.h" |
| 40 | #include "SPIRVOp.h" |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 41 | #include "Types.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 42 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 43 | using namespace clspv; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
| 46 | #define DEBUG_TYPE "ReplaceOpenCLBuiltin" |
| 47 | |
| 48 | namespace { |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 49 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 50 | uint32_t clz(uint32_t v) { |
| 51 | uint32_t r; |
| 52 | uint32_t shift; |
| 53 | |
| 54 | r = (v > 0xFFFF) << 4; |
| 55 | v >>= r; |
| 56 | shift = (v > 0xFF) << 3; |
| 57 | v >>= shift; |
| 58 | r |= shift; |
| 59 | shift = (v > 0xF) << 2; |
| 60 | v >>= shift; |
| 61 | r |= shift; |
| 62 | shift = (v > 0x3) << 1; |
| 63 | v >>= shift; |
| 64 | r |= shift; |
| 65 | r |= (v >> 1); |
| 66 | |
| 67 | return r; |
| 68 | } |
| 69 | |
Kévin Petit | fdfa92e | 2019-09-25 14:20:58 +0100 | [diff] [blame] | 70 | Type *getIntOrIntVectorTyForCast(LLVMContext &C, Type *Ty) { |
| 71 | Type *IntTy = Type::getIntNTy(C, Ty->getScalarSizeInBits()); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 72 | if (auto vec_ty = dyn_cast<VectorType>(Ty)) { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 73 | IntTy = FixedVectorType::get(IntTy, |
| 74 | vec_ty->getElementCount().getKnownMinValue()); |
Kévin Petit | fdfa92e | 2019-09-25 14:20:58 +0100 | [diff] [blame] | 75 | } |
| 76 | return IntTy; |
| 77 | } |
| 78 | |
alan-baker | 4986eff | 2020-10-29 13:38:00 -0400 | [diff] [blame] | 79 | Value *MemoryOrderSemantics(Value *order, bool is_global, |
| 80 | Instruction *InsertBefore, |
| 81 | spv::MemorySemanticsMask base_semantics) { |
| 82 | enum AtomicMemoryOrder : uint32_t { |
| 83 | kMemoryOrderRelaxed = 0, |
| 84 | kMemoryOrderAcquire = 2, |
| 85 | kMemoryOrderRelease = 3, |
| 86 | kMemoryOrderAcqRel = 4, |
| 87 | kMemoryOrderSeqCst = 5 |
| 88 | }; |
| 89 | |
| 90 | IRBuilder<> builder(InsertBefore); |
| 91 | |
| 92 | // Constants for OpenCL C 2.0 memory_order. |
| 93 | const auto relaxed = builder.getInt32(AtomicMemoryOrder::kMemoryOrderRelaxed); |
| 94 | const auto acquire = builder.getInt32(AtomicMemoryOrder::kMemoryOrderAcquire); |
| 95 | const auto release = builder.getInt32(AtomicMemoryOrder::kMemoryOrderRelease); |
| 96 | const auto acq_rel = builder.getInt32(AtomicMemoryOrder::kMemoryOrderAcqRel); |
| 97 | |
| 98 | // Constants for SPIR-V ordering memory semantics. |
| 99 | const auto RelaxedSemantics = builder.getInt32(spv::MemorySemanticsMaskNone); |
| 100 | const auto AcquireSemantics = |
| 101 | builder.getInt32(spv::MemorySemanticsAcquireMask); |
| 102 | const auto ReleaseSemantics = |
| 103 | builder.getInt32(spv::MemorySemanticsReleaseMask); |
| 104 | const auto AcqRelSemantics = |
| 105 | builder.getInt32(spv::MemorySemanticsAcquireReleaseMask); |
| 106 | |
| 107 | // Constants for SPIR-V storage class semantics. |
| 108 | const auto UniformSemantics = |
| 109 | builder.getInt32(spv::MemorySemanticsUniformMemoryMask); |
| 110 | const auto WorkgroupSemantics = |
| 111 | builder.getInt32(spv::MemorySemanticsWorkgroupMemoryMask); |
| 112 | |
| 113 | // Instead of sequentially consistent, use acquire, release or acquire |
| 114 | // release semantics. |
| 115 | Value *base_order = nullptr; |
| 116 | switch (base_semantics) { |
| 117 | case spv::MemorySemanticsAcquireMask: |
| 118 | base_order = AcquireSemantics; |
| 119 | break; |
| 120 | case spv::MemorySemanticsReleaseMask: |
| 121 | base_order = ReleaseSemantics; |
| 122 | break; |
| 123 | default: |
| 124 | base_order = AcqRelSemantics; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | Value *storage = is_global ? UniformSemantics : WorkgroupSemantics; |
| 129 | if (order == nullptr) |
| 130 | return builder.CreateOr({storage, base_order}); |
| 131 | |
| 132 | auto is_relaxed = builder.CreateICmpEQ(order, relaxed); |
| 133 | auto is_acquire = builder.CreateICmpEQ(order, acquire); |
| 134 | auto is_release = builder.CreateICmpEQ(order, release); |
| 135 | auto is_acq_rel = builder.CreateICmpEQ(order, acq_rel); |
| 136 | auto semantics = |
| 137 | builder.CreateSelect(is_relaxed, RelaxedSemantics, base_order); |
| 138 | semantics = builder.CreateSelect(is_acquire, AcquireSemantics, semantics); |
| 139 | semantics = builder.CreateSelect(is_release, ReleaseSemantics, semantics); |
| 140 | semantics = builder.CreateSelect(is_acq_rel, AcqRelSemantics, semantics); |
| 141 | return builder.CreateOr({storage, semantics}); |
| 142 | } |
| 143 | |
| 144 | Value *MemoryScope(Value *scope, bool is_global, Instruction *InsertBefore) { |
| 145 | enum AtomicMemoryScope : uint32_t { |
| 146 | kMemoryScopeWorkItem = 0, |
| 147 | kMemoryScopeWorkGroup = 1, |
| 148 | kMemoryScopeDevice = 2, |
| 149 | kMemoryScopeAllSVMDevices = 3, // not supported |
| 150 | kMemoryScopeSubGroup = 4 |
| 151 | }; |
| 152 | |
| 153 | IRBuilder<> builder(InsertBefore); |
| 154 | |
| 155 | // Constants for OpenCL C 2.0 memory_scope. |
| 156 | const auto work_item = |
| 157 | builder.getInt32(AtomicMemoryScope::kMemoryScopeWorkItem); |
| 158 | const auto work_group = |
| 159 | builder.getInt32(AtomicMemoryScope::kMemoryScopeWorkGroup); |
| 160 | const auto sub_group = |
| 161 | builder.getInt32(AtomicMemoryScope::kMemoryScopeSubGroup); |
| 162 | const auto device = builder.getInt32(AtomicMemoryScope::kMemoryScopeDevice); |
| 163 | |
| 164 | // Constants for SPIR-V memory scopes. |
| 165 | const auto InvocationScope = builder.getInt32(spv::ScopeInvocation); |
| 166 | const auto WorkgroupScope = builder.getInt32(spv::ScopeWorkgroup); |
| 167 | const auto DeviceScope = builder.getInt32(spv::ScopeDevice); |
| 168 | const auto SubgroupScope = builder.getInt32(spv::ScopeSubgroup); |
| 169 | |
| 170 | auto base_scope = is_global ? DeviceScope : WorkgroupScope; |
| 171 | if (scope == nullptr) |
| 172 | return base_scope; |
| 173 | |
| 174 | auto is_work_item = builder.CreateICmpEQ(scope, work_item); |
| 175 | auto is_work_group = builder.CreateICmpEQ(scope, work_group); |
| 176 | auto is_sub_group = builder.CreateICmpEQ(scope, sub_group); |
| 177 | auto is_device = builder.CreateICmpEQ(scope, device); |
| 178 | |
| 179 | scope = builder.CreateSelect(is_work_item, InvocationScope, base_scope); |
| 180 | scope = builder.CreateSelect(is_work_group, WorkgroupScope, scope); |
| 181 | scope = builder.CreateSelect(is_sub_group, SubgroupScope, scope); |
| 182 | scope = builder.CreateSelect(is_device, DeviceScope, scope); |
| 183 | |
| 184 | return scope; |
| 185 | } |
| 186 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 187 | bool replaceCallsWithValue(Function &F, |
| 188 | std::function<Value *(CallInst *)> Replacer) { |
| 189 | |
| 190 | bool Changed = false; |
| 191 | |
| 192 | SmallVector<Instruction *, 4> ToRemoves; |
| 193 | |
| 194 | // Walk the users of the function. |
| 195 | for (auto &U : F.uses()) { |
| 196 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 197 | |
| 198 | auto NewValue = Replacer(CI); |
| 199 | |
| 200 | if (NewValue != nullptr) { |
| 201 | CI->replaceAllUsesWith(NewValue); |
| 202 | |
| 203 | // Lastly, remember to remove the user. |
| 204 | ToRemoves.push_back(CI); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | Changed = !ToRemoves.empty(); |
| 210 | |
| 211 | // And cleanup the calls we don't use anymore. |
| 212 | for (auto V : ToRemoves) { |
| 213 | V->eraseFromParent(); |
| 214 | } |
| 215 | |
| 216 | return Changed; |
| 217 | } |
| 218 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 219 | struct ReplaceOpenCLBuiltinPass final : public ModulePass { |
| 220 | static char ID; |
| 221 | ReplaceOpenCLBuiltinPass() : ModulePass(ID) {} |
| 222 | |
| 223 | bool runOnModule(Module &M) override; |
alan-baker | 6b9d1ee | 2020-11-03 23:11:32 -0500 | [diff] [blame] | 224 | |
| 225 | private: |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 226 | bool runOnFunction(Function &F); |
| 227 | bool replaceAbs(Function &F); |
| 228 | bool replaceAbsDiff(Function &F, bool is_signed); |
| 229 | bool replaceCopysign(Function &F); |
| 230 | bool replaceRecip(Function &F); |
| 231 | bool replaceDivide(Function &F); |
| 232 | bool replaceDot(Function &F); |
| 233 | bool replaceFmod(Function &F); |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 234 | bool replaceExp10(Function &F, const std::string &basename); |
| 235 | bool replaceLog10(Function &F, const std::string &basename); |
gnl21 | 636e799 | 2020-09-09 16:08:16 +0100 | [diff] [blame] | 236 | bool replaceLog1p(Function &F); |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 237 | bool replaceBarrier(Function &F, bool subgroup = false); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 238 | bool replaceMemFence(Function &F, uint32_t semantics); |
Kévin Petit | 1cb4511 | 2020-04-27 18:55:48 +0100 | [diff] [blame] | 239 | bool replacePrefetch(Function &F); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 240 | bool replaceRelational(Function &F, CmpInst::Predicate P, int32_t C); |
| 241 | bool replaceIsInfAndIsNan(Function &F, spv::Op SPIRVOp, int32_t isvec); |
| 242 | bool replaceIsFinite(Function &F); |
| 243 | bool replaceAllAndAny(Function &F, spv::Op SPIRVOp); |
| 244 | bool replaceUpsample(Function &F); |
| 245 | bool replaceRotate(Function &F); |
| 246 | bool replaceConvert(Function &F, bool SrcIsSigned, bool DstIsSigned); |
| 247 | bool replaceMulHi(Function &F, bool is_signed, bool is_mad = false); |
| 248 | bool replaceSelect(Function &F); |
| 249 | bool replaceBitSelect(Function &F); |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 250 | bool replaceStep(Function &F, bool is_smooth); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 251 | bool replaceSignbit(Function &F, bool is_vec); |
| 252 | bool replaceMul(Function &F, bool is_float, bool is_mad); |
| 253 | bool replaceVloadHalf(Function &F, const std::string &name, int vec_size); |
| 254 | bool replaceVloadHalf(Function &F); |
| 255 | bool replaceVloadHalf2(Function &F); |
| 256 | bool replaceVloadHalf4(Function &F); |
| 257 | bool replaceClspvVloadaHalf2(Function &F); |
| 258 | bool replaceClspvVloadaHalf4(Function &F); |
| 259 | bool replaceVstoreHalf(Function &F, int vec_size); |
| 260 | bool replaceVstoreHalf(Function &F); |
| 261 | bool replaceVstoreHalf2(Function &F); |
| 262 | bool replaceVstoreHalf4(Function &F); |
| 263 | bool replaceHalfReadImage(Function &F); |
| 264 | bool replaceHalfWriteImage(Function &F); |
| 265 | bool replaceSampledReadImageWithIntCoords(Function &F); |
| 266 | bool replaceAtomics(Function &F, spv::Op Op); |
| 267 | bool replaceAtomics(Function &F, llvm::AtomicRMWInst::BinOp Op); |
alan-baker | 4986eff | 2020-10-29 13:38:00 -0400 | [diff] [blame] | 268 | bool replaceAtomicLoad(Function &F); |
| 269 | bool replaceExplicitAtomics(Function &F, spv::Op Op, |
| 270 | spv::MemorySemanticsMask semantics = |
| 271 | spv::MemorySemanticsAcquireReleaseMask); |
| 272 | bool replaceAtomicCompareExchange(Function &); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 273 | bool replaceCross(Function &F); |
| 274 | bool replaceFract(Function &F, int vec_size); |
| 275 | bool replaceVload(Function &F); |
| 276 | bool replaceVstore(Function &F); |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 277 | bool replaceAddSubSat(Function &F, bool is_signed, bool is_add); |
Kévin Petit | 8576f68 | 2020-11-02 14:51:32 +0000 | [diff] [blame] | 278 | bool replaceHadd(Function &F, bool is_signed, |
| 279 | Instruction::BinaryOps join_opcode); |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 280 | bool replaceCountZeroes(Function &F, bool leading); |
alan-baker | 6b9d1ee | 2020-11-03 23:11:32 -0500 | [diff] [blame] | 281 | bool replaceMadSat(Function &F, bool is_signed); |
alan-baker | 1510657 | 2020-11-06 15:08:10 -0500 | [diff] [blame^] | 282 | bool replaceOrdered(Function &F, bool is_ordered); |
alan-baker | 6b9d1ee | 2020-11-03 23:11:32 -0500 | [diff] [blame] | 283 | |
| 284 | // Caches struct types for { |type|, |type| }. This prevents |
| 285 | // getOrInsertFunction from introducing a bitcasts between structs with |
| 286 | // identical contents. |
| 287 | Type *GetPairStruct(Type *type); |
| 288 | |
| 289 | DenseMap<Type *, Type *> PairStructMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 290 | }; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 291 | |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 292 | } // namespace |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 293 | |
| 294 | char ReplaceOpenCLBuiltinPass::ID = 0; |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 295 | INITIALIZE_PASS(ReplaceOpenCLBuiltinPass, "ReplaceOpenCLBuiltin", |
| 296 | "Replace OpenCL Builtins Pass", false, false) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 297 | |
| 298 | namespace clspv { |
| 299 | ModulePass *createReplaceOpenCLBuiltinPass() { |
| 300 | return new ReplaceOpenCLBuiltinPass(); |
| 301 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 302 | } // namespace clspv |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 303 | |
| 304 | bool ReplaceOpenCLBuiltinPass::runOnModule(Module &M) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 305 | std::list<Function *> func_list; |
| 306 | for (auto &F : M.getFunctionList()) { |
| 307 | // process only function declarations |
| 308 | if (F.isDeclaration() && runOnFunction(F)) { |
| 309 | func_list.push_front(&F); |
Kévin Petit | 2444e9b | 2018-11-09 14:14:37 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 312 | if (func_list.size() != 0) { |
| 313 | // recursively convert functions, but first remove dead |
| 314 | for (auto *F : func_list) { |
| 315 | if (F->use_empty()) { |
| 316 | F->eraseFromParent(); |
| 317 | } |
| 318 | } |
| 319 | runOnModule(M); |
| 320 | return true; |
| 321 | } |
| 322 | return false; |
Kévin Petit | 2444e9b | 2018-11-09 14:14:37 +0000 | [diff] [blame] | 323 | } |
| 324 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 325 | bool ReplaceOpenCLBuiltinPass::runOnFunction(Function &F) { |
| 326 | auto &FI = Builtins::Lookup(&F); |
| 327 | switch (FI.getType()) { |
| 328 | case Builtins::kAbs: |
| 329 | if (!FI.getParameter(0).is_signed) { |
| 330 | return replaceAbs(F); |
| 331 | } |
| 332 | break; |
| 333 | case Builtins::kAbsDiff: |
| 334 | return replaceAbsDiff(F, FI.getParameter(0).is_signed); |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 335 | |
| 336 | case Builtins::kAddSat: |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 337 | return replaceAddSubSat(F, FI.getParameter(0).is_signed, true); |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 338 | |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 339 | case Builtins::kClz: |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 340 | return replaceCountZeroes(F, true); |
| 341 | |
| 342 | case Builtins::kCtz: |
| 343 | return replaceCountZeroes(F, false); |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 344 | |
alan-baker | b6da513 | 2020-10-29 15:59:06 -0400 | [diff] [blame] | 345 | case Builtins::kHadd: |
Kévin Petit | 8576f68 | 2020-11-02 14:51:32 +0000 | [diff] [blame] | 346 | return replaceHadd(F, FI.getParameter(0).is_signed, Instruction::And); |
alan-baker | b6da513 | 2020-10-29 15:59:06 -0400 | [diff] [blame] | 347 | case Builtins::kRhadd: |
Kévin Petit | 8576f68 | 2020-11-02 14:51:32 +0000 | [diff] [blame] | 348 | return replaceHadd(F, FI.getParameter(0).is_signed, Instruction::Or); |
alan-baker | b6da513 | 2020-10-29 15:59:06 -0400 | [diff] [blame] | 349 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 350 | case Builtins::kCopysign: |
| 351 | return replaceCopysign(F); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 352 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 353 | case Builtins::kHalfRecip: |
| 354 | case Builtins::kNativeRecip: |
| 355 | return replaceRecip(F); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 356 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 357 | case Builtins::kHalfDivide: |
| 358 | case Builtins::kNativeDivide: |
| 359 | return replaceDivide(F); |
| 360 | |
| 361 | case Builtins::kDot: |
| 362 | return replaceDot(F); |
| 363 | |
| 364 | case Builtins::kExp10: |
| 365 | case Builtins::kHalfExp10: |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 366 | case Builtins::kNativeExp10: |
| 367 | return replaceExp10(F, FI.getName()); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 368 | |
| 369 | case Builtins::kLog10: |
| 370 | case Builtins::kHalfLog10: |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 371 | case Builtins::kNativeLog10: |
| 372 | return replaceLog10(F, FI.getName()); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 373 | |
gnl21 | 636e799 | 2020-09-09 16:08:16 +0100 | [diff] [blame] | 374 | case Builtins::kLog1p: |
| 375 | return replaceLog1p(F); |
| 376 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 377 | case Builtins::kFmod: |
| 378 | return replaceFmod(F); |
| 379 | |
| 380 | case Builtins::kBarrier: |
| 381 | case Builtins::kWorkGroupBarrier: |
| 382 | return replaceBarrier(F); |
| 383 | |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 384 | case Builtins::kSubGroupBarrier: |
| 385 | return replaceBarrier(F, true); |
| 386 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 387 | case Builtins::kMemFence: |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 388 | return replaceMemFence(F, spv::MemorySemanticsAcquireReleaseMask); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 389 | case Builtins::kReadMemFence: |
| 390 | return replaceMemFence(F, spv::MemorySemanticsAcquireMask); |
| 391 | case Builtins::kWriteMemFence: |
| 392 | return replaceMemFence(F, spv::MemorySemanticsReleaseMask); |
| 393 | |
| 394 | // Relational |
| 395 | case Builtins::kIsequal: |
| 396 | return replaceRelational(F, CmpInst::FCMP_OEQ, |
| 397 | FI.getParameter(0).vector_size ? -1 : 1); |
| 398 | case Builtins::kIsgreater: |
| 399 | return replaceRelational(F, CmpInst::FCMP_OGT, |
| 400 | FI.getParameter(0).vector_size ? -1 : 1); |
| 401 | case Builtins::kIsgreaterequal: |
| 402 | return replaceRelational(F, CmpInst::FCMP_OGE, |
| 403 | FI.getParameter(0).vector_size ? -1 : 1); |
| 404 | case Builtins::kIsless: |
| 405 | return replaceRelational(F, CmpInst::FCMP_OLT, |
| 406 | FI.getParameter(0).vector_size ? -1 : 1); |
| 407 | case Builtins::kIslessequal: |
| 408 | return replaceRelational(F, CmpInst::FCMP_OLE, |
| 409 | FI.getParameter(0).vector_size ? -1 : 1); |
| 410 | case Builtins::kIsnotequal: |
| 411 | return replaceRelational(F, CmpInst::FCMP_ONE, |
| 412 | FI.getParameter(0).vector_size ? -1 : 1); |
| 413 | |
alan-baker | 1510657 | 2020-11-06 15:08:10 -0500 | [diff] [blame^] | 414 | case Builtins::kIsordered: |
| 415 | return replaceOrdered(F, true); |
| 416 | |
| 417 | case Builtins::kIsunordered: |
| 418 | return replaceOrdered(F, false); |
| 419 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 420 | case Builtins::kIsinf: { |
| 421 | bool is_vec = FI.getParameter(0).vector_size != 0; |
| 422 | return replaceIsInfAndIsNan(F, spv::OpIsInf, is_vec ? -1 : 1); |
| 423 | } |
| 424 | case Builtins::kIsnan: { |
| 425 | bool is_vec = FI.getParameter(0).vector_size != 0; |
| 426 | return replaceIsInfAndIsNan(F, spv::OpIsNan, is_vec ? -1 : 1); |
| 427 | } |
| 428 | |
| 429 | case Builtins::kIsfinite: |
| 430 | return replaceIsFinite(F); |
| 431 | |
| 432 | case Builtins::kAll: { |
| 433 | bool is_vec = FI.getParameter(0).vector_size != 0; |
| 434 | return replaceAllAndAny(F, !is_vec ? spv::OpNop : spv::OpAll); |
| 435 | } |
| 436 | case Builtins::kAny: { |
| 437 | bool is_vec = FI.getParameter(0).vector_size != 0; |
| 438 | return replaceAllAndAny(F, !is_vec ? spv::OpNop : spv::OpAny); |
| 439 | } |
| 440 | |
| 441 | case Builtins::kUpsample: |
| 442 | return replaceUpsample(F); |
| 443 | |
| 444 | case Builtins::kRotate: |
| 445 | return replaceRotate(F); |
| 446 | |
| 447 | case Builtins::kConvert: |
| 448 | return replaceConvert(F, FI.getParameter(0).is_signed, |
| 449 | FI.getReturnType().is_signed); |
| 450 | |
alan-baker | 4986eff | 2020-10-29 13:38:00 -0400 | [diff] [blame] | 451 | // OpenCL 2.0 explicit atomics have different default scopes and semantics |
| 452 | // than legacy atomic functions. |
| 453 | case Builtins::kAtomicLoad: |
| 454 | case Builtins::kAtomicLoadExplicit: |
| 455 | return replaceAtomicLoad(F); |
| 456 | case Builtins::kAtomicStore: |
| 457 | case Builtins::kAtomicStoreExplicit: |
| 458 | return replaceExplicitAtomics(F, spv::OpAtomicStore, |
| 459 | spv::MemorySemanticsReleaseMask); |
| 460 | case Builtins::kAtomicExchange: |
| 461 | case Builtins::kAtomicExchangeExplicit: |
| 462 | return replaceExplicitAtomics(F, spv::OpAtomicExchange); |
| 463 | case Builtins::kAtomicFetchAdd: |
| 464 | case Builtins::kAtomicFetchAddExplicit: |
| 465 | return replaceExplicitAtomics(F, spv::OpAtomicIAdd); |
| 466 | case Builtins::kAtomicFetchSub: |
| 467 | case Builtins::kAtomicFetchSubExplicit: |
| 468 | return replaceExplicitAtomics(F, spv::OpAtomicISub); |
| 469 | case Builtins::kAtomicFetchOr: |
| 470 | case Builtins::kAtomicFetchOrExplicit: |
| 471 | return replaceExplicitAtomics(F, spv::OpAtomicOr); |
| 472 | case Builtins::kAtomicFetchXor: |
| 473 | case Builtins::kAtomicFetchXorExplicit: |
| 474 | return replaceExplicitAtomics(F, spv::OpAtomicXor); |
| 475 | case Builtins::kAtomicFetchAnd: |
| 476 | case Builtins::kAtomicFetchAndExplicit: |
| 477 | return replaceExplicitAtomics(F, spv::OpAtomicAnd); |
| 478 | case Builtins::kAtomicFetchMin: |
| 479 | case Builtins::kAtomicFetchMinExplicit: |
| 480 | return replaceExplicitAtomics(F, FI.getParameter(1).is_signed |
| 481 | ? spv::OpAtomicSMin |
| 482 | : spv::OpAtomicUMin); |
| 483 | case Builtins::kAtomicFetchMax: |
| 484 | case Builtins::kAtomicFetchMaxExplicit: |
| 485 | return replaceExplicitAtomics(F, FI.getParameter(1).is_signed |
| 486 | ? spv::OpAtomicSMax |
| 487 | : spv::OpAtomicUMax); |
| 488 | // Weak compare exchange is generated as strong compare exchange. |
| 489 | case Builtins::kAtomicCompareExchangeWeak: |
| 490 | case Builtins::kAtomicCompareExchangeWeakExplicit: |
| 491 | case Builtins::kAtomicCompareExchangeStrong: |
| 492 | case Builtins::kAtomicCompareExchangeStrongExplicit: |
| 493 | return replaceAtomicCompareExchange(F); |
| 494 | |
| 495 | // Legacy atomic functions. |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 496 | case Builtins::kAtomicInc: |
| 497 | return replaceAtomics(F, spv::OpAtomicIIncrement); |
| 498 | case Builtins::kAtomicDec: |
| 499 | return replaceAtomics(F, spv::OpAtomicIDecrement); |
| 500 | case Builtins::kAtomicCmpxchg: |
| 501 | return replaceAtomics(F, spv::OpAtomicCompareExchange); |
| 502 | case Builtins::kAtomicAdd: |
| 503 | return replaceAtomics(F, llvm::AtomicRMWInst::Add); |
| 504 | case Builtins::kAtomicSub: |
| 505 | return replaceAtomics(F, llvm::AtomicRMWInst::Sub); |
| 506 | case Builtins::kAtomicXchg: |
| 507 | return replaceAtomics(F, llvm::AtomicRMWInst::Xchg); |
| 508 | case Builtins::kAtomicMin: |
| 509 | return replaceAtomics(F, FI.getParameter(0).is_signed |
| 510 | ? llvm::AtomicRMWInst::Min |
| 511 | : llvm::AtomicRMWInst::UMin); |
| 512 | case Builtins::kAtomicMax: |
| 513 | return replaceAtomics(F, FI.getParameter(0).is_signed |
| 514 | ? llvm::AtomicRMWInst::Max |
| 515 | : llvm::AtomicRMWInst::UMax); |
| 516 | case Builtins::kAtomicAnd: |
| 517 | return replaceAtomics(F, llvm::AtomicRMWInst::And); |
| 518 | case Builtins::kAtomicOr: |
| 519 | return replaceAtomics(F, llvm::AtomicRMWInst::Or); |
| 520 | case Builtins::kAtomicXor: |
| 521 | return replaceAtomics(F, llvm::AtomicRMWInst::Xor); |
| 522 | |
| 523 | case Builtins::kCross: |
| 524 | if (FI.getParameter(0).vector_size == 4) { |
| 525 | return replaceCross(F); |
| 526 | } |
| 527 | break; |
| 528 | |
| 529 | case Builtins::kFract: |
| 530 | if (FI.getParameterCount()) { |
| 531 | return replaceFract(F, FI.getParameter(0).vector_size); |
| 532 | } |
| 533 | break; |
| 534 | |
| 535 | case Builtins::kMadHi: |
| 536 | return replaceMulHi(F, FI.getParameter(0).is_signed, true); |
| 537 | case Builtins::kMulHi: |
| 538 | return replaceMulHi(F, FI.getParameter(0).is_signed, false); |
| 539 | |
alan-baker | 6b9d1ee | 2020-11-03 23:11:32 -0500 | [diff] [blame] | 540 | case Builtins::kMadSat: |
| 541 | return replaceMadSat(F, FI.getParameter(0).is_signed); |
| 542 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 543 | case Builtins::kMad: |
| 544 | case Builtins::kMad24: |
| 545 | return replaceMul(F, FI.getParameter(0).type_id == llvm::Type::FloatTyID, |
| 546 | true); |
| 547 | case Builtins::kMul24: |
| 548 | return replaceMul(F, FI.getParameter(0).type_id == llvm::Type::FloatTyID, |
| 549 | false); |
| 550 | |
| 551 | case Builtins::kSelect: |
| 552 | return replaceSelect(F); |
| 553 | |
| 554 | case Builtins::kBitselect: |
| 555 | return replaceBitSelect(F); |
| 556 | |
| 557 | case Builtins::kVload: |
| 558 | return replaceVload(F); |
| 559 | |
| 560 | case Builtins::kVloadaHalf: |
| 561 | case Builtins::kVloadHalf: |
| 562 | return replaceVloadHalf(F, FI.getName(), FI.getParameter(0).vector_size); |
| 563 | |
| 564 | case Builtins::kVstore: |
| 565 | return replaceVstore(F); |
| 566 | |
| 567 | case Builtins::kVstoreHalf: |
| 568 | case Builtins::kVstoreaHalf: |
| 569 | return replaceVstoreHalf(F, FI.getParameter(0).vector_size); |
| 570 | |
| 571 | case Builtins::kSmoothstep: { |
| 572 | int vec_size = FI.getLastParameter().vector_size; |
| 573 | if (FI.getParameter(0).vector_size == 0 && vec_size != 0) { |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 574 | return replaceStep(F, true); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 575 | } |
| 576 | break; |
| 577 | } |
| 578 | case Builtins::kStep: { |
| 579 | int vec_size = FI.getLastParameter().vector_size; |
| 580 | if (FI.getParameter(0).vector_size == 0 && vec_size != 0) { |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 581 | return replaceStep(F, false); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 582 | } |
| 583 | break; |
| 584 | } |
| 585 | |
| 586 | case Builtins::kSignbit: |
| 587 | return replaceSignbit(F, FI.getParameter(0).vector_size != 0); |
| 588 | |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 589 | case Builtins::kSubSat: |
| 590 | return replaceAddSubSat(F, FI.getParameter(0).is_signed, false); |
| 591 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 592 | case Builtins::kReadImageh: |
| 593 | return replaceHalfReadImage(F); |
| 594 | case Builtins::kReadImagef: |
| 595 | case Builtins::kReadImagei: |
| 596 | case Builtins::kReadImageui: { |
| 597 | if (FI.getParameter(1).isSampler() && |
| 598 | FI.getParameter(2).type_id == llvm::Type::IntegerTyID) { |
| 599 | return replaceSampledReadImageWithIntCoords(F); |
| 600 | } |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | case Builtins::kWriteImageh: |
| 605 | return replaceHalfWriteImage(F); |
| 606 | |
Kévin Petit | 1cb4511 | 2020-04-27 18:55:48 +0100 | [diff] [blame] | 607 | case Builtins::kPrefetch: |
| 608 | return replacePrefetch(F); |
| 609 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 610 | default: |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | return false; |
| 615 | } |
| 616 | |
alan-baker | 6b9d1ee | 2020-11-03 23:11:32 -0500 | [diff] [blame] | 617 | Type *ReplaceOpenCLBuiltinPass::GetPairStruct(Type *type) { |
| 618 | auto iter = PairStructMap.find(type); |
| 619 | if (iter != PairStructMap.end()) |
| 620 | return iter->second; |
| 621 | |
| 622 | auto new_struct = StructType::get(type->getContext(), {type, type}); |
| 623 | PairStructMap[type] = new_struct; |
| 624 | return new_struct; |
| 625 | } |
| 626 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 627 | bool ReplaceOpenCLBuiltinPass::replaceAbs(Function &F) { |
| 628 | return replaceCallsWithValue(F, |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 629 | [](CallInst *CI) { return CI->getOperand(0); }); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 630 | } |
| 631 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 632 | bool ReplaceOpenCLBuiltinPass::replaceAbsDiff(Function &F, bool is_signed) { |
| 633 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 634 | auto XValue = CI->getOperand(0); |
| 635 | auto YValue = CI->getOperand(1); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 636 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 637 | IRBuilder<> Builder(CI); |
| 638 | auto XmY = Builder.CreateSub(XValue, YValue); |
| 639 | auto YmX = Builder.CreateSub(YValue, XValue); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 640 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 641 | Value *Cmp = nullptr; |
| 642 | if (is_signed) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 643 | Cmp = Builder.CreateICmpSGT(YValue, XValue); |
| 644 | } else { |
| 645 | Cmp = Builder.CreateICmpUGT(YValue, XValue); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 646 | } |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 647 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 648 | return Builder.CreateSelect(Cmp, YmX, XmY); |
| 649 | }); |
Kévin Petit | 91bc72e | 2019-04-08 15:17:46 +0100 | [diff] [blame] | 650 | } |
| 651 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 652 | bool ReplaceOpenCLBuiltinPass::replaceCopysign(Function &F) { |
| 653 | return replaceCallsWithValue(F, [&F](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 654 | auto XValue = CI->getOperand(0); |
| 655 | auto YValue = CI->getOperand(1); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 656 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 657 | auto Ty = XValue->getType(); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 658 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 659 | Type *IntTy = Type::getIntNTy(F.getContext(), Ty->getScalarSizeInBits()); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 660 | if (auto vec_ty = dyn_cast<VectorType>(Ty)) { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 661 | IntTy = FixedVectorType::get( |
| 662 | IntTy, vec_ty->getElementCount().getKnownMinValue()); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 663 | } |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 664 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 665 | // Return X with the sign of Y |
| 666 | |
| 667 | // Sign bit masks |
| 668 | auto SignBit = IntTy->getScalarSizeInBits() - 1; |
| 669 | auto SignBitMask = 1 << SignBit; |
| 670 | auto SignBitMaskValue = ConstantInt::get(IntTy, SignBitMask); |
| 671 | auto NotSignBitMaskValue = ConstantInt::get(IntTy, ~SignBitMask); |
| 672 | |
| 673 | IRBuilder<> Builder(CI); |
| 674 | |
| 675 | // Extract sign of Y |
| 676 | auto YInt = Builder.CreateBitCast(YValue, IntTy); |
| 677 | auto YSign = Builder.CreateAnd(YInt, SignBitMaskValue); |
| 678 | |
| 679 | // Clear sign bit in X |
| 680 | auto XInt = Builder.CreateBitCast(XValue, IntTy); |
| 681 | XInt = Builder.CreateAnd(XInt, NotSignBitMaskValue); |
| 682 | |
| 683 | // Insert sign bit of Y into X |
| 684 | auto NewXInt = Builder.CreateOr(XInt, YSign); |
| 685 | |
| 686 | // And cast back to floating-point |
| 687 | return Builder.CreateBitCast(NewXInt, Ty); |
| 688 | }); |
Kévin Petit | 8c1be28 | 2019-04-02 19:34:25 +0100 | [diff] [blame] | 689 | } |
| 690 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 691 | bool ReplaceOpenCLBuiltinPass::replaceRecip(Function &F) { |
| 692 | return replaceCallsWithValue(F, [](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 693 | // Recip has one arg. |
| 694 | auto Arg = CI->getOperand(0); |
| 695 | auto Cst1 = ConstantFP::get(Arg->getType(), 1.0); |
| 696 | return BinaryOperator::Create(Instruction::FDiv, Cst1, Arg, "", CI); |
| 697 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 698 | } |
| 699 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 700 | bool ReplaceOpenCLBuiltinPass::replaceDivide(Function &F) { |
| 701 | return replaceCallsWithValue(F, [](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 702 | auto Op0 = CI->getOperand(0); |
| 703 | auto Op1 = CI->getOperand(1); |
| 704 | return BinaryOperator::Create(Instruction::FDiv, Op0, Op1, "", CI); |
| 705 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 706 | } |
| 707 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 708 | bool ReplaceOpenCLBuiltinPass::replaceDot(Function &F) { |
| 709 | return replaceCallsWithValue(F, [](CallInst *CI) { |
Kévin Petit | 1329a00 | 2019-06-15 05:54:05 +0100 | [diff] [blame] | 710 | auto Op0 = CI->getOperand(0); |
| 711 | auto Op1 = CI->getOperand(1); |
| 712 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 713 | Value *V = nullptr; |
Kévin Petit | 1329a00 | 2019-06-15 05:54:05 +0100 | [diff] [blame] | 714 | if (Op0->getType()->isVectorTy()) { |
| 715 | V = clspv::InsertSPIRVOp(CI, spv::OpDot, {Attribute::ReadNone}, |
| 716 | CI->getType(), {Op0, Op1}); |
| 717 | } else { |
| 718 | V = BinaryOperator::Create(Instruction::FMul, Op0, Op1, "", CI); |
| 719 | } |
| 720 | |
| 721 | return V; |
| 722 | }); |
| 723 | } |
| 724 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 725 | bool ReplaceOpenCLBuiltinPass::replaceExp10(Function &F, |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 726 | const std::string &basename) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 727 | // convert to natural |
| 728 | auto slen = basename.length() - 2; |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 729 | std::string NewFName = basename.substr(0, slen); |
| 730 | NewFName = |
| 731 | Builtins::GetMangledFunctionName(NewFName.c_str(), F.getFunctionType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 732 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 733 | Module &M = *F.getParent(); |
| 734 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 735 | auto NewF = M.getOrInsertFunction(NewFName, F.getFunctionType()); |
| 736 | |
| 737 | auto Arg = CI->getOperand(0); |
| 738 | |
| 739 | // Constant of the natural log of 10 (ln(10)). |
| 740 | const double Ln10 = |
| 741 | 2.302585092994045684017991454684364207601101488628772976033; |
| 742 | |
| 743 | auto Mul = BinaryOperator::Create( |
| 744 | Instruction::FMul, ConstantFP::get(Arg->getType(), Ln10), Arg, "", CI); |
| 745 | |
| 746 | return CallInst::Create(NewF, Mul, "", CI); |
| 747 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 748 | } |
| 749 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 750 | bool ReplaceOpenCLBuiltinPass::replaceFmod(Function &F) { |
Kévin Petit | 0644a9c | 2019-06-20 21:08:46 +0100 | [diff] [blame] | 751 | // OpenCL fmod(x,y) is x - y * trunc(x/y) |
| 752 | // The sign for a non-zero result is taken from x. |
| 753 | // (Try an example.) |
| 754 | // So translate to FRem |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 755 | return replaceCallsWithValue(F, [](CallInst *CI) { |
Kévin Petit | 0644a9c | 2019-06-20 21:08:46 +0100 | [diff] [blame] | 756 | auto Op0 = CI->getOperand(0); |
| 757 | auto Op1 = CI->getOperand(1); |
| 758 | return BinaryOperator::Create(Instruction::FRem, Op0, Op1, "", CI); |
| 759 | }); |
| 760 | } |
| 761 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 762 | bool ReplaceOpenCLBuiltinPass::replaceLog10(Function &F, |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 763 | const std::string &basename) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 764 | // convert to natural |
| 765 | auto slen = basename.length() - 2; |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 766 | std::string NewFName = basename.substr(0, slen); |
| 767 | NewFName = |
| 768 | Builtins::GetMangledFunctionName(NewFName.c_str(), F.getFunctionType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 769 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 770 | Module &M = *F.getParent(); |
| 771 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 772 | auto NewF = M.getOrInsertFunction(NewFName, F.getFunctionType()); |
| 773 | |
| 774 | auto Arg = CI->getOperand(0); |
| 775 | |
| 776 | // Constant of the reciprocal of the natural log of 10 (ln(10)). |
| 777 | const double Ln10 = |
| 778 | 0.434294481903251827651128918916605082294397005803666566114; |
| 779 | |
| 780 | auto NewCI = CallInst::Create(NewF, Arg, "", CI); |
| 781 | |
| 782 | return BinaryOperator::Create(Instruction::FMul, |
| 783 | ConstantFP::get(Arg->getType(), Ln10), NewCI, |
| 784 | "", CI); |
| 785 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 786 | } |
| 787 | |
gnl21 | 636e799 | 2020-09-09 16:08:16 +0100 | [diff] [blame] | 788 | bool ReplaceOpenCLBuiltinPass::replaceLog1p(Function &F) { |
| 789 | // convert to natural |
| 790 | std::string NewFName = |
| 791 | Builtins::GetMangledFunctionName("log", F.getFunctionType()); |
| 792 | |
| 793 | Module &M = *F.getParent(); |
| 794 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 795 | auto NewF = M.getOrInsertFunction(NewFName, F.getFunctionType()); |
| 796 | |
| 797 | auto Arg = CI->getOperand(0); |
| 798 | |
| 799 | auto ArgP1 = BinaryOperator::Create( |
| 800 | Instruction::FAdd, ConstantFP::get(Arg->getType(), 1.0), Arg, "", CI); |
| 801 | |
| 802 | return CallInst::Create(NewF, ArgP1, "", CI); |
| 803 | }); |
| 804 | } |
| 805 | |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 806 | bool ReplaceOpenCLBuiltinPass::replaceBarrier(Function &F, bool subgroup) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 807 | |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 808 | enum { |
| 809 | CLK_LOCAL_MEM_FENCE = 0x01, |
| 810 | CLK_GLOBAL_MEM_FENCE = 0x02, |
| 811 | CLK_IMAGE_MEM_FENCE = 0x04 |
| 812 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 813 | |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 814 | return replaceCallsWithValue(F, [subgroup](CallInst *CI) { |
Kévin Petit | c464392 | 2019-06-17 19:32:05 +0100 | [diff] [blame] | 815 | auto Arg = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 816 | |
Kévin Petit | c464392 | 2019-06-17 19:32:05 +0100 | [diff] [blame] | 817 | // We need to map the OpenCL constants to the SPIR-V equivalents. |
| 818 | const auto LocalMemFence = |
| 819 | ConstantInt::get(Arg->getType(), CLK_LOCAL_MEM_FENCE); |
| 820 | const auto GlobalMemFence = |
| 821 | ConstantInt::get(Arg->getType(), CLK_GLOBAL_MEM_FENCE); |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 822 | const auto ImageMemFence = |
| 823 | ConstantInt::get(Arg->getType(), CLK_IMAGE_MEM_FENCE); |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 824 | const auto ConstantAcquireRelease = ConstantInt::get( |
| 825 | Arg->getType(), spv::MemorySemanticsAcquireReleaseMask); |
Kévin Petit | c464392 | 2019-06-17 19:32:05 +0100 | [diff] [blame] | 826 | const auto ConstantScopeDevice = |
| 827 | ConstantInt::get(Arg->getType(), spv::ScopeDevice); |
| 828 | const auto ConstantScopeWorkgroup = |
| 829 | ConstantInt::get(Arg->getType(), spv::ScopeWorkgroup); |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 830 | const auto ConstantScopeSubgroup = |
| 831 | ConstantInt::get(Arg->getType(), spv::ScopeSubgroup); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 832 | |
Kévin Petit | c464392 | 2019-06-17 19:32:05 +0100 | [diff] [blame] | 833 | // Map CLK_LOCAL_MEM_FENCE to MemorySemanticsWorkgroupMemoryMask. |
| 834 | const auto LocalMemFenceMask = |
| 835 | BinaryOperator::Create(Instruction::And, LocalMemFence, Arg, "", CI); |
| 836 | const auto WorkgroupShiftAmount = |
| 837 | clz(spv::MemorySemanticsWorkgroupMemoryMask) - clz(CLK_LOCAL_MEM_FENCE); |
| 838 | const auto MemorySemanticsWorkgroup = BinaryOperator::Create( |
| 839 | Instruction::Shl, LocalMemFenceMask, |
| 840 | ConstantInt::get(Arg->getType(), WorkgroupShiftAmount), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 841 | |
Kévin Petit | c464392 | 2019-06-17 19:32:05 +0100 | [diff] [blame] | 842 | // Map CLK_GLOBAL_MEM_FENCE to MemorySemanticsUniformMemoryMask. |
| 843 | const auto GlobalMemFenceMask = |
| 844 | BinaryOperator::Create(Instruction::And, GlobalMemFence, Arg, "", CI); |
| 845 | const auto UniformShiftAmount = |
| 846 | clz(spv::MemorySemanticsUniformMemoryMask) - clz(CLK_GLOBAL_MEM_FENCE); |
| 847 | const auto MemorySemanticsUniform = BinaryOperator::Create( |
| 848 | Instruction::Shl, GlobalMemFenceMask, |
| 849 | ConstantInt::get(Arg->getType(), UniformShiftAmount), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 850 | |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 851 | // OpenCL 2.0 |
| 852 | // Map CLK_IMAGE_MEM_FENCE to MemorySemanticsImageMemoryMask. |
| 853 | const auto ImageMemFenceMask = |
| 854 | BinaryOperator::Create(Instruction::And, ImageMemFence, Arg, "", CI); |
| 855 | const auto ImageShiftAmount = |
| 856 | clz(spv::MemorySemanticsImageMemoryMask) - clz(CLK_IMAGE_MEM_FENCE); |
| 857 | const auto MemorySemanticsImage = BinaryOperator::Create( |
| 858 | Instruction::Shl, ImageMemFenceMask, |
| 859 | ConstantInt::get(Arg->getType(), ImageShiftAmount), "", CI); |
| 860 | |
Kévin Petit | c464392 | 2019-06-17 19:32:05 +0100 | [diff] [blame] | 861 | // And combine the above together, also adding in |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 862 | // MemorySemanticsSequentiallyConsistentMask. |
| 863 | auto MemorySemantics1 = |
Kévin Petit | c464392 | 2019-06-17 19:32:05 +0100 | [diff] [blame] | 864 | BinaryOperator::Create(Instruction::Or, MemorySemanticsWorkgroup, |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 865 | ConstantAcquireRelease, "", CI); |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 866 | auto MemorySemantics2 = BinaryOperator::Create( |
| 867 | Instruction::Or, MemorySemanticsUniform, MemorySemanticsImage, "", CI); |
| 868 | auto MemorySemantics = BinaryOperator::Create( |
| 869 | Instruction::Or, MemorySemantics1, MemorySemantics2, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 870 | |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 871 | // If the memory scope is not specified explicitly, it is either Subgroup |
| 872 | // or Workgroup depending on the type of barrier. |
| 873 | Value *MemoryScope = |
| 874 | subgroup ? ConstantScopeSubgroup : ConstantScopeWorkgroup; |
| 875 | if (CI->data_operands_size() > 1) { |
| 876 | enum { |
| 877 | CL_MEMORY_SCOPE_WORKGROUP = 0x1, |
| 878 | CL_MEMORY_SCOPE_DEVICE = 0x2, |
| 879 | CL_MEMORY_SCOPE_SUBGROUP = 0x4 |
| 880 | }; |
| 881 | // The call was given an explicit memory scope. |
| 882 | const auto MemoryScopeSubgroup = |
| 883 | ConstantInt::get(Arg->getType(), CL_MEMORY_SCOPE_SUBGROUP); |
| 884 | const auto MemoryScopeDevice = |
| 885 | ConstantInt::get(Arg->getType(), CL_MEMORY_SCOPE_DEVICE); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 886 | |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 887 | auto Cmp = |
| 888 | CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, |
| 889 | MemoryScopeSubgroup, CI->getOperand(1), "", CI); |
| 890 | MemoryScope = SelectInst::Create(Cmp, ConstantScopeSubgroup, |
| 891 | ConstantScopeWorkgroup, "", CI); |
| 892 | Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, |
| 893 | MemoryScopeDevice, CI->getOperand(1), "", CI); |
| 894 | MemoryScope = |
| 895 | SelectInst::Create(Cmp, ConstantScopeDevice, MemoryScope, "", CI); |
| 896 | } |
| 897 | |
| 898 | // Lastly, the Execution Scope is either Workgroup or Subgroup depending on |
| 899 | // the type of barrier; |
| 900 | const auto ExecutionScope = |
| 901 | subgroup ? ConstantScopeSubgroup : ConstantScopeWorkgroup; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 902 | |
Kévin Petit | c464392 | 2019-06-17 19:32:05 +0100 | [diff] [blame] | 903 | return clspv::InsertSPIRVOp(CI, spv::OpControlBarrier, |
alan-baker | 3d90569 | 2020-10-28 14:02:37 -0400 | [diff] [blame] | 904 | {Attribute::NoDuplicate, Attribute::Convergent}, |
| 905 | CI->getType(), |
Kévin Petit | c464392 | 2019-06-17 19:32:05 +0100 | [diff] [blame] | 906 | {ExecutionScope, MemoryScope, MemorySemantics}); |
| 907 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 908 | } |
| 909 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 910 | bool ReplaceOpenCLBuiltinPass::replaceMemFence(Function &F, |
| 911 | uint32_t semantics) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 912 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 913 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 914 | enum { |
| 915 | CLK_LOCAL_MEM_FENCE = 0x01, |
| 916 | CLK_GLOBAL_MEM_FENCE = 0x02, |
| 917 | CLK_IMAGE_MEM_FENCE = 0x04, |
| 918 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 919 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 920 | auto Arg = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 921 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 922 | // We need to map the OpenCL constants to the SPIR-V equivalents. |
| 923 | const auto LocalMemFence = |
| 924 | ConstantInt::get(Arg->getType(), CLK_LOCAL_MEM_FENCE); |
| 925 | const auto GlobalMemFence = |
| 926 | ConstantInt::get(Arg->getType(), CLK_GLOBAL_MEM_FENCE); |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 927 | const auto ImageMemFence = |
| 928 | ConstantInt::get(Arg->getType(), CLK_IMAGE_MEM_FENCE); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 929 | const auto ConstantMemorySemantics = |
| 930 | ConstantInt::get(Arg->getType(), semantics); |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 931 | const auto ConstantScopeWorkgroup = |
| 932 | ConstantInt::get(Arg->getType(), spv::ScopeWorkgroup); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 933 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 934 | // Map CLK_LOCAL_MEM_FENCE to MemorySemanticsWorkgroupMemoryMask. |
| 935 | const auto LocalMemFenceMask = |
| 936 | BinaryOperator::Create(Instruction::And, LocalMemFence, Arg, "", CI); |
| 937 | const auto WorkgroupShiftAmount = |
| 938 | clz(spv::MemorySemanticsWorkgroupMemoryMask) - clz(CLK_LOCAL_MEM_FENCE); |
| 939 | const auto MemorySemanticsWorkgroup = BinaryOperator::Create( |
| 940 | Instruction::Shl, LocalMemFenceMask, |
| 941 | ConstantInt::get(Arg->getType(), WorkgroupShiftAmount), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 942 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 943 | // Map CLK_GLOBAL_MEM_FENCE to MemorySemanticsUniformMemoryMask. |
| 944 | const auto GlobalMemFenceMask = |
| 945 | BinaryOperator::Create(Instruction::And, GlobalMemFence, Arg, "", CI); |
| 946 | const auto UniformShiftAmount = |
| 947 | clz(spv::MemorySemanticsUniformMemoryMask) - clz(CLK_GLOBAL_MEM_FENCE); |
| 948 | const auto MemorySemanticsUniform = BinaryOperator::Create( |
| 949 | Instruction::Shl, GlobalMemFenceMask, |
| 950 | ConstantInt::get(Arg->getType(), UniformShiftAmount), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 951 | |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 952 | // OpenCL 2.0 |
| 953 | // Map CLK_IMAGE_MEM_FENCE to MemorySemanticsImageMemoryMask. |
| 954 | const auto ImageMemFenceMask = |
| 955 | BinaryOperator::Create(Instruction::And, ImageMemFence, Arg, "", CI); |
| 956 | const auto ImageShiftAmount = |
| 957 | clz(spv::MemorySemanticsImageMemoryMask) - clz(CLK_IMAGE_MEM_FENCE); |
| 958 | const auto MemorySemanticsImage = BinaryOperator::Create( |
| 959 | Instruction::Shl, ImageMemFenceMask, |
| 960 | ConstantInt::get(Arg->getType(), ImageShiftAmount), "", CI); |
| 961 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 962 | // And combine the above together, also adding in |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 963 | // |semantics|. |
| 964 | auto MemorySemantics1 = |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 965 | BinaryOperator::Create(Instruction::Or, MemorySemanticsWorkgroup, |
| 966 | ConstantMemorySemantics, "", CI); |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 967 | auto MemorySemantics2 = BinaryOperator::Create( |
| 968 | Instruction::Or, MemorySemanticsUniform, MemorySemanticsImage, "", CI); |
| 969 | auto MemorySemantics = BinaryOperator::Create( |
| 970 | Instruction::Or, MemorySemantics1, MemorySemantics2, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 971 | |
alan-baker | 12d2c18 | 2020-07-20 08:22:42 -0400 | [diff] [blame] | 972 | // Memory Scope is always workgroup. |
| 973 | const auto MemoryScope = ConstantScopeWorkgroup; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 974 | |
alan-baker | 3d90569 | 2020-10-28 14:02:37 -0400 | [diff] [blame] | 975 | return clspv::InsertSPIRVOp(CI, spv::OpMemoryBarrier, |
| 976 | {Attribute::Convergent}, CI->getType(), |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 977 | {MemoryScope, MemorySemantics}); |
| 978 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 979 | } |
| 980 | |
Kévin Petit | 1cb4511 | 2020-04-27 18:55:48 +0100 | [diff] [blame] | 981 | bool ReplaceOpenCLBuiltinPass::replacePrefetch(Function &F) { |
| 982 | bool Changed = false; |
| 983 | |
| 984 | SmallVector<Instruction *, 4> ToRemoves; |
| 985 | |
| 986 | // Find all calls to the function |
| 987 | for (auto &U : F.uses()) { |
| 988 | if (auto CI = dyn_cast<CallInst>(U.getUser())) { |
| 989 | ToRemoves.push_back(CI); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | Changed = !ToRemoves.empty(); |
| 994 | |
| 995 | // Delete them |
| 996 | for (auto V : ToRemoves) { |
| 997 | V->eraseFromParent(); |
| 998 | } |
| 999 | |
| 1000 | return Changed; |
| 1001 | } |
| 1002 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1003 | bool ReplaceOpenCLBuiltinPass::replaceRelational(Function &F, |
| 1004 | CmpInst::Predicate P, |
| 1005 | int32_t C) { |
| 1006 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 1007 | // The predicate to use in the CmpInst. |
| 1008 | auto Predicate = P; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1009 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1010 | // The value to return for true. |
| 1011 | auto TrueValue = ConstantInt::getSigned(CI->getType(), C); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1012 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1013 | // The value to return for false. |
| 1014 | auto FalseValue = Constant::getNullValue(CI->getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1015 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1016 | auto Arg1 = CI->getOperand(0); |
| 1017 | auto Arg2 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1018 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1019 | const auto Cmp = |
| 1020 | CmpInst::Create(Instruction::FCmp, Predicate, Arg1, Arg2, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1021 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1022 | return SelectInst::Create(Cmp, TrueValue, FalseValue, "", CI); |
| 1023 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1024 | } |
| 1025 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1026 | bool ReplaceOpenCLBuiltinPass::replaceIsInfAndIsNan(Function &F, |
| 1027 | spv::Op SPIRVOp, |
| 1028 | int32_t C) { |
| 1029 | Module &M = *F.getParent(); |
| 1030 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 1031 | const auto CITy = CI->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1032 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1033 | // The value to return for true. |
| 1034 | auto TrueValue = ConstantInt::getSigned(CITy, C); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1035 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1036 | // The value to return for false. |
| 1037 | auto FalseValue = Constant::getNullValue(CITy); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1038 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1039 | Type *CorrespondingBoolTy = Type::getInt1Ty(M.getContext()); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1040 | if (auto CIVecTy = dyn_cast<VectorType>(CITy)) { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1041 | CorrespondingBoolTy = |
| 1042 | FixedVectorType::get(Type::getInt1Ty(M.getContext()), |
| 1043 | CIVecTy->getElementCount().getKnownMinValue()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1044 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1045 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1046 | auto NewCI = clspv::InsertSPIRVOp(CI, SPIRVOp, {Attribute::ReadNone}, |
| 1047 | CorrespondingBoolTy, {CI->getOperand(0)}); |
| 1048 | |
| 1049 | return SelectInst::Create(NewCI, TrueValue, FalseValue, "", CI); |
| 1050 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1051 | } |
| 1052 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1053 | bool ReplaceOpenCLBuiltinPass::replaceIsFinite(Function &F) { |
| 1054 | Module &M = *F.getParent(); |
| 1055 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
Kévin Petit | fdfa92e | 2019-09-25 14:20:58 +0100 | [diff] [blame] | 1056 | auto &C = M.getContext(); |
| 1057 | auto Val = CI->getOperand(0); |
| 1058 | auto ValTy = Val->getType(); |
| 1059 | auto RetTy = CI->getType(); |
| 1060 | |
| 1061 | // Get a suitable integer type to represent the number |
| 1062 | auto IntTy = getIntOrIntVectorTyForCast(C, ValTy); |
| 1063 | |
| 1064 | // Create Mask |
| 1065 | auto ScalarSize = ValTy->getScalarSizeInBits(); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1066 | Value *InfMask = nullptr; |
Kévin Petit | fdfa92e | 2019-09-25 14:20:58 +0100 | [diff] [blame] | 1067 | switch (ScalarSize) { |
| 1068 | case 16: |
| 1069 | InfMask = ConstantInt::get(IntTy, 0x7C00U); |
| 1070 | break; |
| 1071 | case 32: |
| 1072 | InfMask = ConstantInt::get(IntTy, 0x7F800000U); |
| 1073 | break; |
| 1074 | case 64: |
| 1075 | InfMask = ConstantInt::get(IntTy, 0x7FF0000000000000ULL); |
| 1076 | break; |
| 1077 | default: |
| 1078 | llvm_unreachable("Unsupported floating-point type"); |
| 1079 | } |
| 1080 | |
| 1081 | IRBuilder<> Builder(CI); |
| 1082 | |
| 1083 | // Bitcast to int |
| 1084 | auto ValInt = Builder.CreateBitCast(Val, IntTy); |
| 1085 | |
| 1086 | // Mask and compare |
| 1087 | auto InfBits = Builder.CreateAnd(InfMask, ValInt); |
| 1088 | auto Cmp = Builder.CreateICmp(CmpInst::ICMP_EQ, InfBits, InfMask); |
| 1089 | |
| 1090 | auto RetFalse = ConstantInt::get(RetTy, 0); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1091 | Value *RetTrue = nullptr; |
Kévin Petit | fdfa92e | 2019-09-25 14:20:58 +0100 | [diff] [blame] | 1092 | if (ValTy->isVectorTy()) { |
| 1093 | RetTrue = ConstantInt::getSigned(RetTy, -1); |
| 1094 | } else { |
| 1095 | RetTrue = ConstantInt::get(RetTy, 1); |
| 1096 | } |
| 1097 | return Builder.CreateSelect(Cmp, RetFalse, RetTrue); |
| 1098 | }); |
| 1099 | } |
| 1100 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1101 | bool ReplaceOpenCLBuiltinPass::replaceAllAndAny(Function &F, spv::Op SPIRVOp) { |
| 1102 | Module &M = *F.getParent(); |
| 1103 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 1104 | auto Arg = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1105 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1106 | Value *V = nullptr; |
Kévin Petit | fd27cca | 2018-10-31 13:00:17 +0000 | [diff] [blame] | 1107 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1108 | // If the argument is a 32-bit int, just use a shift |
| 1109 | if (Arg->getType() == Type::getInt32Ty(M.getContext())) { |
| 1110 | V = BinaryOperator::Create(Instruction::LShr, Arg, |
| 1111 | ConstantInt::get(Arg->getType(), 31), "", CI); |
| 1112 | } else { |
| 1113 | // The value for zero to compare against. |
| 1114 | const auto ZeroValue = Constant::getNullValue(Arg->getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1115 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1116 | // The value to return for true. |
| 1117 | const auto TrueValue = ConstantInt::get(CI->getType(), 1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1118 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1119 | // The value to return for false. |
| 1120 | const auto FalseValue = Constant::getNullValue(CI->getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1121 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1122 | const auto Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_SLT, |
| 1123 | Arg, ZeroValue, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1124 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1125 | Value *SelectSource = nullptr; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1126 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1127 | // If we have a function to call, call it! |
| 1128 | if (SPIRVOp != spv::OpNop) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1129 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1130 | const auto BoolTy = Type::getInt1Ty(M.getContext()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1131 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1132 | const auto NewCI = clspv::InsertSPIRVOp( |
| 1133 | CI, SPIRVOp, {Attribute::ReadNone}, BoolTy, {Cmp}); |
| 1134 | SelectSource = NewCI; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1135 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1136 | } else { |
| 1137 | SelectSource = Cmp; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1138 | } |
| 1139 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1140 | V = SelectInst::Create(SelectSource, TrueValue, FalseValue, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1141 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1142 | return V; |
| 1143 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1144 | } |
| 1145 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1146 | bool ReplaceOpenCLBuiltinPass::replaceUpsample(Function &F) { |
| 1147 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1148 | // Get arguments |
| 1149 | auto HiValue = CI->getOperand(0); |
| 1150 | auto LoValue = CI->getOperand(1); |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 1151 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1152 | // Don't touch overloads that aren't in OpenCL C |
| 1153 | auto HiType = HiValue->getType(); |
| 1154 | auto LoType = LoValue->getType(); |
| 1155 | |
| 1156 | if (HiType != LoType) { |
| 1157 | return nullptr; |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 1158 | } |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 1159 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1160 | if (!HiType->isIntOrIntVectorTy()) { |
| 1161 | return nullptr; |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 1162 | } |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 1163 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1164 | if (HiType->getScalarSizeInBits() * 2 != |
| 1165 | CI->getType()->getScalarSizeInBits()) { |
| 1166 | return nullptr; |
| 1167 | } |
| 1168 | |
| 1169 | if ((HiType->getScalarSizeInBits() != 8) && |
| 1170 | (HiType->getScalarSizeInBits() != 16) && |
| 1171 | (HiType->getScalarSizeInBits() != 32)) { |
| 1172 | return nullptr; |
| 1173 | } |
| 1174 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1175 | if (auto HiVecType = dyn_cast<VectorType>(HiType)) { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1176 | unsigned NumElements = HiVecType->getElementCount().getKnownMinValue(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1177 | if ((NumElements != 2) && (NumElements != 3) && (NumElements != 4) && |
| 1178 | (NumElements != 8) && (NumElements != 16)) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1179 | return nullptr; |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | // Convert both operands to the result type |
| 1184 | auto HiCast = CastInst::CreateZExtOrBitCast(HiValue, CI->getType(), "", CI); |
| 1185 | auto LoCast = CastInst::CreateZExtOrBitCast(LoValue, CI->getType(), "", CI); |
| 1186 | |
| 1187 | // Shift high operand |
| 1188 | auto ShiftAmount = |
| 1189 | ConstantInt::get(CI->getType(), HiType->getScalarSizeInBits()); |
| 1190 | auto HiShifted = |
| 1191 | BinaryOperator::Create(Instruction::Shl, HiCast, ShiftAmount, "", CI); |
| 1192 | |
| 1193 | // OR both results |
| 1194 | return BinaryOperator::Create(Instruction::Or, HiShifted, LoCast, "", CI); |
| 1195 | }); |
Kévin Petit | bf0036c | 2019-03-06 13:57:10 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1198 | bool ReplaceOpenCLBuiltinPass::replaceRotate(Function &F) { |
| 1199 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1200 | // Get arguments |
| 1201 | auto SrcValue = CI->getOperand(0); |
| 1202 | auto RotAmount = CI->getOperand(1); |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 1203 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1204 | // Don't touch overloads that aren't in OpenCL C |
| 1205 | auto SrcType = SrcValue->getType(); |
| 1206 | auto RotType = RotAmount->getType(); |
| 1207 | |
| 1208 | if ((SrcType != RotType) || (CI->getType() != SrcType)) { |
| 1209 | return nullptr; |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 1210 | } |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 1211 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1212 | if (!SrcType->isIntOrIntVectorTy()) { |
| 1213 | return nullptr; |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 1214 | } |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 1215 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1216 | if ((SrcType->getScalarSizeInBits() != 8) && |
| 1217 | (SrcType->getScalarSizeInBits() != 16) && |
| 1218 | (SrcType->getScalarSizeInBits() != 32) && |
| 1219 | (SrcType->getScalarSizeInBits() != 64)) { |
| 1220 | return nullptr; |
| 1221 | } |
| 1222 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1223 | if (auto SrcVecType = dyn_cast<VectorType>(SrcType)) { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1224 | unsigned NumElements = SrcVecType->getElementCount().getKnownMinValue(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1225 | if ((NumElements != 2) && (NumElements != 3) && (NumElements != 4) && |
| 1226 | (NumElements != 8) && (NumElements != 16)) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1227 | return nullptr; |
| 1228 | } |
| 1229 | } |
| 1230 | |
alan-baker | fd22ae1 | 2020-10-29 15:59:22 -0400 | [diff] [blame] | 1231 | // Replace with LLVM's funnel shift left intrinsic because it is more |
| 1232 | // generic than rotate. |
| 1233 | Function *intrinsic = |
| 1234 | Intrinsic::getDeclaration(F.getParent(), Intrinsic::fshl, SrcType); |
| 1235 | return CallInst::Create(intrinsic->getFunctionType(), intrinsic, |
| 1236 | {SrcValue, SrcValue, RotAmount}, "", CI); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1237 | }); |
Kévin Petit | d44eef5 | 2019-03-08 13:22:14 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1240 | bool ReplaceOpenCLBuiltinPass::replaceConvert(Function &F, bool SrcIsSigned, |
| 1241 | bool DstIsSigned) { |
| 1242 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1243 | Value *V = nullptr; |
| 1244 | // Get arguments |
| 1245 | auto SrcValue = CI->getOperand(0); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1246 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1247 | // Don't touch overloads that aren't in OpenCL C |
| 1248 | auto SrcType = SrcValue->getType(); |
| 1249 | auto DstType = CI->getType(); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1250 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1251 | if ((SrcType->isVectorTy() && !DstType->isVectorTy()) || |
| 1252 | (!SrcType->isVectorTy() && DstType->isVectorTy())) { |
| 1253 | return V; |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1256 | if (auto SrcVecType = dyn_cast<VectorType>(SrcType)) { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1257 | unsigned SrcNumElements = |
| 1258 | SrcVecType->getElementCount().getKnownMinValue(); |
| 1259 | unsigned DstNumElements = |
| 1260 | cast<VectorType>(DstType)->getElementCount().getKnownMinValue(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1261 | if (SrcNumElements != DstNumElements) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1262 | return V; |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1265 | if ((SrcNumElements != 2) && (SrcNumElements != 3) && |
| 1266 | (SrcNumElements != 4) && (SrcNumElements != 8) && |
| 1267 | (SrcNumElements != 16)) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1268 | return V; |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1269 | } |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1270 | } |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1271 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1272 | bool SrcIsFloat = SrcType->getScalarType()->isFloatingPointTy(); |
| 1273 | bool DstIsFloat = DstType->getScalarType()->isFloatingPointTy(); |
| 1274 | |
| 1275 | bool SrcIsInt = SrcType->isIntOrIntVectorTy(); |
| 1276 | bool DstIsInt = DstType->isIntOrIntVectorTy(); |
| 1277 | |
| 1278 | if (SrcType == DstType && DstIsSigned == SrcIsSigned) { |
| 1279 | // Unnecessary cast operation. |
| 1280 | V = SrcValue; |
| 1281 | } else if (SrcIsFloat && DstIsFloat) { |
| 1282 | V = CastInst::CreateFPCast(SrcValue, DstType, "", CI); |
| 1283 | } else if (SrcIsFloat && DstIsInt) { |
| 1284 | if (DstIsSigned) { |
| 1285 | V = CastInst::Create(Instruction::FPToSI, SrcValue, DstType, "", CI); |
| 1286 | } else { |
| 1287 | V = CastInst::Create(Instruction::FPToUI, SrcValue, DstType, "", CI); |
| 1288 | } |
| 1289 | } else if (SrcIsInt && DstIsFloat) { |
| 1290 | if (SrcIsSigned) { |
| 1291 | V = CastInst::Create(Instruction::SIToFP, SrcValue, DstType, "", CI); |
| 1292 | } else { |
| 1293 | V = CastInst::Create(Instruction::UIToFP, SrcValue, DstType, "", CI); |
| 1294 | } |
| 1295 | } else if (SrcIsInt && DstIsInt) { |
| 1296 | V = CastInst::CreateIntegerCast(SrcValue, DstType, SrcIsSigned, "", CI); |
| 1297 | } else { |
| 1298 | // Not something we're supposed to handle, just move on |
| 1299 | } |
| 1300 | |
| 1301 | return V; |
| 1302 | }); |
Kévin Petit | 9d1a9d1 | 2019-03-25 15:23:46 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1305 | bool ReplaceOpenCLBuiltinPass::replaceMulHi(Function &F, bool is_signed, |
| 1306 | bool is_mad) { |
| 1307 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1308 | Value *V = nullptr; |
| 1309 | // Get arguments |
| 1310 | auto AValue = CI->getOperand(0); |
| 1311 | auto BValue = CI->getOperand(1); |
| 1312 | auto CValue = CI->getOperand(2); |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1313 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1314 | // Don't touch overloads that aren't in OpenCL C |
| 1315 | auto AType = AValue->getType(); |
| 1316 | auto BType = BValue->getType(); |
| 1317 | auto CType = CValue->getType(); |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1318 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1319 | if ((AType != BType) || (CI->getType() != AType) || |
| 1320 | (is_mad && (AType != CType))) { |
| 1321 | return V; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1324 | if (!AType->isIntOrIntVectorTy()) { |
| 1325 | return V; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1326 | } |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1327 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1328 | if ((AType->getScalarSizeInBits() != 8) && |
| 1329 | (AType->getScalarSizeInBits() != 16) && |
| 1330 | (AType->getScalarSizeInBits() != 32) && |
| 1331 | (AType->getScalarSizeInBits() != 64)) { |
| 1332 | return V; |
| 1333 | } |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1334 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1335 | if (auto AVecType = dyn_cast<VectorType>(AType)) { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1336 | unsigned NumElements = AVecType->getElementCount().getKnownMinValue(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1337 | if ((NumElements != 2) && (NumElements != 3) && (NumElements != 4) && |
| 1338 | (NumElements != 8) && (NumElements != 16)) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1339 | return V; |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1343 | // Our SPIR-V op returns a struct, create a type for it |
alan-baker | 6b9d1ee | 2020-11-03 23:11:32 -0500 | [diff] [blame] | 1344 | auto ExMulRetType = GetPairStruct(AType); |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1345 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1346 | // Select the appropriate signed/unsigned SPIR-V op |
| 1347 | spv::Op opcode = is_signed ? spv::OpSMulExtended : spv::OpUMulExtended; |
| 1348 | |
| 1349 | // Call the SPIR-V op |
| 1350 | auto Call = clspv::InsertSPIRVOp(CI, opcode, {Attribute::ReadNone}, |
| 1351 | ExMulRetType, {AValue, BValue}); |
| 1352 | |
| 1353 | // Get the high part of the result |
| 1354 | unsigned Idxs[] = {1}; |
| 1355 | V = ExtractValueInst::Create(Call, Idxs, "", CI); |
| 1356 | |
| 1357 | // If we're handling a mad_hi, add the third argument to the result |
| 1358 | if (is_mad) { |
| 1359 | V = BinaryOperator::Create(Instruction::Add, V, CValue, "", CI); |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1360 | } |
| 1361 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1362 | return V; |
| 1363 | }); |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1366 | bool ReplaceOpenCLBuiltinPass::replaceSelect(Function &F) { |
| 1367 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1368 | // Get arguments |
| 1369 | auto FalseValue = CI->getOperand(0); |
| 1370 | auto TrueValue = CI->getOperand(1); |
| 1371 | auto PredicateValue = CI->getOperand(2); |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1372 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1373 | // Don't touch overloads that aren't in OpenCL C |
| 1374 | auto FalseType = FalseValue->getType(); |
| 1375 | auto TrueType = TrueValue->getType(); |
| 1376 | auto PredicateType = PredicateValue->getType(); |
| 1377 | |
| 1378 | if (FalseType != TrueType) { |
| 1379 | return nullptr; |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1380 | } |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1381 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1382 | if (!PredicateType->isIntOrIntVectorTy()) { |
| 1383 | return nullptr; |
| 1384 | } |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1385 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1386 | if (!FalseType->isIntOrIntVectorTy() && |
| 1387 | !FalseType->getScalarType()->isFloatingPointTy()) { |
| 1388 | return nullptr; |
| 1389 | } |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1390 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1391 | if (FalseType->isVectorTy() && !PredicateType->isVectorTy()) { |
| 1392 | return nullptr; |
| 1393 | } |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1394 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1395 | if (FalseType->getScalarSizeInBits() != |
| 1396 | PredicateType->getScalarSizeInBits()) { |
| 1397 | return nullptr; |
| 1398 | } |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1399 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1400 | if (auto FalseVecType = dyn_cast<VectorType>(FalseType)) { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1401 | unsigned NumElements = FalseVecType->getElementCount().getKnownMinValue(); |
| 1402 | if (NumElements != cast<VectorType>(PredicateType) |
| 1403 | ->getElementCount() |
| 1404 | .getKnownMinValue()) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1405 | return nullptr; |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1408 | if ((NumElements != 2) && (NumElements != 3) && (NumElements != 4) && |
| 1409 | (NumElements != 8) && (NumElements != 16)) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1410 | return nullptr; |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1411 | } |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1412 | } |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1413 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1414 | // Create constant |
| 1415 | const auto ZeroValue = Constant::getNullValue(PredicateType); |
| 1416 | |
| 1417 | // Scalar and vector are to be treated differently |
| 1418 | CmpInst::Predicate Pred; |
| 1419 | if (PredicateType->isVectorTy()) { |
| 1420 | Pred = CmpInst::ICMP_SLT; |
| 1421 | } else { |
| 1422 | Pred = CmpInst::ICMP_NE; |
| 1423 | } |
| 1424 | |
| 1425 | // Create comparison instruction |
| 1426 | auto Cmp = CmpInst::Create(Instruction::ICmp, Pred, PredicateValue, |
| 1427 | ZeroValue, "", CI); |
| 1428 | |
| 1429 | // Create select |
| 1430 | return SelectInst::Create(Cmp, TrueValue, FalseValue, "", CI); |
| 1431 | }); |
Kévin Petit | f5b78a2 | 2018-10-25 14:32:17 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1434 | bool ReplaceOpenCLBuiltinPass::replaceBitSelect(Function &F) { |
| 1435 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1436 | Value *V = nullptr; |
| 1437 | if (CI->getNumOperands() != 4) { |
| 1438 | return V; |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1439 | } |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1440 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1441 | // Get arguments |
| 1442 | auto FalseValue = CI->getOperand(0); |
| 1443 | auto TrueValue = CI->getOperand(1); |
| 1444 | auto PredicateValue = CI->getOperand(2); |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1445 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1446 | // Don't touch overloads that aren't in OpenCL C |
| 1447 | auto FalseType = FalseValue->getType(); |
| 1448 | auto TrueType = TrueValue->getType(); |
| 1449 | auto PredicateType = PredicateValue->getType(); |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1450 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1451 | if ((FalseType != TrueType) || (PredicateType != TrueType)) { |
| 1452 | return V; |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1453 | } |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1454 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1455 | if (auto TrueVecType = dyn_cast<VectorType>(TrueType)) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1456 | if (!TrueType->getScalarType()->isFloatingPointTy() && |
| 1457 | !TrueType->getScalarType()->isIntegerTy()) { |
| 1458 | return V; |
| 1459 | } |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1460 | unsigned NumElements = TrueVecType->getElementCount().getKnownMinValue(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1461 | if ((NumElements != 2) && (NumElements != 3) && (NumElements != 4) && |
| 1462 | (NumElements != 8) && (NumElements != 16)) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1463 | return V; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | // Remember the type of the operands |
| 1468 | auto OpType = TrueType; |
| 1469 | |
| 1470 | // The actual bit selection will always be done on an integer type, |
| 1471 | // declare it here |
| 1472 | Type *BitType; |
| 1473 | |
| 1474 | // If the operands are float, then bitcast them to int |
| 1475 | if (OpType->getScalarType()->isFloatingPointTy()) { |
| 1476 | |
| 1477 | // First create the new type |
| 1478 | BitType = getIntOrIntVectorTyForCast(F.getContext(), OpType); |
| 1479 | |
| 1480 | // Then bitcast all operands |
| 1481 | PredicateValue = |
| 1482 | CastInst::CreateZExtOrBitCast(PredicateValue, BitType, "", CI); |
| 1483 | FalseValue = CastInst::CreateZExtOrBitCast(FalseValue, BitType, "", CI); |
| 1484 | TrueValue = CastInst::CreateZExtOrBitCast(TrueValue, BitType, "", CI); |
| 1485 | |
| 1486 | } else { |
| 1487 | // The operands have an integer type, use it directly |
| 1488 | BitType = OpType; |
| 1489 | } |
| 1490 | |
| 1491 | // All the operands are now always integers |
| 1492 | // implement as (c & b) | (~c & a) |
| 1493 | |
| 1494 | // Create our negated predicate value |
| 1495 | auto AllOnes = Constant::getAllOnesValue(BitType); |
| 1496 | auto NotPredicateValue = BinaryOperator::Create( |
| 1497 | Instruction::Xor, PredicateValue, AllOnes, "", CI); |
| 1498 | |
| 1499 | // Then put everything together |
| 1500 | auto BitsFalse = BinaryOperator::Create(Instruction::And, NotPredicateValue, |
| 1501 | FalseValue, "", CI); |
| 1502 | auto BitsTrue = BinaryOperator::Create(Instruction::And, PredicateValue, |
| 1503 | TrueValue, "", CI); |
| 1504 | |
| 1505 | V = BinaryOperator::Create(Instruction::Or, BitsFalse, BitsTrue, "", CI); |
| 1506 | |
| 1507 | // If we were dealing with a floating point type, we must bitcast |
| 1508 | // the result back to that |
| 1509 | if (OpType->getScalarType()->isFloatingPointTy()) { |
| 1510 | V = CastInst::CreateZExtOrBitCast(V, OpType, "", CI); |
| 1511 | } |
| 1512 | |
| 1513 | return V; |
| 1514 | }); |
Kévin Petit | e7d0cce | 2018-10-31 12:38:56 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1517 | bool ReplaceOpenCLBuiltinPass::replaceStep(Function &F, bool is_smooth) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1518 | // convert to vector versions |
| 1519 | Module &M = *F.getParent(); |
| 1520 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1521 | SmallVector<Value *, 2> ArgsToSplat = {CI->getOperand(0)}; |
| 1522 | Value *VectorArg = nullptr; |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1523 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1524 | // First figure out which function we're dealing with |
| 1525 | if (is_smooth) { |
| 1526 | ArgsToSplat.push_back(CI->getOperand(1)); |
| 1527 | VectorArg = CI->getOperand(2); |
| 1528 | } else { |
| 1529 | VectorArg = CI->getOperand(1); |
| 1530 | } |
| 1531 | |
| 1532 | // Splat arguments that need to be |
| 1533 | SmallVector<Value *, 2> SplatArgs; |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1534 | auto VecType = cast<VectorType>(VectorArg->getType()); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1535 | |
| 1536 | for (auto arg : ArgsToSplat) { |
| 1537 | Value *NewVectorArg = UndefValue::get(VecType); |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1538 | for (auto i = 0; i < VecType->getElementCount().getKnownMinValue(); i++) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1539 | auto index = ConstantInt::get(Type::getInt32Ty(M.getContext()), i); |
| 1540 | NewVectorArg = |
| 1541 | InsertElementInst::Create(NewVectorArg, arg, index, "", CI); |
| 1542 | } |
| 1543 | SplatArgs.push_back(NewVectorArg); |
| 1544 | } |
| 1545 | |
| 1546 | // Replace the call with the vector/vector flavour |
| 1547 | SmallVector<Type *, 3> NewArgTypes(ArgsToSplat.size() + 1, VecType); |
| 1548 | const auto NewFType = FunctionType::get(CI->getType(), NewArgTypes, false); |
| 1549 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1550 | std::string NewFName = Builtins::GetMangledFunctionName( |
| 1551 | is_smooth ? "smoothstep" : "step", NewFType); |
| 1552 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1553 | const auto NewF = M.getOrInsertFunction(NewFName, NewFType); |
| 1554 | |
| 1555 | SmallVector<Value *, 3> NewArgs; |
| 1556 | for (auto arg : SplatArgs) { |
| 1557 | NewArgs.push_back(arg); |
| 1558 | } |
| 1559 | NewArgs.push_back(VectorArg); |
| 1560 | |
| 1561 | return CallInst::Create(NewF, NewArgs, "", CI); |
| 1562 | }); |
Kévin Petit | 6b0a953 | 2018-10-30 20:00:39 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1565 | bool ReplaceOpenCLBuiltinPass::replaceSignbit(Function &F, bool is_vec) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1566 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1567 | auto Arg = CI->getOperand(0); |
| 1568 | auto Op = is_vec ? Instruction::AShr : Instruction::LShr; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1569 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1570 | auto Bitcast = CastInst::CreateZExtOrBitCast(Arg, CI->getType(), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1571 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1572 | return BinaryOperator::Create(Op, Bitcast, |
| 1573 | ConstantInt::get(CI->getType(), 31), "", CI); |
| 1574 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1575 | } |
| 1576 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1577 | bool ReplaceOpenCLBuiltinPass::replaceMul(Function &F, bool is_float, |
| 1578 | bool is_mad) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1579 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1580 | // The multiply instruction to use. |
| 1581 | auto MulInst = is_float ? Instruction::FMul : Instruction::Mul; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1582 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1583 | SmallVector<Value *, 8> Args(CI->arg_begin(), CI->arg_end()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1584 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1585 | Value *V = BinaryOperator::Create(MulInst, CI->getArgOperand(0), |
| 1586 | CI->getArgOperand(1), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1587 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1588 | if (is_mad) { |
| 1589 | // The add instruction to use. |
| 1590 | auto AddInst = is_float ? Instruction::FAdd : Instruction::Add; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1591 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1592 | V = BinaryOperator::Create(AddInst, V, CI->getArgOperand(2), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1593 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1594 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1595 | return V; |
| 1596 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1597 | } |
| 1598 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1599 | bool ReplaceOpenCLBuiltinPass::replaceVstore(Function &F) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1600 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1601 | Value *V = nullptr; |
| 1602 | auto data = CI->getOperand(0); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1603 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1604 | auto data_type = data->getType(); |
| 1605 | if (!data_type->isVectorTy()) |
| 1606 | return V; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1607 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1608 | auto vec_data_type = cast<VectorType>(data_type); |
| 1609 | |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1610 | auto elems = vec_data_type->getElementCount().getKnownMinValue(); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1611 | if (elems != 2 && elems != 3 && elems != 4 && elems != 8 && elems != 16) |
| 1612 | return V; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1613 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1614 | auto offset = CI->getOperand(1); |
| 1615 | auto ptr = CI->getOperand(2); |
| 1616 | auto ptr_type = ptr->getType(); |
| 1617 | auto pointee_type = ptr_type->getPointerElementType(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1618 | if (pointee_type != vec_data_type->getElementType()) |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1619 | return V; |
alan-baker | f795f39 | 2019-06-11 18:24:34 -0400 | [diff] [blame] | 1620 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1621 | // Avoid pointer casts. Instead generate the correct number of stores |
| 1622 | // and rely on drivers to coalesce appropriately. |
| 1623 | IRBuilder<> builder(CI); |
| 1624 | auto elems_const = builder.getInt32(elems); |
| 1625 | auto adjust = builder.CreateMul(offset, elems_const); |
| 1626 | for (auto i = 0; i < elems; ++i) { |
| 1627 | auto idx = builder.getInt32(i); |
| 1628 | auto add = builder.CreateAdd(adjust, idx); |
| 1629 | auto gep = builder.CreateGEP(ptr, add); |
| 1630 | auto extract = builder.CreateExtractElement(data, i); |
| 1631 | V = builder.CreateStore(extract, gep); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1632 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1633 | return V; |
| 1634 | }); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1637 | bool ReplaceOpenCLBuiltinPass::replaceVload(Function &F) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1638 | return replaceCallsWithValue(F, [&](CallInst *CI) -> llvm::Value * { |
| 1639 | Value *V = nullptr; |
| 1640 | auto ret_type = F.getReturnType(); |
| 1641 | if (!ret_type->isVectorTy()) |
| 1642 | return V; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1643 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1644 | auto vec_ret_type = cast<VectorType>(ret_type); |
| 1645 | |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1646 | auto elems = vec_ret_type->getElementCount().getKnownMinValue(); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1647 | if (elems != 2 && elems != 3 && elems != 4 && elems != 8 && elems != 16) |
| 1648 | return V; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1649 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1650 | auto offset = CI->getOperand(0); |
| 1651 | auto ptr = CI->getOperand(1); |
| 1652 | auto ptr_type = ptr->getType(); |
| 1653 | auto pointee_type = ptr_type->getPointerElementType(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 1654 | if (pointee_type != vec_ret_type->getElementType()) |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1655 | return V; |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1656 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1657 | // Avoid pointer casts. Instead generate the correct number of loads |
| 1658 | // and rely on drivers to coalesce appropriately. |
| 1659 | IRBuilder<> builder(CI); |
| 1660 | auto elems_const = builder.getInt32(elems); |
| 1661 | V = UndefValue::get(ret_type); |
| 1662 | auto adjust = builder.CreateMul(offset, elems_const); |
| 1663 | for (auto i = 0; i < elems; ++i) { |
| 1664 | auto idx = builder.getInt32(i); |
| 1665 | auto add = builder.CreateAdd(adjust, idx); |
| 1666 | auto gep = builder.CreateGEP(ptr, add); |
| 1667 | auto load = builder.CreateLoad(gep); |
| 1668 | V = builder.CreateInsertElement(V, load, i); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1669 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1670 | return V; |
| 1671 | }); |
Derek Chow | cfd368b | 2017-10-19 20:58:45 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1674 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf(Function &F, |
| 1675 | const std::string &name, |
| 1676 | int vec_size) { |
| 1677 | bool is_clspv_version = !name.compare(0, 8, "__clspv_"); |
| 1678 | if (!vec_size) { |
| 1679 | // deduce vec_size from last character of name (e.g. vload_half4) |
| 1680 | vec_size = std::atoi(&name.back()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1681 | } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1682 | switch (vec_size) { |
| 1683 | case 2: |
| 1684 | return is_clspv_version ? replaceClspvVloadaHalf2(F) : replaceVloadHalf2(F); |
| 1685 | case 4: |
| 1686 | return is_clspv_version ? replaceClspvVloadaHalf4(F) : replaceVloadHalf4(F); |
| 1687 | case 0: |
| 1688 | if (!is_clspv_version) { |
| 1689 | return replaceVloadHalf(F); |
| 1690 | } |
| 1691 | default: |
| 1692 | llvm_unreachable("Unsupported vload_half vector size"); |
| 1693 | break; |
| 1694 | } |
| 1695 | return false; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1696 | } |
| 1697 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1698 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf(Function &F) { |
| 1699 | Module &M = *F.getParent(); |
| 1700 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 1701 | // The index argument from vload_half. |
| 1702 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1703 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1704 | // The pointer argument from vload_half. |
| 1705 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1706 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1707 | auto IntTy = Type::getInt32Ty(M.getContext()); |
alan-baker | b3e2b6d | 2020-06-24 23:59:57 -0400 | [diff] [blame] | 1708 | auto Float2Ty = FixedVectorType::get(Type::getFloatTy(M.getContext()), 2); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1709 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
| 1710 | |
| 1711 | // Our intrinsic to unpack a float2 from an int. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1712 | auto SPIRVIntrinsic = clspv::UnpackFunction(); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1713 | |
| 1714 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
| 1715 | |
| 1716 | Value *V = nullptr; |
| 1717 | |
alan-baker | 7efcaaa | 2020-05-06 19:33:27 -0400 | [diff] [blame] | 1718 | bool supports_16bit_storage = true; |
| 1719 | switch (Arg1->getType()->getPointerAddressSpace()) { |
| 1720 | case clspv::AddressSpace::Global: |
| 1721 | supports_16bit_storage = clspv::Option::Supports16BitStorageClass( |
| 1722 | clspv::Option::StorageClass::kSSBO); |
| 1723 | break; |
| 1724 | case clspv::AddressSpace::Constant: |
| 1725 | if (clspv::Option::ConstantArgsInUniformBuffer()) |
| 1726 | supports_16bit_storage = clspv::Option::Supports16BitStorageClass( |
| 1727 | clspv::Option::StorageClass::kUBO); |
| 1728 | else |
| 1729 | supports_16bit_storage = clspv::Option::Supports16BitStorageClass( |
| 1730 | clspv::Option::StorageClass::kSSBO); |
| 1731 | break; |
| 1732 | default: |
| 1733 | // Clspv will emit the Float16 capability if the half type is |
| 1734 | // encountered. That capability covers private and local addressspaces. |
| 1735 | break; |
| 1736 | } |
| 1737 | |
| 1738 | if (supports_16bit_storage) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1739 | auto ShortTy = Type::getInt16Ty(M.getContext()); |
| 1740 | auto ShortPointerTy = |
| 1741 | PointerType::get(ShortTy, Arg1->getType()->getPointerAddressSpace()); |
| 1742 | |
| 1743 | // Cast the half* pointer to short*. |
| 1744 | auto Cast = CastInst::CreatePointerCast(Arg1, ShortPointerTy, "", CI); |
| 1745 | |
| 1746 | // Index into the correct address of the casted pointer. |
| 1747 | auto Index = GetElementPtrInst::Create(ShortTy, Cast, Arg0, "", CI); |
| 1748 | |
| 1749 | // Load from the short* we casted to. |
alan-baker | 741fd1f | 2020-04-14 17:38:15 -0400 | [diff] [blame] | 1750 | auto Load = new LoadInst(ShortTy, Index, "", CI); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1751 | |
| 1752 | // ZExt the short -> int. |
| 1753 | auto ZExt = CastInst::CreateZExtOrBitCast(Load, IntTy, "", CI); |
| 1754 | |
| 1755 | // Get our float2. |
| 1756 | auto Call = CallInst::Create(NewF, ZExt, "", CI); |
| 1757 | |
| 1758 | // Extract out the bottom element which is our float result. |
| 1759 | V = ExtractElementInst::Create(Call, ConstantInt::get(IntTy, 0), "", CI); |
| 1760 | } else { |
| 1761 | // Assume the pointer argument points to storage aligned to 32bits |
| 1762 | // or more. |
| 1763 | // TODO(dneto): Do more analysis to make sure this is true? |
| 1764 | // |
| 1765 | // Replace call vstore_half(i32 %index, half addrspace(1) %base) |
| 1766 | // with: |
| 1767 | // |
| 1768 | // %base_i32_ptr = bitcast half addrspace(1)* %base to i32 |
| 1769 | // addrspace(1)* %index_is_odd32 = and i32 %index, 1 %index_i32 = |
| 1770 | // lshr i32 %index, 1 %in_ptr = getlementptr i32, i32 |
| 1771 | // addrspace(1)* %base_i32_ptr, %index_i32 %value_i32 = load i32, |
| 1772 | // i32 addrspace(1)* %in_ptr %converted = call <2 x float> |
| 1773 | // @spirv.unpack.v2f16(i32 %value_i32) %value = extractelement <2 |
| 1774 | // x float> %converted, %index_is_odd32 |
| 1775 | |
| 1776 | auto IntPointerTy = |
| 1777 | PointerType::get(IntTy, Arg1->getType()->getPointerAddressSpace()); |
| 1778 | |
| 1779 | // Cast the base pointer to int*. |
| 1780 | // In a valid call (according to assumptions), this should get |
| 1781 | // optimized away in the simplify GEP pass. |
| 1782 | auto Cast = CastInst::CreatePointerCast(Arg1, IntPointerTy, "", CI); |
| 1783 | |
| 1784 | auto One = ConstantInt::get(IntTy, 1); |
| 1785 | auto IndexIsOdd = BinaryOperator::CreateAnd(Arg0, One, "", CI); |
| 1786 | auto IndexIntoI32 = BinaryOperator::CreateLShr(Arg0, One, "", CI); |
| 1787 | |
| 1788 | // Index into the correct address of the casted pointer. |
| 1789 | auto Ptr = GetElementPtrInst::Create(IntTy, Cast, IndexIntoI32, "", CI); |
| 1790 | |
| 1791 | // Load from the int* we casted to. |
alan-baker | 741fd1f | 2020-04-14 17:38:15 -0400 | [diff] [blame] | 1792 | auto Load = new LoadInst(IntTy, Ptr, "", CI); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1793 | |
| 1794 | // Get our float2. |
| 1795 | auto Call = CallInst::Create(NewF, Load, "", CI); |
| 1796 | |
| 1797 | // Extract out the float result, where the element number is |
| 1798 | // determined by whether the original index was even or odd. |
| 1799 | V = ExtractElementInst::Create(Call, IndexIsOdd, "", CI); |
| 1800 | } |
| 1801 | return V; |
| 1802 | }); |
| 1803 | } |
| 1804 | |
| 1805 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf2(Function &F) { |
| 1806 | Module &M = *F.getParent(); |
| 1807 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1808 | // The index argument from vload_half. |
| 1809 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1810 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1811 | // The pointer argument from vload_half. |
| 1812 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1813 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1814 | auto IntTy = Type::getInt32Ty(M.getContext()); |
alan-baker | b3e2b6d | 2020-06-24 23:59:57 -0400 | [diff] [blame] | 1815 | auto Float2Ty = FixedVectorType::get(Type::getFloatTy(M.getContext()), 2); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1816 | auto NewPointerTy = |
| 1817 | PointerType::get(IntTy, Arg1->getType()->getPointerAddressSpace()); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1818 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1819 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1820 | // Cast the half* pointer to int*. |
| 1821 | auto Cast = CastInst::CreatePointerCast(Arg1, NewPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1822 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1823 | // Index into the correct address of the casted pointer. |
| 1824 | auto Index = GetElementPtrInst::Create(IntTy, Cast, Arg0, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1825 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1826 | // Load from the int* we casted to. |
alan-baker | 741fd1f | 2020-04-14 17:38:15 -0400 | [diff] [blame] | 1827 | auto Load = new LoadInst(IntTy, Index, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1828 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1829 | // Our intrinsic to unpack a float2 from an int. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1830 | auto SPIRVIntrinsic = clspv::UnpackFunction(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1831 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1832 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1833 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1834 | // Get our float2. |
| 1835 | return CallInst::Create(NewF, Load, "", CI); |
| 1836 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1837 | } |
| 1838 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1839 | bool ReplaceOpenCLBuiltinPass::replaceVloadHalf4(Function &F) { |
| 1840 | Module &M = *F.getParent(); |
| 1841 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1842 | // The index argument from vload_half. |
| 1843 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1844 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1845 | // The pointer argument from vload_half. |
| 1846 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1847 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1848 | auto IntTy = Type::getInt32Ty(M.getContext()); |
alan-baker | b3e2b6d | 2020-06-24 23:59:57 -0400 | [diff] [blame] | 1849 | auto Int2Ty = FixedVectorType::get(IntTy, 2); |
| 1850 | auto Float2Ty = FixedVectorType::get(Type::getFloatTy(M.getContext()), 2); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1851 | auto NewPointerTy = |
| 1852 | PointerType::get(Int2Ty, Arg1->getType()->getPointerAddressSpace()); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1853 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1854 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1855 | // Cast the half* pointer to int2*. |
| 1856 | auto Cast = CastInst::CreatePointerCast(Arg1, NewPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1857 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1858 | // Index into the correct address of the casted pointer. |
| 1859 | auto Index = GetElementPtrInst::Create(Int2Ty, Cast, Arg0, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1860 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1861 | // Load from the int2* we casted to. |
alan-baker | 741fd1f | 2020-04-14 17:38:15 -0400 | [diff] [blame] | 1862 | auto Load = new LoadInst(Int2Ty, Index, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1863 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1864 | // Extract each element from the loaded int2. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1865 | auto X = |
| 1866 | ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 0), "", CI); |
| 1867 | auto Y = |
| 1868 | ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 1), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1869 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1870 | // Our intrinsic to unpack a float2 from an int. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1871 | auto SPIRVIntrinsic = clspv::UnpackFunction(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1872 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1873 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1874 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1875 | // Get the lower (x & y) components of our final float4. |
| 1876 | auto Lo = CallInst::Create(NewF, X, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1877 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1878 | // Get the higher (z & w) components of our final float4. |
| 1879 | auto Hi = CallInst::Create(NewF, Y, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1880 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1881 | Constant *ShuffleMask[4] = { |
| 1882 | ConstantInt::get(IntTy, 0), ConstantInt::get(IntTy, 1), |
| 1883 | ConstantInt::get(IntTy, 2), ConstantInt::get(IntTy, 3)}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1884 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1885 | // Combine our two float2's into one float4. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1886 | return new ShuffleVectorInst(Lo, Hi, ConstantVector::get(ShuffleMask), "", |
| 1887 | CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1888 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1889 | } |
| 1890 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1891 | bool ReplaceOpenCLBuiltinPass::replaceClspvVloadaHalf2(Function &F) { |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1892 | |
| 1893 | // Replace __clspv_vloada_half2(uint Index, global uint* Ptr) with: |
| 1894 | // |
| 1895 | // %u = load i32 %ptr |
| 1896 | // %fxy = call <2 x float> Unpack2xHalf(u) |
| 1897 | // %result = shufflevector %fxy %fzw <4 x i32> <0, 1, 2, 3> |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1898 | Module &M = *F.getParent(); |
| 1899 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1900 | auto Index = CI->getOperand(0); |
| 1901 | auto Ptr = CI->getOperand(1); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1902 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1903 | auto IntTy = Type::getInt32Ty(M.getContext()); |
alan-baker | b3e2b6d | 2020-06-24 23:59:57 -0400 | [diff] [blame] | 1904 | auto Float2Ty = FixedVectorType::get(Type::getFloatTy(M.getContext()), 2); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1905 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1906 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1907 | auto IndexedPtr = GetElementPtrInst::Create(IntTy, Ptr, Index, "", CI); |
alan-baker | 741fd1f | 2020-04-14 17:38:15 -0400 | [diff] [blame] | 1908 | auto Load = new LoadInst(IntTy, IndexedPtr, "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1909 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1910 | // Our intrinsic to unpack a float2 from an int. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1911 | auto SPIRVIntrinsic = clspv::UnpackFunction(); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1912 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1913 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1914 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1915 | // Get our final float2. |
| 1916 | return CallInst::Create(NewF, Load, "", CI); |
| 1917 | }); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1920 | bool ReplaceOpenCLBuiltinPass::replaceClspvVloadaHalf4(Function &F) { |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1921 | |
| 1922 | // Replace __clspv_vloada_half4(uint Index, global uint2* Ptr) with: |
| 1923 | // |
| 1924 | // %u2 = load <2 x i32> %ptr |
| 1925 | // %u2xy = extractelement %u2, 0 |
| 1926 | // %u2zw = extractelement %u2, 1 |
| 1927 | // %fxy = call <2 x float> Unpack2xHalf(uint) |
| 1928 | // %fzw = call <2 x float> Unpack2xHalf(uint) |
| 1929 | // %result = shufflevector %fxy %fzw <4 x i32> <0, 1, 2, 3> |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1930 | Module &M = *F.getParent(); |
| 1931 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1932 | auto Index = CI->getOperand(0); |
| 1933 | auto Ptr = CI->getOperand(1); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1934 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1935 | auto IntTy = Type::getInt32Ty(M.getContext()); |
alan-baker | b3e2b6d | 2020-06-24 23:59:57 -0400 | [diff] [blame] | 1936 | auto Int2Ty = FixedVectorType::get(IntTy, 2); |
| 1937 | auto Float2Ty = FixedVectorType::get(Type::getFloatTy(M.getContext()), 2); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1938 | auto NewFType = FunctionType::get(Float2Ty, IntTy, false); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1939 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1940 | auto IndexedPtr = GetElementPtrInst::Create(Int2Ty, Ptr, Index, "", CI); |
alan-baker | 741fd1f | 2020-04-14 17:38:15 -0400 | [diff] [blame] | 1941 | auto Load = new LoadInst(Int2Ty, IndexedPtr, "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1942 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1943 | // Extract each element from the loaded int2. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1944 | auto X = |
| 1945 | ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 0), "", CI); |
| 1946 | auto Y = |
| 1947 | ExtractElementInst::Create(Load, ConstantInt::get(IntTy, 1), "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1948 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1949 | // Our intrinsic to unpack a float2 from an int. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1950 | auto SPIRVIntrinsic = clspv::UnpackFunction(); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1951 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1952 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1953 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1954 | // Get the lower (x & y) components of our final float4. |
| 1955 | auto Lo = CallInst::Create(NewF, X, "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1956 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1957 | // Get the higher (z & w) components of our final float4. |
| 1958 | auto Hi = CallInst::Create(NewF, Y, "", CI); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1959 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1960 | Constant *ShuffleMask[4] = { |
| 1961 | ConstantInt::get(IntTy, 0), ConstantInt::get(IntTy, 1), |
| 1962 | ConstantInt::get(IntTy, 2), ConstantInt::get(IntTy, 3)}; |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1963 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1964 | // Combine our two float2's into one float4. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1965 | return new ShuffleVectorInst(Lo, Hi, ConstantVector::get(ShuffleMask), "", |
| 1966 | CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1967 | }); |
David Neto | 6ad9323 | 2018-06-07 15:42:58 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1970 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf(Function &F, int vec_size) { |
| 1971 | switch (vec_size) { |
| 1972 | case 0: |
| 1973 | return replaceVstoreHalf(F); |
| 1974 | case 2: |
| 1975 | return replaceVstoreHalf2(F); |
| 1976 | case 4: |
| 1977 | return replaceVstoreHalf4(F); |
| 1978 | default: |
| 1979 | llvm_unreachable("Unsupported vstore_half vector size"); |
| 1980 | break; |
| 1981 | } |
| 1982 | return false; |
| 1983 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1984 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 1985 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf(Function &F) { |
| 1986 | Module &M = *F.getParent(); |
| 1987 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1988 | // The value to store. |
| 1989 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1990 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1991 | // The index argument from vstore_half. |
| 1992 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1993 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1994 | // The pointer argument from vstore_half. |
| 1995 | auto Arg2 = CI->getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1996 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1997 | auto IntTy = Type::getInt32Ty(M.getContext()); |
alan-baker | b3e2b6d | 2020-06-24 23:59:57 -0400 | [diff] [blame] | 1998 | auto Float2Ty = FixedVectorType::get(Type::getFloatTy(M.getContext()), 2); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 1999 | auto NewFType = FunctionType::get(IntTy, Float2Ty, false); |
| 2000 | auto One = ConstantInt::get(IntTy, 1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2001 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2002 | // Our intrinsic to pack a float2 to an int. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2003 | auto SPIRVIntrinsic = clspv::PackFunction(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2004 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2005 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2006 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2007 | // Insert our value into a float2 so that we can pack it. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2008 | auto TempVec = InsertElementInst::Create( |
| 2009 | UndefValue::get(Float2Ty), Arg0, ConstantInt::get(IntTy, 0), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2010 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2011 | // Pack the float2 -> half2 (in an int). |
| 2012 | auto X = CallInst::Create(NewF, TempVec, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2013 | |
alan-baker | 7efcaaa | 2020-05-06 19:33:27 -0400 | [diff] [blame] | 2014 | bool supports_16bit_storage = true; |
| 2015 | switch (Arg2->getType()->getPointerAddressSpace()) { |
| 2016 | case clspv::AddressSpace::Global: |
| 2017 | supports_16bit_storage = clspv::Option::Supports16BitStorageClass( |
| 2018 | clspv::Option::StorageClass::kSSBO); |
| 2019 | break; |
| 2020 | case clspv::AddressSpace::Constant: |
| 2021 | if (clspv::Option::ConstantArgsInUniformBuffer()) |
| 2022 | supports_16bit_storage = clspv::Option::Supports16BitStorageClass( |
| 2023 | clspv::Option::StorageClass::kUBO); |
| 2024 | else |
| 2025 | supports_16bit_storage = clspv::Option::Supports16BitStorageClass( |
| 2026 | clspv::Option::StorageClass::kSSBO); |
| 2027 | break; |
| 2028 | default: |
| 2029 | // Clspv will emit the Float16 capability if the half type is |
| 2030 | // encountered. That capability covers private and local addressspaces. |
| 2031 | break; |
| 2032 | } |
| 2033 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2034 | Value *V = nullptr; |
alan-baker | 7efcaaa | 2020-05-06 19:33:27 -0400 | [diff] [blame] | 2035 | if (supports_16bit_storage) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2036 | auto ShortTy = Type::getInt16Ty(M.getContext()); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2037 | auto ShortPointerTy = |
| 2038 | PointerType::get(ShortTy, Arg2->getType()->getPointerAddressSpace()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2039 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2040 | // Truncate our i32 to an i16. |
| 2041 | auto Trunc = CastInst::CreateTruncOrBitCast(X, ShortTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2042 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2043 | // Cast the half* pointer to short*. |
| 2044 | auto Cast = CastInst::CreatePointerCast(Arg2, ShortPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2045 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2046 | // Index into the correct address of the casted pointer. |
| 2047 | auto Index = GetElementPtrInst::Create(ShortTy, Cast, Arg1, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2048 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2049 | // Store to the int* we casted to. |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2050 | V = new StoreInst(Trunc, Index, CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2051 | } else { |
| 2052 | // We can only write to 32-bit aligned words. |
| 2053 | // |
| 2054 | // Assuming base is aligned to 32-bits, replace the equivalent of |
| 2055 | // vstore_half(value, index, base) |
| 2056 | // with: |
| 2057 | // uint32_t* target_ptr = (uint32_t*)(base) + index / 2; |
| 2058 | // uint32_t write_to_upper_half = index & 1u; |
| 2059 | // uint32_t shift = write_to_upper_half << 4; |
| 2060 | // |
| 2061 | // // Pack the float value as a half number in bottom 16 bits |
| 2062 | // // of an i32. |
| 2063 | // uint32_t packed = spirv.pack.v2f16((float2)(value, undef)); |
| 2064 | // |
| 2065 | // uint32_t xor_value = (*target_ptr & (0xffff << shift)) |
| 2066 | // ^ ((packed & 0xffff) << shift) |
| 2067 | // // We only need relaxed consistency, but OpenCL 1.2 only has |
| 2068 | // // sequentially consistent atomics. |
| 2069 | // // TODO(dneto): Use relaxed consistency. |
| 2070 | // atomic_xor(target_ptr, xor_value) |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2071 | auto IntPointerTy = |
| 2072 | PointerType::get(IntTy, Arg2->getType()->getPointerAddressSpace()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2073 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2074 | auto Four = ConstantInt::get(IntTy, 4); |
| 2075 | auto FFFF = ConstantInt::get(IntTy, 0xffff); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2076 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2077 | auto IndexIsOdd = |
| 2078 | BinaryOperator::CreateAnd(Arg1, One, "index_is_odd_i32", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2079 | // Compute index / 2 |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2080 | auto IndexIntoI32 = |
| 2081 | BinaryOperator::CreateLShr(Arg1, One, "index_into_i32", CI); |
| 2082 | auto BaseI32Ptr = |
| 2083 | CastInst::CreatePointerCast(Arg2, IntPointerTy, "base_i32_ptr", CI); |
| 2084 | auto OutPtr = GetElementPtrInst::Create(IntTy, BaseI32Ptr, IndexIntoI32, |
| 2085 | "base_i32_ptr", CI); |
alan-baker | 741fd1f | 2020-04-14 17:38:15 -0400 | [diff] [blame] | 2086 | auto CurrentValue = new LoadInst(IntTy, OutPtr, "current_value", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2087 | auto Shift = BinaryOperator::CreateShl(IndexIsOdd, Four, "shift", CI); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2088 | auto MaskBitsToWrite = |
| 2089 | BinaryOperator::CreateShl(FFFF, Shift, "mask_bits_to_write", CI); |
| 2090 | auto MaskedCurrent = BinaryOperator::CreateAnd( |
| 2091 | MaskBitsToWrite, CurrentValue, "masked_current", CI); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2092 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2093 | auto XLowerBits = |
| 2094 | BinaryOperator::CreateAnd(X, FFFF, "lower_bits_of_packed", CI); |
| 2095 | auto NewBitsToWrite = |
| 2096 | BinaryOperator::CreateShl(XLowerBits, Shift, "new_bits_to_write", CI); |
| 2097 | auto ValueToXor = BinaryOperator::CreateXor(MaskedCurrent, NewBitsToWrite, |
| 2098 | "value_to_xor", CI); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2099 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2100 | // Generate the call to atomi_xor. |
| 2101 | SmallVector<Type *, 5> ParamTypes; |
| 2102 | // The pointer type. |
| 2103 | ParamTypes.push_back(IntPointerTy); |
| 2104 | // The Types for memory scope, semantics, and value. |
| 2105 | ParamTypes.push_back(IntTy); |
| 2106 | ParamTypes.push_back(IntTy); |
| 2107 | ParamTypes.push_back(IntTy); |
| 2108 | auto NewFType = FunctionType::get(IntTy, ParamTypes, false); |
| 2109 | auto NewF = M.getOrInsertFunction("spirv.atomic_xor", NewFType); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2110 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2111 | const auto ConstantScopeDevice = |
| 2112 | ConstantInt::get(IntTy, spv::ScopeDevice); |
| 2113 | // Assume the pointee is in OpenCL global (SPIR-V Uniform) or local |
| 2114 | // (SPIR-V Workgroup). |
| 2115 | const auto AddrSpaceSemanticsBits = |
| 2116 | IntPointerTy->getPointerAddressSpace() == 1 |
| 2117 | ? spv::MemorySemanticsUniformMemoryMask |
| 2118 | : spv::MemorySemanticsWorkgroupMemoryMask; |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2119 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2120 | // We're using relaxed consistency here. |
| 2121 | const auto ConstantMemorySemantics = |
| 2122 | ConstantInt::get(IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 2123 | AddrSpaceSemanticsBits); |
David Neto | 17852de | 2017-05-29 17:29:31 -0400 | [diff] [blame] | 2124 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2125 | SmallVector<Value *, 5> Params{OutPtr, ConstantScopeDevice, |
| 2126 | ConstantMemorySemantics, ValueToXor}; |
| 2127 | CallInst::Create(NewF, Params, "store_halfword_xor_trick", CI); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2128 | |
| 2129 | // Return a Nop so the old Call is removed |
| 2130 | Function *donothing = Intrinsic::getDeclaration(&M, Intrinsic::donothing); |
| 2131 | V = CallInst::Create(donothing, {}, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2132 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2133 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2134 | return V; |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2135 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2136 | } |
| 2137 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2138 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf2(Function &F) { |
| 2139 | Module &M = *F.getParent(); |
| 2140 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2141 | // The value to store. |
| 2142 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2143 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2144 | // The index argument from vstore_half. |
| 2145 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2146 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2147 | // The pointer argument from vstore_half. |
| 2148 | auto Arg2 = CI->getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2149 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2150 | auto IntTy = Type::getInt32Ty(M.getContext()); |
alan-baker | b3e2b6d | 2020-06-24 23:59:57 -0400 | [diff] [blame] | 2151 | auto Float2Ty = FixedVectorType::get(Type::getFloatTy(M.getContext()), 2); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2152 | auto NewPointerTy = |
| 2153 | PointerType::get(IntTy, Arg2->getType()->getPointerAddressSpace()); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2154 | auto NewFType = FunctionType::get(IntTy, Float2Ty, false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2155 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2156 | // Our intrinsic to pack a float2 to an int. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2157 | auto SPIRVIntrinsic = clspv::PackFunction(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2158 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2159 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2160 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2161 | // Turn the packed x & y into the final packing. |
| 2162 | auto X = CallInst::Create(NewF, Arg0, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2163 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2164 | // Cast the half* pointer to int*. |
| 2165 | auto Cast = CastInst::CreatePointerCast(Arg2, NewPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2166 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2167 | // Index into the correct address of the casted pointer. |
| 2168 | auto Index = GetElementPtrInst::Create(IntTy, Cast, Arg1, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2169 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2170 | // Store to the int* we casted to. |
| 2171 | return new StoreInst(X, Index, CI); |
| 2172 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2173 | } |
| 2174 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2175 | bool ReplaceOpenCLBuiltinPass::replaceVstoreHalf4(Function &F) { |
| 2176 | Module &M = *F.getParent(); |
| 2177 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2178 | // The value to store. |
| 2179 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2180 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2181 | // The index argument from vstore_half. |
| 2182 | auto Arg1 = CI->getOperand(1); |
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 | // The pointer argument from vstore_half. |
| 2185 | auto Arg2 = CI->getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2186 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2187 | auto IntTy = Type::getInt32Ty(M.getContext()); |
alan-baker | b3e2b6d | 2020-06-24 23:59:57 -0400 | [diff] [blame] | 2188 | auto Int2Ty = FixedVectorType::get(IntTy, 2); |
| 2189 | auto Float2Ty = FixedVectorType::get(Type::getFloatTy(M.getContext()), 2); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2190 | auto NewPointerTy = |
| 2191 | PointerType::get(Int2Ty, Arg2->getType()->getPointerAddressSpace()); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2192 | auto NewFType = FunctionType::get(IntTy, Float2Ty, false); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2193 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2194 | Constant *LoShuffleMask[2] = {ConstantInt::get(IntTy, 0), |
| 2195 | ConstantInt::get(IntTy, 1)}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2196 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2197 | // Extract out the x & y components of our to store value. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2198 | auto Lo = new ShuffleVectorInst(Arg0, UndefValue::get(Arg0->getType()), |
| 2199 | ConstantVector::get(LoShuffleMask), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2200 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2201 | Constant *HiShuffleMask[2] = {ConstantInt::get(IntTy, 2), |
| 2202 | ConstantInt::get(IntTy, 3)}; |
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 | // Extract out the z & w components of our to store value. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2205 | auto Hi = new ShuffleVectorInst(Arg0, UndefValue::get(Arg0->getType()), |
| 2206 | ConstantVector::get(HiShuffleMask), "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2207 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2208 | // Our intrinsic to pack a float2 to an int. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2209 | auto SPIRVIntrinsic = clspv::PackFunction(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2210 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2211 | auto NewF = M.getOrInsertFunction(SPIRVIntrinsic, NewFType); |
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 | // Turn the packed x & y into the final component of our int2. |
| 2214 | auto X = CallInst::Create(NewF, Lo, "", CI); |
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 | // Turn the packed z & w into the final component of our int2. |
| 2217 | auto Y = CallInst::Create(NewF, Hi, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2218 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2219 | auto Combine = InsertElementInst::Create( |
| 2220 | UndefValue::get(Int2Ty), X, ConstantInt::get(IntTy, 0), "", CI); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2221 | Combine = InsertElementInst::Create(Combine, Y, ConstantInt::get(IntTy, 1), |
| 2222 | "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2223 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2224 | // Cast the half* pointer to int2*. |
| 2225 | auto Cast = CastInst::CreatePointerCast(Arg2, NewPointerTy, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2226 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2227 | // Index into the correct address of the casted pointer. |
| 2228 | auto Index = GetElementPtrInst::Create(Int2Ty, Cast, Arg1, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2229 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2230 | // Store to the int2* we casted to. |
| 2231 | return new StoreInst(Combine, Index, CI); |
| 2232 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2233 | } |
| 2234 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2235 | bool ReplaceOpenCLBuiltinPass::replaceHalfReadImage(Function &F) { |
| 2236 | // convert half to float |
| 2237 | Module &M = *F.getParent(); |
| 2238 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 2239 | SmallVector<Type *, 3> types; |
| 2240 | SmallVector<Value *, 3> args; |
| 2241 | for (auto i = 0; i < CI->getNumArgOperands(); ++i) { |
| 2242 | types.push_back(CI->getArgOperand(i)->getType()); |
| 2243 | args.push_back(CI->getArgOperand(i)); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2244 | } |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2245 | |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 2246 | auto NewFType = |
| 2247 | FunctionType::get(FixedVectorType::get(Type::getFloatTy(M.getContext()), |
| 2248 | cast<VectorType>(CI->getType()) |
| 2249 | ->getElementCount() |
| 2250 | .getKnownMinValue()), |
| 2251 | types, false); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2252 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2253 | std::string NewFName = |
| 2254 | Builtins::GetMangledFunctionName("read_imagef", NewFType); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2255 | |
| 2256 | auto NewF = M.getOrInsertFunction(NewFName, NewFType); |
| 2257 | |
| 2258 | auto NewCI = CallInst::Create(NewF, args, "", CI); |
| 2259 | |
| 2260 | // Convert to the half type. |
| 2261 | return CastInst::CreateFPCast(NewCI, CI->getType(), "", CI); |
| 2262 | }); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2263 | } |
| 2264 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2265 | bool ReplaceOpenCLBuiltinPass::replaceHalfWriteImage(Function &F) { |
| 2266 | // convert half to float |
| 2267 | Module &M = *F.getParent(); |
| 2268 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 2269 | SmallVector<Type *, 3> types(3); |
| 2270 | SmallVector<Value *, 3> args(3); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2271 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2272 | // Image |
| 2273 | types[0] = CI->getArgOperand(0)->getType(); |
| 2274 | args[0] = CI->getArgOperand(0); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2275 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2276 | // Coord |
| 2277 | types[1] = CI->getArgOperand(1)->getType(); |
| 2278 | args[1] = CI->getArgOperand(1); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2279 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2280 | // Data |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 2281 | types[2] = |
| 2282 | FixedVectorType::get(Type::getFloatTy(M.getContext()), |
| 2283 | cast<VectorType>(CI->getArgOperand(2)->getType()) |
| 2284 | ->getElementCount() |
| 2285 | .getKnownMinValue()); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2286 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2287 | auto NewFType = |
| 2288 | FunctionType::get(Type::getVoidTy(M.getContext()), types, false); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2289 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2290 | std::string NewFName = |
| 2291 | Builtins::GetMangledFunctionName("write_imagef", NewFType); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2292 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2293 | auto NewF = M.getOrInsertFunction(NewFName, NewFType); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2294 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2295 | // Convert data to the float type. |
| 2296 | auto Cast = CastInst::CreateFPCast(CI->getArgOperand(2), types[2], "", CI); |
| 2297 | args[2] = Cast; |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2298 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2299 | return CallInst::Create(NewF, args, "", CI); |
| 2300 | }); |
alan-baker | f7e17cb | 2020-01-02 07:29:59 -0500 | [diff] [blame] | 2301 | } |
| 2302 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2303 | bool ReplaceOpenCLBuiltinPass::replaceSampledReadImageWithIntCoords( |
| 2304 | Function &F) { |
| 2305 | // convert read_image with int coords to float coords |
| 2306 | Module &M = *F.getParent(); |
| 2307 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 2308 | // The image. |
| 2309 | auto Arg0 = CI->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2310 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2311 | // The sampler. |
| 2312 | auto Arg1 = CI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2313 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2314 | // The coordinate (integer type that we can't handle). |
| 2315 | auto Arg2 = CI->getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2316 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2317 | uint32_t dim = clspv::ImageDimensionality(Arg0->getType()); |
| 2318 | uint32_t components = |
| 2319 | dim + (clspv::IsArrayImageType(Arg0->getType()) ? 1 : 0); |
| 2320 | Type *float_ty = nullptr; |
| 2321 | if (components == 1) { |
| 2322 | float_ty = Type::getFloatTy(M.getContext()); |
| 2323 | } else { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 2324 | float_ty = FixedVectorType::get(Type::getFloatTy(M.getContext()), |
| 2325 | cast<VectorType>(Arg2->getType()) |
| 2326 | ->getElementCount() |
| 2327 | .getKnownMinValue()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2328 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2329 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2330 | auto NewFType = FunctionType::get( |
| 2331 | CI->getType(), {Arg0->getType(), Arg1->getType(), float_ty}, false); |
| 2332 | |
| 2333 | std::string NewFName = F.getName().str(); |
| 2334 | NewFName[NewFName.length() - 1] = 'f'; |
| 2335 | |
| 2336 | auto NewF = M.getOrInsertFunction(NewFName, NewFType); |
| 2337 | |
| 2338 | auto Cast = CastInst::Create(Instruction::SIToFP, Arg2, float_ty, "", CI); |
| 2339 | |
| 2340 | return CallInst::Create(NewF, {Arg0, Arg1, Cast}, "", CI); |
| 2341 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2342 | } |
| 2343 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2344 | bool ReplaceOpenCLBuiltinPass::replaceAtomics(Function &F, spv::Op Op) { |
| 2345 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
| 2346 | auto IntTy = Type::getInt32Ty(F.getContext()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2347 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2348 | // We need to map the OpenCL constants to the SPIR-V equivalents. |
| 2349 | const auto ConstantScopeDevice = ConstantInt::get(IntTy, spv::ScopeDevice); |
| 2350 | const auto ConstantMemorySemantics = ConstantInt::get( |
| 2351 | IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 2352 | spv::MemorySemanticsSequentiallyConsistentMask); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2353 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2354 | SmallVector<Value *, 5> Params; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2355 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2356 | // The pointer. |
| 2357 | Params.push_back(CI->getArgOperand(0)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2358 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2359 | // The memory scope. |
| 2360 | Params.push_back(ConstantScopeDevice); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2361 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2362 | // The memory semantics. |
| 2363 | Params.push_back(ConstantMemorySemantics); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2364 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2365 | if (2 < CI->getNumArgOperands()) { |
| 2366 | // The unequal memory semantics. |
| 2367 | Params.push_back(ConstantMemorySemantics); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2368 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2369 | // The value. |
| 2370 | Params.push_back(CI->getArgOperand(2)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2371 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2372 | // The comparator. |
| 2373 | Params.push_back(CI->getArgOperand(1)); |
| 2374 | } else if (1 < CI->getNumArgOperands()) { |
| 2375 | // The value. |
| 2376 | Params.push_back(CI->getArgOperand(1)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2377 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2378 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2379 | return clspv::InsertSPIRVOp(CI, Op, {}, CI->getType(), Params); |
| 2380 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2381 | } |
| 2382 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2383 | bool ReplaceOpenCLBuiltinPass::replaceAtomics(Function &F, |
| 2384 | llvm::AtomicRMWInst::BinOp Op) { |
| 2385 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
alan-baker | d0eb905 | 2020-07-07 13:12:01 -0400 | [diff] [blame] | 2386 | auto align = F.getParent()->getDataLayout().getABITypeAlign( |
| 2387 | CI->getArgOperand(1)->getType()); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2388 | return new AtomicRMWInst(Op, CI->getArgOperand(0), CI->getArgOperand(1), |
alan-baker | d0eb905 | 2020-07-07 13:12:01 -0400 | [diff] [blame] | 2389 | align, AtomicOrdering::SequentiallyConsistent, |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2390 | SyncScope::System, CI); |
| 2391 | }); |
| 2392 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2393 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2394 | bool ReplaceOpenCLBuiltinPass::replaceCross(Function &F) { |
| 2395 | Module &M = *F.getParent(); |
| 2396 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2397 | auto IntTy = Type::getInt32Ty(M.getContext()); |
| 2398 | auto FloatTy = Type::getFloatTy(M.getContext()); |
| 2399 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2400 | Constant *DownShuffleMask[3] = {ConstantInt::get(IntTy, 0), |
| 2401 | ConstantInt::get(IntTy, 1), |
| 2402 | ConstantInt::get(IntTy, 2)}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2403 | |
| 2404 | Constant *UpShuffleMask[4] = { |
| 2405 | ConstantInt::get(IntTy, 0), ConstantInt::get(IntTy, 1), |
| 2406 | ConstantInt::get(IntTy, 2), ConstantInt::get(IntTy, 3)}; |
| 2407 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2408 | Constant *FloatVec[3] = {ConstantFP::get(FloatTy, 0.0f), |
| 2409 | UndefValue::get(FloatTy), |
| 2410 | UndefValue::get(FloatTy)}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2411 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2412 | auto Vec4Ty = CI->getArgOperand(0)->getType(); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2413 | auto Arg0 = |
| 2414 | new ShuffleVectorInst(CI->getArgOperand(0), UndefValue::get(Vec4Ty), |
| 2415 | ConstantVector::get(DownShuffleMask), "", CI); |
| 2416 | auto Arg1 = |
| 2417 | new ShuffleVectorInst(CI->getArgOperand(1), UndefValue::get(Vec4Ty), |
| 2418 | ConstantVector::get(DownShuffleMask), "", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2419 | auto Vec3Ty = Arg0->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2420 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2421 | auto NewFType = FunctionType::get(Vec3Ty, {Vec3Ty, Vec3Ty}, false); |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2422 | auto NewFName = Builtins::GetMangledFunctionName("cross", NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2423 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2424 | auto Cross3Func = M.getOrInsertFunction(NewFName, NewFType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2425 | |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2426 | auto DownResult = CallInst::Create(Cross3Func, {Arg0, Arg1}, "", CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2427 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2428 | return new ShuffleVectorInst(DownResult, ConstantVector::get(FloatVec), |
| 2429 | ConstantVector::get(UpShuffleMask), "", CI); |
Kévin Petit | e8edce3 | 2019-04-10 14:23:32 +0100 | [diff] [blame] | 2430 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2431 | } |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2432 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2433 | bool ReplaceOpenCLBuiltinPass::replaceFract(Function &F, int vec_size) { |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2434 | // OpenCL's float result = fract(float x, float* ptr) |
| 2435 | // |
| 2436 | // In the LLVM domain: |
| 2437 | // |
| 2438 | // %floor_result = call spir_func float @floor(float %x) |
| 2439 | // store float %floor_result, float * %ptr |
| 2440 | // %fract_intermediate = call spir_func float @clspv.fract(float %x) |
| 2441 | // %result = call spir_func float |
| 2442 | // @fmin(float %fract_intermediate, float 0x1.fffffep-1f) |
| 2443 | // |
| 2444 | // Becomes in the SPIR-V domain, where translations of floor, fmin, |
| 2445 | // and clspv.fract occur in the SPIR-V generator pass: |
| 2446 | // |
| 2447 | // %glsl_ext = OpExtInstImport "GLSL.std.450" |
| 2448 | // %just_under_1 = OpConstant %float 0x1.fffffep-1f |
| 2449 | // ... |
| 2450 | // %floor_result = OpExtInst %float %glsl_ext Floor %x |
| 2451 | // OpStore %ptr %floor_result |
| 2452 | // %fract_intermediate = OpExtInst %float %glsl_ext Fract %x |
| 2453 | // %fract_result = OpExtInst %float |
Marco Antognini | 55d5186 | 2020-07-21 17:50:07 +0100 | [diff] [blame] | 2454 | // %glsl_ext Nmin %fract_intermediate %just_under_1 |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2455 | |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2456 | using std::string; |
| 2457 | |
| 2458 | // Mapping from the fract builtin to the floor, fmin, and clspv.fract builtins |
| 2459 | // we need. The clspv.fract builtin is the same as GLSL.std.450 Fract. |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2460 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2461 | Module &M = *F.getParent(); |
| 2462 | return replaceCallsWithValue(F, [&](CallInst *CI) { |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2463 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2464 | // This is either float or a float vector. All the float-like |
| 2465 | // types are this type. |
| 2466 | auto result_ty = F.getReturnType(); |
| 2467 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2468 | std::string fmin_name = Builtins::GetMangledFunctionName("fmin", result_ty); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2469 | Function *fmin_fn = M.getFunction(fmin_name); |
| 2470 | if (!fmin_fn) { |
| 2471 | // Make the fmin function. |
| 2472 | FunctionType *fn_ty = |
| 2473 | FunctionType::get(result_ty, {result_ty, result_ty}, false); |
| 2474 | fmin_fn = |
| 2475 | cast<Function>(M.getOrInsertFunction(fmin_name, fn_ty).getCallee()); |
| 2476 | fmin_fn->addFnAttr(Attribute::ReadNone); |
| 2477 | fmin_fn->setCallingConv(CallingConv::SPIR_FUNC); |
| 2478 | } |
| 2479 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2480 | std::string floor_name = |
| 2481 | Builtins::GetMangledFunctionName("floor", result_ty); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2482 | Function *floor_fn = M.getFunction(floor_name); |
| 2483 | if (!floor_fn) { |
| 2484 | // Make the floor function. |
| 2485 | FunctionType *fn_ty = FunctionType::get(result_ty, {result_ty}, false); |
| 2486 | floor_fn = |
| 2487 | cast<Function>(M.getOrInsertFunction(floor_name, fn_ty).getCallee()); |
| 2488 | floor_fn->addFnAttr(Attribute::ReadNone); |
| 2489 | floor_fn->setCallingConv(CallingConv::SPIR_FUNC); |
| 2490 | } |
| 2491 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 2492 | std::string clspv_fract_name = |
| 2493 | Builtins::GetMangledFunctionName("clspv.fract", result_ty); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2494 | Function *clspv_fract_fn = M.getFunction(clspv_fract_name); |
| 2495 | if (!clspv_fract_fn) { |
| 2496 | // Make the clspv_fract function. |
| 2497 | FunctionType *fn_ty = FunctionType::get(result_ty, {result_ty}, false); |
| 2498 | clspv_fract_fn = cast<Function>( |
| 2499 | M.getOrInsertFunction(clspv_fract_name, fn_ty).getCallee()); |
| 2500 | clspv_fract_fn->addFnAttr(Attribute::ReadNone); |
| 2501 | clspv_fract_fn->setCallingConv(CallingConv::SPIR_FUNC); |
| 2502 | } |
| 2503 | |
| 2504 | // Number of significant significand bits, whether represented or not. |
| 2505 | unsigned num_significand_bits; |
| 2506 | switch (result_ty->getScalarType()->getTypeID()) { |
| 2507 | case Type::HalfTyID: |
| 2508 | num_significand_bits = 11; |
| 2509 | break; |
| 2510 | case Type::FloatTyID: |
| 2511 | num_significand_bits = 24; |
| 2512 | break; |
| 2513 | case Type::DoubleTyID: |
| 2514 | num_significand_bits = 53; |
| 2515 | break; |
| 2516 | default: |
| 2517 | llvm_unreachable("Unhandled float type when processing fract builtin"); |
| 2518 | break; |
| 2519 | } |
| 2520 | // Beware that the disassembler displays this value as |
| 2521 | // OpConstant %float 1 |
| 2522 | // which is not quite right. |
| 2523 | const double kJustUnderOneScalar = |
| 2524 | ldexp(double((1 << num_significand_bits) - 1), -num_significand_bits); |
| 2525 | |
| 2526 | Constant *just_under_one = |
| 2527 | ConstantFP::get(result_ty->getScalarType(), kJustUnderOneScalar); |
| 2528 | if (result_ty->isVectorTy()) { |
| 2529 | just_under_one = ConstantVector::getSplat( |
alan-baker | 931253b | 2020-08-20 17:15:38 -0400 | [diff] [blame] | 2530 | cast<VectorType>(result_ty)->getElementCount(), just_under_one); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | IRBuilder<> Builder(CI); |
| 2534 | |
| 2535 | auto arg = CI->getArgOperand(0); |
| 2536 | auto ptr = CI->getArgOperand(1); |
| 2537 | |
| 2538 | // Compute floor result and store it. |
| 2539 | auto floor = Builder.CreateCall(floor_fn, {arg}); |
| 2540 | Builder.CreateStore(floor, ptr); |
| 2541 | |
| 2542 | auto fract_intermediate = Builder.CreateCall(clspv_fract_fn, arg); |
| 2543 | auto fract_result = |
| 2544 | Builder.CreateCall(fmin_fn, {fract_intermediate, just_under_one}); |
| 2545 | |
| 2546 | return fract_result; |
| 2547 | }); |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 2548 | } |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2549 | |
Kévin Petit | 8576f68 | 2020-11-02 14:51:32 +0000 | [diff] [blame] | 2550 | bool ReplaceOpenCLBuiltinPass::replaceHadd(Function &F, bool is_signed, |
alan-baker | b6da513 | 2020-10-29 15:59:06 -0400 | [diff] [blame] | 2551 | Instruction::BinaryOps join_opcode) { |
Kévin Petit | 8576f68 | 2020-11-02 14:51:32 +0000 | [diff] [blame] | 2552 | return replaceCallsWithValue(F, [is_signed, join_opcode](CallInst *Call) { |
alan-baker | b6da513 | 2020-10-29 15:59:06 -0400 | [diff] [blame] | 2553 | // a_shr = a >> 1 |
| 2554 | // b_shr = b >> 1 |
| 2555 | // add1 = a_shr + b_shr |
| 2556 | // join = a |join_opcode| b |
| 2557 | // and = join & 1 |
| 2558 | // add = add1 + and |
| 2559 | const auto a = Call->getArgOperand(0); |
| 2560 | const auto b = Call->getArgOperand(1); |
| 2561 | IRBuilder<> builder(Call); |
Kévin Petit | 8576f68 | 2020-11-02 14:51:32 +0000 | [diff] [blame] | 2562 | Value *a_shift, *b_shift; |
| 2563 | if (is_signed) { |
| 2564 | a_shift = builder.CreateAShr(a, 1); |
| 2565 | b_shift = builder.CreateAShr(b, 1); |
| 2566 | } else { |
| 2567 | a_shift = builder.CreateLShr(a, 1); |
| 2568 | b_shift = builder.CreateLShr(b, 1); |
| 2569 | } |
alan-baker | b6da513 | 2020-10-29 15:59:06 -0400 | [diff] [blame] | 2570 | auto add = builder.CreateAdd(a_shift, b_shift); |
| 2571 | auto join = BinaryOperator::Create(join_opcode, a, b, "", Call); |
| 2572 | auto constant_one = ConstantInt::get(a->getType(), 1); |
| 2573 | auto and_bit = builder.CreateAnd(join, constant_one); |
| 2574 | return builder.CreateAdd(add, and_bit); |
| 2575 | }); |
| 2576 | } |
| 2577 | |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2578 | bool ReplaceOpenCLBuiltinPass::replaceAddSubSat(Function &F, bool is_signed, |
| 2579 | bool is_add) { |
| 2580 | return replaceCallsWithValue(F, [&F, this, is_signed, |
| 2581 | is_add](CallInst *Call) { |
| 2582 | auto ty = Call->getType(); |
| 2583 | auto a = Call->getArgOperand(0); |
| 2584 | auto b = Call->getArgOperand(1); |
| 2585 | IRBuilder<> builder(Call); |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2586 | if (is_signed) { |
| 2587 | unsigned bitwidth = ty->getScalarSizeInBits(); |
| 2588 | if (bitwidth < 32) { |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2589 | unsigned extended_width = bitwidth << 1; |
| 2590 | Type *extended_ty = |
| 2591 | IntegerType::get(Call->getContext(), extended_width); |
| 2592 | Constant *min = ConstantInt::get( |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2593 | Call->getContext(), |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2594 | APInt::getSignedMinValue(bitwidth).sext(extended_width)); |
| 2595 | Constant *max = ConstantInt::get( |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2596 | Call->getContext(), |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2597 | APInt::getSignedMaxValue(bitwidth).sext(extended_width)); |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2598 | // Don't use the type in GetMangledFunctionName to ensure we get |
| 2599 | // signed parameters. |
| 2600 | std::string sclamp_name = Builtins::GetMangledFunctionName("clamp"); |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2601 | if (auto vec_ty = dyn_cast<VectorType>(ty)) { |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2602 | extended_ty = VectorType::get(extended_ty, vec_ty->getElementCount()); |
| 2603 | min = ConstantVector::getSplat(vec_ty->getElementCount(), min); |
| 2604 | max = ConstantVector::getSplat(vec_ty->getElementCount(), max); |
| 2605 | unsigned vec_width = vec_ty->getElementCount().getKnownMinValue(); |
| 2606 | if (extended_width == 32) { |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2607 | sclamp_name += "Dv" + std::to_string(vec_width) + "_iS_S_"; |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2608 | } else { |
| 2609 | sclamp_name += "Dv" + std::to_string(vec_width) + "_sS_S_"; |
| 2610 | } |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2611 | } else { |
| 2612 | if (extended_width == 32) { |
| 2613 | sclamp_name += "iii"; |
| 2614 | } else { |
| 2615 | sclamp_name += "sss"; |
| 2616 | } |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2617 | } |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2618 | |
| 2619 | auto sext_a = builder.CreateSExt(a, extended_ty); |
| 2620 | auto sext_b = builder.CreateSExt(b, extended_ty); |
| 2621 | Value *op = nullptr; |
| 2622 | // Extended operations won't wrap. |
| 2623 | if (is_add) |
| 2624 | op = builder.CreateAdd(sext_a, sext_b, "", true, true); |
| 2625 | else |
| 2626 | op = builder.CreateSub(sext_a, sext_b, "", true, true); |
| 2627 | auto clamp_ty = FunctionType::get( |
| 2628 | extended_ty, {extended_ty, extended_ty, extended_ty}, false); |
| 2629 | auto callee = F.getParent()->getOrInsertFunction(sclamp_name, clamp_ty); |
| 2630 | auto clamp = builder.CreateCall(callee, {op, min, max}); |
| 2631 | return builder.CreateTrunc(clamp, ty); |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2632 | } else { |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2633 | // Add: |
| 2634 | // c = a + b |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2635 | // if (b < 0) |
| 2636 | // c = c > a ? min : c; |
| 2637 | // else |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2638 | // c = c < a ? max : c; |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2639 | // |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2640 | // Sub: |
| 2641 | // c = a - b; |
| 2642 | // if (b < 0) |
| 2643 | // c = c < a ? max : c; |
| 2644 | // else |
| 2645 | // c = c > a ? min : c; |
| 2646 | Constant *min = ConstantInt::get(Call->getContext(), |
| 2647 | APInt::getSignedMinValue(bitwidth)); |
| 2648 | Constant *max = ConstantInt::get(Call->getContext(), |
| 2649 | APInt::getSignedMaxValue(bitwidth)); |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2650 | if (auto vec_ty = dyn_cast<VectorType>(ty)) { |
| 2651 | min = ConstantVector::getSplat(vec_ty->getElementCount(), min); |
| 2652 | max = ConstantVector::getSplat(vec_ty->getElementCount(), max); |
| 2653 | } |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2654 | Value *op = nullptr; |
| 2655 | if (is_add) { |
| 2656 | op = builder.CreateAdd(a, b); |
| 2657 | } else { |
| 2658 | op = builder.CreateSub(a, b); |
| 2659 | } |
| 2660 | auto b_lt_0 = builder.CreateICmpSLT(b, Constant::getNullValue(ty)); |
| 2661 | auto op_gt_a = builder.CreateICmpSGT(op, a); |
| 2662 | auto op_lt_a = builder.CreateICmpSLT(op, a); |
| 2663 | auto neg_cmp = is_add ? op_gt_a : op_lt_a; |
| 2664 | auto pos_cmp = is_add ? op_lt_a : op_gt_a; |
| 2665 | auto neg_value = is_add ? min : max; |
| 2666 | auto pos_value = is_add ? max : min; |
| 2667 | auto neg_clamp = builder.CreateSelect(neg_cmp, neg_value, op); |
| 2668 | auto pos_clamp = builder.CreateSelect(pos_cmp, pos_value, op); |
| 2669 | return builder.CreateSelect(b_lt_0, neg_clamp, pos_clamp); |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2670 | } |
| 2671 | } else { |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 2672 | // Replace with OpIAddCarry/OpISubBorrow and clamp to max/0 on a |
| 2673 | // carr/borrow. |
| 2674 | spv::Op op = is_add ? spv::OpIAddCarry : spv::OpISubBorrow; |
| 2675 | auto clamp_value = |
| 2676 | is_add ? Constant::getAllOnesValue(ty) : Constant::getNullValue(ty); |
| 2677 | auto struct_ty = GetPairStruct(ty); |
| 2678 | auto call = |
| 2679 | InsertSPIRVOp(Call, op, {Attribute::ReadNone}, struct_ty, {a, b}); |
| 2680 | auto add_sub = builder.CreateExtractValue(call, {0}); |
| 2681 | auto carry_borrow = builder.CreateExtractValue(call, {1}); |
| 2682 | auto cmp = builder.CreateICmpEQ(carry_borrow, Constant::getNullValue(ty)); |
| 2683 | return builder.CreateSelect(cmp, add_sub, clamp_value); |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2684 | } |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 2685 | }); |
| 2686 | } |
alan-baker | 4986eff | 2020-10-29 13:38:00 -0400 | [diff] [blame] | 2687 | |
| 2688 | bool ReplaceOpenCLBuiltinPass::replaceAtomicLoad(Function &F) { |
| 2689 | return replaceCallsWithValue(F, [](CallInst *Call) { |
| 2690 | auto pointer = Call->getArgOperand(0); |
| 2691 | // Clang emits an address space cast to the generic address space. Skip the |
| 2692 | // cast and use the input directly. |
| 2693 | if (auto cast = dyn_cast<AddrSpaceCastOperator>(pointer)) { |
| 2694 | pointer = cast->getPointerOperand(); |
| 2695 | } |
| 2696 | Value *order_arg = |
| 2697 | Call->getNumArgOperands() > 1 ? Call->getArgOperand(1) : nullptr; |
| 2698 | Value *scope_arg = |
| 2699 | Call->getNumArgOperands() > 2 ? Call->getArgOperand(2) : nullptr; |
| 2700 | bool is_global = pointer->getType()->getPointerAddressSpace() == |
| 2701 | clspv::AddressSpace::Global; |
| 2702 | auto order = MemoryOrderSemantics(order_arg, is_global, Call, |
| 2703 | spv::MemorySemanticsAcquireMask); |
| 2704 | auto scope = MemoryScope(scope_arg, is_global, Call); |
| 2705 | return InsertSPIRVOp(Call, spv::OpAtomicLoad, {Attribute::Convergent}, |
| 2706 | Call->getType(), {pointer, scope, order}); |
| 2707 | }); |
| 2708 | } |
| 2709 | |
| 2710 | bool ReplaceOpenCLBuiltinPass::replaceExplicitAtomics( |
| 2711 | Function &F, spv::Op Op, spv::MemorySemanticsMask semantics) { |
| 2712 | return replaceCallsWithValue(F, [Op, semantics](CallInst *Call) { |
| 2713 | auto pointer = Call->getArgOperand(0); |
| 2714 | // Clang emits an address space cast to the generic address space. Skip the |
| 2715 | // cast and use the input directly. |
| 2716 | if (auto cast = dyn_cast<AddrSpaceCastOperator>(pointer)) { |
| 2717 | pointer = cast->getPointerOperand(); |
| 2718 | } |
| 2719 | Value *value = Call->getArgOperand(1); |
| 2720 | Value *order_arg = |
| 2721 | Call->getNumArgOperands() > 2 ? Call->getArgOperand(2) : nullptr; |
| 2722 | Value *scope_arg = |
| 2723 | Call->getNumArgOperands() > 3 ? Call->getArgOperand(3) : nullptr; |
| 2724 | bool is_global = pointer->getType()->getPointerAddressSpace() == |
| 2725 | clspv::AddressSpace::Global; |
| 2726 | auto scope = MemoryScope(scope_arg, is_global, Call); |
| 2727 | auto order = MemoryOrderSemantics(order_arg, is_global, Call, semantics); |
| 2728 | return InsertSPIRVOp(Call, Op, {Attribute::Convergent}, Call->getType(), |
| 2729 | {pointer, scope, order, value}); |
| 2730 | }); |
| 2731 | } |
| 2732 | |
| 2733 | bool ReplaceOpenCLBuiltinPass::replaceAtomicCompareExchange(Function &F) { |
| 2734 | return replaceCallsWithValue(F, [](CallInst *Call) { |
| 2735 | auto pointer = Call->getArgOperand(0); |
| 2736 | // Clang emits an address space cast to the generic address space. Skip the |
| 2737 | // cast and use the input directly. |
| 2738 | if (auto cast = dyn_cast<AddrSpaceCastOperator>(pointer)) { |
| 2739 | pointer = cast->getPointerOperand(); |
| 2740 | } |
| 2741 | auto expected = Call->getArgOperand(1); |
| 2742 | if (auto cast = dyn_cast<AddrSpaceCastOperator>(expected)) { |
| 2743 | expected = cast->getPointerOperand(); |
| 2744 | } |
| 2745 | auto value = Call->getArgOperand(2); |
| 2746 | bool is_global = pointer->getType()->getPointerAddressSpace() == |
| 2747 | clspv::AddressSpace::Global; |
| 2748 | Value *success_arg = |
| 2749 | Call->getNumArgOperands() > 3 ? Call->getArgOperand(3) : nullptr; |
| 2750 | Value *failure_arg = |
| 2751 | Call->getNumArgOperands() > 4 ? Call->getArgOperand(4) : nullptr; |
| 2752 | Value *scope_arg = |
| 2753 | Call->getNumArgOperands() > 5 ? Call->getArgOperand(5) : nullptr; |
| 2754 | auto scope = MemoryScope(scope_arg, is_global, Call); |
| 2755 | auto success = MemoryOrderSemantics(success_arg, is_global, Call, |
| 2756 | spv::MemorySemanticsAcquireReleaseMask); |
| 2757 | auto failure = MemoryOrderSemantics(failure_arg, is_global, Call, |
| 2758 | spv::MemorySemanticsAcquireMask); |
| 2759 | |
| 2760 | // If the value pointed to by |expected| equals the value pointed to by |
| 2761 | // |pointer|, |value| is written into |pointer|, otherwise the value in |
| 2762 | // |pointer| is written into |expected|. In order to avoid extra stores, |
| 2763 | // the basic block with the original atomic is split and the store is |
| 2764 | // performed in the |then| block. The condition is the inversion of the |
| 2765 | // comparison result. |
| 2766 | IRBuilder<> builder(Call); |
| 2767 | auto load = builder.CreateLoad(expected); |
| 2768 | auto cmp_xchg = InsertSPIRVOp( |
| 2769 | Call, spv::OpAtomicCompareExchange, {Attribute::Convergent}, |
| 2770 | value->getType(), {pointer, scope, success, failure, value, load}); |
| 2771 | auto cmp = builder.CreateICmpEQ(cmp_xchg, load); |
| 2772 | auto not_cmp = builder.CreateNot(cmp); |
| 2773 | auto then_branch = SplitBlockAndInsertIfThen(not_cmp, Call, false); |
| 2774 | builder.SetInsertPoint(then_branch); |
| 2775 | builder.CreateStore(cmp_xchg, expected); |
| 2776 | return cmp; |
| 2777 | }); |
| 2778 | } |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2779 | |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2780 | bool ReplaceOpenCLBuiltinPass::replaceCountZeroes(Function &F, bool leading) { |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2781 | if (!isa<IntegerType>(F.getReturnType()->getScalarType())) |
| 2782 | return false; |
| 2783 | |
| 2784 | auto bitwidth = F.getReturnType()->getScalarSizeInBits(); |
| 2785 | if (bitwidth == 32 || bitwidth > 64) |
| 2786 | return false; |
| 2787 | |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2788 | return replaceCallsWithValue(F, [&F, bitwidth, leading](CallInst *Call) { |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2789 | auto in = Call->getArgOperand(0); |
| 2790 | IRBuilder<> builder(Call); |
| 2791 | auto int32_ty = builder.getInt32Ty(); |
| 2792 | Type *ty = int32_ty; |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2793 | Constant *c32 = builder.getInt32(32); |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2794 | if (auto vec_ty = dyn_cast<VectorType>(Call->getType())) { |
| 2795 | ty = VectorType::get(ty, vec_ty->getElementCount()); |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2796 | c32 = ConstantVector::getSplat(vec_ty->getElementCount(), c32); |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2797 | } |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2798 | auto func_32bit_ty = FunctionType::get(ty, {ty}, false); |
| 2799 | std::string func_32bit_name = |
| 2800 | Builtins::GetMangledFunctionName((leading ? "clz" : "ctz"), ty); |
| 2801 | auto func_32bit = |
| 2802 | F.getParent()->getOrInsertFunction(func_32bit_name, func_32bit_ty); |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2803 | if (bitwidth < 32) { |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2804 | // Extend the input to 32-bits and perform a clz/ctz. |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2805 | auto zext = builder.CreateZExt(in, ty); |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2806 | Value *call_input = zext; |
| 2807 | if (!leading) { |
| 2808 | // Or the extended input value with a constant that caps the max to the |
| 2809 | // right bitwidth (e.g. 256 for i8 and 65536 for i16). |
| 2810 | Constant *mask = builder.getInt32(1 << bitwidth); |
| 2811 | if (auto vec_ty = dyn_cast<VectorType>(ty)) { |
| 2812 | mask = ConstantVector::getSplat(vec_ty->getElementCount(), mask); |
| 2813 | } |
| 2814 | call_input = builder.CreateOr(zext, mask); |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2815 | } |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2816 | auto call = builder.CreateCall(func_32bit, {call_input}); |
| 2817 | Value *tmp = call; |
| 2818 | if (leading) { |
| 2819 | // Clz is implemented as 31 - FindUMsb(|zext|), so adjust the result |
| 2820 | // the right bitwidth. |
| 2821 | Constant *sub_const = builder.getInt32(32 - bitwidth); |
| 2822 | if (auto vec_ty = dyn_cast<VectorType>(ty)) { |
| 2823 | sub_const = |
| 2824 | ConstantVector::getSplat(vec_ty->getElementCount(), sub_const); |
| 2825 | } |
| 2826 | tmp = builder.CreateSub(call, sub_const); |
| 2827 | } |
| 2828 | // Truncate the intermediate result to the right size. |
| 2829 | return builder.CreateTrunc(tmp, Call->getType()); |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2830 | } else { |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2831 | // Perform a 32-bit version of clz/ctz on each half of the 64-bit input. |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2832 | auto lshr = builder.CreateLShr(in, 32); |
| 2833 | auto top_bits = builder.CreateTrunc(lshr, ty); |
| 2834 | auto bot_bits = builder.CreateTrunc(in, ty); |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2835 | auto top_func = builder.CreateCall(func_32bit, {top_bits}); |
| 2836 | auto bot_func = builder.CreateCall(func_32bit, {bot_bits}); |
| 2837 | Value *tmp = nullptr; |
| 2838 | if (leading) { |
| 2839 | // For clz, if clz(top) is 32, return 32 + clz(bot). |
| 2840 | auto cmp = builder.CreateICmpEQ(top_func, c32); |
| 2841 | auto adjust = builder.CreateAdd(bot_func, c32); |
| 2842 | tmp = builder.CreateSelect(cmp, adjust, top_func); |
| 2843 | } else { |
| 2844 | // For ctz, if clz(bot) is 32, return 32 + ctz(top) |
| 2845 | auto bot_cmp = builder.CreateICmpEQ(bot_func, c32); |
| 2846 | auto adjust = builder.CreateAdd(top_func, c32); |
| 2847 | tmp = builder.CreateSelect(bot_cmp, adjust, bot_func); |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2848 | } |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 2849 | // Extend the intermediate result to the correct size. |
| 2850 | return builder.CreateZExt(tmp, Call->getType()); |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 2851 | } |
| 2852 | }); |
| 2853 | } |
alan-baker | 6b9d1ee | 2020-11-03 23:11:32 -0500 | [diff] [blame] | 2854 | |
| 2855 | bool ReplaceOpenCLBuiltinPass::replaceMadSat(Function &F, bool is_signed) { |
| 2856 | return replaceCallsWithValue(F, [&F, is_signed, this](CallInst *Call) { |
| 2857 | const auto ty = Call->getType(); |
| 2858 | const auto a = Call->getArgOperand(0); |
| 2859 | const auto b = Call->getArgOperand(1); |
| 2860 | const auto c = Call->getArgOperand(2); |
| 2861 | IRBuilder<> builder(Call); |
| 2862 | if (is_signed) { |
| 2863 | unsigned bitwidth = Call->getType()->getScalarSizeInBits(); |
| 2864 | if (bitwidth < 32) { |
| 2865 | // mul = sext(a) * sext(b) |
| 2866 | // add = mul + sext(c) |
| 2867 | // res = clamp(add, MIN, MAX) |
| 2868 | unsigned extended_width = bitwidth << 1; |
| 2869 | Type *extended_ty = IntegerType::get(F.getContext(), extended_width); |
| 2870 | if (auto vec_ty = dyn_cast<VectorType>(ty)) { |
| 2871 | extended_ty = VectorType::get(extended_ty, vec_ty->getElementCount()); |
| 2872 | } |
| 2873 | auto a_sext = builder.CreateSExt(a, extended_ty); |
| 2874 | auto b_sext = builder.CreateSExt(b, extended_ty); |
| 2875 | auto c_sext = builder.CreateSExt(c, extended_ty); |
| 2876 | // Extended the size so no overflows occur. |
| 2877 | auto mul = builder.CreateMul(a_sext, b_sext, "", true, true); |
| 2878 | auto add = builder.CreateAdd(mul, c_sext, "", true, true); |
| 2879 | auto func_ty = FunctionType::get( |
| 2880 | extended_ty, {extended_ty, extended_ty, extended_ty}, false); |
| 2881 | // Don't use function type because we need signed parameters. |
| 2882 | std::string clamp_name = Builtins::GetMangledFunctionName("clamp"); |
| 2883 | // The clamp values are the signed min and max of the original bitwidth |
| 2884 | // sign extended to the extended bitwidth. |
| 2885 | Constant *min = ConstantInt::get( |
| 2886 | Call->getContext(), |
| 2887 | APInt::getSignedMinValue(bitwidth).sext(extended_width)); |
| 2888 | Constant *max = ConstantInt::get( |
| 2889 | Call->getContext(), |
| 2890 | APInt::getSignedMaxValue(bitwidth).sext(extended_width)); |
| 2891 | if (auto vec_ty = dyn_cast<VectorType>(ty)) { |
| 2892 | min = ConstantVector::getSplat(vec_ty->getElementCount(), min); |
| 2893 | max = ConstantVector::getSplat(vec_ty->getElementCount(), max); |
| 2894 | unsigned vec_width = vec_ty->getElementCount().getKnownMinValue(); |
| 2895 | if (extended_width == 32) |
| 2896 | clamp_name += "Dv" + std::to_string(vec_width) + "_iS_S_"; |
| 2897 | else |
| 2898 | clamp_name += "Dv" + std::to_string(vec_width) + "_sS_S_"; |
| 2899 | } else { |
| 2900 | if (extended_width == 32) |
| 2901 | clamp_name += "iii"; |
| 2902 | else |
| 2903 | clamp_name += "sss"; |
| 2904 | } |
| 2905 | auto callee = F.getParent()->getOrInsertFunction(clamp_name, func_ty); |
| 2906 | auto clamp = builder.CreateCall(callee, {add, min, max}); |
| 2907 | return builder.CreateTrunc(clamp, ty); |
| 2908 | } else { |
| 2909 | auto struct_ty = GetPairStruct(ty); |
| 2910 | // Compute |
| 2911 | // {hi, lo} = smul_extended(a, b) |
| 2912 | // add = lo + c |
| 2913 | auto mul_ext = InsertSPIRVOp(Call, spv::OpSMulExtended, |
| 2914 | {Attribute::ReadNone}, struct_ty, {a, b}); |
| 2915 | auto mul_lo = builder.CreateExtractValue(mul_ext, {0}); |
| 2916 | auto mul_hi = builder.CreateExtractValue(mul_ext, {1}); |
| 2917 | auto add = builder.CreateAdd(mul_lo, c); |
| 2918 | |
| 2919 | // Constants for use in the calculation. |
| 2920 | Constant *min = ConstantInt::get(Call->getContext(), |
| 2921 | APInt::getSignedMinValue(bitwidth)); |
| 2922 | Constant *max = ConstantInt::get(Call->getContext(), |
| 2923 | APInt::getSignedMaxValue(bitwidth)); |
| 2924 | Constant *max_plus_1 = ConstantInt::get( |
| 2925 | Call->getContext(), |
| 2926 | APInt::getSignedMaxValue(bitwidth) + APInt(bitwidth, 1)); |
| 2927 | if (auto vec_ty = dyn_cast<VectorType>(ty)) { |
| 2928 | min = ConstantVector::getSplat(vec_ty->getElementCount(), min); |
| 2929 | max = ConstantVector::getSplat(vec_ty->getElementCount(), max); |
| 2930 | max_plus_1 = |
| 2931 | ConstantVector::getSplat(vec_ty->getElementCount(), max_plus_1); |
| 2932 | } |
| 2933 | |
| 2934 | auto a_xor_b = builder.CreateXor(a, b); |
| 2935 | auto same_sign = |
| 2936 | builder.CreateICmpSGT(a_xor_b, Constant::getAllOnesValue(ty)); |
| 2937 | auto different_sign = builder.CreateNot(same_sign); |
| 2938 | auto hi_eq_0 = builder.CreateICmpEQ(mul_hi, Constant::getNullValue(ty)); |
| 2939 | auto hi_ne_0 = builder.CreateNot(hi_eq_0); |
| 2940 | auto lo_ge_max = builder.CreateICmpUGE(mul_lo, max); |
| 2941 | auto c_gt_0 = builder.CreateICmpSGT(c, Constant::getNullValue(ty)); |
| 2942 | auto c_lt_0 = builder.CreateICmpSLT(c, Constant::getNullValue(ty)); |
| 2943 | auto add_gt_max = builder.CreateICmpUGT(add, max); |
| 2944 | auto hi_eq_m1 = |
| 2945 | builder.CreateICmpEQ(mul_hi, Constant::getAllOnesValue(ty)); |
| 2946 | auto hi_ne_m1 = builder.CreateNot(hi_eq_m1); |
| 2947 | auto lo_le_max_plus_1 = builder.CreateICmpULE(mul_lo, max_plus_1); |
| 2948 | auto max_sub_lo = builder.CreateSub(max, mul_lo); |
| 2949 | auto c_lt_max_sub_lo = builder.CreateICmpULT(c, max_sub_lo); |
| 2950 | |
| 2951 | // Equivalent to: |
| 2952 | // if (((x < 0) == (y < 0)) && mul_hi != 0) |
| 2953 | // return MAX |
| 2954 | // if (mul_hi == 0 && mul_lo >= MAX && (z > 0 || add > MAX)) |
| 2955 | // return MAX |
| 2956 | // if (((x < 0) != (y < 0)) && mul_hi != -1) |
| 2957 | // return MIN |
| 2958 | // if (hi == -1 && mul_lo <= (MAX + 1) && (z < 0 || z < (MAX - mul_lo)) |
| 2959 | // return MIN |
| 2960 | // return add |
| 2961 | auto max_clamp_1 = builder.CreateAnd(same_sign, hi_ne_0); |
| 2962 | auto max_clamp_2 = builder.CreateOr(c_gt_0, add_gt_max); |
| 2963 | auto tmp = builder.CreateAnd(hi_eq_0, lo_ge_max); |
| 2964 | max_clamp_2 = builder.CreateAnd(tmp, max_clamp_2); |
| 2965 | auto max_clamp = builder.CreateOr(max_clamp_1, max_clamp_2); |
| 2966 | auto min_clamp_1 = builder.CreateAnd(different_sign, hi_ne_m1); |
| 2967 | auto min_clamp_2 = builder.CreateOr(c_lt_0, c_lt_max_sub_lo); |
| 2968 | tmp = builder.CreateAnd(hi_eq_m1, lo_le_max_plus_1); |
| 2969 | min_clamp_2 = builder.CreateAnd(tmp, min_clamp_2); |
| 2970 | auto min_clamp = builder.CreateOr(min_clamp_1, min_clamp_2); |
| 2971 | auto sel = builder.CreateSelect(min_clamp, min, add); |
| 2972 | return builder.CreateSelect(max_clamp, max, sel); |
| 2973 | } |
| 2974 | } else { |
| 2975 | // {lo, hi} = mul_extended(a, b) |
| 2976 | // {add, carry} = add_carry(lo, c) |
| 2977 | // cmp = (mul_hi | carry) == 0 |
| 2978 | // mad_sat = cmp ? add : MAX |
| 2979 | auto struct_ty = GetPairStruct(ty); |
| 2980 | auto mul_ext = InsertSPIRVOp(Call, spv::OpUMulExtended, |
| 2981 | {Attribute::ReadNone}, struct_ty, {a, b}); |
| 2982 | auto mul_lo = builder.CreateExtractValue(mul_ext, {0}); |
| 2983 | auto mul_hi = builder.CreateExtractValue(mul_ext, {1}); |
| 2984 | auto add_carry = |
| 2985 | InsertSPIRVOp(Call, spv::OpIAddCarry, {Attribute::ReadNone}, |
| 2986 | struct_ty, {mul_lo, c}); |
| 2987 | auto add = builder.CreateExtractValue(add_carry, {0}); |
| 2988 | auto carry = builder.CreateExtractValue(add_carry, {1}); |
| 2989 | auto or_value = builder.CreateOr(mul_hi, carry); |
| 2990 | auto cmp = builder.CreateICmpEQ(or_value, Constant::getNullValue(ty)); |
| 2991 | return builder.CreateSelect(cmp, add, Constant::getAllOnesValue(ty)); |
| 2992 | } |
| 2993 | }); |
| 2994 | } |
alan-baker | 1510657 | 2020-11-06 15:08:10 -0500 | [diff] [blame^] | 2995 | |
| 2996 | bool ReplaceOpenCLBuiltinPass::replaceOrdered(Function &F, bool is_ordered) { |
| 2997 | if (!isa<IntegerType>(F.getReturnType()->getScalarType())) |
| 2998 | return false; |
| 2999 | |
| 3000 | if (F.getFunctionType()->getNumParams() != 2) |
| 3001 | return false; |
| 3002 | |
| 3003 | if (F.getFunctionType()->getParamType(0) != |
| 3004 | F.getFunctionType()->getParamType(1)) { |
| 3005 | return false; |
| 3006 | } |
| 3007 | |
| 3008 | switch (F.getFunctionType()->getParamType(0)->getScalarType()->getTypeID()) { |
| 3009 | case Type::FloatTyID: |
| 3010 | case Type::HalfTyID: |
| 3011 | case Type::DoubleTyID: |
| 3012 | break; |
| 3013 | default: |
| 3014 | return false; |
| 3015 | } |
| 3016 | |
| 3017 | // Scalar versions all return an int, while vector versions return a vector |
| 3018 | // of an equally sized integer types (e.g. short, int or long). |
| 3019 | if (isa<VectorType>(F.getReturnType())) { |
| 3020 | if (F.getReturnType()->getScalarSizeInBits() != |
| 3021 | F.getFunctionType()->getParamType(0)->getScalarSizeInBits()) { |
| 3022 | return false; |
| 3023 | } |
| 3024 | } else { |
| 3025 | if (F.getReturnType()->getScalarSizeInBits() != 32) |
| 3026 | return false; |
| 3027 | } |
| 3028 | |
| 3029 | return replaceCallsWithValue(F, [is_ordered](CallInst *Call) { |
| 3030 | // Replace with a floating point [un]ordered comparison followed by an |
| 3031 | // extension. |
| 3032 | auto x = Call->getArgOperand(0); |
| 3033 | auto y = Call->getArgOperand(1); |
| 3034 | IRBuilder<> builder(Call); |
| 3035 | Value *tmp = nullptr; |
| 3036 | if (is_ordered) { |
| 3037 | // This leads to a slight inefficiency in the SPIR-V that is easy for |
| 3038 | // drivers to optimize where the SPIR-V for the comparison and the |
| 3039 | // extension could be fused to drop the inversion of the OpIsNan. |
| 3040 | tmp = builder.CreateFCmpORD(x, y); |
| 3041 | } else { |
| 3042 | tmp = builder.CreateFCmpUNO(x, y); |
| 3043 | } |
| 3044 | // OpenCL CTS requires that vector versions use sign extension, but scalar |
| 3045 | // versions use zero extension. |
| 3046 | if (isa<VectorType>(Call->getType())) |
| 3047 | return builder.CreateSExt(tmp, Call->getType()); |
| 3048 | return builder.CreateZExt(tmp, Call->getType()); |
| 3049 | }); |
| 3050 | } |