summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAbid Qadeer <haqadeer@amd.com>2024-04-23 19:11:43 +0100
committerGitHub <noreply@github.com>2024-04-23 19:11:43 +0100
commit5f3f9d1a905b1f293d81758db3fa20c2a1dc3ff4 (patch)
treef295634fec7582387934cfd02b2a2bc7425ba482
parent5fe93b0a4d91f1beb801e3a7588e1fa43955af15 (diff)
[flang] Remove hardcoded bits from AddDebugInfo. (#89231)
This PR adds following options to the AddDebugInfo pass. 1. IsOptimized flag. 2. Level of debug info to generate. 3. Name of the source file This enables us to remove the hard coded values from the code. It also allows us to test the pass with different options. The tests have been modified to take advantage of that. The calling convention flag and producer name have also been improved.
-rw-r--r--flang/include/flang/Optimizer/Transforms/Passes.h4
-rw-r--r--flang/include/flang/Optimizer/Transforms/Passes.td19
-rw-r--r--flang/include/flang/Tools/CLOptions.inc58
-rw-r--r--flang/lib/Frontend/FrontendActions.cpp2
-rw-r--r--flang/lib/Optimizer/Transforms/AddDebugInfo.cpp84
-rw-r--r--flang/lib/Optimizer/Transforms/CMakeLists.txt1
-rw-r--r--flang/test/Transforms/debug-line-table-inc-file.fir4
-rw-r--r--flang/test/Transforms/debug-line-table.fir8
8 files changed, 121 insertions, 59 deletions
diff --git a/flang/include/flang/Optimizer/Transforms/Passes.h b/flang/include/flang/Optimizer/Transforms/Passes.h
index 4d290d87d4cc..402a7a3f8752 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.h
+++ b/flang/include/flang/Optimizer/Transforms/Passes.h
@@ -47,6 +47,7 @@ namespace fir {
#define GEN_PASS_DECL_ALGEBRAICSIMPLIFICATION
#define GEN_PASS_DECL_POLYMORPHICOPCONVERSION
#define GEN_PASS_DECL_OPENACCDATAOPERANDCONVERSION
+#define GEN_PASS_DECL_ADDDEBUGINFO
#include "flang/Optimizer/Transforms/Passes.h.inc"
std::unique_ptr<mlir::Pass> createAffineDemotionPass();
@@ -64,7 +65,8 @@ std::unique_ptr<mlir::Pass> createMemoryAllocationPass();
std::unique_ptr<mlir::Pass> createStackArraysPass();
std::unique_ptr<mlir::Pass> createAliasTagsPass();
std::unique_ptr<mlir::Pass> createSimplifyIntrinsicsPass();
-std::unique_ptr<mlir::Pass> createAddDebugInfoPass();
+std::unique_ptr<mlir::Pass>
+createAddDebugInfoPass(fir::AddDebugInfoOptions options = {});
std::unique_ptr<mlir::Pass> createLoopVersioningPass();
std::unique_ptr<mlir::Pass>
diff --git a/flang/include/flang/Optimizer/Transforms/Passes.td b/flang/include/flang/Optimizer/Transforms/Passes.td
index 467b7e1c472e..c0b32459a935 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.td
+++ b/flang/include/flang/Optimizer/Transforms/Passes.td
@@ -202,6 +202,25 @@ def AddDebugInfo : Pass<"add-debug-info", "mlir::ModuleOp"> {
let dependentDialects = [
"fir::FIROpsDialect", "mlir::func::FuncDialect", "mlir::LLVM::LLVMDialect"
];
+ let options = [
+ Option<"debugLevel", "debug-level",
+ "mlir::LLVM::DIEmissionKind",
+ /*default=*/"mlir::LLVM::DIEmissionKind::Full",
+ "debug level",
+ [{::llvm::cl::values(
+ clEnumValN(mlir::LLVM::DIEmissionKind::Full, "Full", "Emit full debug info"),
+ clEnumValN(mlir::LLVM::DIEmissionKind::LineTablesOnly, "LineTablesOnly", "Emit line tables only"),
+ clEnumValN(mlir::LLVM::DIEmissionKind::None, "None", "Emit no debug information")
+ )}]
+ >,
+ Option<"isOptimized", "is-optimized",
+ "bool", /*default=*/"false",
+ "is optimized.">,
+ Option<"inputFilename", "file-name",
+ "std::string",
+ /*default=*/"std::string{}",
+ "name of the input source file">,
+ ];
}
// This needs to be a "mlir::ModuleOp" pass, because it inserts simplified
diff --git a/flang/include/flang/Tools/CLOptions.inc b/flang/include/flang/Tools/CLOptions.inc
index 44ff2b3f70ff..f02c969ca1c3 100644
--- a/flang/include/flang/Tools/CLOptions.inc
+++ b/flang/include/flang/Tools/CLOptions.inc
@@ -179,9 +179,27 @@ inline void addTargetRewritePass(mlir::PassManager &pm) {
});
}
-inline void addDebugInfoPass(mlir::PassManager &pm) {
- addPassConditionally(
- pm, disableDebugInfo, [&]() { return fir::createAddDebugInfoPass(); });
+inline mlir::LLVM::DIEmissionKind getEmissionKind(
+ llvm::codegenoptions::DebugInfoKind kind) {
+ switch (kind) {
+ case llvm::codegenoptions::DebugInfoKind::FullDebugInfo:
+ return mlir::LLVM::DIEmissionKind::Full;
+ case llvm::codegenoptions::DebugInfoKind::DebugLineTablesOnly:
+ return mlir::LLVM::DIEmissionKind::LineTablesOnly;
+ default:
+ return mlir::LLVM::DIEmissionKind::None;
+ }
+}
+
+inline void addDebugInfoPass(mlir::PassManager &pm,
+ llvm::codegenoptions::DebugInfoKind debugLevel,
+ llvm::OptimizationLevel optLevel, llvm::StringRef inputFilename) {
+ fir::AddDebugInfoOptions options;
+ options.debugLevel = getEmissionKind(debugLevel);
+ options.isOptimized = optLevel != llvm::OptimizationLevel::O0;
+ options.inputFilename = inputFilename;
+ addPassConditionally(pm, disableDebugInfo,
+ [&]() { return fir::createAddDebugInfoPass(options); });
}
inline void addFIRToLLVMPass(
@@ -307,32 +325,21 @@ inline void createOpenMPFIRPassPipeline(
}
#if !defined(FLANG_EXCLUDE_CODEGEN)
-inline void createDebugPasses(
- mlir::PassManager &pm, llvm::codegenoptions::DebugInfoKind debugLevel) {
- // Currently only -g1, -g, -gline-tables-only supported
- switch (debugLevel) {
- case llvm::codegenoptions::DebugLineTablesOnly:
- addDebugInfoPass(pm);
- return;
- case llvm::codegenoptions::NoDebugInfo:
- return;
- default:
- // TODO: Add cases and passes for other debug options.
- // All other debug options not implemented yet, currently emits warning
- // and generates as much debug information as possible.
- addDebugInfoPass(pm);
- return;
- }
+inline void createDebugPasses(mlir::PassManager &pm,
+ llvm::codegenoptions::DebugInfoKind debugLevel,
+ llvm::OptimizationLevel OptLevel, llvm::StringRef inputFilename) {
+ if (debugLevel != llvm::codegenoptions::NoDebugInfo)
+ addDebugInfoPass(pm, debugLevel, OptLevel, inputFilename);
}
-inline void createDefaultFIRCodeGenPassPipeline(
- mlir::PassManager &pm, MLIRToLLVMPassPipelineConfig config) {
+inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
+ MLIRToLLVMPassPipelineConfig config, llvm::StringRef inputFilename = {}) {
fir::addBoxedProcedurePass(pm);
addNestedPassToAllTopLevelOperations(pm, fir::createAbstractResultOpt);
fir::addCodeGenRewritePass(pm);
fir::addTargetRewritePass(pm);
fir::addExternalNameConversionPass(pm, config.Underscoring);
- fir::createDebugPasses(pm, config.DebugInfo);
+ fir::createDebugPasses(pm, config.DebugInfo, config.OptLevel, inputFilename);
if (config.VScaleMin != 0)
pm.addPass(fir::createVScaleAttrPass({config.VScaleMin, config.VScaleMax}));
@@ -366,15 +373,16 @@ inline void createDefaultFIRCodeGenPassPipeline(
/// \param pm - MLIR pass manager that will hold the pipeline definition
/// \param optLevel - optimization level used for creating FIR optimization
/// passes pipeline
-inline void createMLIRToLLVMPassPipeline(
- mlir::PassManager &pm, const MLIRToLLVMPassPipelineConfig &config) {
+inline void createMLIRToLLVMPassPipeline(mlir::PassManager &pm,
+ const MLIRToLLVMPassPipelineConfig &config,
+ llvm::StringRef inputFilename = {}) {
fir::createHLFIRToFIRPassPipeline(pm, config.OptLevel);
// Add default optimizer pass pipeline.
fir::createDefaultFIROptimizerPassPipeline(pm, config);
// Add codegen pass pipeline.
- fir::createDefaultFIRCodeGenPassPipeline(pm, config);
+ fir::createDefaultFIRCodeGenPassPipeline(pm, config, inputFilename);
}
#undef FLANG_EXCLUDE_CODEGEN
#endif
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 8f251997ed40..d91846dde95a 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -809,7 +809,7 @@ void CodeGenAction::generateLLVMIR() {
}
// Create the pass pipeline
- fir::createMLIRToLLVMPassPipeline(pm, config);
+ fir::createMLIRToLLVMPassPipeline(pm, config, getCurrentFile());
(void)mlir::applyPassManagerCLOptions(pm);
// run the pass manager
diff --git a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
index 4ca338066128..68584bef055b 100644
--- a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
+++ b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
@@ -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;
+ // 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.
+ 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))
+ 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,49 @@ 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);
+ // FIXME: Provide proper type for subroutine
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});
+ 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;
}
+ // FIXME: Provide proper line and scopeline.
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,
+ 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);
}
diff --git a/flang/lib/Optimizer/Transforms/CMakeLists.txt b/flang/lib/Optimizer/Transforms/CMakeLists.txt
index d55655c53906..fc08d67540ce 100644
--- a/flang/lib/Optimizer/Transforms/CMakeLists.txt
+++ b/flang/lib/Optimizer/Transforms/CMakeLists.txt
@@ -35,6 +35,7 @@ add_flang_library(FIRTransforms
FIRDialect
FIRDialectSupport
FIRSupport
+ FortranCommon
HLFIRDialect
MLIRAffineUtils
MLIRFuncDialect
diff --git a/flang/test/Transforms/debug-line-table-inc-file.fir b/flang/test/Transforms/debug-line-table-inc-file.fir
index be4f005bf664..dc75482d4f8a 100644
--- a/flang/test/Transforms/debug-line-table-inc-file.fir
+++ b/flang/test/Transforms/debug-line-table-inc-file.fir
@@ -1,5 +1,5 @@
-// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
+// RUN: fir-opt --add-debug-info="debug-level=LineTablesOnly" --mlir-print-debuginfo %s | FileCheck %s
// REQUIRES: system-linux
// Test for included functions that have a different debug location than the current file
@@ -30,7 +30,7 @@ module attributes {} {
// CHECK: #[[MODULE_LOC]] = loc("{{.*}}simple.f90":0:0)
// CHECK: #[[LOC_INC_FILE:.*]] = loc("{{.*}}inc.f90":1:1)
// CHECK: #[[LOC_FILE:.*]] = loc("{{.*}}simple.f90":3:1)
-// CHECK: #[[DI_CU:.*]] = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #[[DI_FILE]], producer = "Flang", isOptimized = false, emissionKind = LineTablesOnly>
+// CHECK: #[[DI_CU:.*]] = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #[[DI_FILE]], producer = "flang{{.*}}", isOptimized = false, emissionKind = LineTablesOnly>
// CHECK: #[[DI_SP_INC:.*]] = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #[[DI_CU]], scope = #[[DI_FILE]], name = "_QPsinc", linkageName = "_QPsinc", file = #[[DI_INC_FILE]], {{.*}}>
// CHECK: #[[DI_SP:.*]] = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #[[DI_CU]], scope = #[[DI_FILE]], name = "_QQmain", linkageName = "_QQmain", file = #[[DI_FILE]], {{.*}}>
// CHECK: #[[FUSED_LOC_INC_FILE]] = loc(fused<#[[DI_SP_INC]]>[#[[LOC_INC_FILE]]])
diff --git a/flang/test/Transforms/debug-line-table.fir b/flang/test/Transforms/debug-line-table.fir
index 0ba88d3d9f7f..3b3a39174df0 100644
--- a/flang/test/Transforms/debug-line-table.fir
+++ b/flang/test/Transforms/debug-line-table.fir
@@ -1,5 +1,7 @@
-// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
+// RUN: fir-opt --add-debug-info="debug-level=Full" --mlir-print-debuginfo %s | FileCheck %s --check-prefix=FULL
+// RUN: fir-opt --add-debug-info="debug-level=LineTablesOnly" --mlir-print-debuginfo %s | FileCheck %s --check-prefix=LINETABLE
+// RUN: fir-opt --add-debug-info="is-optimized=true" --mlir-print-debuginfo %s | FileCheck %s --check-prefix=OPT
module attributes { fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.data_layout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", llvm.target_triple = "aarch64-unknown-linux-gnu"} {
func.func @_QPsb() {
@@ -22,7 +24,9 @@ module attributes { fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.dat
// CHECK: #[[MODULE_LOC]] = loc("[[DIR_NAME]]/[[FILE_NAME]]":1:1)
// CHECK: #[[SB_LOC]] = loc("./simple.f90":2:1)
// CHECK: #[[DECL_LOC:.*]] = loc("./simple.f90":10:1)
-// CHECK: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "Flang", isOptimized = false, emissionKind = LineTablesOnly>
+// FULL: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "flang{{.*}}", isOptimized = false, emissionKind = Full>
+// OPT: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "flang{{.*}}", isOptimized = true, emissionKind = Full>
+// LINETABLE: #di_compile_unit = #llvm.di_compile_unit<id = distinct[{{.*}}]<>, sourceLanguage = DW_LANG_Fortran95, file = #di_file, producer = "flang{{.*}}", isOptimized = false, emissionKind = LineTablesOnly>
// CHECK: #di_subroutine_type = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #di_basic_type, #di_basic_type>
// CHECK: #[[SB_SUBPROGRAM:.*]] = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #di_compile_unit, scope = #di_file, name = "[[SB_NAME]]", linkageName = "[[SB_NAME]]", file = #di_file, line = 1, scopeLine = 1, subprogramFlags = "Definition|Optimized", type = #di_subroutine_type>
// CHECK: #[[DECL_SUBPROGRAM:.*]] = #llvm.di_subprogram<scope = #di_file, name = "[[DECL_NAME]]", linkageName = "[[DECL_NAME]]", file = #di_file, line = 1, scopeLine = 1, subprogramFlags = Optimized, type = #di_subroutine_type>