[llvm] [AArch64] Preserve store width for ptr32 address spaces (PR #229181)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 11:47:58 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-aarch64

Author: Daniel Paoliello (dpaoliello)

<details>
<summary>Changes</summary>

When handling stores for `ptr32` address spaces, we didn't check if they were already truncating stores and emitted an ordinary store. This widened `i8` and `i16` writes to 32 bits and could overwrite adjacent fields.

Fix is to build a truncating store that preserves the memory type.

Fixes #<!-- -->228782

Assisted-by: GPT-6 Sol (via VS Code)

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


2 Files Affected:

- (modified) llvm/lib/Target/AArch64/AArch64ISelLowering.cpp (+3) 
- (modified) llvm/test/CodeGen/AArch64/aarch64-mixed-ptr-sizes.ll (+48-3) 


``````````diff
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index ac8700d09491b..24f34fad6c018 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -27951,6 +27951,9 @@ static SDValue performSTORECombine(SDNode *N,
     MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
     if (PtrVT != Ptr.getSimpleValueType()) {
       SDValue Cast = DAG.getAddrSpaceCast(DL, PtrVT, Ptr, AddrSpace, 0);
+      if (ST->isTruncatingStore())
+        return DAG.getTruncStore(Chain, DL, Value, Cast, MemVT,
+                                 ST->getMemOperand());
       return DAG.getStore(Chain, DL, Value, Cast, ST->getPointerInfo(),
                           ST->getBaseAlign(), ST->getMemOperand()->getFlags(),
                           ST->getAAInfo());
diff --git a/llvm/test/CodeGen/AArch64/aarch64-mixed-ptr-sizes.ll b/llvm/test/CodeGen/AArch64/aarch64-mixed-ptr-sizes.ll
index 74e0aaf752bbb..96a72ce726c26 100644
--- a/llvm/test/CodeGen/AArch64/aarch64-mixed-ptr-sizes.ll
+++ b/llvm/test/CodeGen/AArch64/aarch64-mixed-ptr-sizes.ll
@@ -1,7 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc < %s | FileCheck %s
-; RUN: llc --fast-isel < %s | FileCheck %s
-; RUN: llc --global-isel --global-isel-abort=2 < %s 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK-GI
+; RUN: llc < %s | FileCheck %s --check-prefixes=CHECK,NARROW
+; RUN: llc --fast-isel < %s | FileCheck %s --check-prefixes=CHECK,NARROW
+; RUN: llc --global-isel --global-isel-abort=2 < %s 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK-GI,NARROW
+; RUN: llc -O0 < %s | FileCheck %s --check-prefix=NARROW
 
 ; CHECK-GI:       warning: Instruction selection used fallback path for test_sign_ext
 ; CHECK-GI-NEXT:  warning: Instruction selection used fallback path for test_sign_ext_store_load
@@ -218,5 +219,49 @@ entry:
   ret void
 }
 
+define void @test_store_i8_sptr(ptr addrspace(270) %p, i8 %v) {
+; NARROW-LABEL: test_store_i8_sptr:
+; NARROW:       strb w1, [x{{[0-9]+}}]
+; NARROW-NEXT:  ret
+  store i8 %v, ptr addrspace(270) %p, align 1
+  ret void
+}
+
+define void @test_store_i16_sptr(ptr addrspace(270) %p, i16 %v) {
+; NARROW-LABEL: test_store_i16_sptr:
+; NARROW:       strh w1, [x{{[0-9]+}}]
+; NARROW-NEXT:  ret
+  store i16 %v, ptr addrspace(270) %p, align 2
+  ret void
+}
+
+define void @test_store_i8_uptr(ptr addrspace(271) %p, i8 %v) {
+; NARROW-LABEL: test_store_i8_uptr:
+; NARROW:       strb w1, [x{{[0-9]+}}]
+; NARROW-NEXT:  ret
+  store i8 %v, ptr addrspace(271) %p, align 1
+  ret void
+}
+
+define void @test_store_i16_uptr(ptr addrspace(271) %p, i16 %v) {
+; NARROW-LABEL: test_store_i16_uptr:
+; NARROW:       strh w1, [x{{[0-9]+}}]
+; NARROW-NEXT:  ret
+  store i16 %v, ptr addrspace(271) %p, align 2
+  ret void
+}
+
+define void @test_copy6_uptr(ptr addrspace(271) %dst, ptr %src) {
+; CHECK-LABEL: test_copy6_uptr:
+; CHECK:       str w{{[0-9]+}}, [x{{[0-9]+}}]
+; CHECK-NEXT:  strh w{{[0-9]+}}, [x{{[0-9]+}}]
+; CHECK-NEXT:  ret
+  call void @llvm.memcpy.p271.p0.i64(ptr addrspace(271) align 2 %dst,
+                                     ptr align 2 %src, i64 6, i1 false)
+  ret void
+}
+
+declare void @llvm.memcpy.p271.p0.i64(ptr addrspace(271), ptr, i64, i1 immarg)
+
 attributes #0 = { mustprogress uwtable "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+fp-armv8,+neon,+v8a,-fmv" }
 attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+fp-armv8,+neon,+v8a,-fmv" }

``````````

</details>


https://github.com/llvm/llvm-project/pull/229181


More information about the llvm-commits mailing list