From 35fde150591d7c5f032a5e7c9643315e5f2bedde Mon Sep 17 00:00:00 2001 From: Mikhail Glushenkov Date: Mon, 17 Nov 2008 17:30:25 +0000 Subject: [PATCH] Support dependencies between plugins by priority-sorting. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@59449 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CompilerDriver/Common.td | 5 ++++ include/llvm/CompilerDriver/Plugin.h | 5 ++++ tools/llvmc2/driver/Plugin.cpp | 9 ++++++++ utils/TableGen/LLVMCConfigurationEmitter.cpp | 24 ++++++++++++++++---- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/include/llvm/CompilerDriver/Common.td b/include/llvm/CompilerDriver/Common.td index e017326069e..4589f9179c3 100644 --- a/include/llvm/CompilerDriver/Common.td +++ b/include/llvm/CompilerDriver/Common.td @@ -65,6 +65,11 @@ def or; def inc_weight; def dec_weight; +// Used to specify plugin priority. +class PluginPriority { + int priority = p; +} + // Option list - used to specify aliases and sometimes help strings. class OptionList l> { list options = l; diff --git a/include/llvm/CompilerDriver/Plugin.h b/include/llvm/CompilerDriver/Plugin.h index 1dc0df9d550..5426ac73cd4 100644 --- a/include/llvm/CompilerDriver/Plugin.h +++ b/include/llvm/CompilerDriver/Plugin.h @@ -24,6 +24,11 @@ namespace llvmc { /// BasePlugin - An abstract base class for all LLVMC plugins. struct BasePlugin { + /// Priority - Plugin priority, useful for handling dependencies + /// between plugins. Plugins with lower priorities are loaded + /// first. + virtual int Priority() const = 0; + /// PopulateLanguageMap - The auto-generated function that fills in /// the language map (map from file extensions to language names). virtual void PopulateLanguageMap(LanguageMap&) const = 0; diff --git a/tools/llvmc2/driver/Plugin.cpp b/tools/llvmc2/driver/Plugin.cpp index c9b3960c1e7..17c70869ea6 100644 --- a/tools/llvmc2/driver/Plugin.cpp +++ b/tools/llvmc2/driver/Plugin.cpp @@ -13,6 +13,7 @@ #include "llvm/CompilerDriver/Plugin.h" +#include #include namespace { @@ -27,6 +28,13 @@ namespace { static bool pluginListInitialized = false; typedef std::vector PluginList; static PluginList Plugins; + + struct ByPriority { + bool operator()(const llvmc::BasePlugin* lhs, + const llvmc::BasePlugin* rhs) { + return lhs->Priority() < rhs->Priority(); + } + }; } namespace llvmc { @@ -36,6 +44,7 @@ namespace llvmc { for (PluginRegistry::iterator B = PluginRegistry::begin(), E = PluginRegistry::end(); B != E; ++B) Plugins.push_back(B->instantiate()); + std::sort(Plugins.begin(), Plugins.end(), ByPriority()); } pluginListInitialized = true; } diff --git a/utils/TableGen/LLVMCConfigurationEmitter.cpp b/utils/TableGen/LLVMCConfigurationEmitter.cpp index 42d4d9ea54a..d43e62bad8f 100644 --- a/utils/TableGen/LLVMCConfigurationEmitter.cpp +++ b/utils/TableGen/LLVMCConfigurationEmitter.cpp @@ -771,7 +771,7 @@ void CollectPropertiesFromOptionLists (RecordVector::const_iterator B, GlobalOptionDescriptions& OptDescs) { // Iterate over a properties list of every Tool definition - for (;B!=E;++B) { + for (; B!=E; ++B) { RecordVector::value_type T = *B; // Throws an exception if the value does not exist. ListInit* PropList = T->getValueAsListInit("options"); @@ -1701,9 +1701,10 @@ void EmitHookDeclarations(const ToolPropertiesList& ToolProps, } /// EmitRegisterPlugin - Emit code to register this plugin. -void EmitRegisterPlugin(std::ostream& O) { +void EmitRegisterPlugin(int Priority, std::ostream& O) { O << "namespace {\n\n" - << "struct Plugin : public llvmc::BasePlugin {\n" + << "struct Plugin : public llvmc::BasePlugin {\n\n" + << Indent1 << "int Priority() const { return " << Priority << "; }\n\n" << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n" << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n" << Indent1 @@ -1777,6 +1778,15 @@ void FilterNotInGraph (const Record* CompilationGraph, ToolProps.erase(new_end, ToolProps.end()); } +int CalculatePriority(RecordVector::const_iterator B, + RecordVector::const_iterator E) { + int total = 0; + for (; B!=E; ++B) { + total += static_cast((*B)->getValueAsInt("priority")); + } + return total; +} + // End of anonymous namespace } @@ -1799,7 +1809,8 @@ void LLVMCConfigurationEmitter::run (std::ostream &O) { GlobalOptionDescriptions OptDescs; CollectToolProperties(Tools.begin(), Tools.end(), ToolProps, OptDescs); - RecordVector OptionLists = Records.getAllDerivedDefinitions("OptionList"); + const RecordVector& OptionLists = + Records.getAllDerivedDefinitions("OptionList"); CollectPropertiesFromOptionLists(OptionLists.begin(), OptionLists.end(), OptDescs); @@ -1841,7 +1852,10 @@ void LLVMCConfigurationEmitter::run (std::ostream &O) { EmitPopulateCompilationGraph(CompilationGraphRecord, ToolProps, O); // Emit code for plugin registration. - EmitRegisterPlugin(O); + const RecordVector& Priorities = + Records.getAllDerivedDefinitions("PluginPriority"); + EmitRegisterPlugin(CalculatePriority(Priorities.begin(), Priorities.end()), + O); // EOF } catch (std::exception& Error) { -- 2.34.1