summaryrefslogtreecommitdiff
path: root/0006-simdmath-Enable-simdmath-on-kunpeng.patch
blob: 9d7bb57338f2d0fc0ced140893e7a152a6b9e46f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
From cfd6920125f7968f0c1f5cb225f9fbd5bc8988b9 Mon Sep 17 00:00:00 2001
From: bule <bule1@huawei.com>
Date: Tue, 13 Jul 2021 15:26:54 +0800
Subject: [PATCH 06/13] [simdmath] Enable simdmath on kunpeng

This enable simd math function supported by libmathlib on fortran/c/c++.
Use -fsimdmath to turn on the generation of simdmath function. The
supported functions can be found in simdmath.h. Add more simd declaration
if you need more kinds of math functions. -msimdmath-64 is used to turn
on 64-bit simd math functions which is not supported by libmathlib.
Therefore, this option is default to off.

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index c51d6d34726..dc1a8984871 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -780,6 +780,10 @@ c_common_post_options (const char **pfilename)
   if (cpp_opts->deps.style == DEPS_NONE)
     check_deps_environment_vars ();
 
+  if (flag_simdmath)
+    {
+      defer_opt (OPT_include, "simdmath.h");
+    }
   handle_deferred_opts ();
 
   sanitize_cpp_opts ();
diff --git a/gcc/common.opt b/gcc/common.opt
index ec5235c3a41..8eb05570418 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1977,6 +1977,10 @@ fmath-errno
 Common Report Var(flag_errno_math) Init(1) Optimization SetByCombined
 Set errno after built-in math functions.
 
+fsimdmath
+Common Report Var(flag_simdmath) Init(0) Optimization
+Enable auto-vectorize math functions for mathlib.  This option will turn on -fno-math-errno and -fopenmp-simd.
+
 fmax-errors=
 Common Joined RejectNegative UInteger Var(flag_max_errors)
 -fmax-errors=<number>	Maximum number of errors to report.
diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c
index 9b400c49ac6..79dc8f186f4 100644
--- a/gcc/config/aarch64/aarch64.c
+++ b/gcc/config/aarch64/aarch64.c
@@ -23077,8 +23077,12 @@ aarch64_simd_clone_compute_vecsize_and_simdlen (struct cgraph_node *node,
   elt_bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (base_type));
   if (clonei->simdlen == 0)
     {
-      count = 2;
-      vec_bits = (num == 0 ? 64 : 128);
+      /* Currently mathlib or sleef hasn't provide function for V2SF mode
+      simdclone of single precision functions. (e.g._ZCVnN2v_expf)
+      Therefore this mode is disabled by default to avoid link error.
+      Use -msimdmath-64 option to enable this mode.  */
+      count = flag_simdmath_64 ? 2 : 1;
+      vec_bits = ((num == 0 && flag_simdmath_64) ? 64 : 128);
       clonei->simdlen = vec_bits / elt_bits;
     }
   else
diff --git a/gcc/config/aarch64/aarch64.opt b/gcc/config/aarch64/aarch64.opt
index 1b3d942e0f5..4539156d6f4 100644
--- a/gcc/config/aarch64/aarch64.opt
+++ b/gcc/config/aarch64/aarch64.opt
@@ -190,6 +190,12 @@ precision of square root results to about 16 bits for
 single precision and to 32 bits for double precision.
 If enabled, it implies -mlow-precision-recip-sqrt.
 
+msimdmath-64
+Target Var(flag_simdmath_64) Optimization
+Allow compiler to generate V2SF 64 bits simdclone of math functions,
+which is not currently supported in mathlib or sleef.
+Therefore this option is disabled by default.
+
 mlow-precision-div
 Target Var(flag_mlow_precision_div) Optimization
 Enable the division approximation.  Enabling this reduces
diff --git a/gcc/fortran/scanner.c b/gcc/fortran/scanner.c
index 6f93508f934..42fd5a8be1e 100644
--- a/gcc/fortran/scanner.c
+++ b/gcc/fortran/scanner.c
@@ -2737,6 +2737,10 @@ gfc_new_file (void)
       && !load_file (flag_pre_include, NULL, false))
     exit (FATAL_EXIT_CODE);
 
