Make CallInst::updateProfWeight emit i32 weights instead of i64

Typically branch_weights are i32, not i64.
This fixes entry_counts_cold.ll under NPM.

Reviewed By: asbirlea

Differential Revision: https://reviews.llvm.org/D90539
This commit is contained in:
Arthur Eubanks 2020-10-31 12:38:03 -07:00
parent 01cee921ab
commit 3d1149c6fe
6 changed files with 40 additions and 14 deletions

View File

@ -560,8 +560,9 @@ void CallInst::updateProfWeight(uint64_t S, uint64_t T) {
->getValue()
.getZExtValue());
Val *= APS;
Vals.push_back(MDB.createConstant(ConstantInt::get(
Type::getInt64Ty(getContext()), Val.udiv(APT).getLimitedValue())));
Vals.push_back(MDB.createConstant(
ConstantInt::get(Type::getInt32Ty(getContext()),
Val.udiv(APT).getLimitedValue(UINT32_MAX))));
} else if (ProfDataName->getString().equals("VP"))
for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
// The first value is the key of the value profile, which will not change.

View File

@ -48,13 +48,13 @@ define void @caller() {
!13 = !{i32 999000, i64 100, i32 1}
!14 = !{i32 999999, i64 1, i32 2}
!15 = !{!"function_entry_count", i64 1000}
!16 = !{!"branch_weights", i64 2000}
!17 = !{!"branch_weights", i64 400}
!16 = !{!"branch_weights", i32 2000}
!17 = !{!"branch_weights", i32 400}
!18 = !{!"VP", i32 0, i64 140, i64 111, i64 80, i64 222, i64 40, i64 333, i64 20}
attributes #0 = { alwaysinline }
; CHECK: ![[ENTRY_COUNT]] = !{!"function_entry_count", i64 600}
; CHECK: ![[COUNT_CALLEE1]] = !{!"branch_weights", i64 2000}
; CHECK: ![[COUNT_CALLEE]] = !{!"branch_weights", i64 1200}
; CHECK: ![[COUNT_CALLEE1]] = !{!"branch_weights", i32 2000}
; CHECK: ![[COUNT_CALLEE]] = !{!"branch_weights", i32 1200}
; CHECK: ![[COUNT_IND_CALLEE]] = !{!"VP", i32 0, i64 84, i64 111, i64 48, i64 222, i64 24, i64 333, i64 12}
; CHECK: ![[COUNT_CALLER]] = !{!"branch_weights", i64 800}
; CHECK: ![[COUNT_CALLER]] = !{!"branch_weights", i32 800}
; CHECK: ![[COUNT_IND_CALLER]] = !{!"VP", i32 0, i64 56, i64 111, i64 32, i64 222, i64 16, i64 333, i64 8}

View File

@ -49,12 +49,12 @@ define void @caller() {
!13 = !{i32 999000, i64 100, i32 1}
!14 = !{i32 999999, i64 1, i32 2}
!15 = !{!"function_entry_count", i64 1000}
!16 = !{!"branch_weights", i64 2000}
!17 = !{!"branch_weights", i64 400}
!16 = !{!"branch_weights", i32 2000}
!17 = !{!"branch_weights", i32 400}
!18 = !{!"VP", i32 0, i64 140, i64 111, i64 80, i64 222, i64 40, i64 333, i64 20}
; CHECK: ![[ENTRY_COUNT]] = !{!"function_entry_count", i64 600}
; CHECK: ![[COUNT_CALLEE1]] = !{!"branch_weights", i64 2000}
; CHECK: ![[COUNT_CALLEE]] = !{!"branch_weights", i64 1200}
; CHECK: ![[COUNT_CALLEE1]] = !{!"branch_weights", i32 2000}
; CHECK: ![[COUNT_CALLEE]] = !{!"branch_weights", i32 1200}
; CHECK: ![[COUNT_IND_CALLEE]] = !{!"VP", i32 0, i64 84, i64 111, i64 48, i64 222, i64 24, i64 333, i64 12}
; CHECK: ![[COUNT_CALLER]] = !{!"branch_weights", i64 800}
; CHECK: ![[COUNT_CALLER]] = !{!"branch_weights", i32 800}
; CHECK: ![[COUNT_IND_CALLER]] = !{!"VP", i32 0, i64 56, i64 111, i64 32, i64 222, i64 16, i64 333, i64 8}

View File

@ -1,4 +1,5 @@
; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/entry_counts_cold.prof -S | FileCheck %s
; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/entry_counts_cold.prof -S | FileCheck %s
; ModuleID = 'temp.bc'
source_filename = "temp.c"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
@ -108,7 +109,7 @@ attributes #4 = { nounwind }
; CHECK: [[TOP]] = !{!"function_entry_count", i64 101}
; CHECK: [[FOO]] = !{!"function_entry_count", i64 151}
; CHECK: [[BAR]] = !{!"function_entry_count", i64 303}
; CHECK: [[BAZ]] = !{!"branch_weights", i64 303}
; CHECK: [[BAZ]] = !{!"branch_weights", i32 303}
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 8.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: GNU)
!1 = !DIFile(filename: "temp.c", directory: "llvm/test/Transforms/SampleProfile")

View File

@ -91,7 +91,7 @@ declare i32 @printf(i8*, ...)
; SCALE: name: "sum"
; SCALE-NEXT: {!"function_entry_count", i64 46}
; SCALE: !{!"branch_weights", i32 11, i32 2}
; SCALE: !{!"branch_weights", i64 20}
; SCALE: !{!"branch_weights", i32 20}
; SCALE: name: "sub"
; SCALE-NEXT: {!"function_entry_count", i64 -1}

View File

@ -1376,5 +1376,29 @@ TEST(InstructionsTest, DropLocation) {
}
}
TEST(InstructionsTest, BranchWeightOverflow) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(C,
R"(
declare void @callee()
define void @caller() {
call void @callee(), !prof !1
ret void
}
!1 = !{!"branch_weights", i32 20000}
)");
ASSERT_TRUE(M);
CallInst *CI =
cast<CallInst>(&M->getFunction("caller")->getEntryBlock().front());
uint64_t ProfWeight;
CI->extractProfTotalWeight(ProfWeight);
ASSERT_EQ(ProfWeight, 20000U);
CI->updateProfWeight(10000000, 1);
CI->extractProfTotalWeight(ProfWeight);
ASSERT_EQ(ProfWeight, UINT32_MAX);
}
} // end anonymous namespace
} // end namespace llvm