This is an archive of the discontinued LLVM Phabricator instance.

[InstrProf] Single byte counters in coverage
Needs ReviewPublic

Authored by gulfem on May 27 2022, 8:22 PM.

Details

Summary

This patch inserts 1-byte counters instead of an 8-byte counters
into llvm profiles for source-based code coverage. The origial idea
was proposed as block-cov for PGO, and this patch repurposes that
idea for coverage: https://groups.google.com/g/llvm-dev/c/r03Z6JoN7d4

The current 8-byte counters mechanism add counters to minimal regions,
and infer the counters in the remaining regions via adding or
subtracting counters. For example, it infers the counter in the if.else
region by subtracting the counters between if.entry and if.then regions
in an if statement. Whenever there is a control-flow merge, it adds
the counters from all the incoming regions. However, we are not going to be
able to infer counters by subtracting two execution counts when using
single-byte counters. Therefore, this patch conservatively inserts additional
counters for the cases where we need to add or subtract counters.

Diff Detail

Event Timeline

gulfem created this revision.May 27 2022, 8:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 27 2022, 8:22 PM
gulfem requested review of this revision.May 27 2022, 8:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 27 2022, 8:22 PM
gulfem edited the summary of this revision. (Show Details)May 27 2022, 8:23 PM

I think somewhere in this patch there should be a description of the new algorithm. I also don't see any description of the old algorithm, so maybe we should describe both of them so that it's easy to understand the differences in between the different profiling approaches. For example llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp describes the MST approach in detail. I don't think it needs to be quite that detailed, but a diagram contrasting where counters are inserted in both approaches would help explain the differences.

Can you also describe how this works w/ the SwitchStmt? It appears to me to just exit early, but the summary seems to indicate that it is part of the current patch. Is that part still WIP? I expected to see similar handling there as we see w/ IfStmt.

clang/lib/CodeGen/CodeGenPGO.cpp
245–256
clang/lib/CodeGen/CoverageMappingGen.cpp
1445

Won't ElseCount be uninitialized when S->getElse() == false? originally it was always initialized by subtractCounters.

llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
96–98

Do these changes need to be explained in the comment above?

152

nit: added whitespace

gulfem updated this revision to Diff 434224.Jun 3 2022, 6:05 PM

Describe the high-level changes to adding and subtracting counters.

Herald added a project: Restricted Project. · View Herald TranscriptJun 3 2022, 6:05 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gulfem edited the summary of this revision. (Show Details)Jun 3 2022, 6:06 PM
gulfem added a reviewer: mcgrathr.
gulfem added a comment.EditedJul 11 2022, 4:41 AM

@davidxl and @vsk,
This is a WIP prototype implementation, and I would love to hear your early feedback on using 1-byte counters for coverage idea if possible. I'll continue extending the prototype, but that would be great to learn if there is anything fundamentally wrong or missing.

For coverage mode, why using 'incrementProfileCounter'? Should it be set to '1' instead? Also is the 'or' expression needed? The expression can be folded to either zero or 1.

gulfem added a comment.EditedJul 13 2022, 9:29 AM

For coverage mode, why using 'incrementProfileCounter'? Should it be set to '1' instead? Also is the 'or' expression needed? The expression can be folded to either zero or 1.

@davidxl I used incrementProfileCounter(), but it emits llvm.instrprof.cover intrinsic instead of llvm.instrprof.increment in emitCounterIncrement(). If it's confusing, I can move the boolean counter handling into another function and name it like setProfileCounter, setProfileCovered or something like that.

I have one concern that I would like to hear your recommendation: incrementProfileCounter() is embedded into Clang CodeGen for various AST nodes. So, even for implementing a simple prototype to collect some numbers will require me doing invasive changes to Clang CodeGen. These are going to be small changes to several AST nodes. I have only done it for some frequently used AST nodes, but there are many more of them. Can you think of a better way of designing that? Based on the current implementation of instrumentation, I could not find a less invasive way of implementing boolean counters. If that's the only way to go, shall we land them in stages after the reviews?

gulfem updated this revision to Diff 550163.Aug 14 2023, 6:40 PM
  • Extend the prototype by implementing the rest of the control-flow statements
  • Rename the prototype to single byte counters to be consistent with the single byte coverage used in PGO
