<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
</head>
<body>
<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">On 7 Jan 2020, at 14:00, Kevin Neal via cfe-dev wrote:</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">Right here it says that dealing with non-default modes is the job of the program or programmer: “The responsibilities associated with accessing the floating-point environment fall on the programmer or program that does so explicitly.” It doesn’t say compiler. It also hedges with words like “certain ... conventions”. If only some conventions then that implies that there are other conventions that do things differently and that’s OK.<br>
<br>
It explicitly calls out the programmer to solve these issues when opting in to non-default FP behavior.<br>
<br>
And I say this from a company that always runs with traps enabled and therefore has to deal with these FP issues. Sometimes we work around traps in third party, default FP environment software. Sometimes we _want_ that trap from default FP environment software because it indicates a bug somewhere. We have to examine these cases individually and determine what we need. It’s not the compiler’s job to protect us from ourselves.<br>
<br>
Can we get a language lawyer to settle this once and for all?</p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">I agree with your reading. The standard is quite clear that functions translated under <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">#pragma FENV_ACCESS OFF</code> may assume that the control modes have their default settings, but it also plainly describes functions that are sensitive to their caller’s control modes. If the control modes were meant to be implicitly managed by the implementation, there would have to be some way to declare the difference, but the standard provides no such mechanism. Instead, the standard describes conventions which programmers may follow explicitly to satisfy these control-mode expectations. This all leads me to the straightforward conclusion that it is meant to be undefined behavior to allow control to enter code translated under <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">#pragma FENV_ACCESS OFF</code> with non-default settings for the control modes (at least if that code performs any floating-point operations). It would even be somewhat reasonable to argue that this is an intended implication of the sentence in 7.6.1p2 beginning “If part of a program…”, although really I think it’s just an oversight in the drafting.</p>
<p dir="auto">I’m not sure I <em>like</em> this language design, but it’s definitely the intended design.</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">The next question is: should the compiler support some frontend attribute to mark functions that do not require default FP mode? These are functions that:<br>
- do not involve FP operations,<br>
- work correctly in any FP mode,<br>
- expects particular FP mode,<br>
- modifies FP mode,<br>
- probably something else.<br>
For such functions compiler would not generate save/restore operations. We also could have several attributes if we need to distinguish between these cases.</p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">This isn’t quite what you’re asking, but:</p>
<p dir="auto">If we’re going to do serious optimization work with code translated under <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">#pragma FENV_ACCESS ON</code>, we’ll want to be able to declare the <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">readonly</code>/<code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">readnone</code> equivalents for the FP environment:</p>
<ul>
<li>the function will not access the FP environment at all (essentially meaning that it won’t perform any FP operations) or</li>
<li>the function may read the FP environment, and it may change the status flags, but it at least won’t change the control modes.</li>
</ul>
<p dir="auto">It’s possible that the intermediate positions of “may read the FP environment but will not change the status flags” and “may set status flags, but will not read control modes” may be useful. (I think the latter can only happen with calls to intrinsics like <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">fesetexcept</code>, but, well, those functions exist, and optimizing around them might matter.)</p>
<p dir="auto">Anyway, we’ll want to do that at the IR level at least, and we’ll probably want to do it at the user level eventually. But it’ll be awhile before we get there. And these attributes would be quite different from the “allows non-default control modes” attribute that a different language design might need.</p>
<p dir="auto">John.</p>
</div>
</div>
</body>
</html>