From d4921f4a96a46e24c2fb76ca7feb66e6894395d2 Mon Sep 17 00:00:00 2001 From: Nicolas Vasilache Date: Thu, 31 Jan 2019 07:16:29 -0800 Subject: Address Performance issue in NestedMatcher A performance issue was reported due to the usage of NestedMatcher in ComposeAffineMaps. The main culprit was the ubiquitous copies that were occuring when appending even a single element in `matchOne`. This CL generally simplifies the implementation and removes one level of indirection by getting rid of auxiliary storage as well as simplifying the API. The users of the API are updated accordingly. The implementation was tested on a heavily unrolled example with ComposeAffineMaps and is now close in performance with an implementation based on stateless InstWalker. As a reminder, the whole ComposeAffineMaps pass is slated to disappear but the bug report was very useful as a stress test for NestedMatchers. Lastly, the following cleanups reported by @aminim were addressed: 1. make NestedPatternContext scoped within runFunction rather than at the Pass level. This was caused by a previous misunderstanding of Pass lifetime; 2. use defensive assertions in the constructor of NestedPatternContext to make it clear a unique such locally scoped context is allowed to exist. PiperOrigin-RevId: 231781279 --- mlir/lib/Transforms/MaterializeVectors.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'mlir/lib/Transforms/MaterializeVectors.cpp') diff --git a/mlir/lib/Transforms/MaterializeVectors.cpp b/mlir/lib/Transforms/MaterializeVectors.cpp index 2744b1d624c..432ad1f39b8 100644 --- a/mlir/lib/Transforms/MaterializeVectors.cpp +++ b/mlir/lib/Transforms/MaterializeVectors.cpp @@ -201,9 +201,6 @@ struct MaterializeVectorsPass : public FunctionPass { PassResult runOnFunction(Function *f) override; - // Thread-safe RAII contexts local to pass, BumpPtrAllocator freed on exit. - NestedPatternContext mlContext; - static char passID; }; @@ -744,6 +741,9 @@ static bool materialize(Function *f, } PassResult MaterializeVectorsPass::runOnFunction(Function *f) { + // Thread-safe RAII local context, BumpPtrAllocator freed on exit. + NestedPatternContext mlContext; + // TODO(ntv): Check to see if this supports arbitrary top-level code. if (f->getBlocks().size() != 1) return success(); @@ -768,10 +768,11 @@ PassResult MaterializeVectorsPass::runOnFunction(Function *f) { return matcher::operatesOnSuperVectors(opInst, subVectorType); }; auto pat = Op(filter); - auto matches = pat.match(f); + SmallVector matches; + pat.match(f, &matches); SetVector terminators; for (auto m : matches) { - terminators.insert(cast(m.first)); + terminators.insert(cast(m.getMatchedInstruction())); } auto fail = materialize(f, terminators, &state); -- cgit v1.2.3