Skip to content

Commit acee864

Browse files
committed
[MemCpyOpt][profcheck] Set unknown branch weights for certain selects
1 parent 9ac84a6 commit acee864

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "llvm/IR/LLVMContext.h"
4848
#include "llvm/IR/Module.h"
4949
#include "llvm/IR/PassManager.h"
50+
#include "llvm/IR/ProfDataUtils.h"
5051
#include "llvm/IR/Type.h"
5152
#include "llvm/IR/User.h"
5253
#include "llvm/IR/Value.h"
@@ -1366,6 +1367,10 @@ bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
13661367
Value *SizeDiff = Builder.CreateSub(DestSize, SrcSize);
13671368
Value *MemsetLen = Builder.CreateSelect(
13681369
Ule, ConstantInt::getNullValue(DestSize->getType()), SizeDiff);
1370+
// FIXME (#167968): we could explore estimating the branch_weights based on
1371+
// value profiling data about the 2 sizes.
1372+
if (auto *SI = dyn_cast<SelectInst>(MemsetLen))
1373+
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, DEBUG_TYPE);
13691374
Instruction *NewMemSet =
13701375
Builder.CreateMemSet(Builder.CreatePtrAdd(Dest, SrcSize),
13711376
MemSet->getOperand(1), MemsetLen, Alignment);

llvm/test/Transforms/MemCpyOpt/memset-memcpy-dbgloc.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
77
declare void @llvm.memset.p0.i64(ptr nocapture, i8, i64, i1)
88
declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture readonly, i64, i1)
99

10-
define void @test_constant(i64 %src_size, ptr %dst, i64 %dst_size, i8 %c) !dbg !5 {
10+
define void @test_constant(i64 %src_size, ptr %dst, i64 %dst_size, i8 %c) !dbg !5 !prof !14 {
1111
; CHECK-LABEL: define void @test_constant(
12-
; CHECK-SAME: i64 [[SRC_SIZE:%.*]], ptr [[DST:%.*]], i64 [[DST_SIZE:%.*]], i8 [[C:%.*]]) !dbg [[DBG5:![0-9]+]] {
12+
; CHECK-SAME: i64 [[SRC_SIZE:%.*]], ptr [[DST:%.*]], i64 [[DST_SIZE:%.*]], i8 [[C:%.*]]) !dbg [[DBG5:![0-9]+]] !prof {{.*}} {
1313
; CHECK-NEXT: [[NON_ZERO:%.*]] = icmp ne i64 [[SRC_SIZE]], 0
1414
; CHECK-NEXT: call void @llvm.assume(i1 [[NON_ZERO]])
1515
; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i64 [[DST_SIZE]], [[SRC_SIZE]], !dbg [[DBG11:![0-9]+]]
1616
; CHECK-NEXT: [[TMP2:%.*]] = sub i64 [[DST_SIZE]], [[SRC_SIZE]], !dbg [[DBG11]]
17-
; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], i64 0, i64 [[TMP2]], !dbg [[DBG11]]
17+
; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], i64 0, i64 [[TMP2]], !dbg [[DBG11]], !prof [[SELPROF:![0-9]+]]
1818
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr i8, ptr [[DST]], i64 [[SRC_SIZE]], !dbg [[DBG11]]
1919
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 1 [[TMP4]], i8 [[C]], i64 [[TMP3]], i1 false), !dbg [[DBG11]]
2020
; 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 !
2929

3030
; Validate that the memset is mapped to DILocation for the original memset.
3131
; CHECK: [[DBG11]] = !DILocation(line: 1,
32+
; CHECK: [[SELPROF]] = !{!"unknown", !"memcpyopt"}
3233
; CHECK: [[DBG12]] = !DILocation(line: 2,
3334
; CHECK: [[DBG13]] = !DILocation(line: 3,
3435

@@ -50,3 +51,4 @@ define void @test_constant(i64 %src_size, ptr %dst, i64 %dst_size, i8 %c) !dbg !
5051
!11 = !DILocation(line: 1, column: 1, scope: !5)
5152
!12 = !DILocation(line: 2, column: 1, scope: !5)
5253
!13 = !DILocation(line: 3, column: 1, scope: !5)
54+
!14 = !{!"function_entry_count", i32 10}

0 commit comments

Comments
 (0)