Skip to content

[SelectionDAG] Emit AssertZext for function argument range attributes - #196786

Open
IrisUntainted wants to merge 6 commits into
mainfrom
users/el-ev/_selectiondag_emit_assertzext_for_function_argument_range_attributes
Open

IrisUntainted wants to merge 6 commits into
mainfrom
users/el-ev/_selectiondag_emit_assertzext_for_function_argument_range_attributes

Conversation

@IrisUntainted

@IrisUntainted IrisUntainted commented May 10, 2026 •

Copy link
Copy Markdown
Member

Lower range attributes on function arguments to AssertZext nodes during argument lowering.

Downstream issue: rust-lang/rust#149673

IrisUntainted commented May 10, 2026 •

Copy link
Copy Markdown
Member Author

@github-actions

github-actions Bot commented May 10, 2026 •

Copy link
Copy Markdown

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

@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_emit_assertzext_for_function_argument_range_attributes branch from a34ffeb to e373fab Compare May 10, 2026 07:41
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext branch from 777dbd3 to 4cb20fe Compare May 10, 2026 07:41
@IrisUntainted
IrisUntainted requested review from RKSimon and arsenm May 10, 2026 07:47
@IrisUntainted
IrisUntainted marked this pull request as ready for review May 10, 2026 07:47
@llvmorg-github-actions

llvmorg-github-actions Bot commented May 10, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-selectiondag
@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-backend-x86

Author: Iris Shi (el-ev)

Changes

Lower range attributes on function arguments to AssertZext nodes during argument lowering.

Downstream issue: rust-lang/rust#149673


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

4 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (+6-1)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h (+2)
  • (modified) llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll (+3-3)
  • (added) llvm/test/CodeGen/X86/argument-range-attr.ll (+121)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index c001f0e0bd450..08fc33644e5b7 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -10848,8 +10848,11 @@ void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
 SDValue SelectionDAGBuilder::lowerRangeToAssertZExt(SelectionDAG &DAG,
                                                     const Instruction &I,
                                                     SDValue Op) {
-  std::optional<ConstantRange> CR = getRange(I);
+  return lowerRangeToAssertZExt(DAG, getRange(I), Op);
+}
 
+SDValue SelectionDAGBuilder::lowerRangeToAssertZExt(
+    SelectionDAG &DAG, std::optional<ConstantRange> CR, SDValue Op) {
   if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
     return Op;
 
@@ -12199,6 +12202,8 @@ void SelectionDAGISel::LowerArguments(const Function &F) {
           OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
                                OutVal, SDNoFPClass);
         }
+        if (NumValues == 1 && VT.isInteger())
+          OutVal = SDB->lowerRangeToAssertZExt(DAG, Arg.getRange(), OutVal);
         ArgValues.push_back(OutVal);
       }
 
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
index 21aac333a73cd..7edf005586989 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
@@ -429,6 +429,8 @@ class SelectionDAGBuilder {
   // floor power of two.
   SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I,
                                  SDValue Op);