+  if (flag_simdmath
+      && !load_file ("simdmath_f.h", NULL, false))
+    exit (FATAL_EXIT_CODE);
+
   if (gfc_cpp_enabled ())
     {
       result = gfc_cpp_preprocess (gfc_source_file);
diff --git a/gcc/opts.c b/gcc/opts.c
index 73162528938..e31aa560564 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -189,6 +189,7 @@ static const char use_diagnosed_msg[] = N_("Uses of this option are diagnosed.")
 
 typedef char *char_p; /* For DEF_VEC_P.  */
 
+static void set_simdmath_flags (struct gcc_options *opts, int set);
 static void set_debug_level (enum debug_info_type type, int extended,
 			     const char *arg, struct gcc_options *opts,
 			     struct gcc_options *opts_set,
@@ -2469,6 +2470,10 @@ common_handle_option (struct gcc_options *opts,
       dc->min_margin_width = value;
       break;
 
+    case OPT_fsimdmath:
+      set_simdmath_flags (opts, value);
+      break;
+
     case OPT_fdump_:
       /* Deferred.  */
       break;
@@ -2847,6 +2852,18 @@ common_handle_option (struct gcc_options *opts,
   return true;
 }
 
+/* The following routines are used to set -fno-math-errno and -fopenmp-simd
+   to enable vector mathlib.  */
+static void
+set_simdmath_flags (struct gcc_options *opts, int set)
+{
+  if (set)
+    {
+      opts->x_flag_errno_math = 0;
+      opts->x_flag_openmp_simd = 1;
+    }
+}
+
 /* Used to set the level of strict aliasing warnings in OPTS,
    when no level is specified (i.e., when -Wstrict-aliasing, and not
    -Wstrict-aliasing=level was given).
diff --git a/libgomp/Makefile.am b/libgomp/Makefile.am
index 669b9e4defd..0d9cc96481c 100644
--- a/libgomp/Makefile.am
+++ b/libgomp/Makefile.am
@@ -74,10 +74,10 @@ libgomp_la_SOURCES += openacc.f90
 endif
 
 nodist_noinst_HEADERS = libgomp_f.h
-nodist_libsubinclude_HEADERS = omp.h openacc.h acc_prof.h
+nodist_libsubinclude_HEADERS = omp.h openacc.h acc_prof.h simdmath.h
 if USE_FORTRAN
 nodist_finclude_HEADERS = omp_lib.h omp_lib.f90 omp_lib.mod omp_lib_kinds.mod \
-	openacc_lib.h openacc.f90 openacc.mod openacc_kinds.mod
+	openacc_lib.h openacc.f90 openacc.mod openacc_kinds.mod simdmath_f.h
 endif
 
 LTLDFLAGS = $(shell $(SHELL) $(top_srcdir)/../libtool-ldflags $(LDFLAGS))
diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index ae5d9d54705..dd4b334895e 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -148,7 +148,7 @@ am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log \
  configure.lineno config.status.lineno
 mkinstalldirs = $(SHELL) $(top_srcdir)/../mkinstalldirs
 CONFIG_HEADER = config.h
-CONFIG_CLEAN_FILES = omp.h omp_lib.h omp_lib.f90 libgomp_f.h \
+CONFIG_CLEAN_FILES = omp.h omp_lib.h simdmath.h simdmath_f.h omp_lib.f90 libgomp_f.h \
 	libgomp.spec
 CONFIG_CLEAN_VPATH_FILES =
 am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
@@ -609,9 +609,9 @@ libgomp_la_SOURCES = alloc.c atomic.c barrier.c critical.c env.c \
 @PLUGIN_GCN_TRUE@libgomp_plugin_gcn_la_LIBADD = libgomp.la $(PLUGIN_GCN_LIBS)
 @PLUGIN_GCN_TRUE@libgomp_plugin_gcn_la_LIBTOOLFLAGS = --tag=disable-static
 nodist_noinst_HEADERS = libgomp_f.h
-nodist_libsubinclude_HEADERS = omp.h openacc.h acc_prof.h
+nodist_libsubinclude_HEADERS = omp.h openacc.h acc_prof.h simdmath.h
 @USE_FORTRAN_TRUE@nodist_finclude_HEADERS = omp_lib.h omp_lib.f90 omp_lib.mod omp_lib_kinds.mod \
-@USE_FORTRAN_TRUE@	openacc_lib.h openacc.f90 openacc.mod openacc_kinds.mod
+@USE_FORTRAN_TRUE@	openacc_lib.h openacc.f90 openacc.mod openacc_kinds.mod simdmath_f.h
 
 LTLDFLAGS = $(shell $(SHELL) $(top_srcdir)/../libtool-ldflags $(LDFLAGS))
 LINK = $(LIBTOOL) --tag CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link \
@@ -702,6 +702,10 @@ omp.h: $(top_builddir)/config.status $(srcdir)/omp.h.in
 	cd $(top_builddir) && $(SHELL) ./config.status $@
 omp_lib.h: $(top_builddir)/config.status $(srcdir)/omp_lib.h.in
 	cd $(top_builddir) && $(SHELL) ./config.status $@
+simdmath_f.h: $(top_builddir)/config.status $(srcdir)/simdmath_f.h.in
+	cd $(top_builddir) && $(SHELL) ./config.status $@
+simdmath.h: $(top_builddir)/config.status $(srcdir)/simdmath.h.in
+	cd $(top_builddir) && $(SHELL) ./config.status $@
 omp_lib.f90: $(top_builddir)/config.status $(srcdir)/omp_lib.f90.in
 	cd $(top_builddir) && $(SHELL) ./config.status $@
 libgomp_f.h: $(top_builddir)/config.status $(srcdir)/libgomp_f.h.in
diff --git a/libgomp/configure b/libgomp/configure
index 5240f7e9d39..b03036c2738 100644
--- a/libgomp/configure
+++ b/libgomp/configure
@@ -17050,7 +17050,7 @@ fi
 
 
 
-ac_config_files="$ac_config_files omp.h omp_lib.h omp_lib.f90 libgomp_f.h"
+ac_config_files="$ac_config_files omp.h omp_lib.h simdmath.h simdmath_f.h omp_lib.f90 libgomp_f.h"
 
 ac_config_files="$ac_config_files Makefile testsuite/Makefile libgomp.spec"
 
@@ -18205,6 +18205,8 @@ do
     "libtool") CONFIG_COMMANDS="$CONFIG_COMMANDS libtool" ;;
     "omp.h") CONFIG_FILES="$CONFIG_FILES omp.h" ;;
     "omp_lib.h") CONFIG_FILES="$CONFIG_FILES omp_lib.h" ;;
+    "simdmath.h") CONFIG_FILES="$CONFIG_FILES simdmath.h" ;;
+    "simdmath_f.h") CONFIG_FILES="$CONFIG_FILES simdmath_f.h" ;;
     "omp_lib.f90") CONFIG_FILES="$CONFIG_FILES omp_lib.f90" ;;
     "libgomp_f.h") CONFIG_FILES="$CONFIG_FILES libgomp_f.h" ;;
     "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;;
diff --git a/libgomp/configure.ac b/libgomp/configure.ac
index ef5d293c31e..569c2065a66 100644
--- a/libgomp/configure.ac
+++ b/libgomp/configure.ac
@@ -433,7 +433,7 @@ CFLAGS="$save_CFLAGS"
 # Determine what GCC version number to use in filesystem paths.
 GCC_BASE_VER
 
-AC_CONFIG_FILES(omp.h omp_lib.h omp_lib.f90 libgomp_f.h)
+AC_CONFIG_FILES(omp.h omp_lib.h simdmath.h simdmath_f.h omp_lib.f90 libgomp_f.h)
 AC_CONFIG_FILES(Makefile testsuite/Makefile libgomp.spec)
 AC_CONFIG_FILES([testsuite/libgomp-test-support.pt.exp:testsuite/libgomp-test-support.exp.in])
 AC_CONFIG_FILES([testsuite/libgomp-site-extra.exp])
diff --git a/libgomp/simdmath.h.in b/libgomp/simdmath.h.in
new file mode 100644
index 00000000000..ab91a4ec317
--- /dev/null
+++ b/libgomp/simdmath.h.in
@@ -0,0 +1,40 @@
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#pragma omp declare simd simdlen(2) notinbranch
+double cos (double x);
+
+#pragma omp declare simd simdlen(4) notinbranch
+float cosf (float x);
+
+#pragma omp declare simd simdlen(2) notinbranch
+double sin (double x);
+
+#pragma omp declare simd simdlen(4) notinbranch
+float sinf (float x);
+
+#pragma omp declare simd simdlen(2) notinbranch
+double exp (double x);
+
+#pragma omp declare simd simdlen(4) notinbranch
+float expf (float x);
+
+#pragma omp declare simd simdlen(2) notinbranch
+double log (double x);
+
+#pragma omp declare simd simdlen(4) notinbranch
+float logf (float x);
+
+#pragma omp declare simd simdlen(2) notinbranch
+double pow (double x, double y);
+
+#pragma omp declare simd simdlen(4) notinbranch
+float powf (float x, float y);
+
+#pragma omp declare simd simdlen(4) notinbranch
+float exp2f (float x);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
diff --git a/libgomp/simdmath_f.h.in b/libgomp/simdmath_f.h.in
new file mode 100644
index 00000000000..550595015db
--- /dev/null
+++ b/libgomp/simdmath_f.h.in
@@ -0,0 +1,11 @@
+!GCC$ builtin (cos) attributes simd (notinbranch)
+!GCC$ builtin (cosf) attributes simd (notinbranch)
+!GCC$ builtin (sin) attributes simd (notinbranch)
+!GCC$ builtin (sinf) attributes simd (notinbranch)
+!GCC$ builtin (exp) attributes simd (notinbranch)
+!GCC$ builtin (expf) attributes simd (notinbranch)
+!GCC$ builtin (exp2f) attributes simd (notinbranch)
+!GCC$ builtin (log) attributes simd (notinbranch)
+!GCC$ builtin (logf) attributes simd (notinbranch)
+!GCC$ builtin (pow) attributes simd (notinbranch)
+!GCC$ builtin (powf) attributes simd (notinbranch)
-- 
2.21.0.windows.1