Skip to content

[InstCombine] recognize missed i128 split optimization #129363

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

Conversation

bassiounix
Copy link
Contributor

This pr fixes #126056, recognising a split i128 extension optimization.

Proof for working optimization:

define i128 @src(i32 noundef %x) {
entry:
  %coerce.sroa.0.0.extract.trunc = sext i32 %x to i64
  %0 = ashr i32 %x, 31
  %coerce.sroa.2.0.extract.trunc = sext i32 %0 to i64
  %x.sroa.2.0.insert.ext.i = zext i64 %coerce.sroa.2.0.extract.trunc to i128
  %x.sroa.2.0.insert.shift.i = shl nuw i128 %x.sroa.2.0.insert.ext.i, 64
  %x.sroa.0.0.insert.ext.i = zext i64 %coerce.sroa.0.0.extract.trunc to i128
  %x.sroa.0.0.insert.insert.i = or disjoint i128 %x.sroa.2.0.insert.shift.i, %x.sroa.0.0.insert.ext.i
  ret i128 %x.sroa.0.0.insert.insert.i
}

define i128 @tgt(i32 noundef %x)  {
  %x.sroa.0.0.insert.insert.i = sext i32 %x to i128
  ret i128 %x.sroa.0.0.insert.insert.i
}

@bassiounix bassiounix requested a review from nikic as a code owner March 1, 2025 05:49
Copy link

github-actions bot commented Mar 1, 2025

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.

@llvmbot
Copy link
Member

llvmbot commented Mar 1, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Muhammad Bassiouni (bassiounix)

Changes

This pr fixes #126056, recognising a split i128 extension optimization.

Proof for working optimization:

define i128 @<!-- -->src(i32 noundef %x) {
entry:
  %coerce.sroa.0.0.extract.trunc = sext i32 %x to i64
  %0 = ashr i32 %x, 31
  %coerce.sroa.2.0.extract.trunc = sext i32 %0 to i64
  %x.sroa.2.0.insert.ext.i = zext i64 %coerce.sroa.2.0.extract.trunc to i128
  %x.sroa.2.0.insert.shift.i = shl nuw i128 %x.sroa.2.0.insert.ext.i, 64
  %x.sroa.0.0.insert.ext.i = zext i64 %coerce.sroa.0.0.extract.trunc to i128
  %x.sroa.0.0.insert.insert.i = or disjoint i128 %x.sroa.2.0.insert.shift.i, %x.sroa.0.0.insert.ext.i
  ret i128 %x.sroa.0.0.insert.insert.i
}

define i128 @<!-- -->tgt(i32 noundef %x)  {
  %x.sroa.0.0.insert.insert.i = sext i32 %x to i128
  ret i128 %x.sroa.0.0.insert.insert.i
}

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

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+7)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 175c653f17f07..cff95338650f2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3119,6 +3119,13 @@ static Value *matchOrConcat(Instruction &Or, InstCombiner::BuilderTy &Builder) {
       match(UpperSrc, m_BitReverse(m_Value(UpperBRev))))
     return ConcatIntrinsicCalls(Intrinsic::bitreverse, UpperBRev, LowerBRev);
 
+  Value *X;
+  if (match(LowerSrc, m_SExt(m_Value(X))) &&
+      match(UpperSrc,
+            m_SExt(m_AShr(m_Specific(X), m_SpecificInt(HalfWidth / 2 - 1))))) {
+    return Builder.CreateSExt(X, Ty);
+  }
+
   return nullptr;
 }
 

@dtcxzyw dtcxzyw requested a review from RKSimon March 1, 2025 09:03
@dtcxzyw
Copy link
Member

dtcxzyw commented Mar 1, 2025

Can you please add some tests? See https://llvm.org/docs/InstCombineContributorGuide.html#tests

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

Please can you add another starting type, maybe i16?

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

LGTM - cheers

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

One minor tweak

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

LGTM

@RKSimon RKSimon merged commit c662a9d into llvm:main Mar 6, 2025
11 checks passed
Copy link

github-actions bot commented Mar 6, 2025

@bassiounix Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@bassiounix bassiounix deleted the user/bassiounix/InstCombine-i128-split-optimization branch March 7, 2025 16:55
RKSimon added a commit that referenced this pull request Mar 7, 2025
…) -> (sext x) case

#129363 handled all the cases where there was a sext for the original source value, but not for cases where the source is already half the size of the destination type

Another regression noticed in #76524
RKSimon added a commit to RKSimon/llvm-project that referenced this pull request Mar 7, 2025
…))), bw) -> (sext x) fold

Minor tweak llvm#129363 which handled all the cases where there was a sext for the original source value, but not for cases where the source is already half the size of the destination type

Another regression noticed in llvm#76524
RKSimon added a commit that referenced this pull request Mar 9, 2025
…-1))), bw/2) -> (sext x) fold (#130316)

Minor tweak to #129363 which handled all the cases where there was a sext for the original source value, but not for cases where the source is already half the size of the destination type

Another regression noticed in #76524
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
This pr fixes llvm#126056, recognising a split i128 extension optimization.

Proof for working optimization:

```llvm
define i128 @src(i32 noundef %x) {
entry:
  %coerce.sroa.0.0.extract.trunc = sext i32 %x to i64
  %0 = ashr i32 %x, 31
  %coerce.sroa.2.0.extract.trunc = sext i32 %0 to i64
  %x.sroa.2.0.insert.ext.i = zext i64 %coerce.sroa.2.0.extract.trunc to i128
  %x.sroa.2.0.insert.shift.i = shl nuw i128 %x.sroa.2.0.insert.ext.i, 64
  %x.sroa.0.0.insert.ext.i = zext i64 %coerce.sroa.0.0.extract.trunc to i128
  %x.sroa.0.0.insert.insert.i = or disjoint i128 %x.sroa.2.0.insert.shift.i, %x.sroa.0.0.insert.ext.i
  ret i128 %x.sroa.0.0.insert.insert.i
}

define i128 @tgt(i32 noundef %x)  {
  %x.sroa.0.0.insert.insert.i = sext i32 %x to i128
  ret i128 %x.sroa.0.0.insert.insert.i
}
```
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
…) -> (sext x) case

llvm#129363 handled all the cases where there was a sext for the original source value, but not for cases where the source is already half the size of the destination type

Another regression noticed in llvm#76524
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.

[InstCombine] Failure to recognise a split i128 extension
4 participants