From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wr1-x433.google.com with SMTP id b5so14045326wrr.5 for ; Mon, 12 Sep 2022 01:19:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date; bh=Hc3ns2ZzHxe2EClT4QLAiaku2FGFiieCUl712FkeCQA=; b=Xl47dDVoTptr4ODGGUiLFWmsJvhouHBM9dXJJVEudInm6V9Mh2ptU4D8B5adolbWhg SOvDPECcyzoFDdIXd8nQDR8oAy6C08IVqJtr6rtllgeMDNbjgw/rDWj3esuXveJuNRIk EICQp/JmKmnLem6Ox1XLKZtw935aZ3zU28L04YbQyHJzhJHv5VsJxqbn0qw6xkG2oV8L uxLG+Qu2w7TxVITz+gIwkbRhphq1TsibDdy+wmCHXKsIpP684t5W9jGvBgMqGEdmFyMK 14ZgaoVJ/iSEuVAd0Go21hUqSfVKfWnPAfAxzf4JpFvDmP5VvBoiyHWub8runLnd2Ufa Bj8Q== 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-Google-Smtp-Source: AA6agR74x4vCtvUxAIS5TFLHQK6UxcL8O8BDjlslpmYWK1Qq5UEL5FshebCmLdqn0hhkCF3jRY9S9w== 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 From: Marc =?iso-8859-1?Q?Poulhi=E8s?= To: gcc-patches@gcc.gnu.org Cc: Steve Baird Subject: [Ada] Improve CUDA host-side and device-side binder support Message-ID: <20220912081940.GA1513101@poulhies-Precision-5550> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="k+w/mQv8wyuph6w0" 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 List-Id: --k+w/mQv8wyuph6w0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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. --k+w/mQv8wyuph6w0 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="patch.diff" 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 --k+w/mQv8wyuph6w0--