From patchwork Fri Oct 1 06:14:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1535194 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=IaSmKE8S; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HLL7t0N9Rz9sPf for ; Fri, 1 Oct 2021 16:39:54 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B81CD385780F for ; Fri, 1 Oct 2021 06:39:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B81CD385780F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1633070391; bh=W7KQOqfDMmtkt9QKSXVhnC3kKkgX1YXnvkztoqHAxl4=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=IaSmKE8S0aFY+jWMnrqgLlCyWRaKLY2yF47Cm1PJUp8hnLPA7lSYQlT5wnEP9/T61 h7d8MNvgkEtrs4lU7Rd72sNXoBGEKrgq7oSMberzz1ka97Q0/QG+RCJKUl8Q+pBx0B aKNFN3J87orRxREYCxmNJqF0iPBOEuELDjOtQXeM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by sourceware.org (Postfix) with ESMTPS id 278833857C76 for ; Fri, 1 Oct 2021 06:14:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 278833857C76 Received: by mail-wm1-x336.google.com with SMTP id z184-20020a1c7ec1000000b003065f0bc631so10323475wmc.0 for ; Thu, 30 Sep 2021 23:14:51 -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=W7KQOqfDMmtkt9QKSXVhnC3kKkgX1YXnvkztoqHAxl4=; b=jXWln4m8NDhTKbrg2JIvQovF5zwkNWpQGrFINUncK+1b2vgF9hkLGNORm74j2oDOA7 QTSjBM0vQ/1X4yyqVvamrzmt0h637SoPQ0LreQ9KEHksMLZIXn04fUgQAypnnLD+2nBn 6Fl/B45Z2dNfRxGgn+uLThVDpQwO0vmlEseJdfOB/Lt1Ka462mu3G9o8Esw3fcFqGUEc 9PUS/iuQm1V4jL5b6cWEmOQp6vprDpT14vc1+Zr+Sn/VyaPGWJ7CGZnQ99QIn6iEcEBl kyKWYBuHMu2uVipX83UxyrTUD4cYk6sV9tXt9i8uuk6MrZ/YU9+4glDdx9iUE377fjmF tlpQ== X-Gm-Message-State: AOAM532XxD9REpocolCGP+y8mLuqUxD2/seAnjIbU0TxtI6FIMu8kaNh Xp9cyZ0OwV3Vc0BK1bSq4o4pLwVBZFLdXw== X-Google-Smtp-Source: ABdhPJwncIAKY0CXz5kp4Ey8zbFJlT4bF3wlazyw7PTkhZeg8AVM0bXTFYQoi6+i8hSI1zpJeAwhWw== X-Received: by 2002:a1c:4d2:: with SMTP id 201mr2588647wme.156.1633068890198; Thu, 30 Sep 2021 23:14:50 -0700 (PDT) Received: from adacore.com ([2a02:2ab8:224:2ce:72b5:e8ff:feef:ee60]) by smtp.gmail.com with ESMTPSA id g25sm4764826wrc.88.2021.09.30.23.14.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 23:14:49 -0700 (PDT) Date: Fri, 1 Oct 2021 06:14:48 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Empty CUDA_Global procedures when compiling for host Message-ID: <20211001061448.GA3059463@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Ghjuvan Lacambre Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This commit makes GNAT empty CUDA_Global procedures when compiling for the host. This is required because CUDA_Global procedures could be referencing entities that only exist on the GPU, which would result in errors from the linker at link time. We empty the procedures rather than completely deleting them because we need to keep a symbol representing the procedure in order to be able to register kernels with the CUDA runtime. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * gnat_cuda.adb (Empty_CUDA_Global_Subprograms): New procedure. (Expand_CUDA_Package): Call Empty_CUDA_Global_Subprograms. diff --git a/gcc/ada/gnat_cuda.adb b/gcc/ada/gnat_cuda.adb --- a/gcc/ada/gnat_cuda.adb +++ b/gcc/ada/gnat_cuda.adb @@ -25,20 +25,22 @@ -- This package defines CUDA-specific datastructures and functions. -with Debug; use Debug; -with Elists; use Elists; -with Namet; use Namet; -with Nlists; use Nlists; -with Nmake; use Nmake; -with Rtsfind; use Rtsfind; -with Sinfo; use Sinfo; -with Sinfo.Nodes; use Sinfo.Nodes; -with Stringt; use Stringt; -with Tbuild; use Tbuild; -with Uintp; use Uintp; -with Sem; use Sem; -with Sem_Util; use Sem_Util; -with Snames; use Snames; +with Atree; use Atree; +with Debug; use Debug; +with Elists; use Elists; +with Namet; use Namet; +with Nlists; use Nlists; +with Nmake; use Nmake; +with Rtsfind; use Rtsfind; +with Sinfo; use Sinfo; +with Sinfo.Nodes; use Sinfo.Nodes; +with Stringt; use Stringt; +with Tbuild; use Tbuild; +with Uintp; use Uintp; +with Sem; use Sem; +with Sem_Aux; use Sem_Aux; +with Sem_Util; use Sem_Util; +with Snames; use Snames; with GNAT.HTable; @@ -97,6 +99,17 @@ package body GNAT_CUDA is -- * A procedure that takes care of calling CUDA functions that register -- CUDA_Global procedures with the runtime. + procedure Empty_CUDA_Global_Subprograms (Pack_Id : Entity_Id); + -- For all subprograms marked CUDA_Global in Pack_Id, remove declarations + -- and replace statements with a single null statement. + -- This is required because CUDA_Global subprograms could be referring to + -- device-only symbols, which would result in unknown symbols at link time + -- if kept around. + -- We choose to empty CUDA_Global subprograms rather than completely + -- removing them from the package because registering CUDA_Global + -- subprograms with the CUDA runtime on the host requires knowing the + -- subprogram's host-side address. + function Get_CUDA_Device_Entities (Pack_Id : Entity_Id) return Elist_Id; -- Returns an Elist of all entities marked with pragma CUDA_Device that -- are declared within package body Pack_Body. Returns No_Elist if Pack_Id @@ -153,6 +166,50 @@ package body GNAT_CUDA is Append_Elmt (Kernel, Kernels); end Add_CUDA_Kernel; + ----------------------------------- + -- Empty_CUDA_Global_Subprograms -- + ----------------------------------- + + procedure Empty_CUDA_Global_Subprograms (Pack_Id : Entity_Id) is + Spec_Id : constant Node_Id := Corresponding_Spec (Pack_Id); + Kernels : constant Elist_Id := Get_CUDA_Kernels (Spec_Id); + Kernel_Elm : Elmt_Id; + Kernel : Entity_Id; + Kernel_Body : Node_Id; + Null_Body : Entity_Id; + Loc : Source_Ptr; + begin + -- It is an error to empty CUDA_Global subprograms when not compiling + -- for the host. + pragma Assert (Debug_Flag_Underscore_C); + + if No (Kernels) then + return; + end if; + + Kernel_Elm := First_Elmt (Kernels); + while Present (Kernel_Elm) loop + Kernel := Node (Kernel_Elm); + Kernel_Body := Subprogram_Body (Kernel); + Loc := Sloc (Kernel_Body); + + Null_Body := Make_Subprogram_Body (Loc, + Specification => Subprogram_Specification (Kernel), + Declarations => New_List, + Handled_Statement_Sequence => + Make_Handled_Sequence_Of_Statements (Loc, + Statements => New_List (Make_Null_Statement (Loc)))); + + Rewrite (Kernel_Body, Null_Body); + + Next_Elmt (Kernel_Elm); + end loop; + end Empty_CUDA_Global_Subprograms; + + ------------------------- + -- Expand_CUDA_Package -- + ------------------------- + procedure Expand_CUDA_Package (N : Node_Id) is begin @@ -162,6 +219,13 @@ package body GNAT_CUDA is return; end if; + -- Remove the content (both declarations and statements) of CUDA_Global + -- procedures. This is required because CUDA_Global functions could be + -- referencing entities available only on the device, which would result + -- in unknown symbol errors at link time. + + Empty_CUDA_Global_Subprograms (N); + -- If procedures marked with CUDA_Global have been defined within N, -- we need to register them with the CUDA runtime at program startup. -- This requires multiple declarations and function calls which need