Repository navigation
[SelectionDAG] Emit AssertZext for function argument range attributes - #196786
IrisUntainted wants to merge 6 commits into
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
a34ffeb to
e373fab
Compare
777dbd3 to
4cb20fe
Compare
|
@llvm/pr-subscribers-llvm-selectiondag @llvm/pr-subscribers-backend-x86 Author: Iris Shi (el-ev) ChangesLower range attributes on function arguments to AssertZext nodes during argument lowering. Downstream issue: rust-lang/rust#149673 4 Files Affected:
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
+}
|
e373fab to
90c1ba7
Compare
4cb20fe to
d66420c
Compare
90c1ba7 to
b645665
Compare
d66420c to
a79dea5
Compare
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
a79dea5 to
310084a
Compare
b645665 to
67d7ee6
Compare
|
|
||
| SDValue SelectionDAGBuilder::lowerRangeToAssertZExt( | ||
| SelectionDAG &DAG, std::optional<ConstantRange> CR, SDValue Op) { | ||
| if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped()) |
There was a problem hiding this comment.
Do we really permit the isFullSet and isEmptySet cases in the verifier?
There was a problem hiding this comment.
Not really.
llvm-project/llvm/lib/IR/Verifier.cpp
Lines 4615 to 4618 in 787048a
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
Technically this could be a bit broader. You can encode the range on an atomic load, which unfortunately is not LoadSDNode today
There was a problem hiding this comment.
so !isa<LoadSDNode, AtomicSDNode>(OutVal) here?
There was a problem hiding this comment.
Actually this context is specific to ABI lowering? Atomic won't realistically appear there
There was a problem hiding this comment.
I'm actually not as familiar with this area... Should we keep the guard or remove it?
There was a problem hiding this comment.
I'd probably introduce a canNodeEncodeRange() which comments the rationale and just returns isa<LoadSDNode>
Co-authored-by: Simon Pilgrim <[email protected]> Co-authored-by: Iris Shi <[email protected]>
35526e9 to
8b6c62f
Compare
8b6c62f to
a0da4a5
Compare

Lower range attributes on function arguments to AssertZext nodes during argument lowering.
Downstream issue: rust-lang/rust#149673