Skip to content

Commit 7182b35

Browse files
[MooreToCore] Lower $time to new LLHD current time op (#9378)
Add support for lowering the `moore.builtin.time` op to a new `llhd.current_time` op that simply materializes the current simulation time as an SSA value. Error diff on circt-tests: ```diff -37 error: failed to legalize operation 'moore.builtin.time' +22 error: failed to legalize operation 'moore.time_to_logic' +15 error: failed to legalize operation 'moore.fmt.time' 0 total change ```
1 parent 0cb2374 commit 7182b35

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

include/circt/Dialect/LLHD/IR/LLHDValueOps.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ def ConstantTimeOp : LLHDOp<"constant_time",
4444

4545
let hasFolder = 1;
4646
}
47+
48+
def CurrentTimeOp : LLHDOp<"current_time", [MemoryEffects<[MemRead]>]> {
49+
let summary = "Get the current simulation time";
50+
let description = [{
51+
Materializes the current simulation time as an SSA value. This is equivalent
52+
to the `$time`, `$stime`, and `$realtime` system tasks in SystemVerilog, and
53+
the `now` keyword in VHDL.
54+
55+
This operation has a memory read side effect to avoid motion and CSE across
56+
`llhd.wait` operations, and other operations that may suspend execution.
57+
}];
58+
let results = (outs LLHDTimeType:$result);
59+
let assemblyFormat = "attr-dict";
60+
}

lib/Conversion/MooreToCore/MooreToCore.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,17 @@ static LogicalResult convert(FinishMessageBIOp op,
21002100
return success();
21012101
}
21022102

2103+
//===----------------------------------------------------------------------===//
2104+
// Timing Control Conversion
2105+
//===----------------------------------------------------------------------===//
2106+
2107+
// moore.builtin.time
2108+
static LogicalResult convert(TimeBIOp op, TimeBIOp::Adaptor adaptor,
2109+
ConversionPatternRewriter &rewriter) {
2110+
rewriter.replaceOpWithNewOp<llhd::CurrentTimeOp>(op);
2111+
return success();
2112+
}
2113+
21032114
//===----------------------------------------------------------------------===//
21042115
// Conversion Infrastructure
21052116
//===----------------------------------------------------------------------===//
@@ -2443,6 +2454,9 @@ static void populateOpConversion(ConversionPatternSet &patterns,
24432454
patterns.add<FinishBIOp>(convert);
24442455
patterns.add<FinishMessageBIOp>(convert);
24452456

2457+
// Timing control
2458+
patterns.add<TimeBIOp>(convert);
2459+
24462460
mlir::populateAnyFunctionOpInterfaceTypeConversionPattern(patterns,
24472461
typeConverter);
24482462
hw::populateHWModuleLikeTypeConversionPattern(

test/Conversion/MooreToCore/basic.mlir

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,4 +1412,12 @@ func.func @RealToIntLowering(%arg0: !moore.f32, %arg1: !moore.f64) {
14121412
%0 = moore.real_to_int %arg0 : f32 -> i42
14131413
%1 = moore.real_to_int %arg1 : f64 -> i42
14141414
return
1415-
}
1415+
}
1416+
1417+
// CHECK-LABEL: func.func @CurrentTime
1418+
func.func @CurrentTime() -> !moore.time {
1419+
// CHECK-NEXT: [[TMP:%.+]] = llhd.current_time
1420+
%0 = moore.builtin.time
1421+
// CHECK-NEXT: return [[TMP]] : !llhd.time
1422+
return %0 : !moore.time
1423+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: circt-opt --canonicalize %s | FileCheck %s
2+
3+
// CHECK-LABEL: @CanRemoveUnusedCurrentTime
4+
func.func @CanRemoveUnusedCurrentTime() {
5+
// CHECK-NOT: llhd.current_time
6+
llhd.current_time
7+
// CHECK-NEXT: return
8+
return
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: circt-opt --cse %s | FileCheck %s
2+
3+
// CHECK-LABEL: @CanCSECurrentTimeWithoutSideEffectsInBetween
4+
func.func @CanCSECurrentTimeWithoutSideEffectsInBetween() -> (!llhd.time, !llhd.time) {
5+
// CHECK-NEXT: [[TMP0:%.+]] = llhd.current_time
6+
%0 = llhd.current_time
7+
// CHECK-NOT: llhd.current_time
8+
%1 = llhd.current_time
9+
// CHECK-NEXT: return [[TMP0]], [[TMP0]]
10+
return %0, %1 : !llhd.time, !llhd.time
11+
}
12+
13+
// CHECK-LABEL: @CannotCSECurrentTimeWithSideEffectsInBetween
14+
func.func @CannotCSECurrentTimeWithSideEffectsInBetween() -> (!llhd.time, !llhd.time) {
15+
// CHECK-NEXT: [[TMP0:%.+]] = llhd.current_time
16+
%0 = llhd.current_time
17+
// CHECK-NEXT: call @UnknownSideEffects
18+
call @UnknownSideEffects() : () -> ()
19+
// CHECK-NEXT: [[TMP1:%.+]] = llhd.current_time
20+
%1 = llhd.current_time
21+
// CHECK-NEXT: return [[TMP0]], [[TMP1]]
22+
return %0, %1 : !llhd.time, !llhd.time
23+
}
24+
25+
func.func private @UnknownSideEffects()

test/Dialect/LLHD/IR/basic.mlir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,10 @@ hw.module @CombinationalProcess(in %arg0: i1, in %arg1: i42, in %arg2: i9001, in
197197
llhd.yield %1, %2 : i42, i9001
198198
}
199199
}
200+
201+
// CHECK-LABEL: @CurrentTime
202+
func.func @CurrentTime() -> !llhd.time {
203+
// CHECK: llhd.current_time
204+
%0 = llhd.current_time
205+
return %0 : !llhd.time
206+
}

0 commit comments

Comments
 (0)