From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by sourceware.org (Postfix) with ESMTPS id E70093858D33; Sat, 17 Apr 2021 23:48:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E70093858D33 Received: by mail-wm1-x32b.google.com with SMTP id f195-20020a1c1fcc0000b029012eb88126d7so6132745wmf.3; Sat, 17 Apr 2021 16:48:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=88Is48e6ndcGsSFG2KFdA3UQbnvLq2Fhxwydk2tK13M=; b=riNLefzjI/MGmTh7qzxIDKv6c5po6x/ho1ET9NTLOXfM7Iyxs8uB8ae3Yhr3Hvv1d/ EMQe9AJ5eWSQnqARL5U9hd8QzNWKU9bB72hlzCONzrWl05/NjEnwRP/acWcy5+u5pXuF lWLhFUQLitIh15MLBSr6V2PtIS7s04jDXkB4Ueux0h8IBP7IIFSgk2cWNGTkC5WAkOIc Sb91xRiohQL/tt5cSgO2Un9pZA62G+Od866ODEkN6fwrxClEs6hU5DQIsx1E+EIWTUVx +2UTaXibhFtzuxu3LmnrlB50G+BOWNU7qaTAuuBHlXfJALMm2q6IeroF3Zl3zSJl7Uid fq5g== X-Gm-Message-State: AOAM532RMaNRMFCICZo9LCD6jm7pB4J/gV9pm1KvTrR7MOcBW/2FVVC0 KouZBnJfD/eStzOvQKMijnIq4J2e6FE= X-Google-Smtp-Source: ABdhPJxIIb5pWG1Fl9DFdbegj5UF4jqvuCg3TM7PrxZaiBamnjnF/rPi+mP2JBSXrpUjMTMu5vAwrQ== X-Received: by 2002:a05:600c:203:: with SMTP id 3mr6507086wmi.140.1618703281881; Sat, 17 Apr 2021 16:48:01 -0700 (PDT) Received: from ?IPv6:2001:8a0:7d5c:3000:5dde:5a80:434a:4f82? ([2001:8a0:7d5c:3000:5dde:5a80:434a:4f82]) by smtp.googlemail.com with ESMTPSA id o62sm32167019wmo.3.2021.04.17.16.48.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Apr 2021 16:48:01 -0700 (PDT) To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org From: =?UTF-8?Q?Jos=c3=a9_Rui_Faustino_de_Sousa?= Subject: [Patch, fortran] PR fortran/100132 - Optimization breaks pointer association Message-ID: <9e699dc6-2b0b-8cb7-c497-d31ceaed45ce@gmail.com> Date: Sat, 17 Apr 2021 23:48:00 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------C3F091833F6F8FD4BB7A523F" Content-Language: en-US X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: fortran@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Fortran mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Apr 2021 23:48:13 -0000 This is a multi-part message in MIME format. --------------C3F091833F6F8FD4BB7A523F Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi All! Proposed patch to: PR100132 - Optimization breaks pointer association. Patch tested only on x86_64-pc-linux-gnu. Correct pointer attributes when passing polymorphic pointers. Thank you very much. Best regards, José Rui Fortran: Fix function attributes [PR100132] gcc/fortran/ChangeLog: PR fortran/100132 * trans-types.c (create_fn_spec): fix function attributes when passing polymorphic pointers. gcc/testsuite/ChangeLog: PR fortran/100132 * gfortran.dg/PR100132.f90: New test. --------------C3F091833F6F8FD4BB7A523F Content-Type: text/x-patch; charset=UTF-8; name="PR100132.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="PR100132.patch" diff --git a/gcc/fortran/trans-types.c b/gcc/fortran/trans-types.c index 9f21b3ee780..6cf85dc9a41 100644 --- a/gcc/fortran/trans-types.c +++ b/gcc/fortran/trans-types.c @@ -2981,12 +2981,25 @@ create_fn_spec (gfc_symbol *sym, tree fntype) for (f = gfc_sym_get_dummy_args (sym); f; f = f->next) if (spec_len < sizeof (spec)) { - if (!f->sym || f->sym->attr.pointer || f->sym->attr.target + bool is_class = false; + bool is_pointer = false; + + if (f->sym) + { + symbol_attribute *attr = NULL; + + is_class = f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym) + && f->sym->attr.class_ok; + attr = is_class ? &(CLASS_DATA (f->sym)->attr) : &(f->sym->attr); + is_pointer = is_class ? attr->class_pointer : attr->pointer; + } + + if (f->sym == NULL || is_pointer || f->sym->attr.target || f->sym->attr.external || f->sym->attr.cray_pointer || (f->sym->ts.type == BT_DERIVED && (f->sym->ts.u.derived->attr.proc_pointer_comp || f->sym->ts.u.derived->attr.pointer_comp)) - || (f->sym->ts.type == BT_CLASS + || (is_class && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)) || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop)) diff --git a/gcc/testsuite/gfortran.dg/PR100132.f90 b/gcc/testsuite/gfortran.dg/PR100132.f90 new file mode 100644 index 00000000000..aec0fef3d26 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/PR100132.f90 @@ -0,0 +1,81 @@ +! { dg-do run } +! +! Test the fix for PR100132 +! + +module main_m + + implicit none + + private + + public :: & + foo_t + + public :: & + set, & + get + + type :: foo_t + !private + integer :: i + end type foo_t + + type(foo_t), save, pointer :: data => null() + +contains + + subroutine set(this) + class(foo_t), pointer, intent(in) :: this + + if(associated(data)) stop 1 + data => this + return + end subroutine set + + subroutine get(this) + type(foo_t), pointer, intent(out) :: this + + if(.not.associated(data)) stop 4 + this => data + nullify(data) + return + end subroutine get + +end module main_m + +program main_p + + use :: main_m, only: & + foo_t, set, get + + implicit none + + integer, parameter :: n = 1000 + + type(foo_t), pointer :: ps + type(foo_t), target :: s + integer :: i, j, yay, nay + + yay = 0 + nay = 0 + do i = 1, n + s%i = i + call set(s) + call get(ps) + if(.not.associated(ps)) stop 13 + j = ps%i + if(i/=j) stop 14 + if(i/=s%i) stop 15 + if(ps%i/=s%i) stop 16 + if(associated(ps, s))then + yay = yay + 1 + else + nay = nay + 1 + end if + end do + if((yay/=n).or.(nay/=0)) stop 17 + stop + +end program main_p + --------------C3F091833F6F8FD4BB7A523F--