Skip to content

subnormal_mode #243

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

Closed
wants to merge 2 commits into from
Closed

subnormal_mode #243

wants to merge 2 commits into from

Conversation

sunfishcode
Copy link
Member

This PR proposes a subnormal_mode AST node, as a possible resolution to #148.

* `standard`: standard semantics are followed
* `dont_care`: Subnormal values may or may not be interpreted as zero of the
same sign. Subnormal result values may or may not be replaced by any
subnormal value of the same sign, or zero of the same sign.
Copy link
Member

Choose a reason for hiding this comment

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

What's "standard"? Wouldn't "IEEE754" be a better name?
I thought that we'd settled on "fastest" instead of "dont_care"?

Copy link
Member Author

Choose a reason for hiding this comment

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

"Standard" means the IEEE-754 standard, and also the WebAssembly standard, since it's the behavior we'll otherwise specify. Is there another standard which is remotely as authoritative in this regard?

In my opinion, "fastest" is implied. We always want the fastest performance permitted under the chosen semantics. "dont_care" makes it explicit that the programmer is choosing not to care about what happens, rather than blindly hoping that something named "fastest" will make their program faster without understanding what it means.

@jfbastien
Copy link
Member

I'm still not sure that I like using a block as you propose, versus tagging each individual instruction.

We haven't really discussed what happens when there's a call inside of such a block. Do it affect the callee?

@sunfishcode
Copy link
Member Author

The advantage of a scoped construct is that it eliminates the need for an implementation to run a code placement algorithm to emit reasonably-well optimized mode switching code.

By the phrase "lexical extent", I mean that it doesn't apply to callees. One of the main goals here is to make it possible (and easy) to statically determine the mode for every operation.

@kg
Copy link
Contributor

kg commented Jul 6, 2015

The checked/unchecked mechanism in C# is a relevant example here:

https://msdn.microsoft.com/en-us/library/74b4xzyw.aspx

they recognized that there are multiple levels at which you want to control checking, so they let you decorate a single expression checked(a + b), a block checked { x = a + b; x += y; } or an entire module (by changing compiler options). It's not inherited by callees, so it's pretty easy to compile and audit.

@sunfishcode
Copy link
Member Author

Thanks for the reference @kg. That covers integer overflow checking which has some similar usage patterns as subnormal handling, though a few differences as well.

Block-level: that's what I'm proposing :-).

Module-level: We can achieve similar functionality by just wrapping every function body in a subnormal_mode block. This is also easier for WebAssembly to do than it is for C# because WebAssembly is primarily a compiler target. The user interface can still be to just set a compiler option, and compilers can just make the right thing happen everywhere. Also, module-level flags complicate dynamic linking, so we'd like to avoid them when we can.

Per-operation: Unlike integer-overflow checking, subnormal flushing is typically implemented in hardware by setting a control register, and there is a cost each time the mode is changed. Since the point of the mode is to make code go faster, one typically doesn't wish to take on extra costs by changing modes frequently. Consequently, it's much more common and meaningful to just set it once for an entire block of code.

@titzer
Copy link

titzer commented Jul 6, 2015

I support block-level declaration, though IIUC a conforming implementation
can default to IEEE 754 everywhere, correct?

On Mon, Jul 6, 2015 at 7:51 PM, Dan Gohman [email protected] wrote:

Thanks for the reference @kg https://github.com/kg. That covers integer
overflow checking which has some similar usage patterns as subnormal
handling, though a few differences as well.

Block-level: that's what I'm proposing :-).

Module-level: We can achieve similar functionality by just wrapping every
function body in a subnormal_mode block. This is also easier for
WebAssembly to do than it is for C# because WebAssembly is primarily a
compiler target. The user interface can still be to just set a compiler
option, and compilers can just make the right thing happen everywhere.
Also, module-level flags complicate dynamic linking, so we'd like to avoid
them when we can.

Per-operation: Unlike integer-overflow checking, subnormal flushing is
typically implemented in hardware by setting a control register, and there
is a cost each time the mode is changed. Since the point of the mode is to
make code go faster, one typically doesn't wish to take on extra costs by
changing modes frequently. Consequently, it's much more common and
meaningful to just set it once for an entire block of code.


Reply to this email directly or view it on GitHub
#243 (comment).

@sunfishcode
Copy link
Member Author

Yes. The attribute choices proposed here are "standard" and "dont_care", so using standard mode for everything is fully conforming.

@jfbastien
Copy link
Member

Please hold on this PR, @davidsehr and I are discussing a different / wider solution.

@sunfishcode
Copy link
Member Author

After discussing this with people more, I'm ok closing this for now. While a flag to disable subnormals is worth considering, the real-world experience of asm.js tells us that it ultimately isn't necessary for an MVP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants