This is an archive of the discontinued LLVM Phabricator instance.

[PGO][PGSO] Instrument the code gen / target passes.
ClosedPublic

Authored by hjyamauchi on Dec 5 2019, 9:51 AM.

Details

Summary

Split off of D67120.

Add the profile guided size optimization instrumentation / queries in the code
gen or target passes. This doesn't enable the size optimizations in those passes
yet as they are currently disabled in shouldOptimizeForSize (for non-IR pass
queries).

Diff Detail

Event Timeline

hjyamauchi created this revision.Dec 5 2019, 9:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 5 2019, 9:51 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
davidxl added inline comments.Dec 5 2019, 10:13 AM
llvm/lib/CodeGen/MachineBlockPlacement.cpp
2844

This looks like a new opt-size logic -- better split out.

hjyamauchi marked an inline comment as done.Dec 6 2019, 9:51 AM
hjyamauchi added inline comments.
llvm/lib/CodeGen/MachineBlockPlacement.cpp
2844

I don't mean this to be a new logic. It is the "per-block" version of the hasOptSize() logic at the beginning of this function (at line 2756 above). It's no different from other "hasOptSize() || shouldOptimizeForSize()" pattern except that they are separate from each other due to the existing code structure (the function-level hasOptSize() check is done above outside of the loop. Moving the hasOptSize check down into the loop wouldn't seem like a good idea.)

davidxl accepted this revision.Dec 6 2019, 10:01 AM

lgtm

This revision is now accepted and ready to land.Dec 6 2019, 10:01 AM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Dec 6 2019, 12:20 PM

Looks like this makes clang crash on macOS: http://45.33.8.238/mac/3710/step_6.txt

Working on reverting it.

Locally reproduced the failure

Stack dump:
0.	Program arguments: /llvm-project/llvm/build/bin/clang -cc1 -internal-isystem /llvm-project/llvm/build/lib/clang/10.0.0/include -nostdsysteminc -O3 -triple x86_64-unknown-linux-gnu -target-cpu x86-64 -fprofile-instrument-use-path=/llvm-project/llvm/build/tools/clang/test/CodeGen/Output/opt-record.c.tmp.profdata /llvm-project/clang/test/CodeGen/opt-record.c -o /llvm-project/llvm/build/tools/clang/test/CodeGen/Output/opt-record.c.tmp -dwarf-column-info -opt-record-file /llvm-project/llvm/build/tools/clang/test/CodeGen/Output/opt-record.c.tmp.yaml -emit-obj 
1.	<eof> parser at end of file
2.	Code generation
3.	Running pass 'Function Pass Manager' on module '/llvm-project/clang/test/CodeGen/opt-record.c'.
4.	Running pass 'X86 Assembly Printer' on function '@foo'
 #0 0x00005576f844429e llvm::sys::PrintStackTrace(llvm::raw_ostream&) /llvm-project/llvm/build/../lib/Support/Unix/Signals.inc:548:22
 #1 0x00005576f8444331 PrintStackTraceSignalHandler(void*) /llvm-project/llvm/build/../lib/Support/Unix/Signals.inc:609:1
 #2 0x00005576f844242d llvm::sys::RunSignalHandlers() /llvm-project/llvm/build/../lib/Support/Signals.cpp:68:20
 #3 0x00005576f8443ce1 SignalHandler(int) /llvm-project/llvm/build/../lib/Support/Unix/Signals.inc:390:1
 #4 0x00007f86f323e3a0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x123a0)
 #5 0x00005576f75f9798 llvm::BlockFrequencyInfoImpl<llvm::MachineBasicBlock>::getFunction() const /llvm-project/llvm/build/../include/llvm/Analysis/BlockFrequencyInfoImpl.h:961:49
 #6 0x00005576f75f7a2e llvm::MachineBlockFrequencyInfo::getBlockProfileCount(llvm::MachineBasicBlock const*) const /llvm-project/llvm/build/../lib/CodeGen/MachineBlockFrequencyInfo.cpp:234:56
 #7 0x00005576f76a860f llvm::MachineOptimizationRemarkEmitter::computeHotness(llvm::MachineBasicBlock const&) /llvm-project/llvm/build/../lib/CodeGen/MachineOptimizationRemarkEmitter.cpp:40:1
 #8 0x00005576f76a8651 llvm::MachineOptimizationRemarkEmitter::computeHotness(llvm::DiagnosticInfoMIROptimization&) /llvm-project/llvm/build/../lib/CodeGen/MachineOptimizationRemarkEmitter.cpp:46:22
 #9 0x00005576f76a86a8 llvm::MachineOptimizationRemarkEmitter::emit(llvm::DiagnosticInfoOptimizationBase&) /llvm-project/llvm/build/../lib/CodeGen/MachineOptimizationRemarkEmitter.cpp:54:22
#10 0x00005576f94032d4 llvm::AsmPrinter::EmitFunctionBody() /llvm-project/llvm/build/../lib/CodeGen/AsmPrinter/AsmPrinter.cpp:1157:22
#11 0x00005576f6dfdf4e llvm::X86AsmPrinter::runOnMachineFunction(llvm::MachineFunction&) /llvm-project/llvm/build/../lib/Target/X86/X86AsmPrinter.cpp:82:16
#12 0x00005576f76688b2 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) /llvm-project/llvm/build/../lib/CodeGen/MachineFunctionPass.cpp:73:33
#13 0x00005576f7adbfe3 llvm::FPPassManager::runOnFunction(llvm::Function&) /llvm-project/llvm/build/../lib/IR/LegacyPassManager.cpp:1481:20
#14 0x00005576f7adc232 llvm::FPPassManager::runOnModule(llvm::Module&) /llvm-project/llvm/build/../lib/IR/LegacyPassManager.cpp:1517:13
#15 0x00005576f7adc61a (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /llvm-project/llvm/build/../lib/IR/LegacyPassManager.cpp:1582:20
#16 0x00005576f7adcd6d llvm::legacy::PassManagerImpl::run(llvm::Module&) /llvm-project/llvm/build/../lib/IR/LegacyPassManager.cpp:1694:13
#17 0x00005576f7adcf65 llvm::legacy::PassManager::run(llvm::Module&) /llvm-project/llvm/build/../lib/IR/LegacyPassManager.cpp:1726:1
#18 0x00005576f8cbcbf4 (anonymous namespace)::EmitAssemblyHelper::EmitAssembly(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) /llvm-project/clang/lib/CodeGen/BackendUtil.cpp:916:66
#19 0x00005576f8cc0f46 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::DataLayout const&, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) /llvm-project/clang/lib/CodeGen/BackendUtil.cpp:1546:27
#20 0x00005576f90efd08 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) /llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:312:24
#21 0x00005576faca452c clang::ParseAST(clang::Sema&, bool, bool) /llvm-project/clang/lib/Parse/ParseAST.cpp:178:14
#22 0x00005576f8f8697f clang::ASTFrontendAction::ExecuteAction() /llvm-project/clang/lib/Frontend/FrontendAction.cpp:1043:11
#23 0x00005576f90ed600 clang::CodeGenAction::ExecuteAction() /llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:1114:1
#24 0x00005576f8f862e7 clang::FrontendAction::Execute() /llvm-project/clang/lib/Frontend/FrontendAction.cpp:940:38
#25 0x00005576f8ec850b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /llvm-project/clang/lib/Frontend/CompilerInstance.cpp:964:42
#26 0x00005576f90da2c6 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /llvm-project/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:290:38
#27 0x00005576f5be87a3 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) /llvm-project/clang/tools/driver/cc1_main.cpp:250:40
#28 0x00005576f5bde669 ExecuteCC1Tool(llvm::ArrayRef<char const*>, llvm::StringRef) /llvm-project/clang/tools/driver/driver.cpp:309:64
#29 0x00005576f5bded22 main /llvm-project/clang/tools/driver/driver.cpp:382:26
#30 0x00007f86f24e652b __libc_start_main /build/glibc-XAwaOT/glibc-2.28/csu/../csu/libc-start.c:342:3
#31 0x00005576f5bdd02a _start (/llvm-project/llvm/build/bin/clang+0x8d3202a)
/llvm-project/llvm/build/tools/clang/test/CodeGen/Output/opt-record.c.script: line 11: 123281 Segmentation fault      /llvm-project/llvm/build/bin/clang -cc1 -internal-isystem /llvm-project/llvm/build/lib/clang/10.0.0/include -nostdsysteminc -O3 -triple x86_64-unknown-linux-gnu -target-cpu x86-64 -fprofile-instrument-use-path=/llvm-project/llvm/build/tools/clang/test/CodeGen/Output/opt-record.c.tmp.profdata /llvm-project/clang/test/CodeGen/opt-record.c -o /llvm-project/llvm/build/tools/clang/test/CodeGen/Output/opt-record.c.tmp -dwarf-column-info -opt-record-file /llvm-project/llvm/build/tools/clang/test/CodeGen/Output/opt-record.c.tmp.yaml -emit-obj

--

********************

Testing Time: 101.28s
********************
Failing Tests (1):
    Clang :: CodeGen/opt-record.c

  Expected Passes    : 16377
  Expected Failures  : 21
  Unsupported Tests  : 57
  Unexpected Failures: 1
FAILED: tools/clang/test/CMakeFiles/check-clang

The issue was that in AsmPrinter ORE also depends on LazyMachineBlockFrequencyInfo and *conditionally* computes MBFI. Trying to compute it twice would delete the first one and causes an access-after-free error in ORE. I think a fix would be to compute MBFI once and share it (use one from ORE if it is available and compute one otherwise.)

A fixed version D71072.