Skip to content

Conversation

@Scheremo
Copy link
Contributor

@Scheremo Scheremo commented Sep 17, 2025

This PR adds support for using the %t format specifier in format strings by mapping them to the newly introduced moore.fmt.time / moore::TimeFormatOp in the ImportVerilog string formatter.

According to the system verilog spec IEEE 1800-2023 § 20.4.3 "$timeformat", the exact formatting for any time value is dictated by the last $timeformat system task invoked within the scope or parent scopes of the format string, or alternatively, by a mix of constant default values and

The smallest time precision argument of all the `timescale compiler directives in the source description.

Because of the formatting relying on context (who came up with this anyway? seems like a bad idea), and use of $timeformat is permitted in conditional settings, meaning it can't always be determined statically, I decided to not add a format attribute to moore::FormatTimeOp. I only added the width attribute, which may be defined in the formatter expression, e.g. %4t. Instead the semantics for implementing moore.fmt.time should rely on querying some global state that models the configuration set by the latest $timeformat system call within the scope.

What's not fully clear to me is whether the System Verilog spec allows non-"time typed" values to be formatted with %t; I didn't find an explicit description of allowed values, but all examples in the spec I could find explicitly use the return value of a time-typed system task (e.g. $time) or a time-type parameter (e.g. parameter time _time = 100ns;).
While it seems reasonable to suppose that also integer values are allowed (per the spec $time returns a 64-bit integer representing time), some examples in the doc also allow the use of $realtime (which returns a real value) with %t, so potentially any numeric value is allowed? In either case I didn't add this in the current implementation and instead only allow TimeType values, which in the context of Moore looked like the sanest option to me. Maybe someone has a clearer view on this.

@Scheremo Scheremo force-pushed the pr-t-format-specifier branch 3 times, most recently from 8f72551 to 7b45def Compare September 17, 2025 11:29
@Scheremo Scheremo marked this pull request as ready for review September 17, 2025 12:47
@Scheremo Scheremo force-pushed the pr-t-format-specifier branch from 7b45def to ae2423b Compare September 17, 2025 13:35
This PR adds support for using the %t format specifier in format strings by mapping them to the newly introduced moore.fmt.time / moore::TimeFormatOp in the ImportVerilog string formatter.

According to the system verilog spec IEEE 1800-2023 § 20.4.3 "$timeformat", the exact formatting for any time value is dictated by the last $timeformat system task invoked within the scope or parent scopes of the format string, or alternatively, by a mix of constant default values and

The smallest time precision argument of all the `timescale compiler directives in the source description.
Because of the formatting relying on context (who came up with this anyway? seems like a bad idea), and use of $timeformat is permitted in conditional settings, meaning it can't always be determined statically, I decided to not add a format attribute to moore::FormatTimeOp. I only added the width attribute, which may be defined in the formatter expression, e.g. %4t. Instead the semantics for implementing moore.fmt.time should rely on querying some global state that models the configuration set by the latest $timeformat system call within the scope.

What's not fully clear to me is whether the System Verilog spec allows non-"time typed" values to be formatted with %t; I didn't find an explicit description of allowed values, but all examples in the spec I could find explicitly use the return value of a time-typed system task (e.g. $time) or a time-type parameter (e.g. parameter time _time = 100ns;).
While it seems reasonable to suppose that also integer values are allowed (per the spec $time
returns a 64-bit integer representing time), some examples in the doc also allow the use of
$realtime (which returns a real value) with %t, so potentially any numeric value is allowed? In
either case I didn't add this in the current implementation and instead only allow TimeType values,
which in the context of Moore looked like the sanest option to me. Maybe someone has a clearer view
on this.
@Scheremo Scheremo force-pushed the pr-t-format-specifier branch from ae2423b to 5893656 Compare September 17, 2025 13:38
@Scheremo Scheremo changed the title [ImportVerilog] Add support for %t format specifier, introduce moore.fmt.time [ImportVerilog][Moore] Add support for %t format specifier, introduce moore.fmt.time Sep 17, 2025
Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! I love the idea of keeping this simple and just work on TimeType. That will allow us to handle any $timeformat related business later on somewhere else.

Comment on lines +218 to +222
// Only handle `TimeType` values.
auto value = context.convertRvalueExpression(
arg, moore::TimeType::get(context.getContext()));
if (!value)
return failure();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I think this will already handle integers properly, too. The conversion will insert casts from integers to the time type 🚀

Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor tweaks for easy commit.

@fabianschuiki fabianschuiki merged commit bf6b633 into llvm:main Sep 17, 2025
7 checks passed
@Scheremo Scheremo deleted the pr-t-format-specifier branch October 21, 2025 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants