[flang-commits] [clang] [flang] [llvm] [flang] Add -finit-local= to initialize automatic variables (PR #216164)

via flang-commits flang-commits at lists.llvm.org
Thu Sep 3 00:09:56 PDT 2026


================
@@ -1250,6 +1253,465 @@ getSafeRepackAttrs(Fortran::lower::AbstractConverter &converter) {
   return attrs.empty() ? mlir::ArrayAttr{} : builder.getArrayAttr(attrs);
 }
 
+//===----------------------------------------------------------------------===//
+// -finit-local= helpers
+//===----------------------------------------------------------------------===//
+
+/// Returns true if \p derived or any of its components (recursively) is a
+/// PowerPC vector type. fir::VectorType does not implement
+/// DataLayoutTypeInterface, so any record containing a vector component would
+/// crash record-size calculation. Excluding such records at eligibility time
+/// avoids the crash.
+static bool containsVectorComponent(
+    const Fortran::semantics::DerivedTypeSpec &derived) {
+  if (derived.IsVectorType())
+    return true;
+  const Fortran::semantics::Scope *scope = derived.GetScope();
+  if (!scope)
+    return false;
+  const Fortran::semantics::Symbol &typeSym = derived.typeSymbol();
+  const auto *details =
+      typeSym.detailsIf<Fortran::semantics::DerivedTypeDetails>();
+  if (!details)
+    return false;
+  for (const Fortran::semantics::SourceName &compName :
+       details->componentNames()) {
+    auto it = scope->find(compName);
+    if (it == scope->cend())
+      continue;
+    const Fortran::semantics::Symbol &comp = it->second.get();
+    if (const Fortran::semantics::DeclTypeSpec *compTy = comp.GetType())
+      if (const Fortran::semantics::DerivedTypeSpec *compDerived =
+              compTy->AsDerived())
+        if (containsVectorComponent(*compDerived))
+          return true;
+  }
+  return false;
+}
+
+/// Returns true when \p var is an automatic local variable eligible for
+/// -finit-local= initialization. Excluded: variables without a symbol,
+/// globals, dummy arguments, SAVE'd vars, ALLOCATABLE/POINTER, vars in
+/// an EQUIVALENCE set, vars with explicit or default initialization, and
+/// CUDA variables whose storage is always unreachable by a plain fir.store
+/// (constant, shared, usedevice). The Device case is deferred to genInitLocal
+/// which applies cuf::isCUDADeviceContext to distinguish cuf.alloc from
+/// fir.alloca storage.
+static bool shouldInitLocal(const Fortran::lower::pft::Variable &var) {
+  if (!var.hasSymbol() || var.isGlobal())
+    return false;
+  const Fortran::semantics::Symbol &sym = var.getSymbol();
+  if (Fortran::semantics::IsDummy(sym))
+    return false;
+  if (Fortran::semantics::IsSaved(sym))
+    return false;
+  if (Fortran::semantics::IsAllocatableOrPointer(sym))
+    return false;
+  if (Fortran::lower::hasDefaultInitialization(sym))
+    return false;
+  if (const auto *obj =
+          sym.detailsIf<Fortran::semantics::ObjectEntityDetails>())
+    if (obj->init())
+      return false;
+  if (Fortran::semantics::FindEquivalenceSet(sym))
+    return false;
+  // Cray pointees own no storage of their own; their FIR base is a
+  // pointer-box descriptor. Initializing it would overwrite the
+  // descriptor, not the pointee storage.
+  if (sym.test(Fortran::semantics::Symbol::Flag::CrayPointee))
+    return false;
+  // PowerPC vector types (vector(real(4)) etc.) lower to fir::VectorType
+  // which does not implement DataLayoutTypeInterface at the HLFIR level.
+  // Without this guard, a direct vector local would silently fall back to
+  // zero initialization regardless of the requested mode, and a derived-type
+  // local whose component is a vector type would crash record-size
+  // calculation.  Exclude both cases here by walking components recursively.
+  if (const Fortran::semantics::DeclTypeSpec *declTy = sym.GetType())
+    if (const Fortran::semantics::DerivedTypeSpec *derived =
+            declTy->AsDerived())
+      if (containsVectorComponent(*derived))
+        return false;
+  // CUDA storage accessibility:
+  //   constant / shared / usedevice: always unreachable by a plain fir.store
+  //     from the host -- skip.
+  //   device: the allocation choice (cuf.alloc vs fir.alloca) depends on
+  //     whether the insertion point is in a device context; that check
+  //     requires the MLIR builder and is deferred to genInitLocal, which
+  //     calls cuf::isCUDADeviceContext(builder.getRegion()) after this
+  //     predicate returns true.
+  //   managed / unified / pinned: host-accessible unified memory -- initialize.
+  if (auto cudaAttr = Fortran::semantics::GetCUDADataAttr(&sym)) {
+    switch (*cudaAttr) {
+    case Fortran::common::CUDADataAttr::Constant:
+    case Fortran::common::CUDADataAttr::Shared:
+    case Fortran::common::CUDADataAttr::UseDevice:
+      return false;
+    default:
+      break;
+    }
+  }
+  return true;
+}
+
+/// Build a constant whose every byte equals \p bytePat.
+/// Handles: integer, float (bitcast from integer splat), complex (both parts),
+/// and logical (raw integer, stored via bitcasted address by the caller).
+/// Character, derived-type, and sequence types are all intercepted by
+/// genInitLocalStore or initAddr before this function is called and must
+/// not reach it. fir::VectorType (PowerPC vector types, direct or as a
+/// derived-type component) is excluded upstream by shouldInitLocal via
+/// containsVectorComponent and will never reach this function.
+static mlir::Value genByteSplatInit(fir::FirOpBuilder &builder,
+                                    mlir::Location loc, mlir::Type ty,
+                                    uint8_t bytePat) {
+  mlir::Type eleTy = fir::unwrapSequenceType(ty);
+
+  // Build a signless integer constant from a byte splat.  arith.constant
+  // requires a signless integer type; callers that need a non-signless result
+  // (e.g. unsigned ui32) must fir.convert the returned value themselves.
+  auto makeIntCst = [&](unsigned bits) -> mlir::Value {
+    llvm::APInt byteVal(8, bytePat);
+    llvm::APInt splat = llvm::APInt::getSplat(bits, byteVal);
+    mlir::Type intTy = builder.getIntegerType(bits);
+    return mlir::arith::ConstantOp::create(
+        builder, loc, intTy, builder.getIntegerAttr(intTy, splat));
+  };
+
+  if (auto fpTy = mlir::dyn_cast<mlir::FloatType>(eleTy)) {
+    mlir::Value intCst = makeIntCst(fpTy.getWidth());
+    return mlir::arith::BitcastOp::create(builder, loc, fpTy, intCst);
+  }
+  if (auto intTy = mlir::dyn_cast<mlir::IntegerType>(eleTy)) {
+    mlir::Value cst = makeIntCst(intTy.getWidth());
+    // arith.constant only supports signless integers; fir.convert reinterprets
+    // the bit pattern into the declared signed or unsigned type without
+    // changing any bits, satisfying FIR verification for !fir.ref<ui32> etc.
+    if (!intTy.isSignless())
+      cst = builder.createConvert(loc, intTy, cst);
+    return cst;
+  }
+  // Complex: apply the byte pattern to each (real, imag) part.
+  if (auto cplxTy = mlir::dyn_cast<mlir::ComplexType>(eleTy)) {
+    mlir::Type partTy = cplxTy.getElementType();
+    mlir::Value partVal = genByteSplatInit(builder, loc, partTy, bytePat);
+    return mlir::complex::CreateOp::create(builder, loc, cplxTy, partVal,
+                                           partVal);
+  }
+  // LOGICAL(k) has a fixed size of k bytes. Return the raw integer splat;
+  // the caller stores it via a bitcasted address to preserve the bit pattern
+  // (fir.convert from integer to !fir.logical normalizes nonzero -> true).
+  if (auto logTy = mlir::dyn_cast<fir::LogicalType>(eleTy)) {
+    return makeIntCst(logTy.getFKind() * 8);
+  }
+  // All types that pass shouldInitLocal and reach genInitLocalStore are
+  // handled explicitly above (integer, float, complex, logical) or are
+  // intercepted before this call (character, record, sequence).
+  // PowerPC vector types (direct or as a derived-type component) are excluded
+  // by shouldInitLocal via containsVectorComponent and never reach here.
+  // A silent zero for an unhandled type would violate the hex-mode contract,
+  // so assert rather than fall back silently.
+  llvm_unreachable("genByteSplatInit: unhandled type in hex mode");
+}
+
+/// Emit a store of the -finit-local= pattern for a single scalar address.
+/// Fixed-length CHARACTER in hex mode: byte-loop over every byte of storage.
+/// LOGICAL stores via a bitcasted integer address to preserve the raw bit
+/// pattern past fir.convert normalization.
+static void genInitLocalStore(fir::FirOpBuilder &builder, mlir::Location loc,
+                              mlir::Type ty, mlir::Value addr,
+                              Fortran::lower::InitLocalKind mode,
+                              uint8_t hexByte) {
+  // Fixed-length CHARACTER: for hex mode emit a compile-time byte-loop so
+  // every code-unit gets the requested pattern. Zero falls through to
+  // fir.zero_bits below.
+  if (auto charTy = mlir::dyn_cast<fir::CharacterType>(ty)) {
+    // CHARACTER(0) has zero-length storage -- nothing to initialize.
+    if (charTy.getLen() == 0)
+      return;
+    if (mode == Fortran::lower::InitLocalKind::Hex) {
+      // Loop over every byte of the character storage. For kind=1 each
+      // code unit is one byte; for kind=2/4 (UTF-16/32) each code unit is
+      // kind bytes wide. We use a kind=1 singleton as the view element so
+      // fir.coordinate_of advances exactly one byte per step, and iterate
+      // nUnits * kind times to cover all bytes.
+      int64_t nUnits = charTy.hasConstantLen() ? charTy.getLen() : 0;
+      int64_t kindBytes = charTy.getFKind();
+      int64_t nBytes = nUnits * kindBytes;
+      if (nBytes > 0) {
+        mlir::Type idxTy = builder.getIndexType();
+        mlir::Type i8Ty = builder.getIntegerType(8);
+        // Use a kind=1 singleton so fir.coordinate_of strides by 1 byte.
+        fir::CharacterType byteTy =
+            fir::CharacterType::getSingleton(builder.getContext(), 1);
+        mlir::Type byteSeqTy = fir::SequenceType::get(
+            {fir::SequenceType::getUnknownExtent()}, byteTy);
+        mlir::Value byteBase =
+            builder.createConvertWithVolatileCast(
+                loc, builder.getRefType(byteSeqTy), addr);
+        mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
+        mlir::Value last =
+            builder.createIntegerConstant(loc, idxTy, nBytes - 1);
+        mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
+        auto loop = fir::DoLoopOp::create(builder, loc, zero, last, one,
+                                          /*unordered=*/false,
+                                          /*finalCount=*/false);
+        mlir::OpBuilder::InsertionGuard guard(builder);
+        builder.setInsertionPointToStart(loop.getBody());
+        mlir::Value iv = loop.getInductionVar();
+        mlir::Value byteAddr =
+            fir::CoordinateOp::create(builder, loc, builder.getRefType(byteTy),
+                                      byteBase, mlir::ValueRange{iv});
+        mlir::Value pat = builder.createIntegerConstant(
+            loc, i8Ty, static_cast<int64_t>(hexByte));
+        mlir::Value i8Addr =
+            builder.createConvert(loc, builder.getRefType(i8Ty), byteAddr);
+        fir::StoreOp::create(builder, loc, pat, i8Addr);
+      }
+      return;
+    }
+  }
+  // REAL and COMPLEX: when the allocation size exceeds the store size
+  // (e.g. x86_fp80 stores 10 bytes but occupies 16), fill the full
+  // allocation with a byte loop so padding bytes are also initialized.
+  auto emitByteLoop = [&](uint64_t nBytes) {
+    mlir::Type idxTy = builder.getIndexType();
+    mlir::Type i8Ty = builder.getIntegerType(8);
+    mlir::Type i8SeqTy =
+        fir::SequenceType::get({fir::SequenceType::getUnknownExtent()}, i8Ty);
+    mlir::Value byteBase =
+        builder.createConvertWithVolatileCast(
+            loc, builder.getRefType(i8SeqTy), addr);
+    mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
+    mlir::Value last = builder.createIntegerConstant(
+        loc, idxTy, static_cast<int64_t>(nBytes) - 1);
+    mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
+    auto loop = fir::DoLoopOp::create(builder, loc, zero, last, one,
+                                      /*unordered=*/false,
+                                      /*finalCount=*/false);
+    mlir::OpBuilder::InsertionGuard guard(builder);
+    builder.setInsertionPointToStart(loop.getBody());
+    mlir::Value iv = loop.getInductionVar();
+    mlir::Value byteAddr = fir::CoordinateOp::create(
+        builder, loc, builder.getRefType(i8Ty), byteBase, mlir::ValueRange{iv});
+    int64_t fillByte =
+        (mode == Fortran::lower::InitLocalKind::Zero) ? 0 : hexByte;
+    mlir::Value pat = builder.createIntegerConstant(loc, i8Ty, fillByte);
+    fir::StoreOp::create(builder, loc, pat, byteAddr);
+  };
+
+  if (mlir::isa<mlir::FloatType, mlir::ComplexType>(ty)) {
+    const mlir::DataLayout &dl = builder.getDataLayout();
+    uint64_t storeSize = dl.getTypeSize(ty);
+    uint64_t allocSize = llvm::alignTo(storeSize, dl.getTypeABIAlignment(ty));
+    if (allocSize > storeSize) {
+      emitByteLoop(allocSize);
+      return;
+    }
+  }
+
+  mlir::Value val;
+  switch (mode) {
+  case Fortran::lower::InitLocalKind::Zero:
+    val = fir::ZeroOp::create(builder, loc, ty);
+    break;
+  case Fortran::lower::InitLocalKind::Hex:
+    val = genByteSplatInit(builder, loc, ty, hexByte);
+    break;
+  default:
+    llvm_unreachable("unexpected InitLocalKind in genInitLocalStore");
+  }
+  // For LOGICAL in hex mode, genByteSplatInit returns a raw integer to
+  // preserve the bit pattern. Store it via a bitcasted address to avoid
+  // fir.convert normalization (which would reduce any nonzero value to
+  // logical true).
+  if (mode == Fortran::lower::InitLocalKind::Hex &&
+      mlir::isa<fir::LogicalType>(ty)) {
+    unsigned bits = mlir::cast<fir::LogicalType>(ty).getFKind() * 8;
+    mlir::Type intRefTy = builder.getRefType(builder.getIntegerType(bits));
+    mlir::Value intAddr = builder.createConvert(loc, intRefTy, addr);
----------------
MattPD wrote:

Confirmed: volatile LOGICAL and rank-one arrays now pass strict verification in both zero and hexadecimal modes.


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


More information about the flang-commits mailing list