@@ -217,111 +217,36 @@ static void bindEntryBlockArgs(lower::AbstractConverter &converter,
217217 assert (args.isValid () && " invalid args" );
218218 fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder ();
219219
220- auto bindSingleMapLike = [&converter,
221- &firOpBuilder](const semantics::Symbol &sym,
222- const mlir::Value val,
223- const mlir::BlockArgument &arg) {
224- // Clones the `bounds` placing them inside the entry block and returns
225- // them.
226- auto cloneBound = [&](mlir::Value bound) {
227- if (mlir::isMemoryEffectFree (bound.getDefiningOp ())) {
228- mlir::Operation *definingOp = bound.getDefiningOp ();
229- mlir::Operation *clonedOp = firOpBuilder.clone (*definingOp);
230- // Todo: Do we need to check for more operation types?
231- // For now, specializing only for fir::UnboxCharOp
232- if (auto unboxCharOp = mlir::dyn_cast<fir::UnboxCharOp>(definingOp))
233- return clonedOp->getResult (1 );
234- return clonedOp->getResult (0 );
235- }
236- TODO (converter.getCurrentLocation (),
237- " target map-like clause operand unsupported bound type" );
238- };
239-
240- auto cloneBounds = [cloneBound](llvm::ArrayRef<mlir::Value> bounds) {
241- llvm::SmallVector<mlir::Value> clonedBounds;
242- llvm::transform (bounds, std::back_inserter (clonedBounds),
243- [&](mlir::Value bound) { return cloneBound (bound); });
244- return clonedBounds;
245- };
246-
220+ auto bindSingleMapLike = [&converter](const semantics::Symbol &sym,
221+ const mlir::BlockArgument &arg) {
247222 fir::ExtendedValue extVal = converter.getSymbolExtendedValue (sym);
248223 auto refType = mlir::dyn_cast<fir::ReferenceType>(arg.getType ());
249224 if (refType && fir::isa_builtin_cptr_type (refType.getElementType ())) {
250225 converter.bindSymbol (sym, arg);
251226 } else {
252227 extVal.match (
253228 [&](const fir::BoxValue &v) {
254- converter.bindSymbol (sym,
255- fir::BoxValue (arg, cloneBounds (v.getLBounds ()),
256- v.getExplicitParameters (),
257- v.getExplicitExtents ()));
229+ converter.bindSymbol (sym, fir::BoxValue (arg, v.getLBounds (),
230+ v.getExplicitParameters (),
231+ v.getExplicitExtents ()));
258232 },
259233 [&](const fir::MutableBoxValue &v) {
260234 converter.bindSymbol (
261- sym, fir::MutableBoxValue (arg, cloneBounds ( v.getLBounds () ),
235+ sym, fir::MutableBoxValue (arg, v.getLBounds (),
262236 v.getMutableProperties ()));
263237 },
264238 [&](const fir::ArrayBoxValue &v) {
265- converter.bindSymbol (
266- sym, fir::ArrayBoxValue (arg, cloneBounds (v.getExtents ()),
267- cloneBounds (v.getLBounds ()),
268- v.getSourceBox ()));
239+ converter.bindSymbol (sym, fir::ArrayBoxValue (arg, v.getExtents (),
240+ v.getLBounds (),
241+ v.getSourceBox ()));
269242 },
270243 [&](const fir::CharArrayBoxValue &v) {
271- converter.bindSymbol (
272- sym, fir::CharArrayBoxValue (arg, cloneBound (v.getLen ()),
273- cloneBounds (v.getExtents ()),
274- cloneBounds (v.getLBounds ())));
244+ converter.bindSymbol (sym, fir::CharArrayBoxValue (arg, v.getLen (),
245+ v.getExtents (),
246+ v.getLBounds ()));
275247 },
276248 [&](const fir::CharBoxValue &v) {
277- // In some cases, v.len could reference the input to the
278- // hlfir.declare which is the corresponding v.addr. While this isn't
279- // a big problem by itself, it is desirable to extract this out of
280- // v.addr itself since it's first result will be of type
281- // fir.boxchar<>. For example, consider the following
282- //
283- // func.func private @_QFPrealtest(%arg0: !fir.boxchar<1>)
284- // %2 = fir.dummy_scope : !fir.dscope
285- // %3:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) ->
286- // (!fir.ref<!fir.char<1,?>>, index)
287- // %4:2 = hlfir.declare (%3#0, %3#1, %2):(!fir.ref<!fir.char<1,?>>,
288- // index,!fir.dscope) ->
289- // (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
290-
291- // In the case above,
292- // v.addr is
293- // %4:2 = hlfir.declare (%3#0, %3#1, %2):(!fir.ref<!fir.char<1,?>>,
294- // index,!fir.dscope) ->
295- // (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
296- // v.len is
297- // %3:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) ->
298- // (!fir.ref<!fir.char<1,?>>, index)
299-
300- // Mapping this to the target will create a use of %arg0 on the
301- // target. Since omp.target is IsolatedFromAbove, %arg0 will have to
302- // be mapped. Presently, OpenMP lowering of target barfs when it has
303- // to map a value that doesnt have a defining op. This can be fixed.
304- // Or we ensure that v.len is fir.unboxchar %4#0 which will
305- // cause %4#1 to be used on the target and consequently be
306- // mapped to the target. As such then, there wont be any use of the
307- // block argument %arg0 on the target.
308-
309- mlir::Value len = v.getLen ();
310- if (auto declareOp = val.getDefiningOp <hlfir::DeclareOp>()) {
311- mlir::Value base = declareOp.getBase ();
312- if (auto boxCharType =
313- mlir::dyn_cast<fir::BoxCharType>(base.getType ())) {
314- mlir::Type lenType = firOpBuilder.getCharacterLengthType ();
315- mlir::Type refType =
316- firOpBuilder.getRefType (boxCharType.getEleTy ());
317- mlir::Location loc = converter.getCurrentLocation ();
318- auto unboxed = firOpBuilder.create <fir::UnboxCharOp>(
319- loc, refType, lenType, base);
320- len = unboxed.getResult (1 );
321- }
322- }
323- auto charBoxValue = fir::CharBoxValue (arg, cloneBound (len));
324- converter.bindSymbol (sym, charBoxValue);
249+ converter.bindSymbol (sym, fir::CharBoxValue (arg, v.getLen ()));
325250 },
326251 [&](const fir::UnboxedValue &v) { converter.bindSymbol (sym, arg); },
327252 [&](const auto &) {
@@ -333,7 +258,6 @@ static void bindEntryBlockArgs(lower::AbstractConverter &converter,
333258
334259 auto bindMapLike =
335260 [&bindSingleMapLike](llvm::ArrayRef<const semantics::Symbol *> syms,
336- llvm::ArrayRef<mlir::Value> vars,
337261 llvm::ArrayRef<mlir::BlockArgument> args) {
338262 // Structure component symbols don't have bindings, and can only be
339263 // explicitly mapped individually. If a member is captured implicitly
@@ -342,8 +266,8 @@ static void bindEntryBlockArgs(lower::AbstractConverter &converter,
342266 llvm::copy_if (syms, std::back_inserter (processedSyms),
343267 [](auto *sym) { return !sym->owner ().IsDerivedType (); });
344268
345- for (auto [sym, var, arg] : llvm::zip_equal (processedSyms, vars , args))
346- bindSingleMapLike (*sym, var, arg);
269+ for (auto [sym, arg] : llvm::zip_equal (processedSyms, args))
270+ bindSingleMapLike (*sym, arg);
347271 };
348272
349273 auto bindPrivateLike = [&converter, &firOpBuilder](
@@ -374,20 +298,17 @@ static void bindEntryBlockArgs(lower::AbstractConverter &converter,
374298 // Process in clause name alphabetical order to match block arguments order.
375299 // Do not bind host_eval variables because they cannot be used inside of the
376300 // corresponding region, except for very specific cases handled separately.
377- bindMapLike (args.hasDeviceAddr .syms , args.hasDeviceAddr .vars ,
378- op.getHasDeviceAddrBlockArgs ());
301+ bindMapLike (args.hasDeviceAddr .syms , op.getHasDeviceAddrBlockArgs ());
379302 bindPrivateLike (args.inReduction .syms , args.inReduction .vars ,
380303 op.getInReductionBlockArgs ());
381- bindMapLike (args.map .syms , args. map . vars , op.getMapBlockArgs ());
304+ bindMapLike (args.map .syms , op.getMapBlockArgs ());
382305 bindPrivateLike (args.priv .syms , args.priv .vars , op.getPrivateBlockArgs ());
383306 bindPrivateLike (args.reduction .syms , args.reduction .vars ,
384307 op.getReductionBlockArgs ());
385308 bindPrivateLike (args.taskReduction .syms , args.taskReduction .vars ,
386309 op.getTaskReductionBlockArgs ());
387- bindMapLike (args.useDeviceAddr .syms , args.useDeviceAddr .vars ,
388- op.getUseDeviceAddrBlockArgs ());
389- bindMapLike (args.useDevicePtr .syms , args.useDevicePtr .vars ,
390- op.getUseDevicePtrBlockArgs ());
310+ bindMapLike (args.useDeviceAddr .syms , op.getUseDeviceAddrBlockArgs ());
311+ bindMapLike (args.useDevicePtr .syms , op.getUseDevicePtrBlockArgs ());
391312}
392313
393314// / Get the list of base values that the specified map-like variables point to.
@@ -1427,14 +1348,13 @@ static void genBodyOfTargetOp(
14271348 while (!valuesDefinedAbove.empty ()) {
14281349 for (mlir::Value val : valuesDefinedAbove) {
14291350 mlir::Operation *valOp = val.getDefiningOp ();
1430- assert (valOp != nullptr );
14311351
14321352 // NOTE: We skip BoxDimsOp's as the lesser of two evils is to map the
14331353 // indices separately, as the alternative is to eventually map the Box,
14341354 // which comes with a fairly large overhead comparatively. We could be
14351355 // more robust about this and check using a BackwardsSlice to see if we
14361356 // run the risk of mapping a box.
1437- if (mlir::isMemoryEffectFree (valOp) &&
1357+ if (valOp && mlir::isMemoryEffectFree (valOp) &&
14381358 !mlir::isa<fir::BoxDimsOp>(valOp)) {
14391359 mlir::Operation *clonedOp = valOp->clone ();
14401360 entryBlock->push_front (clonedOp);
@@ -1447,7 +1367,13 @@ static void genBodyOfTargetOp(
14471367 valOp->replaceUsesWithIf (clonedOp, replace);
14481368 } else {
14491369 auto savedIP = firOpBuilder.getInsertionPoint ();
1450- firOpBuilder.setInsertionPointAfter (valOp);
1370+
1371+ if (valOp)
1372+ firOpBuilder.setInsertionPointAfter (valOp);
1373+ else
1374+ // This means val is a block argument
1375+ firOpBuilder.setInsertionPoint (targetOp);
1376+
14511377 auto copyVal =
14521378 firOpBuilder.createTemporary (val.getLoc (), val.getType ());
14531379 firOpBuilder.createStoreWithConvert (copyVal.getLoc (), val, copyVal);
0 commit comments