Repository navigation
[ConstantFolding] Do not fold flushable float denormals for trip counts - #9145
Open
Steven Perron (s-perron) wants to merge 1 commit into
Open
Steven Perron (s-perron) wants to merge 1 commit into
Steven Perron (s-perron) wants to merge 1 commit into
Conversation
Constant folding evaluates float denormals with IEEE results. Unless "fp32-denorm-mode" is "preserve", the driver may flush them to zero, so the result is non-deterministic. ScalarEvolution used such folds to compute a loop's exit count, and IndVarSimplify then removed live loop bounds. When AllowNonDeterministic is false, do not fold FP binary operators, fpext, fptrunc or fcmp with such a denormal. Only ScalarEvolution disables non-deterministic folds, so other folding is unchanged.
This was referenced Nov 18, 2026
Steven Perron (s-perron)
requested a review
from Natalie Chouinard (sudonatalie)
November 18, 2026 20:06
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The user-visible compiler correctness fix lacks the required bug-fix entry under “Upcoming Release” in docs/ReleaseNotes.md.
1 open issue
What changed in this PR
This PR prevents ScalarEvolution from using flushable float denormals to infer loop trip counts, avoiding incorrect removal of live loop bounds by IndVarSimplify.
Changes:
- Reject denormal-dependent arithmetic, conversion, and comparison folds when non-deterministic folding is disabled.
- Use the guarded comparison API in ScalarEvolution while preserving existing behavior for other callers.
- Add regression tests for scalar and vector operations, conversions, and preserve-mode folding.
| File | Description |
|---|---|
tools/clang/test/DXC/Passes/IndVarSimplify/denorm-trip-count.ll |
Tests loop-bound retention and preserve-mode optimization. |
lib/Analysis/ScalarEvolution.cpp |
Disables non-deterministic comparison folding. |
lib/Analysis/ConstantFolding.cpp |
Detects flushable denormals and guards affected folds. |
include/llvm/Analysis/ConstantFolding.h |
Declares the instruction-aware comparison-folding API. |
Review effort: Balanced
Comment on lines
+1094
to
+1098
| // NaN bit patterns and flushable denormals are non-deterministic. | ||
| if (!AllowNonDeterministic) | ||
| if (isConstantNaN(C) || mayFlushDenormal(Ops[0], I) || | ||
| mayFlushDenormal(Ops[1], I) || mayFlushDenormal(C, I)) | ||
| return nullptr; |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Constant folding evaluates float denormals with IEEE results. Unless
"fp32-denorm-mode" is "preserve", the driver may flush them to zero, so
the result is non-deterministic. ScalarEvolution used such folds to
compute a loop's exit count, and IndVarSimplify then removed live loop
bounds.
When AllowNonDeterministic is false, do not fold FP binary operators,
fpext, fptrunc or fcmp with such a denormal. Only ScalarEvolution
disables non-deterministic folds, so other folding is unchanged.