Skip to content

[UnitTests] Add initial set of dedicated early-exit unit tests. #264

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions SingleSource/UnitTests/Vectorizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

# Enable matrix types extension tests for compilers supporting -fenable-matrix.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the comment doesn't match the code below?

check_c_compiler_flag("-mllvm -enable-early-exit-vectorization" COMPILER_HAS_EE_VEC_FLAG)
if (COMPILER_HAS_EE_VEC_FLAG)
set_property(SOURCE early-exit.cpp PROPERTY COMPILE_FLAGS "-mllvm -enable-early-exit-vectorization")
else()
list(REMOVE_ITEM Source early-exit.cpp)
endif()

llvm_singlesource()
set_property(TARGET runtime-checks PROPERTY CXX_STANDARD 17)

Expand Down
100 changes: 100 additions & 0 deletions SingleSource/UnitTests/Vectorizer/early-exit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <stdint.h>

#define N 512

template <typename Ty>
using TestFnTy = std::function<unsigned(std::function<void(Ty *)>)>;

template <typename Ty>
static void checkVectorFunction(TestFnTy<Ty> ScalarFn, TestFnTy<Ty> VectorFn,
TestFnTy<Ty> InterleavedFn, const char *Name) {
std::cout << "Checking " << Name << "\n";

std::array Tests = {std::make_pair(VectorFn, "autovec"),
std::make_pair(InterleavedFn, "interleave-forced")};

// Check finding the target element at all indices between 0 and 512.
for (unsigned IdxToFind = 0; IdxToFind < 512; ++IdxToFind) {
// Labmda to initialize all array elements to one, except the one to look
// for to zero.
auto Init1 = [IdxToFind](int *Arr) {
std::fill_n(Arr, N, 1);
if (IdxToFind < N)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a risk that a future compiler optimisation basically figures out the answer based on this init code (after inlining) and just optimises ScalarFn to return a constant before we even reach the loop vectoriser? It might be better to have Init1 and Init2 as real functions marked as noinline.

Arr[IdxToFind] = 0;
};

// Labmda to initialize all array elements to one, except the one to look
// for and the IdxToFind + 3 to zero.
auto Init2 = [IdxToFind](int *Arr) {
std::fill_n(Arr, N, 1);
if (IdxToFind < N)
Arr[IdxToFind] = 0;
if (IdxToFind + 3 < N)
Arr[IdxToFind + 3] = 0;
};

auto Reference1 = ScalarFn(Init1);
auto Reference2 = ScalarFn(Init2);
// Run vector functions and check against the scalar result.
for (const auto &[Fn, Name] : Tests) {
auto ToCheck1 = Fn(Init1);
if (Reference1 != ToCheck1) {
std::cerr << "Miscompare for " << Name << ": " << Reference1
<< " != " << ToCheck1 << "\n";
exit(1);
}
auto ToCheck2 = Fn(Init2);
if (Reference2 != ToCheck2) {
std::cerr << "Miscompare for " << Name << ": " << Reference1
<< " != " << ToCheck2 << "\n";
exit(1);
}
}
}
}

/// Define 3 test functions
/// * one with vectorization and interleaving disabled,
/// * one with auto-vectorization w/o any pargmas
/// * one where VF is picked automatically and interleaving is forced.
#define DEFINE_SCALAR_AND_VECTOR_EARLY_EXIT(Init, Src, Loop) \
auto ScalarFn = [](std::function<void(int *)> II) -> unsigned { \
Init II(Src); \
Copy link
Contributor

Choose a reason for hiding this comment

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

This is pretty confusing as it looks like constructing a variable. Can you split this out over different lines so it's clearer, i.e.

  InitSrc;
  II(Src);

_Pragma("clang loop vectorize(disable) interleave_count(1)") Loop \
}; \
auto VectorFn = [](std::function<void(int *)> II) -> unsigned { \
Init II(Src); \
_Pragma("clang loop vectorize(enable)") Loop \
}; \
auto InterleavedFn = [](std::function<void(int *)> II) -> unsigned { \
Init II(Src); \
_Pragma("clang loop vectorize(enable) interleave_count(4)") Loop \
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this PR cannot be merged until the pragma has handled correctly.

};

int main(void) {
{
DEFINE_SCALAR_AND_VECTOR_EARLY_EXIT(
int A[N];, A, for (unsigned I = 0; I < N; I++) {
if (A[I] == 0)
return I;
} return -1;);
checkVectorFunction<int>(ScalarFn, VectorFn, InterleavedFn,
"early_exit_find_step_1");
}

{
DEFINE_SCALAR_AND_VECTOR_EARLY_EXIT(
int A[N]; unsigned J = 2;, A, for (unsigned I = 0; I < N; I++) {
if (A[I] == 0)
return J;
J += 3;
} return -1;);
checkVectorFunction<int>(ScalarFn, VectorFn, InterleavedFn,
"early_exit_find_step_3");
}
return 0;
}