This is an archive of the discontinued LLVM Phabricator instance.

[MF][MBB]: Add the ability to register callbacks for removal and insertion of MachineInstrs
ClosedPublic

Authored by aditya_nandakumar on Sep 14 2018, 5:17 PM.

Details

Summary

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.

Diff Detail

Repository
rL LLVM

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().

386–394

You should try to make these private with some friend declaration for the ilist traits.

lib/CodeGen/MachineFunction.cpp
238–239

I think this function is mostly about freeing memory. Setting the pointer should not be necessary.

I've seen callbacks lead to unintuitive/hard to understand code. Can you motivate the use case some more?

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).

Hi Aditya,

Could you use a delegate pattern like what we do for MachineRegisterInfio and LiveRangeEdit?

Other than that, the direction looks reasonable.

I will update it shortly with that approach. Thanks

Cheers,
-Quentin

Updated based on feedback from Quentin and Matthias.

MatzeB accepted this revision.Sep 20 2018, 1:35 PM

LGTM with nitpicks addressed.

include/llvm/CodeGen/MachineFunction.h
376–377

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).

384–385

should also use references.

386

Add doxygen comment to public API.

395

Add a doxygen comment.

This revision is now accepted and ready to land.Sep 20 2018, 1:35 PM

Submitted in r342696. Thanks Matthias for the review.