Skip to content

Commit bff7664

Browse files
committed
[flang] Lowering support for -gdwarf-N flag.
This PR builds on the llvm#158314 and adds the lowering support for -gdwarf-N flag. The changes to pass the information to AddDebugInfo pass are mostly mechanical. The AddDebugInfo pass adds ModuleFlagsOp in the module. This gets translated to correct llvm metadata during mlir->llvmir translation. There is minor correction where the version is set to 0 in case no -debug-version flag is provided. Previously it was set to 2 in this case due to misreading of clang code.
1 parent e299d9a commit bff7664

File tree

8 files changed

+74
-9
lines changed

8 files changed

+74
-9
lines changed

flang/include/flang/Optimizer/Passes/Pipelines.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void addCompilerGeneratedNamesConversionPass(mlir::PassManager &pm);
102102
void addDebugInfoPass(mlir::PassManager &pm,
103103
llvm::codegenoptions::DebugInfoKind debugLevel,
104104
llvm::OptimizationLevel optLevel,
105-
llvm::StringRef inputFilename);
105+
llvm::StringRef inputFilename, int32_t dwarfVersion);
106106

107107
void addFIRToLLVMPass(mlir::PassManager &pm,
108108
const MLIRToLLVMPassPipelineConfig &config);
@@ -158,7 +158,7 @@ void createOpenMPFIRPassPipeline(mlir::PassManager &pm,
158158
void createDebugPasses(mlir::PassManager &pm,
159159
llvm::codegenoptions::DebugInfoKind debugLevel,
160160
llvm::OptimizationLevel OptLevel,
161-
llvm::StringRef inputFilename);
161+
llvm::StringRef inputFilename, int32_t dwarfVersion);
162162

163163
void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
164164
MLIRToLLVMPassPipelineConfig config,

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ def AddDebugInfo : Pass<"add-debug-info", "mlir::ModuleOp"> {
242242
"std::string",
243243
/*default=*/"std::string{}",
244244
"name of the input source file">,
245+
Option<"dwarfVersion", "dwarf-version",
246+
"int32_t",
247+
/*default=*/"0",
248+
"dwarf version">,
245249
];
246250
}
247251

flang/include/flang/Tools/CrossToolHelpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
108108
InstrumentFunctionEntry = "__cyg_profile_func_enter";
109109
InstrumentFunctionExit = "__cyg_profile_func_exit";
110110
}
111+
DwarfVersion = opts.DwarfVersion;
111112
}
112113

113114
llvm::OptimizationLevel OptLevel; ///< optimisation level
@@ -143,6 +144,7 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
143144
Fortran::frontend::CodeGenOptions::ComplexRangeKind ComplexRange =
144145
Fortran::frontend::CodeGenOptions::ComplexRangeKind::
145146
CX_Full; ///< Method for calculating complex number division
147+
int32_t DwarfVersion = 0; ///< Version of DWARF debug info to generate
146148
};
147149

148150
struct OffloadModuleOpts {

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,9 @@ static bool parseDebugArgs(Fortran::frontend::CodeGenOptions &opts,
157157
clang::DiagnosticsEngine::Warning, "Unsupported debug option: %0");
158158
diags.Report(debugWarning) << arg->getValue();
159159
}
160-
// The default value of 2 here is to match clang.
161160
opts.DwarfVersion =
162161
getLastArgIntValue(args, clang::driver::options::OPT_dwarf_version_EQ,
163-
/*Default=*/2, diags);
162+
/*Default=*/0, diags);
164163
}
165164
return true;
166165
}

