diff --git a/flang/include/flang/Optimizer/Support/Utils.h b/flang/include/flang/Optimizer/Support/Utils.h index 566fd2a18648f..d2f0be15d7dd9 100644 --- a/flang/include/flang/Optimizer/Support/Utils.h +++ b/flang/include/flang/Optimizer/Support/Utils.h @@ -24,6 +24,7 @@ #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/IR/BuiltinAttributes.h" #include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/Location.h" #include "mlir/Interfaces/FunctionInterfaces.h" #include "llvm/ADT/StringRef.h" #include @@ -31,6 +32,14 @@ #include "flang/Optimizer/CodeGen/TypeConverter.h" namespace fir { +/// Return the line of a location, or 1 if it does not carry one. +inline uint32_t getLineFromLoc(mlir::Location loc) { + uint32_t line = 1; + if (auto fileLoc = mlir::dyn_cast(loc)) + line = fileLoc.getLine(); + return line; +} + /// Return the integer value of a arith::ConstantOp. inline std::int64_t toInt(mlir::arith::ConstantOp cop) { return mlir::cast(cop.getValue()) diff --git a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h b/flang/include/flang/Optimizer/Transforms/DebugTypeGenerator.h similarity index 97% rename from flang/lib/Optimizer/Transforms/DebugTypeGenerator.h rename to flang/include/flang/Optimizer/Transforms/DebugTypeGenerator.h index 3c4455d72a5d7..155b41f688579 100644 --- a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h +++ b/flang/include/flang/Optimizer/Transforms/DebugTypeGenerator.h @@ -167,11 +167,4 @@ class DebugTypeGenerator { } // namespace fir -static uint32_t getLineFromLoc(mlir::Location loc) { - uint32_t line = 1; - if (auto fileLoc = mlir::dyn_cast(loc)) - line = fileLoc.getLine(); - return line; -} - #endif // FORTRAN_OPTIMIZER_TRANSFORMS_DEBUGTYPEGENERATOR_H diff --git a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp index 6bee203f55c5f..8ab96172349ce 100644 --- a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp +++ b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp @@ -11,7 +11,6 @@ /// This pass populates some debug information for the module and functions. //===----------------------------------------------------------------------===// -#include "DebugTypeGenerator.h" #include "flang/Optimizer/Builder/FIRBuilder.h" #include "flang/Optimizer/Dialect/FIRCG/CGOps.h" #include "flang/Optimizer/Dialect/FIRDialect.h" @@ -21,6 +20,7 @@ #include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/Support/InternalNames.h" #include "flang/Optimizer/Support/Utils.h" +#include "flang/Optimizer/Transforms/DebugTypeGenerator.h" #include "flang/Optimizer/Transforms/Passes.h" #include "flang/Support/Version.h" #include "mlir/Dialect/DLTI/DLTI.h" @@ -262,14 +262,14 @@ bool AddDebugInfoPass::createCommonBlockGlobal( commonName = commonName.drop_back(); // Create the debug attributes. - unsigned line = getLineFromLoc(global.getLoc()); + unsigned line = fir::getLineFromLoc(global.getLoc()); mlir::LLVM::DICommonBlockAttr commonBlock = getOrCreateCommonBlockAttr(commonName, fileAttr, scopeAttr, line); mlir::LLVM::DITypeAttr diType = typeGen.convertType( fir::unwrapRefType(declOp.getType()), fileAttr, scopeAttr, declOp); - line = getLineFromLoc(declOp.getLoc()); + line = fir::getLineFromLoc(declOp.getLoc()); auto gvAttr = mlir::LLVM::DIGlobalVariableAttr::get( context, commonBlock, mlir::StringAttr::get(context, name), declOp.getUniqName(), fileAttr, line, diType, @@ -366,7 +366,7 @@ void AddDebugInfoPass::handleLocalVariable(Op declOp, llvm::StringRef name, auto localVarAttr = mlir::LLVM::DILocalVariableAttr::get( context, scopeAttr, mlir::StringAttr::get(context, name), fileAttr, - getLineFromLoc(declOp.getLoc()), argNo, /* alignInBits*/ 0, tyAttr, + fir::getLineFromLoc(declOp.getLoc()), argNo, /* alignInBits*/ 0, tyAttr, mlir::LLVM::DIFlags::Zero); declOp->setLoc(builder.getFusedLoc({declOp->getLoc()}, localVarAttr)); } @@ -447,7 +447,7 @@ mlir::LLVM::DIModuleAttr AddDebugInfoPass::getOrCreateModuleAttr( // caller's guess, which is derived from a member's declaration. if (auto iter{moduleDebugImportsByName.find(name)}; iter != moduleDebugImportsByName.end()) - line = getLineFromLoc(iter->second.getLoc()); + line = fir::getLineFromLoc(iter->second.getLoc()); // When decl is true, it means that module is only being used in this // compilation unit and it is defined elsewhere. But if the file/line/scope @@ -487,7 +487,7 @@ AddDebugInfoPass::getModuleAttrFromGlobalOp(fir::GlobalOp globalOp, // one). The isInitialized() seems to provide the right information // but inverted. It is true where module is actually defined but false where // it is used. - unsigned line = getLineFromLoc(globalOp.getLoc()); + unsigned line = fir::getLineFromLoc(globalOp.getLoc()); mlir::LLVM::DISubprogramAttr sp = mlir::dyn_cast_if_present(scope); @@ -518,7 +518,7 @@ void AddDebugInfoPass::handleGlobalOp(fir::GlobalOp globalOp, if (fir::NameUniquer::isSpecialSymbol(result.second.name)) return; - unsigned line = getLineFromLoc(globalOp.getLoc()); + unsigned line = fir::getLineFromLoc(globalOp.getLoc()); std::optional modOpt = getModuleAttrFromGlobalOp(globalOp, fileAttr, scope); if (modOpt) @@ -648,7 +648,7 @@ void AddDebugInfoPass::handleFuncOp(mlir::func::FuncOp funcOp, subprogramFlags = subprogramFlags | mlir::LLVM::DISubprogramFlags::Recursive; - unsigned line = getLineFromLoc(l); + unsigned line = fir::getLineFromLoc(l); if (fir::isInternalProcedure(funcOp)) { // For contained functions, the scope is the parent subroutine. mlir::SymbolRefAttr sym = mlir::cast( @@ -688,7 +688,7 @@ void AddDebugInfoPass::handleFuncOp(mlir::func::FuncOp funcOp, // target op and make sure that all the variables inside the target region // get the correct scope in the first place. funcOp.walk([&](mlir::omp::TargetOp targetOp) { - unsigned line = getLineFromLoc(targetOp.getLoc()); + unsigned line = fir::getLineFromLoc(targetOp.getLoc()); mlir::StringAttr name = getTargetFunctionName(context, targetOp.getLoc(), funcOp.getName()); mlir::LLVM::DISubprogramFlags flags = diff --git a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp index b6f3b6e1d4d67..96f7078044e78 100644 --- a/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp +++ b/flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp @@ -12,7 +12,7 @@ #define DEBUG_TYPE "flang-debug-type-generator" -#include "DebugTypeGenerator.h" +#include "flang/Optimizer/Transforms/DebugTypeGenerator.h" #include "flang/Optimizer/CodeGen/DescriptorModel.h" #include "flang/Optimizer/Support/InternalNames.h" #include "flang/Optimizer/Support/Utils.h"