diff options
author | Rong Xu <xur@google.com> | 2019-07-08 21:03:12 +0000 |
---|---|---|
committer | Rong Xu <xur@google.com> | 2019-07-08 21:03:12 +0000 |
commit | f0d3dcec97a1e335452071440a73081908da712f (patch) | |
tree | 8249eaa6e8dfe759615ad7a80a507f6c23dc20b4 /llvm/tools/llvm-profdata | |
parent | c5630ac641396b37699364043bb8da297991e066 (diff) | |
download | bcm5719-llvm-f0d3dcec97a1e335452071440a73081908da712f.tar.gz bcm5719-llvm-f0d3dcec97a1e335452071440a73081908da712f.zip |
llvm-profdata] Handle the cases of overlapping input file and output file
Currently llvm-profdata does not expect the same file name for the input profile
and the output profile.
>llvm-profdata merge A.profraw B.profraw -o B.profraw
The above command runs successfully but the resulted B.profraw is not correct.
This patch fixes the issue by moving the initialization of writer after loading
the profile.
For the show command, the following will report a confusing error of
"Empty raw profile file":
>llvm-profdata show B.profraw -o B.profraw
It's harder to fix as we need to output something before loading the input profile.
I don't think that a fix for this is worth the effort. I just make the error explicit for
the show command.
Differential Revision: https://reviews.llvm.org/D64360
llvm-svn: 365386
Diffstat (limited to 'llvm/tools/llvm-profdata')
-rw-r--r-- | llvm/tools/llvm-profdata/llvm-profdata.cpp | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp index 10551fd98e1..16d3ebe3fcb 100644 --- a/llvm/tools/llvm-profdata/llvm-profdata.cpp +++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp @@ -317,11 +317,6 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs, OutputFormat != PF_Text) exitWithError("Unknown format is specified."); - std::error_code EC; - raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None); - if (EC) - exitWithErrorCode(EC, OutputFilename); - std::mutex ErrorLock; SmallSet<instrprof_error, 4> WriterErrorCodes; @@ -384,6 +379,11 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs, WC->ErrWhence); } + std::error_code EC; + raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None); + if (EC) + exitWithErrorCode(EC, OutputFilename); + InstrProfWriter &Writer = Contexts[0]->Writer; if (OutputFormat == PF_Text) { if (Error E = Writer.writeText(Output)) @@ -433,12 +433,6 @@ static void mergeSampleProfile(const WeightedFileVector &Inputs, StringRef OutputFilename, ProfileFormat OutputFormat) { using namespace sampleprof; - auto WriterOrErr = - SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]); - if (std::error_code EC = WriterOrErr.getError()) - exitWithErrorCode(EC, OutputFilename); - - auto Writer = std::move(WriterOrErr.get()); StringMap<FunctionSamples> ProfileMap; SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers; LLVMContext Context; @@ -473,6 +467,12 @@ static void mergeSampleProfile(const WeightedFileVector &Inputs, } } } + auto WriterOrErr = + SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]); + if (std::error_code EC = WriterOrErr.getError()) + exitWithErrorCode(EC, OutputFilename); + + auto Writer = std::move(WriterOrErr.get()); Writer->write(ProfileMap); } @@ -1020,6 +1020,12 @@ static int show_main(int argc, const char *argv[]) { if (OutputFilename.empty()) OutputFilename = "-"; + if (!Filename.compare(OutputFilename)) { + errs() << sys::path::filename(argv[0]) + << ": Input file name cannot be the same as the output file name!\n"; + return 1; + } + std::error_code EC; raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text); if (EC) |