Skip to content

X86: Add pattern for optimized BLSIC IR form#209814

Merged
RKSimon merged 10 commits into
llvm:mainfrom
Logans-olo:blsic-pattern-fix
Jul 17, 2026
Merged

X86: Add pattern for optimized BLSIC IR form#209814
RKSimon merged 10 commits into
llvm:mainfrom
Logans-olo:blsic-pattern-fix

Conversation

@Logans-olo

@Logans-olo Logans-olo commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Add DAG pattern to recognize the optimized IR form of BLSIC: (xor (and x, -x), -1)

Fixes #209718 - matches the IR form produced after InstCombine optimizations

Added tests for both 32-bit and 64-bit BLSIC patterns

Assisted by: Claude Code, helped me understand pattern-matching/intrinsics, and some file structure. As well as how testing for LLVM works.

@github-actions

Copy link
Copy Markdown

Hello @Logans-olo 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

@llvmorg-github-actions

llvmorg-github-actions Bot commented Jul 15, 2026

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-binary-utilities

@llvm/pr-subscribers-backend-x86

Author: Logan (Logans-olo)

Changes

Add DAG pattern to recognize the optimized IR form of BLSIC:
(xor (and x, -x), -1)

This matches the IR form produced after InstCombine optimizations, fixing code generation regression from issue #209718.

Added tests for both 32-bit and 64-bit BLSIC patterns


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

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86InstrTBM.td (+5)
  • (added) llvm/test/CodeGen/X86/tbm-blsic-patterns.ll (+40)
