Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure type conversion materialization callbacks return values of the correct type #7583

Merged
merged 2 commits into from
Sep 6, 2024

Conversation

maerhart
Copy link
Member

@maerhart maerhart commented Sep 4, 2024

The materialization callbacks have to return either a nullopt or a value of the type provided as argument. This is not guaranteed by the ones changed in this PR.
This callback is invoked when an unrealized_conversion_cast operation inserted by the dialect conversion framework cannot be reconciled by the framework itself (e.g., if the input and output types of the op match, it gets folded away automatically).
This PR takes the conservative route, just telling the dialect conversion framework that it is fine to leave the conversion cast op in there if it cannot get rid of it. We could also return nullopt to tell it that it should fail in those cases.

Copy link
Member

@uenoku uenoku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for taking care of Conversion framework. This matches what upstream code usually does.

@@ -1857,14 +1857,16 @@ static void populateTypeConverter(TypeConverter &converter) {
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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above this says the materialization here is to fold away the casts, which seems unlikely to be what is accomplished by the new version.

Should this just be dropped? (And the other here?)
(I can help answer that later)

Thanks for helping maintain this!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it is also not what is accomplished by the old version. Let me change that comment to make clear what it does.

@maerhart
Copy link
Member Author

maerhart commented Sep 6, 2024

Note: once the next LLVM bump has landed, some passes can remove their add{Target,Source}Materialization callbacks and use the buildMaterializations = false ConversionConfig (only if all the callbacks only insert an unrealized_conversion_cast).
I'll leave this for another PR(s) because it's just a cleanup, and each pass needs to be checked to see whether this applies.

@maerhart maerhart merged commit 2c2ee6e into main Sep 6, 2024
4 checks passed
@maerhart maerhart deleted the maerhart-fix-type-conversion-materializations branch September 6, 2024 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants