This is an archive of the discontinued LLVM Phabricator instance.

[GISel] Fix match tree emitter.
ClosedPublic

Authored by Kai on Sep 3 2022, 9:24 AM.

Details

Summary

The following changes are necessary to get the generated tree matcher to compile:

  • In CodeExpansions::declare(), the assert() prevents connecting two instructions. E.g. the match code (match (MUL $t, $s1, $s2), (SUB $d, $t, $s3)), results in two declarations of $t, one for the def and one for the use. Removing the assertion allows this construct. If $t is later used, it is one of the operands, which should be perfectly fine for now.
    • The code emitted in GIMatchTreeVRegDefPartitioner::generatePartitionSelectorCode() is not compilable:
      • The value of NewInstrID should be emitted, not the name
      • Both calls involving getOperand() end with one parenthesis too many
    • Swaps generated condition for the partition code in the latter function

With these changes it is possible to use linear patterns.

I also change the rules i2p_to_p2i, fabs_fabs_fold, and fneg_fneg_fold
to use the tree matcher for a linear match. These rules are tested by:

CodeGen/AArch64/GlobalISel/combine-fabs.mir
CodeGen/AArch64/GlobalISel/combine-fneg.mir
CodeGen/AArch64/GlobalISel/combine-ptrtoint.mir
CodeGen/AMDGPU/GlobalISel/combine-add-nullptr.mir

Diff Detail

Event Timeline

Kai created this revision.Sep 3 2022, 9:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 3 2022, 9:24 AM
Kai requested review of this revision.Sep 3 2022, 9:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 3 2022, 9:24 AM

Did you forget the test case?

llvm/utils/TableGen/GlobalISel/CodeExpansions.h
26–27

You don't need the Inserted variable anymore.

Kai updated this revision to Diff 457803.Sep 3 2022, 10:28 AM

Added missing test.

Kai added a comment.Sep 3 2022, 10:33 AM

Did you forget the test case?

Oops, yes. The crash is easily verified by running llvm-tblgen without -gicombiner-stop-after-build option.
For the syntax errors I currently do not have a good test. I tried to rewrite one of the combine rules in
Combine.td, but this resulted in some regression. :-( One issue with this code here is that it is obviously
not really used.

Kai updated this revision to Diff 457806.Sep 3 2022, 10:37 AM

Remove unused Inserted variable.

Kai marked an inline comment as done.Sep 3 2022, 10:37 AM

Would a new file match-tree2.td help you? Without include Combine.td. Then you can limit the test to a minimum.

Kai updated this revision to Diff 460166.Sep 14 2022, 11:45 AM
Kai edited the summary of this revision. (Show Details)

I made another fix which enables matching of linear patterns.
To showcase the functionality I changed 3 simple combine rules
to use linear matching. All changed rules are covered by existing
tests.

It would be good to add a dedicated tablegen test for this checking the generated code. I find them helpful when trying to extend tablegen without having to make some manageable sized extract to start from

llvm/include/llvm/Target/GlobalISel/Combine.td
534

Should also delete matchCombineFNegOfFNeg

559

Should also delete matchCombineFAbsOfFAbs

llvm/utils/TableGen/GlobalISel/CodeExpansions.h
27–30

Comment doesn't read right. Maybe you meant "refers not to different operands"?

Kai updated this revision to Diff 460881.Sep 16 2022, 1:35 PM
  • Remove unused functions in CombineHelper
  • Add test checking source code
Kai added inline comments.Sep 16 2022, 1:44 PM
llvm/utils/TableGen/GlobalISel/CodeExpansions.h
27–30

What I am trying to describe: Consider

(match (MUL $t, $s1, $s2),
       (SUB $d, $t, $s3)),

the variable $t appears twice in the pattern, and declare() is called twice.

    • One replacement is MIs[0]->getOperand(1), that is, the use in the SUB instruction
  • The other replacement is MIs[1]->getOperand(0), the definition in the MUL instruction

Thus the operands are different (even of different instructions), but both refer to the same virtual register.

arsenm added inline comments.Sep 16 2022, 3:52 PM
llvm/utils/TableGen/GlobalISel/CodeExpansions.h
27–30

How about

Duplicates are not inserted. The expansion refers to different MachineOperands using the same virtual register

Kai updated this revision to Diff 460939.Sep 16 2022, 4:17 PM

Update comment.

Kai marked an inline comment as done.Sep 16 2022, 4:17 PM
arsenm accepted this revision.Sep 16 2022, 5:02 PM

LGTM

This revision is now accepted and ready to land.Sep 16 2022, 5:02 PM
This revision was landed with ongoing or failed builds.Sep 17 2022, 5:04 PM
This revision was automatically updated to reflect the committed changes.