Skip to content

Conversation

jinhuang1102
Copy link
Contributor

@jinhuang1102 jinhuang1102 commented Sep 24, 2025

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

@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Jin Huang (jinhuang1102)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/160455.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp (+8-4)
diff --git a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
index 40de36d81ddd2..d00a3bc86d6b8 100644
--- a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
+++ b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
@@ -29,6 +29,7 @@
 #include "llvm/IR/Function.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/PatternMatch.h"
+#include "llvm/IR/ProfDataUtils.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/BuildLibCalls.h"
 #include "llvm/Transforms/Utils/Local.h"
@@ -1283,11 +1284,14 @@ void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
     Value *VR =
         ConstantInt::get(CI->getType(), static_cast<unsigned char>(RHS[i]));
     Value *Sub = Swapped ? B.CreateSub(VR, VL) : B.CreateSub(VL, VR);
-    if (i < N - 1)
-      B.CreateCondBr(B.CreateICmpNE(Sub, ConstantInt::get(CI->getType(), 0)),
-                     BBNE, BBSubs[i + 1]);
-    else
+    if (i < N - 1) {
+      BranchInst *CondBrInst = B.CreateCondBr(
+          B.CreateICmpNE(Sub, ConstantInt::get(CI->getType(), 0)), BBNE,
+          BBSubs[i + 1]);
+      setExplicitlyUnknownBranchWeights(*CondBrInst, DEBUG_TYPE);
+    } else {
       B.CreateBr(BBNE);
+    }
 
     Phi->addIncoming(Sub, BBSubs[i]);
   }

BranchInst *CondBrInst = B.CreateCondBr(
B.CreateICmpNE(Sub, ConstantInt::get(CI->getType(), 0)), BBNE,
BBSubs[i + 1]);
setExplicitlyUnknownBranchWeights(*CondBrInst, DEBUG_TYPE);
Copy link
Member

Choose a reason for hiding this comment

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

Set this only if the function entry count is non-zero. See this discussion: #158743 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

you also need a unittest. You could modify or clone the one that failed originally, to also have a function entry count and then you can check the unknown insertion.

Copy link
Member

Choose a reason for hiding this comment

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

This looks good with a unit test and the entry count change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the entry_count checking and modified the IR test case for !prof check.

@mtrofin
Copy link
Member

mtrofin commented Sep 24, 2025

(took the liberty and added the reference to the tracking issue)

@jinhuang1102 jinhuang1102 force-pushed the fixup/missing-prof-Transforms/AggressiveInstCombine branch from 7795f82 to 7848841 Compare September 24, 2025 22:25
Copy link

github-actions bot commented Sep 24, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@jinhuang1102 jinhuang1102 force-pushed the fixup/missing-prof-Transforms/AggressiveInstCombine branch from 25af10f to 6912751 Compare September 24, 2025 23:03
@@ -53,4 +53,6 @@ declare i32 @strcmp(ptr, ptr)
; CHECK: [[DBG4]] = !DILocation(line: 258, column: 10, scope: [[META5:![0-9]+]])
; CHECK: [[META5]] = distinct !DISubprogram(name: "streq", scope: [[META1]], file: [[META1]], line: 257, type: [[META6:![0-9]+]], scopeLine: 257, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META2]])
; CHECK: [[META6]] = !DISubroutineType(types: [[META2]])
; CHECK: [[PROF8]] = !{!"function_entry_count", i64 1000}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mtrofin Add function entry count and check for unknown insert here.

Copy link
Member

Choose a reason for hiding this comment

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

I think you need to give main here its function_entry_count !prof explicitly, otherwise this only passes in the build with profcheck enabled.

otherwise lgtm

@jinhuang1102 jinhuang1102 force-pushed the fixup/missing-prof-Transforms/AggressiveInstCombine branch 2 times, most recently from 224bbc7 to 48f2644 Compare September 25, 2025 07:58
@@ -5,22 +5,23 @@

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

define i32 @main() {
; CHECK-LABEL: define i32 @main() {
define i32 @main() !prof !8{
Copy link
Member

Choose a reason for hiding this comment

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

nit: place a space after !8

@jinhuang1102 jinhuang1102 force-pushed the fixup/missing-prof-Transforms/AggressiveInstCombine branch 3 times, most recently from 404d2f9 to f737e69 Compare September 25, 2025 19:16
@jinhuang1102 jinhuang1102 force-pushed the fixup/missing-prof-Transforms/AggressiveInstCombine branch from f737e69 to 23308e8 Compare September 25, 2025 20:11
@mtrofin mtrofin merged commit c4a134f into llvm:main Sep 26, 2025
9 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 26, 2025

LLVM Buildbot has detected a new failure on builder clang-aarch64-quick running on linaro-clang-aarch64-quick while building llvm at step 5 "ninja check 1".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/65/builds/23166

Here is the relevant piece of the build log for the reference
Step 5 (ninja check 1) failure: stage 1 checked (failure)
******************** TEST 'Clangd Unit Tests :: ./ClangdTests/44/332' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:/home/tcwg-buildbot/worker/clang-aarch64-quick/stage1/tools/clang/tools/extra/clangd/unittests/./ClangdTests-Clangd Unit Tests-3983049-44-332.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=332 GTEST_SHARD_INDEX=44 /home/tcwg-buildbot/worker/clang-aarch64-quick/stage1/tools/clang/tools/extra/clangd/unittests/./ClangdTests
--

Script:
--
/home/tcwg-buildbot/worker/clang-aarch64-quick/stage1/tools/clang/tools/extra/clangd/unittests/./ClangdTests --gtest_filter=ClangdServerTest.Reparse
--
ASTWorker building file /clangd-test/foo.cpp version null with command 
[/clangd-test]
clang -ffreestanding /clangd-test/foo.cpp
Driver produced command: cc1 -cc1 -triple aarch64-unknown-linux-gnu -fsyntax-only -disable-free -clear-ast-before-backend -main-file-name foo.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=non-leaf -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -enable-tlsdesc -target-cpu generic -target-feature +v8a -target-feature +fp-armv8 -target-feature +neon -target-abi aapcs -debugger-tuning=gdb -fdebug-compilation-dir=/clangd-test -fcoverage-compilation-dir=/clangd-test -resource-dir lib/clang/22 -internal-isystem lib/clang/22/include -internal-isystem /usr/local/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -ferror-limit 19 -fno-signed-char -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -no-round-trip-args -target-feature -fmv -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -x c++ /clangd-test/foo.cpp
Building first preamble for /clangd-test/foo.cpp version null
../llvm/clang-tools-extra/clangd/unittests/ClangdTests.cpp:241: Failure
Value of: Server.blockUntilIdleForTest()
  Actual: false
Expected: true
Waiting for diagnostics

Built preamble of size 740008 for file /clangd-test/foo.cpp version null in 17.16 seconds

../llvm/clang-tools-extra/clangd/unittests/ClangdTests.cpp:241
Value of: Server.blockUntilIdleForTest()
  Actual: false
Expected: true
Waiting for diagnostics



********************


mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
…vm#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 llvm#147390
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.

6 participants