Skip to content

Commit 51722d2

Browse files
More fixes based on some out-of-tree end-to-end tests
1 parent 0c20966 commit 51722d2

File tree

5 files changed

+93
-23
lines changed

5 files changed

+93
-23
lines changed

flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@
3838
#include <type_traits>
3939

4040
#define DEBUG_TYPE "omp-maps-for-privatized-symbols"
41-
41+
#define PDBGS() (llvm::dbgs() << "[" << DEBUG_TYPE << "]: ")
4242
namespace flangomp {
4343
#define GEN_PASS_DEF_MAPSFORPRIVATIZEDSYMBOLSPASS
4444
#include "flang/Optimizer/OpenMP/Passes.h.inc"
4545
} // namespace flangomp
46+
4647
using namespace mlir;
48+
4749
namespace {
4850
class MapsForPrivatizedSymbolsPass
4951
: public flangomp::impl::MapsForPrivatizedSymbolsPassBase<
@@ -60,14 +62,14 @@ class MapsForPrivatizedSymbolsPass
6062
// We want the first result of the hlfir.declare op because our goal
6163
// is to map the descriptor (fir.box or fir.boxchar) and the first
6264
// result for hlfir.declare is the descriptor if a the symbol being
63-
// decalred needs a descriptor.
65+
// declared needs a descriptor.
6466
// Some types are boxed immediately before privatization. These have other
6567
// operations in between the privatization and the declaration. It is safe
6668
// to use var directly here because they will be boxed anyway.
6769
if (auto declOp = llvm::dyn_cast_if_present<hlfir::DeclareOp>(definingOp))
6870
varPtr = declOp.getBase();
6971

70-
// If we do not have a reference to descritor, but the descriptor itself
72+
// If we do not have a reference to a descriptor but the descriptor itself,
7173
// then we need to store that on the stack so that we can map the
7274
// address of the descriptor.
7375
if (mlir::isa<fir::BaseBoxType>(varPtr.getType()) ||
@@ -81,6 +83,15 @@ class MapsForPrivatizedSymbolsPass
8183
builder.create<fir::StoreOp>(loc, varPtr, alloca);
8284
varPtr = alloca;
8385
}
86+
assert(mlir::isa<omp::PointerLikeType>(varPtr.getType()) &&
87+
"Dealing with a varPtr that is not a PointerLikeType");
88+
89+
// Figure out the bounds because knowing the bounds will help the subsequent
90+
// MapInfoFinalizationPass map the underlying data of the descriptor.
91+
llvm::SmallVector<mlir::Value> boundsOps;
92+
if (needsBoundsOps(varPtr))
93+
genBoundsOps(builder, varPtr, boundsOps);
94+
8495
return builder.create<omp::MapInfoOp>(
8596
loc, varPtr.getType(), varPtr,
8697
TypeAttr::get(llvm::cast<omp::PointerLikeType>(varPtr.getType())
@@ -92,7 +103,7 @@ class MapsForPrivatizedSymbolsPass
92103
/*varPtrPtr=*/Value{},
93104
/*members=*/SmallVector<Value>{},
94105
/*member_index=*/mlir::ArrayAttr{},
95-
/*bounds=*/ValueRange{},
106+
/*bounds=*/boundsOps.empty() ? SmallVector<Value>{} : boundsOps,
96107
/*mapperId=*/mlir::FlatSymbolRefAttr(), /*name=*/StringAttr(),
97108
builder.getBoolAttr(false));
98109
}
@@ -143,8 +154,8 @@ class MapsForPrivatizedSymbolsPass
143154
omp::MapInfoOp mapInfoOp = createMapInfo(loc, privVar, builder);
144155
mapInfoOps.push_back(mapInfoOp);
145156

146-
LLVM_DEBUG(llvm::dbgs() << "MapsForPrivatizedSymbolsPass created ->\n");
147-
LLVM_DEBUG(mapInfoOp.dump());
157+
LLVM_DEBUG(PDBGS() << "MapsForPrivatizedSymbolsPass created ->\n"
158+
<< mapInfoOp << "\n");
148159
}
149160
if (!mapInfoOps.empty()) {
150161
mapInfoOpsForTarget.insert({targetOp.getOperation(), mapInfoOps});
@@ -158,5 +169,50 @@ class MapsForPrivatizedSymbolsPass
158169
}
159170
}
160171
}
172+
// As the name suggests, this function examines var to determine if
173+
// it has dynamic size. If true, this pass'll have to extract these
174+
// bounds from descriptor of var and add the bounds to the resultant
175+
// MapInfoOp.
176+
bool needsBoundsOps(mlir::Value var) {
177+
assert(mlir::isa<omp::PointerLikeType>(var.getType()) &&
178+
"needsBoundsOps can deal only with pointer types");
179+
mlir::Type t = fir::unwrapRefType(var.getType());
180+
// t could be a box, so look inside the box
181+
auto innerType = fir::dyn_cast_ptrOrBoxEleTy(t);
182+
if (innerType)
183+
return fir::hasDynamicSize(innerType);
184+
return fir::hasDynamicSize(t);
185+
}
186+
void genBoundsOps(fir::FirOpBuilder &builder, mlir::Value var,
187+
llvm::SmallVector<mlir::Value> &boundsOps) {
188+
if (!fir::isBoxAddress(var.getType()))
189+
return;
190+
191+
unsigned int rank = 0;
192+
rank = fir::getBoxRank(fir::unwrapRefType(var.getType()));
193+
mlir::Location loc = var.getLoc();
194+
mlir::Type idxTy = builder.getIndexType();
195+
mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
196+
mlir::Type boundTy = builder.getType<omp::MapBoundsType>();
197+
mlir::Value box = builder.create<fir::LoadOp>(loc, var);
198+
for (unsigned int i = 0; i < rank; ++i) {
199+
mlir::Value dimNo = builder.createIntegerConstant(loc, idxTy, i);
200+
auto dimInfo =
201+
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dimNo);
202+
auto normalizedLB = builder.create<mlir::arith::ConstantOp>(
203+
loc, idxTy, builder.getIntegerAttr(idxTy, 0));
204+
mlir::Value lb = dimInfo.getLowerBound();
205+
mlir::Value extent = dimInfo.getExtent();
206+
mlir::Value byteStride = dimInfo.getByteStride();
207+
mlir::Value ub = builder.create<mlir::arith::SubIOp>(loc, extent, one);
208+
209+
mlir::Value boundsOp = builder.create<omp::MapBoundsOp>(
210+
loc, boundTy, /*lower_bound=*/normalizedLB,
211+
/*upper_bound=*/ub, /*extent=*/extent, /*stride=*/byteStride,
212+
/*stride_in_bytes = */ true, /*start_idx=*/lb);
213+
LLVM_DEBUG(PDBGS() << "Created BoundsOp " << boundsOp << "\n");
214+
boundsOps.push_back(boundsOp);
215+
}
216+
}
161217
};
162218
} // namespace

