Skip to content

Commit d27394a

Browse files
[LLVM][SelectionDAG] Ensure Constant[FP]SDnode only store references to scalar Constant{Int,FP}. (#111005)
This fixes a failure path when the use-constant-##-for-###-splat IR options are enabled.
1 parent 9786198 commit d27394a

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

llvm/include/llvm/CodeGen/SelectionDAGNodes.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,7 @@ class ConstantSDNode : public SDNode {
16781678
: SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, 0, DebugLoc(),
16791679
VTs),
16801680
Value(val) {
1681+
assert(!isa<VectorType>(val->getType()) && "Unexpected vector type!");
16811682
ConstantSDNodeBits.IsOpaque = isOpaque;
16821683
}
16831684

@@ -1730,7 +1731,9 @@ class ConstantFPSDNode : public SDNode {
17301731
ConstantFPSDNode(bool isTarget, const ConstantFP *val, SDVTList VTs)
17311732
: SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, 0,
17321733
DebugLoc(), VTs),
1733-
Value(val) {}
1734+
Value(val) {
1735+
assert(!isa<VectorType>(val->getType()) && "Unexpected vector type!");
1736+
}
17341737

17351738
public:
17361739
const APFloat& getValueAPF() const { return Value->getValueAPF(); }

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,11 @@ SDValue SelectionDAG::getConstant(const ConstantInt &Val, const SDLoc &DL,
16561656
EVT EltVT = VT.getScalarType();
16571657
const ConstantInt *Elt = &Val;
16581658

1659+
// Vector splats are explicit within the DAG, with ConstantSDNode holding the
1660+
// to-be-splatted scalar ConstantInt.
1661+
if (isa<VectorType>(Elt->getType()))
1662+
Elt = ConstantInt::get(*getContext(), Elt->getValue());
1663+
16591664
// In some cases the vector type is legal but the element type is illegal and
16601665
// needs to be promoted, for example v8i8 on ARM. In this case, promote the
16611666
// inserted value (the type does not need to match the vector element type).
@@ -1809,6 +1814,12 @@ SDValue SelectionDAG::getConstantFP(const ConstantFP &V, const SDLoc &DL,
18091814
assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
18101815

18111816
EVT EltVT = VT.getScalarType();
1817+
const ConstantFP *Elt = &V;
1818+
1819+
// Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1820+
// the to-be-splatted scalar ConstantFP.
1821+
if (isa<VectorType>(Elt->getType()))
1822+
Elt = ConstantFP::get(*getContext(), Elt->getValue());
18121823

18131824
// Do the map lookup using the actual bit pattern for the floating point
18141825
// value, so that we don't have problems with 0.0 comparing equal to -0.0, and
@@ -1817,15 +1828,15 @@ SDValue SelectionDAG::getConstantFP(const ConstantFP &V, const SDLoc &DL,
18171828
SDVTList VTs = getVTList(EltVT);
18181829
FoldingSetNodeID ID;
18191830
AddNodeIDNode(ID, Opc, VTs, {});
1820-
ID.AddPointer(&V);
1831+
ID.AddPointer(Elt);
18211832
void *IP = nullptr;
18221833
SDNode *N = nullptr;
18231834
if ((N = FindNodeOrInsertPos(ID, DL, IP)))
18241835
if (!VT.isVector())
18251836
return SDValue(N, 0);
18261837

18271838
if (!N) {
1828-
N = newSDNode<ConstantFPSDNode>(isTarget, &V, VTs);
1839+
N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
18291840
CSEMap.InsertNode(N, IP);
18301841
InsertNode(N);
18311842
}

llvm/test/CodeGen/AArch64/sve-fp-immediates-merging.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
22
; RUN: llc < %s | FileCheck %s
3+
; RUN: llc -use-constant-fp-for-scalable-splat < %s | FileCheck %s
34

45
target triple = "aarch64-unknown-linux-gnu"
56

llvm/test/CodeGen/AArch64/sve-int-imm.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: llc -mtriple=aarch64-linux-gnu -mattr=+sve < %s | FileCheck %s
2+
; RUN: llc -mtriple=aarch64-linux-gnu -mattr=+sve -use-constant-int-for-scalable-splat < %s | FileCheck %s
23

34
;
45
; SVE Arith Vector Immediate Unpredicated CodeGen

0 commit comments

Comments
 (0)