This is an archive of the discontinued LLVM Phabricator instance.

[llvm-profdata] Add option to cap profile output size
ClosedPublic

Authored by huangjd on Jan 10 2023, 4:45 PM.

Details

Summary

D139603 (add option to llvm-profdata to reduce output profile size) contains test cases that are not cross-platform. Moving those tests to unit test and making sure the feature is callable from llvm library

Diff Detail

Event Timeline

huangjd created this revision.Jan 10 2023, 4:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 10 2023, 4:45 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
huangjd requested review of this revision.Jan 10 2023, 4:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 10 2023, 4:45 PM
snehasish accepted this revision.Jan 10 2023, 6:01 PM

lgtm with some comments.

llvm/lib/ProfileData/SampleProfWriter.cpp
100

This will become unused in non-DEBUG build, maybe add the workaround here https://reviews.llvm.org/rG9f4a9d3f44501fa755eb71fe855e15cf0e59e8b8

Or wrap this in LLVM_DEBUG too.

Also IterationCount below as mentioned in this comment on the previous revision: https://reviews.llvm.org/D139603#inline-1365026

llvm/unittests/tools/llvm-profdata/OutputSizeLimitTest.cpp
41

I think we should replace report_fatal_error with

std::error_code EC = ReaderOrErr.getError();
ASSERT_FALSE(EC) << EC.message().c_str();

to let gtest handle the failure cleanly.

69

Perhaps TestWriteWithSizeLimit instead of TestOutputSizeLimit1 is a little more informative?

71

This should probably be EXPECT_LE.
The rationale is explained here https://stackoverflow.com/a/2565309

This revision is now accepted and ready to land.Jan 10 2023, 6:01 PM
huangjd updated this revision to Diff 488407.Jan 11 2023, 3:58 PM

Updating D141446: Fix to D139603(reverted) - moved size check to unit test so that it is cross-platform

huangjd updated this revision to Diff 488409.Jan 11 2023, 3:59 PM

Updating D141446: Fix to D139603(reverted) - moved size check to unit test so that it is cross-platform

This revision was landed with ongoing or failed builds.Jan 11 2023, 4:42 PM
This revision was automatically updated to reflect the committed changes.

Hi @huangjd ,

check-llvm gets failed with the following linker errors:

FAILED: unittests/tools/llvm-profdata/LLVMProfdataTests 
: && /usr/bin/c++ -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -fuse-ld=gold     -Wl,--gc-sections unittests/tools/llvm-profdata/CMakeFiles/LLVMProfdataTests.dir/OutputSizeLimitTest.cpp.o -o unittests/tools/llvm-profdata/LLVMProfdataTests  -Wl,-rpath,/home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/build/lib  lib/libLLVMProfileData.so.16git  lib/libllvm_gtest_main.so.16git  lib/libLLVMTestingSupport.so.16git  lib/libllvm_gtest.so.16git  lib/libLLVMSupport.so.16git  -Wl,-rpath-link,/home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/build/lib && :
unittests/tools/llvm-profdata/CMakeFiles/LLVMProfdataTests.dir/OutputSizeLimitTest.cpp.o:OutputSizeLimitTest.cpp:function TestOutputSizeLimit_TestOutputSizeLimit1_Test::TestBody() [clone .localalias]: error: undefined reference to 'llvm::LLVMContext::LLVMContext()'
unittests/tools/llvm-profdata/CMakeFiles/LLVMProfdataTests.dir/OutputSizeLimitTest.cpp.o:OutputSizeLimitTest.cpp:function TestOutputSizeLimit_TestOutputSizeLimit1_Test::TestBody() [clone .localalias]: error: undefined reference to 'llvm::LLVMContext::~LLVMContext()'
collect2: error: ld returned 1 exit status

https://lab.llvm.org/buildbot/#/builders/234/builds/2655

Would you take a look?

This unit-test based test also fails on non-linux, see e.g. http://45.33.8.238/macm1/52570/step_11.txt

I think that's because writeWithSizeLimitInternal calls Strategy->Erase(StringBuffer.size()); and that erases an element from an unordered_map, and it depends on the C++ standard library if an "important" function name gets removed. Depending on that, the error is either too_large (which the test expects) or truncated_name_table (which it doesn't).

Please take a look and revert for now if it takes a while to fix. If you do end up reverting, it'd be cool if you could revert 6be251352e6b4d9708a1b7b7b146ea199342de22 in the same commit (and reland it when you reland).

llvm/unittests/tools/llvm-profdata/OutputSizeLimitTest.cpp
38

We are also seeing an assertion failure in the added unit test on our internal Windows builder similar to what this bot is hitting:

