Re: [PATCH v2 2/6] LoongArch: genopts: Add infrastructure to generate code for new features in ISA evolution

2023-11-20 Thread Xi Ruoyao
On Mon, 2023-11-20 at 23:15 +, Joseph Myers wrote:
> On Sat, 18 Nov 2023, Xi Ruoyao wrote:
> 
> > diff --git a/gcc/config/loongarch/loongarch-def.h 
> > b/gcc/config/loongarch/loongarch-def.h
> > index b319cded456..6123c8e0f19 100644
> > --- a/gcc/config/loongarch/loongarch-def.h
> > +++ b/gcc/config/loongarch/loongarch-def.h
> > @@ -46,6 +46,7 @@ along with GCC; see the file COPYING3.  If not see
> >  #ifndef LOONGARCH_DEF_H
> >  #define LOONGARCH_DEF_H
> >  
> > +#include 
> >  #include "loongarch-tune.h"
> >  
> >  #ifdef __cplusplus
> 
> This has broken libgcc builds when target libc isn't yet available.
> 
> In file included from 
> /scratch/jmyers/glibc-bot/src/gcc/libgcc/../gcc/config/loongarch/loongarch-def.h:49,
>  from 
> /scratch/jmyers/glibc-bot/src/gcc/libgcc/../gcc/config/loongarch/loongarch-opts.h:24,
>  from ../.././gcc/options.h:8,
>  from ../.././gcc/tm.h:49,
>  from /scratch/jmyers/glibc-bot/src/gcc/libgcc/libgcc2.c:29:
> /scratch/jmyers/glibc-bot/build/compilers/loongarch64-linux-gnu-lp64d/gcc-first/gcc/include/stdint.h:9:16:
>  fatal error: stdint.h: No such file or directory
>     9 | # include_next 
>   |    ^~
> compilation terminated.
> make[3]: *** [Makefile:505: _muldi3.o] Error 1
> 
> https://sourceware.org/pipermail/libc-testresults/2023q4/012109.html
> 
> My guess would be that the definitions needing  are not actually 
> needed in code built for the target, and so there should be more
> 
> #if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS) && !defined(IN_RTS)
> 
> conditions like the one already present in loongarch-opts.h, to avoid 
> depending on a target header like this being present.

Ah, makes sense.  I thought stdint.h was usable without libc but it's
not true.

The only use for stdint.h is in struct loongarch_isa (using int64_t for
a 64-bit bitset).  This struct is not used by target code.

I'll test building a cross compiler from x86_64 with stdint.h and struct
loongarch_isa guarded with the ifdef.


-- 
Xi Ruoyao 
School of Aerospace Science and Technology, Xidian University


Re: [PATCH v2 2/6] LoongArch: genopts: Add infrastructure to generate code for new features in ISA evolution

2023-11-20 Thread Joseph Myers
On Sat, 18 Nov 2023, Xi Ruoyao wrote:

> diff --git a/gcc/config/loongarch/loongarch-def.h 
> b/gcc/config/loongarch/loongarch-def.h
> index b319cded456..6123c8e0f19 100644
> --- a/gcc/config/loongarch/loongarch-def.h
> +++ b/gcc/config/loongarch/loongarch-def.h
> @@ -46,6 +46,7 @@ along with GCC; see the file COPYING3.  If not see
>  #ifndef LOONGARCH_DEF_H
>  #define LOONGARCH_DEF_H
>  
> +#include 
>  #include "loongarch-tune.h"
>  
>  #ifdef __cplusplus

This has broken libgcc builds when target libc isn't yet available.

In file included from 
/scratch/jmyers/glibc-bot/src/gcc/libgcc/../gcc/config/loongarch/loongarch-def.h:49,
 from 
/scratch/jmyers/glibc-bot/src/gcc/libgcc/../gcc/config/loongarch/loongarch-opts.h:24,
 from ../.././gcc/options.h:8,
 from ../.././gcc/tm.h:49,
 from /scratch/jmyers/glibc-bot/src/gcc/libgcc/libgcc2.c:29:
/scratch/jmyers/glibc-bot/build/compilers/loongarch64-linux-gnu-lp64d/gcc-first/gcc/include/stdint.h:9:16:
 fatal error: stdint.h: No such file or directory
9 | # include_next 
  |^~
compilation terminated.
make[3]: *** [Makefile:505: _muldi3.o] Error 1

https://sourceware.org/pipermail/libc-testresults/2023q4/012109.html

My guess would be that the definitions needing  are not actually 
needed in code built for the target, and so there should be more

#if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS) && !defined(IN_RTS)

conditions like the one already present in loongarch-opts.h, to avoid 
depending on a target header like this being present.

(Alternatively, one could argue for building much libgcc code with 
-ffreestanding, which would also avoid GCC's  expecting a libc 
header to be available - but that would have more risk for other targets.)

-- 
Joseph S. Myers
jos...@codesourcery.com


[PATCH v2 2/6] LoongArch: genopts: Add infrastructure to generate code for new features in ISA evolution

2023-11-17 Thread Xi Ruoyao
LoongArch v1.10 introduced the concept of ISA evolution.  During ISA
evolution, many independent features can be added and enumerated via
CPUCFG.

