[llvm] [LLVM][CodeGen][DirectX] Fix scalarisation when vector ConstantFP is used. (PR #172684)
Paul Walker via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 4 08:49:18 PST 2026
https://github.com/paulwalker-arm updated https://github.com/llvm/llvm-project/pull/172684
>From 4d480780f805708f70872d74a2ef1fc74127855b Mon Sep 17 00:00:00 2001
From: Paul Walker <paul.walker at arm.com>
Date: Tue, 16 Dec 2025 17:46:30 +0000
Subject: [PATCH 1/2] [LLVM][CodeGen][DirectX] Fix scalarisation when vector
ConstantFP is used.
When using -use-constant-fp-for-fixed-length-splat `splat (float C)`
becomes ConstantFP(C) rather than ConstantVector(C, C, C...).
---
.../Target/DirectX/DXILDataScalarization.cpp | 18 +++++++-----------
.../DirectX/DXILWriter/DXILBitcodeWriter.cpp | 2 +-
llvm/test/CodeGen/DirectX/scalar-bug-117273.ll | 1 +
3 files changed, 9 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Target/DirectX/DXILDataScalarization.cpp b/llvm/lib/Target/DirectX/DXILDataScalarization.cpp
index b752c49c75dbf..1e069e24e0ade 100644
--- a/llvm/lib/Target/DirectX/DXILDataScalarization.cpp
+++ b/llvm/lib/Target/DirectX/DXILDataScalarization.cpp
@@ -367,18 +367,14 @@ static Constant *transformInitializer(Constant *Init, Type *OrigType,
if (isa<VectorType>(OrigType) && isa<ArrayType>(NewType)) {
// Convert vector initializer to array initializer
SmallVector<Constant *, MaxVecSize> ArrayElements;
- if (ConstantVector *ConstVecInit = dyn_cast<ConstantVector>(Init)) {
- for (unsigned I = 0; I < ConstVecInit->getNumOperands(); ++I)
- ArrayElements.push_back(ConstVecInit->getOperand(I));
- } else if (ConstantDataVector *ConstDataVecInit =
- llvm::dyn_cast<llvm::ConstantDataVector>(Init)) {
- for (unsigned I = 0; I < ConstDataVecInit->getNumElements(); ++I)
- ArrayElements.push_back(ConstDataVecInit->getElementAsConstant(I));
- } else {
- assert(false && "Expected a ConstantVector or ConstantDataVector for "
- "vector initializer!");
- }
+ unsigned E = cast<FixedVectorType>(OrigType)->getNumElements();
+ for (unsigned I = 0; I != E; ++I)
+ if (Constant *Elt = Init->getAggregateElement(I))
+ ArrayElements.push_back(Elt);
+
+ assert(ArrayElements.size() == E &&
+ "Expected fixed length constant aggregate for vector initializer!");
return ConstantArray::get(cast<ArrayType>(NewType), ArrayElements);
}
diff --git a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
index 48a9085820471..57b94ebe19375 100644
--- a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
+++ b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
@@ -2016,7 +2016,7 @@ void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
}
} else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Code = bitc::CST_CODE_FLOAT;
- Type *Ty = CFP->getType();
+ Type *Ty = CFP->getType()->getScalarType();
if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
} else if (Ty->isX86_FP80Ty()) {
diff --git a/llvm/test/CodeGen/DirectX/scalar-bug-117273.ll b/llvm/test/CodeGen/DirectX/scalar-bug-117273.ll
index 43bbe9249aac0..c77a3043303e5 100644
--- a/llvm/test/CodeGen/DirectX/scalar-bug-117273.ll
+++ b/llvm/test/CodeGen/DirectX/scalar-bug-117273.ll
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes='dxil-data-scalarization,dxil-flatten-arrays,function(scalarizer<load-store>),dxil-op-lower' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+; RUN: opt -S -passes='dxil-data-scalarization,dxil-flatten-arrays,function(scalarizer<load-store>),dxil-op-lower' -mtriple=dxil-pc-shadermodel6.3-library -use-constant-fp-for-fixed-length-splat %s | FileCheck %s
@StaticArr = internal constant [8 x <3 x float>] [<3 x float> zeroinitializer, <3 x float> splat (float 5.000000e-01), <3 x float> <float 1.000000e+00, float 5.000000e-01, float 5.000000e-01>, <3 x float> <float 5.000000e-01, float 1.000000e+00, float 5.000000e-01>, <3 x float> <float 5.000000e-01, float 5.000000e-01, float 1.000000e+00>, <3 x float> <float 5.000000e-01, float 1.000000e+00, float 1.000000e+00>, <3 x float> <float 1.000000e+00, float 5.000000e-01, float 1.000000e+00>, <3 x float> <float 1.000000e+00, float 1.000000e+00, float 5.000000e-01>], align 16
>From 991c6072684a88f5c567600ca153ad1f3735d564 Mon Sep 17 00:00:00 2001
From: Paul Walker <paul.walker at arm.com>
Date: Wed, 4 Feb 2026 15:17:38 +0000
Subject: [PATCH 2/2] Rename scalar-bug-117273.ll
---
...r-bug-117273.ll => scalarize-static-array-of-float-vectors.ll} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename llvm/test/CodeGen/DirectX/{scalar-bug-117273.ll => scalarize-static-array-of-float-vectors.ll} (100%)
diff --git a/llvm/test/CodeGen/DirectX/scalar-bug-117273.ll b/llvm/test/CodeGen/DirectX/scalarize-static-array-of-float-vectors.ll
similarity index 100%
rename from llvm/test/CodeGen/DirectX/scalar-bug-117273.ll
rename to llvm/test/CodeGen/DirectX/scalarize-static-array-of-float-vectors.ll
More information about the llvm-commits
mailing list