MLIR 22.0.0git
DIExpressionRewriter.cpp
Go to the documentation of this file.
1//===- DIExpressionRewriter.cpp - Rewriter for DIExpression operators -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/Support/DebugLog.h"
11
12using namespace mlir;
13using namespace LLVM;
14
15#define DEBUG_TYPE "llvm-di-expression-simplifier"
16
17//===----------------------------------------------------------------------===//
18// DIExpressionRewriter
19//===----------------------------------------------------------------------===//
20
22 std::unique_ptr<ExprRewritePattern> pattern) {
23 patterns.emplace_back(std::move(pattern));
24}
25
26DIExpressionAttr
27DIExpressionRewriter::simplify(DIExpressionAttr expr,
28 std::optional<uint64_t> maxNumRewrites) const {
29 ArrayRef<OperatorT> operators = expr.getOperations();
30
31 // `inputs` contains the unprocessed postfix of operators.
32 // `result` contains the already finalized prefix of operators.
33 // Invariant: concat(result, inputs) is equivalent to `operators` after some
34 // application of the rewrite patterns.
35 // Using a deque for inputs so that we have efficient front insertion and
36 // removal. Random access is not necessary for patterns.
37 std::deque<OperatorT> inputs(operators.begin(), operators.end());
39
40 uint64_t numRewrites = 0;
41 while (!inputs.empty() &&
42 (!maxNumRewrites || numRewrites < *maxNumRewrites)) {
43 bool foundMatch = false;
44 for (const std::unique_ptr<ExprRewritePattern> &pattern : patterns) {
45 ExprRewritePattern::OpIterT matchEnd = pattern->match(inputs);
46 if (matchEnd == inputs.begin())
47 continue;
48
49 foundMatch = true;
51 pattern->replace(llvm::make_range(inputs.cbegin(), matchEnd));
52 inputs.erase(inputs.begin(), matchEnd);
53 inputs.insert(inputs.begin(), replacement.begin(), replacement.end());
54 ++numRewrites;
55 break;
56 }
57
58 if (!foundMatch) {
59 // If no match, pass along the current operator.
60 result.push_back(inputs.front());
61 inputs.pop_front();
62 }
63 }
64
65 if (maxNumRewrites && numRewrites >= *maxNumRewrites) {
66 LDBG() << "LLVMDIExpressionSimplifier exceeded max num rewrites ("
67 << maxNumRewrites << ")";
68 // Skip rewriting the rest.
69 result.append(inputs.begin(), inputs.end());
70 }
71
72 return LLVM::DIExpressionAttr::get(expr.getContext(), result);
73}
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be the output argument nBegin is set to its * replacement(set to `begin` if no invalidation happens). Since outgoing *copies could have been inserted at `end`
std::deque< OperatorT >::const_iterator OpIterT
void addPattern(std::unique_ptr< ExprRewritePattern > pattern)
Register a rewrite pattern with the rewriter.
LLVM::DIExpressionAttr simplify(LLVM::DIExpressionAttr expr, std::optional< uint64_t > maxNumRewrites={}) const
Simplify a DIExpression according to all the patterns registered.
Include the generated interface declarations.