flang/lib/Optimizer/Passes/Pipelines.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ getEmissionKind(llvm::codegenoptions::DebugInfoKind kind) {
9595
void addDebugInfoPass(mlir::PassManager &pm,
9696
llvm::codegenoptions::DebugInfoKind debugLevel,
9797
llvm::OptimizationLevel optLevel,
98-
llvm::StringRef inputFilename) {
98+
llvm::StringRef inputFilename, int32_t dwarfVersion) {
9999
fir::AddDebugInfoOptions options;
100100
options.debugLevel = getEmissionKind(debugLevel);
101101
options.isOptimized = optLevel != llvm::OptimizationLevel::O0;
102102
options.inputFilename = inputFilename;
103+
options.dwarfVersion = dwarfVersion;
103104
addPassConditionally(pm, disableDebugInfo,
104105
[&]() { return fir::createAddDebugInfoPass(options); });
105106
}
@@ -333,9 +334,9 @@ void createOpenMPFIRPassPipeline(mlir::PassManager &pm,
333334
void createDebugPasses(mlir::PassManager &pm,
334335
llvm::codegenoptions::DebugInfoKind debugLevel,
335336
llvm::OptimizationLevel OptLevel,
336-
llvm::StringRef inputFilename) {
337+
llvm::StringRef inputFilename, int32_t dwarfVersion) {
337338
if (debugLevel != llvm::codegenoptions::NoDebugInfo)
338-
addDebugInfoPass(pm, debugLevel, OptLevel, inputFilename);
339+
addDebugInfoPass(pm, debugLevel, OptLevel, inputFilename, dwarfVersion);
339340
}
340341

341342
void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
@@ -352,7 +353,8 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
352353
fir::addCodeGenRewritePass(
353354
pm, (config.DebugInfo != llvm::codegenoptions::NoDebugInfo));
354355
fir::addExternalNameConversionPass(pm, config.Underscoring);
355-
fir::createDebugPasses(pm, config.DebugInfo, config.OptLevel, inputFilename);
356+
fir::createDebugPasses(pm, config.DebugInfo, config.OptLevel, inputFilename,
357+
config.DwarfVersion);
356358
fir::addTargetRewritePass(pm);
357359
fir::addCompilerGeneratedNamesConversionPass(pm);
358360

flang/lib/Optimizer/Transforms/AddDebugInfo.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,19 @@ void AddDebugInfoPass::runOnOperation() {
649649
signalPassFailure();
650650
return;
651651
}
652+
mlir::OpBuilder builder(context);
653+
if (dwarfVersion > 0) {
654+
mlir::OpBuilder::InsertionGuard guard(builder);
655+
builder.setInsertionPointToEnd(module.getBody());
656+
llvm::SmallVector<mlir::Attribute> moduleFlags;
657+
mlir::IntegerType int32Ty = mlir::IntegerType::get(context, 32);
658+
moduleFlags.push_back(builder.getAttr<mlir::LLVM::ModuleFlagAttr>(
659+
mlir::LLVM::ModFlagBehavior::Max,
660+
mlir::StringAttr::get(context, "Dwarf Version"),
661+
mlir::IntegerAttr::get(int32Ty, dwarfVersion)));
662+
mlir::LLVM::ModuleFlagsOp::create(builder, module.getLoc(),
663+
builder.getArrayAttr(moduleFlags));
664+
}
652665
fir::DebugTypeGenerator typeGen(module, &symbolTable, *dl);
653666
// We need 2 type of file paths here.
654667
// 1. Name of the file as was presented to compiler. This can be absolute
@@ -686,7 +699,6 @@ void AddDebugInfoPass::runOnOperation() {
686699
module.walk([&](mlir::func::FuncOp funcOp) {
687700
handleFuncOp(funcOp, fileAttr, cuAttr, typeGen, &symbolTable);
688701
});
689-
mlir::OpBuilder builder(context);
690702
// We have processed all function. Attach common block variables to the
691703
// global that represent the storage.
692704
for (auto [global, exprs] : globalToGlobalExprsMap) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone -dwarf-version=5 %s \
2+
! RUN: -o - | FileCheck --check-prefix=CHECK-DWARF5 %s
3+
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=line-tables-only -dwarf-version=5 \
4+
! RUN: %s -o - | FileCheck --check-prefix=CHECK-DWARF5 %s
5+
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone -dwarf-version=4 %s \
6+
! RUN: -o - | FileCheck --check-prefix=CHECK-DWARF4 %s
7+
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone -dwarf-version=3 %s \
8+
! RUN: -o - | FileCheck --check-prefix=CHECK-DWARF3 %s
9+
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone -dwarf-version=2 %s \
10+
! RUN: -o - | FileCheck --check-prefix=CHECK-DWARF2 %s
11+
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone %s -o -\
12+
! RUN: | FileCheck --check-prefix=CHECK-WITHOUT-VERSION %s
13+
! RUN: %flang_fc1 -emit-llvm -dwarf-version=5 %s -o - \
14+
! RUN: | FileCheck --check-prefix=CHECK-WITHOUT-DEBUG-KIND %s
15+
16+
program test
17+
end program test
18+
19+
! CHECK-DWARF5: !{i32 7, !"Dwarf Version", i32 5}
20+
! CHECK-DWARF4: !{i32 7, !"Dwarf Version", i32 4}
21+
! CHECK-DWARF3: !{i32 7, !"Dwarf Version", i32 3}
22+
! CHECK-DWARF2: !{i32 7, !"Dwarf Version", i32 2}
23+
! CHECK-WITHOUT-VERSION-NOT: "Dwarf Version"
24+
! CHECK-WITHOUT-DEBUG-KIND-NOT: "Dwarf Version"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
// RUN: fir-opt --add-debug-info="dwarf-version=5" --mlir-print-debuginfo %s \
3+
// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
4+
// RUN: fir-opt --add-debug-info="dwarf-version=4" --mlir-print-debuginfo %s \
5+
// RUN: | FileCheck --check-prefix=CHECK-DWARF4 %s
6+
// RUN: fir-opt --add-debug-info="dwarf-version=3" --mlir-print-debuginfo %s \
7+
// RUN: | FileCheck --check-prefix=CHECK-DWARF3 %s
8+
// RUN: fir-opt --add-debug-info="dwarf-version=2" --mlir-print-debuginfo %s \
9+
// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
10+
// RUN: fir-opt --add-debug-info= --mlir-print-debuginfo %s \
11+
// RUN: | FileCheck --check-prefix=CHECK-WITHOUT-VERSION %s
12+
// REQUIRES: system-linux
13+
14+
module {
15+
} loc(#loc)
16+
#loc = loc("simple.f90":0:0)
17+
18+
// CHECK-DWARF5: llvm.module_flags [#llvm.mlir.module_flag<max, "Dwarf Version", 5 : i32>]
19+
// CHECK-DWARF4: llvm.module_flags [#llvm.mlir.module_flag<max, "Dwarf Version", 4 : i32>]
20+
// CHECK-DWARF3: llvm.module_flags [#llvm.mlir.module_flag<max, "Dwarf Version", 3 : i32>]
21+
// CHECK-DWARF2: llvm.module_flags [#llvm.mlir.module_flag<max, "Dwarf Version", 2 : i32>]
22+
// CHECK-WITHOUT-VERSION-NOT: llvm.module_flags

0 commit comments

Comments
 (0)