SBallantyne created this revision.
SBallantyne added reviewers: MaskRay, awarzynski, DavidTruby, 
kiranchandramohan, tblah.
Herald added a reviewer: sscalpone.
Herald added a subscriber: sunshaoce.
Herald added projects: Flang, All.
SBallantyne requested review of this revision.
Herald added subscribers: cfe-commits, jdoerfert.
Herald added a project: clang.

While a pass exists to generate basic debug information, currently there is not 
a corresponding flag to enable it.
This patch adds support for activating this pass at any debug level >= -g1, as 
well as emiting a warning for higher levels that the functionality is not yet 
fully implemented.

This patch also adds -g and -gline-tables-only to appear when `flang-new` 
--help is run


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146814

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/CodeGenOptions.def
  flang/include/flang/Frontend/CodeGenOptions.h
  flang/include/flang/Tools/CLOptions.inc
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help-hidden.f90
  flang/test/Driver/driver-help.f90

Index: flang/test/Driver/driver-help.f90
===================================================================
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -56,6 +56,8 @@
 ! HELP-NEXT: -fsyntax-only          Run the preprocessor, parser and semantic analysis stages
 ! HELP-NEXT: -funderscoring         Appends one trailing underscore to external names
 ! HELP-NEXT: -fxor-operator         Enable .XOR. as a synonym of .NEQV.
+! HELP-NEXT: -gline-tables-only     Emit debug line number tables only
+! HELP-NEXT: -g                     Generate source-level debug information
 ! HELP-NEXT: -help                  Display available options
 ! HELP-NEXT: -I <dir>               Add directory to the end of the list of include search paths
 ! HELP-NEXT: -mllvm=<arg>           Alias for -mllvm
Index: flang/test/Driver/driver-help-hidden.f90
===================================================================
--- flang/test/Driver/driver-help-hidden.f90
+++ flang/test/Driver/driver-help-hidden.f90
@@ -60,6 +60,8 @@
 ! CHECK-NEXT: -fsyntax-only          Run the preprocessor, parser and semantic analysis stages
 ! CHECK-NEXT: -funderscoring         Appends one trailing underscore to external names
 ! CHECK-NEXT: -fxor-operator         Enable .XOR. as a synonym of .NEQV.
+! CHECK-NEXT: -gline-tables-only     Emit debug line number tables only
+! CHECK-NEXT: -g                     Generate source-level debug information
 ! CHECK-NEXT: -help     Display available options
 ! CHECK-NEXT: -I <dir>               Add directory to the end of the list of include search paths
 ! CHECK-NEXT: -mllvm=<arg>           Alias for -mllvm
Index: flang/lib/Frontend/FrontendActions.cpp
===================================================================
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -55,6 +55,7 @@
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
+#include <clang/Basic/DebugInfoOptions.h>
 #include <memory>
 
 using namespace Fortran::frontend;
@@ -555,7 +556,7 @@
 
   // Create the pass pipeline
   fir::createMLIRToLLVMPassPipeline(pm, level, opts.StackArrays,
-                                    opts.Underscoring);
+                                    opts.Underscoring, opts.getDebugInfo());
   mlir::applyPassManagerCLOptions(pm);
 
   // run the pass manager
Index: flang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- flang/lib/Frontend/CompilerInvocation.cpp
+++ flang/lib/Frontend/CompilerInvocation.cpp
@@ -35,6 +35,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TargetParser/Host.h"
 #include "llvm/TargetParser/Triple.h"
+#include <clang/Basic/DebugInfoOptions.h>
 #include <memory>
 #include <optional>
 
@@ -117,9 +118,43 @@
   return true;
 }
 
