This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking][SimplifyLibCalls] Fix bug in getConstantDataArrayInfo for wchar_t
ClosedPublic

Authored by bjope on Oct 5 2022, 5:45 AM.

Details

Summary

When SimplifyLibCalls is dealing with wchar_t (e.g. optimizing wcslen)
it uses ValueTracking helpers with a CharSize/ElementSize that isn't
8, but rather 16 or 32 (to match with the size in bits of a wchar_t).

Problem I've seen is that llvm::getConstantDataArrayInfo is taking
both a "ElementSize" argument (basically indicating size of a
char/element in bits) and an "Offset" which afaict is an offset
in the unit "number of elements". Then it also use
stripAndAccumulateConstantOffsets to get a "StartIdx" which afaict
is calculated in bytes. The returned Slice.Length is based on
arithmetics that add/subtract variables that are having different
units (bytes vs elements). Most notably I think the "StartIdx" must
be scaled using the "ElementSize" to get correct results.

The symptom of the above problem can be seen in the wcslen-7.ll test
case which miscompiles.

This patch is supposed to resolve the bug by converting between
bytes and elements when needed.

Diff Detail

Event Timeline

bjope created this revision.Oct 5 2022, 5:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 5 2022, 5:45 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
bjope requested review of this revision.Oct 5 2022, 5:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 5 2022, 5:45 AM
nikic added a reviewer: nikic.Oct 5 2022, 6:10 AM
nikic added a subscriber: nikic.
nikic added inline comments.
llvm/lib/Analysis/ValueTracking.cpp
4269

I don't think this assertions holds. What happens if you have something like getelementptr i8, ptr @ws, i64 3?

Ka-Ka added inline comments.Oct 5 2022, 11:37 PM
llvm/lib/Analysis/ValueTracking.cpp
4269

I guess it is better to simply return false instead of hitting an assert like this.

bjope updated this revision to Diff 465792.Oct 6 2022, 11:06 AM

Replaced assert by 'return false' in case the offset isn't a multiple of the ElementSize.

nikic accepted this revision.Oct 7 2022, 2:40 AM

LGTM

This revision is now accepted and ready to land.Oct 7 2022, 2:40 AM
This revision was landed with ongoing or failed builds.Oct 7 2022, 6:29 AM
This revision was automatically updated to reflect the committed changes.