Index: llvm/trunk/include/llvm/IR/Module.h =================================================================== --- llvm/trunk/include/llvm/IR/Module.h +++ llvm/trunk/include/llvm/IR/Module.h @@ -637,7 +637,7 @@ void setPICLevel(PICLevel::Level PL); /// @} - /// @name Utility functions for querying and setting PGO counts + /// @name Utility functions for querying and setting PGO summary /// @{ /// \brief Set maximum function count in PGO mode @@ -645,6 +645,12 @@ /// \brief Returns maximum function count in PGO mode Optional getMaximumFunctionCount(); + + /// \brief Attach profile summary metadata to this module. + void setProfileSummary(Metadata *M); + + /// \brief Returns profile summary metadata + Metadata *getProfileSummary(); /// @} }; Index: llvm/trunk/lib/IR/Module.cpp =================================================================== --- llvm/trunk/lib/IR/Module.cpp +++ llvm/trunk/lib/IR/Module.cpp @@ -485,3 +485,11 @@ return None; return cast(Val->getValue())->getZExtValue(); } + +void Module::setProfileSummary(Metadata *M) { + addModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M); +} + +Metadata *Module::getProfileSummary() { + return getModuleFlag("ProfileSummary"); +} Index: llvm/trunk/unittests/ProfileData/InstrProfTest.cpp =================================================================== --- llvm/trunk/unittests/ProfileData/InstrProfTest.cpp +++ llvm/trunk/unittests/ProfileData/InstrProfTest.cpp @@ -180,6 +180,8 @@ }; InstrProfSummary &PS = Reader->getSummary(); VerifySummary(PS); + + // Test that conversion of summary to and from Metadata works. Metadata *MD = PS.getMD(getGlobalContext()); ASSERT_TRUE(MD); ProfileSummary *PSFromMD = ProfileSummary::getFromMD(MD); @@ -188,6 +190,18 @@ InstrProfSummary *IPS = cast(PSFromMD); VerifySummary(*IPS); delete IPS; + + // Test that summary can be attached to and read back from module. + Module M("my_module", getGlobalContext()); + M.setProfileSummary(MD); + MD = M.getProfileSummary(); + ASSERT_TRUE(MD); + PSFromMD = ProfileSummary::getFromMD(MD); + ASSERT_TRUE(PSFromMD); + ASSERT_TRUE(isa(PSFromMD)); + IPS = cast(PSFromMD); + VerifySummary(*IPS); + delete IPS; } TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) { Index: llvm/trunk/unittests/ProfileData/SampleProfTest.cpp =================================================================== --- llvm/trunk/unittests/ProfileData/SampleProfTest.cpp +++ llvm/trunk/unittests/ProfileData/SampleProfTest.cpp @@ -126,6 +126,7 @@ SampleProfileSummary &Summary = Reader->getSummary(); VerifySummary(Summary); + // Test that conversion of summary to and from Metadata works. Metadata *MD = Summary.getMD(getGlobalContext()); ASSERT_TRUE(MD); ProfileSummary *PS = ProfileSummary::getFromMD(MD); @@ -134,6 +135,18 @@ SampleProfileSummary *SPS = cast(PS); VerifySummary(*SPS); delete SPS; + + // Test that summary can be attached to and read back from module. + Module M("my_module", getGlobalContext()); + M.setProfileSummary(MD); + MD = M.getProfileSummary(); + ASSERT_TRUE(MD); + PS = ProfileSummary::getFromMD(MD); + ASSERT_TRUE(PS); + ASSERT_TRUE(isa(PS)); + SPS = cast(PS); + VerifySummary(*SPS); + delete SPS; } };