Skip to content

Commit f993f36

Browse files
[Flang][OpenMP] - When mapping a fir.boxchar, map the underlying data pointer as a member (#141715)
This PR adds functionality to the `MapInfoFinalization` pass wherein the underlying data pointer of a `fir.boxchar` is mapped as a member of the parent boxchar.
1 parent 1b53037 commit f993f36

File tree

7 files changed

+333
-61
lines changed

7 files changed

+333
-61
lines changed

flang/include/flang/Optimizer/Builder/DirectivesCommon.h

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ inline AddrAndBoundsInfo getDataOperandBaseAddr(fir::FirOpBuilder &builder,
8888

8989
return AddrAndBoundsInfo(symAddr, rawInput, isPresent, boxTy);
9090
}
91+
// For boxchar references, do the same as what is done above for box
92+
// references - Load the boxchar so that it is easier to retrieve the length
93+
// of the underlying character and the data pointer.
94+
if (auto boxCharType = mlir::dyn_cast<fir::BoxCharType>(
95+
fir::unwrapRefType((symAddr.getType())))) {
96+
if (!isOptional && mlir::isa<fir::ReferenceType>(symAddr.getType())) {
97+
mlir::Value boxChar = builder.create<fir::LoadOp>(loc, symAddr);
98+
return AddrAndBoundsInfo(boxChar, rawInput, isPresent);
99+
}
100+
}
91101
return AddrAndBoundsInfo(symAddr, rawInput, isPresent);
92102
}
93103

@@ -134,26 +144,64 @@ template <typename BoundsOp, typename BoundsType>
134144
mlir::Value
135145
genBoundsOpFromBoxChar(fir::FirOpBuilder &builder, mlir::Location loc,
136146
fir::ExtendedValue dataExv, AddrAndBoundsInfo &info) {
137-
// TODO: Handle info.isPresent.
138-
if (auto boxCharType =
139-
mlir::dyn_cast<fir::BoxCharType>(info.addr.getType())) {
140-
mlir::Type idxTy = builder.getIndexType();
141-
mlir::Type lenType = builder.getCharacterLengthType();
147+
148+
if (!mlir::isa<fir::BoxCharType>(fir::unwrapRefType(info.addr.getType())))
149+
return mlir::Value{};
150+
151+
mlir::Type idxTy = builder.getIndexType();
152+
mlir::Type lenType = builder.getCharacterLengthType();
153+
mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
154+
mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
155+
using ExtentAndStride = std::tuple<mlir::Value, mlir::Value>;
156+
auto [extent, stride] = [&]() -> ExtentAndStride {
157+
if (info.isPresent) {
158+
llvm::SmallVector<mlir::Type> resTypes = {idxTy, idxTy};
159+
mlir::Operation::result_range ifRes =
160+
builder
161+
.genIfOp(loc, resTypes, info.isPresent, /*withElseRegion=*/true)
162+
.genThen([&]() {
163+
mlir::Value boxChar =
164+
fir::isa_ref_type(info.addr.getType())
165+
? builder.create<fir::LoadOp>(loc, info.addr)
166+
: info.addr;
167+
fir::BoxCharType boxCharType =
168+
mlir::cast<fir::BoxCharType>(boxChar.getType());
169+
mlir::Type refType = builder.getRefType(boxCharType.getEleTy());
170+
auto unboxed = builder.create<fir::UnboxCharOp>(
171+
loc, refType, lenType, boxChar);
172+
mlir::SmallVector<mlir::Value> results = {unboxed.getResult(1),
173+
one};
174+
builder.create<fir::ResultOp>(loc, results);
175+
})
176+
.genElse([&]() {
177+
mlir::SmallVector<mlir::Value> results = {zero, zero};
178+
builder.create<fir::ResultOp>(loc, results);
179+
})
180+
.getResults();
181+
return {ifRes[0], ifRes[1]};
182+
}
183+
// We have already established that info.addr.getType() is a boxchar
184+
// or a boxchar address. If an address, load the boxchar.
185+
mlir::Value boxChar = fir::isa_ref_type(info.addr.getType())
186+
? builder.create<fir::LoadOp>(loc, info.addr)
187+
: info.addr;
188+
fir::BoxCharType boxCharType =
189+
mlir::cast<fir::BoxCharType>(boxChar.getType());
142190
mlir::Type refType = builder.getRefType(boxCharType.getEleTy());
143191
auto unboxed =
144-
builder.create<fir::UnboxCharOp>(loc, refType, lenType, info.addr);
145-
mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
146-
mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
147-
mlir::Value extent = unboxed.getResult(1);
148-
mlir::Value stride = one;
149-
mlir::Value ub = builder.create<mlir::arith::SubIOp>(loc, extent, one);
150-
mlir::Type boundTy = builder.getType<mlir::omp::MapBoundsType>();
151-
return builder.create<mlir::omp::MapBoundsOp>(
152-
loc, boundTy, /*lower_bound=*/zero,
153-
/*upper_bound=*/ub, /*extent=*/extent, /*stride=*/stride,
154-
/*stride_in_bytes=*/true, /*start_idx=*/zero);
155-
}
156-
return mlir::Value{};
192+
builder.create<fir::UnboxCharOp>(loc, refType, lenType, boxChar);
193+
return {unboxed.getResult(1), one};
194+
}();
195+
196+
mlir::Value ub = builder.create<mlir::arith::SubIOp>(loc, extent, one);
197+
mlir::Type boundTy = builder.getType<BoundsType>();
198+
return builder.create<BoundsOp>(loc, boundTy,
199+
/*lower_bound=*/zero,
200+
/*upper_bound=*/ub,
201+
/*extent=*/extent,
202+
/*stride=*/stride,
203+
/*stride_in_bytes=*/true,
204+
/*start_idx=*/zero);
157205
}
158206

