This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC][GISel]add support for float point arithmetic operations
ClosedPublic

Authored by shchenz on Aug 30 2022, 7:44 AM.

Details

Summary

This is based on https://reviews.llvm.org/D127530

Add global isel support for {G_FADD, G_FSUB, G_FMUL, G_FDIV}

Diff Detail

Event Timeline

shchenz created this revision.Aug 30 2022, 7:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2022, 7:44 AM
shchenz requested review of this revision.Aug 30 2022, 7:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2022, 7:44 AM
Kai accepted this revision.Aug 30 2022, 8:31 AM

LGTM with the message in the assert changed.

llvm/lib/Target/PowerPC/GISel/PPCRegisterBankInfo.cpp
92
This revision is now accepted and ready to land.Aug 30 2022, 8:31 AM
nemanjai requested changes to this revision.Aug 31 2022, 5:55 AM

This patch may not actually require code changes, but I would like to have the questions I posted answered before I feel confident letting it go in.

llvm/lib/Target/PowerPC/GISel/PPCInstructionSelector.cpp
93

It seems that this function does something sensible in 3 of 4 possible situations:

  1. Both SrcReg and DstReg are physical regs, just return true
  2. DstReg is virtual and SrcReg is physical, constrain DstReg
  3. DstReg is physical and SrcReg is virtual, constrain SrcReg
  4. Both DstReg and SrcReg are virtual, constrain DstReg and leave SrcReg untouched

Why do we behave this way for 4. above?

llvm/lib/Target/PowerPC/GISel/PPCRegisterBankInfo.cpp
40–42

Why are these classes included? Is it expected that global isel works with -ppc-enable-gpr-to-vsr-spills?

57–59

It is not clear from the description why the handling of copies is changing here. Also, it would seem that the comment is no longer valid.

93–96
OperansMapping = getValueMapping(Size == 32 ? PMI_FPR32 : PMI_FPR64);
llvm/lib/Target/PowerPC/GISel/PPCRegisterBanks.td
17

I probably don't understand this well enough, but I find it surprising that the list of register classes only includes VSSRC (i.e. all 64 single precision VSX registers). Why not VSFRC for double precision - and why not the subclasses F8RC, F4RC?

llvm/test/CodeGen/PowerPC/GlobalISel/float-arithmetic.ll
3

What would happen if -mattr=-vsx was added to this invocation? A crash? An error? We still get VSX instructions? We defer to SDAG ISel?

This revision now requires changes to proceed.Aug 31 2022, 5:55 AM
Kai added inline comments.Aug 31 2022, 7:58 AM
llvm/lib/Target/PowerPC/GISel/PPCInstructionSelector.cpp
93

Good point. I believe the outlier is case 3. The copy instruction is somewhat special. My understanding is that we need to care only about the DstReg, because the SrcReg is either a physical register or is constraint somewhere else. Since most of the code comes from my PR, my proposal is that I change the code in my PR to

for (const MachineOperand &MO : I.operands()) {
  if (MO.getReg().isPhysical())
    continue;

  const TargetRegisterClass *RC =
          TRI.getConstrainedRegClassForOperand(MO, *MRI);
  if (!RC)
    continue;
  RBI.constrainGenericRegister(MO.getReg(), *RC, *MRI);
}
return true;

This is imho more uniform, and better to read.

shchenz added inline comments.Sep 5 2022, 1:55 AM
llvm/lib/Target/PowerPC/GISel/PPCRegisterBankInfo.cpp
40–42

This is from the auto-generated td file PPCGenRegisterBank.inc for register bank FPRRegBankID. This function should be TableGen'ed.

57–59

See comment https://reviews.llvm.org/D127530#3769630
I will update the comments later.

llvm/lib/Target/PowerPC/GISel/PPCRegisterBanks.td
17

This is because the register bank contains the specified register classes and all their sub register classes. So we just need to specify the largest register class. In PPCRegisterInfo.td, we have:

def VSFRC : RegisterClass<"PPC", [f64], 64, (add F8RC, VFRC)>;
def VSSRC : RegisterClass<"PPC", [f32], 32, (add VSFRC)>;

So adding VSSRC will automatically make FPRRegBank contain VSFRCand further contain F8RC and VFRC because they are sub register classes of VSFRC.

llvm/test/CodeGen/PowerPC/GlobalISel/float-arithmetic.ll
3

