<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">For better readability we typically create remarks and call OptimizationRemarkEmitter::emit unconditionally.  E.g.:<div class=""><div class=""><br class=""></div><div class=""><div class="">Transforms/IPO/Inliner.cpp:    ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "TooCostly", Call)</div><div class="">Transforms/IPO/Inliner.cpp-             << NV("Callee", Callee) << " not inlined into "</div><div class="">Transforms/IPO/Inliner.cpp-             << NV("Caller", Caller) << " because too costly to inline (cost="</div><div class="">Transforms/IPO/Inliner.cpp-             << NV("Cost", IC.getCost())</div><div class="">Transforms/IPO/Inliner.cpp-             << ", threshold=" << NV("Threshold", IC.getThreshold()) << ")");</div></div><div class=""><br class=""></div><div class="">Then inside ORE we return right away if the remarks for the given pass is not enabled.<div class=""><br class=""></div><div class="">This is nice and concise however there is still some overhead involved in this if remarks are not enabled:</div><div class=""><br class=""></div><div class="">1. Constructing the remark object</div><div class="">2. Computing and inserting the strings, named-value arguments</div></div><div class="">3. Performing the comparison between the pass name and the passes enabled by the user</div><div class="">4. Virtual destructor</div><div class=""><br class=""></div><div class="">Now that Vivek added support for asking LLVMContext for what remarks are enabled by the user [1] we can improve this.  The idea is to allow ORE::emit to take a closure.  This delays all this work until we know remarks are enabled.  E.g. the above code with closure:</div><div class=""><br class=""></div><div class=""><div class="">    ORE.emit(<b class="">[&]() {</b></div><div class="">      <b class="">return</b> OptimizationRemarkMissed(DEBUG_TYPE, "TooCostly", Call)</div><div class="">             << NV("Callee", Callee) << " not inlined into "</div><div class="">             << NV("Caller", Caller) << " because too costly to inline (cost="</div><div class="">             << NV("Cost", IC.getCost())</div><div class="">             << ", threshold=" << NV("Threshold", IC.getThreshold()) << ")"<b class="">;</b></div><div class=""><b class="">    }</b>);</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">I have a proof-of-concept implementation at <a href="https://reviews.llvm.org/D37921" class="">https://reviews.llvm.org/D37921</a>.</div><div class=""> </div><div class="">The main change is that since in the lambda the remark is now returned by value, we need to preserve its type in the insertion operator.  This requires making the insertion operator generic.  I am also curious if people see C++ portability problems with the code.</div><div class=""><br class=""></div><div class="">Feedback welcome.</div><div class=""><br class=""></div><div class="">Adam</div><div class=""><br class=""></div><div class="">[1] <a href="https://reviews.llvm.org/D33514" class="">https://reviews.llvm.org/D33514</a></div></div><div class=""><br class=""></div></body></html>