You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C++][CI] a crossbow job with MinRelSize enabled #31132
Weston Pace / @westonpace:
Ok, so I was finally able to track this down. Fortunately (unfortunately?) it is not really a compiler bug (or maybe it is, I'm not sure). At the very least, I think we can avoid it.
level_comparison.cc is compiled with -msse4.2.
level_comparison_avx2.cc is compiled with -mavx2
This is expected and the functions they generate are housed in separate namespaces so they don't get confused. However, both functions rely on the function arrow::internal::FirstTimeBitmapWriter::AppendWord. The function is not templated but it is defined in the header file (and is not marked inline). I'm not really sure how we aren't getting a duplicate symbol error but some reading suggests it is implicitly inlined at link time.
In the object file (libparquet.a), there are two identical symbols named __ZN5arrow8internal21FirstTimeBitmapWriter10AppendWordEyx. One of them has SHLX and one of them has SHL. This disassembly of the SHLX version matches exactly the disassembly in the stack trace that @jonkeane posted in the PR. The two calling functions are (parquet::internal::standard::DefLevelsBatchToBitmap and parquet::internal::bmi2::DefLevelsBatchToBitmap.
So I think, the -O3 version is inlining the functions. The -Os version is not (-Os seems to discourage inlining in general). The linker is then faced with two identical symbols and just picks one (again, trying to optimize for size). It just so happens the version it picked was the one with SHLX.
So, as a test, we can try splitting the implementation part of bitmap_writer.h into bitmap_writer.cc (at least for FirstTimeBitmapWriter). The .cc file should then only be compiled once (with sse4.2). However, it's very possible we are just hitting the tip of the iceberg here, as any header file linked in by these avx2 compiled versions could be a ticking time bomb.
// SumArray must be parameterized with the SIMD level since it's called both from
// translation units with and without vectorization. Normally it gets inlined but
// if not, without the parameter, we'll have multiple definitions of the same
// symbol and we'll get unexpected results.
(ARROW-13382). I wonder if we should reconsider the plan of vectorizing kernels by rebuilding the same source multiple times given this potential pitfall.
Weston Pace / @westonpace:
We can perhaps add a static check that reports symbols outside the appropriate namespace. We might need some configurable suppression. For example, level_conversion_bmi2.cc.o would report:
0000000000000000 W arrow::util::ArrowLogBase& arrow::util::ArrowLogBase::operator<< <char [51]>(char const (&) [51])
0000000000000000 W std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > arrow::util::StringBuilder<char const (&) [33]>(char const (&) [33])
0000000000000000 W void arrow::util::StringBuilderRecursive<char const (&) [33]>(std::ostream&, char const (&) [33])
0000000000000000 W arrow::util::detail::StringStreamWrapper::stream()
0000000000000000 W arrow::util::Voidify::operator&(arrow::util::ArrowLogBase&)
0000000000000000 W arrow::util::Voidify::Voidify()
0000000000000000 W arrow::bit_util::BytesForBits(long)
0000000000000000 W arrow::internal::FirstTimeBitmapWriter::AppendWord(unsigned long, long)
0000000000000000 W arrow::internal::FirstTimeBitmapWriter::Finish()
0000000000000000 W arrow::internal::FirstTimeBitmapWriter::FirstTimeBitmapWriter(unsigned char*, long, long)
0000000000000000 W parquet::ParquetException::ParquetException<char const (&) [33]>(char const (&) [33])
0000000000000000 W parquet::ParquetException::~ParquetException()
0000000000000000 W parquet::ParquetException::~ParquetException()
0000000000000000 W arrow::internal::FirstTimeBitmapWriter::position() const
0000000000000000 W parquet::ParquetException::what() const
0000000000000000 W std::exception::exception()
0000000000000000 W char const (&std::forward<char const (&) [33]>(std::remove_reference<char const (&) [33]>::type&)) [33]
level_comparison_avx.cc.o looks to be in better shape:
0000000000000000 W short const& std::max<short>(short const&, short const&)
0000000000000000 W short const& std::min<short>(short const&, short const&)
But yes, if we have a better solution for this problem it might be safer.
Antoine Pitrou / @pitrou:
Wow, thanks for the diagnosis @westonpace.
So, it turns out that our method for compiling multiple versions of code is violating the one-definition-rule for any inline function or method used in the caller code.
This way we would avoid enabling the particular instruction set on code inlined from other headers. Of course, perhaps that's not actually desirable...
Antoine Pitrou / @pitrou:
In any case, this is probably too involved a change for 8.0.0, so the 8.0.0 fix would simply to disable SIMD optimizations for Homebrew?
Weston Pace / @westonpace:
That seems like an good solution to me. I had no idea it was possible.
If we want the other headers to be included then we already have a bit of a solution demonstrated in level_conversion_inc.h. In the common header file you require some kind of target namespace to be defined.
The comment at #12928 (comment) has a good explanation of what's going on and following that there are a few possible fixes (though none of them were fully implemented or decided
Antoine Pitrou / @pitrou: @kou We can do that for the specific symptoms here. However, a more general solution will have to be found since other files have the same problem: compiling SIMD-specific code which calls into other routines.
Ben Kietzman / @bkietz: @pitrou The most robust solution I can think of is to avoid linking between objects with differing instruction sets altogether. We'd have something like
That library would be acquired with dlopen(path, RTLD_LOCAL)/LoadLibrary(path) which would guarantee that any functions like FirstTimeBitmapWriter::* which might have been recompiled with illegal instructions are not available outside libarrow_compute_avx2.so.
Ben Kietzman / @bkietz:
IIUC, we'll still need to pass -mavx2 so that we can include immintrin.h so the attribute described in the ARROW_SPECIALIZED_SIMD_TARGET approach would need to be attached to the {}non{}-SIMD functions to ensure that they're compiled with no special instructions
... or I suppose we could try to declare all the intrinsics manually at function scope
Ben Kietzman / @bkietz:
On further investigation, we can include immintrin.h with or without -mavx2 and clang at least will not complain unless the intrinsics are referenced, so
compiles and runs happily without any special compilation flags. Using an attribute like this seems viable provided we can be certain that the modified target isn't transitively applied to functions which might be invoked for the first time inside a SIMD enabled function
Jonathan Keane / @jonkeane:
I have no updates beyond what's discussed above: there are a few approaches, none of them ideal, we need someone to champion this (or risk the homebrew maintainers turning off optimizations on us)
Raúl Cumplido / @raulcd:
This was a blocker for the last release and is still a blocker for the 10.0.0 release. @jonkeane do you know if there has been any move?
Jonathan Keane / @jonkeane:
I thought that @kou was going to take a look at this (or at least the underlying multiple SIMD instruction ordering issue that causes the failures...)
The only update I have is that I continue to run into the segfault in CI for downstream projects I'm working on, so it continues to be an issue for pre-built libarrow on machines like github's macos runners.
Kouhei Sutou / @kou:
I propose one more approach for the proposed solution 1.:
How about always enabling inline optimization for SIMD optimized compile units (level_conversion_bmi2.cc) even when an user specifies -DCMAKE_BUILD_TYPE=MinSizeRel?
It may increases binary size but it may be better that SIMD related code prioritizes performance than binary size.
We don't need to write manual template/\_\_attribute\_\_((always\_inline)) s with this approach.
Activity
asfimport commentedon Mar 2, 2022
Jonathan Keane / @jonkeane:
The pull request linked has the starts of this — but there's still an unidentified segfault in one of the tests
asfimport commentedon Apr 17, 2022
Weston Pace / @westonpace:
Ok, so I was finally able to track this down. Fortunately (unfortunately?) it is not really a compiler bug (or maybe it is, I'm not sure). At the very least, I think we can avoid it.
level_comparison.ccis compiled with-msse4.2.level_comparison_avx2.ccis compiled with-mavx2This is expected and the functions they generate are housed in separate namespaces so they don't get confused. However, both functions rely on the function arrow::internal::FirstTimeBitmapWriter::AppendWord. The function is not templated but it is defined in the header file (and is not marked inline). I'm not really sure how we aren't getting a duplicate symbol error but some reading suggests it is implicitly inlined at link time.
In the object file (libparquet.a), there are two identical symbols named
__ZN5arrow8internal21FirstTimeBitmapWriter10AppendWordEyx. One of them hasSHLXand one of them hasSHL. This disassembly of theSHLXversion matches exactly the disassembly in the stack trace that @jonkeane posted in the PR. The two calling functions are (parquet::internal::standard::DefLevelsBatchToBitmapandparquet::internal::bmi2::DefLevelsBatchToBitmap.So I think, the -O3 version is inlining the functions. The -Os version is not (-Os seems to discourage inlining in general). The linker is then faced with two identical symbols and just picks one (again, trying to optimize for size). It just so happens the version it picked was the one with
SHLX.So, as a test, we can try splitting the implementation part of
bitmap_writer.hintobitmap_writer.cc(at least forFirstTimeBitmapWriter). The .cc file should then only be compiled once (with sse4.2). However, it's very possible we are just hitting the tip of the iceberg here, as any header file linked in by these avx2 compiled versions could be a ticking time bomb.asfimport commentedon Apr 19, 2022
David Li / @lidavidm:
Good catch. This is exactly the same problem we ran into before with kernels:
arrow/cpp/src/arrow/compute/kernels/aggregate_internal.h
Lines 135 to 138 in 6c10a38
asfimport commentedon Apr 19, 2022
Weston Pace / @westonpace:
We can perhaps add a static check that reports symbols outside the appropriate namespace. We might need some configurable suppression. For example,
level_conversion_bmi2.cc.owould report:level_comparison_avx.cc.olooks to be in better shape:But yes, if we have a better solution for this problem it might be safer.
asfimport commentedon Apr 21, 2022
Antoine Pitrou / @pitrou:
Wow, thanks for the diagnosis @westonpace.
So, it turns out that our method for compiling multiple versions of code is violating the one-definition-rule for any inline function or method used in the caller code.
asfimport commentedon Apr 21, 2022
Antoine Pitrou / @pitrou:
@bkietz You may have some idea about how to fix this cleanly and reliably.
asfimport commentedon Apr 21, 2022
Antoine Pitrou / @pitrou:
So, currently we are doing something such as:
An alternative would be not to pass the optimization flag on the command line but enable it selectively inside the source code, e.g.:
This way we would avoid enabling the particular instruction set on code inlined from other headers. Of course, perhaps that's not actually desirable...
asfimport commentedon Apr 21, 2022
Antoine Pitrou / @pitrou:
In any case, this is probably too involved a change for 8.0.0, so the 8.0.0 fix would simply to disable SIMD optimizations for Homebrew?
asfimport commentedon Apr 21, 2022
Weston Pace / @westonpace:
That seems like an good solution to me. I had no idea it was possible.
If we want the other headers to be included then we already have a bit of a solution demonstrated in
level_conversion_inc.h. In the common header file you require some kind oftargetnamespace to be defined.However, anything that includes one of these "common headers" must define that namespace...
asfimport commentedon May 18, 2022
Jonathan Keane / @jonkeane:
@kou Do you think you might be able to take a look at this?
The comment at #12928 (comment) has a good explanation of what's going on and following that there are a few possible fixes (though none of them were fully implemented or decided
asfimport commentedon May 19, 2022
Kouhei Sutou / @kou:
How about using template to distinct implementation for each architecture?
asfimport commentedon May 19, 2022
Antoine Pitrou / @pitrou:
@kou We can do that for the specific symptoms here. However, a more general solution will have to be found since other files have the same problem: compiling SIMD-specific code which calls into other routines.
asfimport commentedon May 25, 2022
Ben Kietzman / @bkietz:
@pitrou The most robust solution I can think of is to avoid linking between objects with differing instruction sets altogether. We'd have something like
That library would be acquired with
dlopen(path, RTLD_LOCAL)/LoadLibrary(path)which would guarantee that any functions likeFirstTimeBitmapWriter::*which might have been recompiled with illegal instructions are not available outsidelibarrow_compute_avx2.so.asfimport commentedon May 25, 2022
Ben Kietzman / @bkietz:
IIUC, we'll still need to pass
-mavx2so that we can include immintrin.h so the attribute described in theARROW_SPECIALIZED_SIMD_TARGETapproach would need to be attached to the {}non{}-SIMD functions to ensure that they're compiled with no special instructions... or I suppose we could try to declare all the intrinsics manually at function scope
asfimport commentedon May 25, 2022
Ben Kietzman / @bkietz:
On further investigation, we can include immintrin.h with or without -mavx2 and clang at least will not complain unless the intrinsics are referenced, so
compiles and runs happily without any special compilation flags. Using an attribute like this seems viable provided we can be certain that the modified target isn't transitively applied to functions which might be invoked for the first time inside a SIMD enabled function
11 remaining items
asfimport commentedon Jul 25, 2022
Krisztian Szucs / @kszucs:
Postponing to 10.0 for now.
asfimport commentedon Jul 25, 2022
Jonathan Keane / @jonkeane:
I have no updates beyond what's discussed above: there are a few approaches, none of them ideal, we need someone to champion this (or risk the homebrew maintainers turning off optimizations on us)
asfimport commentedon Oct 6, 2022
Raúl Cumplido / @raulcd:
This was a blocker for the last release and is still a blocker for the 10.0.0 release. @jonkeane do you know if there has been any move?
asfimport commentedon Oct 6, 2022
Jonathan Keane / @jonkeane:
I thought that @kou was going to take a look at this (or at least the underlying multiple SIMD instruction ordering issue that causes the failures...)
The only update I have is that I continue to run into the segfault in CI for downstream projects I'm working on, so it continues to be an issue for pre-built libarrow on machines like github's macos runners.
asfimport commentedon Oct 6, 2022
Kouhei Sutou / @kou:
Yes. I will fix this before the 10.0.0 release. Sorry for not working on this yet.
asfimport commentedon Oct 7, 2022
Kouhei Sutou / @kou:
Summary of this problem:
Problem:
Parquet module is crashed with
-DCMAKE_BUILD_TYPE=MinSizeRelWhy the problem is happened:
We compile the same code (
level_conversion_inc.h) multiple times with different optimization flags such as-msse4.2and-mavx2The code calls the same function (
arrow::internal::FirstTimeBitmapWriter::AppendWord()) that is defined in header fileThe called function isn't inlined with
-DCMAKE_BUILD_TYPE=MinSizeRelIt generates multiple definitions for the called (not-inlined) function (
arrow::internal::FirstTimeBitmapWriter::AppendWord())Proposed solutions so far:
Force to inline functions that are called from the code that are compiled with SIMD related optimization flags
Restrict SIMD related optimization area to only the target function
For 1., we have two approaches for it:
\_\_attribute\_\_((always\_inline))ARROW-15678: [C++][CI] a crossbow job with MinRelSize enabled #12928 (comment)
This approach seems ad-hoc. We need to apply this approach to called functions when we find this problem in other codes.
For 2., we have one approach for it:
targetattribute:https://issues.apache.org/jira/browse/ARROW-15678?focusedCommentId=17526053#comment-17526053 : A sample code that shows this approach
(I'm not sure that this approach is portable. For example, can we use this approach with MSVC?)
asfimport commentedon Oct 7, 2022
Kouhei Sutou / @kou:
I propose one more approach for the proposed solution 1.:
How about always enabling inline optimization for SIMD optimized compile units (
level_conversion_bmi2.cc) even when an user specifies-DCMAKE_BUILD_TYPE=MinSizeRel?It may increases binary size but it may be better that SIMD related code prioritizes performance than binary size.
We don't need to write manual
template/\_\_attribute\_\_((always\_inline))s with this approach.asfimport commentedon Oct 7, 2022
Antoine Pitrou / @pitrou:
Well, I don't think we can force the compiler to inline everything.
asfimport commentedon Oct 7, 2022
Kouhei Sutou / @kou:
We don't have this problem with
-DCMAKE_BUILD_TYPE=Release. So it may work with most cases.asfimport commentedon Oct 10, 2022
Kouhei Sutou / @kou:
Issue resolved by pull request 14342
#14342
GH-36685: [R][C++] Fix illegal opcode failure with Homebrew (#36705)
apacheGH-36685: [R][C++] Fix illegal opcode failure with Homebrew (ap…