Add a data file into genopts storing the CPUCFG word, bit, the name
of the command line option controlling if this feature should be used
for compilation, and the text description.  Make genstr.sh process these
info and add the command line options into loongarch.opt and
loongarch-str.h, and generate a new file loongarch-cpucfg-map.h for
mapping CPUCFG output to the corresponding option.  When handling
-march=native, use the information in loongarch-cpucfg-map.h to generate
the corresponding option mask.  Enable the features implied by -march
setting unless the user has explicitly disabled the feature.

The added options (-mdiv32 and -mld-seq-sa) are not really handled yet.
They'll be used in the following patches.

gcc/ChangeLog:

* config/loongarch/genopts/isa-evolution.in: New data file.
* config/loongarch/genopts/genstr.sh: Translate info in
isa-evolution.in when generating loongarch-str.h, loongarch.opt,
and loongarch-cpucfg-map.h.
* config/loongarch/genopts/loongarch.opt.in (isa_evolution):
New variable.
* config/loongarch/t-loongarch: (loongarch-cpucfg-map.h): New
rule.
(loongarch-str.h): Depend on isa-evolution.in.
(loongarch.opt): Depend on isa-evolution.in.
(loongarch-cpu.o): Depend on loongarch-cpucfg-map.h.
* config/loongarch/loongarch-str.h: Regenerate.
* config/loongarch/loongarch-def.h (loongarch_isa):  Add field
for evolution features.  Add helper function to enable features
in this field.
Probe native CPU capability and save the corresponding options
into preset.
* config/loongarch/loongarch-cpu.cc (fill_native_cpu_config):
Probe native CPU capability and save the corresponding options
into preset.
(cache_cpucfg): Simplify with C++11-style for loop.
(cpucfg_useful_idx, N_CPUCFG_WORDS): Move to ...
* config/loongarch/loongarch.cc
(loongarch_option_override_internal): Enable the ISA evolution
feature options implied by -march and not explicitly disabled.
(loongarch_asm_code_end): New function, print ISA information as
comments in the assembly if -fverbose-asm.  It makes easier to
debug things like -march=native.
(TARGET_ASM_CODE_END): Define.
* config/loongarch/loongarch.opt: Regenerate.
* config/loongarch/loongarch-cpucfg-map.h: Generate.
(cpucfg_useful_idx, N_CPUCFG_WORDS) ... here.
---
 gcc/config/loongarch/genopts/genstr.sh| 92 ++-
 gcc/config/loongarch/genopts/isa-evolution.in |  2 +
 gcc/config/loongarch/genopts/loongarch.opt.in |  7 ++
 gcc/config/loongarch/loongarch-cpu.cc | 46 +-
 gcc/config/loongarch/loongarch-cpucfg-map.h   | 48 ++
 gcc/config/loongarch/loongarch-def.h  |  7 ++
 gcc/config/loongarch/loongarch-str.h  |  7 +-
 gcc/config/loongarch/loongarch.cc | 31 +++
 gcc/config/loongarch/loongarch.opt| 20 +++-
 gcc/config/loongarch/t-loongarch  | 21 -
 10 files changed, 245 insertions(+), 36 deletions(-)
 create mode 100644 gcc/config/loongarch/genopts/isa-evolution.in
 create mode 100644 gcc/config/loongarch/loongarch-cpucfg-map.h

diff --git a/gcc/config/loongarch/genopts/genstr.sh 
b/gcc/config/loongarch/genopts/genstr.sh
index 04e785576bb..cc83496ae38 100755
--- a/gcc/config/loongarch/genopts/genstr.sh
+++ b/gcc/config/loongarch/genopts/genstr.sh
@@ -25,8 +25,8 @@ cd "$(dirname "$0")"
 # Generate a header containing definitions from the string table.
 gen_defines() {
 cat .  */
+
+#ifndef LOONGARCH_CPUCFG_MAP_H
+#define LOONGARCH_CPUCFG_MAP_H
+
+#include "options.h"
+
+static constexpr struct {
+  int cpucfg_word;
+  unsigned int cpucfg_bit;
+  HOST_WIDE_INT isa_evolution_bit;
+} cpucfg_map[] = {
+EOF
+
+# Generate the strings from isa-evolution.in.
+awk '{
+  gsub(/-/, "_", $3)
+  print("  { "$1", 1u << "$2", OPTION_MASK_ISA_"toupper($3)" },")
+}' isa-evolution.in
+
+echo "};"
+echo
+echo "static constexpr int cpucfg_useful_idx[] = {"
+
+awk 'BEGIN { print("  0,\n  1,\n  2,\n  16,\n  17,\n  18,\n  19,") }
+{if ($1+0 > max+0) max=$1; print("  "$1",")}' \
+   isa-evolution.in | sort -n | uniq
+
+echo "};"
+echo ""
+
+awk 'BEGIN { max=19 }
+{ if ($1+0 > max+0) max=$1 }
+END { print "static constexpr int N_CPUCFG_WORDS = "1+max";" }' \
+   isa-evolution.in
+
+echo "#endif /* LOONGARCH_CPUCFG_MAP_H */"
 }
 
 main() {
 case "$1" in
+   cpucfg-map) gen_cpucfg_map;;
header) gen_defines;;
opt) gen_options;;
-   *) echo "Unknown Command: \"$1\". Available: header, opt"; exit 1;;
+   *) echo "Unknown Command: \"$1\". Available: cpucfg-ma