Skip to content

Commit 86e4c17

Browse files
authored
[NFC][flang][OpenMP] Create FortranUtils lib and move createMapInfoOp to it (#154483)
1 parent 82917c8 commit 86e4c17

File tree

10 files changed

+110
-45
lines changed

10 files changed

+110
-45
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "flang/Lower/Support/ReductionProcessor.h"
2020
#include "flang/Parser/tools.h"
2121
#include "flang/Semantics/tools.h"
22+
#include "flang/Utils/OpenMP.h"
2223
#include "llvm/Frontend/OpenMP/OMP.h.inc"
2324
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
2425

@@ -1281,7 +1282,7 @@ void ClauseProcessor::processMapObjects(
12811282
auto location = mlir::NameLoc::get(
12821283
mlir::StringAttr::get(firOpBuilder.getContext(), asFortran.str()),
12831284
baseOp.getLoc());
1284-
mlir::omp::MapInfoOp mapOp = createMapInfoOp(
1285+
mlir::omp::MapInfoOp mapOp = utils::openmp::createMapInfoOp(
12851286
firOpBuilder, location, baseOp,
12861287
/*varPtrPtr=*/mlir::Value{}, asFortran.str(), bounds,
12871288
/*members=*/{}, /*membersIndex=*/mlir::ArrayAttr{},

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <flang/Parser/parse-tree.h>
2525
#include <flang/Parser/tools.h>
2626
#include <flang/Semantics/tools.h>
27+
#include <flang/Utils/OpenMP.h>
2728
#include <llvm/Support/CommandLine.h>
2829

2930
#include <iterator>
@@ -108,38 +109,6 @@ void gatherFuncAndVarSyms(
108109
symbolAndClause.emplace_back(clause, *object.sym(), automap);
109110
}
110111

111-
mlir::omp::MapInfoOp
112-
createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
113-
mlir::Value baseAddr, mlir::Value varPtrPtr,
114-
llvm::StringRef name, llvm::ArrayRef<mlir::Value> bounds,
115-
llvm::ArrayRef<mlir::Value> members,
116-
mlir::ArrayAttr membersIndex, uint64_t mapType,
117-
mlir::omp::VariableCaptureKind mapCaptureType, mlir::Type retTy,
118-
bool partialMap, mlir::FlatSymbolRefAttr mapperId) {
119-
if (auto boxTy = llvm::dyn_cast<fir::BaseBoxType>(baseAddr.getType())) {
120-
baseAddr = fir::BoxAddrOp::create(builder, loc, baseAddr);
121-
retTy = baseAddr.getType();
122-
}
123-
124-
mlir::TypeAttr varType = mlir::TypeAttr::get(
125-
llvm::cast<mlir::omp::PointerLikeType>(retTy).getElementType());
126-
127-
// For types with unknown extents such as <2x?xi32> we discard the incomplete
128-
// type info and only retain the base type. The correct dimensions are later
129-
// recovered through the bounds info.
130-
if (auto seqType = llvm::dyn_cast<fir::SequenceType>(varType.getValue()))
131-
if (seqType.hasDynamicExtents())
132-
varType = mlir::TypeAttr::get(seqType.getEleTy());
133-
134-
mlir::omp::MapInfoOp op = mlir::omp::MapInfoOp::create(
135-
builder, loc, retTy, baseAddr, varType,
136-
builder.getIntegerAttr(builder.getIntegerType(64, false), mapType),
137-
builder.getAttr<mlir::omp::VariableCaptureKindAttr>(mapCaptureType),
138-
varPtrPtr, members, membersIndex, bounds, mapperId,
139-
builder.getStringAttr(name), builder.getBoolAttr(partialMap));
140-
return op;
141-
}
142-
143112
// This function gathers the individual omp::Object's that make up a
144113
// larger omp::Object symbol.
145114
//
@@ -403,7 +372,7 @@ mlir::Value createParentSymAndGenIntermediateMaps(
403372

404373
// Create a map for the intermediate member and insert it and it's
405374
// indices into the parentMemberIndices list to track it.
406-
mlir::omp::MapInfoOp mapOp = createMapInfoOp(
375+
mlir::omp::MapInfoOp mapOp = utils::openmp::createMapInfoOp(
407376
firOpBuilder, clauseLocation, curValue,
408377
/*varPtrPtr=*/mlir::Value{}, asFortran,
409378
/*bounds=*/interimBounds,
@@ -563,7 +532,7 @@ void insertChildMapInfoIntoParent(
563532
converter.getCurrentLocation(), asFortran, bounds,
564533
treatIndexAsSection);
565534

566-
mlir::omp::MapInfoOp mapOp = createMapInfoOp(
535+
mlir::omp::MapInfoOp mapOp = utils::openmp::createMapInfoOp(
567536
firOpBuilder, info.rawInput.getLoc(), info.rawInput,
568537
/*varPtrPtr=*/mlir::Value(), asFortran.str(), bounds, members,
569538
firOpBuilder.create2DI64ArrayAttr(

flang/lib/Lower/OpenMP/Utils.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,6 @@ struct OmpMapParentAndMemberData {
114114
semantics::SemanticsContext &semaCtx);
115115
};
116116

117-
mlir::omp::MapInfoOp
118-
createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
119-
mlir::Value baseAddr, mlir::Value varPtrPtr,
120-
llvm::StringRef name, llvm::ArrayRef<mlir::Value> bounds,
121-
llvm::ArrayRef<mlir::Value> members,
122-
mlir::ArrayAttr membersIndex, uint64_t mapType,
123-
mlir::omp::VariableCaptureKind mapCaptureType, mlir::Type retTy,
124-
bool partialMap = false,
125-
mlir::FlatSymbolRefAttr mapperId = mlir::FlatSymbolRefAttr());
126-
127117
void insertChildMapInfoIntoParent(
128118
Fortran::lower::AbstractConverter &converter,
129119
Fortran::semantics::SemanticsContext &semaCtx,

flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp

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

56+
// TODO Use `createMapInfoOp` from `flang/Utils/OpenMP.h`.
5657
omp::MapInfoOp createMapInfo(Location loc, Value var,
5758
fir::FirOpBuilder &builder) {
5859
// 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)