159207
/// Generate the bounds operation from the descriptor information.
@@ -293,11 +341,11 @@ genImplicitBoundsOps(fir::FirOpBuilder &builder, AddrAndBoundsInfo &info,
293341
bounds = genBaseBoundsOps<BoundsOp, BoundsType>(builder, loc, dataExv,
294342
dataExvIsAssumedSize);
295343
}
296-
if (characterWithDynamicLen(fir::unwrapRefType(baseOp.getType()))) {
344+
if (characterWithDynamicLen(fir::unwrapRefType(baseOp.getType())) ||
345+
mlir::isa<fir::BoxCharType>(fir::unwrapRefType(info.addr.getType()))) {
297346
bounds = {genBoundsOpFromBoxChar<BoundsOp, BoundsType>(builder, loc,
298347
dataExv, info)};
299348
}
300-
301349
return bounds;
302350
}
303351

flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#include <numeric>
4949

5050
#define DEBUG_TYPE "omp-map-info-finalization"
51-
#define PDBGS() (llvm::dbgs() << "[" << DEBUG_TYPE << "]: ")
51+
5252
namespace flangomp {
5353
#define GEN_PASS_DEF_MAPINFOFINALIZATIONPASS
5454
#include "flang/Optimizer/OpenMP/Passes.h.inc"
@@ -285,6 +285,62 @@ class MapInfoFinalizationPass
285285
return false;
286286
}
287287

