[llvm] [Offload]: Skip copying of unused kernel-mapped data (PR #124723)

via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 13 12:43:25 PST 2025


================
@@ -1197,6 +1197,82 @@ class PrivateArgumentManagerTy {
   }
 };
 
+/// Try to find redundant mappings associated with a kernel launch,
+/// and provide a masked version of the kernel argument types that
+/// avoid redundant to data transfers between the host and device.
+static std::unique_ptr<int64_t[]>
+maskRedundantTransfers(DeviceTy &Device, int32_t ArgNum, int64_t *ArgTypes,
+                       int64_t *ArgSizes, map_var_info_t *ArgNames,
+                       void **ArgPtrs, void **ArgMappers) {
+  std::unique_ptr<int64_t[]> ArgTypesOverride =
+      std::make_unique<int64_t[]>(ArgNum);
+
+  MappingInfoTy &MappingInfo = Device.getMappingInfo();
+  MappingInfoTy::HDTTMapAccessorTy HDTTMap =
+      MappingInfo.HostDataToTargetMap.getExclusiveAccessor();
+
+  int64_t UnusedArgs = 0;
+
+  for (int32_t I = 0; I < ArgNum; ++I) {
+    tgt_map_type ArgType = (tgt_map_type)ArgTypes[I];
+
+    // Check for unused implicit mappings
+    bool IsArgUnused = ArgType == OMP_TGT_MAPTYPE_NONE;
+
+    // Check for unused `map(buf[0:size])` mappings
+    IsArgUnused |= ArgType == OMP_TGT_MAPTYPE_FROM ||
+                   ArgType == OMP_TGT_MAPTYPE_TO ||
+                   ArgType == (OMP_TGT_MAPTYPE_FROM | OMP_TGT_MAPTYPE_TO);
+
+    // Check for unused `map(wrapper.buf[0:size])` mappings
+    IsArgUnused |= UnusedArgs == ArgNum - 1 &&
+                   ArgType & OMP_TGT_MAPTYPE_MEMBER_OF &&
+                   ((ArgType & ~OMP_TGT_MAPTYPE_MEMBER_OF) ==
+                        OMP_TGT_MAPTYPE_PTR_AND_OBJ ||
+                    (ArgType & ~OMP_TGT_MAPTYPE_MEMBER_OF) ==
+                        (OMP_TGT_MAPTYPE_PTR_AND_OBJ | OMP_TGT_MAPTYPE_TO) ||
+                    (ArgType & ~OMP_TGT_MAPTYPE_MEMBER_OF) ==
+                        (OMP_TGT_MAPTYPE_PTR_AND_OBJ | OMP_TGT_MAPTYPE_FROM |
+                         OMP_TGT_MAPTYPE_TO));
+
+    bool IsExistingMapping =
+        !MappingInfo.lookupMapping(HDTTMap, ArgPtrs[I], ArgSizes[I]).isEmpty();
+
+    bool IsCustomMapped = ArgMappers && ArgMappers[I];
+
+    if (IsExistingMapping | IsCustomMapped | !IsArgUnused) {
+      ArgTypesOverride[I] = ArgTypes[I];
+      continue;
+    }
+
+    const std::string Name = ArgNames && ArgNames[I]
+                                 ? getNameFromMapping(ArgNames[I])
+                                 : std::string("unknown");
+
+    bool IsArgFrom = ArgType & OMP_TGT_MAPTYPE_FROM;
+    bool IsArgTo = ArgType & OMP_TGT_MAPTYPE_TO;
+
+    const char *Type = IsArgFrom && IsArgTo ? "tofrom"
+                       : IsArgFrom          ? "from"
+                       : IsArgTo            ? "to"
+                                            : "unknown";
+
+    // Optimisation:
+    // A new mapping is not used by the kernel.
+    // Change the type such that no data is transferred to and/or from the
+    // device.
----------------
pradt2 wrote:

Done

https://github.com/llvm/llvm-project/pull/124723


More information about the llvm-commits mailing list