gulfem retitled this revision from [InstrProf][WIP] Implement boolean counters in coverage to [InstrProf] Single byte counters in coverage.Aug 14 2023, 6:41 PM
gulfem edited the summary of this revision. (Show Details)
gulfem edited the summary of this revision. (Show Details)
ayzhao added a subscriber: ayzhao.Aug 17 2023, 2:59 PM
gulfem edited the summary of this revision. (Show Details)Aug 18 2023, 4:52 PM
gulfem edited the summary of this revision. (Show Details)
gulfem edited the summary of this revision. (Show Details)
gulfem updated this revision to Diff 551686.EditedAug 18 2023, 5:49 PM

Ensure the correct traversal order for loops in CodeGenPGO to fix the profile test failures (Profile/cxx-hash-v2.cpp and Profile/cxx-rangefor.cpp)

gulfem updated this revision to Diff 551687.Aug 18 2023, 6:05 PM

Remove hyphen between "single counter" for consistency

Thanks for your work on this!

FYI, I'm getting the following crash when I try to build Chrome's base_unittests with -mllvm -enable-single-byte-coverage=true:

[195/4447] CXX obj/base/allocator/partition_allocator/allocator_core/pcscan_internal.o
FAILED: obj/base/allocator/partition_allocator/allocator_core/pcscan_internal.o
"python3" ../../build/toolchain/clang_code_coverage_wrapper.py --target-os=linux  /usr/local/google/home/ayzhao/src/llvm-project/build-debug/bin/clang++ -MMD -MF obj/base/allocator/partition_allocator/allocator_core/pcscan_internal.o.d -DUSE_UDEV -DUSE_AURA=1 -DUSE_GLIB=1 -DUSE_OZONE=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -D_LIBCPP_ENABLE_ASSERTIONS=1 -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -DCR_LIBCXX_REVISION=84fb809dd6dae36d556dc0bb702c6cc2ce9d4b80 -DCR_SYSROOT_KEY=20230611T210420Z-2 -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DIS_PARTITION_ALLOC_IMPL -I../.. -Igen -I../../buildtools/third_party/libc++ -Wall -Werror -Wextra -Wimplicit-fallthrough -Wextra-semi -Wunreachable-code-aggressive -Wthread-safety -Wno-missing-field-initializers -Wno-unused-parameter -Wno-psabi -Wloop-analysis -Wno-unneeded-internal-declaration -Wenum-compare-conditional -Wno-ignored-pragma-optimize -Wno-deprecated-builtins -Wno-bitfield-constant-conversion -Wno-deprecated-this-capture -Wshadow -fno-delete-null-pointer-checks -fno-ident -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -funwind-tables -fPIC -pthread -fcolor-diagnostics -fmerge-all-constants -fcrash-diagnostics-dir=../../tools/clang/crashreports -mllvm -instcombine-lower-dbg-declare=0 -ffp-contract=off -fcomplete-member-pointers -m64 -msse3 -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -ffile-compilation-dir=. -no-canonical-prefixes -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -g0 -fprofile-instr-generate -fcoverage-mapping -mllvm -enable-single-byte-coverage=true -mllvm -limited-coverage-experimental=true -fno-use-cxa-atexit -fvisibility=hidden -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wexit-time-destructors -O3 -fdata-sections -ffunction-sections -fno-unique-section-names -std=c++20 -Wno-trigraphs -gsimple-template-names -fno-exceptions -fno-rtti -nostdinc++ -isystem../../third_party/libc++/src/include -isystem../../third_party/libc++abi/src/include --sysroot=../../build/linux/debian_bullseye_amd64-sysroot -fvisibility-inlines-hidden -c ../../base/allocator/partition_allocator/starscan/pcscan_internal.cc -o obj/base/allocator/partition_allocator/allocator_core/pcscan_internal.o
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/local/google/home/ayzhao/src/llvm-project/build-debug/bin/clang++ -MMD -MF obj/base/allocator/partition_allocator/allocator_core/pcscan_internal.o.d -DUSE_UDEV -DUSE_AURA=1 -DUSE_GLIB=1 -DUSE_OZONE=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -D_LIBCPP_ENABLE_ASSERTIONS=1 -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -DCR_LIBCXX_REVISION=84fb809dd6dae36d556dc0bb702c6cc2ce9d4b80 -DCR_SYSROOT_KEY=20230611T210420Z-2 -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DIS_PARTITION_ALLOC_IMPL -I../.. -Igen -I../../buildtools/third_party/libc++ -Wall -Werror -Wextra -Wimplicit-fallthrough -Wextra-semi -Wunreachable-code-aggressive -Wthread-safety -Wno-missing-field-initializers -Wno-unused-parameter -Wno-psabi -Wloop-analysis -Wno-unneeded-internal-declaration -Wenum-compare-conditional -Wno-ignored-pragma-optimize -Wno-deprecated-builtins -Wno-bitfield-constant-conversion -Wno-deprecated-this-capture -Wshadow -fno-delete-null-pointer-checks -fno-ident -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -funwind-tables -fPIC -pthread -fcolor-diagnostics -fmerge-all-constants -fcrash-diagnostics-dir=../../tools/clang/crashreports -mllvm -instcombine-lower-dbg-declare=0 -ffp-contract=off -fcomplete-member-pointers -m64 -msse3 -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -ffile-compilation-dir=. -no-canonical-prefixes -ftrivial-auto-var-init=pattern -fno-omit-frame-pointer -g0 -fprofile-instr-generate -fcoverage-mapping -mllvm -enable-single-byte-coverage=true -mllvm -limited-coverage-experimental=true -fno-use-cxa-atexit -fvisibility=hidden -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wexit-time-destructors -O3 -fdata-sections -ffunction-sections -fno-unique-section-names -std=c++20 -Wno-trigraphs -gsimple-template-names -fno-exceptions -fno-rtti -nostdinc++ -isystem../../third_party/libc++/src/include -isystem../../third_party/libc++abi/src/include --sysroot=../../build/linux/debian_bullseye_amd64-sysroot -fvisibility-inlines-hidden -c ../../base/allocator/partition_allocator/starscan/pcscan_internal.cc -o obj/base/allocator/partition_allocator/allocator_core/pcscan_internal.o
1.      <eof> parser at end of file
2.      Optimizer
 #0 0x000055a9e376956d llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:602:11
 #1 0x000055a9e3769a0b PrintStackTraceSignalHandler(void*) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:675:1
 #2 0x000055a9e3767c86 llvm::sys::RunSignalHandlers() /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/Signals.cpp:104:5
 #3 0x000055a9e3768dde llvm::sys::CleanupOnSignal(unsigned long) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:368:1
 #4 0x000055a9e36a2f04 (anonymous namespace)::CrashRecoveryContextImpl::HandleCrash(int, unsigned long) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:0:7
 #5 0x000055a9e36a32c2 CrashRecoverySignalHandler(int) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:391:1
 #6 0x00007f2407a5a540 (/lib/x86_64-linux-gnu/libc.so.6+0x3c540)
 #7 0x000055a9e120d134 llvm::detail::PunnedPointer<llvm::ilist_node_base<true>*>::asInt() const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/PointerIntPair.h:41:5
 #8 0x000055a9e120d115 llvm::detail::PunnedPointer<llvm::ilist_node_base<true>*>::operator long() const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/PointerIntPair.h:45:41
 #9 0x000055a9e120e655 llvm::PointerIntPair<llvm::ilist_node_base<true>*, 1u, unsigned int, llvm::PointerLikeTypeTraits<llvm::ilist_node_base<true>*>, llvm::PointerIntPairInfo<llvm::ilist_node_base<true>*, 1u, llvm::PointerLikeTypeTraits<llvm::ilist_node_base<true>*>>>::getInt() const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/PointerIntPair.h:96:57
