https://gcc.gnu.org/g:b399967afe8eca0690fd073b1d4d2cd684f417c2

commit r17-6629-gb399967afe8eca0690fd073b1d4d2cd684f417c2
Author: Uros Bizjak <[email protected]>
Date:   Mon Nov 23 22:01:01 2026 +0100

    i386: Save explicit-set bits of target options [PR128960]
    
    -mstack-protector-guard=, -mstackrealign and -mbranch-cost= were
    declared as manual TargetSave entries, with hand-written copies in
    ix86_function_specific_save and ix86_function_specific_restore.  That
    saves and restores their values, but not their explicit-set bits.
    
    ix86_valid_target_attribute_p restores the target options into a zeroed
    func_options_set, so ix86_option_override_internal sees these options
    as unset and resets them to the target defaults for functions with a
    target attribute.  For example, a function declared with
    __attribute__((target("bmi2"))) silently loses
    -mstack-protector-guard=global and uses the TLS guard.
    
    Declare the options with Save instead.  The generated
    cl_target_option_save and cl_target_option_restore then preserve both
    the value and the explicit-set bit, which also keeps them across LTO
    streaming.
    
    Assisted-by: Claude Sonnet 5.5
    
            PR target/128960
    
    gcc/ChangeLog:
    
            * config/i386/i386.opt (x_ix86_branch_cost)
            (x_ix86_force_align_arg_pointer, x_ix86_stack_protector_guard):
            Remove TargetSave entries.
            (mbranch-cost=, mstackrealign, mstack-protector-guard=): Add Save.
            * config/i386/i386-options.cc (ix86_function_specific_save)
            (ix86_function_specific_restore): Do not copy x_ix86_branch_cost,
            x_ix86_force_align_arg_pointer and x_ix86_stack_protector_guard.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/i386/pr128960-1.c: New test.
            * gcc.target/i386/pr128960-2.c: Ditto.

Diff:
---
 gcc/config/i386/i386-options.cc            |  6 ------
 gcc/config/i386/i386.opt                   | 18 +++---------------
 gcc/testsuite/gcc.target/i386/pr128960-1.c | 25 +++++++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr128960-2.c | 25 +++++++++++++++++++++++++
 4 files changed, 53 insertions(+), 21 deletions(-)

