forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGen_X86.cpp
More file actions
501 lines (428 loc) · 16.5 KB
/
CodeGen_X86.cpp
File metadata and controls
501 lines (428 loc) · 16.5 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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include <iostream>
#include "CodeGen_X86.h"
#include "JITModule.h"
#include "IROperator.h"
#include "IRMatch.h"
#include "Debug.h"
#include "Util.h"
#include "Var.h"
#include "Param.h"
#include "LLVM_Headers.h"
#include "IRMutator.h"
namespace Halide {
namespace Internal {
using std::vector;
using std::string;
using namespace llvm;
CodeGen_X86::CodeGen_X86(Target t) : CodeGen_Posix(t) {
#if !(WITH_X86)
user_error << "x86 not enabled for this build of Halide.\n";
#endif
user_assert(llvm_X86_enabled) << "llvm build not configured with X86 target enabled.\n";
#if !(WITH_NATIVE_CLIENT)
user_assert(t.os != Target::NaCl) << "llvm build not configured with native client enabled.\n";
#endif
}
Expr _i64(Expr e) {
return cast(Int(64, e.type().lanes()), e);
}
Expr _u64(Expr e) {
return cast(UInt(64, e.type().lanes()), e);
}
Expr _i32(Expr e) {
return cast(Int(32, e.type().lanes()), e);
}
Expr _u32(Expr e) {
return cast(UInt(32, e.type().lanes()), e);
}
Expr _i16(Expr e) {
return cast(Int(16, e.type().lanes()), e);
}
Expr _u16(Expr e) {
return cast(UInt(16, e.type().lanes()), e);
}
Expr _i8(Expr e) {
return cast(Int(8, e.type().lanes()), e);
}
Expr _u8(Expr e) {
return cast(UInt(8, e.type().lanes()), e);
}
Expr _f32(Expr e) {
return cast(Float(32, e.type().lanes()), e);
}
Expr _f64(Expr e) {
return cast(Float(64, e.type().lanes()), e);
}
namespace {
// i32(i16_a)*i32(i16_b) +/- i32(i16_c)*i32(i16_d) can be done by
// interleaving a, c, and b, d, and then using pmaddwd. We
// recognize it here, and implement it in the initial module.
bool should_use_pmaddwd(Expr a, Expr b, vector<Expr> &result) {
Type t = a.type();
internal_assert(b.type() == t);
const Mul *ma = a.as<Mul>();
const Mul *mb = b.as<Mul>();
if (!(ma && mb && t.is_int() && t.bits() == 32 && (t.lanes() >= 4))) {
return false;
}
Type narrow = t.with_bits(16);
vector<Expr> args = {lossless_cast(narrow, ma->a),
lossless_cast(narrow, ma->b),
lossless_cast(narrow, mb->a),
lossless_cast(narrow, mb->b)};
if (!args[0].defined() || !args[1].defined() ||
!args[2].defined() || !args[3].defined()) {
return false;
}
result.swap(args);
return true;
}
}
void CodeGen_X86::visit(const Add *op) {
vector<Expr> matches;
if (should_use_pmaddwd(op->a, op->b, matches)) {
codegen(Call::make(op->type, "pmaddwd", matches, Call::Extern));
} else {
CodeGen_Posix::visit(op);
}
}
void CodeGen_X86::visit(const Sub *op) {
vector<Expr> matches;
if (should_use_pmaddwd(op->a, op->b, matches)) {
// Negate one of the factors in the second expression
if (is_const(matches[2])) {
matches[2] = -matches[2];
} else {
matches[3] = -matches[3];
}
codegen(Call::make(op->type, "pmaddwd", matches, Call::Extern));
} else {
CodeGen_Posix::visit(op);
}
}
void CodeGen_X86::visit(const GT *op) {
Type t = op->a.type();
int bits = t.lanes() * t.bits();
if (t.lanes() == 1 || bits % 128 == 0) {
// LLVM is fine for native vector widths or scalars
CodeGen_Posix::visit(op);
} else {
// Non-native vector widths get legalized poorly by llvm. We
// split it up ourselves.
Value *a = codegen(op->a), *b = codegen(op->b);
int slice_size = 128 / t.bits();
if (target.has_feature(Target::AVX) && bits > 128) {
slice_size = 256 / t.bits();
}
vector<Value *> result;
for (int i = 0; i < op->type.lanes(); i += slice_size) {
Value *sa = slice_vector(a, i, slice_size);
Value *sb = slice_vector(b, i, slice_size);
Value *slice_value;
if (t.is_float()) {
slice_value = builder->CreateFCmpOGT(sa, sb);
} else if (t.is_int()) {
slice_value = builder->CreateICmpSGT(sa, sb);
} else {
slice_value = builder->CreateICmpUGT(sa, sb);
}
result.push_back(slice_value);
}
value = concat_vectors(result);
value = slice_vector(value, 0, t.lanes());
}
}
void CodeGen_X86::visit(const EQ *op) {
Type t = op->a.type();
int bits = t.lanes() * t.bits();
if (t.lanes() == 1 || bits % 128 == 0) {
// LLVM is fine for native vector widths or scalars
CodeGen_Posix::visit(op);
} else {
// Non-native vector widths get legalized poorly by llvm. We
// split it up ourselves.
Value *a = codegen(op->a), *b = codegen(op->b);
int slice_size = 128 / t.bits();
if (target.has_feature(Target::AVX) && bits > 128) {
slice_size = 256 / t.bits();
}
vector<Value *> result;
for (int i = 0; i < op->type.lanes(); i += slice_size) {
Value *sa = slice_vector(a, i, slice_size);
Value *sb = slice_vector(b, i, slice_size);
Value *slice_value;
if (t.is_float()) {
slice_value = builder->CreateFCmpOEQ(sa, sb);
} else {
slice_value = builder->CreateICmpEQ(sa, sb);
}
result.push_back(slice_value);
}
value = concat_vectors(result);
value = slice_vector(value, 0, t.lanes());
}
}
void CodeGen_X86::visit(const LT *op) {
codegen(op->b > op->a);
}
void CodeGen_X86::visit(const LE *op) {
codegen(!(op->a > op->b));
}
void CodeGen_X86::visit(const GE *op) {
codegen(!(op->b > op->a));
}
void CodeGen_X86::visit(const NE *op) {
codegen(!(op->a == op->b));
}
void CodeGen_X86::visit(const Select *op) {
// LLVM doesn't correctly use pblendvb for u8 vectors that aren't
// width 16, so we peephole optimize them to intrinsics.
struct Pattern {
string intrin;
Expr pattern;
};
static Pattern patterns[] = {
{"pblendvb_ult_i8x16", select(wild_u8x_ < wild_u8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_ult_i8x16", select(wild_u8x_ < wild_u8x_, wild_u8x_, wild_u8x_)},
{"pblendvb_slt_i8x16", select(wild_i8x_ < wild_i8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_slt_i8x16", select(wild_i8x_ < wild_i8x_, wild_u8x_, wild_u8x_)},
{"pblendvb_ule_i8x16", select(wild_u8x_ <= wild_u8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_ule_i8x16", select(wild_u8x_ <= wild_u8x_, wild_u8x_, wild_u8x_)},
{"pblendvb_sle_i8x16", select(wild_i8x_ <= wild_i8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_sle_i8x16", select(wild_i8x_ <= wild_i8x_, wild_u8x_, wild_u8x_)},
{"pblendvb_ne_i8x16", select(wild_u8x_ != wild_u8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_ne_i8x16", select(wild_u8x_ != wild_u8x_, wild_u8x_, wild_u8x_)},
{"pblendvb_ne_i8x16", select(wild_i8x_ != wild_i8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_ne_i8x16", select(wild_i8x_ != wild_i8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_eq_i8x16", select(wild_u8x_ == wild_u8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_eq_i8x16", select(wild_u8x_ == wild_u8x_, wild_u8x_, wild_u8x_)},
{"pblendvb_eq_i8x16", select(wild_i8x_ == wild_i8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_eq_i8x16", select(wild_i8x_ == wild_i8x_, wild_i8x_, wild_i8x_)},
{"pblendvb_i8x16", select(wild_u1x_, wild_i8x_, wild_i8x_)},
{"pblendvb_i8x16", select(wild_u1x_, wild_u8x_, wild_u8x_)}
};
if (target.has_feature(Target::SSE41) &&
op->condition.type().is_vector() &&
op->type.bits() == 8 &&
op->type.lanes() != 16) {
vector<Expr> matches;
for (size_t i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++) {
if (expr_match(patterns[i].pattern, op, matches)) {
value = call_intrin(op->type, 16, patterns[i].intrin, matches);
return;
}
}
}
CodeGen_Posix::visit(op);
}
void CodeGen_X86::visit(const Cast *op) {
if (!op->type.is_vector()) {
// We only have peephole optimizations for vectors in here.
CodeGen_Posix::visit(op);
return;
}
vector<Expr> matches;
struct Pattern {
bool needs_sse_41;
bool wide_op;
Type type;
string intrin;
Expr pattern;
};
static Pattern patterns[] = {
{false, true, Int(8, 16), "llvm.x86.sse2.padds.b",
_i8(clamp(wild_i16x_ + wild_i16x_, -128, 127))},
{false, true, Int(8, 16), "llvm.x86.sse2.psubs.b",
_i8(clamp(wild_i16x_ - wild_i16x_, -128, 127))},
{false, true, UInt(8, 16), "llvm.x86.sse2.paddus.b",
_u8(min(wild_u16x_ + wild_u16x_, 255))},
{false, true, UInt(8, 16), "llvm.x86.sse2.psubus.b",
_u8(max(wild_i16x_ - wild_i16x_, 0))},
{false, true, Int(16, 8), "llvm.x86.sse2.padds.w",
_i16(clamp(wild_i32x_ + wild_i32x_, -32768, 32767))},
{false, true, Int(16, 8), "llvm.x86.sse2.psubs.w",
_i16(clamp(wild_i32x_ - wild_i32x_, -32768, 32767))},
{false, true, UInt(16, 8), "llvm.x86.sse2.paddus.w",
_u16(min(wild_u32x_ + wild_u32x_, 65535))},
{false, true, UInt(16, 8), "llvm.x86.sse2.psubus.w",
_u16(max(wild_i32x_ - wild_i32x_, 0))},
{false, true, Int(16, 8), "llvm.x86.sse2.pmulh.w",
_i16((wild_i32x_ * wild_i32x_) / 65536)},
{false, true, UInt(16, 8), "llvm.x86.sse2.pmulhu.w",
_u16((wild_u32x_ * wild_u32x_) / 65536)},
{false, true, UInt(8, 16), "llvm.x86.sse2.pavg.b",
_u8(((wild_u16x_ + wild_u16x_) + 1) / 2)},
{false, true, UInt(16, 8), "llvm.x86.sse2.pavg.w",
_u16(((wild_u32x_ + wild_u32x_) + 1) / 2)},
{false, false, Int(16, 8), "packssdwx8",
_i16(clamp(wild_i32x_, -32768, 32767))},
{false, false, Int(8, 16), "packsswbx16",
_i8(clamp(wild_i16x_, -128, 127))},
{false, false, UInt(8, 16), "packuswbx16",
_u8(clamp(wild_i16x_, 0, 255))},
{true, false, UInt(16, 8), "packusdwx8",
_u16(clamp(wild_i32x_, 0, 65535))}
};
for (size_t i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++) {
const Pattern &pattern = patterns[i];
if (!target.has_feature(Target::SSE41) && pattern.needs_sse_41) {
continue;
}
if (expr_match(pattern.pattern, op, matches)) {
bool match = true;
if (pattern.wide_op) {
// Try to narrow the matches to the target type.
for (size_t i = 0; i < matches.size(); i++) {
matches[i] = lossless_cast(op->type, matches[i]);
if (!matches[i].defined()) match = false;
}
}
if (match) {
value = call_intrin(op->type, pattern.type.lanes(), pattern.intrin, matches);
return;
}
}
}
#if LLVM_VERSION >= 38
// Workaround for https://llvm.org/bugs/show_bug.cgi?id=24512
// LLVM uses a numerically unstable method for vector
// uint32->float conversion before AVX.
if (op->value.type().element_of() == UInt(32) &&
op->type.is_float() &&
op->type.is_vector() &&
!target.has_feature(Target::AVX)) {
Type signed_type = Int(32, op->type.lanes());
// Convert the top 31 bits to float using the signed version
Expr top_bits = cast(signed_type, op->value / 2);
top_bits = cast(op->type, top_bits);
// Convert the bottom bit
Expr bottom_bit = cast(signed_type, op->value % 2);
bottom_bit = cast(op->type, bottom_bit);
// Recombine as floats
codegen(top_bits + top_bits + bottom_bit);
return;
}
#endif
CodeGen_Posix::visit(op);
}
Expr CodeGen_X86::mulhi_shr(Expr a, Expr b, int shr) {
Type ty = a.type();
if (ty.is_vector() && ty.bits() == 16) {
// We can use pmulhu for this op.
Expr p = _u16(_u32(a) * _u32(b) / 65536);
if (shr) {
p = p >> shr;
}
return p;
}
return CodeGen_Posix::mulhi_shr(a, b, shr);
}
void CodeGen_X86::visit(const Min *op) {
if (!op->type.is_vector()) {
CodeGen_Posix::visit(op);
return;
}
bool use_sse_41 = target.has_feature(Target::SSE41);
if (op->type.element_of() == UInt(8)) {
value = call_intrin(op->type, 16, "llvm.x86.sse2.pminu.b", {op->a, op->b});
} else if (use_sse_41 && op->type.element_of() == Int(8)) {
value = call_intrin(op->type, 16, "llvm.x86.sse41.pminsb", {op->a, op->b});
} else if (op->type.element_of() == Int(16)) {
value = call_intrin(op->type, 8, "llvm.x86.sse2.pmins.w", {op->a, op->b});
} else if (use_sse_41 && op->type.element_of() == UInt(16)) {
value = call_intrin(op->type, 8, "llvm.x86.sse41.pminuw", {op->a, op->b});
} else if (use_sse_41 && op->type.element_of() == Int(32)) {
value = call_intrin(op->type, 4, "llvm.x86.sse41.pminsd", {op->a, op->b});
} else if (use_sse_41 && op->type.element_of() == UInt(32)) {
value = call_intrin(op->type, 4, "llvm.x86.sse41.pminud", {op->a, op->b});
} else if (op->type.element_of() == Float(32)) {
if (op->type.lanes() % 8 == 0 && target.has_feature(Target::AVX)) {
// This condition should possibly be > 4, rather than a
// multiple of 8, but shuffling in undefs seems to work
// poorly with avx.
value = call_intrin(op->type, 8, "min_f32x8", {op->a, op->b});
} else {
value = call_intrin(op->type, 4, "min_f32x4", {op->a, op->b});
}
} else if (op->type.element_of() == Float(64)) {
if (op->type.lanes() % 4 == 0 && target.has_feature(Target::AVX)) {
value = call_intrin(op->type, 4, "min_f64x4", {op->a, op->b});
} else {
value = call_intrin(op->type, 2, "min_f64x2", {op->a, op->b});
}
} else {
CodeGen_Posix::visit(op);
}
}
void CodeGen_X86::visit(const Max *op) {
if (!op->type.is_vector()) {
CodeGen_Posix::visit(op);
return;
}
bool use_sse_41 = target.has_feature(Target::SSE41);
if (op->type.element_of() == UInt(8)) {
value = call_intrin(op->type, 16, "llvm.x86.sse2.pmaxu.b", {op->a, op->b});
} else if (use_sse_41 && op->type.element_of() == Int(8)) {
value = call_intrin(op->type, 16, "llvm.x86.sse41.pmaxsb", {op->a, op->b});
} else if (op->type.element_of() == Int(16)) {
value = call_intrin(op->type, 8, "llvm.x86.sse2.pmaxs.w", {op->a, op->b});
} else if (use_sse_41 && op->type.element_of() == UInt(16)) {
value = call_intrin(op->type, 8, "llvm.x86.sse41.pmaxuw", {op->a, op->b});
} else if (use_sse_41 && op->type.element_of() == Int(32)) {
value = call_intrin(op->type, 4, "llvm.x86.sse41.pmaxsd", {op->a, op->b});
} else if (use_sse_41 && op->type.element_of() == UInt(32)) {
value = call_intrin(op->type, 4, "llvm.x86.sse41.pmaxud", {op->a, op->b});
} else if (op->type.element_of() == Float(32)) {
if (op->type.lanes() % 8 == 0 && target.has_feature(Target::AVX)) {
value = call_intrin(op->type, 8, "max_f32x8", {op->a, op->b});
} else {
value = call_intrin(op->type, 4, "max_f32x4", {op->a, op->b});
}
} else if (op->type.element_of() == Float(64)) {
if (op->type.lanes() % 4 == 0 && target.has_feature(Target::AVX)) {
value = call_intrin(op->type, 4, "max_f64x4", {op->a, op->b});
} else {
value = call_intrin(op->type, 2, "max_f64x2", {op->a, op->b});
}
} else {
CodeGen_Posix::visit(op);
}
}
string CodeGen_X86::mcpu() const {
if (target.has_feature(Target::AVX)) return "corei7-avx";
// We want SSE4.1 but not SSE4.2, hence "penryn" rather than "corei7"
if (target.has_feature(Target::SSE41)) return "penryn";
// Default should not include SSSE3, hence "k8" rather than "core2"
return "k8";
}
string CodeGen_X86::mattrs() const {
std::string features;
std::string separator;
#if LLVM_VERSION >= 35
// These attrs only exist in llvm 3.5+
if (target.has_feature(Target::FMA)) {
features += "+fma";
separator = ",";
}
if (target.has_feature(Target::FMA4)) {
features += separator + "+fma4";
separator = ",";
}
if (target.has_feature(Target::F16C)) {
features += separator + "+f16c";
separator = ",";
}
#endif
return features;
}
bool CodeGen_X86::use_soft_float_abi() const {
return false;
}
int CodeGen_X86::native_vector_bits() const {
if (target.has_feature(Target::AVX)) {
return 256;
} else {
return 128;
}
}
}}