Skip to content

[ConstantFolding] Do not fold flushable float denormals for trip counts - #9145

Open
Steven Perron (s-perron) wants to merge 1 commit into
microsoft:mainfrom
s-perron:fix-denorm-in-constant-folding
Open

Steven Perron (s-perron) wants to merge 1 commit into
microsoft:mainfrom
s-perron:fix-denorm-in-constant-folding

Conversation

@s-perron

Copy link
Copy Markdown
Collaborator

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.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

2 participants