PATCH: Registry template

This is a template that takes care of managing plugin registries. I wrote it because I got the distinct feeling I was needlessly reinventing the wheel as I wrote a collector registry. I haven't converted anything else to use it, though. Still, it's orthogonal and has become stable in my usage, so I thought I'd submit it in advance.

To get my collector registry going, all I needed was 5 lines:

   // GCRegistry.h
   typedef Registry<TargetCollector> CollectorRegistry;

   // GCRegistry.cpp
   template<> CollectorRegistry::node *CollectorRegistry::Head = 0;
   template<> CollectorRegistry::node *CollectorRegistry::Tail = 0;
   template<> CollectorRegistry::listener *CollectorRegistry::ListenerHead = 0;
   template<> CollectorRegistry::listener *CollectorRegistry::ListenerTail = 0;

Afterwards, the most important features work more or less as to be expected:

   // ShadowStackCollector.cpp
   Y("shadow-stack", "Shadow-stack collector, for uncooperative code generators");

   // llc.cpp
   static cl::opt<const CollectorRegistry::entry*, false, CollectorRegistry::Parser>
   GC("gc", cl::desc("Enable garbage collection."));

   Collector = GC->instantiate();

   // nowhere
   for (CollectorRegistry::iterator I = CollectorRegistry::begin(),
                                    E = CollectorRegistry::end(); I != E; ++I)
     cout << I->getName() << ": " << I->getDesc() << "\n";

Granted, my needs are currently simple. The default entry type has just the name, description, and use the default constructor. To convert, many registries in the system would need to also define:

  1. An custom entry type, particularly where the default constructor was insufficient.
  2. A custom traits type.
  3. The RegisterFoo template, to create the constructor wrapper springboard.


Index: include/llvm/Support/Registry.h

Woo, that got mangled by wrapping. Here's an attachment.

registry-template.patch (6.97 KB)

-50 LOC for TargetMachineRegistry.

— Gordon

targetmachineregistry.patch (12 KB)

Woot! I relized the need for such a thing when I did the pluggable
register coalescer work.

In particular, register allocators should use this. They use a special
regalloc-only thing that as far as I can tell only exists for historical
reasons (perhaps it was the first one?).