<div dir="ltr"><div>Best guess is that you're clobbering something in the red zone. <a href="https://en.wikipedia.org/wiki/Red_zone_(computing)">https://en.wikipedia.org/wiki/Red_zone_(computing)</a>.  It's not guarantee that the area of the stack above the stack pointer is unused. I think you can check if the redzone is used by checking getUsesRedZone() in X86MachineFunctionInfo. If the red zone is used, its not safe to insert a push/pop. I think you can turn off the red zone by passing -mno-red-zone to clang.</div><div><br></div><div><br></div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Nov 12, 2020 at 10:18 AM vignesh babu via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>I am working on a project where I need to insert some logic before each machine basic block.</div><div>In particular, it involves setting some global variables and calling a function. I'm able to add the instructions and verify they get added, but when the compiled program runs, it stops with a segfault.</div><div><br></div><div>For brevity, I'm not sharing the whole code here but basically I have a X86 MachineFunctionPass added to addPreEmitPass2 stage which simply inserts a push rcx immediately followed by pop rcx before each basic block (only the relevant logic portions are included):</div><div><br></div><div>/* Inserts push rcx followed by pop rcx before each MachineBasicBlock */</div><div>void VirtualTimeManager::__insertVtlLogic(MachineFunction &MF,<br>    MachineBasicBlock* origMBB) {<br>    const llvm::TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();</div><div>    auto MMI = &MF.getMMI();<br>    llvm::Module &M = const_cast<Module &>(*MMI->getModule());<br>    if (origMBB->empty() || !origMBB->isLegalToHoistInto())<br>         return;<br></div><div>    llvm::BuildMI(*origMBB, origMBB->begin(), DebugLoc(),</div>        TII.get(X86::POP64r)).addReg(X86::RCX);<div>    /* SOME ADDITIONAL LOGIC COMMENTED OUT */</div><div>    llvm::BuildMI(*origMBB, origMBB->begin(), DebugLoc(),<br>        TII.get(X86::PUSH64r)).addReg(X86::RCX);<br><div>}</div><div>bool VirtualTimeManager::runOnMachineFunction(MachineFunction &MF) {</div><div>         for (auto &MBB : MF) {<br>               MachineBasicBlock* origMBB = &MBB; <br>                __insertVtlLogic(MF, origMBB);           <br>        }<br></div><div>       return true;</div><div>}</div></div><div><br></div><div>When I compile and run a program (e.g from SPEC-2006 benchmark (sjeng)), using this pass, the program segfaults. I don't understand how simply adding a push, followed by pop is affecting the program execution. Could anyone please offer some insights. It would be greatly appreciated. I'm new to LLVM and struggling to understand what is causing this issue.</div><div><br></div><div>Thanks,</div><div>Vignesh </div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>