+static void parseDebugArgs(Fortran::frontend::CodeGenOptions &opts,
+                           llvm::opt::ArgList &args,
+                           clang::DiagnosticsEngine &diags) {
+  if (llvm::opt::Arg *arg =
+          args.getLastArg(clang::driver::options::OPT_debug_info_kind_EQ)) {
+    unsigned val =
+        llvm::StringSwitch<unsigned>(arg->getValue())
+            .Case("line-tables-only",
+                  clang::codegenoptions::DebugLineTablesOnly)
+            .Case("line-directives-only",
+                  clang::codegenoptions::DebugDirectivesOnly)
+            .Case("constructor", clang::codegenoptions::DebugInfoConstructor)
+            .Case("limited", clang::codegenoptions::LimitedDebugInfo)
+            .Case("standalone", clang::codegenoptions::FullDebugInfo)
+            .Case("unused-types", clang::codegenoptions::UnusedTypeInfo)
+            .Default(~0U);
+    if (val == ~0U) {
+      diags.Report(clang::diag::err_drv_invalid_value)
+          << arg->getAsString(args) << arg->getValue();
+    } else {
+      opts.setDebugInfo(static_cast<clang::codegenoptions::DebugInfoKind>(val));
+    }
+    if (val != clang::codegenoptions::DebugLineTablesOnly &&
+        val != clang::codegenoptions::NoDebugInfo) {
+      // TODO: This is not great, could be improved
+      const auto debugErr =
+          diags.getCustomDiagID(clang::DiagnosticsEngine::Warning,
+                                "Debug option '%0' is not implemented");
+      diags.Report(debugErr) << arg->getAsString(args);
+    }
+  }
+}
+
 static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
                              llvm::opt::ArgList &args,
                              clang::DiagnosticsEngine &diags) {
+  parseDebugArgs(opts, args, diags);
   opts.OptimizationLevel = getOptimizationLevel(args, diags);
 
   if (args.hasFlag(clang::driver::options::OPT_fdebug_pass_manager,
Index: flang/include/flang/Tools/CLOptions.inc
===================================================================
--- flang/include/flang/Tools/CLOptions.inc
+++ flang/include/flang/Tools/CLOptions.inc
@@ -18,6 +18,7 @@
 #include "flang/Optimizer/Transforms/Passes.h"
 #include "llvm/Passes/OptimizationLevel.h"
 #include "llvm/Support/CommandLine.h"
+#include <clang/Basic/DebugInfoOptions.h>
 
 #define DisableOption(DOName, DOOption, DODescription) \
   static llvm::cl::opt<bool> disable##DOName("disable-" DOOption, \
@@ -56,6 +57,9 @@
 const static llvm::OptimizationLevel &defaultOptLevel{
     llvm::OptimizationLevel::O0};
 
+const static clang::codegenoptions::DebugInfoKind &NoDebugInfo{
+    clang::codegenoptions::NoDebugInfo};
+
 /// Optimizer Passes
 DisableOption(CfgConversion, "cfg-conversion", "disable FIR to CFG pass");
 DisableOption(FirAvc, "avc", "array value copy analysis and transformation");
@@ -228,9 +232,28 @@
 }
 
 #if !defined(FLANG_EXCLUDE_CODEGEN)
+inline void createDebugPasses(
+    mlir::PassManager &pm, clang::codegenoptions::DebugInfoKind debugLevel) {
+  // Currently only -g1, -g, -gline-tables-only supported
+  switch(debugLevel) {
+    case clang::codegenoptions::DebugLineTablesOnly:
+      addDebugFoundationPass(pm);
+      return;
+    case clang::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.
+      addDebugFoundationPass(pm);
+      return;
+  }
+}
+
 inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
     llvm::OptimizationLevel optLevel = defaultOptLevel,
-    bool underscoring = true) {
+    bool underscoring = true,
+    clang::codegenoptions::DebugInfoKind debugInfo = NoDebugInfo) {
   fir::addBoxedProcedurePass(pm);
   pm.addNestedPass<mlir::func::FuncOp>(
       fir::createAbstractResultOnFuncOptPass());
@@ -238,6 +261,7 @@
   fir::addCodeGenRewritePass(pm);
   fir::addTargetRewritePass(pm);
   fir::addExternalNameConversionPass(pm, underscoring);
+  fir::createDebugPasses(pm, debugInfo);
   fir::addFIRToLLVMPass(pm, optLevel);
 }
 
@@ -248,14 +272,15 @@
 ///   passes pipeline
 inline void createMLIRToLLVMPassPipeline(mlir::PassManager &pm,
     llvm::OptimizationLevel optLevel = defaultOptLevel,
-    bool stackArrays = false, bool underscoring = true) {
+    bool stackArrays = false, bool underscoring = true,
+    clang::codegenoptions::DebugInfoKind debugInfo = NoDebugInfo) {
   fir::createHLFIRToFIRPassPipeline(pm, optLevel);
 
   // Add default optimizer pass pipeline.
   fir::createDefaultFIROptimizerPassPipeline(pm, optLevel, stackArrays);
 
   // Add codegen pass pipeline.
-  fir::createDefaultFIRCodeGenPassPipeline(pm, optLevel, underscoring);
+  fir::createDefaultFIRCodeGenPassPipeline(pm, optLevel, underscoring, debugInfo);
 }
 #undef FLANG_EXCLUDE_CODEGEN
 #endif
Index: flang/include/flang/Frontend/CodeGenOptions.h
===================================================================
--- flang/include/flang/Frontend/CodeGenOptions.h
+++ flang/include/flang/Frontend/CodeGenOptions.h
@@ -15,6 +15,7 @@
 #ifndef LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 #define LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 
+#include "clang/Basic/DebugInfoOptions.h"
 #include "llvm/Support/CodeGen.h"
 #include "llvm/Support/Regex.h"
 #include "llvm/Target/TargetOptions.h"
Index: flang/include/flang/Frontend/CodeGenOptions.def
===================================================================
--- flang/include/flang/Frontend/CodeGenOptions.def
+++ flang/include/flang/Frontend/CodeGenOptions.def
@@ -34,6 +34,7 @@
 
 CODEGENOPT(Underscoring, 1, 1)
 ENUM_CODEGENOPT(RelocationModel, llvm::Reloc::Model, 3, llvm::Reloc::PIC_) ///< Name of the relocation model to use.
+ENUM_CODEGENOPT(DebugInfo,  clang::codegenoptions::DebugInfoKind, 4,  clang::codegenoptions::NoDebugInfo) ///< Level of debug info to generate
 
 #undef CODEGENOPT
 #undef ENUM_CODEGENOPT
Index: clang/lib/Driver/ToolChains/Flang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Flang.cpp
+++ clang/lib/Driver/ToolChains/Flang.cpp
@@ -10,6 +10,7 @@
 #include "Flang.h"
 #include "CommonArgs.h"
 
+#include "clang/Basic/DebugInfoOptions.h"
 #include "clang/Driver/Options.h"
 
 #include <cassert>
@@ -27,6 +28,49 @@
   CmdArgs.push_back(types::getTypeName(Input.getType()));
 }
 
