This is an automated email from the ASF dual-hosted git repository.

Mryange pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 7038bb70bf2 [opt](expr) Vectorize AVG serialized state merge (#65729)
7038bb70bf2 is described below

commit 7038bb70bf25d2b5d23600fc0b0dd7f02f63abd4
Author: Mryange <[email protected]>
AuthorDate: Thu Jul 23 17:37:59 2026 +0800

    [opt](expr) Vectorize AVG serialized state merge (#65729)
    
    ### What problem does this PR solve?
    
    
    Clang preserves the original floating-point accumulation order when
    merging serialized AVG states,
    preventing vectorization of this contiguous hot loop. This change adds a
    reusable reassociation hint
    and applies it to AVG range merge. Existing raw Clang pragmas in AVG,
    SUM, and AVG_WEIGHTED are also
    replaced with the shared macro.
    
    The focused benchmark reduced CPU time by 35.6% for 4K states and 38.5%
    for 64K states.
---
 be/src/common/compiler_util.h                            | 10 +++++++++-
 be/src/exprs/aggregate/aggregate_function_avg.h          |  6 +++---
 be/src/exprs/aggregate/aggregate_function_avg_weighted.h |  9 +++------
 be/src/exprs/aggregate/aggregate_function_sum.h          |  5 ++---
 4 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/be/src/common/compiler_util.h b/be/src/common/compiler_util.h
index 559fca903e6..1d2a25b7742 100644
--- a/be/src/common/compiler_util.h
+++ b/be/src/common/compiler_util.h
@@ -56,4 +56,12 @@
 #define NO_SANITIZE_UNDEFINED __attribute__((__no_sanitize__("undefined")))
 #else
 #define NO_SANITIZE_UNDEFINED
-#endif
\ No newline at end of file
+#endif
+
+// Allow reassociation only when the evaluation order is not part of the 
operation's contract.
+// This may change the least significant bits of floating-point results.
+#ifdef __clang__
+#define ALLOW_FP_REASSOCIATION _Pragma("clang fp reassociate(on)")
+#else
+#define ALLOW_FP_REASSOCIATION
+#endif
diff --git a/be/src/exprs/aggregate/aggregate_function_avg.h 
b/be/src/exprs/aggregate/aggregate_function_avg.h
index 876bf1ede7b..795dce34ec3 100644
--- a/be/src/exprs/aggregate/aggregate_function_avg.h
+++ b/be/src/exprs/aggregate/aggregate_function_avg.h
@@ -29,6 +29,7 @@
 #include <type_traits>
 #include <vector>
 
+#include "common/compiler_util.h"
 #include "core/assert_cast.h"
 #include "core/column/column.h"
 #include "core/column/column_fixed_length_object.h"
@@ -167,9 +168,7 @@ public:
     template <bool is_add>
     NO_SANITIZE_UNDEFINED void update_value(AggregateDataPtr __restrict place,
                                             const IColumn** columns, ssize_t 
row_num) const {
-#ifdef __clang__
-#pragma clang fp reassociate(on)
-#endif
+        ALLOW_FP_REASSOCIATION
         const auto& column =
                 assert_cast<const ColVecType&, 
TypeCheckOnRelease::DISABLE>(*columns[0]);
         if constexpr (is_add) {
@@ -262,6 +261,7 @@ public:
     NO_SANITIZE_UNDEFINED void deserialize_and_merge_from_column_range(
             AggregateDataPtr __restrict place, const IColumn& column, size_t 
begin, size_t end,
             Arena&) const override {
+        ALLOW_FP_REASSOCIATION
         DCHECK(end <= column.size() && begin <= end)
                 << ", begin:" << begin << ", end:" << end << ", 
column.size():" << column.size();
         auto& col = assert_cast<const ColumnFixedLengthObject&>(column);
diff --git a/be/src/exprs/aggregate/aggregate_function_avg_weighted.h 
b/be/src/exprs/aggregate/aggregate_function_avg_weighted.h
index 3984b4de41e..7385281d5b6 100644
--- a/be/src/exprs/aggregate/aggregate_function_avg_weighted.h
+++ b/be/src/exprs/aggregate/aggregate_function_avg_weighted.h
@@ -26,6 +26,7 @@
 #include <memory>
 #include <type_traits>
 
+#include "common/compiler_util.h"
 #include "common/status.h"
 #include "core/assert_cast.h"
 #include "core/binary_cast.hpp"
@@ -47,9 +48,7 @@ template <PrimitiveType T>
 struct AggregateFunctionAvgWeightedData {
     using DataType = typename PrimitiveTypeTraits<T>::CppType;
     void add(const DataType& data_val, double weight_val) {
-#ifdef __clang__
-#pragma clang fp reassociate(on)
-#endif
+        ALLOW_FP_REASSOCIATION
         data_sum = data_sum + (double(data_val) * weight_val);
         weight_sum = weight_sum + weight_val;
     }
@@ -65,9 +64,7 @@ struct AggregateFunctionAvgWeightedData {
     }
 
     void merge(const AggregateFunctionAvgWeightedData& rhs) {
-#ifdef __clang__
-#pragma clang fp reassociate(on)
-#endif
+        ALLOW_FP_REASSOCIATION
         data_sum = data_sum + rhs.data_sum;
         weight_sum = weight_sum + rhs.weight_sum;
     }
diff --git a/be/src/exprs/aggregate/aggregate_function_sum.h 
b/be/src/exprs/aggregate/aggregate_function_sum.h
index ca7c1aaf557..8eddb893ed9 100644
--- a/be/src/exprs/aggregate/aggregate_function_sum.h
+++ b/be/src/exprs/aggregate/aggregate_function_sum.h
@@ -25,6 +25,7 @@
 #include <memory>
 #include <vector>
 
+#include "common/compiler_util.h"
 #include "core/assert_cast.h"
 #include "core/column/column.h"
 #include "core/data_type/data_type.h"
@@ -51,9 +52,7 @@ struct AggregateFunctionSumData {
     typename PrimitiveTypeTraits<T>::CppType sum {};
 
     NO_SANITIZE_UNDEFINED void add(typename PrimitiveTypeTraits<T>::CppType 
value) {
-#ifdef __clang__
-#pragma clang fp reassociate(on)
-#endif
+        ALLOW_FP_REASSOCIATION
         sum += value;
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to