Skip to content

Commit

Permalink
[RTG] More convenient InstructionOpInterface methods (#8100)
Browse files Browse the repository at this point in the history
  • Loading branch information
maerhart authored Jan 31, 2025
1 parent c1ba95e commit 4d3c258
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
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

0 comments on commit 4d3c258

Please sign in to comment.