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