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

via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 26 06:09:07 PDT 2026


================
@@ -1225,6 +1226,54 @@ getSafeRepackAttrs(Fortran::lower::AbstractConverter &converter) {
   return attrs.empty() ? mlir::ArrayAttr{} : builder.getArrayAttr(attrs);
 }
 
+// Helper class to encapsulate utilities 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 these utilities outside the
+// scope detailed here is discouraged, and is probably wrong.
+class ImplicitAssignmentGenerator {
+private:
+  bool isInitLocalZeroFlagDefined;
+
+public:
+  ImplicitAssignmentGenerator(bool isInitLocalZeroFlagDefined)
+      : isInitLocalZeroFlagDefined(isInitLocalZeroFlagDefined) {}
+
+  void emitAssignment(Fortran::lower::AbstractConverter &converter,
+                      mlir::Location loc, const Fortran::semantics::Symbol &sym,
+                      Fortran::lower::SymMap &symMap) {
+    if (isInitLocalZeroFlagDefined) {
+      mlir::Type eleTy = hlfir::getFortranElementType(converter.genType(sym));
+      auto *builder = &converter.getFirOpBuilder();
+
+      if (mlir::isa<fir::CharacterType>(eleTy)) {
+        fir::factory::CharacterExprHelper helper{*builder, loc};
+        fir::CharacterType::KindTy kind =
+            helper.getCharacterType(eleTy).getFKind();
+        mlir::Value zeroCode =
+            builder->createIntegerConstant(loc, builder->getI32Type(), 0);
+        mlir::Value zero = helper.createSingletonFromCode(zeroCode, kind);
+        hlfir::Entity lhs{symMap.lookupSymbol(sym).getAddr()};
+        lhs = hlfir::derefPointersAndAllocatables(loc, *builder, lhs);
+        mlir::Value rhsTmp = builder->createTemporary(loc, zero.getType());
+        builder->create<fir::StoreOp>(loc, zero, rhsTmp);
+        builder->create<hlfir::AssignOp>(loc, rhsTmp, lhs);
----------------
NimishMishra wrote:

Thanks @jeanPerier . I have updated this. Do you see any value in moving this to an utility in `Optimizer/Builder/Character.h`? In case you expect this to be shared across functions in the lowering?

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


More information about the cfe-commits mailing list