Skip to content

Conversation

@adeel10x
Copy link

No description provided.

@adeel10x adeel10x requested a review from a team as a code owner August 23, 2024 21:04
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Aug 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 23, 2024

@llvm/pr-subscribers-libcxx

Author: None (adeel10x)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/105888.diff

1 Files Affected:

  • (modified) libcxx/include/__algorithm/count_if.h (+29-4)
diff --git a/libcxx/include/__algorithm/count_if.h b/libcxx/include/__algorithm/count_if.h
index 25782069d03275..ba1cdb16b56b41 100644
--- a/libcxx/include/__algorithm/count_if.h
+++ b/libcxx/include/__algorithm/count_if.h
@@ -10,8 +10,10 @@
 #ifndef _LIBCPP___ALGORITHM_COUNT_IF_H
 #define _LIBCPP___ALGORITHM_COUNT_IF_H
 
+#include <__algorithm/for_each.h>
 #include <__config>
 #include <__iterator/iterator_traits.h>
+#include <__iterator/segmented_iterator.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -19,10 +21,12 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _InputIterator, class _Predicate>
+template <class _InputIterator,
+          class _Predicate,
+          __enable_if_t<!__is_segmented_iterator<_InputIterator>::value, int> = 0>
 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-typename iterator_traits<_InputIterator>::difference_type
-count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
+    typename iterator_traits<_InputIterator>::difference_type
+    __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
   typename iterator_traits<_InputIterator>::difference_type __r(0);
   for (; __first != __last; ++__first)
     if (__pred(*__first))
@@ -30,6 +34,27 @@ count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
   return __r;
 }
 
+template <class _SegmentedIterator,
+          class _Predicate,
+          __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
+_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+    typename iterator_traits<_SegmentedIterator>::difference_type
+    __count_if(_SegmentedIterator __first, _SegmentedIterator __last, _Predicate __pred) {
+  typename iterator_traits<_SegmentedIterator>::difference_type __r(0);
+  std::for_each(__first, __last, [&__r, __pred](auto& __val) mutable {
+    if (__pred(__val))
+      ++__r;
+  });
+  return __r;
+}
+
+template <class _InputIterator, class _Predicate>
+_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+    typename iterator_traits<_InputIterator>::difference_type
+    count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
+  return __count_if(__first, __last, __pred);
+}
+
 _LIBCPP_END_NAMESPACE_STD
 
-#endif // _LIBCPP___ALGORITHM_COUNT_IF_H
+#endif // _LIBCPP___ALGORITHM_COUNT_IF_H
\ No newline at end of file

@adeel10x
Copy link
Author

@philnik777 I have pushed the changes for count_if. If it looks fine to you, then I'll go ahead and benchmark.

@ldionne ldionne requested a review from philnik777 August 29, 2024 14:40
#ifndef _LIBCPP___ALGORITHM_COUNT_IF_H
#define _LIBCPP___ALGORITHM_COUNT_IF_H

#include <__algorithm/for_each.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Not attached: This optimization should also be applied to ranges::count_if.

Copy link
Author

@adeel10x adeel10x Sep 5, 2024

Choose a reason for hiding this comment

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

@philnik777 I just want to confirm that you mean to say ranges::count_if and not std::count?
Also, Is ranges::for_each optimized for segmented iterators?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know whether ranges::for_each is optimized. If not, we should definitely do it.

#include <__algorithm/for_each.h>
#include <__config>
#include <__iterator/iterator_traits.h>
#include <__iterator/segmented_iterator.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Not attached: Please add a benchmark to show that this actually improves performance.

@adeel10x
Copy link
Author

adeel10x commented Sep 2, 2024

@philnik777 I have added the benchmark for count_if to test performance with segmented_iterators: libcxx/test/benchmarks/algorithms/count_if.bench.cpp.

@adeel10x
Copy link
Author

adeel10x commented Sep 6, 2024

Benchmark compilation was failing due to this error:

