Skip to content

Commit

Permalink
[OM] Extract interfaces for ClassLike ops and ClassFieldLike ops, NFC. (
Browse files Browse the repository at this point in the history
#5595)

This adds interfaces around the existing ClassOp and ClassFieldOp. The
interfaces are just abstracting over generated methods for the
ClassOp. For the ClassFieldOp, the interface just has one method to
get a type, which comes from the field's value.

By adding these interfaces and using them for parsing, printing,
verifying, etc., it will be simple to add new kinds of ClassLike
operations with fields, that can share functionality with the original
ClassOp. One immediate use case is a class declaration with only types
in its fields, suitable for use to declare external classes.
  • Loading branch information
mikeurbach authored Jul 17, 2023
1 parent cceb170 commit 54839d3
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 71 deletions.
1 change: 1 addition & 0 deletions include/circt/Dialect/OM/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ mlir_tablegen(OMAttributes.h.inc -gen-attrdef-decls)
mlir_tablegen(OMAttributes.cpp.inc -gen-attrdef-defs)
add_public_tablegen_target(MLIROMAttrIncGen)
add_dependencies(circt-headers MLIROMAttrIncGen)
add_circt_interface(OMOpInterfaces)
20 changes: 20 additions & 0 deletions include/circt/Dialect/OM/OMOpInterfaces.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===- OMOpInterfaces.h - Object Model operation interfaces ---------------===//
//
// 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 the Object Model operation declarations.
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_OM_OMOPINTERFACES_H
#define CIRCT_DIALECT_OM_OMOPINTERFACES_H

#include "mlir/IR/OpDefinition.h"

#include "circt/Dialect/OM/OMOpInterfaces.h.inc"

#endif // CIRCT_DIALECT_OM_OMOPINTERFACES_H
57 changes: 57 additions & 0 deletions include/circt/Dialect/OM/OMOpInterfaces.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===- OMOpInterfaces.td - Object Model dialect op interfaces -------------===//
//
// 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 contains the Object Model dialect operation interfaces.
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_OM_OMOPINTERFACES_TD
#define CIRCT_DIALECT_OM_OMOPINTERFACES_TD

include "mlir/IR/OpBase.td"

def ClassLike : OpInterface<"ClassLike"> {
let cppNamespace = "circt::om";

let description = [{
Common functionality for class-like operations.
}];

let methods = [
InterfaceMethod<"Get the class-like symbol name",
"llvm::StringRef", "getSymName", (ins)>,
InterfaceMethod<"Get the class-like symbol name attribute",
"mlir::StringAttr", "getSymNameAttr", (ins)>,
InterfaceMethod<"Get the class-like symbol name attribute name",
"mlir::StringAttr", "getSymNameAttrName", (ins)>,
InterfaceMethod<"Get the class-like formal parameter names attribute",
"mlir::ArrayAttr", "getFormalParamNames", (ins)>,
InterfaceMethod<"Get the class-like formal parameter names attribute name",
"mlir::StringAttr", "getFormalParamNamesAttrName", (ins)>,
InterfaceMethod<"Get the class-like body region",
"mlir::Region &", "getBody", (ins)>,
InterfaceMethod<"Get the class-like body block",
"mlir::Block *", "getBodyBlock", (ins),
/*methodBody=*/[{ return $_op.getBodyBlock(); }]>
];
}

def ClassFieldLike : OpInterface<"ClassFieldLike"> {
let cppNamespace = "circt::om";

let description = [{
Common functionality for class-like field operations.
}];

let methods = [
InterfaceMethod<"Get the class-like field's type",
"mlir::Type", "getType", (ins)>
];
}

#endif // CIRCT_DIALECT_OM_OMOPINTERFACES_TD
1 change: 1 addition & 0 deletions include/circt/Dialect/OM/OMOps.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef CIRCT_DIALECT_OM_OMOPS_H
#define CIRCT_DIALECT_OM_OMOPS_H

#include "circt/Dialect/OM/OMOpInterfaces.h"
#include "circt/Dialect/OM/OMTypes.h"

#include "mlir/IR/BuiltinOps.h"
Expand Down
31 changes: 23 additions & 8 deletions include/circt/Dialect/OM/OMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef CIRCT_DIALECT_OM_OMOPS_TD
#define CIRCT_DIALECT_OM_OMOPS_TD

include "circt/Dialect/OM/OMOpInterfaces.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/BuiltinAttributeInterfaces.td"
include "mlir/IR/OpAsmInterface.td"
Expand All @@ -22,14 +23,16 @@ class OMOp<string mnemonic, list<Trait> traits = []> :
Op<OMDialect, mnemonic, traits>;

//===----------------------------------------------------------------------===//
// Class definitions
// Shared definitions
//===----------------------------------------------------------------------===//

def ClassOp : OMOp<"class",
[SingleBlock, NoTerminator, Symbol, SymbolTable,
HasParent<"mlir::ModuleOp">,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmBlockArgumentNames"]>
]> {
class OMClassLike<string mnemonic, list<Trait> traits = []> :
OMOp<mnemonic, traits # [
SingleBlock, NoTerminator, Symbol, SymbolTable,
HasParent<"mlir::ModuleOp">,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmBlockArgumentNames"]>,
DeclareOpInterfaceMethods<ClassLike>]> {

let arguments = (ins
SymbolNameAttr:$sym_name,
StrArrayAttr:$formalParamNames
Expand All @@ -48,7 +51,19 @@ def ClassOp : OMOp<"class",
let hasCustomAssemblyFormat = 1;

let hasVerifier = 1;
}

class OMClassFieldLike<string mnemonic, list<Trait> traits = []> :
OMOp<mnemonic, traits # [
Symbol,
DeclareOpInterfaceMethods<ClassFieldLike>]> {
}

//===----------------------------------------------------------------------===//
// Class definitions
//===----------------------------------------------------------------------===//

def ClassOp : OMClassLike<"class"> {
let extraClassDeclaration = [{
mlir::Block *getBodyBlock() { return &getBody().front(); }
// This builds a ClassOp, and populates it with the CLassFieldOps.
Expand All @@ -62,8 +77,8 @@ def ClassOp : OMOp<"class",
}];
}

def ClassFieldOp : OMOp<"class.field",
[HasParent<"ClassOp">, Symbol]> {
def ClassFieldOp : OMClassFieldLike<"class.field",
[HasParent<"ClassOp">]> {
let arguments = (ins
SymbolNameAttr:$sym_name,
AnyType:$value
Expand Down
2 changes: 1 addition & 1 deletion lib/Conversion/ExportVerilog/ExportVerilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5726,7 +5726,7 @@ void SharedEmitterState::gatherFiles(bool separateModules) {
.Case<MacroDeclOp>([&](auto op) {
symbolCache.addDefinition(op.getSymNameAttr(), op);
})
.Case<om::ClassOp>([&](auto op) {
.Case<om::ClassLike>([&](auto op) {
symbolCache.addDefinition(op.getSymNameAttr(), op);
})
.Case<om::ConstantOp>([&](auto op) {
Expand Down
1 change: 1 addition & 0 deletions lib/Dialect/FIRRTL/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ add_circt_dialect_library(CIRCTFIRRTLTransforms
CIRCTFIRRTLTransformsIncGen
MLIROMIncGen
MLIROMAttrIncGen
MLIROMOpInterfacesIncGen

LINK_LIBS PUBLIC
CIRCTFIRRTL
Expand Down
2 changes: 2 additions & 0 deletions lib/Dialect/OM/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ add_circt_dialect_library(

OMAttributes.cpp
OMDialect.cpp
OMOpInterfaces.cpp
OMOps.cpp
OMTypes.cpp

DEPENDS
MLIROMIncGen
MLIROMAttrIncGen
MLIROMOpInterfacesIncGen

LINK_COMPONENTS
Support
Expand Down
1 change: 1 addition & 0 deletions lib/Dialect/OM/Evaluator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_circt_library(CIRCTOMEvaluator
DEPENDS
MLIROMIncGen
MLIROMAttrIncGen
MLIROMOpInterfacesIncGen

LINK_LIBS
CIRCTOM
Expand Down
15 changes: 15 additions & 0 deletions lib/Dialect/OM/OMOpInterfaces.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===- OMOpInterfacess.cpp - Object Model operation interface definitions -===//
//
// 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 the Object Model operation interface definitions.
//
//===----------------------------------------------------------------------===//

#include "circt/Dialect/OM/OMOpInterfaces.h"

#include "circt/Dialect/OM/OMOpInterfaces.cpp.inc"
Loading

0 comments on commit 54839d3

Please sign in to comment.