From nobody Tue Nov 18 15:27:38 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1611084897; cv=none; d=zohomail.com; s=zohoarc; b=KD0BctihK2bA5+7afRnwQ+LYkMI6AfB0ZoQEsXU5JaA/yJnVCafu33QBF95xSBbh2EmDv75jWRZ74K1rSpy6O4URCBUSODM8/VZt4zRLptmoB86K0UfAOkz1n2Fm9gERQV1VrzDBkXsPqJVry9xvB5s0E3u9yQuDkiKiPcuCoLM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611084897; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=zOwDuzEJMzDZCUyUS+fhCAa4pwhGVLCD5TGZAxSX0pU=; b=TluwMKp5CTEBdqhHpYTrW6r91FEgoYXBzGviicay1AZwbmSm9HjwxZ9YQ/nti4ELylzyTWrwbe97d2WzsyA22QIvfuxRjgfsLlc6jpGHTN6B06TfeiQHKu2vXu+EAPsR3vmW4avtdo2sBjx2o1v2cinx7Xz3oCY4nLh9GvpbhA8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1611084897130637.1562415274094; Tue, 19 Jan 2021 11:34:57 -0800 (PST) Received: from localhost ([::1]:37898 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l1wmF-0001Hz-In for importer@patchew.org; Tue, 19 Jan 2021 14:34:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:50046) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l1vq0-0002oD-5f for qemu-devel@nongnu.org; Tue, 19 Jan 2021 13:34:44 -0500 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]:33024) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l1vpx-0001Gh-TP for qemu-devel@nongnu.org; Tue, 19 Jan 2021 13:34:43 -0500 Received: by mail-pj1-x1032.google.com with SMTP id lw17so1807767pjb.0 for ; Tue, 19 Jan 2021 10:34:41 -0800 (PST) Received: from localhost.localdomain (cpe-66-75-72-126.hawaii.res.rr.com. [66.75.72.126]) by smtp.gmail.com with ESMTPSA id a136sm20651359pfd.149.2021.01.19.10.34.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Jan 2021 10:34:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zOwDuzEJMzDZCUyUS+fhCAa4pwhGVLCD5TGZAxSX0pU=; b=rzOUb5ZF/eVr/EnXqI9R72RKraBsGeZqOyoR/rU9u2X8ly60Q9BAnoyHgml96LeAIt lBDiwsL9OOD1U/UuUAseHZRwSq4Aqd9wo9vgjngsDoffnIRtsoVZEOEVCUR0dmeELEYp AtN0+8p+5GACoNhThUBy+a2ygZ8vnQSCTAFxWijvsbTI8I1Um4Z9Ndl+mlhzt7As/ZNS pkmGXT7b6oZR1lcy5whsiVWFQ07q5GK0kow0DMbjZI6wdec+vpBe/9uQU3Qlkmx0pcvh w44aHeBAtWytcKPjeF9xWsl5RduY1eneQ1Md4ZtH4avSIGFJ+Bkppt9MgGrv3MG6+IXU 5JcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zOwDuzEJMzDZCUyUS+fhCAa4pwhGVLCD5TGZAxSX0pU=; b=iTLZeJmGb2280U7YB9dFzZpzDt9mlvFOEIu2GjFqQpUY2GnuE6+AIMPhDwA4eFu/Wa pxJC8IjewbJ05/iKIa6KYhkRNna7JxOYmLQ9SMjamSFSGB+XzXGlMYqvZ6Q0Zyc8jlbf uqlh7BLEJY3A7j9qXEc8U/VKWW2gZ2YD9Vwgt5bRlSkU4MUtRaZCqUXiQT1qsWdifm7h sc3Wdr81bU1DzXnRnUYI5ILCiTOj4W/sly2EX/dYjulo9R9dNBoy2YEWWbgPCWyBSzGY Q+E47CIVwzInVcJUTe8gA5fnDc5OOiKLjngq/P9HxUSvuHuplHQYae7bmErHIpnJgwIQ w9Eg== X-Gm-Message-State: AOAM532YhYVh4xw5FdNhxw9UgmTlLdAWtkxwHXS/jnCYJ+fcA+qgD+Ab bQ92xOpey385HOpSWvP27OcxQW06CYV6aA== X-Google-Smtp-Source: ABdhPJw9yoU5YlYF41miGrl9hFEQV05YtavaRu2k31e7V5sNcp8q9LjMU2UM5pbP9GnHp4UfDnDYRA== X-Received: by 2002:a17:90a:2e88:: with SMTP id r8mr1151552pjd.84.1611081280372; Tue, 19 Jan 2021 10:34:40 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 5/5] tcg: Dynamically allocate temporaries Date: Tue, 19 Jan 2021 08:34:28 -1000 Message-Id: <20210119183428.556706-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210119183428.556706-1-richard.henderson@linaro.org> References: <20210119183428.556706-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1032.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lvivier@redhat.com, alistair23@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The caching of constant temps has resulted in a larger overall usage, overflowing the statically allocated array. Instead, allocate temps as needed, placing pointers to the allocated temps into a GPtrArray for later indexing. Buglink: https://bugs.launchpad.net/bugs/1912065 Signed-off-by: Richard Henderson --- include/tcg/tcg.h | 6 ++-- tcg/tcg.c | 74 +++++++++++++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/include/tcg/tcg.h b/include/tcg/tcg.h index 996addd90c..1da3bce0c8 100644 --- a/include/tcg/tcg.h +++ b/include/tcg/tcg.h @@ -275,7 +275,7 @@ typedef struct TCGPool { =20 #define TCG_POOL_CHUNK_SIZE 32768 =20 -#define TCG_MAX_TEMPS 512 +#define TCG_INIT_TEMPS 512 #define TCG_MAX_INSNS 512 =20 /* when the size of the arguments of a called function is smaller than @@ -696,7 +696,7 @@ struct TCGContext { =20 GHashTable *const_table[TCG_TYPE_COUNT]; TCGTempSet free_temps[TCG_TYPE_COUNT * 2]; - TCGTemp temps[TCG_MAX_TEMPS]; /* globals first, temps after */ + GPtrArray *temps; /* globals first, temps after */ =20 QTAILQ_HEAD(, TCGOp) ops, free_ops; QSIMPLEQ_HEAD(, TCGLabel) labels; @@ -749,7 +749,7 @@ static inline void *tcg_splitwx_to_rw(const void *rx) static inline TCGTemp *tcg_temp(TCGContext *s, size_t idx) { tcg_debug_assert(idx < s->nb_temps); - return &s->temps[idx]; + return g_ptr_array_index(s->temps, idx); } =20 static inline size_t temp_idx(TCGTemp *ts) diff --git a/tcg/tcg.c b/tcg/tcg.c index a505457cee..5ca6860107 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -816,19 +816,24 @@ void tcg_register_thread(void) void tcg_register_thread(void) { MachineState *ms =3D MACHINE(qdev_get_machine()); - TCGContext *s =3D g_malloc(sizeof(*s)); - unsigned int i, n; + TCGContext *s =3D g_memdup(&tcg_init_ctx, sizeof(*s)); + unsigned int i, n =3D tcg_init_ctx.nb_globals; + TCGTemp *glob; bool err; =20 - *s =3D tcg_init_ctx; - - /* Relink mem_base. */ - for (i =3D 0, n =3D tcg_init_ctx.nb_globals; i < n; ++i) { + /* Copy the globals. */ + s->temps =3D g_ptr_array_sized_new(TCG_INIT_TEMPS); + glob =3D g_new(TCGTemp, n); + for (i =3D 0; i < n; ++i) { TCGTemp *its =3D tcg_temp(&tcg_init_ctx, i); + TCGTemp *ots =3D glob + i; + + *ots =3D *its; if (its->mem_base) { - TCGTemp *ots =3D tcg_temp(s, i); - ots->mem_base =3D tcg_temp(s, temp_idx(its->mem_base)); + /* Relink mem_base. */ + ots->mem_base =3D glob + temp_idx(its->mem_base); } + g_ptr_array_add(s->temps, ots); } =20 /* Claim an entry in tcg_ctxs */ @@ -986,11 +991,11 @@ void tcg_context_init(TCGContext *s) =20 memset(s, 0, sizeof(*s)); s->nb_globals =3D 0; + s->temps =3D g_ptr_array_sized_new(TCG_INIT_TEMPS); =20 - /* Count total number of arguments and allocate the corresponding - space */ + /* Count total number of arguments and allocate the corresponding spac= e */ total_args =3D 0; - for(op =3D 0; op < NB_OPS; op++) { + for (op =3D 0; op < NB_OPS; op++) { def =3D &tcg_op_defs[op]; n =3D def->nb_iargs + def->nb_oargs; total_args +=3D n; @@ -998,7 +1003,7 @@ void tcg_context_init(TCGContext *s) =20 args_ct =3D g_new0(TCGArgConstraint, total_args); =20 - for(op =3D 0; op < NB_OPS; op++) { + for (op =3D 0; op < NB_OPS; op++) { def =3D &tcg_op_defs[op]; def->args_ct =3D args_ct; n =3D def->nb_iargs + def->nb_oargs; @@ -1179,11 +1184,13 @@ void tcg_prologue_init(TCGContext *s) void tcg_func_start(TCGContext *s) { tcg_pool_reset(s); + s->nb_temps =3D s->nb_globals; + g_ptr_array_set_size(s->temps, s->nb_globals); =20 /* No temps have been previously allocated for size or locality. */ for (int i =3D 0; i < ARRAY_SIZE(s->free_temps); ++i) { - tempset_init(&s->free_temps[i], TCG_MAX_TEMPS); + tempset_init(&s->free_temps[i], TCG_INIT_TEMPS); } =20 /* No constant temps have been previously allocated. */ @@ -1208,27 +1215,40 @@ void tcg_func_start(TCGContext *s) =20 static TCGTemp *tcg_temp_alloc(TCGContext *s) { - int n =3D s->nb_temps++; TCGTemp *ret; + int n =3D s->nb_temps; =20 - tcg_debug_assert(n < TCG_MAX_TEMPS); - ret =3D &s->temps[n]; + /* Note that TCGTemp.index is 16 bits. */ + tcg_debug_assert(n <=3D UINT16_MAX); + s->nb_temps =3D n + 1; + + /* Non-global temps are allocated from the pool. */ + ret =3D tcg_malloc(sizeof(TCGTemp)); memset(ret, 0, sizeof(TCGTemp)); ret->index =3D n; =20 + g_ptr_array_add(s->temps, ret); return ret; } =20 -static inline TCGTemp *tcg_global_alloc(TCGContext *s) +static TCGTemp *tcg_global_alloc(TCGContext *s) { - TCGTemp *ts; + TCGTemp *ret; + int n =3D s->nb_globals; =20 + /* Note that TCGTemp.index is 16 bits. */ + tcg_debug_assert(n <=3D UINT16_MAX); tcg_debug_assert(s->nb_globals =3D=3D s->nb_temps); - s->nb_globals++; - ts =3D tcg_temp_alloc(s); - ts->kind =3D TEMP_GLOBAL; + s->nb_globals =3D n + 1; + s->nb_temps =3D n + 1; =20 - return ts; + /* Global temps are allocated from the main heap and live forever. */ + ret =3D g_new0(TCGTemp, 1); + ret->index =3D n; + ret->kind =3D TEMP_GLOBAL; + + g_ptr_array_add(s->temps, ret); + return ret; } =20 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type, @@ -1236,9 +1256,7 @@ static TCGTemp *tcg_global_reg_new_internal(TCGContex= t *s, TCGType type, { TCGTemp *ts; =20 - if (TCG_TARGET_REG_BITS =3D=3D 32 && type !=3D TCG_TYPE_I32) { - tcg_abort(); - } + tcg_debug_assert(TCG_TARGET_REG_BITS !=3D 32 || type =3D=3D TCG_TYPE_I= 32); =20 ts =3D tcg_global_alloc(s); ts->base_type =3D type; @@ -1299,7 +1317,7 @@ TCGTemp *tcg_global_mem_new_internal(TCGType type, TC= Gv_ptr base, pstrcat(buf, sizeof(buf), "_0"); ts->name =3D strdup(buf); =20 - tcg_debug_assert(ts2 =3D=3D ts + 1); + tcg_debug_assert(temp_idx(ts2) =3D=3D temp_idx(ts) + 1); ts2->base_type =3D TCG_TYPE_I64; ts2->type =3D TCG_TYPE_I32; ts2->indirect_reg =3D indirect_reg; @@ -1347,7 +1365,7 @@ TCGTemp *tcg_temp_new_internal(TCGType type, bool tem= p_local) ts->temp_allocated =3D 1; ts->kind =3D kind; =20 - tcg_debug_assert(ts2 =3D=3D ts + 1); + tcg_debug_assert(temp_idx(ts2) =3D=3D temp_idx(ts) + 1); ts2->base_type =3D TCG_TYPE_I64; ts2->type =3D TCG_TYPE_I32; ts2->temp_allocated =3D 1; @@ -1456,7 +1474,7 @@ TCGTemp *tcg_constant_internal(TCGType type, int64_t = val) */ ts->val =3D val; =20 - tcg_debug_assert(ts2 =3D=3D ts + 1); + tcg_debug_assert(temp_idx(ts2) =3D=3D temp_idx(ts) + 1); ts2->base_type =3D TCG_TYPE_I64; ts2->type =3D TCG_TYPE_I32; ts2->kind =3D TEMP_CONST; --=20 2.25.1