From 37ae97a438bc7060678f3878aa6744267cfa2562 Mon Sep 17 00:00:00 2001 From: Adam Kallai Date: Fri, 21 Jul 2023 12:13:07 +0200 Subject: [PATCH] Fix the dllimport code generation for opaque type Added test cases to verify the 'dllimport' usage in LLVM IR. Fixes: #1407 --- test/llvm_ir_correct/dllimport_test.f90 | 20 +++++++++++++ .../dllimport_test.f90_mod.f90 | 28 +++++++++++++++++++ tools/flang2/flang2exe/llassem.cpp | 18 +++++++++--- 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 test/llvm_ir_correct/dllimport_test.f90 create mode 100644 test/llvm_ir_correct/dllimport_test.f90_mod.f90 diff --git a/test/llvm_ir_correct/dllimport_test.f90 b/test/llvm_ir_correct/dllimport_test.f90 new file mode 100644 index 00000000000..fca17b1ccad --- /dev/null +++ b/test/llvm_ir_correct/dllimport_test.f90 @@ -0,0 +1,20 @@ +! 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 +! +! https://github.com/flang-compiler/flang/issues/320 +! https://github.com/flang-compiler/flang/issues/1407 +! +! REQUIRES: system-windows +! +! RUN: %flang -S -emit-llvm -S -emit-llvm %s_mod.f90 %s +! RUN: cat dllimport_test.ll | FileCheck %s +! CHECK: %structdllimport_module__t_type__td_ = type opaque +! CHECK: @_dllimport_module_10_ = external dllimport global +! CHECK: @dllimport_module__t_type__td_ = external dllimport global +program h_main + use dllimport_module + implicit none + + call foobar(array) +end program \ No newline at end of file diff --git a/test/llvm_ir_correct/dllimport_test.f90_mod.f90 b/test/llvm_ir_correct/dllimport_test.f90_mod.f90 new file mode 100644 index 00000000000..ef323c5737e --- /dev/null +++ b/test/llvm_ir_correct/dllimport_test.f90_mod.f90 @@ -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 +! +! REQUIRES: system-windows +! RUN: true + +module dllimport_module + implicit none + + type t_type + private + integer :: a, b + end type + + type(t_type), parameter :: array(2) = (/t_type(1, 1), t_type(1, 0)/) + + interface foobar + module procedure test + end interface + + contains + subroutine test(a) + type(t_type), dimension(:) :: a + return + end subroutine +end module \ No newline at end of file diff --git a/tools/flang2/flang2exe/llassem.cpp b/tools/flang2/flang2exe/llassem.cpp index fc8477215e5..22fa8552b34 100644 --- a/tools/flang2/flang2exe/llassem.cpp +++ b/tools/flang2/flang2exe/llassem.cpp @@ -1202,8 +1202,13 @@ assemble_end(void) for (gblsym = ag_global; gblsym; gblsym = AG_SYMLK(gblsym)) { if (AG_TYPEDESC(gblsym) && !AG_DEFD(gblsym)) { fprintf(ASMFIL, "%%%s = type opaque\n", AG_TYPENAME(gblsym)); - fprintf(ASMFIL, "@%s = external global %%%s\n", AG_NAME(gblsym), - AG_TYPENAME(gblsym)); + if (strstr(cpu_llvm_module->target_triple, "windows-msvc") != NULL) { + fprintf(ASMFIL, "@%s = external dllimport global %%%s\n", AG_NAME(gblsym), + AG_TYPENAME(gblsym)); + } else { + fprintf(ASMFIL, "@%s = external global %%%s\n", AG_NAME(gblsym), + AG_TYPENAME(gblsym)); + } } } for (gblsym = ag_typedef; gblsym; gblsym = AG_SYMLK(gblsym)) { @@ -1212,8 +1217,13 @@ assemble_end(void) AG_TYPENAME(gblsym)); else if (AG_TYPEDESC(gblsym) && !AG_DEFD(gblsym)) { fprintf(ASMFIL, "%%%s = type opaque\n", AG_TYPENAME(gblsym)); - fprintf(ASMFIL, "@%s = external global %%%s\n", AG_NAME(gblsym), - AG_TYPENAME(gblsym)); + if (strstr(cpu_llvm_module->target_triple, "windows-msvc") != NULL) { + fprintf(ASMFIL, "@%s = external dllimport global %%%s\n", AG_NAME(gblsym), + AG_TYPENAME(gblsym)); + } else { + fprintf(ASMFIL, "@%s = external global %%%s\n", AG_NAME(gblsym), + AG_TYPENAME(gblsym)); + } } } for (gblsym = ag_other; gblsym; gblsym = AG_SYMLK(gblsym)) {