qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: Anton Johansson <anjo@rev.ng>, qemu-devel@nongnu.org
Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com,
	philmd@linaro.org, alex.bennee@linaro.org
Subject: Re: [RFC PATCH v1 06/43] tcg: Introduce tcg-global-mappings
Date: Fri, 22 Nov 2024 13:14:21 -0600	[thread overview]
Message-ID: <fb4bba88-99a8-46f3-bc32-c7902d909fc6@linaro.org> (raw)
In-Reply-To: <20241121014947.18666-7-anjo@rev.ng>

On 11/20/24 19:49, Anton Johansson wrote:
> Adds a cpu_mapping struct to describe, in a declarative fashion, the
> mapping between fields in a struct, and a corresponding TCG global.  As
> such, tcg_global_mem_new() can be automatically called given an array of
> cpu_mappings.
> 
> This change is not limited to helper-to-tcg, but will be required in
> future commits to map between offsets into CPUArchState and TCGv
> globals in a target-agnostic way.
> 
> Signed-off-by: Anton Johansson <anjo@rev.ng>
> ---
>   include/tcg/tcg-global-mappings.h | 111 ++++++++++++++++++++++++++++++
>   tcg/meson.build                   |   1 +
>   tcg/tcg-global-mappings.c         |  61 ++++++++++++++++
>   3 files changed, 173 insertions(+)
>   create mode 100644 include/tcg/tcg-global-mappings.h
>   create mode 100644 tcg/tcg-global-mappings.c

Plausible.

In the most ideal of cases, helpers would *never* access TCG globals directly.  They would 
always be passed in/out via normal parameters.  I know this sometimes becomes impractical 
with multiple outputs.  But quite often we create TCG globals for e.g. control registers 
which ought not be TCG globals in the first place.  This can often allow helpers to be 
marked TCG_CALL_NO_RWG etc, which helps even out-of-line helpers.

How often is Hexagon referencing globals directly?


r~

