!518 [Sync] Sync patch from openeuler/gcc

From: @zhenyu--zhao_admin 
Reviewed-by: @li-yancheng 
Signed-off-by: @li-yancheng
This commit is contained in:
openeuler-ci-bot 2024-12-04 08:40:55 +00:00 committed by Gitee
commit e45d40ee63
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 1251 additions and 1 deletions

View File

@ -0,0 +1,234 @@
From 05bece3d79daa886a469b066061f0606ca6ebed8 Mon Sep 17 00:00:00 2001
From: huang-xioaquan <huangxiaoquan1@huawei.com>
Date: Mon, 2 Dec 2024 17:39:11 +0800
Subject: [PATCH 2/5] [Bugfix] Add no var recored check for ssa_name in struct
reorg
---
gcc/ipa-struct-reorg/escapes.def | 1 +
gcc/ipa-struct-reorg/ipa-struct-reorg.cc | 44 ++++++
.../gcc.dg/struct/rf_void_ptr_ssa_name.c | 125 ++++++++++++++++++
3 files changed, 170 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/struct/rf_void_ptr_ssa_name.c
diff --git a/gcc/ipa-struct-reorg/escapes.def b/gcc/ipa-struct-reorg/escapes.def
index 996a09bac..4ba9cc2d0 100644
--- a/gcc/ipa-struct-reorg/escapes.def
+++ b/gcc/ipa-struct-reorg/escapes.def
@@ -61,5 +61,6 @@ DEF_ESCAPE (escape_unhandled_rewrite, "Type escapes via a unhandled rewrite stmt
DEF_ESCAPE (escape_via_orig_escape, "Type escapes via a original escape type")
DEF_ESCAPE (escape_instance_field, "Type escapes via a field of instance")
DEF_ESCAPE (escape_via_empty_no_orig, "Type escapes via empty and no original")
+DEF_ESCAPE (escape_no_record_var, "Type escapes via no record var")
#undef DEF_ESCAPE
diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc
index 1a169c635..b93b8a5b5 100644
--- a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc
+++ b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc
@@ -1433,6 +1433,7 @@ public:
void propagate_escape_via_original (void);
void propagate_escape_via_empty_with_no_original (void);
void propagate_escape_via_ext_func_types (void);
+ void propagate_escape_via_no_record_var (void);
void analyze_types (void);
void clear_visited (void);
bool create_new_types (void);
@@ -4467,6 +4468,13 @@ ipa_struct_reorg::check_type_and_push (tree newdecl, srdecl *decl,
}
/* At this point there should only be unkown void* ssa names. */
gcc_assert (TREE_CODE (newdecl) == SSA_NAME);
+ tree inner = SSA_NAME_VAR (newdecl);
+ if (current_layout_opt_level >= STRUCT_REORDER_FIELDS &&
+ inner && find_decl (inner) == NULL)
+ {
+ type->mark_escape (escape_no_record_var, stmt);
+ return;
+ }
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "\nrecording unkown decl: ");
@@ -5512,6 +5520,41 @@ ipa_struct_reorg::propagate_escape_via_ext_func_types (void)
}
}
+/* Escape propagation is performed on ssa_name decl that no record var in
+ decls. */
+
+void
+ipa_struct_reorg::propagate_escape_via_no_record_var (void)
+{
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\n propagate_escape_via_no_record_var: \n\n");
+
+ for (unsigned i = 0; i < functions.length (); i++)
+ {
+ if (functions[i]->node)
+ set_cfun (DECL_STRUCT_FUNCTION (functions[i]->node->decl));
+
+ for (unsigned j = 0; j < functions[i]->decls.length (); j++)
+ {
+ srdecl *decl = functions[i]->decls[j];
+ srtype *type = decl->type;
+
+ if (TREE_CODE (decl->decl) == SSA_NAME)
+ {
+ tree inner = SSA_NAME_VAR (decl->decl);
+
+ if (inner && functions[i]->find_decl (inner) == NULL)
+ type->mark_escape (escape_no_record_var, NULL);
+ }
+ }
+
+ set_cfun (NULL);
+ }
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\n end propagate_escape_via_no_record_var \n\n");
+}
+
/* Prune the escaped types and their decls from what was recorded. */
void
@@ -5530,6 +5573,7 @@ ipa_struct_reorg::prune_escaped_types (void)
propagate_escape_via_original ();
propagate_escape_via_empty_with_no_original ();
propagate_escape_via_ext_func_types ();
+ propagate_escape_via_no_record_var ();
}
if (dump_file && (dump_flags & TDF_DETAILS))
diff --git a/gcc/testsuite/gcc.dg/struct/rf_void_ptr_ssa_name.c b/gcc/testsuite/gcc.dg/struct/rf_void_ptr_ssa_name.c
new file mode 100644
index 000000000..0f624b6b9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/struct/rf_void_ptr_ssa_name.c
@@ -0,0 +1,125 @@
+// Add a void* ssa_name check and escape.
+/* { dg-do compile } */
+
+// includes
+#include "stdio.h"
+#include "stdlib.h"
+#include "time.h"
+#include "string.h"
+#include "limits.h"
+#include "float.h"
+
+#define JOTAI_NUM_RANDS_ 25
+
+const unsigned rand_primes[JOTAI_NUM_RANDS_] = {179, 103, 479, 647, 229, 37,
+271, 557, 263, 607, 18743, 50359, 21929, 48757, 98179, 12907, 52937, 64579,
+49957, 52567, 507163, 149939, 412157, 680861, 757751};
+
+int next_i ()
+{
+ int counter = 0;
+ return rand_primes[(++counter)%JOTAI_NUM_RANDS_];
+}
+
+float next_f ()
+{
+ int counter = 0;
+ return rand_primes[(++counter)%JOTAI_NUM_RANDS_] / 757751.0F;
+}
+
+// Usage menu
+void usage()
+{
+ printf("%s", "Usage:\n\
+ prog [ARGS]\n\
+\nARGS:\n\
+ 0 big-arr\n\
+ 1 big-arr-10x\n\
+ 2 empty\n\
+\n\
+");
+}
+
+// ------------------------------------------------------------------------- //
+
+typedef unsigned long size_t; // Customize by platform.
+typedef long intptr_t;
+typedef unsigned long uintptr_t;
+typedef long scalar_t__; // Either arithmetic or pointer type.
+/* By default, we understand bool (as a convenience). */
+typedef int bool;
+#define false 0
+#define true 1
+
+/* Forward declarations */
+
+/* Type definitions */
+typedef size_t u32 ;
+struct octeon_device {int octeon_id; } ;
+
+/* Variables and functions */
+ size_t MAX_OCTEON_DEVICES ;
+ struct octeon_device** octeon_device ;
+
+int lio_get_device_id(void *dev)
+{
+ struct octeon_device *octeon_dev = (struct octeon_device *)dev;
+ u32 i;
+
+ for (i = 0; i < MAX_OCTEON_DEVICES; i++)
+ {
+ if (octeon_device[i] == octeon_dev)
+ return octeon_dev->octeon_id;
+ }
+ return -1;
+}
+
+// ------------------------------------------------------------------------- //
+
+int main(int argc, char *argv[])
+{
+ if (argc != 2)
+ {
+ usage();
+ return 1;
+ }
+
+ int opt = atoi(argv[1]);
+ switch(opt)
+ {
+ // big-arr
+ case 0:
+ {
+ void * dev;
+ int benchRet = lio_get_device_id(dev);
+ printf("%d\n", benchRet);
+ break;
+ }
+
+ // big-arr-10x
+ case 1:
+ {
+ void * dev;
+ int benchRet = lio_get_device_id(dev);
+ printf("%d\n", benchRet);
+ break;
+ }
+
+ // empty
+ case 2:
+ {
+ void * dev;
+ int benchRet = lio_get_device_id(dev);
+ printf("%d\n", benchRet);
+ break;
+ }
+
+ default:
+ usage();
+ break;
+ }
+
+ return 0;
+}
+
+/* { dg-final { scan-ipa-dump "No structures to transform" "struct_reorg" } } */
--
2.33.0

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,27 @@
From bc468838ffa1991e50cb4b82b45154d44302417b Mon Sep 17 00:00:00 2001
From: zhenyu--zhao_admin <zhaozhenyu17@huawei.com>
Date: Tue, 3 Dec 2024 22:39:36 +0800
Subject: [PATCH 5/5] Bugfix: set default value when tune_native is NULL.
---
gcc/gcc.cc | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 0032735db..90f6dfe85 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -5800,6 +5800,10 @@ do_self_spec (const char *spec)
do_spec_1 (" ", 0, NULL);
const char* tune_native = eval_spec_function ("local_cpu_detect", "cpu", "");
+ if (tune_native == NULL)
+ {
+ tune_native = "native";
+ }
setenv ("GCC_AI4C_TUNE_INFO", tune_native, 1);
/* Mark %<S switches processed by do_self_spec to be ignored permanently.
--
2.33.0

View File

@ -0,0 +1,221 @@
From 0de967c51189535f8d00943ccc91a3ad07e50ef2 Mon Sep 17 00:00:00 2001
From: wangchunyang <wangchunyang15@huawei.com>
Date: Wed, 11 Sep 2024 17:27:19 +0800
Subject: [PATCH 4/5] add flag -flto-try enable LTO and automatically skip in
inapplicable situation.
---
gcc/collect2.cc | 51 ++++++++++++++++++++++++++++++++++++++++++-
gcc/common.opt | 8 +++++++
gcc/opts-common.cc | 54 +++++++++++++++++++++++++++++++++++++++++++++-
gcc/opts.cc | 20 +++++++++++++++++
4 files changed, 131 insertions(+), 2 deletions(-)
diff --git a/gcc/collect2.cc b/gcc/collect2.cc
index 9715e8eee..690363880 100644
--- a/gcc/collect2.cc
+++ b/gcc/collect2.cc
@@ -200,6 +200,7 @@ static enum lto_mode_d lto_mode = LTO_MODE_WHOPR;
#else
static enum lto_mode_d lto_mode = LTO_MODE_NONE;
#endif
+static bool maybe_relink_without_lto = false;
bool helpflag; /* true if --help */
@@ -751,7 +752,53 @@ do_link (char **ld_argv, const char *atsuffix)
PEX_LAST | PEX_SEARCH,
HAVE_GNU_LD && at_file_supplied, atsuffix);
int ret = collect_wait (prog, pex);
- if (ret)
+ if (ret && maybe_relink_without_lto)
+ {
+ bool link_with_lto_plugin_before = false;
+ for (int i = 0, j = -1; ld_argv[i]; ++i)
+ {
+ if (endswith (ld_argv[i], "liblto_plugin.so"))
+ {
+ link_with_lto_plugin_before = true;
+ for (j = i + 1; ld_argv[j]; ++j)
+ {
+ if (!startswith (ld_argv[j], "-plugin-opt="))
+ break;
+ }
+ for (i = i - 1; ; ++i, ++j)
+ {
+ ld_argv[i] = ld_argv[j];
+ if (ld_argv[j] == NULL)
+ break;
+ }
+ break;
+ }
+ }
+ int ret2 = 0;
+ if (link_with_lto_plugin_before)
+ {
+ fprintf (stderr, "lto link fail, relinking without lto");
+ lto_mode = LTO_MODE_NONE;
+ pex = collect_execute (prog, ld_argv, NULL, NULL,
+ PEX_LAST | PEX_SEARCH,
+ HAVE_GNU_LD && at_file_supplied, atsuffix);
+ ret2 = collect_wait (prog, pex);
+ }
+ else
+ ret2 = ret;
+ if (ret2)
+ {
+ error ("ld returned %d exit status", ret);
+ exit (ret);
+ }
+ else
+ {
+ /* We have just successfully produced an output file, so assume that
+ we may unlink it if need be for now on. */
+ may_unlink_output_file = true;
+ }
+ }
+ else if (ret)
{
error ("ld returned %d exit status", ret);
exit (ret);
@@ -1009,6 +1056,8 @@ main (int argc, char **argv)
num_c_args++;
if (startswith (q, "-flto-partition=none"))
no_partition = true;
+ else if (startswith (q, "-flto-try"))
+ maybe_relink_without_lto = true;
else if (startswith (q, "-fno-lto"))
lto_mode = LTO_MODE_NONE;
else if (startswith (q, "-save-temps"))
diff --git a/gcc/common.opt b/gcc/common.opt
index 96888cf1b..0895c6114 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -70,6 +70,10 @@ bool flag_warn_unused_result = false
Variable
int flag_generate_lto
+; Nonzero if we should write GIMPLE bytecode for link-time optimization.
+Variable
+int flag_relink_whthout_lto = 0
+
; Nonzero if we should write GIMPLE bytecode for offload compilation.
Variable
int flag_generate_offload = 0
@@ -2161,6 +2165,10 @@ flto
Common
Enable link-time optimization.
+flto-try
+Common Var(flag_lto_try) Init(0)
+Do link-time optimization as much as possible.
+
flto=
Common RejectNegative Joined Var(flag_lto)
Link-time optimization with number of parallel jobs or jobserver.
diff --git a/gcc/opts-common.cc b/gcc/opts-common.cc
index 33c696f3d..176041bfe 100644
--- a/gcc/opts-common.cc
+++ b/gcc/opts-common.cc
@@ -1162,7 +1162,50 @@ decode_cmdline_options_to_array (unsigned int argc, const char **argv,
struct cl_decoded_option *opt_array;
unsigned int num_decoded_options;
- int opt_array_len = argc;
+ enum LTO_SKIP_STAT
+ {
+ NO_NEED_TO_SKIP,
+ NEED_TO_SKIP,
+ ALREADY_SKIP,
+ };
+ LTO_SKIP_STAT lto_skip_stat = NO_NEED_TO_SKIP;
+ bool try_use_lto = false;
+ const char* lto_option_conflict = NULL;
+ const char* wrap_option = "-Wl,--wrap=";
+ const char* start_lib_option = "-Wl,--start-lib";
+ for (i = 1; i < argc; i += 1)
+ {
+ if (startswith (argv[i], "-flto-try"))
+ {
+ try_use_lto = true;
+ }
+
+ if (startswith (argv[i], wrap_option)
+ && (lto_skip_stat == NO_NEED_TO_SKIP))
+ {
+ lto_option_conflict = wrap_option;
+ lto_skip_stat = NEED_TO_SKIP;
+ }
+ else if (startswith (argv[i], start_lib_option)
+ && (lto_skip_stat == NO_NEED_TO_SKIP))
+ {
+ lto_option_conflict = start_lib_option;
+ lto_skip_stat = NEED_TO_SKIP;
+ }
+ else if (startswith (argv[i], "-fno-lto"))
+ {
+ lto_option_conflict = NULL;
+ lto_skip_stat = ALREADY_SKIP;
+ break;
+ }
+ }
+ if (!try_use_lto)
+ {
+ lto_skip_stat = NO_NEED_TO_SKIP;
+ lto_option_conflict = NULL;
+ }
+
+ int opt_array_len = lto_skip_stat == NEED_TO_SKIP ? argc + 1 : argc;
opt_array = XNEWVEC (struct cl_decoded_option, opt_array_len);
opt_array[0].opt_index = OPT_SPECIAL_program_name;
@@ -1244,6 +1287,15 @@ decode_cmdline_options_to_array (unsigned int argc, const char **argv,
num_decoded_options += handle_machine_option (lang_mask, num_decoded_options,
argc, argv, opt_array);
+ if (lto_skip_stat == NEED_TO_SKIP)
+ {
+ const char * nolto = "-fno-lto";
+ fprintf (stderr, "skip lto for %s\n", lto_option_conflict);
+ decode_cmdline_option (&nolto, lang_mask,
+ &opt_array[num_decoded_options]);
+ num_decoded_options++;
+ }
+
*decoded_options = opt_array;
*decoded_options_count = num_decoded_options;
prune_options (decoded_options, decoded_options_count, lang_mask);
diff --git a/gcc/opts.cc b/gcc/opts.cc
index 84dd8925a..9ccc22510 100644
--- a/gcc/opts.cc
+++ b/gcc/opts.cc
@@ -1143,6 +1143,26 @@ finish_options (struct gcc_options *opts, struct gcc_options *opts_set,
SET_OPTION_IF_UNSET (opts, opts_set, param_stack_frame_growth, 40);
}
+ if (opts->x_flag_lto_try)
+ {
+#ifdef ENABLE_LTO
+ if (opts_set->x_flag_lto && opts->x_flag_lto)
+ {
+ inform (loc, "%<-flto-try%> don't guarantee that lto "
+ "will be enabled.");
+ }
+ opts->x_flag_lto = "";
+ if (opts_set->x_flag_fat_lto_objects && !opts->x_flag_fat_lto_objects)
+ {
+ error_at (loc, "%<-flto-try%> are not supported with "
+ "-fno-fat-lto-objects");
+ }
+ opts->x_flag_fat_lto_objects = 1;
+#else
+ error_at (loc, "LTO support has not been enabled in this configuration");
+#endif
+ }
+
if (opts->x_flag_lto)
{
#ifdef ENABLE_LTO
--
2.33.0

View File

@ -2,7 +2,7 @@
%global gcc_major 12
# Note, gcc_release must be integer, if you want to add suffixes to
# %%{release}, append them after %%{gcc_release} on Release: line.
%global gcc_release 51
%global gcc_release 54
%global _unpackaged_files_terminate_build 0
%global _performance_build 1
@ -420,6 +420,10 @@ Patch311: 0311-PATCH-Add-if-split-optimization-pass.patch
Patch312: 0312-Add-late-slp-vectorization-pass-with-additional-chec.patch
Patch313: 0313-Add-tracer-transformation-for-static-probabilities.patch
Patch314: 0314-bugfix-Modify-the-hip09-tune-flags.patch
Patch315: 0315-Bugfix-Add-no-var-recored-check-for-ssa_name-in-stru.patch
Patch316: 0316-Use-ai-ability-to-guide-optimization.patch
Patch317: 0317-Bugfix-set-default-value-when-tune_native-is-NULL.patch
Patch318: 0318-add-flag-flto-try.patch
# Part 1001-1999
%ifarch sw_64
@ -1520,6 +1524,10 @@ not stable, so plugins must be rebuilt any time GCC is updated.
%patch -P312 -p1
%patch -P313 -p1
%patch -P314 -p1
%patch -P315 -p1
%patch -P316 -p1
%patch -P317 -p1
%patch -P318 -p1
%ifarch sw_64
%patch -P1001 -p1
@ -3145,6 +3153,7 @@ end
%{_prefix}/libexec/gcc/%{gcc_target_platform}/%{gcc_major}/lto-wrapper
%{_prefix}/libexec/gcc/%{gcc_target_platform}/%{gcc_major}/liblto_plugin.so*
%{_libdir}/bfd-plugins/liblto_plugin.so
%{_prefix}/libexec/gcc/%{gcc_target_platform}/%{gcc_major}/onnx.fdata
%ifarch aarch64
%{_prefix}/libexec/gcc/%{gcc_target_platform}/%{gcc_major}/libbolt_plugin.so*
%endif
@ -4141,6 +4150,24 @@ end
%doc rpm.doc/changelogs/libcc1/ChangeLog*
%changelog
* Wed Dec 04 2024 Zhenyu Zhao <zhaozhenyu17@huawei.com> - 12.3.1-54
- Type:Sync
- ID:NA
- SUG:NA
- DESC:Sync patches from openeuler/gcc.
* Wed Dec 04 2024 Zhenyu Zhao <zhaozhenyu17@huawei.com> - 12.3.1-53
- Type:Sync
- ID:NA
- SUG:NA
- DESC:Sync patches from openeuler/gcc.
* Wed Dec 04 2024 Zhenyu Zhao <zhaozhenyu17@huawei.com> - 12.3.1-52
- Type:Sync
- ID:NA
- SUG:NA
- DESC:Bugfix add no var recored check for ssa_name in struct reorg.
* Wed Dec 04 2024 Mingchuan Wu <wumingchuan1992@foxmail.com> - 12.3.1-51
- Type:Sync
- ID:NA