-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathControlFlowVectorization.cpp
More file actions
181 lines (162 loc) · 8.58 KB
/
ControlFlowVectorization.cpp
File metadata and controls
181 lines (162 loc) · 8.58 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
#include <iostream>
#include <memory>
#include <random>
#include "benchmark/benchmark.h"
#define ITERATIONS 100000
template <typename T> using ControlFlowLoopFunc = void (*)(T *, unsigned);
// Define conditional loop with given branch taken frequency.
#define DEF_COND_LOOP(name, branch_every_N) \
template <typename T> \
__attribute__((noinline)) static void run_##name##_autovec(T *A, \
unsigned N) { \
for (unsigned i = 0; i < N; i++) { \
if (i % branch_every_N == 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 % branch_every_N == 0) { \
A[i] = A[i] + 1; \
} \
} \
}
// Define conditional loop with taken frequency by data equals to marker.
#define DEF_COND_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_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_novec(T *A, unsigned N) {
_Pragma("clang loop vectorize(disable) interleave(disable)")
for (unsigned i = 0; i < N; i++) {
A[i] = A[i] + 1;
}
}
DEF_COND_LOOP(cond_every_1_iter, 1)
DEF_COND_LOOP(cond_every_2_iter, 2)
DEF_COND_LOOP(cond_every_4_iter, 4)
DEF_COND_LOOP(cond_every_8_iter, 8)
DEF_COND_LOOP(cond_every_16_iter, 16)
DEF_COND_LOOP(cond_every_32_iter, 32)
DEF_COND_LOOP(cond_every_64_iter, 64)
// Branch taken by value
DEF_COND_VALUE_LOOP(cond_by_value, 100)
// 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_control_flow_loop_autovec(benchmark::State &state,
ControlFlowLoopFunc<T> VecFn,
ControlFlowLoopFunc<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_control_flow_loop_novec(benchmark::State &state,
ControlFlowLoopFunc<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_CONTROL_FLOW_VEC_CASE(name, ty) \
void BENCHMARK_##name##_autovec_##ty##_(benchmark::State &state) { \
benchmark_control_flow_loop_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_control_flow_loop_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_autovec_##ty##_(benchmark::State &state) { \
benchmark_control_flow_loop_autovec<ty>(state, run_uncond_autovec, \
run_uncond_novec); \
} \
BENCHMARK(BENCHMARK_uncond_autovec_##ty##_)->Unit(benchmark::kNanosecond); \
\
void BENCHMARK_uncond_novec_##ty##_(benchmark::State &state) { \
benchmark_control_flow_loop_novec<ty>(state, run_uncond_novec); \
} \
BENCHMARK(BENCHMARK_uncond_novec_##ty##_)->Unit(benchmark::kNanosecond);
// Add benchmarks for all branch frequency variants.
#define ADD_CONTROL_FLOW_VEC_BENCHMARKS(ty) \
BENCHMARK_UNCOND_CASE(ty) \
BENCHMARK_CONTROL_FLOW_VEC_CASE(cond_every_1_iter, ty) \
BENCHMARK_CONTROL_FLOW_VEC_CASE(cond_every_2_iter, ty) \
BENCHMARK_CONTROL_FLOW_VEC_CASE(cond_every_4_iter, ty) \
BENCHMARK_CONTROL_FLOW_VEC_CASE(cond_every_8_iter, ty) \
BENCHMARK_CONTROL_FLOW_VEC_CASE(cond_every_16_iter, ty) \
BENCHMARK_CONTROL_FLOW_VEC_CASE(cond_every_32_iter, ty) \
BENCHMARK_CONTROL_FLOW_VEC_CASE(cond_every_64_iter, ty) \
BENCHMARK_CONTROL_FLOW_VEC_CASE(cond_by_value, ty)
ADD_CONTROL_FLOW_VEC_BENCHMARKS(int64_t)
ADD_CONTROL_FLOW_VEC_BENCHMARKS(int32_t)
ADD_CONTROL_FLOW_VEC_BENCHMARKS(int16_t)