Skip to content

[flang] Modifications to ieee_support_standard #128895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions flang/docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,15 @@ end
have this capability. An Arm executable will run on either type of
processor, so it is effectively unknown at compile time whether or
not this support will be available at runtime. The standard requires
that a call to intrinsic module procedure `IEEE_SUPPORT_HALTING` with
that a call to intrinsic module procedure `ieee_support_halting` with
a constant argument has a compile time constant result in `constant
expression` and `specification expression` contexts. In compilations
where this information is not known at compile time, f18 generates code
to determine the absence or presence of this capability at runtime.
A call to `IEEE_SUPPORT_HALTING` in contexts that the standard requires
to be constant will generate a compilation error.
A call to `ieee_support_halting` in contexts that the standard requires
to be constant will generate a compilation error. `ieee_support_standard`
depends in part on `ieee_support_halting`, so this also applies to
`ieee_support_standard` calls.

## Extensions, deletions, and legacy features supported by default

Expand Down
5 changes: 4 additions & 1 deletion flang/include/flang/Optimizer/Builder/IntrinsicCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ struct IntrinsicLibrary {
llvm::ArrayRef<fir::ExtendedValue>);
fir::ExtendedValue genIeeeSupportHalting(mlir::Type,
llvm::ArrayRef<fir::ExtendedValue>);
mlir::Value genIeeeSupportRounding(mlir::Type, llvm::ArrayRef<mlir::Value>);
fir::ExtendedValue genIeeeSupportRounding(mlir::Type,
llvm::ArrayRef<fir::ExtendedValue>);
fir::ExtendedValue genIeeeSupportStandard(mlir::Type,
llvm::ArrayRef<fir::ExtendedValue>);
template <mlir::arith::CmpIPredicate pred>
mlir::Value genIeeeTypeCompare(mlir::Type, llvm::ArrayRef<mlir::Value>);
mlir::Value genIeeeUnordered(mlir::Type, llvm::ArrayRef<mlir::Value>);
Expand Down
10 changes: 7 additions & 3 deletions flang/lib/Evaluate/fold-logical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
IeeeFeature::Flags)};
} else if (name == "__builtin_ieee_support_halting") {
if (!context.targetCharacteristics()
.haltingSupportIsUnknownAtCompileTime()) {
.haltingSupportIsUnknownAtCompileTime()) {
return Expr<T>{context.targetCharacteristics().ieeeFeatures().test(
IeeeFeature::Halting)};
}
Expand All @@ -906,8 +906,12 @@ Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
return Expr<T>{
context.targetCharacteristics().ieeeFeatures().test(IeeeFeature::Sqrt)};
} else if (name == "__builtin_ieee_support_standard") {
return Expr<T>{context.targetCharacteristics().ieeeFeatures().test(
IeeeFeature::Standard)};
// ieee_support_standard depends in part on ieee_support_halting.
if (!context.targetCharacteristics()
.haltingSupportIsUnknownAtCompileTime()) {
return Expr<T>{context.targetCharacteristics().ieeeFeatures().test(
IeeeFeature::Standard)};
}
} else if (name == "__builtin_ieee_support_subnormal") {
return Expr<T>{context.targetCharacteristics().ieeeFeatures().test(
IeeeFeature::Subnormal)};
Expand Down
34 changes: 12 additions & 22 deletions flang/lib/Evaluate/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1022,45 +1022,35 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
{"__builtin_ieee_next_up", {{"x", SameReal}}, SameReal},
{"__builtin_ieee_real", {{"a", AnyIntOrReal}, DefaultingKIND}, KINDReal},
{"__builtin_ieee_support_datatype",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_ieee_support_denormal",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_ieee_support_divide",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_ieee_support_flag",
{{"flag", IeeeFlagType, Rank::scalar},
{"x", AnyReal, Rank::elemental, Optionality::optional}},
{"x", AnyReal, Rank::known, Optionality::optional}},
DefaultLogical},
{"__builtin_ieee_support_halting", {{"flag", IeeeFlagType, Rank::scalar}},
DefaultLogical},
{"__builtin_ieee_support_inf",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_ieee_support_io",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_ieee_support_nan",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_ieee_support_rounding",
{{"round_value", IeeeRoundType, Rank::scalar},
{"x", AnyReal, Rank::elemental, Optionality::optional}},
{"x", AnyReal, Rank::known, Optionality::optional}},
DefaultLogical},
{"__builtin_ieee_support_sqrt",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_ieee_support_standard",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_ieee_support_subnormal",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_ieee_support_underflow_control",
{{"x", AnyReal, Rank::elemental, Optionality::optional}},
DefaultLogical},
{{"x", AnyReal, Rank::known, Optionality::optional}}, DefaultLogical},
{"__builtin_numeric_storage_size", {}, DefaultInt},
};

