Skip to content

[NFC][LLVM][CodeGen][X86] Add ConstantInt/FP based vector support to MachineInstr fixup and printing code. #137331

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions llvm/lib/Target/X86/X86FixupVectorConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,19 @@ static std::optional<APInt> extractConstantBits(const Constant *C) {
if (isa<UndefValue>(C))
return APInt::getZero(NumBits);

if (auto *CInt = dyn_cast<ConstantInt>(C))
if (auto *CInt = dyn_cast<ConstantInt>(C)) {
if (isa<VectorType>(CInt->getType()))
return APInt::getSplat(NumBits, CInt->getValue());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what this is doing. How is this different than the direct getValue? How is this a splat if APInt doesn't have vectors?

Copy link
Collaborator Author

@paulwalker-arm paulwalker-arm Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that vector constants are represented as vector-size-integers. I just copied the handling done for ConstantVector based splats.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having to do this everywhere, could we not add a helper function to ConstantInt/ConstantFP ?

Copy link
Collaborator Author

@paulwalker-arm paulwalker-arm Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No objections but is it worth waiting to see what everywhere means first? because up to press this is the first time I've needed this idiom and the IR side is mostly complete.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the first time I've seen ConstantInt/FP being used as splats like this - hiding the handling inside existing use cases feels like it going to cause people to miss this in future as well. All I had in mind was a ConstantInt/FP::getRawBits() wrapper.


return CInt->getValue();
}

if (auto *CFP = dyn_cast<ConstantFP>(C)) {
if (isa<VectorType>(CFP->getType()))
return APInt::getSplat(NumBits, CFP->getValue().bitcastToAPInt());

if (auto *CFP = dyn_cast<ConstantFP>(C))
return CFP->getValue().bitcastToAPInt();
}

if (auto *CV = dyn_cast<ConstantVector>(C)) {
if (auto *CVSplat = getSplatValueAllowUndef(CV)) {
Expand Down
18 changes: 16 additions & 2 deletions llvm/lib/Target/X86/X86MCInstLower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1568,9 +1568,23 @@ static void printConstant(const Constant *COp, unsigned BitWidth,
if (isa<UndefValue>(COp)) {
CS << "u";
} else if (auto *CI = dyn_cast<ConstantInt>(COp)) {
printConstant(CI->getValue(), CS, PrintZero);
if (auto VTy = dyn_cast<FixedVectorType>(CI->getType())) {
for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
if (I != 0)
CS << ',';
printConstant(CI->getValue(), CS, PrintZero);
}
} else
printConstant(CI->getValue(), CS, PrintZero);
} else if (auto *CF = dyn_cast<ConstantFP>(COp)) {
printConstant(CF->getValueAPF(), CS, PrintZero);
if (auto VTy = dyn_cast<FixedVectorType>(CF->getType())) {
for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
if (I != 0)
CS << ',';
printConstant(CF->getValueAPF(), CS, PrintZero);
}
} else
printConstant(CF->getValueAPF(), CS, PrintZero);
} else if (auto *CDS = dyn_cast<ConstantDataSequential>(COp)) {
Type *EltTy = CDS->getElementType();
bool IsInteger = EltTy->isIntegerTy();
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/X86/sse2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx | FileCheck %s --check-prefixes=AVX,X64-AVX,AVX1,X64-AVX1
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx512f,+avx512bw,+avx512dq,+avx512vl | FileCheck %s --check-prefixes=AVX,X64-AVX,AVX512,X64-AVX512

; RUN: llc < %s -mtriple=i386-unknown-unknown -mattr=+sse2 -use-constant-int-for-fixed-length-splat -use-constant-fp-for-fixed-length-splat | FileCheck %s --check-prefixes=SSE,X86-SSE

; Tests for SSE2 and below, without SSE3+.

define void @test1(ptr %r, ptr %A, double %B) nounwind {
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/X86/vector-shuffle-128-v16.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+xop,+avx | FileCheck %s --check-prefixes=ALL,AVX,AVX1OR2,XOP,XOPAVX1
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+xop,+avx2 | FileCheck %s --check-prefixes=ALL,AVX,AVX1OR2,XOP,XOPAVX2

; RUN: llc < %s -mtriple=x86_64-unknown-unknown -use-constant-int-for-fixed-length-splat -use-constant-fp-for-fixed-length-splat | FileCheck %s --check-prefixes=ALL,SSE,SSE2

define <16 x i8> @shuffle_v16i8_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00(<16 x i8> %a, <16 x i8> %b) {
; SSE2-LABEL: shuffle_v16i8_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00:
; SSE2: # %bb.0:
Expand Down
Loading