This is an archive of the discontinued LLVM Phabricator instance.

[IR] Drop const in DILocation::getMergedLocation
ClosedPublic

Authored by Dinistro on May 5 2023, 2:12 AM.

Details

Summary

This commit removes constness from DILocation::getMergedLocation and
fixes all its users accordingly.

Having constness on the parameters forced the return type to be const
as well, which does force usage of const_cast when the location needs
to be used in metadata nodes.

Diff Detail

Event Timeline

Dinistro created this revision.May 5 2023, 2:12 AM
Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
Dinistro requested review of this revision.May 5 2023, 2:12 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMay 5 2023, 2:12 AM

I'm not sure who should review this. If anyone has someone to assign, feel free to do so.

ftynse accepted this revision.May 12 2023, 7:46 AM

LGTM

This revision is now accepted and ready to land.May 12 2023, 7:46 AM
This revision was automatically updated to reflect the committed changes.

any particular examples of these problematic const_casts? (is there a subsequent cleanup patch showing the motivation/value in this patch?)

any particular examples of these problematic const_casts? (is there a subsequent cleanup patch showing the motivation/value in this patch?)

DILocation has a special relationship with constness. This revision removes the traces of constness in MLIR's import, which was the main motivation for this cleanup.

In general, once DILocations are treated like metadata, they are usually stripped from their constness, which can cause UB.
For example, the DebugLoc constructors (https://github.com/llvm/llvm-project/blob/main/llvm/lib/IR/DebugLoc.cpp#L17) contain const_casts to strip away constness, which seems dangerous considering that they provided accessors to that casted pointer.

Currently, we have no plans to push this further, as constness changes can be invasive, and we do not have the resources to commit to this.

any particular examples of these problematic const_casts? (is there a subsequent cleanup patch showing the motivation/value in this patch?)

DILocation has a special relationship with constness. This revision removes the traces of constness in MLIR's import, which was the main motivation for this cleanup.

"MLIR's import"? Did this allow the removal of some const_casts in MLIR? (could you link to those patches here - be good to have the context connected here)

"MLIR's import"? Did this allow the removal of some const_casts in MLIR? (could you link to those patches here - be good to have the context connected here)

I was referring to the LLVM IR import into MLIR. DebugTranslation.cpp, which was changed in this revision is a part of this. The removal of constness not only drops the need for const_cast in this file, but also affect down stream users of the location translation.
For now, there are no other revisions we can link here, as this is the only place where this is used in MLIR.

Fair enough. Thanks for the context!