Skip to content

Commit

Permalink
[MooreToCore] Add support for string_constant (llvm#7628)
Browse files Browse the repository at this point in the history
Lower string constant ops to constant op in the hw dialect.
  • Loading branch information
jpinot committed Nov 4, 2024
1 parent 8aa4794 commit 19a4bfa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,35 @@ struct ConstantOpConv : public OpConversionPattern<ConstantOp> {
}
};

struct StringConstantOpConv : public OpConversionPattern<StringConstantOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(moore::StringConstantOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
const auto str = op.getValue();
const unsigned byteWidth = str.size() * 8;
const auto resultType =
typeConverter->convertType(op.getResult().getType());
if (const auto intType = mlir::dyn_cast<IntegerType>(resultType)) {
if (intType.getWidth() < byteWidth) {
return rewriter.notifyMatchFailure(op,
"invalid string constant type size");
}
} else {
return rewriter.notifyMatchFailure(op, "invalid string constant type");
}
APInt value(byteWidth, 0);
for (size_t i = 0; i < str.size(); ++i) {
const auto asciiChar = static_cast<uint8_t>(str[i]);
value |= APInt(byteWidth, asciiChar) << (8 * (str.size() - 1 - i));
}
const auto type = IntegerType::get(rewriter.getContext(), byteWidth);
rewriter.replaceOpWithNewOp<hw::ConstantOp>(
op, type, rewriter.getIntegerAttr(type, value));
return success();
}
};

struct ConcatOpConversion : public OpConversionPattern<ConcatOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
Expand Down Expand Up @@ -1477,7 +1506,7 @@ static void populateOpConversion(RewritePatternSet &patterns,
ConversionOpConversion, ReadOpConversion,
StructExtractOpConversion, StructExtractRefOpConversion,
ExtractRefOpConversion, StructCreateOpConversion, ConditionalOpConversion,
YieldOpConversion, OutputOpConversion,
YieldOpConversion, OutputOpConversion, StringConstantOpConv,

// Patterns of unary operations.
ReduceAndOpConversion, ReduceOrOpConversion, ReduceXorOpConversion,
Expand Down
11 changes: 11 additions & 0 deletions test/Conversion/MooreToCore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,14 @@ moore.module @Assert(in %cond : !moore.l1) {
moore.return
}
}

// CHECK-LABEL: hw.module @StringConstant
moore.module @StringConstant() {
moore.procedure initial {
// CHECK: hw.constant 1415934836 : i32
%str = moore.string_constant "Test" : i32
// CHECK: hw.constant 0 : i0
%str_empty = moore.string_constant "" : i0
moore.return
}
}
9 changes: 9 additions & 0 deletions test/Conversion/MooreToCore/errors.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ func.func @invalidType() {

return
}

// -----

func.func @invalidStringContant() {
// expected-error @below {{failed to legalize operation 'moore.string_constant'}}
%str = moore.string_constant "Test" : i8

return
}
5 changes: 5 additions & 0 deletions test/Dialect/Moore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ moore.module @Expressions(
// CHECK: moore.struct_inject [[STRUCT1]], "a", [[B]] : struct<{a: i32, b: i32}>, i32
moore.struct_inject %struct1, "a", %b : struct<{a: i32, b: i32}>, i32

// CHECK: moore.string_constant "Test" : i128
moore.string_constant "Test" : i128
// CHECK: moore.string_constant "" : i128
moore.string_constant "" : i128

moore.output
}

Expand Down

0 comments on commit 19a4bfa

Please sign in to comment.