[llvm] [LLVM][CodeGen][X86] Add ConstantInt/FP based vector support to MachineInstr fixup and printing code. (PR #137331)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 25 06:40:54 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-x86
Author: Paul Walker (paulwalker-arm)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/137331.diff
4 Files Affected:
- (modified) llvm/lib/Target/X86/X86FixupVectorConstants.cpp (+10-2)
- (modified) llvm/lib/Target/X86/X86MCInstLower.cpp (+16-2)
- (modified) llvm/test/CodeGen/X86/sse2.ll (+2)
- (modified) llvm/test/CodeGen/X86/vector-shuffle-128-v16.ll (+2)
``````````diff
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<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());
+
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)) {
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<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();
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:
``````````
</details>
https://github.com/llvm/llvm-project/pull/137331
More information about the llvm-commits
mailing list