This is an archive of the discontinued LLVM Phabricator instance.

Add a pass to dump a given operation
Needs ReviewPublic

Authored by tpopp on Dec 12 2022, 7:29 AM.

Details

Summary

For use while debugging pipelines and wanting some intermediate stages.

Diff Detail

Event Timeline

tpopp created this revision.Dec 12 2022, 7:29 AM
tpopp requested review of this revision.Dec 12 2022, 7:29 AM
tpopp added a comment.Dec 12 2022, 7:31 AM

I often add a simple dump command inside of a pass at the beginning or end when I'm quickly trying to gather some data and don't want to dump everything. I considered making a tiny rfc regarding this to see if others prefer another way, but want to at least first run it by a couple of folks. I'm sure there's several ways to do the same thing, but this would be my preferred way of debugging a pipeline not presented in a test using opt and before my morning coffee.

tpopp updated this revision to Diff 482125.Dec 12 2022, 7:37 AM

NFC: cleanup some ugliness

And this is a more fine grained version of mlir-print-ir-after ? What is missing there? Triggering only particular pipeline, particular part of pipeline?

tpopp added a comment.Dec 12 2022, 8:52 AM

And this is a more fine grained version of mlir-print-ir-after ? What is missing there? Triggering only particular pipeline, particular part of pipeline?

This allows, for example, printing after an (unfortunately) unnamed pass, adding print statements when looking at the C++ code without first having to locate the opt version of the pass' name, a single pipeline with printing after several milestone passes. The overall thought is that this lends itself better to simple C++ based debugging when one is not starting with a textual opt pipeline. It does not add much in the way of new functionality.

LGTM, but please wait for converging with Jacques on this!

And this is a more fine grained version of mlir-print-ir-after ? What is missing there? Triggering only particular pipeline, particular part of pipeline?

While mlir-print-ir-after is a good debugging tool, it does not compose well when "using MLIR as a library". That is someone building their pipeline programmatically and wanting some control on the debuggability / traceability of the flow would need something more custom that "pass name".
As an example, what if you wanted to get some print to a file after "phase1" of something like "iree_compiler" where "phase1" (whatever that means) ends with running canonicalization?
You can't really ask mlir-print-ir-after=canonicalize right?
Also, the whole PassManagerOptions::addPrinterInstrumentation() logic hard-codes llvm::errs() right now. Of course one can re-implement their own instrumentation, but that becomes kind of "not using mlir-print-ir-after" as well.

The only thing I could wonder here would be how to make it convenient to have this as an instrumentation instead of a pass so that is stays decoupled from the pass-pipeline dump, the time statistics, etc.

printing after an (unfortunately) unnamed pass

Uh, seems like a bug that I wouldn't want to justify a feature to work around this.

mlir/include/mlir/Transforms/Passes.td
145

print-op ?

mlir/test/IR/print-ops.mlir
1

Can we write the test so that we don't need this flag?

Nice, this is definitely useful. Agree that "print op" is better than plural "print ops".

I'm not super familiar with the concept of "instrumentation" in this context, but even on the opt command line it is still useful to be able to drop this pass in somewhere specific as just a pass. I think that if you are printing out stuff then there is no point in measuring compilation time -- all the context switches, cache invalidation, etc. make it unrepresentative of the overall running time even if you were to somehow "factor out" the time between starting/ending printing.

I'm not super familiar with the concept of "instrumentation" in this context,

See https://mlir.llvm.org/docs/PassManagement/#pass-instrumentation ; basically it is conceptually important from the infra point of view to keep the separation between the actual pass-pipeline from the thing we do around it, hence the numerous hooks there that stays out of the "pipeline configuration" itself.

but even on the opt command line it is still useful to be able to drop this pass in somewhere specific as just a pass.

--mlir-print-ir-after=pass-name is the recommended way for this right now, it'll have the same effect as this pass (except in cases where you invoke multiple times the same pass I believe, as I mentioned before).