It is ok to compile for -mattr=-vsx and fadds will be emitted as expected. Generic global-isel node G_FADD is mapped to DAG node fadd. The fadd will be selected in ppcinstr.*.td files like DAG instruction does, i.e., select xsaddsp first and then fadds if vsx is not available.

shchenz updated this revision to Diff 457907.Sep 5 2022, 2:07 AM
shchenz marked 3 inline comments as done.

address comments

llvm/lib/Target/PowerPC/GISel/PPCRegisterBankInfo.cpp
57–59

The comments seems wrong before the change and right now.

amyk added a subscriber: amyk.Sep 14 2022, 9:36 PM
amyk added inline comments.
llvm/lib/Target/PowerPC/GISel/PPCRegisterBankInfo.cpp
40–42

A question that I thought of and was curious about is regarding these register classes and the test cases you added.

Are those tests only corresponding to a single/certain register classes out of the ones you added? Should we be testing all types of register classes in the tests (if that is even possible)?

llvm/lib/Target/PowerPC/GISel/PPCRegisterBanks.td
16

nit: Floating point Registers.

llvm/test/CodeGen/PowerPC/GlobalISel/float-arithmetic.ll
2

I was thinking it might be good to include -ppc-vsr-nums-as-vr -ppc-asm-full-reg-names like we do in our other test cases.

arsenm added inline comments.Sep 15 2022, 6:54 AM
llvm/lib/Target/PowerPC/GISel/PPCInstructionSelector.cpp
100–114

This won't correctly handle the case where the incoming copy already has a class assigned. This may end up widening the class constraint

shchenz updated this revision to Diff 463110.Sep 27 2022, 12:07 AM
shchenz marked 3 inline comments as done.

address comments

Thanks for review @amyk @arsenm . Updated accordingly.

llvm/lib/Target/PowerPC/GISel/PPCInstructionSelector.cpp
100–114

Make sense. Updated accordingly. FYI @Kai

llvm/lib/Target/PowerPC/GISel/PPCRegisterBankInfo.cpp
40–42

The tests I added in this patch are all related to register class PPC::SPILLTOVSRRC_and_F4RCRegClassID. This function is called for register bank selection for COPY instructions that have a physical register operand. And for floating point physical register, like $f1(for both type float and double), TargetRegisterInfo::getMinimalPhysRegClass() returns PPC::SPILLTOVSRRC_and_F4RCRegClassID.

So I thought it would be difficult to add a case for each register class here. Maybe some register classes listed here can not be returned from TargetRegisterInfo::getMinimalPhysRegClass(). Reason I listed them here is I thought the final TableGen'ed version must contain these mappings.

llvm/test/CodeGen/PowerPC/GlobalISel/float-arithmetic.ll
2

Good idea.

shchenz updated this revision to Diff 463153.Sep 27 2022, 1:59 AM
arsenm added inline comments.Sep 27 2022, 12:37 PM
llvm/lib/Target/PowerPC/GISel/PPCInstructionSelector.cpp
122

Most of this function is repeating the work RBI.constrainGenericRegister already does. You just need to get the class implied by the bank with the type, and pass that to RBI.constrainGenericRegister. It figures out if there's a class set or not and handles subclass checks

shchenz updated this revision to Diff 463399.Sep 27 2022, 7:39 PM

address comments

amyk added a comment.Sep 28 2022, 9:49 AM

Thanks for answering my questions and addressing the comments I left.

llvm/lib/Target/PowerPC/GISel/PPCGenRegisterBankInfo.def
44–48

Aren't these comments more accurately reflected as the following?

shchenz updated this revision to Diff 464140.Sep 29 2022, 10:19 PM

fix comments

shchenz marked an inline comment as done.Sep 29 2022, 10:21 PM
shchenz added inline comments.
llvm/lib/Target/PowerPC/GISel/PPCGenRegisterBankInfo.def
44–48

good catch.

arsenm accepted this revision.Sep 30 2022, 8:58 AM

LGTM

nemanjai accepted this revision.Nov 1 2022, 6:46 AM

LGTM.

This revision is now accepted and ready to land.Nov 1 2022, 6:46 AM
shchenz updated this revision to Diff 477051.Nov 21 2022, 7:32 PM
shchenz marked an inline comment as done.

rebase

amyk accepted this revision.Nov 21 2022, 9:21 PM

LGTM as well. Thank you!

This revision was landed with ongoing or failed builds.Nov 22 2022, 12:00 AM
This revision was automatically updated to reflect the committed changes.