Skip to content

Commit fc9ce03

Browse files
authored
[flang][rt] Enable Count and CountDim for device build (#141684)
1 parent a41e20a commit fc9ce03

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

flang-rt/lib/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ set(supported_sources
5656
product.cpp
5757
pseudo-unit.cpp
5858
ragged.cpp
59+
reduction.cpp
5960
stat.cpp
6061
stop.cpp
6162
sum.cpp

flang-rt/lib/runtime/reduction.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,13 @@ enum class LogicalReduction { All, Any, Parity };
223223
template <LogicalReduction REDUCTION> class LogicalAccumulator {
224224
public:
225225
using Type = bool;
226-
explicit LogicalAccumulator(const Descriptor &array) : array_{array} {}
227-
void Reinitialize() { result_ = REDUCTION == LogicalReduction::All; }
228-
bool Result() const { return result_; }
229-
bool Accumulate(bool x) {
226+
RT_API_ATTRS explicit LogicalAccumulator(const Descriptor &array)
227+
: array_{array} {}
228+
RT_API_ATTRS void Reinitialize() {
229+
result_ = REDUCTION == LogicalReduction::All;
230+
}
231+
RT_API_ATTRS bool Result() const { return result_; }
232+
RT_API_ATTRS bool Accumulate(bool x) {
230233
if constexpr (REDUCTION == LogicalReduction::Parity) {
231234
result_ = result_ != x;
232235
} else if (x != (REDUCTION == LogicalReduction::All)) {
@@ -236,7 +239,7 @@ template <LogicalReduction REDUCTION> class LogicalAccumulator {
236239
return true;
237240
}
238241
template <typename IGNORED = void>
239-
bool AccumulateAt(const SubscriptValue at[]) {
242+
RT_API_ATTRS bool AccumulateAt(const SubscriptValue at[]) {
240243
return Accumulate(IsLogicalElementTrue(array_, at));
241244
}
242245

@@ -246,9 +249,9 @@ template <LogicalReduction REDUCTION> class LogicalAccumulator {
246249
};
247250

248251
template <typename ACCUMULATOR>
249-
inline auto GetTotalLogicalReduction(const Descriptor &x, const char *source,
250-
int line, int dim, ACCUMULATOR &&accumulator, const char *intrinsic) ->
251-
typename ACCUMULATOR::Type {
252+
RT_API_ATTRS inline auto GetTotalLogicalReduction(const Descriptor &x,
253+
const char *source, int line, int dim, ACCUMULATOR &&accumulator,
254+
const char *intrinsic) -> typename ACCUMULATOR::Type {
252255
Terminator terminator{source, line};
253256
if (dim < 0 || dim > 1) {
254257
terminator.Crash("%s: bad DIM=%d for ARRAY with rank=1", intrinsic, dim);
@@ -264,8 +267,9 @@ inline auto GetTotalLogicalReduction(const Descriptor &x, const char *source,
264267
}
265268

266269
template <typename ACCUMULATOR>
267-
inline auto ReduceLogicalDimToScalar(const Descriptor &x, int zeroBasedDim,
268-
SubscriptValue subscripts[]) -> typename ACCUMULATOR::Type {
270+
RT_API_ATTRS inline auto ReduceLogicalDimToScalar(
271+
const Descriptor &x, int zeroBasedDim, SubscriptValue subscripts[]) ->
272+
typename ACCUMULATOR::Type {
269273
ACCUMULATOR accumulator{x};
270274
SubscriptValue xAt[maxRank];
271275
GetExpandedSubscripts(xAt, x, zeroBasedDim, subscripts);
@@ -282,8 +286,8 @@ inline auto ReduceLogicalDimToScalar(const Descriptor &x, int zeroBasedDim,
282286

283287
template <LogicalReduction REDUCTION> struct LogicalReduceHelper {
284288
template <int KIND> struct Functor {
285-
void operator()(Descriptor &result, const Descriptor &x, int dim,
286-
Terminator &terminator, const char *intrinsic) const {
289+
RT_API_ATTRS void operator()(Descriptor &result, const Descriptor &x,
290+
int dim, Terminator &terminator, const char *intrinsic) const {
287291
// Standard requires result to have same LOGICAL kind as argument.
288292
CreatePartialReductionResult(
289293
result, x, x.ElementBytes(), dim, terminator, intrinsic, x.type());
@@ -301,8 +305,9 @@ template <LogicalReduction REDUCTION> struct LogicalReduceHelper {
301305
};
302306

303307
template <LogicalReduction REDUCTION>
304-
inline void DoReduceLogicalDimension(Descriptor &result, const Descriptor &x,
305-
int dim, Terminator &terminator, const char *intrinsic) {
308+
RT_API_ATTRS inline void DoReduceLogicalDimension(Descriptor &result,
309+
const Descriptor &x, int dim, Terminator &terminator,
310+
const char *intrinsic) {
306311
auto catKind{x.type().GetCategoryAndKind()};
307312
RUNTIME_CHECK(terminator, catKind && catKind->first == TypeCategory::Logical);
308313
ApplyLogicalKind<LogicalReduceHelper<REDUCTION>::template Functor, void>(
@@ -314,11 +319,12 @@ inline void DoReduceLogicalDimension(Descriptor &result, const Descriptor &x,
314319
class CountAccumulator {
315320
public:
316321
using Type = std::int64_t;
317-
explicit CountAccumulator(const Descriptor &array) : array_{array} {}
318-
void Reinitialize() { result_ = 0; }
319-
Type Result() const { return result_; }
322+
RT_API_ATTRS explicit CountAccumulator(const Descriptor &array)
323+
: array_{array} {}
324+
RT_API_ATTRS void Reinitialize() { result_ = 0; }
325+
RT_API_ATTRS Type Result() const { return result_; }
320326
template <typename IGNORED = void>
321-
bool AccumulateAt(const SubscriptValue at[]) {
327+
RT_API_ATTRS bool AccumulateAt(const SubscriptValue at[]) {
322328
if (IsLogicalElementTrue(array_, at)) {
323329
++result_;
324330
}
@@ -331,7 +337,7 @@ class CountAccumulator {
331337
};
332338

333339
template <int KIND> struct CountDimension {
334-
void operator()(Descriptor &result, const Descriptor &x, int dim,
340+
RT_API_ATTRS void operator()(Descriptor &result, const Descriptor &x, int dim,
335341
Terminator &terminator) const {
336342
// Element size of the descriptor descriptor is the size
337343
// of {TypeCategory::Integer, KIND}.

0 commit comments

Comments
 (0)