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. I've given it the MemAlloc effect to model this. def fir_DeclareOp : fir_Op<"declare", [AttrSizedOperandSegments, + MemoryEffects<[MemAlloc]>, 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. This is modelled as a MemAlloc effect. def hlfir_DeclareOp : hlfir_Op<"declare", [AttrSizedOperandSegments, + MemoryEffects<[MemAlloc]>, DeclareOpInterfaceMethods]> { let summary = "declare a variable and produce an SSA value that can be used as a variable in HLFIR operations";