comparison mlir/lib/Target/LLVMIR/DebugTranslation.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 0572611fdcc8
children c4bab56944e8
comparison
equal deleted inserted replaced
173:0572611fdcc8 207:2e18cbf3894f
30 30
31 // If the module has no location information, there is nothing to do. 31 // If the module has no location information, there is nothing to do.
32 if (!module->walk(interruptIfValidLocation).wasInterrupted()) 32 if (!module->walk(interruptIfValidLocation).wasInterrupted())
33 return; 33 return;
34 34
35 // TODO(riverriddle) Several parts of this are incorrect. Different source 35 // TODO: Several parts of this are incorrect. Different source
36 // languages may interpret different parts of the debug information 36 // languages may interpret different parts of the debug information
37 // differently. Frontends will also want to pipe in various information, like 37 // differently. Frontends will also want to pipe in various information, like
38 // flags. This is fine for now as we only emit line-table information and not 38 // flags. This is fine for now as we only emit line-table information and not
39 // types or variables. This should disappear as the debug information story 39 // types or variables. This should disappear as the debug information story
40 // evolves; i.e. when we have proper attributes for LLVM debug metadata. 40 // evolves; i.e. when we have proper attributes for LLVM debug metadata.
46 // Mark this module as having debug information. 46 // Mark this module as having debug information.
47 StringRef debugVersionKey = "Debug Info Version"; 47 StringRef debugVersionKey = "Debug Info Version";
48 if (!llvmModule.getModuleFlag(debugVersionKey)) 48 if (!llvmModule.getModuleFlag(debugVersionKey))
49 llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey, 49 llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey,
50 llvm::DEBUG_METADATA_VERSION); 50 llvm::DEBUG_METADATA_VERSION);
51
52 if (auto targetTripleAttr =
53 module->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) {
54 auto targetTriple =
55 llvm::Triple(targetTripleAttr.cast<StringAttr>().getValue());
56 if (targetTriple.isKnownWindowsMSVCEnvironment()) {
57 // Dwarf debugging files will be generated by default, unless "CodeView"
58 // is set explicitly. Windows/MSVC should use CodeView instead.
59 llvmModule.addModuleFlag(llvm::Module::Warning, "CodeView", 1);
60 }
61 }
51 } 62 }
52 63
53 /// Finalize the translation of debug information. 64 /// Finalize the translation of debug information.
54 void DebugTranslation::finalize() { builder.finalize(); } 65 void DebugTranslation::finalize() { builder.finalize(); }
55 66
69 // If the function doesn't have location information, there is nothing to 80 // If the function doesn't have location information, there is nothing to
70 // translate. 81 // translate.
71 if (!compileUnit || !func.walk(interruptIfValidLocation).wasInterrupted()) 82 if (!compileUnit || !func.walk(interruptIfValidLocation).wasInterrupted())
72 return; 83 return;
73 84
85 // If we are to create debug info for the function, we need to ensure that all
86 // inlinable calls in it are with debug info, otherwise the LLVM verifier will
87 // complain. For now, be more restricted and treat all calls as inlinable.
88 const bool hasCallWithoutDebugInfo =
89 func.walk([](LLVM::CallOp call) {
90 return call.getLoc().isa<UnknownLoc>() ? WalkResult::interrupt()
91 : WalkResult::advance();
92 })
93 .wasInterrupted();
94 if (hasCallWithoutDebugInfo)
95 return;
96
74 FileLineColLoc fileLoc = extractFileLoc(func.getLoc()); 97 FileLineColLoc fileLoc = extractFileLoc(func.getLoc());
75 auto *file = translateFile(fileLoc ? fileLoc.getFilename() : "<unknown>"); 98 auto *file =
99 translateFile(fileLoc ? fileLoc.getFilename().strref() : "<unknown>");
76 unsigned line = fileLoc ? fileLoc.getLine() : 0; 100 unsigned line = fileLoc ? fileLoc.getLine() : 0;
77 101
78 // TODO(riverriddle) This is the bare essentials for now. We will likely end 102 // TODO: This is the bare essentials for now. We will likely end
79 // up with wrapper metadata around LLVMs metadata in the future, so this 103 // up with wrapper metadata around LLVMs metadata in the future, so this
80 // doesn't need to be smart until then. 104 // doesn't need to be smart until then.
81 llvm::DISubroutineType *type = 105 llvm::DISubroutineType *type =
82 builder.createSubroutineType(builder.getOrCreateTypeArray(llvm::None)); 106 builder.createSubroutineType(builder.getOrCreateTypeArray(llvm::None));
83 llvm::DISubprogram::DISPFlags spFlags = llvm::DISubprogram::SPFlagDefinition | 107 llvm::DISubprogram::DISPFlags spFlags = llvm::DISubprogram::SPFlagDefinition |
113 auto existingIt = locationToLoc.find(std::make_pair(loc, scope)); 137 auto existingIt = locationToLoc.find(std::make_pair(loc, scope));
114 if (existingIt != locationToLoc.end()) 138 if (existingIt != locationToLoc.end())
115 return existingIt->second; 139 return existingIt->second;
116 140
117 const llvm::DILocation *llvmLoc = nullptr; 141 const llvm::DILocation *llvmLoc = nullptr;
118 switch (loc->getKind()) { 142 if (auto callLoc = loc.dyn_cast<CallSiteLoc>()) {
119 case StandardAttributes::CallSiteLocation: {
120 auto callLoc = loc.dyn_cast<CallSiteLoc>();
121
122 // For callsites, the caller is fed as the inlinedAt for the callee. 143 // For callsites, the caller is fed as the inlinedAt for the callee.
123 const auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt); 144 const auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
124 llvmLoc = translateLoc(callLoc.getCallee(), scope, callerLoc); 145 llvmLoc = translateLoc(callLoc.getCallee(), scope, callerLoc);
125 break; 146
126 } 147 } else if (auto fileLoc = loc.dyn_cast<FileLineColLoc>()) {
127 case StandardAttributes::FileLineColLocation: {
128 auto fileLoc = loc.dyn_cast<FileLineColLoc>();
129 auto *file = translateFile(fileLoc.getFilename()); 148 auto *file = translateFile(fileLoc.getFilename());
130 auto *fileScope = builder.createLexicalBlockFile(scope, file); 149 auto *fileScope = builder.createLexicalBlockFile(scope, file);
131 llvmLoc = llvm::DILocation::get(llvmCtx, fileLoc.getLine(), 150 llvmLoc = llvm::DILocation::get(llvmCtx, fileLoc.getLine(),
132 fileLoc.getColumn(), fileScope, 151 fileLoc.getColumn(), fileScope,
133 const_cast<llvm::DILocation *>(inlinedAt)); 152 const_cast<llvm::DILocation *>(inlinedAt));
134 break; 153
135 } 154 } else if (auto fusedLoc = loc.dyn_cast<FusedLoc>()) {
136 case StandardAttributes::FusedLocation: {
137 auto fusedLoc = loc.dyn_cast<FusedLoc>();
138 ArrayRef<Location> locations = fusedLoc.getLocations(); 155 ArrayRef<Location> locations = fusedLoc.getLocations();
139 156
140 // For fused locations, merge each of the nodes. 157 // For fused locations, merge each of the nodes.
141 llvmLoc = translateLoc(locations.front(), scope, inlinedAt); 158 llvmLoc = translateLoc(locations.front(), scope, inlinedAt);
142 for (Location locIt : locations.drop_front()) { 159 for (Location locIt : locations.drop_front()) {
143 llvmLoc = llvm::DILocation::getMergedLocation( 160 llvmLoc = llvm::DILocation::getMergedLocation(
144 llvmLoc, translateLoc(locIt, scope, inlinedAt)); 161 llvmLoc, translateLoc(locIt, scope, inlinedAt));
145 } 162 }
146 break; 163
147 } 164 } else if (auto nameLoc = loc.dyn_cast<NameLoc>()) {
148 case StandardAttributes::NameLocation:
149 llvmLoc = translateLoc(loc.cast<NameLoc>().getChildLoc(), scope, inlinedAt); 165 llvmLoc = translateLoc(loc.cast<NameLoc>().getChildLoc(), scope, inlinedAt);
150 break; 166
151 case StandardAttributes::OpaqueLocation: 167 } else if (auto opaqueLoc = loc.dyn_cast<OpaqueLoc>()) {
152 llvmLoc = translateLoc(loc.cast<OpaqueLoc>().getFallbackLocation(), scope, 168 llvmLoc = translateLoc(loc.cast<OpaqueLoc>().getFallbackLocation(), scope,
153 inlinedAt); 169 inlinedAt);
154 break; 170 } else {
155 default:
156 llvm_unreachable("unknown location kind"); 171 llvm_unreachable("unknown location kind");
157 } 172 }
173
158 locationToLoc.try_emplace(std::make_pair(loc, scope), llvmLoc); 174 locationToLoc.try_emplace(std::make_pair(loc, scope), llvmLoc);
159 return llvmLoc; 175 return llvmLoc;
160 } 176 }
161 177
162 /// Create an llvm debug file for the given file path. 178 /// Create an llvm debug file for the given file path.