Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTGTest] Add dialect materializer #8063

Open
wants to merge 1 commit into
base: maerhart-rtg-register-allocation-pass
Choose a base branch
from
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
1 change: 1 addition & 0 deletions include/circt/Dialect/RTGTest/IR/RTGTestDialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef CIRCT_DIALECT_RTGTEST_IR_RTGTESTDIALECT_H
#define CIRCT_DIALECT_RTGTEST_IR_RTGTESTDIALECT_H

#include "circt/Dialect/RTG/IR/RTGDialect.h"
#include "circt/Support/LLVM.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Dialect.h"
Expand Down
5 changes: 5 additions & 0 deletions include/circt/Dialect/RTGTest/IR/RTGTestDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def RTGTestDialect : Dialect {

let useDefaultTypePrinterParser = 1;
let useDefaultAttributePrinterParser = 1;

let hasConstantMaterializer = 1;

let dependentDialects = ["::circt::rtg::RTGDialect"];

let cppNamespace = "::circt::rtgtest";

let extraClassDeclaration = [{
Expand Down
34 changes: 34 additions & 0 deletions lib/Dialect/RTGTest/IR/RTGTestDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "circt/Dialect/RTGTest/IR/RTGTestDialect.h"
#include "circt/Dialect/RTG/IR/RTGOps.h"
#include "circt/Dialect/RTGTest/IR/RTGTestOps.h"
#include "circt/Dialect/RTGTest/IR/RTGTestTypes.h"
#include "mlir/IR/Builders.h"
Expand All @@ -34,6 +35,39 @@ void RTGTestDialect::initialize() {
>();
}

/// Registered hook to materialize a single constant operation from a given
/// attribute value with the desired resultant type. This method should use
/// the provided builder to create the operation without changing the
/// insertion position. The generated operation is expected to be constant
/// like, i.e. single result, zero operands, non side-effecting, etc. On
/// success, this hook should return the value generated to represent the
/// constant value. Otherwise, it should return null on failure.
Operation *RTGTestDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
Location loc) {
if (auto attr = dyn_cast<CPUAttr>(value))
if (isa<CPUType>(type))
return builder.create<CPUDeclOp>(loc, type, attr);

if (auto attr = dyn_cast<rtg::RegisterAttrInterface>(value))
if (isa<rtg::RegisterTypeInterface>(type))
return builder.create<rtg::FixedRegisterOp>(loc, attr);

if (auto attr = dyn_cast<Imm12Attr>(value))
if (isa<Imm12Type>(type))
return builder.create<ImmediateOp>(loc, attr);

if (auto attr = dyn_cast<Imm21Attr>(value))
if (isa<Imm21Type>(type))
return builder.create<ImmediateOp>(loc, attr);

if (auto attr = dyn_cast<Imm32Attr>(value))
if (isa<Imm32Type>(type))
return builder.create<ImmediateOp>(loc, attr);

return nullptr;
}

#include "circt/Dialect/RTGTest/IR/RTGTestEnums.cpp.inc"

#include "circt/Dialect/RTGTest/IR/RTGTestDialect.cpp.inc"
1 change: 1 addition & 0 deletions unittests/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ add_subdirectory(FIRRTL)
add_subdirectory(ESI)
add_subdirectory(HW)
add_subdirectory(OM)
add_subdirectory(RTGTest)
add_subdirectory(SMT)
9 changes: 9 additions & 0 deletions unittests/Dialect/RTGTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_circt_unittest(CIRCTRTGTestTests
MaterializerTest.cpp
)

target_link_libraries(CIRCTRTGTestTests
PRIVATE
CIRCTRTGTestDialect
MLIRIR
)
71 changes: 71 additions & 0 deletions unittests/Dialect/RTGTest/MaterializerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//===- MaterializerTest.cpp - RTGTest Dialect Materializer unit tests -----===//
//
// 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/RTGTest/IR/RTGTestAttributes.h"
#include "circt/Dialect/RTGTest/IR/RTGTestDialect.h"
#include "circt/Dialect/RTGTest/IR/RTGTestOps.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "gtest/gtest.h"

using namespace mlir;
using namespace circt;
using namespace rtgtest;

namespace {

TEST(MaterializerTest, CPUAttr) {
MLIRContext context;
context.loadDialect<RTGTestDialect>();
Location loc(UnknownLoc::get(&context));
auto moduleOp = ModuleOp::create(loc);
OpBuilder builder = OpBuilder::atBlockBegin(moduleOp.getBody());

auto attr = CPUAttr::get(&context, 0);
auto *op = context.getLoadedDialect<RTGTestDialect>()->materializeConstant(
builder, attr, attr.getType(), loc);
ASSERT_TRUE(op && isa<CPUDeclOp>(op));
}

TEST(MaterializerTest, ImmediateAttr) {
MLIRContext context;
context.loadDialect<RTGTestDialect>();
Location loc(UnknownLoc::get(&context));
auto moduleOp = ModuleOp::create(loc);
OpBuilder builder = OpBuilder::atBlockBegin(moduleOp.getBody());

auto attr12 = Imm12Attr::get(&context, 0);
auto attr21 = Imm32Attr::get(&context, 0);
auto attr32 = Imm21Attr::get(&context, 0);

auto *op12 = context.getLoadedDialect<RTGTestDialect>()->materializeConstant(
builder, attr12, attr12.getType(), loc);
auto *op21 = context.getLoadedDialect<RTGTestDialect>()->materializeConstant(
builder, attr21, attr21.getType(), loc);
auto *op32 = context.getLoadedDialect<RTGTestDialect>()->materializeConstant(
builder, attr32, attr32.getType(), loc);

ASSERT_TRUE(op12 && isa<ImmediateOp>(op12));
ASSERT_TRUE(op21 && isa<ImmediateOp>(op21));
ASSERT_TRUE(op32 && isa<ImmediateOp>(op32));
}

TEST(MaterializerTest, RegisterAttr) {
MLIRContext context;
context.loadDialect<RTGTestDialect>();
Location loc(UnknownLoc::get(&context));
auto moduleOp = ModuleOp::create(loc);
OpBuilder builder = OpBuilder::atBlockBegin(moduleOp.getBody());

auto attr = RegZeroAttr::get(&context);
auto *op = context.getLoadedDialect<RTGTestDialect>()->materializeConstant(
builder, attr, attr.getType(), loc);
ASSERT_TRUE(op && isa<rtg::FixedRegisterOp>(op));
}

} // namespace
Loading