[llvm] 3146911 - [LLVM][AsmPrinter] Add vector ConstantInt/FP support to emitGlobalConstantImpl. (#120077)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 18 03:51:10 PST 2024
Author: Paul Walker
Date: 2024-12-18T11:51:01Z
New Revision: 3146911eb0eee821535444aa207a4ec5020c9c6a
URL: https://github.com/llvm/llvm-project/commit/3146911eb0eee821535444aa207a4ec5020c9c6a
DIFF: https://github.com/llvm/llvm-project/commit/3146911eb0eee821535444aa207a4ec5020c9c6a.diff
LOG: [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.
Added:
Modified:
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/lib/IR/Constants.cpp
llvm/test/CodeGen/AArch64/sve-streaming-mode-fixed-length-splat-vector.ll
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 47a93d624dfa9c..d2e60bb7f6318c 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3643,10 +3643,11 @@ static void emitGlobalConstantArray(const DataLayout &DL,
static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP);
-static void emitGlobalConstantVector(const DataLayout &DL,
- const ConstantVector *CV, AsmPrinter &AP,
+static void emitGlobalConstantVector(const DataLayout &DL, const Constant *CV,
+ AsmPrinter &AP,
AsmPrinter::AliasMapTy *AliasList) {
- Type *ElementType = CV->getType()->getElementType();
+ auto *VTy = cast<FixedVectorType>(CV->getType());
+ Type *ElementType = VTy->getElementType();
uint64_t ElementSizeInBits = DL.getTypeSizeInBits(ElementType);
uint64_t ElementAllocSizeInBits = DL.getTypeAllocSizeInBits(ElementType);
uint64_t EmittedSize;
@@ -3659,7 +3660,7 @@ static void emitGlobalConstantVector(const DataLayout &DL,
Type *IntT =
IntegerType::get(CV->getContext(), DL.getTypeSizeInBits(CV->getType()));
ConstantInt *CI = dyn_cast_or_null<ConstantInt>(ConstantFoldConstant(
- ConstantExpr::getBitCast(const_cast<ConstantVector *>(CV), IntT), DL));
+ ConstantExpr::getBitCast(const_cast<Constant *>(CV), IntT), DL));
if (!CI) {
report_fatal_error(
"Cannot lower vector global with unusual element type");
@@ -3668,12 +3669,11 @@ static void emitGlobalConstantVector(const DataLayout &DL,
emitGlobalConstantLargeInt(CI, AP);
EmittedSize = DL.getTypeStoreSize(CV->getType());
} else {
- for (unsigned I = 0, E = CV->getType()->getNumElements(); I != E; ++I) {
+ for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
emitGlobalAliasInline(AP, DL.getTypeAllocSize(CV->getType()) * I, AliasList);
- emitGlobalConstantImpl(DL, CV->getOperand(I), AP);
+ emitGlobalConstantImpl(DL, CV->getAggregateElement(I), AP);
}
- EmittedSize =
- DL.getTypeAllocSize(ElementType) * CV->getType()->getNumElements();
+ EmittedSize = DL.getTypeAllocSize(ElementType) * VTy->getNumElements();
}
unsigned Size = DL.getTypeAllocSize(CV->getType());
@@ -3943,8 +3943,10 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
return AP.OutStreamer->emitZeros(Size);
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
- const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType());
+ if (isa<VectorType>(CV->getType()))
+ return emitGlobalConstantVector(DL, CV, AP, AliasList);
+ const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType());
if (StoreSize <= 8) {
if (AP.isVerbose())
AP.OutStreamer->getCommentOS()
@@ -3961,8 +3963,12 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
return;
}
- if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
- return emitGlobalConstantFP(CFP, AP);
+ if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
+ if (isa<VectorType>(CV->getType()))
+ return emitGlobalConstantVector(DL, CV, AP, AliasList);
+ else
+ return emitGlobalConstantFP(CFP, AP);
+ }
if (isa<ConstantPointerNull>(CV)) {
AP.OutStreamer->emitIntValue(0, Size);
@@ -3994,8 +4000,8 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
}
}
- if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
- return emitGlobalConstantVector(DL, V, AP, AliasList);
+ if (isa<ConstantVector>(CV))
+ return emitGlobalConstantVector(DL, CV, AP, AliasList);
// Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
// thread the streamer with EmitValue.
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 949c23609c9d05..db5effbd9a43e7 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -451,6 +451,13 @@ Constant *Constant::getAggregateElement(unsigned Elt) const {
? ConstantInt::get(getContext(), CI->getValue())
: nullptr;
+ if (const auto *CFP = dyn_cast<ConstantFP>(this))
+ return Elt < cast<VectorType>(getType())
+ ->getElementCount()
+ .getKnownMinValue()
+ ? ConstantFP::get(getContext(), CFP->getValue())
+ : nullptr;
+
// FIXME: getNumElements() will fail for non-fixed vector types.
if (isa<ScalableVectorType>(getType()))
return nullptr;
diff --git a/llvm/test/CodeGen/AArch64/sve-streaming-mode-fixed-length-splat-vector.ll b/llvm/test/CodeGen/AArch64/sve-streaming-mode-fixed-length-splat-vector.ll
index a4cf5d608fed6d..96be762b4c8f67 100644
--- a/llvm/test/CodeGen/AArch64/sve-streaming-mode-fixed-length-splat-vector.ll
+++ b/llvm/test/CodeGen/AArch64/sve-streaming-mode-fixed-length-splat-vector.ll
@@ -1,8 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mattr=+sve -force-streaming-compatible < %s | FileCheck %s
; RUN: llc -force-streaming-compatible < %s | FileCheck %s --check-prefix=NONEON-NOSVE
-
-
+; 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
target triple = "aarch64-unknown-linux-gnu"
More information about the llvm-commits
mailing list