288+
mlir::omp::MapInfoOp genBoxcharMemberMap(mlir::omp::MapInfoOp op,
289+
fir::FirOpBuilder &builder) {
290+
if (!op.getMembers().empty())
291+
return op;
292+
mlir::Location loc = op.getVarPtr().getLoc();
293+
mlir::Value boxChar = op.getVarPtr();
294+
295+
if (mlir::isa<fir::ReferenceType>(op.getVarPtr().getType()))
296+
boxChar = builder.create<fir::LoadOp>(loc, op.getVarPtr());
297+
298+
fir::BoxCharType boxCharType =
299+
mlir::dyn_cast<fir::BoxCharType>(boxChar.getType());
300+
mlir::Value boxAddr = builder.create<fir::BoxOffsetOp>(
301+
loc, op.getVarPtr(), fir::BoxFieldAttr::base_addr);
302+
303+
uint64_t mapTypeToImplicit = static_cast<
304+
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
305+
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO |
306+
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT);
307+
308+
mlir::ArrayAttr newMembersAttr;
309+
llvm::SmallVector<llvm::SmallVector<int64_t>> memberIdx = {{0}};
310+
newMembersAttr = builder.create2DI64ArrayAttr(memberIdx);
311+
312+
mlir::Value varPtr = op.getVarPtr();
313+
mlir::omp::MapInfoOp memberMapInfoOp = builder.create<mlir::omp::MapInfoOp>(
314+
op.getLoc(), varPtr.getType(), varPtr,
315+
mlir::TypeAttr::get(boxCharType.getEleTy()),
316+
builder.getIntegerAttr(builder.getIntegerType(64, /*isSigned=*/false),
317+
mapTypeToImplicit),
318+
builder.getAttr<mlir::omp::VariableCaptureKindAttr>(
319+
mlir::omp::VariableCaptureKind::ByRef),
320+
/*varPtrPtr=*/boxAddr,
321+
/*members=*/llvm::SmallVector<mlir::Value>{},
322+
/*member_index=*/mlir::ArrayAttr{},
323+
/*bounds=*/op.getBounds(),
324+
/*mapperId=*/mlir::FlatSymbolRefAttr(), /*name=*/op.getNameAttr(),
325+
builder.getBoolAttr(false));
326+
327+
mlir::omp::MapInfoOp newMapInfoOp = builder.create<mlir::omp::MapInfoOp>(
328+
op.getLoc(), op.getResult().getType(), varPtr,
329+
mlir::TypeAttr::get(
330+
llvm::cast<mlir::omp::PointerLikeType>(varPtr.getType())
331+
.getElementType()),
332+
op.getMapTypeAttr(), op.getMapCaptureTypeAttr(),
333+
/*varPtrPtr=*/mlir::Value{},
334+
/*members=*/llvm::SmallVector<mlir::Value>{memberMapInfoOp},
335+
/*member_index=*/newMembersAttr,
336+
/*bounds=*/llvm::SmallVector<mlir::Value>{},
337+
/*mapperId=*/mlir::FlatSymbolRefAttr(), op.getNameAttr(),
338+
/*partial_map=*/builder.getBoolAttr(false));
339+
op.replaceAllUsesWith(newMapInfoOp.getResult());
340+
op->erase();
341+
return newMapInfoOp;
342+
}
343+
288344
mlir::omp::MapInfoOp genDescriptorMemberMaps(mlir::omp::MapInfoOp op,
289345
fir::FirOpBuilder &builder,
290346
mlir::Operation *target) {
@@ -575,6 +631,7 @@ class MapInfoFinalizationPass
575631
fir::factory::AddrAndBoundsInfo info =
576632
fir::factory::getDataOperandBaseAddr(
577633
builder, varPtr, /*isOptional=*/false, varPtr.getLoc());
634+
578635
fir::ExtendedValue extendedValue =
579636
hlfir::translateToExtendedValue(varPtr.getLoc(), builder,
580637
hlfir::Entity{info.addr},
@@ -743,6 +800,37 @@ class MapInfoFinalizationPass
743800
return mlir::WalkResult::advance();
744801
});
745802

