This is an archive of the discontinued LLVM Phabricator instance.

GlobalISel: Fix legalization error where CSE leaves behind dead defs
ClosedPublic

Authored by arsenm on Jan 10 2022, 7:22 AM.

Details

Summary

If the conversion artifact introduced in the unmerge of cast of merge
combine already existed in the function, this would introduce dead
copies which kept the old casts around, neither of which were deleted,
and would fail legalization.

This would fail as follows:

The G_UNMERGE_VALUES of the G_SEXT of the G_BUILD_VECTOR would
introduce a G_SEXT for each of the scalars.

Some of the required G_SEXTs already existed in the function, so CSE
moves them up in the function and introduces a copy to the original
result register.

The introduced CSE copies are dead, since the originally G_SEXTs were
already directly used. These copies add a use to the illegal G_SEXTs,
so they are not deleted.

The artifact combiner does not see the defs that need to be updated,
since it was hidden inside the CSE builder.

I see 2 potential fixes, and opted for the mechanically simpler one,
which is to just not insert the cast if the result operand isn't
used. Alternatively, we could not insert the cast directly into the
result register, and use replaceRegOrBuildCopy similar to the case
where there is no conversion.

I suspect this is a wider problem in the artifact combiner.

Diff Detail

Event Timeline

arsenm created this revision.Jan 10 2022, 7:22 AM
arsenm requested review of this revision.Jan 10 2022, 7:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 10 2022, 7:22 AM
Herald added a subscriber: wdng. · View Herald Transcript
Petar.Avramovic accepted this revision.Jan 10 2022, 8:34 AM

LGTM.

The G_UNMERGE_VALUES of the G_SEXT of the G_BUILD_VECTOR would introduce a G_SEXT for each of the scalars.

Is there a reason why we don't have simpler pattern where conversion opcode of merge like opcode is not used by G_UNMERGE_VALUES (in this case only G_SEXT of the G_BUILD_VECTOR -> G_BUILD_VECTOR of G_SEXT for each of the scalars).

This revision is now accepted and ready to land.Jan 10 2022, 8:34 AM

LGTM.

The G_UNMERGE_VALUES of the G_SEXT of the G_BUILD_VECTOR would introduce a G_SEXT for each of the scalars.

Is there a reason why we don't have simpler pattern where conversion opcode of merge like opcode is not used by G_UNMERGE_VALUES (in this case only G_SEXT of the G_BUILD_VECTOR -> G_BUILD_VECTOR of G_SEXT for each of the scalars).

The ArtifactValueFinder should probably be tracking a stack of encountered conversion artifacts and replaying them with the new value types