Commit 57540c96 authored by River Riddle's avatar River Riddle
Browse files

[mlir] Replace toy::DeadFunctionEliminationPass with symbolDCEPass.

Summary:
The dead function elimination pass in toy was a temporary stopgap until we had proper dead function elimination support in MLIR. Now that this functionality is available, this pass is no longer necessary.

Differential Revision: https://reviews.llvm.org/D72483
parent f4261e11
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@ add_toy_chapter(toyc-ch4
  parser/AST.cpp
  mlir/MLIRGen.cpp
  mlir/Dialect.cpp
  mlir/DeadFunctionEliminationPass.cpp
  mlir/ShapeInferencePass.cpp
  mlir/ToyCombine.cpp
  )
+0 −1
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ class Pass;

namespace toy {
std::unique_ptr<Pass> createShapeInferencePass();
std::unique_ptr<Pass> createDeadFunctionEliminationPass();
} // end namespace toy
} // end namespace mlir

+0 −59
Original line number Diff line number Diff line
//===- DeadFunctionEliminationPass.cpp - Eliminate inlined functions ------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements a Module level pass performing dead function
// elimination. This is required as a post-processing step after function
// inlining.
//
//===----------------------------------------------------------------------===//

#include "mlir/Analysis/Verifier.h"
#include "mlir/IR/BlockAndValueMapping.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/StandardTypes.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Support/LogicalResult.h"
#include "toy/Passes.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>

namespace {
/// This is a simple function DCE pass that deletes all non-main functions after
/// inlining.
/// TODO(riverriddle) This is only necessary because MLIR currently does not
/// have generic DCE support for functions.
class DeadFunctionEliminationPass
    : public mlir::ModulePass<DeadFunctionEliminationPass> {
public:
  void runOnModule() override {
    mlir::ModuleOp module = getModule();
    mlir::SymbolTable moduleSymTable(module);

    // Eliminate non-main functions.
    auto mainFn = moduleSymTable.lookup<mlir::FuncOp>("main");
    for (mlir::FuncOp func :
         llvm::make_early_inc_range(module.getOps<mlir::FuncOp>())) {
      if (func != mainFn)
        func.erase();
    }
  }
};
} // end anonymous namespace

/// Create a pass that eliminates inlined functions in toy.
std::unique_ptr<mlir::Pass> mlir::toy::createDeadFunctionEliminationPass() {
  return std::make_unique<DeadFunctionEliminationPass>();
}
+4 −0
Original line number Diff line number Diff line
@@ -170,6 +170,10 @@ private:
                                               getType(VarType{})));
    }

    // If this function isn't main, then set the visibility to private.
    if (funcAST.getProto()->getName() != "main")
      function.setVisibility(mlir::FuncOp::Visibility::Private);

    return function;
  }

+1 −1
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ int dumpMLIR() {

    // Inline all functions into main and then delete them.
    pm.addPass(mlir::createInlinerPass());
    pm.addPass(mlir::toy::createDeadFunctionEliminationPass());
    pm.addPass(mlir::createSymbolDCEPass());

    // Now that there is only one function, we can infer the shapes of each of
    // the operations.
Loading