Skip to content

Commit 5a9d631

Browse files
author
Kavya Chopra
committed
minor bugfixes + builtins tests added
1 parent 82c2ce3 commit 5a9d631

File tree

5 files changed

+68
-37
lines changed

5 files changed

+68
-37
lines changed

include/circt/Dialect/Moore/MooreOps.td

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,16 +2030,17 @@ def FormatRealOp : MooreOp<"fmt.real", [Pure]> {
20302030
See IEEE 1800-2017 § 21.2.1.2 "Format specifications".
20312031
}];
20322032
let arguments = (ins
2033-
RealType:$value,
2034-
RealFormatAttr:$format,
2035-
IntAlignAttr:$alignment,
2036-
OptionalAttr<I32Attr>:$fieldWidth,
2037-
OptionalAttr<I32Attr>:$fracDigits
2033+
RealType:$value,
2034+
RealFormatAttr:$format,
2035+
IntAlignAttr:$alignment,
2036+
OptionalAttr<I32Attr>:$fieldWidth,
2037+
OptionalAttr<I32Attr>:$fracDigits
20382038
);
20392039
let results = (outs FormatStringType:$result);
20402040
let assemblyFormat = [{
2041-
$format $value `,`
2042-
attr-dict `:` type($value)
2041+
$format $value `,`
2042+
`align` $alignment
2043+
attr-dict `:` type($value)
20432044
}];
20442045
}
20452046

