[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
//===-- runtime/reduction.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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-06-16 06:19:51 +08:00
|
|
|
// Implements ALL, ANY, COUNT, IALL, IANY, IPARITY, & PARITY for all required
|
|
|
|
// operand types and shapes.
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
//
|
2021-06-16 06:18:41 +08:00
|
|
|
// DOT_PRODUCT, FINDLOC, MATMUL, SUM, and PRODUCT are in their own eponymous
|
|
|
|
// source files.
|
|
|
|
// NORM2, MAXLOC, MINLOC, MAXVAL, and MINVAL are in extrema.cpp.
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/Runtime/reduction.h"
|
2021-04-23 05:23:45 +08:00
|
|
|
#include "reduction-templates.h"
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
namespace Fortran::runtime {
|
|
|
|
|
2021-06-16 06:19:51 +08:00
|
|
|
// IALL, IANY, IPARITY
|
|
|
|
|
|
|
|
template <typename INTERMEDIATE> class IntegerAndAccumulator {
|
|
|
|
public:
|
|
|
|
explicit IntegerAndAccumulator(const Descriptor &array) : array_{array} {}
|
|
|
|
void Reinitialize() { and_ = ~INTERMEDIATE{0}; }
|
|
|
|
template <typename A> void GetResult(A *p, int /*zeroBasedDim*/ = -1) const {
|
|
|
|
*p = static_cast<A>(and_);
|
|
|
|
}
|
|
|
|
template <typename A> bool AccumulateAt(const SubscriptValue at[]) {
|
|
|
|
and_ &= *array_.Element<A>(at);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Descriptor &array_;
|
|
|
|
INTERMEDIATE and_{~INTERMEDIATE{0}};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename INTERMEDIATE> class IntegerOrAccumulator {
|
|
|
|
public:
|
|
|
|
explicit IntegerOrAccumulator(const Descriptor &array) : array_{array} {}
|
|
|
|
void Reinitialize() { or_ = 0; }
|
|
|
|
template <typename A> void GetResult(A *p, int /*zeroBasedDim*/ = -1) const {
|
|
|
|
*p = static_cast<A>(or_);
|
|
|
|
}
|
|
|
|
template <typename A> bool AccumulateAt(const SubscriptValue at[]) {
|
|
|
|
or_ |= *array_.Element<A>(at);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Descriptor &array_;
|
|
|
|
INTERMEDIATE or_{0};
|
|
|
|
};
|
2021-04-21 00:19:21 +08:00
|
|
|
|
|
|
|
template <typename INTERMEDIATE> class IntegerXorAccumulator {
|
|
|
|
public:
|
|
|
|
explicit IntegerXorAccumulator(const Descriptor &array) : array_{array} {}
|
|
|
|
void Reinitialize() { xor_ = 0; }
|
|
|
|
template <typename A> void GetResult(A *p, int /*zeroBasedDim*/ = -1) const {
|
|
|
|
*p = static_cast<A>(xor_);
|
|
|
|
}
|
|
|
|
template <typename A> bool AccumulateAt(const SubscriptValue at[]) {
|
|
|
|
xor_ ^= *array_.Element<A>(at);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Descriptor &array_;
|
|
|
|
INTERMEDIATE xor_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
2021-06-16 06:19:51 +08:00
|
|
|
CppTypeFor<TypeCategory::Integer, 1> RTNAME(IAll1)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 1>(x, source, line, dim, mask,
|
|
|
|
IntegerAndAccumulator<CppTypeFor<TypeCategory::Integer, 4>>{x}, "IALL");
|
|
|
|
}
|
|
|
|
CppTypeFor<TypeCategory::Integer, 2> RTNAME(IAll2)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 2>(x, source, line, dim, mask,
|
|
|
|
IntegerAndAccumulator<CppTypeFor<TypeCategory::Integer, 4>>{x}, "IALL");
|
|
|
|
}
|
|
|
|
CppTypeFor<TypeCategory::Integer, 4> RTNAME(IAll4)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 4>(x, source, line, dim, mask,
|
|
|
|
IntegerAndAccumulator<CppTypeFor<TypeCategory::Integer, 4>>{x}, "IALL");
|
|
|
|
}
|
|
|
|
CppTypeFor<TypeCategory::Integer, 8> RTNAME(IAll8)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 8>(x, source, line, dim, mask,
|
|
|
|
IntegerAndAccumulator<CppTypeFor<TypeCategory::Integer, 8>>{x}, "IALL");
|
|
|
|
}
|
|
|
|
#ifdef __SIZEOF_INT128__
|
|
|
|
CppTypeFor<TypeCategory::Integer, 16> RTNAME(IAll16)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 16>(x, source, line, dim,
|
|
|
|
mask, IntegerAndAccumulator<CppTypeFor<TypeCategory::Integer, 16>>{x},
|
|
|
|
"IALL");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
void RTNAME(IAllDim)(Descriptor &result, const Descriptor &x, int dim,
|
|
|
|
const char *source, int line, const Descriptor *mask) {
|
|
|
|
Terminator terminator{source, line};
|
|
|
|
auto catKind{x.type().GetCategoryAndKind()};
|
|
|
|
RUNTIME_CHECK(terminator,
|
|
|
|
catKind.has_value() && catKind->first == TypeCategory::Integer);
|
|
|
|
PartialIntegerReduction<IntegerAndAccumulator>(
|
|
|
|
result, x, dim, catKind->second, mask, "IALL", terminator);
|
|
|
|
}
|
|
|
|
|
|
|
|
CppTypeFor<TypeCategory::Integer, 1> RTNAME(IAny1)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 1>(x, source, line, dim, mask,
|
|
|
|
IntegerOrAccumulator<CppTypeFor<TypeCategory::Integer, 4>>{x}, "IANY");
|
|
|
|
}
|
|
|
|
CppTypeFor<TypeCategory::Integer, 2> RTNAME(IAny2)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 2>(x, source, line, dim, mask,
|
|
|
|
IntegerOrAccumulator<CppTypeFor<TypeCategory::Integer, 4>>{x}, "IANY");
|
|
|
|
}
|
|
|
|
CppTypeFor<TypeCategory::Integer, 4> RTNAME(IAny4)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 4>(x, source, line, dim, mask,
|
|
|
|
IntegerOrAccumulator<CppTypeFor<TypeCategory::Integer, 4>>{x}, "IANY");
|
|
|
|
}
|
|
|
|
CppTypeFor<TypeCategory::Integer, 8> RTNAME(IAny8)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 8>(x, source, line, dim, mask,
|
|
|
|
IntegerOrAccumulator<CppTypeFor<TypeCategory::Integer, 8>>{x}, "IANY");
|
|
|
|
}
|
|
|
|
#ifdef __SIZEOF_INT128__
|
|
|
|
CppTypeFor<TypeCategory::Integer, 16> RTNAME(IAny16)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 16>(x, source, line, dim,
|
|
|
|
mask, IntegerOrAccumulator<CppTypeFor<TypeCategory::Integer, 16>>{x},
|
|
|
|
"IANY");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
void RTNAME(IAnyDim)(Descriptor &result, const Descriptor &x, int dim,
|
|
|
|
const char *source, int line, const Descriptor *mask) {
|
|
|
|
Terminator terminator{source, line};
|
|
|
|
auto catKind{x.type().GetCategoryAndKind()};
|
|
|
|
RUNTIME_CHECK(terminator,
|
|
|
|
catKind.has_value() && catKind->first == TypeCategory::Integer);
|
|
|
|
PartialIntegerReduction<IntegerOrAccumulator>(
|
|
|
|
result, x, dim, catKind->second, mask, "IANY", terminator);
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:19:21 +08:00
|
|
|
CppTypeFor<TypeCategory::Integer, 1> RTNAME(IParity1)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 1>(x, source, line, dim, mask,
|
|
|
|
IntegerXorAccumulator<CppTypeFor<TypeCategory::Integer, 4>>{x},
|
|
|
|
"IPARITY");
|
|
|
|
}
|
|
|
|
CppTypeFor<TypeCategory::Integer, 2> RTNAME(IParity2)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 2>(x, source, line, dim, mask,
|
|
|
|
IntegerXorAccumulator<CppTypeFor<TypeCategory::Integer, 4>>{x},
|
|
|
|
"IPARITY");
|
|
|
|
}
|
|
|
|
CppTypeFor<TypeCategory::Integer, 4> RTNAME(IParity4)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 4>(x, source, line, dim, mask,
|
|
|
|
IntegerXorAccumulator<CppTypeFor<TypeCategory::Integer, 4>>{x},
|
|
|
|
"IPARITY");
|
|
|
|
}
|
|
|
|
CppTypeFor<TypeCategory::Integer, 8> RTNAME(IParity8)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 8>(x, source, line, dim, mask,
|
|
|
|
IntegerXorAccumulator<CppTypeFor<TypeCategory::Integer, 8>>{x},
|
|
|
|
"IPARITY");
|
|
|
|
}
|
|
|
|
#ifdef __SIZEOF_INT128__
|
|
|
|
CppTypeFor<TypeCategory::Integer, 16> RTNAME(IParity16)(const Descriptor &x,
|
|
|
|
const char *source, int line, int dim, const Descriptor *mask) {
|
|
|
|
return GetTotalReduction<TypeCategory::Integer, 16>(x, source, line, dim,
|
|
|
|
mask, IntegerXorAccumulator<CppTypeFor<TypeCategory::Integer, 16>>{x},
|
|
|
|
"IPARITY");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
void RTNAME(IParityDim)(Descriptor &result, const Descriptor &x, int dim,
|
|
|
|
const char *source, int line, const Descriptor *mask) {
|
|
|
|
Terminator terminator{source, line};
|
|
|
|
auto catKind{x.type().GetCategoryAndKind()};
|
|
|
|
RUNTIME_CHECK(terminator,
|
|
|
|
catKind.has_value() && catKind->first == TypeCategory::Integer);
|
|
|
|
PartialIntegerReduction<IntegerXorAccumulator>(
|
|
|
|
result, x, dim, catKind->second, mask, "IPARITY", terminator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ALL, ANY, COUNT, & PARITY
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
|
2021-04-21 00:19:21 +08:00
|
|
|
enum class LogicalReduction { All, Any, Parity };
|
|
|
|
|
|
|
|
template <LogicalReduction REDUCTION> class LogicalAccumulator {
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
public:
|
|
|
|
using Type = bool;
|
|
|
|
explicit LogicalAccumulator(const Descriptor &array) : array_{array} {}
|
2021-04-21 00:19:21 +08:00
|
|
|
void Reinitialize() { result_ = REDUCTION == LogicalReduction::All; }
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
bool Result() const { return result_; }
|
|
|
|
bool Accumulate(bool x) {
|
2021-04-21 00:19:21 +08:00
|
|
|
if constexpr (REDUCTION == LogicalReduction::Parity) {
|
|
|
|
result_ = result_ != x;
|
|
|
|
} else if (x != (REDUCTION == LogicalReduction::All)) {
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
result_ = x;
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-21 00:19:21 +08:00
|
|
|
return true;
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
}
|
|
|
|
template <typename IGNORED = void>
|
|
|
|
bool AccumulateAt(const SubscriptValue at[]) {
|
|
|
|
return Accumulate(IsLogicalElementTrue(array_, at));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Descriptor &array_;
|
2021-04-21 00:19:21 +08:00
|
|
|
bool result_{REDUCTION == LogicalReduction::All};
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ACCUMULATOR>
|
|
|
|
inline auto GetTotalLogicalReduction(const Descriptor &x, const char *source,
|
|
|
|
int line, int dim, ACCUMULATOR &&accumulator, const char *intrinsic) ->
|
|
|
|
typename ACCUMULATOR::Type {
|
|
|
|
Terminator terminator{source, line};
|
|
|
|
if (dim < 0 || dim > 1) {
|
|
|
|
terminator.Crash("%s: bad DIM=%d", intrinsic, dim);
|
|
|
|
}
|
|
|
|
SubscriptValue xAt[maxRank];
|
|
|
|
x.GetLowerBounds(xAt);
|
|
|
|
for (auto elements{x.Elements()}; elements--; x.IncrementSubscripts(xAt)) {
|
|
|
|
if (!accumulator.AccumulateAt(xAt)) {
|
|
|
|
break; // cut short, result is known
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accumulator.Result();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ACCUMULATOR>
|
|
|
|
inline auto ReduceLogicalDimToScalar(const Descriptor &x, int zeroBasedDim,
|
|
|
|
SubscriptValue subscripts[]) -> typename ACCUMULATOR::Type {
|
|
|
|
ACCUMULATOR accumulator{x};
|
|
|
|
SubscriptValue xAt[maxRank];
|
|
|
|
GetExpandedSubscripts(xAt, x, zeroBasedDim, subscripts);
|
|
|
|
const auto &dim{x.GetDimension(zeroBasedDim)};
|
|
|
|
SubscriptValue at{dim.LowerBound()};
|
|
|
|
for (auto n{dim.Extent()}; n-- > 0; ++at) {
|
|
|
|
xAt[zeroBasedDim] = at;
|
|
|
|
if (!accumulator.AccumulateAt(xAt)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accumulator.Result();
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:19:21 +08:00
|
|
|
template <LogicalReduction REDUCTION> struct LogicalReduceHelper {
|
|
|
|
template <int KIND> struct Functor {
|
|
|
|
void operator()(Descriptor &result, const Descriptor &x, int dim,
|
|
|
|
Terminator &terminator, const char *intrinsic) const {
|
|
|
|
// Standard requires result to have same LOGICAL kind as argument.
|
|
|
|
CreatePartialReductionResult(
|
|
|
|
result, x, dim, terminator, intrinsic, x.type());
|
|
|
|
SubscriptValue at[maxRank];
|
|
|
|
result.GetLowerBounds(at);
|
Fix runtime internal error with certain intrinsics that can take a scalar
result descriptor (e.g., maxloc, minloc, maxval, minval, all, any, count,
parity, findloc, etc.)
Also add a scalar case for these intrinsic unit tests.
Differential Revision: https://reviews.llvm.org/D106820
2021-07-30 04:05:08 +08:00
|
|
|
INTERNAL_CHECK(result.rank() == 0 || at[0] == 1);
|
2021-04-21 00:19:21 +08:00
|
|
|
using CppType = CppTypeFor<TypeCategory::Logical, KIND>;
|
|
|
|
for (auto n{result.Elements()}; n-- > 0; result.IncrementSubscripts(at)) {
|
|
|
|
*result.Element<CppType>(at) =
|
|
|
|
ReduceLogicalDimToScalar<LogicalAccumulator<REDUCTION>>(
|
|
|
|
x, dim - 1, at);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
|
2021-04-21 00:19:21 +08:00
|
|
|
template <LogicalReduction REDUCTION>
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
inline void DoReduceLogicalDimension(Descriptor &result, const Descriptor &x,
|
|
|
|
int dim, Terminator &terminator, const char *intrinsic) {
|
|
|
|
auto catKind{x.type().GetCategoryAndKind()};
|
|
|
|
RUNTIME_CHECK(terminator, catKind && catKind->first == TypeCategory::Logical);
|
2021-04-21 00:19:21 +08:00
|
|
|
ApplyLogicalKind<LogicalReduceHelper<REDUCTION>::template Functor, void>(
|
|
|
|
catKind->second, terminator, result, x, dim, terminator, intrinsic);
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// COUNT
|
|
|
|
|
|
|
|
class CountAccumulator {
|
|
|
|
public:
|
|
|
|
using Type = std::int64_t;
|
|
|
|
explicit CountAccumulator(const Descriptor &array) : array_{array} {}
|
2021-04-21 00:19:21 +08:00
|
|
|
void Reinitialize() { result_ = 0; }
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
Type Result() const { return result_; }
|
|
|
|
template <typename IGNORED = void>
|
|
|
|
bool AccumulateAt(const SubscriptValue at[]) {
|
|
|
|
if (IsLogicalElementTrue(array_, at)) {
|
|
|
|
++result_;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Descriptor &array_;
|
|
|
|
Type result_{0};
|
|
|
|
};
|
|
|
|
|
2021-04-21 00:19:21 +08:00
|
|
|
template <int KIND> struct CountDimension {
|
|
|
|
void operator()(Descriptor &result, const Descriptor &x, int dim,
|
|
|
|
Terminator &terminator) const {
|
|
|
|
CreatePartialReductionResult(result, x, dim, terminator, "COUNT",
|
|
|
|
TypeCode{TypeCategory::Integer, KIND});
|
|
|
|
SubscriptValue at[maxRank];
|
|
|
|
result.GetLowerBounds(at);
|
Fix runtime internal error with certain intrinsics that can take a scalar
result descriptor (e.g., maxloc, minloc, maxval, minval, all, any, count,
parity, findloc, etc.)
Also add a scalar case for these intrinsic unit tests.
Differential Revision: https://reviews.llvm.org/D106820
2021-07-30 04:05:08 +08:00
|
|
|
INTERNAL_CHECK(result.rank() == 0 || at[0] == 1);
|
2021-04-21 00:19:21 +08:00
|
|
|
using CppType = CppTypeFor<TypeCategory::Integer, KIND>;
|
|
|
|
for (auto n{result.Elements()}; n-- > 0; result.IncrementSubscripts(at)) {
|
|
|
|
*result.Element<CppType>(at) =
|
|
|
|
ReduceLogicalDimToScalar<CountAccumulator>(x, dim - 1, at);
|
|
|
|
}
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
}
|
2021-04-21 00:19:21 +08:00
|
|
|
};
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
bool RTNAME(All)(const Descriptor &x, const char *source, int line, int dim) {
|
2021-04-21 00:19:21 +08:00
|
|
|
return GetTotalLogicalReduction(x, source, line, dim,
|
|
|
|
LogicalAccumulator<LogicalReduction::All>{x}, "ALL");
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
}
|
|
|
|
void RTNAME(AllDim)(Descriptor &result, const Descriptor &x, int dim,
|
|
|
|
const char *source, int line) {
|
|
|
|
Terminator terminator{source, line};
|
2021-04-21 00:19:21 +08:00
|
|
|
DoReduceLogicalDimension<LogicalReduction::All>(
|
|
|
|
result, x, dim, terminator, "ALL");
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RTNAME(Any)(const Descriptor &x, const char *source, int line, int dim) {
|
2021-04-21 00:19:21 +08:00
|
|
|
return GetTotalLogicalReduction(x, source, line, dim,
|
|
|
|
LogicalAccumulator<LogicalReduction::Any>{x}, "ANY");
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
}
|
|
|
|
void RTNAME(AnyDim)(Descriptor &result, const Descriptor &x, int dim,
|
|
|
|
const char *source, int line) {
|
|
|
|
Terminator terminator{source, line};
|
2021-04-21 00:19:21 +08:00
|
|
|
DoReduceLogicalDimension<LogicalReduction::Any>(
|
|
|
|
result, x, dim, terminator, "ANY");
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::int64_t RTNAME(Count)(
|
|
|
|
const Descriptor &x, const char *source, int line, int dim) {
|
|
|
|
return GetTotalLogicalReduction(
|
|
|
|
x, source, line, dim, CountAccumulator{x}, "COUNT");
|
|
|
|
}
|
2021-04-21 00:19:21 +08:00
|
|
|
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
void RTNAME(CountDim)(Descriptor &result, const Descriptor &x, int dim,
|
|
|
|
int kind, const char *source, int line) {
|
|
|
|
Terminator terminator{source, line};
|
2021-04-21 00:19:21 +08:00
|
|
|
ApplyIntegerKind<CountDimension, void>(
|
|
|
|
kind, terminator, result, x, dim, terminator);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RTNAME(Parity)(
|
|
|
|
const Descriptor &x, const char *source, int line, int dim) {
|
|
|
|
return GetTotalLogicalReduction(x, source, line, dim,
|
|
|
|
LogicalAccumulator<LogicalReduction::Parity>{x}, "PARITY");
|
|
|
|
}
|
|
|
|
void RTNAME(ParityDim)(Descriptor &result, const Descriptor &x, int dim,
|
|
|
|
const char *source, int line) {
|
|
|
|
Terminator terminator{source, line};
|
|
|
|
DoReduceLogicalDimension<LogicalReduction::Parity>(
|
|
|
|
result, x, dim, terminator, "PARITY");
|
[flang] Implement reductions in the runtime
Add runtime APIs, implementations, and tests for ALL, ANY, COUNT,
MAXLOC, MAXVAL, MINLOC, MINVAL, PRODUCT, and SUM reduction
transformantional intrinsic functions for all relevant argument
and result types and kinds, both without DIM= arguments
(total reductions) and with (partial reductions).
Complex-valued reductions have their APIs in C so that
C's _Complex types can be used for their results.
Some infrastructure work was also necessary or noticed:
* Usage of "long double" in the compiler was cleaned up a
bit, and host dependences on x86 / MSVC have been isolated
in a new Common/long-double header.
* Character comparison has been exposed via an extern template
so that reductions could use it.
* Mappings from Fortran type category/kind to host C++ types
and vice versa have been isolated into runtime/cpp-type.h and
then used throughout the runtime as appropriate.
* The portable 128-bit integer package in Common/uint128.h
was generalized to support signed comparisons.
* Bugs in descriptor indexing code were fixed.
Differential Revision: https://reviews.llvm.org/D99666
2021-04-01 00:14:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // extern "C"
|
|
|
|
} // namespace Fortran::runtime
|