https://gcc.gnu.org/g:9ca1edea42f626844222ac4befa1e97675514b6c

commit r17-5811-g9ca1edea42f626844222ac4befa1e97675514b6c
Author: Andrew Stubbs <[email protected]>
Date:   Thu Oct 29 16:04:16 2026 +0000

    combine, amdgcn: Fix ICE truncating wide vectors; allow packed accesses 
[PR122583]
    
    GCN has been disallowing misaligned vector accesses when there are packed
    structs in order to work around an ICE I couldn't previously explain.  This
    of course was not ideal and caused other failures, including pr122583,
    indirectly.
    
    The original ICE occurred during the combine pass, when building testcase
    gfortran.dg/recursive_alloc_comp_4.f08, but I had assumed it was caused by
    something in the way the GCN port handles subregs.  In general, subregs of
    vectors are complicated, on GCN, because vector registers cannot be treated 
as
    a single sequence of bits that can be subdivided and reinterpreted any which
    way, which is contrary to assumptions made in various parts of the compiler.
    
    However, I now believe this issue to be a latent bug -- or at least a new 
kind
    of edge case -- in the combine pass itself.
    
    Given
    
      (set (reg:SI 7) (subreg:SI (vec_merge:V4DI ...) 0))
    
    force_to_mode calls "gen_lowpart_or_truncate (SImode, <V4DI>)".  On GCN,
    truncating 256 bits to 32 bits is not a no-op, so the function tries to
    reinterpret the operand as an integer of the same size, using
    "int_mode_for_mode (V4DImode).require ()".  GCN has no integer mode wider 
than
    TImode, so this fails.  Unpatched trunk hits the same ICE in
    gcc.dg/Warray-bounds-69.c, gcc.dg/Wstringop-overflow-47.c and
    gfortran.fortran-torture/compile/{20080806-1,pr66251}.f90.
    
    This patch makes gen_lowpart_or_truncate return (clobber (const_int 0)) when
    there is no suitable integer mode, so that the combination fails cleanly, 
and
    removes the GCN workaround.
    
    With the ICE fixed, GCN now gives the expected pr97027 diagnostics in
    Wstringop-overflow-47.c, as aarch64 does, so add amdgcn to the xfail
    exceptions there.
    
    recursive_alloc_comp_4.f08 no longer reproduces the ICE on current trunk, so
    add an RTL testcase that starts at combine.
    
    Assisted-By: Claude Opus 5.5
    
    Claude assisted with the analysis, and generated the testcase.
    
    gcc/ChangeLog:
    
            PR target/122583
            * combine.cc (gen_lowpart_or_truncate): Return a CLOBBER if either
            mode has no equivalent integer mode, instead of ICEing.
            * config/gcn/gcn.cc (gcn_vectorize_support_vector_misalignment):
            Allow packed accesses.
    
    gcc/testsuite/ChangeLog:
    
            PR target/122583
            * gcc.dg/Wstringop-overflow-47.c: Don't xfail pr97027 on amdgcn.
            * gcc.target/gcn/pr122583.c: New test.

Diff:
---
 gcc/combine.cc                               | 16 +++++++++----
 gcc/config/gcn/gcn.cc                        |  8 ++-----
 gcc/testsuite/gcc.dg/Wstringop-overflow-47.c |  6 ++---
 gcc/testsuite/gcc.target/gcn/pr122583.c      | 35 ++++++++++++++++++++++++++++
 4 files changed, 52 insertions(+), 13 deletions(-)

diff --git a/gcc/combine.cc b/gcc/combine.cc
index c57cd631b8b2..939c5495e399 100644
--- a/gcc/combine.cc
+++ b/gcc/combine.cc
@@ -8667,7 +8667,11 @@ canon_reg_for_combine (rtx x, rtx reg)
 
 /* Return X converted to MODE.  If the value is already truncated to
    MODE we can just return a subreg even though in the general case we
-   would need an explicit truncation.  */
+   would need an explicit truncation.
+
+   If an explicit truncation is needed but either mode has no
+   equivalent integer mode (e.g. a wide vector mode), return
+   (clobber (const_int 0)) so that the combination fails.  */
 
 static rtx
 gen_lowpart_or_truncate (machine_mode mode, rtx x)
@@ -8677,11 +8681,15 @@ gen_lowpart_or_truncate (machine_mode mode, rtx x)
       && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
       && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
     {
+      scalar_int_mode imode, omode;
+      if (!int_mode_for_mode (GET_MODE (x)).exists (&imode)
+         || !int_mode_for_mode (mode).exists (&omode))
+       return gen_rtx_CLOBBER (mode, const0_rtx);
+
       /* Bit-cast X into an integer mode.  */
       if (!SCALAR_INT_MODE_P (GET_MODE (x)))
-       x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
-      x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
-                             x, GET_MODE (x));
+       x = gen_lowpart (imode, x);
+      x = simplify_gen_unary (TRUNCATE, omode, x, GET_MODE (x));
     }
 
   return gen_lowpart (mode, x);
