| 1 |
//===- SPIRVToOCL12.cpp - Transform SPIR-V builtins to OCL 1.2 |
| 2 |
// builtins------===// |
| 3 |
// |
| 4 |
// The LLVM/SPIRV Translator |
| 5 |
// |
| 6 |
// This file is distributed under the University of Illinois Open Source |
| 7 |
// License. See LICENSE.TXT for details. |
| 8 |
// |
| 9 |
// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved. |
| 10 |
// |
| 11 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 12 |
// copy of this software and associated documentation files (the "Software"), |
| 13 |
// to deal with the Software without restriction, including without limitation |
| 14 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 15 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 16 |
// Software is furnished to do so, subject to the following conditions: |
| 17 |
// |
| 18 |
// Redistributions of source code must retain the above copyright notice, |
| 19 |
// this list of conditions and the following disclaimers. |
| 20 |
// Redistributions in binary form must reproduce the above copyright notice, |
| 21 |
// this list of conditions and the following disclaimers in the documentation |
| 22 |
// and/or other materials provided with the distribution. |
| 23 |
// Neither the names of Advanced Micro Devices, Inc., nor the names of its |
| 24 |
// contributors may be used to endorse or promote products derived from this |
| 25 |
// Software without specific prior written permission. |
| 26 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 29 |
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 32 |
// THE SOFTWARE. |
| 33 |
// |
| 34 |
//===----------------------------------------------------------------------===// |
| 35 |
// |
| 36 |
// This file implements transform of SPIR-V builtins to OCL 1.2 builtins. |
| 37 |
// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
#include "SPIRVToOCL.h" |
| 40 |
#include "llvm/IR/Verifier.h" |
| 41 |
|
| 42 |
#define DEBUG_TYPE "spvtocl12" |
| 43 |
|
| 44 |
namespace SPIRV { |
| 45 |
|
| 46 |
char SPIRVToOCL12Legacy::ID = 0; |
| 47 |
|
| 48 |
bool SPIRVToOCL12Legacy::runOnModule(Module &Module) { |
| 49 |
return SPIRVToOCL12Base::runSPIRVToOCL(Module); |
| 50 |
} |
| 51 |
|
| 52 |
bool SPIRVToOCL12Base::runSPIRVToOCL(Module &Module) { |
| 53 |
M = &Module; |
| 54 |
Ctx = &M->getContext(); |
| 55 |
|
| 56 |
// Lower builtin variables to builtin calls first. |
| 57 |
lowerBuiltinVariablesToCalls(M); |
| 58 |
translateOpaqueTypes(); |
| 59 |
|
| 60 |
visit(*M); |
| 61 |
|
| 62 |
postProcessBuiltinsReturningStruct(M); |
| 63 |
postProcessBuiltinsWithArrayArguments(M); |
| 64 |
|
| 65 |
eraseUselessFunctions(&Module); |
| 66 |
|
| 67 |
LLVM_DEBUG(dbgs() << "After SPIRVToOCL12:\n" << *M); |
| 68 |
|
| 69 |
std::string Err; |
| 70 |
raw_string_ostream ErrorOS(Err); |
| 71 |
if (verifyModule(*M, &ErrorOS)) { |
| 72 |
LLVM_DEBUG(errs() << "Fails to verify module: " << ErrorOS.str()); |
| 73 |
} |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
void SPIRVToOCL12Base::visitCallSPIRVMemoryBarrier(CallInst *CI) { |
| 78 |
mutateCallInst(CI, kOCLBuiltinName::MemFence) |
| 79 |
.mapArg(1, |
| 80 |
[=](Value *V) { |
| 81 |
return transSPIRVMemorySemanticsIntoOCLMemFenceFlags(V, CI); |
| 82 |
}) |
| 83 |
.removeArg(0); |
| 84 |
} |
| 85 |
|
| 86 |
void SPIRVToOCL12Base::visitCallSPIRVControlBarrier(CallInst *CI) { |
| 87 |
mutateCallInst(CI, kOCLBuiltinName::Barrier) |
| 88 |
.mapArg(2, |
| 89 |
[=](Value *V) { |
| 90 |
return transSPIRVMemorySemanticsIntoOCLMemFenceFlags(V, CI); |
| 91 |
}) |
| 92 |
.removeArg(1) |
| 93 |
.removeArg(0); |
| 94 |
} |
| 95 |
|
| 96 |
void SPIRVToOCL12Base::visitCallSPIRVSplitBarrierINTEL(CallInst *CI, Op OC) { |
| 97 |
mutateCallInst(CI, OCLSPIRVBuiltinMap::rmap(OC)) |
| 98 |
.mapArg(2, |
| 99 |
[=](Value *V) { |
| 100 |
return transSPIRVMemorySemanticsIntoOCLMemFenceFlags(V, CI); |
| 101 |
}) |
| 102 |
.removeArg(1) |
| 103 |
.removeArg(0); |
| 104 |
} |
| 105 |
|
| 106 |
void SPIRVToOCL12Base::visitCallSPIRVAtomicIncDec(CallInst *CI, Op OC) { |
| 107 |
mutateCallInst(CI, mapAtomicName(OC, CI->getType())) |
| 108 |
.removeArg(2) |
| 109 |
.removeArg(1); |
| 110 |
} |
| 111 |
|
| 112 |
CallInst *SPIRVToOCL12Base::mutateCommonAtomicArguments(CallInst *CI, Op OC) { |
| 113 |
auto Ptr = findFirstPtr(CI->args()); |
| 114 |
auto NumOrder = getSPIRVAtomicBuiltinNumMemoryOrderArgs(OC); |
| 115 |
auto ArgsToRemove = NumOrder + 1; // OpenCL1.2 builtins does not use |
| 116 |
// scope and memory order arguments |
| 117 |
|
| 118 |
auto Mutator = mutateCallInst(CI, mapAtomicName(OC, CI->getType())); |
| 119 |
Mutator.removeArgs(Ptr + 1, ArgsToRemove); |
| 120 |
return cast<CallInst>(Mutator.getMutated()); |
| 121 |
} |
| 122 |
|
| 123 |
void SPIRVToOCL12Base::visitCallSPIRVAtomicUMinUMax(CallInst *CI, Op OC) { |
| 124 |
mutateCallInst(CI, mapAtomicName(OC, CI->getType())) |
| 125 |
.moveArg(3, 1) |
| 126 |
.removeArg(3) |
| 127 |
.removeArg(2); |
| 128 |
} |
| 129 |
|
| 130 |
void SPIRVToOCL12Base::visitCallSPIRVAtomicLoad(CallInst *CI) { |
| 131 |
// There is no atomic_load in OpenCL 1.2 spec. |
| 132 |
// Emit this builtin via call of atomic_add(*p, 0). |
| 133 |
Type *PtrElemTy = CI->getType(); |
| 134 |
mutateCallInst(CI, mapAtomicName(OpAtomicIAdd, PtrElemTy)) |
| 135 |
.removeArg(2) |
| 136 |
.removeArg(1) |
| 137 |
.appendArg(Constant::getNullValue(PtrElemTy)); |
| 138 |
} |
| 139 |
|
| 140 |
void SPIRVToOCL12Base::visitCallSPIRVAtomicStore(CallInst *CI) { |
| 141 |
Type *RetTy = CI->getArgOperand(3)->getType(); |
| 142 |
mutateCallInst(CI, mapAtomicName(OpAtomicExchange, RetTy)) |
| 143 |
.removeArg(2) |
| 144 |
.removeArg(1) |
| 145 |
.changeReturnType(RetTy, nullptr); |
| 146 |
} |
| 147 |
|
| 148 |
void SPIRVToOCL12Base::visitCallSPIRVAtomicFlagClear(CallInst *CI) { |
| 149 |
Type *RetTy = Type::getInt32Ty(M->getContext()); |
| 150 |
mutateCallInst(CI, mapAtomicName(OpAtomicExchange, RetTy)) |
| 151 |
.removeArg(2) |
| 152 |
.removeArg(1) |
| 153 |
.appendArg(getInt32(M, 0)) |
| 154 |
.changeReturnType(RetTy, nullptr); |
| 155 |
} |
| 156 |
|
| 157 |
void SPIRVToOCL12Base::visitCallSPIRVAtomicFlagTestAndSet(CallInst *CI) { |
| 158 |
Type *RetTy = Type::getInt32Ty(M->getContext()); |
| 159 |
mutateCallInst(CI, mapAtomicName(OpAtomicExchange, RetTy)) |
| 160 |
.removeArg(2) |
| 161 |
.removeArg(1) |
| 162 |
.appendArg(getInt32(M, 1)) |
| 163 |
.changeReturnType(RetTy, [](IRBuilder<> &Builder, CallInst *NewCI) { |
| 164 |
return Builder.CreateTrunc(NewCI, Builder.getInt1Ty()); |
| 165 |
}); |
| 166 |
} |
| 167 |
|
| 168 |
void SPIRVToOCL12Base::visitCallSPIRVAtomicCmpExchg(CallInst *CI) { |
| 169 |
mutateCallInst(CI, mapAtomicName(OpAtomicCompareExchange, CI->getType())) |
| 170 |
.removeArg(3) |
| 171 |
.removeArg(2) |
| 172 |
.removeArg(1) |
| 173 |
// SPIRV OpAtomicCompareExchange and OpAtomicCompareExchangeWeak has Value |
| 174 |
// and Comparator in different order than ocl functions both of them are |
| 175 |
// translated into atomic_cmpxchg |
| 176 |
.moveArg(2, 1); |
| 177 |
} |
| 178 |
|
| 179 |
void SPIRVToOCL12Base::visitCallSPIRVAtomicBuiltin(CallInst *CI, Op OC) { |
| 180 |
switch (OC) { |
| 181 |
case OpAtomicLoad: |
| 182 |
visitCallSPIRVAtomicLoad(CI); |
| 183 |
break; |
| 184 |
case OpAtomicStore: |
| 185 |
visitCallSPIRVAtomicStore(CI); |
| 186 |
break; |
| 187 |
case OpAtomicFlagClear: |
| 188 |
visitCallSPIRVAtomicFlagClear(CI); |
| 189 |
break; |
| 190 |
case OpAtomicFlagTestAndSet: |
| 191 |
visitCallSPIRVAtomicFlagTestAndSet(CI); |
| 192 |
break; |
| 193 |
case OpAtomicUMin: |
| 194 |
case OpAtomicUMax: |
| 195 |
visitCallSPIRVAtomicUMinUMax(CI, OC); |
| 196 |
break; |
| 197 |
case OpAtomicCompareExchange: |
| 198 |
case OpAtomicCompareExchangeWeak: |
| 199 |
visitCallSPIRVAtomicCmpExchg(CI); |
| 200 |
break; |
| 201 |
default: |
| 202 |
mutateCommonAtomicArguments(CI, OC); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void SPIRVToOCL12Base::visitCallSPIRVEnqueueKernel(CallInst *CI, Op OC) { |
| 207 |
assert(0 && "OpenCL 1.2 doesn't support enqueue_kernel!"); |
| 208 |
} |
| 209 |
|
| 210 |
std::string SPIRVToOCL12Base::mapFPAtomicName(Op OC) { |
| 211 |
assert(isFPAtomicOpCode(OC) && "Not intended to handle other opcodes than " |
| 212 |
"AtomicF{Add/Min/Max}EXT!"); |
| 213 |
switch (OC) { |
| 214 |
case OpAtomicFAddEXT: |
| 215 |
return "atomic_add"; |
| 216 |
case OpAtomicFMinEXT: |
| 217 |
return "atomic_min"; |
| 218 |
case OpAtomicFMaxEXT: |
| 219 |
return "atomic_max"; |
| 220 |
default: |
| 221 |
llvm_unreachable("Unsupported opcode!"); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void SPIRVToOCL12Base::mutateAtomicName(CallInst *CI, Op OC) { |
| 226 |
mutateCallInst(CI, OCL12SPIRVBuiltinMap::rmap(OC)); |
| 227 |
} |
| 228 |
|
| 229 |
std::string SPIRVToOCL12Base::mapAtomicName(Op OC, Type *Ty) { |
| 230 |
std::string Prefix = Ty->isIntegerTy(64) ? kOCLBuiltinName::AtomPrefix |
| 231 |
: kOCLBuiltinName::AtomicPrefix; |
| 232 |
// Map fp atomic instructions to regular OpenCL built-ins. |
| 233 |
if (isFPAtomicOpCode(OC)) |
| 234 |
return mapFPAtomicName(OC); |
| 235 |
return Prefix += OCL12SPIRVBuiltinMap::rmap(OC); |
| 236 |
} |
| 237 |
|
| 238 |
} // namespace SPIRV |
| 239 |
|
| 240 |
INITIALIZE_PASS(SPIRVToOCL12Legacy, "spvtoocl12", |
| 241 |
"Translate SPIR-V builtins to OCL 1.2 builtins", false, false) |
| 242 |
|
| 243 |
ModulePass *llvm::createSPIRVToOCL12Legacy() { |
| 244 |
return new SPIRVToOCL12Legacy(); |
| 245 |
} |