diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h --- a/llvm/include/llvm/IR/Instruction.h +++ b/llvm/include/llvm/IR/Instruction.h @@ -393,6 +393,9 @@ /// Drops metadata that may generate poison. void dropPoisonGeneratingMetadata(); + /// Drops profile metadata + void dropProfMetadata(); + /// Return true if this instruction has poison-generating flags or metadata. bool hasPoisonGeneratingFlagsOrMetadata() const { return hasPoisonGeneratingFlags() || hasPoisonGeneratingMetadata(); diff --git a/llvm/include/llvm/IR/ProfDataUtils.h b/llvm/include/llvm/IR/ProfDataUtils.h --- a/llvm/include/llvm/IR/ProfDataUtils.h +++ b/llvm/include/llvm/IR/ProfDataUtils.h @@ -99,5 +99,7 @@ /// metadata was found. bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalWeights); +void StripProfileInfo(Module &M); + } // namespace llvm #endif diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp --- a/llvm/lib/IR/Instruction.cpp +++ b/llvm/lib/IR/Instruction.cpp @@ -917,6 +917,8 @@ llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); } +void Instruction::dropProfMetadata() { eraseMetadata(LLVMContext::MD_prof); } + void Instruction::swapProfMetadata() { MDNode *ProfileData = getBranchWeightMDNode(*this); if (!ProfileData || ProfileData->getNumOperands() != 3) diff --git a/llvm/lib/IR/ProfDataUtils.cpp b/llvm/lib/IR/ProfDataUtils.cpp --- a/llvm/lib/IR/ProfDataUtils.cpp +++ b/llvm/lib/IR/ProfDataUtils.cpp @@ -18,6 +18,7 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Metadata.h" +#include "llvm/IR/Module.h" #include "llvm/Support/BranchProbability.h" #include "llvm/Support/CommandLine.h" @@ -184,4 +185,20 @@ return extractProfTotalWeight(I.getMetadata(LLVMContext::MD_prof), TotalVal); } +void StripProfileInfo(Module &M) { + for (auto &GV : M.globals()) { + GV.eraseMetadata(LLVMContext::MD_prof); + } + for (Function &F : M) { + for (BasicBlock &BB : F) { + for (Instruction &I : llvm::make_early_inc_range(BB)) { + if (hasProfMD(I)) { + I.dropProfMetadata(); + } + } + } + F.eraseMetadata(LLVMContext::MD_prof); + } +} + } // namespace llvm diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp --- a/llvm/tools/opt/opt.cpp +++ b/llvm/tools/opt/opt.cpp @@ -30,6 +30,7 @@ #include "llvm/IR/LegacyPassNameParser.h" #include "llvm/IR/Module.h" #include "llvm/IR/ModuleSummaryIndex.h" +#include "llvm/IR/ProfDataUtils.h" #include "llvm/IR/Verifier.h" #include "llvm/IRReader/IRReader.h" #include "llvm/InitializePasses.h" @@ -139,6 +140,10 @@ StripDebug("strip-debug", cl::desc("Strip debugger symbol info from translation unit")); +static cl::opt + StripProfile("strip-profile", + cl::desc("Strip profiler info from translation unit")); + static cl::opt StripNamedMetadata("strip-named-metadata", cl::desc("Strip module-level named metadata")); @@ -540,6 +545,10 @@ if (StripDebug) StripDebugInfo(*M); + // Strip profile info before running the verifier. + if (StripProfile) + StripProfileInfo(*M); + // Erase module-level named metadata, if requested. if (StripNamedMetadata) { while (!M->named_metadata_empty()) {