+  SDValue lowerRangeToAssertZExt(SelectionDAG &DAG,
+                                 std::optional<ConstantRange> CR, SDValue Op);
 
   // Lower nofpclass attributes to AssertNoFPClass
   SDValue lowerNoFPClassToAssertNoFPClass(SelectionDAG &DAG,
diff --git a/llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll b/llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll
index ad26dfa7f93e8..a8be7e5827af0 100644
--- a/llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll
+++ b/llvm/test/CodeGen/AMDGPU/bit-op-reduce-width-known-bits.ll
@@ -11,7 +11,7 @@ define i64 @v_xor_i64_known_hi_i32_from_arg_range(i64 range(i64 0, 4294967296) %
 ; CHECK-LABEL: v_xor_i64_known_hi_i32_from_arg_range:
 ; CHECK:       ; %bb.0:
 ; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; CHECK-NEXT:    v_xor_b32_e32 v1, v1, v3
+; CHECK-NEXT:    v_mov_b32_e32 v1, v3
 ; CHECK-NEXT:    v_xor_b32_e32 v0, v0, v2
 ; CHECK-NEXT:    s_setpc_b64 s[30:31]
   %xor = xor i64 %arg0, %arg1
@@ -24,7 +24,7 @@ define i64 @v_or_i64_known_hi_i32_from_arg_range(i64 range(i64 0, 4294967296) %a
 ; CHECK-LABEL: v_or_i64_known_hi_i32_from_arg_range:
 ; CHECK:       ; %bb.0:
 ; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; CHECK-NEXT:    v_or_b32_e32 v1, v1, v3
+; CHECK-NEXT:    v_mov_b32_e32 v1, v3
 ; CHECK-NEXT:    v_or_b32_e32 v0, v0, v2
 ; CHECK-NEXT:    s_setpc_b64 s[30:31]
   %or = or i64 %arg0, %arg1
@@ -50,7 +50,7 @@ define i64 @s_xor_i64_known_i32_from_arg_range(i64 range(i64 0, 65) inreg %arg)
 ; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
 ; CHECK-NEXT:    s_not_b64 s[4:5], s[16:17]
 ; CHECK-NEXT:    v_mov_b32_e32 v0, s4
-; CHECK-NEXT:    v_mov_b32_e32 v1, s5
+; CHECK-NEXT:    v_mov_b32_e32 v1, -1
 ; CHECK-NEXT:    s_setpc_b64 s[30:31]
   %xor = xor i64 %arg, -1
   ret i64 %xor
diff --git a/llvm/test/CodeGen/X86/argument-range-attr.ll b/llvm/test/CodeGen/X86/argument-range-attr.ll
new file mode 100644
index 0000000000000..9d684f7f596b2
--- /dev/null
+++ b/llvm/test/CodeGen/X86/argument-range-attr.ll
@@ -0,0 +1,121 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
+; RUN: llc -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+
+; range: 0..2^61
+; mask: 2^61 - 8
+define i64 @arg_range_top3(i64 range(i64 0, 2305843009213693952) %n) {
+; CHECK-LABEL: arg_range_top3:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq %rdi, %rax
+; CHECK-NEXT:    andq $-8, %rax
+; CHECK-NEXT:    retq
+  %r = and i64 %n, 2305843009213693944
+  ret i64 %r
+}
+
+; range: 0..256
+; mask: 0xF8 = 248
+define i64 @arg_range_byte(i64 range(i64 0, 256) %n) {
+; CHECK-LABEL: arg_range_byte:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq %rdi, %rax
+; CHECK-NEXT:    andl $-8, %eax
+; CHECK-NEXT:    retq
+  %r = and i64 %n, 248
+  ret i64 %r
+}
+
+define i64 @arg_range_nonzero_lo(i64 range(i64 1, 2305843009213693952) %n) {
+; CHECK-LABEL: arg_range_nonzero_lo:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq %rdi, %rax
+; CHECK-NEXT:    andq $-8, %rax
+; CHECK-NEXT:    retq
+  %r = and i64 %n, 2305843009213693944
+  ret i64 %r
+}
+
+define i64 @arg_range_byte_nonzero_lo(i64 range(i64 1, 256) %n) {
+; CHECK-LABEL: arg_range_byte_nonzero_lo:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq %rdi, %rax
+; CHECK-NEXT:    andl $-8, %eax
+; CHECK-NEXT:    retq
+  %r = and i64 %n, 248
+  ret i64 %r
+}
+
+define i32 @arg_range_i32_byte(i32 range(i32 0, 256) %n) {
+; CHECK-LABEL: arg_range_i32_byte:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl %edi, %eax
+; CHECK-NEXT:    andl $-8, %eax
+; CHECK-NEXT:    retq
+  %r = and i32 %n, 248
+  ret i32 %r
+}
+
+define i64 @arg_range_top1(i64 range(i64 0, 9223372036854775808) %n) {
+; CHECK-LABEL: arg_range_top1:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:    retq
+  %r = lshr i64 %n, 63
+  ret i64 %r
+}
+
+define i64 @arg_range_two_args(i64 range(i64 0, 256) %a, i64 range(i64 0, 65536) %b) {
+; CHECK-LABEL: arg_range_two_args:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movq %rdi, %rax
+; CHECK-NEXT:    orl %esi, %eax
+; CHECK-NEXT:    andl $-8, %eax
+; CHECK-NEXT:    retq
+  %ra = and i64 %a, 248
+  %rb = and i64 %b, 65528
+  %r = or i64 %ra, %rb
+  ret i64 %r
+}
+
+define i32 @arg_range_zeroext(i8 zeroext range(i8 0, 16) %n) {
+; CHECK-LABEL: arg_range_zeroext:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl %edi, %eax
+; CHECK-NEXT:    andl $-2, %eax
+; CHECK-NEXT:    retq
+  %z = zext i8 %n to i32
+  %r = and i32 %z, 14
+  ret i32 %r
+}
+
+; Negative tests
+
+define i64 @neg_no_range(i64 %n) {
+; CHECK-LABEL: neg_no_range:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movabsq $2305843009213693944, %rax # imm = 0x1FFFFFFFFFFFFFF8
+; CHECK-NEXT:    andq %rdi, %rax
+; CHECK-NEXT:    retq
+  %r = and i64 %n, 2305843009213693944
+  ret i64 %r
+}
+
+define i64 @neg_wrapped_range(i64 range(i64 -100, 100) %n) {
+; CHECK-LABEL: neg_wrapped_range:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movabsq $2305843009213693944, %rax # imm = 0x1FFFFFFFFFFFFFF8
+; CHECK-NEXT:    andq %rdi, %rax
+; CHECK-NEXT:    retq
+  %r = and i64 %n, 2305843009213693944
+  ret i64 %r
+}
+
+define i32 @neg_near_full_range(i32 range(i32 0, -1) %n) {
+; CHECK-LABEL: neg_near_full_range:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl %edi, %eax
+; CHECK-NEXT:    andl $248, %eax
+; CHECK-NEXT:    retq
+  %r = and i32 %n, 248
+  ret i32 %r
+}

@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_emit_assertzext_for_function_argument_range_attributes branch from e373fab to 90c1ba7 Compare May 10, 2026 09:57
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext branch from 4cb20fe to d66420c Compare May 10, 2026 09:57
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_emit_assertzext_for_function_argument_range_attributes branch from 90c1ba7 to b645665 Compare May 11, 2026 10:36
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext branch from d66420c to a79dea5 Compare May 11, 2026 10:36
@github-actions

github-actions Bot commented May 11, 2026 •

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 195147 tests passed
  • 5196 tests skipped

✅ The build succeeded and all tests passed.

@github-actions

github-actions Bot commented May 11, 2026 •

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 134529 tests passed
  • 3263 tests skipped

✅ The build succeeded and all tests passed.

@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext branch from a79dea5 to 310084a Compare May 11, 2026 11:43
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_emit_assertzext_for_function_argument_range_attributes branch from b645665 to 67d7ee6 Compare May 11, 2026 11:43
Base automatically changed from users/el-ev/_selectiondag_drop_unnecessary_lower_bound_check_in_lowerrangetoassertzext to main May 11, 2026 14:52

SDValue SelectionDAGBuilder::lowerRangeToAssertZExt(
SelectionDAG &DAG, std::optional<ConstantRange> CR, SDValue Op) {
if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())

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.

Do we really permit the isFullSet and isEmptySet cases in the verifier?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not really.

Check(!CurRange.isEmptySet() &&
(Kind == RangeLikeMetadataKind::AbsoluteSymbol ||
!CurRange.isFullSet()),
"Range must not be empty!", Range);

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.

We do allow the empty set.

@IrisUntainted IrisUntainted May 11, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What about the full set?

They are both invalid per LangRef.

The range should not represent the full or empty set.


Added both checks back

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.

The rules are different for !range and range(). The former does not allow empty sets, the latter does.

OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
OutVal, SDNoFPClass);
}
if (NumValues == 1 && VT.isInteger() && !isa<LoadSDNode>(OutVal))

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.

Technically this could be a bit broader. You can encode the range on an atomic load, which unfortunately is not LoadSDNode today

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

so !isa<LoadSDNode, AtomicSDNode>(OutVal) here?

@IrisUntainted IrisUntainted May 11, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed.

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.

Actually this context is specific to ABI lowering? Atomic won't realistically appear there

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm actually not as familiar with this area... Should we keep the guard or remove it?

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.

I'd probably introduce a canNodeEncodeRange() which comments the rationale and just returns isa<LoadSDNode>

IrisUntainted and others added 2 commits May 11, 2026 23:15
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_emit_assertzext_for_function_argument_range_attributes branch from 35526e9 to 8b6c62f Compare May 11, 2026 15:16
@IrisUntainted
IrisUntainted force-pushed the users/el-ev/_selectiondag_emit_assertzext_for_function_argument_range_attributes branch from 8b6c62f to a0da4a5 Compare May 11, 2026 15:36
@github-actions
github-actions Bot deleted the users/el-ev/_selectiondag_emit_assertzext_for_function_argument_range_attributes branch November 5, 2026 08:10

This branch has not been deployed

No deployments
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