This is an archive of the discontinued LLVM Phabricator instance.

[FPEnv Core 04/14] Skip constant folding to preserve FPEnv
AcceptedPublic

Authored by sdmitrouk on Oct 26 2015, 6:55 AM.

Details

Summary

Consider floating point environment flags when constant folding
expressions.

Diff Detail

Repository
rL LLVM

Event Timeline

sdmitrouk updated this revision to Diff 38402.Oct 26 2015, 6:55 AM
sdmitrouk retitled this revision from to [FPEnv Core 04/14] Skip constant folding to preserve FPEnv.
sdmitrouk updated this object.
sdmitrouk added reviewers: hfinkel, mehdi_amini.
sdmitrouk set the repository for this revision to rL LLVM.
sdmitrouk added subscribers: llvm-commits, resistor, scanon.
scanon added inline comments.Oct 26 2015, 8:29 AM
include/llvm/Analysis/ValueTracking.h
261

I think we should leave the one-sentence intro as-is to maximize readability; it's still correct with the new settings, AFAICT. Then let's add a paragraph to explain the floating-point-specific details:

"If KeepExceptions is true, then speculative execution of most floating-point operations will result in observably different floating-point exceptions being raised."

Worth noting: it's not obvious that KeepRounding fits the bill for isSafeToSpeculativelyExecute, but rounding-mode does change the observable exceptions by moving the overflow/underflow boundaries. However, that's just one small part of what you're trying to get at here with rounding, the larger part of which seems conceptually to be closer to "memory dependent" (except that the "memory" is floating-point control).

majnemer added inline comments.
include/llvm/IR/Constants.h
1106–1107

Why not accept an FastMathFlags instead?

lib/Analysis/ValueTracking.cpp
3371–3372

Unless I am mistaken, could you figure out these flags from V itself?

3375

const auto *

lib/IR/Constants.cpp
347–354

Why was this changed?

sdmitrouk updated this revision to Diff 41912.Dec 4 2015, 12:02 PM

Updated text of comments, updated code to pass fast-math flags in several places, adjusted code according to review comments, updated tests which are affected by inversion of meaning of flags.

sdmitrouk updated this revision to Diff 42097.Dec 7 2015, 1:07 PM
sdmitrouk edited edge metadata.

Revert questionable change in undef-label.ll (it shouldn't be here anyway), this also removes four tests that are now in D14067.

mehdi_amini added inline comments.Dec 7 2015, 2:28 PM
include/llvm/Analysis/ValueTracking.h
265

The KeepExceptions parameter is not clear to me, what would it be used for? (there is no use in this patch)

include/llvm/IR/Constants.h
1106

Document the new FMF parameter, especially how it differs from Flags, it is confusing right now.

Also, seeing multiple places where you need to explicitly add 0, nullptr, to be able to pass a FMF, it can be worthwhile to add an override that does not take Flags and OnlyIfReducedTy and forward to this one.

1243

Should it be allowed on non-FP operations? I could see it assert instead.

lib/Analysis/InstructionSimplify.cpp
3783 ↗(On Diff #41912)

It this related to this patch or could this be committed separately?

lib/Analysis/ValueTracking.cpp
3400

You already tested for KeepExceptions at this scope.

lib/IR/ConstantFold.cpp
1182

I'd refactor the test with a named lambda defined before the switch.
That said the full switch could be refactored better already.

lib/IR/Constants.cpp
1804

You may want to assert that if FMF is supplied we have a Floating point Opcode?

lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1214

Initialize closer to its use, there are multiple path that early return and don't need this.

lib/Transforms/InstCombine/InstructionCombining.cpp
215

No braces

sdmitrouk updated this revision to Diff 42225.Dec 8 2015, 2:12 PM

Addressing review comments.

sdmitrouk marked 5 inline comments as done.Dec 8 2015, 2:23 PM

@joker.eph, thanks. Changed according to your comments, but tests on this revision don't pass... Can't reorder commits nicely due to changes of flags (now that default value FastMathFlags() is "bad" for incremental changes). Instead I'm leaving patches separated for review, but when committing will squash them to make tests pass on each revision.

include/llvm/Analysis/ValueTracking.h
265

It's left from one of previous versions, the one with function attributes. Removed.

include/llvm/IR/Constants.h
1106

Should be done, at least for this patch.

1243

Moved it to place where it's used (used to be two such places), changed type of argument and added an assert.

lib/Analysis/ValueTracking.cpp
3400

Accidentally applied stash to the next patch instead of this one.

lib/IR/ConstantFold.cpp
1182

Changed it using Optional.

mehdi_amini accepted this revision.Dec 8 2015, 2:59 PM
mehdi_amini edited edge metadata.

LGTM with the few nitpicks below.

include/llvm/Analysis/ValueTracking.h
265

It seems you left the doxygen change in?

lib/IR/ConstantFold.cpp
1197

sweet!

lib/Transforms/InstCombine/InstructionCombining.cpp
212

Keep the name FMF for consistency with everywhere else.

This revision is now accepted and ready to land.Dec 8 2015, 2:59 PM
sdmitrouk updated this revision to Diff 42270.Dec 9 2015, 1:47 AM
sdmitrouk edited edge metadata.
sdmitrouk marked 5 inline comments as done.

Address the nitpicks, thanks for noticing them.

mehdi_amini added inline comments.Dec 11 2015, 3:28 PM
include/llvm/IR/FastMathFlags.h
39

Shouldn't the default be NoExceptions | NoRounding ?

mehdi_amini requested changes to this revision.Dec 11 2015, 3:36 PM
mehdi_amini edited edge metadata.
This revision now requires changes to proceed.Dec 11 2015, 3:36 PM
mehdi_amini accepted this revision.Dec 11 2015, 3:56 PM
mehdi_amini edited edge metadata.

Thinking about it more we will never reach correctness without having the safe option by default.

This revision is now accepted and ready to land.Dec 11 2015, 3:56 PM
sdmitrouk added inline comments.Dec 11 2015, 4:53 PM
include/llvm/IR/FastMathFlags.h
39

Well, it was initially, and it was easier then, but meaning of flags was changed after comments in D14067 partly to make default value all zeroes.