Skip to content

[flang] Erase fir.local ops before lowering fir to llvm #143687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3294,6 +3294,30 @@ struct LoadOpConversion : public fir::FIROpConversion<fir::LoadOp> {
}
};

struct LocalitySpecifierOpConversion
: public fir::FIROpConversion<fir::LocalitySpecifierOp> {
using FIROpConversion::FIROpConversion;
llvm::LogicalResult
matchAndRewrite(fir::LocalitySpecifierOp localizer, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
#ifdef EXPENSIVE_CHECKS
auto uses = mlir::SymbolTable::getSymbolUses(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid this may be a bit expensive on big programs. I think this will walk the whole IR to check for usages. So, this is likely introducing a number_of_fir_local * number_of_operation_in_module behavior, and number_of_operation_in_module can be quite big in some apps (>1million).

I would unconditionally erase it at that point in the pipeline (and let any bogus later usage complain if it fails to find the symbol), maybe leaving this check as an assert under #ifdef EXPENSIVE_CHECKS.

If at some point we do need the flexibility of this pass to actually leave fir.local in place when still used for some reason, we should use a SymbolUserMap created once and maintained.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick review. Done.

localizer, localizer->getParentOfType<mlir::ModuleOp>());

// `fir.local` ops are not supposed to have any uses at this point (i.e.
// during lowering to LLVM). In case of serialization, the
// `fir.do_concurrent` users are expected to have been lowered to
// `fir.do_loop` nests. In case of parallelization, the `fir.do_concurrent`
// users are expected to have been lowered to the target parallel model
// (e.g. OpenMP).
assert(uses && uses->empty());
#endif

rewriter.eraseOp(localizer);
return mlir::success();
}
};

/// Lower `fir.no_reassoc` to LLVM IR dialect.
/// TODO: how do we want to enforce this in LLVM-IR? Can we manipulate the fast
/// math flags?
Expand Down Expand Up @@ -4249,15 +4273,15 @@ void fir::populateFIRToLLVMConversionPatterns(
FieldIndexOpConversion, FirEndOpConversion, FreeMemOpConversion,
GlobalLenOpConversion, GlobalOpConversion, InsertOnRangeOpConversion,
IsPresentOpConversion, LenParamIndexOpConversion, LoadOpConversion,
MulcOpConversion, NegcOpConversion, NoReassocOpConversion,
SelectCaseOpConversion, SelectOpConversion, SelectRankOpConversion,
SelectTypeOpConversion, ShapeOpConversion, ShapeShiftOpConversion,
ShiftOpConversion, SliceOpConversion, StoreOpConversion,
StringLitOpConversion, SubcOpConversion, TypeDescOpConversion,
TypeInfoOpConversion, UnboxCharOpConversion, UnboxProcOpConversion,
UndefOpConversion, UnreachableOpConversion, XArrayCoorOpConversion,
XEmboxOpConversion, XReboxOpConversion, ZeroOpConversion>(converter,
options);
LocalitySpecifierOpConversion, MulcOpConversion, NegcOpConversion,
NoReassocOpConversion, SelectCaseOpConversion, SelectOpConversion,
SelectRankOpConversion, SelectTypeOpConversion, ShapeOpConversion,
ShapeShiftOpConversion, ShiftOpConversion, SliceOpConversion,
StoreOpConversion, StringLitOpConversion, SubcOpConversion,
TypeDescOpConversion, TypeInfoOpConversion, UnboxCharOpConversion,
UnboxProcOpConversion, UndefOpConversion, UnreachableOpConversion,
XArrayCoorOpConversion, XEmboxOpConversion, XReboxOpConversion,
ZeroOpConversion>(converter, options);

// Patterns that are populated without a type converter do not trigger
// target materializations for the operands of the root op.
Expand Down
10 changes: 10 additions & 0 deletions flang/test/Fir/local.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: fir-opt --fir-to-llvm-ir %s | FileCheck %s

// Tests that `fir.local` ops are dropped from the module before LLVM lowering.

fir.local {type = local} @local_privatizer : i32
func.func @foo() {
return
}

// CHECK-NOT: fir.local