| 1 | //===- MarkDeclareTarget.cpp ----------------------------------------------===// |
| 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 | // Mark functions called from explicit target code as implicitly declare target. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Dialect/OpenMP/OpenMPDialect.h" |
| 14 | #include "mlir/IR/Operation.h" |
| 15 | #include "mlir/IR/SymbolTable.h" |
| 16 | #include "mlir/Interfaces/FunctionInterfaces.h" |
| 17 | #include "mlir/Pass/Pass.h" |
| 18 | #include "mlir/Support/LLVM.h" |
| 19 | #include "llvm/ADT/StringMap.h" |
| 20 | #include "llvm/ADT/StringSet.h" |
| 21 | #include "llvm/ADT/TypeSwitch.h" |
| 22 | |
| 23 | namespace mlir { |
| 24 | namespace omp { |
| 25 | |
| 26 | #define GEN_PASS_DEF_MARKDECLARETARGETPASS |
| 27 | #include "mlir/Dialect/OpenMP/Transforms/Passes.h.inc" |
| 28 | |
| 29 | } // namespace omp |
| 30 | } // namespace mlir |
| 31 | |
| 32 | using namespace mlir; |
| 33 | |
| 34 | /// Check whether the given operation is located inside of an \c omp.target. |
| 35 | static bool isInTargetRegion(Operation &op) { |
| 36 | // TODO: Detection of callees inside of a target region might need an update |
| 37 | // once reverse offloading is implemented. |
| 38 | // Reverse offload target regions would then have to propagate the "host" |
| 39 | // device type. |
| 40 | return op.getParentOfType<omp::TargetOp>(); |
| 41 | } |
| 42 | |
| 43 | /// Add to \c callees all names of the functions called from regions owned by |
| 44 | /// \c op. If \c targetCallees is provided, split non-target and target uses |
| 45 | /// between these two output sets. |
| 46 | static void gatherNestedCallees(Operation &op, llvm::StringSet<> &callees, |
| 47 | llvm::StringSet<> *targetCallees = nullptr) { |
| 48 | op.walk(callback: [&](CallOpInterface callOp) { |
| 49 | CallInterfaceCallable callable = callOp.getCallableForCallee(); |
| 50 | if (auto callableSymRef = dyn_cast<SymbolRefAttr>(Val&: callable)) { |
| 51 | StringRef callee = callableSymRef.getLeafReference(); |
| 52 | if (targetCallees && isInTargetRegion(op&: *callOp)) |
| 53 | targetCallees->insert(key: callee); |
| 54 | else |
| 55 | callees.insert(key: callee); |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | /// Extract from \c arrayAttr and into \c syms the list of symbol names stored |
| 61 | /// in the attribute. |
| 62 | static void gatherSymsFromAttr(ArrayAttr arrayAttr, llvm::StringSet<> &syms) { |
| 63 | if (!arrayAttr) |
| 64 | return; |
| 65 | |
| 66 | for (Attribute attr : arrayAttr) |
| 67 | if (auto symbolRefAttr = dyn_cast<SymbolRefAttr>(Val&: attr)) |
| 68 | syms.insert(key: symbolRefAttr.getLeafReference()); |
| 69 | } |
| 70 | |
| 71 | /// Go through all OpenMP dialect operations located in regions owned by \c op |
| 72 | /// looking for symbol references to \c accomp::RecipeInterface or |
| 73 | /// \c FunctionOpInterface operations and, based on whether they are located |
| 74 | /// within a nested \c omp.target region, add them to the corresponding output |
| 75 | /// \c StringSet. |
| 76 | static void gatherNestedSymbolUses(Operation &op, |
| 77 | llvm::StringSet<> &nestedRecipeUses, |
| 78 | llvm::StringSet<> &targetRecipeUses, |
| 79 | llvm::StringSet<> &nestedFunctionUses, |
| 80 | llvm::StringSet<> &targetFunctionUses) { |
| 81 | op.walk(callback: [&](Operation *op) { |
| 82 | bool inTarget = isInTargetRegion(op&: *op); |
| 83 | llvm::StringSet<> &recipeUses = |
| 84 | inTarget ? targetRecipeUses : nestedRecipeUses; |
| 85 | llvm::StringSet<> &functionUses = |
| 86 | inTarget ? targetFunctionUses : nestedFunctionUses; |
| 87 | |
| 88 | // Handle each op holding clauses linked to a recipe op separately. This |
| 89 | // must be kept in sync with dialect changes. |
| 90 | llvm::TypeSwitch<Operation &>(*op) |
| 91 | .Case(caseFn: [&](omp::DistributeOp op) { |
| 92 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 93 | }) |
| 94 | .Case(caseFn: [&](omp::LoopOp op) { |
| 95 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 96 | gatherSymsFromAttr(arrayAttr: op.getReductionSymsAttr(), syms&: recipeUses); |
| 97 | }) |
| 98 | .Case(caseFn: [&](omp::MapInfoOp op) { |
| 99 | if (FlatSymbolRefAttr mapperAttr = op.getMapperIdAttr()) |
| 100 | recipeUses.insert(key: mapperAttr.getValue()); |
| 101 | }) |
| 102 | .Case(caseFn: [&](omp::ParallelOp op) { |
| 103 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 104 | gatherSymsFromAttr(arrayAttr: op.getReductionSymsAttr(), syms&: recipeUses); |
| 105 | }) |
| 106 | .Case(caseFn: [&](omp::ScopeOp op) { |
| 107 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 108 | gatherSymsFromAttr(arrayAttr: op.getReductionSymsAttr(), syms&: recipeUses); |
| 109 | }) |
| 110 | .Case(caseFn: [&](omp::SectionsOp op) { |
| 111 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 112 | gatherSymsFromAttr(arrayAttr: op.getReductionSymsAttr(), syms&: recipeUses); |
| 113 | }) |
| 114 | .Case(caseFn: [&](omp::SimdOp op) { |
| 115 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 116 | gatherSymsFromAttr(arrayAttr: op.getReductionSymsAttr(), syms&: recipeUses); |
| 117 | }) |
| 118 | .Case(caseFn: [&](omp::SingleOp op) { |
| 119 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 120 | // This goes directly to the called functions, as it's pointing to a |
| 121 | // function, not a recipe op. |
| 122 | gatherSymsFromAttr(arrayAttr: op.getCopyprivateSymsAttr(), syms&: functionUses); |
| 123 | }) |
| 124 | .Case(caseFn: [&](omp::TargetOp op) { |
| 125 | // omp.private is inlined inside of the target region, hence we need |
| 126 | // to add it with the target uses rather than base it on context. |
| 127 | // TODO: The reverse-offload case would require adding it to |
| 128 | // nestedRecipeUses. |
| 129 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: targetRecipeUses); |
| 130 | gatherSymsFromAttr(arrayAttr: op.getInReductionSymsAttr(), syms&: recipeUses); |
| 131 | }) |
| 132 | .Case(caseFn: [&](omp::TaskgroupOp op) { |
| 133 | gatherSymsFromAttr(arrayAttr: op.getTaskReductionSymsAttr(), syms&: recipeUses); |
| 134 | }) |
| 135 | .Case(caseFn: [&](omp::TaskloopContextOp op) { |
| 136 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 137 | gatherSymsFromAttr(arrayAttr: op.getReductionSymsAttr(), syms&: recipeUses); |
| 138 | gatherSymsFromAttr(arrayAttr: op.getInReductionSymsAttr(), syms&: recipeUses); |
| 139 | }) |
| 140 | .Case(caseFn: [&](omp::TaskOp op) { |
| 141 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 142 | gatherSymsFromAttr(arrayAttr: op.getInReductionSymsAttr(), syms&: recipeUses); |
| 143 | }) |
| 144 | .Case(caseFn: [&](omp::TeamsOp op) { |
| 145 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 146 | gatherSymsFromAttr(arrayAttr: op.getReductionSymsAttr(), syms&: recipeUses); |
| 147 | }) |
| 148 | .Case(caseFn: [&](omp::WsloopOp op) { |
| 149 | gatherSymsFromAttr(arrayAttr: op.getPrivateSymsAttr(), syms&: recipeUses); |
| 150 | gatherSymsFromAttr(arrayAttr: op.getReductionSymsAttr(), syms&: recipeUses); |
| 151 | }); |
| 152 | }); |
| 153 | } |
| 154 | |
| 155 | namespace { |
| 156 | |
| 157 | class MarkDeclareTargetPass |
| 158 | : public omp::impl::MarkDeclareTargetPassBase<MarkDeclareTargetPass> { |
| 159 | |
| 160 | // This pass executes on mlir::ModuleOp, marking functions contained within |
| 161 | // as implicitly declare target if they are called from within an explicitly |
| 162 | // marked declare target function or a target region (TargetOp), or |
| 163 | // transitively through recipe ops (e.g. omp.declare_reduction, omp.private) |
| 164 | // or other function calls. |
| 165 | void runOnOperation() override { |
| 166 | // Illegal as an MLIR symbol name to avoid collisions. Used to gather all |
| 167 | // calls from within omp.target regions as a single "function". |
| 168 | constexpr const static ::llvm::StringLiteral kTargetRegionsSymName = |
| 169 | "omp targets" ; |
| 170 | |
| 171 | ModuleOp modOp = getOperation(); |
| 172 | |
| 173 | // Gather and store the set of called functions by each recipe. |
| 174 | // TODO: This doesn't currently support recipe ops holding references to |
| 175 | // other recipe ops. |
| 176 | llvm::StringMap<llvm::StringSet<>> calls; |
| 177 | for (auto recipeOp : modOp.getOps<accomp::RecipeInterface>()) { |
| 178 | StringAttr recipeSymName; |
| 179 | if (auto symOp = dyn_cast<SymbolOpInterface>(Val&: *recipeOp)) |
| 180 | recipeSymName = symOp.getNameAttr(); |
| 181 | else if (auto privateOp = dyn_cast<omp::PrivateClauseOp>(Val&: *recipeOp)) |
| 182 | recipeSymName = privateOp.getSymNameAttr(); |
| 183 | |
| 184 | if (recipeSymName) { |
| 185 | llvm::StringSet<> recipeCalls; |
| 186 | gatherNestedCallees(op&: *recipeOp, callees&: recipeCalls); |
| 187 | calls[recipeSymName] = recipeCalls; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Gather and store the set of called functions by each function. |
| 192 | for (auto funcOp : modOp.getOps<FunctionOpInterface>()) { |
| 193 | llvm::StringSet<> functionCalls, targetCalls; |
| 194 | gatherNestedCallees(op&: *funcOp, callees&: functionCalls, targetCallees: &targetCalls); |
| 195 | |
| 196 | // Transitively include functions called from recipe op users, as if |
| 197 | // inlined. |
| 198 | llvm::StringSet<> recipeUses, targetRecipeUses; |
| 199 | gatherNestedSymbolUses(op&: *funcOp, nestedRecipeUses&: recipeUses, targetRecipeUses, |
| 200 | nestedFunctionUses&: functionCalls, targetFunctionUses&: targetCalls); |
| 201 | for (auto &recipe : recipeUses) { |
| 202 | const llvm::StringSet<> &recipeCalls = calls.at(Val: recipe.getKey()); |
| 203 | functionCalls.insert_range(R: recipeCalls); |
| 204 | } |
| 205 | for (auto &recipe : targetRecipeUses) { |
| 206 | const llvm::StringSet<> &recipeCalls = calls.at(Val: recipe.getKey()); |
| 207 | targetCalls.insert_range(R: recipeCalls); |
| 208 | } |
| 209 | |
| 210 | calls[funcOp.getName()] = functionCalls; |
| 211 | calls[kTargetRegionsSymName].insert_range(R&: targetCalls); |
| 212 | } |
| 213 | |
| 214 | // Create worklist with all functions that are directly reachable from |
| 215 | // declare_target functions or target regions. |
| 216 | llvm::SmallVector<std::pair<StringRef, omp::DeclareTargetDeviceType>> |
| 217 | worklist; |
| 218 | for (auto funcOp : getOperation().getOps<FunctionOpInterface>()) { |
| 219 | auto declareTargetOp = |
| 220 | llvm::dyn_cast<omp::DeclareTargetInterface>(Val: funcOp.getOperation()); |
| 221 | omp::DeclareTargetAttr declareTargetAttr = |
| 222 | declareTargetOp ? declareTargetOp.getDeclareTarget() : nullptr; |
| 223 | |
| 224 | if (!declareTargetAttr) |
| 225 | continue; |
| 226 | |
| 227 | // Add to the worklist all called functions with the declare_target |
| 228 | // information of this one, so it gets propagated. |
| 229 | for (auto &callee : calls[funcOp.getName()]) |
| 230 | worklist.push_back( |
| 231 | Elt: {callee.getKey(), declareTargetAttr.getDeviceType()}); |
| 232 | } |
| 233 | |
| 234 | // Add to the worklist all functions reached from target regions. |
| 235 | for (auto &callee : calls[kTargetRegionsSymName]) |
| 236 | worklist.push_back( |
| 237 | Elt: {callee.getKey(), omp::DeclareTargetDeviceType::nohost}); |
| 238 | |
| 239 | // Process the work list by propagating changes to other non-explicit |
| 240 | // declare_target functions based on the call graph, until no updates are |
| 241 | // left. |
| 242 | while (!worklist.empty()) { |
| 243 | std::pair<StringRef, omp::DeclareTargetDeviceType> workItem = |
| 244 | worklist.pop_back_val(); |
| 245 | auto funcOp = modOp.lookupSymbol<FunctionOpInterface>(name: workItem.first); |
| 246 | assert(funcOp && "a work item must point to an existing function" ); |
| 247 | |
| 248 | // Skip if the function is explicitly marked as declare_target or if it |
| 249 | // doesn't support the interface. We only want to propagate implicit |
| 250 | // declare_target information to functions for which the user hasn't |
| 251 | // specified an explicit behavior. |
| 252 | auto declareTargetOp = dyn_cast<omp::DeclareTargetInterface>(Val&: *funcOp); |
| 253 | omp::DeclareTargetAttr declareTargetAttr = |
| 254 | declareTargetOp ? declareTargetOp.getDeclareTarget() : nullptr; |
| 255 | if (!declareTargetOp || |
| 256 | (declareTargetAttr && !declareTargetAttr.getImplicit())) |
| 257 | continue; |
| 258 | |
| 259 | omp::DeclareTargetDeviceType changedDeviceType; |
| 260 | if (declareTargetAttr) { |
| 261 | // Implicit declare_target update. |
| 262 | omp::DeclareTargetDeviceType currentDeviceType = |
| 263 | declareTargetAttr.getDeviceType(); |
| 264 | |
| 265 | // Skip the update (and adding callees to the worklist) if the added |
| 266 | // info doesn't change anything. |
| 267 | if (currentDeviceType == omp::DeclareTargetDeviceType::any || |
| 268 | currentDeviceType == workItem.second) { |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | // Update intermediate information about this function. By the previous |
| 273 | // check, we know it's host + nohost = any. |
| 274 | changedDeviceType = omp::DeclareTargetDeviceType::any; |
| 275 | } else { |
| 276 | // No declare_target information present. |
| 277 | |
| 278 | // Prevent public and external functions from being restricted to a |
| 279 | // device. We don't have visibility over all their uses. |
| 280 | if (funcOp.isPublic() || funcOp.isExternal()) |
| 281 | changedDeviceType = omp::DeclareTargetDeviceType::any; |
| 282 | else |
| 283 | changedDeviceType = workItem.second; |
| 284 | } |
| 285 | |
| 286 | // Update the operation and add callees to the worklist to propagate it. |
| 287 | declareTargetOp.setDeclareTarget(deviceType: changedDeviceType, |
| 288 | captureClause: omp::DeclareTargetCaptureClause::to, |
| 289 | /*automap=*/false, /*implicit=*/true); |
| 290 | |
| 291 | for (auto &callee : calls[workItem.first]) |
| 292 | worklist.push_back(Elt: {callee.getKey(), changedDeviceType}); |
| 293 | } |
| 294 | } |
| 295 | }; |
| 296 | } // namespace |
| 297 | |