From 44dcc358cd6f7f8518782a37babee9025f9fcd91 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 25 Apr 2021 16:47:25 -0700 Subject: [PATCH] Introduce a new -simple-canonicalizer pass, which is currently just a clone of Canonicalize. Canonicalize does a bunch of stuff that is slow and not useful for either the RTL or the firrtl dialects, because it is heavily focused on CFG-like things. We'll trim some of this fat out in our local clone, as the main MLIR canonicalizer is carrying some significant compatibility issues that make it difficult to improve. --- include/circt/CMakeLists.txt | 1 + include/circt/InitAllPasses.h | 2 + include/circt/Transforms/CMakeLists.txt | 4 ++ include/circt/Transforms/Passes.h | 37 +++++++++++++++++ include/circt/Transforms/Passes.td | 28 +++++++++++++ lib/CMakeLists.txt | 1 + lib/Transforms/CMakeLists.txt | 11 +++++ lib/Transforms/SimpleCanonicalizer.cpp | 50 +++++++++++++++++++++++ test/Dialect/FIRRTL/canonicalization.mlir | 2 +- tools/circt-opt/CMakeLists.txt | 1 + 10 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 include/circt/Transforms/CMakeLists.txt create mode 100644 include/circt/Transforms/Passes.h create mode 100644 include/circt/Transforms/Passes.td create mode 100644 lib/Transforms/CMakeLists.txt create mode 100644 lib/Transforms/SimpleCanonicalizer.cpp diff --git a/include/circt/CMakeLists.txt b/include/circt/CMakeLists.txt index 629c08af67..495310c662 100644 --- a/include/circt/CMakeLists.txt +++ b/include/circt/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(Conversion) add_subdirectory(Dialect) +add_subdirectory(Transforms) diff --git a/include/circt/InitAllPasses.h b/include/circt/InitAllPasses.h index 42b38c5445..ab523142ae 100644 --- a/include/circt/InitAllPasses.h +++ b/include/circt/InitAllPasses.h @@ -20,6 +20,7 @@ #include "circt/Dialect/LLHD/Transforms/Passes.h" #include "circt/Dialect/SV/SVPasses.h" #include "circt/Dialect/Seq/SeqDialect.h" +#include "circt/Transforms/Passes.h" namespace circt { @@ -33,6 +34,7 @@ inline void registerAllPasses() { llhd::initLLHDTransformationPasses(); seq::registerSeqPasses(); sv::registerPasses(); + circt::registerPasses(); } } // namespace circt diff --git a/include/circt/Transforms/CMakeLists.txt b/include/circt/Transforms/CMakeLists.txt new file mode 100644 index 0000000000..a661e77992 --- /dev/null +++ b/include/circt/Transforms/CMakeLists.txt @@ -0,0 +1,4 @@ +set(LLVM_TARGET_DEFINITIONS Passes.td) +mlir_tablegen(Passes.h.inc -gen-pass-decls) +add_public_tablegen_target(CIRCTTransformsIncGen) +add_circt_doc(Passes -gen-pass-doc Passes ./) diff --git a/include/circt/Transforms/Passes.h b/include/circt/Transforms/Passes.h new file mode 100644 index 0000000000..bd50e6e9cd --- /dev/null +++ b/include/circt/Transforms/Passes.h @@ -0,0 +1,37 @@ +//===- Passes.h - Generic CIRCT Passes --------------------------*- C++ -*-===// +// +// 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 header file defines prototypes that expose pass constructors. These +// passes are dialect independent. +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_TRANSFORMS_PASSES_H +#define CIRCT_TRANSFORMS_PASSES_H + +#include "mlir/Pass/Pass.h" + +#include + +namespace mlir { +class ModuleOp; +template +class OperationPass; +} // namespace mlir + +namespace circt { + +std::unique_ptr createSimpleCanonicalizerPass(); + +/// Generate the code for registering passes. +#define GEN_PASS_REGISTRATION +#include "circt/Transforms/Passes.h.inc" + +} // namespace circt + +#endif // CIRCT_TRANSFORMS_PASSES_H diff --git a/include/circt/Transforms/Passes.td b/include/circt/Transforms/Passes.td new file mode 100644 index 0000000000..7a4f3a1ac1 --- /dev/null +++ b/include/circt/Transforms/Passes.td @@ -0,0 +1,28 @@ +//===-- SVPasses.td - SV pass definition file --------------*- tablegen -*-===// +// +// 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 contains definitions for passes that work on the SV dialect. +// +//===----------------------------------------------------------------------===// + +#ifndef CIRCT_DIALECT_SV_SVPASSES +#define CIRCT_DIALECT_SV_SVPASSES + +include "mlir/Pass/PassBase.td" + +def SimpleCanonicalizer : Pass<"simple-canonicalizer"> { + let summary = "Run operation canonicalization patterns"; + let description = [{ + This is a lighter-weight version of the standard MLIR canonicalization + pass that doesn't do CFG optimizations and has other differences. + }]; + + let constructor = "circt::createSimpleCanonicalizerPass()"; +} + +#endif // CIRCT_DIALECT_SV_SVPASSES diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index cf07feb52a..869460d323 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -7,4 +7,5 @@ add_subdirectory(CAPI) add_subdirectory(Conversion) add_subdirectory(Dialect) add_subdirectory(Support) +add_subdirectory(Transforms) add_subdirectory(Translation) diff --git a/lib/Transforms/CMakeLists.txt b/lib/Transforms/CMakeLists.txt new file mode 100644 index 0000000000..573eea42e5 --- /dev/null +++ b/lib/Transforms/CMakeLists.txt @@ -0,0 +1,11 @@ +add_circt_library(CIRCTTransforms + SimpleCanonicalizer.cpp + + DEPENDS + CIRCTTransformsIncGen + + LINK_LIBS PUBLIC + MLIRIR + MLIRPass +) + diff --git a/lib/Transforms/SimpleCanonicalizer.cpp b/lib/Transforms/SimpleCanonicalizer.cpp new file mode 100644 index 0000000000..86fb332b75 --- /dev/null +++ b/lib/Transforms/SimpleCanonicalizer.cpp @@ -0,0 +1,50 @@ +//===- SimpleCanonicalizer.cpp - A simplified Canonicalizer pass ----------===// +// +// 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 simplified canonicalizer pass that doesn't do CFG +// optimizations and other things that aren't helpful for many hardware IRs. +// +//===----------------------------------------------------------------------===// + +#include "circt/Transforms/Passes.h" + +#include "circt/Support/LLVM.h" +#include "mlir/Pass/Pass.h" +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" + +using namespace circt; + +#define GEN_PASS_CLASSES +#include "circt/Transforms/Passes.h.inc" + +namespace { +/// Canonicalize operations in nested regions. +struct SimpleCanonicalizer + : public SimpleCanonicalizerBase { + /// Initialize the canonicalizer by building the set of patterns used during + /// execution. + LogicalResult initialize(MLIRContext *context) override { + RewritePatternSet owningPatterns(context); + for (auto *op : context->getRegisteredOperations()) + op->getCanonicalizationPatterns(owningPatterns, context); + patterns = std::move(owningPatterns); + return success(); + } + void runOnOperation() override { + (void)mlir::applyPatternsAndFoldGreedily(getOperation()->getRegions(), + patterns, 10); + } + + mlir::FrozenRewritePatternSet patterns; +}; +} // end anonymous namespace + +/// Create a Canonicalizer pass. +std::unique_ptr circt::createSimpleCanonicalizerPass() { + return std::make_unique(); +} diff --git a/test/Dialect/FIRRTL/canonicalization.mlir b/test/Dialect/FIRRTL/canonicalization.mlir index 7e20624774..fd050dab33 100644 --- a/test/Dialect/FIRRTL/canonicalization.mlir +++ b/test/Dialect/FIRRTL/canonicalization.mlir @@ -1,4 +1,4 @@ -// RUN: circt-opt -canonicalize %s | FileCheck %s +// RUN: circt-opt -simple-canonicalizer %s | FileCheck %s firrtl.circuit "And" { diff --git a/tools/circt-opt/CMakeLists.txt b/tools/circt-opt/CMakeLists.txt index fa962f246e..01fa78e4fe 100644 --- a/tools/circt-opt/CMakeLists.txt +++ b/tools/circt-opt/CMakeLists.txt @@ -27,6 +27,7 @@ target_link_libraries(circt-opt CIRCTStaticLogicOps CIRCTSV CIRCTSVTransforms + CIRCTTransforms MLIRParser MLIRSupport