Skip to content
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

[ScalarEvolutionExpander] Don't drop nowrap flags on addrec expansion #78199

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wsmoses
Copy link
Member

@wsmoses wsmoses commented Jan 15, 2024

When performing expand on a SCEVAddRecNode with nowrap applied (e.g. if deduced from an iv with an increment with nowrap), the generated multiply will not have the nowrap. This PR preserves the nowrap behavior on the generated code.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 15, 2024

@llvm/pr-subscribers-llvm-transforms

Author: William Moses (wsmoses)

Changes

When performing expand on a SCEVAddRecNode with nowrap applied (e.g. if deduced from an iv with an increment with nowrap), the generated multiply will not have the nowrap. This PR preserves the nowrap behavior on the generated code.


Full diff: https://github.com/llvm/llvm-project/pull/78199.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp (+2-1)
diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index a1d7f0f9ba0f74..e87b430d6bee8f 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -1254,7 +1254,8 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
       expand(SE.getTruncateOrNoop(
         SE.getMulExpr(SE.getUnknown(CanonicalIV),
                       SE.getNoopOrAnyExtend(S->getOperand(1),
-                                            CanonicalIV->getType())),
+                                            CanonicalIV->getType()),
+                                        S->getNoWrapFlags(SCEV::FlagNW)),
         Ty));
 
   // If this is a chain of recurrences, turn it into a closed form, using the

Copy link

github-actions bot commented Jan 15, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Tests?

SE.getMulExpr(
SE.getUnknown(CanonicalIV),
SE.getNoopOrAnyExtend(S->getOperand(1), CanonicalIV->getType()),
S->getNoWrapFlags(SCEV::FlagNW)),
Copy link
Contributor

@nikic nikic Jan 15, 2024

Choose a reason for hiding this comment

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

This doesn't make sense. NW is only meaningful/supported on addrecs, while you're applying it to a mul.

@wsmoses
Copy link
Member Author

wsmoses commented Jan 15, 2024

Tests?

Yeah I wasn't quite sure where to add them, if you had any pointers

@nikic
Copy link
Contributor

nikic commented Jan 16, 2024

I'm not sure where exactly we expand addrecs in canonical mode, so I'd suggest adding an assert into the branch and seeing which tests fail, then modify those.

@nikic
Copy link
Contributor

nikic commented Jan 16, 2024

I'm not sure where exactly we expand addrecs in canonical mode, so I'd suggest adding an assert into the branch and seeing which tests fail, then modify those.

Ah well, it looks like you effectively already did that by trying to use an invalid nowrap flag. List of failing tests:

  LLVM :: CodeGen/AMDGPU/loop-prefetch-data.ll
  LLVM :: CodeGen/PowerPC/hardware-loops-crash.ll
  LLVM :: CodeGen/PowerPC/loop-data-prefetch-inner.ll
  LLVM :: CodeGen/PowerPC/loop-data-prefetch.ll
  LLVM :: CodeGen/PowerPC/loop-hoist-toc-save.ll
  LLVM :: CodeGen/PowerPC/ppc64-get-cache-line-size.ll
  LLVM :: CodeGen/SystemZ/prefetch-02.ll
  LLVM :: CodeGen/SystemZ/prefetch-03.ll
  LLVM :: CodeGen/SystemZ/prefetch-04.ll
  LLVM :: CodeGen/Thumb2/LowOverheadLoops/tail-pred-reduce.ll
  LLVM :: CodeGen/Thumb2/LowOverheadLoops/varying-outer-2d-reduction.ll
  LLVM :: Transforms/IRCE/pr57335.ll
  LLVM :: Transforms/LICM/AliasSetMemSet.ll
  LLVM :: Transforms/LoopDataPrefetch/AArch64/kryo-large-stride.ll
  LLVM :: Transforms/LoopDataPrefetch/AArch64/large-stride.ll
  LLVM :: Transforms/LoopDataPrefetch/AArch64/opt-remark-with-hotness.ll
  LLVM :: Transforms/LoopDataPrefetch/AArch64/opt-remark.ll
  LLVM :: Transforms/LoopDataPrefetch/PowerPC/basic.ll
  LLVM :: Transforms/LoopDataPrefetch/RISCV/basic.ll
  LLVM :: Transforms/LoopIdiom/basic-address-space.ll
  LLVM :: Transforms/LoopIdiom/basic.ll
  LLVM :: Transforms/LoopIdiom/lir-heurs-multi-block-loop.ll
  LLVM :: Transforms/LoopVectorize/multiple-strides-vectorization.ll
  LLVM :: Transforms/LoopVectorize/pr55100-expand-scev-predicate-used.ll
  LLVM :: Transforms/LoopVectorize/runtime-checks-difference.ll
  LLVM :: Transforms/LoopVersioningLICM/loopversioningLICM1.ll
  LLVM :: Transforms/LoopVersioningLICM/loopversioningLICM2.ll
  LLVM :: Transforms/LoopVersioningLICM/metadata.ll

Probably LoopIdiom is the best candidate for a test case.

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

Successfully merging this pull request may close these issues.

None yet

3 participants