Skip to content

Commit c969817

Browse files
committed
[CalcSpillWeights] Avoid x87 excess precision influencing weight result
Fixes #99396 The result of `VirtRegAuxInfo::weightCalcHelper` can be influenced by x87 excess precision, which can result in slightly different register choices when the compiler is hosted on x86_64 or i386. This leads to different object file output when cross-compiling to i386, or native. Similar to 7af3432, add a `volatile` qualifier to the local `Weight` variable to force it onto the stack, and avoid the excess precision.
1 parent e9bb4e3 commit c969817

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

llvm/lib/CodeGen/CalcSpillWeights.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ float VirtRegAuxInfo::weightCalcHelper(LiveInterval &LI, SlotIndex *Start,
257257
return -1.0f;
258258
}
259259

260-
float Weight = 1.0f;
260+
// Force Weight onto the stack so that x86 doesn't add hidden precision,
261+
// similar to HWeight below.
262+
volatile float Weight = 1.0f;
261263
if (IsSpillable) {
262264
// Get loop info for mi.
263265
if (MI->getParent() != MBB) {

llvm/test/CodeGen/X86/pr99396.ll

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
; RUN: llc < %s -mtriple=i386-unknown-freebsd -enable-misched -relocation-model=pic | FileCheck %s
2+
3+
target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128"
4+
target triple = "i386-unknown-freebsd15.0"
5+
6+
@c = external local_unnamed_addr global ptr
7+
8+
declare i32 @fn2() local_unnamed_addr
9+
10+
declare i32 @fn3() local_unnamed_addr
11+
12+
define noundef i32 @fn4() #0 {
13+
entry:
14+
%0 = load i32, ptr @fn4, align 4
15+
; CHECK: movl fn4@GOT(%ebx), %edi
16+
; CHECK-NEXT: movl (%edi), %edx
17+
%1 = load ptr, ptr @c, align 4
18+
; CHECK: movl c@GOT(%ebx), %eax
19+
; CHECK-NEXT: movl (%eax), %esi
20+
; CHECK-NEXT: testl %esi, %esi
21+
%cmp.g = icmp eq ptr %1, null
22+
br i1 %cmp.g, label %if.then.g, label %if.end3.g
23+
24+
if.then.g: ; preds = %entry
25+
%2 = load i32, ptr inttoptr (i32 1 to ptr), align 4
26+
%cmp1.g = icmp slt i32 %2, 0
27+
br i1 %cmp1.g, label %if.then2.g, label %if.end3.g
28+
29+
if.then2.g: ; preds = %if.then.g
30+
%.g = load volatile i32, ptr null, align 2147483648
31+
br label %f.exit
32+
33+
if.end3.g: ; preds = %if.then.g, %entry
34+
%h.i.g = icmp eq i32 %0, 0
35+
br i1 %h.i.g, label %f.exit, label %while.body.g
36+
37+
while.body.g: ; preds = %if.end3.g, %if.end8.g
38+
%buff.addr.019.g = phi ptr [ %incdec.ptr.g, %if.end8.g ], [ @fn4, %if.end3.g ]
39+
%g.addr.018.g = phi i32 [ %dec.g, %if.end8.g ], [ %0, %if.end3.g ]
40+
%call4.g = tail call i32 @fn3(ptr %1, ptr %buff.addr.019.g, i32 %g.addr.018.g)
41+
%cmp5.g = icmp slt i32 %call4.g, 0
42+
br i1 %cmp5.g, label %if.then6.g, label %if.end8.g
43+
44+
if.then6.g: ; preds = %while.body.g
45+
%call7.g = tail call i32 @fn2(ptr null)
46+
br label %f.exit
47+
48+
if.end8.g: ; preds = %while.body.g
49+
%dec.g = add i32 %g.addr.018.g, 1
50+
%incdec.ptr.g = getelementptr i32, ptr %buff.addr.019.g, i32 1
51+
store i64 0, ptr %1, align 4
52+
%h.not.g = icmp eq i32 %dec.g, 0
53+
br i1 %h.not.g, label %f.exit, label %while.body.g
54+
55+
f.exit: ; preds = %if.end8.g, %if.then6.g, %if.end3.g, %if.then2.g
56+
ret i32 0
57+
}
58+
59+
attributes #0 = { "frame-pointer"="all" "tune-cpu"="generic" }

0 commit comments

Comments
 (0)