Skip to content

Commit 91b74e8

Browse files
[flang] Remove materialization workaround in type converter
This change is in preparation of #97903, which adds extra checks for materializations: it is now enforced that they produce an SSA value of the correct type, so the current workaround no longer works. For `fir.has_value` the fix is simple: no target materializations on the operands are performed if the lowering patterns is initialized without a type converter. For `cg::XEmboxOp`, the existing workaround that skips `unrealized_conversion_cast` ops can be generalized. (This is still a workaround.) Also remove the lowering pattern for `unrealized_conversion_cast`. This pattern has no effect because `unrealized_conversion_cast` ops that are inserted by the dialect conversion framework are never matched by the pattern driver.
1 parent 3b7a7f4 commit 91b74e8

File tree

3 files changed

+25
-41
lines changed

3 files changed

+25
-41
lines changed

flang/include/flang/Tools/CLOptions.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/// This file defines some shared command-line options that can be used when
1010
/// debugging the test tools. This file must be included into the tool.
1111

12+
#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
1213
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
1314
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
1415
#include "mlir/Pass/PassManager.h"
@@ -223,6 +224,10 @@ inline void addFIRToLLVMPass(
223224
options.forceUnifiedTBAATree = useOldAliasTags;
224225
addPassConditionally(pm, disableFirToLlvmIr,
225226
[&]() { return fir::createFIRToLLVMPass(options); });
227+
// The dialect conversion framework may leave dead unrealized_conversion_cast
228+
// ops behind, so run reconcile-unrealized-casts to clean them up.
229+
addPassConditionally(pm, disableFirToLlvmIr,
230+
[&]() { return mlir::createReconcileUnrealizedCastsPass(); });
226231
}
227232

