[llvm] [Hexagon] Fix APInt assertion in getBuildVectorConstInts (PR #181202)

Brian Cain via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 12 10:38:25 PST 2026


https://github.com/androm3da created https://github.com/llvm/llvm-project/pull/181202

Truncate the ConstantSDNode APInt value to the element bit width before creating the ConstantInt. After type legalization, a ConstantSDNode may have a wider type than the vector element (e.g., i32 constant for an i16 element).

>From c1650efa6569867f2fe55e397cc6ad54f50718cf Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Thu, 12 Feb 2026 10:36:25 -0800
Subject: [PATCH] [Hexagon] Fix APInt assertion in getBuildVectorConstInts

Truncate the ConstantSDNode APInt value to the element bit width before
creating the ConstantInt. After type legalization, a ConstantSDNode may
have a wider type than the vector element (e.g., i32 constant for an
i16 element).
---
 llvm/lib/Target/Hexagon/HexagonISelLowering.cpp    |  3 ++-
 .../test/CodeGen/Hexagon/udiv-vector-nonuniform.ll | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/Hexagon/udiv-vector-nonuniform.ll

diff --git a/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp b/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp
index 87c31eba8e9b9..1a4be036d4d45 100644
--- a/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp
@@ -2450,7 +2450,8 @@ HexagonTargetLowering::getBuildVectorConstInts(ArrayRef<SDValue> Values,
     // Make sure to always cast to IntTy.
     if (auto *CN = dyn_cast<ConstantSDNode>(V.getNode())) {
       const ConstantInt *CI = CN->getConstantIntValue();
-      Consts[i] = ConstantInt::getSigned(IntTy, CI->getValue().getSExtValue());
+      Consts[i] = cast<ConstantInt>(
+          ConstantInt::get(IntTy, CI->getValue().trunc(ElemWidth)));
     } else if (auto *CN = dyn_cast<ConstantFPSDNode>(V.getNode())) {
       const ConstantFP *CF = CN->getConstantFPValue();
       APInt A = CF->getValueAPF().bitcastToAPInt();
diff --git a/llvm/test/CodeGen/Hexagon/udiv-vector-nonuniform.ll b/llvm/test/CodeGen/Hexagon/udiv-vector-nonuniform.ll
new file mode 100644
index 0000000000000..2ff970f1e5aa3
--- /dev/null
+++ b/llvm/test/CodeGen/Hexagon/udiv-vector-nonuniform.ll
@@ -0,0 +1,14 @@
+; RUN: llc -mtriple=hexagon < %s | FileCheck %s
+; REQUIRES: asserts
+
+; Check that non-uniform udiv of v8i16 compiles without asserting.
+; The udiv expansion produces magic-number constants (e.g. 0xAAAB) that
+; don't fit in signed 16-bit, which previously triggered an APInt assertion
+; in getBuildVectorConstInts.
+
+; CHECK-LABEL: uq65656565:
+define <8 x i16> @uq65656565(<8 x i16> %0) {
+entry:
+  %div = udiv <8 x i16> %0, <i16 6, i16 5, i16 6, i16 5, i16 6, i16 5, i16 6, i16 5>
+  ret <8 x i16> %div
+}



More information about the llvm-commits mailing list