This patch adds the ability to watch for insertions/deletions of MachineInstructions. Currently in GISel, we rely on the MachineIRBuilder::recordInsertion to further legalize the intermediate instructions - this won't work if the target builds instructions with BuildMI interface. Similarly, this makes it easy to watch for MI::eraseFromParent to keep various data structures up to date in the legalizer/combiner etc. I suspect there would be non GISel use cases as well.
While this patch adds the ability to register callbacks, nothing makes use of it as I didn't want to put in too much effort before getting feedback. The GISel legalizer would be one place that would be able to use of this.
Details
Diff Detail
Event Timeline
Hi Aditya,
Could you use a delegate pattern like what we do for MachineRegisterInfio and LiveRangeEdit?
Other than that, the direction looks reasonable.
Cheers,
-Quentin
I've seen callbacks lead to unintuitive/hard to understand code. Can you motivate the use case some more?
Some additional coding style comments
include/llvm/CodeGen/MachineFunction.h | ||
---|---|---|
355–356 | Use InsertCallbackFn = nullptr; here instead of setting it in init(). | |
401–409 | You should try to make these private with some friend declaration for the ilist traits. | |
lib/CodeGen/MachineFunction.cpp | ||
249–250 | I think this function is mostly about freeing memory. Setting the pointer should not be necessary. |
One of the motivations was the above - guaranteeing that the legalizer will always legalize intermediate instructions - right now it only guarantees legalization when the intermediate instructions are created with the MachineIRBuilder that's passed in - it would be nice to not rely on that mechanism. Besides insertions, it would be nice to keep track of instruction deletions -for eg in the combiner - it's useful to know which instructions were erased and needs to be removed the various worklists rather than conservatively assuming that the current instruction is always considered erased even if it isn't. Additionally if a combine deletes any other instruction besides the current root, we will currently not know about this and will result in calling combine(MI) on a deleted instruction resulting in UB (same in the legalizer).
Lastly I think this mechanism will allow a CSEing builder to also keep track of new instructions(again without relying on usage of MIRBuilder) as well as deletions (currently very hard).
LGTM with nitpicks addressed.
include/llvm/CodeGen/MachineFunction.h | ||
---|---|---|
372–373 | Use const MachineInstr& here to make it obvious that the parameters cannot be nullptr. (We didn't do that in older APIs but I think we should do it for new APIs we create). | |
380–381 | should also use references. | |
401 | Add doxygen comment to public API. | |
410 | Add a doxygen comment. |
Use InsertCallbackFn = nullptr; here instead of setting it in init().