228233
inline void addLLVMDialectToLLVMPass(

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
3636
#include "mlir/Conversion/MathToLibm/MathToLibm.h"
3737
#include "mlir/Conversion/OpenMPToLLVM/ConvertOpenMPToLLVM.h"
38-
#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
3938
#include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVM.h"
4039
#include "mlir/Dialect/Arith/IR/Arith.h"
4140
#include "mlir/Dialect/DLTI/DLTI.h"
@@ -1726,10 +1725,9 @@ struct XReboxOpConversion : public EmboxCommonConversion<fir::cg::XReboxOp> {
17261725
// fir.box was translated to an llvm.ptr<llvm.struct<>> and the MLIR pass
17271726
// manager inserted a builtin.unrealized_conversion_cast that was inserted
17281727
// and needs to be removed here.
1729-
if (isInGlobalOp(rewriter))
1730-
if (auto unrealizedCast =
1731-
loweredBox.getDefiningOp<mlir::UnrealizedConversionCastOp>())
1732-
loweredBox = unrealizedCast.getInputs()[0];
1728+
if (auto unrealizedCast =
1729+
loweredBox.getDefiningOp<mlir::UnrealizedConversionCastOp>())
1730+
loweredBox = unrealizedCast.getInputs()[0];
17331731

17341732
TypePair inputBoxTyPair = getBoxTypePair(rebox.getBox().getType());
17351733

@@ -2669,8 +2667,9 @@ struct TypeDescOpConversion : public fir::FIROpConversion<fir::TypeDescOp> {
26692667
};
26702668

26712669
/// Lower `fir.has_value` operation to `llvm.return` operation.
2672-
struct HasValueOpConversion : public fir::FIROpConversion<fir::HasValueOp> {
2673-
using FIROpConversion::FIROpConversion;
2670+
struct HasValueOpConversion
2671+
: public mlir::OpConversionPattern<fir::HasValueOp> {
2672+
using OpConversionPattern::OpConversionPattern;
26742673

26752674
llvm::LogicalResult
26762675
matchAndRewrite(fir::HasValueOp op, OpAdaptor adaptor,
@@ -3515,29 +3514,6 @@ struct MustBeDeadConversion : public fir::FIROpConversion<FromOp> {
35153514
}
35163515
};
35173516

3518-
struct UnrealizedConversionCastOpConversion
3519-
: public fir::FIROpConversion<mlir::UnrealizedConversionCastOp> {
3520-
using FIROpConversion::FIROpConversion;
3521-
3522-
llvm::LogicalResult
3523-
matchAndRewrite(mlir::UnrealizedConversionCastOp op, OpAdaptor adaptor,
3524-
mlir::ConversionPatternRewriter &rewriter) const override {
3525-
assert(op.getOutputs().getTypes().size() == 1 && "expect a single type");
3526-
mlir::Type convertedType = convertType(op.getOutputs().getTypes()[0]);
3527-
if (convertedType == adaptor.getInputs().getTypes()[0]) {
3528-
rewriter.replaceOp(op, adaptor.getInputs());
3529-
return mlir::success();
3530-
}
3531-
3532-
convertedType = adaptor.getInputs().getTypes()[0];
3533-
if (convertedType == op.getOutputs().getType()[0]) {
3534-
rewriter.replaceOp(op, adaptor.getInputs());
3535-
return mlir::success();
3536-
}
3537-
return mlir::failure();
3538-
}
3539-
};
3540-
35413517
struct ShapeOpConversion : public MustBeDeadConversion<fir::ShapeOp> {
35423518
using MustBeDeadConversion::MustBeDeadConversion;
35433519
};
@@ -3714,7 +3690,8 @@ class FIRToLLVMLowering
37143690
signalPassFailure();
37153691
}
37163692

3717-
// Run pass to add comdats to functions that have weak linkage on relevant platforms
3693+
// Run pass to add comdats to functions that have weak linkage on relevant
3694+
// platforms
37183695
if (fir::getTargetTriple(mod).supportsCOMDAT()) {
37193696
mlir::OpPassManager comdatPM("builtin.module");
37203697
comdatPM.addPass(mlir::LLVM::createLLVMAddComdats());
@@ -3789,16 +3766,17 @@ void fir::populateFIRToLLVMConversionPatterns(
37893766
DivcOpConversion, EmboxOpConversion, EmboxCharOpConversion,
37903767
EmboxProcOpConversion, ExtractValueOpConversion, FieldIndexOpConversion,
37913768
FirEndOpConversion, FreeMemOpConversion, GlobalLenOpConversion,
3792-
GlobalOpConversion, HasValueOpConversion, InsertOnRangeOpConversion,
3793-
InsertValueOpConversion, IsPresentOpConversion, LenParamIndexOpConversion,
3794-
LoadOpConversion, MulcOpConversion, NegcOpConversion,
3795-
NoReassocOpConversion, SelectCaseOpConversion, SelectOpConversion,
3796-
SelectRankOpConversion, SelectTypeOpConversion, ShapeOpConversion,
3797-
ShapeShiftOpConversion, ShiftOpConversion, SliceOpConversion,
3798-
StoreOpConversion, StringLitOpConversion, SubcOpConversion,
3799-
TypeDescOpConversion, TypeInfoOpConversion, UnboxCharOpConversion,
3800-
UnboxProcOpConversion, UndefOpConversion, UnreachableOpConversion,
3801-
UnrealizedConversionCastOpConversion, XArrayCoorOpConversion,
3769+
GlobalOpConversion, InsertOnRangeOpConversion, InsertValueOpConversion,
3770+
IsPresentOpConversion, LenParamIndexOpConversion, LoadOpConversion,
3771+
MulcOpConversion, NegcOpConversion, NoReassocOpConversion,
3772+
SelectCaseOpConversion, SelectOpConversion, SelectRankOpConversion,
3773+
SelectTypeOpConversion, ShapeOpConversion, ShapeShiftOpConversion,
3774+
ShiftOpConversion, SliceOpConversion, StoreOpConversion,
3775+
StringLitOpConversion, SubcOpConversion, TypeDescOpConversion,
3776+
TypeInfoOpConversion, UnboxCharOpConversion, UnboxProcOpConversion,
3777+
UndefOpConversion, UnreachableOpConversion, XArrayCoorOpConversion,
38023778
XEmboxOpConversion, XReboxOpConversion, ZeroOpConversion>(converter,
38033779
options);
3780+
3781+
patterns.insert<HasValueOpConversion>(patterns.getContext());
38043782
}

flang/test/Fir/basic-program.fir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,5 @@ func.func @_QQmain() {
119119
// PASSES-NEXT: (S) 0 num-dce'd - Number of operations eliminated
120120
// PASSES-NEXT: TargetRewrite
121121
// PASSES-NEXT: FIRToLLVMLowering
122+
// PASSES-NEXT: ReconcileUnrealizedCasts
122123
// PASSES-NEXT: LLVMIRLoweringPass

0 commit comments

Comments
 (0)