From af423b83f1b73dd97dc182fef1fa2787ac6691aa Mon Sep 17 00:00:00 2001 From: Martin Erhart Date: Sun, 6 Apr 2025 12:02:57 +0100 Subject: [PATCH] [RTG] Add comment op --- frontends/PyRTG/src/pyrtg/__init__.py | 2 +- frontends/PyRTG/src/pyrtg/tests.py | 8 ++++++++ frontends/PyRTG/test/basic.py | 10 +++++++++- include/circt/Dialect/RTG/IR/RTGOps.td | 10 ++++++++++ include/circt/Dialect/RTG/IR/RTGVisitors.h | 5 ++++- lib/Dialect/RTG/Transforms/ElaborationPass.cpp | 2 ++ .../RTG/Transforms/EmitRTGISAAssemblyPass.cpp | 18 ++++++++++++------ test/Dialect/RTG/IR/basic.mlir | 3 +++ test/Dialect/RTG/Transform/elaboration.mlir | 6 ++++++ .../RTG/Transform/emit-rtg-isa-assembly.mlir | 4 ++++ 10 files changed, 59 insertions(+), 9 deletions(-) diff --git a/frontends/PyRTG/src/pyrtg/__init__.py b/frontends/PyRTG/src/pyrtg/__init__.py index fcf06290e1c5..042bebef68ac 100644 --- a/frontends/PyRTG/src/pyrtg/__init__.py +++ b/frontends/PyRTG/src/pyrtg/__init__.py @@ -4,7 +4,7 @@ from . import tests from . import core -from .tests import test +from .tests import test, embed_comment from .labels import Label from .rtg import rtg from .rtgtest import rtgtest diff --git a/frontends/PyRTG/src/pyrtg/tests.py b/frontends/PyRTG/src/pyrtg/tests.py index 3a1f64d79e93..83a5161d7395 100644 --- a/frontends/PyRTG/src/pyrtg/tests.py +++ b/frontends/PyRTG/src/pyrtg/tests.py @@ -44,3 +44,11 @@ def wrapper(func): return Test(func, list(args)) return wrapper + + +def embed_comment(comment: str) -> None: + """ + Embeds a comment in the instruction stream. + """ + + rtg.CommentOp(comment) diff --git a/frontends/PyRTG/test/basic.py b/frontends/PyRTG/test/basic.py index e433d1f791f0..c7ba24f8c6b4 100644 --- a/frontends/PyRTG/test/basic.py +++ b/frontends/PyRTG/test/basic.py @@ -2,7 +2,7 @@ # RUN: %rtgtool% %s --seed=0 --output-format=elaborated | FileCheck %s --check-prefix=ELABORATED # RUN: %rtgtool% %s --seed=0 -o %t --output-format=asm && FileCheck %s --input-file=%t --check-prefix=ASM -from pyrtg import test, sequence, target, entry, rtg, Label, Set, Integer, Bag, rtgtest, Immediate, IntegerRegister, Array, Bool, MemoryBlock, Memory, Tuple +from pyrtg import test, sequence, target, entry, rtg, Label, Set, Integer, Bag, rtgtest, Immediate, IntegerRegister, Array, Bool, MemoryBlock, Memory, Tuple, embed_comment # MLIR-LABEL: rtg.target @Singleton : !rtg.dict<> # MLIR-NEXT: } @@ -225,6 +225,8 @@ def test1_args(set: Set): # MLIR-NEXT: [[RAND4:%.+]] = rtg.randomize_sequence [[SEQ1]] # MLIR-NEXT: rtg.embed_sequence [[RAND4]] +# MLIR-NEXT: rtg.comment "this is a comment" + # MLIR-NEXT: } # ELABORATED-LABEL: rtg.test @test2_labels @@ -253,6 +255,8 @@ def test1_args(set: Set): # ELABORATED-NEXT: [[L5:%.+]] = rtg.label_decl "s1" # ELABORATED-NEXT: rtg.label local [[L5]] +# ELABORATED-NEXT: rtg.comment "this is a comment" + # ELABORATED-NEXT: } # ASM-LABEL: Begin of test2_labels @@ -277,6 +281,8 @@ def test1_args(set: Set): # ASM-NEXT: s1: +# ASM-NEXT: # this is a comment + # ASM-EMPTY: # ASM: End of test2_labels @@ -326,6 +332,8 @@ def test2_labels(): seq1() + embed_comment("this is a comment") + # MLIR-LABEL: rtg.test @test3_registers_and_immediates() # MLIR-NEXT: [[IMM32:%.+]] = rtg.constant #rtg.isa.immediate<32, 32> diff --git a/include/circt/Dialect/RTG/IR/RTGOps.td b/include/circt/Dialect/RTG/IR/RTGOps.td index 5f9fc894ddd0..a71eb74fa0f1 100644 --- a/include/circt/Dialect/RTG/IR/RTGOps.td +++ b/include/circt/Dialect/RTG/IR/RTGOps.td @@ -641,6 +641,16 @@ def RandomNumberInRangeOp : RTGOp<"random_number_in_range", []> { let assemblyFormat = "` ` `[` $lowerBound `,` $upperBound `)` attr-dict"; } +//===- Misc Operations ----------------------------------------------------===// + +def CommentOp : RTGOp<"comment", []> { + let summary = "emit a comment in instruction stream"; + + let arguments = (ins StrAttr:$comment); + + let assemblyFormat = "$comment attr-dict"; +} + //===- ISA Register Handling Operations -----------------------------------===// def FixedRegisterOp : RTGOp<"fixed_reg", [ diff --git a/include/circt/Dialect/RTG/IR/RTGVisitors.h b/include/circt/Dialect/RTG/IR/RTGVisitors.h index d2821973339a..96524fbc49e9 100644 --- a/include/circt/Dialect/RTG/IR/RTGVisitors.h +++ b/include/circt/Dialect/RTG/IR/RTGVisitors.h @@ -62,7 +62,9 @@ class RTGOpVisitor { // Memories MemoryAllocOp, MemoryBaseAddressOp, MemorySizeOp, // Memory Blocks - MemoryBlockDeclareOp>([&](auto expr) -> ResultType { + MemoryBlockDeclareOp, + // Misc ops + CommentOp>([&](auto expr) -> ResultType { return thisCast->visitOp(expr, args...); }) .Default([&](auto expr) -> ResultType { @@ -123,6 +125,7 @@ class RTGOpVisitor { HANDLE(ArraySizeOp, Unhandled); HANDLE(TupleCreateOp, Unhandled); HANDLE(TupleExtractOp, Unhandled); + HANDLE(CommentOp, Unhandled); HANDLE(LabelDeclOp, Unhandled); HANDLE(LabelUniqueDeclOp, Unhandled); HANDLE(LabelOp, Unhandled); diff --git a/lib/Dialect/RTG/Transforms/ElaborationPass.cpp b/lib/Dialect/RTG/Transforms/ElaborationPass.cpp index ec5697b13cb0..e3ee123c8c90 100644 --- a/lib/Dialect/RTG/Transforms/ElaborationPass.cpp +++ b/lib/Dialect/RTG/Transforms/ElaborationPass.cpp @@ -1665,6 +1665,8 @@ class Elaborator : public RTGOpVisitor> { return DeletionKind::Keep; } + FailureOr visitOp(CommentOp op) { return DeletionKind::Keep; } + FailureOr visitOp(scf::IfOp op) { bool cond = get(op.getCondition()); auto &toElaborate = cond ? op.getThenRegion() : op.getElseRegion(); diff --git a/lib/Dialect/RTG/Transforms/EmitRTGISAAssemblyPass.cpp b/lib/Dialect/RTG/Transforms/EmitRTGISAAssemblyPass.cpp index 9930fac9e594..2511afd4587d 100644 --- a/lib/Dialect/RTG/Transforms/EmitRTGISAAssemblyPass.cpp +++ b/lib/Dialect/RTG/Transforms/EmitRTGISAAssemblyPass.cpp @@ -104,6 +104,11 @@ class Emitter { return success(); } + LogicalResult emit(CommentOp op) { + os << llvm::indent(4) << "# " << op.getComment() << "\n"; + return success(); + } + LogicalResult emitTest(rtg::TestOp test, bool emitHeaderFooter = false) { if (emitHeaderFooter) os << "# Begin of " << test.getSymName() << "\n\n"; @@ -125,12 +130,13 @@ class Emitter { continue; } - auto res = TypeSwitch(&op) - .Case( - [&](auto op) { return emit(op); }) - .Default([](auto op) { - return op->emitError("emitter unknown RTG operation"); - }); + auto res = + TypeSwitch(&op) + .Case( + [&](auto op) { return emit(op); }) + .Default([](auto op) { + return op->emitError("emitter unknown RTG operation"); + }); if (failed(res)) return failure(); diff --git a/test/Dialect/RTG/IR/basic.mlir b/test/Dialect/RTG/IR/basic.mlir index 6c824dafee7c..6e96a443ea9a 100644 --- a/test/Dialect/RTG/IR/basic.mlir +++ b/test/Dialect/RTG/IR/basic.mlir @@ -9,6 +9,9 @@ rtg.test @constants() { // CHECK-NEXT: rtg.isa.int_to_immediate [[V0]] : !rtg.isa.immediate<32> %1 = index.constant 5 %2 = rtg.isa.int_to_immediate %1 : !rtg.isa.immediate<32> + + // CHECK-NEXT: rtg.comment "this is a comment" + rtg.comment "this is a comment" } // CHECK-LABEL: rtg.sequence @ranomizedSequenceType diff --git a/test/Dialect/RTG/Transform/elaboration.mlir b/test/Dialect/RTG/Transform/elaboration.mlir index ab027f0821bd..9130995b4f61 100644 --- a/test/Dialect/RTG/Transform/elaboration.mlir +++ b/test/Dialect/RTG/Transform/elaboration.mlir @@ -728,6 +728,12 @@ rtg.test @subtypeMatching(b = %b: index) { func.call @dummy2(%b) : (index) -> () } +// CHECK-LABEL: rtg.test @comments +rtg.test @comments(singleton = %none: index) { + // CHECK-NEXT: rtg.comment "this is a comment" + rtg.comment "this is a comment" +} + // ----- rtg.target @singletonTarget : !rtg.dict { diff --git a/test/Dialect/RTG/Transform/emit-rtg-isa-assembly.mlir b/test/Dialect/RTG/Transform/emit-rtg-isa-assembly.mlir index c3f5cee42589..aeae2523c5cc 100644 --- a/test/Dialect/RTG/Transform/emit-rtg-isa-assembly.mlir +++ b/test/Dialect/RTG/Transform/emit-rtg-isa-assembly.mlir @@ -214,6 +214,10 @@ rtg.test @test0() { // CHECK-NEXT: # srai ra, s0, 31 // CHECK-NEXT: .word 0x41F45093 rtgtest.rv32i.srai %rd, %rs, %imm5 + + // CHECK-ALLOWED-NEXT: # this is a comment + // CHECK-NEXT: # this is a comment + rtg.comment "this is a comment" } // CHECK-EMPTY: