-
Notifications
You must be signed in to change notification settings - Fork 408
[MicroBenchmarks] Add benchmark for control-flow-vectorization. #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ElvisWang123
wants to merge
3
commits into
llvm:main
Choose a base branch
from
ElvisWang123:control-flow-vectorization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
MicroBenchmarks/LoopVectorization/ControlFlowVectorization.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the meaning of this benchmark ? Just track current state of cf vectorization of novec and autovec or help to identify better LMUL to vectorize the loop ? If latter, it does make sense to add similar functions with forced vectorization for default LMUL and specified LMULs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, this benchmark serves as a test suite for other targets to measure the performance impact of enabling control-flow vectorization.
I've updated the PR description to make it more clear.