lib/Conversion/ImportVerilog/FormatStrings.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,21 @@ struct FormatStringParser {
212212
auto value = context.convertRvalueExpression(
213213
arg, moore::RealType::get(context.getContext(), moore::RealWidth::f64));
214214

215+
mlir::IntegerAttr widthAttr = nullptr;
216+
if (options.width) {
217+
widthAttr = builder.getI32IntegerAttr(*options.width);
218+
}
219+
220+
mlir::IntegerAttr precisionAttr = nullptr;
221+
if (options.precision) {
222+
if (*options.precision)
223+
precisionAttr = builder.getI32IntegerAttr(*options.precision);
224+
else
225+
// If precision is 0, we set it to 1 instead
226+
precisionAttr = builder.getI32IntegerAttr(1);
227+
}
228+
229+
auto alignment = options.leftJustify ? IntAlign::Left : IntAlign::Right;
215230
if (!value)
216231
return failure();
217232

lib/Dialect/Sim/SimOps.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,20 @@ OpFoldResult FormatBinOp::fold(FoldAdaptor adaptor) {
222222
OpFoldResult FormatScientificOp::fold(FoldAdaptor adaptor) {
223223

224224
if (auto floatAttr = llvm::dyn_cast_or_null<FloatAttr>(adaptor.getValue())) {
225-
int bufferSize = 2 + adaptor.getFracDigits();
226225
std::string widthString = getIsLeftAligned() ? "-" : "";
227226
if (adaptor.getFieldWidth().has_value()) {
228-
widthString = std::to_string(adaptor.getFieldWidth().value());
229-
bufferSize += adaptor.getFieldWidth().value();
227+
widthString += std::to_string(adaptor.getFieldWidth().value());
230228
}
231229
std::string fmtSpecifier =
232230
"%" + widthString + "." + std::to_string(adaptor.getFracDigits()) + "e";
233-
char *floatFmtBuffer = (char *)malloc(bufferSize);
234231

235-
while (snprintf(floatFmtBuffer, bufferSize, fmtSpecifier.c_str(),
236-
floatAttr.getValue().convertToDouble()) >= bufferSize) {
237-
bufferSize *= 2;
238-
floatFmtBuffer = (char *)realloc(floatFmtBuffer, bufferSize);
239-
}
232+
// Calculates number of bytes needed to store the format string
233+
// excluding the null terminator
234+
int bufferSize = std::snprintf(nullptr, 0, fmtSpecifier.c_str(),
235+
floatAttr.getValue().convertToDouble());
236+
std::string floatFmtBuffer(bufferSize, '\0');
237+
snprintf(floatFmtBuffer.data(), bufferSize + 1, fmtSpecifier.c_str(),
238+
floatAttr.getValue().convertToDouble());
240239
return StringAttr::get(getContext(), floatFmtBuffer);
241240
}
242241
return {};
@@ -245,21 +244,20 @@ OpFoldResult FormatScientificOp::fold(FoldAdaptor adaptor) {
245244
OpFoldResult FormatFloatOp::fold(FoldAdaptor adaptor) {
246245

247246
if (auto floatAttr = llvm::dyn_cast_or_null<FloatAttr>(adaptor.getValue())) {
248-
int bufferSize = 2 + adaptor.getFracDigits();
249247
std::string widthString = getIsLeftAligned() ? "-" : "";
250248
if (adaptor.getFieldWidth().has_value()) {
251-
widthString = std::to_string(adaptor.getFieldWidth().value());
252-
bufferSize += adaptor.getFieldWidth().value();
249+
widthString += std::to_string(adaptor.getFieldWidth().value());
253250
}
254251
std::string fmtSpecifier =
255252
"%" + widthString + "." + std::to_string(adaptor.getFracDigits()) + "f";
256-
char *floatFmtBuffer = (char *)malloc(bufferSize);
257253

258-
while (snprintf(floatFmtBuffer, bufferSize, fmtSpecifier.c_str(),
259-
floatAttr.getValue().convertToDouble()) >= bufferSize) {
260-
bufferSize *= 2;
261-
floatFmtBuffer = (char *)realloc(floatFmtBuffer, bufferSize);
262-
}
254+
// Calculates number of bytes needed to store the format string
255+
// excluding the null terminator
256+
int bufferSize = std::snprintf(nullptr, 0, fmtSpecifier.c_str(),
257+
floatAttr.getValue().convertToDouble());
258+
std::string floatFmtBuffer(bufferSize, '\0');
259+
snprintf(floatFmtBuffer.data(), bufferSize + 1, fmtSpecifier.c_str(),
260+
floatAttr.getValue().convertToDouble());
263261
return StringAttr::get(getContext(), floatFmtBuffer);
264262
}
265263
return {};
@@ -268,21 +266,20 @@ OpFoldResult FormatFloatOp::fold(FoldAdaptor adaptor) {
268266
OpFoldResult FormatGeneralOp::fold(FoldAdaptor adaptor) {
269267

270268
if (auto floatAttr = llvm::dyn_cast_or_null<FloatAttr>(adaptor.getValue())) {
271-
int bufferSize = 2 + adaptor.getFracDigits();
272269
std::string widthString = getIsLeftAligned() ? "-" : "";
273270
if (adaptor.getFieldWidth().has_value()) {
274-
widthString = std::to_string(adaptor.getFieldWidth().value());
275-
bufferSize += adaptor.getFieldWidth().value();
271+
widthString += std::to_string(adaptor.getFieldWidth().value());
276272
}
277273
std::string fmtSpecifier =
278274
"%" + widthString + "." + std::to_string(adaptor.getFracDigits()) + "g";
279-
char *floatFmtBuffer = (char *)malloc(bufferSize);
280275

281-
while (snprintf(floatFmtBuffer, bufferSize, fmtSpecifier.c_str(),
282-
floatAttr.getValue().convertToDouble()) >= bufferSize) {
283-
bufferSize *= 2;
284-
floatFmtBuffer = (char *)realloc(floatFmtBuffer, bufferSize);
285-
}
276+
// Calculates number of bytes needed to store the format string
277+
// excluding the null terminator
278+
int bufferSize = std::snprintf(nullptr, 0, fmtSpecifier.c_str(),
279+
floatAttr.getValue().convertToDouble());
280+
std::string floatFmtBuffer(bufferSize, '\0');
281+
snprintf(floatFmtBuffer.data(), bufferSize + 1, fmtSpecifier.c_str(),
282+
floatAttr.getValue().convertToDouble());
286283
return StringAttr::get(getContext(), floatFmtBuffer);
287284
}
288285
return {};

test/Conversion/ImportVerilog/basic.sv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ module realToTimeConversion;
16471647
// CHECK: [[REAL:%.+]] = moore.uint_to_real [[INT]] : i64 -> f64
16481648
// CHECK: [[SCALE:%.+]] = moore.constant_real 1.000000e+05 : f64
16491649
// CHECK: [[DIV:%.+]] = moore.fdiv [[REAL]], [[SCALE]] : f64
1650-
// CHECK: [[FMT:%.+]] = moore.fmt.real float [[DIV]], : f64
1650+
// CHECK: [[FMT:%.+]] = moore.fmt.real float [[DIV]], align right {fracDigits = 3 : i32} : f64
16511651
initial
16521652
$display("%0.3f", $time);
16531653
endmodule
@@ -1663,7 +1663,7 @@ module timeToRealConversion;
16631663
// CHECK: [[UINT_TO_REAL:%.+]] = moore.uint_to_real [[LOGIC_TO_INT]] : i64 -> f64
16641664
// CHECK: [[SCALE:%.+]] = moore.constant_real 1.000000e+05 : f64
16651665
// CHECK: [[DIV:%.+]] = moore.fdiv [[UINT_TO_REAL]], [[SCALE]] : f64
1666-
// CHECK: [[FMT:%.+]] = moore.fmt.real float [[DIV]], : f64
1666+
// CHECK: [[FMT:%.+]] = moore.fmt.real float [[DIV]], align right : f64
16671667
initial begin
16681668
$display("%f", x);
16691669
end

test/Conversion/ImportVerilog/builtins.sv

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,26 @@ function void DisplayAndSeverityBuiltins(int x, real r);
9292
// CHECK: moore.fmt.int octal [[X]], align left, pad zero width 19 : i32
9393
$write("%-19o", x);
9494

95-
// CHECK: moore.fmt.real float [[R]]
95+
// CHECK: moore.fmt.real float [[R]], align right : f64
9696
$write("%f", r);
97+
// CHECK: moore.fmt.real exponential [[R]], align right : f64
98+
$write("%e", r);
99+
// CHECK: moore.fmt.real general [[R]], align right : f64
100+
$write("%g", r);
101+
// CHECK: moore.fmt.real float [[R]], align left {fracDigits = 1 : i32} : f64
102+
$write("%-.f", r);
103+
// CHECK: moore.fmt.real exponential [[R]], align right {fieldWidth = 10 : i32} : f64
104+
$write("%10e", r);
105+
// CHECK: moore.fmt.real general [[R]], align right {fracDigits = 5 : i32} : f64
106+
$write("%0.5g", r);
107+
// CHECK: moore.fmt.real float [[R]], align left {fieldWidth = 20 : i32, fracDigits = 5 : i32} : f64
108+
$write("%-20.5f", r);
109+
// CHECK: moore.fmt.real exponential [[R]], align right {fieldWidth = 10 : i32, fracDigits = 1 : i32} : f64
110+
$write("%10.e", r);
111+
// CHECK: moore.fmt.real general [[R]], align right {fieldWidth = 9 : i32, fracDigits = 8 : i32} : f64
112+
$write("%9.8g", r);
113+
114+
97115
// CHECK: [[XR:%.+]] = moore.sint_to_real [[X]] : i32 -> f64
98116
// CHECK: [[TMP:%.+]] = moore.fmt.real float [[XR]]
99117
// CHECK: moore.builtin.display [[TMP]]

0 commit comments

Comments
 (0)