This is an archive of the discontinued LLVM Phabricator instance.

[AArch64][SelectionDAG] fix infinite loop caused by legalizing & combining CONCAT_VECTORS
ClosedPublic

Authored by FLZ101 on Jun 19 2023, 10:17 PM.

Details

Summary

Legalizing in AArch64TargetLowering::LowerCONCAT_VECTORS() and combining in DAGCombiner::visitCONCAT_VECTORS() could cause an infinite loop, as described in https://github.com/llvm/llvm-project/issues/63322.

This commit fixes that issue by conditionally skipping the combining.

Diff Detail

Event Timeline

FLZ101 created this revision.Jun 19 2023, 10:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 19 2023, 10:17 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
FLZ101 requested review of this revision.Jun 19 2023, 10:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 19 2023, 10:17 PM
FLZ101 retitled this revision from [Arch64][SelectionDAG] fix infinite loop caused by legalizing & combining CONCAT_VECTORS to [AArch64][SelectionDAG] fix infinite loop caused by legalizing & combining CONCAT_VECTORS.Jun 19 2023, 10:22 PM
FLZ101 edited the summary of this revision. (Show Details)
FLZ101 added a reviewer: RKSimon.
FLZ101 added a project: Restricted Project.
FLZ101 added a subscriber: RKSimon.

Please regenerate the diff with context

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
23176

We don't usually refer to issue# in code - we add that to the tests instead. The description here should be more generic

23180

The comment doesn't appear to match the code - the code just prevents folding scalable vectors after LegalDAG.