[1/2] /home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/Desktop/llvm-project/build/include/c++/v1 -L/home/adeel/Desktop/llvm-project/build/lib -Wl,-rpath,/home/adeel/Desktop/llvm-project/build/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -c /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
FAILED: CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o
/home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/Desktop/llvm-project/build/include/c++/v1 -L/home/adeel/Desktop/llvm-project/build/lib -Wl,-rpath,/home/adeel/Desktop/llvm-project/build/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -c /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
In file included from /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp:2:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/string:647:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/string_view:958:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/algorithm:1832:
/home/adeel/Desktop/llvm-project/build/include/c++/v1/__algorithm/count_if.h:28:56: error: 'auto' not allowed in lambda parameter before C++14
   28 |   std::for_each(__first, __last, [&__r, &__pred](const auto& __val) mutable {
      |                                                        ^~~~
1 error generated.
ninja: build stopped: subcommand failed.

So, I guess I can't use auto for lambda parameter type here.

  • For segmented iterators, the lambda argument type would be:const iterator_traits<Traits::__local_iterator>::value_type& where Traits is __segmented_iterator_traits<_SegmentedIterator>,
  • For others, it would be: const iterator_traits<_InputIterator>::value_type&
    Should I create separate template functions for segmented and non-segmented iterators to implement this? Just like I had earlier...

@philnik777
Copy link
Contributor

Benchmark compilation was failing due to this error:

[1/2] /home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/Desktop/llvm-project/build/include/c++/v1 -L/home/adeel/Desktop/llvm-project/build/lib -Wl,-rpath,/home/adeel/Desktop/llvm-project/build/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -c /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
FAILED: CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o
/home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/Desktop/llvm-project/build/include/c++/v1 -L/home/adeel/Desktop/llvm-project/build/lib -Wl,-rpath,/home/adeel/Desktop/llvm-project/build/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -c /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
In file included from /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp:2:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/string:647:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/string_view:958:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/algorithm:1832:
/home/adeel/Desktop/llvm-project/build/include/c++/v1/__algorithm/count_if.h:28:56: error: 'auto' not allowed in lambda parameter before C++14
   28 |   std::for_each(__first, __last, [&__r, &__pred](const auto& __val) mutable {
      |                                                        ^~~~
1 error generated.
ninja: build stopped: subcommand failed.

So, I guess I can't use auto for lambda parameter type here.

* For segmented iterators, the lambda argument type would be:`const iterator_traits<Traits::__local_iterator>::value_type&` where Traits is `__segmented_iterator_traits<_SegmentedIterator>`,

* For others, it would be: `const iterator_traits<_InputIterator>::value_type&`
  Should I create separate template functions for segmented and non-segmented iterators to implement this? Just like I had earlier...

You should be able to use []<class _Tp>(const _Tp&) mutable {...}.

@philnik777
Copy link
Contributor

@philnik777 I have added the benchmark for count_if to test performance with segmented_iterators: libcxx/test/benchmarks/algorithms/count_if.bench.cpp.

Could you also provide numbers before and after your patch?

@adeel10x
Copy link
Author

adeel10x commented Sep 9, 2024

Benchmark compilation was failing due to this error:

[1/2] /home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/Desktop/llvm-project/build/include/c++/v1 -L/home/adeel/Desktop/llvm-project/build/lib -Wl,-rpath,/home/adeel/Desktop/llvm-project/build/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -c /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
FAILED: CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o
/home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/Desktop/llvm-project/build/include/c++/v1 -L/home/adeel/Desktop/llvm-project/build/lib -Wl,-rpath,/home/adeel/Desktop/llvm-project/build/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -c /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
In file included from /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp:2:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/string:647:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/string_view:958:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/algorithm:1832:
/home/adeel/Desktop/llvm-project/build/include/c++/v1/__algorithm/count_if.h:28:56: error: 'auto' not allowed in lambda parameter before C++14
   28 |   std::for_each(__first, __last, [&__r, &__pred](const auto& __val) mutable {
      |                                                        ^~~~
1 error generated.
ninja: build stopped: subcommand failed.

So, I guess I can't use auto for lambda parameter type here.

* For segmented iterators, the lambda argument type would be:`const iterator_traits<Traits::__local_iterator>::value_type&` where Traits is `__segmented_iterator_traits<_SegmentedIterator>`,

* For others, it would be: `const iterator_traits<_InputIterator>::value_type&`
  Should I create separate template functions for segmented and non-segmented iterators to implement this? Just like I had earlier...

You should be able to use []<class _Tp>(const _Tp&) mutable {...}.

Can we use the template parameter with lambda expressions with C++11? Many files are being compiled with -std=c++11. I am getting a different error again.

@adeel10x
Copy link
Author

adeel10x commented Sep 9, 2024

Benchmark compilation was failing due to this error:

[1/2] /home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/Desktop/llvm-project/build/include/c++/v1 -L/home/adeel/Desktop/llvm-project/build/lib -Wl,-rpath,/home/adeel/Desktop/llvm-project/build/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -c /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
FAILED: CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o
/home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/Desktop/llvm-project/build/include/c++/v1 -L/home/adeel/Desktop/llvm-project/build/lib -Wl,-rpath,/home/adeel/Desktop/llvm-project/build/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_8bf2e.dir/posix_regex.cpp.o -c /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
In file included from /home/adeel/Desktop/llvm-project/third-party/benchmark/cmake/posix_regex.cpp:2:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/string:647:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/string_view:958:
In file included from /home/adeel/Desktop/llvm-project/build/include/c++/v1/algorithm:1832:
/home/adeel/Desktop/llvm-project/build/include/c++/v1/__algorithm/count_if.h:28:56: error: 'auto' not allowed in lambda parameter before C++14
   28 |   std::for_each(__first, __last, [&__r, &__pred](const auto& __val) mutable {
      |                                                        ^~~~
1 error generated.
ninja: build stopped: subcommand failed.

So, I guess I can't use auto for lambda parameter type here.

* For segmented iterators, the lambda argument type would be:`const iterator_traits<Traits::__local_iterator>::value_type&` where Traits is `__segmented_iterator_traits<_SegmentedIterator>`,

* For others, it would be: `const iterator_traits<_InputIterator>::value_type&`
  Should I create separate template functions for segmented and non-segmented iterators to implement this? Just like I had earlier...

You should be able to use []<class _Tp>(const _Tp&) mutable {...}.

Can we use the template parameter with lambda expressions with C++11? Many files are being compiled with -std=c++11. I am getting a different error again.

This is the new code:

template <class _InputIterator, class _Predicate>
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
    typename iterator_traits<_InputIterator>::difference_type
    count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
  typename iterator_traits<_InputIterator>::difference_type __r(0);
  std::for_each(__first, __last, [&__r, &__pred]<class _Tp>(const _Tp& __val) mutable {
    if (__pred(__val))
      ++__r;
  });
  return __r;
}

This is the command that causes clang crash:

Program arguments: /home/adeel/llvm-project/build/bin/clang++ -DBENCHMARK_HAS_PTHREAD_AFFINITY -DBENCHMARK_STATIC_DEFINE -DHAVE_POSIX_REGEX -DHAVE_PTHREAD_AFFINITY -DHAVE_STD_REGEX -DHAVE_STEADY_CLOCK -DHAVE_THREAD_SAFETY_ATTRIBUTES -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -I/home/adeel/llvm-project/third-party/benchmark/include -I/home/adeel/llvm-project/third-party/benchmark/src -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/llvm-project/build2/include/c++/v1 -L/home/adeel/llvm-project/build2/lib -Wl,-rpath,/home/adeel/llvm-project/build2/lib -Wall -Wextra -Wshadow -Wfloat-equal -Wold-style-cast -Werror -Wsuggest-override -pedantic -pedantic-errors -Wshorten-64-to-32 -fstrict-aliasing -Wno-deprecated-declarations -Wno-deprecated -Wstrict-aliasing -Wthread-safety -stdlib=libc++ -O3 -DNDEBUG -std=c++11 -fvisibility=hidden -fvisibility-inlines-hidden -MD -MT src/CMakeFiles/benchmark.dir/statistics.cc.o -MF src/CMakeFiles/benchmark.dir/statistics.cc.o.d -o src/CMakeFiles/benchmark.dir/statistics.cc.o -c /home/adeel/llvm-project/third-party/benchmark/src/statistics.cc

And this is the crash stack:

PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /home/adeel/llvm-project/build/bin/clang++ -DBENCHMARK_HAS_PTHREAD_AFFINITY -DBENCHMARK_STATIC_DEFINE -DHAVE_POSIX_REGEX -DHAVE_PTHREAD_AFFINITY -DHAVE_STD_REGEX -DHAVE_STEADY_CLOCK -DHAVE_THREAD_SAFETY_ATTRIBUTES -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -I/home/adeel/llvm-project/third-party/benchmark/include -I/home/adeel/llvm-project/third-party/benchmark/src -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/llvm-project/build2/include/c++/v1 -L/home/adeel/llvm-project/build2/lib -Wl,-rpath,/home/adeel/llvm-project/build2/lib -Wall -Wextra -Wshadow -Wfloat-equal -Wold-style-cast -Werror -Wsuggest-override -pedantic -pedantic-errors -Wshorten-64-to-32 -fstrict-aliasing -Wno-deprecated-declarations -Wno-deprecated -Wstrict-aliasing -Wthread-safety -stdlib=libc++ -O3 -DNDEBUG -std=c++11 -fvisibility=hidden -fvisibility-inlines-hidden -MD -MT src/CMakeFiles/benchmark.dir/statistics.cc.o -MF src/CMakeFiles/benchmark.dir/statistics.cc.o.d -o src/CMakeFiles/benchmark.dir/statistics.cc.o -c /home/adeel/llvm-project/third-party/benchmark/src/statistics.cc
1.	<eof> parser at end of file
2.	Per-file LLVM IR generation
3.	/home/adeel/llvm-project/build2/include/c++/v1/__algorithm/for_each.h:32:1: Generating code for declaration 'std::for_each'
 #0 0x00005d26a86fe310 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/adeel/llvm-project/build/bin/clang+++0x1fc0310)
 #1 0x00005d26a86fc204 llvm::sys::CleanupOnSignal(unsigned long) (/home/adeel/llvm-project/build/bin/clang+++0x1fbe204)
 #2 0x00005d26a864ff78 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x0000741845a42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00005d26a82038c6 llvm::FunctionType::get(llvm::Type*, llvm::ArrayRef<llvm::Type*>, bool) (/home/adeel/llvm-project/build/bin/clang+++0x1ac58c6)
 #5 0x00005d26a8d75d44 clang::CodeGen::CodeGenTypes::GetFunctionType(clang::CodeGen::CGFunctionInfo const&) (/home/adeel/llvm-project/build/bin/clang+++0x2637d44)
 #6 0x00005d26a8e2f7bf clang::CodeGen::CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr(clang::CallExpr const*, clang::CXXMethodDecl const*, clang::CodeGen::ReturnValueSlot, bool, clang::NestedNameSpecifier*, bool, clang::Expr const*) (/home/adeel/llvm-project/build/bin/clang+++0x26f17bf)
 #7 0x00005d26a8e3088f clang::CodeGen::CodeGenFunction::EmitCXXOperatorMemberCallExpr(clang::CXXOperatorCallExpr const*, clang::CXXMethodDecl const*, clang::CodeGen::ReturnValueSlot) (/home/adeel/llvm-project/build/bin/clang+++0x26f288f)
 #8 0x00005d26a8e009a6 clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/home/adeel/llvm-project/build/bin/clang+++0x26c29a6)
 #9 0x00005d26a8e5b839 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) CGExprScalar.cpp:0:0
#10 0x00005d26a8e5821f clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/home/adeel/llvm-project/build/bin/clang+++0x271a21f)
#11 0x00005d26a8ded4ce clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*, clang::CodeGen::AggValueSlot, bool) (/home/adeel/llvm-project/build/bin/clang+++0x26af4ce)
#12 0x00005d26a8dff28e clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) (/home/adeel/llvm-project/build/bin/clang+++0x26c128e)
#13 0x00005d26a89f2b8a clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/home/adeel/llvm-project/build/bin/clang+++0x22b4b8a)
#14 0x00005d26a89f7705 clang::CodeGen::CodeGenFunction::EmitForStmt(clang::ForStmt const&, llvm::ArrayRef<clang::Attr const*>) (/home/adeel/llvm-project/build/bin/clang+++0x22b9705)
#15 0x00005d26a89fa141 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/home/adeel/llvm-project/build/bin/clang+++0x22bc141)
#16 0x00005d26a8a5a924 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/home/adeel/llvm-project/build/bin/clang+++0x231c924)
#17 0x00005d26a8a6d592 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/home/adeel/llvm-project/build/bin/clang+++0x232f592)
#18 0x00005d26a8ab6e4c clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/home/adeel/llvm-project/build/bin/clang+++0x2378e4c)
#19 0x00005d26a8ab1fd5 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/home/adeel/llvm-project/build/bin/clang+++0x2373fd5)
#20 0x00005d26a8abbfc0 clang::CodeGen::CodeGenModule::EmitDeferred() (/home/adeel/llvm-project/build/bin/clang+++0x237dfc0)
#21 0x00005d26a8abbfd8 clang::CodeGen::CodeGenModule::EmitDeferred() (/home/adeel/llvm-project/build/bin/clang+++0x237dfd8)
#22 0x00005d26a8abce24 clang::CodeGen::CodeGenModule::Release() (/home/adeel/llvm-project/build/bin/clang+++0x237ee24)
#23 0x00005d26a8f80966 (anonymous namespace)::CodeGeneratorImpl::HandleTranslationUnit(clang::ASTContext&) ModuleBuilder.cpp:0:0
#24 0x00005d26a8f7e415 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/home/adeel/llvm-project/build/bin/clang+++0x2840415)
#25 0x00005d26aa9dcddc clang::ParseAST(clang::Sema&, bool, bool) (/home/adeel/llvm-project/build/bin/clang+++0x429eddc)
#26 0x00005d26a923a6c9 clang::FrontendAction::Execute() (/home/adeel/llvm-project/build/bin/clang+++0x2afc6c9)
#27 0x00005d26a91b0cc9 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/adeel/llvm-project/build/bin/clang+++0x2a72cc9)
#28 0x00005d26a93043f3 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/adeel/llvm-project/build/bin/clang+++0x2bc63f3)
#29 0x00005d26a7403e33 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/home/adeel/llvm-project/build/bin/clang+++0xcc5e33)
#30 0x00005d26a73fd372 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#31 0x00005d26a8fc95dd void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#32 0x00005d26a86503c7 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/home/adeel/llvm-project/build/bin/clang+++0x1f123c7)
#33 0x00005d26a8fc9977 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#34 0x00005d26a8f8fb01 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/home/adeel/llvm-project/build/bin/clang+++0x2851b01)
#35 0x00005d26a8f905bd clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/home/adeel/llvm-project/build/bin/clang+++0x28525bd)
#36 0x00005d26a8fa24cc clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/home/adeel/llvm-project/build/bin/clang+++0x28644cc)
#37 0x00005d26a7400892 clang_main(int, char**, llvm::ToolContext const&) (/home/adeel/llvm-project/build/bin/clang+++0xcc2892)
#38 0x00005d26a732b98b main (/home/adeel/llvm-project/build/bin/clang+++0xbed98b)
#39 0x0000741845a29d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#40 0x0000741845a29e40 call_init ./csu/../csu/libc-start.c:128:20
#41 0x0000741845a29e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#42 0x00005d26a73fcdd5 _start (/home/adeel/llvm-project/build/bin/clang+++0xcbedd5)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
clang version 20.0.0git ([email protected]:adeel10x/llvm-project.git 2aee2511c4035846cd25a230dac5675f17964158)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/adeel/llvm-project/build/bin
clang++: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: /tmp/statistics-54c2d5.cpp
clang++: note: diagnostic msg: /tmp/statistics-54c2d5.sh
clang++: note: diagnostic msg: 

********************

No crash if I set -std=c++14/17/20/23
Is there any specific reason we can't use at least --std=c++14 with these files? If that is possible I can easily use auto in lambda argument type.

@philnik777
Copy link
Contributor

@adeel10x oh, sweet. Please file a bug against clang for the crash.

No, there isn't a specific requirement to make this work in C++11. If there isn't a good reason not to do it we should though. Given that the simple option makes clang crash this seems like a good reason to not do it. Please add a >= C++14 guard and link the bug report.

@adeel10x
Copy link
Author

adeel10x commented Oct 11, 2024

@adeel10x oh, sweet. Please file a bug against clang for the crash.

No, there isn't a specific requirement to make this work in C++11. If there isn't a good reason not to do it we should though. Given that the simple option makes clang crash this seems like a good reason to not do it. Please add a >= C++14 guard and link the bug report.

Hi @philnik777,
I'm a bit confused about the next step. Do you want me to add a #if __cplusplus guard like this at the file top and change the compilation command for this file?

#if __cplusplus < 201402L 
#error "C++14 or better required\n"
#endif

If not, could you please tell me what guard I have to use here?

@philnik777
Copy link
Contributor

@adeel10x oh, sweet. Please file a bug against clang for the crash.
No, there isn't a specific requirement to make this work in C++11. If there isn't a good reason not to do it we should though. Given that the simple option makes clang crash this seems like a good reason to not do it. Please add a >= C++14 guard and link the bug report.

Hi @philnik777, I'm a bit confused about the next step. Do you want me to add a #if __cplusplus guard like this at the file top and change the compilation command for this file?

#if __cplusplus < 201402L 
#error "C++14 or better required\n"
#endif

If not, could you please tell me what guard I have to use here?

I think it'd actually be simpler to just refactor the lambda to a function object. That should definitely work in C++03. (Also, you'd want to use #if _LIBCPP_STD_VER >= 14 when guarding against a version)

@philnik777
Copy link
Contributor

@adeel10x do you plan to continue working on this?

@adeel10x
Copy link
Author

adeel10x commented Nov 4, 2024

@adeel10x do you plan to continue working on this?

yes, I'll upload a new patch very soon.

@adeel10x
Copy link
Author

adeel10x commented Nov 5, 2024

@philnik777 I tried using auto and function object like this:

template <class _InputIterator, class _Predicate>
struct __count_if{
	typename iterator_traits<_InputIterator>::difference_type __r;
	_Predicate __pred;
	_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __count_if(typename iterator_traits<_InputIterator>::difference_type& r, _Predicate& pred): __r(r), __pred(pred) {}
	_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR void operator()(const auto& __val) {
	  if (__pred(__val))
    	    ++__r;
  }
};


template <class _InputIterator, class _Predicate>
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
    typename iterator_traits<_InputIterator>::difference_type
    count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
  typename iterator_traits<_InputIterator>::difference_type __r(0);
  std::for_each(__first, __last, __count_if<_InputIterator, _Predicate>(__r, __pred));
  return __r;
}

It also causes c++ std version issue while compiling cxx-benchmarks. This is the error message:

/home/adeel/llvm-project/build/bin/clang++   -Wno-unused-command-line-argument -nostdinc++ -isystem /home/adeel/llvm-project/build2/include/c++/v1 -L/home/adeel/llvm-project/build2/lib -Wl,-rpath,/home/adeel/llvm-project/build2/lib  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -stdlib=libc++  -std=c++11 -MD -MT CMakeFiles/cmTC_a2c87.dir/posix_regex.cpp.o -MF CMakeFiles/cmTC_a2c87.dir/posix_regex.cpp.o.d -o CMakeFiles/cmTC_a2c87.dir/posix_regex.cpp.o -c /home/adeel/llvm-project/third-party/benchmark/cmake/posix_regex.cpp
In file included from /home/adeel/llvm-project/third-party/benchmark/cmake/posix_regex.cpp:2:
In file included from /home/adeel/llvm-project/build2/include/c++/v1/string:648:
In file included from /home/adeel/llvm-project/build2/include/c++/v1/string_view:959:
In file included from /home/adeel/llvm-project/build2/include/c++/v1/algorithm:1841:
/home/adeel/llvm-project/build2/include/c++/v1/__algorithm/count_if.h:28:64: error: 'auto' not allowed in function prototype
  28 |         _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR void operator()(const auto& __val) {
     |                                                                       ^~~~
1 error generated.
ninja: build stopped: subcommand failed.

Auto cannot be used in function parameters for >C++20. See this for reference.

@philnik777
Copy link
Contributor

@adeel10x You can just use a classic template.

@adeel10x adeel10x closed this Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants