Skip to content

Commit 429380d

Browse files
committed
[NFC][flang][OpenMP] Create FortranUtils lib and move createMapInfoOp to it (llvm#154483)
Applies upstream PR llvm#154483. This moves `createMapInfoOp` from `flang/lib/Lower/OpenMP/Utils.h` to new utils library. Context: I am working on upstreaming (from AMD's downstream fork) support for `do concurrent` mapping to the GPU. This means that `DoConcurrentConversion.cpp` needs access to `createMapInfoOp`. Hence, we moved it downstream to a shared location between that is linked by both `FlangOpenMPTransforms` (where the `do concurrent` pass lives) and `FortranLower` (where `createMapInfoOp` originally were). Note that, so far, upstream `DoConcurrentConversion.cpp` does not reference `createMapInfoOp` yet. Follow-up PRs will upstream this later.
1 parent a41b546 commit 429380d

File tree

9 files changed

+111
-6
lines changed

9 files changed

+111
-6
lines changed

flang/include/flang/Utils/OpenMP.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- include/flang/Utils/OpenMP.h ----------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef FORTRAN_UTILS_OPENMP_H_
10+
#define FORTRAN_UTILS_OPENMP_H_
11+
12+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
13+
14+
namespace Fortran::utils::openmp {
15+
// TODO We can probably move the stuff inside `Support/OpenMP-utils.h/.cpp` here
16+
// as well.
17+
18+
/// Create an `omp.map.info` op. Parameters other than the ones documented below
19+
/// correspond to operation arguments in the OpenMPOps.td file, see op docs for
20+
/// more details.
21+
///
22+
/// \param [in] builder - MLIR operation builder.
23+
/// \param [in] loc - Source location of the created op.
24+
mlir::omp::MapInfoOp createMapInfoOp(mlir::OpBuilder &builder,
25+
mlir::Location loc, mlir::Value baseAddr, mlir::Value varPtrPtr,
26+
llvm::StringRef name, llvm::ArrayRef<mlir::Value> bounds,
27+
llvm::ArrayRef<mlir::Value> members, mlir::ArrayAttr membersIndex,
28+
uint64_t mapType, mlir::omp::VariableCaptureKind mapCaptureType,
29+
mlir::Type retTy, bool partialMap = false,
30+
mlir::FlatSymbolRefAttr mapperId = mlir::FlatSymbolRefAttr());
31+
} // namespace Fortran::utils::openmp
32+
33+
#endif // FORTRAN_UTILS_OPENMP_H_

flang/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_subdirectory(Semantics)
66
add_subdirectory(Support)
77
add_subdirectory(Frontend)
88
add_subdirectory(FrontendTool)
9+
add_subdirectory(Utils)
910

1011
add_subdirectory(Optimizer)
1112

flang/lib/Lower/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ add_flang_library(FortranLower
6060
FortranParser
6161
FortranEvaluate
6262
FortranSemantics
63+
FortranUtils
6364

6465
LINK_COMPONENTS
6566
Support

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "flang/Lower/Support/ReductionProcessor.h"
2020
#include "flang/Parser/tools.h"
2121
#include "flang/Semantics/tools.h"
22-
#include "flang/Support/OpenMP-utils.h"
22+
#include "flang/Utils/OpenMP.h"
2323
#include "llvm/Frontend/OpenMP/OMP.h.inc"
2424
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
2525

@@ -1248,7 +1248,7 @@ void ClauseProcessor::processMapObjects(
12481248
auto location = mlir::NameLoc::get(
12491249
mlir::StringAttr::get(firOpBuilder.getContext(), asFortran.str()),
12501250
baseOp.getLoc());
1251-
mlir::omp::MapInfoOp mapOp = Fortran::common::openmp::createMapInfoOp(
1251+
mlir::omp::MapInfoOp mapOp = utils::openmp::createMapInfoOp(
12521252
firOpBuilder, location, baseOp,
12531253
/*varPtrPtr=*/mlir::Value{}, asFortran.str(), bounds,
12541254
/*members=*/{}, /*membersIndex=*/mlir::ArrayAttr{},

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
#include "ClauseProcessor.h"
1717
#include "DataSharingProcessor.h"
1818
#include "Decomposer.h"
19-
#include "flang/Lower/OpenMP/Utils.h"
2019
#include "flang/Common/idioms.h"
2120
#include "flang/Lower/Bridge.h"
2221
#include "flang/Lower/ConvertExpr.h"
2322
#include "flang/Lower/ConvertVariable.h"
2423
#include "flang/Lower/DirectivesCommon.h"
2524
#include "flang/Lower/OpenMP/Clauses.h"
25+
#include "flang/Lower/OpenMP/Utils.h"
2626
#include "flang/Lower/StatementContext.h"
2727
#include "flang/Lower/SymbolMap.h"
2828
#include "flang/Optimizer/Builder/BoxValue.h"
@@ -38,6 +38,7 @@
3838
#include "flang/Semantics/tools.h"
3939
#include "flang/Support/Flags.h"
4040
#include "flang/Support/OpenMP-utils.h"
41+
#include "flang/Utils/OpenMP.h"
4142
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
4243
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
4344
#include "mlir/Support/StateStack.h"
@@ -47,6 +48,7 @@
4748

4849
using namespace Fortran::lower::omp;
4950
using namespace Fortran::common::openmp;
51+
using namespace Fortran::utils::openmp;
5052

5153
//===----------------------------------------------------------------------===//
5254
// Code generation helper functions

flang/lib/Lower/OpenMP/Utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <flang/Parser/parse-tree.h>
2929
#include <flang/Parser/tools.h>
3030
#include <flang/Semantics/tools.h>
31-
#include <flang/Support/OpenMP-utils.h>
31+
#include <flang/Utils/OpenMP.h>
3232
#include <llvm/Support/CommandLine.h>
3333
#include <mlir/Analysis/TopologicalSortUtils.h>
3434
#include <mlir/Dialect/Arith/IR/Arith.h>
@@ -405,7 +405,7 @@ mlir::Value createParentSymAndGenIntermediateMaps(
405405

406406
// Create a map for the intermediate member and insert it and it's
407407
// indices into the parentMemberIndices list to track it.
408-
mlir::omp::MapInfoOp mapOp = Fortran::common::openmp::createMapInfoOp(
408+
mlir::omp::MapInfoOp mapOp = utils::openmp::createMapInfoOp(
409409
firOpBuilder, clauseLocation, curValue,
410410
/*varPtrPtr=*/mlir::Value{}, asFortran,
411411
/*bounds=*/interimBounds,
@@ -564,7 +564,7 @@ void insertChildMapInfoIntoParent(
564564
converter.getCurrentLocation(), asFortran, bounds,
565565
treatIndexAsSection);
566566

567-
mlir::omp::MapInfoOp mapOp = Fortran::common::openmp::createMapInfoOp(
567+
mlir::omp::MapInfoOp mapOp = utils::openmp::createMapInfoOp(
568568
firOpBuilder, info.rawInput.getLoc(), info.rawInput,
569569
/*varPtrPtr=*/mlir::Value(), asFortran.str(), bounds, members,
570570
firOpBuilder.create2DI64ArrayAttr(

flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class MapsForPrivatizedSymbolsPass
5555
: public flangomp::impl::MapsForPrivatizedSymbolsPassBase<
5656
MapsForPrivatizedSymbolsPass> {
5757

58+
// TODO Use `createMapInfoOp` from `flang/Utils/OpenMP.h`.
5859
omp::MapInfoOp createMapInfo(Location loc, Value var,
5960
fir::FirOpBuilder &builder) {
6061
// Check if a value of type `type` can be passed to the kernel by value.

flang/lib/Utils/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#===-- lib/Utils/CMakeLists.txt --------------------------------------------===#
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
#===------------------------------------------------------------------------===#
8+
9+
add_flang_library(FortranUtils
10+
OpenMP.cpp
11+
12+
DEPENDS
13+
FIRDialect
14+
15+
LINK_LIBS
16+
FIRDialect
17+
18+
MLIR_LIBS
19+
MLIROpenMPDialect
20+
)

flang/lib/Utils/OpenMP.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===-- lib/Utisl/OpenMP.cpp ------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "flang/Utils/OpenMP.h"
10+
11+
#include "flang/Optimizer/Dialect/FIROps.h"
12+
#include "flang/Optimizer/Dialect/FIRType.h"
13+
14+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
15+
16+
namespace Fortran::utils::openmp {
17+
mlir::omp::MapInfoOp createMapInfoOp(mlir::OpBuilder &builder,
18+
mlir::Location loc, mlir::Value baseAddr, mlir::Value varPtrPtr,
19+
llvm::StringRef name, llvm::ArrayRef<mlir::Value> bounds,
20+
llvm::ArrayRef<mlir::Value> members, mlir::ArrayAttr membersIndex,
21+
uint64_t mapType, mlir::omp::VariableCaptureKind mapCaptureType,
22+
mlir::Type retTy, bool partialMap, mlir::FlatSymbolRefAttr mapperId) {
23+
24+
if (auto boxTy = llvm::dyn_cast<fir::BaseBoxType>(baseAddr.getType())) {
25+
baseAddr = fir::BoxAddrOp::create(builder, loc, baseAddr);
26+
retTy = baseAddr.getType();
27+
}
28+
29+
mlir::TypeAttr varType = mlir::TypeAttr::get(
30+
llvm::cast<mlir::omp::PointerLikeType>(retTy).getElementType());
31+
32+
// For types with unknown extents such as <2x?xi32> we discard the incomplete
33+
// type info and only retain the base type. The correct dimensions are later
34+
// recovered through the bounds info.
35+
if (auto seqType = llvm::dyn_cast<fir::SequenceType>(varType.getValue()))
36+
if (seqType.hasDynamicExtents())
37+
varType = mlir::TypeAttr::get(seqType.getEleTy());
38+
39+
mlir::omp::MapInfoOp op =
40+
mlir::omp::MapInfoOp::create(builder, loc, retTy, baseAddr, varType,
41+
builder.getIntegerAttr(builder.getIntegerType(64, false), mapType),
42+
builder.getAttr<mlir::omp::VariableCaptureKindAttr>(mapCaptureType),
43+
varPtrPtr, members, membersIndex, bounds, mapperId,
44+
builder.getStringAttr(name), builder.getBoolAttr(partialMap));
45+
return op;
46+
}
47+
} // namespace Fortran::utils::openmp

0 commit comments

Comments
 (0)