From patchwork Fri Jul 19 14:57:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 1962528 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=o9bxicXQ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WQXqM0x7Zz1xpQ for ; Sat, 20 Jul 2024 00:58:23 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4A6AA3842FF1 for ; Fri, 19 Jul 2024 14:58:21 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by sourceware.org (Postfix) with ESMTPS id 82610384A43A for ; Fri, 19 Jul 2024 14:57:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 82610384A43A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=baylibre.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 82610384A43A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::52d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721401072; cv=none; b=UK4D6j1yR5+Pydwrx7mEJdK/OFd/AsB+v801HqAz1dxhHNfg4/dXHRCosx9U5u7c03mnmIkntfI6+99/DKYNG7821l9/M5+umrj0K2nt3VICDg0LWU52+BdTPRSAgIf4Oq7yBcgQSY01ZiFY5MawyWtF7Dcb3Q2mE+gnblOH4pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721401072; c=relaxed/simple; bh=JpRGeVe4UH1F3Kxm3H4kSZErybDIHlpreciIIklc8zI=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=XetMPEHBaFj9l4rAXdy9r/OlzUr1xaic4+9K7FQ9GRtleSQ00ufUf5dFA/XBqzPmETrRdflpGUuzsNCxP7A2sY41WJNqO370bEhvEyZ+skbPDt5ehbV7LMTkr+7ZNMi2/70OvCSUVJOFxhHleivLkSE26f2tjcBMw0sO+bi4pK8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-58b0beaf703so1004515a12.2 for ; Fri, 19 Jul 2024 07:57:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1721401068; x=1722005868; darn=gcc.gnu.org; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=cNyhx1hLz5qNWWQl9rYPGoSiIutUt6L4+y74mkNAd54=; b=o9bxicXQ6zClAU8WEy89lZC4eiuVYpEKGJYF+dAzBJD56GUL0JjfIv0tzNAkHzoBql A6rWSiqSaEsCeqfN9oy1bXPZKJ7y/DxGKwIHdVGFg84wnNN4j8v9PcGfSIjHeDjCONh4 lIilPQ9jnUFv4aU1EWW6fP6pi0Qh2rtRBUlbFIw94s1q2gaK6zBP+NlopDIEx6Q2D/jF FOkyWMZpq2nr2HxayWRLdYTsE9K6gLPKJFldRK8BWIWdVFVA5iCDP6aTRrNNif7t96rl ZbVwH6X/d72zGGG/LZoXgkVaRWGDRIEeCax7wd2Lo2f6xwqPfBVXEkfJ1Jbm9XS8ppYJ xOEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721401068; x=1722005868; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=cNyhx1hLz5qNWWQl9rYPGoSiIutUt6L4+y74mkNAd54=; b=vjllZV/cfrWux5S/pDtTr3fcbx8dkwqCEcZ34zMolvcxoLIRHcrJJPdvfzPM7d/MZm XnFt9SX4GG59pQrNzgJ+g7Oq/+NJ0qg+SySTfl7Flc+4IiU/+lbqGySd+SvZWq06PLwy OVo+/piBW9sLTqaf5FpIOPz8ELTPd7DTRPUXMqNk2DfYwsmHn1UQZlBVfTNCVkzcMUie UZiZqJ+YWlCxFxTK+K6mdAafE5d/feiwevnB/dbfJ/XWX5Ie6AvaaQ5kkUBwn0gGaW8K AM4yhMydojitugwtZ+2osyJRFl8nrQdNJcH0JtoPVefiB5tAMfjSs6+ghJH3q6B5jb9V H0vw== X-Gm-Message-State: AOJu0Yy4QZHkIZXOC8Lk1dsd1TFuBkqOLBf6ZSNo/oozWIKexgPPRPEJ 9gmBDCmiR8qtUFy/o7+h/Pq1veCH0utUf1jI1/KZODXrqBb/DCDHIHGFW8BITN8= X-Google-Smtp-Source: AGHT+IEkyha2ThsjJMwZFEP76ghOEpNyPuYtVSwAle5yWebaCGP9gQ85ExBbOkjFlobiEdlA7FPxnQ== X-Received: by 2002:a17:906:546:b0:a72:8100:c3e with SMTP id a640c23a62f3a-a7a01337211mr564381966b.48.1721401067719; Fri, 19 Jul 2024 07:57:47 -0700 (PDT) Received: from ?IPV6:2001:16b8:26b8:2500:d5d0:b87:ced5:5491? (200116b826b82500d5d00b87ced55491.dip.versatel-1u1.de. [2001:16b8:26b8:2500:d5d0:b87:ced5:5491]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7a3c8bf2f5sm41544966b.120.2024.07.19.07.57.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 19 Jul 2024 07:57:47 -0700 (PDT) Message-ID: <8e92be89-e9f0-457c-962a-e8804c59b7ad@baylibre.com> Date: Fri, 19 Jul 2024 16:57:46 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [Patch, v3] gcn/mkoffload.cc: Use #embed for including the generated ELF file To: Jakub Jelinek , Andrew Stubbs Cc: gcc-patches References: <48aa0cd1-20cd-4a95-a729-699e2c02a9d9@baylibre.com> <4723211d-6c9e-4226-8950-531ed1b8dec1@baylibre.com> Content-Language: en-US From: Tobias Burnus In-Reply-To: X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, 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~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, Jakub Jelinek wrote: >> + "#if defined(__STDC_EMBED_FOUND__) && __has_embed (\"%s\") == __STDC_EMBED_FOUND__\n" > If this was an attempt to deal gracefully with no #embed support, then > the above would be wrong and should have been > #if defined(__STDC_EMBED_FOUND__) && defined(__has_embed) > #if __has_embed ("whatever") == __STDC_EMBED_FOUND__ I was kind of both – assuming that #embed is available (as it should be compiled by the accompanied compiler) but handle the case that it is not. However, as '#embed' is well diagnosed if unsupported, that part is not really needed. > Now, if all you want is an error if the file doesn't exist, then > #embed "whatever" > will do that too […] > > If you want an error not just when it doesn't exist, but also when it > is empty, then you could do > #embed "whatever" if_empty (%%%) The idea was to also error out if the file is empty – as that shouldn't happen here: if offloading code was found, the code gen should be done. However, using an invalid expression seems to be a good idea as that's really a special case that shouldn't happen. * * * I have additionally replaced the #include by __UINTPTR_TYPE__ and __SIZE_TYPE__ to avoid including 3 header files; this doesn't have a large effect, but still. Updated patch attached. OK for mainline, once Jakub's #embed is committed? * * * BTW: Testing shows for a hello world program (w/o #embed patch) For -foffload=...: 'disable' 0.04s, 'nvptx-none' 0.15s, 'amdgcn-amdhsa' 1.2s. With a simple #embed (this patch plus Jakub's first patch), the performance is unchanged. I then applied Jakub's follow up patches, but I then get an ICE (Jakub will have a look). But compiling it with 'g++' (→ COLLECT_GCC is g++) works; result: takes 0.2s (~6× faster) and compiling for both nvptx and gcn takes 0.3s, nearly 5× faster. Tobias gcn/mkoffload.cc: Use #embed for including the generated ELF file gcc/ChangeLog: * config/gcn/mkoffload.cc (read_file): Remove. (process_asm): Do not add '#include' to generated C file. (process_obj): Generate C file that uses #embed and use __SIZE_TYPE__ and __UINTPTR_TYPE__ instead the #include-defined size_t and uintptr. (main): Update call to it; remove no longer needed file I/O. gcc/config/gcn/mkoffload.cc | 79 +++++++-------------------------------------- 1 file changed, 12 insertions(+), 67 deletions(-) diff --git a/gcc/config/gcn/mkoffload.cc b/gcc/config/gcn/mkoffload.cc index 810298a799b..c3c998639ff 100644 --- a/gcc/config/gcn/mkoffload.cc +++ b/gcc/config/gcn/mkoffload.cc @@ -182,44 +182,6 @@ xputenv (const char *string) putenv (CONST_CAST (char *, string)); } -/* Read the whole input file. It will be NUL terminated (but - remember, there could be a NUL in the file itself. */ - -static const char * -read_file (FILE *stream, size_t *plen) -{ - size_t alloc = 16384; - size_t base = 0; - char *buffer; - - if (!fseek (stream, 0, SEEK_END)) - { - /* Get the file size. */ - long s = ftell (stream); - if (s >= 0) - alloc = s + 100; - fseek (stream, 0, SEEK_SET); - } - buffer = XNEWVEC (char, alloc); - - for (;;) - { - size_t n = fread (buffer + base, 1, alloc - base - 1, stream); - - if (!n) - break; - base += n; - if (base + 1 == alloc) - { - alloc *= 2; - buffer = XRESIZEVEC (char, buffer, alloc); - } - } - buffer[base] = 0; - *plen = base; - return buffer; -} - /* Parse STR, saving found tokens into PVALUES and return their number. Tokens are assumed to be delimited by ':'. */ @@ -657,10 +619,6 @@ process_asm (FILE *in, FILE *out, FILE *cfile) struct oaccdims *dims = XOBFINISH (&dims_os, struct oaccdims *); struct regcount *regcounts = XOBFINISH (®counts_os, struct regcount *); - fprintf (cfile, "#include \n"); - fprintf (cfile, "#include \n"); - fprintf (cfile, "#include \n\n"); - fprintf (cfile, "static const int gcn_num_vars = %d;\n\n", var_count); fprintf (cfile, "static const int gcn_num_ind_funcs = %d;\n\n", ind_fn_count); @@ -725,35 +683,28 @@ process_asm (FILE *in, FILE *out, FILE *cfile) /* Embed an object file into a C source file. */ static void -process_obj (FILE *in, FILE *cfile, uint32_t omp_requires) +process_obj (const char *fname_in, FILE *cfile, uint32_t omp_requires) { - size_t len = 0; - const char *input = read_file (in, &len); - /* Dump out an array containing the binary. - FIXME: do this with objcopy. */ - fprintf (cfile, "static unsigned char gcn_code[] = {"); - for (size_t i = 0; i < len; i += 17) - { - fprintf (cfile, "\n\t"); - for (size_t j = i; j < i + 17 && j < len; j++) - fprintf (cfile, "%3u,", (unsigned char) input[j]); - } - fprintf (cfile, "\n};\n\n"); + If the file is empty, a parse error is shown as the argument to is_empty + is an undeclared identifier. */ + fprintf (cfile, + "static unsigned char gcn_code[] = {\n" + "#embed \"%s\" if_empty (error_file_is_empty)\n" + "};\n\n", fname_in); fprintf (cfile, "static const struct gcn_image {\n" - " size_t size;\n" + " __SIZE_TYPE__ size;\n" " void *image;\n" "} gcn_image = {\n" - " %zu,\n" + " sizeof(gcn_code),\n" " gcn_code\n" - "};\n\n", - len); + "};\n\n"); fprintf (cfile, "static const struct gcn_data {\n" - " uintptr_t omp_requires_mask;\n" + " __UINTPTR_TYPE__ omp_requires_mask;\n" " const struct gcn_image *gcn_image;\n" " unsigned kernel_count;\n" " const struct hsa_kernel_description *kernel_infos;\n" @@ -1312,13 +1263,7 @@ main (int argc, char **argv) fork_execute (ld_argv[0], CONST_CAST (char **, ld_argv), true, ".ld_args"); obstack_free (&ld_argv_obstack, NULL); - in = fopen (gcn_o_name, "r"); - if (!in) - fatal_error (input_location, "cannot open intermediate gcn obj file"); - - process_obj (in, cfile, omp_requires); - - fclose (in); + process_obj (gcn_o_name, cfile, omp_requires); xputenv (concat ("GCC_EXEC_PREFIX=", execpath, NULL)); xputenv (concat ("COMPILER_PATH=", cpath, NULL));