Add a simple common sub expression elimination pass.

The algorithm collects defining operations within a scoped hash table. The scopes within the hash table correspond to nodes within the dominance tree for a function. This cl only adds support for simple operations, i.e non side-effecting. Such operations, e.g. load/store/call, will be handled in later patches.

PiperOrigin-RevId: 223811328
This commit is contained in:
River Riddle 2018-12-03 09:45:35 -08:00 committed by jpienaar
parent 5858102ab1
commit 7669a259c4
3 changed files with 449 additions and 0 deletions

View File

@ -36,6 +36,9 @@ FunctionPass *createConstantFoldPass();
/// Creates an instance of the Canonicalizer pass.
FunctionPass *createCanonicalizerPass();
/// Creates a pass to perform common sub expression elimination.
FunctionPass *createCSEPass();
/// Creates a pass to vectorize loops, operations and data types using a
/// target-independent, n-D super-vector abstraction.
FunctionPass *createVectorizePass();

246
mlir/lib/Transforms/CSE.cpp Normal file
View File

@ -0,0 +1,246 @@
//===- CSE.cpp - Common Sub-expression Elimination ------------------------===//
//
// Copyright 2019 The MLIR Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================
//
// This transformation pass performs a simple common sub-expression elimination
// algorithm on operations within a function.
//
//===----------------------------------------------------------------------===//
#include "mlir/Analysis/Dominance.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/CFGFunction.h"
#include "mlir/IR/StmtVisitor.h"
#include "mlir/Pass.h"
#include "mlir/Support/Functional.h"
#include "mlir/Transforms/Passes.h"
#include "mlir/Transforms/Utils.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/ScopedHashTable.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/RecyclingAllocator.h"
#include <deque>
using namespace mlir;
namespace {
/// Simple common sub-expression elimination.
struct CSE : public FunctionPass {
CSE() : FunctionPass(&CSE::passID) {}
PassResult runOnCFGFunction(CFGFunction *f) override;
PassResult runOnMLFunction(MLFunction *f) override;
static char passID;
};
// TODO(riverriddle) Handle commutative operations.
struct SimpleOperationInfo : public llvm::DenseMapInfo<Operation *> {
static unsigned getHashValue(const Operation *op) {
// Hash the operations based upon their:
// - Operation Name
// - Attributes
// - Result Types
// - Operands
return hash_combine(
op->getName(), op->getAttrs(),
hash_combine_range(op->result_type_begin(), op->result_type_end()),
hash_combine_range(op->operand_begin(), op->operand_end()));
}
static bool isEqual(const Operation *lhs, const Operation *rhs) {
if (lhs == rhs)
return true;
if (lhs == getTombstoneKey() || lhs == getEmptyKey() ||
rhs == getTombstoneKey() || rhs == getEmptyKey())
return false;
// Compare the operation name.
if (lhs->getName() != rhs->getName())
return false;
// Check operand and result type counts.
if (lhs->getNumOperands() != rhs->getNumOperands() ||
lhs->getNumResults() != rhs->getNumResults())
return false;
// Compare attributes.
if (lhs->getAttrs() != rhs->getAttrs())
return false;
// Compare operands.
if (!std::equal(lhs->operand_begin(), lhs->operand_end(),
rhs->operand_begin()))
return false;
// Compare result types.
return std::equal(lhs->result_type_begin(), lhs->result_type_end(),
rhs->result_type_begin());
}
};
/// Shared implementation of operation elimination and scoped map definitions.
struct CSEImpl {
using AllocatorTy = llvm::RecyclingAllocator<
llvm::BumpPtrAllocator,
llvm::ScopedHashTableVal<Operation *, Operation *>>;
using ScopedMapTy = llvm::ScopedHashTable<Operation *, Operation *,
SimpleOperationInfo, AllocatorTy>;
/// Erase any operations that were marked as dead during simplification.
void eraseDeadOperations() {
for (auto *op : opsToErase)
op->erase();
}
/// Attempt to eliminate a redundant operation.
void simplifyOperation(Operation *op) {
// TODO(riverriddle) We currently only eliminate non side-effecting
// operations.
if (!op->hasNoSideEffect())
return;
// If the operation is already trivially dead just add it to the erase list.
if (op->use_empty()) {
opsToErase.push_back(op);
return;
}
// Look for an existing definition for the operation.
if (auto *existing = knownValues.lookup(op)) {
// If we find one then replace all uses of the current operation with the
// existing one and mark it for deletion.
for (unsigned i = 0, e = existing->getNumResults(); i != e; ++i)
op->getResult(i)->replaceAllUsesWith(existing->getResult(i));
opsToErase.push_back(op);
// If the existing operation has an unknown location and the current
// operation doesn't, then set the existing op's location to that of the
// current op.
if (existing->getLoc().isa<UnknownLoc>() &&
!op->getLoc().isa<UnknownLoc>()) {
existing->setLoc(op->getLoc());
}
} else {
// Otherwise, we add this operation to the known values map.
knownValues.insert(op, op);
}
}
/// A scoped hash table of defining operations within a function.
ScopedMapTy knownValues;
/// Operations marked as dead and to be erased.
std::vector<Operation *> opsToErase;
};
/// Common sub-expression elimination for CFG functions.
struct CFGCSE : public CSEImpl {
/// Represents a single entry in the depth first traversal of a CFG.
struct CFGStackNode {
CFGStackNode(ScopedMapTy &knownValues, DominanceInfoNode *node)
: scope(knownValues), node(node), childIterator(node->begin()),
processed(false) {}
/// Scope for the known values.
ScopedMapTy::ScopeTy scope;
DominanceInfoNode *node;
DominanceInfoNode::iterator childIterator;
/// If this node has been fully processed yet or not.
bool processed;
};
void run(CFGFunction *f) {
// Note, deque is being used here because there was significant performance
// gains over vector when the container becomes very large due to the
// specific access patterns. If/when these performance issues are no
// longer a problem we can change this to vector. For more information see
// the llvm mailing list discussion on this:
// http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20120116/135228.html
std::deque<std::unique_ptr<CFGStackNode>> stack;
// Process the nodes of the dom tree.
DominanceInfo domInfo(f);
stack.emplace_back(
std::make_unique<CFGStackNode>(knownValues, domInfo.getRootNode()));
while (!stack.empty()) {
auto &currentNode = stack.back();
// Check to see if we need to process this node.
if (!currentNode->processed) {
currentNode->processed = true;
simplifyBasicBlock(currentNode->node->getBlock());
// Otherwise, check to see if we need to process a child node.
} else if (currentNode->childIterator != currentNode->node->end()) {
auto *childNode = *(currentNode->childIterator++);
stack.emplace_back(
std::make_unique<CFGStackNode>(knownValues, childNode));
} else {
// Finally, if the node and all of its children have been processed
// then we delete the node.
stack.pop_back();
}
}
// Erase any operations marked as redundant.
eraseDeadOperations();
}
void simplifyBasicBlock(BasicBlock *bb) {
for (auto &i : *bb)
simplifyOperation(&i);
}
};
/// Common sub-expression elimination for ML functions.
struct MLCSE : public CSEImpl, StmtWalker<MLCSE> {
using StmtWalker<MLCSE>::walk;
void run(MLFunction *f) {
// Walk the function statements.
walk(f);
// Finally, erase any redundant operations.
eraseDeadOperations();
}
// Insert a scope for each statement range.
template <class Iterator> void walk(Iterator Start, Iterator End) {
ScopedMapTy::ScopeTy scope(knownValues);
StmtWalker<MLCSE>::walk(Start, End);
}
void visitOperationStmt(OperationStmt *stmt) { simplifyOperation(stmt); }
};
} // end anonymous namespace
char CSE::passID = 0;
PassResult CSE::runOnCFGFunction(CFGFunction *f) {
CFGCSE().run(f);
return success();
}
PassResult CSE::runOnMLFunction(MLFunction *f) {
MLCSE().run(f);
return success();
}
FunctionPass *mlir::createCSEPass() { return new CSE(); }
static PassRegistration<CSE>
pass("cse", "Eliminate common sub-expressions in functions");

