<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 8 August 2018 at 18:30, Ahmad Nouralizadeh via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>LLVM is built in Release mode. The version is 6.0.0. I think that a similar code worked on verison 3.9.0. It is probably a null pointer dereference occurring in eraseFromParent(). I checked and reconfirmed that the instruction had no uses. <span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">Perhaps I should rebuild LLVM. Thanks.</span></div></div></blockquote><div>Ahmad,<br></div><div><br></div><div>I would highly recommend, at least if you are running into problems, that debug using asserts enabled. This will (most of the time) give you a better "explanation" as to what you've done wrong. Not every time, but for many of the "we expect someone to get this wrong at some pont" type of problems.<br><br></div><div>Trying to figure out why something failed in a release build without asserts is often painfully hard, because when you crash, the "reason" for the crash is in code that has been inlined, the actual understanding of what went wrong is several layers of calls above the crash-point.</div><div><br></div><div>--</div><div>Mats<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 8, 2018 at 9:03 PM, <a href="http://mayuyu.io" target="_blank">mayuyu.io</a> <span dir="ltr"><<a href="mailto:admin@mayuyu.io" target="_blank">admin@mayuyu.io</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">Hmmmm that’s strange. Do you get an assert when you build LLVM with this code in Debug Mode or is it simply null pointer dereference? <div>On a side note, usually when doing transforms similar to yours I use IRBuilder’s CreateXXX followed by a ReplaceInstWithInst which works perfectly fine for me.</div><div><br><div id="m_5020683863353166431m_321749027144279699AppleMailSignature">Zhang</div><div><div class="m_5020683863353166431h5"><div><br>在 2018年8月9日,00:29,Ahmad Nouralizadeh <<a href="mailto:ahmadnouralizadeh@gmail.com" target="_blank">ahmadnouralizadeh@gmail.com</a>> 写道:<br><br></div><blockquote type="cite"><div><div dir="ltr">Hi. Thanks. I changed the code but the problem exists. This is my new code which is again very simple:<div><div>...</div><div>bool runOnFunction(Function &F) override {</div><div><br></div><div>vector<Instruction *> dels;</div><div>dels.clear();</div><div><br></div><div>for (inst_iterator It = inst_begin(&F), Ie = inst_end(&F); It != Ie;) {</div><div><span style="white-space:pre-wrap"> </span>Instruction *I = &*(It++);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>if (auto* op = dyn_cast<BinaryOperator>(I)) {</div><div><span style="white-space:pre-wrap"> </span>IRBuilder<NoFolder> builder(op);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>Value* lhs = op->getOperand(0);</div><div><span style="white-space:pre-wrap"> </span>Value* rhs = op->getOperand(1);</div><div><span style="white-space:pre-wrap"> </span>Value* mul = builder.CreateMul(lhs, rhs);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>for (auto& U : op->uses()) {</div><div><span style="white-space:pre-wrap"> </span> User* user = U.getUser();</div><div><span style="white-space:pre-wrap"> </span> user->setOperand(U.getOperandN<wbr>o(), mul);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>//I->eraseFromParent();</div><div><span style="white-space:pre-wrap"> </span>dels.push_back(I);</div><div><span style="white-space:pre-wrap"> </span>}</div><div>}<span style="white-space:pre-wrap"> </span></div><div><br></div><div>for (auto &I : dels) {</div><div><span style="white-space:pre-wrap"> </span>I->eraseFromParent();</div><div>}</div></div><div>...</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 8, 2018 at 6:16 PM, <a href="http://mayuyu.io" target="_blank">mayuyu.io</a> <span dir="ltr"><<a href="mailto:admin@mayuyu.io" target="_blank">admin@mayuyu.io</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">Hi:<div>As stated in the documentation you shouldn’t modify it while iterating as this invalidates the iterator. Try save the pointers to a vector outside your loop and perform erasing in a new loop following your current one.<br><br><div id="m_5020683863353166431m_321749027144279699m_-2881597292314478193AppleMailSignature">Zhang</div><div><div class="m_5020683863353166431m_321749027144279699h5"><div><br>在 2018年8月8日,07:54,Ahmad Nouralizadeh via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> 写道:<br><br></div><blockquote type="cite"><div><div dir="ltr">Thanks Bjorn! But The problem is still there.</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 8, 2018 at 2:04 AM, Björn Pettersson A <span dir="ltr"><<a href="mailto:bjorn.a.pettersson@ericsson.com" target="_blank">bjorn.a.pettersson@ericsson.c<wbr>om</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>It looks quite dangerous to erase the instruction I inside the loop when iterating over all instructions in the function.<br>
I guess it depends on how the range based iterator is implemented if that works or not.<br>
<br>
</span>I think that you, for example, can change the range based iteration<br>
<span><br>
for (auto &I : instructions(F))<br>
<br>
into something like this<br>
<br>
for (inst_iterator It = inst_begin(&F), Ie = inst_end(&F); It != Ie;) {<br>
Instruction *I = &*(It++);<br>
...<br>
}<br>
<br>
That will make sure that you step the iterator before modifying the data structure you iterate over.<br>
<br>
Regards,<br>
Björn<br>
______________________________<wbr>__________<br>
From: llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.o<wbr>rg</a>> on behalf of Ahmad Nouralizadeh via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
</span>Sent: Tuesday, August 7, 2018 22:26<br>
<span class="m_5020683863353166431m_321749027144279699m_-2881597292314478193im m_5020683863353166431m_321749027144279699m_-2881597292314478193HOEnZb">To: <a href="mailto:paul.robinson@sony.com" target="_blank">paul.robinson@sony.com</a><br>
Cc: <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
Subject: Re: [llvm-dev] Error Calling eraseFromParent()<br>
<br>
</span><div class="m_5020683863353166431m_321749027144279699m_-2881597292314478193HOEnZb"><div class="m_5020683863353166431m_321749027144279699m_-2881597292314478193h5">The code is really simple. But I can not the reason for the segmentation fault. I only know that the eraseFromParent() function leads to it. The whole code is:<br>
...<br>
bool runOnFunction(Function &F) override {<br>
<br>
for (auto &I : instructions(F)) {<br>
<br>
if (auto* op = dyn_cast<BinaryOperator>(&I)) {<br>
IRBuilder<> builder(op);<br>
<br>
Value* lhs = op->getOperand(0);<br>
Value* rhs = op->getOperand(1);<br>
Value* mul = builder.CreateMul(lhs, rhs);<br>
<br>
for (auto& U : op->uses()) {<br>
User* user = U.getUser();<br>
user->setOperand(U.getOperandN<wbr>o(), mul);<br>
}<br>
<br>
I.eraseFromParent();<br>
}<br>
<br>
}<br>
...<br>
And I think that the code worked well with LLVM-3.6.0 that I tested one year ago. Now, I use LLVM-6.0.0.<br>
Regards.<br>
</div></div></blockquote></div><br></div>
</div></blockquote></div></div><blockquote type="cite"><div><span>______________________________<wbr>_________________</span><br><span>LLVM Developers mailing list</span><br><span><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a></span><br><span><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a></span><br></div></blockquote></div></div></blockquote></div><br></div>
</div></blockquote></div></div></div></div></blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div></div>