[lld] r299748 - [Core] Fix parallel_for for Linux
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 7 01:11:28 PDT 2017
Author: jhenderson
Date: Fri Apr 7 03:11:28 2017
New Revision: 299748
URL: http://llvm.org/viewvc/llvm-project?rev=299748&view=rev
Log:
[Core] Fix parallel_for for Linux
r299635 exposed a latent bug in the Linux implementation of parallel_for, which
resulted in it calling the function outside of the range requested, resulting
later in a segmentation fault. This change fixes this issue and adds a unit test.
Modified:
lld/trunk/include/lld/Core/Parallel.h
lld/trunk/unittests/CoreTests/ParallelTest.cpp
Modified: lld/trunk/include/lld/Core/Parallel.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/Parallel.h?rev=299748&r1=299747&r2=299748&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/Parallel.h (original)
+++ lld/trunk/include/lld/Core/Parallel.h Fri Apr 7 03:11:28 2017
@@ -318,12 +318,11 @@ void parallel_for(IndexTy Begin, IndexTy
TaskGroup Tg;
IndexTy I = Begin;
- for (; I < End; I += TaskSize) {
+ for (; I + TaskSize < End; I += TaskSize) {
Tg.spawn([=, &Fn] {
for (IndexTy J = I, E = I + TaskSize; J != E; ++J)
Fn(J);
});
- Begin += TaskSize;
}
Tg.spawn([=, &Fn] {
for (IndexTy J = I; J < End; ++J)
Modified: lld/trunk/unittests/CoreTests/ParallelTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/CoreTests/ParallelTest.cpp?rev=299748&r1=299747&r2=299748&view=diff
==============================================================================
--- lld/trunk/unittests/CoreTests/ParallelTest.cpp (original)
+++ lld/trunk/unittests/CoreTests/ParallelTest.cpp Fri Apr 7 03:11:28 2017
@@ -29,3 +29,18 @@ TEST(Parallel, sort) {
lld::parallel_sort(std::begin(array), std::end(array));
ASSERT_TRUE(std::is_sorted(std::begin(array), std::end(array)));
}
+
+TEST(Parallel, parallel_for) {
+ // We need to test the case with a TaskSize > 1. We are white-box testing
+ // here. The TaskSize is calculated as (End - Begin) / 1024 at the time of
+ // writing.
+ uint32_t range[2050];
+ std::fill(range, range + 2050, 1);
+ lld::parallel_for(0, 2049, [&range](size_t I) { ++range[I]; });
+
+ uint32_t expected[2049];
+ std::fill(expected, expected + 2049, 2);
+ ASSERT_TRUE(std::equal(range, range + 2049, expected));
+ // Check that we don't write past the end of the requested range.
+ ASSERT_EQ(range[2049], 1);
+}
More information about the llvm-commits
mailing list