This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Introduce `replaceUsesOfWith` to `RewriterBase`
ClosedPublic

Authored by guraypp on Nov 16 2022, 3:03 AM.

Details

Summary

Finding uses of a value and replacing them with a new one is a common method. I have not seen an safe and easy shortcut that does that. This revision attempts to address that by intoroducing replaceUsesOfWith to RewriterBase.

Diff Detail

Event Timeline

guraypp created this revision.Nov 16 2022, 3:03 AM
guraypp requested review of this revision.Nov 16 2022, 3:03 AM
rriddle added inline comments.Nov 16 2022, 3:08 AM
mlir/lib/IR/PatternMatch.cpp
315–317

Why is this collecting all of the uses into a vector?

guraypp added inline comments.Nov 16 2022, 3:20 AM
mlir/lib/IR/PatternMatch.cpp
315–317

My motivation of this change comes from D138029. Shortly, the old code looked like this. getUsers iterates on actual users in this situation. It is incorrect because I am modifying uses while iterating. Ofc, I am happy to change if there is better way.

for (Operation *user : llvm::make_early_inc_range(from.getUsers())) 
  rewriter.updateRootInPlace(user, [&]() {
    user->replaceUsesOfWith(from, to);
});
rriddle added inline comments.Nov 16 2022, 3:23 AM
mlir/lib/IR/PatternMatch.cpp
315–317

Have you tried an early inc range of getUses?

guraypp updated this revision to Diff 475756.Nov 16 2022, 3:28 AM

get rid of vector and use make_early_inc_range

mehdi_amini accepted this revision.Nov 16 2022, 7:25 AM
mehdi_amini added inline comments.
mlir/include/mlir/IR/PatternMatch.h
505

Note that using this instead of from.replaceUsesOfWith(to) will mark every modified users and notify the listener.

This revision is now accepted and ready to land.Nov 16 2022, 7:25 AM
mehdi_amini added inline comments.Nov 16 2022, 7:28 AM
mlir/include/mlir/IR/PatternMatch.h
505

Because otherwise, one may wonder "why reimplement it here?"

506

Please rename it to replaceAllUsesWith for consitency with Value::replaceAllUsesWith

guraypp updated this revision to Diff 475833.Nov 16 2022, 8:24 AM

Address @mehdi_amini comments

This revision was automatically updated to reflect the committed changes.