Skip to content

[InlineSpiller] Fix live-range update in hoisting within bb - #193880

Merged
qcolombet merged 1 commit into
llvm:mainfrom
qcolombet:fix_inlinespiller_hoistinsidebb
Apr 27, 2026
Merged

qcolombet merged 1 commit into
llvm:mainfrom
qcolombet:fix_inlinespiller_hoistinsidebb

Conversation

@qcolombet

@qcolombet qcolombet commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

The InlineSpiller tries to shorten the live-ranges used when storing a value that is defined by a sibling register by performing the following transformation:

a = copy b
store a

=>

store b

That is, it eliminates the copy and store the original value at the copy location.

As far as b's live-range is concerned, this transformation is neutral as long as the store is inserted in place of the copy being removed.

That doesn't hold when the copy is part of the basic block prologue. In that case, the earliest insertion point for the store is after the prologue:

a = copy b # part of bb prologue
... # more prologue instrs
store a <-- earliest insertion point

Therefore when this optimization kicks in and the copy is in the bb's prologue, b's live-range needs to be extended to that insertion point.

This is exactly what this fix does.

Note: This issue is fairly easy to understand from an IR stand point and one would expect a small reproducer.
Indeed, we would need only something that looks like:

a = copy b # prologue compatible copy
# some more prologue instrs
# high pressure point
= use a

The problem is the InlineSpiller optimization (implemented in InlineSpiller::hoistSpillInsideBB) triggers only if a and b are sibling registers, meaning they are the product of a live-range splitting. Controlling live-range splitting is hard and I couldn't come up with a smaller test case. The problem also disappears if we run greedy alone because the live-ranges are repacked (well the slot indices to be exact) compared to what the scheduler produces. That changes the allocation order and the bug is not hit anymore.
The alternative would be to run the pipeline from the coalescer/scheduler but that's not a whole lot more robust than the LLVM IR path, plus the reproducer would be ~20k line long!

The other option (maybe something we should do in the long term) would be to serialize/deserialize the VirtRegMap object so that we can set the Virt2SplitMap map in a state that register a and b as siblings.

@llvmbot

llvmbot commented Apr 24, 2026 •

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-llvm-regalloc

@llvm/pr-subscribers-backend-amdgpu

Author: Quentin Colombet (qcolombet)

Changes

The InlineSpiller tries to shorten the live-ranges used when storing a value that is defined by a sibling register by performing the following transformation:

a = copy b
store a

=>

store b

That is, it eliminates the copy and store the original value at the copy location.

As far as b's live-range is concerned, this transformation is neutral as long as the store is inserted in place of the copy being removed.

That doesn't hold when the copy is part of the basic block prologue. In that case, the earliest insertion point for the store is after the prologue:

a = copy b # part of bb prologue
... # more prologue instrs
store a &lt;-- earliest insertion point

Therefore when this optimization kicks in and the copy is in the bb's prologue, b's live-range needs to be extended to that insertion point.

This is exactly what this fix does.

Note: This issue is fairly easy to understand from an IR stand point and one would expect a small reproducer.
Indeed, we would need only something that looks like:

a = copy b # prologue compatible copy
= use a

The problem is the InlineSpiller optimization (implemented in InlineSpiller::hoistSpillInsideBB) triggers only if a and b are sibling registers, meaning they are the product of a live-range splitting. Controlling live-range splitting is hard and I couldn't come up with a smaller test case. The problem also disappears if we run greedy alone because the live-ranges are repacked (well the slot indices to be exact) compared to what the scheduler produces. That changes the allocation order and the bug is not hit anymore.
The alternative would be to run the pipeline from the coalescer/scheduler but that's not a whole lot more robust than the LLVM IR path, plus the reproducer would be ~20k line long!

The other option (maybe something we should do in the long term) would be to serialize/deserialize the VirtRegMap object so that we can set the Virt2SplitMap map in a state that register a and b as siblings.


Patch is 164.86 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/193880.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/InlineSpiller.cpp (+8)
  • (added) llvm/test/CodeGen/AMDGPU/regalloc-hoist-spill-live-range-upd.ll (+2873)
