-
Notifications
You must be signed in to change notification settings - Fork 417
Add MicroBenchmark for Small Trip Count Loop vectorization #404
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // This program tests the performance impact of vectorization in loops with | ||
| // small trip counts. These cases exercise the LoopVectorize path that accepts | ||
| // trip counts one larger than the vectorization factor. | ||
|
|
||
| #include <array> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <limits> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| #define NOINLINE __attribute__((noinline)) | ||
| #define LOOP_VECTORIZE_ENABLE \ | ||
| _Pragma("clang loop vectorize(enable) unroll(disable)") | ||
| #define LOOP_VECTORIZE_DISABLE \ | ||
| _Pragma("clang loop vectorize(disable) interleave(disable) unroll(disable)") | ||
| #define LOOP_INTERLEAVE_COUNT_2 \ | ||
| _Pragma("clang loop vectorize(enable) interleave_count(2) unroll(disable)") | ||
|
|
||
| static uint64_t g_small_loop_trip_count_sum = 0; | ||
|
|
||
| template <typename Ty> | ||
| NOINLINE void loopTc5Vector(const Ty *__restrict A, Ty *__restrict B) { | ||
| LOOP_VECTORIZE_ENABLE | ||
| for (uint64_t I = 0; I != 5; ++I) | ||
| B[I] = A[I] + static_cast<Ty>(1); | ||
| } | ||
|
|
||
| template <typename Ty> | ||
| NOINLINE void loopTc5Scalar(const Ty *__restrict A, Ty *__restrict B) { | ||
| LOOP_VECTORIZE_DISABLE | ||
| for (uint64_t I = 0; I != 5; ++I) | ||
| B[I] = A[I] + static_cast<Ty>(1); | ||
| } | ||
|
|
||
| NOINLINE void loopTc5I64InterleaveCount2Vector(const uint64_t *__restrict A, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to not use the templated version for this one as well?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No there isn't, I have made it consistent now. |
||
| uint64_t *__restrict B) { | ||
| LOOP_INTERLEAVE_COUNT_2 | ||
| for (uint64_t I = 0; I != 5; ++I) | ||
| B[I] = A[I] + 1; | ||
| } | ||
|
|
||
| template <typename Ty> using KernelFn = void (*)(const Ty *, Ty *); | ||
|
|
||
| template <typename Ty> static void initData(std::array<Ty, 16> &A) { | ||
| for (size_t I = 0; I != A.size(); ++I) | ||
| A[I] = static_cast<Ty>(0x0102030405060708ULL + I); | ||
| } | ||
|
|
||
| template <typename Ty> static uint64_t checksum(const std::array<Ty, 16> &A) { | ||
| uint64_t Sum = 0; | ||
| for (size_t I = 0; I != 5; ++I) { | ||
| auto Value = static_cast<uint64_t>(A[I]); | ||
| for (size_t Byte = 0; Byte != sizeof(Ty); ++Byte) { | ||
| Sum = Sum * 131 + (Value & std::numeric_limits<uint8_t>::max()); | ||
| Value >>= 8; | ||
| } | ||
| } | ||
| return Sum; | ||
| } | ||
|
|
||
| template <typename Ty> | ||
| static void runBenchForSmallLoopTripCount(benchmark::State &State, | ||
| KernelFn<Ty> Fn) { | ||
| std::array<Ty, 16> A; | ||
| std::array<Ty, 16> B = {}; | ||
| initData(A); | ||
|
|
||
| for (auto _ : State) { | ||
| benchmark::DoNotOptimize(A.data()); | ||
| benchmark::DoNotOptimize(B.data()); | ||
| Fn(A.data(), B.data()); | ||
| benchmark::ClobberMemory(); | ||
| } | ||
|
|
||
| g_small_loop_trip_count_sum ^= checksum(B); | ||
| benchmark::DoNotOptimize(g_small_loop_trip_count_sum); | ||
| State.SetItemsProcessed(State.iterations() * 5); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to comment why this is needed
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not. I missed this when first reviewing the codex generated benchmark. I've removed it. |
||
| } | ||
|
|
||
| template <typename Ty> void benchTc5Vector(benchmark::State &State) { | ||
| runBenchForSmallLoopTripCount<Ty>(State, loopTc5Vector<Ty>); | ||
| } | ||
|
|
||
| template <typename Ty> void benchTc5Scalar(benchmark::State &State) { | ||
| runBenchForSmallLoopTripCount<Ty>(State, loopTc5Scalar<Ty>); | ||
| } | ||
|
|
||
| void benchTc5I64InterleaveCount2Vector(benchmark::State &State) { | ||
| runBenchForSmallLoopTripCount<uint64_t>(State, | ||
| loopTc5I64InterleaveCount2Vector); | ||
| } | ||
|
|
||
| BENCHMARK_TEMPLATE(benchTc5Vector, uint8_t)->Name("tc5/i8/vector"); | ||
| BENCHMARK_TEMPLATE(benchTc5Scalar, uint8_t)->Name("tc5/i8/scalar"); | ||
| BENCHMARK_TEMPLATE(benchTc5Vector, uint16_t)->Name("tc5/i16/vector"); | ||
| BENCHMARK_TEMPLATE(benchTc5Scalar, uint16_t)->Name("tc5/i16/scalar"); | ||
| BENCHMARK_TEMPLATE(benchTc5Vector, uint32_t)->Name("tc5/i32/vector"); | ||
| BENCHMARK_TEMPLATE(benchTc5Scalar, uint32_t)->Name("tc5/i32/scalar"); | ||
| BENCHMARK_TEMPLATE(benchTc5Vector, uint64_t)->Name("tc5/i64/vector"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the potential worst case would be i64 with TC =3, could you also cover this?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added cases for all data types for TC=3 for full coverage. |
||
| BENCHMARK_TEMPLATE(benchTc5Scalar, uint64_t)->Name("tc5/i64/scalar"); | ||
| BENCHMARK(benchTc5I64InterleaveCount2Vector)->Name("tc5/i64/ic2/vector"); | ||
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.
This is a case where there is basically no overhead for the vector code compared to the scalar code.
Would be good to also include cases where there is some overhead from the vector code compared to scalar, e.g. some scalarization
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.
I have added an example that has scalarization in the loop, if this is not what you meant please let me know!