@@ -1010,9 +1010,6 @@ struct GenericPluginTy {
10101010 // / Get the target triple of this plugin.
10111011 virtual Triple::ArchType getTripleArch () const = 0;
10121012
1013- // / Get the constant name identifier for this plugin.
1014- virtual const char *getName () const = 0;
1015-
10161013 // / Allocate a structure using the internal allocator.
10171014 template <typename Ty> Ty *allocate () {
10181015 return reinterpret_cast <Ty *>(Allocator.Allocate (sizeof (Ty), alignof (Ty)));
@@ -1229,7 +1226,7 @@ namespace Plugin {
12291226// / Create a success error. This is the same as calling Error::success(), but
12301227// / it is recommended to use this one for consistency with Plugin::error() and
12311228// / Plugin::check().
1232- static inline Error success () { return Error::success (); }
1229+ static Error success () { return Error::success (); }
12331230
12341231// / Create a string error.
12351232template <typename ... ArgsTy>
@@ -1249,6 +1246,95 @@ template <typename... ArgsTy>
12491246static Error check (int32_t ErrorCode, const char *ErrFmt, ArgsTy... Args);
12501247} // namespace Plugin
12511248
1249+ // / Class for simplifying the getter operation of the plugin. Anywhere on the
1250+ // / code, the current plugin can be retrieved by Plugin::get(). The class also
1251+ // / declares functions to create plugin-specific object instances. The check(),
1252+ // / createPlugin(), createDevice() and createGlobalHandler() functions should be
1253+ // / defined by each plugin implementation.
1254+ class PluginTy {
1255+ // Reference to the plugin instance.
1256+ static GenericPluginTy *SpecificPlugin;
1257+
1258+ PluginTy () {
1259+ if (auto Err = init ())
1260+ REPORT (" Failed to initialize plugin: %s\n " ,
1261+ toString (std::move (Err)).data ());
1262+ }
1263+
1264+ ~PluginTy () {
1265+ if (auto Err = deinit ())
1266+ REPORT (" Failed to deinitialize plugin: %s\n " ,
1267+ toString (std::move (Err)).data ());
1268+ }
1269+
1270+ PluginTy (const PluginTy &) = delete ;
1271+ void operator =(const PluginTy &) = delete ;
1272+
1273+ // / Create and intialize the plugin instance.
1274+ static Error init () {
1275+ assert (!SpecificPlugin && " Plugin already created" );
1276+
1277+ // Create the specific plugin.
1278+ SpecificPlugin = createPlugin ();
1279+ assert (SpecificPlugin && " Plugin was not created" );
1280+
1281+ // Initialize the plugin.
1282+ return SpecificPlugin->init ();
1283+ }
1284+
1285+ // Deinitialize and destroy the plugin instance.
1286+ static Error deinit () {
1287+ assert (SpecificPlugin && " Plugin no longer valid" );
1288+
1289+ for (int32_t DevNo = 0 , NumDev = SpecificPlugin->getNumDevices ();
1290+ DevNo < NumDev; ++DevNo)
1291+ if (auto Err = SpecificPlugin->deinitDevice (DevNo))
1292+ return Err;
1293+
1294+ // Deinitialize the plugin.
1295+ if (auto Err = SpecificPlugin->deinit ())
1296+ return Err;
1297+
1298+ // Delete the plugin instance.
1299+ delete SpecificPlugin;
1300+
1301+ // Invalidate the plugin reference.
1302+ SpecificPlugin = nullptr ;
1303+
1304+ return Plugin::success ();
1305+ }
1306+
1307+ public:
1308+ // / Initialize the plugin if needed. The plugin could have been initialized by
1309+ // / a previous call to Plugin::get().
1310+ static Error initIfNeeded () {
1311+ // Trigger the initialization if needed.
1312+ get ();
1313+
1314+ return Error::success ();
1315+ }
1316+
1317+ // / Get a reference (or create if it was not created) to the plugin instance.
1318+ static GenericPluginTy &get () {
1319+ // This static variable will initialize the underlying plugin instance in
1320+ // case there was no previous explicit initialization. The initialization is
1321+ // thread safe.
1322+ static PluginTy Plugin;
1323+
1324+ assert (SpecificPlugin && " Plugin is not active" );
1325+ return *SpecificPlugin;
1326+ }
1327+
1328+ // / Get a reference to the plugin with a specific plugin-specific type.
1329+ template <typename Ty> static Ty &get () { return static_cast <Ty &>(get ()); }
1330+
1331+ // / Indicate whether the plugin is active.
1332+ static bool isActive () { return SpecificPlugin != nullptr ; }
1333+
1334+ // / Create a plugin instance.
1335+ static GenericPluginTy *createPlugin ();
1336+ };
1337+
12521338// / Auxiliary interface class for GenericDeviceResourceManagerTy. This class
12531339// / acts as a reference to a device resource, such as a stream, and requires
12541340// / some basic functions to be implemented. The derived class should define an
0 commit comments