| 1 | //=====-- NVPTXTargetStreamer.h - NVPTX Target Streamer ------*- C++ -*--=====// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIB_TARGET_NVPTX_MCTARGETDESC_NVPTXTARGETSTREAMER_H |
| 10 | #define LLVM_LIB_TARGET_NVPTX_MCTARGETDESC_NVPTXTARGETSTREAMER_H |
| 11 | |
| 12 | #include "llvm/ADT/ArrayRef.h" |
| 13 | #include "llvm/MC/MCStreamer.h" |
| 14 | #include <optional> |
| 15 | |
| 16 | namespace llvm { |
| 17 | class MCSection; |
| 18 | class MCSymbol; |
| 19 | class formatted_raw_ostream; |
| 20 | |
| 21 | /// Implments NVPTX-specific streamer. |
| 22 | class NVPTXTargetStreamer : public MCTargetStreamer { |
| 23 | private: |
| 24 | SmallVector<std::string, 4> DwarfFiles; |
| 25 | bool HasSections = false; |
| 26 | |
| 27 | public: |
| 28 | NVPTXTargetStreamer(MCStreamer &S); |
| 29 | ~NVPTXTargetStreamer() override; |
| 30 | |
| 31 | /// Emit the banner which specifies details of PTX generator. |
| 32 | virtual void emitBanner() {} |
| 33 | |
| 34 | /// Emit the PTX ISA version number. |
| 35 | virtual void emitVersionDirective(unsigned PTXVersion) {} |
| 36 | |
| 37 | /// Emit architecture and platform target. |
| 38 | virtual void emitTargetDirective(StringRef Target, bool TexModeIndependent, |
| 39 | bool HasDebug) {} |
| 40 | |
| 41 | /// Emit address size used for this PTX module. |
| 42 | virtual void emitAddressSizeDirective(unsigned AddrSize) {} |
| 43 | |
| 44 | /// Emit the list of branch targets a brx.idx may jump to. |
| 45 | virtual void emitBranchTargetsDirective(ArrayRef<const MCSymbol *> Targets) {} |
| 46 | |
| 47 | /// Declare a register \p SizeInBits wide. \p Count declares a numbered bank |
| 48 | /// of that many registers sharing \p Name as their prefix, rather than a |
| 49 | /// single register named \p Name. |
| 50 | virtual void emitRegDirective(unsigned SizeInBits, StringRef Name, |
| 51 | std::optional<unsigned> Count = std::nullopt) {} |
| 52 | |
| 53 | /// Declare \p Size bytes of local (stack) memory named \p Name. |
| 54 | virtual void emitLocalDirective(Align Alignment, const MCSymbol *Name, |
| 55 | uint64_t Size) {} |
| 56 | |
| 57 | /// Emit an alias from \p Name to \p Aliasee. |
| 58 | virtual void emitAliasDirective(const MCSymbol *Name, |
| 59 | const MCSymbol *Aliasee) {} |
| 60 | |
| 61 | /// Emit a pragma governing the code that follows it. |
| 62 | virtual void emitPragmaDirective(StringRef Pragma) {} |
| 63 | |
| 64 | /// Emit a section with an empty body. |
| 65 | virtual void emitEmptySectionDirective(StringRef Name) {} |
| 66 | |
| 67 | /// Outputs the list of the DWARF '.file' directives to the streamer. |
| 68 | void outputDwarfFileDirectives(); |
| 69 | /// Close last section. |
| 70 | void closeLastSection(); |
| 71 | |
| 72 | /// Record DWARF file directives for later output. |
| 73 | /// According to PTX ISA, CUDA Toolkit documentation, 11.5.3. Debugging |
| 74 | /// Directives: .file |
| 75 | /// (http://docs.nvidia.com/cuda/parallel-thread-execution/index.html#debugging-directives-file), |
| 76 | /// The .file directive is allowed only in the outermost scope, i.e., at the |
| 77 | /// same level as kernel and device function declarations. Also, the order of |
| 78 | /// the .loc and .file directive does not matter, .file directives may follow |
| 79 | /// the .loc directives where the file is referenced. |
| 80 | /// LLVM emits .file directives immediately the location debug info is |
| 81 | /// emitted, i.e. they may be emitted inside functions. We gather all these |
| 82 | /// directives and emit them outside of the sections and, thus, outside of the |
| 83 | /// functions. |
| 84 | void emitDwarfFileDirective(StringRef Directive) override; |
| 85 | void changeSection(const MCSection *CurSection, MCSection *Section, |
| 86 | uint32_t SubSection, raw_ostream &OS) override; |
| 87 | /// Emit the bytes in \p Data into the output. |
| 88 | /// |
| 89 | /// This is used to emit bytes in \p Data as sequence of .byte directives. |
| 90 | void emitRawBytes(StringRef Data) override; |
| 91 | /// Makes sure that labels are mangled the same way as the actual symbols. |
| 92 | void emitValue(const MCExpr *Value) override; |
| 93 | }; |
| 94 | |
| 95 | class NVPTXAsmTargetStreamer : public NVPTXTargetStreamer { |
| 96 | formatted_raw_ostream &OS; |
| 97 | |
| 98 | public: |
| 99 | NVPTXAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS); |
| 100 | ~NVPTXAsmTargetStreamer() override; |
| 101 | |
| 102 | void emitBanner() override; |
| 103 | |
| 104 | void emitVersionDirective(unsigned PTXVersion) override; |
| 105 | |
| 106 | void emitTargetDirective(StringRef Target, bool TexModeIndependent, |
| 107 | bool HasDebug) override; |
| 108 | |
| 109 | void emitAddressSizeDirective(unsigned AddrSize) override; |
| 110 | |
| 111 | void emitBranchTargetsDirective(ArrayRef<const MCSymbol *> Targets) override; |
| 112 | |
| 113 | void emitRegDirective(unsigned SizeInBits, StringRef Name, |
| 114 | std::optional<unsigned> Count) override; |
| 115 | |
| 116 | void emitLocalDirective(Align Alignment, const MCSymbol *Name, |
| 117 | uint64_t Size) override; |
| 118 | |
| 119 | void emitAliasDirective(const MCSymbol *Name, |
| 120 | const MCSymbol *Aliasee) override; |
| 121 | |
| 122 | void emitPragmaDirective(StringRef Pragma) override; |
| 123 | |
| 124 | void emitEmptySectionDirective(StringRef Name) override; |
| 125 | }; |
| 126 | |
| 127 | } // end namespace llvm |
| 128 | |
| 129 | #endif |
| 130 | |