Skip to content

[X86] use vdivph for i8 integer division on avx512fp16 - #215131

Open
AryanBhirud wants to merge 1 commit into
llvm:mainfrom
AryanBhirud:fp16-vector-div-for-i8
Open

[X86] use vdivph for i8 integer division on avx512fp16#215131
AryanBhirud wants to merge 1 commit into
llvm:mainfrom
AryanBhirud:fp16-vector-div-for-i8

Conversation

@AryanBhirud

Copy link
Copy Markdown

Closes #214431

This patch enables lowering of i8 vector integer divisions through FP16 vector division when the target has fast vector FP16 division support.

For i8 vector divisions, the existing lowering path can require widening through larger integer types before converting to floating point, introducing unnecessary conversions and preventing generation of the native vdivph instruction available on AVX512FP16 targets.

This change:

  • Adds a new X86 tuning feature TuningFastVectorFP16Div.
  • Updates X86ISelLowering to use FP16 vector division for eligible i8 vector operations.

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

Hello @AryanBhirud 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-x86

Author: Aryan Bhirud (AryanBhirud)

Changes

Closes #214431

This patch enables lowering of i8 vector integer divisions through FP16 vector division when the target has fast vector FP16 division support.

For i8 vector divisions, the existing lowering path can require widening through larger integer types before converting to floating point, introducing unnecessary conversions and preventing generation of the native vdivph instruction available on AVX512FP16 targets.

This change:

  • Adds a new X86 tuning feature TuningFastVectorFP16Div.
  • Updates X86ISelLowering to use FP16 vector division for eligible i8 vector operations.

Full diff: https://github.com/llvm/llvm-project/pull/215131.diff

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86.td (+4)
  • (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+12-2)
diff --git a/llvm/lib/Target/X86/X86.td b/llvm/lib/Target/X86/X86.td
index f40b937efa74b..299950c061356 100644
--- a/llvm/lib/Target/X86/X86.td
+++ b/llvm/lib/Target/X86/X86.td
@@ -798,6 +798,10 @@ def TuningFastDPWSSD
           "Prefer vpdpwssd instruction over vpmaddwd+vpaddd instruction sequence",
           [], InlineIgnore>;
 
+def TuningFastVectorFP16Div
+    : SubtargetFeature<"fast-vector-fp16-div", "HasFastFP16Div", "true",
+                        "Vector FP16 division is fast",
+                        [], InlineIgnore>;
 def TuningPreferNoGather
     : SubtargetFeature<"prefer-no-gather", "PreferGather", "false",
                        "Prefer no gather instructions",
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index c74d342fed1cb..d12b14861bb56 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -50669,7 +50669,10 @@ static SDValue combineIntDivRem(SDNode *N, SelectionDAG &DAG,
 
   // f32 recovers the quotient exactly when both operands fit in 24 bits
   MVT FPSclVT = MVT::f64;
-  if (EltBits <= 16 || BothFitFP(APFloat::IEEEsingle()))
+  if (EltBits == 8 && Subtarget.hasFastFP16Div() &&
+      BothFitFP(APFloat::IEEEhalf()))
+    FPSclVT = MVT::f16;
+  else if (EltBits <= 16 || BothFitFP(APFloat::IEEEsingle()))
     FPSclVT = MVT::f32;
   EVT FPVT = VT.changeVectorElementType(*DAG.getContext(), FPSclVT);
 
@@ -50728,8 +50731,15 @@ static SDValue combineIntDivRem(SDNode *N, SelectionDAG &DAG,
     Q = IsSigned ? DAG.getSExtOrTrunc(Q, DL, VT)
                  : DAG.getZExtOrTrunc(Q, DL, VT);
   } else {
+    SDValue FPQuot = DAG.getNode(ISD::FDIV, DL, FPVT, X, Y);
     unsigned FromFP = IsSigned ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
-    Q = DAG.getNode(FromFP, DL, VT, DAG.getNode(ISD::FDIV, DL, FPVT, X, Y));
+    if (FPSclVT == MVT::f16 && EltBits == 8) {
+      EVT I16VT = VT.changeVectorElementType(*DAG.getContext(), MVT::i16);
+      SDValue Q16 = DAG.getNode(FromFP, DL, I16VT, FPQuot);
+      Q = DAG.getNode(ISD::TRUNCATE, DL, VT, Q16);
+    } else {
+      Q = DAG.getNode(FromFP, DL, VT, FPQuot);
+    }
   }
   if (!IsRem)
     return Q;

// f32 recovers the quotient exactly when both operands fit in 24 bits
MVT FPSclVT = MVT::f64;
if (EltBits <= 16 || BothFitFP(APFloat::IEEEsingle()))
if (EltBits == 8 && Subtarget.hasFastFP16Div() &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(EltBits == 8 || BothFitFP(APFloat::IEEEhalf())) && Subtarget.hasFastFP16Div()

@Andarwinux

Copy link
Copy Markdown
Member

test coverage?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[X86] use vdivph for i8 integer division on avx512fp16/avx10.2

2 participants