Skip to content

Conversation

Scheremo
Copy link
Contributor

@Scheremo Scheremo commented Oct 19, 2025

Extend ImportVerilog to lower SystemVerilog string constants by:

  • Introducing moore.int_to_string to convert integer-backed literals to !moore.string.

  • Materializing Slang string constants via a new Context::materializeString path.

  • Integrating string handling into the general constant materialization flow.

  • Dialect

    • Add IntToStringOp (moore.int_to_string) to MooreOps.td with:
      • TwoValuedIntType input -> StringType result
  • ImportVerilog conversion

    • Implement Context::materializeString(constant, type, loc):
      • Converts Slang ConstantValue string to an integer representation and creates a moore.string_constant, then wraps it with moore.int_to_string.
    • Update materializeConstant to recognize constant.isString() and route to the new helper.

@Scheremo Scheremo force-pushed the pr-string-const branch 6 times, most recently from 61842d6 to 06f396c Compare October 19, 2025 14:47
@Scheremo Scheremo marked this pull request as ready for review October 19, 2025 16:04
@Scheremo Scheremo force-pushed the pr-string-const branch 4 times, most recently from b26c580 to 5417a15 Compare October 19, 2025 18:16
@Scheremo Scheremo changed the title [ImportVerilog] Add support for string materialization and typed ret [ImportVerilog] Add support for string materialization Oct 19, 2025
Comment on lines 574 to 588
def LiteralStringConstantOp : MooreOp<"string_literal", [Pure]> {
let summary = "Produce a constant string value";
let description = [{
Produces a constant value of string type.

Example:
```mlir
%0 = moore.string_literal "hello world"
```
}];
let arguments = (ins StrAttr:$value);
let results = (outs StringType:$result);
let assemblyFormat = "$value attr-dict";
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use the existing moore.string_constant for this, and then use a new moore.int_to_string cast to go from an integer to a !moore.string type? It would be good if we had just one way of materializing a string literal in the IR, which SV really wants to be an integer 😭. That can then be converted to a string, but use as an integer is also possible.

Strings are very annoying… on the one hand, I'd love to just have a moore.string_constant op that returns a !moore.string. But that makes it harder to convert the string to an integer when needed, since !moore.string is a dynamic string. So maybe sticking close to the SV spec with a string constant that produces and integer makes sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think no matter which way we pick it will feel bad 🙈
moore.int_to_string is a workable way; It just hurts to think we take a perfectly well-formed string, cast it to an int, and cast it back to a string 😭
Let's go for that solution for the moment though - I understand and agree with the concern of having a single way to handle StringLiteral expressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched the lowering path to StringLiteral -> int -> StringType, even though it makes my eyes bleed a little bit 😅

@Scheremo Scheremo force-pushed the pr-string-const branch 3 times, most recently from 2da86b6 to 9c44570 Compare October 20, 2025 19:16
Extend ImportVerilog to lower SystemVerilog string constants by:
- Introducing `moore.int_to_string` to convert integer-backed literals to `!moore.string`.
- Materializing Slang string constants via a new `Context::materializeString` path.
- Integrating string handling into the general constant materialization flow.

- **Dialect**
  - Add `IntToStringOp` (`moore.int_to_string`) to `MooreOps.td` with:
    - `TwoValuedIntType` input -> `StringType` result

- **ImportVerilog conversion**
  - Implement `Context::materializeString(constant, type, loc)`:
    - Converts Slang `ConstantValue` string to an integer representation and creates a `moore.string_constant`, then wraps it with `moore.int_to_string`.
  - Update `materializeConstant` to recognize `constant.isString()` and route to the new helper.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants