[PATCH] Changing the Back-end when Target Options Change

Du Toit, Stefanus stefanus.du.toit at intel.com
Thu Jun 20 06:48:28 PDT 2013


Hi Bill,

From:  Bill Wendling <wendling at apple.com>
> For those not interested in slugging their way through a ton of
> refactoring code, here's a cleaner patch.
> 
> The executive summary:
> 
> This regenerates the TargetMachine object if the any of the values in
> TargetOptions changes. The regeneration is recursive, because creating
> TargetMachine pretty much creates all of the other back-end objects that
> are needed for code generation.

You could use the "curiously recurring template pattern" and add an
instance of a class template in the middle of the inheritance hierarchy,
e.g. something alone the lines of:

template<typename Derived>
class TargetMachineHelper : public TargetMachine {
  TargetMachine *
  regenerateWithOptionsImpl(const TargetOptions &Opts,
                            Reloc::Model RM,
                            CodeModel::Model CM,
                            CodeGenOpt::Level OptLvl,
                            std::string &targetTriple,
                            std::string &targetCPU,
                            std::string &targetFS) {
    TargetPassConfig *TPC = PassConfig;

    this->~Derived();
    Derived *NewTM = new (this)
      Derived(TheTarget, targetTriple, targetCPU, targetFS,
              Opts, RM, CM, OptLvl);

    if (TPC)
      NewTM->PassConfig = TPC->regenerateWithTargetMachine(NewTM);

    return NewTM;
  }
};

// Then, for each backend, change the inheritance like so:

class MyTargetMachine : public TargetMachineHelper<MyTargetMachine> {
 // ...

(I'm sure there are errors in the above, but hopefully it gets the idea
across)

This would remove the need to define all of these similar-looking
functions in the backends. Should work for PassConfig too.

Stefanus

--
Stefanus Du Toit <stefanus.du.toit at intel.com>
Intel Waterloo
Phone: 519-591-1738











More information about the llvm-commits mailing list