FarisRehman updated this revision to Diff 324590.
FarisRehman added a comment.

Rebase off main

Rebase off main and add a comment to MeasurementVisitor.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96884/new/

https://reviews.llvm.org/D96884

Files:
  clang/include/clang/Driver/Options.td
  flang/include/flang/Frontend/FrontendActions.h
  flang/include/flang/Frontend/FrontendOptions.h
  flang/lib/Frontend/CMakeLists.txt
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
  flang/test/Flang-Driver/debug-measure-parse-tree.f90
  flang/test/Flang-Driver/debug-parsing-log.f90
  flang/test/Flang-Driver/driver-help.f90
  flang/test/Lower/pre-fir-tree01.f90
  flang/test/Lower/pre-fir-tree02.f90
  flang/test/Lower/pre-fir-tree03.f90
  flang/test/Lower/pre-fir-tree05.f90

Index: flang/test/Lower/pre-fir-tree05.f90
===================================================================
--- flang/test/Lower/pre-fir-tree05.f90
+++ flang/test/Lower/pre-fir-tree05.f90
@@ -1,4 +1,4 @@
-! RUN: %f18 -fdebug-pre-fir-tree -fsyntax-only -fopenacc %s | FileCheck %s
+! RUN: %flang_fc1 -fsyntax-only -fdebug-pre-fir-tree -fopenacc %s | FileCheck %s
 
 ! Test structure of the Pre-FIR tree with OpenACC construct
 
Index: flang/test/Lower/pre-fir-tree03.f90
===================================================================
--- flang/test/Lower/pre-fir-tree03.f90
+++ flang/test/Lower/pre-fir-tree03.f90
@@ -1,4 +1,4 @@
-! RUN: %f18 -fdebug-pre-fir-tree -fsyntax-only -fopenmp %s | FileCheck %s
+! RUN: %flang_fc1 -fsyntax-only -fdebug-pre-fir-tree -fopenmp %s | FileCheck %s
 
 ! Test Pre-FIR Tree captures OpenMP related constructs
 
Index: flang/test/Lower/pre-fir-tree02.f90
===================================================================
--- flang/test/Lower/pre-fir-tree02.f90
+++ flang/test/Lower/pre-fir-tree02.f90
@@ -1,4 +1,4 @@
-! RUN: %f18 -fdebug-pre-fir-tree -fsyntax-only %s | FileCheck %s
+! RUN: %flang_fc1 -fsyntax-only -fdebug-pre-fir-tree %s | FileCheck %s
 
 ! Test Pre-FIR Tree captures all the intended nodes from the parse-tree
 ! Coarray and OpenMP related nodes are tested in other files.
Index: flang/test/Lower/pre-fir-tree01.f90
===================================================================
--- flang/test/Lower/pre-fir-tree01.f90
+++ flang/test/Lower/pre-fir-tree01.f90
@@ -1,4 +1,4 @@
-! RUN: %f18 -fdebug-pre-fir-tree -fsyntax-only %s | FileCheck %s
+! RUN: %flang_fc1 -fsyntax-only -fdebug-pre-fir-tree %s | FileCheck %s
 
 ! Test structure of the Pre-FIR tree
 
Index: flang/test/Flang-Driver/driver-help.f90
===================================================================
--- flang/test/Flang-Driver/driver-help.f90
+++ flang/test/Flang-Driver/driver-help.f90
@@ -59,6 +59,10 @@
 ! HELP-FC1-NEXT: -fdebug-dump-parse-tree Dump the parse tree
 ! HELP-FC1-NEXT: -fdebug-dump-provenance Dump provenance
 ! HELP-FC1-NEXT: -fdebug-dump-symbols    Dump symbols after the semantic analysis
+! HELP-FC1-NEXT: -fdebug-measure-parse-tree
+! HELP-FC1-NEXT:                         Measure the parse tree
+! HELP-FC1-NEXT: -fdebug-parsing-log     Dump the parsing log
+! HELP-FC1-NEXT: -fdebug-pre-fir-tree    Dump the pre-FIR tree
 ! HELP-FC1-NEXT: -fdebug-unparse-with-symbols
 ! HELP-FC1-NEXT:                        Unparse and stop.
 ! HELP-FC1-NEXT: -fdebug-unparse        Unparse and stop.
