[flang-commits] [flang] [flang][runtime] Runtime support for REDUCE() (PR #86214)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Fri Mar 22 08:22:40 PDT 2024


================
@@ -0,0 +1,526 @@
+//===-- runtime/reduce.cpp ------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REDUCE() implementation
+
+#include "flang/Runtime/reduce.h"
+#include "reduction-templates.h"
+#include "terminator.h"
+#include "tools.h"
+#include "flang/Runtime/descriptor.h"
+
+namespace Fortran::runtime {
+
+template <typename T> class ReduceAccumulator {
+public:
+  RT_API_ATTRS ReduceAccumulator(const Descriptor &array,
+      ReductionOperation<T> operation, const T *identity,
+      Terminator &terminator)
+      : array_{array}, operation_{operation}, identity_{identity},
+        terminator_{terminator} {}
+  RT_API_ATTRS void Reinitialize() { result_.reset(); }
+  template <typename A>
+  RT_API_ATTRS bool AccumulateAt(const SubscriptValue at[]) {
+    const auto *operand{array_.Element<A>(at)};
+    if (result_) {
+      result_ = operation_(&*result_, operand);
+    } else {
+      result_ = *operand;
+    }
+    return true;
+  }
+  template <typename A>
+  RT_API_ATTRS void GetResult(A *to, int /*zeroBasedDim*/ = -1) {
+    if (result_) {
+      *to = *result_;
+    } else if (identity_) {
+      *to = *identity_;
+    } else {
+      terminator_.Crash("REDUCE() without IDENTITY= has no result");
+    }
+  }
+
+private:
+  const Descriptor &array_;
+  std::optional<T> result_;
----------------
vzakhari wrote:

Ideally, yes, using `common::optional` is preferred.

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


More information about the flang-commits mailing list