<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: arial,helvetica,sans-serif; font-size: 10pt; color: #000000'><br><hr id="zwchr"><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><b>From: </b>"via llvm-dev" <llvm-dev@lists.llvm.org><br><b>To: </b>"llvm-dev" <llvm-dev@lists.llvm.org><br><b>Sent: </b>Tuesday, March 15, 2016 5:07:29 PM<br><b>Subject: </b>[llvm-dev] RFC: DenseMap grow() slowness<br><br>
There’s a few passes in LLVM that make heavy use of a big DenseMap, one that potentially gets filled with up to 1 entry for each instruction in the function. EarlyCSE is the best example, but Reassociate and MachineCSE have this to some degree as well (there might be others?). To put it simply: at least in my profile, EarlyCSE spends ~1/5 of its time growing DenseMaps. This is kind of… bad.<div class=""><br class=""></div><div class="">grow() is inherently slow because it needs to rehash and reinsert everything. This means growing a DenseMap costs much, much more than growing, for example, a vector. I talked about this with a few people and here are some possibilities we’ve come up with to improve this (some of which probably aren’t what we want):</div><div class=""><br class=""></div><div class="">1. Use a map that doesn’t require rehashing and reinsertion to grow. Chaining lets you do this, but std::unordered_map is probably so much slower than DenseMap we’d lose more than we gain.</div><div class="">2. Include the hash code in the map so that we don’t have to rehash. 32 bits more per entry (or whatever), and it might not help that much, since we still have to do the whole reinsertion routine.</div><div class="">3. Pre-calculate an estimate as to the map size we need. For example, in EarlyCSE, this is possibly gross overestimate of size needed:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">  <span style="color: rgb(187, 44, 162);" class="">unsigned</span> InstCount = <span style="color: rgb(39, 42, 216);" class="">0</span>;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">  <span style="color: rgb(187, 44, 162);" class="">unsigned</span> LoadCount = <span style="color: rgb(39, 42, 216);" class="">0</span>;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">  <span style="color: rgb(187, 44, 162);" class="">unsigned</span> CallCount = <span style="color: rgb(39, 42, 216);" class="">0</span>;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">  <span style="color: rgb(187, 44, 162);" class="">for</span> (<span style="color: rgb(79, 129, 135);" class="">inst_iterator</span> FI = <span style="color: rgb(49, 89, 93);" class="">inst_begin</span>(<span style="color: rgb(79, 129, 135);" class="">F</span>), FE = <span style="color: rgb(49, 89, 93);" class="">inst_end</span>(<span style="color: rgb(79, 129, 135);" class="">F</span>); FI != FE; ++FI) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="color: rgb(0, 0, 0);" class="">    </span><span style="color: rgb(187, 44, 162);" class="">if</span><span style="color: rgb(0, 0, 0);" class=""> (FI-></span>mayReadOrWriteMemory<span style="color: rgb(0, 0, 0);" class="">())</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">      ++LoadCount;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">    <span style="color: rgb(187, 44, 162);" class="">else</span> <span style="color: rgb(187, 44, 162);" class="">if</span> (<span style="color: rgb(49, 89, 93);" class="">isa</span><<span style="color: rgb(79, 129, 135);" class="">CallInst</span>>(*FI))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">      ++CallCount;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">    <span style="color: rgb(187, 44, 162);" class="">else</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">      ++InstCount;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">  }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">  <span style="color: rgb(79, 129, 135);" class="">AvailableValues</span>.<span style="color: rgb(61, 29, 129);" class="">resize</span>(InstCount);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">  <span style="color: rgb(79, 129, 135);" class="">AvailableLoads</span>.<span style="color: rgb(61, 29, 129);" class="">resize</span>(LoadCount);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">  <span style="color: rgb(79, 129, 135);" class="">AvailableCalls</span>.<span style="color: rgb(61, 29, 129);" class="">resize</span>(CallCount);</div></div><div class=""><br class=""></div><div id="DWT7802" class="">But it does the job, and saves ~20% of time in EarlyCSE on my profiles. Yes, iterating over the entire function is way cheaper than grow(). Downsides are that while it’s still bounded by function size, it could end up allocating a good bit more depending on — in EarlyCSE’s case — the control flow/dominator structure.</div></blockquote><br>This last option makes perfect sense to me. One thing we might be able to do regarding the extra memory overhead is, instead of actually resizing up front, to start with a relatively small map, but use the function size to set the growth factor so that we grow only a small number of times (say once) in the worst case.<br><br> -Hal<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><div class=""></div><div class=""><br class=""></div><div class="">Any thoughts on this, or other less ugly alternatives? I estimate that, at least in our pass pipeline, we’re losing at least ~1% of total time to avoidable DenseMap::grow() operations, which feels a little bit… unnecessary.</div><div class=""><br class=""></div><div class="">—escha</div><br>_______________________________________________<br>LLVM Developers mailing list<br>llvm-dev@lists.llvm.org<br>http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br></blockquote><br><br><br>-- <br><div><span name="x"></span>Hal Finkel<br>Assistant Computational Scientist<br>Leadership Computing Facility<br>Argonne National Laboratory<span name="x"></span><br></div></div></body></html>