When compiling with -g "DBG_VALUE <reg>" instructions are added in the MIR, if such a instruction is inserted between instructions that use <reg> then MachineCopyPropagation invalidates <reg> , this causes some copies to not be propagated and causes differences in code generation (ex bugs 50580 and 49446 ). DBG_VALUE instructions should be ignored since they don't actually modify the register.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
There is a patch https://reviews.llvm.org/D98401 doing the same. As discussed in D98401, the register in DBG_VALUE should be also updated. I think backward mcp is missing tracking usage when MO.isDebug() like forward mcp does.
@predator5047 This patch is worth pursuing -- D98401 involves codegen alterations and will necessarily take a long while to review; wheras this patch could land before llvm-13.
This patch is obviously effective at preventing codegen from being changed by debug-info; the downside is that the variable location is wrong afterwards (DBG_VALUE $rcx in the CHECK lines). I'm fairly confident we would prefer to have a false variable location over unstable code generation so it could land as-is; @aprantl @probinson @dblaikie would you agree?
To get the variable location right, you need to populate the CopyDbgUsers container, and re-write them when propagateDefs deletes the copy.
Reverse ping. Hi @predator5047 are you still working on this? One solution might be tracking debug instruction like what MachineCopyPropagation::ReadRegister does.
| llvm/lib/CodeGen/MachineCopyPropagation.cpp | ||
|---|---|---|
| 829 | findCopyDefViaUnit or ReadRegister don't work for backwards propagation, because Tracker.trackCopy sets the CopyInfo::MI filed for the source register to null (line 167), so both would always return null. | |
| llvm/lib/CodeGen/MachineCopyPropagation.cpp | ||
|---|---|---|
| 876 | - if (MO.readsReg())
- Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
+ if (MO.readsReg()) {
+ if (MO.isDebug()) {
+ for (MCRegUnitIterator RUI(MO.getReg(), TRI); RUI.isValid(); ++RUI) {
+ if (MachineInstr *Copy = Tracker.findCopyDefViaUnit(*RUI, *TRI)) {
+ CopyDbgUsers[Copy].insert(MI);
+ }
+ }
+ } else {
+ Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
+ }
+ }Doesn't it work? | |
Yes, can you commit on my behalf.
name: Butiu Alexandru Octavian
email: alexandru.octavian.butiu at gmail.com
This looks unnecessary, can use findCopyDefViaUnit to find out corresponding MachineInstr.