Repository navigation
[flang] Remove hardcoded bits from AddDebugInfo. #89231
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,12 +11,14 @@ | |
| /// This pass populates some debug information for the module and functions. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "flang/Common/Version.h" | ||
| #include "flang/Optimizer/Builder/FIRBuilder.h" | ||
| #include "flang/Optimizer/Builder/Todo.h" | ||
| #include "flang/Optimizer/Dialect/FIRDialect.h" | ||
| #include "flang/Optimizer/Dialect/FIROps.h" | ||
| #include "flang/Optimizer/Dialect/FIRType.h" | ||
| #include "flang/Optimizer/Dialect/Support/FIRContext.h" | ||
| #include "flang/Optimizer/Support/InternalNames.h" | ||
| #include "flang/Optimizer/Transforms/Passes.h" | ||
| #include "mlir/Dialect/Func/IR/FuncOps.h" | ||
| #include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
|
|
@@ -28,12 +30,12 @@ | |
| #include "mlir/Transforms/RegionUtils.h" | ||
| #include "llvm/BinaryFormat/Dwarf.h" | ||
| #include "llvm/Support/Debug.h" | ||
| #include "llvm/Support/FileSystem.h" | ||
| #include "llvm/Support/Path.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
|
|
||
| namespace fir { | ||
| #define GEN_PASS_DEF_ADDDEBUGINFO | ||
| #define GEN_PASS_DECL_ADDDEBUGINFO | ||
| #include "flang/Optimizer/Transforms/Passes.h.inc" | ||
| } // namespace fir | ||
|
|
||
|
|
@@ -43,6 +45,7 @@ namespace { | |
|
|
||
| class AddDebugInfoPass : public fir::impl::AddDebugInfoBase<AddDebugInfoPass> { | ||
| public: | ||
| AddDebugInfoPass(fir::AddDebugInfoOptions options) : Base(options) {} | ||
| void runOnOperation() override; | ||
| }; | ||
|
|
||
|
|
@@ -52,21 +55,40 @@ void AddDebugInfoPass::runOnOperation() { | |
| mlir::ModuleOp module = getOperation(); | ||
| mlir::MLIRContext *context = &getContext(); | ||
| mlir::OpBuilder builder(context); | ||
| std::string inputFilePath("-"); | ||
| if (auto fileLoc = module.getLoc().dyn_cast<mlir::FileLineColLoc>()) | ||
| inputFilePath = fileLoc.getFilename().getValue(); | ||
| llvm::StringRef fileName; | ||
| std::string filePath; | ||
|
Comment on lines
+58
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does DWARF support paths with wide characters?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that some of the functions in |
||
| // We need 2 type of file paths here. | ||
| // 1. Name of the file as was presented to compiler. This can be absolute | ||
| // or relative to 2. | ||
| // 2. Current working directory | ||
| // | ||
| // We are also dealing with 2 different situations below. One is normal | ||
| // compilation where we will have a value in 'inputFilename' and we can | ||
| // obtain the current directory using 'current_path'. | ||
| // The 2nd case is when this pass is invoked directly from 'fir-opt' tool. | ||
| // In that case, 'inputFilename' may be empty. Location embedded in the | ||
| // module will be used to get file name and its directory. | ||
|
Comment on lines
+65
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not always using the module location since it must have been provided?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In ideal case, we would like it to be the current working directory as I explained above. But we don't have that information in 2nd case so using parent directory is sort of a compromise. |
||
| if (inputFilename.empty()) { | ||
| if (auto fileLoc = module.getLoc().dyn_cast<mlir::FileLineColLoc>()) { | ||
| fileName = llvm::sys::path::filename(fileLoc.getFilename().getValue()); | ||
| filePath = llvm::sys::path::parent_path(fileLoc.getFilename().getValue()); | ||
| } else | ||
| fileName = "-"; | ||
| } else { | ||
| fileName = inputFilename; | ||
| llvm::SmallString<256> cwd; | ||
| if (!llvm::sys::fs::current_path(cwd)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I explained in my earlier comment, |
||
| filePath = cwd.str(); | ||
| } | ||
|
|
||
| auto getFileAttr = [context](llvm::StringRef path) -> mlir::LLVM::DIFileAttr { | ||
| return mlir::LLVM::DIFileAttr::get(context, llvm::sys::path::filename(path), | ||
| llvm::sys::path::parent_path(path)); | ||
| }; | ||
|
|
||
| mlir::LLVM::DIFileAttr fileAttr = getFileAttr(inputFilePath); | ||
| mlir::StringAttr producer = mlir::StringAttr::get(context, "Flang"); | ||
| mlir::LLVM::DIFileAttr fileAttr = | ||
| mlir::LLVM::DIFileAttr::get(context, fileName, filePath); | ||
| mlir::StringAttr producer = | ||
| mlir::StringAttr::get(context, Fortran::common::getFlangFullVersion()); | ||
| mlir::LLVM::DICompileUnitAttr cuAttr = mlir::LLVM::DICompileUnitAttr::get( | ||
| mlir::DistinctAttr::create(mlir::UnitAttr::get(context)), | ||
| llvm::dwarf::getLanguage("DW_LANG_Fortran95"), fileAttr, producer, | ||
| /*isOptimized=*/false, mlir::LLVM::DIEmissionKind::LineTablesOnly); | ||
| isOptimized, debugLevel); | ||
|
|
||
| module.walk([&](mlir::func::FuncOp funcOp) { | ||
| mlir::Location l = funcOp->getLoc(); | ||
|
|
@@ -75,43 +97,46 @@ void AddDebugInfoPass::runOnOperation() { | |
| if (l.dyn_cast<mlir::FusedLoc>()) | ||
| return; | ||
|
|
||
| llvm::StringRef funcFilePath; | ||
| if (l.dyn_cast<mlir::FileLineColLoc>()) | ||
| funcFilePath = | ||
| l.dyn_cast<mlir::FileLineColLoc>().getFilename().getValue(); | ||
| else | ||
| funcFilePath = inputFilePath; | ||
| unsigned int CC = (funcOp.getName() == fir::NameUniquer::doProgramEntry()) | ||
| ? llvm::dwarf::getCallingConvention("DW_CC_program") | ||
| : llvm::dwarf::getCallingConvention("DW_CC_normal"); | ||
|
|
||
| if (auto funcLoc = l.dyn_cast<mlir::FileLineColLoc>()) { | ||
| fileName = llvm::sys::path::filename(funcLoc.getFilename().getValue()); | ||
| filePath = llvm::sys::path::parent_path(funcLoc.getFilename().getValue()); | ||
| } | ||
|
|
||
| mlir::StringAttr funcName = | ||
| mlir::StringAttr::get(context, funcOp.getName()); | ||
| mlir::LLVM::DIBasicTypeAttr bT = mlir::LLVM::DIBasicTypeAttr::get( | ||
| context, llvm::dwarf::DW_TAG_base_type, "void", /*sizeInBits=*/0, | ||
| /*encoding=*/1); | ||
| mlir::LLVM::DISubroutineTypeAttr subTypeAttr = | ||
| mlir::LLVM::DISubroutineTypeAttr::get( | ||
| context, llvm::dwarf::getCallingConvention("DW_CC_normal"), | ||
| {bT, bT}); | ||
| mlir::LLVM::DIFileAttr funcFileAttr = getFileAttr(funcFilePath); | ||
| mlir::LLVM::DISubroutineTypeAttr::get(context, CC, {bT, bT}); | ||
|
abidh marked this conversation as resolved.
|
||
| mlir::LLVM::DIFileAttr funcFileAttr = | ||
| mlir::LLVM::DIFileAttr::get(context, fileName, filePath); | ||
|
|
||
| // Only definitions need a distinct identifier and a compilation unit. | ||
| mlir::DistinctAttr id; | ||
| mlir::LLVM::DICompileUnitAttr compilationUnit; | ||
| auto subprogramFlags = mlir::LLVM::DISubprogramFlags::Optimized; | ||
| mlir::LLVM::DISubprogramFlags subprogramFlags = | ||
| mlir::LLVM::DISubprogramFlags{}; | ||
| if (isOptimized) | ||
| subprogramFlags = mlir::LLVM::DISubprogramFlags::Optimized; | ||
| if (!funcOp.isExternal()) { | ||
| id = mlir::DistinctAttr::create(mlir::UnitAttr::get(context)); | ||
| compilationUnit = cuAttr; | ||
| subprogramFlags = | ||
| subprogramFlags | mlir::LLVM::DISubprogramFlags::Definition; | ||
| subprogramFlags = mlir::LLVM::DISubprogramFlags::Definition; | ||
|
jeanPerier marked this conversation as resolved.
Outdated
|
||
| } | ||
| auto spAttr = mlir::LLVM::DISubprogramAttr::get( | ||
| context, id, compilationUnit, fileAttr, funcName, funcName, | ||
| funcFileAttr, | ||
| /*line=*/1, | ||
| /*scopeline=*/1, subprogramFlags, subTypeAttr); | ||
| funcFileAttr, /*line=*/1, /*scopeline=*/1, subprogramFlags, | ||
|
abidh marked this conversation as resolved.
|
||
| subTypeAttr); | ||
| funcOp->setLoc(builder.getFusedLoc({funcOp->getLoc()}, spAttr)); | ||
| }); | ||
| } | ||
|
|
||
| std::unique_ptr<mlir::Pass> fir::createAddDebugInfoPass() { | ||
| return std::make_unique<AddDebugInfoPass>(); | ||
| std::unique_ptr<mlir::Pass> | ||
| fir::createAddDebugInfoPass(fir::AddDebugInfoOptions options) { | ||
| return std::make_unique<AddDebugInfoPass>(options); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.