From patchwork Mon Sep 12 08:19:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 1153 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp1781794wrt; Mon, 12 Sep 2022 01:32:31 -0700 (PDT) X-Google-Smtp-Source: AA6agR5cn+XYP4DiOZn6We/McukUYkhDPMVRqb32opuATl44UCpAmbxqo1jSV+QoKmpTjcXlOwZJ X-Received: by 2002:a17:907:60d6:b0:77d:8aed:cf86 with SMTP id hv22-20020a17090760d600b0077d8aedcf86mr2475935ejc.43.1662971551221; Mon, 12 Sep 2022 01:32:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662971551; cv=none; d=google.com; s=arc-20160816; b=vdx9pTwFTsbu+DSkDMTaM3KlPGOjbhKiJiScZdibkwX5+BFBb1b9fI5tkrkuBCLVdr zvJv+FEB//OZvqqmw3wbZn5DJlelN05VKMExLfJR/pFmEwHzvycCg6OUi64SLufMTI0s w0sa93mGpoZ2W5qoNVrmwwEJ8wTl7Df9OUw2rUwqe6s2jhETiwiRiTvrR3JGn0RokHyJ u+WrJ/bDZiDCmTkXCwvN0l+p2YtFV6yp+t5xy7jrFc6lISSgLuMyByk/K/217HKgQB6E pemf28kt0fiZeA/UtNYZgDivaC0eUOIaXeEYWS6QdUNp6voXAT98rgWQNhiVHAo+7Tus Z70A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Hc3ns2ZzHxe2EClT4QLAiaku2FGFiieCUl712FkeCQA=; b=C9ZdnAKsrlaa63hY/ACq3q/VVGkR+YagNLrs3mzncLz8RMIaCaYdmk9Io0m4DFvwFj wZeX4ur4QmVSJHLYpRvIn3dTaOra7nbJyx04KCznQit5ftckctPH5asfh4zScNtlLh5e 6fK0yLceRJTgtEsNTMrF3bnEQX55IZzqCm7wGN9gAYDVxVtW40zEID960dVf5TSLxqSz WkKZbjhj/83TuqtmZr2Mj18N4dqdG1HAd0l1WYm6N/Fwk4oFsgEUuYnpKLK5gNvYCr33 9YKWskSs3/AIquaZIBANZ8Zh1CnUhOZXQRxoJwcutTvvv0S6gG4DkA29PCG/Jjb4jjRC sgjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=TuCA0gKb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id q23-20020a50cc97000000b004479e9c138dsi6007825edi.447.2022.09.12.01.32.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Sep 2022 01:32:31 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=TuCA0gKb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3397339960D9 for ; Mon, 12 Sep 2022 08:25:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3397339960D9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662971116; bh=Hc3ns2ZzHxe2EClT4QLAiaku2FGFiieCUl712FkeCQA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=TuCA0gKbyh+2fa34hqtZQ6bXaA9L4MrDCkcy/ekt+xFIL0ddaNdSD74bKjNV9j3F/ 9FUEw/CFDBIFOKM2t8WrO1zEF9C+CJvJMKVBcrvFutfV0w9fPYj+usf7OCFT1LeZ/D ugXlg+2i6M9Qy6b5xg8DJG6dfgIcJvLquZnHoOmI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id CEC2F3850867 for ; Mon, 12 Sep 2022 08:19:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CEC2F3850867 Received: by mail-wr1-x433.google.com with SMTP id b5so14045326wrr.5 for ; Mon, 12 Sep 2022 01:19:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date; bh=Hc3ns2ZzHxe2EClT4QLAiaku2FGFiieCUl712FkeCQA=; b=tbzt1OncSEibwMRLq4qdhLsgS6eq0gqKixh7iTkhFOgSz74arM6zMD7Cl9pH8UawYU L5HBfFgKK8w/Ehk+rgoSsIMUUYb8HlapZcWzo5MwfIXDiuKRsc++NWv39cm8tuUHDltz VeYW46Sy6AdVri/4qkO7k3uP6EgSijzehS8p10Qs9gDoa8y5r1LAN8Gcbh+vTE1XiO1M +LCw8AZGO/oGxuTFaC9xi7Ekg0AvGjiA/7bFJCtxUZZoTZqkfD2ngU9tqjQWmtHWBH24 AL6EdoRG0PhwgVQxgJLWgHDbWsUDBkmS4NKLB7Ndw+VKNSfGAirriaddt1HkaesNUI49 qlfA== X-Gm-Message-State: ACgBeo0TV6lisEA/GycTm7OdMG5ObM/1Qb3TFutl0tqd/xQhvVIJXcNn 9M3NVb7ifTkJ2U2xEhvrZ7t1s2oOVOc3hg== X-Received: by 2002:a5d:588b:0:b0:22a:a41c:9a9c with SMTP id n11-20020a5d588b000000b0022aa41c9a9cmr932379wrf.357.1662970781473; Mon, 12 Sep 2022 01:19:41 -0700 (PDT) Received: from poulhies-Precision-5550 (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id l128-20020a1c2586000000b003b340f00f10sm9236259wml.31.2022.09.12.01.19.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Sep 2022 01:19:40 -0700 (PDT) Date: Mon, 12 Sep 2022 10:19:40 +0200 To: gcc-patches@gcc.gnu.org Subject: [Ada] Improve CUDA host-side and device-side binder support Message-ID: <20220912081940.GA1513101@poulhies-Precision-5550> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: Marc =?iso-8859-1?q?Poulhi=E8s?= Cc: Steve Baird Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743751975971209597?= X-GMAIL-MSGID: =?utf-8?q?1743752057034684632?= Use switches (one already existing, one newly added here) to indicate to the binder that CUDA support code is to be generated for either the host side or for the device side. Add an invocation of Adainit on the device side from Adainit on the host side; similarly for Adafinal. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * bindgen.adb: When the binder is invoked for the host, it declares imported subprograms corresponding to the Adainit and Adafinal routines on the device. Declare string constants and expression functions for the Ada source names and the link names of these routines. Generate these subprogram declarations (and accompanying Import pragmas) in Gen_CUDA_Defs. Generate CUDA_Execute pragmas to call these subprograms from the host in Gen_Adafinal and Gen_CUDA_Init. When the binder is invoked for the device, include a CUDA_Global aspect declaration in the declarations of Adainit and Adafinal and use the aforementioned link names in the Export pragmas generated for those two routines. * debug.adb: Update comments about "d_c" and "d_d" switches. * opt.ads: Declare new Boolean variable, Enable_CUDA_Device_Expansion. This complements the existing Enable_CUDA_Expansion variable, which is used to enable host-side CUDA expansion. The new variable enables device-side CUDA expansion. It is currently never set during compilation; it is only set via a binder switch. * switch-b.adb (scan_debug_switches): Add new use of the "-d_d" binder switch. The new switch and the variable Opt.Enabled_CUDA_Device_Expansion follow the existing pattern of the "-d_c" switch and the variable Opt.Enabled_CUDA_Expansion. Flag error if both "-d_c" and "-d_d" are specified. diff --git a/gcc/ada/bindgen.adb b/gcc/ada/bindgen.adb --- a/gcc/ada/bindgen.adb +++ b/gcc/ada/bindgen.adb @@ -114,6 +114,29 @@ package body Bindgen is -- For CodePeer, introduce a wrapper subprogram which calls the -- user-defined main subprogram. + -- Names and link_names for CUDA device adainit/adafinal procs. + + Device_Subp_Name_Prefix : constant String := "imported_device_"; + Device_Link_Name_Prefix : constant String := "__device_"; + + function Device_Ada_Final_Link_Name return String is + (Device_Link_Name_Prefix & Ada_Final_Name.all); + + function Device_Ada_Final_Subp_Name return String is + (Device_Subp_Name_Prefix & Ada_Final_Name.all); + + function Device_Ada_Init_Link_Name return String is + (Device_Link_Name_Prefix & Ada_Init_Name.all); + + function Device_Ada_Init_Subp_Name return String is + (Device_Subp_Name_Prefix & Ada_Init_Name.all); + + -- Text for aspect specifications (if any) given as part of the + -- Adainit and Adafinal spec declarations. + + function Aspect_Text return String is + (if Enable_CUDA_Device_Expansion then " with CUDA_Global" else ""); + ---------------------------------- -- Interface_State Pragma Table -- ---------------------------------- @@ -501,6 +524,12 @@ package body Bindgen is WBI (" System.Standard_Library.Adafinal;"); end if; + -- perform device (as opposed to host) finalization + if Enable_CUDA_Expansion then + WBI (" pragma CUDA_Execute (" & + Device_Ada_Final_Subp_Name & ", 1, 1);"); + end if; + WBI (" end " & Ada_Final_Name.all & ";"); WBI (""); end Gen_Adafinal; @@ -512,7 +541,6 @@ package body Bindgen is procedure Gen_Adainit (Elab_Order : Unit_Id_Array) is Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority; Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU; - begin -- Declare the access-to-subprogram type used for initialization of -- of __gnat_finalize_library_objects. This is declared at library @@ -1334,6 +1362,13 @@ package body Bindgen is end; end loop; + WBI (" procedure " & Device_Ada_Init_Subp_Name & ";"); + WBI (" pragma Import (C, " & Device_Ada_Init_Subp_Name & + ", Link_Name => """ & Device_Ada_Init_Link_Name & """);"); + WBI (" procedure " & Device_Ada_Final_Subp_Name & ";"); + WBI (" pragma Import (C, " & Device_Ada_Final_Subp_Name & + ", Link_Name => """ & Device_Ada_Final_Link_Name & """);"); + WBI (""); end Gen_CUDA_Defs; @@ -1393,6 +1428,10 @@ package body Bindgen is end loop; WBI (" CUDA_Register_Fat_Binary_End (Fat_Binary_Handle);"); + + -- perform device (as opposed to host) elaboration + WBI (" pragma CUDA_Execute (" & + Device_Ada_Init_Subp_Name & ", 1, 1);"); end Gen_CUDA_Init; -------------------------- @@ -2602,9 +2641,14 @@ package body Bindgen is end if; WBI (""); - WBI (" procedure " & Ada_Init_Name.all & ";"); - WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ & - Ada_Init_Name.all & """);"); + WBI (" procedure " & Ada_Init_Name.all & Aspect_Text & ";"); + if Enable_CUDA_Device_Expansion then + WBI (" pragma Export (C, " & Ada_Init_Name.all & + ", Link_Name => """ & Device_Ada_Init_Link_Name & """);"); + else + WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ & + Ada_Init_Name.all & """);"); + end if; -- If -a has been specified use pragma Linker_Constructor for the init -- procedure and pragma Linker_Destructor for the final procedure. @@ -2615,9 +2659,15 @@ package body Bindgen is if not Cumulative_Restrictions.Set (No_Finalization) then WBI (""); - WBI (" procedure " & Ada_Final_Name.all & ";"); - WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ & - Ada_Final_Name.all & """);"); + WBI (" procedure " & Ada_Final_Name.all & Aspect_Text & ";"); + + if Enable_CUDA_Device_Expansion then + WBI (" pragma Export (C, " & Ada_Final_Name.all & + ", Link_Name => """ & Device_Ada_Final_Link_Name & """);"); + else + WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ & + Ada_Final_Name.all & """);"); + end if; if Use_Pragma_Linker_Constructor then WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");"); diff --git a/gcc/ada/debug.adb b/gcc/ada/debug.adb --- a/gcc/ada/debug.adb +++ b/gcc/ada/debug.adb @@ -142,7 +142,7 @@ package body Debug is -- d_a Stop elaboration checks on accept or select statement -- d_b Use designated type model under No_Dynamic_Accessibility_Checks -- d_c CUDA compilation : compile for the host - -- d_d + -- d_d CUDA compilation : compile for the device -- d_e Ignore entry calls and requeue statements for elaboration -- d_f Issue info messages related to GNATprove usage -- d_g Disable large static aggregates @@ -345,8 +345,8 @@ package body Debug is -- d_a Ignore the effects of pragma Elaborate_All -- d_b Ignore the effects of pragma Elaborate_Body - -- d_c - -- d_d + -- d_c CUDA compilation : compile/bind for the host + -- d_d CUDA compilation : compile/bind for the device -- d_e Ignore the effects of pragma Elaborate -- d_f -- d_g diff --git a/gcc/ada/opt.ads b/gcc/ada/opt.ads --- a/gcc/ada/opt.ads +++ b/gcc/ada/opt.ads @@ -544,6 +544,13 @@ package Opt is -- Set to True to enable CUDA host expansion: -- - Removal of CUDA_Global and CUDA_Device symbols -- - Generation of kernel registration code in packages + -- - Binder invokes device elaboration/finalization code + + Enable_CUDA_Device_Expansion : Boolean := False; + -- GNATBIND + -- Set to True to enable CUDA device (as opposed to host) expansion: + -- - Binder generates elaboration/finalization code that can be + -- invoked from corresponding binder-generated host-side code. Error_Msg_Line_Length : Nat := 0; -- GNAT diff --git a/gcc/ada/switch-b.adb b/gcc/ada/switch-b.adb --- a/gcc/ada/switch-b.adb +++ b/gcc/ada/switch-b.adb @@ -158,9 +158,18 @@ package body Switch.B is elsif Underscore then Set_Underscored_Debug_Flag (C); + if Debug_Flag_Underscore_C then Enable_CUDA_Expansion := True; end if; + if Debug_Flag_Underscore_D then + Enable_CUDA_Device_Expansion := True; + end if; + if Enable_CUDA_Expansion and Enable_CUDA_Device_Expansion + then + Bad_Switch (Switch_Chars); + end if; + Underscore := False; -- letter