plotfi created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
plotfi added a reviewer: compnerd.

Simplifying the phases generation process, first by copying the phases info 
into the Table.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64098

Files:
  clang/include/clang/Driver/Types.def
  clang/include/clang/Driver/Types.h
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/Types.cpp

Index: clang/lib/Driver/Types.cpp
===================================================================
--- clang/lib/Driver/Types.cpp
+++ clang/lib/Driver/Types.cpp
@@ -9,8 +9,8 @@
 #include "clang/Driver/Types.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringSwitch.h"
+#include "llvm/ADT/SmallVector.h"
 #include <cassert>
-#include <string.h>
 
 using namespace clang::driver;
 using namespace clang::driver::types;
@@ -20,11 +20,13 @@
   const char *Flags;
   const char *TempSuffix;
   ID PreprocessedType;
+  const llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> Phases;
 };
 
 static const TypeInfo TypeInfos[] = {
-#define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, FLAGS) \
-  { NAME, FLAGS, TEMP_SUFFIX, TY_##PP_TYPE, },
+#define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, FLAGS, PHASES) \
+  { NAME, FLAGS, TEMP_SUFFIX, TY_##PP_TYPE, PHASES, },
+#define PHASES llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases>
 #include "clang/Driver/Types.def"
 #undef TYPE
 };
@@ -264,7 +266,10 @@
 }
 
 // FIXME: Why don't we just put this list in the defs file, eh.
-void types::getCompilationPhases(ID Id, llvm::SmallVectorImpl<phases::ID> &P) {
+// FIXME: The list is now in Types.def but for now this function will verify
+//        the old behavior and a subsequent change will delete most of the body.
+const llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> &types::getCompilationPhases(ID Id) {
+  llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> P;
   if (Id != TY_Object) {
     if (getPreprocessedType(Id) != TY_INVALID) {
       P.push_back(phases::Preprocess);
@@ -288,6 +293,13 @@
   }
   assert(0 < P.size() && "Not enough phases in list");
   assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
+
+  const llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> &Phases = getInfo(Id).Phases;
+  assert(Phases.size() == P.size() && "Invalid size.");
+  for (unsigned i = 0; i < Phases.size(); i++)
+    assert(Phases[i] == P[i] && "Invalid Phase");
+
+  return Phases;
 }
 
 ID types::lookupCXXTypeForCType(ID Id) {
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -2257,7 +2257,7 @@
     virtual ActionBuilderReturnCode
     getDeviceDependences(OffloadAction::DeviceDependences &DA,
                          phases::ID CurPhase, phases::ID FinalPhase,
-                         PhasesTy &Phases) {
+                         const PhasesTy &Phases) {
       return ABRT_Inactive;
     }
 
@@ -2519,7 +2519,7 @@
     ActionBuilderReturnCode
     getDeviceDependences(OffloadAction::DeviceDependences &DA,
                          phases::ID CurPhase, phases::ID FinalPhase,
-                         PhasesTy &Phases) override {
+                         const PhasesTy &Phases) override {
       if (!IsActive)
         return ABRT_Inactive;
 
@@ -2636,7 +2636,7 @@
     ActionBuilderReturnCode
     getDeviceDependences(OffloadAction::DeviceDependences &DA,
                          phases::ID CurPhase, phases::ID FinalPhase,
-                         PhasesTy &Phases) override {
+                         const PhasesTy &Phases) override {
       // amdgcn does not support linking of object files, therefore we skip
       // backend and assemble phases to output LLVM IR. Except for generating
       // non-relocatable device coee, where we generate fat binary for device
@@ -2753,7 +2753,7 @@
     ActionBuilderReturnCode
     getDeviceDependences(OffloadAction::DeviceDependences &DA,
                          phases::ID CurPhase, phases::ID FinalPhase,
-                         PhasesTy &Phases) override {
+                         const PhasesTy &Phases) override {
       if (OpenMPDeviceActions.empty())
         return ABRT_Inactive;
 
@@ -2955,7 +2955,7 @@
   Action *
   addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg,
                                    phases::ID CurPhase, phases::ID FinalPhase,
-                                   DeviceActionBuilder::PhasesTy &Phases) {
+                                   const DeviceActionBuilder::PhasesTy &Phases) {
     if (!IsValid)
       return nullptr;
 
@@ -3228,13 +3228,14 @@
   HeaderModulePrecompileJobAction *HeaderModuleAction = nullptr;
   ActionList LinkerInputs;
 
-  llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PL;
+  unsigned LastPLSize = 0;
   for (auto &I : Inputs) {
     types::ID InputType = I.first;
     const Arg *InputArg = I.second;
 
-    PL.clear();
-    types::getCompilationPhases(InputType, PL);
+    const llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> &PL =
+        types::getCompilationPhases(InputType);
+    LastPLSize = PL.size();
 
     // If the first step comes after the final phase we are doing as part of
     // this compilation, warn the user about it.
@@ -3275,8 +3276,8 @@
       // Add a separate precompile phase for the compile phase.
       if (FinalPhase >= phases::Compile) {
         const types::ID HeaderType = lookupHeaderTypeForSourceType(InputType);
-        llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PCHPL;
-        types::getCompilationPhases(HeaderType, PCHPL);
+        const llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> &PCHPL =
+            types::getCompilationPhases(HeaderType);
         // Build the pipeline for the pch file.
         Action *ClangClPch =
             C.MakeAction<InputAction>(*InputArg, HeaderType);
@@ -3300,8 +3301,7 @@
     if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg))
       break;
 
-    for (SmallVectorImpl<phases::ID>::iterator i = PL.begin(), e = PL.end();
-         i != e; ++i) {
+    for (auto i = PL.begin(), e = PL.end(); i != e; ++i) {
       phases::ID Phase = *i;
 
       // We are done if this step is past what the user requested.
@@ -3373,7 +3373,8 @@
 
   // If we are linking, claim any options which are obviously only used for
   // compilation.
-  if (FinalPhase == phases::Link && PL.size() == 1) {
+  // FIXME: Understand why the last Phase List length is used here.
+  if (FinalPhase == phases::Link && LastPLSize == 1) {
     Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
     Args.ClaimAllArgs(options::OPT_cl_compile_Group);
   }
Index: clang/include/clang/Driver/Types.h
===================================================================
--- clang/include/clang/Driver/Types.h
+++ clang/include/clang/Driver/Types.h
@@ -20,7 +20,7 @@
 namespace types {
   enum ID {
     TY_INVALID,
-#define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, FLAGS) TY_##ID,
+#define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, FLAGS, PHASES) TY_##ID,
 #include "clang/Driver/Types.def"
 #undef TYPE
     TY_LAST
@@ -97,9 +97,7 @@
 
   /// getCompilationPhases - Get the list of compilation phases ('Phases') to be
   /// done for type 'Id'.
-  void getCompilationPhases(
-    ID Id,
-    llvm::SmallVectorImpl<phases::ID> &Phases);
+  const llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> &getCompilationPhases(ID Id);
 
   /// lookupCXXTypeForCType - Lookup CXX input type that corresponds to given
   /// C type (used for clang++ emulation of g++ behaviour)
Index: clang/include/clang/Driver/Types.def
===================================================================
--- clang/include/clang/Driver/Types.def
+++ clang/include/clang/Driver/Types.def
@@ -39,67 +39,67 @@
 
 
 // C family source language (with and without preprocessing).
-TYPE("cpp-output",               PP_C,         INVALID,         "i",     "u")
-TYPE("c",                        C,            PP_C,            "c",     "u")
-TYPE("cl",                       CL,           PP_C,            "cl",    "u")
-TYPE("cuda-cpp-output",          PP_CUDA,      INVALID,         "cui",   "u")
-TYPE("cuda",                     CUDA,         PP_CUDA,         "cu",    "u")
-TYPE("cuda",                     CUDA_DEVICE,  PP_CUDA,         "cu",    "")
-TYPE("hip-cpp-output",           PP_HIP,       INVALID,         "cui",   "u")
-TYPE("hip",                      HIP,          PP_HIP,          "cu",    "u")
-TYPE("hip",                      HIP_DEVICE,   PP_HIP,          "cu",    "")
-TYPE("objective-c-cpp-output",   PP_ObjC,      INVALID,         "mi",    "u")
-TYPE("objc-cpp-output",          PP_ObjC_Alias, INVALID,        "mi",    "u")
-TYPE("objective-c",              ObjC,         PP_ObjC,         "m",     "u")
-TYPE("c++-cpp-output",           PP_CXX,       INVALID,         "ii",    "u")
-TYPE("c++",                      CXX,          PP_CXX,          "cpp",   "u")
-TYPE("objective-c++-cpp-output", PP_ObjCXX,    INVALID,         "mii",   "u")
-TYPE("objc++-cpp-output",        PP_ObjCXX_Alias, INVALID,      "mii",   "u")
-TYPE("objective-c++",            ObjCXX,       PP_ObjCXX,       "mm",    "u")
-TYPE("renderscript",             RenderScript, PP_C,            "rs",    "u")
+TYPE("cpp-output",               PP_C,         INVALID,         "i",     "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("c",                        C,            PP_C,            "c",     "u",  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("cl",                       CL,           PP_C,            "cl",    "u",  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("cuda-cpp-output",          PP_CUDA,      INVALID,         "cui",   "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("cuda",                     CUDA,         PP_CUDA,         "cu",    "u",  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("cuda",                     CUDA_DEVICE,  PP_CUDA,         "cu",    "" ,  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("hip-cpp-output",           PP_HIP,       INVALID,         "cui",   "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("hip",                      HIP,          PP_HIP,          "cu",    "u",  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("hip",                      HIP_DEVICE,   PP_HIP,          "cu",    "" ,  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("objective-c-cpp-output",   PP_ObjC,      INVALID,         "mi",    "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("objc-cpp-output",          PP_ObjC_Alias, INVALID,        "mi",    "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("objective-c",              ObjC,         PP_ObjC,         "m",     "u",  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("c++-cpp-output",           PP_CXX,       INVALID,         "ii",    "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("c++",                      CXX,          PP_CXX,          "cpp",   "u",  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("objective-c++-cpp-output", PP_ObjCXX,    INVALID,         "mii",   "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("objc++-cpp-output",        PP_ObjCXX_Alias, INVALID,      "mii",   "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("objective-c++",            ObjCXX,       PP_ObjCXX,       "mm",    "u",  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("renderscript",             RenderScript, PP_C,            "rs",    "u",  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
 
 // C family input files to precompile.
-TYPE("c-header-cpp-output",      PP_CHeader,   INVALID,         "i",     "p")
-TYPE("c-header",                 CHeader,      PP_CHeader,      "h",     "pu")
-TYPE("cl-header",                CLHeader,     PP_CHeader,      "h",     "pu")
-TYPE("objective-c-header-cpp-output", PP_ObjCHeader, INVALID,   "mi",    "p")
-TYPE("objective-c-header",       ObjCHeader,   PP_ObjCHeader,   "h",     "pu")
-TYPE("c++-header-cpp-output",    PP_CXXHeader, INVALID,         "ii",    "p")
-TYPE("c++-header",               CXXHeader,    PP_CXXHeader,    "hh",    "pu")
-TYPE("objective-c++-header-cpp-output", PP_ObjCXXHeader, INVALID, "mii", "p")
-TYPE("objective-c++-header",     ObjCXXHeader, PP_ObjCXXHeader, "h",     "pu")
-TYPE("c++-module",               CXXModule,    PP_CXXModule,    "cppm",  "mu")
-TYPE("c++-module-cpp-output",    PP_CXXModule, INVALID,         "iim",   "m")
+TYPE("c-header-cpp-output",      PP_CHeader,   INVALID,         "i",     "p",  (PHASES { phases::Precompile }))
+TYPE("c-header",                 CHeader,      PP_CHeader,      "h",     "pu", (PHASES { phases::Preprocess, phases::Precompile }))
+TYPE("cl-header",                CLHeader,     PP_CHeader,      "h",     "pu", (PHASES { phases::Preprocess, phases::Precompile }))
+TYPE("objective-c-header-cpp-output", PP_ObjCHeader, INVALID,   "mi",    "p",  (PHASES { phases::Precompile }))
+TYPE("objective-c-header",       ObjCHeader,   PP_ObjCHeader,   "h",     "pu", (PHASES { phases::Preprocess, phases::Precompile }))
+TYPE("c++-header-cpp-output",    PP_CXXHeader, INVALID,         "ii",    "p",  (PHASES { phases::Precompile }))
+TYPE("c++-header",               CXXHeader,    PP_CXXHeader,    "hh",    "pu", (PHASES { phases::Preprocess, phases::Precompile }))
+TYPE("objective-c++-header-cpp-output", PP_ObjCXXHeader, INVALID, "mii", "p",  (PHASES { phases::Precompile }))
+TYPE("objective-c++-header",     ObjCXXHeader, PP_ObjCXXHeader, "h",     "pu", (PHASES { phases::Preprocess, phases::Precompile }))
+TYPE("c++-module",               CXXModule,    PP_CXXModule,    "cppm",  "mu", (PHASES { phases::Preprocess, phases::Precompile, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("c++-module-cpp-output",    PP_CXXModule, INVALID,         "iim",   "m",  (PHASES { phases::Precompile, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
 
 // Other languages.
-TYPE("ada",                      Ada,          INVALID,         nullptr, "u")
-TYPE("assembler",                PP_Asm,       INVALID,         "s",     "au")
-TYPE("assembler-with-cpp",       Asm,          PP_Asm,          "S",     "au")
-TYPE("f95",                      PP_Fortran,   INVALID,         nullptr, "u")
-TYPE("f95-cpp-input",            Fortran,      PP_Fortran,      nullptr, "u")
-TYPE("java",                     Java,         INVALID,         nullptr, "u")
+TYPE("ada",                      Ada,          INVALID,         nullptr, "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("assembler",                PP_Asm,       INVALID,         "s",     "au", (PHASES { phases::Assemble, phases::Link }))
+TYPE("assembler-with-cpp",       Asm,          PP_Asm,          "S",     "au", (PHASES { phases::Preprocess, phases::Assemble, phases::Link }))
+TYPE("f95",                      PP_Fortran,   INVALID,         nullptr, "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("f95-cpp-input",            Fortran,      PP_Fortran,      nullptr, "u",  (PHASES { phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("java",                     Java,         INVALID,         nullptr, "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
 
 // LLVM IR/LTO types. We define separate types for IR and LTO because LTO
 // outputs should use the standard suffixes.
-TYPE("ir",                       LLVM_IR,      INVALID,         "ll",    "u")
-TYPE("ir",                       LLVM_BC,      INVALID,         "bc",    "u")
-TYPE("lto-ir",                   LTO_IR,       INVALID,         "s",     "")
-TYPE("lto-bc",                   LTO_BC,       INVALID,         "o",     "")
+TYPE("ir",                       LLVM_IR,      INVALID,         "ll",    "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("ir",                       LLVM_BC,      INVALID,         "bc",    "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("lto-ir",                   LTO_IR,       INVALID,         "s",     "",   (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("lto-bc",                   LTO_BC,       INVALID,         "o",     "",   (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
 
 // Misc.
-TYPE("ast",                      AST,          INVALID,         "ast",   "u")
-TYPE("ifs",                      IFS,          INVALID,         "ifs",   "u")
-TYPE("pcm",                      ModuleFile,   INVALID,         "pcm",   "u")
-TYPE("plist",                    Plist,        INVALID,         "plist", "")
-TYPE("rewritten-objc",           RewrittenObjC,INVALID,         "cpp",   "")
-TYPE("rewritten-legacy-objc",    RewrittenLegacyObjC,INVALID,   "cpp",   "")
-TYPE("remap",                    Remap,        INVALID,         "remap", "")
-TYPE("precompiled-header",       PCH,          INVALID,         "gch",   "A")
-TYPE("object",                   Object,       INVALID,         "o",     "")
-TYPE("treelang",                 Treelang,     INVALID,         nullptr, "u")
-TYPE("image",                    Image,        INVALID,         "out",   "")
-TYPE("dSYM",                     dSYM,         INVALID,         "dSYM",  "A")
-TYPE("dependencies",             Dependencies, INVALID,         "d",     "")
-TYPE("cuda-fatbin",              CUDA_FATBIN,  INVALID,         "fatbin","A")
-TYPE("hip-fatbin",               HIP_FATBIN,   INVALID,         "hipfb", "A")
-TYPE("none",                     Nothing,      INVALID,         nullptr, "u")
+TYPE("ast",                      AST,          INVALID,         "ast",   "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("ifs",                      IFS,          INVALID,         "ifs",   "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("pcm",                      ModuleFile,   INVALID,         "pcm",   "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("plist",                    Plist,        INVALID,         "plist", "",   (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("rewritten-objc",           RewrittenObjC,INVALID,         "cpp",   "",   (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("rewritten-legacy-objc",    RewrittenLegacyObjC,INVALID,   "cpp",   "",   (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("remap",                    Remap,        INVALID,         "remap", "",   (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("precompiled-header",       PCH,          INVALID,         "gch",   "A",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("object",                   Object,       INVALID,         "o",     "",   (PHASES { phases::Link }))
+TYPE("treelang",                 Treelang,     INVALID,         nullptr, "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("image",                    Image,        INVALID,         "out",   "",   (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("dSYM",                     dSYM,         INVALID,         "dSYM",  "A",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("dependencies",             Dependencies, INVALID,         "d",     "",   (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("cuda-fatbin",              CUDA_FATBIN,  INVALID,         "fatbin","A",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("hip-fatbin",               HIP_FATBIN,   INVALID,         "hipfb", "A",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
+TYPE("none",                     Nothing,      INVALID,         nullptr, "u",  (PHASES { phases::Compile, phases::Backend, phases::Assemble, phases::Link }))
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to