Skip to content

Commit 59fb35e

Browse files
[ImportVerilog] Add support for $clog2 (#7645)
Add a `moore.builtin.clog2` op and use it to convert `$clog2` calls in ImportVerilog.
1 parent 4177849 commit 59fb35e

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

include/circt/Dialect/Moore/MooreOps.td

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ class Builtin<string mnemonic, list<Trait> traits = []> :
14431443
MooreOp<"builtin." # mnemonic, traits>;
14441444

14451445
//===----------------------------------------------------------------------===//
1446-
// Simulation Control
1446+
// Simulation Control Builtins
14471447
//===----------------------------------------------------------------------===//
14481448

14491449
def StopBIOp : Builtin<"stop"> {
@@ -1499,7 +1499,7 @@ def FinishMessageBIOp : Builtin<"finish_message"> {
14991499
}
15001500

15011501
//===----------------------------------------------------------------------===//
1502-
// Severity and Display
1502+
// Severity and Display Builtins
15031503
//===----------------------------------------------------------------------===//
15041504

15051505
def DisplayBIOp : Builtin<"display"> {
@@ -1544,4 +1544,26 @@ def SeverityBIOp : Builtin<"severity"> {
15441544
let assemblyFormat = "$severity $message attr-dict";
15451545
}
15461546

1547+
//===----------------------------------------------------------------------===//
1548+
// Math Builtins
1549+
//===----------------------------------------------------------------------===//
1550+
1551+
def Clog2BIOp : Builtin<"clog2", [SameOperandsAndResultType]> {
1552+
let summary = "Compute ceil(log2(x)) of x";
1553+
let description = [{
1554+
Computes the ceiling of the base-2 logarithm of the argument. The argument
1555+
is interpreted as unsigned. The result is 0 if the argument is 0. The result
1556+
corresponds to the minimum address width necessary to address a given number
1557+
of elements, or the number of bits necessary to represent a given number of
1558+
states.
1559+
1560+
If any of the bits in the argument are X or Z, the result is X.
1561+
1562+
See IEEE 1800-2017 § 20.8.1 "Integer math functions".
1563+
}];
1564+
let arguments = (ins IntType:$value);
1565+
let results = (outs IntType:$result);
1566+
let assemblyFormat = "$value attr-dict `:` type($value)";
1567+
}
1568+
15471569
#endif // CIRCT_DIALECT_MOORE_MOOREOPS

lib/Conversion/ImportVerilog/Expressions.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,18 @@ struct RvalueExprVisitor {
691691
Value visitCall(const slang::ast::CallExpression &expr,
692692
const slang::ast::CallExpression::SystemCallInfo &info) {
693693
const auto &subroutine = *info.subroutine;
694+
auto args = expr.arguments();
694695

695696
if (subroutine.name == "$signed" || subroutine.name == "$unsigned")
696-
return context.convertRvalueExpression(*expr.arguments()[0]);
697+
return context.convertRvalueExpression(*args[0]);
698+
699+
if (subroutine.name == "$clog2") {
700+
auto value = context.convertToSimpleBitVector(
701+
context.convertRvalueExpression(*args[0]));
702+
if (!value)
703+
return {};
704+
return builder.create<moore::Clog2BIOp>(loc, value);
705+
}
697706

698707
mlir::emitError(loc) << "unsupported system call `" << subroutine.name
699708
<< "`";

test/Conversion/ImportVerilog/builtins.sv

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// Internal issue in Slang v3 about jump depending on uninitialised value.
66
// UNSUPPORTED: valgrind
77

8+
function void dummyA(int x); endfunction
9+
10+
// IEEE 1800-2017 § 20.2 "Simulation control system tasks"
811
// CHECK-LABEL: func.func private @SimulationControlBuiltins(
912
function void SimulationControlBuiltins(bit x);
1013
// CHECK: moore.builtin.finish_message false
@@ -35,6 +38,8 @@ function void SimulationControlBuiltins(bit x);
3538
$exit;
3639
endfunction
3740

41+
// IEEE 1800-2017 § 20.10 "Severity tasks"
42+
// IEEE 1800-2017 § 21.2 "Display system tasks"
3843
// CHECK-LABEL: func.func private @DisplayAndSeverityBuiltins(
3944
// CHECK-SAME: [[X:%.+]]: !moore.i32
4045
function void DisplayAndSeverityBuiltins(int x);
@@ -181,3 +186,14 @@ function void DisplayAndSeverityBuiltins(int x);
181186
// CHECK: moore.unreachable
182187
if (0) $fatal(1, "%d", x);
183188
endfunction
189+
190+
// IEEE 1800-2017 § 20.8 "Math functions"
191+
// CHECK-LABEL: func.func private @MathBuiltins(
192+
// CHECK-SAME: [[X:%.+]]: !moore.i32
193+
// CHECK-SAME: [[Y:%.+]]: !moore.l42
194+
function void MathBuiltins(int x, logic [41:0] y);
195+
// CHECK: moore.builtin.clog2 [[X]] : i32
196+
dummyA($clog2(x));
197+
// CHECK: moore.builtin.clog2 [[Y]] : l42
198+
dummyA($clog2(y));
199+
endfunction

test/Dialect/Moore/basic.mlir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,12 @@ func.func @SeverityAndDisplayBuiltins(%arg0: !moore.format_string) {
385385
moore.builtin.severity fatal %arg0
386386
return
387387
}
388+
389+
// CHECK-LABEL: func.func @MathBuiltins
390+
func.func @MathBuiltins(%arg0: !moore.i32, %arg1: !moore.l42) {
391+
// CHECK: moore.builtin.clog2 %arg0 : i32
392+
moore.builtin.clog2 %arg0 : i32
393+
// CHECK: moore.builtin.clog2 %arg1 : l42
394+
moore.builtin.clog2 %arg1 : l42
395+
return
396+
}

0 commit comments

Comments
 (0)