Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MicroBenchmarks/LoopVectorization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ llvm_test_executable(LoopVectorizationBenchmarks
main.cpp
MathFunctions.cpp
RuntimeChecks.cpp
SmallLoopTripCount.cpp
VectorOperations.cpp
EarlyExit.cpp
)
Expand Down
102 changes: 102 additions & 0 deletions MicroBenchmarks/LoopVectorization/SmallLoopTripCount.cpp
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);
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Author

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!

}

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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to comment why this is needed

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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");