MLIR 22.0.0git
DIExpressionLegalization.cpp
Go to the documentation of this file.
1//===- DIExpressionLegalization.cpp - DIExpression Legalization Patterns --===//
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
11#include "llvm/BinaryFormat/Dwarf.h"
12
13using namespace mlir;
14using namespace LLVM;
15
16//===----------------------------------------------------------------------===//
17// MergeFragments
18//===----------------------------------------------------------------------===//
19
21 OpIterT it = operators.begin();
22 if (it == operators.end() ||
23 it->getOpcode() != llvm::dwarf::DW_OP_LLVM_fragment)
24 return operators.begin();
25
26 ++it;
27 if (it == operators.end() ||
28 it->getOpcode() != llvm::dwarf::DW_OP_LLVM_fragment)
29 return operators.begin();
30
31 return ++it;
32}
33
36 OpIterT it = operators.begin();
37 OperatorT first = *(it++);
38 OperatorT second = *it;
39 // Add offsets & select the size of the earlier operator (the one closer to
40 // the IR value).
41 uint64_t offset = first.getArguments()[0] + second.getArguments()[0];
42 uint64_t size = first.getArguments()[1];
43 OperatorT newOp = OperatorT::get(
44 first.getContext(), llvm::dwarf::DW_OP_LLVM_fragment, {offset, size});
45 return SmallVector<OperatorT>{newOp};
46}
47
48//===----------------------------------------------------------------------===//
49// Runner
50//===----------------------------------------------------------------------===//
51
54 rewriter.addPattern(std::make_unique<MergeFragments>());
55
56 AttrTypeReplacer replacer;
57 replacer.addReplacement([&rewriter](LLVM::DIExpressionAttr expr) {
58 return rewriter.simplify(expr);
59 });
61}
This is an attribute/type replacer that is naively cached.
std::deque< OperatorT >::const_iterator OpIterT
Rewriter for DIExpressionAttr.
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.
OpIterT match(OpIterRange operators) const override
Checks whether a particular prefix of operators matches this pattern.
SmallVector< OperatorT > replace(OpIterRange operators) const override
Replace the operators with a new list of operators.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
void recursivelyReplaceElementsIn(Operation *op, bool replaceAttrs=true, bool replaceLocs=false, bool replaceTypes=false)
Replace the elements within the given operation, and all nested operations.
void addReplacement(ReplaceFn< Attribute > fn)
AttrTypeReplacerBase.
void legalizeDIExpressionsRecursively(Operation *op)
Register all known legalization patterns declared here and apply them to all ops in op.
Include the generated interface declarations.