Implement a stubbed out "-lower-firrt-to-rtl" pass that just lowers constants for now.

This commit is contained in:
Chris Lattner 2020-05-30 16:16:30 -07:00
parent 77a501d62b
commit 8420dce471
7 changed files with 123 additions and 3 deletions

View File

@ -33,6 +33,9 @@ public:
/// value.
StringAttr getFIRRTLNameAttr(ArrayRef<NamedAttribute> attrs);
/// Register all of the FIRRTL transformation passes with the PassManager.
void registerFIRRTLPasses();
} // namespace firrtl
} // namespace cirt

View File

@ -0,0 +1,30 @@
//===- Passes.h - FIRRTL pass entry points ----------------------*- 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.
//
//===----------------------------------------------------------------------===//
#ifndef CIRT_DIALECT_FIRRTL_PASSES_H
#define CIRT_DIALECT_FIRRTL_PASSES_H
#include <memory>
namespace mlir {
class Pass;
} // namespace mlir
namespace cirt {
namespace firrtl {
std::unique_ptr<mlir::Pass> createLowerFIRRTLToRTLPass();
} // namespace firrtl
} // namespace cirt
#endif // CIRT_DIALECT_FIRRTL_PASSES_H

View File

@ -4,8 +4,8 @@
//
//===----------------------------------------------------------------------===//
#ifndef CIRT_DIALECT_FIRRTL_IR_TYPES_H
#define CIRT_DIALECT_FIRRTL_IR_TYPES_H
#ifndef CIRT_DIALECT_FIRRTL_TYPES_H
#define CIRT_DIALECT_FIRRTL_TYPES_H
#include "mlir/IR/Types.h"
@ -291,4 +291,4 @@ struct DenseMapInfo<cirt::firrtl::FIRRTLType> {
} // namespace llvm
#endif // CIRT_DIALECT_FIRRTL_IR_TYPES_H
#endif // CIRT_DIALECT_FIRRTL_TYPES_H

View File

@ -3,8 +3,19 @@
//===----------------------------------------------------------------------===//
#include "cirt/Dialect/FIRRTL/Dialect.h"
#include "cirt/Dialect/FIRRTL/Passes.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassRegistry.h"
using namespace cirt;
using namespace firrtl;
// Static initialization for FIRRTL dialect registration.
static mlir::DialectRegistration<FIRRTLDialect> FIRRTLOps;
/// Register all of the FIRRTL transformation passes with the PassManager.
void cirt::firrtl::registerFIRRTLPasses() {
using namespace mlir;
registerPass(
"lower-firrt-to-rtl", "Lower FIRRTL to RTL",
[]() -> std::unique_ptr<Pass> { return createLowerFIRRTLToRTLPass(); });
}

View File

@ -0,0 +1,63 @@
//===- LowerFIRRTLToRTL.cpp - Lower FIRRTL -> RTL dialect -----------------===//
//
//===----------------------------------------------------------------------===//
#include "cirt/Dialect/FIRRTL/Ops.h"
#include "cirt/Dialect/FIRRTL/Passes.h"
#include "cirt/Dialect/RTL/Ops.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
using namespace cirt;
using namespace firrtl;
using namespace rtl;
/// Utility class for operation conversions targeting that
/// match exactly one source operation.
template <typename OpTy>
class ConvertOpToRTLPattern : public ConversionPattern {
public:
ConvertOpToRTLPattern(MLIRContext *ctx)
: ConversionPattern(OpTy::getOperationName(), /*benefit*/ 1, ctx) {}
};
// Lower ConstantOp to ConstantOp.
struct LowerConstantOp : public ConvertOpToRTLPattern<firrtl::ConstantOp> {
using ConvertOpToRTLPattern::ConvertOpToRTLPattern;
LogicalResult
matchAndRewrite(Operation *op, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) const override {
auto cstOp = cast<firrtl::ConstantOp>(op);
rewriter.replaceOpWithNewOp<rtl::ConstantOp>(op, cstOp.value());
return success();
}
};
namespace {
class RTLConversionTarget : public ConversionTarget {
public:
explicit RTLConversionTarget(MLIRContext &ctx) : ConversionTarget(ctx) {
addLegalDialect<RTLDialect>();
this->addIllegalOp<firrtl::ConstantOp>();
}
};
} // end anonymous namespace
namespace {
struct FIRRTLLowering
: public PassWrapper<FIRRTLLowering, OperationPass<FModuleOp>> {
/// Run the dialect converter on the FModule.
void runOnOperation() override {
OwningRewritePatternList patterns;
patterns.insert<LowerConstantOp>(&getContext());
RTLConversionTarget target(getContext());
if (failed(applyPartialConversion(getOperation(), target, patterns)))
signalPassFailure();
}
};
} // end anonymous namespace
std::unique_ptr<mlir::Pass> cirt::firrtl::createLowerFIRRTLToRTLPass() {
return std::make_unique<FIRRTLLowering>();
}

View File

@ -0,0 +1,11 @@
// RUN: cirt-opt -lower-firrt-to-rtl %s | FileCheck %s
// CHECK-LABEL: firrtl.module @Constant
firrtl.module @Constant() {
// CHECK: rtl.constant(-4 : i4) : i4
%c12_ui4 = firrtl.constant(12 : ui4) : !firrtl.uint<4>
// CHECK: rtl.constant(2 : i3) : i3
%c2_si3 = firrtl.constant(2 : si3) : !firrtl.sint<3>
}

View File

@ -72,6 +72,8 @@ int main(int argc, char **argv) {
// Register our dialects.
registerDialect<firrtl::FIRRTLDialect>();
firrtl::registerFIRRTLPasses();
registerDialect<rtl::RTLDialect>();
PassPipelineCLParser passPipeline("", "Compiler passes to run");