Skip to content

[GlobalISel] Add G_STEP_VECTOR to computeKnownBits.#190598

Merged
arsenm merged 2 commits into
llvm:mainfrom
Xylecrack:gisel-step-vector
Apr 9, 2026
Merged

[GlobalISel] Add G_STEP_VECTOR to computeKnownBits.#190598
arsenm merged 2 commits into
llvm:mainfrom
Xylecrack:gisel-step-vector

Conversation

@Xylecrack

Copy link
Copy Markdown
Contributor

This code is adapted from SelectionDAG::computeKnownBits.
part of #150515.
ticks off STEP_VECTOR.

@llvmbot

llvmbot commented Apr 6, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-llvm-globalisel

@llvm/pr-subscribers-backend-aarch64

Author: Dhruva (Xylecrack)

Changes

This code is adapted from SelectionDAG::computeKnownBits.
part of #150515.
ticks off STEP_VECTOR.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp (+24)
  • (added) llvm/test/CodeGen/AArch64/GlobalISel/knownbits-stepvector.mir (+59)
diff --git a/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp b/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
index f0b455fbdc7d0..a00e17cb3e441 100644
--- a/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
@@ -285,6 +285,30 @@ void GISelValueTracking::computeKnownBitsImpl(Register R, KnownBits &Known,
     }
     break;
   }
