[llvm] 98d5b27 - [AMDGPU] Fold constants in 2-elt vector canonicalization (#214384)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 9 23:58:14 PDT 2026


Author: LU-JOHN
Date: 2026-08-10T01:58:09-05:00
New Revision: 98d5b27f79fc897cfc8b72a91a20890fb7443156

URL: https://github.com/llvm/llvm-project/commit/98d5b27f79fc897cfc8b72a91a20890fb7443156
DIFF: https://github.com/llvm/llvm-project/commit/98d5b27f79fc897cfc8b72a91a20890fb7443156.diff

LOG: [AMDGPU] Fold constants in 2-elt vector canonicalization (#214384)

For 2-element vectors convert:

`fcanonicalize (build_vector x, k)` -> `build_vector (fcanonicalize x),
(fcanonicalize k)`
`fcanonicalize (build_vector x, undef)` -> `build_vector (fcanonicalize
x), 0`

if fcanonicalize is Legal for a single element.  

Previously only v2f16 was transformed. Now v2f32 and v2f64 are also
transformed, but v2f16 is naturally excluded.

---------

Signed-off-by: John Lu <John.Lu at amd.com>

Added: 
    llvm/test/CodeGen/AMDGPU/fcanonicalize-v2-poison.ll

Modified: 
    llvm/lib/Target/AMDGPU/SIISelLowering.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 1d165910adeba..b3e6ebcb7109f 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -16404,10 +16404,8 @@ SITargetLowering::performFCanonicalizeCombine(SDNode *N,
     return DAG.getConstantFP(QNaN, SDLoc(N), VT);
   }
 
-  if (ConstantFPSDNode *CFP = isConstOrConstSplatFP(N0)) {
-    EVT VT = N->getValueType(0);
+  if (ConstantFPSDNode *CFP = isConstOrConstSplatFP(N0))
     return getCanonicalConstantFP(DAG, SDLoc(N), VT, CFP->getValueAPF());
-  }
 
   // fcanonicalize (build_vector x, k) -> build_vector (fcanonicalize x),
   //                                                   (fcanonicalize k)
@@ -16416,14 +16414,20 @@ SITargetLowering::performFCanonicalizeCombine(SDNode *N,
 
   // TODO: This could be better with wider vectors that will be split to v2f16,
   // and to consider uses since there aren't that many packed operations.
-  if (N0.getOpcode() == ISD::BUILD_VECTOR && VT == MVT::v2f16 &&
-      isTypeLegal(MVT::v2f16)) {
+  if (N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNumOperands() == 2 &&
+      isTypeLegal(VT)) {
     SDLoc SL(N);
     SDValue NewElts[2];
     SDValue Lo = N0.getOperand(0);
     SDValue Hi = N0.getOperand(1);
     EVT EltVT = Lo.getValueType();
 
+    // Only apply this optimization if scalar canonicalize is legal for the
+    // element type. Otherwise, scalarizing may require widening the scalar back
+    // to a vector, adding overhead (e.g., bf16 has no scalar instructions).
+    if (getOperationAction(ISD::FCANONICALIZE, EltVT) != Legal)
+      return SDValue();
+
     if (vectorEltWillFoldAway(Lo) || vectorEltWillFoldAway(Hi)) {
       for (unsigned I = 0; I != 2; ++I) {
         SDValue Op = N0.getOperand(I);

diff  --git a/llvm/test/CodeGen/AMDGPU/fcanonicalize-v2-poison.ll b/llvm/test/CodeGen/AMDGPU/fcanonicalize-v2-poison.ll
new file mode 100644
index 0000000000000..925f647f35470
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/fcanonicalize-v2-poison.ll
@@ -0,0 +1,213 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=amdgpu9.00 < %s | FileCheck -check-prefixes=GFX9 %s
+; RUN: llc -mtriple=amdgpu11.00 -mattr=+real-true16 < %s | FileCheck -check-prefixes=GFX11-TRUE16 %s
+
+; Test fcanonicalize optimization that scalarizes when building 2-element vectors
+; with poison or constant elements. This optimization only applies when scalar
+; fcanonicalize is legal for the element type (f16, f32, f64 - not bf16).
+
+; Test v2f16 with register in low lane, poison in high lane
+define <2 x half> @test_canonicalize_v2f16_reg_poison(half %val) {
+; GFX9-LABEL: test_canonicalize_v2f16_reg_poison:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f16_e32 v0, v0, v0
+; GFX9-NEXT:    v_pack_b32_f16 v0, v0, 0
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f16_reg_poison:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_max_f16_e32 v0.l, v0.l, v0.l
+; GFX11-TRUE16-NEXT:    s_delay_alu instid0(VALU_DEP_1)
+; GFX11-TRUE16-NEXT:    v_cvt_u32_u16_e32 v0, v0.l
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec = insertelement <2 x half> poison, half %val, i32 0
+  %canonicalized = call <2 x half> @llvm.canonicalize.v2f16(<2 x half> %vec)
+  ret <2 x half> %canonicalized
+}
+
+; Test v2f16 with poison in low lane, register in high lane
+define <2 x half> @test_canonicalize_v2f16_poison_reg(half %val) {
+; GFX9-LABEL: test_canonicalize_v2f16_poison_reg:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f16_e32 v0, v0, v0
+; GFX9-NEXT:    v_pack_b32_f16 v0, 0, v0
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f16_poison_reg:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_max_f16_e32 v0.h, v0.l, v0.l
+; GFX11-TRUE16-NEXT:    v_mov_b16_e32 v0.l, 0
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec = insertelement <2 x half> poison, half %val, i32 1
+  %canonicalized = call <2 x half> @llvm.canonicalize.v2f16(<2 x half> %vec)
+  ret <2 x half> %canonicalized
+}
+
+; Test v2f16 with register in low lane, constant in high lane
+define <2 x half> @test_canonicalize_v2f16_reg_const(half %val) {
+; GFX9-LABEL: test_canonicalize_v2f16_reg_const:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f16_e32 v0, v0, v0
+; GFX9-NEXT:    v_pack_b32_f16 v0, v0, 2.0
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f16_reg_const:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_max_f16_e32 v0.l, v0.l, v0.l
+; GFX11-TRUE16-NEXT:    v_mov_b16_e32 v0.h, 0x4000
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec0 = insertelement <2 x half> poison, half %val, i32 0
+  %vec1 = insertelement <2 x half> %vec0, half 2.0, i32 1
+  %canonicalized = call <2 x half> @llvm.canonicalize.v2f16(<2 x half> %vec1)
+  ret <2 x half> %canonicalized
+}
+
+; Test v2f16 with constant in low lane, register in high lane
+define <2 x half> @test_canonicalize_v2f16_const_reg(half %val) {
+; GFX9-LABEL: test_canonicalize_v2f16_const_reg:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f16_e32 v0, v0, v0
+; GFX9-NEXT:    s_movk_i32 s4, 0x4200
+; GFX9-NEXT:    v_pack_b32_f16 v0, s4, v0
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f16_const_reg:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_max_f16_e32 v0.h, v0.l, v0.l
+; GFX11-TRUE16-NEXT:    v_mov_b16_e32 v0.l, 0x4200
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec0 = insertelement <2 x half> poison, half 3.0, i32 0
+  %vec1 = insertelement <2 x half> %vec0, half %val, i32 1
+  %canonicalized = call <2 x half> @llvm.canonicalize.v2f16(<2 x half> %vec1)
+  ret <2 x half> %canonicalized
+}
+
+; Test v2f32 with register in low lane, poison in high lane
+define <2 x float> @test_canonicalize_v2f32_reg_poison(float %val) {
+; GFX9-LABEL: test_canonicalize_v2f32_reg_poison:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f32_e32 v0, v0, v0
+; GFX9-NEXT:    v_mov_b32_e32 v1, 0
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f32_reg_poison:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_dual_max_f32 v0, v0, v0 :: v_dual_mov_b32 v1, 0
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec = insertelement <2 x float> poison, float %val, i32 0
+  %canonicalized = call <2 x float> @llvm.canonicalize.v2f32(<2 x float> %vec)
+  ret <2 x float> %canonicalized
+}
+
+; Test v2f32 with poison in low lane, register in high lane
+define <2 x float> @test_canonicalize_v2f32_poison_reg(float %val) {
+; GFX9-LABEL: test_canonicalize_v2f32_poison_reg:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f32_e32 v1, v0, v0
+; GFX9-NEXT:    v_mov_b32_e32 v0, 0
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f32_poison_reg:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_dual_max_f32 v1, v0, v0 :: v_dual_mov_b32 v0, 0
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec = insertelement <2 x float> poison, float %val, i32 1
+  %canonicalized = call <2 x float> @llvm.canonicalize.v2f32(<2 x float> %vec)
+  ret <2 x float> %canonicalized
+}
+
+; Test v2f32 with register and constant
+define <2 x float> @test_canonicalize_v2f32_reg_const(float %val) {
+; GFX9-LABEL: test_canonicalize_v2f32_reg_const:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f32_e32 v0, v0, v0
+; GFX9-NEXT:    v_mov_b32_e32 v1, 4.0
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f32_reg_const:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_dual_max_f32 v0, v0, v0 :: v_dual_mov_b32 v1, 4.0
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec0 = insertelement <2 x float> poison, float %val, i32 0
+  %vec1 = insertelement <2 x float> %vec0, float 4.0, i32 1
+  %canonicalized = call <2 x float> @llvm.canonicalize.v2f32(<2 x float> %vec1)
+  ret <2 x float> %canonicalized
+}
+
+; Test v2f64 with register in low lane, poison in high lane
+define <2 x double> @test_canonicalize_v2f64_reg_poison(double %val) {
+; GFX9-LABEL: test_canonicalize_v2f64_reg_poison:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f64 v[0:1], v[0:1], v[0:1]
+; GFX9-NEXT:    v_mov_b32_e32 v2, 0
+; GFX9-NEXT:    v_mov_b32_e32 v3, 0
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f64_reg_poison:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_max_f64 v[0:1], v[0:1], v[0:1]
+; GFX11-TRUE16-NEXT:    v_dual_mov_b32 v2, 0 :: v_dual_mov_b32 v3, 0
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec = insertelement <2 x double> poison, double %val, i32 0
+  %canonicalized = call <2 x double> @llvm.canonicalize.v2f64(<2 x double> %vec)
+  ret <2 x double> %canonicalized
+}
+
+; Test v2f64 with poison in low lane, register in high lane
+define <2 x double> @test_canonicalize_v2f64_poison_reg(double %val) {
+; GFX9-LABEL: test_canonicalize_v2f64_poison_reg:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f64 v[2:3], v[0:1], v[0:1]
+; GFX9-NEXT:    v_mov_b32_e32 v0, 0
+; GFX9-NEXT:    v_mov_b32_e32 v1, 0
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f64_poison_reg:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_max_f64 v[2:3], v[0:1], v[0:1]
+; GFX11-TRUE16-NEXT:    v_dual_mov_b32 v0, 0 :: v_dual_mov_b32 v1, 0
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec = insertelement <2 x double> poison, double %val, i32 1
+  %canonicalized = call <2 x double> @llvm.canonicalize.v2f64(<2 x double> %vec)
+  ret <2 x double> %canonicalized
+}
+
+; Test v2f64 with register and constant
+define <2 x double> @test_canonicalize_v2f64_reg_const(double %val) {
+; GFX9-LABEL: test_canonicalize_v2f64_reg_const:
+; GFX9:       ; %bb.0:
+; GFX9-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX9-NEXT:    v_max_f64 v[0:1], v[0:1], v[0:1]
+; GFX9-NEXT:    v_mov_b32_e32 v2, 0
+; GFX9-NEXT:    v_mov_b32_e32 v3, 0x40140000
+; GFX9-NEXT:    s_setpc_b64 s[30:31]
+;
+; GFX11-TRUE16-LABEL: test_canonicalize_v2f64_reg_const:
+; GFX11-TRUE16:       ; %bb.0:
+; GFX11-TRUE16-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GFX11-TRUE16-NEXT:    v_max_f64 v[0:1], v[0:1], v[0:1]
+; GFX11-TRUE16-NEXT:    v_dual_mov_b32 v2, 0 :: v_dual_mov_b32 v3, 0x40140000
+; GFX11-TRUE16-NEXT:    s_setpc_b64 s[30:31]
+  %vec0 = insertelement <2 x double> poison, double %val, i32 0
+  %vec1 = insertelement <2 x double> %vec0, double 5.0, i32 1
+  %canonicalized = call <2 x double> @llvm.canonicalize.v2f64(<2 x double> %vec1)
+  ret <2 x double> %canonicalized
+}


        


More information about the llvm-commits mailing list