Skip to content

Commit e438513

Browse files
[AIX][AsmPrinter] Fix unsigned subtraction wrap-around (#122214)
Unsigned subtraction wrap-around occurs in `emitGlobalConstantImpl` on an AIX-specific code path from 8e4423e when a structure type has zero elements. With assertions enabled, this manifests as: ``` TypeSize llvm::StructLayout::getElementOffset(unsigned int) const: Assertion `Idx < NumElements && "Invalid element idx!"' failed. ```
1 parent c6b7bd4 commit e438513

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3914,21 +3914,22 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
39143914
if (isa<ConstantAggregateZero>(CV)) {
39153915
StructType *structType;
39163916
if (AliasList && (structType = llvm::dyn_cast<StructType>(CV->getType()))) {
3917-
// Handle cases of aliases to direct struct elements
3918-
const StructLayout *Layout = DL.getStructLayout(structType);
3919-
uint64_t SizeSoFar = 0;
3920-
for (unsigned int i = 0, n = structType->getNumElements(); i < n - 1;
3921-
++i) {
3922-
uint64_t GapToNext = Layout->getElementOffset(i + 1) - SizeSoFar;
3923-
AP.OutStreamer->emitZeros(GapToNext);
3924-
SizeSoFar += GapToNext;
3925-
emitGlobalAliasInline(AP, Offset + SizeSoFar, AliasList);
3917+
unsigned numElements = {structType->getNumElements()};
3918+
if (numElements != 0) {
3919+
// Handle cases of aliases to direct struct elements
3920+
const StructLayout *Layout = DL.getStructLayout(structType);
3921+
uint64_t SizeSoFar = 0;
3922+
for (unsigned int i = 0; i < numElements - 1; ++i) {
3923+
uint64_t GapToNext = Layout->getElementOffset(i + 1) - SizeSoFar;
3924+
AP.OutStreamer->emitZeros(GapToNext);
3925+
SizeSoFar += GapToNext;
3926+
emitGlobalAliasInline(AP, Offset + SizeSoFar, AliasList);
3927+
}
3928+
AP.OutStreamer->emitZeros(Size - SizeSoFar);
3929+
return;
39263930
}
3927-
AP.OutStreamer->emitZeros(Size - SizeSoFar);
3928-
return;
3929-
} else {
3930-
return AP.OutStreamer->emitZeros(Size);
39313931
}
3932+
return AP.OutStreamer->emitZeros(Size);
39323933
}
39333934

39343935
if (isa<UndefValue>(CV))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff -mcpu=pwr7 < %s | FileCheck %s
2+
3+
; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff -mcpu=pwr7 --filetype=obj -o %t.o < %s
4+
; RUN: llvm-objdump --syms %t.o | FileCheck %s --check-prefix=OBJ
5+
6+
%struct.anon = type {}
7+
8+
@a = internal constant %struct.anon zeroinitializer, align 1
9+
@b = internal constant [6 x i8] c"hello\00", align 1
10+
11+
; CHECK: .csect L.._MergedGlobals[RO],2
12+
; CHECK-NEXT: .lglobl a # @_MergedGlobals
13+
; CHECK-NEXT: .lglobl b
14+
; CHECK-NEXT: a:
15+
; CHECK-NEXT: b:
16+
; CHECK-NEXT: .string "hello"
17+
18+
; OBJ: 0000000000000000 l .text 0000000000000006 L.._MergedGlobals
19+
; OBJ-NEXT: 0000000000000000 l .text (csect: L.._MergedGlobals) 0000000000000000 a
20+
; OBJ-NEXT: 0000000000000000 l .text (csect: L.._MergedGlobals) 0000000000000000 b

0 commit comments

Comments
 (0)