#10 0x000055a9e120e625 llvm::ilist_node_base<true>::isSentinel() const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/ilist_node_base.h:45:36
#11 0x000055a9e120e5e5 llvm::ilist_node_base<true>::isKnownSentinel() const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/ilist_node_base.h:46:34
#12 0x000055a9e12c5b8c llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, true, true>::operator*() const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/ilist_iterator.h:138:5
#13 0x000055a9e12c5a92 llvm::simple_ilist<llvm::Instruction>::back() const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/simple_ilist.h:139:34
#14 0x000055a9e12c5a12 llvm::BasicBlock::getTerminator() const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/IR/BasicBlock.h:128:39
#15 0x000055a9e12c1ef5 llvm::BasicBlock::getTerminator() /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/IR/BasicBlock.h:133:5
#16 0x000055a9e56a9e55 llvm::SparseSolver<llvm::PointerIntPair<llvm::Value*, 2u, (anonymous namespace)::IPOGrouping, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 2u, llvm::PointerLikeTypeTraits<llvm::Value*>>>, (anonymous namespace)::CVPLatticeVal, llvm::LatticeKeyInfo<llvm::PointerIntPair<llvm::Value*, 2u, (anonymous namespace)::IPOGrouping, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 2u, llvm::PointerLikeTypeTraits<llvm::Value*>>>>>::isEdgeFeasible(llvm::BasicBlock*, llvm::BasicBlock*, bool) /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/Analysis/SparsePropagation.h:374:27
#17 0x000055a9e56a96a4 llvm::SparseSolver<llvm::PointerIntPair<llvm::Value*, 2u, (anonymous namespace)::IPOGrouping, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 2u, llvm::PointerLikeTypeTraits<llvm::Value*>>>, (anonymous namespace)::CVPLatticeVal, llvm::LatticeKeyInfo<llvm::PointerIntPair<llvm::Value*, 2u, (anonymous namespace)::IPOGrouping, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 2u, llvm::PointerLikeTypeTraits<llvm::Value*>>>>>::visitPHINode(llvm::PHINode&) /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/Analysis/SparsePropagation.h:433:9
#18 0x000055a9e56a91e6 llvm::SparseSolver<llvm::PointerIntPair<llvm::Value*, 2u, (anonymous namespace)::IPOGrouping, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 2u, llvm::PointerLikeTypeTraits<llvm::Value*>>>, (anonymous namespace)::CVPLatticeVal, llvm::LatticeKeyInfo<llvm::PointerIntPair<llvm::Value*, 2u, (anonymous namespace)::IPOGrouping, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 2u, llvm::PointerLikeTypeTraits<llvm::Value*>>>>>::visitInst(llvm::Instruction&) /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/Analysis/SparsePropagation.h:455:5
#19 0x000055a9e56a59fe llvm::SparseSolver<llvm::PointerIntPair<llvm::Value*, 2u, (anonymous namespace)::IPOGrouping, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 2u, llvm::PointerLikeTypeTraits<llvm::Value*>>>, (anonymous namespace)::CVPLatticeVal, llvm::LatticeKeyInfo<llvm::PointerIntPair<llvm::Value*, 2u, (anonymous namespace)::IPOGrouping, llvm::PointerLikeTypeTraits<llvm::Value*>, llvm::PointerIntPairInfo<llvm::Value*, 2u, llvm::PointerLikeTypeTraits<llvm::Value*>>>>>::Solve() /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/Analysis/SparsePropagation.h:495:27
#20 0x000055a9e56a5367 runCVP(llvm::Module&) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp:386:8
#21 0x000055a9e56a525c llvm::CalledValuePropagationPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp:403:3
#22 0x000055a9e5474cc7 llvm::detail::PassModel<llvm::Module, llvm::CalledValuePropagationPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/IR/PassManagerInternal.h:89:17
#23 0x000055a9e302d9aa llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/IR/PassManager.h:521:7
#24 0x000055a9e3b0a68c (anonymous namespace)::EmitAssemblyHelper::RunOptimizationPipeline(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>&, std::unique_ptr<llvm::ToolOutputFile, std::default_delete<llvm::ToolOutputFile>>&) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/BackendUtil.cpp:1100:5
#25 0x000055a9e3b04545 (anonymous namespace)::EmitAssemblyHelper::EmitAssembly(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/BackendUtil.cpp:1157:3
#26 0x000055a9e3b03a17 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/BackendUtil.cpp:1320:3
#27 0x000055a9e52f0154 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:386:7
#28 0x000055a9e79475b3 clang::ParseAST(clang::Sema&, bool, bool) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Parse/ParseAST.cpp:183:12
#29 0x000055a9e47d1e2c clang::ASTFrontendAction::ExecuteAction() /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1170:1
#30 0x000055a9e52eb256 clang::CodeGenAction::ExecuteAction() /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:1208:5
#31 0x000055a9e47d182c clang::FrontendAction::Execute() /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1062:7
#32 0x000055a9e46fbc68 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1053:23
#33 0x000055a9e499cd47 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:272:8
#34 0x000055a9e12092d3 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) /usr/local/google/home/ayzhao/src/llvm-project/clang/tools/driver/cc1_main.cpp:249:13
#35 0x000055a9e11f8f8a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) /usr/local/google/home/ayzhao/src/llvm-project/clang/tools/driver/driver.cpp:366:5
#36 0x000055a9e11fa8fd clang_main(int, char**, llvm::ToolContext const&)::$_0::operator()(llvm::SmallVectorImpl<char const*>&) const /usr/local/google/home/ayzhao/src/llvm-project/clang/tools/driver/driver.cpp:506:7
#37 0x000055a9e11fa8cd int llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::callback_fn<clang_main(int, char**, llvm::ToolContext const&)::$_0>(long, llvm::SmallVectorImpl<char const*>&) /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:45:5
#38 0x000055a9e459a6a9 llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::operator()(llvm::SmallVectorImpl<char const*>&) const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:68:5
#39 0x000055a9e4596e08 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::$_1::operator()() const /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Driver/Job.cpp:440:34
#40 0x000055a9e4596dd5 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::$_1>(long) /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:45:5
#41 0x000055a9e2468ca9 llvm::function_ref<void ()>::operator()() const /usr/local/google/home/ayzhao/src/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:68:5
#42 0x000055a9e36a2d1a llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:427:3
#43 0x000055a9e4596727 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Driver/Job.cpp:440:7
#44 0x000055a9e4530abf clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Driver/Compilation.cpp:199:15
#45 0x000055a9e4530cc7 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Driver/Compilation.cpp:253:13
#46 0x000055a9e454b9d8 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Driver/Driver.cpp:1906:7
#47 0x000055a9e11f8a48 clang_main(int, char**, llvm::ToolContext const&) /usr/local/google/home/ayzhao/src/llvm-project/clang/tools/driver/driver.cpp:542:9
#48 0x000055a9e122d74d main /usr/local/google/home/ayzhao/src/llvm-project/build-debug/tools/clang/tools/driver/clang-driver.cpp:15:3
#49 0x00007f2407a456ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#50 0x00007f2407a45785 call_init ./csu/../csu/libc-start.c:128:20
#51 0x00007f2407a45785 __libc_start_main ./csu/../csu/libc-start.c:347:5
#52 0x000055a9e11f76e1 _start (/usr/local/google/home/ayzhao/src/llvm-project/build-debug/bin/clang+++0x1b156e1)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
clang version 18.0.0 (git@github.com:llvm/llvm-project.git f5980dad855c06e6110d954f8f33d0a385b77756)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/google/home/ayzhao/src/llvm-project/build-debug/bin
clang++: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: ../../tools/clang/crashreports/pcscan_internal-09d280.cpp
clang++: note: diagnostic msg: ../../tools/clang/crashreports/pcscan_internal-09d280.sh
clang++: note: diagnostic msg:

