Skip to content

[LLVM][AsmPrinter] Add vector ConstantInt/FP support to emitGlobalConstantImpl. #120077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3602,10 +3602,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;
Expand All @@ -3618,7 +3619,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");
Expand All @@ -3627,12 +3628,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());
Expand Down Expand Up @@ -3902,8 +3902,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()
Expand All @@ -3920,8 +3922,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);
Expand Down Expand Up @@ -3953,8 +3959,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.
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
Loading