[flang-commits] [flang] [flang][runtime] Distinguish VALUE from non-VALUE operations in REDUCE (PR #95297)

via flang-commits flang-commits at lists.llvm.org
Wed Jun 12 12:44:10 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

Accommodate operations with VALUE dummy arguments in the runtime support for the REDUCE intrinsic function by splitting most entry points into Reduce...Ref and Reduce...Value variants.

Further work will be needed in lowering to call the ...Value entry points.

---

Patch is 104.69 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/95297.diff


6 Files Affected:

- (modified) flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h (+12-12) 
- (modified) flang/include/flang/Runtime/reduce.h (+303-122) 
- (modified) flang/lib/Optimizer/Builder/Runtime/Reduction.cpp (+72-58) 
- (modified) flang/runtime/reduce.cpp (+481-134) 
- (modified) flang/test/Lower/Intrinsics/reduce.f90 (+33-33) 
- (modified) flang/unittests/Runtime/Reduction.cpp (+5-4) 


``````````diff
diff --git a/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h b/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h
index 99161c57fbe28..809d5b8d569dc 100644
--- a/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h
+++ b/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h
@@ -53,10 +53,10 @@ namespace fir::runtime {
 using TypeBuilderFunc = mlir::Type (*)(mlir::MLIRContext *);
 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *);
 
-#define REDUCTION_OPERATION_MODEL(T)                                           \
+#define REDUCTION_REF_OPERATION_MODEL(T)                                       \
   template <>                                                                  \
   constexpr TypeBuilderFunc                                                    \
-  getModel<Fortran::runtime::ReductionOperation<T>>() {                        \
+  getModel<Fortran::runtime::ReferenceReductionOperation<T>>() {               \
     return [](mlir::MLIRContext *context) -> mlir::Type {                      \
       TypeBuilderFunc f{getModel<T>()};                                        \
       auto refTy = fir::ReferenceType::get(f(context));                        \
@@ -480,18 +480,18 @@ constexpr TypeBuilderFunc getModel<void>() {
   };
 }
 
-REDUCTION_OPERATION_MODEL(std::int8_t)
-REDUCTION_OPERATION_MODEL(std::int16_t)
-REDUCTION_OPERATION_MODEL(std::int32_t)
-REDUCTION_OPERATION_MODEL(std::int64_t)
-REDUCTION_OPERATION_MODEL(Fortran::common::int128_t)
+REDUCTION_REF_OPERATION_MODEL(std::int8_t)
+REDUCTION_REF_OPERATION_MODEL(std::int16_t)
+REDUCTION_REF_OPERATION_MODEL(std::int32_t)
+REDUCTION_REF_OPERATION_MODEL(std::int64_t)
+REDUCTION_REF_OPERATION_MODEL(Fortran::common::int128_t)
 
-REDUCTION_OPERATION_MODEL(float)
-REDUCTION_OPERATION_MODEL(double)
-REDUCTION_OPERATION_MODEL(long double)
+REDUCTION_REF_OPERATION_MODEL(float)
+REDUCTION_REF_OPERATION_MODEL(double)
+REDUCTION_REF_OPERATION_MODEL(long double)
 
-REDUCTION_OPERATION_MODEL(std::complex<float>)
-REDUCTION_OPERATION_MODEL(std::complex<double>)
+REDUCTION_REF_OPERATION_MODEL(std::complex<float>)
+REDUCTION_REF_OPERATION_MODEL(std::complex<double>)
 
 REDUCTION_CHAR_OPERATION_MODEL(char)
 REDUCTION_CHAR_OPERATION_MODEL(char16_t)
diff --git a/flang/include/flang/Runtime/reduce.h b/flang/include/flang/Runtime/reduce.h
index 975aa6dea305f..60f54c393b4bb 100644
--- a/flang/include/flang/Runtime/reduce.h
+++ b/flang/include/flang/Runtime/reduce.h
@@ -28,7 +28,9 @@ namespace Fortran::runtime {
 
 class Descriptor;
 
-template <typename T> using ReductionOperation = T (*)(const T *, const T *);
+template <typename T>
+using ReferenceReductionOperation = T (*)(const T *, const T *);
+template <typename T> using ValueReductionOperation = T (*)(T, T);
 template <typename CHAR>
 using ReductionCharOperation = void (*)(CHAR *hiddenResult,
     std::size_t resultLen, const CHAR *x, const CHAR *y, std::size_t xLen,
@@ -38,185 +40,364 @@ using ReductionDerivedTypeOperation = void (*)(
 
 extern "C" {
 
-std::int8_t RTDECL(ReduceInteger1)(const Descriptor &,
-    ReductionOperation<std::int8_t>, const char *source, int line, int dim = 0,
-    const Descriptor *mask = nullptr, const std::int8_t *identity = nullptr,
-    bool ordered = true);
-void RTDECL(ReduceInteger1Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<std::int8_t>, const char *source, int line, int dim,
+std::int8_t RTDECL(ReduceInteger1Ref)(const Descriptor &,
+    ReferenceReductionOperation<std::int8_t>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const std::int8_t *identity = nullptr, bool ordered = true);
+std::int8_t RTDECL(ReduceInteger1Value)(const Descriptor &,
+    ValueReductionOperation<std::int8_t>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const std::int8_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger1DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<std::int8_t>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const std::int8_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger1DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<std::int8_t>, const char *source, int line, int dim,
     const Descriptor *mask = nullptr, const std::int8_t *identity = nullptr,
     bool ordered = true);
-std::int16_t RTDECL(ReduceInteger2)(const Descriptor &,
-    ReductionOperation<std::int16_t>, const char *source, int line, int dim = 0,
-    const Descriptor *mask = nullptr, const std::int16_t *identity = nullptr,
-    bool ordered = true);
-void RTDECL(ReduceInteger2Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<std::int16_t>, const char *source, int line, int dim,
-    const Descriptor *mask = nullptr, const std::int16_t *identity = nullptr,
-    bool ordered = true);
-std::int32_t RTDECL(ReduceInteger4)(const Descriptor &,
-    ReductionOperation<std::int32_t>, const char *source, int line, int dim = 0,
-    const Descriptor *mask = nullptr, const std::int32_t *identity = nullptr,
-    bool ordered = true);
-void RTDECL(ReduceInteger4Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<std::int32_t>, const char *source, int line, int dim,
-    const Descriptor *mask = nullptr, const std::int32_t *identity = nullptr,
-    bool ordered = true);
-std::int64_t RTDECL(ReduceInteger8)(const Descriptor &,
-    ReductionOperation<std::int64_t>, const char *source, int line, int dim = 0,
-    const Descriptor *mask = nullptr, const std::int64_t *identity = nullptr,
-    bool ordered = true);
-void RTDECL(ReduceInteger8Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<std::int64_t>, const char *source, int line, int dim,
-    const Descriptor *mask = nullptr, const std::int64_t *identity = nullptr,
-    bool ordered = true);
+std::int16_t RTDECL(ReduceInteger2Ref)(const Descriptor &,
+    ReferenceReductionOperation<std::int16_t>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const std::int16_t *identity = nullptr, bool ordered = true);
+std::int16_t RTDECL(ReduceInteger2Value)(const Descriptor &,
+    ValueReductionOperation<std::int16_t>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const std::int16_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger2DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<std::int16_t>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const std::int16_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger2DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<std::int16_t>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const std::int16_t *identity = nullptr, bool ordered = true);
+std::int32_t RTDECL(ReduceInteger4Ref)(const Descriptor &,
+    ReferenceReductionOperation<std::int32_t>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const std::int32_t *identity = nullptr, bool ordered = true);
+std::int32_t RTDECL(ReduceInteger4Value)(const Descriptor &,
+    ValueReductionOperation<std::int32_t>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const std::int32_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger4DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<std::int32_t>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const std::int32_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger4DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<std::int32_t>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const std::int32_t *identity = nullptr, bool ordered = true);
+std::int64_t RTDECL(ReduceInteger8Ref)(const Descriptor &,
+    ReferenceReductionOperation<std::int64_t>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const std::int64_t *identity = nullptr, bool ordered = true);
+std::int64_t RTDECL(ReduceInteger8Value)(const Descriptor &,
+    ValueReductionOperation<std::int64_t>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const std::int64_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger8DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<std::int64_t>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const std::int64_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger8DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<std::int64_t>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const std::int64_t *identity = nullptr, bool ordered = true);
 #ifdef __SIZEOF_INT128__
-common::int128_t RTDECL(ReduceInteger16)(const Descriptor &,
-    ReductionOperation<common::int128_t>, const char *source, int line,
+common::int128_t RTDECL(ReduceInteger16Ref)(const Descriptor &,
+    ReferenceReductionOperation<common::int128_t>, const char *source, int line,
     int dim = 0, const Descriptor *mask = nullptr,
     const common::int128_t *identity = nullptr, bool ordered = true);
-void RTDECL(ReduceInteger16Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<common::int128_t>, const char *source, int line, int dim,
-    const Descriptor *mask = nullptr,
+common::int128_t RTDECL(ReduceInteger16Value)(const Descriptor &,
+    ValueReductionOperation<common::int128_t>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const common::int128_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger16DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<common::int128_t>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const common::int128_t *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceInteger16DimValue)(Descriptor &result,
+    const Descriptor &array, ValueReductionOperation<common::int128_t>,
+    const char *source, int line, int dim, const Descriptor *mask = nullptr,
     const common::int128_t *identity = nullptr, bool ordered = true);
 #endif
 
 // REAL/COMPLEX(2 & 3) return 32-bit float results for the caller to downconvert
-float RTDECL(ReduceReal2)(const Descriptor &, ReductionOperation<float>,
-    const char *source, int line, int dim = 0, const Descriptor *mask = nullptr,
+float RTDECL(ReduceReal2Ref)(const Descriptor &,
+    ReferenceReductionOperation<float>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
     const float *identity = nullptr, bool ordered = true);
-void RTDECL(ReduceReal2Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<float>, const char *source, int line, int dim,
+float RTDECL(ReduceReal2Value)(const Descriptor &,
+    ValueReductionOperation<float>, const char *source, int line, int dim = 0,
     const Descriptor *mask = nullptr, const float *identity = nullptr,
     bool ordered = true);
-float RTDECL(ReduceReal3)(const Descriptor &, ReductionOperation<float>,
-    const char *source, int line, int dim = 0, const Descriptor *mask = nullptr,
+void RTDECL(ReduceReal2DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<float>, const char *source, int line, int dim,
+    const Descriptor *mask = nullptr, const float *identity = nullptr,
+    bool ordered = true);
+void RTDECL(ReduceReal2DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<float>, const char *source, int line, int dim,
+    const Descriptor *mask = nullptr, const float *identity = nullptr,
+    bool ordered = true);
+float RTDECL(ReduceReal3Ref)(const Descriptor &,
+    ReferenceReductionOperation<float>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
     const float *identity = nullptr, bool ordered = true);
-void RTDECL(ReduceReal3Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<float>, const char *source, int line, int dim,
+float RTDECL(ReduceReal3Value)(const Descriptor &,
+    ValueReductionOperation<float>, const char *source, int line, int dim = 0,
     const Descriptor *mask = nullptr, const float *identity = nullptr,
     bool ordered = true);
-float RTDECL(ReduceReal4)(const Descriptor &, ReductionOperation<float>,
-    const char *source, int line, int dim = 0, const Descriptor *mask = nullptr,
+void RTDECL(ReduceReal3DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<float>, const char *source, int line, int dim,
+    const Descriptor *mask = nullptr, const float *identity = nullptr,
+    bool ordered = true);
+void RTDECL(ReduceReal3DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<float>, const char *source, int line, int dim,
+    const Descriptor *mask = nullptr, const float *identity = nullptr,
+    bool ordered = true);
+float RTDECL(ReduceReal4Ref)(const Descriptor &,
+    ReferenceReductionOperation<float>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
     const float *identity = nullptr, bool ordered = true);
-void RTDECL(ReduceReal4Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<float>, const char *source, int line, int dim,
+float RTDECL(ReduceReal4Value)(const Descriptor &,
+    ValueReductionOperation<float>, const char *source, int line, int dim = 0,
     const Descriptor *mask = nullptr, const float *identity = nullptr,
     bool ordered = true);
-double RTDECL(ReduceReal8)(const Descriptor &, ReductionOperation<double>,
-    const char *source, int line, int dim = 0, const Descriptor *mask = nullptr,
+void RTDECL(ReduceReal4DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<float>, const char *source, int line, int dim,
+    const Descriptor *mask = nullptr, const float *identity = nullptr,
+    bool ordered = true);
+void RTDECL(ReduceReal4DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<float>, const char *source, int line, int dim,
+    const Descriptor *mask = nullptr, const float *identity = nullptr,
+    bool ordered = true);
+double RTDECL(ReduceReal8Ref)(const Descriptor &,
+    ReferenceReductionOperation<double>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
     const double *identity = nullptr, bool ordered = true);
-void RTDECL(ReduceReal8Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<double>, const char *source, int line, int dim,
+double RTDECL(ReduceReal8Value)(const Descriptor &,
+    ValueReductionOperation<double>, const char *source, int line, int dim = 0,
     const Descriptor *mask = nullptr, const double *identity = nullptr,
     bool ordered = true);
-#if LDBL_MANT_DIG == 64
-long double RTDECL(ReduceReal10)(const Descriptor &,
-    ReductionOperation<long double>, const char *source, int line, int dim = 0,
-    const Descriptor *mask = nullptr, const long double *identity = nullptr,
+void RTDECL(ReduceReal8DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<double>, const char *source, int line, int dim,
+    const Descriptor *mask = nullptr, const double *identity = nullptr,
     bool ordered = true);
-void RTDECL(ReduceReal10Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<long double>, const char *source, int line, int dim,
+void RTDECL(ReduceReal8DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<double>, const char *source, int line, int dim,
+    const Descriptor *mask = nullptr, const double *identity = nullptr,
+    bool ordered = true);
+#if LDBL_MANT_DIG == 64
+long double RTDECL(ReduceReal10Ref)(const Descriptor &,
+    ReferenceReductionOperation<long double>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const long double *identity = nullptr, bool ordered = true);
+long double RTDECL(ReduceReal10Value)(const Descriptor &,
+    ValueReductionOperation<long double>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const long double *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceReal10DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<long double>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const long double *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceReal10DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<long double>, const char *source, int line, int dim,
     const Descriptor *mask = nullptr, const long double *identity = nullptr,
     bool ordered = true);
 #endif
 #if LDBL_MANT_DIG == 113 || HAS_FLOAT128
-CppFloat128Type RTDECL(ReduceReal16)(const Descriptor &,
-    ReductionOperation<CppFloat128Type>, const char *source, int line,
+CppFloat128Type RTDECL(ReduceReal16Ref)(const Descriptor &,
+    ReferenceReductionOperation<CppFloat128Type>, const char *source, int line,
     int dim = 0, const Descriptor *mask = nullptr,
     const CppFloat128Type *identity = nullptr, bool ordered = true);
-void RTDECL(ReduceReal16Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<CppFloat128Type>, const char *source, int line, int dim,
-    const Descriptor *mask = nullptr, const CppFloat128Type *identity = nullptr,
-    bool ordered = true);
+CppFloat128Type RTDECL(ReduceReal16Value)(const Descriptor &,
+    ValueReductionOperation<CppFloat128Type>, const char *source, int line,
+    int dim = 0, const Descriptor *mask = nullptr,
+    const CppFloat128Type *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceReal16DimRef)(Descriptor &result, const Descriptor &array,
+    ReferenceReductionOperation<CppFloat128Type>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const CppFloat128Type *identity = nullptr, bool ordered = true);
+void RTDECL(ReduceReal16DimValue)(Descriptor &result, const Descriptor &array,
+    ValueReductionOperation<CppFloat128Type>, const char *source, int line,
+    int dim, const Descriptor *mask = nullptr,
+    const CppFloat128Type *identity = nullptr, bool ordered = true);
 #endif
 
-void RTDECL(CppReduceComplex2)(std::complex<float> &, const Descriptor &,
-    ReductionOperation<std::complex<float>>, const char *source, int line,
+void RTDECL(CppReduceComplex2Ref)(std::complex<float> &, const Descriptor &,
+    ReferenceReductionOperation<std::complex<float>>, const char *source,
+    int line, int dim = 0, const Descriptor *mask = nullptr,
+    const std::complex<float> *identity = nullptr, bool ordered = true);
+void RTDECL(CppReduceComplex2Value)(std::complex<float> &, const Descriptor &,
+    ValueReductionOperation<std::complex<float>>, const char *source, int line,
     int dim = 0, const Descriptor *mask = nullptr,
     const std::complex<float> *identity = nullptr, bool ordered = true);
-void RTDECL(CppReduceComplex2Dim)(Descriptor &result, const Descriptor &array,
-    ReductionOperation<std::complex<float>>, const char *source, int line,
-    int dim, const Descriptor *mask = nullptr,
+void RTDECL(CppReduceComplex2DimRef)(Descriptor &result,
+    const Descriptor &array, ReferenceReductionOperation<std::complex<float>>,
+    const char *source, int line, int dim, const Descriptor *mask = nullptr,
+    const std::complex<float> *identity = nullptr, bool ordered = true);
+void RTDECL(CppReduceComplex2DimValue)(Descriptor &result,
+    const Descriptor &array, ValueReductionOperation<std::complex<float>>,
+    const char *source, int line, int dim, const Descriptor *mask = nullptr,
     const std::complex<float> *identity = nullptr, bool ordered = true);
-void RTDECL(CppReduceComplex3)(std::complex<float> &, const Descriptor &,
-    ReductionOperation<std::complex<float>>, const char *source, int l...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/95297


More information about the flang-commits mailing list