Skip to content

Commit 900c056

Browse files
authored
[RISCV] Add an implementation of findRepresentativeClass to assign i32 to GPRRegClass for RV64. (#116165)
This is an alternative fix for #81192. This allows the SelectionDAG scheduler to be able to find a representative register class for i32 on RV64. The representative register class is the super register class with the largest spill size that is also legal. The default implementation of findRepresentativeClass only works for legal types which i32 is not for RV64. I did some investigation of why tablegen uses i32 in output patterns on RV64. It appears it comes down to a function called ForceArbitraryInstResultType that picks a type for the output pattern when the isel pattern isn't specific enough. I believe it picks the smallest type(lowested numbered) to resolve the conflict. A similar issue occurs for f16 and bf16 which both use the FPR16 register class. If the isel pattern doesn't specify, tablegen may find both f16 and bf16 and may pick bf16 from Zfh pattern when Zfbfmin isn't present. Since bf16 isn't legal in that case, findRepresentativeClass will fail. For i8, i16, i32, this patch calls the base class with XLenVT to get the representative class since XLenVT is always legal. For bf16/f16, we call the base class with f32 since all of the f16/bf16 extensions depend on either F or Zfinx which will make f32 a legal type. The final representative register class further depends on whether D or Zdinx is also enabled, but that should be handled by the default implementation.
1 parent 0ae58c4 commit 900c056

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22051,6 +22051,36 @@ SDValue RISCVTargetLowering::expandIndirectJTBranch(const SDLoc &dl,
2205122051
return TargetLowering::expandIndirectJTBranch(dl, Value, Addr, JTI, DAG);
2205222052
}
2205322053

22054+
// If an output pattern produces multiple instructions tablegen may pick an
22055+
// arbitrary type from an instructions destination register class to use for the
22056+
// VT of that MachineSDNode. This VT may be used to look up the representative
22057+
// register class. If the type isn't legal, the default implementation will
22058+
// not find a register class.
22059+
//
22060+
// Some integer types smaller than XLen are listed in the GPR register class to
22061+
// support isel patterns for GISel, but are not legal in SelectionDAG. The
22062+
// arbitrary type tablegen picks may be one of these smaller types.
22063+
//
22064+
// f16 and bf16 are both valid for the FPR16 or GPRF16 register class. It's
22065+
// possible for tablegen to pick bf16 as the arbitrary type for an f16 pattern.
22066+
std::pair<const TargetRegisterClass *, uint8_t>
22067+
RISCVTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
22068+
MVT VT) const {
22069+
switch (VT.SimpleTy) {
22070+
default:
22071+
break;
22072+
case MVT::i8:
22073+
case MVT::i16:
22074+
case MVT::i32:
22075+
return TargetLowering::findRepresentativeClass(TRI, Subtarget.getXLenVT());
22076+
case MVT::bf16:
22077+
case MVT::f16:
22078+
return TargetLowering::findRepresentativeClass(TRI, MVT::f32);
22079+
}
22080+
22081+
return TargetLowering::findRepresentativeClass(TRI, VT);
22082+
}
22083+
2205422084
namespace llvm::RISCVVIntrinsicsTable {
2205522085

2205622086
#define GET_RISCVVIntrinsicsTable_IMPL

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,9 @@ class RISCVTargetLowering : public TargetLowering {
10681068

10691069
SDValue emitFlushICache(SelectionDAG &DAG, SDValue InChain, SDValue Start,
10701070
SDValue End, SDValue Flags, SDLoc DL) const;
1071+
1072+
std::pair<const TargetRegisterClass *, uint8_t>
1073+
findRepresentativeClass(const TargetRegisterInfo *TRI, MVT VT) const override;
10711074
};
10721075

10731076
namespace RISCVVIntrinsicsTable {

0 commit comments

Comments
 (0)