Skip to content

Commit

Permalink
[flang] Support SELECTED_LOGICAL_KIND() up to lowering
Browse files Browse the repository at this point in the history
Process and fold the new F'2023 intrinsic function SELECTED_LOGICAL_KIND.

Differential Revision: https://reviews.llvm.org/D159039
  • Loading branch information
klausler committed Aug 29, 2023
1 parent f254bbf commit 8383d76
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions flang/include/flang/Evaluate/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TargetCharacteristics {
bool IsTypeEnabled(common::TypeCategory category, std::int64_t kind) const;

int SelectedIntKind(std::int64_t precision = 0) const;
int SelectedLogicalKind(std::int64_t bits = 1) const;
int SelectedRealKind(std::int64_t precision = 0, std::int64_t range = 0,
std::int64_t radix = 2) const;

Expand Down
4 changes: 4 additions & 0 deletions flang/lib/Evaluate/fold-integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,10 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
if (auto p{ToInt64(args[0])}) {
return Expr<T>{context.targetCharacteristics().SelectedIntKind(*p)};
}
} else if (name == "selected_logical_kind") {
if (auto p{ToInt64(args[0])}) {
return Expr<T>{context.targetCharacteristics().SelectedLogicalKind(*p)};
}
} else if (name == "selected_real_kind" ||
name == "__builtin_ieee_selected_real_kind") {
if (auto p{GetInt64ArgOr(args[0], 0)}) {
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Evaluate/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,8 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
Rank::scalar, IntrinsicClass::transformationalFunction},
{"selected_int_kind", {{"r", AnyInt, Rank::scalar}}, DefaultInt,
Rank::scalar, IntrinsicClass::transformationalFunction},
{"selected_logical_kind", {{"bits", AnyInt, Rank::scalar}}, DefaultInt,
Rank::scalar, IntrinsicClass::transformationalFunction},
{"selected_real_kind",
{{"p", AnyInt, Rank::scalar},
{"r", AnyInt, Rank::scalar, Optionality::optional},
Expand Down
30 changes: 30 additions & 0 deletions flang/lib/Evaluate/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,36 @@ int TargetCharacteristics::SelectedIntKind(std::int64_t precision) const {
}
}

// SELECTED_LOGICAL_KIND() -- F'2023 16.9.182
class SelectedLogicalKindVisitor {
public:
SelectedLogicalKindVisitor(
const TargetCharacteristics &targetCharacteristics, std::int64_t bits)
: targetCharacteristics_{targetCharacteristics}, bits_{bits} {}
using Result = std::optional<int>;
using Types = LogicalTypes;
template <typename T> Result Test() const {
if (Scalar<T>::bits >= bits_ &&
targetCharacteristics_.IsTypeEnabled(T::category, T::kind)) {
return T::kind;
} else {
return std::nullopt;
}
}

private:
const TargetCharacteristics &targetCharacteristics_;
std::int64_t bits_;
};

int TargetCharacteristics::SelectedLogicalKind(std::int64_t bits) const {
if (auto kind{common::SearchTypes(SelectedLogicalKindVisitor{*this, bits})}) {
return *kind;
} else {
return -1;
}
}

// SELECTED_REAL_KIND() -- F'2018 16.9.170
class SelectedRealKindVisitor {
public:
Expand Down
17 changes: 17 additions & 0 deletions flang/test/Evaluate/fold-selected_logical_kind.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
! RUN: %python %S/test_folding.py %s %flang_fc1
module m
logical, parameter :: test_0 = selected_logical_kind( 0) == 1
logical, parameter :: test_1 = selected_logical_kind( 1) == 1
logical, parameter :: test_7 = selected_logical_kind( 7) == 1
logical, parameter :: test_8 = selected_logical_kind( 8) == 1
logical, parameter :: test_9 = selected_logical_kind( 9) == 2
logical, parameter :: test_15 = selected_logical_kind(15) == 2
logical, parameter :: test_16 = selected_logical_kind(16) == 2
logical, parameter :: test_17 = selected_logical_kind(17) == 4
logical, parameter :: test_31 = selected_logical_kind(31) == 4
logical, parameter :: test_32 = selected_logical_kind(32) == 4
logical, parameter :: test_33 = selected_logical_kind(33) == 8
logical, parameter :: test_63 = selected_logical_kind(63) == 8
logical, parameter :: test_64 = selected_logical_kind(64) == 8
logical, parameter :: test_65 = selected_logical_kind(65) == -1
end

0 comments on commit 8383d76

Please sign in to comment.