llvm/test/CodeGen/AArch64/aarch64-sve-concat_vectors.ll
2 ↗(On Diff #532788)

Better to use the update_llc_test_checks script

18 ↗(On Diff #532788)

remove the unnecessary metadata etc.

Can you upload with context? https://llvm.org/docs/Phabricator.html#requesting-a-review-via-the-web-interface. It makes the reviews easier to read.

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
23171

I'm not sure this comment adds a lot to the existing code. It might make a better summary for the patch.

23182

Does this need to check for isScalableVector, or can it just check for !LegalDAG? Or does that alter other tests?

llvm/test/CodeGen/AArch64/aarch64-sve-concat_vectors.ll
1 ↗(On Diff #532788)

This likely needs a -mtriple=aarch64-none-eabi, in order to work when the default target is not aarch64.
And is it possible to use -mattr=+sve2 instead of a specific cpu?

4 ↗(On Diff #532788)

I usually remove ; Function Attrs:..

18 ↗(On Diff #532788)

Can this be removed?

FLZ101 updated this revision to Diff 532842.Jun 20 2023, 2:50 AM
FLZ101 updated this revision to Diff 533146.Jun 20 2023, 11:29 PM
FLZ101 marked 5 inline comments as done.Jun 20 2023, 11:34 PM
FLZ101 marked 4 inline comments as done.Jun 21 2023, 12:30 AM
FLZ101 added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
23180

The legalizing happens only when the following conditions are met:

LegalDAG is true

DAGCombiner::Run():

// If this combine is running after legalizing the DAG, re-legalize any
// nodes pulled off the worklist.
if (LegalDAG) {
  SmallSetVector<SDNode *, 16> UpdatedNodes;
  bool NIsValid = DAG.LegalizeOp(N, UpdatedNodes);

  for (SDNode *LN : UpdatedNodes)
    AddToWorklistWithUsers(LN);

  if (!NIsValid)
    continue;
}

input of LowerCONCAT_VECTORS is scalable

AArch64TargetLowering::LowerCONCAT_VECTORS():

assert(Op.getValueType().isScalableVector() &&
       isTypeLegal(Op.getValueType()) &&
       "Expected legal scalable vector type!");

So if we skip the combining here when any of the following conditions is met, the infinite loop will not happen:

  • LegalDAG is true
  • the combining won't produce a scalable vector

I also want targets that does not support SVE are not affected by this change. However I am not sure whether the code does exactly what I want.

23182

The legalizing happens only when the following conditions are met:

LegalDAG is true

DAGCombiner::Run():

// If this combine is running after legalizing the DAG, re-legalize any
// nodes pulled off the worklist.
if (LegalDAG) {
  SmallSetVector<SDNode *, 16> UpdatedNodes;
  bool NIsValid = DAG.LegalizeOp(N, UpdatedNodes);

  for (SDNode *LN : UpdatedNodes)
    AddToWorklistWithUsers(LN);

  if (!NIsValid)
    continue;
}

input of LowerCONCAT_VECTORS is scalable

AArch64TargetLowering::LowerCONCAT_VECTORS():

assert(Op.getValueType().isScalableVector() &&
       isTypeLegal(Op.getValueType()) &&
       "Expected legal scalable vector type!");

So if we skip the combining here when any of the following conditions is met, the infinite loop will not happen:

  • LegalDAG is true
  • the combining won't produce a scalable vector

I also want targets that does not support SVE are not affected by this change. However I am not sure whether the code does exactly what I want.

FLZ101 marked 2 inline comments as done.Jun 21 2023, 12:56 AM
RKSimon added inline comments.Jun 21 2023, 2:07 AM
llvm/test/CodeGen/AArch64/dag-combine-concat-vectors.ll
2

Add a FileCheck and run update_llc_test_checks to generate codegen to test

FLZ101 marked 4 inline comments as not done.Jun 21 2023, 2:43 AM
FLZ101 added inline comments.Jun 21 2023, 2:55 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
23180

the combining won't produce a scalable vector

should be

the combining will produce a scalable vector

23182

the combining won't produce a scalable vector

should be

the combining will produce a scalable vector

llvm/test/CodeGen/AArch64/dag-combine-concat-vectors.ll
2

Is there a way to check whether the case could be finished in for example 10s ? I think that would be more appropriate

Matt added a subscriber: Matt.Jun 21 2023, 12:36 PM
RKSimon added inline comments.Jun 22 2023, 7:19 AM
llvm/test/CodeGen/AArch64/dag-combine-concat-vectors.ll
2

We don't tend to bother with timelimit tests as (IIRC) some buildbots struggle to handle them correctly.

FLZ101 added inline comments.Jun 24 2023, 8:06 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
23180

So if we skip the combining here when any of the following conditions is met

should be

So if we skip the combining here when the following conditions are met

23182

So if we skip the combining here when any of the following conditions is met

should be

So if we skip the combining here when the following conditions are met

FLZ101 updated this revision to Diff 534322.Jun 25 2023, 2:34 AM

Add comments

FLZ101 added inline comments.Jun 25 2023, 2:37 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
23180

Comments updated

23182

Comments updated

FLZ101 updated this revision to Diff 534411.Jun 25 2023, 7:58 PM

Fix a format issue

FLZ101 edited the summary of this revision. (Show Details)Jun 26 2023, 2:26 AM
FLZ101 added reviewers: aemerson, dblaikie, MaskRay, nikic.
RKSimon added inline comments.Jun 26 2023, 2:30 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
23175

Drop the "To avoid the infinite loop..." sentence - its just repeating what is explained earlier.

FLZ101 updated this revision to Diff 534474.Jun 26 2023, 2:57 AM
FLZ101 edited the summary of this revision. (Show Details)

Update comments

RKSimon accepted this revision.Jun 26 2023, 3:10 AM

LGTM - cheers

This revision is now accepted and ready to land.Jun 26 2023, 3:10 AM
MaskRay accepted this revision.Jun 26 2023, 10:54 AM
MaskRay added inline comments.
llvm/test/CodeGen/AArch64/dag-combine-concat-vectors.ll
1

Run PATH=$build_dir/bin:$PATH llvm-project/llvm/utils/update_llc_test_checks.py dag-combine-concat-vectors.ll to regenerate this test.

https://maskray.me/blog/2021-08-08-toolchain-testing "A regression test which simply runs the compiler and expects it not to crash has low utilization. It is often better to additionally test some properties of the produced object file."

The comment can be simplified:

Test that we do not end in an infinite loop https://github.com/llvm/llvm-project/issues/63322

FLZ101 updated this revision to Diff 534821.Jun 26 2023, 8:01 PM

Update a test case

LGTM - cheers

Thanks

llvm/test/CodeGen/AArch64/dag-combine-concat-vectors.ll
1

Very helpful. Thanks

1

Run PATH=$build_dir/bin:$PATH llvm-project/llvm/utils/update_llc_test_checks.py dag-combine-concat-vectors.ll to regenerate this test.

https://maskray.me/blog/2021-08-08-toolchain-testing "A regression test which simply runs the compiler and expects it not to crash has low utilization. It is often better to additionally test some properties of the produced object file."

The comment can be simplified:

Test that we do not end in an infinite loop https://github.com/llvm/llvm-project/issues/63322

I do not have commit access, @MaskRay Could you please commit this patch on my behalf?

Below is the name and email address I would like to use in the Author property of the commit:

FLZ101
zhangfenglei@huawei.com

I do not have commit access, @MaskRay Could you please commit this patch on my behalf?

Below is the name and email address I would like to use in the Author property of the commit:

FLZ101
zhangfenglei@huawei.com

I'll wait a bit in case there is more feedback. Thanks for the patience.