1 //===- DIExpressionRewriter.h - 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 // 9 // A driver for running rewrite patterns on DIExpression operators. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef MLIR_DIALECT_LLVMIR_TRANSFORMS_DIEXPRESSIONREWRITER_H 14 #define MLIR_DIALECT_LLVMIR_TRANSFORMS_DIEXPRESSIONREWRITER_H 15 16 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 17 #include <deque> 18 19 namespace mlir { 20 namespace LLVM { 21 22 /// Rewriter for DIExpressionAttr. 23 /// 24 /// Users of this rewriter register their own rewrite patterns. Each pattern 25 /// matches on a contiguous range of LLVM DIExpressionElemAttrs, and can be 26 /// used to rewrite it into a new range of DIExpressionElemAttrs of any length. 27 class DIExpressionRewriter { 28 public: 29 using OperatorT = LLVM::DIExpressionElemAttr; 30 31 class ExprRewritePattern { 32 public: 33 using OperatorT = DIExpressionRewriter::OperatorT; 34 using OpIterT = std::deque<OperatorT>::const_iterator; 35 using OpIterRange = llvm::iterator_range<OpIterT>; 36 37 virtual ~ExprRewritePattern() = default; 38 /// Checks whether a particular prefix of operators matches this pattern. 39 /// The provided argument is guaranteed non-empty. 40 /// Return the iterator after the last matched element. 41 virtual OpIterT match(OpIterRange) const = 0; 42 /// Replace the operators with a new list of operators. 43 /// The provided argument is guaranteed to be the same length as returned 44 /// by the `match` function. 45 virtual SmallVector<OperatorT> replace(OpIterRange) const = 0; 46 }; 47 48 /// Register a rewrite pattern with the rewriter. 49 /// Rewrite patterns are attempted in the order of registration. 50 void addPattern(std::unique_ptr<ExprRewritePattern> pattern); 51 52 /// Simplify a DIExpression according to all the patterns registered. 53 /// An optional `maxNumRewrites` can be passed to limit the number of rewrites 54 /// that gets applied. 55 LLVM::DIExpressionAttr 56 simplify(LLVM::DIExpressionAttr expr, 57 std::optional<uint64_t> maxNumRewrites = {}) const; 58 59 private: 60 /// The registered patterns. 61 SmallVector<std::unique_ptr<ExprRewritePattern>> patterns; 62 }; 63 64 } // namespace LLVM 65 } // namespace mlir 66 67 #endif // MLIR_DIALECT_LLVMIR_TRANSFORMS_DIEXPRESSIONREWRITER_H 68