Skip to content

Commit b26c580

Browse files
author
Moritz Scherer
committed
[ImportVerilog] Add support for string materialization and typed ret
This patch extends the ImportVerilog conversion to handle SystemVerilog string constants and improves function return handling. - **String literal support** - Added `Context::materializeString` to convert Slang string constants into `moore.format_literal` ops. - Integrated string materialization into `Context::materializeConstant`, enabling automatic lowering of string constant values. - **Typed return conversion** - Updated `StmtVisitor::visit(ReturnStatement)` to infer the enclosing function’s return type. - When known, the return expression is converted using the expected type, ensuring proper type consistency for function returns. - Added `testStrLiteralReturn` to `basic.sv` verifying: - Emission of `moore.fmt.literal` for string literals. - Proper conversion and return of string-typed values.
1 parent 8c321f9 commit b26c580

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

include/circt/Dialect/Moore/MooreOps.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,21 @@ def StringConstantOp : MooreOp<"string_constant", [Pure]> {
571571
let assemblyFormat = "$value attr-dict `:` type($result)";
572572
}
573573

574+
def LiteralStringConstantOp : MooreOp<"string_literal", [Pure]> {
575+
let summary = "Produce a constant string value";
576+
let description = [{
577+
Produces a constant value of string type.
578+
579+
Example:
580+
```mlir
581+
%0 = moore.string_literal "hello world"
582+
```
583+
}];
584+
let arguments = (ins StrAttr:$value);
585+
let results = (outs StringType:$result);
586+
let assemblyFormat = "$value attr-dict";
587+
}
588+
574589
def ShortrealLiteralOp : MooreOp<"shortreal_constant", [Pure]> {
575590
let summary = "Produce a constant shortreal value";
576591
let description = [{

lib/Conversion/ImportVerilog/Expressions.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,17 @@ Value Context::materializeSVReal(const slang::ConstantValue &svreal,
15781578
return {};
15791579
}
15801580

1581+
/// Materialize a Slang string literal as a literal string constant op.
1582+
Value Context::materializeString(const slang::ConstantValue &stringLiteral,
1583+
const slang::ast::Type &astType,
1584+
Location loc) {
1585+
if (astType.isString())
1586+
return moore::LiteralStringConstantOp::create(builder, loc,
1587+
stringLiteral.toString())
1588+
.getResult();
1589+
return {};
1590+
}
1591+
15811592
/// Materialize a Slang integer literal as a constant op.
15821593
Value Context::materializeSVInt(const slang::SVInt &svint,
15831594
const slang::ast::Type &astType, Location loc) {
@@ -1660,6 +1671,8 @@ Value Context::materializeConstant(const slang::ConstantValue &constant,
16601671
return materializeSVInt(constant.integer(), type, loc);
16611672
if (constant.isReal() || constant.isShortReal())
16621673
return materializeSVReal(constant, type, loc);
1674+
if (constant.isString())
1675+
return materializeString(constant, type, loc);
16631676

16641677
return {};
16651678
}

lib/Conversion/ImportVerilog/ImportVerilogInternals.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ struct Context {
180180
Value materializeSVReal(const slang::ConstantValue &svreal,
181181
const slang::ast::Type &type, Location loc);
182182

183+
/// Helper function to materialize a string as an SSA value.
184+
Value materializeString(const slang::ConstantValue &string,
185+
const slang::ast::Type &astType, Location loc);
186+
183187
/// Helper function to materialize an unpacked array of `SVInt`s as an SSA
184188
/// value.
185189
Value materializeFixedSizeUnpackedArrayType(

test/Conversion/ImportVerilog/basic.sv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3381,3 +3381,11 @@ module testFunctionCapture();
33813381
// CHECK: [[CAPTUREDA:%.+]] = moore.read %arg0 : <l1>
33823382
// CHECK: return [[CAPTUREDA]] : !moore.l1
33833383
endmodule
3384+
3385+
// CHECK: func.func private @testStrLiteralReturn() -> !moore.string {
3386+
function string testStrLiteralReturn;
3387+
// CHECK-NEXT: [[STR:%.+]] = moore.string_literal "\22A string literal\22"
3388+
parameter string testStrLiteral = "A string literal";
3389+
// CHECK-NEXT: return [[STR]] : !moore.string
3390+
return testStrLiteral;
3391+
endfunction // testStrLiteralReturn

0 commit comments

Comments
 (0)