-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
`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).
@llvm/pr-subscribers-flang-fir-hlfir @llvm/pr-subscribers-flang-codegen Author: Kareem Ergawy (ergawy) Changes
Full diff: https://github.com/llvm/llvm-project/pull/143687.diff 2 Files Affected:
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 82d960a6fc61e..ecce5c6f9d159 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -3294,6 +3294,29 @@ 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 {
+ auto uses = mlir::SymbolTable::getSymbolUses(
+ 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).
+ if (!uses || !uses->empty())
+ return mlir::failure();
+
+ 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?
@@ -4249,15 +4272,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.
diff --git a/flang/test/Fir/local.fir b/flang/test/Fir/local.fir
new file mode 100644
index 0000000000000..006f5ca944670
--- /dev/null
+++ b/flang/test/Fir/local.fir
@@ -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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, other than my comment about the cost of testing the usage, LGTM.
llvm::LogicalResult | ||
matchAndRewrite(fir::LocalitySpecifierOp localizer, OpAdaptor adaptor, | ||
mlir::ConversionPatternRewriter &rewriter) const override { | ||
auto uses = mlir::SymbolTable::getSymbolUses( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why aren't the fir.local ops removed when the privatization is lowered to either openmp or sequential code?
This is a unified location where we can take care of this. Also, since the same |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/157/builds/30566 Here is the relevant piece of the build log for the reference
|
…43687) `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). This hopefully resolved a build issue introduced by llvm#142567 (see for example: https://lab.llvm.org/buildbot/#/builders/199/builds/4009).
…43687) `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). This hopefully resolved a build issue introduced by llvm#142567 (see for example: https://lab.llvm.org/buildbot/#/builders/199/builds/4009).
fir.local
ops are not supposed to have any uses at this point (i.e. during lowering to LLVM). In case of serialization, thefir.do_concurrent
users are expected to have been lowered tofir.do_loop
nests. In case of parallelization, thefir.do_concurrent
users are expected to have been lowered to the target parallel model (e.g. OpenMP).This hopefully resolved a build issue introduced by #142567 (see for example: https://lab.llvm.org/buildbot/#/builders/199/builds/4009).