[flang] move getKindMapping() calls out of FIROpBuilder ctor calls

FirOpBuilder takes a fir::KindMapping reference. When the getKindMapping()
call is made inside the ctor call, the lifetime of this reference may
be as short as the ctor call (at least with when building flang in
release mode with clang 8). This can cause segfaults when later using
the FirOpBuilder.

Ensure the kindMap passed to the FirOpBuilder ctor is the same as the
FirOpBuilder.

Differential Revision: https://reviews.llvm.org/D129494
This commit is contained in:
Jean Perier 2022-07-12 09:22:13 +02:00
parent 0fbb0ca810
commit 906784a399
3 changed files with 18 additions and 10 deletions

View File

@ -221,7 +221,8 @@ public:
if (embox.getHost()) {
// Create the thunk.
auto module = embox->getParentOfType<mlir::ModuleOp>();
FirOpBuilder builder(rewriter, getKindMapping(module));
fir::KindMapping kindMap = getKindMapping(module);
FirOpBuilder builder(rewriter, kindMap);
auto loc = embox.getLoc();
mlir::Type i8Ty = builder.getI8Type();
mlir::Type i8Ptr = builder.getRefType(i8Ty);

View File

@ -294,7 +294,8 @@ public:
}
mlir::Type funcPointerType = tuple.getType(0);
mlir::Type lenType = tuple.getType(1);
fir::FirOpBuilder builder(*rewriter, fir::getKindMapping(module));
fir::KindMapping kindMap = fir::getKindMapping(module);
fir::FirOpBuilder builder(*rewriter, kindMap);
auto [funcPointer, len] =
fir::factory::extractCharacterProcedureTuple(builder, loc,
oper);
@ -697,8 +698,8 @@ public:
func.front().addArgument(trailingTys[fixup.second], loc);
auto tupleType = oldArgTys[fixup.index - offset];
rewriter->setInsertionPointToStart(&func.front());
fir::FirOpBuilder builder(*rewriter,
fir::getKindMapping(getModule()));
fir::KindMapping kindMap = fir::getKindMapping(getModule());
fir::FirOpBuilder builder(*rewriter, kindMap);
auto tuple = fir::factory::createCharacterProcedureTuple(
builder, loc, tupleType, newProcPointerArg, newLenArg);
func.getArgument(fixup.index + 1).replaceAllUsesWith(tuple);

View File

@ -809,7 +809,8 @@ static bool getAdjustedExtents(mlir::Location loc,
auto triples = sliceOp.getTriples();
const std::size_t tripleSize = triples.size();
auto module = arrLoad->getParentOfType<mlir::ModuleOp>();
FirOpBuilder builder(rewriter, getKindMapping(module));
fir::KindMapping kindMap = getKindMapping(module);
FirOpBuilder builder(rewriter, kindMap);
size = builder.genExtentFromTriplet(loc, triples[tripleSize - 3],
triples[tripleSize - 2],
triples[tripleSize - 1], idxTy);
@ -895,7 +896,8 @@ static mlir::Value genCoorOp(mlir::PatternRewriter &rewriter,
assert(seqTy && seqTy.isa<SequenceType>());
const auto dimension = seqTy.cast<SequenceType>().getDimension();
auto module = load->getParentOfType<mlir::ModuleOp>();
FirOpBuilder builder(rewriter, getKindMapping(module));
fir::KindMapping kindMap = getKindMapping(module);
FirOpBuilder builder(rewriter, kindMap);
auto typeparams = getTypeParamsIfRawData(loc, builder, load, alloc.getType());
mlir::Value result = rewriter.create<ArrayCoorOp>(
loc, eleTy, alloc, shape, slice,
@ -959,7 +961,8 @@ void genArrayCopy(mlir::Location loc, mlir::PatternRewriter &rewriter,
// Reverse the indices so they are in column-major order.
std::reverse(indices.begin(), indices.end());
auto module = arrLoad->getParentOfType<mlir::ModuleOp>();
FirOpBuilder builder(rewriter, getKindMapping(module));
fir::KindMapping kindMap = getKindMapping(module);
FirOpBuilder builder(rewriter, kindMap);
auto fromAddr = rewriter.create<ArrayCoorOp>(
loc, getEleTy(src.getType()), src, shapeOp,
CopyIn && copyUsingSlice ? sliceOp : mlir::Value{},
@ -997,7 +1000,8 @@ genArrayLoadTypeParameters(mlir::Location loc, mlir::PatternRewriter &rewriter,
if (auto charTy = eleTy.dyn_cast<CharacterType>()) {
assert(load.getMemref().getType().isa<BoxType>());
auto module = load->getParentOfType<mlir::ModuleOp>();
FirOpBuilder builder(rewriter, getKindMapping(module));
fir::KindMapping kindMap = getKindMapping(module);
FirOpBuilder builder(rewriter, kindMap);
return {getCharacterLen(loc, builder, load, charTy)};
}
TODO(loc, "unhandled dynamic type parameters");
@ -1049,12 +1053,14 @@ allocateArrayTemp(mlir::Location loc, mlir::PatternRewriter &rewriter,
loc, fir::BoxType::get(baseType), allocmem, shape,
/*slice=*/mlir::Value{}, typeParams);
auto module = load->getParentOfType<mlir::ModuleOp>();
FirOpBuilder builder(rewriter, getKindMapping(module));
fir::KindMapping kindMap = getKindMapping(module);
FirOpBuilder builder(rewriter, kindMap);
runtime::genDerivedTypeInitialize(builder, loc, box);
// Any allocatable component that may have been allocated must be
// deallocated during the clean-up.
auto cleanup = [=](mlir::PatternRewriter &r) {
FirOpBuilder builder(r, getKindMapping(module));
fir::KindMapping kindMap = getKindMapping(module);
FirOpBuilder builder(r, kindMap);
runtime::genDerivedTypeDestroy(builder, loc, box);
r.create<FreeMemOp>(loc, allocmem);
};