Use cases • Static analysis • Code instrumentation • https: //github. com/LLNL/caliper-compiler • Extract control flow graph • How are STL containers used (push_back(), fixed-size/dynamic etc. )
Compiler plugins • Clang plugins vs. llvm passes • • For source code / IR: clang plugins For LLVM assembly: llvm passes Both can do loop analysis Pass info from clang to llvm through artifacts • Other compilers • • gcc: Undocumented plugin interface Intel / XL: good luck Nvidia: run away Can use source-to-source compilation from clang • Vs. ROSE etc. • ROSE has easier source-code insertion • Clang has bigger user base
How to write a clang plugin • Find source locations for patterns of interest with clang-query • E. g. match for. Stmt(has. Descendant(decl. Ref. Expr())) • Respects defs and includes in compile_commands. json • Can be created with Cmake, bare make • Use arcane compiler knowledge • Implement AST visitors • Visits AST nodes (ifs, fors, function decls, . . . ) • Overwrite corresponding visitor method and do stuff • Build plugin (use recipe, e. g. from Eli’s website (see Resources)) • Use plugin: • clang++ -fplugin=myplugin. so source. cpp • Can also build executables with plugin functionality