From 4dd0f7975f03fdac1fda87fffac46760d45ea670 Mon Sep 17 00:00:00 2001 From: xieyihui Date: Mon, 11 Sep 2023 11:52:50 +0800 Subject: [PATCH] Add patch for fix sizeof for data with Bind(C) bug --- ...ug-about-sizeof-for-data-with-Bind-C.patch | 167 ++++++++++++++++++ flang.spec | 6 +- 2 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 23-fix-bug-about-sizeof-for-data-with-Bind-C.patch diff --git a/23-fix-bug-about-sizeof-for-data-with-Bind-C.patch b/23-fix-bug-about-sizeof-for-data-with-Bind-C.patch new file mode 100644 index 0000000..bf1486f --- /dev/null +++ b/23-fix-bug-about-sizeof-for-data-with-Bind-C.patch @@ -0,0 +1,167 @@ +From 8c7c92dd89826946443deeae34bd371ec930e924 Mon Sep 17 00:00:00 2001 +From: xieyihui +Date: Mon, 11 Sep 2023 11:45:05 +0800 +Subject: [PATCH] Fix bug about sizeof for data with Bind(C) + +diff --git a/test/f90_correct/inc/sizeof_for_cdata.mk b/test/f90_correct/inc/sizeof_for_cdata.mk +new file mode 100644 +index 00000000..fa97d0c6 +--- /dev/null ++++ b/test/f90_correct/inc/sizeof_for_cdata.mk +@@ -0,0 +1,28 @@ ++# ++# 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 ++# ++ ++########## Make rule for test sizeof_for_cdata ######## ++ ++ ++sizeof_for_cdata: run ++ ++ ++build: $(SRC)/sizeof_for_cdata.f90 ++ -$(RM) sizeof_for_cdata.$(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)/sizeof_for_cdata.f90 -o sizeof_for_cdata.$(OBJX) ++ -$(FC) $(FFLAGS) $(LDFLAGS) sizeof_for_cdata.$(OBJX) check.$(OBJX) $(LIBS) -o sizeof_for_cdata.$(EXESUFFIX) ++ ++ ++run: ++ @echo ------------------------------------ executing test sizeof_for_cdata ++ sizeof_for_cdata.$(EXESUFFIX) ++ ++verify: ; ++ ++sizeof_for_cdata.run: run ++ +diff --git a/test/f90_correct/lit/sizeof_for_cdata.sh b/test/f90_correct/lit/sizeof_for_cdata.sh +new file mode 100644 +index 00000000..33c953a1 +--- /dev/null ++++ b/test/f90_correct/lit/sizeof_for_cdata.sh +@@ -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 +diff --git a/test/f90_correct/src/sizeof_for_cdata.f90 b/test/f90_correct/src/sizeof_for_cdata.f90 +new file mode 100644 +index 00000000..93ea734e +--- /dev/null ++++ b/test/f90_correct/src/sizeof_for_cdata.f90 +@@ -0,0 +1,33 @@ ++ ++module test ++ use ISO_C_BINDING ++ type, bind(C) :: addrinfo ++ integer(KIND=C_INT) :: flag1, flag2, flag3, flag4 ++ integer(KIND=C_SIZE_T) :: addrlen ++ type(C_PTR) :: addr, name, next ++ end type addrinfo ++ ++ interface ++ type(C_PTR) function memset(s, c, n) bind(C, name="memset") ++ use ISO_C_BINDING ++ type(C_PTR), intent(IN), value :: s ++ integer(KIND=C_INT), intent(IN), value :: c ++ integer(KIND=C_SIZE_T), intent(IN), value :: n ++ end function memset ++ end interface ++ ++ contains ++ subroutine test1 ++ implicit none ++ type(addrinfo), target :: hints ++ type(C_PTR) :: ptr ++ INTEGER(KIND=4) :: my_integer ++ ptr = memset(c_loc(hints), my_integer, SIZEOF(hints)) ++ end subroutine ++end module ++program main ++ use test ++ implicit none ++ call test1() ++ print *, "PASS" ++end program main +\ No newline at end of file +diff --git a/tools/flang1/flang1exe/semant.c b/tools/flang1/flang1exe/semant.c +index b2c8d2ae..6b20ca78 100644 +--- a/tools/flang1/flang1exe/semant.c ++++ b/tools/flang1/flang1exe/semant.c +@@ -121,6 +121,7 @@ static LOGICAL is_entry; + static LOGICAL is_exe_stmt; + static LOGICAL entry_seen; + static LOGICAL seen_options; ++static LOGICAL is_cfunc; + static struct { + int kind; + INT len; +@@ -1283,6 +1284,7 @@ semant1(int rednum, SST *top) + sem.pgphase = PHASE_SPEC; + } + sem.gdtype = -1; ++ is_cfunc = FALSE; + lenspec[0].kind = 0; + goto statement_shared; + /* +@@ -4842,6 +4844,10 @@ semant1(int rednum, SST *top) + */ + sem.stag_dtype = DTYPEG(sptr); + sem.gdtype = sem.ogdtype = sem.stag_dtype; ++ if(CFUNCG(sptr)) ++ is_cfunc = TRUE; ++ else ++ is_cfunc = FALSE; + defer_put_kind_type_param(0, 0, NULL, sem.stag_dtype, 0, 2); + } else { + sem.stag_dtype = DTYPEG(sptr); +@@ -9654,8 +9660,11 @@ semant1(int rednum, SST *top) + STYPEP(sptr, ST_IDENT); /* stype will be filled in later*/ + /* ...else stype will be set by the actions for */ + +- if (!dtypeset) ++ if (!dtypeset) { + dtype = sem.gdtype; ++ if (is_cfunc) ++ CFUNCP(sptr, 1); ++ } + if (dtype > 0) + DTYPEP(sptr, dtype); + if (stype == ST_ARRAY) { +diff --git a/tools/flang1/flang1exe/semfunc.c b/tools/flang1/flang1exe/semfunc.c +index 1750ddfd..9a58ef6c 100644 +--- a/tools/flang1/flang1exe/semfunc.c ++++ b/tools/flang1/flang1exe/semfunc.c +@@ -10798,14 +10798,14 @@ ref_pd(SST *stktop, ITEM *list) + (void)mkarg(ARG_STK(0), &dum); + XFR_ARGAST(0); + ast = ARG_AST(0); ++ sptr = 0; ++ if (A_TYPEG(ast) == A_MEM) { ++ sptr = A_SPTRG(A_MEMG(ast)); ++ } else if (A_TYPEG(ast) == A_ID) { ++ sptr = A_SPTRG(ast); ++ } + +- if (pdtype == PD_c_sizeof) { +- sptr = 0; +- if (A_TYPEG(ast) == A_MEM) { +- sptr = A_SPTRG(A_MEMG(ast)); +- } else if (A_TYPEG(ast) == A_ID) { +- sptr = A_SPTRG(ast); +- } ++ if (pdtype == PD_c_sizeof || CFUNCG(sptr)) { + if (sptr) { + if (POINTERG(sptr) || ALLOCG(sptr) || CLASSG(sptr) || ASSUMSHPG(sptr) || + ASUMSZG(sptr) || +-- +2.25.1 + diff --git a/flang.spec b/flang.spec index d8f08d3..1c25f91 100644 --- a/flang.spec +++ b/flang.spec @@ -2,7 +2,7 @@ Name: flang Version: flang_20210324 -Release: 26 +Release: 27 Summary: Fortran language compiler targeting LLVM License: Apache-2.0 @@ -33,6 +33,7 @@ Patch18: 19-add-test-cases-for-types-3.patch Patch19: 20-add-align-prgma-for-derived-type.patch Patch20: 21-add-align-prgma-for-fix-shape-array-character-type.patch Patch21: 22-fix-import-use.patch +Patch22: 23-fix-bug-about-sizeof-for-data-with-Bind-C.patch %description Flang depends on a fork of the LLVM project (https://github.com/flang-compiler/classic-flang-llvm-project). The fork made some changes to the upstream LLVM project to support Flang toolchain. Flang cannot build independently for now. @@ -54,6 +55,9 @@ TODO: support build Flang. %changelog +* Mon Sep 11 2023 xieyihui - flang_20210324-27 +- Add patch for fix sizeof for data with Bind(C) bug + * Fri July 7 2023 zhushengtao1 - flang_20210324-26 - Add patch for fix import use bug -- Gitee