diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -560,8 +560,9 @@ ->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. diff --git a/llvm/test/Transforms/Inline/prof-update-sample-alwaysinline.ll b/llvm/test/Transforms/Inline/prof-update-sample-alwaysinline.ll --- a/llvm/test/Transforms/Inline/prof-update-sample-alwaysinline.ll +++ b/llvm/test/Transforms/Inline/prof-update-sample-alwaysinline.ll @@ -48,13 +48,13 @@ !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} diff --git a/llvm/test/Transforms/Inline/prof-update-sample.ll b/llvm/test/Transforms/Inline/prof-update-sample.ll --- a/llvm/test/Transforms/Inline/prof-update-sample.ll +++ b/llvm/test/Transforms/Inline/prof-update-sample.ll @@ -49,12 +49,12 @@ !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} diff --git a/llvm/test/Transforms/SampleProfile/entry_counts_cold.ll b/llvm/test/Transforms/SampleProfile/entry_counts_cold.ll --- a/llvm/test/Transforms/SampleProfile/entry_counts_cold.ll +++ b/llvm/test/Transforms/SampleProfile/entry_counts_cold.ll @@ -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 @@ ; 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") diff --git a/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll b/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll --- a/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll +++ b/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll @@ -91,7 +91,7 @@ ; 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} diff --git a/llvm/unittests/IR/InstructionsTest.cpp b/llvm/unittests/IR/InstructionsTest.cpp --- a/llvm/unittests/IR/InstructionsTest.cpp +++ b/llvm/unittests/IR/InstructionsTest.cpp @@ -1376,5 +1376,29 @@ } } +TEST(InstructionsTest, BranchWeightOverflow) { + LLVMContext C; + std::unique_ptr 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(&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