Skip to content

Commit 3e32b80

Browse files
[flang] Consider bind(c) when lowering calls to intrinsic module procedures (#70386)
When attempting to lower an intrinsic module procedure call, take into account bind(c). Such procedures cannot be lowered as intrinsics since their implementation is external to the module. With this solution, the hardcoded "omp_lib" string can be removed when checking if intrinsic module procedure since all procedure interfaces in that module use bind(c).
1 parent 574c5cc commit 3e32b80

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

flang/lib/Lower/ConvertCall.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,12 @@ genProcedureRef(CallContext &callContext) {
21222122
mlir::Location loc = callContext.loc;
21232123
if (auto *intrinsic = callContext.procRef.proc().GetSpecificIntrinsic())
21242124
return genIntrinsicRef(intrinsic, callContext);
2125-
if (Fortran::lower::isIntrinsicModuleProcRef(callContext.procRef))
2125+
// If it is an intrinsic module procedure reference - then treat as
2126+
// intrinsic unless it is bind(c) (since implementation is external from
2127+
// module).
2128+
if (Fortran::lower::isIntrinsicModuleProcRef(callContext.procRef) &&
2129+
!Fortran::semantics::IsBindCProcedure(
2130+
*callContext.procRef.proc().GetSymbol()))
21262131
return genIntrinsicRef(nullptr, callContext);
21272132

21282133
if (callContext.isStatementFunctionCall())
@@ -2227,8 +2232,7 @@ bool Fortran::lower::isIntrinsicModuleProcRef(
22272232
return false;
22282233
const Fortran::semantics::Symbol *module =
22292234
symbol->GetUltimate().owner().GetSymbol();
2230-
return module && module->attrs().test(Fortran::semantics::Attr::INTRINSIC) &&
2231-
module->name().ToString().find("omp_lib") == std::string::npos;
2235+
return module && module->attrs().test(Fortran::semantics::Attr::INTRINSIC);
22322236
}
22332237

22342238
std::optional<hlfir::EntityWithAttributes> Fortran::lower::convertCallToHLFIR(

flang/module/omp_lib.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
omp_atv_blocked = 17, &
9191
omp_atv_interleaved = 18
9292

93-
type :: omp_alloctrait
93+
type, bind(c) :: omp_alloctrait
9494
integer(kind=omp_alloctrait_key_kind) :: key, value
9595
end type omp_alloctrait
9696

@@ -264,23 +264,23 @@
264264
integer(kind=omp_integer_kind), intent(out) :: place_nums(*)
265265
end subroutine omp_get_partition_place_nums
266266

267-
subroutine omp_set_affinity_format(format)
267+
subroutine omp_set_affinity_format(format) bind(c)
268268
import
269269
character(len=*), intent(in) :: format
270270
end subroutine omp_set_affinity_format
271271

272-
function omp_get_affinity_format(buffer)
272+
function omp_get_affinity_format(buffer) bind(c)
273273
import
274274
character(len=*), intent(out) :: buffer
275275
integer(kind=omp_integer_kind) :: omp_get_affinity_format
276276
end function omp_get_affinity_format
277277

278-
subroutine omp_display_affinity(format)
278+
subroutine omp_display_affinity(format) bind(c)
279279
import
280280
character(len=*), intent(in) :: format
281281
end subroutine omp_display_affinity
282282

283-
function omp_capture_affinity(buffer, format)
283+
function omp_capture_affinity(buffer, format) bind(c)
284284
import
285285
character(len=*), intent(out) :: buffer
286286
character(len=*), intent(in) :: format
@@ -339,7 +339,7 @@
339339
integer(kind=omp_integer_kind) :: omp_pause_resource
340340
end function omp_pause_resource
341341

342-
function omp_pause_resource_all(kind)
342+
function omp_pause_resource_all(kind) bind(c)
343343
import
344344
integer(kind=omp_pause_resource_kind), value :: kind
345345
integer(kind=omp_integer_kind) :: omp_pause_resource_all
@@ -428,7 +428,7 @@
428428
! Device Memory Routines
429429

430430
! Memory Management Routines
431-
function omp_init_allocator(memspace, ntraits, traits)
431+
function omp_init_allocator(memspace, ntraits, traits) bind(c)
432432
import
433433
integer(kind=omp_memspace_handle_kind), value :: memspace
434434
integer, value :: ntraits
@@ -446,7 +446,7 @@
446446
integer(kind=omp_allocator_handle_kind), value :: allocator
447447
end subroutine omp_set_default_allocator
448448

449-
function omp_get_default_allocator()
449+
function omp_get_default_allocator() bind(c)
450450
import
451451
integer(kind=omp_allocator_handle_kind) :: omp_get_default_allocator
452452
end function omp_get_default_allocator
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - 2>&1 | FileCheck %s
2+
! RUN: bbc -fopenmp -emit-hlfir -o - %s 2>&1 | FileCheck %s
3+
!
4+
! Test that the calls to omp_lib's omp_get_num_threads and omp_set_num_threads
5+
! get lowered even though their implementation is not in the omp_lib module
6+
! (and this matters because this is an intrinsic module - and calls to
7+
! intrinsics are specially resolved).
8+
9+
program main
10+
use omp_lib
11+
integer(omp_integer_kind) :: num_threads
12+
integer(omp_integer_kind), parameter :: requested_num_threads = 4
13+
call omp_set_num_threads(requested_num_threads)
14+
num_threads = omp_get_num_threads()
15+
print *, num_threads
16+
end program
17+
18+
!CHECK-NOT: not yet implemented: intrinsic: omp_set_num_threads
19+
!CHECK-NOT: not yet implemented: intrinsic: omp_get_num_threads
20+
!CHECK: fir.call @omp_set_num_threads
21+
!CHECK: fir.call @omp_get_num_threads

0 commit comments

Comments
 (0)