From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 11B25383E068; Wed, 31 Aug 2022 12:41:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 11B25383E068 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661949719; bh=g3bV6LFVeSWpLQ6XF2Lj7Mxg0E6boxcSNGDSrffZJCg=; h=From:To:Subject:Date:From; b=ejE2sWEAcu218VnPJZghfxzYlF1ui8UWWwI8YpQ8RaqE4VkrbN7z79ukV79D56ib2 4CrHRwP3asnWkj1L+/UFfghHko9xq6cFQV9WL7fXmHNulhH+1fDL6E5ZdaEkovBWGq gtJB/ev7R10g1ctSQqTXpNSRxQyeYfpQyqm01EvM= From: "federico.perini at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug fortran/106790] New: Weird SIGSEV using polymorphic routine with "select type" and optimization (-O3) Date: Wed, 31 Aug 2022 12:41:58 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: federico.perini at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106790 Bug ID: 106790 Summary: Weird SIGSEV using polymorphic routine with "select type" and optimization (-O3) Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: federico.perini at gmail dot com Target Milestone: --- I'm getting a weird SIGSEV error when running the following code with full optimization (-O3). Error happens if:=20 - type guard ("select type") is is "type is", no segfault if "class is" - the non-abstract derived type contains more than 1 scalar I suspect something is wrong with the compiler trying to optimize out the c= ode in the main program (it's all constants).=20 - Fails only on gfortran 11, 12.=20 - Works on gfortran 7,8,9,10 Test it here: https://godbolt.org/z/qKPMvKnzf Here is the minimum working example:=20 module ttt implicit none type, abstract :: t0 contains procedure(merge), deferred :: mergeWith procedure(pprint), deferred :: print end type type,extends(t0) :: t integer :: from1,to1,face contains=20 procedure :: mergeWith =3D> t_merge procedure :: print =3D> link_print end type t abstract interface pure subroutine merge(this,that) import t0 class(t0), intent(inout) :: this class(t0), intent(in) :: that end subroutine merge function pprint(this) result(msg) import t0 class(t0), intent(in) :: this character(len=3D:), allocatable :: msg end function pprint end interface contains function link_print(this) result(msg) class(t), intent(in) :: this character(len=3D:), allocatable :: msg character(len=3D1024) :: buffer integer :: lt write(buffer,1) this%to1,this%from1,this%face lt =3D len_trim(buffer) allocate(character(len=3Dlt) :: msg) if (lt>0) msg(1:lt) =3D buffer(1:lt) 1 format('to1=3D',i0,' from1=3D',i0,' face=3D',i0) end function link_print pure subroutine t_merge(this,that) class(t), intent(inout) :: this class(t0), intent(in) :: that select type (ttype =3D> that) type is (t) ! Does not SIGSEV if using "class is (t)" ! SIGSEV at any of the following lines ! Does not crash anymore if commenting any of them if (this%to1<0 .and. this%from1<0) then this%to1 =3D ttype%to1 this%face =3D ttype%face end if end select end subroutine t_merge end module program test_t use ttt implicit none type(t) :: t1,t2 t1 =3D t(from1=3D123,to1=3D435,face=3D789) t2 =3D t(from1=3D-1,to1=3D-1,face=3D-1) call t1%mergeWith(t2) print *, 't1=3D',t1%print(),' t2=3D',t2%print() t1 =3D t(from1=3D123,to1=3D435,face=3D789) t2 =3D t(from1=3D-1,to1=3D-1,face=3D-1) ! Crash here on -O3, apparently during the unrolling of constants call t2%mergeWith(t1) print *, 't1=3D',t1%print(),' t2=3D',t2%print() end program Thanks, Federico=