From 60e56af0758d44b7e570ccd79fbf40714466cdda Mon Sep 17 00:00:00 2001 From: Hiroshi Yamauchi Date: Tue, 3 Dec 2019 15:02:37 -0800 Subject: [PATCH] [PGO][PGSO] Distinguish queries from unit tests and explicitly enable for the existing IR passes only. NFC. Summary: This is one more prep step necessary before the code gen pass instrumentation code could go in. Reviewers: davidxl Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D70988 --- include/llvm/Transforms/Utils/SizeOpts.h | 16 ++++++++++++++-- lib/Transforms/Utils/SizeOpts.cpp | 5 +++++ unittests/Target/X86/MachineSizeOptsTest.cpp | 14 +++++++------- unittests/Transforms/Utils/SizeOptsTest.cpp | 14 +++++++------- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/include/llvm/Transforms/Utils/SizeOpts.h b/include/llvm/Transforms/Utils/SizeOpts.h index 2d2edfac8c4..ba0f86c4526 100644 --- a/include/llvm/Transforms/Utils/SizeOpts.h +++ b/include/llvm/Transforms/Utils/SizeOpts.h @@ -21,6 +21,7 @@ using namespace llvm; extern cl::opt EnablePGSO; extern cl::opt PGSOLargeWorkingSetSizeOnly; +extern cl::opt PGSOIRPassOrTestOnly; extern cl::opt PGSOColdCodeOnly; extern cl::opt ForcePGSO; extern cl::opt PgsoCutoffInstrProf; @@ -34,8 +35,9 @@ class Function; class ProfileSummaryInfo; enum class PGSOQueryType { - IRPass, // A query call from an IR-level transform pass. - Other, // Others. + IRPass, // A query call from an IR-level transform pass. + Test, // A query call from a unit test. + Other, // Others. }; template @@ -48,6 +50,11 @@ bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI, return true; if (!EnablePGSO) return false; + // Temporarily enable size optimizations only for the IR pass or test query + // sites for gradual commit/rollout. This is to be removed later. + if (PGSOIRPassOrTestOnly && !(QueryType == PGSOQueryType::IRPass || + QueryType == PGSOQueryType::Test)) + return false; if (PGSOColdCodeOnly || (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize())) { // Even if the working set size isn't large, size-optimize cold code. @@ -68,6 +75,11 @@ bool shouldOptimizeForSizeImpl(const BlockT *BB, ProfileSummaryInfo *PSI, return true; if (!EnablePGSO) return false; + // Temporarily enable size optimizations only for the IR pass or test query + // sites for gradual commit/rollout. This is to be removed later. + if (PGSOIRPassOrTestOnly && !(QueryType == PGSOQueryType::IRPass || + QueryType == PGSOQueryType::Test)) + return false; if (PGSOColdCodeOnly || (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize())) { // Even if the working set size isn't large, size-optimize cold code. diff --git a/lib/Transforms/Utils/SizeOpts.cpp b/lib/Transforms/Utils/SizeOpts.cpp index 555073af0b2..cab375225e8 100644 --- a/lib/Transforms/Utils/SizeOpts.cpp +++ b/lib/Transforms/Utils/SizeOpts.cpp @@ -28,6 +28,11 @@ cl::opt PGSOColdCodeOnly( cl::desc("Apply the profile guided size optimizations only " "to cold code.")); +cl::opt PGSOIRPassOrTestOnly( + "pgso-ir-pass-or-test-only", cl::Hidden, cl::init(true), + cl::desc("Apply the profile guided size optimizations only" + "to the IR passes or tests.")); + cl::opt ForcePGSO( "force-pgso", cl::Hidden, cl::init(false), cl::desc("Force the (profiled-guided) size optimizations. ")); diff --git a/unittests/Target/X86/MachineSizeOptsTest.cpp b/unittests/Target/X86/MachineSizeOptsTest.cpp index 2d1ddf11afc..449d426d9b6 100644 --- a/unittests/Target/X86/MachineSizeOptsTest.cpp +++ b/unittests/Target/X86/MachineSizeOptsTest.cpp @@ -113,13 +113,13 @@ TEST_F(MachineSizeOptsTest, Test) { ASSERT_TRUE(iter == BB0.succ_end()); MachineBasicBlock *BB3 = *BB1->succ_begin(); ASSERT_TRUE(BB3 == *BB2->succ_begin()); - EXPECT_FALSE(shouldOptimizeForSize(F, &PSI, MBFI_F)); - EXPECT_TRUE(shouldOptimizeForSize(G, &PSI, MBFI_G)); - EXPECT_FALSE(shouldOptimizeForSize(H, &PSI, MBFI_H)); - EXPECT_FALSE(shouldOptimizeForSize(&BB0, &PSI, MBFI_F)); - EXPECT_FALSE(shouldOptimizeForSize(BB1, &PSI, MBFI_F)); - EXPECT_TRUE(shouldOptimizeForSize(BB2, &PSI, MBFI_F)); - EXPECT_FALSE(shouldOptimizeForSize(BB3, &PSI, MBFI_F)); + EXPECT_FALSE(shouldOptimizeForSize(F, &PSI, MBFI_F, PGSOQueryType::Test)); + EXPECT_TRUE(shouldOptimizeForSize(G, &PSI, MBFI_G, PGSOQueryType::Test)); + EXPECT_FALSE(shouldOptimizeForSize(H, &PSI, MBFI_H, PGSOQueryType::Test)); + EXPECT_FALSE(shouldOptimizeForSize(&BB0, &PSI, MBFI_F, PGSOQueryType::Test)); + EXPECT_FALSE(shouldOptimizeForSize(BB1, &PSI, MBFI_F, PGSOQueryType::Test)); + EXPECT_TRUE(shouldOptimizeForSize(BB2, &PSI, MBFI_F, PGSOQueryType::Test)); + EXPECT_FALSE(shouldOptimizeForSize(BB3, &PSI, MBFI_F, PGSOQueryType::Test)); } const char* MachineSizeOptsTest::MIRString = R"MIR( diff --git a/unittests/Transforms/Utils/SizeOptsTest.cpp b/unittests/Transforms/Utils/SizeOptsTest.cpp index 55ca7863575..7caa5ed319a 100644 --- a/unittests/Transforms/Utils/SizeOptsTest.cpp +++ b/unittests/Transforms/Utils/SizeOptsTest.cpp @@ -68,13 +68,13 @@ TEST_F(SizeOptsTest, Test) { BasicBlock *BB3 = BB1->getSingleSuccessor(); EXPECT_TRUE(PSI.hasProfileSummary()); - EXPECT_FALSE(shouldOptimizeForSize(F, &PSI, BFI_F)); - EXPECT_TRUE(shouldOptimizeForSize(G, &PSI, BFI_G)); - EXPECT_FALSE(shouldOptimizeForSize(H, &PSI, BFI_H)); - EXPECT_FALSE(shouldOptimizeForSize(&BB0, &PSI, BFI_F)); - EXPECT_FALSE(shouldOptimizeForSize(BB1, &PSI, BFI_F)); - EXPECT_TRUE(shouldOptimizeForSize(BB2, &PSI, BFI_F)); - EXPECT_FALSE(shouldOptimizeForSize(BB3, &PSI, BFI_F)); + EXPECT_FALSE(shouldOptimizeForSize(F, &PSI, BFI_F, PGSOQueryType::Test)); + EXPECT_TRUE(shouldOptimizeForSize(G, &PSI, BFI_G, PGSOQueryType::Test)); + EXPECT_FALSE(shouldOptimizeForSize(H, &PSI, BFI_H, PGSOQueryType::Test)); + EXPECT_FALSE(shouldOptimizeForSize(&BB0, &PSI, BFI_F, PGSOQueryType::Test)); + EXPECT_FALSE(shouldOptimizeForSize(BB1, &PSI, BFI_F, PGSOQueryType::Test)); + EXPECT_TRUE(shouldOptimizeForSize(BB2, &PSI, BFI_F, PGSOQueryType::Test)); + EXPECT_FALSE(shouldOptimizeForSize(BB3, &PSI, BFI_F, PGSOQueryType::Test)); } const char* SizeOptsTest::IRString = R"IR(