Skip to content

Commit c4a134f

Browse files
authored
[profcheck] Add unknown branch weights for inlined strcmp/strncmp (#160455)
The strcmp/strncmp inliner creates new conditional branches but was failing to add profile metadata. This caused the ProfileVerifierPass to fail when profcheck is enabled. This patch fixes the issue by explicitly adding unknown branch weights to these branches. Issue #147390
1 parent b4b89a6 commit c4a134f

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/IR/Function.h"
3030
#include "llvm/IR/IRBuilder.h"
3131
#include "llvm/IR/PatternMatch.h"
32+
#include "llvm/IR/ProfDataUtils.h"
3233
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
3334
#include "llvm/Transforms/Utils/BuildLibCalls.h"
3435
#include "llvm/Transforms/Utils/Local.h"
@@ -1283,11 +1284,19 @@ void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
12831284
Value *VR =
12841285
ConstantInt::get(CI->getType(), static_cast<unsigned char>(RHS[i]));
12851286
Value *Sub = Swapped ? B.CreateSub(VR, VL) : B.CreateSub(VL, VR);
1286-
if (i < N - 1)
1287-
B.CreateCondBr(B.CreateICmpNE(Sub, ConstantInt::get(CI->getType(), 0)),
1288-
BBNE, BBSubs[i + 1]);
1289-
else
1287+
if (i < N - 1) {
1288+
BranchInst *CondBrInst = B.CreateCondBr(
1289+
B.CreateICmpNE(Sub, ConstantInt::get(CI->getType(), 0)), BBNE,
1290+
BBSubs[i + 1]);
1291+
1292+
Function *F = CI->getFunction();
1293+
assert(F && "Instruction does not belong to a function!");
1294+
std::optional<Function::ProfileCount> EC = F->getEntryCount();
1295+
if (EC && EC->getCount() > 0)
1296+
setExplicitlyUnknownBranchWeights(*CondBrInst, DEBUG_TYPE);
1297+
} else {
12901298
B.CreateBr(BBNE);
1299+
}
12911300

12921301
Phi->addIncoming(Sub, BBSubs[i]);
12931302
}

llvm/test/Transforms/AggressiveInstCombine/inline-strcmp-debugloc.ll

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55

66
@.str = constant [3 x i8] c"-h\00"
77

8-
define i32 @main() {
9-
; CHECK-LABEL: define i32 @main() {
8+
define i32 @main() !prof !8 {
9+
; CHECK-LABEL: define i32 @main()
10+
; CHECK: !prof [[PROF_0:![0-9]+]]
1011
; CHECK-NEXT: [[ENTRY:.*:]]
1112
; CHECK-NEXT: br label %[[SUB_0:.*]], !dbg [[DBG4:![0-9]+]]
1213
; CHECK: [[SUB_0]]:
1314
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr null, align 1, !dbg [[DBG4]]
1415
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[TMP0]] to i32, !dbg [[DBG4]]
1516
; CHECK-NEXT: [[TMP2:%.*]] = sub i32 [[TMP1]], 45, !dbg [[DBG4]]
1617
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0, !dbg [[DBG4]]
17-
; CHECK-NEXT: br i1 [[TMP3]], label %[[NE:.*]], label %[[SUB_1:.*]], !dbg [[DBG4]]
18+
; CHECK-NEXT: br i1 [[TMP3]], label %[[NE:.*]], label %[[SUB_1:.*]], !dbg [[DBG4]], !prof [[PROF_1:![0-9]+]]
1819
; CHECK: [[SUB_1]]:
1920
; CHECK-NEXT: [[TMP4:%.*]] = load i8, ptr getelementptr inbounds (i8, ptr null, i64 1), align 1, !dbg [[DBG4]]
2021
; CHECK-NEXT: [[TMP5:%.*]] = zext i8 [[TMP4]] to i32, !dbg [[DBG4]]
2122
; CHECK-NEXT: [[TMP6:%.*]] = sub i32 [[TMP5]], 104, !dbg [[DBG4]]
2223
; CHECK-NEXT: [[TMP7:%.*]] = icmp ne i32 [[TMP6]], 0, !dbg [[DBG4]]
23-
; CHECK-NEXT: br i1 [[TMP7]], label %[[NE]], label %[[SUB_2:.*]], !dbg [[DBG4]]
24+
; CHECK-NEXT: br i1 [[TMP7]], label %[[NE]], label %[[SUB_2:.*]], !dbg [[DBG4]], !prof [[PROF_1]]
2425
; CHECK: [[SUB_2]]:
2526
; CHECK-NEXT: br label %[[NE]], !dbg [[DBG4]]
2627
; CHECK: [[NE]]:
@@ -46,11 +47,14 @@ declare i32 @strcmp(ptr, ptr)
4647
!4 = !DILocation(line: 258, column: 10, scope: !5)
4748
!5 = distinct !DISubprogram(name: "streq", scope: !1, file: !1, line: 257, type: !7, scopeLine: 257, unit: !0, retainedNodes: !2)
4849
!7 = !DISubroutineType(types: !2)
50+
!8 = !{!"function_entry_count", i64 1000}
4951
;.
5052
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: [[META2:![0-9]+]], retainedTypes: [[META2]], globals: [[META2]])
5153
; CHECK: [[META1]] = !DIFile(filename: "test.c", directory: {{.*}})
5254
; CHECK: [[META2]] = !{}
55+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
5356
; CHECK: [[DBG4]] = !DILocation(line: 258, column: 10, scope: [[META5:![0-9]+]])
5457
; CHECK: [[META5]] = distinct !DISubprogram(name: "streq", scope: [[META1]], file: [[META1]], line: 257, type: [[META6:![0-9]+]], scopeLine: 257, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META2]])
5558
; CHECK: [[META6]] = !DISubroutineType(types: [[META2]])
59+
; CHECK: [[PROF_1]] = !{!"unknown", !"aggressive-instcombine"}
5660
;.

0 commit comments

Comments
 (0)