Expand Down
36 changes: 29 additions & 7 deletions flang/lib/Optimizer/Builder/IntrinsicCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,18 @@ static constexpr IntrinsicHandler handlers[]{
&I::genIeeeSupportFlag,
{{{"flag", asValue}, {"x", asInquired, handleDynamicOptional}}},
/*isElemental=*/false},
{"ieee_support_halting", &I::genIeeeSupportHalting},
{"ieee_support_rounding", &I::genIeeeSupportRounding},
{"ieee_support_halting",
&I::genIeeeSupportHalting,
{{{"flag", asValue}}},
/*isElemental=*/false},
{"ieee_support_rounding",
&I::genIeeeSupportRounding,
{{{"round_value", asValue}, {"x", asInquired, handleDynamicOptional}}},
/*isElemental=*/false},
{"ieee_support_standard",
&I::genIeeeSupportStandard,
{{{"flag", asValue}, {"x", asInquired, handleDynamicOptional}}},
/*isElemental=*/false},
{"ieee_unordered", &I::genIeeeUnordered},
{"ieee_value", &I::genIeeeValue},
{"ieor", &I::genIeor},
Expand Down Expand Up @@ -5613,7 +5623,7 @@ IntrinsicLibrary::genIeeeSupportFlag(mlir::Type resultType,
// is therefore ignored. Standard flags are all supported. The nonstandard
// DENORM extension is not supported, at least for now.
assert(args.size() == 1 || args.size() == 2);
auto [fieldRef, fieldTy] = getFieldRef(builder, loc, fir::getBase(args[0]));
auto [fieldRef, fieldTy] = getFieldRef(builder, loc, getBase(args[0]));
mlir::Value flag = builder.create<fir::LoadOp>(loc, fieldRef);
mlir::Value mask = builder.createIntegerConstant( // values are powers of 2
loc, fieldTy,
Expand Down Expand Up @@ -5645,9 +5655,8 @@ fir::ExtendedValue IntrinsicLibrary::genIeeeSupportHalting(
}

// IEEE_SUPPORT_ROUNDING
mlir::Value
IntrinsicLibrary::genIeeeSupportRounding(mlir::Type resultType,
llvm::ArrayRef<mlir::Value> args) {
fir::ExtendedValue IntrinsicLibrary::genIeeeSupportRounding(
mlir::Type resultType, llvm::ArrayRef<fir::ExtendedValue> args) {
// Check if floating point rounding mode ROUND_VALUE is supported.
// Rounding is supported either for all type kinds or none.
// An optional X kind argument is therefore ignored.
Expand All @@ -5658,7 +5667,7 @@ IntrinsicLibrary::genIeeeSupportRounding(mlir::Type resultType,
// 3 - toward negative infinity [supported]
// 4 - to nearest, ties away from zero [not supported]
assert(args.size() == 1 || args.size() == 2);
auto [fieldRef, fieldTy] = getFieldRef(builder, loc, args[0]);
auto [fieldRef, fieldTy] = getFieldRef(builder, loc, getBase(args[0]));
mlir::Value mode = builder.create<fir::LoadOp>(loc, fieldRef);
mlir::Value lbOk = builder.create<mlir::arith::CmpIOp>(
loc, mlir::arith::CmpIPredicate::sge, mode,
Expand All @@ -5671,6 +5680,19 @@ IntrinsicLibrary::genIeeeSupportRounding(mlir::Type resultType,
loc, resultType, builder.create<mlir::arith::AndIOp>(loc, lbOk, ubOk));
}

// IEEE_SUPPORT_STANDARD
fir::ExtendedValue IntrinsicLibrary::genIeeeSupportStandard(
mlir::Type resultType, llvm::ArrayRef<fir::ExtendedValue> args) {
// Check if IEEE standard support is available, which reduces to checking
// if halting control is supported, as that is the only support component
// that may not be available.
assert(args.size() <= 1);
mlir::Value nearest = builder.createIntegerConstant(
loc, builder.getIntegerType(32), _FORTRAN_RUNTIME_IEEE_NEAREST);
return builder.createConvert(
loc, resultType, fir::runtime::genSupportHalting(builder, loc, nearest));
}

// IEEE_UNORDERED
mlir::Value
IntrinsicLibrary::genIeeeUnordered(mlir::Type resultType,
Expand Down