forked from KhronosGroup/SPIRV-LLVM-Translator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPIRVToOCL12.cpp
More file actions
328 lines (288 loc) · 12.2 KB
/
SPIRVToOCL12.cpp
File metadata and controls
328 lines (288 loc) · 12.2 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//===- 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 {
class SPIRVToOCL12 : public SPIRVToOCL {
public:
SPIRVToOCL12() : SPIRVToOCL(ID) {
initializeSPIRVToOCL12Pass(*PassRegistry::getPassRegistry());
}
bool runOnModule(Module &M) override;
/// Transform __spirv_MemoryBarrier to atomic_work_item_fence.
/// __spirv_MemoryBarrier(scope, sema) =>
/// atomic_work_item_fence(flag(sema), order(sema), map(scope))
void visitCallSPIRVMemoryBarrier(CallInst *CI) override;
/// Transform __spirv_ControlBarrier to barrier.
/// __spirv_ControlBarrier(execScope, memScope, sema) =>
/// barrier(flag(sema))
void visitCallSPIRVControlBarrier(CallInst *CI) override;
/// Transform __spirv_OpAtomic functions. It firstly conduct generic
/// mutations for all builtins and then mutate some of them seperately
Instruction *visitCallSPIRVAtomicBuiltin(CallInst *CI, Op OC) override;
/// Transform __spirv_OpAtomicIIncrement / OpAtomicIDecrement to
/// atomic_inc / atomic_dec
Instruction *visitCallSPIRVAtomicIncDec(CallInst *CI, Op OC) override;
/// Transform __spirv_OpAtomicUMin/SMin/UMax/SMax into
/// atomic_min/atomic_max, as there is no distinction in OpenCL 1.2
/// between signed and unsigned version of those functions
Instruction *visitCallSPIRVAtomicUMinUMax(CallInst *CI, Op OC);
/// Transform __spirv_OpAtomicLoad to atomic_add(*ptr, 0)
Instruction *visitCallSPIRVAtomicLoad(CallInst *CI);
/// Transform __spirv_OpAtomicStore to atomic_xchg(*ptr, value)
Instruction *visitCallSPIRVAtomicStore(CallInst *CI);
/// Transform __spirv_OpAtomicFlagClear to atomic_xchg(*ptr, 0)
/// with ignoring the result
Instruction *visitCallSPIRVAtomicFlagClear(CallInst *CI);
/// Transform __spirv_OpAtomicFlagTestAndTest to
/// (bool)atomic_xchg(*ptr, 1)
Instruction *visitCallSPIRVAtomicFlagTestAndSet(CallInst *CI);
/// Transform __spirv_OpAtomicCompareExchange and
/// __spirv_OpAtomicCompareExchangeWeak into atomic_cmpxchg. There is no
/// weak version of function in OpenCL 1.2
Instruction *visitCallSPIRVAtomicCmpExchg(CallInst *CI, Op OC) override;
/// Conduct generic mutations for all atomic builtins
CallInst *mutateCommonAtomicArguments(CallInst *CI, Op OC) override;
/// Transform atomic builtin name into correct ocl-dependent name
Instruction *mutateAtomicName(CallInst *CI, Op OC) override;
/// Transform SPIR-V atomic instruction opcode into OpenCL 1.2 builtin name.
/// Depending on the type, the return name starts with "atomic_" for 32-bit
/// types or with "atom_" for 64-bit types, as specified by
/// cl_khr_int64_base_atomics and cl_khr_int64_extended_atomics extensions.
std::string mapAtomicName(Op OC, Type *Ty);
static char ID;
};
char SPIRVToOCL12::ID = 0;
bool SPIRVToOCL12::runOnModule(Module &Module) {
M = &Module;
Ctx = &M->getContext();
visit(*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 SPIRVToOCL12::visitCallSPIRVMemoryBarrier(CallInst *CI) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
Value *MemFenceFlags =
transSPIRVMemorySemanticsIntoOCLMemFenceFlags(Args[1], CI);
Args.assign(1, MemFenceFlags);
return kOCLBuiltinName::MemFence;
},
&Attrs);
}
void SPIRVToOCL12::visitCallSPIRVControlBarrier(CallInst *CI) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
Attrs = Attrs.addAttribute(CI->getContext(), AttributeList::FunctionIndex,
Attribute::Convergent);
mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
auto *MemFenceFlags =
transSPIRVMemorySemanticsIntoOCLMemFenceFlags(Args[2], CI);
Args.assign(1, MemFenceFlags);
return kOCLBuiltinName::Barrier;
},
&Attrs);
}
Instruction *SPIRVToOCL12::visitCallSPIRVAtomicIncDec(CallInst *CI, Op OC) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
return mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
Args.resize(1);
return mapAtomicName(OC, CI->getType());
},
&Attrs);
}
CallInst *SPIRVToOCL12::mutateCommonAtomicArguments(CallInst *CI, Op OC) {
assert(CI->getCalledFunction() && "Unexpected indirect call");
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
return mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
auto Ptr = findFirstPtr(Args);
auto NumOrder = getSPIRVAtomicBuiltinNumMemoryOrderArgs(OC);
auto ArgsToRemove = NumOrder + 1; // OpenCL1.2 builtins does not use
// scope and memory order arguments
auto StartIdx = Ptr + 1;
auto StopIdx = StartIdx + ArgsToRemove;
Args.erase(Args.begin() + StartIdx, Args.begin() + StopIdx);
return mapAtomicName(OC, CI->getType());
},
&Attrs);
}
Instruction *SPIRVToOCL12::visitCallSPIRVAtomicUMinUMax(CallInst *CI, Op OC) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
return mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
std::swap(Args[1], Args[3]);
Args.resize(2);
return mapAtomicName(OC, CI->getType());
},
&Attrs);
}
Instruction *SPIRVToOCL12::visitCallSPIRVAtomicLoad(CallInst *CI) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
return mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
Args.resize(1);
// There is no atomic_load in OpenCL 1.2 spec.
// Emit this builtin via call of atomic_add(*p, 0).
Type *ptrElemTy = Args[0]->getType()->getPointerElementType();
Args.push_back(Constant::getNullValue(ptrElemTy));
return mapAtomicName(OpAtomicIAdd, ptrElemTy);
},
&Attrs);
}
Instruction *SPIRVToOCL12::visitCallSPIRVAtomicStore(CallInst *CI) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
return mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args, Type *&RetTy) {
std::swap(Args[1], Args[3]);
Args.resize(2);
// The type of the value pointed to by Pointer (1st argument)
// must be the same as Result Type.
RetTy = Args[0]->getType()->getPointerElementType();
return mapAtomicName(OpAtomicExchange, RetTy);
},
[=](CallInst *CI) -> Instruction * { return CI; }, &Attrs);
}
Instruction *SPIRVToOCL12::visitCallSPIRVAtomicFlagClear(CallInst *CI) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
return mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args, Type *&RetTy) {
Args.resize(1);
Args.push_back(getInt32(M, 0));
RetTy = Type::getInt32Ty(M->getContext());
return mapAtomicName(OpAtomicExchange, RetTy);
},
[=](CallInst *CI) -> Instruction * { return CI; }, &Attrs);
}
Instruction *SPIRVToOCL12::visitCallSPIRVAtomicFlagTestAndSet(CallInst *CI) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
return mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args, Type *&RetTy) {
Args.resize(1);
Args.push_back(getInt32(M, 1));
RetTy = Type::getInt32Ty(M->getContext());
return mapAtomicName(OpAtomicExchange, RetTy);
},
[=](CallInst *CI) -> Instruction * {
return BitCastInst::Create(Instruction::Trunc, CI,
Type::getInt1Ty(CI->getContext()), "",
CI->getNextNode());
},
&Attrs);
}
Instruction *SPIRVToOCL12::visitCallSPIRVAtomicCmpExchg(CallInst *CI, Op OC) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
return mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
Args.erase(Args.begin() + 1, Args.begin() + 4);
// SPIRV OpAtomicCompareExchange and OpAtomicCompareExchangeWeak
// has Value and Comparator in different order than ocl functions
// both of them are translated into atomic_cmpxchg
std::swap(Args[1], Args[2]);
// Type of return value, pointee of the pointer operand, other operands,
// all match, and should be integer scalar types.
return mapAtomicName(OpAtomicCompareExchange, CI->getType());
},
&Attrs);
}
Instruction *SPIRVToOCL12::visitCallSPIRVAtomicBuiltin(CallInst *CI, Op OC) {
Instruction *NewCI = nullptr;
switch (OC) {
case OpAtomicLoad:
NewCI = visitCallSPIRVAtomicLoad(CI);
break;
case OpAtomicStore:
NewCI = visitCallSPIRVAtomicStore(CI);
break;
case OpAtomicFlagClear:
NewCI = visitCallSPIRVAtomicFlagClear(CI);
break;
case OpAtomicFlagTestAndSet:
NewCI = visitCallSPIRVAtomicFlagTestAndSet(CI);
break;
case OpAtomicUMin:
case OpAtomicUMax:
NewCI = visitCallSPIRVAtomicUMinUMax(CI, OC);
break;
case OpAtomicCompareExchange:
case OpAtomicCompareExchangeWeak:
NewCI = visitCallSPIRVAtomicCmpExchg(CI, OC);
break;
default:
NewCI = mutateCommonAtomicArguments(CI, OC);
}
return NewCI;
}
Instruction *SPIRVToOCL12::mutateAtomicName(CallInst *CI, Op OC) {
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
return mutateCallInstOCL(
M, CI,
[=](CallInst *, std::vector<Value *> &Args) {
return OCL12SPIRVBuiltinMap::rmap(OC);
},
&Attrs);
}
std::string SPIRVToOCL12::mapAtomicName(Op OC, Type *Ty) {
std::string Prefix = Ty->isIntegerTy(64) ? kOCLBuiltinName::AtomPrefix
: kOCLBuiltinName::AtomicPrefix;
return Prefix += OCL12SPIRVBuiltinMap::rmap(OC);
}
} // namespace SPIRV
INITIALIZE_PASS(SPIRVToOCL12, "spvtoocl12",
"Translate SPIR-V builtins to OCL 1.2 builtins", false, false)
ModulePass *llvm::createSPIRVToOCL12() { return new SPIRVToOCL12(); }