********************
ninja: build stopped: subcommand failed.{F28818444}

{F28818443}
gulfem updated this revision to Diff 553992.EditedAug 28 2023, 10:51 AM

Fixed the issue that causes a crash in InstCombinePHI.cpp. Phi instructions need to be inserted at the beginning of basic blocks, and profile increments need to be inserted after phis for conditional operators.

zequanwu added inline comments.Aug 28 2023, 11:34 AM
clang/lib/CodeGen/CGExprScalar.cpp
4812 ↗(On Diff #553992)

This can be hoist outside if (llvm::EnableSingleByteCoverage)

clang/lib/CodeGen/CodeGenPGO.cpp
1047–1048

I thinkit's better to emit the profile version global variable in CoverageMappingModuleGen::emit so this check CGM.getCodeGenOpts().hasProfileClangInstr() is not necessary.

Also we should always emit the profile version global variable so that the runtime/backend knows if single byte coverage is enabled or not by looking at the version.

Fixed the issue that causes a crash in InstCombinePHI.cpp. Phi instructions need to be inserted at the beginning of basic blocks, and profile increments need to be inserted after phis for conditional operators.

I can confirm that this fixes the crash reported in https://reviews.llvm.org/D126586#4615344. Thanks!

gulfem updated this revision to Diff 555542.Sep 1 2023, 6:35 PM
  1. Use || to merge counters that refer to the same regions
  2. Use || to merge counters from raw profiles
gulfem marked an inline comment as done.Sep 1 2023, 6:43 PM
gulfem added inline comments.
clang/lib/CodeGen/CodeGenPGO.cpp
1047–1048

CoverageMappingModuleGen is only invoked when -fcoverage-mapping flag is provided, and single byte counters are emitted when -fprofile-instr-generate flag is provided. We provide both flags for coverage purposes, but if we move this global to CoverageMappingModuleGen::emit, single byte counters won't work if -fcoverage-mapping is not provided. I don't know whether there will be such use cases, but for this reason it might not be a good idea to put that into CoverageMappingModuleGen.