|
6 | 6 | // |
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | | -//#include "mlir/Dialect/LLVMIR/Transforms/OpenMPOffloadPrivatizationPrepare.h" |
10 | | -#include "mlir/Dialect/LLVMIR/Transforms/Passes.h" |
| 9 | +#include "mlir/Dialect/LLVMIR/Transforms/OpenMPOffloadPrivatizationPrepare.h" |
11 | 10 | #include "mlir/Analysis/SliceAnalysis.h" |
12 | 11 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
13 | 12 | #include "mlir/Dialect/LLVMIR/FunctionCallUtils.h" |
|
17 | 16 | #include "mlir/IR/Dominance.h" |
18 | 17 | #include "mlir/Pass/Pass.h" |
19 | 18 | #include "mlir/Support/LLVM.h" |
| 19 | +#include "llvm/Support/DebugLog.h" |
20 | 20 | #include <cstdint> |
21 | 21 | #include <utility> |
22 | 22 |
|
@@ -95,15 +95,31 @@ class PrepareForOMPOffloadPrivatizationPass |
95 | 95 |
|
96 | 96 | // Allocate heap memory that corresponds to the type of memory |
97 | 97 | // pointed to by varPtr |
98 | | - // TODO: For boxchars this likely wont be a pointer. |
| 98 | + // For boxchars this won't be a pointer. But, MapsForPrivatizedSymbols |
| 99 | + // should have mapped the pointer the boxchar so use that as varPtr. |
99 | 100 | Value varPtr = privVar; |
100 | | - Value heapMem = allocateHeapMem(targetOp, privVar, mod, rewriter); |
| 101 | + if (!isa<LLVM::LLVMPointerType>(privVar.getType())) |
| 102 | + varPtr = mapInfoOp.getVarPtr(); |
| 103 | + |
| 104 | + assert(isa<LLVM::LLVMPointerType>(varPtr.getType())); |
| 105 | + Value heapMem = allocateHeapMem(targetOp, varPtr, mod, rewriter); |
101 | 106 | if (!heapMem) |
102 | | - targetOp.emitError("Unable to allocate heap memory when try to move " |
103 | | - "a private variable out of the stack and into the " |
104 | | - "heap for use by a deferred target task"); |
| 107 | + targetOp.emitError( |
| 108 | + "Unable to allocate heap memory when trying to move " |
| 109 | + "a private variable out of the stack and into the " |
| 110 | + "heap for use by a deferred target task"); |
| 111 | + |
| 112 | + // The types of private vars should match before and after the |
| 113 | + // transformation. In particular, if the type is a pointer, |
| 114 | + // simply record the newly allocated malloc location as the |
| 115 | + // new private variable. If, however, the type is not a pointer |
| 116 | + // then, we need to load the value from the newly allocated |
| 117 | + // location. We'll inser that load later after we have updated |
| 118 | + // the malloc'd location with the contents of the original |
| 119 | + // variable. |
| 120 | + if (isa<LLVM::LLVMPointerType>(privVar.getType())) |
| 121 | + newPrivVars.push_back(heapMem); |
105 | 122 |
|
106 | | - newPrivVars.push_back(heapMem); |
107 | 123 | // Find the earliest insertion point for the copy. This will be before |
108 | 124 | // the first in the list of omp::MapInfoOp instances that use varPtr. |
109 | 125 | // After the copy these omp::MapInfoOp instances will refer to heapMem |
@@ -250,6 +266,18 @@ class PrepareForOMPOffloadPrivatizationPass |
250 | 266 | }); |
251 | 267 | rewriter.eraseOp(origOp); |
252 | 268 | } |
| 269 | + |
| 270 | + // If the type of the private variable is not a pointer, |
| 271 | + // which is typically the case with !fir.boxchar types, then |
| 272 | + // we need to ensure that the new private variable is also |
| 273 | + // not a pointer. Insert a load from heapMem right before |
| 274 | + // targetOp. |
| 275 | + if (!isa<LLVM::LLVMPointerType>(privVar.getType())) { |
| 276 | + rewriter.setInsertionPoint(targetOp); |
| 277 | + auto newPrivVar = rewriter.create<LLVM::LoadOp>(mapInfoOp.getLoc(), |
| 278 | + varType, heapMem); |
| 279 | + newPrivVars.push_back(newPrivVar); |
| 280 | + } |
253 | 281 | } |
254 | 282 | assert(newPrivVars.size() == privateVars.size() && |
255 | 283 | "The number of private variables must match before and after " |
@@ -358,7 +386,7 @@ class PrepareForOMPOffloadPrivatizationPass |
358 | 386 |
|
359 | 387 | template <typename OpTy> |
360 | 388 | Value allocateHeapMem(OpTy targetOp, Value privVar, ModuleOp mod, |
361 | | - IRRewriter &rewriter) const { |
| 389 | + IRRewriter &rewriter) const { |
362 | 390 | Value varPtr = privVar; |
363 | 391 | Operation *definingOp = varPtr.getDefiningOp(); |
364 | 392 | OpBuilder::InsertionGuard guard(rewriter); |
|
0 commit comments