Skip to content

Conversation

@mtrofin
Copy link
Member

@mtrofin mtrofin commented Nov 11, 2025

Issue #147390

Copy link
Member Author

mtrofin commented Nov 11, 2025

@mtrofin mtrofin marked this pull request as ready for review November 11, 2025 23:04
@mtrofin mtrofin requested a review from nikic as a code owner November 11, 2025 23:04
@mtrofin mtrofin removed the request for review from nikic November 11, 2025 23:04
@llvmbot
Copy link
Member

llvmbot commented Nov 11, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Mircea Trofin (mtrofin)

Changes

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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp (+3)
  • (modified) llvm/test/Transforms/MemCpyOpt/memset-memcpy-dbgloc.ll (+5-3)
diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index 08be5df9872b7..5d11286e2cd7f 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -47,6 +47,7 @@
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PassManager.h"
+#include "llvm/IR/ProfDataUtils.h"
 #include "llvm/IR/Type.h"
 #include "llvm/IR/User.h"
 #include "llvm/IR/Value.h"
@@ -1366,6 +1367,8 @@ bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
   Value *SizeDiff = Builder.CreateSub(DestSize, SrcSize);
   Value *MemsetLen = Builder.CreateSelect(
       Ule, ConstantInt::getNullValue(DestSize->getType()), SizeDiff);
+  if (auto *SI = dyn_cast<SelectInst>(MemsetLen))
+    setExplicitlyUnknownBranchWeightsIfProfiled(*SI, DEBUG_TYPE);
   Instruction *NewMemSet =
       Builder.CreateMemSet(Builder.CreatePtrAdd(Dest, SrcSize),
                            MemSet->getOperand(1), MemsetLen, Alignment);
diff --git a/llvm/test/Transforms/MemCpyOpt/memset-memcpy-dbgloc.ll b/llvm/test/Transforms/MemCpyOpt/memset-memcpy-dbgloc.ll
index 3f577f09ada1e..edd64e5efe1aa 100644
--- a/llvm/test/Transforms/MemCpyOpt/memset-memcpy-dbgloc.ll
+++ b/llvm/test/Transforms/MemCpyOpt/memset-memcpy-dbgloc.ll
@@ -7,14 +7,14 @@ target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
 declare void @llvm.memset.p0.i64(ptr nocapture, i8, i64, i1)
 declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture readonly, i64, i1)
 
-define void @test_constant(i64 %src_size, ptr %dst, i64 %dst_size, i8 %c) !dbg !5 {
+define void @test_constant(i64 %src_size, ptr %dst, i64 %dst_size, i8 %c) !dbg !5 !prof !14 {
 ; CHECK-LABEL: define void @test_constant(
-; CHECK-SAME: i64 [[SRC_SIZE:%.*]], ptr [[DST:%.*]], i64 [[DST_SIZE:%.*]], i8 [[C:%.*]]) !dbg [[DBG5:![0-9]+]] {
+; CHECK-SAME: i64 [[SRC_SIZE:%.*]], ptr [[DST:%.*]], i64 [[DST_SIZE:%.*]], i8 [[C:%.*]]) !dbg [[DBG5:![0-9]+]] !prof {{.*}} {
 ; CHECK-NEXT:    [[NON_ZERO:%.*]] = icmp ne i64 [[SRC_SIZE]], 0
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[NON_ZERO]])
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i64 [[DST_SIZE]], [[SRC_SIZE]], !dbg [[DBG11:![0-9]+]]
 ; CHECK-NEXT:    [[TMP2:%.*]] = sub i64 [[DST_SIZE]], [[SRC_SIZE]], !dbg [[DBG11]]
-; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[TMP1]], i64 0, i64 [[TMP2]], !dbg [[DBG11]]
+; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[TMP1]], i64 0, i64 [[TMP2]], !dbg [[DBG11]], !prof [[SELPROF:![0-9]+]]
 ; CHECK-NEXT:    [[TMP4:%.*]] = getelementptr i8, ptr [[DST]], i64 [[SRC_SIZE]], !dbg [[DBG11]]
 ; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 1 [[TMP4]], i8 [[C]], i64 [[TMP3]], i1 false), !dbg [[DBG11]]
 ; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i64(ptr [[DST]], ptr @C, i64 [[SRC_SIZE]], i1 false), !dbg [[DBG12:![0-9]+]]
