Skip to content

Commit 80194f3

Browse files
committed
[Flang][OpenMP] Update declare mapper lookup via use-module
- Implemented semantic TODO to catch undeclared mappers. - Fix mapper lookup to include modules imported through USE. - Update and add tests. Fixes #163385.
1 parent 581b654 commit 80194f3

File tree

5 files changed

+59
-24
lines changed

5 files changed

+59
-24
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,10 +1402,14 @@ bool ClauseProcessor::processMap(
14021402
}
14031403
if (mappers) {
14041404
assert(mappers->size() == 1 && "more than one mapper");
1405-
mapperIdName = mappers->front().v.id().symbol->name().ToString();
1406-
if (mapperIdName != "default")
1407-
mapperIdName = converter.mangleName(
1408-
mapperIdName, mappers->front().v.id().symbol->owner());
1405+
const semantics::Symbol *mapperSym = mappers->front().v.id().symbol;
1406+
mapperIdName = mapperSym->name().ToString();
1407+
if (mapperIdName != "default") {
1408+
// Mangle with the ultimate owner so that use-associated mapper
1409+
// identifiers resolve to the same symbol as their defining scope.
1410+
const semantics::Symbol &ultimate = mapperSym->GetUltimate();
1411+
mapperIdName = converter.mangleName(mapperIdName, ultimate.owner());
1412+
}
14091413
}
14101414

