[flang-commits] [PATCH] D129494: [flang] move getKindMapping() calls out of FIROpBuilder ctor calls

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Jul 11 08:41:30 PDT 2022


jeanPerier created this revision.
jeanPerier added reviewers: clementval, PeteSteinfeld.
jeanPerier added a project: Flang.
Herald added subscribers: mehdi_amini, jdoerfert.
Herald added a project: All.
jeanPerier requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129494

Files:
  flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
  flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp


Index: flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp
===================================================================
--- flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp
+++ flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp
@@ -809,7 +809,8 @@
         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 @@
   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 @@
   // 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 @@
       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 @@
         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);
     };
Index: flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
===================================================================
--- flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
+++ flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
@@ -221,7 +221,8 @@
           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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129494.443659.patch
Type: text/x-patch
Size: 3920 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220711/a96e52ee/attachment.bin>


More information about the flang-commits mailing list