> 
> diff --git a/include/tcg/tcg-global-mappings.h b/include/tcg/tcg-global-mappings.h
> new file mode 100644
> index 0000000000..736380fb20
> --- /dev/null
> +++ b/include/tcg/tcg-global-mappings.h
> @@ -0,0 +1,111 @@
> +/*
> + *  Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved.
> + *
> + *  This program is free software; you can redistribute it and/or modify
> + *  it under the terms of the GNU General Public License as published by
> + *  the Free Software Foundation; either version 2 of the License, or
> + *  (at your option) any later version.
> + *
> + *  This program is distributed in the hope that it will be useful,
> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + *  GNU General Public License for more details.
> + *
> + *  You should have received a copy of the GNU General Public License
> + *  along with this program; if not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef TCG_GLOBAL_MAP_H
> +#define TCG_GLOBAL_MAP_H
> +
> +#include "qemu/osdep.h"
> +
> +/**
> + * cpu_tcg_mapping: Declarative mapping of offsets into a struct to global
> + *                  TCGvs.  Parseable by LLVM-based tools.
> + * @tcg_var_name: String name of the TCGv to use as destination of the mapping.
> + * @tcg_var_base_address: Address of the above TCGv.
> + * @cpu_var_names: Array of printable names of TCGvs, used when calling
> + *                 tcg_global_mem_new from init_cpu_tcg_mappings.
> + * @cpu_var_base_offset: Base offset of field in the source struct.
> + * @cpu_var_size: Size of field in the source struct, if the field is an array,
> + *                this holds the size of the element type.
> + * @cpu_var_stride: Stride between array elements in the source struct.  This
> + *                  can be greater than the element size when mapping a field
> + *                  in an array of structs.
> + * @number_of_elements: Number of elements of array in the source struct.
> + */
> +typedef struct cpu_tcg_mapping {
> +    const char *tcg_var_name;
> +    void *tcg_var_base_address;
> +
> +    const char *const *cpu_var_names;
> +    size_t cpu_var_base_offset;
> +    size_t cpu_var_size;
> +    size_t cpu_var_stride;
> +
> +    size_t number_of_elements;
> +} cpu_tcg_mapping;
> +
> +#define STRUCT_SIZEOF_FIELD(S, member) sizeof(((S *)0)->member)
> +
> +#define STRUCT_ARRAY_SIZE(S, array)                                            \
> +    (STRUCT_SIZEOF_FIELD(S, array) / STRUCT_SIZEOF_FIELD(S, array[0]))
> +
> +/*
> + * Following are a few macros that aid in constructing
> + * `cpu_tcg_mapping`s for a few common cases.
> + */
> +
> +/* Map between single CPU register and to TCG global */
> +#define CPU_TCG_MAP(struct_type, tcg_var, cpu_var, name_str)                   \
> +    (cpu_tcg_mapping)                                                          \
> +    {                                                                          \
> +        .tcg_var_name = stringify(tcg_var), .tcg_var_base_address = &tcg_var,  \
> +        .cpu_var_names = (const char *[]){name_str},                           \
> +        .cpu_var_base_offset = offsetof(struct_type, cpu_var),                 \
> +        .cpu_var_size = STRUCT_SIZEOF_FIELD(struct_type, cpu_var),             \
> +        .cpu_var_stride = 0, .number_of_elements = 1,                          \
> +    }
> +
> +/* Map between array of CPU registers and array of TCG globals. */
> +#define CPU_TCG_MAP_ARRAY(struct_type, tcg_var, cpu_var, names)                \
> +    (cpu_tcg_mapping)                                                          \
> +    {                                                                          \
> +        .tcg_var_name = #tcg_var, .tcg_var_base_address = tcg_var,             \
> +        .cpu_var_names = names,                                                \
> +        .cpu_var_base_offset = offsetof(struct_type, cpu_var),                 \
> +        .cpu_var_size = STRUCT_SIZEOF_FIELD(struct_type, cpu_var[0]),          \
> +        .cpu_var_stride = STRUCT_SIZEOF_FIELD(struct_type, cpu_var[0]),        \
> +        .number_of_elements = STRUCT_ARRAY_SIZE(struct_type, cpu_var),         \
> +    }
> +
> +/*
> + * Map between single member in an array of structs to an array
> + * of TCG globals, e.g. maps
> + *
> + *     cpu_state.array_of_structs[i].member
> + *
> + * to
> + *
> + *     tcg_global_member[i]
> + */
> +#define CPU_TCG_MAP_ARRAY_OF_STRUCTS(struct_type, tcg_var, cpu_struct,         \
> +                                     cpu_var, names)                           \
> +    (cpu_tcg_mapping)                                                          \
> +    {                                                                          \
> +        .tcg_var_name = #tcg_var, .tcg_var_base_address = tcg_var,             \
> +        .cpu_var_names = names,                                                \
> +        .cpu_var_base_offset = offsetof(struct_type, cpu_struct[0].cpu_var),   \
> +        .cpu_var_size =                                                        \
> +            STRUCT_SIZEOF_FIELD(struct_type, cpu_struct[0].cpu_var),           \
> +        .cpu_var_stride = STRUCT_SIZEOF_FIELD(struct_type, cpu_struct[0]),     \
> +        .number_of_elements = STRUCT_ARRAY_SIZE(struct_type, cpu_struct),      \
> +    }
> +
> +extern cpu_tcg_mapping tcg_global_mappings[];
> +extern size_t tcg_global_mapping_count;
> +
> +void init_cpu_tcg_mappings(cpu_tcg_mapping *mappings, size_t size);
> +
> +#endif /* TCG_GLOBAL_MAP_H */
> diff --git a/tcg/meson.build b/tcg/meson.build
> index 69ebb4908a..a0d6b09d85 100644
> --- a/tcg/meson.build
> +++ b/tcg/meson.build
> @@ -13,6 +13,7 @@ tcg_ss.add(files(
>     'tcg-op-ldst.c',
>     'tcg-op-gvec.c',
>     'tcg-op-vec.c',
> +  'tcg-global-mappings.c',
>   ))
>   
>   if get_option('tcg_interpreter')
> diff --git a/tcg/tcg-global-mappings.c b/tcg/tcg-global-mappings.c
> new file mode 100644
> index 0000000000..cc1f07fae4
> --- /dev/null
> +++ b/tcg/tcg-global-mappings.c
> @@ -0,0 +1,61 @@
> +/*
> + *  Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved.
> + *
> + *  This program is free software; you can redistribute it and/or modify
> + *  it under the terms of the GNU General Public License as published by
> + *  the Free Software Foundation; either version 2 of the License, or
> + *  (at your option) any later version.
> + *
> + *  This program is distributed in the hope that it will be useful,
> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + *  GNU General Public License for more details.
> + *
> + *  You should have received a copy of the GNU General Public License
> + *  along with this program; if not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#include "tcg/tcg-global-mappings.h"
> +#include "tcg/tcg-op-common.h"
> +#include "tcg/tcg.h"
> +
> +void init_cpu_tcg_mappings(cpu_tcg_mapping *mappings, size_t size)
> +{
> +    uintptr_t tcg_addr;
> +    size_t cpu_offset;
> +    const char *name;
> +    cpu_tcg_mapping m;
> +
> +    /*
> +     * Paranoid assertion, this should always hold since
> +     * they're typedef'd to pointers. But you never know!
> +     */
> +    g_assert(sizeof(TCGv_i32) == sizeof(TCGv_i64));
> +
> +    /*
> +     * Loop over entries in tcg_global_mappings and
> +     * create the `mapped to` TCGv's.
> +     */
> +    for (int i = 0; i < size; ++i) {
> +        m = mappings[i];
> +
> +        for (int j = 0; j < m.number_of_elements; ++j) {
> +            /*
> +             * Here we are using the fact that
> +             * sizeof(TCGv_i32) == sizeof(TCGv_i64) == sizeof(TCGv)
> +             */
> +            assert(sizeof(TCGv_i32) == sizeof(TCGv_i64));
> +            tcg_addr = (uintptr_t)m.tcg_var_base_address + j * sizeof(TCGv_i32);
> +            cpu_offset = m.cpu_var_base_offset + j * m.cpu_var_stride;
> +            name = m.cpu_var_names[j];
> +
> +            if (m.cpu_var_size < 8) {
> +                *(TCGv_i32 *)tcg_addr =
> +                    tcg_global_mem_new_i32(tcg_env, cpu_offset, name);
> +            } else {
> +                *(TCGv_i64 *)tcg_addr =
> +                    tcg_global_mem_new_i64(tcg_env, cpu_offset, name);
> +            }
> +        }
> +    }
> +}



  reply	other threads:[~2024-11-22 19:15 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-21  1:49 [RFC PATCH v1 00/43] Introduce helper-to-tcg Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 01/43] Add option to enable/disable helper-to-tcg Anton Johansson via
