Skip to content

Commit

Permalink
Make sure type conversion materialization callbacks return values of …
Browse files Browse the repository at this point in the history
…the correct type (#7583)
  • Loading branch information
maerhart authored Sep 6, 2024
1 parent 9a91f33 commit 2c2ee6e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 17 deletions.
8 changes: 6 additions & 2 deletions lib/Conversion/DCToHW/DCToHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ class ESITypeConverter : public TypeConverter {
if (inputs.size() != 1)
return std::nullopt;

return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});

addSourceMaterialization(
Expand All @@ -128,7 +130,9 @@ class ESITypeConverter : public TypeConverter {
if (inputs.size() != 1)
return std::nullopt;

return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});
}
};
Expand Down
8 changes: 6 additions & 2 deletions lib/Conversion/HWArithToHW/HWArithToHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ HWArithToHWTypeConverter::HWArithToHWTypeConverter() {
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});

addSourceMaterialization(
Expand All @@ -402,7 +404,9 @@ HWArithToHWTypeConverter::HWArithToHWTypeConverter() {
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});
}

Expand Down
8 changes: 6 additions & 2 deletions lib/Conversion/HandshakeToDC/HandshakeToDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class DCTypeConverter : public TypeConverter {
if (vt && !vt.getInnerType())
return pack(builder, inputs.front());

return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});

addSourceMaterialization(
Expand All @@ -112,7 +114,9 @@ class DCTypeConverter : public TypeConverter {
if (vt && !vt.getInnerType())
return pack(builder, inputs.front());

return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});
}
};
Expand Down
8 changes: 6 additions & 2 deletions lib/Conversion/HandshakeToHW/HandshakeToHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class ESITypeConverter : public TypeConverter {
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});

addSourceMaterialization(
Expand All @@ -80,7 +82,9 @@ class ESITypeConverter : public TypeConverter {
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});
}
};
Expand Down
8 changes: 6 additions & 2 deletions lib/Conversion/LTLToCore/LTLToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ void LowerLTLToCorePass::runOnOperation() {
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});

converter.addSourceMaterialization(
Expand All @@ -147,7 +149,9 @@ void LowerLTLToCorePass::runOnOperation() {
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});

// Create the operation rewrite patters
Expand Down
4 changes: 3 additions & 1 deletion lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,9 @@ static void populateTypeConversion(TypeConverter &typeConverter) {
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
return inputs[0];
return builder
.create<UnrealizedConversionCastOp>(loc, resultType, inputs[0])
->getResult(0);
});
}

Expand Down
10 changes: 8 additions & 2 deletions lib/Conversion/SeqToSV/SeqToSV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ struct SeqToSVTypeConverter : public TypeConverter {
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
return inputs[0];
return builder
.create<mlir::UnrealizedConversionCastOp>(loc, resultType,
inputs[0])
->getResult(0);
});

addSourceMaterialization(
Expand All @@ -372,7 +375,10 @@ struct SeqToSVTypeConverter : public TypeConverter {
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
return inputs[0];
return builder
.create<mlir::UnrealizedConversionCastOp>(loc, resultType,
inputs[0])
->getResult(0);
});
}
};
Expand Down
12 changes: 8 additions & 4 deletions lib/Dialect/FIRRTL/Transforms/LowerClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,18 +1849,22 @@ static void populateTypeConverter(TypeConverter &converter) {
converter.addConversion(
[](DoubleType type) { return FloatType::getF64(type.getContext()); });

// Add a target materialization to fold away unrealized conversion casts.
// Add a target materialization such that the conversion does not fail when a
// type conversion could not be reconciled automatically by the framework.
converter.addTargetMaterialization(
[](OpBuilder &builder, Type type, ValueRange values, Location loc) {
assert(values.size() == 1);
return values[0];
return builder.create<UnrealizedConversionCastOp>(loc, type, values[0])
->getResult(0);
});

// Add a source materialization to fold away unrealized conversion casts.
// Add a source materialization such that the conversion does not fail when a
// type conversion could not be reconciled automatically by the framework.
converter.addSourceMaterialization(
[](OpBuilder &builder, Type type, ValueRange values, Location loc) {
assert(values.size() == 1);
return values[0];
return builder.create<UnrealizedConversionCastOp>(loc, type, values[0])
->getResult(0);
});
}

Expand Down

0 comments on commit 2c2ee6e

Please sign in to comment.