Skip to content

[flang] Unsupported rounding modes #128240

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 24, 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: 8 additions & 0 deletions flang/docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,14 @@ print *, [(j,j=1,10)]
integer overflow never occurs in address calculations and increment of
do-variable unless the option `-fwrapv` is enabled.

* Two new ieee_round_type values were added in f18 beyond the four values
defined in f03 and f08: ieee_away and ieee_other. Contemporary hardware
typically does not have support for these rounding modes;
ieee_support_rounding calls for these values return false.
ieee_set_rounding_mode calls that attempt to set the rounding mode to one
of these values in violation of the restriction in f23 clause 17.11.42 set
the mode to ieee_nearest.

## De Facto Standard Features

* `EXTENDS_TYPE_OF()` returns `.TRUE.` if both of its arguments have the
Expand Down
24 changes: 22 additions & 2 deletions flang/lib/Optimizer/Builder/IntrinsicCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5450,13 +5450,33 @@ void IntrinsicLibrary::genIeeeSetRoundingMode(
llvm::ArrayRef<fir::ExtendedValue> args) {
// Set the current floating point rounding mode to the value of arg
// ROUNDING_VALUE. Values are llvm.get.rounding encoding values.
// Generate an error if the value of optional arg RADIX is not 2.
// Modes ieee_to_zero, ieee_nearest, ieee_up, and ieee_down are supported.
// Modes ieee_away and ieee_other are not supported, and are treated as
// ieee_nearest. Generate an error if the optional RADIX arg is not 2.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document this for the user in Extension.md or Intrinsics.md? (not sure which document makes the most sense).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a short item in Extensions.md.

assert(args.size() == 1 || args.size() == 2);
if (args.size() == 2)
checkRadix(builder, loc, fir::getBase(args[1]), "ieee_set_rounding_mode");
auto [fieldRef, ignore] = getFieldRef(builder, loc, fir::getBase(args[0]));
auto [fieldRef, fieldTy] = getFieldRef(builder, loc, fir::getBase(args[0]));
mlir::func::FuncOp setRound = fir::factory::getLlvmSetRounding(builder);
mlir::Value mode = builder.create<fir::LoadOp>(loc, fieldRef);
static_assert(
_FORTRAN_RUNTIME_IEEE_TO_ZERO >= 0 &&
_FORTRAN_RUNTIME_IEEE_TO_ZERO <= 3 &&
_FORTRAN_RUNTIME_IEEE_NEAREST >= 0 &&
_FORTRAN_RUNTIME_IEEE_NEAREST <= 3 && _FORTRAN_RUNTIME_IEEE_UP >= 0 &&
_FORTRAN_RUNTIME_IEEE_UP <= 3 && _FORTRAN_RUNTIME_IEEE_DOWN >= 0 &&
_FORTRAN_RUNTIME_IEEE_DOWN <= 3 && "unexpected rounding mode mapping");
mlir::Value mask = builder.create<mlir::arith::ShLIOp>(
loc, builder.createAllOnesInteger(loc, fieldTy),
builder.createIntegerConstant(loc, fieldTy, 2));
mlir::Value modeIsSupported = builder.create<mlir::arith::CmpIOp>(
loc, mlir::arith::CmpIPredicate::eq,
builder.create<mlir::arith::AndIOp>(loc, mode, mask),
builder.createIntegerConstant(loc, fieldTy, 0));
mlir::Value nearest = builder.createIntegerConstant(
loc, fieldTy, _FORTRAN_RUNTIME_IEEE_NEAREST);
mode = builder.create<mlir::arith::SelectOp>(loc, modeIsSupported, mode,
nearest);
mode = builder.create<fir::ConvertOp>(
loc, setRound.getFunctionType().getInput(0), mode);
builder.create<fir::CallOp>(loc, setRound, mode);
Expand Down
Loading