Skip to content

Commit f4a13ea

Browse files
Handle boxchars
1 parent f7dacb3 commit f4a13ea

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

mlir/lib/Dialect/LLVMIR/Transforms/OpenMPOffloadPrivatizationPrepare.cpp

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
//#include "mlir/Dialect/LLVMIR/Transforms/OpenMPOffloadPrivatizationPrepare.h"
10-
#include "mlir/Dialect/LLVMIR/Transforms/Passes.h"
9+
#include "mlir/Dialect/LLVMIR/Transforms/OpenMPOffloadPrivatizationPrepare.h"
1110
#include "mlir/Analysis/SliceAnalysis.h"
1211
#include "mlir/Dialect/Func/IR/FuncOps.h"
1312
#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
@@ -17,6 +16,7 @@
1716
#include "mlir/IR/Dominance.h"
1817
#include "mlir/Pass/Pass.h"
1918
#include "mlir/Support/LLVM.h"
19+
#include "llvm/Support/DebugLog.h"
2020
#include <cstdint>
2121
#include <utility>
2222

@@ -95,15 +95,31 @@ class PrepareForOMPOffloadPrivatizationPass
9595

9696
// Allocate heap memory that corresponds to the type of memory
9797
// 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.
99100
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);
101106
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);
105122

106-
newPrivVars.push_back(heapMem);
107123
// Find the earliest insertion point for the copy. This will be before
108124
// the first in the list of omp::MapInfoOp instances that use varPtr.
109125
// After the copy these omp::MapInfoOp instances will refer to heapMem
@@ -250,6 +266,18 @@ class PrepareForOMPOffloadPrivatizationPass
250266
});
251267
rewriter.eraseOp(origOp);
252268
}
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+
}
253281
}
254282
assert(newPrivVars.size() == privateVars.size() &&
255283
"The number of private variables must match before and after "
@@ -358,7 +386,7 @@ class PrepareForOMPOffloadPrivatizationPass
358386

359387
template <typename OpTy>
360388
Value allocateHeapMem(OpTy targetOp, Value privVar, ModuleOp mod,
361-
IRRewriter &rewriter) const {
389+
IRRewriter &rewriter) const {
362390
Value varPtr = privVar;
363391
Operation *definingOp = varPtr.getDefiningOp();
364392
OpBuilder::InsertionGuard guard(rewriter);

0 commit comments

Comments
 (0)