Skip to content

Commit 3d41cbb

Browse files
authored
[PILC][profcheck] Bias branch weights when optimizing sqrt (#167742)
Biasing towards the native `sqrt`​ not returning NaN. Issue #147390
1 parent 471d804 commit 3d41cbb

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/Analysis/TargetTransformInfo.h"
2020
#include "llvm/IR/Dominators.h"
2121
#include "llvm/IR/IRBuilder.h"
22+
#include "llvm/IR/MDBuilder.h"
2223
#include "llvm/InitializePasses.h"
2324
#include "llvm/Support/DebugCounter.h"
2425
#include "llvm/Transforms/Scalar.h"
@@ -27,6 +28,10 @@
2728

2829
using namespace llvm;
2930

31+
namespace llvm {
32+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
33+
} // namespace llvm
34+
3035
#define DEBUG_TYPE "partially-inline-libcalls"
3136

3237
DEBUG_COUNTER(PILCounter, "partially-inline-libcalls-transform",
@@ -94,7 +99,14 @@ static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
9499
: Builder.CreateFCmpOGE(Call->getOperand(0),
95100
ConstantFP::get(Ty, 0.0));
96101
CurrBBTerm->setCondition(FCmp);
97-
102+
if (!ProfcheckDisableMetadataFixes &&
103+
CurrBBTerm->getFunction()->getEntryCount()) {
104+
// Presume the quick path - where we don't call the library call - is the
105+
// frequent one
106+
MDBuilder MDB(CurrBBTerm->getContext());
107+
CurrBBTerm->setMetadata(LLVMContext::MD_prof,
108+
MDB.createLikelyBranchWeights());
109+
}
98110
// Add phi operands.
99111
Phi->addIncoming(Call, &CurrBB);
100112
Phi->addIncoming(LibCall, LibCallBB);

llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
22
; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
33

4-
define float @f(float %val) {
4+
define float @f(float %val) !prof !0 {
55
; CHECK-LABEL: @f(
66
; CHECK-NEXT: entry:
77
; CHECK-NEXT: [[RES:%.*]] = tail call float @sqrtf(float [[VAL:%.*]]) #[[ATTR0:[0-9]+]]
88
; CHECK-NEXT: [[TMP0:%.*]] = fcmp oge float [[VAL]], 0.000000e+00
9-
; CHECK-NEXT: br i1 [[TMP0]], label [[ENTRY_SPLIT:%.*]], label [[CALL_SQRT:%.*]]
9+
; CHECK-NEXT: br i1 [[TMP0]], label [[ENTRY_SPLIT:%.*]], label [[CALL_SQRT:%.*]], !prof [[PROF1:![0-9]+]]
1010
; CHECK: call.sqrt:
1111
; CHECK-NEXT: [[TMP1:%.*]] = tail call float @sqrtf(float [[VAL]])
1212
; CHECK-NEXT: br label [[ENTRY_SPLIT]]
@@ -19,11 +19,11 @@ entry:
1919
ret float %res
2020
}
2121

22-
define float @f_writeonly(float %val) {
22+
define float @f_writeonly(float %val) !prof !0 {
2323
; CHECK-LABEL: @f_writeonly(
2424
; CHECK-NEXT: [[RES:%.*]] = tail call float @sqrtf(float [[VAL:%.*]]) #[[ATTR0]]
2525
; CHECK-NEXT: [[TMP1:%.*]] = fcmp oge float [[VAL]], 0.000000e+00
26-
; CHECK-NEXT: br i1 [[TMP1]], label [[DOTSPLIT:%.*]], label [[CALL_SQRT:%.*]]
26+
; CHECK-NEXT: br i1 [[TMP1]], label [[DOTSPLIT:%.*]], label [[CALL_SQRT:%.*]], !prof [[PROF1]]
2727
; CHECK: call.sqrt:
2828
; CHECK-NEXT: [[TMP2:%.*]] = tail call float @sqrtf(float [[VAL]]) #[[ATTR1:[0-9]+]]
2929
; CHECK-NEXT: br label [[DOTSPLIT]]
@@ -45,8 +45,13 @@ define float @f_readonly(float %val) {
4545
}
4646

4747
declare float @sqrtf(float)
48+
49+
!0 = !{!"function_entry_count", i32 10}
4850
;.
4951
; CHECK: attributes #[[ATTR0]] = { memory(none) }
5052
; CHECK: attributes #[[ATTR1]] = { memory(write) }
5153
; CHECK: attributes #[[ATTR2]] = { memory(read) }
5254
;.
55+
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i32 10}
56+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 1048575, i32 1}
57+
;.

0 commit comments

Comments
 (0)