forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeGenHelpers.h
271 lines (252 loc) · 7.92 KB
/
CodeGenHelpers.h
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <asmjit/asmjit.h>
#include "fbgemm/Utils.h"
namespace fbgemm {
namespace x86 = asmjit::x86;
/**
* @brief Create instruction sequence to generate 16-bit 1s
* @tparam T Register type of destination, e.g., x86::Ymm or x86::Zmm
*
* @param dest Once the instruction sequence is executed,
* dest[0:15] will have 0x0001, dest[16:31]
* will have 0x0001 and so on
*/
template <
inst_set_t instSet,
typename T,
typename std::enable_if<instSet == inst_set_t::avx2, int>::type = 0>
void gen16BitVectorOne(x86::Emitter* a, T dest) {
a->vpcmpeqw(dest, dest, dest);
a->vpsrlw(dest, dest, 15);
}
template <
inst_set_t instSet,
typename T,
typename std::enable_if<
instSet == inst_set_t::avx512 || instSet == inst_set_t::avx512_ymm ||
instSet == inst_set_t::avx512_vnni ||
instSet == inst_set_t::avx512_vnni_ymm,
int>::type = 0>
void gen16BitVectorOne(x86::Emitter* a, T dest) {
a->vpternlogd(dest, dest, dest, 0xff);
a->vpsrlw(dest, dest, 15);
}
/**
* @brief Emit instruction do load 32-bit integer. AVX512 has
* different instrunction to load registers with index >= 16
* @tparam T Register type of destination, e.g., x86::Ymm or x86::Zmm
*
* @param dest Destination vector register
*/
template <
inst_set_t instSet,
typename T,
typename std::enable_if<instSet == inst_set_t::avx2, int>::type = 0>
void emitLoadDWord(x86::Emitter* a, T dest, const x86::Mem& ptr) {
a->vmovdqa(dest, ptr);
}
template <
inst_set_t instSet,
typename T,
typename std::enable_if<
instSet == inst_set_t::avx512 || instSet == inst_set_t::avx512_ymm ||
instSet == inst_set_t::avx512_vnni ||
instSet == inst_set_t::avx512_vnni_ymm,
int>::type = 0>
void emitLoadDWord(x86::Emitter* a, T dest, const x86::Mem& ptr) {
a->vmovdqa32(dest, ptr);
}
/**
* @brief Emit partial extract from Wide regiter to Half Register, eg.
* Zmm -> Ymm or Ymm -> Xmm
* @tparam instSet instruction set to be used
*
* @param half Destination (half) vector register
* @param vec Source (full) vector register
* @param idx Index of of the half vector 0 or 1
*/
template <
inst_set_t instSet,
typename T,
typename std::enable_if<
instSet == inst_set_t::avx512 || instSet == inst_set_t::avx512_ymm ||
instSet == inst_set_t::avx512_vnni ||
instSet == inst_set_t::avx512_vnni_ymm,
int>::type = 0>
void emitExtractHalfVector(
x86::Emitter* a,
x86::Ymm half,
const x86::Zmm vec,
int idx) {
a->vextracti32x8(half, vec, idx);
}
template <
inst_set_t instSet,
typename T,
typename std::enable_if<
instSet == inst_set_t::avx512 || instSet == inst_set_t::avx512_ymm ||
instSet == inst_set_t::avx512_vnni ||
instSet == inst_set_t::avx512_vnni_ymm,
int>::type = 0>
void emitExtractHalfVector(
x86::Emitter* a,
x86::Xmm half,
x86::Ymm vec,
int idx) {
a->vextracti32x4(half, vec, idx);
}
template <
inst_set_t instSet,
typename T,
typename std::enable_if<instSet == inst_set_t::avx2, int>::type = 0>
void emitExtractHalfVector(
x86::Emitter* a,
x86::Xmm half,
x86::Ymm vec,
int idx) {
a->vextracti128(half, vec, idx);
}
/**
* @brief Create instruction sequence to generate 8-bit 1s
* @tparam T Register type of destination, e.g., x86::Ymm or x86::Zmm
*
* @param dest Once the instruction sequence is executed,
* dest[0:7] will have 0x01, dest[8:15]
* will have 0x01 and so on
*/
template <
typename T,
typename std::enable_if<std::is_same<T, x86::Ymm>::value, int>::type = 0>
void gen8BitVectorOne(x86::Emitter* a, T dest) {
a->vpcmpeqw(dest, dest, dest);
a->vpabsb(dest, dest);
}
template <
typename T,
typename std::enable_if<std::is_same<T, x86::Zmm>::value, int>::type = 0>
void gen8BitVectorOne(x86::Emitter* a, T dest) {
a->vpternlogd(dest, dest, dest, 0xff);
a->vpabsb(dest, dest);
}
/**
* @brief Generates instruction sequence to compute s32 += U8 * I8
* @tparam T Register type of destination, e.g., x86::Ymm or x86::Zmm
*
* @param cReg contains result
*
*/
template <
inst_set_t INST_SET,
typename std::enable_if<
INST_SET == inst_set_t::avx2 || INST_SET == inst_set_t::avx512,
int>::type = 0>
void genU8I8S32FMA(
x86::Emitter* a,
typename simd_info<INST_SET>::vec_reg_t aReg,
typename simd_info<INST_SET>::vec_reg_t bReg,
typename simd_info<INST_SET>::vec_reg_t cReg,
typename simd_info<INST_SET>::vec_reg_t oneReg16Bit,
typename simd_info<INST_SET>::vec_reg_t tmpReg) {
a->vpmaddubsw(tmpReg, aReg, bReg);
a->vpmaddwd(tmpReg, oneReg16Bit, tmpReg);
a->vpaddd(cReg, tmpReg, cReg);
}
template <
inst_set_t INST_SET,
typename std::enable_if<INST_SET == inst_set_t::avx512_vnni, int>::type = 0>
void genU8I8S32FMA(
x86::Emitter* a,
typename simd_info<INST_SET>::vec_reg_t aReg,
typename simd_info<INST_SET>::vec_reg_t bReg,
typename simd_info<INST_SET>::vec_reg_t cReg,
typename simd_info<INST_SET>::vec_reg_t /*oneReg16Bit*/,
typename simd_info<INST_SET>::vec_reg_t /*tmpReg*/) {
a->vpdpbusd(cReg, aReg, bReg);
}
/**
* @brief Add 4 consecutive numbers of type uint8
* and emit their sum as 32-bit numbers.
* i.e., dest[0:31] contains
* src[0:7] + src[8:15] + src[16:23] + src[24:31]
* @tparam T Register type of destination, e.g., x86::Ymm or x86::Zmm
*
* @param dest contains result
*
*/
template <
inst_set_t INST_SET,
typename std::enable_if<
INST_SET == inst_set_t::avx2 || INST_SET == inst_set_t::avx512,
int>::type = 0>
void genU8Sum4(
x86::Emitter* a,
typename simd_info<INST_SET>::vec_reg_t src,
typename simd_info<INST_SET>::vec_reg_t dest,
typename simd_info<INST_SET>::vec_reg_t oneReg16Bit,
typename simd_info<INST_SET>::vec_reg_t tmpReg) {
gen8BitVectorOne(a, tmpReg);
a->vpmaddubsw(tmpReg, src, tmpReg);
a->vpmaddwd(tmpReg, tmpReg, oneReg16Bit);
a->vpaddd(dest, tmpReg, dest);
/*a->vxorps(tmpReg, tmpReg, tmpReg);*/
/*a->vmpsadbw(tmpReg, src, tmpReg, static_cast<asmjit::Imm>(0));*/
/*a->vpermilps(tmpReg, tmpReg, static_cast<asmjit::Imm>(4));*/
/*a->vpmovzxwd(tmpReg, tmpReg.half());*/
/*a->vpaddd(dest, tmpReg, dest);*/
}
template <
inst_set_t INST_SET,
typename std::enable_if<INST_SET == inst_set_t::avx512_vnni, int>::type = 0>
void genU8Sum4(
x86::Emitter* a,
typename simd_info<INST_SET>::vec_reg_t src,
typename simd_info<INST_SET>::vec_reg_t dest,
typename simd_info<INST_SET>::vec_reg_t /*oneReg16Bit*/,
typename simd_info<INST_SET>::vec_reg_t tmpReg) {
gen8BitVectorOne(a, tmpReg);
a->vpdpbusd(dest, src, tmpReg);
}
/**
* @brief Add 8 consecutive numbers of type uint8
* and emit their sum as 16-bit numbers.
* i.e., dest[0:15] contains
* src[0:7] + src[8:15] + src[16:23] + src[24:31]
* src[32:39] + src[40:47] + src[48:55] + src[56:63]
*
* and
*
* dest[64:79] contains
* src[64:71] + src[71:79] + src[80:87] + src[88:95]
* src[96:103] + src[104:111] + src[112:119] + src[120:127]
*
* so on
*
* @tparam T Register type of destination, e.g., x86::Ymm or x86::Zmm
*
* @param dest contains result
*
*/
template <typename T>
void genU8Sum8(x86::Emitter* a, T src, T dest, T tmpReg) {
a->vxorps(tmpReg, tmpReg, tmpReg);
a->vpsadbw(tmpReg, src, tmpReg);
a->vpaddd(dest, tmpReg, dest);
}
/**
* @brief Broadcast lower 8-bits of src to destination vector
* register.
*/
template <typename T>
void broadcast8Bit(x86::Emitter* a, x86::Gp src, T dest) {
// move src to dest
auto xmm = dest.xmm();
a->movq(xmm, src);
a->vpbroadcastb(dest, xmm);
}
} // namespace fbgemm