Skip to content

Commit

Permalink
[flang2] Fix bug in implied DO expression in a used module
Browse files Browse the repository at this point in the history
The variables in a common block from a module will be copied into
current scope when a USE exists. However, the dinit feature of those
copied variables was removed when lowering symbols in flang1. This
patch suppresses the transformation of those copied variables to the
corresponding constant initialized in the common block, since we cannot
get their initialization info anyway.
  • Loading branch information
zhangruinan authored and bryanpkc committed Jun 12, 2024
1 parent 39997ea commit 3de1cba
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
23 changes: 23 additions & 0 deletions test/f90_correct/inc/test-implied-do-in-module.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

$(TEST): run


build: $(SRC)/$(TEST).f90
-$(RM) $(TEST).$(EXESUFFIX) core *.d *.mod FOR*.DAT FTN* ftn* fort.*
@echo ------------------------------------ building test $@
-$(CC) -c $(CFLAGS) $(SRC)/check.c -o check.$(OBJX)
-$(FC) -c $(FFLAGS) $(LDFLAGS) $(SRC)/$(TEST).f90 -o $(TEST).$(OBJX)
-$(FC) $(FFLAGS) $(LDFLAGS) $(TEST).$(OBJX) check.$(OBJX) $(LIBS) -o $(TEST).$(EXESUFFIX)


run:
@echo ------------------------------------ executing test $(TEST)
$(TEST).$(EXESUFFIX)

verify: ;

9 changes: 9 additions & 0 deletions test/f90_correct/lit/test-implied-do-in-module.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Shared lit script for each tests. Run bash commands that run tests with make.

# RUN: KEEP_FILES=%keep FLAGS=%flags TEST_SRC=%s MAKE_FILE_DIR=%S/.. bash %S/runmake | tee %t
# RUN: cat %t | FileCheck %S/runmake
21 changes: 21 additions & 0 deletions test/f90_correct/src/test-implied-do-in-module.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
! Part of the LLVM Project, under the Apache License v2.0 with LLVM
! Exceptions.
! See https://llvm.org/LICENSE.txt for license information.
! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
!
! Test the implied DO expression in module.
!

module mod_impliedDo
integer :: i
character(*), parameter :: a = "Hello world!"
character(*), parameter :: b1(2) = [(a(i:i+4),i=1,7,6)]
end module

program p
use mod_impliedDo
character(5) :: c(2)
c(1) = b1(1)
c(2) = b1(2)
call check(c, "Helloworld", 1)
end program
12 changes: 10 additions & 2 deletions tools/flang2/flang2exe/dinit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5824,8 +5824,7 @@ eval_init_expr_item(CONST *cur_e)
}
if (PARAMG(cur_e->sptr) || (DOVARG(cur_e->sptr) && DINITG(cur_e->sptr)) ||
(CCSYMG(cur_e->sptr) && DINITG(cur_e->sptr))) {
if (!PARAMVALG(cur_e->sptr) && DTY(DTYPEG(cur_e->sptr)) == TY_CHAR
&& SCG(cur_e->sptr) == SC_STATIC) {
if (!PARAMVALG(cur_e->sptr) && DTY(DTYPEG(cur_e->sptr)) == TY_CHAR) {
new_e = get_static_str(cur_e->sptr);
break;
}
Expand All @@ -5834,6 +5833,15 @@ eval_init_expr_item(CONST *cur_e)
if (cur_e->mbr) {
new_e->sptr = cur_e->mbr;
}
break;
}
if (SCG(cur_e->sptr) == SC_CMBLK &&
FROMMODG(MIDNUMG(cur_e->sptr)) && MODCMNG(MIDNUMG(cur_e->sptr))) {
/* The dinit flag will be removed when lowering if the variable is from an
* external module. The branch deals with those variables from module
* commons to directly return the original variable.
*/
new_e = clone_init_const(cur_e, true);
}
break;
case AC_CONST:
Expand Down

0 comments on commit 3de1cba

Please sign in to comment.