Skip to content

Commit 3eff3c0

Browse files
committed
[flang] add memory effects interface to (hl)fir.declare
These operations should have no memory effect, but doing so causes the declare to be removed by dead code elimination if the result value is unused. fir.declare is intended to be used to generate debug information about variables. Debug information may still be desirable even about unused variables, so we don't want to remove the declare operations when performing dead code elimination. Differential Revision: https://reviews.llvm.org/D157626
1 parent 059da56 commit 3eff3c0

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

flang/include/flang/Optimizer/Dialect/FIRAttr.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ def fir_FortranVariableFlagsAttr : fir_Attr<"FortranVariableFlags"> {
5757
"::fir::FortranVariableFlagsAttr::get($_builder.getContext(), $0)";
5858
}
5959

60+
// mlir::SideEffects::Resource for modelling operations which add debugging information
61+
def DebuggingResource : Resource<"::fir::DebuggingResource">;
62+
6063
#endif // FIR_DIALECT_FIR_ATTRS

flang/include/flang/Optimizer/Dialect/FIROps.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ static constexpr llvm::StringRef getNormalizedLowerBoundAttrName() {
4545
return "normalized.lb";
4646
}
4747

48+
/// Model operations which affect global debugging information
49+
struct DebuggingResource
50+
: public mlir::SideEffects::Resource::Base<DebuggingResource> {
51+
mlir::StringRef getName() final { return "DebuggingResource"; }
52+
};
53+
4854
} // namespace fir
4955

5056
#define GET_OP_CLASSES

flang/include/flang/Optimizer/Dialect/FIROps.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,7 +2919,13 @@ def fir_IsPresentOp : fir_SimpleOp<"is_present", [NoMemoryEffect]> {
29192919
let results = (outs BoolLike);
29202920
}
29212921

2922+
// fir.declare leads to no codegen so the side effects implementation should be
2923+
// Pure. However, this would allow dead code elimination to remove these
2924+
// operations if the values are unused. fir.declare may be used to generate
2925+
// debug information so we would like to keep this around even if the value
2926+
// is not used.
29222927
def fir_DeclareOp : fir_Op<"declare", [AttrSizedOperandSegments,
2928+
MemoryEffects<[MemWrite<DebuggingResource>]>,
29232929
DeclareOpInterfaceMethods<fir_FortranVariableOpInterface>]> {
29242930
let summary = "declare a variable";
29252931

flang/include/flang/Optimizer/HLFIR/HLFIROps.td

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ class hlfir_Op<string mnemonic, list<Trait> traits>
2828
: Op<hlfir_Dialect, mnemonic, traits>;
2929

3030

31-
31+
// DeclareOp can be lowered to EmboxOp, EmboxCharOp, ReboxOp, etc and so could
32+
// generate code. All of the operations it can generate are modelled with
33+
// NoMemoryEffect. However, if hlfir.declare is given NoMemoryEffect, it can be
34+
// removed by dead code elimination if the value result is unused. Information
35+
// from the delclare operation can be used to generate debug information so we
36+
// don't want to remove it as dead code
3237
def hlfir_DeclareOp : hlfir_Op<"declare", [AttrSizedOperandSegments,
38+
MemoryEffects<[MemWrite<DebuggingResource>]>,
3339
DeclareOpInterfaceMethods<fir_FortranVariableOpInterface>]> {
3440
let summary = "declare a variable and produce an SSA value that can be used as a variable in HLFIR operations";
3541

0 commit comments

Comments
 (0)