803+
func->walk([&](mlir::omp::MapInfoOp op) {
804+
if (!op.getMembers().empty())
805+
return;
806+
807+
if (!mlir::isa<fir::BoxCharType>(fir::unwrapRefType(op.getVarType())))
808+
return;
809+
810+
// POSSIBLE_HACK_ALERT: If the boxchar has been implicitly mapped then
811+
// it is likely that the underlying pointer to the data
812+
// (!fir.ref<fir.char<k,?>>) has already been mapped. So, skip such
813+
// boxchars. We are primarily interested in boxchars that were mapped
814+
// by passes such as MapsForPrivatizedSymbols that map boxchars that
815+
// are privatized. At present, such boxchar maps are not marked
816+
// implicit. Should they be? I don't know. If they should be then
817+
// we need to change this check for early return OR live with
818+
// over-mapping.
819+
bool hasImplicitMap =
820+
(llvm::omp::OpenMPOffloadMappingFlags(op.getMapType()) &
821+
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT) ==
822+
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT;
823+
if (hasImplicitMap)
824+
return;
825+
826+
assert(llvm::hasSingleElement(op->getUsers()) &&
827+
"OMPMapInfoFinalization currently only supports single users "
828+
"of a MapInfoOp");
829+
830+
builder.setInsertionPoint(op);
831+
genBoxcharMemberMap(op, builder);
832+
});
833+
746834
func->walk([&](mlir::omp::MapInfoOp op) {
747835
// TODO: Currently only supports a single user for the MapInfoOp. This
748836
// is fine for the moment, as the Fortran frontend will generate a

flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
// 2. Generalize this for more than just omp.target ops.
2323
//===----------------------------------------------------------------------===//
2424

25+
#include "flang/Optimizer/Builder/DirectivesCommon.h"
2526
#include "flang/Optimizer/Builder/FIRBuilder.h"
27+
#include "flang/Optimizer/Builder/HLFIRTools.h"
2628
#include "flang/Optimizer/Dialect/FIRType.h"
2729
#include "flang/Optimizer/Dialect/Support/KindMapping.h"
2830
#include "flang/Optimizer/HLFIR/HLFIROps.h"
@@ -184,37 +186,23 @@ class MapsForPrivatizedSymbolsPass
184186
return fir::hasDynamicSize(t);
185187
}
186188

187-
// TODO: Remove this in favor of fir::factory::genImplicitBoundsOps
188-
// in a subsequent PR.
189189
void genBoundsOps(fir::FirOpBuilder &builder, mlir::Value var,
190190
llvm::SmallVector<mlir::Value> &boundsOps) {
191-
if (!fir::isBoxAddress(var.getType()))
192-
return;
193-
194-
unsigned int rank = 0;
195-
rank = fir::getBoxRank(fir::unwrapRefType(var.getType()));
196191
mlir::Location loc = var.getLoc();
197-
mlir::Type idxTy = builder.getIndexType();
198-
mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
199-
mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
200-
mlir::Type boundTy = builder.getType<omp::MapBoundsType>();
201-
mlir::Value box = builder.create<fir::LoadOp>(loc, var);
202-
for (unsigned int i = 0; i < rank; ++i) {
203-
mlir::Value dimNo = builder.createIntegerConstant(loc, idxTy, i);
204-
auto dimInfo =
205-
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dimNo);
206-
mlir::Value lb = dimInfo.getLowerBound();
207-
mlir::Value extent = dimInfo.getExtent();
208-
mlir::Value byteStride = dimInfo.getByteStride();
209-
mlir::Value ub = builder.create<mlir::arith::SubIOp>(loc, extent, one);
210-
211-
mlir::Value boundsOp = builder.create<omp::MapBoundsOp>(
212-
loc, boundTy, /*lower_bound=*/zero,
213-
/*upper_bound=*/ub, /*extent=*/extent, /*stride=*/byteStride,
214-
/*stride_in_bytes = */ true, /*start_idx=*/lb);
215-
LLVM_DEBUG(PDBGS() << "Created BoundsOp " << boundsOp << "\n");
216-
boundsOps.push_back(boundsOp);
217-
}
192+
fir::factory::AddrAndBoundsInfo info =
193+
fir::factory::getDataOperandBaseAddr(builder, var,
194+
/*isOptional=*/false, loc);
195+
fir::ExtendedValue extendedValue =
196+
hlfir::translateToExtendedValue(loc, builder, hlfir::Entity{info.addr},
197+
/*continguousHint=*/true)
198+
.first;
199+
llvm::SmallVector<mlir::Value> boundsOpsVec =
200+
fir::factory::genImplicitBoundsOps<mlir::omp::MapBoundsOp,
201+
mlir::omp::MapBoundsType>(
202+
builder, info, extendedValue,
203+
/*dataExvIsAssumedSize=*/false, loc);
204+
for (auto bounds : boundsOpsVec)
205+
boundsOps.push_back(bounds);
218206
}
219207
};
220208
} // namespace