diff --git a/llvm/lib/Target/X86/X86InstrTBM.td b/llvm/lib/Target/X86/X86InstrTBM.td
index 09200f0c1a9f6..a324925dc46cb 100644
--- a/llvm/lib/Target/X86/X86InstrTBM.td
+++ b/llvm/lib/Target/X86/X86InstrTBM.td
@@ -125,6 +125,11 @@ let Predicates = [HasTBM] in {
   def : Pat<(or GR64:$src, (add GR64:$src, -1)),
             (BLSFILL64rr GR64:$src)>;
 
+  def : Pat<(xor (and GR32:$src, (sub 0, GR32:$src)), -1),
+            (BLSIC32rr GR32:$src)>;
+  def : Pat<(xor (and GR64:$src, (sub 0, GR64:$src)), -1),
+            (BLSIC64rr GR64:$src)>;
+
   def : Pat<(or (not GR32:$src), (add GR32:$src, -1)),
             (BLSIC32rr GR32:$src)>;
   def : Pat<(or (not GR64:$src), (add GR64:$src, -1)),
diff --git a/llvm/test/CodeGen/X86/tbm-blsic-patterns.ll b/llvm/test/CodeGen/X86/tbm-blsic-patterns.ll
new file mode 100644
index 0000000000000..98370869c5073
--- /dev/null
+++ b/llvm/test/CodeGen/X86/tbm-blsic-patterns.ll
@@ -0,0 +1,40 @@
+; RUN: llc -mattr=+tbm < %s | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+; CHECK-LABEL: test_blsic_original
+; CHECK: blsicl
+define i32 @test_blsic_original(i32 %x) {
+  %_2 = xor i32 %x, -1
+  %_0.i = add i32 %x, -1
+  %_0 = or i32 %_0.i, %_2
+  ret i32 %_0
+}
+
+; CHECK-LABEL: test_blsic_optimized
+; CHECK: blsicl
+define i32 @test_blsic_optimized(i32 %x) {
+  %1 = sub i32 0, %x
+  %2 = and i32 %x, %1
+  %3 = xor i32 %2, -1
+  ret i32 %3
+}
+
+; 64-bit versions
+; CHECK-LABEL: test_blsic_64_original
+; CHECK: blsicq
+define i64 @test_blsic_64_original(i64 %x) {
+  %_2 = xor i64 %x, -1
+  %_0.i = add i64 %x, -1
+  %_0 = or i64 %_0.i, %_2
+  ret i64 %_0
+}
+
+; CHECK-LABEL: test_blsic_64_optimized
+; CHECK: blsicq
+define i64 @test_blsic_64_optimized(i64 %x) {
+  %1 = sub i64 0, %x
+  %2 = and i64 %x, %1
+  %3 = xor i64 %2, -1
+  ret i64 %3
+}

@RKSimon RKSimon 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.

don't add a new test file - just add the additional tests to tbm_patterns.ll close to the original blsic tests - and make sure you use the update script

@Logans-olo
Logans-olo force-pushed the blsic-pattern-fix branch 2 times, most recently from cdc482d to 27cc3c1 Compare July 15, 2026 17:44

@RKSimon RKSimon 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.

regenerate the test file with the update script

Comment thread llvm/test/CodeGen/X86/tbm_patterns.ll Outdated
Comment thread llvm/test/CodeGen/X86/tbm_patterns.ll Outdated
@Logans-olo

Copy link
Copy Markdown
Contributor Author

Thank you for the review, the changes should be resolved now.

Maybe it is a me problem, but why is the update branch option still available? For example, I just rebased using the Github UI, but the option is still there. Is there something I should do from CLI?

Thank you again, and sorry to be such a pain.

@Logans-olo
Logans-olo requested a review from RKSimon July 15, 2026 19:10
@Logans-olo

Copy link
Copy Markdown
Contributor Author

Thank you for the review, the changes should be resolved now.

Maybe it is a me problem, but why is the update branch option still available? For example, I just rebased using the Github UI, but the option is still there. Is there something I should do from CLI?

Thank you again, and sorry to be such a pain.

Nevermind, seems I just got unlucky, and needed to do it again

@github-actions

github-actions Bot commented Jul 15, 2026

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 138574 tests passed
  • 3556 tests skipped

✅ The build succeeded and all tests passed.

Comment thread llvm/test/CodeGen/X86/tbm_patterns.ll Outdated
@Logans-olo
Logans-olo requested review from a team, DeinAlptraum and JDevlieghere as code owners July 15, 2026 20:33
@Logans-olo
Logans-olo force-pushed the blsic-pattern-fix branch 2 times, most recently from 4da0175 to 4055107 Compare July 15, 2026 22:54
Comment thread llvm/test/CodeGen/X86/tbm_patterns.ll Outdated
Comment thread llvm/lib/Target/X86/X86InstrTBM.td Outdated
Comment thread llvm/test/CodeGen/X86/tbm_patterns.ll Outdated
Comment thread llvm/test/CodeGen/X86/tbm_patterns.ll Outdated
Comment thread llvm/test/CodeGen/X86/tbm_patterns.ll Outdated
logans-a11y and others added 5 commits July 16, 2026 12:08
Add DAG pattern to recognize the optimized IR form of BLSIC:
  (xor (and x, -x), -1)

This matches the IR form produced after InstCombine optimizations,
fixing code generation regression from issue llvm#209718.
…iling whitespace and remove sub in favor of ineg
@Logans-olo

Copy link
Copy Markdown
Contributor Author

Thank you guys again for your repeated reviews, and all of your patience with me. I believe I have resolved all of your comments, and am passing all of the test cases. Let me know of anything further.

Thank you again!

@Logans-olo
Logans-olo requested a review from RKSimon July 16, 2026 16:46
@RKSimon

RKSimon commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

@Logans-olo when we talk about the update script - we're asking you to manually run llvm-project\llvm\utils\update_llc_test_checks.py on the test file to regenerate all the CHECK lines.

One of your earlier comments suggested you thought it had something to with github updating the patch against trunk latest - sorry if there was any confusion.

@nikic

nikic commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

You can also use build/bin/llvm-lit --update-tests llvm/test/CodeGen/X86/tbm_patterns.ll so you don't have to mess with paths.

@Logans-olo

Copy link
Copy Markdown
Contributor Author

So I run build/bin/llvm-lit --update-tests llvm/test/CodeGen/X86/tbm_patterns.ll, and then move into build directory, and run ninja check-llvm-codegen. I am passing all that are supported. Is that sufficient?

@Logans-olo

Copy link
Copy Markdown
Contributor Author

So I run build/bin/llvm-lit --update-tests llvm/test/CodeGen/X86/tbm_patterns.ll, and then move into build directory, and run ninja check-llvm-codegen. I am passing all that are supported. Is that sufficient?

And I should be running this command and ninja after every change to a testing file correct? I think that was part of my problem as well.

@Logans-olo

Copy link
Copy Markdown
Contributor Author

@Logans-olo when we talk about the update script - we're asking you to manually run llvm-project\llvm\utils\update_llc_test_checks.py on the test file to regenerate all the CHECK lines.

One of your earlier comments suggested you thought it had something to with github updating the patch against trunk latest - sorry if there was any confusion.

Also sorry, that's completely on me. Should know to be running (build/update) scripts after every change. I think its good now though?

@nikic nikic 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.

LGTM

@RKSimon RKSimon 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.

LGTM - cheers

@RKSimon
RKSimon enabled auto-merge (squash) July 17, 2026 17:03
@Logans-olo

Copy link
Copy Markdown
Contributor Author

Is there anything I should do about the checks failing on a different test now?

@RKSimon
RKSimon disabled auto-merge July 17, 2026 21:06
@RKSimon
RKSimon merged commit 466503d into llvm:main Jul 17, 2026
9 of 12 checks passed
@github-actions

Copy link
Copy Markdown

@Logans-olo 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!

nikic pushed a commit to nikic/llvm-project that referenced this pull request Jul 20, 2026
Add DAG pattern to recognize the optimized IR form of BLSIC: (xor (and
x, -x), -1)

Fixes llvm#209718 - matches the IR form produced after InstCombine
optimizations

Added tests for both 32-bit and 64-bit BLSIC patterns

Assisted by: Claude Code, helped me understand
pattern-matching/intrinsics, and some file structure. As well as how
testing for LLVM works.

(cherry picked from commit 466503d)
dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 22, 2026
Add DAG pattern to recognize the optimized IR form of BLSIC: (xor (and
x, -x), -1)

Fixes llvm#209718 - matches the IR form produced after InstCombine
optimizations

Added tests for both 32-bit and 64-bit BLSIC patterns

Assisted by: Claude Code, helped me understand
pattern-matching/intrinsics, and some file structure. As well as how
testing for LLVM works.

(cherry picked from commit 466503d)
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.

[X86] InstCombine change breaks blsic matching

4 participants