Skip to content

Commit 80b79ce

Browse files
authored
[ConstantFolding] Handle reading from type padding (#144330)
ReadDataFromGlobal() did not handle reads from the padding of types (in the sense of type store size != type alloc size, rather than struct padding). Return zero in that case. Fixes #144279.
1 parent 7e6c1bd commit 80b79ce

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
432432
assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&
433433
"Out of range access");
434434

435+
// Reading type padding, return zero.
436+
if (ByteOffset >= DL.getTypeStoreSize(C->getType()))
437+
return true;
438+
435439
// If this element is zero or undefined, we can just return since *CurPtr is
436440
// zero initialized.
437441
if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))

llvm/test/Transforms/InstSimplify/ConstProp/loads.ll

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,39 @@ define i128 @load-128bit(){
441441
%1 = load i128, ptr @global128, align 4
442442
ret i128 %1
443443
}
444+
445+
446+
@i40_struct = constant { i40, i8 } { i40 0, i8 1 }
447+
@i40_array = constant [2 x i40] [i40 0, i40 1]
448+
449+
define i8 @load_i40_struct_padding() {
450+
; CHECK-LABEL: @load_i40_struct_padding(
451+
; CHECK-NEXT: ret i8 0
452+
;
453+
%v = load i8, ptr getelementptr (i8, ptr @i40_struct, i64 6)
454+
ret i8 %v
455+
}
456+
457+
define i16 @load_i40_struct_partial_padding() {
458+
; CHECK-LABEL: @load_i40_struct_partial_padding(
459+
; CHECK-NEXT: ret i16 0
460+
;
461+
%v = load i16, ptr getelementptr (i8, ptr @i40_struct, i64 4)
462+
ret i16 %v
463+
}
464+
465+
define i8 @load_i40_array_padding() {
466+
; CHECK-LABEL: @load_i40_array_padding(
467+
; CHECK-NEXT: ret i8 0
468+
;
469+
%v = load i8, ptr getelementptr (i8, ptr @i40_array, i64 6)
470+
ret i8 %v
471+
}
472+
473+
define i16 @load_i40_array_partial_padding() {
474+
; CHECK-LABEL: @load_i40_array_partial_padding(
475+
; CHECK-NEXT: ret i16 0
476+
;
477+
%v = load i16, ptr getelementptr (i8, ptr @i40_array, i64 4)
478+
ret i16 %v
479+
}

0 commit comments

Comments
 (0)