Susan Horwitz
2012-Oct-31 22:54 UTC
[LLVMdev] problem trying to write an LLVM register-allocation pass
Thanks Lang! Here's another question: I'm trying to process this input: int main() { return 0; } but I'm getting an error Assertion `!Fn.getRegInfo().getNumVirtRegs() && "Regalloc must assign all vregs"' failed. At the start of runOnMachineFunction I call Fn.getRegInfo().getNumVirtRegs(); and find that there is 1 virtual register. However, MRI->reg_empty(vreg) tells me that it is not used or defined. So my register-allocation code never sees it, and thus can't allocate a preg for it. I tried using MRI->replaceRegWith(vreg, preg); (where preg is available to vreg's register class) but that didn't work. When I look, the number of vregs in the function is still 1. Can you help with this? Thanks again! Susan On 10/31/2012 04:55 PM, Lang Hames wrote:> Hi Susan, > > The meaning of "addRequired(X)" is that your pass needs X to be run, and > for X to be preserved by all passes that run after X and before your > pass. The PHIElemination and TwoAddressInstruction passes do not > preserve each other, hence there's no way for the pass manager to > schedule them for you if you addRequire(...) them. > > The trick is that CodeGen will schedule both of these passes to be run > before _any_ register allocation pass (see Passes.cpp), so you needn't > require them explicitly - you can just assume they have been run. If you > just remove those lines from your getAnalysisUsage method your pass > should now run as you expect. > > Cheers, > Lang. > > On Wed, Oct 31, 2012 at 1:46 PM, Susan Horwitz <horwitz at cs.wisc.edu > <mailto:horwitz at cs.wisc.edu>> wrote: > > I'm trying to write a MachineFunctionPass to do register allocation. > I have code that worked with an old version of LLVM. It does not > work with llvm-3.1. (or various other versions that I've tried). > > The first problem is that including this line: > > AU.addRequiredID(__TwoAddressInstructionPassID); > > in method getAnalysisUsage causes a runtime error: > > Unable to schedule 'Eliminate PHI nodes for register allocation' > required by 'Unnamed pass: implement Pass::getPassName()' > Unable to schedule pass > UNREACHABLE executed at ... > > I'm invoking the pass like this (given input file foo.c): > > clang -emit-llvm -O0 -c foo.c -o foo.bc > opt -mem2reg foo.bc > foo.ssa > mv foo.ssa foo.bc > llc -load Debug/lib/P4.so -regalloc=gc foo.bc > > > I've attached my entire file (it's very short). Any help would be > much appreciated! > > Susan Horwitz > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >
Lang Hames
2012-Nov-01 00:51 UTC
[LLVMdev] problem trying to write an LLVM register-allocation pass
Hi Susan, I'm having trouble reproducing that error on my end, but I think the problem is probably that you're not using the VirtRegRewriter infrastructure. What your allocator needs to do is populate the virtual register mapping (VirtRegMap pass) with your allocation, rather than rewriting the registers directly through MachineRegisterInfo. Have your allocator require and preserve the VirtRegMap pass, then in your runOnMachineFunction pass grab a reference to the pass with: VirtRegMap &vrm = getAnalysis<VirtRegMap>(); You can then describe your register allocations with: vrm.assignVirt2Phys(<virtreg>, <physreg>) The VirtRegRewriter pass (in VirtRegMap.cpp) will run after your allocator and apply the mapping that you described in the VirtRegMap. I hope this helps. Let me know if it doesn't fix your issue. Cheers, Lang. On Wed, Oct 31, 2012 at 3:54 PM, Susan Horwitz <horwitz at cs.wisc.edu> wrote:> Thanks Lang! > > Here's another question: I'm trying to process this input: > > int main() { > return 0; > } > > but I'm getting an error > Assertion `!Fn.getRegInfo().**getNumVirtRegs() && "Regalloc must assign > all vregs"' failed. > > At the start of runOnMachineFunction I call Fn.getRegInfo().** > getNumVirtRegs(); > and find that there is 1 virtual register. However, MRI->reg_empty(vreg) > tells me that it is not used or defined. So my register-allocation code > never sees it, and thus can't allocate a preg for it. I tried using > MRI->replaceRegWith(vreg, preg); > (where preg is available to vreg's register class) but that didn't work. > When I look, the number of vregs in the function is still 1. > > Can you help with this? > > Thanks again! > > Susan > > > On 10/31/2012 04:55 PM, Lang Hames wrote: > >> Hi Susan, >> >> The meaning of "addRequired(X)" is that your pass needs X to be run, and >> for X to be preserved by all passes that run after X and before your >> pass. The PHIElemination and TwoAddressInstruction passes do not >> preserve each other, hence there's no way for the pass manager to >> schedule them for you if you addRequire(...) them. >> >> The trick is that CodeGen will schedule both of these passes to be run >> before _any_ register allocation pass (see Passes.cpp), so you needn't >> require them explicitly - you can just assume they have been run. If you >> just remove those lines from your getAnalysisUsage method your pass >> should now run as you expect. >> >> Cheers, >> Lang. >> >> On Wed, Oct 31, 2012 at 1:46 PM, Susan Horwitz <horwitz at cs.wisc.edu >> <mailto:horwitz at cs.wisc.edu>> wrote: >> >> I'm trying to write a MachineFunctionPass to do register allocation. >> I have code that worked with an old version of LLVM. It does not >> work with llvm-3.1. (or various other versions that I've tried). >> >> The first problem is that including this line: >> >> AU.addRequiredID(__**TwoAddressInstructionPassID); >> >> >> in method getAnalysisUsage causes a runtime error: >> >> Unable to schedule 'Eliminate PHI nodes for register allocation' >> required by 'Unnamed pass: implement Pass::getPassName()' >> Unable to schedule pass >> UNREACHABLE executed at ... >> >> I'm invoking the pass like this (given input file foo.c): >> >> clang -emit-llvm -O0 -c foo.c -o foo.bc >> opt -mem2reg foo.bc > foo.ssa >> mv foo.ssa foo.bc >> llc -load Debug/lib/P4.so -regalloc=gc foo.bc >> >> >> I've attached my entire file (it's very short). Any help would be >> much appreciated! >> >> Susan Horwitz >> >> ______________________________**_________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> >> http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/**mailman/listinfo/llvmdev<http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev> >> >> >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121031/5e3eb831/attachment.html>
Susan Horwitz
2012-Nov-01 15:33 UTC
[LLVMdev] problem trying to write an LLVM register-allocation pass
Lang - My previous problem (the failed assertion due to a vreg that was not used or defined) was because I was using a debugging version of LLVM with assertions enabled. It seems that there are "extra" assertions in that version. When I run my register allocator in the "normal" version of LLVM, there is no problem with that simple example. However, for other examples that have instructions that do use/define virtual registers, I'm getting bad assembly code. I would really like to avoid changing to the "indirect" approach to register mapping that you suggest (if possible). My code using the "direct" approach worked fine with an old version of LLVM, and I would like to get it to work with llvm-3.1. I wrote a very simple register allocator (attached) that just replaces each vreg in the code with the first preg in its class. I understand that this is not going to give me correct register usage, but it should at least give me assembly code that can be assembled. However, it does not. I get this error from the assembler: Error: Incorrect register `%rax' used with `l' suffix cause by this instruction: cmpl $1, %rax The input is tst.c (also attached). Any help you can provide in understanding what is causing this problem would be much appreciated. I'm not even sure which instance of a vreg is causing the problem. I used to be able to print machine instructions by just using <<, but that no longer works. So even help with that aspect of the problem would be very helpful. Thanks! Susan On 10/31/2012 07:51 PM, Lang Hames wrote:> Hi Susan, > > I'm having trouble reproducing that error on my end, but I think the > problem is probably that you're not using the VirtRegRewriter > infrastructure. What your allocator needs to do is populate the virtual > register mapping (VirtRegMap pass) with your allocation, rather than > rewriting the registers directly through MachineRegisterInfo. > > Have your allocator require and preserve the VirtRegMap pass, then in > your runOnMachineFunction pass grab a reference to the pass with: > > VirtRegMap &vrm = getAnalysis<VirtRegMap>(); > > You can then describe your register allocations with: > > vrm.assignVirt2Phys(<virtreg>, <physreg>) > > The VirtRegRewriter pass (in VirtRegMap.cpp) will run after your > allocator and apply the mapping that you described in the VirtRegMap. > > I hope this helps. Let me know if it doesn't fix your issue. > > Cheers, > Lang. > > On Wed, Oct 31, 2012 at 3:54 PM, Susan Horwitz <horwitz at cs.wisc.edu > <mailto:horwitz at cs.wisc.edu>> wrote: > > Thanks Lang! > > Here's another question: I'm trying to process this input: > > int main() { > return 0; > } > > but I'm getting an error > Assertion `!Fn.getRegInfo(). getNumVirtRegs() && "Regalloc must > assign all vregs"' failed. > > At the start of runOnMachineFunction I call Fn.getRegInfo(). > getNumVirtRegs(); > and find that there is 1 virtual register. However, > MRI->reg_empty(vreg) > tells me that it is not used or defined. So my register-allocation > code never sees it, and thus can't allocate a preg for it. I tried > using MRI->replaceRegWith(vreg, preg); > (where preg is available to vreg's register class) but that didn't > work. When I look, the number of vregs in the function is still 1. > > Can you help with this? > > Thanks again! > > Susan > > > On 10/31/2012 04:55 PM, Lang Hames wrote: > > Hi Susan, > > The meaning of "addRequired(X)" is that your pass needs X to be > run, and > for X to be preserved by all passes that run after X and before your > pass. The PHIElemination and TwoAddressInstruction passes do not > preserve each other, hence there's no way for the pass manager to > schedule them for you if you addRequire(...) them. > > The trick is that CodeGen will schedule both of these passes to > be run > before _any_ register allocation pass (see Passes.cpp), so you > needn't > require them explicitly - you can just assume they have been > run. If you > just remove those lines from your getAnalysisUsage method your pass > should now run as you expect. > > Cheers, > Lang. > > On Wed, Oct 31, 2012 at 1:46 PM, Susan Horwitz > <horwitz at cs.wisc.edu <mailto:horwitz at cs.wisc.edu> > <mailto:horwitz at cs.wisc.edu <mailto:horwitz at cs.wisc.edu>>> wrote: > > I'm trying to write a MachineFunctionPass to do register > allocation. > I have code that worked with an old version of LLVM. It > does not > work with llvm-3.1. (or various other versions that I've > tried). > > The first problem is that including this line: > > AU.addRequiredID(__ TwoAddressInstructionPassID); > > > in method getAnalysisUsage causes a runtime error: > > Unable to schedule 'Eliminate PHI nodes for register > allocation' > required by 'Unnamed pass: implement Pass::getPassName()' > Unable to schedule pass > UNREACHABLE executed at ... > > I'm invoking the pass like this (given input file foo.c): > > clang -emit-llvm -O0 -c foo.c -o foo.bc > opt -mem2reg foo.bc > foo.ssa > mv foo.ssa foo.bc > llc -load Debug/lib/P4.so -regalloc=gc foo.bc > > > I've attached my entire file (it's very short). Any help > would be > much appreciated! > > Susan Horwitz > > ______________________________ _________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> > <mailto:LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>> > http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/ mailman/listinfo/llvmdev > <http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev> > > > >-------------- next part -------------- A non-text attachment was scrubbed... Name: Gcra.cpp Type: text/x-c++src Size: 5783 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121101/b44b4e4f/attachment.cpp> -------------- next part -------------- A non-text attachment was scrubbed... Name: tst.c Type: text/x-csrc Size: 198 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121101/b44b4e4f/attachment.c>
Susan Horwitz
2012-Nov-01 21:41 UTC
[LLVMdev] problem trying to write an LLVM register-allocation pass
Hi again Lang, I decided to try the approach you proposed to see whether it makes the assembly-code problem go away. Again, I tried a very simple register allocator (attached) that just calls vrm.assignVirt2Phys for every vreg in each function, mapping the vreg to the first preg in the register class. I tried two versions: one maps *every* vreg, and the other only maps those for which MRI->reg_empty(vreg) returns false. In both cases I get a core dump somewhere after my reg-allocation pass has run (when I use the "tst.c" file that I sent last time as input). Note also that there is no VirtRegMap.h in the "include" directory of my installed llvm-3.1. I had to copy that file from the source directory. That seems suspicious. Any thoughts? Thanks! Susan On 10/31/2012 07:51 PM, Lang Hames wrote:> Hi Susan, > > I'm having trouble reproducing that error on my end, but I think the > problem is probably that you're not using the VirtRegRewriter > infrastructure. What your allocator needs to do is populate the virtual > register mapping (VirtRegMap pass) with your allocation, rather than > rewriting the registers directly through MachineRegisterInfo. > > Have your allocator require and preserve the VirtRegMap pass, then in > your runOnMachineFunction pass grab a reference to the pass with: > > VirtRegMap &vrm = getAnalysis<VirtRegMap>(); > > You can then describe your register allocations with: > > vrm.assignVirt2Phys(<virtreg>, <physreg>) > > The VirtRegRewriter pass (in VirtRegMap.cpp) will run after your > allocator and apply the mapping that you described in the VirtRegMap. > > I hope this helps. Let me know if it doesn't fix your issue. > > Cheers, > Lang. > > On Wed, Oct 31, 2012 at 3:54 PM, Susan Horwitz <horwitz at cs.wisc.edu > <mailto:horwitz at cs.wisc.edu>> wrote: > > Thanks Lang! > > Here's another question: I'm trying to process this input: > > int main() { > return 0; > } > > but I'm getting an error > Assertion `!Fn.getRegInfo(). getNumVirtRegs() && "Regalloc must > assign all vregs"' failed. > > At the start of runOnMachineFunction I call Fn.getRegInfo(). > getNumVirtRegs(); > and find that there is 1 virtual register. However, > MRI->reg_empty(vreg) > tells me that it is not used or defined. So my register-allocation > code never sees it, and thus can't allocate a preg for it. I tried > using MRI->replaceRegWith(vreg, preg); > (where preg is available to vreg's register class) but that didn't > work. When I look, the number of vregs in the function is still 1. > > Can you help with this? > > Thanks again! > > Susan > > > On 10/31/2012 04:55 PM, Lang Hames wrote: > > Hi Susan, > > The meaning of "addRequired(X)" is that your pass needs X to be > run, and > for X to be preserved by all passes that run after X and before your > pass. The PHIElemination and TwoAddressInstruction passes do not > preserve each other, hence there's no way for the pass manager to > schedule them for you if you addRequire(...) them. > > The trick is that CodeGen will schedule both of these passes to > be run > before _any_ register allocation pass (see Passes.cpp), so you > needn't > require them explicitly - you can just assume they have been > run. If you > just remove those lines from your getAnalysisUsage method your pass > should now run as you expect. > > Cheers, > Lang. > > On Wed, Oct 31, 2012 at 1:46 PM, Susan Horwitz > <horwitz at cs.wisc.edu <mailto:horwitz at cs.wisc.edu> > <mailto:horwitz at cs.wisc.edu <mailto:horwitz at cs.wisc.edu>>> wrote: > > I'm trying to write a MachineFunctionPass to do register > allocation. > I have code that worked with an old version of LLVM. It > does not > work with llvm-3.1. (or various other versions that I've > tried). > > The first problem is that including this line: > > AU.addRequiredID(__ TwoAddressInstructionPassID); > > > in method getAnalysisUsage causes a runtime error: > > Unable to schedule 'Eliminate PHI nodes for register > allocation' > required by 'Unnamed pass: implement Pass::getPassName()' > Unable to schedule pass > UNREACHABLE executed at ... > > I'm invoking the pass like this (given input file foo.c): > > clang -emit-llvm -O0 -c foo.c -o foo.bc > opt -mem2reg foo.bc > foo.ssa > mv foo.ssa foo.bc > llc -load Debug/lib/P4.so -regalloc=gc foo.bc > > > I've attached my entire file (it's very short). Any help > would be > much appreciated! > > Susan Horwitz > > ______________________________ _________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> > <mailto:LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>> > http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/ mailman/listinfo/llvmdev > <http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev> > > > >-------------- next part -------------- A non-text attachment was scrubbed... Name: Gcra.cpp Type: text/x-c++src Size: 8001 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121101/8d008a71/attachment.cpp>
Seemingly Similar Threads
- [LLVMdev] problem trying to write an LLVM register-allocation pass
- [LLVMdev] problem trying to write an LLVM register-allocation pass
- [LLVMdev] problem trying to write an LLVM register-allocation pass
- [LLVMdev] problem trying to write an LLVM register-allocation pass
- [LLVMdev] problem trying to write an LLVM register-allocation pass