Skip to content

Commit 4235e44

Browse files
authored
[GlobalISel] Constant-fold G_PTR_ADD with different type sizes (#81473)
All other opcodes in the list are constrained to have the same type on both operands, but not G_PTR_ADD. Fixes #81464
1 parent 1f99a45 commit 4235e44

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

llvm/lib/CodeGen/GlobalISel/Utils.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,11 @@ std::optional<APInt> llvm::ConstantFoldBinOp(unsigned Opcode,
660660
default:
661661
break;
662662
case TargetOpcode::G_ADD:
663-
case TargetOpcode::G_PTR_ADD:
664663
return C1 + C2;
664+
case TargetOpcode::G_PTR_ADD:
665+
// Types can be of different width here.
666+
// Result needs to be the same width as C1, so trunc or sext C2.
667+
return C1 + C2.sextOrTrunc(C1.getBitWidth());
665668
case TargetOpcode::G_AND:
666669
return C1 & C2;
667670
case TargetOpcode::G_ASHR:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=amdgcn -run-pass=amdgpu-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s
3+
4+
# Tries to emit a foldable G_PTR_ADD with (p1, s32) operands.
5+
---
6+
name: test_ptradd_crash__offset_smaller
7+
tracksRegLiveness: true
8+
body: |
9+
bb.0:
10+
; CHECK-LABEL: name: test_ptradd_crash__offset_smaller
11+
; CHECK: [[C:%[0-9]+]]:_(p1) = G_CONSTANT i64 12
12+
; CHECK-NEXT: [[LOAD:%[0-9]+]]:_(s32) = G_LOAD [[C]](p1) :: (load (s32), addrspace 1)
13+
; CHECK-NEXT: $sgpr0 = COPY [[LOAD]](s32)
14+
; CHECK-NEXT: SI_RETURN_TO_EPILOG implicit $sgpr0
15+
%1:_(p1) = G_CONSTANT i64 0
16+
%3:_(s32) = G_CONSTANT i32 3
17+
%0:_(<4 x s32>) = G_LOAD %1 :: (load (<4 x s32>) from `ptr addrspace(1) null`, addrspace 1)
18+
%2:_(s32) = G_EXTRACT_VECTOR_ELT %0, %3
19+
$sgpr0 = COPY %2
20+
SI_RETURN_TO_EPILOG implicit $sgpr0
21+
...
22+
23+
# Tries to emit a foldable G_PTR_ADD with (p1, s128) operands.
24+
---
25+
name: test_ptradd_crash__offset_wider
26+
tracksRegLiveness: true
27+
body: |
28+
bb.0:
29+
; CHECK-LABEL: name: test_ptradd_crash__offset_wider
30+
; CHECK: [[C:%[0-9]+]]:_(p1) = G_CONSTANT i64 12
31+
; CHECK-NEXT: [[LOAD:%[0-9]+]]:_(s32) = G_LOAD [[C]](p1) :: (load (s32), addrspace 1)
32+
; CHECK-NEXT: $sgpr0 = COPY [[LOAD]](s32)
33+
; CHECK-NEXT: SI_RETURN_TO_EPILOG implicit $sgpr0
34+
%1:_(p1) = G_CONSTANT i64 0
35+
%3:_(s128) = G_CONSTANT i128 3
36+
%0:_(<4 x s32>) = G_LOAD %1 :: (load (<4 x s32>) from `ptr addrspace(1) null`, addrspace 1)
37+
%2:_(s32) = G_EXTRACT_VECTOR_ELT %0, %3
38+
$sgpr0 = COPY %2
39+
SI_RETURN_TO_EPILOG implicit $sgpr0
40+
...

0 commit comments

Comments
 (0)