This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Add a postprocessing parameter in Pattern
ClosedPublic

Authored by jcai19 on Aug 3 2023, 1:49 PM.

Details

Summary

This adds a parameter SupplementalPatterns in tablegen class Pattern for
postprocessing code. For example, this can be used to ensure ops are
placed in the correct device by copying the atttributes that decide
devicement placement in Tensorflow dialect to prevent performance
regression.

Diff Detail

Event Timeline

jcai19 created this revision.Aug 3 2023, 1:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 3 2023, 1:49 PM
jcai19 requested review of this revision.Aug 3 2023, 1:49 PM

This is unfortunately not safe in general, the same named attribute may mean different things etc. Now having a postprocess option where it could be opted in, that we could do.

jcai19 updated this revision to Diff 547900.Aug 7 2023, 12:07 PM

Add a postprocessing parameter in Pattern.

jcai19 updated this revision to Diff 547911.Aug 7 2023, 12:28 PM

Remove unnecessary code.

jcai19 added a comment.Aug 7 2023, 1:11 PM

This is unfortunately not safe in general, the same named attribute may mean different things etc. Now having a postprocess option where it could be opted in, that we could do.

Thanks for the information! I've made the suggested change.

jcai19 updated this revision to Diff 547938.Aug 7 2023, 2:06 PM
jcai19 retitled this revision from [mlir][tblgen] Copy op attributes during rewrites to [mlir] Add a postprocessing option in a Pattern..
jcai19 edited the summary of this revision. (Show Details)

Update caption and summary.

jcai19 updated this revision to Diff 547939.Aug 7 2023, 2:08 PM
jcai19 retitled this revision from [mlir] Add a postprocessing option in a Pattern. to [mlir] Add a postprocessing parameter in Pattern.

Update caption.

@jpienaar @rriddle @antiagainst this change is required to fix a performance issue in Tensorflow. Please let me know if it makes sense to you. Thanks.

jcai19 updated this revision to Diff 550153.Aug 14 2023, 5:31 PM

Fix how ResultIndex is calculated.

jpienaar accepted this revision.Aug 15 2023, 7:20 AM

(ugh sorry forgot to hit send!)

Overall looks good, just wanted to check on restrictions/ordering.

mlir/docs/DeclarativeRewrites.md
696

OOC why not just have single post-process hook that points to a function?

This does look useful for reusing though. And allow for different API's for post-processing.

Do these execute in the order mentioned here?
Is nesting of these supported? (e.g., (CopyAttrFoo (GetParent $src), $dest))

mlir/include/mlir/IR/PatternBase.td
109

Invoke additional code on ... ?

Actually what restrictions re here? Does this allow arbitrary changes to source and targets ops?

mlir/tools/mlir-tblgen/RewriterGen.cpp
1108

supplementary

This revision is now accepted and ready to land.Aug 15 2023, 7:20 AM
jcai19 updated this revision to Diff 550465.Aug 15 2023, 2:06 PM
jcai19 marked an inline comment as done.

Update the example in the doc.

jcai19 added inline comments.Aug 15 2023, 2:07 PM
mlir/docs/DeclarativeRewrites.md
696

OOC why not just have single post-process hook that points to a function?

I'm not sure how a post-process hook works, but the number of post-processing patterns we need for each rewrite varies, and a pattern may either return mlir::OpResult or mlir::Instruction. Below is an example in Tensorflow,

def CopyAttrs2: NativeCodeCallVoid<"CopyAttributes($0, $1)">;

def CopyAttrs3: NativeCodeCallVoid<"CopyAttributes($0, $1.getOwner())">;

def HashTableAndInitializeTableToV2 : Pat<
  (TF_InitializeTableFromTextFileOp:$src (TF_HashTableOp $container,
    $shared_name, $use_node_name_sharing, $key_dtype, $value_dtype), $filename,
    $key_index, $value_index, $vocab_size, $delimiter, $offset),
  (TF_InitializeTableFromTextFileV2Op:$dest1 (TF_HashTableV2Op:$dest2 $container,
    $shared_name, $use_node_name_sharing, $key_dtype, $value_dtype),
    $filename, $key_index, $value_index, $vocab_size, $delimiter, $offset),
  [], [(CopyAttrs2 $src, $dest1), (CopyAttrs3 $src, $dest2)]>;

Would it be easy to implement this example with a function?

Do these execute in the order mentioned here?

Yes patterns are executed in the specified order. I've updated the example to make it clearer.

Is nesting of these supported? (e.g., (CopyAttrFoo (GetParent $src), $dest))

Yes they are. I've added nested patterns in the example.

mlir/include/mlir/IR/PatternBase.td
109

Invoke additional code on ... ?

Done.

Actually what restrictions re here? Does this allow arbitrary changes to source and targets ops?

I'm not aware of any restrictions other than their results will never be used to replace source patterns.

This revision was landed with ongoing or failed builds.Aug 15 2023, 2:08 PM
This revision was automatically updated to reflect the committed changes.
jcai19 added inline comments.Aug 15 2023, 2:16 PM
mlir/docs/DeclarativeRewrites.md
696

FYI I've committed this change as this is need to unblock our work in Tensorflow. Let me know if you think having a post-process hook that points to a function is a better solution and I'll address that in another patch. Thanks!

mehdi_amini added a comment.EditedAug 15 2023, 3:53 PM

This broke the build (you can see it in the pre-merge build log on this revision as well, but you should have got some emails already)

This broke the build (you can see it in the pre-merge build log on this revision as well, but you should have got some emails already)

Thanks Mehdi! Applogy for the inconvenience. I've fixed the test failure and relanded the change in https://reviews.llvm.org/rGd22965f0d623392fdf87e5a09a0276aa70e8dfed