Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions include/circt/Dialect/RTG/Reductions/RTGReductions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_RTG_REDUCTIONS_RTGREDUCTIONS_H
#define CIRCT_DIALECT_RTG_REDUCTIONS_RTGREDUCTIONS_H

#include "circt/Reduce/Reduction.h"

namespace circt {
namespace rtg {

/// A dialect interface to provide reduction patterns to a reducer tool.
struct RTGReducePatternDialectInterface : public ReducePatternDialectInterface {
using ReducePatternDialectInterface::ReducePatternDialectInterface;
void populateReducePatterns(circt::ReducePatternSet &patterns) const override;
};

/// Register the RTG Reduction pattern dialect interface to the given registry.
void registerReducePatternDialectInterface(mlir::DialectRegistry &registry);

} // namespace rtg
} // namespace circt

#endif // CIRCT_DIALECT_RTG_REDUCTIONS_RTGREDUCTIONS_H
1 change: 1 addition & 0 deletions lib/Dialect/RTG/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(IR)
add_subdirectory(Reductions)
add_subdirectory(Transforms)
8 changes: 8 additions & 0 deletions lib/Dialect/RTG/Reductions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_circt_library(CIRCTRTGReductions
RTGReductions.cpp

LINK_LIBS PUBLIC
CIRCTReduceLib
CIRCTRTGDialect
MLIRIR
)
58 changes: 58 additions & 0 deletions lib/Dialect/RTG/Reductions/RTGReductions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "circt/Dialect/RTG/Reductions/RTGReductions.h"
#include "circt/Dialect/RTG/IR/RTGOps.h"

using namespace mlir;
using namespace circt;
using namespace rtg;

//===----------------------------------------------------------------------===//
// Reduction patterns
//===----------------------------------------------------------------------===//

/// Replace virtual registers with a constant register.
struct VirtualRegisterConstantifier : public OpReduction<VirtualRegisterOp> {
LogicalResult rewrite(VirtualRegisterOp op) override {
if (op.getAllowedRegs().getAllowedRegs().empty())
return failure();

OpBuilder builder(op);
auto constReg = ConstantOp::create(builder, op.getLoc(),
op.getAllowedRegs().getAllowedRegs()[0]);
op.getResult().replaceAllUsesWith(constReg);
op.erase();
return success();
}

std::string getName() const override {
return "rtg-virtual-register-constantifier";
}
};
Comment on lines +20 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this go into an anonymous namespace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, good catch!


//===----------------------------------------------------------------------===//
// Reduction Registration
//===----------------------------------------------------------------------===//

void RTGReducePatternDialectInterface::populateReducePatterns(
circt::ReducePatternSet &patterns) const {
// Gather a list of reduction patterns that we should try. Ideally these are
// assigned reasonable benefit indicators (higher benefit patterns are
// prioritized). For example, things that can knock out entire modules while
// being cheap should be tried first (and thus have higher benefit), before
// trying to tweak operands of individual arithmetic ops.
patterns.add<VirtualRegisterConstantifier, 2>();
}

void rtg::registerReducePatternDialectInterface(
mlir::DialectRegistry &registry) {
registry.addExtension(+[](MLIRContext *ctx, RTGDialect *dialect) {
dialect->addInterfaces<RTGReducePatternDialectInterface>();
});
}
11 changes: 11 additions & 0 deletions test/Dialect/RTG/Reduction/virtual-register-constantifier.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// UNSUPPORTED: system-windows
// See https://github.com/llvm/circt/issues/4129
// RUN: circt-reduce %s --test /usr/bin/env --test-arg grep --test-arg -q --test-arg "rtg.test @test" --keep-best=0 --include rtg-virtual-register-constantifier | FileCheck %s

// CHECK-LABEL: rtg.test @test
rtg.test @test() {
// CHECK-NEXT: [[V0:%.+]] = rtg.constant #rtgtest.t0 : !rtgtest.ireg
// CHECK-NEXT: rtgtest.rv32i.add [[V0]], [[V0]], [[V0]]
%reg = rtg.virtual_reg [#rtgtest.t0, #rtgtest.t1, #rtgtest.t2]
rtgtest.rv32i.add %reg, %reg, %reg
}
1 change: 1 addition & 0 deletions tools/circt-reduce/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(LIBS
CIRCTEmitReductions
CIRCTHWReductions
CIRCTFIRRTLReductions
CIRCTRTGReductions
CIRCTReduceLib
MLIRIR
MLIRBytecodeWriter
Expand Down
2 changes: 2 additions & 0 deletions tools/circt-reduce/circt-reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "circt/Dialect/FIRRTL/FIRRTLReductions.h"
#include "circt/Dialect/HW/HWDialect.h"
#include "circt/Dialect/HW/HWReductions.h"
#include "circt/Dialect/RTG/Reductions/RTGReductions.h"
#include "circt/InitAllDialects.h"
#include "circt/Reduce/GenericReductions.h"
#include "circt/Reduce/Tester.h"
Expand Down Expand Up @@ -531,6 +532,7 @@ int main(int argc, char **argv) {
emit::registerReducePatternDialectInterface(registry);
firrtl::registerReducePatternDialectInterface(registry);
hw::registerReducePatternDialectInterface(registry);
rtg::registerReducePatternDialectInterface(registry);

// Create a context.
mlir::MLIRContext context(registry);
Expand Down