Index: flang/test/Flang-Driver/debug-parsing-log.f90
===================================================================
--- /dev/null
+++ flang/test/Flang-Driver/debug-parsing-log.f90
@@ -0,0 +1,25 @@
+! Ensure argument -fdebug-parsing-log works as expected.
+
+! REQUIRES: new-flang-driver
+
+!--------------------------
+! FLANG DRIVER (flang-new)
+!--------------------------
+! RUN: not %flang-new -fdebug-parsing-log %s  2>&1 | FileCheck %s --check-prefix=FLANG
+
+!----------------------------------------
+! FRONTEND FLANG DRIVER (flang-new -fc1)
+!----------------------------------------
+! RUN: %flang-new -fc1 -fdebug-parsing-log %s  2>&1 | FileCheck %s --check-prefix=FRONTEND
+
+!----------------------------------
+! EXPECTED OUTPUT WITH `flang-new`
+!----------------------------------
+! FLANG:warning: argument unused during compilation: '-fdebug-parsing-log'
+
+!---------------------------------------
+! EXPECTED OUTPUT WITH `flang-new -fc1`
+!---------------------------------------
+! FRONTEND:in the context: END PROGRAM statement
+
+INTEGER :: i = 1
Index: flang/test/Flang-Driver/debug-measure-parse-tree.f90
===================================================================
--- /dev/null
+++ flang/test/Flang-Driver/debug-measure-parse-tree.f90
@@ -0,0 +1,27 @@
+! Ensure argument -fdebug-measure-parse-tree works as expected.
+
+! REQUIRES: new-flang-driver
+
+!--------------------------
+! FLANG DRIVER (flang-new)
+!--------------------------
+! RUN: not %flang-new -fdebug-measure-parse-tree %s  2>&1 | FileCheck %s --check-prefix=FLANG
+
+!----------------------------------------
+! FRONTEND FLANG DRIVER (flang-new -fc1)
+!----------------------------------------
+! RUN: %flang-new -fc1 -fdebug-measure-parse-tree %s  2>&1 | FileCheck %s --check-prefix=FRONTEND
+
+!----------------------------------
+! EXPECTED OUTPUT WITH `flang-new`
+!----------------------------------
+! FLANG:warning: argument unused during compilation: '-fdebug-measure-parse-tree'
+
+!---------------------------------------
+! EXPECTED OUTPUT WITH `flang-new -fc1`
+!---------------------------------------
+! FRONTEND:18 objects
+! FRONTEND:1496 total bytes
+
+program A
+end
Index: flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
===================================================================
--- flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -52,6 +52,15 @@
   case DebugDumpProvenance:
     return std::make_unique<DebugDumpProvenanceAction>();
     break;
+  case DebugParsingLog:
+    return std::make_unique<DebugParsingLogAction>();
+    break;
+  case DebugMeasureParseTree:
+    return std::make_unique<DebugMeasureParseTreeAction>();
+    break;
+  case DebugPreFIRTree:
+    return std::make_unique<DebugPreFIRTreeAction>();
+    break;
   default:
     break;
     // TODO:
Index: flang/lib/Frontend/FrontendActions.cpp
===================================================================
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -10,6 +10,7 @@
 #include "flang/Common/default-kinds.h"
 #include "flang/Frontend/CompilerInstance.h"
 #include "flang/Frontend/FrontendOptions.h"
+#include "flang/Lower/PFTBuilder.h"
 #include "flang/Parser/dump-parse-tree.h"
 #include "flang/Parser/parsing.h"
 #include "flang/Parser/provenance.h"
@@ -23,13 +24,22 @@
 
 using namespace Fortran::frontend;
 
-void reportFatalSemanticErrors(const Fortran::semantics::Semantics &semantics,
+/// Report fatal semantic errors if present.
+///
+/// \param semantics The semantics instance
+/// \param diags The diagnostics engine instance
+/// \param bufferName The file or buffer name
+///
+/// \return True if fatal semantic errors are present, false if not
+bool reportFatalSemanticErrors(const Fortran::semantics::Semantics &semantics,
     clang::DiagnosticsEngine &diags, const llvm::StringRef &bufferName) {
   if (semantics.AnyFatalError()) {
     unsigned DiagID = diags.getCustomDiagID(
         clang::DiagnosticsEngine::Error, "Semantic errors in %0");
     diags.Report(DiagID) << bufferName;
+    return true;
   }
+  return false;
 }
 
 bool PrescanAction::BeginSourceFileAction(CompilerInstance &c1) {
@@ -247,6 +257,63 @@
       GetCurrentFileOrBufferName());
 }
 
