Skip to content

Commit 64a4bac

Browse files
committed
[ConstantFolding] Bail out when reading padding of type
ReadDataFromGlobal() did not handle reads from the padding of types (in the sense of type store size != type alloc size, rather than struct padding). Just bail out in that case. (Alternatively could make this return zero as well, but the value seems dubious.) Fixes #144279.
1 parent 3824a2d commit 64a4bac

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-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+
// Trying to read type padding.
436+
if (ByteOffset >= DL.getTypeStoreSize(C->getType()))
437+
return false;
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,41 @@ 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: [[V:%.*]] = load i8, ptr getelementptr (i8, ptr @i40_struct, i64 6), align 1
452+
; CHECK-NEXT: ret i8 [[V]]
453+
;
454+
%v = load i8, ptr getelementptr (i8, ptr @i40_struct, i64 6)
455+
ret i8 %v
456+
}
457+
458+
define i16 @load_i40_struct_partial_padding() {
459+
; CHECK-LABEL: @load_i40_struct_partial_padding(
460+
; CHECK-NEXT: ret i16 0
461+
;
462+
%v = load i16, ptr getelementptr (i8, ptr @i40_struct, i64 4)
463+
ret i16 %v
464+
}
465+
466+
define i8 @load_i40_array_padding() {
467+
; CHECK-LABEL: @load_i40_array_padding(
468+
; CHECK-NEXT: [[V:%.*]] = load i8, ptr getelementptr (i8, ptr @i40_array, i64 6), align 1
469+
; CHECK-NEXT: ret i8 [[V]]
470+
;
471+
%v = load i8, ptr getelementptr (i8, ptr @i40_array, i64 6)
472+
ret i8 %v
473+
}
474+
475+
define i16 @load_i40_array_partial_padding() {
476+
; CHECK-LABEL: @load_i40_array_partial_padding(
477+
; CHECK-NEXT: ret i16 0
478+
;
479+
%v = load i16, ptr getelementptr (i8, ptr @i40_array, i64 4)
480+
ret i16 %v
481+
}

0 commit comments

Comments
 (0)