[llvm] [RISCV] Create new build vector instead of relying on getNode constan… (PR #67944)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 1 20:52:58 PDT 2023


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/67944

…t folding.

We want to create a build_vector with narrower elements here. Normally getNode on the ISD::TRUNCATE will constant fold this to a new BUILD_VECTOR. If it doesn't constant fold, we end up with a cycle in the DAG because we truncate the node we are replacing. Constant folding can fail if one of the elements is an opaque constant.

The failing case I saw involved an opaque constant created by a memset that was expanded. Not sure exactly what happened after that.

This patch creates a new build_vector with the new type directly.

>From fb2f28161c2fb6c1006471a6932e8a157cab5c45 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Sun, 1 Oct 2023 20:45:51 -0700
Subject: [PATCH] [RISCV] Create new build vector instead of relying on getNode
 constant folding.

We want to create a build_vector with narrower elements here.
Normally getNode on the ISD::TRUNCATE will constant fold this to
a new BUILD_VECTOR. If it doesn't constant fold, we end up with a
cycle in the DAG because we truncate the node we are replacing.
Constant folding can fail if one of the elements is an opaque constant.

The failing case I saw involved an opaque constant created by a
memset that was expanded. Not sure exactly what happened after that.

This patch creates a new build_vector with the new type directly.
---
 llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index fb7ed79c7888421..c9d74856e0a15aa 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -3530,8 +3530,8 @@ static SDValue lowerBuildVectorOfConstants(SDValue Op, SelectionDAG &DAG,
       (NumElts <= 4 || VT.getSizeInBits() > Subtarget.getRealMinVLen())) {
     unsigned SignBits = DAG.ComputeNumSignBits(Op);
     if (EltBitSize - SignBits < 8) {
-      SDValue Source =
-        DAG.getNode(ISD::TRUNCATE, DL, VT.changeVectorElementType(MVT::i8), Op);
+      SDValue Source = DAG.getBuildVector(VT.changeVectorElementType(MVT::i8),
+                                          DL, Op->ops());
       Source = convertToScalableVector(ContainerVT.changeVectorElementType(MVT::i8),
                                        Source, DAG, Subtarget);
       SDValue Res = DAG.getNode(RISCVISD::VSEXT_VL, DL, ContainerVT, Source, Mask, VL);



More information about the llvm-commits mailing list