<div dir="ltr">Hi,<div><br></div><div>I looked into LLVMContextImpl::dropTriviallyDeadConstantArrays (<a href="https://llvm.org/doxygen/LLVMContextImpl_8cpp_source.html#l00131">https://llvm.org/doxygen/LLVMContextImpl_8cpp_source.html#l00131</a>) for performance purpose, and have some questions</div><div><br></div><div>////////////////////////////////////////////////////////////////////////////</div><div><b>1) How the WorkList ensures no double-release.</b></div><div><div>////////////////////////////////////////////////////////////////////////////</div><div></div></div><div><br></div><div>In the worklist loop, it inserts operands of a ConstantArray to delete back to the worklist. Is it possible that the operand to insert was already deleted?</div><div>This seems possible if the element order in the worklist is changed. For example, if we change its code like the following, recycled is 0 in my test case.</div><div><br></div><div>  SmallSetVector<ConstantArray *, 4> WorkList(ArrayConstants.begin(),</div>                                                                             ArrayConstants.end());<div><b>  SmallPtrSet<ConstantArray *, 4> D;<br>  int recycled = 0;</b><br><br>  while (!WorkList.empty()) {<br>    ConstantArray *C = WorkList.pop_back_val();<br>    <b>if (D.contains(C)) {<br>      ++recycled;<br>      continue;<br>    }</b><br>    if (C->use_empty()) {<br>      for (const Use &Op : C->operands()) {<br>        if (auto *COp = dyn_cast<ConstantArray>(Op))<br>          WorkList.insert(COp);<br>      }<br>      <b>D.insert(C);</b><br>      C->destroyConstant();<br>    }<br>  }<br></div><div><br></div><div>However, if I changed the code like the following, then recycled could be > 0 in the same test case.</div><div><br></div><div><div> <b>SmallSetVector<ConstantArray *, 4> WorkList;<br></b></div><div><b> SmallPtrSet<ConstantArray *, 4> D;</b><br></div><div><b> int recycled = 0;</b></div><div><br></div><div>  <b>for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {<br>    auto *C = *I++;<br>    if (C->use_empty()) {<br>      for (const Use &Op : C->operands()) {<br>        if (auto *COp = dyn_cast<ConstantArray>(Op)) {<br>          WorkList.insert(COp);<br>        }<br>      }</b></div><div><b>      D.insert(C);<br>      C->destroyConstant();<br>    }<br>  }</b><br>  while (!WorkList.empty()) {<br>    ConstantArray *C = WorkList.pop_back_val();</div><div><b>    if (D.contains(C)) {<br>      ++recycled;<br>      continue;<br>    }</b><br>    if (C->use_empty()) {<br>      for (const Use &Op : C->operands()) {<br>        if (auto *COp = dyn_cast<ConstantArray>(Op)) {<br>          WorkList.insert(COp);<br>        }<br>      }</div><div>      <b>D.insert(C);</b><br>      C->destroyConstant();<br>    }<br>  }<br><br></div><div>The difference is that the first code (the same as the current code at HEAD) processes the worklist sorted by SmallVector, while the second one processes the ConstrantArray first by the order of ArrayConstants, then in the order sorted by SmallVector. </div><div><br></div><div>Maybe I miss anything. Is the current code always safe?</div><div><br></div><div><div>////////////////////////////////////////////////////////////////////////////</div><div></div></div><div><b>2) The insertion of SmallSetVector is slow</b></div><div>////////////////////////////////////////////////////////////////////////////<br></div><div><div><br></div><div>I saw the current version was to address the performance issue of the previous version (<a href="https://reviews.llvm.org/rG81f385b0c6ea37dd7195a65be162c75bbdef29d2">https://reviews.llvm.org/rG81f385b0c6ea37dd7195a65be162c75bbdef29d2</a>).</div><div></div></div><div><br></div><div>In many of my test cases, I found the most time cost of dropTriviallyDeadConstantArrays is by the SetVector::insert caused by constructing the worklist from ConstantArray with size >50k. And in practice, a very few new operands are added into WorkList by the worklist loop. </div><div><br></div><div>If this is the case, the code above, which is like a combination of the previous and the current versions of dropTriviallyDeadConstantArrays, could be a trade-off, because 1) worklist can still resolve the n^2 loop issue 2) worklist is only used when necessary, and its length is usually small with less insertion overhead. In my case this reduced the time cost of dropTriviallyDeadConstantArrays from 17% to 4%. </div><div><br></div><div>Thank you, s</div><div><br></div><div><br></div><div><br></div></div></div>