This is an archive of the discontinued LLVM Phabricator instance.

AddGlobalAnnotations for function with or without function body.
ClosedPublic

Authored by python3kgae on Oct 4 2021, 7:13 PM.

Details

Summary

When AnnotateAttr is on a function, AddGlobalAnnotations is only called in CodeGenModule::EmitGlobalFunctionDefinition which means AnnotateAttr on function declaration without function body will be ignored.
The patch will move AddGlobalAnnotations to CodeGenModule::SetFunctionAttributes, so with or without function body, the AnnotateAttr will get code gen for a function.

It'll help case when AnnotateAttr is on external function, and the AnnotateAttr will be consumed in IR level.

For example, a pass to collect num of uses for functions with attribute((annotate("count_use"))) after optimizations,
As long as there's
attribute((annotate("count_use"))), function with or without function body should be counted.

Diff Detail

Event Timeline

python3kgae requested review of this revision.Oct 4 2021, 7:13 PM
python3kgae created this revision.
beanz added a reviewer: beanz.Oct 5 2021, 9:38 AM

I think this is reasonable, but can you update the summary to also explain why these changes are useful?

RKSimon resigned from this revision.Oct 6 2021, 5:51 AM
python3kgae edited the summary of this revision. (Show Details)Oct 7 2021, 3:03 PM

Updated summary.

Tried to debug the linux fail with Hyper-V VM, cannot repro.
Restarted Buildkite build to see if it still happens.

aaron.ballman accepted this revision.Oct 11 2021, 9:58 AM

LGTM, thanks!

This revision is now accepted and ready to land.Oct 11 2021, 9:58 AM

Thanks for the review, the PR is committed.

alexfh added a subscriber: alexfh.Oct 19 2021, 7:21 AM

It looks like this patch causes clang to crash on valid code with the following stack trace:

 #0 0x0000560442f12f45 SignalHandler(int)
 #1 0x00007ffac7c789a0 __restore_rt
 #2 0x0000560440083992 llvm::LazyCallGraph::Node::populateSlow()
 #3 0x0000560440082a60 llvm::LazyCallGraph::buildRefSCCs()
 #4 0x000056043fef166d llvm::ModuleToPostOrderCGSCCPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) 
 #5 0x000056043f8852f2 llvm::detail::PassModel<llvm::Module, llvm::ModuleToPostOrderCGSCCPassAdaptor, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) 
 #6 0x000056043f617204 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&)
 #7 0x000056043f616e34 llvm::ModuleInlinerWrapperPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) 
 #8 0x000056043f616cb2 llvm::detail::PassModel<llvm::Module, llvm::ModuleInlinerWrapperPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) 
 #9 0x000056043f617204 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&)
#10 0x0000560440100459 (anonymous namespace)::EmitAssemblyHelper::RunOptimizationPipeline(clang::BackendAction, std::__u::unique_ptr<llvm::raw_pwrite_stream, std::__u::default_delete<llvm::raw_pwrite_stream> >&, std::__u::unique_ptr<llvm::ToolOutputFile, std::__u::default_delete<llvm::ToolOutputFile> >&)
#11 0x000056043f63dff7 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, std::__u::unique_ptr<llvm::raw_pwrite_stream, std::__u::default_delete<llvm::raw_pwrite_stream> >)
#12 0x00005604400f6703 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) 
#13 0x000056043f638300 clang::ParseAST(clang::Sema&, bool, bool)
#14 0x000056043f6f96d5 clang::FrontendAction::Execute()
#15 0x000056043f6f9515 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) 
#16 0x000056043f6f9381 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) 
#17 0x00005604400b84f8 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) 
#18 0x000056043f7a7a79 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&)
#19 0x000056043ff4b6f4 main
#20 0x00007ffac7ae6bbd __libc_start_main
#21 0x000056043f815679 _start

Working on a reduced test.

Reduced test case:

template <class a> void b(a);
template <typename c> class d {
public:
  class e {
  public:
    c *f();
  };
  e *g();
};
template <class> class j;
template <class h, class... i> class j<h(i...)> {
public:
  class k {
  public:
    k(int *);
    ~k();
  };
  int n();
  d<int *> l;
};
template <class h, class... i> int j<h(i...)>::n() {
  auto m = l.g()->f();
  k a(*m);
  b(a);
}
template <class h, class... i>
j<h(i...)>::k::~k() __attribute__((annotate(""))) {}
class o {
  int m_fn4();
  j<int()> p;
};
int o::m_fn4() { p.n(); }

Please revert the commit if there is no quick and obvious solution. Thanks!

Reduced the test further to:

struct k {
  ~k() __attribute__((annotate(""))) {}
};
void m() { k(); }