+  case TargetOpcode::G_STEP_VECTOR: {
+    APInt Step = MI.getOperand(1).getCImm()->getValue();
+
+    if (Step.isPowerOf2())
+      Known.Zero.setLowBits(Step.logBase2());
+
+    if (!isUIntN(BitWidth, DstTy.getElementCount().getKnownMinValue()))
+      break;
+
+    const APInt MinNumElts =
+        APInt(BitWidth, DstTy.getElementCount().getKnownMinValue());
+    const Function &F = getMachineFunction().getFunction();
+    bool Overflow;
+    const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
+                                 .getUnsignedMax()
+                                 .umul_ov(MinNumElts, Overflow);
+    if (Overflow)
+      break;
+    const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
+    if (Overflow)
+      break;
+    Known.Zero.setHighBits(MaxValue.countl_zero());
+    break;
+  }
   case TargetOpcode::G_CONSTANT: {
     Known = KnownBits::makeConstant(MI.getOperand(1).getCImm()->getValue());
     break;
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-stepvector.mir b/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-stepvector.mir
new file mode 100644
index 0000000000000..73c76f78d4ac0
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-stepvector.mir
@@ -0,0 +1,59 @@
+# NOTE: Assertions have been autogenerated by utils/update_givaluetracking_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=aarch64 -passes="print<gisel-value-tracking>" -filetype=null %s 2>&1 | FileCheck %s
+
+---
+name: StepVector_One
+body: |
+  bb.0:
+  ; CHECK-LABEL: name: @StepVector_One
+  ; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????? SignBits:1
+    %0:_(<vscale x 4 x s32>) = G_STEP_VECTOR i32 1
+...
+---
+name: StepVector_Pow2
+body: |
+  bb.0:
+  ; CHECK-LABEL: name: @StepVector_Pow2
+  ; CHECK-NEXT: %0:_ KnownBits:????????????????????????????0000 SignBits:1
+    %0:_(<vscale x 4 x s32>) = G_STEP_VECTOR i32 16
+...
+---
+name: StepVector_NonPow2
+body: |
+  bb.0:
+  ; CHECK-LABEL: name: @StepVector_NonPow2
+  ; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????? SignBits:1
+    %0:_(<vscale x 4 x s32>) = G_STEP_VECTOR i32 3
+...
+---
+name: StepVector_Narrow
+body: |
+  bb.0:
+  ; CHECK-LABEL: name: @StepVector_Narrow
+  ; CHECK-NEXT: %0:_ KnownBits:???????? SignBits:1
+    %0:_(<vscale x 8 x s8>) = G_STEP_VECTOR i8 1
+...
+---
+name: StepVector_Narrow_Pow2
+body: |
+  bb.0:
+  ; CHECK-LABEL: name: @StepVector_Narrow_Pow2
+  ; CHECK-NEXT: %0:_ KnownBits:??????00 SignBits:1
+    %0:_(<vscale x 8 x s8>) = G_STEP_VECTOR i8 4
+...
+---
+name: StepVector_Wide
+body: |
+  bb.0:
+  ; CHECK-LABEL: name: @StepVector_Wide
+  ; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????????????????????????????????????? SignBits:1
+    %0:_(<vscale x 2 x s64>) = G_STEP_VECTOR i64 1
+...
+---
+name: StepVector_Wide_Pow2
+body: |
+  bb.0:
+  ; CHECK-LABEL: name: @StepVector_Wide_Pow2
+  ; CHECK-NEXT: %0:_ KnownBits:?????????????????????????????????????????????????????????????000 SignBits:1
+    %0:_(<vscale x 2 x s64>) = G_STEP_VECTOR i64 8
+...

Comment on lines +304 to +308
if (Overflow)
break;
const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
if (Overflow)
break;

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.

Missing negative tests for these cases?

if (Step.isPowerOf2())
Known.Zero.setLowBits(Step.logBase2());

if (!isUIntN(BitWidth, DstTy.getElementCount().getKnownMinValue()))

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.

Missing negative test?

Comment thread llvm/test/CodeGen/AArch64/GlobalISel/knownbits-stepvector.mir
; CHECK-LABEL: name: @StepVector_Wide_Pow2
; CHECK-NEXT: %0:_ KnownBits:?????????????????????????????????????????????????????????????000 SignBits:1
%0:_(<vscale x 2 x s64>) = G_STEP_VECTOR i64 8
...

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.

Test a > i64 case ?

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

code change lgtm but should add the missing test cases

@Xylecrack Xylecrack force-pushed the gisel-step-vector branch from 6fc0a25 to 8dcf051 Compare April 9, 2026 05:29
@arsenm arsenm merged commit a9b863e into llvm:main Apr 9, 2026
10 checks passed
@llvm-ci

llvm-ci commented Apr 9, 2026

Copy link
Copy Markdown

LLVM Buildbot has detected a new failure on builder intel-sycl-gpu running on intel-sycl-gpu-01 while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/225/builds/5447

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'libomptarget :: x86_64-unknown-linux-gnu :: mapping/map_ordering_tgt_exit_data_delete_from.c' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
/home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./bin/clang -fopenmp    -I /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/llvm-project/offload/test -I /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/openmp/runtime/src -L /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/offload -L /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./lib -L /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./lib -L /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/openmp/runtime/src  -Wl,-rpath,/home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/offload -Wl,-rpath,/home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/openmp/runtime/src -Wl,-rpath,/home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./lib -Wl,-rpath,/home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./lib  -fopenmp-targets=x86_64-unknown-linux-gnu /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/llvm-project/offload/test/mapping/map_ordering_tgt_exit_data_delete_from.c -o /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/offload/test/x86_64-unknown-linux-gnu/mapping/Output/map_ordering_tgt_exit_data_delete_from.c.tmp && /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/offload/test/x86_64-unknown-linux-gnu/mapping/Output/map_ordering_tgt_exit_data_delete_from.c.tmp | /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./bin/FileCheck /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/llvm-project/offload/test/mapping/map_ordering_tgt_exit_data_delete_from.c
# executed command: /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./bin/clang -fopenmp -I /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/llvm-project/offload/test -I /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/openmp/runtime/src -L /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/offload -L /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./lib -L /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./lib -L /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/openmp/runtime/src -Wl,-rpath,/home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/offload -Wl,-rpath,/home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/openmp/runtime/src -Wl,-rpath,/home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./lib -Wl,-rpath,/home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./lib -fopenmp-targets=x86_64-unknown-linux-gnu /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/llvm-project/offload/test/mapping/map_ordering_tgt_exit_data_delete_from.c -o /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/offload/test/x86_64-unknown-linux-gnu/mapping/Output/map_ordering_tgt_exit_data_delete_from.c.tmp
# executed command: /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/runtimes/runtimes-bins/offload/test/x86_64-unknown-linux-gnu/mapping/Output/map_ordering_tgt_exit_data_delete_from.c.tmp
# executed command: /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/build/./bin/FileCheck /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/llvm-project/offload/test/mapping/map_ordering_tgt_exit_data_delete_from.c
# .---command stderr------------
# | /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/llvm-project/offload/test/mapping/map_ordering_tgt_exit_data_delete_from.c:13:43: error: CHECK-NOT: excluded string found in input
# |  printf("In tgt: %d\n", x); // CHECK-NOT: In tgt: 111
# |                                           ^
# | <stdin>:1:1: note: found here
# | In tgt: 1113602848
# | ^~~~~~~~~~~
# | 
# | Input file: <stdin>
# | Check file: /home/test-user/llvm-buildbot-worker/intel-sycl-gpu/llvm-project/offload/test/mapping/map_ordering_tgt_exit_data_delete_from.c
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |         1: In tgt: 1113602848 
# | not:13     !~~~~~~~~~~         error: no match expected
# |         2: After tgt exit data: 222 
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1

--

********************


YonahGoldberg pushed a commit to YonahGoldberg/llvm-project that referenced this pull request Apr 21, 2026
This code is adapted from SelectionDAG::computeKnownBits.
part of llvm#150515.
ticks off STEP_VECTOR.
@Xylecrack Xylecrack deleted the gisel-step-vector branch June 10, 2026 15:57
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.

4 participants