[llvm] [GlobalISel][NFC] Explicitly narrow conversions in two combine rules (PR #215210)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 10 01:06:05 PDT 2026


https://github.com/gretay-amd created https://github.com/llvm/llvm-project/pull/215210

This patch handles the following cases:

In sext_inreg_to_zext_inreg, MachineOperand::getImm() returns int64_t while
APInt::getOneBitSet takes an unsigned bit number. Add a static_cast to make the
existing narrowing conversion explicit. The value is a G_SEXT_INREG immediate,
which is a bit position within the scalar type whose width was read into
BitWidth on the line above, so it cannot exceed unsigned.

In merge_unmerge, the variadic operand list ${merge_srcs} expands to
getRemainingOperands(*State.MIs[0], 1), which returns ArrayRef<MachineOperand>
(GIMatchTableExecutor.h), so .size() is size_t and is assigned to an unsigned
local. Add a static_cast to make the existing narrowing conversion explicit. The
value is a MachineInstr operand count, which is itself unsigned
(MachineInstr::getNumOperands), so it cannot exceed unsigned.

Both conversions are therefore NFC.

This patch changes the C++ match code of GICombineRules, which is copied by
TableGen verbatim into the generated combiner tables. It is a shared file, so it
regenerates the GICombiner tables of every target that uses these rules rather
than just one backend; that is why it is a separate patch from the AMDGPU .td
changes.

This fixes 2 instances of MSVC warning C4244 and 2 of C4267 ("possible loss of
data"). They are reported in the generated tables rather than in hand-written
sources: AMDGPUGenPreLegalizeGICombiner.inc and
AMDGPUGenPostLegalizeGICombiner.inc, from 2 source lines emitted into 2 tables.

Assisted-by: Claude <noreply at anthropic.com>


>From 7be1cf46acb3669bebb0c364ce76dd3f7ed90c1c Mon Sep 17 00:00:00 2001
From: Greta Y <Greta.Yorsh at amd.com>
Date: Thu, 6 Aug 2026 16:14:29 +0100
Subject: [PATCH] [GlobalISel][NFC] Explicitly narrow conversions in two
 combine rules

This patch handles the following cases:

In sext_inreg_to_zext_inreg, MachineOperand::getImm() returns int64_t while
APInt::getOneBitSet takes an unsigned bit number. Add a static_cast to make the
existing narrowing conversion explicit. The value is a G_SEXT_INREG immediate,
which is a bit position within the scalar type whose width was read into
BitWidth on the line above, so it cannot exceed unsigned.

In merge_unmerge, the variadic operand list ${merge_srcs} expands to
getRemainingOperands(*State.MIs[0], 1), which returns ArrayRef<MachineOperand>
(GIMatchTableExecutor.h), so .size() is size_t and is assigned to an unsigned
local. Add a static_cast to make the existing narrowing conversion explicit. The
value is a MachineInstr operand count, which is itself unsigned
(MachineInstr::getNumOperands), so it cannot exceed unsigned.

Both conversions are therefore NFC.

This patch changes the C++ match code of GICombineRules, which is copied by
TableGen verbatim into the generated combiner tables. It is a shared file, so it
regenerates the GICombiner tables of every target that uses these rules rather
than just one backend; that is why it is a separate patch from the AMDGPU .td
changes.

This fixes 2 instances of MSVC warning C4244 and 2 of C4267 ("possible loss of
data"). They are reported in the generated tables rather than in hand-written
sources: AMDGPUGenPreLegalizeGICombiner.inc and
AMDGPUGenPostLegalizeGICombiner.inc, from 2 source lines emitted into 2 tables.

Assisted-by: Claude <noreply at anthropic.com>
---
 llvm/include/llvm/Target/GlobalISel/Combine.td | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index 830c48c10a39b..46f1ed8a857e3 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -276,7 +276,9 @@ def sext_inreg_to_zext_inreg : GICombineRule<
       [{
         unsigned BitWidth = MRI.getType(${src}.getReg()).getScalarSizeInBits();
         return Helper.getValueTracking()->maskedValueIsZero(${src}.getReg(),
-                 APInt::getOneBitSet(BitWidth, ${imm}.getImm() - 1)); }]),
+                 APInt::getOneBitSet(
+                     BitWidth,
+                     static_cast<unsigned>(${imm}.getImm() - 1))); }]),
     (apply [{
       Helper.getBuilder().setInstrAndDebugLoc(*${root});
       Helper.getBuilder().buildZExtInReg(${dst}, ${src}, ${imm}.getImm());
@@ -995,7 +997,7 @@ def merge_unmerge : GICombineRule<
              return false;
 
            // Check counts match.
-           unsigned NumMergeSrcs = ${merge_srcs}.size();
+           unsigned NumMergeSrcs = static_cast<unsigned>(${merge_srcs}.size());
            unsigned NumUnmergeDefs = UnmergeMI->getNumDefs();
            if (NumMergeSrcs != NumUnmergeDefs)
              return false;



More information about the llvm-commits mailing list