[PATCH] D150515: [ConstantFold] use StoreSize for VectorType byte checking

Kohei Asano via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat May 13 17:14:55 PDT 2023


khei4 created this revision.
khei4 added reviewers: nikic, jsilvanus, RKSimon.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
khei4 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

While working on https://reviews.llvm.org/D150422. Using `Val.extractBits(8, n * 8).getZextValue()` on ReadDataFromGlobal in ConstantFolding crashed in this case. 
Checking StoreSize on VectorType seems to match the TODO comment in tests.


https://reviews.llvm.org/D150515

Files:
  llvm/lib/Analysis/ConstantFolding.cpp
  llvm/test/Transforms/InstCombine/load-gep-overalign.ll


Index: llvm/test/Transforms/InstCombine/load-gep-overalign.ll
===================================================================
--- llvm/test/Transforms/InstCombine/load-gep-overalign.ll
+++ llvm/test/Transforms/InstCombine/load-gep-overalign.ll
@@ -11,10 +11,6 @@
 ; Access and report each individual byte in @foo.
 ; OVERALIGNED and NATURAL should have the same result, because the layout of vectors ignores
 ; element type alignment, and thus the representation of @foo is the same in both cases.
-;
-; TODO: The OVERALIGNED result is incorrect, as apparently padding bytes
-; are assumed as they would appear in an array. In vectors, there is no padding.
-;
 ; NATURAL-LABEL: @test_vector_load_i8(
 ; NATURAL-NEXT:    call void @report(i64 0, i8 1)
 ; NATURAL-NEXT:    call void @report(i64 1, i8 35)
@@ -29,12 +25,12 @@
 ; OVERALIGNED-LABEL: @test_vector_load_i8(
 ; OVERALIGNED-NEXT:    call void @report(i64 0, i8 1)
 ; OVERALIGNED-NEXT:    call void @report(i64 1, i8 35)
-; OVERALIGNED-NEXT:    call void @report(i64 2, i8 0)
-; OVERALIGNED-NEXT:    call void @report(i64 3, i8 0)
-; OVERALIGNED-NEXT:    call void @report(i64 4, i8 69)
-; OVERALIGNED-NEXT:    call void @report(i64 5, i8 103)
-; OVERALIGNED-NEXT:    call void @report(i64 6, i8 0)
-; OVERALIGNED-NEXT:    call void @report(i64 7, i8 0)
+; OVERALIGNED-NEXT:    call void @report(i64 2, i8 69)
+; OVERALIGNED-NEXT:    call void @report(i64 3, i8 103)
+; OVERALIGNED-NEXT:    call void @report(i64 4, i8 -119)
+; OVERALIGNED-NEXT:    call void @report(i64 5, i8 -85)
+; OVERALIGNED-NEXT:    call void @report(i64 6, i8 -51)
+; OVERALIGNED-NEXT:    call void @report(i64 7, i8 -17)
 ; OVERALIGNED-NEXT:    ret void
 ;
   %ptr0 = getelementptr i8, ptr @foo, i64 0
Index: llvm/lib/Analysis/ConstantFolding.cpp
===================================================================
--- llvm/lib/Analysis/ConstantFolding.cpp
+++ llvm/lib/Analysis/ConstantFolding.cpp
@@ -501,16 +501,17 @@
 
   if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
       isa<ConstantDataSequential>(C)) {
-    uint64_t NumElts;
+    uint64_t NumElts, EltSize;
     Type *EltTy;
     if (auto *AT = dyn_cast<ArrayType>(C->getType())) {
       NumElts = AT->getNumElements();
       EltTy = AT->getElementType();
+      EltSize = DL.getTypeAllocSize(EltTy);
     } else {
       NumElts = cast<FixedVectorType>(C->getType())->getNumElements();
       EltTy = cast<FixedVectorType>(C->getType())->getElementType();
+      EltSize = DL.getTypeStoreSize(EltTy);
     }
-    uint64_t EltSize = DL.getTypeAllocSize(EltTy);
     uint64_t Index = ByteOffset / EltSize;
     uint64_t Offset = ByteOffset - Index * EltSize;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150515.521956.patch
Type: text/x-patch
Size: 2668 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230514/64cfb6cc/attachment.bin>


More information about the llvm-commits mailing list