Skip to content

Commit 49ee674

Browse files
[NFC][LLVM][CodeGen][X86] Add ConstantInt/FP based vector support to MachineInstr fixup and printing code. (#137331)
When -use-constant-{int,fp}-for-fixed-length-splat are enabled, constant vector splats take the form of ConstantInt/FP instead of ConstantVector. These constants get linked to MachineInstrs via constant pools for later processing. The processing assumes ConstantInt/FP to always represent scalar constants with this PR extending the code to support vector types. NOTE: The test choices are somewhat artificial because pretty much all the vector tests failed without these changes when the new constants are enabled. --------- Co-authored-by: Matt Arsenault <[email protected]>
1 parent 7866c40 commit 49ee674

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

llvm/lib/Target/X86/X86FixupVectorConstants.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,19 @@ static std::optional<APInt> extractConstantBits(const Constant *C) {
8888
if (isa<UndefValue>(C))
8989
return APInt::getZero(NumBits);
9090

91-
if (auto *CInt = dyn_cast<ConstantInt>(C))
91+
if (auto *CInt = dyn_cast<ConstantInt>(C)) {
92+
if (isa<VectorType>(CInt->getType()))
93+
return APInt::getSplat(NumBits, CInt->getValue());
94+
9295
return CInt->getValue();
96+
}
97+
98+
if (auto *CFP = dyn_cast<ConstantFP>(C)) {
99+
if (isa<VectorType>(CFP->getType()))
100+
return APInt::getSplat(NumBits, CFP->getValue().bitcastToAPInt());
93101

94-
if (auto *CFP = dyn_cast<ConstantFP>(C))
95102
return CFP->getValue().bitcastToAPInt();
103+
}
96104

97105
if (auto *CV = dyn_cast<ConstantVector>(C)) {
98106
if (auto *CVSplat = getSplatValueAllowUndef(CV)) {

llvm/lib/Target/X86/X86MCInstLower.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,9 +1568,23 @@ static void printConstant(const Constant *COp, unsigned BitWidth,
15681568
if (isa<UndefValue>(COp)) {
15691569
CS << "u";
15701570
} else if (auto *CI = dyn_cast<ConstantInt>(COp)) {
1571-
printConstant(CI->getValue(), CS, PrintZero);
1571+
if (auto VTy = dyn_cast<FixedVectorType>(CI->getType())) {
1572+
for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
1573+
if (I != 0)
1574+
CS << ',';
1575+
printConstant(CI->getValue(), CS, PrintZero);
1576+
}
1577+
} else
1578+
printConstant(CI->getValue(), CS, PrintZero);
15721579
} else if (auto *CF = dyn_cast<ConstantFP>(COp)) {
1573-
printConstant(CF->getValueAPF(), CS, PrintZero);
1580+
if (auto VTy = dyn_cast<FixedVectorType>(CF->getType())) {
1581+
for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
1582+
if (I != 0)
1583+
CS << ',';
1584+
printConstant(CF->getValueAPF(), CS, PrintZero);
1585+
}
1586+
} else
1587+
printConstant(CF->getValueAPF(), CS, PrintZero);
15741588
} else if (auto *CDS = dyn_cast<ConstantDataSequential>(COp)) {
15751589
Type *EltTy = CDS->getElementType();
15761590
bool IsInteger = EltTy->isIntegerTy();

llvm/test/CodeGen/X86/sse2.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx | FileCheck %s --check-prefixes=AVX,X64-AVX,AVX1,X64-AVX1
77
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx512f,+avx512bw,+avx512dq,+avx512vl | FileCheck %s --check-prefixes=AVX,X64-AVX,AVX512,X64-AVX512
88

9+
; 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
10+
911
; Tests for SSE2 and below, without SSE3+.
1012

1113
define void @test1(ptr %r, ptr %A, double %B) nounwind {

llvm/test/CodeGen/X86/vector-shuffle-128-v16.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+xop,+avx | FileCheck %s --check-prefixes=ALL,AVX,AVX1OR2,XOP,XOPAVX1
1414
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+xop,+avx2 | FileCheck %s --check-prefixes=ALL,AVX,AVX1OR2,XOP,XOPAVX2
1515

16+
; 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
17+
1618
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) {
1719
; SSE2-LABEL: shuffle_v16i8_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00:
1820
; SSE2: # %bb.0:

0 commit comments

Comments
 (0)