From patchwork Fri Dec 1 15:10:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 81117 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E762C385EC55 for ; Fri, 1 Dec 2023 15:11:15 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id B9F133856DD4 for ; Fri, 1 Dec 2023 15:10:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B9F133856DD4 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B9F133856DD4 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701443459; cv=none; b=dtIB3nuINo46yhSA4CNYIgRsPwkjI+7v7DftBUqL58Da4v6qR84bXDdZqfyKZOnTlM/7KL78M2K0MNfcv0QuqlfrEmlv6W8le8KTymPEka7+1Ft12CW4nVx23hIF5sx7aCx+MKDnT3jdSNb8wPOOS6i5+X3vfbui+ejAEZmFsw4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701443459; c=relaxed/simple; bh=BE7p4xeuu7NNBfZ8jWy1rN9Itbeef7xMBMHSnq4uaUk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Yf17c7pR8/5sqJRx007a+DFMRRbcIKD4ysENxPAg7lYth/Vy6g7sacYDlHXzk8zAokIcUV8yVMIfEpeciMh4zYz/s7OITpttEo+hI1ZpJ4eZ/6V8uuESlgXbYNZms95673/OgM4WWrngUa1Dt3GzGseiyBEFXHdcnsMPj4BsnK0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1d04c097e34so5029995ad.0 for ; Fri, 01 Dec 2023 07:10:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1701443457; x=1702048257; darn=gcc.gnu.org; h=mime-version:user-agent:message-id:in-reply-to:date:references :organization:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=fMEhtv4c2Bh6ZGYPwnRCCPWZ2C7BFqveCchwqwH457c=; b=UJTlvZ1NaCIZs7jXMVfDUoOgyjb9eKcJ8VF3ozCv51DVJsZq/6tVVNvUVJYieQl1Ka BSNFbt8CmnelMaxQUigTAOZziXBaLlxHZ7jnGqYtrBbPcsbiTlAU6hGgBsmXt1AVXD7G 4MhuYbmVMqJVwCZ2Rt0EkAtiM1RoyKvs07ZP4qdtYTGviNVGw+CGErzrEp9Yy9SMiJlr LXy4j+rsRAnJYcECBJVZsdTUeikj/r2pusdz57DqI7vmXFBnZz+1YAg3uZIfhPYG22rt 0tPTe2IlaJdcbxi7VAX5dlQnzQCQ2NrnLWi368MsAz9J94Qa3pPPdm4utRTX9iLCeUm/ FViA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701443457; x=1702048257; h=mime-version:user-agent:message-id:in-reply-to:date:references :organization:subject:to:from:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=fMEhtv4c2Bh6ZGYPwnRCCPWZ2C7BFqveCchwqwH457c=; b=IObaaEbtLR+qXj61VFX655iI7+rEftuRDZON2TOaeggMBPrw6qFEQ7XQTAf2Et3MSX SwxKXtVmQRoUcG/1OiMI76fxOyzOtj01tKcami7FKIXmDLgO2hT7zTYOFByfbaRRAY3E SMfoXSYrX0ke473NSFiCy+6xZBtqJ1ybanGGX99qpIeso3/WmMKYCPvohZD6GhF4+GrK NL1crK2EmZXsnLLH8dkvpjWMdfQBfRZZ5ILcsONtFxxwjCj54bQJ00JreKQkqL7ZfjnO SBFQUc/W08sEWeyVDB1fyNlzlTaVGfsiMHWQxoqp1Ue822EVsCPiqiZ06vprm1eyo28I s1DQ== X-Gm-Message-State: AOJu0Yxq3cVtAQa/Y0wPaIr2hNE8muNcvZU1rOfHHYdhlCMiSHnKuAGz xX0CmWEXxIxJ5qmP8m67CraHdlp1jLeFmkNB6jnRfw== X-Google-Smtp-Source: AGHT+IHD4sRO/xG3wJS49SMppPMZXm92HEKPvqx8evIoLM24j3zI6Ws9IMa3XzgwP3qSmp2YWAQSYA== X-Received: by 2002:a17:902:d487:b0:1d0:45c6:1cc1 with SMTP id c7-20020a170902d48700b001d045c61cc1mr5659213plg.27.1701443456750; Fri, 01 Dec 2023 07:10:56 -0800 (PST) Received: from free.home ([2804:14c:4d1:44a5:f4d9:b7a4:4fb8:376f]) by smtp.gmail.com with ESMTPSA id e4-20020a170902d38400b001cfc1b931a9sm3417882pld.249.2023.12.01.07.10.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 07:10:56 -0800 (PST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 3B1FAlDp098544 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 1 Dec 2023 12:10:47 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Subject: [PATCH] untyped calls: enable target switching [PR112334] Organization: Free thinker, does not speak for AdaCore References: Date: Fri, 01 Dec 2023 12:10:47 -0300 In-Reply-To: (Alexandre Oliva's message of "Fri, 01 Dec 2023 09:57:29 -0300") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, WEIRD_QUOTING autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org On Dec 1, 2023, Alexandre Oliva wrote: > Also tested on arm-eabi, but it's *not* enough (or needed) to fix the > PR, there's another bug lurking there, with a separate patch coming > up. Here it is. ---- The computation of apply_args_size and apply_result_size is saved in a static variable, so that the corresponding _mode arrays are initialized only once. That is not compatible with switchable targets, and ARM's arm_set_current_function, by saving and restoring target globals, exercises this problem with a testcase such as that in the PR, in which more than one function in the translation unit calls __builtin_apply or __builtin_return, respectively. This patch moves the _size statics into the target_builtins array, with a bit of ugliness over _plus_one so that zero initialization of the struct does the right thing. Regstrapped on x86_64-linux-gnu, tested on arm-eabi with and without the upthread patch. It fixes the hardcfr fails either way. As for the ugliness, there's a follow up patch below that attempts to alleviate it a little (also regstrapped and tested), but I'm not sure we want to go down that path. WDYT? for gcc/ChangeLog PR target/112334 * builtins.h (target_builtins): Add fields for apply_args_size and apply_result_size. * builtins.cc (apply_args_size, apply_result_size): Cache results in fields rather than in static variables. (get_apply_args_size, set_apply_args_size): New. (get_apply_result_size, set_apply_result_size): New. --- gcc/builtins.cc | 16 ++++++++++++++-- gcc/builtins.h | 7 +++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/gcc/builtins.cc b/gcc/builtins.cc index 4fc58a0bda9b8..039bb5e997a2c 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -1398,8 +1398,16 @@ get_memory_rtx (tree exp, tree len) /* Built-in functions to perform an untyped call and return. */ +#define set_apply_args_size(x) \ + (this_target_builtins->x_apply_args_size_plus_one = 1 + (x)) +#define get_apply_args_size() \ + (this_target_builtins->x_apply_args_size_plus_one - 1) #define apply_args_mode \ (this_target_builtins->x_apply_args_mode) +#define set_apply_result_size(x) \ + (this_target_builtins->x_apply_result_size_plus_one = 1 + (x)) +#define get_apply_result_size() \ + (this_target_builtins->x_apply_result_size_plus_one - 1) #define apply_result_mode \ (this_target_builtins->x_apply_result_mode) @@ -1409,7 +1417,7 @@ get_memory_rtx (tree exp, tree len) static int apply_args_size (void) { - static int size = -1; + int size = get_apply_args_size (); int align; unsigned int regno; @@ -1442,6 +1450,8 @@ apply_args_size (void) } else apply_args_mode[regno] = as_a (VOIDmode); + + set_apply_args_size (size); } return size; } @@ -1452,7 +1462,7 @@ apply_args_size (void) static int apply_result_size (void) { - static int size = -1; + int size = get_apply_result_size (); int align, regno; /* The values computed by this function never change. */ @@ -1484,6 +1494,8 @@ apply_result_size (void) #ifdef APPLY_RESULT_SIZE size = APPLY_RESULT_SIZE; #endif + + set_apply_result_size (size); } return size; } diff --git a/gcc/builtins.h b/gcc/builtins.h index 88a26d70cd5a8..1a26fc63a6d10 100644 --- a/gcc/builtins.h +++ b/gcc/builtins.h @@ -37,6 +37,13 @@ struct target_builtins { register windows, this gives only the outbound registers. INCOMING_REGNO gives the corresponding inbound register. */ fixed_size_mode_pod x_apply_result_mode[FIRST_PSEUDO_REGISTER]; + + /* Nonzero iff the arrays above have been initialized. The _plus_one suffix + is for zero initialization to make it an unreasonable size, used to signal + that the size and the corresponding mode array has not been + initialized. */ + int x_apply_args_size_plus_one; + int x_apply_result_size_plus_one; }; extern struct target_builtins default_target_builtins; ---- untyped calls: use wrapper class type for implicit plus_one Instead of get and set macros to apply a delta, use a single macro that resorts to a temporary wrapper class to apply it. To be combined (or not) with the previous patch. --- gcc/builtins.cc | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/gcc/builtins.cc b/gcc/builtins.cc index 039bb5e997a2c..a60e0a7084513 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -1398,16 +1398,24 @@ get_memory_rtx (tree exp, tree len) /* Built-in functions to perform an untyped call and return. */ -#define set_apply_args_size(x) \ - (this_target_builtins->x_apply_args_size_plus_one = 1 + (x)) -#define get_apply_args_size() \ - (this_target_builtins->x_apply_args_size_plus_one - 1) +/* Wrapper that implicitly applies a delta when getting or setting the + enclosed value. */ +template +class delta_type +{ + T &value; T const delta; +public: + delta_type (T &val, T dlt) : value (val), delta (dlt) {} + operator T () const { return value + delta; } + T operator = (T val) const { value = val - delta; return val; } +}; + +#define saved_apply_args_size \ + (delta_type (this_target_builtins->x_apply_args_size_plus_one, -1)) #define apply_args_mode \ (this_target_builtins->x_apply_args_mode) -#define set_apply_result_size(x) \ - (this_target_builtins->x_apply_result_size_plus_one = 1 + (x)) -#define get_apply_result_size() \ - (this_target_builtins->x_apply_result_size_plus_one - 1) +#define saved_apply_result_size \ + (delta_type (this_target_builtins->x_apply_result_size_plus_one, -1)) #define apply_result_mode \ (this_target_builtins->x_apply_result_mode) @@ -1417,7 +1425,7 @@ get_memory_rtx (tree exp, tree len) static int apply_args_size (void) { - int size = get_apply_args_size (); + int size = saved_apply_args_size; int align; unsigned int regno; @@ -1451,7 +1459,7 @@ apply_args_size (void) else apply_args_mode[regno] = as_a (VOIDmode); - set_apply_args_size (size); + saved_apply_args_size = size; } return size; } @@ -1462,7 +1470,7 @@ apply_args_size (void) static int apply_result_size (void) { - int size = get_apply_result_size (); + int size = saved_apply_result_size; int align, regno; /* The values computed by this function never change. */ @@ -1495,7 +1503,7 @@ apply_result_size (void) size = APPLY_RESULT_SIZE; #endif - set_apply_result_size (size); + saved_apply_result_size = size; } return size; }