Skip to content

Commit 3146911

Browse files
[LLVM][AsmPrinter] Add vector ConstantInt/FP support to emitGlobalConstantImpl. (#120077)
The fixes a failure path for fixed length vector globals when ConstantInt/FP is used to represent splats instead of ConstantDataVector.
1 parent 1941f34 commit 3146911

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,10 +3643,11 @@ static void emitGlobalConstantArray(const DataLayout &DL,
36433643

36443644
static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP);
36453645

3646-
static void emitGlobalConstantVector(const DataLayout &DL,
3647-
const ConstantVector *CV, AsmPrinter &AP,
3646+
static void emitGlobalConstantVector(const DataLayout &DL, const Constant *CV,
3647+
AsmPrinter &AP,
36483648
AsmPrinter::AliasMapTy *AliasList) {
3649-
Type *ElementType = CV->getType()->getElementType();
3649+
auto *VTy = cast<FixedVectorType>(CV->getType());
3650+
Type *ElementType = VTy->getElementType();
36503651
uint64_t ElementSizeInBits = DL.getTypeSizeInBits(ElementType);
36513652
uint64_t ElementAllocSizeInBits = DL.getTypeAllocSizeInBits(ElementType);
36523653
uint64_t EmittedSize;
@@ -3659,7 +3660,7 @@ static void emitGlobalConstantVector(const DataLayout &DL,
36593660
Type *IntT =
36603661
IntegerType::get(CV->getContext(), DL.getTypeSizeInBits(CV->getType()));
36613662
ConstantInt *CI = dyn_cast_or_null<ConstantInt>(ConstantFoldConstant(
3662-
ConstantExpr::getBitCast(const_cast<ConstantVector *>(CV), IntT), DL));
3663+
ConstantExpr::getBitCast(const_cast<Constant *>(CV), IntT), DL));
36633664
if (!CI) {
36643665
report_fatal_error(
36653666
"Cannot lower vector global with unusual element type");
@@ -3668,12 +3669,11 @@ static void emitGlobalConstantVector(const DataLayout &DL,
36683669
emitGlobalConstantLargeInt(CI, AP);
36693670
EmittedSize = DL.getTypeStoreSize(CV->getType());
36703671
} else {
3671-
for (unsigned I = 0, E = CV->getType()->getNumElements(); I != E; ++I) {
3672+
for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
36723673
emitGlobalAliasInline(AP, DL.getTypeAllocSize(CV->getType()) * I, AliasList);
3673-
emitGlobalConstantImpl(DL, CV->getOperand(I), AP);
3674+
emitGlobalConstantImpl(DL, CV->getAggregateElement(I), AP);
36743675
}
3675-
EmittedSize =
3676-
DL.getTypeAllocSize(ElementType) * CV->getType()->getNumElements();
3676+
EmittedSize = DL.getTypeAllocSize(ElementType) * VTy->getNumElements();
36773677
}
36783678

36793679
unsigned Size = DL.getTypeAllocSize(CV->getType());
@@ -3943,8 +3943,10 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
39433943
return AP.OutStreamer->emitZeros(Size);
39443944

39453945
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
3946-
const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType());
3946+
if (isa<VectorType>(CV->getType()))
3947+
return emitGlobalConstantVector(DL, CV, AP, AliasList);
39473948

3949+
const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType());
39483950
if (StoreSize <= 8) {
39493951
if (AP.isVerbose())
39503952
AP.OutStreamer->getCommentOS()
@@ -3961,8 +3963,12 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
39613963
return;
39623964
}
39633965

3964-
if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
3965-
return emitGlobalConstantFP(CFP, AP);
3966+
if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
3967+
if (isa<VectorType>(CV->getType()))
3968+
return emitGlobalConstantVector(DL, CV, AP, AliasList);
3969+
else
3970+
return emitGlobalConstantFP(CFP, AP);
3971+
}
39663972

39673973
if (isa<ConstantPointerNull>(CV)) {
39683974
AP.OutStreamer->emitIntValue(0, Size);
@@ -3994,8 +4000,8 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
39944000
}
39954001
}
39964002

3997-
if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
3998-
return emitGlobalConstantVector(DL, V, AP, AliasList);
4003+
if (isa<ConstantVector>(CV))
4004+
return emitGlobalConstantVector(DL, CV, AP, AliasList);
39994005

40004006
// Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
40014007
// thread the streamer with EmitValue.

llvm/lib/IR/Constants.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,13 @@ Constant *Constant::getAggregateElement(unsigned Elt) const {
451451
? ConstantInt::get(getContext(), CI->getValue())
452452
: nullptr;
453453

454+
if (const auto *CFP = dyn_cast<ConstantFP>(this))
455+
return Elt < cast<VectorType>(getType())
456+
->getElementCount()
457+
.getKnownMinValue()
458+
? ConstantFP::get(getContext(), CFP->getValue())
459+
: nullptr;
460+
454461
// FIXME: getNumElements() will fail for non-fixed vector types.
455462
if (isa<ScalableVectorType>(getType()))
456463
return nullptr;

llvm/test/CodeGen/AArch64/sve-streaming-mode-fixed-length-splat-vector.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
22
; RUN: llc -mattr=+sve -force-streaming-compatible < %s | FileCheck %s
33
; RUN: llc -force-streaming-compatible < %s | FileCheck %s --check-prefix=NONEON-NOSVE
4-
5-
4+
; RUN: llc -force-streaming-compatible -use-constant-int-for-fixed-length-splat -use-constant-fp-for-fixed-length-splat < %s | FileCheck %s --check-prefix=NONEON-NOSVE
65

76
target triple = "aarch64-unknown-linux-gnu"
87

0 commit comments

Comments
 (0)