+void DebugParsingLogAction::ExecuteAction() {
+  CompilerInstance &ci = this->instance();
+
+  // Parse
+  ci.parsing().Parse(llvm::outs());
+  // Dump parsing log
+  ci.parsing().DumpParsingLog(llvm::outs());
+}
+
+void DebugMeasureParseTreeAction::ExecuteAction() {
+  CompilerInstance &ci = this->instance();
+
+  // Parse. In case of failure, report and return.
+  ci.parsing().Parse(llvm::outs());
+
+  if (ci.parsing().messages().AnyFatalError()) {
+    unsigned diagID = ci.diagnostics().getCustomDiagID(
+        clang::DiagnosticsEngine::Error, "Could not parse %0");
+    ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
+
+    ci.parsing().messages().Emit(
+        llvm::errs(), this->instance().allCookedSources());
+    return;
+  }
+
+  // Report the diagnostics from parsing
+  ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
+
+  auto &parseTree{*ci.parsing().parseTree()};
+
+  // Measure the parse tree
+  MeasurementVisitor visitor;
+  Fortran::parser::Walk(parseTree, visitor);
+  llvm::outs() << "Parse tree comprises of " << visitor.objects
+               << " objects and occupies " << visitor.bytes
+               << " total bytes.\n";
+}
+
+void DebugPreFIRTreeAction::ExecuteAction() {
+  CompilerInstance &ci = this->instance();
+  // Report and exit if fatal semantic errors are present
+  if (reportFatalSemanticErrors(
+          semantics(), ci.diagnostics(), GetCurrentFileOrBufferName())) {
+    return;
+  }
+
+  auto &parseTree{*ci.parsing().parseTree()};
+
+  // Dump pre-FIR tree
+  if (auto ast{Fortran::lower::createPFT(
+          parseTree, ci.invocation().semanticsContext())}) {
+    Fortran::lower::dumpPFT(llvm::outs(), *ast);
+  } else {
+    llvm::errs() << "Pre FIR Tree is NULL.\n";
+  }
+}
+
 void EmitObjAction::ExecuteAction() {
   CompilerInstance &ci = this->instance();
   unsigned DiagID = ci.diagnostics().getCustomDiagID(
Index: flang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- flang/lib/Frontend/CompilerInvocation.cpp
+++ flang/lib/Frontend/CompilerInvocation.cpp
@@ -125,6 +125,15 @@
     case clang::driver::options::OPT_fdebug_dump_provenance:
       opts.programAction_ = DebugDumpProvenance;
       break;
+    case clang::driver::options::OPT_fdebug_parsing_log:
+      opts.programAction_ = DebugParsingLog;
+      break;
+    case clang::driver::options::OPT_fdebug_measure_parse_tree:
+      opts.programAction_ = DebugMeasureParseTree;
+      break;
+    case clang::driver::options::OPT_fdebug_pre_fir_tree:
+      opts.programAction_ = DebugPreFIRTree;
+      break;
 
       // TODO:
       // case calng::driver::options::OPT_emit_llvm:
Index: flang/lib/Frontend/CMakeLists.txt
===================================================================
--- flang/lib/Frontend/CMakeLists.txt
+++ flang/lib/Frontend/CMakeLists.txt
@@ -16,6 +16,7 @@
   FortranSemantics
   FortranEvaluate
   FortranCommon
+  FortranLower
   clangBasic
   clangDriver
 
Index: flang/include/flang/Frontend/FrontendOptions.h
===================================================================
--- flang/include/flang/Frontend/FrontendOptions.h
+++ flang/include/flang/Frontend/FrontendOptions.h
@@ -48,7 +48,17 @@
   DebugDumpParseTree,
 
   /// Dump provenance
-  DebugDumpProvenance
+  DebugDumpProvenance,
+
+  /// Parse then output the log
+  DebugParsingLog,
+
+  /// Parse then output the number of objects in the parse tree and the overall
+  /// size
+  DebugMeasureParseTree,
+
+  /// Parse, run semantics and then output the pre-FIR tree
+  DebugPreFIRTree
 
   /// TODO: RunPreprocessor, EmitLLVM, EmitLLVMOnly,
   /// EmitCodeGenOnly, EmitAssembly, (...)
Index: flang/include/flang/Frontend/FrontendActions.h
===================================================================
--- flang/include/flang/Frontend/FrontendActions.h
+++ flang/include/flang/Frontend/FrontendActions.h
@@ -15,6 +15,17 @@
 
 namespace Fortran::frontend {
 
+/// This has been taken from f18.cpp
+/// TODO: Move to a more suitable location.
+struct MeasurementVisitor {
+  template <typename A> bool Pre(const A &) { return true; }
+  template <typename A> void Post(const A &) {
+    ++objects;
+    bytes += sizeof(A);
+  }
+  size_t objects{0}, bytes{0};
+};
+
 //===----------------------------------------------------------------------===//
 // Custom Consumer Actions
 //===----------------------------------------------------------------------===//
@@ -43,6 +54,14 @@
   void ExecuteAction() override;
 };
 
+class DebugParsingLogAction : public PrescanAction {
+  void ExecuteAction() override;
+};
+
+class DebugMeasureParseTreeAction : public PrescanAction {
+  void ExecuteAction() override;
+};
+
 //===----------------------------------------------------------------------===//
 // PrescanAndSema Actions
 //===----------------------------------------------------------------------===//
@@ -77,6 +96,10 @@
   void ExecuteAction() override;
 };
 
+class DebugPreFIRTreeAction : public PrescanAndSemaAction {
+  void ExecuteAction() override;
+};
+
 class ParseSyntaxOnlyAction : public PrescanAndSemaAction {
   void ExecuteAction() override;
 };
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -4275,6 +4275,12 @@
   HelpText<"Dump the parse tree">;
 def fdebug_dump_provenance : Flag<["-"], "fdebug-dump-provenance">, Group<Action_Group>,
   HelpText<"Dump provenance">;
+def fdebug_parsing_log : Flag<["-"], "fdebug-parsing-log">, Group<Action_Group>,
+  HelpText<"Dump the parsing log">;
+def fdebug_measure_parse_tree : Flag<["-"], "fdebug-measure-parse-tree">, Group<Action_Group>,
+  HelpText<"Measure the parse tree">;
+def fdebug_pre_fir_tree : Flag<["-"], "fdebug-pre-fir-tree">, Group<Action_Group>,
+  HelpText<"Dump the pre-FIR tree">;
 
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to