[llvm] [GlobalISel] avoid G_TRUNC with floating-point G_MERGE_VALUES source (PR #206733)

Kacper Doga via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 9 03:13:56 PDT 2026


https://github.com/varev-dev updated https://github.com/llvm/llvm-project/pull/206733

>From aa702f937c998a2f9ed8ffdc13b102e107e65815 Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Tue, 30 Jun 2026 15:13:22 +0200
Subject: [PATCH 1/3] [GlobalISel] avoid G_TRUNC with floating-point
 G_MERGE_VALUES source

With extended LLT, scalar LLT carry an integer/float kind information. The `G_TRUNC(G_MERGE_VALUES)`
fold in `LegalizationArtifactCombiner::tryCombineTrunc` truncated, copied or rebuilt a merge directly
from the merge's source register.

**Problem**
When those sources are floating-point this produces artifacts with float source operand - e.g.
`i1 = G_TRUNC f32` or a rebuilt `iN = G_MERGE_VALUES f32, ...` which are bit level integer operations
and must not take a float operand.

**Fix**
Reinterpret a floating-point merge source to an integer of the same size via `G_BITCAST` before
truncating, copying or rebuilding the merge. So the emitted artifacts stay on integer operands.

Non-float sources and non-extended-LLT builds are unaffected.
---
 .../GlobalISel/LegalizationArtifactCombiner.h | 25 +++++--
 .../legalize-trunc-merge-float-src.mir        | 72 +++++++++++++++++++
 2 files changed, 90 insertions(+), 7 deletions(-)
 create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/legalize-trunc-merge-float-src.mir

diff --git a/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h b/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
index d8d7ccc0bd7a7..bd83b23eca38b 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
@@ -292,29 +292,40 @@ class LegalizationArtifactCombiner {
       if (!DstTy.isScalar() || !MergeSrcTy.isScalar())
         return false;
 
+      // G_TRUNC/G_MERGE_VALUES operate on the raw bit pattern - if the merge
+      // feeds us float sources, reinterpret them to integer of the same size
+      // so we never emit a G_TRUNC or G_MERGE_VALUES with a floating-point
+      // source operand.
+      const LLT WorkTy =
+          MergeSrcTy.isFloat() ? LLT::integer(MergeSrcSize) : MergeSrcTy;
+      auto AsInt = [&](Register R) {
+        if (MergeSrcTy != WorkTy)
+          return Builder.buildBitcast(WorkTy, R).getReg(0);
+        return R;
+      };
+
       if (DstSize < MergeSrcSize) {
         // When the merge source is larger than the destination, we can just
         // truncate the merge source directly
-        if (isInstUnsupported({TargetOpcode::G_TRUNC, {DstTy, MergeSrcTy}}))
+        if (isInstUnsupported({TargetOpcode::G_TRUNC, {DstTy, WorkTy}}))
           return false;
 
         LLVM_DEBUG(dbgs() << "Combining G_TRUNC(G_MERGE_VALUES) to G_TRUNC: "
                           << MI);
 
-        Builder.buildTrunc(DstReg, MergeSrcReg);
+        Builder.buildTrunc(DstReg, AsInt(MergeSrcReg));
         UpdatedDefs.push_back(DstReg);
       } else if (DstSize == MergeSrcSize) {
         // If the sizes match we can simply try to replace the register
         LLVM_DEBUG(
             dbgs() << "Replacing G_TRUNC(G_MERGE_VALUES) with merge input: "
                    << MI);
-        replaceRegOrBuildCopy(DstReg, MergeSrcReg, MRI, Builder, UpdatedDefs,
-                              Observer);
+        replaceRegOrBuildCopy(DstReg, AsInt(MergeSrcReg), MRI, Builder,
+                              UpdatedDefs, Observer);
       } else if (DstSize % MergeSrcSize == 0) {
         // If the trunc size is a multiple of the merge source size we can use
         // a smaller merge instead
-        if (isInstUnsupported(
-                {TargetOpcode::G_MERGE_VALUES, {DstTy, MergeSrcTy}}))
+        if (isInstUnsupported({TargetOpcode::G_MERGE_VALUES, {DstTy, WorkTy}}))
           return false;
 
         LLVM_DEBUG(
@@ -326,7 +337,7 @@ class LegalizationArtifactCombiner {
                "trunc(merge) should require less inputs than merge");
         SmallVector<Register, 8> SrcRegs(NumSrcs);
         for (unsigned i = 0; i < NumSrcs; ++i)
-          SrcRegs[i] = SrcMerge->getSourceReg(i);
+          SrcRegs[i] = AsInt(SrcMerge->getSourceReg(i));
 
         Builder.buildMergeValues(DstReg, SrcRegs);
         UpdatedDefs.push_back(DstReg);
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/legalize-trunc-merge-float-src.mir b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-trunc-merge-float-src.mir
new file mode 100644
index 0000000000000..60d5c2e2b21fc
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-trunc-merge-float-src.mir
@@ -0,0 +1,72 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=aarch64 -run-pass=legalizer %s -o - | FileCheck %s
+
+# Regression test for LegalizationArtifactCombiner::tryCombineTrunc: a G_TRUNC
+# whose G_MERGE_VALUES source operands are floating-point must not fold to a
+# G_TRUNC (or rebuilt G_MERGE_VALUES) with a float source operand. The float
+# source is reinterpreted to an integer of the same size via G_BITCAST first.
+
+---
+# DstSize < MergeSrcSize - trunc folds trought the merge to the first source.
+name:           trunc_merge_float_src_smaller
+legalized:      true
+body:            |
+  bb.0:
+    liveins: $s0, $s1
+    ; CHECK-LABEL: name: trunc_merge_float_src_smaller
+    ; CHECK: liveins: $s0, $s1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(f32) = COPY $s0
+    ; CHECK-NEXT: [[BITCAST:%[0-9]+]]:_(i32) = G_BITCAST [[COPY]](f32)
+    ; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(i16) = G_TRUNC [[BITCAST]](i32)
+    ; CHECK-NEXT: $h0 = COPY [[TRUNC]](i16)
+    %0:_(f32) = COPY $s0
+    %1:_(f32) = COPY $s1
+    %2:_(i64) = G_MERGE_VALUES %0(f32), %1(f32)
+    %3:_(i16) = G_TRUNC %2(i64)
+    $h0 = COPY %3(i16)
+...
+---
+# DstSize == MergeSrcSize - trunc becomes the (bitcast of the) first source.
+name:           trunc_merge_float_src_equal
+legalized:      true
+body:            |
+  bb.0:
+    liveins: $s0, $s1
+    ; CHECK-LABEL: name: trunc_merge_float_src_equal
+    ; CHECK: liveins: $s0, $s1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(f32) = COPY $s0
+    ; CHECK-NEXT: [[BITCAST:%[0-9]+]]:_(i32) = G_BITCAST [[COPY]](f32)
+    ; CHECK-NEXT: $w0 = COPY [[BITCAST]](i32)
+    %0:_(f32) = COPY $s0
+    %1:_(f32) = COPY $s1
+    %2:_(i64) = G_MERGE_VALUES %0(f32), %1(f32)
+    %3:_(i32) = G_TRUNC %2(i64)
+    $w0 = COPY %3(i32)
+...
+---
+# DstSize % MergeSrcSize == 0 - rebuild a smaller merge, whose sources must be
+# the integer-reinterpreted halves, not the floats.
+name:           trunc_merge_float_src_multiple
+legalized:      true
+body:            |
+  bb.0:
+    liveins: $h0, $h1, $h2, $h3
+    ; CHECK-LABEL: name: trunc_merge_float_src_multiple
+    ; CHECK: liveins: $h0, $h1, $h2, $h3
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(f16) = COPY $h0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(f16) = COPY $h1
+    ; CHECK-NEXT: [[BITCAST:%[0-9]+]]:_(i16) = G_BITCAST [[COPY]](f16)
+    ; CHECK-NEXT: [[BITCAST1:%[0-9]+]]:_(i16) = G_BITCAST [[COPY1]](f16)
+    ; CHECK-NEXT: [[MV:%[0-9]+]]:_(i32) = G_MERGE_VALUES [[BITCAST]](i16), [[BITCAST1]](i16)
+    ; CHECK-NEXT: $w0 = COPY [[MV]](i32)
+    %0:_(f16) = COPY $h0
+    %1:_(f16) = COPY $h1
+    %2:_(f16) = COPY $h2
+    %3:_(f16) = COPY $h3
+    %4:_(i64) = G_MERGE_VALUES %0(f16), %1(f16), %2(f16), %3(f16)
+    %5:_(i32) = G_TRUNC %4(i64)
+    $w0 = COPY %5(i32)
+...

>From ce41b5f42a7ef0ba8a235c6555079cafadf83400 Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Mon, 6 Jul 2026 12:34:58 +0200
Subject: [PATCH 2/3] [VectorCombine] Fix bailing out of the
 foldSingleElementStore with a freeze-pending index

`foldSingleElementStore` calls `canScalarizeAccess` to decide whether a single-element store can be scalarized. When the insert index is not a constant, is provably in-bounds and may be poison, `canScalarizeAccess` returns `SafeWithFreeze` and stashes a `ToFreeze` value that must later be consumed by `freeze()` or cleared by `discard()`.

The bail-out for memory being modified between the load and store was folded into same condition as the scalarizability check:

```cpp
auto ScalarizableIdx = canScalarizeAccess(...);
if (ScalarizableIdx.isUnsafe() || isMemModifiedBetween(...))
  return false;
```

When the index took the `SafeWithFreeze` path and memory was modified between the load and the store, `isMemModifiedBetween` returns true and hit `return false` while ScalarizableIdx still holds a live `ToFreeze`.

Its destructor then raises:
> Assertion '!ToFreeze && "freeze() not called with ToFreeze being set"'

**Fix**
Move the `isMemModifiedBetween` bail-out above the `canScalarizeAccess` call so the freeze-pending result is never constructed on the path where we give up.

**Test**
Added `@insert_store_mem_modify_nonconst_index` to `llvm/test/Transforms/VectorCombine/load-insert-store.ll`
---
 .../Transforms/Vectorize/VectorCombine.cpp    |  7 ++++---
 .../VectorCombine/load-insert-store.ll        | 20 +++++++++++++++++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 6910bda06007a..747051ccd4eb3 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -1986,11 +1986,12 @@ bool VectorCombine::foldSingleElementStore(Instruction &I) {
         SrcAddr != SI->getPointerOperand()->stripPointerCasts())
       return false;
 
+    if (isMemModifiedBetween(Load->getIterator(), SI->getIterator(),
+                             MemoryLocation::get(SI), AA))
+      return false;
     auto ScalarizableIdx =
         canScalarizeAccess(VecTy, Idx, SQ.getWithInstruction(Load));
-    if (ScalarizableIdx.isUnsafe() ||
-        isMemModifiedBetween(Load->getIterator(), SI->getIterator(),
-                             MemoryLocation::get(SI), AA))
+    if (ScalarizableIdx.isUnsafe())
       return false;
 
     // Ensure we add the load back to the worklist BEFORE its users so they can
diff --git a/llvm/test/Transforms/VectorCombine/load-insert-store.ll b/llvm/test/Transforms/VectorCombine/load-insert-store.ll
index 93565c1a708eb..897b113c197cf 100644
--- a/llvm/test/Transforms/VectorCombine/load-insert-store.ll
+++ b/llvm/test/Transforms/VectorCombine/load-insert-store.ll
@@ -713,6 +713,26 @@ entry:
   ret void
 }
 
+; Non-constant index makes the access "safe with freeze", but memory is
+; modified between the load and store, so the fold must bail out without
+; leaving the pending ToFreeze value set.
+define void @insert_store_mem_modify_nonconst_index(ptr %p, i8 %s) {
+; CHECK-LABEL: @insert_store_mem_modify_nonconst_index(
+; CHECK-NEXT:    [[LD:%.*]] = load <2 x i8>, ptr [[P:%.*]], align 2
+; CHECK-NEXT:    [[A:%.*]] = and i8 [[S:%.*]], 1
+; CHECK-NEXT:    [[INS:%.*]] = insertelement <2 x i8> [[LD]], i8 0, i8 [[A]]
+; CHECK-NEXT:    store i32 0, ptr [[P]], align 4
+; CHECK-NEXT:    store <2 x i8> [[INS]], ptr [[P]], align 2
+; CHECK-NEXT:    ret void
+;
+  %ld = load <2 x i8>, ptr %p
+  %a = and i8 %s, 1
+  %ins = insertelement <2 x i8> %ld, i8 0, i8 %a
+  store i32 0, ptr %p
+  store <2 x i8> %ins, ptr %p
+  ret void
+}
+
 ; Check cases when calls may modify memory
 define void @insert_store_with_call(ptr %p, ptr %q, i8 %s) {
 ; CHECK-LABEL: @insert_store_with_call(

>From 87cd1da0a0090f6856b411e3e0fdc54ece892808 Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Thu, 9 Jul 2026 12:09:01 +0200
Subject: [PATCH 3/3] fix typos

---
 .../llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h      | 2 +-
 .../AArch64/GlobalISel/legalize-trunc-merge-float-src.mir       | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h b/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
index bd83b23eca38b..4a8f5297b66ef 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h
@@ -293,7 +293,7 @@ class LegalizationArtifactCombiner {
         return false;
 
       // G_TRUNC/G_MERGE_VALUES operate on the raw bit pattern - if the merge
-      // feeds us float sources, reinterpret them to integer of the same size
+      // feeds us float sources, reinterpret them as integers of the same size
       // so we never emit a G_TRUNC or G_MERGE_VALUES with a floating-point
       // source operand.
       const LLT WorkTy =
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/legalize-trunc-merge-float-src.mir b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-trunc-merge-float-src.mir
index 60d5c2e2b21fc..e440a515f1b2b 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/legalize-trunc-merge-float-src.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-trunc-merge-float-src.mir
@@ -7,7 +7,7 @@
 # source is reinterpreted to an integer of the same size via G_BITCAST first.
 
 ---
-# DstSize < MergeSrcSize - trunc folds trought the merge to the first source.
+# DstSize < MergeSrcSize - trunc folds through the merge to the first source.
 name:           trunc_merge_float_src_smaller
 legalized:      true
 body:            |



More information about the llvm-commits mailing list