<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">You should look into the existing ThinLTO infrastructure, it does what you describe here, but at the IR level directly. This should be enough to get you going <a href="https://clang.llvm.org/docs/ThinLTO.html" class="">https://clang.llvm.org/docs/ThinLTO.html</a> <a href="http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html" class="">http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html</a> for a high level description of how this works.<div class=""><br class=""></div><div class="">Best,<br class=""><div class="">
<div>—Daniel</div>
</div>
<div><br class=""><blockquote type="cite" class=""><div class="">On 11 Jun 2020, at 13:23, Probir Roy via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">I will perform link-time optimization. At the front-end, I want to<br class="">generate a module summary in the IR guided by the user pragmas. At<br class="">link-time, I will perform a summary based whole program analysis.<br class=""><br class="">For this should I consider approach (2): IR-gen in the front-end? If<br class="">that's the case, I will need to modify Clang to insert a new AST node<br class="">class, right? Where should I write the pass to generate the module<br class="">summary? Also, how to get the associated CapturedStmt? Code examples<br class="">would be very helpful.<br class=""><br class="">-Probir<br class=""><br class=""><br class="">On Wed, Jun 10, 2020 at 4:31 PM Michael Kruse <<a href="mailto:llvm@meinersbur.de" class="">llvm@meinersbur.de</a>> wrote:<br class=""><blockquote type="cite" class=""><br class="">Hi,<br class=""><br class="">I am working on user-directed optimizations/transformations as well. A<br class="">lot depends on where you want to apply the optimization: On the (1)<br class="">AST-level in the frontend, during (2) IR-gen in the front-end or on<br class="">the (3) IR-level in the mid/back-end, (4) as a source-to-source<br class="">transformation.<br class=""><br class="">For (1), there a TreeTransform to transform an AST subtree into<br class="">another one. This an approach I took for<br class=""><a href="https://reviews.llvm.org/D76342" class="">https://reviews.llvm.org/D76342</a><br class=""><br class="">The approach (2) is used by clang's OpenMP implementation.<br class=""><br class="">For (3), one emits metadata into the LLVM-IR.<br class="">https://reviews.llvm.org/D70572 uses this approach. Instead of<br class="">introducing a new AST node kind, one can also use the AttributedStmt<br class="">kind and introduce an attribute on statements. This is the approach by<br class="">https://github.com/SOLLVE/llvm-project/tree/pragma-clang-loop and<br class="">generally used to implement the current `#pragma clang loop`<br class="">annotations.<br class=""><br class="">With (4), one would use clang to only parse the source into an AST and<br class="">use a Rewriter class to emit a new source code.<br class=""><br class=""><br class="">Since there is no interface for clang library users to insert custom<br class="">AST nodes or attributes, this leaves two choices:<br class=""><br class="">A source-to-source transformation (4):<br class=""> * Ask clang to parse some source code and return the AST<br class=""> * Register a pragma handler to callback when your custom pragma is seen.<br class=""> * In the pragma handler insert the annotate attribute (which just<br class="">takes a string)<br class=""> * When encountering the annotation during the AST walk, your tool can react.<br class=""><br class="">Or process the annotation attribute which is emitted as a<br class="">`llvm.*.annotation` builtin in the IR (1):<br class=""> * Register the pragma handler as before<br class=""> * Register a pass plugin in LLVM which looks for the annotation builtins.<br class=""><br class=""><br class="">Michael<br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class="">Am Mi., 10. Juni 2020 um 14:45 Uhr schrieb Probir Roy via cfe-dev<br class=""><cfe-dev@lists.llvm.org>:<br class=""><blockquote type="cite" class=""><br class="">Hi cfe-dev,<br class=""><br class="">I am implementing a user-guided optimization (xTool).<br class="">Programmers will provide pragma directives to guide the backend. Since<br class="">I am new to LLVM and clang, I am having a hard time figuring out how<br class="">to implement it. Here are the steps I am considering for the frontend:<br class=""><br class="">User will provide directives as:<br class=""><br class="">#pragma xTool someClause<br class="">{<br class="">     Compound statements<br class="">}<br class=""><br class="">I want to implement directives handling as a "clang plug-in". I am<br class="">thinking to create AST node classes for xTool directive inheriting<br class="">Stmt class from the plug-in. Is it possible with current clang? If it<br class="">is possible I can access the compound statements enclosed in {} with<br class="">"CapturedStmt". Or should I consider to "annotate" the stmt node<br class="">instead of creating a new class?<br class=""><br class="">Any feedback on how to implement this on the clang frontend would be<br class="">really helpful. So far I have looked into few codes of the OpenMP<br class="">directive implementation.<br class=""><br class="">--<br class="">Probir<br class="">_______________________________________________<br class="">cfe-dev mailing list<br class="">cfe-dev@lists.llvm.org<br class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev<br class=""></blockquote></blockquote>_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a><br class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev<br class=""></div></div></blockquote></div><br class=""></div></body></html>