diff --git a/flang/include/flang/Optimizer/Dialect/FIRAttr.td b/flang/include/flang/Optimizer/Dialect/FIRAttr.td --- a/flang/include/flang/Optimizer/Dialect/FIRAttr.td +++ b/flang/include/flang/Optimizer/Dialect/FIRAttr.td @@ -57,4 +57,7 @@ "::fir::FortranVariableFlagsAttr::get($_builder.getContext(), $0)"; } +// mlir::SideEffects::Resource for modelling operations which add debugging information +def DebuggingResource : Resource<"::fir::DebuggingResource">; + #endif // FIR_DIALECT_FIR_ATTRS diff --git a/flang/include/flang/Optimizer/Dialect/FIROps.h b/flang/include/flang/Optimizer/Dialect/FIROps.h --- a/flang/include/flang/Optimizer/Dialect/FIROps.h +++ b/flang/include/flang/Optimizer/Dialect/FIROps.h @@ -45,6 +45,12 @@ return "normalized.lb"; } +/// Model operations which affect global debugging information +struct DebuggingResource + : public mlir::SideEffects::Resource::Base { + mlir::StringRef getName() final { return "DebuggingResource"; } +}; + } // namespace fir #define GET_OP_CLASSES diff --git a/flang/include/flang/Optimizer/Dialect/FIROps.td b/flang/include/flang/Optimizer/Dialect/FIROps.td --- a/flang/include/flang/Optimizer/Dialect/FIROps.td +++ b/flang/include/flang/Optimizer/Dialect/FIROps.td @@ -2919,7 +2919,13 @@ let results = (outs BoolLike); } +// fir.declare leads to no codegen so the side effects implementation should be +// Pure. However, this would allow dead code elimination to remove these +// operations if the values are unused. fir.declare may be used to generate +// debug information so we would like to keep this around even if the value +// is not used. def fir_DeclareOp : fir_Op<"declare", [AttrSizedOperandSegments, + MemoryEffects<[MemWrite]>, DeclareOpInterfaceMethods]> { let summary = "declare a variable"; diff --git a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td --- a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td +++ b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td @@ -28,8 +28,14 @@ : Op; - +// DeclareOp can be lowered to EmboxOp, EmboxCharOp, ReboxOp, etc and so could +// generate code. All of the operations it can generate are modelled with +// NoMemoryEffect. However, if hlfir.declare is given NoMemoryEffect, it can be +// removed by dead code elimination if the value result is unused. Information +// from the delclare operation can be used to generate debug information so we +// don't want to remove it as dead code def hlfir_DeclareOp : hlfir_Op<"declare", [AttrSizedOperandSegments, + MemoryEffects<[MemWrite]>, DeclareOpInterfaceMethods]> { let summary = "declare a variable and produce an SSA value that can be used as a variable in HLFIR operations";