Home | History | Annotate | Download | only in AMDGPU
      1 //===-- AMDGPUAsmPrinter.h - Print AMDGPU assembly code ---------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 /// \file
     11 /// \brief AMDGPU Assembly printer class.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
     16 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
     17 
     18 #include "llvm/CodeGen/AsmPrinter.h"
     19 #include <vector>
     20 
     21 namespace llvm {
     22 
     23 class AMDGPUAsmPrinter final : public AsmPrinter {
     24 private:
     25   struct SIProgramInfo {
     26     SIProgramInfo() :
     27       VGPRBlocks(0),
     28       SGPRBlocks(0),
     29       Priority(0),
     30       FloatMode(0),
     31       Priv(0),
     32       DX10Clamp(0),
     33       DebugMode(0),
     34       IEEEMode(0),
     35       ScratchSize(0),
     36       ComputePGMRSrc1(0),
     37       LDSBlocks(0),
     38       ScratchBlocks(0),
     39       ComputePGMRSrc2(0),
     40       NumVGPR(0),
     41       NumSGPR(0),
     42       FlatUsed(false),
     43       ReservedVGPRFirst(0),
     44       ReservedVGPRCount(0),
     45       DebuggerWavefrontPrivateSegmentOffsetSGPR((uint16_t)-1),
     46       DebuggerPrivateSegmentBufferSGPR((uint16_t)-1),
     47       VCCUsed(false),
     48       CodeLen(0) {}
     49 
     50     // Fields set in PGM_RSRC1 pm4 packet.
     51     uint32_t VGPRBlocks;
     52     uint32_t SGPRBlocks;
     53     uint32_t Priority;
     54     uint32_t FloatMode;
     55     uint32_t Priv;
     56     uint32_t DX10Clamp;
     57     uint32_t DebugMode;
     58     uint32_t IEEEMode;
     59     uint32_t ScratchSize;
     60 
     61     uint64_t ComputePGMRSrc1;
     62 
     63     // Fields set in PGM_RSRC2 pm4 packet.
     64     uint32_t LDSBlocks;
     65     uint32_t ScratchBlocks;
     66 
     67     uint64_t ComputePGMRSrc2;
     68 
     69     uint32_t NumVGPR;
     70     uint32_t NumSGPR;
     71     uint32_t LDSSize;
     72     bool FlatUsed;
     73 
     74     // If ReservedVGPRCount is 0 then must be 0. Otherwise, this is the first
     75     // fixed VGPR number reserved.
     76     uint16_t ReservedVGPRFirst;
     77     // The number of consecutive VGPRs reserved.
     78     uint16_t ReservedVGPRCount;
     79 
     80     // Fixed SGPR number used to hold wave scratch offset for entire kernel
     81     // execution, or uint16_t(-1) if the register is not used or not known.
     82     uint16_t DebuggerWavefrontPrivateSegmentOffsetSGPR;
     83     // Fixed SGPR number of the first 4 SGPRs used to hold scratch V# for entire
     84     // kernel execution, or uint16_t(-1) if the register is not used or not
     85     // known.
     86     uint16_t DebuggerPrivateSegmentBufferSGPR;
     87 
     88     // Bonus information for debugging.
     89     bool VCCUsed;
     90     uint64_t CodeLen;
     91   };
     92 
     93   void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF) const;
     94   void findNumUsedRegistersSI(const MachineFunction &MF,
     95                               unsigned &NumSGPR,
     96                               unsigned &NumVGPR) const;
     97 
     98   /// \brief Emit register usage information so that the GPU driver
     99   /// can correctly setup the GPU state.
    100   void EmitProgramInfoR600(const MachineFunction &MF);
    101   void EmitProgramInfoSI(const MachineFunction &MF, const SIProgramInfo &KernelInfo);
    102   void EmitAmdKernelCodeT(const MachineFunction &MF,
    103                           const SIProgramInfo &KernelInfo) const;
    104 
    105 public:
    106   explicit AMDGPUAsmPrinter(TargetMachine &TM,
    107                             std::unique_ptr<MCStreamer> Streamer);
    108 
    109   bool runOnMachineFunction(MachineFunction &MF) override;
    110 
    111   const char *getPassName() const override {
    112     return "AMDGPU Assembly Printer";
    113   }
    114 
    115   /// Implemented in AMDGPUMCInstLower.cpp
    116   void EmitInstruction(const MachineInstr *MI) override;
    117 
    118   void EmitFunctionBodyStart() override;
    119 
    120   void EmitFunctionEntryLabel() override;
    121 
    122   void EmitGlobalVariable(const GlobalVariable *GV) override;
    123 
    124   void EmitStartOfAsmFile(Module &M) override;
    125 
    126   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
    127                        unsigned AsmVariant, const char *ExtraCode,
    128                        raw_ostream &O) override;
    129 
    130 protected:
    131   std::vector<std::string> DisasmLines, HexLines;
    132   size_t DisasmLineMaxLen;
    133 };
    134 
    135 } // End anonymous llvm
    136 
    137 #endif
    138