diff --git a/gcc/config/i386/i386-options.cc b/gcc/config/i386/i386-options.cc
index a7b0ad03cadc..2c1787b98801 100644
--- a/gcc/config/i386/i386-options.cc
+++ b/gcc/config/i386/i386-options.cc
@@ -719,14 +719,11 @@ ix86_function_specific_save (struct cl_target_option *ptr,
   ptr->x_ix86_arch_string = opts->x_ix86_arch_string;
   ptr->x_ix86_tune_string = opts->x_ix86_tune_string;
   ptr->x_ix86_asm_dialect = opts->x_ix86_asm_dialect;
-  ptr->x_ix86_branch_cost = opts->x_ix86_branch_cost;
   ptr->x_ix86_dump_tunes = opts->x_ix86_dump_tunes;
-  ptr->x_ix86_force_align_arg_pointer = opts->x_ix86_force_align_arg_pointer;
   ptr->x_ix86_force_drap = opts->x_ix86_force_drap;
   ptr->x_ix86_recip_name = opts->x_ix86_recip_name;
   ptr->x_ix86_section_threshold = opts->x_ix86_section_threshold;
   ptr->x_ix86_sse2avx = opts->x_ix86_sse2avx;
-  ptr->x_ix86_stack_protector_guard = opts->x_ix86_stack_protector_guard;
   ptr->x_ix86_stringop_alg = opts->x_ix86_stringop_alg;
   ptr->x_ix86_tls_dialect = opts->x_ix86_tls_dialect;
   ptr->x_ix86_tune_ctrl_string = opts->x_ix86_tune_ctrl_string;
@@ -868,14 +865,11 @@ ix86_function_specific_restore (struct gcc_options *opts,
   opts->x_ix86_arch_string = ptr->x_ix86_arch_string;
   opts->x_ix86_tune_string = ptr->x_ix86_tune_string;
   opts->x_ix86_asm_dialect = ptr->x_ix86_asm_dialect;
-  opts->x_ix86_branch_cost = ptr->x_ix86_branch_cost;
   opts->x_ix86_dump_tunes = ptr->x_ix86_dump_tunes;
-  opts->x_ix86_force_align_arg_pointer = ptr->x_ix86_force_align_arg_pointer;
   opts->x_ix86_force_drap = ptr->x_ix86_force_drap;
   opts->x_ix86_recip_name = ptr->x_ix86_recip_name;
   opts->x_ix86_section_threshold = ptr->x_ix86_section_threshold;
   opts->x_ix86_sse2avx = ptr->x_ix86_sse2avx;
-  opts->x_ix86_stack_protector_guard = ptr->x_ix86_stack_protector_guard;
   opts->x_ix86_stringop_alg = ptr->x_ix86_stringop_alg;
   opts->x_ix86_tls_dialect = ptr->x_ix86_tls_dialect;
   opts->x_ix86_tune_ctrl_string = ptr->x_ix86_tune_ctrl_string;
diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt
index e6f4af3a2ec9..66012352b9ca 100644
--- a/gcc/config/i386/i386.opt
+++ b/gcc/config/i386/i386.opt
@@ -116,18 +116,10 @@ enum calling_abi ix86_abi = SYSV_ABI
 TargetSave
 enum asm_dialect x_ix86_asm_dialect
 
-;; -mbranch-cost=
-TargetSave
-int x_ix86_branch_cost
-
 ;; -mdump-tune-features=
 TargetSave
 int x_ix86_dump_tunes
 
-;; -mstackrealign=
-TargetSave
-int x_ix86_force_align_arg_pointer
-
 ;; -mforce-drap=
 TargetSave
 int x_ix86_force_drap
@@ -160,10 +152,6 @@ int x_ix86_section_threshold
 TargetSave
 int x_ix86_sse2avx
 
-;; -mstack-protector-guard=
-TargetSave
-enum stack_protector_guard x_ix86_stack_protector_guard
-
 ;; -mstringop-strategy=
 TargetSave
 enum stringop_alg x_ix86_stringop_alg
@@ -280,7 +268,7 @@ EnumValue
 Enum(asm_dialect) String(att) Value(ASM_ATT)
 
 mbranch-cost=
-Target Undocumented RejectNegative Joined UInteger Var(ix86_branch_cost) 
IntegerRange(0, 5)
+Target Save Undocumented RejectNegative Joined UInteger Var(ix86_branch_cost) 
IntegerRange(0, 5)
 Branches are this expensive (arbitrary units).
 
 mlarge-data-threshold=
@@ -465,7 +453,7 @@ Target RejectNegative Mask(SSEREGPARM) Save
 Use SSE register passing conventions for SF and DF mode.
 
 mstackrealign
-Target Var(ix86_force_align_arg_pointer)
+Target Save Var(ix86_force_align_arg_pointer)
 Realign stack in prologue.
 
 mstack-arg-probe
@@ -1008,7 +996,7 @@ Target Mask(ISA_PKU) Var(ix86_isa_flags) Save
 Support PKU built-in functions and code generation.
 
 mstack-protector-guard=
-Target RejectNegative Joined Enum(stack_protector_guard) 
Var(ix86_stack_protector_guard) Init(SSP_TLS)
+Target Save RejectNegative Joined Enum(stack_protector_guard) 
Var(ix86_stack_protector_guard) Init(SSP_TLS)
 Use given stack-protector guard.
 
 Enum
diff --git a/gcc/testsuite/gcc.target/i386/pr128960-1.c 
b/gcc/testsuite/gcc.target/i386/pr128960-1.c
new file mode 100644
index 000000000000..89c3db340020
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128960-1.c
@@ -0,0 +1,25 @@
+/* PR target/128960 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fstack-protector-all -mstack-protector-guard=global" } */
+/* { dg-require-effective-target fstack_protector } */
+
+void sink (char *);
+
+__attribute__((target ("bmi2")))
+void
+with_bmi2 (void)
+{
+  char buf[16];
+  sink (buf);
+}
+
+void
+without_bmi2 (void)
+{
+  char buf[16];
+  sink (buf);
+}
+
+/* Both functions must use the global guard, not the TLS one.  */
+/* { dg-final { scan-assembler-not "%\[fg\]s:" } } */
+/* { dg-final { scan-assembler-times "__stack_chk_guard" 4 } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr128960-2.c 
b/gcc/testsuite/gcc.target/i386/pr128960-2.c
new file mode 100644
index 000000000000..ae4af60c8fa0
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128960-2.c
@@ -0,0 +1,25 @@
+/* PR target/128960 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -msse2 -mstackrealign" } */
+
+typedef float v4sf __attribute__ ((vector_size (16)));
+
+void sink (v4sf *);
+
+__attribute__((target ("bmi2")))
+void
+with_bmi2 (void)
+{
+  v4sf v = { 0, 0, 0, 0 };
+  sink (&v);
+}
+
+void
+without_bmi2 (void)
+{
+  v4sf v = { 0, 0, 0, 0 };
+  sink (&v);
+}
+
+/* Both functions must realign the stack in the prologue.  */
+/* { dg-final { scan-assembler-times "and\[lq\]\[\t \]+\\$-16, %\[er\]sp" 2 } 
} */

Reply via email to