This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel][AArch64] Fix miscompile caused by wrong G_ZEXT selection in GISel
ClosedPublic

Authored by bcl5980 on Oct 21 2022, 2:07 AM.

Details

Summary

The miscompile case's G_ZEXT has a G_FREEZE source. Similar to D127154, this patch removed isDef32, relying on the AArch64MIPeephole optimizer to remove redundant SUBREG_TO_REG nodes also in GISel.

Fix #58431

Diff Detail

Event Timeline

bcl5980 created this revision.Oct 21 2022, 2:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 21 2022, 2:07 AM
bcl5980 requested review of this revision.Oct 21 2022, 2:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 21 2022, 2:07 AM
bcl5980 updated this revision to Diff 469515.Oct 21 2022, 2:13 AM

Update test for the case.

bcl5980 planned changes to this revision.Oct 21 2022, 3:48 AM

Need to fix tests failure.

I think the bug is that GlobalISel is emitting SUBREG_TO_REG, not that it's getting eliminated later. See https://reviews.llvm.org/D127154 .

bcl5980 updated this revision to Diff 469849.Oct 21 2022, 8:17 PM
bcl5980 retitled this revision from [AArch64] Fix miscompile caused by subreg_to_reg to [GlobalISel][AArch64] Fix miscompile caused by wrong G_ZEXT selection in GISel.
bcl5980 edited the summary of this revision. (Show Details)

Update by @efriedma 's comment.

Maybe GlobalISel should stop using isDef32, like we did for SelectionDAG? I mean, this fix isn't really wrong, but I think we have the same general issue as SelectionDAG; it's hard to tell how a generic operation is going to be lowered before it's actually lowered.

Yeah I agree. I don't have anything setup to test Global ISel, and we don't have the same breadth of lit tests, but I hope that it is similar to Selection DAG where the later peephole optimizations are enough to remove the extra instructions.

bcl5980 updated this revision to Diff 470361.Oct 24 2022, 8:26 PM
bcl5980 edited the summary of this revision. (Show Details)

@efriedma @dmgreen, do we need to add G_FREEZE into AArch64InstructionSelector::isDef32 for AArch64InstructionSelector::selectArithExtendedRegister to match SelectionDAG?

bcl5980 edited the summary of this revision. (Show Details)Oct 24 2022, 9:24 PM
bcl5980 updated this revision to Diff 470372.Oct 24 2022, 10:28 PM

Update tests.

I don't think the usage in selectArithExtendedRegister actually needs to work reliably for correctness; the pattern doesn't assume anything about the high bits, and if we don't use the pattern, we just fallback to a normal zext. So it doesn't really matter that much. (See also the SelectionDAG version of this check in AArch64DAGToDAGISel::SelectArithExtendedRegister.) Ideally, I guess it would look through freeze instructions. Or maybe we should just never match "uxtw" during isel, and just add something to AArch64MIPeepholeOpt. In any case, let's fix the miscompile, and leave it for later.

Patch looks fine, but this should be approved by one of the AArch64 GlobalISel maintainers.

paquette accepted this revision.Oct 25 2022, 11:18 AM

This looks fine to me.

This revision is now accepted and ready to land.Oct 25 2022, 11:18 AM