diff --git a/bolt/include/bolt/Core/DynoStats.h b/bolt/include/bolt/Core/DynoStats.h --- a/bolt/include/bolt/Core/DynoStats.h +++ b/bolt/include/bolt/Core/DynoStats.h @@ -146,8 +146,7 @@ /// Return program-wide dynostats. template -inline DynoStats getDynoStats(const FuncsType &Funcs) { - bool IsAArch64 = Funcs.begin()->second.getBinaryContext().isAArch64(); +inline DynoStats getDynoStats(const FuncsType &Funcs, bool IsAArch64) { DynoStats dynoStats(IsAArch64); for (auto &BFI : Funcs) { auto &BF = BFI.second; @@ -160,16 +159,16 @@ /// Call a function with optional before and after dynostats printing. template inline void callWithDynoStats(FnType &&Func, const FuncsType &Funcs, - StringRef Phase, const bool Flag) { - bool IsAArch64 = Funcs.begin()->second.getBinaryContext().isAArch64(); + StringRef Phase, const bool Flag, + bool IsAArch64) { DynoStats DynoStatsBefore(IsAArch64); if (Flag) - DynoStatsBefore = getDynoStats(Funcs); + DynoStatsBefore = getDynoStats(Funcs, IsAArch64); Func(); if (Flag) { - const DynoStats DynoStatsAfter = getDynoStats(Funcs); + const DynoStats DynoStatsAfter = getDynoStats(Funcs, IsAArch64); const bool Changed = (DynoStatsAfter != DynoStatsBefore); outs() << "BOLT-INFO: program-wide dynostats after running " << Phase << (Changed ? "" : " (no change)") << ":\n\n" diff --git a/bolt/include/bolt/Passes/BinaryPasses.h b/bolt/include/bolt/Passes/BinaryPasses.h --- a/bolt/include/bolt/Passes/BinaryPasses.h +++ b/bolt/include/bolt/Passes/BinaryPasses.h @@ -71,7 +71,8 @@ bool shouldPrint(const BinaryFunction &BF) const override { return false; } void runOnFunctions(BinaryContext &BC) override { - const DynoStats NewDynoStats = getDynoStats(BC.getBinaryFunctions()); + const DynoStats NewDynoStats = + getDynoStats(BC.getBinaryFunctions(), BC.isAArch64()); const bool Changed = (NewDynoStats != PrevDynoStats); outs() << "BOLT-INFO: program-wide dynostats " << Title << (Changed ? "" : " (no change)") << ":\n\n" diff --git a/bolt/lib/Rewrite/BinaryPassManager.cpp b/bolt/lib/Rewrite/BinaryPassManager.cpp --- a/bolt/lib/Rewrite/BinaryPassManager.cpp +++ b/bolt/lib/Rewrite/BinaryPassManager.cpp @@ -268,7 +268,7 @@ TimerGroupDesc, TimeOpts); callWithDynoStats([this, &Pass] { Pass->runOnFunctions(BC); }, BFs, - Pass->getName(), opts::DynoStatsAll); + Pass->getName(), opts::DynoStatsAll, BC.isAArch64()); if (opts::VerifyCFG && !std::accumulate( @@ -307,7 +307,8 @@ void BinaryFunctionPassManager::runAllPasses(BinaryContext &BC) { BinaryFunctionPassManager Manager(BC); - const DynoStats InitialDynoStats = getDynoStats(BC.getBinaryFunctions()); + const DynoStats InitialDynoStats = + getDynoStats(BC.getBinaryFunctions(), BC.isAArch64()); Manager.registerPass(std::make_unique(), opts::AsmDump.getNumOccurrences()); diff --git a/bolt/unittests/Core/CMakeLists.txt b/bolt/unittests/Core/CMakeLists.txt --- a/bolt/unittests/Core/CMakeLists.txt +++ b/bolt/unittests/Core/CMakeLists.txt @@ -8,6 +8,7 @@ add_bolt_unittest(CoreTests BinaryContext.cpp MCPlusBuilder.cpp + DynoStats.cpp ) target_link_libraries(CoreTests diff --git a/bolt/unittests/Core/DynoStats.cpp b/bolt/unittests/Core/DynoStats.cpp new file mode 100644 --- /dev/null +++ b/bolt/unittests/Core/DynoStats.cpp @@ -0,0 +1,24 @@ +//===- llvm/unittest/MC/MCInstPrinter.cpp ---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "bolt/Core/DynoStats.h" +#include "bolt/Core/BinaryFunction.h" +#include "gtest/gtest.h" +#include + +using namespace llvm::bolt; + +TEST(DynoStatsTest, emptyFuncs) { + std::map BinaryFunctions; + DynoStats DynoStatsAArch64 = + getDynoStats(BinaryFunctions, /* BC.isAArch64() = */ true); + DynoStats DynoStatsNonAArch64 = + getDynoStats(BinaryFunctions, /* BC.isAArch64() = */ false); + // Both should be null + ASSERT_EQ(DynoStatsAArch64, DynoStatsNonAArch64); +}