brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · 23b4130 Raw
74 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "mlir/Dialect/LLVMIR/Transforms/DIExpressionRewriter.h"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// DIExpressionRewriter19//===----------------------------------------------------------------------===//20 21void DIExpressionRewriter::addPattern(22    std::unique_ptr<ExprRewritePattern> pattern) {23  patterns.emplace_back(std::move(pattern));24}25 26DIExpressionAttr27DIExpressionRewriter::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 some34  // application of the rewrite patterns.35  // Using a deque for inputs so that we have efficient front insertion and36  // removal. Random access is not necessary for patterns.37  std::deque<OperatorT> inputs(operators.begin(), operators.end());38  SmallVector<OperatorT> result;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;50      SmallVector<OperatorT> replacement =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}74