+// Convert an arg of the form "-gN" or one of their aliases
+// to the corresponding DebugInfoKind.
+static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) {
+  assert(A.getOption().matches(options::OPT_gN_Group) &&
+         "Not a -g option that specifies a debug-info level");
+  if (A.getOption().matches(options::OPT_g0) ||
+      A.getOption().matches(options::OPT_ggdb0))
+    return codegenoptions::NoDebugInfo;
+  if (A.getOption().matches(options::OPT_gline_tables_only) ||
+      A.getOption().matches(options::OPT_ggdb1))
+    return codegenoptions::DebugLineTablesOnly;
+  if (A.getOption().matches(options::OPT_gline_directives_only))
+    return codegenoptions::DebugDirectivesOnly;
+  return codegenoptions::DebugInfoConstructor;
+}
+
+static void
+addFortranDebugOptions(ArgStringList &CmdArgs,
+                       codegenoptions::DebugInfoKind DebugInfoKind) {
+  switch (DebugInfoKind) {
+  case codegenoptions::DebugDirectivesOnly:
+    CmdArgs.push_back("-debug-info-kind=line-directives-only");
+    break;
+  case codegenoptions::DebugLineTablesOnly:
+    CmdArgs.push_back("-debug-info-kind=line-tables-only");
+    break;
+  case codegenoptions::DebugInfoConstructor:
+    CmdArgs.push_back("-debug-info-kind=constructor");
+    break;
+  case codegenoptions::LimitedDebugInfo:
+    CmdArgs.push_back("-debug-info-kind=limited");
+    break;
+  case codegenoptions::FullDebugInfo:
+    CmdArgs.push_back("-debug-info-kind=standalone");
+    break;
+  case codegenoptions::UnusedTypeInfo:
+    CmdArgs.push_back("-debug-info-kind=unused-types");
+    break;
+  default:
+    break;
+  }
+}
+
 void Flang::addFortranDialectOptions(const ArgList &Args,
                                      ArgStringList &CmdArgs) const {
   Args.AddAllArgs(
@@ -68,6 +112,17 @@
 
   if (Args.hasArg(options::OPT_flang_experimental_hlfir))
     CmdArgs.push_back("-flang-experimental-hlfir");
+
+  codegenoptions::DebugInfoKind DebugInfoKind;
+  if (Args.hasArg(options::OPT_gN_Group)) {
+    Arg *gNArg = Args.getLastArg(options::OPT_gN_Group);
+    DebugInfoKind = DebugLevelToInfoKind(*gNArg);
+  } else if (Args.hasArg(options::OPT_g_Flag)) {
+    DebugInfoKind = codegenoptions::DebugLineTablesOnly;
+  } else {
+    DebugInfoKind = codegenoptions::NoDebugInfo;
+  }
+  addFortranDebugOptions(CmdArgs, DebugInfoKind);
 }
 
 void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -3228,9 +3228,9 @@
   NormalizedValuesScope<"llvm::EmitDwarfUnwindType">,
   MarshallingInfoEnum<CodeGenOpts<"EmitDwarfUnwind">, "Default">;
 def g_Flag : Flag<["-"], "g">, Group<g_Group>,
-  HelpText<"Generate source-level debug information">;
+    Flags<[CoreOption,FlangOption]>, HelpText<"Generate source-level debug information">;
 def gline_tables_only : Flag<["-"], "gline-tables-only">, Group<gN_Group>,
-  Flags<[CoreOption]>, HelpText<"Emit debug line number tables only">;
+  Flags<[CoreOption,FlangOption]>, HelpText<"Emit debug line number tables only">;
 def gline_directives_only : Flag<["-"], "gline-directives-only">, Group<gN_Group>,
   Flags<[CoreOption]>, HelpText<"Emit debug line info directives only">;
 def gmlt : Flag<["-"], "gmlt">, Alias<gline_tables_only>;
@@ -5464,12 +5464,12 @@
   NormalizedValuesScope<"llvm::Reloc">,
   NormalizedValues<["Static", "PIC_", "ROPI", "RWPI", "ROPI_RWPI", "DynamicNoPIC"]>,
   MarshallingInfoEnum<CodeGenOpts<"RelocationModel">, "PIC_">;
+def debug_info_kind_EQ : Joined<["-"], "debug-info-kind=">;
 
 } // let Flags = [CC1Option, CC1AsOption, FC1Option, NoDriverOption]
 
 let Flags = [CC1Option, CC1AsOption, NoDriverOption] in {
 
-def debug_info_kind_EQ : Joined<["-"], "debug-info-kind=">;
 def debug_info_macro : Flag<["-"], "debug-info-macro">,
   HelpText<"Emit macro debug information">,
   MarshallingInfoFlag<CodeGenOpts<"MacroDebugInfo">>;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to