@@ -29,6 +29,7 @@ define void @test_constant(i64 %src_size, ptr %dst, i64 %dst_size, i8 %c) !dbg !
 
 ; Validate that the memset is mapped to DILocation for the original memset.
 ; CHECK: [[DBG11]] = !DILocation(line: 1,
+; CHECK: [[SELPROF]] = !{!"unknown", !"memcpyopt"}
 ; CHECK: [[DBG12]] = !DILocation(line: 2,
 ; CHECK: [[DBG13]] = !DILocation(line: 3,
 
@@ -50,3 +51,4 @@ define void @test_constant(i64 %src_size, ptr %dst, i64 %dst_size, i8 %c) !dbg !
 !11 = !DILocation(line: 1, column: 1, scope: !5)
 !12 = !DILocation(line: 2, column: 1, scope: !5)
 !13 = !DILocation(line: 3, column: 1, scope: !5)
+!14 = !{!"function_entry_count", i32 10}
\ No newline at end of file

@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_memcpyopt_profcheck_set_unknown_branch_weights_for_certain_selects branch from 9821ae7 to 562be61 Compare November 12, 2025 01:02
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_mergeicmp_profcheck_propagate_profile_info branch 2 times, most recently from e5f8a76 to 32b01bd Compare November 12, 2025 01:47
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_memcpyopt_profcheck_set_unknown_branch_weights_for_certain_selects branch 2 times, most recently from c9da492 to 28594e2 Compare November 13, 2025 20:27
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_mergeicmp_profcheck_propagate_profile_info branch from 32b01bd to cd79889 Compare November 13, 2025 20:27
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_mergeicmp_profcheck_propagate_profile_info branch from cd79889 to c1e4550 Compare November 13, 2025 20:47
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_memcpyopt_profcheck_set_unknown_branch_weights_for_certain_selects branch from 28594e2 to 99a9fce Compare November 13, 2025 20:47
Copy link
Contributor

@teresajohnson teresajohnson left a comment

Choose a reason for hiding this comment

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

lgtm with one question below

Value *SizeDiff = Builder.CreateSub(DestSize, SrcSize);
Value *MemsetLen = Builder.CreateSelect(
Ule, ConstantInt::getNullValue(DestSize->getType()), SizeDiff);
if (auto *SI = dyn_cast<SelectInst>(MemsetLen))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this have a TODO or FIXME? I.e. is there a hope that we would be able to put something other than unknown here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Chatted offline with @xur-llvm, actually we do record a histogram of frequency of various kinds of sizes, so some kind of analysis would be possible. Let's see how many of these selects make it in CodeGenPrepare (we have the DEBUG_TYPE to help with that). My suspicion is that we (1) don't do a good VP propagation for memops in the first place, and (2) some of these memops are synthetic (from intrinsics). Currently the profcheck work is about branches (br, switch, select), but we should get back to this.

@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_memcpyopt_profcheck_set_unknown_branch_weights_for_certain_selects branch from 99a9fce to b85a685 Compare November 13, 2025 22:33
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_mergeicmp_profcheck_propagate_profile_info branch from c1e4550 to 7e5054e Compare November 13, 2025 22:33
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_memcpyopt_profcheck_set_unknown_branch_weights_for_certain_selects branch from b85a685 to 52e98af Compare November 14, 2025 15:42
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_mergeicmp_profcheck_propagate_profile_info branch from 7e5054e to 1f12bc6 Compare November 14, 2025 15:42
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_memcpyopt_profcheck_set_unknown_branch_weights_for_certain_selects branch from 52e98af to 10f16a8 Compare November 14, 2025 15:55
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_mergeicmp_profcheck_propagate_profile_info branch from 1f12bc6 to 22d3e3b Compare November 14, 2025 15:55
Base automatically changed from users/mtrofin/11-11-_mergeicmp_profcheck_propagate_profile_info to main November 14, 2025 17:29
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_memcpyopt_profcheck_set_unknown_branch_weights_for_certain_selects branch from 10f16a8 to acee864 Compare November 14, 2025 17:31
@mtrofin mtrofin force-pushed the users/mtrofin/11-11-_memcpyopt_profcheck_set_unknown_branch_weights_for_certain_selects branch from acee864 to 48c758d Compare November 14, 2025 17:35
Copy link
Member Author

mtrofin commented Nov 14, 2025

Merge activity

  • Nov 14, 6:35 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Nov 14, 6:36 PM UTC: @mtrofin merged this pull request with Graphite.

@mtrofin mtrofin merged commit 17789e9 into main Nov 14, 2025
10 checks passed
@mtrofin mtrofin deleted the users/mtrofin/11-11-_memcpyopt_profcheck_set_unknown_branch_weights_for_certain_selects branch November 14, 2025 18:36
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.

4 participants