[llvm] [SDAG] Don't allow implicit trunc in getConstant() (PR #117558)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 25 09:10:32 PST 2024


https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/117558

>From 1195b051fae41568e80ef7aaadbf82e6c36cd855 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Fri, 15 Nov 2024 16:55:59 +0100
Subject: [PATCH 1/2] [SDAG] Don't allow implicit trunc in getConstant()

Assert that the passed value is a valid unsigned integer value
for the specified type.

For signed values getSignedConstant() / getSignedTargetConstant()
should be used instead.
---
 llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 3a8ec3c6105bc0..fa6a0bdbd13710 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -1638,14 +1638,8 @@ SDValue SelectionDAG::getBoolConstant(bool V, const SDLoc &DL, EVT VT,
 SDValue SelectionDAG::getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
                                   bool isT, bool isO) {
   EVT EltVT = VT.getScalarType();
-  assert((EltVT.getSizeInBits() >= 64 ||
-          (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
-         "getConstant with a uint64_t value that doesn't fit in the type!");
-  // TODO: Avoid implicit trunc?
-  // See https://github.com/llvm/llvm-project/issues/112510.
-  return getConstant(APInt(EltVT.getSizeInBits(), Val, /*isSigned=*/false,
-                           /*implicitTrunc=*/true),
-                     DL, VT, isT, isO);
+  return getConstant(APInt(EltVT.getSizeInBits(), Val, /*isSigned=*/false), DL,
+                     VT, isT, isO);
 }
 
 SDValue SelectionDAG::getConstant(const APInt &Val, const SDLoc &DL, EVT VT,

>From 3f6bf2782596b027740c56394c2c5adea9070417 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 25 Nov 2024 18:10:08 +0100
Subject: [PATCH 2/2] use getScalarSizeInBits

---
 llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index fa6a0bdbd13710..947533a27efc78 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -1637,9 +1637,8 @@ SDValue SelectionDAG::getBoolConstant(bool V, const SDLoc &DL, EVT VT,
 
 SDValue SelectionDAG::getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
                                   bool isT, bool isO) {
-  EVT EltVT = VT.getScalarType();
-  return getConstant(APInt(EltVT.getSizeInBits(), Val, /*isSigned=*/false), DL,
-                     VT, isT, isO);
+  return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
+                     DL, VT, isT, isO);
 }
 
 SDValue SelectionDAG::getConstant(const APInt &Val, const SDLoc &DL, EVT VT,



More information about the llvm-commits mailing list