From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id 5F5E33815FFD for ; Mon, 30 May 2022 08:32:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5F5E33815FFD Received: by mail-wr1-x42b.google.com with SMTP id s24so6287161wrb.10 for ; Mon, 30 May 2022 01:32:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=iNKVbwzgIgrUiEq8ESwY2o8SW9F0ixN6r+YiUFgmc4Y=; b=LyM9aGIben0c97KTbm7eLAVrhSaM18qePUASL09nIxPTOtnL0wLTKWr8A25GeTMDkU qz4kNyGMUJt/debBGsJICOuuUUD7/nwTpdXxoEbBUSOEw2l2nUdywhtfMyvbGvVsXQ4p DFIjQk8IFTlDVffNA9Dmu9vy3DBoY+7Qep+8G7DCVxOByZZh/qNq3lP98R+dtVY+nX+9 IRRlA1IP73GRYlPat+TYnZB/A5cshYXvDZn9qUx2JSSgNMuGnpTRtc8lD/XPGjonVprF n6Zf6WtbQJTzmZkMFMAhbR20x07Y1qcyN7ravODDoMCwtECsuNWALyP/Cr+Sk06d57tV j6sQ== X-Gm-Message-State: AOAM53063CO0ATH9hq0oO6H+IIL+xvQKF6hN+34gOP0axVMYJG3ZofvF Jk4sLIks5cVvJSm/EUrkRSm2VaSFtQyDvA== X-Google-Smtp-Source: ABdhPJwSYA3P5SZDv5l3YWzSkPAiz3wg03TmYWpFV6BLXCV9oWsiEFOgyMuV2WzniryCsXV1HnO+gA== X-Received: by 2002:a05:6000:156d:b0:210:3135:ce1c with SMTP id 13-20020a056000156d00b002103135ce1cmr4670888wrz.409.1653899554988; Mon, 30 May 2022 01:32:34 -0700 (PDT) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id 10-20020a5d47aa000000b0020fe35aec4bsm8884157wrb.70.2022.05.30.01.32.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 May 2022 01:32:34 -0700 (PDT) Date: Mon, 30 May 2022 08:32:33 +0000 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Ghjuvan Lacambre Subject: [Ada] Fix new CUDA kernel registration scheme Message-ID: <20220530083233.GA210555@adacore.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="0F1p//8PRICkK4MW" Content-Disposition: inline X-Spam-Status: No, score=-13.2 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-List-Received-Date: Mon, 30 May 2022 08:32:38 -0000 --0F1p//8PRICkK4MW Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Removal of the previous kernel registration scheme unearthed mistakes in the new one, which were: - The new kernel registration code relied on the binder expansion phase, which didn't happen because the registration code was already generated by the binder. - The kernel handle passed to CUDA_Register_Function was the first eight bytes of the code of the host-side procedure representing the kernel rather than its address. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * bindgen.adb (Gen_CUDA_Init): Remove code generating CUDA definitions. (Gen_CUDA_Defs): New function, generating definitions initialized by Gen_CUDA_Init. (Gen_Output_File_Ada): Call Gen_CUDA_Defs instead of Gen_CUDA_Init. (Gen_Adainit): Call Gen_CUDA_Init. --0F1p//8PRICkK4MW 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 @@ -311,8 +311,11 @@ package body Bindgen is procedure Gen_CodePeer_Wrapper; -- For CodePeer, generate wrapper which calls user-defined main subprogram + procedure Gen_CUDA_Defs; + -- Generate definitions needed in order to register kernels + procedure Gen_CUDA_Init; - -- When CUDA registration code is needed. + -- Generate calls needed in order to register kernels procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array); -- Generate sequence of elaboration calls @@ -1115,6 +1118,8 @@ package body Bindgen is WBI (""); end if; + Gen_CUDA_Init; + Gen_Elab_Calls (Elab_Order); if not CodePeer_Mode then @@ -1221,10 +1226,10 @@ package body Bindgen is end Gen_Bind_Env_String; ------------------- - -- Gen_CUDA_Init -- + -- Gen_CUDA_Defs -- ------------------- - procedure Gen_CUDA_Init is + procedure Gen_CUDA_Defs is Unit_Name : constant String := Get_Name_String (Units.Table (First_Unit_Entry).Uname); Unit : constant String := @@ -1237,7 +1242,7 @@ package body Bindgen is WBI (""); WBI (" "); - WBI (" function CUDA_Register_Function"); + WBI (" procedure CUDA_Register_Function"); WBI (" (Fat_Binary_Handle : System.Address;"); WBI (" Func : System.Address;"); WBI (" Kernel_Name : Interfaces.C.Strings.chars_ptr;"); @@ -1247,7 +1252,7 @@ package body Bindgen is WBI (" Nullptr2 : System.Address;"); WBI (" Nullptr3 : System.Address;"); WBI (" Nullptr4 : System.Address;"); - WBI (" Nullptr5 : System.Address) return Boolean;"); + WBI (" Nullptr5 : System.Address);"); WBI (" pragma Import"); WBI (" (Convention => C,"); WBI (" Entity => CUDA_Register_Function,"); @@ -1261,8 +1266,8 @@ package body Bindgen is WBI (" Entity => CUDA_Register_Fat_Binary,"); WBI (" External_Name => ""__cudaRegisterFatBinary"");"); WBI (""); - WBI (" function CUDA_Register_Fat_Binary_End"); - WBI (" (Fat_Binary : System.Address) return Boolean;"); + WBI (" procedure CUDA_Register_Fat_Binary_End"); + WBI (" (Fat_Binary : System.Address);"); WBI (" pragma Import"); WBI (" (Convention => C,"); WBI (" Entity => CUDA_Register_Fat_Binary_End,"); @@ -1287,8 +1292,7 @@ package body Bindgen is WBI (" Fat_Binary'Address,"); WBI (" System.Null_Address);"); WBI (""); - WBI (" Fat_Binary_Handle : System.Address :="); - WBI (" CUDA_Register_Fat_Binary (Wrapper'Address);"); + WBI (" Fat_Binary_Handle : System.Address;"); WBI (""); for K in CUDA_Kernels.First .. CUDA_Kernels.Last loop @@ -1300,9 +1304,9 @@ package body Bindgen is -- K_Symbol is a unique identifier used to derive all symbol names -- related to kernel K. - Kernel_Addr : constant String := Kernel_Symbol & "_Addr"; - -- Kernel_Addr is the name of the symbol representing the address - -- of the host-side procedure of the kernel. The address is + Kernel_Proc : constant String := Kernel_Symbol & "_Proc"; + -- Kernel_Proc is the name of the symbol representing the + -- host-side procedure of the kernel. The address is -- pragma-imported and then used while registering the kernel with -- the CUDA runtime. Kernel_String : constant String := Kernel_Symbol & "_String"; @@ -1315,40 +1319,80 @@ package body Bindgen is begin -- Import host-side kernel address. - WBI (" " & Kernel_Addr & " : constant System.Address;"); + WBI (" procedure " & Kernel_Proc & ";"); WBI (" pragma Import"); WBI (" (Convention => C,"); - WBI (" Entity => " & Kernel_Addr & ","); + WBI (" Entity => " & Kernel_Proc & ","); WBI (" External_Name => """ & Kernel_Name & """);"); WBI (""); -- Generate C-string containing name of kernel. WBI - (" " & Kernel_String & " : Interfaces.C.Strings.Chars_Ptr :="); - WBI (" Interfaces.C.Strings.New_Char_Array (""" - & Kernel_Name - & """);"); + (" " & Kernel_String & " : Interfaces.C.Strings.Chars_Ptr;"); WBI (""); + end; + end loop; + + WBI (""); + end Gen_CUDA_Defs; + + ------------------- + -- Gen_CUDA_Init -- + ------------------- + + procedure Gen_CUDA_Init is + begin + if not Enable_CUDA_Expansion then + return; + end if; + + WBI (" Fat_Binary_Handle :="); + WBI (" CUDA_Register_Fat_Binary (Wrapper'Address);"); + + for K in CUDA_Kernels.First .. CUDA_Kernels.Last loop + declare + K_String : constant String := CUDA_Kernel_Id'Image (K); + N : constant String := + K_String (K_String'First + 1 .. K_String'Last); + Kernel_Symbol : constant String := "Kernel_" & N; + -- K_Symbol is a unique identifier used to derive all symbol names + -- related to kernel K. + + Kernel_Proc : constant String := Kernel_Symbol & "_Proc"; + -- Kernel_Proc is the name of the symbol representing the + -- host-side procedure of the kernel. The address is + -- pragma-imported and then used while registering the kernel with + -- the CUDA runtime. + Kernel_String : constant String := Kernel_Symbol & "_String"; + -- Kernel_String is the name of the C-string containing the name + -- of the kernel. It is used for registering the kernel with the + -- CUDA runtime. + Kernel_Name : constant String := + Get_Name_String (CUDA_Kernels.Table (K).Kernel_Name); + -- Kernel_Name is the name of the kernel, after package expansion. + begin + WBI (" " & Kernel_String & " :="); + WBI (" Interfaces.C.Strings.New_Char_Array (""" + & Kernel_Name + & """);"); -- Generate call to CUDA runtime to register function. - WBI (" CUDA_Register" & N & " : Boolean :="); - WBI (" CUDA_Register_Function ("); - WBI (" Fat_Binary_Handle, "); - WBI (" " & Kernel_Addr & ","); - WBI (" " & Kernel_String & ","); - WBI (" " & Kernel_String & ","); - WBI (" -1,"); - WBI (" System.Null_Address,"); - WBI (" System.Null_Address,"); - WBI (" System.Null_Address,"); - WBI (" System.Null_Address,"); - WBI (" System.Null_Address);"); + WBI (" CUDA_Register_Function ("); + WBI (" Fat_Binary_Handle, "); + WBI (" " & Kernel_Proc & "'Address,"); + WBI (" " & Kernel_String & ","); + WBI (" " & Kernel_String & ","); + WBI (" -1,"); + WBI (" System.Null_Address,"); + WBI (" System.Null_Address,"); + WBI (" System.Null_Address,"); + WBI (" System.Null_Address,"); + WBI (" System.Null_Address);"); WBI (""); end; end loop; - WBI (" CUDA_End : Boolean := "); - WBI (" CUDA_Register_Fat_Binary_End(Fat_Binary_Handle);"); + WBI (" CUDA_Register_Fat_Binary_End (Fat_Binary_Handle);"); end Gen_CUDA_Init; -------------------------- @@ -2619,7 +2663,7 @@ package body Bindgen is Get_Main_Name & """);"); end if; - Gen_CUDA_Init; + Gen_CUDA_Defs; -- Generate version numbers for units, only if needed. Be very safe on -- the condition. --0F1p//8PRICkK4MW--