Supporting a subtarget

A target may have a subtarget—typically, a variant with instructions—way of handling operands, among others. This subtarget feature can be supported in the LLVM backend. A subtarget may contain some additional instructions, registers, scheduling models, and so on. ARM has subtargets such as NEON and THUMB, while x86 has subtarget features such as SSE, AVX, and so on. The instruction set differs for the subtarget feature, for example, NEON for ARM and SSE/AVX for subtarget features that support vector instructions. SSE and AVX also support the vector instruction set, but their instructions differ from each other.

How to do it…

This recipe will demonstrate how to add a support subtarget feature in the backend. A new class that will inherit the TargetSubtargetInfo class has to be defined:

  1. Create a new file called TOYSubtarget.h:
    $ vi TOYSubtarget.h
    
  2. Include the following files:
    #include "TOY.h"
    #include "TOYFrameLowering.h"
    #include "TOYISelLowering.h"
    #include "TOYInstrInfo.h"
    #include "TOYSelectionDAGInfo.h"
    #include "TOYSubtarget.h"
    #include "llvm/Target/TargetMachine.h"
    #include "llvm/Target/TargetSubtargetInfo.h"
    #include "TOYGenSubtargetInfo.inc"
    
  3. Define a new class, called TOYSubtarget, with some private members that have information on the data layout, target lowering, target selection DAG, target frame lowering, and so on:
    class TOYSubtarget : public TOYGenSubtargetInfo {
      virtual void anchor();
    
    private:
      const DataLayout DL;       // Calculates type size & alignment.
      TOYInstrInfo InstrInfo;
      TOYTargetLowering TLInfo;
      TOYSelectionDAGInfo TSInfo;
      TOYFrameLowering FrameLowering;
      InstrItineraryData InstrItins;
  4. Declare its constructor:
    TOYSubtarget(const std::string &TT, const std::string &CPU, const std::string &FS, TOYTargetMachine &TM);

    This constructor initializes the data members to match that of the specified triplet.

  5. Define some helper functions to return the class-specific data:
    const InstrItineraryData *getInstrItineraryData() const override {
      return &InstrItins;
    }
    
    const TOYInstrInfo *getInstrInfo() const override { return &InstrInfo; }
    
    const TOYRegisterInfo *getRegisterInfo() const override {
      return &InstrInfo.getRegisterInfo();
    }
    
    const TOYTargetLowering *getTargetLowering() const override {
      return &TLInfo;
    }
    
    const TOYFrameLowering *getFrameLowering() const override {
      return &FrameLowering;
    }
    
    const TOYSelectionDAGInfo *getSelectionDAGInfo() const override {
      return &TSInfo;
    }
    
    const DataLayout *getDataLayout() const override { return &DL; }
    
    void ParseSubtargetFeatures(StringRef CPU, StringRef FS);
    
    TO LC,
    
    Please maintain the representation of the above code EXACTLY as seen above.
  6. Create a new file called TOYSubtarget.cpp, and define the constructor as follows:
    TOYSubtarget::TOYSubtarget(const std::string &TT, const std::string &CPU, const std::string &FS, TOYTargetMachine &TM)
          DL("e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i64:32- f64:32-a:0:32-n32"),
          InstrInfo(), TLInfo(TM), TSInfo(DL), FrameLowering() {}

The subtarget has its own data layout defined, with other information such as frame lowering, instruction information, subtarget information, and so on.

See also

  • To dive into the details of subtarget implementation, refer to the lib/Target/ARM/ARMSubtarget.cpp file in the LLVM source code
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.38.14