forked from plaidml/plaidml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitocl.cc
450 lines (414 loc) · 12.1 KB
/
emitocl.cc
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright 2017-2018 Intel Corporation.
#include "tile/hal/opencl/emitocl.h"
#include <algorithm>
#include <limits>
#include <map>
#include <utility>
#include "base/util/error.h"
#include "tile/lang/exprtype.h"
#include "tile/lang/fpconv.h"
namespace vertexai {
namespace tile {
namespace hal {
namespace opencl {
static std::map<std::string, std::string> FuncNameMap = {
{"recip", "native_recip"}, {"exp", "native_exp"}, {"log", "native_log"}, {"sqrt", "native_sqrt"}};
void Emit::Visit(const sem::LoadExpr& n) {
auto ty = TypeOf(n.inner);
auto inner = std::dynamic_pointer_cast<sem::SubscriptLVal>(n.inner);
if (!cl_khr_fp16_ && inner && ty.dtype == DataType::FLOAT16) {
// Half-width floats can't be loaded directly on this device.
if (ty.vec_width == 1) {
emit("vload_half");
} else {
emit("vloada_half" + std::to_string(ty.vec_width));
}
emit("(");
inner->offset->Accept(*this);
emit(", ");
inner->ptr->Accept(*this);
emit(")");
} else {
EmitC::Visit(n);
}
}
void Emit::Visit(const sem::StoreStmt& n) {
auto ty_lhs = TypeOf(n.lhs);
auto lhs = std::dynamic_pointer_cast<sem::SubscriptLVal>(n.lhs);
if (!cl_khr_fp16_ && lhs && ty_lhs.dtype == DataType::FLOAT16) {
// Half-width floats can't be stored directly on this device.
emitTab();
if (ty_lhs.vec_width == 1) {
emit("vstore_half");
} else {
emit("vstorea_half" + std::to_string(ty_lhs.vec_width));
}
emit("(");
n.rhs->Accept(*this);
emit(", ");
lhs->offset->Accept(*this);
emit(", ");
lhs->ptr->Accept(*this);
emit(");\n");
} else {
emitTab();
n.lhs->Accept(*this);
emit(" = ");
EmitWithTypeConversion(TypeOf(n.rhs), ty_lhs, n.rhs);
emit(";\n");
}
}
void Emit::Visit(const sem::DeclareStmt& n) {
sem::Type ty = n.type;
sem::Type init_type;
if (n.init) {
init_type = TypeOf(n.init);
}
if (ty.base == sem::Type::VALUE) {
if (ty.dtype == DataType::FLOAT16 && !cl_khr_fp16_) {
ty.dtype = DataType::FLOAT32;
} else if (ty.dtype == DataType::BOOLEAN) {
if (n.init) {
ty.dtype = lang::Promote({init_type}).dtype;
if (ty.dtype == DataType::BOOLEAN) {
// If the initializer was booleans, make it INT8.
ty.dtype = DataType::INT8;
}
} else {
// Assume that this is being initialized from an inter-kernel
// boolean tensor -- which, in OpenCL, we represent as INT8.
ty.dtype = DataType::INT8;
}
}
}
emitTab();
emitType(ty);
emit(" ");
emit(n.name);
if (n.type.array) {
emit("[" + std::to_string(n.type.array) + "]");
}
if (n.init) {
emit(" = ");
if (n.type.array) {
emit("{");
for (size_t i = 0; i < n.type.array; i++) {
n.init->Accept(*this);
emit(", ");
}
emit("}");
} else {
EmitWithTypeConversion(init_type, ty, n.init);
}
}
emit(";\n");
CheckValidType(ty);
scope_->Bind(n.name, ty);
}
void Emit::Visit(const sem::BinaryExpr& n) {
auto ty_lhs = TypeOf(n.lhs);
auto ty_rhs = TypeOf(n.rhs);
auto ty = lang::Promote({ty_lhs, ty_rhs});
emit("(");
EmitWithTypeConversion(ty_lhs, ty, n.lhs);
emit(" ");
emit(n.op);
emit(" ");
EmitWithTypeConversion(ty_rhs, ty, n.rhs);
emit(")");
}
void Emit::Visit(const sem::CondExpr& n) {
auto ty_tcase = TypeOf(n.tcase);
auto ty_fcase = TypeOf(n.fcase);
auto ty_cond = TypeOf(n.cond);
auto ty = lang::Promote({ty_tcase, ty_fcase});
ty.vec_width = std::max(ty.vec_width, ty_cond.vec_width);
emit("select(");
EmitWithTypeConversion(ty_fcase, ty, n.fcase, true);
emit(", ");
EmitWithTypeConversion(ty_tcase, ty, n.tcase, true);
emit(", ");
EmitWithWidthConversion(ty_cond, ty, n.cond, true);
emit(")");
}
void Emit::Visit(const sem::SelectExpr& n) {
auto ty_tcase = TypeOf(n.tcase);
auto ty_fcase = TypeOf(n.fcase);
auto ty_cond = TypeOf(n.cond);
auto ty = lang::Promote({ty_tcase, ty_fcase});
ty.vec_width = std::max(ty.vec_width, ty_cond.vec_width);
emit("select(");
EmitWithTypeConversion(ty_fcase, ty, n.fcase, true);
emit(", ");
EmitWithTypeConversion(ty_tcase, ty, n.tcase, true);
emit(", ");
EmitWithWidthConversion(ty_cond, ty, n.cond, true);
emit(")");
}
void Emit::Visit(const sem::ClampExpr& n) {
auto ty_val = TypeOf(n.val);
auto ty_min = TypeOf(n.min);
auto ty_max = TypeOf(n.max);
// Align value dtypes and vector widths.
sem::Type ty_clamp{sem::Type::VALUE};
if (ty_val.base == sem::Type::VALUE) {
ty_clamp.dtype = ty_val.dtype;
} else {
ty_clamp.dtype = DataType::INT32;
}
if (ty_min.vec_width != 1) {
ty_clamp.vec_width = ty_min.vec_width;
} else {
ty_clamp.vec_width = ty_max.vec_width;
}
emit("clamp(");
EmitWithTypeConversion(ty_val, ty_clamp, n.val, true);
emit(", ");
EmitWithTypeConversion(ty_min, ty_clamp, n.min, true);
emit(", ");
EmitWithTypeConversion(ty_max, ty_clamp, n.max, true);
emit(")");
}
void Emit::Visit(const sem::CastExpr& n) { n.val->Accept(*this); }
void Emit::Visit(const sem::CallExpr& n) {
switch (n.function) {
case sem::CallExpr::Function::CEIL:
case sem::CallExpr::Function::FLOOR: {
assert(1 == n.vals.size());
emit(n.name);
emit("(");
auto val_type = TypeOf(n.vals[0]);
auto need_type = val_type;
switch (need_type.dtype) {
case DataType::BOOLEAN:
case DataType::INT8:
case DataType::INT16:
case DataType::UINT8:
case DataType::UINT16:
need_type.dtype = DataType::FLOAT16;
break;
case DataType::INT32:
case DataType::UINT32:
need_type.dtype = DataType::FLOAT32;
break;
case DataType::INT64:
case DataType::UINT64:
need_type.dtype = DataType::FLOAT64;
break;
default:
break;
}
EmitWithTypeConversion(val_type, need_type, n.vals[0], false);
emit(")");
} break;
default: {
auto it = FuncNameMap.find(n.name);
if (it != FuncNameMap.end()) {
emit(it->second);
} else {
// Assume this is an OpenCL function.
// TODO: Enumerate the set of callable functions.
emit(n.name);
}
emit("(");
for (size_t i = 0; i < n.vals.size(); i++) {
n.vals[i]->Accept(*this);
if (i != n.vals.size() - 1) {
emit(", ");
}
}
emit(")");
} break;
}
}
void Emit::Visit(const sem::IndexExpr& n) {
switch (n.type) {
case sem::IndexExpr::GLOBAL:
emit("get_global_id(" + std::to_string(n.dim) + ")");
break;
case sem::IndexExpr::GROUP:
emit("get_group_id(" + std::to_string(n.dim) + ")");
break;
case sem::IndexExpr::LOCAL:
emit("get_local_id(" + std::to_string(n.dim) + ")");
break;
default:
throw std::runtime_error("Invalid IndexExpr type");
}
}
void Emit::Visit(const sem::Block& n) {
auto previous_scope = scope_;
lang::Scope<sem::Type> scope{scope_};
scope_ = &scope;
EmitC::Visit(n);
scope_ = previous_scope;
}
void Emit::Visit(const sem::ForStmt& n) {
auto previous_scope = scope_;
lang::Scope<sem::Type> scope{scope_};
scope_ = &scope;
scope.Bind(n.var, sem::Type{sem::Type::INDEX});
EmitC::Visit(n);
scope_ = previous_scope;
}
void Emit::Visit(const sem::BarrierStmt& n) {
emitTab();
if (n.subgroup) {
// emit("sub_group_barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE);\n");
} else {
emit("barrier(CLK_LOCAL_MEM_FENCE);\n");
}
}
void Emit::Visit(const sem::Function& n) {
if (n.subgroup_size) {
emit("__attribute__((intel_reqd_sub_group_size(" + std::to_string(n.subgroup_size) + ")))");
}
emit("__kernel ");
lang::Scope<sem::Type> scope;
scope_ = &scope;
for (const auto& p : n.params) {
auto ty = p.first;
if (ty.dtype == DataType::BOOLEAN) {
// Global booleans are stored as INT8.
ty.dtype = DataType::INT8;
}
CheckValidType(ty);
scope.Bind(p.second, ty);
}
emitType(n.ret);
emit(" ");
emit(n.name);
emit("(");
bool first_param = true;
for (const auto& p : n.params) {
if (first_param) {
first_param = false;
} else {
emit(", ");
}
auto ty = p.first;
if (!cl_khr_fp16_ && ty.dtype == DataType::FLOAT16) {
// The device can only use half-width floats as a pointer type, not as a
// value
// or a vector element type. We'll use vloada_half and vstorea_half to
// access
// the memory, but the parameter must be declared with a vector width of
// 1.
// Note that what's stored in the scope must have the original vector
// width,
// which is why we set up the scope using a separate loop.
ty.vec_width = 1;
} else if (ty.dtype == DataType::BOOLEAN) {
// Global booleans are stored as INT8.
ty.dtype = DataType::INT8;
}
emitType(ty);
emit(" ");
emit(p.second);
}
emit(")\n");
n.body->Accept(*this);
scope_ = nullptr;
}
void Emit::CheckValidType(const sem::Type& ty) {
if (cl_khr_fp64_) {
return;
}
if (ty.base == sem::Type::TVOID || ty.base == sem::Type::INDEX) {
return;
}
if (ty.dtype == DataType::FLOAT64) {
throw error::Unimplemented{"The device does not support 64-bit floating-point types"};
}
}
sem::Type Emit::TypeOf(const sem::ExprPtr& expr) { return lang::ExprType::TypeOf(scope_, cl_khr_fp16_, true, expr); }
sem::Type Emit::TypeOf(const sem::LValPtr& lvalue) {
return lang::ExprType::TypeOf(scope_, cl_khr_fp16_, true, lvalue);
}
void Emit::EmitWithTypeConversion(const sem::Type& from, const sem::Type& to, const sem::ExprPtr& expr,
bool force_conversion) {
if (to.base == sem::Type::POINTER_MUT || to.base == sem::Type::POINTER_CONST) {
// No conversion required for pointer types.
expr->Accept(*this);
return;
}
if (!force_conversion && (from.vec_width == to.vec_width) &&
((from.vec_width == 1 && from.base == sem::Type::VALUE && is_int(from.dtype) &&
(to.base == sem::Type::INDEX || (to.base == sem::Type::VALUE && is_int(to.dtype)))) ||
(from.base == to.base && from.dtype == to.dtype))) {
// No conversion required.
expr->Accept(*this);
return;
}
if (from.base == sem::Type::INDEX || (from.base == sem::Type::VALUE && from.vec_width == 1)) {
emit("(");
EmitC::emitType(to);
emit(")");
expr->Accept(*this);
return;
}
emit("convert_");
EmitC::emitType(to);
emit("(");
expr->Accept(*this);
emit(")");
}
void Emit::EmitWithWidthConversion(const sem::Type& from, const sem::Type& to, const sem::ExprPtr& expr,
bool force_conversion) {
if (to.base == sem::Type::POINTER_MUT || to.base == sem::Type::POINTER_CONST) {
// No conversion required.
expr->Accept(*this);
return;
}
// Based on the target type, convert to the appropriate condition type.
sem::Type condition_type = to;
switch (condition_type.dtype) {
case DataType::BOOLEAN:
case DataType::INT8:
case DataType::UINT8:
condition_type.dtype = DataType::INT8;
break;
case DataType::INT16:
case DataType::UINT16:
case DataType::FLOAT16:
condition_type.dtype = DataType::INT16;
break;
case DataType::INT32:
case DataType::UINT32:
case DataType::FLOAT32:
condition_type.dtype = DataType::INT32;
break;
case DataType::INT64:
case DataType::UINT64:
case DataType::FLOAT64:
condition_type.dtype = DataType::INT64;
break;
default:
break;
}
EmitWithTypeConversion(from, condition_type, expr, force_conversion);
if (from.vec_width != to.vec_width) {
// We need to convert a scalar into a vector.
// See the OpenCL 1.2 spec (pg 219, Section 6.3: Operators, e.)
emit(" != ");
emit("(");
EmitC::emitType(condition_type);
emit(")");
emit("0");
}
}
void Emit::emitType(const sem::Type& t) {
if (t.region == sem::Type::LOCAL) {
emit("__local ");
} else if (t.region == sem::Type::GLOBAL) {
emit("__global ");
}
EmitC::emitType(t);
if (t.region == sem::Type::GLOBAL) {
emit(" restrict ");
}
}
} // namespace opencl
} // namespace hal
} // namespace tile
} // namespace vertexai