From feea4ce32484368a7f486037f87c6a6ecb940175 Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Thu, 24 Apr 2025 16:42:16 +0000 Subject: [PATCH 1/2] [LLVM][CodeGen][X86] Add ConstantInt/FP based vector support to MachineInst fixup and printing code. 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 MachineInsts 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. --- .../lib/Target/X86/X86FixupVectorConstants.cpp | 12 ++++++++++-- llvm/lib/Target/X86/X86MCInstLower.cpp | 18 ++++++++++++++++-- llvm/test/CodeGen/X86/sse2.ll | 2 ++ .../test/CodeGen/X86/vector-shuffle-128-v16.ll | 2 ++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Target/X86/X86FixupVectorConstants.cpp b/llvm/lib/Target/X86/X86FixupVectorConstants.cpp index 2c870d1171658..a415a45775984 100644 --- a/llvm/lib/Target/X86/X86FixupVectorConstants.cpp +++ b/llvm/lib/Target/X86/X86FixupVectorConstants.cpp @@ -88,11 +88,19 @@ static std::optional extractConstantBits(const Constant *C) { if (isa(C)) return APInt::getZero(NumBits); - if (auto *CInt = dyn_cast(C)) + if (auto *CInt = dyn_cast(C)) { + if (isa(CInt->getType())) + return APInt::getSplat(NumBits, CInt->getValue()); + return CInt->getValue(); + } + + if (auto *CFP = dyn_cast(C)) { + if (isa(CFP->getType())) + return APInt::getSplat(NumBits, CFP->getValue().bitcastToAPInt()); - if (auto *CFP = dyn_cast(C)) return CFP->getValue().bitcastToAPInt(); + } if (auto *CV = dyn_cast(C)) { if (auto *CVSplat = getSplatValueAllowUndef(CV)) { diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp index d9945bdf2db60..07cbd54c160df 100644 --- a/llvm/lib/Target/X86/X86MCInstLower.cpp +++ b/llvm/lib/Target/X86/X86MCInstLower.cpp @@ -1568,9 +1568,23 @@ static void printConstant(const Constant *COp, unsigned BitWidth, if (isa(COp)) { CS << "u"; } else if (auto *CI = dyn_cast(COp)) { - printConstant(CI->getValue(), CS, PrintZero); + if (auto VTy = dyn_cast(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(COp)) { - printConstant(CF->getValueAPF(), CS, PrintZero); + if (auto VTy = dyn_cast(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(COp)) { Type *EltTy = CDS->getElementType(); bool IsInteger = EltTy->isIntegerTy(); diff --git a/llvm/test/CodeGen/X86/sse2.ll b/llvm/test/CodeGen/X86/sse2.ll index cf5f527b16114..3e5d76eae0bb3 100644 --- a/llvm/test/CodeGen/X86/sse2.ll +++ b/llvm/test/CodeGen/X86/sse2.ll @@ -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 { diff --git a/llvm/test/CodeGen/X86/vector-shuffle-128-v16.ll b/llvm/test/CodeGen/X86/vector-shuffle-128-v16.ll index 442bfde6a9e2e..152814fbc631b 100644 --- a/llvm/test/CodeGen/X86/vector-shuffle-128-v16.ll +++ b/llvm/test/CodeGen/X86/vector-shuffle-128-v16.ll @@ -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: From 58220fcbb1793e25aded8a6b9b14de595a1c143b Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Fri, 25 Apr 2025 15:35:49 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Matt Arsenault --- llvm/lib/Target/X86/X86MCInstLower.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp index 07cbd54c160df..fd97208042871 100644 --- a/llvm/lib/Target/X86/X86MCInstLower.cpp +++ b/llvm/lib/Target/X86/X86MCInstLower.cpp @@ -1571,7 +1571,7 @@ static void printConstant(const Constant *COp, unsigned BitWidth, if (auto VTy = dyn_cast(CI->getType())) { for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { if (I != 0) - CS << ","; + CS << ','; printConstant(CI->getValue(), CS, PrintZero); } } else @@ -1580,7 +1580,7 @@ static void printConstant(const Constant *COp, unsigned BitWidth, if (auto VTy = dyn_cast(CF->getType())) { for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { if (I != 0) - CS << ","; + CS << ','; printConstant(CF->getValueAPF(), CS, PrintZero); } } else