[llvm] [AMDGPU] expand-fp: Change frem expansion criterion (PR #158285)
Frederik Harwath via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 17 07:16:34 PDT 2025
================
@@ -74,11 +74,64 @@ class FRemExpander {
/// Constant 1 of type \p ExTy.
Value *One;
+ /// The frem argument/return types that can be expanded by this class.
+ // TODO The expansion could work for other floating point types
+ // as well, but this would require additional testing.
+ static constexpr std::array<MVT, 3> ExpandableTypes{MVT::f16, MVT::f32,
+ MVT::f64};
+
+ /// Libcalls for frem instructions of the type at the corresponding
+ /// positions of ExpandableTypes.
+ static constexpr std::array<RTLIB::Libcall, 3> FremLibcalls{
+ RTLIB::REM_F32, RTLIB::REM_F32, RTLIB::REM_F64};
+
+ /// Return the Libcall for frem instructions of expandable type \p VT or
+ /// std::nullopt if \p VT is not expandable.
+ static std::optional<RTLIB::Libcall> getFremLibcallForType(EVT VT) {
+ MVT V = VT.getSimpleVT();
+ for (unsigned I = 0; I < ExpandableTypes.size(); I++)
+ if (ExpandableTypes[I] == V)
+ return FremLibcalls[I];
+
+ return {};
+ };
+
public:
static bool canExpandType(Type *Ty) {
- // TODO The expansion should work for other floating point types
- // as well, but this would require additional testing.
- return Ty->isIEEELikeFPTy() && !Ty->isBFloatTy() && !Ty->isFP128Ty();
+ EVT VT = EVT::getEVT(Ty);
+ assert(VT.isSimple() && "Can expand only simple types");
+
+ return find(ExpandableTypes, VT.getSimpleVT()) != ExpandableTypes.end();
+ }
+
+ /// Return true if the pass should expand a frem instruction of the
+ /// given \p Ty for the target represented by \p TLI. Expansion
+ /// should happen if the legalization for the scalar type uses a
+ /// non-existing libcall.
+ static bool shouldExpandFremType(const TargetLowering &TLI, EVT VT) {
+ assert(!VT.isVector() && "Cannot handle vector type; must scalarize first");
+
+ TargetLowering::LegalizeAction LA = TLI.getOperationAction(ISD::FREM, VT);
+ if (LA != TargetLowering::LegalizeAction::Expand)
+ return false;
+
+ auto Libcall = getFremLibcallForType(VT);
+ return Libcall.has_value() && !TLI.getLibcallName(*Libcall);
----------------
frederik-h wrote:
> This pass shouldn't really need to consider the libcall name. Can you fix codegen to use the libcall action for frem
[Here](https://github.com/llvm/llvm-project/pull/158285#discussion_r2345728760) you wrote:
> Expand is the correct action. Every other target should be using LibCall. I.e. change the default action to Libcall. This is another stalled migration, Expand should imply emit standalone code that doesn't depend on a function
That is, you suggest that I change the legalization action for `ISD::FREM` for every target that currently uses `Expand` to `LibCall` (unless it provides a custom lowering function, I suppose; in this case `Custom`, as e.g. used by the NVPTX target, would be correct?) and this pass would simply start handling frem for the types that it supports if the target uses `Expand` for frem instructions of this type. Is this correct?
https://github.com/llvm/llvm-project/pull/158285
More information about the llvm-commits
mailing list