-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathControlFlowVectorization.cpp
More file actions
188 lines (168 loc) · 8.66 KB
/
ControlFlowVectorization.cpp
File metadata and controls
188 lines (168 loc) · 8.66 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
#include <iostream>
#include <memory>
#include <random>
#include "benchmark/benchmark.h"
#define ITERATIONS 100000
template <typename T> using CFVFunc = void (*)(T *, unsigned);
// Define conditional increment loop with given stride.
#define DEF_COND_INC_LOOP(name, stride) \
template <typename T> \
__attribute__((noinline)) static void run_##name##_autovec(T *A, \
unsigned N) { \
for (unsigned i = 0; i < N; i++) { \
if (i % stride == 0) { \
A[i] = A[i] + 1; \
} \
} \
} \
template <typename T> \
__attribute__((noinline)) static void run_##name##_novec(T *A, unsigned N) { \
_Pragma("clang loop vectorize(disable) interleave(disable)") \
for (unsigned i = 0; i < N; i++) { \
if (i % stride == 0) { \
A[i] = A[i] + 1; \
} \
} \
}
// Define conditional increment by value loop.
#define DEF_COND_INC_VALUE_LOOP(name, marker) \
template <typename T> \
__attribute__((noinline)) static void run_##name##_autovec(T *A, \
unsigned N) { \
for (unsigned i = 0; i < N; i++) { \
if (A[i] == marker) { \
A[i] = A[i] + 1; \
} \
} \
} \
template <typename T> \
__attribute__((noinline)) static void run_##name##_novec(T *A, unsigned N) { \
_Pragma("clang loop vectorize(disable) interleave(disable)") \
for (unsigned i = 0; i < N; i++) { \
if (A[i] == marker) { \
A[i] = A[i] + 1; \
} \
} \
}
// Define unconditional increment loop.
template <typename T>
__attribute__((noinline)) static void run_uncond_inc_autovec(T *A, unsigned N) {
for (unsigned i = 0; i < N; i++) {
A[i] = A[i] + 1;
}
}
template <typename T>
__attribute__((noinline)) static void run_uncond_inc_novec(T *A, unsigned N) {
_Pragma("clang loop vectorize(disable) interleave(disable)")
for (unsigned i = 0; i < N; i++) {
A[i] = A[i] + 1;
}
}
// Define loops with different strides.
// stride=2: 50% active lanes
// stride=4: 25% active lanes
// stride=8: 12.5% active lanes
// stride=16: 6.25% active lanes
// stride=32: 3.125% active lanes
// stride=64: 1.5625% active lanes
// stride=128: 0.78% active lanes
DEF_COND_INC_LOOP(cond_inc_stride_2, 2)
DEF_COND_INC_LOOP(cond_inc_stride_4, 4)
DEF_COND_INC_LOOP(cond_inc_stride_8, 8)
DEF_COND_INC_LOOP(cond_inc_stride_16, 16)
DEF_COND_INC_LOOP(cond_inc_stride_32, 32)
DEF_COND_INC_LOOP(cond_inc_stride_64, 64)
DEF_COND_INC_LOOP(cond_inc_stride_128, 128)
// Conditional increment by value (sparse condition).
DEF_COND_INC_VALUE_LOOP(cond_inc_by_value, 42)
// Initialize array with random numbers.
template <typename T> static void init_data(T *A) {
std::uniform_int_distribution<T> dist(0, 100);
std::mt19937 rng(12345);
for (unsigned i = 0; i < ITERATIONS; i++) {
A[i] = dist(rng);
}
}
// Benchmark vectorized version.
template <typename T>
static void __attribute__((always_inline))
benchmark_cfv_autovec(benchmark::State &state, CFVFunc<T> VecFn,
CFVFunc<T> NoVecFn) {
std::unique_ptr<T[]> A(new T[ITERATIONS]);
std::unique_ptr<T[]> A_vec(new T[ITERATIONS]);
std::unique_ptr<T[]> A_novec(new T[ITERATIONS]);
init_data(&A[0]);
#ifdef BENCH_AND_VERIFY
// Verify the vectorized and scalar versions produce the same results.
{
std::copy(&A[0], &A[0] + ITERATIONS, &A_vec[0]);
std::copy(&A[0], &A[0] + ITERATIONS, &A_novec[0]);
VecFn(&A_vec[0], ITERATIONS);
NoVecFn(&A_novec[0], ITERATIONS);
for (unsigned i = 0; i < ITERATIONS; i++) {
if (A_vec[i] != A_novec[i]) {
std::cerr << "ERROR: vectorization result different at index " << i
<< "; " << A_vec[i] << " != " << A_novec[i] << "\n";
exit(1);
}
}
}
#endif
for (auto _ : state) {
std::copy(&A[0], &A[0] + ITERATIONS, &A_vec[0]);
VecFn(&A_vec[0], ITERATIONS);
benchmark::DoNotOptimize(A_vec);
benchmark::ClobberMemory();
}
}
// Benchmark version with vectorization disabled.
template <typename T>
static void __attribute__((always_inline))
benchmark_cfv_novec(benchmark::State &state, CFVFunc<T> NoVecFn) {
std::unique_ptr<T[]> A(new T[ITERATIONS]);
std::unique_ptr<T[]> A_work(new T[ITERATIONS]);
init_data(&A[0]);
for (auto _ : state) {
std::copy(&A[0], &A[0] + ITERATIONS, &A_work[0]);
NoVecFn(&A_work[0], ITERATIONS);
benchmark::DoNotOptimize(A_work);
benchmark::ClobberMemory();
}
}
#define BENCHMARK_CFV_CASE(name, ty) \
void BENCHMARK_##name##_autovec_##ty##_(benchmark::State &state) { \
benchmark_cfv_autovec<ty>(state, run_##name##_autovec, run_##name##_novec);\
} \
BENCHMARK(BENCHMARK_##name##_autovec_##ty##_)->Unit(benchmark::kNanosecond); \
\
void BENCHMARK_##name##_novec_##ty##_(benchmark::State &state) { \
benchmark_cfv_novec<ty>(state, run_##name##_novec); \
} \
BENCHMARK(BENCHMARK_##name##_novec_##ty##_)->Unit(benchmark::kNanosecond);
// Unconditional increment benchmark.
#define BENCHMARK_UNCOND_CASE(ty) \
void BENCHMARK_uncond_inc_autovec_##ty##_(benchmark::State &state) { \
benchmark_cfv_autovec<ty>(state, run_uncond_inc_autovec, \
run_uncond_inc_novec); \
} \
BENCHMARK(BENCHMARK_uncond_inc_autovec_##ty##_) \
->Unit(benchmark::kNanosecond); \
\
void BENCHMARK_uncond_inc_novec_##ty##_(benchmark::State &state) { \
benchmark_cfv_novec<ty>(state, run_uncond_inc_novec); \
} \
BENCHMARK(BENCHMARK_uncond_inc_novec_##ty##_)->Unit(benchmark::kNanosecond);
// Add benchmarks for all variants.
#define ADD_CFV_BENCHMARKS(ty) \
BENCHMARK_UNCOND_CASE(ty) \
BENCHMARK_CFV_CASE(cond_inc_stride_2, ty) \
BENCHMARK_CFV_CASE(cond_inc_stride_4, ty) \
BENCHMARK_CFV_CASE(cond_inc_stride_8, ty) \
BENCHMARK_CFV_CASE(cond_inc_stride_16, ty) \
BENCHMARK_CFV_CASE(cond_inc_stride_32, ty) \
BENCHMARK_CFV_CASE(cond_inc_stride_64, ty) \
BENCHMARK_CFV_CASE(cond_inc_stride_128, ty) \
BENCHMARK_CFV_CASE(cond_inc_by_value, ty)
ADD_CFV_BENCHMARKS(int64_t)
ADD_CFV_BENCHMARKS(int32_t)
ADD_CFV_BENCHMARKS(int16_t)