diff --git a/gcc/config/gcn/gcn.cc b/gcc/config/gcn/gcn.cc
index cd6cee28f85e..0fd61fbf1be9 100644
--- a/gcc/config/gcn/gcn.cc
+++ b/gcc/config/gcn/gcn.cc
@@ -5573,16 +5573,12 @@ gcn_preferred_vector_alignment (const_tree type)
 static bool
 gcn_vectorize_support_vector_misalignment (machine_mode ARG_UNUSED (mode),
                                           int ARG_UNUSED (misalignment),
-                                          bool is_packed,
+                                          bool ARG_UNUSED (is_packed),
                                           bool ARG_UNUSED (is_gather_scatter))
 {
   /* All Flat and Global load instructions support arbitrary alignment, so
      the types and such are irrelevant (Buffer instructions are not used).  */
-
-  /* Disallow packed accesses because expand attempts to take scalar subregs of
-     vector registers, which is nonsense.
-     Testcase: gfortran.dg/recursive_alloc_comp_4.f08   */
-  return !is_packed;
+  return true;
 }
 
 /* Implement TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE.
diff --git a/gcc/testsuite/gcc.dg/Wstringop-overflow-47.c 
b/gcc/testsuite/gcc.dg/Wstringop-overflow-47.c
index ffa3c01f5107..2e0ab6e63873 100644
--- a/gcc/testsuite/gcc.dg/Wstringop-overflow-47.c
+++ b/gcc/testsuite/gcc.dg/Wstringop-overflow-47.c
@@ -65,15 +65,15 @@ void warn_i16_64 (int16_t i)
    like x86_64 it's a series of BIT_FIELD_REFs.  The overflow by
    the former is detected but the latter is not yet.  */
 
- extern char warn_a64[64];   // { dg-message "at offset (1|128) into 
destination object 'warn_a64' of size (63|64)" "pr97027 note" { xfail { ! { 
aarch64-*-* || { arm*-*-* && { arm_neon || { arm_mve || arm_thumb1 } } } } } } }
+ extern char warn_a64[64];   // { dg-message "at offset (1|128) into 
destination object 'warn_a64' of size (63|64)" "pr97027 note" { xfail { ! { 
aarch64-*-* || { amdgcn-*-* || { arm*-*-* && { arm_neon || { arm_mve || 
arm_thumb1 } } } } } } } }
 
   void *p = warn_a64 + 1;
   I16_64 *q = (I16_64*)p;
-  *q = (I16_64){ i };         // { dg-warning "writing (1 byte|64 bytes) into 
a region of size (0|63)" "pr97027" { xfail { ! { aarch64-*-* || { arm*-*-* && { 
arm_neon || { arm_mve || arm_thumb1 } } } } } } }
+  *q = (I16_64){ i };         // { dg-warning "writing (1 byte|64 bytes) into 
a region of size (0|63)" "pr97027" { xfail { ! { aarch64-*-* || { amdgcn-*-* || 
{ arm*-*-* && { arm_neon || { arm_mve || arm_thumb1 } } } } } } } }
 
   char a64[64];
   p = a64 + 1;
   q = (I16_64*)p;
-  *q = (I16_64){ i };         // { dg-warning "writing (1 byte|64 bytes) into 
a region of size (0|63)" "pr97027" { xfail { ! { aarch64-*-* || { arm*-*-* && { 
arm_neon || { arm_mve || arm_thumb1 } } } } } } }
+  *q = (I16_64){ i };         // { dg-warning "writing (1 byte|64 bytes) into 
a region of size (0|63)" "pr97027" { xfail { ! { aarch64-*-* || { amdgcn-*-* || 
{ arm*-*-* && { arm_neon || { arm_mve || arm_thumb1 } } } } } } } }
   sink (p);
 }
diff --git a/gcc/testsuite/gcc.target/gcn/pr122583.c 
b/gcc/testsuite/gcc.target/gcn/pr122583.c
new file mode 100644
index 000000000000..8b892b675713
--- /dev/null
+++ b/gcc/testsuite/gcc.target/gcn/pr122583.c
@@ -0,0 +1,35 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+/* PR target/122583.  Combine used to ICE trying to truncate this lowpart
+   SUBREG of a V4DImode VEC_MERGE, because there is no 256-bit integer mode
+   to bit-cast it into.  Previously this was hidden by disallowing packed
+   accesses in gcn_vectorize_support_vector_misalignment.  */
+
+void __RTL (startwith ("combine")) f1 (void)
+{
+(function "f1"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cinsn 2 (parallel [(set (reg/f:DI <0>) (symbol_ref:DI ("g")))
+                         (clobber (reg:BI scc))]))
+      (cinsn 3 (parallel [(set (reg/f:DI <1>) (symbol_ref:DI ("h")))
+                         (clobber (reg:BI scc))]))
+      (cinsn 4 (parallel [(set (reg/f:DI <2>) (symbol_ref:DI ("r")))
+                         (clobber (reg:BI scc))]))
+      (cinsn 5 (set (reg:DI <3>) (mem/c:DI (reg/f:DI <0>) [0  S8 A64])))
+      (cinsn 6 (set (reg:DI <4>) (mem/c:DI (reg/f:DI <1>) [0  S8 A64])))
+      (cinsn 7 (set (reg:V4DI <5>) (vec_duplicate:V4DI (reg:DI <3>))))
+      (cinsn 8 (set (reg:V4DI <6>)
+                   (vec_merge:V4DI (vec_duplicate:V4DI (reg:DI <4>))
+                                   (reg:V4DI <5>)
+                                   (ashift (const_int 1) (const_int 3)))))
+      (cinsn 9 (set (reg:SI <7>) (subreg:SI (reg:V4DI <6>) 0)))
+      (cinsn 10 (set (mem/c:SI (reg/f:DI <2>) [0  S4 A32]) (reg:SI <7>)))
+      (edge-to exit (flags "FALLTHRU"))
+    ) ;; block 2
+  ) ;; insn-chain
+) ;; function
+}

Reply via email to