flang/test/Fir/convert-to-llvm-openmp-and-fir.fir

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,3 +1288,30 @@ omp.declare_mapper @my_mapper : !fir.type<_QFdeclare_mapperTmy_type{data:i32}> {
12881288
omp.declare_mapper.info map_entries(%4, %3 : !fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>>, !fir.ref<i32>)
12891289
// CHECK: }
12901290
}
1291+
1292+
// -----
1293+
omp.private {type = firstprivate} @boxchar_privatizer : !fir.boxchar<1> copy {
1294+
^bb0(%arg0: !fir.boxchar<1>, %arg1: !fir.boxchar<1>):
1295+
omp.yield(%arg1 : !fir.boxchar<1>)
1296+
}
1297+
1298+
func.func @map_privatized_boxchar(%arg0 : !fir.boxchar<1>) {
1299+
%0 = fir.alloca !fir.boxchar<1>
1300+
fir.store %arg0 to %0 : !fir.ref<!fir.boxchar<1>>
1301+
%7 = fir.box_offset %0 base_addr : (!fir.ref<!fir.boxchar<1>>) -> !fir.llvm_ptr<!fir.ref<!fir.char<1,?>>>
1302+
%8 = omp.map.info var_ptr(%0 : !fir.ref<!fir.boxchar<1>>, !fir.char<1,?>) map_clauses(implicit, to) capture(ByRef) var_ptr_ptr(%7 : !fir.llvm_ptr<!fir.ref<!fir.char<1,?>>>) -> !fir.ref<!fir.boxchar<1>>
1303+
%9 = omp.map.info var_ptr(%0 : !fir.ref<!fir.boxchar<1>>, !fir.boxchar<1>) map_clauses(to) capture(ByRef) members(%8 : [0] : !fir.ref<!fir.boxchar<1>>) -> !fir.ref<!fir.boxchar<1>>
1304+
omp.target map_entries(%9 -> %arg1, %8 -> %arg2 : !fir.ref<!fir.boxchar<1>>, !fir.ref<!fir.boxchar<1>>) private(@boxchar_privatizer %arg0 -> %arg3 [map_idx=0] : !fir.boxchar<1>) {
1305+
omp.terminator
1306+
}
1307+
return
1308+
}
1309+
1310+
// CHECK-LABEL: llvm.func @map_privatized_boxchar(
1311+
// CHECK-SAME: %[[ARG0:.*]]: !llvm.struct<(ptr, i64)>) {
1312+
// CHECK: %[[BOXCHAR_ALLOCA:.*]] = llvm.alloca {{.*}} x !llvm.struct<(ptr, i64)> : (i64) -> !llvm.ptr
1313+
// CHECK: llvm.store %[[ARG0]], %[[BOXCHAR_ALLOCA]] : !llvm.struct<(ptr, i64)>, !llvm.ptr
1314+
// CHECK: %[[BASE_ADDR:.*]] = llvm.getelementptr %[[BOXCHAR_ALLOCA]][0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(ptr, i64)>
1315+
// CHECK: %[[MAP_BASE_ADDR:.*]] = omp.map.info var_ptr(%[[BOXCHAR_ALLOCA]] : !llvm.ptr, i8) map_clauses(implicit, to) capture(ByRef) var_ptr_ptr(%[[BASE_ADDR]] : !llvm.ptr) -> !llvm.ptr
1316+
// CHECK: %[[MAP_BOXCHAR:.*]] = omp.map.info var_ptr(%[[BOXCHAR_ALLOCA]] : !llvm.ptr, !llvm.struct<(ptr, i64)>) map_clauses(to) capture(ByRef) members(%[[MAP_BASE_ADDR]] : [0] : !llvm.ptr) -> !llvm.ptr
1317+
// CHECK: omp.target map_entries(%[[MAP_BOXCHAR]] -> %arg1, %[[MAP_BASE_ADDR]] -> %arg2 : !llvm.ptr, !llvm.ptr) private(@boxchar_privatizer %[[ARG0]] -> %arg3 [map_idx=0] : !llvm.struct<(ptr, i64)>) {

0 commit comments

Comments
 (0)