Skip to content

Add statistics to Util/Transforms/FoldGlobals.cpp. - #7535

Merged
ScottTodd merged 5 commits into
iree-org:mainfrom
ScottTodd:stats-fold-globals
Nov 4, 2021
Merged

ScottTodd merged 5 commits into
iree-org:mainfrom
ScottTodd:stats-fold-globals

Conversation

@ScottTodd

Copy link
Copy Markdown
Member

Following up on #7312 (comment), this adds more statistics to IREE's compiler passes. I personally build with the CMake option -DLLVM_FORCE_ENABLE_STATS=ON so the -pass-statistics flag works in RelWithDebInfo builds, but we don't yet have anything like that configured on our CI or recommended in our developer documentation. See also #6161 (comment) for how this could be useful.

Sample output on bert_encoder_unrolled_fake_weights.mlir (fake weights are folded aggressively 😛):

mlir::iree_compiler::IREE::Util::`anonymous-namespace'::FoldGlobalsPass
  (S) 1113 global ops before folding - Number of util.global ops before folding
  (S)    7 global ops after folding  - Number of util.global ops after folding

@ScottTodd ScottTodd added the compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm) label Nov 3, 2021
@ScottTodd
ScottTodd requested a review from benvanik November 3, 2021 22:58
@google-cla google-cla Bot added the cla: yes label Nov 3, 2021
class FoldGlobalsPass
: public PassWrapper<FoldGlobalsPass, OperationPass<mlir::ModuleOp>> {
public:
explicit FoldGlobalsPass() {}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= default; on these

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still needed {} for the copy constructor:

[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30037\include\memory(3416): error C2280: 'mlir::iree_compiler::IREE::Util::`anonymous-namespace'::FoldGlobalsPass::FoldGlobalsPass(const mlir::iree_compiler::IREE::Util::`anonymous-namespace'::FoldGlobalsPass &)': attempting to reference a deleted function [build] D:\dev\projects\iree\iree\compiler\Dialect\Util\Transforms\FoldGlobals.cpp(360): note: see declaration of 'mlir::iree_compiler::IREE::Util::`anonymous-namespace'::FoldGlobalsPass::FoldGlobalsPass'

FrozenRewritePatternSet frozenPatterns(std::move(patterns));

auto moduleOp = getOperation();
beforeFoldingGlobals =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use llvm::count for these - we don't want to be allocating a new vector and growing it several times just to get the size - especially because this won't be stripped in release builds when statistics are off (as it has side effects)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about stripping in builds with statistics being off.

This works, but there may be a simpler helper?

    afterFoldingGlobals =
        llvm::count_if(moduleOp.getOps<IREE::Util::GlobalOp>(),
                       [](IREE::Util::GlobalOp op) { return true; });

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm ADT is always weird about these things - there's an llvm::size, but that may not work here (worth a shot) - otherwise would need to do something like std::distance, which doesn't take ranges, so could have a count helper:

template <typename R>
static size_t count(R &&range) {
  return std::distance(range.begin(), range.end());
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(that should really be in the ADT as an llvm::distance that takes a range, but ehhh)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beforeFoldingGlobals = llvm::size(moduleOp.getOps<IREE::Util::GlobalOp>());

--->

[build] D:\dev\projects\iree\iree\compiler\Dialect\Util\Transforms\FoldGlobals.cpp(394): error C2672: 'llvm::size': no matching overloaded function found [build] D:\dev\projects\iree\iree\compiler\Dialect\Util\Transforms\FoldGlobals.cpp(394): error C2893: Failed to specialize function template 'auto llvm::size(R &&,enable_if<std::is_base_of<std::random_access_iterator_tag,std::iterator_traits<unknown-type>::iterator_category>::value,void>::type *)' [build] D:\dev\projects\iree\third_party\llvm-project\llvm\include\llvm/ADT/STLExtras.h(1532): note: see declaration of 'llvm::size' [build] D:\dev\projects\iree\iree\compiler\Dialect\Util\Transforms\FoldGlobals.cpp(394): note: With the following template arguments: [build] D:\dev\projects\iree\iree\compiler\Dialect\Util\Transforms\FoldGlobals.cpp(394): note: 'R=llvm::iterator_range<mlir::detail::op_iterator<OpT,mlir::Region::OpIterator>>'

https://github.com/llvm/llvm-project/blob/4ae8c8310401b665cb2728c1bf68ecd4b10cee78/llvm/include/llvm/ADT/STLExtras.h#L1529-L1539 🤔

count helper worked though :)

@ScottTodd
ScottTodd requested a review from benvanik November 3, 2021 23:17
@ScottTodd
ScottTodd merged commit f06c804 into iree-org:main Nov 4, 2021
@ScottTodd
ScottTodd deleted the stats-fold-globals branch November 4, 2021 15:09
@not-jenni not-jenni mentioned this pull request Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants