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

[RTG] More convenient InstructionOpInterface methods #8100

Merged
merged 1 commit into from
Jan 31, 2025
Merged
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/IR/RTGISAAssemblyInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,33 @@ def InstructionOpInterface : OpInterface<"InstructionOpInterface"> {
];
}

def InstructionOpAdaptorTrait : NativeOpTrait<"InstructionOpAdaptorTrait"> {
let extraConcreteClassDeclaration = [{
static void printInstructionBinary(llvm::raw_ostream &os,
FoldAdaptor adaptor);

static void printInstructionAssembly(llvm::raw_ostream &os,
FoldAdaptor adaptor);
}];

let extraConcreteClassDefinition = [{
void $cppClass::printInstructionBinary(
llvm::raw_ostream &os, llvm::ArrayRef<mlir::Attribute> operands) {
printInstructionBinary(os, FoldAdaptor(operands));
}

void $cppClass::printInstructionAssembly(
llvm::raw_ostream &os, llvm::ArrayRef<mlir::Attribute> operands) {
printInstructionAssembly(os, FoldAdaptor(operands));
}
}];

let cppNamespace = "::circt::rtg";
}

def InstructionOpAdaptor : TraitList<[
DeclareOpInterfaceMethods<InstructionOpInterface>,
InstructionOpAdaptorTrait,
]>;

#endif // CIRCT_DIALECT_RTG_IR_RTGISAASSEMBLYINTERFACES_TD
11 changes: 11 additions & 0 deletions include/circt/Dialect/RTG/IR/RTGISAAssemblyOpInterfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@

#include "circt/Dialect/RTG/IR/RTGISAAssemblyOpInterfaces.h.inc"

namespace circt {
namespace rtg {

template <typename ConcreteType>
class InstructionOpAdaptorTrait
: public mlir::OpTrait::TraitBase<ConcreteType, InstructionOpAdaptorTrait> {
};

} // namespace rtg
} // namespace circt

#endif // CIRCT_DIALECT_RTG_IR_RTGISAASSEMBLYOPINTERFACES_H
28 changes: 12 additions & 16 deletions include/circt/Dialect/RTGTest/IR/RTGTestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,17 @@ def ConstantTestOp : RTGTestOp<"constant_test", [
//===- Instruction Formats -------------------------------------------------===//

class InstFormatIOpBase<string mnemonic, int opcode7, int funct3>
: RTGTestOp<"rv32i." # mnemonic, [InstructionOpInterface]> {
: RTGTestOp<"rv32i." # mnemonic, [InstructionOpAdaptor]> {

let arguments = (ins IntegerRegisterType:$rd,
IntegerRegisterType:$rs,
Imm12Type:$imm);

let assemblyFormat = "$rd `,` $rs `,` $imm attr-dict";

let extraClassDeclaration = [{
static void printInstructionBinary(llvm::raw_ostream &os,
ArrayRef<Attribute> operands) {
FoldAdaptor adaptor(operands);

let extraClassDefinition = [{
void $cppClass::printInstructionBinary(llvm::raw_ostream &os,
FoldAdaptor adaptor) {
auto binary = APInt(12, cast<Imm12Attr>(adaptor.getImm()).getValue())
.concat(APInt(5, cast<rtg::RegisterAttrInterface>(
adaptor.getRs()).getClassIndex()))
Expand All @@ -93,10 +91,8 @@ class InstFormatIOpBase<string mnemonic, int opcode7, int funct3>
os << str;
}

static void printInstructionAssembly(llvm::raw_ostream &os,
ArrayRef<Attribute> operands) {
FoldAdaptor adaptor(operands);

void $cppClass::printInstructionAssembly(llvm::raw_ostream &os,
FoldAdaptor adaptor) {
os << getOperationName().rsplit('.').second << " "
<< cast<rtg::RegisterAttrInterface>(adaptor.getRd())
.getRegisterAssembly()
Expand All @@ -111,13 +107,13 @@ class InstFormatIOpBase<string mnemonic, int opcode7, int funct3>
}

class InstFormatIImmOpBase<string mnemonic, int opcode7, int funct12>
: RTGTestOp<"rv32i." # mnemonic, [InstructionOpInterface]> {
: RTGTestOp<"rv32i." # mnemonic, [InstructionOpAdaptor]> {

let assemblyFormat = "attr-dict";

let extraClassDeclaration = [{
static void printInstructionBinary(llvm::raw_ostream &os,
ArrayRef<Attribute> operands) {
let extraClassDefinition = [{
void $cppClass::printInstructionBinary(llvm::raw_ostream &os,
FoldAdaptor adaptor) {
auto binary = APInt(12, }] # funct12 # [{)
.concat(APInt(13, 0))
.concat(llvm::APInt(7, }] # opcode7 # [{));
Expand All @@ -127,8 +123,8 @@ class InstFormatIImmOpBase<string mnemonic, int opcode7, int funct12>
os << str;
}

static void printInstructionAssembly(llvm::raw_ostream &os,
ArrayRef<Attribute> operands) {
void $cppClass::printInstructionAssembly(llvm::raw_ostream &os,
FoldAdaptor adaptor) {
os << getOperationName().rsplit('.').second;
}
}];
Expand Down