From patchwork Fri Nov 18 17:20:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 22402 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp316226wrr; Fri, 18 Nov 2022 09:21:35 -0800 (PST) X-Google-Smtp-Source: AA0mqf5HjanRuo5XohHBkzFO7GTuHMVEcDYB2T1avksQ21k37Uj0U847xvPhQwc8a7Ra2vQARX6a X-Received: by 2002:a50:ed0a:0:b0:468:ff2d:def8 with SMTP id j10-20020a50ed0a000000b00468ff2ddef8mr4598081eds.399.1668792094809; Fri, 18 Nov 2022 09:21:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668792094; cv=none; d=google.com; s=arc-20160816; b=YD2Wg7yachK4pVc02crB5o6pOJmGYI7V93lQTETpB8g8co3suSx3MZ7rCNChn+bgsX 9OXgSmxYyjq2OJWMtVXF86i3BZ3vYhylw23KMBOZQup07+cLWx2L518yZYY4NeuT6+YD F8rPNRiEd2r5Ellq91Fs0zkaEwSvTxLUiOrE0aPFIgUPIFKsUwWPhiV8MOUsReKfkMAN ZbuRUJgjGBbWi3kQwpeh1oy8AEb4HcHYJxSotolhEuo0z04AEwd0YW1SCaVgMYIJ9eFu umHlkFHEskR9eaR+aOBydfmiRykkmHQfc0esZMwbZugT8p/JGVRxJ8HJClBj7Qq1YpYt 3xvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:from:to :content-language:user-agent:mime-version:date:message-id :ironport-sdr:dmarc-filter:delivered-to; bh=dVCJHMOSFbmD3th0DBf+Q5NdPbm/wGMjLl7ZbfF9MNE=; b=VZIpkZP5xgOVjhVWN02CiHbYUqvjPdDFhwsNI89vRVHMBepPrrpXheqmTWCEJKm0f7 KAcOWGdXqZcR/+f4N5FDlQXwAiunMfx/1yEqsSicoo9GLc4bmigqRTtEdk8WaV+o8n/A be2fzxqn601Hnoq16iL1Ty3NNMXCgcwp1OjTbWG8S9qg/NJxWO91A/2kBVtOP+l93QeJ hTJ3ZHrt5jO2DfAJ3VWTgpi2ukoeXsXXVuZzcmhwjYFu46mP1zv59ohlOXOlFQgItroh 18sVWFJVfca4796IdOLQcG0rRdgN9xy4z9fuxe04r5CsgKS/Cst/iMHZ8t9GN7vJqO/g RZsg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id qk31-20020a1709077f9f00b0073d5f9aa5e9si3709978ejc.327.2022.11.18.09.21.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 09:21:34 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E17B3842405 for ; Fri, 18 Nov 2022 17:21:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa1.mentor.iphmx.com (esa1.mentor.iphmx.com [68.232.129.153]) by sourceware.org (Postfix) with ESMTPS id C3CCF3853D4D for ; Fri, 18 Nov 2022 17:20:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C3CCF3853D4D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="5.96,175,1665475200"; d="diff'?scan'208";a="90264797" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa1.mentor.iphmx.com with ESMTP; 18 Nov 2022 09:20:50 -0800 IronPort-SDR: bg2BuMexl0hWIeEWyPj3eNuZuMn0N+11UsgqcLv8SzZyJd2lmr+12Hbq4WD5goClYSfBOXVQIQ WWoMaWoro3g5cRtV8Fo+LfRlL+PM7Gvv5+IIjqK5lVfqmZ2DvMSTnr7HYGohrac3bxuX0lb1nH iZaEuYpiW9gozbZhYkZpH8gMF0vn3AXKgo1Mf56AtSgApJbzWG9mzxmgzldc/qUkdi2IF6uQtj 7shOfvOA0ifgEMtbupyFiMCz3gH5TFutEpoebGAXIA5bJmPoxafAwY2Vo7xFR2aXj22uGDz0QO 3Cg= Message-ID: <1bec26d6-e2c5-3408-4f61-0fb17e730b3e@codesourcery.com> Date: Fri, 18 Nov 2022 18:20:29 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Content-Language: en-US To: Andrew Stubbs , gcc-patches From: Tobias Burnus Subject: [Patch] gcn: Add __builtin_gcn_{get_stack_limit,first_call_this_thread_p} X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-15.mgc.mentorg.com (139.181.222.15) To svr-ies-mbx-11.mgc.mentorg.com (139.181.222.11) X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749855339722935754?= X-GMAIL-MSGID: =?utf-8?q?1749855339722935754?= This patch adds two builtins (getting end-of-stack pointer and a Boolean answer whether it was the first call to the builtin on this thread). The idea is to replace some hard-coded values in newlib, permitting to move later to a manually allocated stack on the compiler side without the need to modify newlib again. The GCC patch matches what newlib did in reent; I could imagine that we change this later on. Lightly tested (especially by visual inspection). Currently doing a final regtest, OK when it passes? Any comments to this patch - or the attached newlib patch?* Tobias (*) I also included a patch to newlib to see where were are heading + to actually use them for regtesting ... ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955 amdgcn: Use __builtin_gcn_ in libc/machine/amdgcn/getreent.c Call __builtin_gcn_get_stack_limit and __builtin_gcn_first_call_this_thread_p to reduce dependency on some register/layout assumptions by using the new GCC mainline (GCC 13) builtins, if they are available. If not, the existing code is used. newlib/libc/machine/amdgcn/getreent.c | 38 ++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/newlib/libc/machine/amdgcn/getreent.c b/newlib/libc/machine/amdgcn/getreent.c index be7d2edc9..ef731f649 100644 --- a/newlib/libc/machine/amdgcn/getreent.c +++ b/newlib/libc/machine/amdgcn/getreent.c @@ -29,22 +29,42 @@ typedef struct hsa_kernel_dispatch_packet_s { struct _reent * __getreent (void) { - /* Place the reent data at the top of the stack allocation. - s[0:1] contains a 48-bit private segment base address. + /* Place the reent data at the top of the stack allocation. */ + struct data { + int marker; + struct _reent reent; + } *data; + +#if defined(__has_builtin) \ + && __has_builtin(__builtin_gcn_get_stack_limit) \ + && __has_builtin(__builtin_gcn_first_call_this_thread_p) + unsigned long addr = (((unsigned long) __builtin_gcn_get_stack_limit() + - sizeof(struct data)) & ~7); + data = (struct data *)addr; + + register long sp asm("s16"); + + if (sp >= addr) + goto stackoverflow; + if (__builtin_gcn_first_call_this_thread_p()) + { + data->marker = 12345; + __builtin_memset (&data->reent, 0, sizeof(struct _reent)); + _REENT_INIT_PTR_ZEROED (&data->reent); + } + else if (data->marker != 12345) + goto stackoverflow; +#else + /* s[0:1] contains a 48-bit private segment base address. s11 contains the offset to the base of the stack. s[4:5] contains the dispatch pointer. - + WARNING: this code will break if s[0:1] is ever used for anything! */ const register unsigned long buffer_descriptor asm("s0"); unsigned long private_segment = buffer_descriptor & 0x0000ffffffffffff; const register unsigned int stack_offset asm("s11"); const register hsa_kernel_dispatch_packet_t *dispatch_ptr asm("s4"); - struct data { - int marker; - struct _reent reent; - } *data; - unsigned long stack_base = private_segment + stack_offset; unsigned long stack_end = stack_base + dispatch_ptr->private_segment_size * 64; unsigned long addr = (stack_end - sizeof(struct data)) & ~7; @@ -69,7 +89,7 @@ __getreent (void) } else if (data->marker != 12345) goto stackoverflow; - +#endif return &data->reent;