From patchwork Fri Mar 22 07:30:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajit Agarwal X-Patchwork-Id: 87497 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 2A12B3858407 for ; Fri, 22 Mar 2024 07:30:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 779B63858D28 for ; Fri, 22 Mar 2024 07:30:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 779B63858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 779B63858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711092634; cv=none; b=jmh1huaIbh0lbtl/M9geEDgQkVc1ao+rjYXCVJAz/1dvjNTQGB1kzGlQNOJsTFhQkpt/y7bUkGZJHbRzLiSrb/XZYD9lsKgCH7csJ5L0Naa7c2e4hW5dMtPy7fZazd72foAQiXqraNKs4yKsGziLhK7CAkHQdKYwibVEOGHf1y8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711092634; c=relaxed/simple; bh=6RB12Kov03RpV2vvIZRyTkgz81SCZIBY5Vo3cYZ5Fp8=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=dwbzXVyL4mN3WvfKOTpmOsn9qaqKlsfJ5xGwN3w7Vy5+gJ9NMDPpmSANc18riWCjxbrrMUkeU6xWo5PiJLKBcW3ezgIAateqyr/+WowBFBll1oFt/UoDo9eQjXw7kIPqc+GzgCxRrPFhLptRtZGCqiy7u2/R6WGkOO7jMVuY+NA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0360083.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42M7D7gZ030552; Fri, 22 Mar 2024 07:30:31 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : mime-version : to : from : subject : content-type : content-transfer-encoding; s=pp1; bh=muLbRC/Pwe7zFqZRkTUu17GlLqSy7SmyPSC7RFOowkI=; b=rWLkGxMGfonozNrQT5svoZ4CzwR+XTBeWwubt3s+tKaZSf5gD+Ec9pKII/CgluR0k0QX YRjLgIIqc9TMtCXK4coXxR6B6YJXvjZR6scDRzVqf3vw041QCIKUZaYagt0GlwlKfq51 RKgFFEqBQ9s4sll28j5TWGRqL81l8USIO0Ud9K1ylMRXsIMwX118FTFDeYwd0ZLdUFMh eIlFf/kP+Tn2t7B3JsowAlGDnkAPutMUoYKRl565FWRiBOd2YFlpxsfnj5kginKti3YN o6GBxgYo7s53kW55vNAfq9l7YVKttwvIHC7fR6w/yWk3VxkWQKb0pXoqIuxfBhAJs9ra 4g== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3x15d30154-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 22 Mar 2024 07:30:31 +0000 Received: from m0360083.ppops.net (m0360083.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 42M7UUoR026208; Fri, 22 Mar 2024 07:30:30 GMT Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3x15d3014u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 22 Mar 2024 07:30:30 +0000 Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 42M5QwhU023091; Fri, 22 Mar 2024 07:30:29 GMT Received: from smtprelay02.dal12v.mail.ibm.com ([172.16.1.4]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3x0x14j9f3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 22 Mar 2024 07:30:29 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay02.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 42M7UQDH30605676 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 22 Mar 2024 07:30:28 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E993A58045; Fri, 22 Mar 2024 07:30:25 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EEEAE58054; Fri, 22 Mar 2024 07:30:22 +0000 (GMT) Received: from [9.43.31.19] (unknown [9.43.31.19]) by smtpav04.wdc07v.mail.ibm.com (Postfix) with ESMTP; Fri, 22 Mar 2024 07:30:22 +0000 (GMT) Message-ID: <17782aae-98cd-41e0-ac3c-18e0ed68f432@linux.ibm.com> Date: Fri, 22 Mar 2024 13:00:21 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Jakub Jelinek , "Kewen.Lin" , Segher Boessenkool , Peter Bergner , David Edelsohn , Michael Meissner , gcc-patches From: Ajit Agarwal Subject: [PATCH] rs6000: Stackoverflow in optimized code on PPC (PR100799) X-TM-AS-GCONF: 00 X-Proofpoint-GUID: EGkqSutHoZPmWe5TVDNH-wB5wZNSD1u1 X-Proofpoint-ORIG-GUID: ZyfiP7TPwttuM99xBQ4w03WL2h8KWy8m X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-22_04,2024-03-21_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 phishscore=0 clxscore=1011 priorityscore=1501 impostorscore=0 bulkscore=0 adultscore=0 spamscore=0 malwarescore=0 lowpriorityscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403210000 definitions=main-2403220052 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_MANYTO, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, 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.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 Hello All: When using FlexiBLAS with OpenBLAS we noticed corruption of the parameters passed to OpenBLAS functions. FlexiBLAS basically provides a BLAS interface where each function is a stub that forwards the arguments to a real BLAS lib, like OpenBLAS. Fixes the corruption of caller frame checking number of arguments is less than equal to GP_ARG_NUM_REG (8) excluding hidden unused DECLS. Bootstrapped and regtested on powerpc64-linux-gnu. Thanks & Regards Ajit rs6000: Stackoverflow in optimized code on PPC (PR100799) When using FlexiBLAS with OpenBLAS we noticed corruption of the parameters passed to OpenBLAS functions. FlexiBLAS basically provides a BLAS interface where each function is a stub that forwards the arguments to a real BLAS lib, like OpenBLAS. Fixes the corruption of caller frame checking number of arguments is less than equal to GP_ARG_NUM_REG (8) excluding hidden unused DECLS. 2024-03-22 Ajit Kumar Agarwal gcc/ChangeLog: PR rtk-optimization/100799 * config/rs600/rs600-calls.cc (rs6000_function_arg): Don't generate parameter save area if number of arguments passed less than equal to GP_ARG_NUM_REG (8) excluding hidden paramter. * function.cc (assign_parms_initialize_all): Check for hidden parameter in fortran code and set the flag hidden_string_length and actual paramter passed excluding hidden unused DECLS. * function.h: Add new field hidden_string_length and actual_parm_length in function structure. --- gcc/config/rs6000/rs6000-call.cc | 11 ++++++++++- gcc/function.cc | 26 ++++++++++++++++++++++++++ gcc/function.h | 10 ++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/gcc/config/rs6000/rs6000-call.cc b/gcc/config/rs6000/rs6000-call.cc index 1f8f93a2ee7..8e6e3de6804 100644 --- a/gcc/config/rs6000/rs6000-call.cc +++ b/gcc/config/rs6000/rs6000-call.cc @@ -1857,7 +1857,16 @@ rs6000_function_arg (cumulative_args_t cum_v, const function_arg_info &arg) return rs6000_finish_function_arg (mode, rvec, k); } - else if (align_words < GP_ARG_NUM_REG) + /* Workaround buggy C/C++ wrappers around Fortran routines with + character(len=constant) arguments if the hidden string length arguments + are passed on the stack; if the callers forget to pass those arguments, + attempting to tail call in such routines leads to stack corruption. + Avoid return stack space for parameters <= 8 excluding hidden string + length argument is passed (partially or fully) on the stack in the + caller and the callee needs to pass any arguments on the stack. */ + else if (align_words < GP_ARG_NUM_REG + || (cfun->hidden_string_length + && cfun->actual_parm_length <= GP_ARG_NUM_REG)) { if (TARGET_32BIT && TARGET_POWERPC64) return rs6000_mixed_function_arg (mode, type, align_words); diff --git a/gcc/function.cc b/gcc/function.cc index 3cef6c17bce..1318564b466 100644 --- a/gcc/function.cc +++ b/gcc/function.cc @@ -2326,6 +2326,32 @@ assign_parms_initialize_all (struct assign_parm_data_all *all) #endif all->args_so_far = pack_cumulative_args (&all->args_so_far_v); + unsigned int num_args = 0; + unsigned int hidden_length = 0; + + /* Workaround buggy C/C++ wrappers around Fortran routines with + character(len=constant) arguments if the hidden string length arguments + are passed on the stack; if the callers forget to pass those arguments, + attempting to tail call in such routines leads to stack corruption. + Avoid return stack space for parameters <= 8 excluding hidden string + length argument is passed (partially or fully) on the stack in the + caller and the callee needs to pass any arguments on the stack. */ + for (tree arg = DECL_ARGUMENTS (current_function_decl); + arg; arg = DECL_CHAIN (arg)) + { + num_args++; + if (DECL_HIDDEN_STRING_LENGTH (arg)) + { + tree parmdef = ssa_default_def (cfun, arg); + if (parmdef == NULL || has_zero_uses (parmdef)) + { + cfun->hidden_string_length = 1; + hidden_length++; + } + } + } + + cfun->actual_parm_length = num_args - hidden_length; #ifdef INCOMING_REG_PARM_STACK_SPACE all->reg_parm_stack_space = INCOMING_REG_PARM_STACK_SPACE (current_function_decl); diff --git a/gcc/function.h b/gcc/function.h index 19e15bd63b0..5984f0007c2 100644 --- a/gcc/function.h +++ b/gcc/function.h @@ -346,6 +346,11 @@ struct GTY(()) function { /* Last assigned dependence info clique. */ unsigned short last_clique; + /* Actual parameter length ignoring hidden paramter. + This is done to C++ wrapper calling fortran module + which has hidden parameter that are not used. */ + unsigned int actual_parm_length; + /* Collected bit flags. */ /* Number of units of general registers that need saving in stdarg @@ -442,6 +447,11 @@ struct GTY(()) function { /* Set for artificial function created for [[assume (cond)]]. These should be GIMPLE optimized, but not expanded to RTL. */ unsigned int assume_function : 1; + + /* Hidden parameters while calling C++ wrapper to fortran + module. Set if there is hidden parameter in fortran + module while called C++ wrapper. */ + unsigned int hidden_string_length : 1; }; /* Add the decl D to the local_decls list of FUN. */