Skip to content

Commit 16f899d

Browse files
authored
[MooreToCore] Lower empty string constant to expected bit width (#8688)
Fix type bit width mismatch when lowering empty string constants.
1 parent 1f671d6 commit 16f899d

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

lib/Conversion/MooreToCore/MooreToCore.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -604,23 +604,23 @@ struct StringConstantOpConv : public OpConversionPattern<StringConstantOp> {
604604
LogicalResult
605605
matchAndRewrite(moore::StringConstantOp op, OpAdaptor adaptor,
606606
ConversionPatternRewriter &rewriter) const override {
607-
const auto str = op.getValue();
608-
const unsigned byteWidth = str.size() * 8;
609607
const auto resultType =
610608
typeConverter->convertType(op.getResult().getType());
611-
if (const auto intType = mlir::dyn_cast<IntegerType>(resultType)) {
612-
if (intType.getWidth() < byteWidth) {
613-
return rewriter.notifyMatchFailure(op,
614-
"invalid string constant type size");
615-
}
616-
} else {
617-
return rewriter.notifyMatchFailure(op, "invalid string constant type");
618-
}
609+
const auto intType = mlir::cast<IntegerType>(resultType);
610+
611+
const auto str = op.getValue();
612+
const unsigned byteWidth = intType.getWidth();
619613
APInt value(byteWidth, 0);
620-
for (size_t i = 0; i < str.size(); ++i) {
621-
const auto asciiChar = static_cast<uint8_t>(str[i]);
622-
value |= APInt(byteWidth, asciiChar) << (8 * (str.size() - 1 - i));
614+
615+
// Pack ascii chars from the end of the string, until it fits.
616+
const size_t maxChars =
617+
std::min(str.size(), static_cast<size_t>(byteWidth / 8));
618+
for (size_t i = 0; i < maxChars; i++) {
619+
const size_t pos = str.size() - 1 - i;
620+
const auto asciiChar = static_cast<uint8_t>(str[pos]);
621+
value |= APInt(byteWidth, asciiChar) << (8 * i);
623622
}
623+
624624
rewriter.replaceOpWithNewOp<hw::ConstantOp>(
625625
op, resultType, rewriter.getIntegerAttr(resultType, value));
626626
return success();

test/Conversion/MooreToCore/basic.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,18 @@ moore.module @StringConstant() {
10471047
moore.procedure initial {
10481048
// CHECK: hw.constant 1415934836 : i32
10491049
%str = moore.string_constant "Test" : i32
1050+
// CHECK: hw.constant 1415934836 : i36
1051+
%str1 = moore.string_constant "Test" : i36
1052+
// CHECK: hw.constant 116 : i8
1053+
%str2 = moore.string_constant "Test" : i8
1054+
// CHECK: hw.constant 0 : i7
1055+
%str_trunc = moore.string_constant "Test" : i7
1056+
// CHECK: hw.constant 29556 : i17
1057+
%str_trunc1 = moore.string_constant "Test" : i17
10501058
// CHECK: hw.constant 0 : i0
10511059
%str_empty = moore.string_constant "" : i0
1060+
// CHECK: hw.constant 0 : i8
1061+
%str_empty_zext = moore.string_constant "" : i8
10521062
moore.return
10531063
}
10541064
}

test/Conversion/MooreToCore/errors.mlir

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,3 @@ func.func @invalidType() {
66

77
return
88
}
9-
10-
// -----
11-
12-
func.func @invalidStringContant() {
13-
// expected-error @below {{failed to legalize operation 'moore.string_constant'}}
14-
%str = moore.string_constant "Test" : i8
15-
16-
return
17-
}

0 commit comments

Comments
 (0)