From patchwork Wed Aug 5 14:35:23 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyrylo Tkachov X-Patchwork-Id: 140662 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 274B74BAE7D3 for ; Wed, 5 Aug 2026 14:37:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 274B74BAE7D3 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=mWLISWn8 X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azon11010034.outbound.protection.outlook.com [52.101.46.34]) by sourceware.org (Postfix) with ESMTPS id 8FD354BA2E35 for ; Wed, 5 Aug 2026 14:36:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8FD354BA2E35 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=nvidia.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8FD354BA2E35 Authentication-Results: sourceware.org; arc=pass smtp.remote-ip=52.101.46.34 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1785940579; cv=pass; b=N5/UBhYP/0yl+sw3eJf9eDh1dFDFwsCqLd0zfTM7Y6F7Q9yIJez8Okj7lmKgUzQtMCFl2tIlqdEW1DVVCjTjqaiGc8j1X0bSHghcXDl/mFRjwhXpOAGJdaSBDmaHLiqY4Z2INnIELWUiFxzmeK8INFJ6mHFURsmP/X5HioFq/pE= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1785940579; c=relaxed/simple; bh=sZouonBnYa8bOG2SkBcMUCd55A4JynSYuJFNMcYBuvE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rDaUYtSiDPp361gCz6XF4K7GDCPSIevAu6BIdwPt7Lnc3xKJoanxBXH5ZD7uPO+FA4p9DF1qqtTbQint6IYT74/EyRRWE8I5aRoV6irhGumCtVi6Z34PiwZsHjbxYdKJnrEKih6/B4/HGn5uP7anQMKW1FtRm/g+0Dz57PGsQbs= ARC-Authentication-Results: i=2; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=mWLISWn8 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8FD354BA2E35 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=dJgIDK/urVbiDVPhLteIsMeDbgWwhe/zUMM1gsf1G33d7njZh4QSrNvQECwUngQNeEoVLSCu1i1pcthEplw/rxN2PywN1HlA8ukp0GTc7GsJCSKrqYA9NjmmtFljHkUwW7tAoXmo/pKE78hyCmS2IIdm09oiaF9oCg7JtUxsf1DLQe4SeHDDUYfgDKSQbrhvkXDfxie9BNCSRjASegMWYakTi+8ghUMiiOMTyPRil6wLZtBF/R2Zd/KDW5yBGU/9tYbU78+lbQzWchxuf3l6j/MKodkb9BmCWVR092Yw1hgC5XvpNVtL5t9juRW13b0KQLJp9BtL57+y9iG5mqU/Qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=QItjbUOUrnKzll4C3vOTqLYHgXicBgOPPQHU29jWr6A=; b=xDA3ivVZN7F//wgYsOAZHkexB1eruR6IBvMjWz5b15urVzXgdozC/5qCav8zlcEIW1PgfZBQZMPODnO6O19TzY/n21WT4C/ALN/3U7qePWBRSHwyMoQlfpdJPxygwPqmiWncA6B6AXqkrxpRvbJkQs8taKtNv8/DvbGGTapzWZ1W6RSpxQtSUAxkoe2FDSnPZ5GDI7szdX1IQdMkGYL2GdBnsZfUtE/YLEiiQUe6TKZlMYehZdqsgPog5ycO0Hh0qta1vI9ude5P4wUgoHS/T1EXgR6QD5/zA4drZB8hmHBpmPpjmckOXB0zFaGzB2+1ekhSHylCnpNFjquyzQj8Cg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=gcc.gnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QItjbUOUrnKzll4C3vOTqLYHgXicBgOPPQHU29jWr6A=; b=mWLISWn8NzKBSMfg8mJU1MHjLLd8NWb6fNKm/OMcftdgC1jnAw2e7o7Uy5KKwUTNm2IK/xbTRAYVzOv3ZCoxzvB6nJvWQlCNsU+VMFyLKQ0pTeTFoKLhOBi0m1WkoBIRz4qfrx+xuno6ko9Zpyfij+6lFw/5stptSPJMbr7K1RZww0V9l9pwV87PyQrdG0aQ+G03P5WUli9B+2bpLzyKjo/sM4jPj2ytkcN43kvDFFHXLqCgXb9+id+SEHT+sxOjtDvCTyN4fdppnpbty4oC5FHRe/tAW9G665MYIAkN8KlTQz3xSS0uY6pt6rPupcuCXZyixuem5z4OWQiKQUGt3A== Received: from BN9PR03CA0549.namprd03.prod.outlook.com (2603:10b6:408:138::14) by SN7PR12MB6885.namprd12.prod.outlook.com (2603:10b6:806:263::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.292.19; Wed, 5 Aug 2026 14:36:07 +0000 Received: from BL6PEPF00020E66.namprd04.prod.outlook.com (2603:10b6:408:138:cafe::8e) by BN9PR03CA0549.outlook.office365.com (2603:10b6:408:138::14) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.270.17 via Frontend Transport; Wed, 5 Aug 2026 14:36:07 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL6PEPF00020E66.mail.protection.outlook.com (10.167.249.27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.292.8 via Frontend Transport; Wed, 5 Aug 2026 14:36:06 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Wed, 5 Aug 2026 07:35:39 -0700 Received: from ktkachov-mlt.nvidia.com (10.126.231.37) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Wed, 5 Aug 2026 07:35:37 -0700 From: To: CC: , , , Kyrylo Tkachov Subject: [PATCH 1/4] gensupport: Centralize machine-generator output management Date: Wed, 5 Aug 2026 16:35:23 +0200 Message-ID: <20260805143526.27275-1-ktkachov@nvidia.com> X-Mailer: git-send-email 2.50.1 MIME-Version: 1.0 X-Originating-IP: [10.126.231.37] X-ClientProxiedBy: rnnvmail202.nvidia.com (10.129.68.7) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL6PEPF00020E66:EE_|SN7PR12MB6885:EE_ X-MS-Office365-Filtering-Correlation-Id: a2e9dd7d-2585-49fd-3dec-08def2fee2aa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|1800799024|36860700016|376014|23010399003|18002099003|11063799006|5023799004|56012099006|3023799007|6133799003|10067099003; X-Microsoft-Antispam-Message-Info: Kjx71WKpXQUDndVq5WNKx046IgbmuMTXjiSxbYUpgRVmJFTEMRYSKjpSIDU2Ykxsm6rAMsZAzm3fvNopPlVoq3jUhedH8NZhxtIsr2v45xuvoGS47EHJdoZwYZnf02GwTCekEDqxOtTKwgNfX8IU+pFsg8ZQWVdt0wv44ZUOsVjoPo0XvMLbUSqsXBaCI1CGBzC+gyGFSp76SBCiJfH6M3dmC+vwDi8j984ZJzR8es0F8vDfNKa0cGjSWB68K/eR2ohGr5TNZcPoT1ErS/PZMIaLv2uRM0LRQQ71vl219Y+/56BwjU/5dkAdVIMH6YTRQ9kUTwkC8s3+uD/HZkMY4mMTbXftbZyZn/J6pxTp82A4+P3OGa5AMEF/DdexWd9ncQW+C7CSXuvZd/A4ebL05hZ6i0sxhm74GGxoopxG/6SvdNUrAIgv+k79+u85SwP42vzsukNEAH8RbS7nAyJfgcY5YvsQrP9TCsgxG30rUKIkcMJUvDcjkYeGBNPtdhtrE+7JugWxqqFFGdxM73bUIPo3YPunPnIkmTf+Ksb4F+4n7FgRJSSWQGS0OgibLU6+5LM/X7qlbrrYh957RxYlGUhNc2hHtXHUw5vma+heL4S9e18oDLdGPpf8FMp20dUuCkew2OAREy41r35NCG10LdeAiK1u5XqvhJ2+kg1rv3XJImzGNjpYb8PWV9oeGgDRvIhaAS2rEa2lCTrJUr3e3A== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(82310400026)(1800799024)(36860700016)(376014)(23010399003)(18002099003)(11063799006)(5023799004)(56012099006)(3023799007)(6133799003)(10067099003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xULS2Y49nDBpGifBL5v4q5gHvYk8F3a1cSTosN4HGP/gQCKShsjX0KPhg2sSlSFszB1XYvKJTTK5QQJyG6dti96I/Fjzna/9fAehRHKcmmDBEFvIjsxkO09E5gsw3PeZawMYVb9JJQOUobfdEbsjz8nXBDODKSrEol5fiENnPCy4M465Meq6/LSi41ohQ9+K2vS4kUBue/hlPjWWJKMR+8KUrXbufjU7Xz+P3Slk8uJFt4BNQzbLWea49gVzUVgfw+i4amGQBD42/gRWuWMnz9cTB2pXoHW/7Z6+GAsVRkgAhX/bQbG2nYY763BclhrcB9+7UaIXcU2BokVA0NC0DjlKbAP+NMrfg1ZLxNGT8brAV0CASN82vVQRXgrIjCidtN7VBQt4F5WxUlzJGBSafwp95rbW8Fj1Y9WLHG9kt5+FagNiv3bV+88GMEVT07V8 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Aug 2026 14:36:06.9678 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a2e9dd7d-2585-49fd-3dec-08def2fee2aa X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL6PEPF00020E66.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6885 X-Spam-Status: No, score=-8.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FORGED_SPF_HELO, GIT_PATCH_0, LOCAL_AUTHENTICATION_FAIL_SPF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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 From: Kyrylo Tkachov genemit and genrecog use the same size-based output selection, but each generator owns its file names, opens files during option parsing, and closes them separately. Adding more partitioned generators would copy this logic. Put the common output record and lifecycle helpers in gensupport. The helpers validate names, delay file opening until option parsing is complete, select only partition outputs by current size, and close every output with a useful diagnostic. Fixed outputs, such as headers, use the same record but do not participate in selection. Convert genemit and genrecog to the common interface. Also remove the unused output index and disabled round-robin code from choose_output, and remove a genrecog selection whose result is discarded. Diagnose missing or repeated genrecog -H options, remove its fixed header-name limit, and stop printing parsed output options to standard output. Robin added the shared choose_output helper for PR111600, following the size-based policy that Tamar used in genmatch. Bootstrapped and tested on aarch64-none-linux-gnu and x86_64-linux. Ok for trunk? Thanks, Kyrill gcc/ChangeLog: * gensupport.h (generator_output): New structure. (add_generator_output, open_generator_outputs): Declare. (close_generator_outputs): Likewise. (choose_output): Accept generator_output records. Remove the output index parameter. * gensupport.cc (add_generator_output): New function. (open_generator_outputs, close_generator_outputs): Likewise. (choose_output): Select only partition records. Remove the output index and disabled round-robin implementation. * genemit.cc (output_files): Use generator_output records. (handle_arg): Register output names. (main): Use the common open, selection, and close helpers. * genrecog.cc (print_subroutine_group): Accept generator_output records. (header_name): Change to a pointer. (header): Remove global variable. (output_files): Use generator_output records. (handle_arg): Register output names and validate -H. (main): Validate and register the header output. Use the common open, selection, and close helpers. Remove an unused output selection. Suggested-by: Tamar Christina Signed-off-by: Kyrylo Tkachov --- gcc/genemit.cc | 25 ++++++--------- gcc/genrecog.cc | 64 +++++++++++++++---------------------- gcc/gensupport.cc | 80 ++++++++++++++++++++++++++++++++++++----------- gcc/gensupport.h | 16 +++++++++- 4 files changed, 112 insertions(+), 73 deletions(-) diff --git a/gcc/genemit.cc b/gcc/genemit.cc index 6323aeb19fc..4cddaaa59b9 100644 --- a/gcc/genemit.cc +++ b/gcc/genemit.cc @@ -878,15 +878,14 @@ from the machine description file `md'. */\n\n"); fprintf (file, "#include \"target.h\"\n\n"); } -auto_vec output_files; +auto_vec output_files; static bool handle_arg (const char *arg) { if (arg[1] == 'O') { - FILE *file = fopen (&arg[2], "w"); - output_files.safe_push (file); + add_generator_output (output_files, &arg[2], true); return true; } return false; @@ -910,13 +909,13 @@ main (int argc, const char **argv) md_rtx_info info; if (output_files.is_empty ()) - output_files.safe_push (stdout); + add_generator_output (output_files, NULL, true); + open_generator_outputs (output_files); - for (auto f : output_files) - print_header (f); + for (const generator_output &output : output_files) + print_header (output.file); FILE *file = NULL; - unsigned file_idx; /* Read the machine description. */ while (read_md_rtx (&info)) @@ -941,7 +940,7 @@ main (int argc, const char **argv) for (auto &info : queue) { - file = choose_output (output_files, file_idx); + file = choose_output (output_files); fprintf (file, "/* %s:%d */\n", info.loc.filename, info.loc.lineno); switch (GET_CODE (info.def)) @@ -964,7 +963,7 @@ main (int argc, const char **argv) } } - file = choose_output (output_files, file_idx); + file = choose_output (output_files); /* Write out the routines to add CLOBBERs to a pattern and say whether they clobber a hard reg. */ @@ -978,10 +977,6 @@ main (int argc, const char **argv) handle_overloaded_gen (oname, file); } - int ret = SUCCESS_EXIT_CODE; - for (FILE *f : output_files) - if (fclose (f) != 0) - ret = FATAL_EXIT_CODE; - - return ret; + return (close_generator_outputs (output_files) + ? SUCCESS_EXIT_CODE : FATAL_EXIT_CODE); } diff --git a/gcc/genrecog.cc b/gcc/genrecog.cc index 663620224ab..ab9f3723e89 100644 --- a/gcc/genrecog.cc +++ b/gcc/genrecog.cc @@ -5288,11 +5288,10 @@ print_subroutine (FILE *f, output_state *os, state *s, int proc_id, /* Print out a routine of type TYPE that performs ROOT. */ static void -print_subroutine_group (vec &vec, FILE *header, output_state *os, +print_subroutine_group (const vec &outputs, FILE *header, + output_state *os, routine_type type, state *root) { - FILE *f; - unsigned idx; os->type = type; if (use_subroutines_p) { @@ -5305,19 +5304,14 @@ print_subroutine_group (vec &vec, FILE *header, output_state *os, unsigned int i; state *s; - FILE *f = header; FOR_EACH_VEC_ELT (subroutines, i, s) print_subroutine (header, os, s, i + 1, true); FOR_EACH_VEC_ELT (subroutines, i, s) - { - f = choose_output (vec, idx); - print_subroutine (f, os, s, i + 1); - } + print_subroutine (choose_output (outputs), os, s, i + 1); } /* Output the main routine. */ - f = choose_output (vec, idx); - print_subroutine (f, os, root, 0); + print_subroutine (choose_output (outputs), os, root, 0); } /* Return the rtx pattern for the list of rtxes in a define_peephole2. */ @@ -5388,24 +5382,22 @@ remove_clobbers (acceptance_type *acceptance_ptr, rtx *pattern_ptr) return true; } -auto_vec output_files; -char header_name[255]; -FILE *header = NULL; +auto_vec output_files; +const char *header_name; static bool handle_arg (const char *arg) { - printf ("%s\n", arg); if (arg[1] == 'O') { - FILE *file = fopen (&arg[2], "w"); - output_files.safe_push (file); + add_generator_output (output_files, &arg[2], true); return true; } if (arg[1] == 'H') { - snprintf (header_name, 255, "%s", &arg[2]); - header = fopen (header_name, "w"); + if (header_name) + fatal ("option -H specified more than once"); + header_name = &arg[2]; return true; } return false; @@ -5421,14 +5413,18 @@ main (int argc, const char **argv) if (!init_rtx_reader_args_cb (argc, argv, handle_arg)) return (FATAL_EXIT_CODE); + if (!header_name) + fatal ("no -H output file specified"); if (output_files.is_empty ()) - output_files.safe_push (stdout); - - for (auto f : output_files) - write_header (f, header_name); + add_generator_output (output_files, NULL, true); + unsigned int header_index + = add_generator_output (output_files, header_name, false); + open_generator_outputs (output_files); + FILE *header = output_files[header_index].file; - FILE *file = NULL; - unsigned file_idx; + for (const generator_output &output : output_files) + if (output.partition_p) + write_header (output.file, header_name); /* Read the machine description. */ @@ -5436,7 +5432,6 @@ main (int argc, const char **argv) while (read_md_rtx (&info)) { rtx def = info.def; - file = choose_output (output_files, file_idx); acceptance_type acceptance; acceptance.partial_p = false; @@ -5494,8 +5489,9 @@ main (int argc, const char **argv) if (have_error) return FATAL_EXIT_CODE; - for (auto f : output_files) - fprintf (f, "%s", "\n\n"); + for (const generator_output &output : output_files) + if (output.partition_p) + fprintf (output.file, "%s", "\n\n"); /* Optimize each routine in turn. */ optimize_subroutine_group ("recog", &insn_root); @@ -5522,10 +5518,7 @@ main (int argc, const char **argv) print_pattern (header, &os, routine, true); FOR_EACH_VEC_ELT (patterns, i, routine) - { - file = choose_output (output_files, file_idx); - print_pattern (file, &os, routine); - } + print_pattern (choose_output (output_files), &os, routine); } /* Print out the matching routines. */ @@ -5533,11 +5526,6 @@ main (int argc, const char **argv) print_subroutine_group (output_files, header, &os, SPLIT, &split_root); print_subroutine_group (output_files, header, &os, PEEPHOLE2, &peephole2_root); - fclose (header); - - int ret = SUCCESS_EXIT_CODE; - for (FILE *f : output_files) - if (fclose (f) != 0) - ret = FATAL_EXIT_CODE; - return ret; + return (close_generator_outputs (output_files) + ? SUCCESS_EXIT_CODE : FATAL_EXIT_CODE); } diff --git a/gcc/gensupport.cc b/gcc/gensupport.cc index 5fcc30d340e..9439ddd97d8 100644 --- a/gcc/gensupport.cc +++ b/gcc/gensupport.cc @@ -3935,35 +3935,77 @@ find_optab (optab_pattern *p, const char *name) return false; } -/* Find the file to write into next. We try to evenly distribute the contents - over the different files. */ +/* Add output NAME to OUTPUTS. A null NAME means standard output. + PARTITION_P is true if the output participates in size-based selection. + Return its index. */ -#define SIZED_BASED_CHUNKS 1 +unsigned int +add_generator_output (vec &outputs, const char *name, + bool partition_p) +{ + gcc_assert (name || outputs.is_empty ()); + if (name) + for (const generator_output &output : outputs) + if (output.name && canonical_filename_eq (name, output.name)) + fatal ("output file %s specified more than once", name); + + generator_output output = { name, name ? NULL : stdout, partition_p }; + unsigned int index = outputs.length (); + outputs.safe_push (output); + return index; +} + +/* Open each named file in OUTPUTS. */ + +void +open_generator_outputs (vec &outputs) +{ + for (generator_output &output : outputs) + if (!output.file) + { + output.file = fopen (output.name, "w"); + if (!output.file) + fatal ("cannot open file %s: %s", output.name, xstrerror (errno)); + } +} + +/* Return the shortest partition file in OUTPUTS. */ FILE * -choose_output (const vec &parts, unsigned &idx) +choose_output (const vec &outputs) { - if (parts.length () == 0) - gcc_unreachable (); -#ifdef SIZED_BASED_CHUNKS FILE *shortest = NULL; long min = 0; - idx = 0; - for (unsigned i = 0; i < parts.length (); i++) + for (const generator_output &output : outputs) { - FILE *part = parts[i]; - long len = ftell (part); + if (!output.partition_p) + continue; + long len = ftell (output.file); if (!shortest || min > len) { - shortest = part; + shortest = output.file; min = len; - idx = i; - } + } } + if (!shortest) + gcc_unreachable (); return shortest; -#else - static int current_file; - idx = current_file++ % parts.length (); - return parts[idx]; -#endif +} + +/* Close all files in OUTPUTS. Return true if every close succeeds. */ + +bool +close_generator_outputs (const vec &outputs) +{ + bool ok = true; + for (const generator_output &output : outputs) + { + if (fclose (output.file) != 0) + { + error ("cannot close output %s: %s", + output.name ? output.name : "", xstrerror (errno)); + ok = false; + } + } + return ok; } diff --git a/gcc/gensupport.h b/gcc/gensupport.h index 86dd1103436..880505355b2 100644 --- a/gcc/gensupport.h +++ b/gcc/gensupport.h @@ -232,6 +232,20 @@ extern void compute_test_codes (rtx, file_location, char *); extern file_location get_file_location (rtx); extern const char *get_emit_function (rtx); extern bool find_optab (optab_pattern *, const char *); -extern FILE *choose_output (const vec &, unsigned &); + +/* An output file produced by a machine-description generator. Partition + files participate in size-based output selection. */ +struct generator_output +{ + const char *name; + FILE *file; + bool partition_p; +}; + +extern unsigned int add_generator_output (vec &, + const char *, bool); +extern void open_generator_outputs (vec &); +extern FILE *choose_output (const vec &); +extern bool close_generator_outputs (const vec &); #endif /* GCC_GENSUPPORT_H */