diff --git a/llvm/lib/CodeGen/InlineSpiller.cpp b/llvm/lib/CodeGen/InlineSpiller.cpp
index a9341a3c3253c..03eae0080600c 100644
--- a/llvm/lib/CodeGen/InlineSpiller.cpp
+++ b/llvm/lib/CodeGen/InlineSpiller.cpp
@@ -482,6 +482,14 @@ bool InlineSpiller::hoistSpillInsideBB(LiveInterval &SpillLI,
   --MII; // Point to store instruction.
   LLVM_DEBUG(dbgs() << "\thoisted: " << SrcVNI->def << '\t' << *MII);
 
+  // When the def is a PHI, SkipPHIsLabelsAndDebug may place the store past
+  // prologue instructions. Therefore if that copy was the end of a segment
+  // we need to extend it to the store.
+  if (SrcVNI->isPHIDef()) {
+    SlotIndex StoreUseIdx = LIS.getInstructionIndex(*MII).getRegSlot(true);
+    SrcLI.extendInBlock(LIS.getMBBStartIdx(MBB), StoreUseIdx);
+  }
+
   // If there is only 1 store instruction is required for spill, add it
   // to mergeable list. In X86 AMX, 2 intructions are required to store.
   // We disable the merge for this case.
diff --git a/llvm/test/CodeGen/AMDGPU/regalloc-hoist-spill-live-range-upd.ll b/llvm/test/CodeGen/AMDGPU/regalloc-hoist-spill-live-range-upd.ll
new file mode 100644
index 0000000000000..1a89c16912971
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/regalloc-hoist-spill-live-range-upd.ll
@@ -0,0 +1,2873 @@
+; RUN: llc -mcpu=gfx950 -o - -verify-regalloc %s | FileCheck %s
+;
+; This test is extremely fragile and chances are it won't test what it
+; was meant to pretty quickly.
+;
+; The gist of it is we want something that triggers the hoistSpillInsideBB
+; spilling optimization placement, while spilling a value that is defined
+; by a copy inserted by live-range splitting within the prologue of the basic
+; block (i.e., where we do our exec masks shenanigans).
+; Since the spiller cannot insert the spill code within that prologue, it
+; needs to extend the live-range through the prologue region and that test
+; is supposed to capture that.
+; The verify-regalloc flag makes sure the live-ranges are properly updated.
+; Before it would complain about missing live segment.
+
+; CHECK-LABEL: foo:
+
+
+target datalayout = "e-m:e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9"
+target triple = "amdgcn-amd-amdhsa"
+
+@global_smem = external addrspace(3) global [0 x i8]
+
+; Function Attrs: nofree norecurse nounwind
+define amdgpu_kernel void @foo(ptr addrspace(1) inreg readonly captures(none) %arg, ptr addrspace(1) inreg readonly %arg1, ptr addrspace(1) inreg readonly %arg2, ptr addrspace(1) inreg %arg3, i32 inreg %arg4, i32 inreg %arg5, i32 inreg %arg6, i32 inreg %arg7, i32 inreg %arg8, i32 inreg %arg9, i32 %arg10, i32 %arg11, i32 %arg12, i32 %arg13, i32 %.frozen, i32 %arg14, i32 %arg15, i32 %arg16, i32 %arg17, i32 %arg18, i32 %arg19, i32 %arg20, i64 %arg21, i32 %arg22, i64 %arg23, i64 %arg24, i32 %arg25, i64 %arg26, i64 %arg27, i64 %arg28, i64 %arg29, i64 %arg30, i32 %arg31, i64 %arg32, i64 %arg33, ptr addrspace(8) %arg34, i32 %.decomposed, i64 %arg35, i32 %arg36, i32 %arg37, i32 %arg38, i32 %arg39, i32 %arg40, i32 %arg41, i32 %arg42, i32 %arg43, i32 %arg44, i32 %arg45, i32 %arg46, i32 %arg47, i32 %arg48, i32 %arg49, i32 %arg50, i32 %arg51, i32 %arg52, i32 %arg53, i32 %arg54, i32 %arg55, i32 %arg56, i32 %arg57, i32 %arg58, i32 %arg59, i32 %arg60, i32 %arg61, i32 %arg62, i32 %arg63, i32 %arg64, i32 %arg65, i32 %arg66, i32 %arg67, i32 %arg68, i32 %arg69, i32 %arg70, i32 %arg71, i32 %arg72, i32 %arg73, i32 %arg74, i32 %arg75, i32 %arg76, i32 %arg77, i32 %arg78, i32 %arg79, i32 %arg80, i32 %arg81, i32 %arg82, i32 %arg83, i32 %arg84, i32 %arg85, i32 %arg86, i32 %arg87, i32 %arg88, i32 %arg89, i32 %arg90, i32 %arg91, i32 %arg92, i32 %arg93, i32 %arg94, i32 %arg95, i32 %arg96, i32 %arg97, i32 %arg98, i32 %arg99, i32 %arg100, i32 %arg101, i32 %arg102, i32 %arg103, i32 %arg104, i32 %arg105, i32 %arg106, i32 %arg107, i32 %arg108, i32 %arg109, i32 %arg110, i32 %arg111, i32 %arg112, i32 %arg113, i32 %arg114, i32 %arg115, i32 %arg116, i32 %arg117, i32 %arg118, i32 %arg119, i32 %arg120, i32 %arg121, i32 %arg122, i32 %arg123, i32 %arg124, i32 %arg125, i32 %arg126, i32 %arg127, i32 %arg128, i32 %arg129, i32 %arg130, i32 %arg131, i32 %arg132, i32 %arg133, i32 %arg134, i32 %arg135, i32 %arg136, i32 %arg137, i32 %arg138, i32 %arg139, i32 %arg140, i32 %arg141, i32 %arg142, i32 %arg143, i32 %arg144, i32 %arg145, i32 %arg146, i64 %arg147, i64 %arg148, i64 %arg149, i64 %arg150, i64 %arg151, i64 %arg152, i64 %arg153, i64 %arg154, i64 %arg155, i64 %arg156, i64 %arg157, i64 %arg158, i64 %arg159, i64 %arg160, i64 %arg161, i64 %arg162, i64 %arg163, i64 %arg164, i64 %arg165, i64 %arg166, i64 %arg167, i64 %arg168, i64 %arg169, i64 %arg170, i64 %arg171, i64 %arg172, i64 %arg173, i64 %arg174, i64 %arg175, i64 %arg176, i64 %arg177, i64 %arg178, i64 %arg179, i64 %arg180, i64 %arg181, i64 %arg182, i64 %arg183, i64 %arg184, i64 %arg185, i64 %arg186, i64 %arg187, i64 %arg188, i64 %arg189, i64 %arg190, i64 %arg191, i64 %arg192, i64 %arg193, i64 %arg194, i64 %arg195, i64 %arg196, i64 %arg197, i64 %arg198, i64 %arg199, i64 %arg200, i64 %arg201, i64 %arg202, i64 %arg203, i64 %arg204, i64 %arg205, i64 %arg206, i64 %arg207, i64 %arg208, i64 %arg209, i64 %arg210, i64 %arg211, i64 %arg212, i64 %arg213, i64 %arg214, i64 %arg215, i64 %arg216, i64 %arg217, i64 %arg218, i64 %arg219, i64 %arg220, i64 %arg221, i64 %arg222, i64 %arg223, i64 %arg224, i64 %arg225, i64 %arg226, i64 %arg227, i64 %arg228, i64 %arg229, i64 %arg230, i64 %arg231, i64 %arg232, i64 %arg233, i64 %arg234, i64 %arg235, i64 %arg236, i64 %arg237, i64 %arg238, i64 %arg239, i64 %arg240, i64 %arg241, i64 %arg242, i64 %arg243, i64 %arg244, i64 %arg245, i64 %arg246, i64 %arg247, i64 %arg248, i64 %arg249, i64 %arg250, i64 %arg251, i64 %arg252, i64 %arg253, i64 %arg254, i64 %arg255, i64 %arg256, i64 %arg257, i64 %arg258, i64 %arg259, i64 %arg260, i64 %arg261, i64 %arg262, i64 %arg263, i64 %arg264, i64 %arg265, i64 %arg266, i64 %arg267, i64 %arg268, i64 %arg269, i64 %arg270, i64 %arg271, i64 %arg272, i64 %arg273, i64 %arg274, i64 %arg275, i64 %arg276, i64 %arg277, i64 %arg278, i64 %arg279, i64 %arg280, i64 %arg281, i64 %arg282, i64 %arg283, i64 %arg284, i64 %arg285, i64 %arg286, i64 %arg287, i64 %arg288, i64 %arg289, i64 %arg290, i64 %arg291, i64 %arg292, i64 %arg293, i64 %arg294, i64 %arg295, i64 %arg296, i64 %arg297, i64 %arg298, i64 %arg299, i64 %arg300, i64 %arg301, i64 %arg302, i64 %arg303, i64 %arg304, i64 %arg305, i64 %arg306, i64 %arg307, i64 %arg308, i64 %arg309, i64 %arg310, i64 %arg311, i64 %arg312, i64 %arg313, i64 %arg314, i64 %arg315, i64 %arg316, i64 %arg317, i64 %arg318, i64 %arg319, i64 %arg320, i64 %arg321, i64 %arg322, i64 %arg323, i64 %arg324, i64 %arg325, i64 %arg326, i64 %arg327, i64 %arg328, i64 %arg329, i64 %arg330, i64 %arg331, i64 %arg332, i64 %arg333, i64 %arg334, i64 %arg335, i64 %arg336, i64 %arg337, i64 %arg338, i64 %arg339, i64 %arg340, i64 %arg341, i64 %arg342, i64 %arg343, i64 %arg344, i64 %arg345, i64 %arg346, i64 %arg347, i64 %arg348, i64 %arg349, i64 %arg350, i64 %arg351, i64 %arg352, i64 %arg353, i64 %arg354, i64 %arg355, i64 %arg356, i64 %arg357, i64 %arg358, i64 %arg359, i64 %arg360, i64 %arg361, i64 %arg362, i64 %arg363, i64 %arg364, i64 %arg365, i64 %arg366, i64 %arg367, i64 %arg368, i64 %arg369, i64 %arg370, i64 %arg371, i64 %arg372, i64 %arg373, i64 %arg374, i64 %arg375, i64 %arg376, i64 %arg377, i64 %arg378, i64 %arg379, i64 %arg380, i64 %arg381, i64 %arg382, i64 %arg383, i64 %arg384, i64 %arg385, i64 %arg386, i64 %arg387, i64 %arg388, i64 %arg389, i64 %arg390, i64 %arg391, i64 %arg392, i64 %arg393, i64 %arg394, i64 %arg395, i64 %arg396, i64 %arg397, i64 %arg398, i64 %arg399, i64 %arg400, i64 %arg401, i64 %arg402, i64 %arg403, i64 %arg404, i64 %arg405, i64 %arg406, i64 %arg407, i64 %arg408, i64 %arg409, i64 %arg410, i64 %arg411, i64 %arg412, i64 %arg413, i64 %arg414, i64 %arg415, i64 %arg416, i64 %arg417, i64 %arg418, i64 %arg419, i64 %arg420, i64 %arg421, i64 %arg422, i64 %arg423, i64 %arg424, i64 %arg425, i64 %arg426, i64 %arg427, i64 %arg428, i64 %arg429, i64 %arg430, i64 %arg431, i64 %arg432, i64 %arg433, i64 %arg434, i64 %arg435, i64 %arg436, i64 %arg437, i64 %arg438, i64 %arg439, i64 %arg440, i64 %arg441, i64 %arg442, i64 %arg443, i64 %arg444, i64 %arg445, i64 %arg446, i64 %arg447, i64 %arg448, i64 %arg449, i64 %arg450, i64 %arg451, i64 %arg452, i64 %arg453, i64 %arg454, i64 %arg455, i64 %arg456, i64 %arg457, i64 %arg458, i64 %arg459, i64 %arg460, i64 %arg461, i64 %arg462, i64 %arg463, i64 %arg464, i64 %arg465, i64 %arg466, i64 %arg467, i64 %arg468, i64 %arg469, i64 %arg470, i64 %arg471, i64 %arg472, i64 %arg473, i64 %arg474, i64 %arg475, i64 %arg476, i64 %arg477, i64 %arg478, i64 %arg479, i64 %arg480, i64 %arg481, i64 %arg482, i64 %arg483, i64 %arg484, i64 %arg485, i64 %arg486, i64 %arg487, i64 %arg488, i64 %arg489, i64 %arg490, i64 %arg491, i64 %arg492, i64 %arg493, i64 %arg494, i64 %arg495, i64 %arg496, i64 %arg497, i64 %arg498, i64 %arg499, i64 %arg500, i64 %arg501, i64 %arg502, i64 %arg503, i64 %arg504, i64 %arg505, i64 %arg506, i64 %arg507, i64 %arg508, i64 %arg509, i64 %arg510, i64 %arg511, i64 %arg512, i64 %arg513, i64 %arg514, i64 %arg515, i64 %arg516, i64 %arg517, i64 %arg518, i64 %arg519, i64 %arg520, i64 %arg521, i64 %arg522, i64 %arg523, i64 %arg524, i64 %arg525, i64 %arg526, i64 %arg527, i64 %arg528, i64 %arg529, i64 %arg530, i64 %arg531, i64 %arg532, i64 %arg533, i64 %arg534, i64 %arg535, i64 %arg536, i64 %arg537, i64 %arg538, i64 %arg539, i64 %arg540, i64 %arg541, i64 %arg542, i64 %arg543, i64 %arg544, i64 %arg545, i64 %arg546, i64 %arg547, i64 %arg548, i64 %arg549, i64 %arg550, i64 %arg551, i64 %arg552, i64 %arg553, i64 %arg554, i64 %arg555, i64 %arg556, i64 %arg557, i64 %arg558, i64 %arg559, i64 %arg560, i64 %arg561, i64 %arg562, i64 %arg563, i64 %arg564, i64 %arg565, i64 %arg566, i64 %arg567, i64 %arg568, i64 %arg569, i64 %arg570, i64 %arg571, i64 %arg572, i64 %arg573, i64 %arg574, i64 %arg575, i64 %arg576, i64 %arg577, i64 %arg578, i64 %arg579, i1 %arg580, i1 %arg581, i1 %arg582, i1 %arg583, i1 %arg584, i1 %arg585, i1 %arg586, i1 %arg587, i1 %arg588, i1 %arg589, i1 %arg590, i1 %arg591, i1 %arg592, i1 %arg593, i1 %arg594, i1 %arg595, i1 %arg596, i1 %arg597, i1 %arg598, i1 %arg599, i1 %arg600, i1 %arg601, i1 %arg602, i1 %arg603, i1 %arg604, i1 %arg605, i1 %arg606, i1 %arg607, i1 %arg608, i1 %arg609, i1 %arg610, i1 %arg611, i1 %arg612, i1 %arg613, i1 %arg614, i1 %arg615, i1 %arg616, i1 %arg617, i1 %arg618, i1 %arg619, i1 %arg620, i1 %arg621, i1 %arg622, i1 %arg623, i1 %arg624, i1 %arg625, i1 %arg626, i1 %arg627, i1 %arg628, i1 %arg629, i1 %arg630, i1 %arg631, i1 %arg632, i1 %arg633, i1 %arg634, i1 %arg635, i1 %arg636, i1 %arg637, i1 %arg638, i1 %arg639, i1 %arg640, i1 %arg641, i1 %arg642, i1 %arg643, i1 %arg644, i1 %arg645, i1 %arg646, i1 %arg647, i1 %arg648, i1 %arg649, i1 %arg650, i1 %arg651, i1 %arg652, i1 %arg653, i1 %arg654, i1 %arg655, i1 %arg656, i1 %arg657, i1 %arg658, i1 %arg659, i1 %arg660, i1 %arg661, i1 %arg662, i1 %arg663, i1 %arg664, i1 %arg665, i1 %arg666, i1 %arg667, i1 %arg668, i1 %arg669, i1 %arg670, i1 %arg671, i1 %arg672, i1 %arg673, i1 %arg674, i1 %arg675, i1 %arg676, i1 %arg677, i1 %arg678, i1 %arg679, i1 %arg680, i1 %arg681, i1 %arg682, i1 %arg683, i1 %arg684, i32 %arg685, i1 %arg686, i32 %arg687, ptr addrspace(8) %arg688, i32 %arg689, i32 %arg690, i1 %arg691, i32 %arg692, i32 %arg693, i1 %arg694, i32 %arg695, i32 %arg696, i1 %arg697, i32 %arg698, i32 %arg699, i32 %arg700, i1 %arg701, i32 %arg702, i32 %arg703, i32 %arg704, i1 %arg705, i32 %arg706, i32 %arg707, i1 %arg708, i32 %arg709, i32 %arg710, i32 %arg711, i1 %arg712, i32 %arg713, i32 %arg714, i32 %arg715, i1 %arg716, i32 %arg717, i32 %arg718, i1 %arg719, i32 %arg720, i32 %arg721, i32 %arg722, i1 %arg723, i32 %arg724, i32 %arg725, i32 %arg726, i1 %arg727, i32 %arg728, i32 %arg729, i32 %arg730, i32 %arg731, i32 %arg732, i32 %arg733, i32 %arg734, i1 %arg735, i32 %arg736, i32 %arg737, i32 %arg738, i1 %arg739, i32 %arg740, i32 %arg741, i32 %arg742, i1 %arg743, i32 %arg744, i32 %arg745, i32 %arg746, i1 %arg747, i32 %arg748, i32 %arg749, i32 %arg750, i1 %arg751, i32 %arg752, i32 %arg753, i32 %arg754, i32 %arg755, i32 %arg756, i1 %arg757, i32 %arg758, i32 %arg759, i32 %arg760, i1 %arg761, i32 %arg762, i32 %arg763, i32 %arg764, i1 %arg765, i32 %arg766, i32 %arg767, i32 %arg768, i32 %arg769, i32 %arg770, i1 %arg771, i1 %arg772, i32 %arg773, i1 %arg774, i1 %arg775, i1 %arg776, i1 %arg777, i1 %arg778, i32 %arg779, i1 %arg780, i32 %arg781, i32 %arg782, i32 %arg783, i1 %arg784, i32 %arg785, i32 %arg786, i32 %arg787, i1 %arg788, i32 %arg789, i32 %arg790, i32 %arg791, i1 %arg792, i32 %arg793, i32 %arg794, i32 %arg795, i1 %arg796, i32 %arg797, i32 %arg798, i32 %arg799, i1 %arg800, i32 %arg801, i32 %arg802, i32 %arg803, i1 %arg804, i32 %arg805, i32 %arg806, i32 %arg807, i1 %arg808, i32 %arg809, i32 %arg810, i32 %arg811, i1 %arg812, i32 %arg813, i32 %arg814, i32 %arg815, i1 %arg816, i32 %arg817, i32 %arg818, i32 %arg819, i1 %arg820, i32 %arg821, i32 %arg822, i32 %arg823, i1 %arg824, i32 %arg825, i32 %arg826, i32 %arg827, i1 %arg828, i32 %arg829, i32 %arg830, i32 %arg831, ptr addrspace(3) %global_smem, i32 %arg832, i16 %arg833, i32 %arg834, ptr addrspace(3) %arg835, i16 %arg836, i16 %arg837, i16 %arg838, i16 %arg839, i16 %arg840, i16 %arg841, i16 %arg842, i32 %arg843, i16 %arg844, i32 %arg845, i16 %arg846, ptr addrspace(3) %arg847, i16 %arg848, i32 %arg849, i16 %arg850, ptr addrspace(3) %arg851, i16 %arg852, i32 %arg853, i16 %arg854, ptr addrspace(3) %arg855, i32 %arg856, i16 %arg857, i16 %arg858, i16 %arg859, i32 %arg860, i32 %arg861, i16 %arg862, ptr addrspace(3) %arg863, i16 %arg864, i16 %arg865, i16 %arg866, i32 %arg867, i16 %arg868, i32 %arg869, i16 %arg870, ptr addrspace(3) %arg871, i16 %arg872, i32 %arg873, i16 %arg874, ptr addrspace(3) %arg875, i16 %arg876, i32 %arg877, i16 %arg878, ptr addrspace(3) %arg879, i16 %arg880, i16 %arg881, i16 %arg882, i32 %arg883, i16 %arg884, ptr addrspace(3) %arg885, i16 %arg886, i16 %arg887, i16 %arg888, i16 %arg889, i32 %arg890, i16 %arg891, ptr addrspace(3) %arg892, i16 %arg893, i32 %arg894, i16 %arg895, ptr addrspace(3) %arg896, i16 %arg897, i32 %arg898, i16 %arg899, ptr addrspace(3) %arg900, i16 %arg901, i32 %arg902, i1 %arg903, i32 %arg904, i32 %arg905, i32 %arg906, i32 %arg907, i32 %arg908, i32 %arg909, i32 %arg910, i32 %arg911, i32 %arg912, i32 %arg913, i32 %arg914, i32 %arg915, i32 %arg916, i32 %arg917, i32 %arg918, i32 %arg919, i32 %arg920, i32 %arg921, i32 %arg922, i32 %arg923, i32 %arg924, i32 %arg925, i32 %arg926, i32 %arg927, i32 %arg928, i32 %arg929, i32 %arg930, i32 %arg931, i32 %arg932, i32 %arg933, i32 %arg934, i32 %arg935, i32 %arg936, i32 %arg937, i32 %arg938, i32 %arg939, i32 %arg940, i32 %arg941, i32 %arg942, i32 %arg943, i32 %arg944, i32 %arg945, i32 %arg946, i32 %arg947, i32 %arg948, i32 %arg949, i32 %arg950, i32 %arg951, i32 %arg952, i32 %arg953, i32 %arg954, i32 %arg955, i32 %arg956, i32 %arg957, i32 %arg958, i32 %arg959, i32 %arg960, i32 %arg961, i32 %arg962, i32 %arg963, i32 %arg964, i32 %arg965, i32 %arg966, i32 %arg967, i32 %arg968, i32 %arg969, i32 %arg970, i32 %arg971, i32 %arg972, i32 %arg973, i32 %arg974, i32 %arg975, i32 %arg976, i32 %arg977, i32 %arg978, i32 %arg979, i32 %arg980, i32 %arg981, i32 %arg982, i32 %arg983, i32 %arg984, i32 %arg985, i32 %arg986, i32 %arg987, i32 %arg988, i32 %arg989, i32 %arg990, i32 %arg991, i32 %arg992, i32 %arg993, i32 %arg994, i32 %arg995, i32 %arg996, i32 %arg997, i32 %arg998, i32 %arg999, float %arg1000, float %arg1001, i32 %arg1002, i32 %arg1003, i32 %arg1004, i32 %.pn3661, i32 %.pn261662, i32 %.pn259663, i32 %.pn257664, i32 %.pn253666, i32 %.pn249668, i32 %.pn247669, i32 %.pn245670, i32 %.pn243671, i32 %.pn241672, i32 %.pn239673, i32 %.pn237674, i32 %.pn233676, i32 %.pn231677, i32 %.pn223681, i32 %.pn105740, i32 %.pn103741, i32 %.pn101742, i32 %.pn99743, i32 %.pn93746, i32 %.pn91747, i32 %.pn89748, i32 %.pn87749, i32 %.pn85750, i32 %.pn83751, i32 %.pn81752, i32 %.pn79753, i32 %.pn77754, i32 %.pn75755, i32 %.pn73756, i32 %.pn71757, i32 %.pn69758, i32 %.pn67759, i32 %.pn65760, i32 %.pn63761, i32 %.pn61762, i32 %.pn59763, i32 %.pn57764, i32 %.pn55765, i32 %.pn53766, i32 %.pn51767, i32 %.pn49768, i32 %.pn41772, i1 %arg1005, i1 %arg1006, i1 %arg1007, i1 %arg1008, i1 %arg1009, i32 %arg1010, i32 %arg1011, i1 %arg1012, i32 %arg1013, i32 %arg1014, i1 %arg1015, i32 %arg1016, i32 %arg1017, i1 %arg1018, i32 %arg1019, i32 %arg1020, i1 %arg1021, i32 %arg1022, i32 %arg1023, i32 %arg1024, i1 %arg1025, i32 %arg1026, i32 %arg1027, i1 %arg1028, i32 %arg1029, i32 %arg1030, i1 %arg1031, i32 %arg1032, i32 %arg1033, i1 %arg1034, i32 %arg1035, i1 %arg1036, i32 %arg1037, i1 %arg1038, i32 %arg1039, i1 %arg1040, i32 %arg1041, i32 %arg1042, i1 %arg1043, i32 %arg1044, i1 %arg1045, i32 %arg1046, i1 %arg1047, i32 %arg1048, i1 %arg1049, i32 %arg1050, i1 %arg1051, i32 %arg1052, i1 %arg1053, i1 %arg1054, i32 %arg1055, i32 %arg1056, i1 %arg1057, i32 %arg1058, i1 %arg1059, i32 %arg1060, i32 %arg1061, float %arg1062, <4 x float> %arg1063, float %arg1064, <4 x float> %arg1065, <4 x float> %arg1066, <4 x float> %arg1067, <4 x float> %arg1068, <4 x float> %arg1069, <4 x float> %arg1070, <4 x float> %arg1071, <4 x float> %arg1072, <4 x float> %arg1073, <4 x float> %arg1074, ptr addrspace(3) %arg1075, i32 %arg1076, i32 %arg1077, ptr addrspace(3) %arg1078, i32 %arg1079, ptr addrspace(3) %arg1080, ptr addrspace(3) %arg1081, i32 %arg1082, ptr addrspace(3) %arg1083, i32 %arg1084, i32 %arg1085, i32 %arg1086, ptr addrspace(3) %arg1087, i32 %arg1088, i32 %arg1089, i32 %arg1090, ptr addrspace(3) %arg1091, ptr addrspace(3) %arg1092, i32 %arg1093, i32 %arg1094, ptr addrspace(3) %arg1095, i32 %arg1096, i32 %arg1097, i32 %arg1098, ptr addrspace(3) %arg1099, ptr addrspace(3) %arg1100, i32 %arg1101, ptr addrspace(3) %arg1102, i32 %arg1103, i16 %arg1104, ptr addrspace(3) %arg1105, i32 %arg1106, i16 %arg1107, i16 %arg1108, i32 %arg1109, ptr addrspace(3) %arg1110, i32 %arg1111, i32 %arg1112, i32 %arg1113, i32 %arg1114, i32 %arg1115, i32 %arg1116, i32 %arg1117, i32 %arg1118, i32 %arg1119, i32 %arg1120, i32 %arg1121, i32 %arg1122, i32 %arg1123, i32 %arg1124, i32 %arg1125, i32 %arg1126, i32 %arg1127, i32 %arg1128, i32 %arg1129, i32 %arg1130, i32 %arg1131, i32 %arg1132, i32 %arg1133, i32 %arg1134, i32 %arg1135, i32 %arg1136, i32 %arg1137, i32 %arg1138, i32 %arg1139, i32 %arg1140, i32 %arg1141, i32 %arg1142, i32 %arg1143, i32 %arg1144, i32 %arg1145, i32 %arg1146, i32 %arg1147, i32 %arg1148, i32 %arg1149, i32 %arg1150, i32 %arg1151, i32 %arg1152, i32 %arg1153, i32 %arg1154, i32 %arg1155, i32 %arg1156, i32 %arg1157, i32 %arg1158, i32 %arg1159, i1 %arg1160, i1 %arg1161, i1 %arg1162, i1 %arg1163, i1 %arg1164, i1 %arg1165, i1 %arg1166, i1 %arg1167, i1 %arg1168, i1 %arg1169, i1 %arg1170, i1 %arg1171, i1 %arg1172, i1 %arg1173, i1 %arg1174, i1 %arg1175, i1 %arg1176, i1 %arg1177, i1 %arg1178, i1 %arg1179, i1 %arg1180, i1 %arg1181, i1 %arg1182, i1 %arg1183, i1 %arg1184, i1 %arg1185, i1 %arg1186, i1 %arg1187, i1 %arg1188, i1 %arg1189, i1 %arg1190, i1 %arg1191, i1 %arg1192, i1 %arg1193, i1 %arg1194, i1 %arg1195, i1 %arg1196, i1 %arg1197, i1 %arg1198, i1 %arg1199, i1 %arg1200, i1 %arg1201, i1 %arg1202, i1 %arg1203, i1 %arg1204, i1 %arg1205, i1 %arg1206, i1 %arg1207, ptr addrspace(8) %arg1208, i32 %arg1209, i1 %arg1210, i32 %arg1211, i1 %arg1212, i32 %arg1213, i1 %arg1214, i32 %arg1215, i1 %arg1216, i32 %arg1217, i1 %arg1218, i32 %arg1219, i1 %arg1220, i32 %arg1221, i1 %arg1222, i32 %arg1223, i1 %arg1224, i32 %arg1225, i1 %arg1226, i32 %arg1227, i1 %arg1228, i32 %arg1229, i1 %arg1230, i32 %arg1231, i1 %arg1232, i32 %arg1233, i1 %arg1234, i32 %arg1235, i1 %arg1236, i32 %arg1237, i1 %arg1238, i32 %arg1239, i1 %arg1240, i32 %arg1241, i1 %arg1242, i32 %arg1243, i1 %arg1244, i32 %arg1245, i1 %arg1246, i32 %arg1247, i1 %arg1248, i32 %arg1249, i1 %arg1250, i32 %arg1251, i1 %arg1252, i32 %arg1253, i1 %arg1254, i32 %arg1255, i1 %arg1256, i32 %arg1257, i1 %arg1258, i32 %arg1259, i1 %arg1260, i32 %arg1261, i1 %arg1262, i32 %arg1263, i1 %arg1264, i32 %arg1265, i1 %arg1266, i32 %arg1267, i1 %arg1268, i32 %arg1269, i1 %arg1270, i32 %arg1271, i1 %arg1272, i32 %arg1273, i1 %arg1274, i32 %arg1275, i1 %arg1276, i32 %arg1277, i1 %arg1278, i32 %arg1279, i1 %arg1280, i32 %arg1281, i1 %arg1282, i32 %arg1283, i1 %arg1284...
[truncated]

@qcolombet

Copy link
Copy Markdown
Contributor Author

@arsenm I don't particularly need a review, I wanted to kick the pre-commit checks, but if you want to take a look, that's appreciated!

@arsenm

arsenm commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

The other option (maybe something we should do in the long term) would be to serialize/deserialize the VirtRegMap object so that we can set the Virt2SplitMap map in a state that register a and b as siblings.

We need to do something about this. If you spend days on the testcase, you could probably reduce this. But it's a lot of time, and the result is still likely fragile

%i2395 = tail call i16 @llvm.amdgcn.raw.ptr.buffer.load.i16(ptr addrspace(8) %i2383, i32 %i2394, i32 0, i32 0)
%i2396 = shl i32 %i1952, 1
%i2397 = select i1 %i2281, i32 %i2396, i32 0
%i2398 = tail call i16 @llvm.amdgcn.raw.ptr.buffer.load.i16(ptr addrspace(8) null, i32 %i2397, i32 0, i32 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to avoid UB in tests, but it's hard to de-UBify these sorts of hard to reproduce cases

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, but it was llvm-reduced, so hard to go back.

The InlineSpiller tries to shorten the live-ranges used when storing
a value that is defined by a sibling register by performing the following
transformation:
```
a = copy b
store a
```
=>
```
store b
```
That is, it eliminates the copy and store the original value at the copy
location.

As far as `b`'s live-range is concerned, this transformation is neutral
as long as the store is inserted in place of the copy being removed.

That doesn't hold when the copy is part of the basic block prologue.
In that case, the earliest insertion point for the store is after the
prologue:
```
a = copy b # part of bb prologue
... # more prologue instrs
store a <-- earliest insertion point
```
Therefore when this optimization kicks in and the copy is in the bb's
prologue, b's live-range needs to be extended to that insertion point.

This is exactly what this fix does.

Note: This issue is fairly easy to understand from an IR stand point
and one would expect a small reproducer.
Indeed, we would need only something that looks like:
```
a = copy b # prologue compatible copy
... # basic block prologue
... # high pressure point
= use a
```

The problem is the InlineSpiller optimization (implemented in
InlineSpiller::hoistSpillInsideBB) triggers only if `a` and `b` are
sibling registers, meaning they are the product of a live-range splitting.
Controlling live-range splitting is hard and I couldn't come up
with a smaller test case. The problem also disappears if we run greedy
alone because the live-ranges are repacked (well the slot indices to be
exact) compared to what the scheduler produces. That changes the
allocation order and the bug is not hit anymore.
The alternative would be to run the pipeline from the coalescer/scheduler
but that's not a whole lot more robust than the LLVM IR path, plus
the reproducer would be ~20k line long!

The other option (maybe something we should do in the long term) would be
to serialize/deserialize the VirtRegMap object so that we can set the
Virt2SplitMap map in a state that register a and b as siblings.
@qcolombet
qcolombet force-pushed the fix_inlinespiller_hoistinsidebb branch from 0a6fc37 to 17d107e Compare April 25, 2026 02:39
@qcolombet

Copy link
Copy Markdown
Contributor Author

The other option (maybe something we should do in the long term) would be to serialize/deserialize the VirtRegMap object so that we can set the Virt2SplitMap map in a state that register a and b as siblings.

We need to do something about this. If you spend days on the testcase, you could probably reduce this. But it's a lot of time, and the result is still likely fragile

FYI, I'm trying this in parallel, it is kind of a can of worms because when I expose this, I kind of break the VNI or other internal structure if I'm not careful. I'll report back on what the final test looks, but although the test will be less fragile, if it pans out, the use of such thing will be "use at your own risk" type of guarantee :).

@arsenm arsenm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did add a dummy priority advisor in 11e482c, but so far haven't managed do to make real use of it. It only eliminates one source of variance, but is only theoretically usable with handcrafted MIR.

@qcolombet
qcolombet merged commit 2774ab1 into llvm:main Apr 27, 2026
8 checks passed
@qcolombet
qcolombet deleted the fix_inlinespiller_hoistinsidebb branch April 27, 2026 23:42
yingopq pushed a commit to yingopq/llvm-project that referenced this pull request Apr 29, 2026
)

The InlineSpiller tries to shorten the live-ranges used when storing a
value that is defined by a sibling register by performing the following
transformation:
```
a = copy b
store a
```
=>
```
store b
```
That is, it eliminates the copy and store the original value at the copy
location.

As far as `b`'s live-range is concerned, this transformation is neutral
as long as the store is inserted in place of the copy being removed.

That doesn't hold when the copy is part of the basic block prologue. In
that case, the earliest insertion point for the store is after the
prologue:
```
a = copy b # part of bb prologue
... # more prologue instrs
store a <-- earliest insertion point
```
Therefore when this optimization kicks in and the copy is in the bb's
prologue, b's live-range needs to be extended to that insertion point.

This is exactly what this fix does.

Note: This issue is fairly easy to understand from an IR stand point and
one would expect a small reproducer.
Indeed, we would need only something that looks like:
```
a = copy b # prologue compatible copy
... # some more prologue instrs
... # high pressure point
= use a
```

The problem is the InlineSpiller optimization (implemented in
InlineSpiller::hoistSpillInsideBB) triggers only if `a` and `b` are
sibling registers, meaning they are the product of a live-range
splitting. Controlling live-range splitting is hard and I couldn't come
up with a smaller test case. The problem also disappears if we run
greedy alone because the live-ranges are repacked (well the slot indices
to be exact) compared to what the scheduler produces. That changes the
allocation order and the bug is not hit anymore.
The alternative would be to run the pipeline from the
coalescer/scheduler but that's not a whole lot more robust than the LLVM
IR path, plus the reproducer would be ~20k line long!

The other option (maybe something we should do in the long term) would
be to serialize/deserialize the VirtRegMap object so that we can set the
Virt2SplitMap map in a state that register a and b as siblings.
KHicketts pushed a commit to KHicketts/llvm-project that referenced this pull request Apr 30, 2026
)

The InlineSpiller tries to shorten the live-ranges used when storing a
value that is defined by a sibling register by performing the following
transformation:
```
a = copy b
store a
```
=>
```
store b
```
That is, it eliminates the copy and store the original value at the copy
location.

As far as `b`'s live-range is concerned, this transformation is neutral
as long as the store is inserted in place of the copy being removed.

That doesn't hold when the copy is part of the basic block prologue. In
that case, the earliest insertion point for the store is after the
prologue:
```
a = copy b # part of bb prologue
... # more prologue instrs
store a <-- earliest insertion point
```
Therefore when this optimization kicks in and the copy is in the bb's
prologue, b's live-range needs to be extended to that insertion point.

This is exactly what this fix does.

Note: This issue is fairly easy to understand from an IR stand point and
one would expect a small reproducer.
Indeed, we would need only something that looks like:
```
a = copy b # prologue compatible copy
... # some more prologue instrs
... # high pressure point
= use a
```

The problem is the InlineSpiller optimization (implemented in
InlineSpiller::hoistSpillInsideBB) triggers only if `a` and `b` are
sibling registers, meaning they are the product of a live-range
splitting. Controlling live-range splitting is hard and I couldn't come
up with a smaller test case. The problem also disappears if we run
greedy alone because the live-ranges are repacked (well the slot indices
to be exact) compared to what the scheduler produces. That changes the
allocation order and the bug is not hit anymore.
The alternative would be to run the pipeline from the
coalescer/scheduler but that's not a whole lot more robust than the LLVM
IR path, plus the reproducer would be ~20k line long!

The other option (maybe something we should do in the long term) would
be to serialize/deserialize the VirtRegMap object so that we can set the
Virt2SplitMap map in a state that register a and b as siblings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants