Skip to content

SROA generate a noundef instead of splatting a noundef int #145122

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

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

Conversation

saethlin
Copy link

@saethlin saethlin commented Jun 20, 2025

Fixes #145121

This is just a fix for the SROA behavior, and I think that will also avoid what EarlyCSE is doing. Which is a bit unfortunate, because I'd like to be well-motivated to fix both passes.

Where should I add a test for this? I think I should add a regression test, but I don't understand how the tests are organized.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@nikic
Copy link
Contributor

nikic commented Jun 21, 2025

The SROA tests can be found here: https://github.com/llvm/llvm-project/tree/main/llvm/test/Transforms/SROA You can either create a new file or try to find an existing one for this code path -- the easiest way to do that is to add an assert and see which tests fail :)

It's worth noting that we have another copy of the memset splat generation code here, using a different implementation:

// We know that this method is only called when the mem transfer fully
// provides the bits for the load.
if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
// memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
// independently of what the offset is.
Value *Val = MSI->getValue();
if (LoadSize != 1)
Val =
Builder.CreateZExtOrBitCast(Val, IntegerType::get(Ctx, LoadSize * 8));
Value *OneElt = Val;
// Splat the value out to the right number of bits.
for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
// If we can double the number of bytes set, do it.
if (NumBytesSet * 2 <= LoadSize) {
Value *ShVal = Builder.CreateShl(
Val, ConstantInt::get(Val->getType(), NumBytesSet * 8));
Val = Builder.CreateOr(Val, ShVal);
NumBytesSet <<= 1;
continue;
}
// Otherwise insert one byte at a time.
Value *ShVal =
Builder.CreateShl(Val, ConstantInt::get(Val->getType(), 1 * 8));
Val = Builder.CreateOr(OneElt, ShVal);
++NumBytesSet;
}
We should probably unify these as a followup.

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- llvm/lib/Transforms/Scalar/SROA.cpp
View the diff from clang-format here.
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index 028bcde5a..403baed93 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -3222,14 +3222,14 @@ private:
 
     Type *SplatIntTy = Type::getIntNTy(VTy->getContext(), Size * 8);
     if (isa<UndefValue>(V)) {
-        V = UndefValue::get(VTy);
+      V = UndefValue::get(VTy);
     } else {
-        V = IRB.CreateMul(
-            IRB.CreateZExt(V, SplatIntTy, "zext"),
-            IRB.CreateUDiv(Constant::getAllOnesValue(SplatIntTy),
-                           IRB.CreateZExt(Constant::getAllOnesValue(V->getType()),
-                                          SplatIntTy)),
-            "isplat");
+      V = IRB.CreateMul(
+          IRB.CreateZExt(V, SplatIntTy, "zext"),
+          IRB.CreateUDiv(Constant::getAllOnesValue(SplatIntTy),
+                         IRB.CreateZExt(Constant::getAllOnesValue(V->getType()),
+                                        SplatIntTy)),
+          "isplat");
     }
     return V;
   }

Copy link

⚠️ undef deprecator found issues in your code. ⚠️

You can test this locally with the following command:
git diff -U0 --pickaxe-regex -S '([^a-zA-Z0-9#_-]undef[^a-zA-Z0-9_-]|UndefValue::get)' 'HEAD~1' HEAD llvm/lib/Transforms/Scalar/SROA.cpp

The following files introduce new uses of undef:

  • llvm/lib/Transforms/Scalar/SROA.cpp

Undef is now deprecated and should only be used in the rare cases where no replacement is possible. For example, a load of uninitialized memory yields undef. You should use poison values for placeholders instead.

In tests, avoid using undef and having tests that trigger undefined behavior. If you need an operand with some unimportant value, you can add a new argument to the function and use that instead.

For example, this is considered a bad practice:

define void @fn() {
  ...
  br i1 undef, ...
}

Please use the following instead:

define void @fn(i1 %cond) {
  ...
  br i1 %cond, ...
}

Please refer to the Undefined Behavior Manual for more information.

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.

Small arrays of MaybeUninit::<u8>::uninit() are deoptimized to all zeroes
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy