[flang-commits] [flang] [flang] Put ISO_Fortran_binding.h where it can be easily used (PR #68756)

Pete Steinfeld via flang-commits flang-commits at lists.llvm.org
Wed Oct 11 17:05:34 PDT 2023


https://github.com/psteinfeld updated https://github.com/llvm/llvm-project/pull/68756

>From 7ddf2600229d5e51923a5c119d76107675ce14b8 Mon Sep 17 00:00:00 2001
From: Peter Steinfeld <psteinfeld at nvidia.com>
Date: Tue, 10 Oct 2023 16:45:22 -0700
Subject: [PATCH 1/3] [flang] Put ISO_Fortran_binding.h where it can be easily
 used

The update stems from the discussion in
https://discourse.llvm.org/t/adding-flang-specific-header-files-to-clang/72442

I decided to put ISO_Fortran_binding.h in a place where it would be
accessible with the include: "#include<ISO_Fortran_binding.h>" rather
than "#include<fortran/ISO_Fortran_binding.h>" because this is what
gfortran does.

The changes to put the header file into the build and install areas are
in .../flang/CMakeLists.txt.  This code  calls "get_clang_resource_dir",
which requires "PACKAGE_VERSION" to be defined, so I added code to
define it.

Note that the file is also installed into ".../include/flang", so if a
user wanted to access the file from a compiler other than clang, it
would be available.

I added a test in ".../flang/test/Examples".  Since the flang project
depends on clang, clang will always be available in a flang build.  To
make the test work, I also needed to put ISO_Fortran_binding.h into the
build area.
---
 flang/CMakeLists.txt               | 20 ++++++++--
 flang/test/CMakeLists.txt          |  1 +
 flang/test/Examples/ctofortran.f90 | 59 ++++++++++++++++++++++++++++++
 flang/test/lit.cfg.py              |  1 +
 4 files changed, 78 insertions(+), 3 deletions(-)
 create mode 100644 flang/test/Examples/ctofortran.f90

diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index ac30da89995ed31..74245374166ba9c 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -273,10 +273,10 @@ if (NOT(FLANG_DEFAULT_RTLIB STREQUAL ""))
       "Default runtime library to use (empty for platform default)" FORCE)
 endif()
 
-
-
 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
-
+if (NOT PACKAGE_VERSION)
+  set(PACKAGE_VERSION ${LLVM_VERSION_MAJOR})
+endif()
 
 if (NOT DEFINED FLANG_VERSION_MAJOR)
   set(FLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
@@ -490,3 +490,17 @@ if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
     PATTERN "*.inc"
     )
 endif()
+
+# Put ISO_Fortran_binding.h into the include files of the build area now
+# so that we can run tests before installing
+include(GetClangResourceDir)
+get_clang_resource_dir(HEADER_BINARY_DIR PREFIX ${LLVM_LIBRARY_OUTPUT_INTDIR}/.. SUBDIR include)
+configure_file(
+  ${FLANG_SOURCE_DIR}/include/flang/ISO_Fortran_binding.h
+  ${HEADER_BINARY_DIR}/ISO_Fortran_binding.h)
+
+# And also install it into the install area
+get_clang_resource_dir(HEADER_INSTALL_DIR PREFIX ${CMAKE_INSTALL_PREFIX} SUBDIR include)
+install(
+  FILES ${CMAKE_INSTALL_PREFIX}/include/flang/ISO_Fortran_binding.h 
+  DESTINATION ${HEADER_INSTALL_DIR})
diff --git a/flang/test/CMakeLists.txt b/flang/test/CMakeLists.txt
index 7d96a72e5f36d6d..1d18f6c697dc34a 100644
--- a/flang/test/CMakeLists.txt
+++ b/flang/test/CMakeLists.txt
@@ -64,6 +64,7 @@ set(FLANG_TEST_DEPENDS
   FortranRuntime
   Fortran_main
   FortranDecimal
+  clang
 )
 if (LLVM_ENABLE_PLUGINS AND NOT WIN32)
   list(APPEND FLANG_TEST_DEPENDS Bye)
diff --git a/flang/test/Examples/ctofortran.f90 b/flang/test/Examples/ctofortran.f90
new file mode 100644
index 000000000000000..aecc6b19b3597c0
--- /dev/null
+++ b/flang/test/Examples/ctofortran.f90
@@ -0,0 +1,59 @@
+! UNSUPPORTED: system-windows
+! RUN: split-file %s %t
+! RUN: %clang -c %t/cfile.c
+! RUN: %flang -flang-experimental-hlfir cfile.o %t/src.f90
+! RUN: a.out | FileCheck %s
+
+!--- src.f90
+subroutine foo(a) bind(c)
+  integer :: a(:)
+  if (lbound(a, 1) .ne. 1) then
+     print *, 'FAIL expected 1 for lbound but got ',lbound(a, 1)
+     stop 1
+  endif
+  
+  if (ubound(a, 1) .ne. 10) then
+     print *, 'FAIL expected 10 for ubound but got ',ubound(a, 1)
+     stop 1
+  endif
+  
+  do i = lbound(a,1),ubound(a,1)
+     !print *, a(i)
+     if (a(i) .ne. i) then
+        print *, 'FAIL expected', i, ' for index ',i, ' but got ',a(i)
+        stop 1
+     endif
+  enddo
+  print *, 'PASS'
+end subroutine foo
+
+! CHECK: PASS
+!--- cfile.c
+#include <stdio.h>
+#include <stdlib.h>
+#include <ISO_Fortran_binding.h>
+
+void foo(CFI_cdesc_t*);
+
+int a[10];
+
+int main() {
+  int i, res;
+  static CFI_CDESC_T(1) r1;
+  CFI_cdesc_t *desc = (CFI_cdesc_t*)&r1;
+  CFI_index_t extent[1] = {10};
+
+  for(i=0; i<10; ++i) {
+    a[i] = i+1;
+  }
+
+  res = CFI_establish(desc, (void*)a, CFI_attribute_other, CFI_type_int32_t, 
+                      sizeof(int), 1, extent);
+  if (res != 0) {
+    printf("FAIL CFI_establish returned %d instead of 0.\n",res);
+    exit(1);
+  }
+
+  foo(desc);
+  return 0;
+}
diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py
index dda8ed456c986a5..1ac8ac09a6f58a9 100644
--- a/flang/test/lit.cfg.py
+++ b/flang/test/lit.cfg.py
@@ -121,6 +121,7 @@
 # For each occurrence of a flang tool name, replace it with the full path to
 # the build directory holding that tool.
 tools = [
+    ToolSubst("%clang", command=FindTool("clang"), unresolved="fatal"),
     ToolSubst("%flang", command=FindTool("flang-new"), unresolved="fatal"),
     ToolSubst(
         "%flang_fc1",

>From 96748c9af22a1dadd27e708d8b9a2600aa90a044 Mon Sep 17 00:00:00 2001
From: Peter Steinfeld <psteinfeld at nvidia.com>
Date: Wed, 11 Oct 2023 16:58:10 -0700
Subject: [PATCH 2/3] Responding to Slava's suggestion.

---
 flang/test/Examples/ctofortran.f90 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/flang/test/Examples/ctofortran.f90 b/flang/test/Examples/ctofortran.f90
index aecc6b19b3597c0..2b840bcd4d1ea2d 100644
--- a/flang/test/Examples/ctofortran.f90
+++ b/flang/test/Examples/ctofortran.f90
@@ -1,8 +1,8 @@
 ! UNSUPPORTED: system-windows
 ! RUN: split-file %s %t
 ! RUN: %clang -c %t/cfile.c
-! RUN: %flang -flang-experimental-hlfir cfile.o %t/src.f90
-! RUN: a.out | FileCheck %s
+! RUN: %flang -flang-experimental-hlfir cfile.o %t/src.f90 -o ctofortran
+! RUN: ./ctofortran | FileCheck %s
 
 !--- src.f90
 subroutine foo(a) bind(c)

>From a4bafb55960dbe27c5c5ce9cb75b6d992f0234d5 Mon Sep 17 00:00:00 2001
From: Peter Steinfeld <psteinfeld at nvidia.com>
Date: Wed, 11 Oct 2023 17:00:39 -0700
Subject: [PATCH 3/3] Fixing up my previous change.

---
 flang/test/Examples/ctofortran.f90 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/flang/test/Examples/ctofortran.f90 b/flang/test/Examples/ctofortran.f90
index 2b840bcd4d1ea2d..b1ca8d363302d36 100644
--- a/flang/test/Examples/ctofortran.f90
+++ b/flang/test/Examples/ctofortran.f90
@@ -1,8 +1,8 @@
 ! UNSUPPORTED: system-windows
 ! RUN: split-file %s %t
 ! RUN: %clang -c %t/cfile.c
-! RUN: %flang -flang-experimental-hlfir cfile.o %t/src.f90 -o ctofortran
-! RUN: ./ctofortran | FileCheck %s
+! RUN: %flang -flang-experimental-hlfir cfile.o %t/src.f90 -o %t/ctofortran
+! RUN: %t/ctofortran | FileCheck %s
 
 !--- src.f90
 subroutine foo(a) bind(c)



More information about the flang-commits mailing list