| 1 |
//===- LLVMToSPIRVDbgTran.h - Converts LLVM DebugInfo to SPIR-V -*- C++ -*-===// |
| 2 |
// |
| 3 |
// The LLVM/SPIR-V Translator |
| 4 |
// |
| 5 |
// This file is distributed under the University of Illinois Open Source |
| 6 |
// License. See LICENSE.TXT for details. |
| 7 |
// |
| 8 |
// Copyright (c) 2018 Intel Corporation. All rights reserved. |
| 9 |
// |
| 10 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 11 |
// copy of this software and associated documentation files (the "Software"), |
| 12 |
// to deal with the Software without restriction, including without limitation |
| 13 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 15 |
// Software is furnished to do so, subject to the following conditions: |
| 16 |
// |
| 17 |
// Redistributions of source code must retain the above copyright notice, |
| 18 |
// this list of conditions and the following disclaimers. |
| 19 |
// Redistributions in binary form must reproduce the above copyright notice, |
| 20 |
// this list of conditions and the following disclaimers in the documentation |
| 21 |
// and/or other materials provided with the distribution. |
| 22 |
// Neither the names of Intel Corporation, nor the names of its |
| 23 |
// contributors may be used to endorse or promote products derived from this |
| 24 |
// Software without specific prior written permission. |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 |
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 31 |
// THE SOFTWARE. |
| 32 |
// |
| 33 |
//===----------------------------------------------------------------------===// |
| 34 |
// |
| 35 |
// This file implements translation of debug info from LLVM metadata to SPIR-V |
| 36 |
// |
| 37 |
//===----------------------------------------------------------------------===// |
| 38 |
|
| 39 |
#ifndef LLVMTOSPIRVDBGTRAN_HPP_ |
| 40 |
#define LLVMTOSPIRVDBGTRAN_HPP_ |
| 41 |
|
| 42 |
#include "SPIRVModule.h" |
| 43 |
#include "llvm/IR/DebugInfo.h" |
| 44 |
#include "llvm/IR/Module.h" |
| 45 |
|
| 46 |
using namespace llvm; |
| 47 |
|
| 48 |
namespace SPIRV { |
| 49 |
class LLVMToSPIRVBase; |
| 50 |
|
| 51 |
class LLVMToSPIRVDbgTran { |
| 52 |
public: |
| 53 |
typedef std::vector<SPIRVWord> SPIRVWordVec; |
| 54 |
|
| 55 |
LLVMToSPIRVDbgTran(Module *TM = nullptr, SPIRVModule *TBM = nullptr, |
| 56 |
LLVMToSPIRVBase *Writer = nullptr) |
| 57 |
: BM(TBM), M(TM), SPIRVWriter(Writer), VoidT(nullptr), |
| 58 |
DebugInfoNone(nullptr) {} |
| 59 |
void transDebugMetadata(); |
| 60 |
void setModule(Module *Mod) { M = Mod; } |
| 61 |
|
| 62 |
// Mixing translation of regular instructions and debug info creates a mess. |
| 63 |
// To avoid it we translate debug info intrinsics in two steps: |
| 64 |
// 1. First time we meet debug info intrinsic during translation of a basic |
| 65 |
// block. At this time we create corresponding SPIRV debug info instruction, |
| 66 |
// but with dummy operands. Doing so we a) map llvm value to spirv value, |
| 67 |
// b) get a place for SPIRV debug info intrinsic in SPIRV basic block. |
| 68 |
// We also remember all debug intrinsics. |
| 69 |
SPIRVValue *createDebugDeclarePlaceholder(const DbgVariableIntrinsic *DbgDecl, |
| 70 |
SPIRVBasicBlock *BB); |
| 71 |
SPIRVValue *createDebugValuePlaceholder(const DbgVariableIntrinsic *DbgValue, |
| 72 |
SPIRVBasicBlock *BB); |
| 73 |
|
| 74 |
private: |
| 75 |
// 2. After translation of all regular instructions we deal with debug info. |
| 76 |
// We iterate over debug intrinsics stored on the first step, get its mapped |
| 77 |
// SPIRV instruction and tweak the operands. |
| 78 |
void finalizeDebugDeclare(const DbgVariableIntrinsic *DbgDecl); |
| 79 |
void finalizeDebugValue(const DbgVariableIntrinsic *DbgValue); |
| 80 |
|
| 81 |
// Emit DebugScope and OpLine instructions |
| 82 |
void transLocationInfo(); |
| 83 |
|
| 84 |
// Dispatcher |
| 85 |
SPIRVEntry *transDbgEntry(const MDNode *DIEntry); |
| 86 |
SPIRVEntry *transDbgEntryImpl(const MDNode *MDN); |
| 87 |
|
| 88 |
// Helper methods |
| 89 |
SPIRVType *getVoidTy(); |
| 90 |
SPIRVType *getInt32Ty(); |
| 91 |
SPIRVEntry *getScope(DIScope *SR); |
| 92 |
SPIRVEntry *getGlobalVariable(const DIGlobalVariable *GV); |
| 93 |
inline bool isNonSemanticDebugInfo(); |
| 94 |
void transformToConstant(std::vector<SPIRVWord> &Ops, |
| 95 |
std::vector<SPIRVWord> Idxs); |
| 96 |
|
| 97 |
// No debug info |
| 98 |
SPIRVEntry *getDebugInfoNone(); |
| 99 |
SPIRVId getDebugInfoNoneId(); |
| 100 |
|
| 101 |
// Compilation unit |
| 102 |
SPIRVEntry *transDbgCompileUnit(const DICompileUnit *CU); |
| 103 |
|
| 104 |
/// The following methods (till the end of the file) implement translation |
| 105 |
/// of debug instrtuctions described in the spec. |
| 106 |
|
| 107 |
// Types |
| 108 |
SPIRVEntry *transDbgBaseType(const DIBasicType *BT); |
| 109 |
SPIRVEntry *transDbgPointerType(const DIDerivedType *PT); |
| 110 |
SPIRVEntry *transDbgQualifiedType(const DIDerivedType *QT); |
| 111 |
SPIRVEntry *transDbgArrayType(const DICompositeType *AT); |
| 112 |
SPIRVEntry *transDbgArrayTypeOpenCL(const DICompositeType *AT); |
| 113 |
SPIRVEntry *transDbgArrayTypeNonSemantic(const DICompositeType *AT); |
| 114 |
SPIRVEntry *transDbgArrayTypeDynamic(const DICompositeType *AT); |
| 115 |
SPIRVEntry *transDbgSubrangeType(const DISubrange *ST); |
| 116 |
SPIRVEntry *transDbgStringType(const DIStringType *ST); |
| 117 |
SPIRVEntry *transDbgTypeDef(const DIDerivedType *D); |
| 118 |
SPIRVEntry *transDbgSubroutineType(const DISubroutineType *FT); |
| 119 |
SPIRVEntry *transDbgEnumType(const DICompositeType *ET); |
| 120 |
SPIRVEntry *transDbgCompositeType(const DICompositeType *CT); |
| 121 |
SPIRVEntry *transDbgMemberType(const DIDerivedType *MT); |
| 122 |
SPIRVEntry *transDbgMemberTypeOpenCL(const DIDerivedType *MT); |
| 123 |
SPIRVEntry *transDbgMemberTypeNonSemantic(const DIDerivedType *MT); |
| 124 |
SPIRVEntry *transDbgInheritance(const DIDerivedType *DT); |
| 125 |
SPIRVEntry *transDbgPtrToMember(const DIDerivedType *DT); |
| 126 |
|
| 127 |
// Templates |
| 128 |
SPIRVEntry *transDbgTemplateParams(DITemplateParameterArray TPA, |
| 129 |
const SPIRVEntry *Target); |
| 130 |
SPIRVEntry *transDbgTemplateParameter(const DITemplateParameter *TP); |
| 131 |
SPIRVEntry * |
| 132 |
transDbgTemplateTemplateParameter(const DITemplateValueParameter *TP); |
| 133 |
SPIRVEntry *transDbgTemplateParameterPack(const DITemplateValueParameter *TP); |
| 134 |
|
| 135 |
// Global objects |
| 136 |
SPIRVEntry *transDbgGlobalVariable(const DIGlobalVariable *GV); |
| 137 |
SPIRVEntry *transDbgFunction(const DISubprogram *Func); |
| 138 |
|
| 139 |
SPIRVEntry *transDbgFuncDefinition(SPIRVValue *SPVFunc, SPIRVEntry *DbgFunc); |
| 140 |
SPIRVEntry *transDbgEntryPoint(const DISubprogram *Func, SPIRVEntry *DbgFunc); |
| 141 |
|
| 142 |
// Location information |
| 143 |
SPIRVEntry *transDbgScope(const DIScope *S); |
| 144 |
SPIRVEntry *transDebugLoc(const DebugLoc &Loc, SPIRVBasicBlock *BB, |
| 145 |
SPIRVInstruction *InsertBefore = nullptr); |
| 146 |
SPIRVEntry *transDbgInlinedAt(const DILocation *D); |
| 147 |
SPIRVEntry *transDbgInlinedAtNonSemanticShader200(const DILocation *D); |
| 148 |
|
| 149 |
template <class T> SPIRVExtInst *getSource(const T *DIEntry); |
| 150 |
SPIRVEntry *transDbgFileType(const DIFile *F); |
| 151 |
|
| 152 |
// Generate instructions recording identifier and file where debug information |
| 153 |
// was split to |
| 154 |
void generateBuildIdentifierAndStoragePath(const DICompileUnit *DIEntry); |
| 155 |
|
| 156 |
// Local Variables |
| 157 |
SPIRVEntry *transDbgLocalVariable(const DILocalVariable *Var); |
| 158 |
|
| 159 |
// DWARF expressions |
| 160 |
SPIRVEntry *transDbgExpression(const DIExpression *Expr); |
| 161 |
|
| 162 |
// Imported declarations and modules |
| 163 |
SPIRVEntry *transDbgImportedEntry(const DIImportedEntity *IE); |
| 164 |
|
| 165 |
// A module in programming language. Example - Fortran module, clang module. |
| 166 |
SPIRVEntry *transDbgModule(const DIModule *IE); |
| 167 |
|
| 168 |
// Flags |
| 169 |
SPIRVWord mapDebugFlags(DINode::DIFlags DFlags); |
| 170 |
SPIRVWord transDebugFlags(const DINode *DN); |
| 171 |
|
| 172 |
SPIRVModule *BM; |
| 173 |
Module *M; |
| 174 |
LLVMToSPIRVBase *SPIRVWriter; |
| 175 |
std::unordered_map<const MDNode *, SPIRVEntry *> MDMap; |
| 176 |
std::unordered_map<std::string, SPIRVExtInst *> FileMap; |
| 177 |
DebugInfoFinder DIF; |
| 178 |
SPIRVType *VoidT = nullptr; |
| 179 |
SPIRVType *Int32T = nullptr; |
| 180 |
SPIRVEntry *DebugInfoNone; |
| 181 |
std::unordered_map<const DICompileUnit *, SPIRVExtInst *> SPIRVCUMap; |
| 182 |
std::vector<const DbgVariableIntrinsic *> DbgDeclareIntrinsics; |
| 183 |
std::vector<const DbgVariableIntrinsic *> DbgValueIntrinsics; |
| 184 |
|
| 185 |
SPIRVExtInst *BuildIdentifierInsn{nullptr}; |
| 186 |
SPIRVExtInst *StoragePathInsn{nullptr}; |
| 187 |
}; // class LLVMToSPIRVDbgTran |
| 188 |
|
| 189 |
} // namespace SPIRV |
| 190 |
|
| 191 |
#endif // LLVMTOSPIRVDBGTRAN_HPP_ |