From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 82174 invoked by alias); 4 Jul 2017 10:16:19 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 82137 invoked by uid 89); 4 Jul 2017 10:16:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.6 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= 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; Tue, 04 Jul 2017 10:16:16 +0000 Received: from nat-ies.mentorg.com ([192.94.31.2] helo=SVR-IES-MBX-04.mgc.mentorg.com) by relay1.mentorg.com with esmtp id 1dSKsQ-0002J9-4S from Tom_deVries@mentor.com ; Tue, 04 Jul 2017 03:16:14 -0700 Received: from [127.0.0.1] (137.202.0.87) by SVR-IES-MBX-04.mgc.mentorg.com (139.181.222.4) with Microsoft SMTP Server (TLS) id 15.0.1263.5; Tue, 4 Jul 2017 11:16:09 +0100 Subject: [PATCH, 1/3] Handle GOMP_NVPTX_{DISASM,SAVE_TEMPS} in libgomp nvptx plugin From: Tom de Vries To: Thomas Schwinge , Jakub Jelinek CC: GCC Patches , Joseph Myers References: <2413b0f6-9cb2-243f-d805-08323a9c9a0a@mentor.com> <1de94c83-96da-f380-9964-1472f63270c9@mentor.com> <20170626152911.GK2123@tucnak> <87ziclsjed.fsf@hertz.schwinge.homeip.net> <80acb38b-1828-7d1b-e7fc-2a87cc0ad0ae@mentor.com> <809397ef-5508-5d7e-d4eb-f03213c5e690@mentor.com> Message-ID: <249c791d-6772-8938-bab5-55ee01659f0a@mentor.com> Date: Tue, 04 Jul 2017 10:16:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: <809397ef-5508-5d7e-d4eb-f03213c5e690@mentor.com> Content-Type: multipart/mixed; boundary="------------ADD517F200EF4CAB2D6996E6" 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) X-SW-Source: 2017-07/txt/msg00168.txt.bz2 --------------ADD517F200EF4CAB2D6996E6 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Content-length: 2186 On 07/04/2017 12:05 PM, Tom de Vries wrote: > On 07/03/2017 04:24 PM, Tom de Vries wrote: >> On 07/03/2017 04:08 PM, Thomas Schwinge wrote: >>> Hi! >>> >>> On Mon, 26 Jun 2017 17:29:11 +0200, Jakub Jelinek >>> wrote: >>>> On Mon, Jun 26, 2017 at 03:26:57PM +0000, Joseph Myers wrote: >>>>> On Mon, 26 Jun 2017, Tom de Vries wrote: >>>>> >>>>>>> 2. Handle GOMP_OPENACC_NVPTX_{DISASM,SAVE_TEMPS} in libgomp nvptx >>>>>>> plugin >>>>>> >>>>>> This patch adds handling of: >>>>>> - GOMP_OPENACC_NVPTX_SAVE_TEMPS=[01], and >>>>>> - GOMP_OPENACC_NVPTX_DISASM=[01] >>> >>> Why the "OPENACC" in these names? >> >> I took the format from 'GOMP_OPENACC_DIM'. >> >>> Doesn't this debugging aid apply to >>> any variant of offloading? >> >> I guess you're right. These environment variables would also be >> applicable for f.i. offloading via openmp on nvptx. I'll strip the >> 'OPENACC_' bit from the variables. >> >>>>>> The filename used for dumping the module is plugin-nvptx..cubin. >>> >>> Also, I suggest to make these names similar to their controlling >>> options, >>> that is: "gomp-nvptx*", for example. >>> >> >> Makes sense, will do. > > Changes in the patch series: > - removed OPENACC_ from environment variable names > - made temp files use gomp-nvptx prefix. > - fixed build error due to missing _GNU_SOURCE in libgomp-nvptx.c. > - merged the three GOMP_NVPTX_JIT patches into one > - rewrote GOMP_NVPTX_JIT to add no extra flags to the JIT compiler > invocation if GOMP_NVPTX_JIT if not defined, removing the need for > hardcoding default values > - added CU_JIT_TARGET to plugin/cuda/cuda.h > > Build on x86_64 with nvptx offloading enabled (using plugin/cuda/cuda.h). > > The patch series now looks like: > 1. Handle GOMP_NVPTX_{DISASM,SAVE_TEMPS} in libgomp nvptx plugin > 2. Handle GOMP_NVPTX_PTXRW in libgomp nvptx plugin > 3. Handle GOMP_NVPTX_JIT={-O[0-4],-ori,-arch=} in libgomp nvptx > plugin > > I'll repost the patch series in reply to this email. 1. Handle GOMP_NVPTX_{DISASM,SAVE_TEMPS} in libgomp nvptx plugin ( original submission at https://gcc.gnu.org/ml/gcc-patches/2017-06/msg01918.html ) Thanks, - Tom --------------ADD517F200EF4CAB2D6996E6 Content-Type: text/x-patch; name="0001-Handle-GOMP_NVPTX_-DISASM-SAVE_TEMPS-in-libgomp-nvptx-plugin.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename*0="0001-Handle-GOMP_NVPTX_-DISASM-SAVE_TEMPS-in-libgomp-nvptx-p"; filename*1="lugin.patch" Content-length: 4309 Handle GOMP_NVPTX_{DISASM,SAVE_TEMPS} in libgomp nvptx plugin 2017-06-26 Tom de Vries * plugin/plugin-nvptx.c (do_prog, debug_linkout): New function. (link_ptx): Use debug_linkout. --- libgomp/plugin/plugin-nvptx.c | 106 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/libgomp/plugin/plugin-nvptx.c b/libgomp/plugin/plugin-nvptx.c index 71630b5..3e33c5b 100644 --- a/libgomp/plugin/plugin-nvptx.c +++ b/libgomp/plugin/plugin-nvptx.c @@ -31,6 +31,7 @@ is not clear as to what that state might be. Or how one might propagate it from one thread to another. */ +#define _GNU_SOURCE #include "openacc.h" #include "config.h" #include "libgomp-plugin.h" @@ -47,6 +48,9 @@ #include #include #include +#include +#include +#include #if PLUGIN_NVPTX_DYNAMIC # include @@ -138,6 +142,8 @@ init_cuda_lib (void) # define init_cuda_lib() true #endif +#include "secure_getenv.h" + /* Convenience macros for the frequently used CUDA library call and error handling sequence as well as CUDA library calls that do the error checking themselves or don't do it at all. */ @@ -876,6 +882,104 @@ notify_var (const char *var_name, const char *env_var) GOMP_PLUGIN_debug (0, "%s: '%s'\n", var_name, env_var); } +static void +do_prog (const char *prog, const char *arg) +{ + pid_t pid = fork (); + + if (pid == -1) + { + GOMP_PLUGIN_error ("Fork failed"); + return; + } + else if (pid > 0) + { + int status; + waitpid (pid, &status, 0); + if (!WIFEXITED (status)) + GOMP_PLUGIN_error ("Running %s %s failed", prog, arg); + } + else + { + execlp (prog, prog /* argv[0] */, arg, NULL); + abort (); + } +} + +static void +debug_linkout (void *linkout, size_t linkoutsize) +{ + static int gomp_nvptx_disasm = -1; + if (gomp_nvptx_disasm == -1) + { + const char *var_name = "GOMP_NVPTX_DISASM"; + const char *env_var = secure_getenv (var_name); + notify_var (var_name, env_var); + gomp_nvptx_disasm + = ((env_var != NULL && env_var[0] == '1' && env_var[1] == '\0') + ? 1 : 0); + } + + static int gomp_nvptx_save_temps = -1; + if (gomp_nvptx_save_temps == -1) + { + const char *var_name = "GOMP_NVPTX_SAVE_TEMPS"; + const char *env_var = secure_getenv (var_name); + notify_var (var_name, env_var); + gomp_nvptx_save_temps + = ((env_var != NULL && env_var[0] == '1' && env_var[1] == '\0') + ? 1 : 0); + } + + if (gomp_nvptx_disasm == 0 + && gomp_nvptx_save_temps == 0) + return; + + const char *prefix = "gomp-nvptx."; + const char *postfix = ".cubin"; + const int len = (strlen (prefix) + + 20 /* %lld. */ + + strlen (postfix) + + 1 /* '\0'. */); + char file_name[len]; + int res = snprintf (file_name, len, "%s%lld%s", prefix, + (long long)getpid (), postfix); + assert (res < len); /* Assert there's no truncation. */ + + GOMP_PLUGIN_debug (0, "Generating %s with size %zu\n", + file_name, linkoutsize); + FILE *cubin_file = fopen (file_name, "wb"); + if (cubin_file == NULL) + { + GOMP_PLUGIN_debug (0, "Opening %s failed\n", file_name); + return; + } + + fwrite (linkout, linkoutsize, 1, cubin_file); + unsigned int write_succeeded = ferror (cubin_file) == 0; + if (!write_succeeded) + GOMP_PLUGIN_debug (0, "Writing %s failed\n", file_name); + + res = fclose (cubin_file); + if (res != 0) + GOMP_PLUGIN_debug (0, "Closing %s failed\n", file_name); + + if (!write_succeeded) + return; + + if (gomp_nvptx_disasm == 1) + { + GOMP_PLUGIN_debug (0, "Disassembling %s\n", file_name); + do_prog ("nvdisasm", file_name); + } + + if (gomp_nvptx_save_temps == 0) + { + GOMP_PLUGIN_debug (0, "Removing %s\n", file_name); + remove (file_name); + } +} + static bool link_ptx (CUmodule *module, const struct targ_ptx_obj *ptx_objs, unsigned num_objs) @@ -939,6 +1043,8 @@ link_ptx (CUmodule *module, const struct targ_ptx_obj *ptx_objs, return false; } + debug_linkout (linkout, linkoutsize); + CUDA_CALL (cuModuleLoadData, module, linkout); CUDA_CALL (cuLinkDestroy, linkstate); return true; --------------ADD517F200EF4CAB2D6996E6--