View File

@ -0,0 +1,200 @@
// RUN: mlir-opt %s -cse | FileCheck %s
// CHECK-DAG: #map0 = (d0) -> (d0 mod 2, d0 mod 2)
#map0 = (d0) -> (d0 mod 2, d0 mod 2)
// CHECK-LABEL: @simple_constant_ml
mlfunc @simple_constant_ml() -> (i32, i32) {
// CHECK: %c1_i32 = constant 1 : i32
%0 = constant 1 : i32
%1 = constant 1 : i32
// CHECK-NEXT: return %c1_i32, %c1_i32 : i32, i32
return %0, %1 : i32, i32
}
// CHECK-LABEL: @simple_constant_cfg
cfgfunc @simple_constant_cfg() -> (i32, i32) {
bb0: // CHECK: bb0
// CHECK-NEXT: %c1_i32 = constant 1 : i32
%0 = constant 1 : i32
// CHECK-NEXT: return %c1_i32, %c1_i32 : i32, i32
%1 = constant 1 : i32
return %0, %1 : i32, i32
}
// CHECK-LABEL: @basic_ml
mlfunc @basic_ml() -> (index, index) {
// CHECK: %c0 = constant 0 : index
%c0 = constant 0 : index
%c1 = constant 0 : index
// CHECK-NEXT: %0 = affine_apply #map0(%c0)
%0 = affine_apply #map0(%c0)
%1 = affine_apply #map0(%c1)
// CHECK-NEXT: return %0#0, %0#0 : index, index
return %0, %1 : index, index
}
// CHECK-LABEL: @many_cfg
cfgfunc @many_cfg(f32, f32) -> (f32) {
bb0(%a : f32, %b : f32): // CHECK: bb0
// CHECK-NEXT: %0 = addf %arg0, %arg1 : f32
%c = addf %a, %b : f32
%d = addf %a, %b : f32
%e = addf %a, %b : f32
%f = addf %a, %b : f32
// CHECK-NEXT: %1 = addf %0, %0 : f32
%g = addf %c, %d : f32
%h = addf %e, %f : f32
%i = addf %c, %e : f32
// CHECK-NEXT: %2 = addf %1, %1 : f32
%j = addf %g, %h : f32
%k = addf %h, %i : f32
// CHECK-NEXT: %3 = addf %2, %2 : f32
%l = addf %j, %k : f32
// CHECK-NEXT: return %3 : f32
return %l : f32
}
/// Check that operations are not eliminated if they have different operands.
// CHECK-LABEL: @different_ops_ml
mlfunc @different_ops_ml() -> (i32, i32) {
// CHECK: %c0_i32 = constant 0 : i32
// CHECK: %c1_i32 = constant 1 : i32
%0 = constant 0 : i32
%1 = constant 1 : i32
// CHECK-NEXT: return %c0_i32, %c1_i32 : i32, i32
return %0, %1 : i32, i32
}
/// Check that operations are not eliminated if they have different result
/// types.
// CHECK-LABEL: @different_results_ml
mlfunc @different_results_ml(%arg0 : tensor<*xf32>) -> (tensor<?x?xf32>, tensor<4x?xf32>) {
// CHECK: %0 = tensor_cast %arg0 : tensor<*xf32> to tensor<?x?xf32>
// CHECK-NEXT: %1 = tensor_cast %arg0 : tensor<*xf32> to tensor<4x?xf32>
%0 = tensor_cast %arg0 : tensor<*xf32> to tensor<?x?xf32>
%1 = tensor_cast %arg0 : tensor<*xf32> to tensor<4x?xf32>
// CHECK-NEXT: return %0, %1 : tensor<?x?xf32>, tensor<4x?xf32>
return %0, %1 : tensor<?x?xf32>, tensor<4x?xf32>
}
/// Check that operations are not eliminated if they have different attributes.
// CHECK-LABEL: @different_attributes_cfg
cfgfunc @different_attributes_cfg(index, index) -> (i1, i1, i1) {
bb0(%a : index, %b : index): // CHECK: bb0
// CHECK: %0 = cmpi "slt", %arg0, %arg1 : index
%0 = cmpi "slt", %a, %b : index
// CHECK-NEXT: %1 = cmpi "ne", %arg0, %arg1 : index
/// Predicate 1 means inequality comparison.
%1 = cmpi "ne", %a, %b : index
%2 = "cmpi"(%a, %b) {predicate: 1} : (index, index) -> i1
// CHECK-NEXT: return %0, %1, %1 : i1, i1, i1
return %0, %1, %2 : i1, i1, i1
}
/// Check that operations with side effects are not eliminated.
// CHECK-LABEL: @side_effect_ml
mlfunc @side_effect_ml() -> (memref<2x1xf32>, memref<2x1xf32>) {
// CHECK: %0 = alloc() : memref<2x1xf32>
%0 = alloc() : memref<2x1xf32>
// CHECK-NEXT: %1 = alloc() : memref<2x1xf32>
%1 = alloc() : memref<2x1xf32>
// CHECK-NEXT: return %0, %1 : memref<2x1xf32>, memref<2x1xf32>
return %0, %1 : memref<2x1xf32>, memref<2x1xf32>
}
/// Check that operation definitions are properly propagated down the dominance
/// tree.
// CHECK-LABEL: @down_propagate_for_ml
mlfunc @down_propagate_for_ml() {
// CHECK: %c1_i32 = constant 1 : i32
%0 = constant 1 : i32
// CHECK-NEXT: for %i0 = 0 to 4 {
for %i = 0 to 4 {
// CHECK-NEXT: "foo"(%c1_i32, %c1_i32) : (i32, i32) -> ()
%1 = constant 1 : i32
"foo"(%0, %1) : (i32, i32) -> ()
}
return
}
cfgfunc @down_propagate_cfg() -> i32 {
bb0: // CHECK: bb0:
// CHECK-NEXT: %c1_i32 = constant 1 : i32
%0 = constant 1 : i32
// CHECK-NEXT: %true = constant 1 : i1
%cond = constant 1 : i1
// CHECK-NEXT: cond_br %true, bb1, bb2(%c1_i32 : i32)
cond_br %cond, bb1, bb2(%0 : i32)
bb1: // CHECK: bb1:
// CHECK-NEXT: br bb2(%c1_i32 : i32)
%1 = constant 1 : i32
br bb2(%1 : i32)
bb2(%arg : i32):
return %arg : i32
}
/// Check that operation definitions are NOT propagated up the dominance tree.
// CHECK-LABEL: @up_propagate_ml
mlfunc @up_propagate_ml() -> i32 {
// CHECK: for %i0 = 0 to 4 {
for %i = 0 to 4 {
// CHECK-NEXT: %c1_i32 = constant 1 : i32
// CHECK-NEXT: "foo"(%c1_i32) : (i32) -> ()
%0 = constant 1 : i32
"foo"(%0) : (i32) -> ()
}
// CHECK: %c1_i32_0 = constant 1 : i32
// CHECK-NEXT: return %c1_i32_0 : i32
%1 = constant 1 : i32
return %1 : i32
}
cfgfunc @up_propagate_cfg() -> i32 {
bb0: // CHECK: bb0:
// CHECK-NEXT: %c0_i32 = constant 0 : i32
%0 = constant 0 : i32
// CHECK-NEXT: %true = constant 1 : i1
%cond = constant 1 : i1
// CHECK-NEXT: cond_br %true, bb1, bb2(%c0_i32 : i32)
cond_br %cond, bb1, bb2(%0 : i32)
bb1: // CHECK: bb1:
// CHECK-NEXT: %c1_i32 = constant 1 : i32
%1 = constant 1 : i32
// CHECK-NEXT: br bb2(%c1_i32 : i32)
br bb2(%1 : i32)
bb2(%arg : i32): // CHECK: bb2
// CHECK-NEXT: %c1_i32_0 = constant 1 : i32
%2 = constant 1 : i32
// CHECK-NEXT: %1 = addi %0, %c1_i32_0 : i32
%add = addi %arg, %2 : i32
// CHECK-NEXT: return %1 : i32
return %add : i32
}