Skip to content

Commit

Permalink
[flang] Reduce recursion in common::visit (#85483)
Browse files Browse the repository at this point in the history
This patch yields small speed-ups in compiler build and execution times,
but more importantly, reduces the stack depth needed in a build
environment where tail call optimization does not appear to occur.
  • Loading branch information
klausler committed Mar 18, 2024
1 parent ea72c08 commit 0007d7e
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions flang/include/flang/Common/visit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,6 @@
//
// Define FLANG_USE_STD_VISIT to avoid this code and make common::visit() an
// alias for ::std::visit().
//
//
// With GCC 9.3.0 on a Haswell x86 Ubuntu system, doing out-of-tree builds:
// Before:
// build:
// 6948.53user 212.48system 27:32.92elapsed 433%CPU
// (0avgtext+0avgdata 6429568maxresident)k
// 36181912inputs+8943720outputs (3613684major+97908699minor)pagefaults 0swaps
// execution of tests:
// 205.99user 26.05system 1:08.87elapsed 336%CPU
// (0avgtext+0avgdata 2671452maxresident)k
// 244432inputs+355464outputs (422major+8746468minor)pagefaults 0swaps
// After:
// build:
// 6651.91user 182.57system 25:15.73elapsed 450%CPU
// (0avgtext+0avgdata 6209296maxresident)k
// 17413480inputs+6376360outputs (1567210major+93068230minor)pagefaults 0swaps
// execution of tests:
// 201.42user 25.91system 1:04.68elapsed 351%CPU
// (0avgtext+0avgdata 2661424maxresident)k
// 238840inputs+295912outputs (428major+8489300minor)pagefaults 0swaps

#ifndef FORTRAN_COMMON_VISIT_H_
#define FORTRAN_COMMON_VISIT_H_
Expand All @@ -52,7 +31,22 @@ template <std::size_t LOW, std::size_t HIGH, typename RESULT, typename VISITOR,
typename... VARIANT>
inline RESULT Log2VisitHelper(
VISITOR &&visitor, std::size_t which, VARIANT &&...u) {
if constexpr (LOW == HIGH) {
if constexpr (LOW + 7 >= HIGH) {
switch (which - LOW) {
#define VISIT_CASE_N(N) \
case N: \
if constexpr (LOW + N <= HIGH) { \
return visitor(std::get<(LOW + N)>(std::forward<VARIANT>(u))...); \
}
VISIT_CASE_N(1)
VISIT_CASE_N(2)
VISIT_CASE_N(3)
VISIT_CASE_N(4)
VISIT_CASE_N(5)
VISIT_CASE_N(6)
VISIT_CASE_N(7)
#undef VISIT_CASE_N
}
return visitor(std::get<LOW>(std::forward<VARIANT>(u))...);
} else {
static constexpr std::size_t mid{(HIGH + LOW) / 2};
Expand Down

0 comments on commit 0007d7e

Please sign in to comment.