https://lab.llvm.org/buildbot/#/builders/231/builds/7174

[ RUN      ] TestOutputSizeLimit.TestOutputSizeLimit1
LLVMProfdataTests: /home/buildbots/ppc64be-clang-test-suite/clang-ppc64be-test-suite/llvm-project/llvm/lib/ProfileData/SampleProfWriter.cpp:85: virtual void llvm::sampleprof::DefaultFunctionPruningStrategy::Erase(size_t): Assertion `NumToRemove <= SortedFunctions.size()' failed.
vitalybuka reopened this revision.Jan 11 2023, 11:26 PM
This revision is now accepted and ready to land.Jan 11 2023, 11:26 PM
huangjd updated this revision to Diff 489993.Jan 17 2023, 5:11 PM

Updated unit test

huangjd updated this revision to Diff 489997.Jan 17 2023, 5:26 PM

Updated unit test

I have tested the latest changes on llvm-nvptx-nvidia-ubuntu (https://lab.llvm.org/buildbot/#/builders/234) builder locally and the unit tests get built and run successfully.
Thank you @huangjd.

huangjd updated this revision to Diff 492241.Jan 25 2023, 1:13 PM

Update unit test, previously may crash on mac OS

huangjd updated this revision to Diff 493715.Jan 31 2023, 1:07 PM

Refactored to fix bugs on non linux platforms

Moved all the tests into unit test since

  1. Output size limit doesn't require the preservation of specific functions if a different pruning strategy is used in the future, so CHECK-NEXT is too restrictive. In unit test each rewritten sample is checked against the original sample to confirm all its fields are identical
  1. Windows use '\r\n' line ending so the behavior of output size limit in text mode is different (more functions being pruned than linux for the same limit because extra char per line). This breaks lit test
  1. Write once for many tests with repetative output
huangjd updated this revision to Diff 493781.Jan 31 2023, 4:57 PM

use llvm::unittest::TempFile instead

huangjd retitled this revision from Fix to D139603(reverted) - moved size check to unit test so that it is cross-platform to [llvm-profdata] Add option to cap profile output size.Feb 5 2023, 11:13 PM

@snehasish Please re-review since I introduced a major change to the code.

snehasish accepted this revision.Feb 6 2023, 3:45 PM

lgtm

llvm/lib/ProfileData/SampleProfWriter.cpp
111

Is this leaking the raw_svector_ostream objects?

Can we rewrite this as the following?

raw_svector_ostream OS(StringBuffer);
OutputStream.reset(&OS);

Also might be worth it to use a separate OutputStream object within the loop scope so that we don't have to worry about the swap before and after and the lifetimes of the raw_vector_ostream objects.

llvm/unittests/tools/llvm-profdata/OutputSizeLimitTest.cpp
72

I think these parameters name should be capitalized based on the guide.
https://llvm.org/docs/CodingStandards.html

Also consider moving this to the implementation of FunctionSamples since it seems generally useful to have operator== implemented?

174

VAR_RETURN_IF_ERROR can be used here?

198

EXPECT_THAT_EXPECTED is probably better to continue with other test cases rather than aborting the test on the first failure?

huangjd added inline comments.Feb 6 2023, 5:07 PM
llvm/lib/ProfileData/SampleProfWriter.cpp
111

unique_ptr.reset(new obj) is the correct usage.
https://en.cppreference.com/w/cpp/memory/unique_ptr/reset

snehasish added inline comments.Feb 7 2023, 9:12 AM
llvm/lib/ProfileData/SampleProfWriter.cpp
111

Thanks, knowing the type makes it clear! Perhaps that is additional motivation for a separate, local OutputStream var?

huangjd added inline comments.Feb 7 2023, 1:42 PM
llvm/unittests/tools/llvm-profdata/OutputSizeLimitTest.cpp
198

The callee returns an error only if there's something wrong with I/O or profile reading, which is not expected to happen at all, so ASSERT is used properly here. This test checks the correctness of the sample map after size reduction, which is checked with EXPECT_EQ

huangjd updated this revision to Diff 495678.Feb 7 2023, 4:25 PM

Cleanup unit test

This revision was landed with ongoing or failed builds.Feb 7 2023, 6:19 PM
This revision was automatically updated to reflect the committed changes.
huangjd reopened this revision.Feb 7 2023, 7:01 PM
This revision is now accepted and ready to land.Feb 7 2023, 7:01 PM
huangjd updated this revision to Diff 495711.Feb 7 2023, 7:01 PM

update tests because API change from main

This revision was landed with ongoing or failed builds.Feb 8 2023, 2:22 PM
This revision was automatically updated to reflect the committed changes.