[clang] [flang] [flang] Add support for -finit-local-zero (PR #159788)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 28 00:59:24 PDT 2026


================
@@ -1225,6 +1226,69 @@ getSafeRepackAttrs(Fortran::lower::AbstractConverter &converter) {
   return attrs.empty() ? mlir::ArrayAttr{} : builder.getArrayAttr(attrs);
 }
 
+// Helper function to related to emission of implicit
+// assignments. `Implicit` here implies the assignment does not
+// exist in the Fortran source, but is implicit through definition
+// of one or more flagsets (like -finit-* family of flags).
+// General purpose usage of this function outside the
+// scope detailed here is discouraged, and is probably wrong.
+static void emitImplicitAssignment(Fortran::lower::AbstractConverter &converter,
+                                   mlir::Location loc,
+                                   const Fortran::semantics::Symbol &sym,
+                                   Fortran::lower::SymMap &symMap) {
+  if (converter.getLoweringOptions().getInitLocalZeroDef()) {
+    mlir::Type eleTy = hlfir::getFortranElementType(converter.genType(sym));
+    auto *builder = &converter.getFirOpBuilder();
+
+    if (mlir::isa<fir::CharacterType>(eleTy)) {
+      fir::ExtendedValue ext = converter.getSymbolExtendedValue(sym);
+      const auto *charBox = ext.getCharBox();
+      mlir::Value buffer = charBox->getBuffer();
+      assert(buffer && "CharBox buffer is null");
+      auto eleTy = fir::unwrapRefType(buffer.getType());
+      auto charTy =
+          mlir::cast<fir::CharacterType>(fir::unwrapSequenceType(eleTy));
+      unsigned kindBytes =
+          builder->getKindMap().getCharacterBitsize(charTy.getFKind()) / 8;
+      auto lenVal = charBox->getLen();
+      mlir::Value byteLen;
+      if (lenVal) {
+
+        mlir::Value lenI64 =
+            builder->createConvert(loc, builder->getI64Type(), lenVal);
+        mlir::Value kind = builder->createIntegerConstant(
+            loc, builder->getI64Type(), kindBytes);
+        byteLen = mlir::arith::MulIOp::create(*builder, loc, lenI64, kind);
+        byteLen = builder->createConvert(loc, builder->getI64Type(), byteLen);
+      } else {
+
+        assert(charTy.hasConstantLen() && "expected constant character length");
+        byteLen = builder->createIntegerConstant(loc, builder->getI64Type(),
+                                                 charTy.getLen() * kindBytes);
+      }
+
+      auto ptrTy = mlir::LLVM::LLVMPointerType::get(builder->getContext());
+      mlir::Value ptr = builder->createConvert(loc, ptrTy, buffer);
+      mlir::Value zero =
+          builder->createIntegerConstant(loc, builder->getI8Type(), 0);
+      mlir::ModuleOp mod = builder->getModule();
+      mlir::OpBuilder modBuilder(mod.getBodyRegion());
+      modBuilder.setInsertionPointToEnd(zero.getParentBlock());
----------------
jeanPerier wrote:

I do not follow why the insertion point needs to be changed here.

https://github.com/llvm/llvm-project/pull/159788


More information about the cfe-commits mailing list