This is an archive of the discontinued LLVM Phabricator instance.

Set rounding_mode to tonearest in presence of a #pragma STDC FENV_ACCESS OFF.
ClosedPublic

Authored by zahiraam on Apr 6 2023, 12:29 PM.

Details

Summary

In strict mode the 'roundin_mode' is set to 'dynamic'. Using this pragma to get out of strict mode doesn't have any effect on the 'rounding_mode'.
See https://godbolt.org/z/zoGTf4j1G
This patch fixes that.

Diff Detail

Event Timeline

zahiraam created this revision.Apr 6 2023, 12:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 6 2023, 12:29 PM
zahiraam requested review of this revision.Apr 6 2023, 12:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 6 2023, 12:29 PM
zahiraam updated this revision to Diff 511522.Apr 6 2023, 1:58 PM

Remove the fenv_04_06.patch

pengfei added inline comments.Apr 6 2023, 11:04 PM
clang/test/CodeGen/pragma-fenv_access.c
239

Should this be ignore?

andrew.w.kaylor added inline comments.Apr 7 2023, 9:54 AM
clang/lib/Sema/SemaAttr.cpp
1340

Why is this only needed for "!IsEnabled"? Where is the rounding mode set in the IsEnabled case? It looks like setAllowFEnvAccessOverride() is defined by a macro and just sets a bit in the OverrideMask, but somehow it seems to be setting the rounding mode to Round.Dynamic. I'm just concerned that there is a disconnect in the implementation here.

clang/test/CodeGen/pragma-fenv_access.c
239

This is a tricky case. By a strict reading of the C standard, this could be ignore, because the standard says the compiler can assume the default floating point environment when FENV_ACCESS is OFF and that if code compiled with FENV_ACCESS OFF is executed with anything other than the default environment the behavior is undefined. However, in this case strict exception semantics have been enabled elsewhere in the compilation unit, so floating point exceptions may be unmasked. The standard allows us to ignore exceptions, but raising a spurious exception may be bad for users.

I'm unsure about this case. I lean towards leaving it as Zahira has it here because I don't think the use of strict exception semantics will be common enough to justify the less conservative behavior.

zahiraam added inline comments.Apr 7 2023, 10:46 AM
clang/lib/Sema/SemaAttr.cpp
1340

That all depends on if we are using the -frounding-math in the command line option or not. The LIT test that exercices the pragma is not using this option so it's actually not testing the pragma in a strict mode. Using -ffp-model=strict trigger -frounding-math and -ffp-exception-behavior=strict.

I think I want to make this change only when we are in real strict mode. Meaning we need both of these options to be set.
#pragma STDC FENV_ACCESS OFF should trigger the round.tonearest when both these options are used. So when IsEnabled is false and RoundingMath is 1.

zahiraam updated this revision to Diff 511760.Apr 7 2023, 12:00 PM
pengfei accepted this revision.Apr 7 2023, 9:50 PM

The change makes more sense to me, thanks!

clang/test/CodeGen/pragma-fenv_access.c
239

Maybe use float %0, float %1 to reduce unnecessary differences.

This revision is now accepted and ready to land.Apr 7 2023, 9:50 PM
zahiraam updated this revision to Diff 512108.Apr 10 2023, 3:27 AM
zahiraam marked an inline comment as done.
zahiraam updated this revision to Diff 512203.Apr 10 2023, 11:37 AM
zahiraam added inline comments.
clang/test/CodeGen/pragma-fenv_access.c
239

Andy has noticed that the rest of the test is using the %, so to stay consistent I have put back the %.

clang/test/CodeGen/pragma-fenv_access.c
3

Why doesn't this case use STRICT-RND? round.tonearest, but I don't understand why.

zahiraam updated this revision to Diff 512388.Apr 11 2023, 3:23 AM
zahiraam added inline comments.
clang/test/CodeGen/pragma-fenv_access.c
3

It does. I just forgot to add the ":" after the RUN.

andrew.w.kaylor accepted this revision.Apr 11 2023, 4:22 PM

Looks good to me.