|
33 | 33 | #include "flang/Parser/parse-tree.h"
|
34 | 34 | #include "flang/Semantics/openmp-directive-sets.h"
|
35 | 35 | #include "flang/Semantics/tools.h"
|
| 36 | +#include "mlir/Analysis/SliceAnalysis.h" |
36 | 37 | #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
|
37 | 38 | #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
|
38 | 39 | #include "mlir/Transforms/RegionUtils.h"
|
@@ -123,9 +124,100 @@ class HostClausesInsertionGuard {
|
123 | 124 | mlir::OpBuilder::InsertPoint ip;
|
124 | 125 | mlir::omp::TargetOp targetOp;
|
125 | 126 |
|
| 127 | + // Finds the list of op operands that escape the target op's region; that is: |
| 128 | + // the operands that are used outside the target op but defined inside it. |
| 129 | + void |
| 130 | + findEscapingOpOperands(llvm::DenseSet<mlir::OpOperand *> &escapingOperands) { |
| 131 | + if (!targetOp) |
| 132 | + return; |
| 133 | + |
| 134 | + mlir::Region *targetParentRegion = targetOp->getParentRegion(); |
| 135 | + assert(targetParentRegion != nullptr && |
| 136 | + "Expected omp.target op to be nested in a parent region."); |
| 137 | + |
| 138 | + // Walk the parent region in pre-order to make sure we visit `targetOp` |
| 139 | + // before its nested ops. |
| 140 | + targetParentRegion->walk<mlir::WalkOrder::PreOrder>( |
| 141 | + [&](mlir::Operation *op) { |
| 142 | + // Once we come across `targetOp`, we interrupt the walk since we |
| 143 | + // already visited all the ops that come before it in the region. |
| 144 | + if (op == targetOp) |
| 145 | + return mlir::WalkResult::interrupt(); |
| 146 | + |
| 147 | + for (mlir::OpOperand &operand : op->getOpOperands()) { |
| 148 | + mlir::Operation *operandDefiningOp = operand.get().getDefiningOp(); |
| 149 | + |
| 150 | + if (operandDefiningOp == nullptr) |
| 151 | + continue; |
| 152 | + |
| 153 | + auto parentTargetOp = |
| 154 | + operandDefiningOp->getParentOfType<mlir::omp::TargetOp>(); |
| 155 | + |
| 156 | + if (parentTargetOp != targetOp) |
| 157 | + continue; |
| 158 | + |
| 159 | + escapingOperands.insert(&operand); |
| 160 | + } |
| 161 | + |
| 162 | + return mlir::WalkResult::advance(); |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + // For an escaping operand, clone its use-def chain (i.e. its backward slice) |
| 167 | + // outside the target region. |
| 168 | + // |
| 169 | + // \return the last op in the chain (this is the op that defines the escaping |
| 170 | + // operand). |
| 171 | + mlir::Operation * |
| 172 | + cloneOperandSliceOutsideTargetOp(mlir::OpOperand *escapingOperand) { |
| 173 | + mlir::Operation *operandDefiningOp = escapingOperand->get().getDefiningOp(); |
| 174 | + llvm::SetVector<mlir::Operation *> backwardSlice; |
| 175 | + mlir::BackwardSliceOptions sliceOptions; |
| 176 | + sliceOptions.inclusive = true; |
| 177 | + mlir::getBackwardSlice(operandDefiningOp, &backwardSlice, sliceOptions); |
| 178 | + |
| 179 | + auto ip = builder.saveInsertionPoint(); |
| 180 | + |
| 181 | + mlir::IRMapping mapper; |
| 182 | + builder.setInsertionPoint(escapingOperand->getOwner()); |
| 183 | + mlir::Operation *lastSliceOp; |
| 184 | + |
| 185 | + for (auto *op : backwardSlice) |
| 186 | + lastSliceOp = builder.clone(*op, mapper); |
| 187 | + |
| 188 | + builder.restoreInsertionPoint(ip); |
| 189 | + return lastSliceOp; |
| 190 | + } |
| 191 | + |
126 | 192 | /// Fixup any uses of target region block arguments that we have just created
|
127 | 193 | /// outside of the target region, and replace them by their host values.
|
128 | 194 | void fixupExtractedHostOps() {
|
| 195 | + llvm::DenseSet<mlir::OpOperand *> escapingOperands; |
| 196 | + findEscapingOpOperands(escapingOperands); |
| 197 | + |
| 198 | + for (mlir::OpOperand *operand : escapingOperands) { |
| 199 | + mlir::Operation *operandDefiningOp = operand->get().getDefiningOp(); |
| 200 | + assert(operandDefiningOp != nullptr && |
| 201 | + "Expected escaping operand to have a defining op (i.e. not to be " |
| 202 | + "a block argument)"); |
| 203 | + mlir::Operation *lastSliceOp = cloneOperandSliceOutsideTargetOp(operand); |
| 204 | + |
| 205 | + // Find the index of the operand in the list of results produced by its |
| 206 | + // defining op. |
| 207 | + unsigned operandResultIdx = 0; |
| 208 | + for (auto [idx, res] : llvm::enumerate(operandDefiningOp->getResults())) { |
| 209 | + if (res == operand->get()) { |
| 210 | + operandResultIdx = idx; |
| 211 | + break; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + // Replace the escaping operand with the corresponding value from the |
| 216 | + // op that we cloned outside the target op. |
| 217 | + operand->getOwner()->setOperand(operand->getOperandNumber(), |
| 218 | + lastSliceOp->getResult(operandResultIdx)); |
| 219 | + } |
| 220 | + |
129 | 221 | auto useOutsideTargetRegion = [](mlir::OpOperand &operand) {
|
130 | 222 | if (mlir::Operation *owner = operand.getOwner())
|
131 | 223 | return !owner->getParentOfType<mlir::omp::TargetOp>();
|
|
0 commit comments