<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
</head>
<body>
<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">On 6 Feb 2020, at 9:50, Hal Finkel via llvm-dev wrote:</p>

</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">On 2/6/20 4:06 AM, Nicolai Hähnle via llvm-dev wrote:</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">On Wed, Feb 5, 2020 at 10:56 PM Nikita Popov via llvm-dev<br>
<llvm-dev@lists.llvm.org> wrote:</p>
<blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">The IRBuilder is currently templated over a constant folder, and an instruction inserter. <a href="https://reviews.llvm.org/D73835" style="color:#BBB">https://reviews.llvm.org/D73835</a> proposes to move this towards using virtual dispatch instead. As this is a larger design change, I would like to get some feedback on this.<br>
<br>
The current templated design of IRBuilder has a couple of problems:<br>
1. It's not possible to share code between use-sites that use different IRBuilder folders/inserters (short of templating the code and moving it into headers).<br>
2. Methods currently defined on IRBuilderBase (which is not templated) do not use the custom inserter, resulting in subtle bugs (e.g. incorrect InstCombine worklist management). It would be possible to move those into the templated IRBuilder, but...<br>
3. The vast majority of the IRBuilder implementation has to live in the header, because it depends on the template arguments.<br>
4. We have many unnecessary dependencies on IRBuilder.h, because it is not easy to forward-declare. (Significant parts of the backend depend on it via TargetLowering.h, for example.)<br>
<br>
The referenced patch makes the constant folder and instruction inserter use virtual dispatch instead. IRBuilder remains templated, but is only a thin wrapper around IRBuilderBase, which contains all the logic and creation methods. Functions using the IR builder only need to accept IRBuilderBase&, and headers can forward-declare that type.<br>
<br>
The disadvantage of the change is that additional virtual dispatch may make the IRBuilder a bit more expensive. Moving the implementation of IRBuilder methods from the header into the cpp file (not direct part of the proposed change, but a natural followup it enables) would further limit inlining opportunities.<br>
<br>
What are your thoughts on this?</p>
</blockquote><p dir="auto">I am in favor of this, for all the reasons you mention. In fact, I was<br>
tempted to do this myself in the past. This is particularly an issue<br>
for external tools that want to leverage LLVM without forking the<br>
upstream, which is a usecase that LLVM should support better.<br>
<br>
Generally, I think LLVM errs too much on the side of monomorphizing<br>
everything, which sounds nice in theory, but the runtime performance<br>
implications are often questionable, and the compile time certainly<br>
suffers. I think we should take some time to think through the details<br>
of this change, but we should make it.<br>
<br>
Cheers,<br>
Nicolai</p>
</blockquote><p dir="auto">So long as we don't see any measurable (negative) performance impact, I agree that we should do it.</p>
</blockquote></div>
<div style="white-space:normal">

<p dir="auto">Agreed.  This would also be beneficial to projects like Clang and (maybe?) the frontend library that wish to expose interfaces for emitting code without necessarily exposing their internal function-builder types (e.g. Clang’s <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">CodeGenFunction</code>).  Currently this would only be possible when either (1) defining everything in a header or (2) hard-coding a constant-folder and insertion callback.</p>

<p dir="auto">John.</p>
</div>
</div>
</body>
</html>