diff options
| author | Mingming Liu <mingmingl@google.com> | 2025-09-10 15:25:31 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-10 15:25:31 -0700 |
| commit | 1417dafa1db9cb1b2b09438aa9f53ea5ab6e36e2 (patch) | |
| tree | 57f4b1f313c8cf74eed8819870f39c36ea263c68 /flang/lib/Optimizer/CodeGen/CodeGen.cpp | |
| parent | 898b813bc8a6d0276bf0f4769f5f2f64b34e632d (diff) | |
| parent | b8cefcb601ddaa18482555c4ff363c01a270c2fe (diff) | |
Merge branch 'main' into users/mingmingl-llvm/samplefdo-profile-formatusers/mingmingl-llvm/samplefdo-profile-format
Diffstat (limited to 'flang/lib/Optimizer/CodeGen/CodeGen.cpp')
| -rw-r--r-- | flang/lib/Optimizer/CodeGen/CodeGen.cpp | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp index 76f3cbd421cb..0800ed4db8c3 100644 --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -329,6 +329,31 @@ struct AllocaOpConversion : public fir::FIROpConversion<fir::AllocaOp> { } // namespace namespace { + +static bool isInGlobalOp(mlir::ConversionPatternRewriter &rewriter) { + auto *thisBlock = rewriter.getInsertionBlock(); + return thisBlock && mlir::isa<mlir::LLVM::GlobalOp>(thisBlock->getParentOp()); +} + +// Inside a fir.global, the input box was produced as an llvm.struct<> +// because objects cannot be handled in memory inside a fir.global body that +// must be constant foldable. However, the type translation are not +// contextual, so the fir.box<T> type of the operation that produced the +// fir.box was translated to an llvm.ptr<llvm.struct<>> and the MLIR pass +// manager inserted a builtin.unrealized_conversion_cast that was inserted +// and needs to be removed here. +// This should be called by any pattern operating on operations that are +// accepting fir.box inputs and are used in fir.global. +static mlir::Value +fixBoxInputInsideGlobalOp(mlir::ConversionPatternRewriter &rewriter, + mlir::Value box) { + if (isInGlobalOp(rewriter)) + if (auto unrealizedCast = + box.getDefiningOp<mlir::UnrealizedConversionCastOp>()) + return unrealizedCast.getInputs()[0]; + return box; +} + /// Lower `fir.box_addr` to the sequence of operations to extract the first /// element of the box. struct BoxAddrOpConversion : public fir::FIROpConversion<fir::BoxAddrOp> { @@ -341,6 +366,7 @@ struct BoxAddrOpConversion : public fir::FIROpConversion<fir::BoxAddrOp> { auto loc = boxaddr.getLoc(); if (auto argty = mlir::dyn_cast<fir::BaseBoxType>(boxaddr.getVal().getType())) { + a = fixBoxInputInsideGlobalOp(rewriter, a); TypePair boxTyPair = getBoxTypePair(argty); rewriter.replaceOp(boxaddr, getBaseAddrFromBox(loc, boxTyPair, a, rewriter)); @@ -1737,12 +1763,6 @@ struct EmboxCommonConversion : public fir::FIROpConversion<OP> { xbox.getSubcomponent().size()); } - static bool isInGlobalOp(mlir::ConversionPatternRewriter &rewriter) { - auto *thisBlock = rewriter.getInsertionBlock(); - return thisBlock && - mlir::isa<mlir::LLVM::GlobalOp>(thisBlock->getParentOp()); - } - /// If the embox is not in a globalOp body, allocate storage for the box; /// store the value inside and return the generated alloca. Return the input /// value otherwise. @@ -2076,21 +2096,10 @@ struct XReboxOpConversion : public EmboxCommonConversion<fir::cg::XReboxOp> { mlir::ConversionPatternRewriter &rewriter) const override { mlir::Location loc = rebox.getLoc(); mlir::Type idxTy = lowerTy().indexType(); - mlir::Value loweredBox = adaptor.getOperands()[0]; + mlir::Value loweredBox = + fixBoxInputInsideGlobalOp(rewriter, adaptor.getBox()); mlir::ValueRange operands = adaptor.getOperands(); - // Inside a fir.global, the input box was produced as an llvm.struct<> - // because objects cannot be handled in memory inside a fir.global body that - // must be constant foldable. However, the type translation are not - // contextual, so the fir.box<T> type of the operation that produced the - // fir.box was translated to an llvm.ptr<llvm.struct<>> and the MLIR pass - // manager inserted a builtin.unrealized_conversion_cast that was inserted - // and needs to be removed here. - if (isInGlobalOp(rewriter)) - if (auto unrealizedCast = - loweredBox.getDefiningOp<mlir::UnrealizedConversionCastOp>()) - loweredBox = unrealizedCast.getInputs()[0]; - TypePair inputBoxTyPair = getBoxTypePair(rebox.getBox().getType()); // Create new descriptor and fill its non-shape related data. |