flang/lib/Optimizer/Passes/Pipelines.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,14 @@ void createHLFIRToFIRPassPipeline(mlir::PassManager &pm, bool enableOpenMP,
287287
/// \param isTargetDevice - Whether code is being generated for a target device
288288
/// rather than the host device.
289289
void createOpenMPFIRPassPipeline(mlir::PassManager &pm, bool isTargetDevice) {
290-
pm.addPass(flangomp::createMapInfoFinalizationPass());
290+
// The MapsForPrivatizedSymbols pass needs to run before
291+
// MapInfoFinalizationPass because the former creates new
292+
// MapInfoOp instances, typically for descriptors.
293+
// MapInfoFinalizationPass adds MapInfoOp instances for the descriptors
294+
// underlying data which is necessary to access the data on the offload
295+
// target device.
291296
pm.addPass(flangomp::createMapsForPrivatizedSymbolsPass());
297+
pm.addPass(flangomp::createMapInfoFinalizationPass());
292298
pm.addPass(flangomp::createMarkDeclareTargetPass());
293299
pm.addPass(flangomp::createGenericLoopConversionPass());
294300
if (isTargetDevice)

flang/test/Lower/OpenMP/DelayedPrivatization/target-private-allocatable.f90

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ end subroutine target_allocatable
5858
! CHECK: %[[VAR_ALLOC:.*]] = fir.alloca [[DESC_TYPE]]
5959
! CHECK-SAME: {bindc_name = "alloc_var", {{.*}}}
6060
! CHECK: %[[VAR_DECL:.*]]:2 = hlfir.declare %[[VAR_ALLOC]]
61+
! CHECK: %[[BASE_ADDR:.*]] = fir.box_offset %[[VAR_DECL]]#0 base_addr : (!fir.ref<!fir.box<!fir.heap<i32>>>) -> [[MEMBER_TYPE:.*]]
62+
! CHECK: %[[MEMBER:.*]] = omp.map.info var_ptr(%[[VAR_DECL]]#0 : [[TYPE]], i32) var_ptr_ptr(%[[BASE_ADDR]] : [[MEMBER_TYPE:.*]]) map_clauses(to) capture(ByRef) -> {{.*}}
63+
! CHECK: %[[MAP_VAR:.*]] = omp.map.info var_ptr(%[[VAR_DECL]]#0 : [[TYPE]], [[DESC_TYPE]]) map_clauses(to) capture(ByRef) members(%[[MEMBER]] : [0] : !fir.llvm_ptr<!fir.ref<i32>>) -> !fir.ref<!fir.box<!fir.heap<i32>>>
6164

62-
! CHECK: %[[MAP_VAR:.*]] = omp.map.info var_ptr(%[[VAR_DECL]]#0 : [[TYPE]], [[DESC_TYPE]])
63-
! CHECK-SAME: map_clauses(to) capture(ByRef) -> [[TYPE]]
64-
! CHECK: omp.target map_entries(%[[MAP_VAR]] -> %arg0 : [[TYPE]]) private(
65-
! CHECK-SAME: @[[VAR_PRIVATIZER_SYM]] %[[VAR_DECL]]#0 -> %{{.*}} : [[TYPE]]) {
65+
! CHECK: omp.target map_entries(%[[MAP_VAR]] -> %arg0, %[[MEMBER]] -> %arg1 : [[TYPE]], [[MEMBER_TYPE]]) private(
66+
! CHECK-SAME: @[[VAR_PRIVATIZER_SYM]] %[[VAR_DECL]]#0 -> %{{.*}} [map_idx=0] : [[TYPE]]) {

flang/test/Lower/OpenMP/DelayedPrivatization/target-private-multiple-variables.f90

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,19 @@ end subroutine target_allocatable
140140
! CHECK: %[[REAL_ARR_DECL:.*]]:2 = hlfir.declare %[[REAL_ARR_ALLOC]]({{.*}})
141141
! CHECK: fir.store %[[REAL_ARR_DECL]]#0 to %[[REAL_ARR_DESC_ALLOCA]] : !fir.ref<!fir.box<!fir.array<?xf32>>>
142142
! CHECK: %[[MAPPED_MI0:.*]] = omp.map.info var_ptr(%[[MAPPED_DECL]]#1 : !fir.ref<i32>, i32) {{.*}}
143-
! CHECK: %[[ALLOC_VAR_MAP:.*]] = omp.map.info var_ptr(%[[ALLOC_VAR_DECL]]#0 : !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.box<!fir.heap<i32>>)
144-
! CHECK: %[[REAL_ARR_DESC_MAP:.*]] = omp.map.info var_ptr(%[[REAL_ARR_DESC_ALLOCA]] : !fir.ref<!fir.box<!fir.array<?xf32>>>, !fir.box<!fir.array<?xf32>>)
143+
! CHECK: %[[ALLOC_VAR_MEMBER:.*]] = omp.map.info var_ptr(%[[ALLOC_VAR_DECL]]#0 : !fir.ref<!fir.box<!fir.heap<i32>>>, i32)
144+
! CHECK: %[[ALLOC_VAR_MAP:.*]] = omp.map.info var_ptr(%[[ALLOC_VAR_DECL]]#0 : !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.box<!fir.heap<i32>>) {{.*}} members(%[[ALLOC_VAR_MEMBER]] :
145+
! CHECK: %[[REAL_ARR_MEMBER:.*]] = omp.map.info var_ptr(%[[REAL_ARR_DESC_ALLOCA]] : !fir.ref<!fir.box<!fir.array<?xf32>>>, f32)
146+
! CHECK: %[[REAL_ARR_DESC_MAP:.*]] = omp.map.info var_ptr(%[[REAL_ARR_DESC_ALLOCA]] : !fir.ref<!fir.box<!fir.array<?xf32>>>, !fir.box<!fir.array<?xf32>>) {{.*}} members(%[[REAL_ARR_MEMBER]] :
145147
! CHECK: fir.store %[[CHAR_VAR_DECL]]#0 to %[[CHAR_VAR_DESC_ALLOCA]] : !fir.ref<!fir.boxchar<1>>
146148
! CHECK: %[[CHAR_VAR_DESC_MAP:.*]] = omp.map.info var_ptr(%[[CHAR_VAR_DESC_ALLOCA]] : !fir.ref<!fir.boxchar<1>>, !fir.boxchar<1>)
147149
! CHECK: omp.target
148150
! CHECK-SAME: map_entries(
149151
! CHECK-SAME: %[[MAPPED_MI0]] -> %[[MAPPED_ARG0:[^,]+]],
150152
! CHECK-SAME: %[[ALLOC_VAR_MAP]] -> %[[MAPPED_ARG1:[^,]+]]
151153
! CHECK-SAME: %[[REAL_ARR_DESC_MAP]] -> %[[MAPPED_ARG2:[^,]+]]
152-
! CHECK-SAME: %[[CHAR_VAR_DESC_MAP]] -> %[[MAPPED_ARG3:.[^,]+]] :
153-
! CHECK-SAME: !fir.ref<i32>, !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<!fir.box<!fir.array<?xf32>>>, !fir.ref<!fir.boxchar<1>>)
154+
! CHECK-SAME: %[[CHAR_VAR_DESC_MAP]] -> %[[MAPPED_ARG3:.[^,]+]]
155+
! CHECK-SAME: !fir.ref<i32>, !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<!fir.box<!fir.array<?xf32>>>, !fir.ref<!fir.boxchar<1>>, !fir.llvm_ptr<!fir.ref<i32>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xf32>>>
154156
! CHECK-SAME: private(
155157
! CHECK-SAME: @[[ALLOC_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[ALLOC_ARG:[^,]+]] [map_idx=1],
156158
! CHECK-SAME: @[[REAL_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[REAL_ARG:[^,]+]],

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,7 @@ allocatePrivateVars(llvm::IRBuilderBase &builder,
14551455
assert(allocaTerminator->getNumSuccessors() == 1 &&
14561456
"This is an unconditional branch created by splitBB");
14571457

1458+
llvm::DataLayout dataLayout = builder.GetInsertBlock()->getDataLayout();
14581459
llvm::BasicBlock *afterAllocas = allocaTerminator->getSuccessor(0);
14591460

14601461
unsigned int allocaAS =
@@ -1463,6 +1464,7 @@ allocatePrivateVars(llvm::IRBuilderBase &builder,
14631464
->getDataLayout()
14641465
.getProgramAddressSpace();
14651466

1467+
14661468
for (auto [privDecl, mlirPrivVar, blockArg] :
14671469
llvm::zip_equal(privateVarsInfo.privatizers, privateVarsInfo.mlirVars,
14681470
privateVarsInfo.blockArgs)) {
@@ -1476,17 +1478,18 @@ allocatePrivateVars(llvm::IRBuilderBase &builder,
14761478
builder.getPtrTy(defaultAS));
14771479

14781480
privateVarsInfo.llvmVars.push_back(llvmPrivateVar);
1481+
14791482
}
14801483

14811484
return afterAllocas;
14821485
}
14831486

1484-
static LogicalResult
1485-
copyFirstPrivateVars(llvm::IRBuilderBase &builder,
1486-
LLVM::ModuleTranslation &moduleTranslation,
1487-
SmallVectorImpl<mlir::Value> &mlirPrivateVars,
1488-
ArrayRef<llvm::Value *> llvmPrivateVars,
1489-
SmallVectorImpl<omp::PrivateClauseOp> &privateDecls) {
1487+
static LogicalResult copyFirstPrivateVars(
1488+
llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation,
1489+
SmallVectorImpl<mlir::Value> &mlirPrivateVars,
1490+
ArrayRef<llvm::Value *> llvmPrivateVars,
1491+
SmallVectorImpl<omp::PrivateClauseOp> &privateDecls,
1492+
llvm::DenseMap<Value, Value> *mappedPrivateVars = nullptr) {
14901493
// Apply copy region for firstprivate.
14911494
bool needsFirstprivate =
14921495
llvm::any_of(privateDecls, [](omp::PrivateClauseOp &privOp) {
@@ -1510,7 +1513,8 @@ copyFirstPrivateVars(llvm::IRBuilderBase &builder,
15101513
Region &copyRegion = decl.getCopyRegion();
15111514

15121515
// map copyRegion rhs arg
1513-
llvm::Value *nonPrivateVar = moduleTranslation.lookupValue(mlirVar);
1516+
llvm::Value *nonPrivateVar = findAssociatedValue(
1517+
mlirVar, builder, moduleTranslation, mappedPrivateVars);
15141518
assert(nonPrivateVar);
15151519
moduleTranslation.mapValue(decl.getCopyMoldArg(), nonPrivateVar);
15161520

@@ -4851,7 +4855,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
48514855
return llvm::make_error<PreviouslyReportedError>();
48524856

48534857
if (failed(copyFirstPrivateVars(builder, moduleTranslation, mlirPrivateVars,
4854-
llvmPrivateVars, privateDecls)))
4858+
llvmPrivateVars, privateDecls,
4859+
&mappedPrivateVars)))
48554860
return llvm::make_error<PreviouslyReportedError>();
48564861

48574862
SmallVector<Region *> privateCleanupRegions;

0 commit comments

Comments
 (0)