2024-11-22 17:30   ` Richard Henderson
2024-11-22 18:23     ` Paolo Bonzini
2024-12-03 19:05       ` Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 02/43] accel/tcg: Add bitreverse and funnel-shift runtime helper functions Anton Johansson via
2024-11-22 17:35   ` Richard Henderson
2024-12-03 17:50     ` Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 03/43] accel/tcg: Add gvec size changing operations Anton Johansson via
2024-11-22 17:50   ` Richard Henderson
2024-12-03 18:08     ` Anton Johansson via
2024-12-03 18:57       ` Richard Henderson
2024-12-03 20:15         ` Anton Johansson via
2024-12-03 21:14           ` Richard Henderson
2024-11-21  1:49 ` [RFC PATCH v1 04/43] tcg: Add gvec functions for creating consant vectors Anton Johansson via
2024-11-22 18:00   ` Richard Henderson
2024-12-03 18:19     ` Anton Johansson via
2024-12-03 19:03       ` Richard Henderson
2024-11-21  1:49 ` [RFC PATCH v1 05/43] tcg: Add helper function dispatcher and hook tcg_gen_callN Anton Johansson via
2024-11-22 18:04   ` Richard Henderson
2024-12-03 18:45     ` Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 06/43] tcg: Introduce tcg-global-mappings Anton Johansson via
2024-11-22 19:14   ` Richard Henderson [this message]
2024-11-21  1:49 ` [RFC PATCH v1 07/43] tcg: Increase maximum TB size and maximum temporaries Anton Johansson via
2024-11-22 18:11   ` Richard Henderson
2024-11-21  1:49 ` [RFC PATCH v1 08/43] include/helper-to-tcg: Introduce annotate.h Anton Johansson via
2024-11-22 18:12   ` Richard Henderson
2024-11-25 11:27     ` Philippe Mathieu-Daudé
2024-12-03 19:00       ` Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 09/43] helper-to-tcg: Introduce get-llvm-ir.py Anton Johansson via
2024-11-22 18:14   ` Richard Henderson
2024-12-03 18:49     ` Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 10/43] helper-to-tcg: Add meson.build Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 11/43] helper-to-tcg: Introduce llvm-compat Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 12/43] helper-to-tcg: Introduce custom LLVM pipeline Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 13/43] helper-to-tcg: Introduce Error.h Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 14/43] helper-to-tcg: Introduce PrepareForOptPass Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 15/43] helper-to-tcg: PrepareForOptPass, map annotations Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 16/43] helper-to-tcg: PrepareForOptPass, Cull unused functions Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 17/43] helper-to-tcg: PrepareForOptPass, undef llvm.returnaddress Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 18/43] helper-to-tcg: PrepareForOptPass, Remove noinline attribute Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 19/43] helper-to-tcg: Pipeline, run optimization pass Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 20/43] helper-to-tcg: Introduce pseudo instructions Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 21/43] helper-to-tcg: Introduce PrepareForTcgPass Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 22/43] helper-to-tcg: PrepareForTcgPass, remove functions w. cycles Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 23/43] helper-to-tcg: PrepareForTcgPass, demote phi nodes Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 24/43] helper-to-tcg: PrepareForTcgPass, map TCG globals Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 25/43] helper-to-tcg: PrepareForTcgPass, transform GEPs Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 26/43] helper-to-tcg: PrepareForTcgPass, canonicalize IR Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 27/43] helper-to-tcg: PrepareForTcgPass, identity map trivial expressions Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 28/43] helper-to-tcg: Introduce TcgType.h Anton Johansson via
2024-11-22 18:26   ` Richard Henderson
2024-12-03 18:50     ` Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 29/43] helper-to-tcg: Introduce TCG register allocation Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 30/43] helper-to-tcg: TcgGenPass, introduce TcgEmit.[cpp|h] Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 31/43] helper-to-tcg: Introduce TcgGenPass Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 32/43] helper-to-tcg: Add README Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 33/43] helper-to-tcg: Add end-to-end tests Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 34/43] target/hexagon: Add get_tb_mmu_index() Anton Johansson via
2024-11-22 18:34   ` Richard Henderson
2024-12-03 18:50     ` Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 35/43] target/hexagon: Use argparse in all python scripts Anton Johansson via
2024-12-05 15:23   ` Brian Cain
2024-11-21  1:49 ` [RFC PATCH v1 36/43] target/hexagon: Add temporary vector storage Anton Johansson via
2024-11-22 18:35   ` Richard Henderson
2024-12-03 18:56     ` Anton Johansson via
2024-12-03 20:28       ` Brian Cain
2024-12-04  0:37         ` ltaylorsimpson
2024-11-21  1:49 ` [RFC PATCH v1 37/43] target/hexagon: Make HVX vector args. restrict * Anton Johansson via
2024-11-25 11:36   ` Philippe Mathieu-Daudé
2024-11-25 12:00     ` Paolo Bonzini
2024-12-03 18:57       ` Anton Johansson via
2024-12-03 18:58         ` Brian Cain
2024-11-21  1:49 ` [RFC PATCH v1 38/43] target/hexagon: Use cpu_mapping to map env -> TCG Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 39/43] target/hexagon: Keep gen_slotval/check_noshuf for helper-to-tcg Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 40/43] target/hexagon: Emit annotations for helpers Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 41/43] target/hexagon: Manually call generated HVX instructions Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 42/43] target/hexagon: Only translate w. idef-parser if helper-to-tcg failed Anton Johansson via
2024-11-21  1:49 ` [RFC PATCH v1 43/43] target/hexagon: Use helper-to-tcg Anton Johansson via
2024-11-25 11:34 ` [RFC PATCH v1 00/43] Introduce helper-to-tcg Philippe Mathieu-Daudé
2024-12-03 18:58   ` Anton Johansson via

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fb4bba88-99a8-46f3-bc32-c7902d909fc6@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=ale@rev.ng \
    --cc=alex.bennee@linaro.org \
    --cc=anjo@rev.ng \
    --cc=bcain@quicinc.com \
    --cc=ltaylorsimpson@gmail.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).