From patchwork Thu Apr 12 07:30:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 897511 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-476249-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mentor.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="okmc9/HM"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40MCJJ3fxFz9s1R for ; Thu, 12 Apr 2018 17:31:11 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=myUcHwJNnV/q0BgOmmTpTuOc4BuvQ83jKyRvEHNG0vf98UBO4g ampKpQAHbuDTOvmr0lJNfUsJCvCOSEAbevPivBmdxZ7P+5s1s5g0qrnPdL/RjxCr QNBT98U/C0kqk88foweUyqCD3MNBgTsGnP02zkDVVYuGllTG0tioCpQDI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=maEk62lPMj4hTk8Z/Is0FeLhpeM=; b=okmc9/HM5gz1NCOyLT7u mGFU/W4JtkcFGxL5Xac54vCptMuLTSF5QLFVyUrkhE6hBm3KjgsfByijJhsolgCk gq5FUruRRpK4DUh65mcfVVHEp8Va6d/BbCDMhQw+EaMFjKS4PKfUGLSR9QwTVm3V btA1ocw38hCf3okhGlT8Z9Y= Received: (qmail 119658 invoked by alias); 12 Apr 2018 07:31:04 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 119638 invoked by uid 89); 12 Apr 2018 07:31:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS, URIBL_RED autolearn=ham version=3.3.2 spammy=SIZE, omitting X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 12 Apr 2018 07:31:02 +0000 Received: from nat-ies.mentorg.com ([192.94.31.2] helo=SVR-IES-MBX-04.mgc.mentorg.com) by relay1.mentorg.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-SHA384:256) id 1f6WhA-0003iq-IZ from Tom_deVries@mentor.com for gcc-patches@gcc.gnu.org; Thu, 12 Apr 2018 00:31:00 -0700 Received: from [172.30.72.177] (137.202.0.87) by SVR-IES-MBX-04.mgc.mentorg.com (139.181.222.4) with Microsoft SMTP Server (TLS) id 15.0.1320.4; Thu, 12 Apr 2018 08:30:56 +0100 To: GCC Patches From: Tom de Vries Subject: [nvptx, PR85296] Fix handling of extern var with flexible array member Message-ID: Date: Thu, 12 Apr 2018 09:30:55 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 X-ClientProxiedBy: svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) To SVR-IES-MBX-04.mgc.mentorg.com (139.181.222.4) Hi, for the recently added test-case pr85244-1.c, we run into the following failure with the standalone nvptx toolchain: ... spawn nvptx-none-run ./pr85244-1.exe error : Size doesn't match for 'val' in 'input file 2 at offset 3047', first specified in 'input file 1 at offset 1805' nvptx-run: cuLinkAddData failed: unknown error (CUDA_ERROR_UNKNOWN, 999) ... The linking problem happens because while in pr85244-2.s we have an array of size 3: ... .visible .const .align 8 .u64 val[3] = { 0, 180388626432, 1337 }; ... in pr85244-1.s we have an array of size 2: ... .extern .const .align 8 .u64 val[2]; ... The ptx declarations correspond to this source bit in pr85244-1.c: ... struct s { long a; int b; int tab[]; }; extern const struct s val; ... and this one in pr85244-2.c (omitting type decl): ... const struct s val = { 0, 0, { 42, 1337 } }; ... Because ptx has no structs, structs are declared as arrays of a certain base type, in this case u64. In pr85244-2.c we calculate the size of the array, and based on the initializer we arrive at a size of 3. In pr85244-1.c we calculate the size of the array, and based on the type we arrive at at size of 2. The patch fixes this by declaring extern structs which have a flexible array member as an array without given dimension. Build and tested on nvptx. Committed to stage4 trunk. Thanks, - Tom [nvptx] Fix handling of extern var with flexible array member 2018-04-12 Tom de Vries PR target/85296 * config/nvptx/nvptx.c (flexible_array_member_type_p): New function. (nvptx_assemble_decl_begin): Add undefined param. Declare undefined array with flexible array member as array without given dimension. (nvptx_assemble_undefined_decl): Set nvptx_assemble_decl_begin call argument for undefined param to true. --- gcc/config/nvptx/nvptx.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/gcc/config/nvptx/nvptx.c b/gcc/config/nvptx/nvptx.c index a9a3053..131b495 100644 --- a/gcc/config/nvptx/nvptx.c +++ b/gcc/config/nvptx/nvptx.c @@ -2021,6 +2021,30 @@ nvptx_output_ascii (FILE *, const char *str, unsigned HOST_WIDE_INT size) nvptx_assemble_value (str[i], 1); } +/* Return true if TYPE is a record type where the last field is an array without + given dimension. */ + +static bool +flexible_array_member_type_p (const_tree type) +{ + if (TREE_CODE (type) != RECORD_TYPE) + return false; + + const_tree last_field = NULL_TREE; + for (const_tree f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f)) + last_field = f; + + if (!last_field) + return false; + + const_tree last_field_type = TREE_TYPE (last_field); + if (TREE_CODE (last_field_type) != ARRAY_TYPE) + return false; + + return (! TYPE_DOMAIN (last_field_type) + || ! TYPE_MAX_VALUE (TYPE_DOMAIN (last_field_type))); +} + /* Emit a PTX variable decl and prepare for emission of its initializer. NAME is the symbol name and SETION the PTX data area. The type is TYPE, object size SIZE and alignment is ALIGN. @@ -2031,11 +2055,18 @@ nvptx_output_ascii (FILE *, const char *str, unsigned HOST_WIDE_INT size) static void nvptx_assemble_decl_begin (FILE *file, const char *name, const char *section, - const_tree type, HOST_WIDE_INT size, unsigned align) + const_tree type, HOST_WIDE_INT size, unsigned align, + bool undefined = false) { bool atype = (TREE_CODE (type) == ARRAY_TYPE) && (TYPE_DOMAIN (type) == NULL_TREE); + if (undefined && flexible_array_member_type_p (type)) + { + size = 0; + atype = true; + } + while (TREE_CODE (type) == ARRAY_TYPE) type = TREE_TYPE (type); @@ -2172,7 +2203,7 @@ nvptx_assemble_undefined_decl (FILE *file, const char *name, const_tree decl) tree size = DECL_SIZE_UNIT (decl); nvptx_assemble_decl_begin (file, name, section_for_decl (decl), TREE_TYPE (decl), size ? tree_to_shwi (size) : 0, - DECL_ALIGN (decl)); + DECL_ALIGN (decl), true); nvptx_assemble_decl_end (); }