14111415
processMapObjects(stmtCtx, clauseLocation,

flang/lib/Semantics/resolve-names.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,21 +1824,24 @@ bool OmpVisitor::Pre(const parser::OmpMapClause &x) {
18241824
// TODO: Do we need a specific flag or type here, to distinghuish against
18251825
// other ConstructName things? Leaving this for the full implementation
18261826
// of mapper lowering.
1827-
auto *misc{symbol->detailsIf<MiscDetails>()};
1827+
const Symbol &ultimate{symbol->GetUltimate()};
1828+
auto *misc{const_cast<Symbol &>(ultimate).detailsIf<MiscDetails>()};
18281829
if (!misc || misc->kind() != MiscDetails::Kind::ConstructName)
18291830
context().Say(mapper->v.source,
18301831
"Name '%s' should be a mapper name"_err_en_US, mapper->v.source);
18311832
else
18321833
mapper->v.symbol = symbol;
18331834
} else {
1834-
mapper->v.symbol =
1835-
&MakeSymbol(mapper->v, MiscDetails{MiscDetails::Kind::ConstructName});
1836-
// TODO: When completing the implementation, we probably want to error if
1837-
// the symbol is not declared, but right now, testing that the TODO for
1838-
// OmpMapClause happens is obscured by the TODO for declare mapper, so
1839-
// leaving this out. Remove the above line once the declare mapper is
1840-
// implemented. context().Say(mapper->v.source, "'%s' not
1841-
// declared"_err_en_US, mapper->v.source);
1835+
// Allow the special 'default' mapper identifier without prior
1836+
// declaration so lowering can recognize and handle it. Emit an
1837+
// error for any other missing mapper identifier.
1838+
if (mapper->v.source.ToString() == "default") {
1839+
mapper->v.symbol = &MakeSymbol(
1840+
mapper->v, MiscDetails{MiscDetails::Kind::ConstructName});
1841+
} else {
1842+
context().Say(
1843+
mapper->v.source, "'%s' not declared"_err_en_US, mapper->v.source);
1844+
}
18421845
}
18431846
}
18441847
return true;
@@ -3614,10 +3617,18 @@ void ModuleVisitor::Post(const parser::UseStmt &x) {
36143617
rename.u);
36153618
}
36163619
for (const auto &[name, symbol] : *useModuleScope_) {
3620+
// Default USE imports public names, excluding intrinsic-only and most
3621+
// miscellaneous details. However, allow OpenMP mapper identifiers,
3622+
// which are currently represented with MiscDetails::ConstructName.
3623+
bool isMapper{false};
3624+
if (const auto *misc{symbol->detailsIf<MiscDetails>()}) {
3625+
isMapper = misc->kind() == MiscDetails::Kind::ConstructName;
3626+
}
36173627
if (symbol->attrs().test(Attr::PUBLIC) && !IsUseRenamed(symbol->name()) &&
36183628
(!symbol->implicitAttrs().test(Attr::INTRINSIC) ||
36193629
symbol->has<UseDetails>()) &&
3620-
!symbol->has<MiscDetails>() && useNames.count(name) == 0) {
3630+
(!symbol->has<MiscDetails>() || isMapper) &&
3631+
useNames.count(name) == 0) {
36213632
SourceName location{x.moduleName.source};
36223633
if (auto *localSymbol{FindInScope(name)}) {
36233634
DoAddUse(location, localSymbol->name(), *localSymbol, *symbol);

flang/test/Lower/OpenMP/declare-mapper.f90

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-3.f90 -o - | FileCheck %t/omp-declare-mapper-3.f90
77
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-4.f90 -o - | FileCheck %t/omp-declare-mapper-4.f90
88
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-5.f90 -o - | FileCheck %t/omp-declare-mapper-5.f90
9-
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=51 %t/omp-declare-mapper-6.f90 -o - | FileCheck %t/omp-declare-mapper-6.f90
9+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-6.f90 -o - | FileCheck %t/omp-declare-mapper-6.f90
10+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-7.f90 -o - | FileCheck %t/omp-declare-mapper-7.f90
1011

1112
!--- omp-declare-mapper-1.f90
1213
subroutine declare_mapper_1
@@ -301,3 +302,25 @@ subroutine declare_mapper_nested_parent
301302
r%real_arr = r%base_arr(1) + r%inner%deep_arr(1)
302303
!$omp end target
303304
end subroutine declare_mapper_nested_parent
305+
306+
!--- omp-declare-mapper-7.f90
307+
! Check mappers declared inside modules and used via USE association.
308+
module m_mod
309+
implicit none
310+
type :: mty
311+
integer :: x
312+
end type mty
313+
!$omp declare mapper(mymap : mty :: v) map(tofrom: v%x)
314+
end module m_mod
315+
316+
program use_module_mapper
317+
use m_mod
318+
implicit none
319+
type(mty) :: a
320+
321+
! CHECK: omp.declare_mapper @[[MODMAP:[^ ]*mymap]]
322+
! CHECK: %{{.*}} = omp.map.info {{.*}} mapper(@[[MODMAP]]) {{.*}} {name = "a"}
323+
!$omp target map(mapper(mymap) : a)
324+
a%x = 42
325+
!$omp end target
326+
end program use_module_mapper

flang/test/Parser/OpenMP/map-modifiers.f90

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ subroutine f21(x, y)
320320
integer :: x(10)
321321
integer :: y
322322
integer, parameter :: p = 23
323-
!$omp target map(mapper(xx), from: x)
323+
!$omp target map(mapper(default), from: x)
324324
x = x + 1
325325
!$omp end target
326326
end
@@ -329,15 +329,15 @@ subroutine f21(x, y)
329329
!UNPARSE: INTEGER x(10_4)
330330
!UNPARSE: INTEGER y
331331
!UNPARSE: INTEGER, PARAMETER :: p = 23_4
332-
!UNPARSE: !$OMP TARGET MAP(MAPPER(XX), FROM: X)
332+
!UNPARSE: !$OMP TARGET MAP(MAPPER(DEFAULT), FROM: X)
333333
!UNPARSE: x=x+1_4
334334
!UNPARSE: !$OMP END TARGET
335335
!UNPARSE: END SUBROUTINE
336336

337337
!PARSE-TREE: OmpBeginDirective
338338
!PARSE-TREE: | OmpDirectiveName -> llvm::omp::Directive = target
339339
!PARSE-TREE: | OmpClauseList -> OmpClause -> Map -> OmpMapClause
340-
!PARSE-TREE: | | Modifier -> OmpMapper -> Name = 'xx'
340+
!PARSE-TREE: | | Modifier -> OmpMapper -> Name = 'default'
341341
!PARSE-TREE: | | Modifier -> OmpMapType -> Value = From
342342
!PARSE-TREE: | | OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'x'
343343

@@ -375,4 +375,3 @@ subroutine f22(x)
375375
!PARSE-TREE: | | SectionSubscript -> Integer -> Expr = 'i'
376376
!PARSE-TREE: | | | Designator -> DataRef -> Name = 'i'
377377
!PARSE-TREE: | bool = 'true'
378-
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
! RUN: %flang_fc1 -fdebug-dump-symbols -fopenmp -fopenmp-version=50 %s | FileCheck %s
1+
! RUN: not %flang_fc1 -fopenmp -fopenmp-version=50 %s 2>&1 | FileCheck %s
22
program main
3-
!CHECK-LABEL: MainProgram scope: MAIN
43
integer, parameter :: n = 256
54
real(8) :: a(256)
65
!$omp target map(mapper(xx), from:a)
76
do i=1,n
87
a(i) = 4.2
98
end do
109
!$omp end target
11-
!CHECK: OtherConstruct scope: size=0 alignment=1 sourceRange=74 bytes
12-
!CHECK: OtherClause scope: size=0 alignment=1 sourceRange=0 bytes
13-
!CHECK: xx: Misc ConstructName
1410
end program main
11+
12+
! CHECK: error: '{{.*}}' not declared

0 commit comments

Comments
 (0)