@@ -203,7 +203,7 @@ int64_t CompileUnit::getDefaultLowerBound() const
203
203
}
204
204
205
205
// / Check whether the DIE for this MDNode can be shared across CUs.
206
- static bool isShareableAcrossCUs (llvm::MDNode* D)
206
+ static bool isShareableAcrossCUs (const llvm::MDNode* D)
207
207
{
208
208
// When the MDNode can be part of the type system, the DIE can be
209
209
// shared across CUs.
@@ -440,6 +440,20 @@ void CompileUnit::addSourceLine(DIE* Die, DIScope* S, unsigned Line)
440
440
addUInt (Die, dwarf::DW_AT_decl_line, None, Line);
441
441
}
442
442
443
+ // / addSourceLine - Add location information to specified debug information
444
+ // / entry.
445
+ void CompileUnit::addSourceLine (DIE* Die, DIImportedEntity* IE, unsigned Line)
446
+ {
447
+ // If the line number is 0, don't add it.
448
+ if (Line == 0 )
449
+ return ;
450
+
451
+ unsigned FileID = DD->getOrCreateSourceID (IE->getFile ()->getFilename (), IE->getFile ()->getDirectory (), getUniqueID ());
452
+ IGC_ASSERT_MESSAGE (FileID, " Invalid file id" );
453
+ addUInt (Die, dwarf::DW_AT_decl_file, None, FileID);
454
+ addUInt (Die, dwarf::DW_AT_decl_line, None, Line);
455
+ }
456
+
443
457
// / addSourceLine - Add location information to specified debug information
444
458
// / entry.
445
459
void CompileUnit::addSourceLine (DIE* Die, DIVariable* V)
@@ -736,7 +750,6 @@ IGC::DIE* CompileUnit::getOrCreateContextDIE(DIScope* Context)
736
750
return getOrCreateModuleDIE (MD);
737
751
738
752
return getDIE (Context);
739
-
740
753
}
741
754
742
755
// / getOrCreateTypeDIE - Find existing DIE or create new DIE for the
@@ -1658,6 +1671,27 @@ std::string CompileUnit::getParentContextString(DIScope* Context) const
1658
1671
return CS;
1659
1672
}
1660
1673
1674
+ // Decode line number, file name and location from a string, where a line no., file name
1675
+ // and directory are separated by '?' character: lineNumber?fileName?directory
1676
+ // There is a workaround for DIModule creation in earlier LLVM versions, where a line and a file
1677
+ // parameters are not supported in DIBuilder.
1678
+ void CompileUnit::decodeLineAndFileForISysRoot (StringRef& lineAndFile, unsigned int * line, std::string* file,
1679
+ std::string* directory)
1680
+ {
1681
+ #if LLVM_VERSION_MAJOR < 11
1682
+ SmallVector<StringRef, 8 > splitStr;
1683
+ lineAndFile.split (splitStr, " ?" );// substr(0, posOfLineAndDirSeparator).str().copy()
1684
+ unsigned int posOfFirstQ = lineAndFile.find_first_of (' ?' , 0 );
1685
+ std::string lineStr = " " ;
1686
+ lineStr.append (splitStr[0 ].str ().c_str (), posOfFirstQ);
1687
+ unsigned int posOfSecondQ = lineAndFile.find_first_of (' ?' , posOfFirstQ + 1 );
1688
+
1689
+ directory->append (splitStr[1 ].str ().c_str (), posOfSecondQ - posOfFirstQ);
1690
+ file->append (splitStr[2 ].str ().c_str (), splitStr[2 ].size ());
1691
+ *line = (unsigned int )std::atoi (splitStr[0 ].data ());
1692
+ #endif // LLVM_VERSION_MAJOR < 11
1693
+ }
1694
+
1661
1695
// / constructTypeDIE - Construct basic type die from DIBasicType.
1662
1696
void CompileUnit::constructTypeDIE (DIE& Buffer, DIBasicType* BTy)
1663
1697
{
@@ -2013,6 +2047,42 @@ void CompileUnit::constructTemplateValueParameterDIE(
2013
2047
}
2014
2048
2015
2049
2050
+ // / constructImportedEntityDIE - Create a DIE for DIImportedEntity.
2051
+ IGC::DIE* CompileUnit::constructImportedEntityDIE (
2052
+ DIImportedEntity* Module)
2053
+ {
2054
+ DINode* Entity = Module->getEntity ();
2055
+ if (auto * GV = dyn_cast<DIGlobalVariable>(Entity))
2056
+ return nullptr ; // Missing support for imported entity linked to a global variable
2057
+
2058
+ DIE* IMDie = new DIE (Module->getTag ());
2059
+ insertDIE (Module, IMDie);
2060
+
2061
+ DIE* EntityDie;
2062
+ if (auto * NS = dyn_cast<DINamespace>(Entity))
2063
+ EntityDie = getOrCreateNameSpace (NS);
2064
+ else if (auto * M = dyn_cast<DIModule>(Entity))
2065
+ EntityDie = getOrCreateModuleDIE (M);
2066
+ else if (auto * SP = dyn_cast<DISubprogram>(Entity))
2067
+ EntityDie = getOrCreateSubprogramDIE (SP);
2068
+ else if (auto * T = dyn_cast<DIType>(Entity))
2069
+ EntityDie = getOrCreateTypeDIE (T);
2070
+ // else if (auto* GV = dyn_cast<DIGlobalVariable>(Entity)) // TODO missing support
2071
+ // EntityDie = getOrCreateGlobalVariableDIE(GV, {});
2072
+ else
2073
+ EntityDie = getDIE (Entity);
2074
+
2075
+ assert (EntityDie);
2076
+
2077
+ addSourceLine (IMDie, Module, Module->getLine ());
2078
+ addDIEEntry (IMDie, dwarf::DW_AT_import, EntityDie);
2079
+ StringRef Name = Module->getName ();
2080
+ if (!Name.empty ())
2081
+ addString (IMDie, dwarf::DW_AT_name, Name);
2082
+
2083
+ return IMDie;
2084
+ }
2085
+
2016
2086
// / getOrCreateNameSpace - Create a DIE for DINameSpace.
2017
2087
IGC::DIE* CompileUnit::getOrCreateNameSpace (DINamespace* NS)
2018
2088
{
@@ -2145,6 +2215,8 @@ IGC::DIE* CompileUnit::getOrCreateSubprogramDIE(DISubprogram* SP)
2145
2215
{
2146
2216
DIE* Arg = createAndAddDIE (dwarf::DW_TAG_formal_parameter, *SPDie);
2147
2217
DIType* ATy = resolve (Args[i]);
2218
+ if (!ATy)
2219
+ continue ;
2148
2220
addType (Arg, ATy);
2149
2221
if (ATy->isArtificial ())
2150
2222
{
@@ -2176,10 +2248,58 @@ IGC::DIE* CompileUnit::getOrCreateModuleDIE(DIModule* MD)
2176
2248
// Construct the context before querying for the existence of the DIE in case
2177
2249
// such construction creates the DIE (as is the case for member function
2178
2250
// declarations).
2251
+ DIE* ContextDIE = getOrCreateContextDIE (MD->getScope ());
2252
+ DIE* MDDie = getDIE (MD);
2253
+ if (MDDie)
2254
+ return MDDie;
2255
+
2256
+ MDDie = createAndAddDIE (dwarf::DW_TAG_module, *ContextDIE, MD);
2257
+ IGC_ASSERT (MDDie);
2258
+
2259
+ if (!MD->getName ().empty ())
2260
+ {
2261
+ addString (MDDie, dwarf::DW_AT_name, MD->getName ());
2262
+ }
2263
+
2264
+ #if LLVM_VERSION_MAJOR < 11
2265
+ #if LLVM_VERSION_MAJOR < 10
2266
+ StringRef iSysRoot = MD->getISysRoot ();
2267
+ #else // LLVM_VERSION_MAJOR == 10
2268
+ StringRef iSysRoot = MD->getSysRoot ();
2269
+ #endif // LLVM_VERSION_MAJOR == 10
2270
+ unsigned int line;
2271
+ std::string file = " " ;
2272
+ std::string directory = " " ;
2273
+
2274
+ decodeLineAndFileForISysRoot (iSysRoot, &line, &file, &directory);
2179
2275
2180
- IGC_ASSERT_MESSAGE (false , " Missing implementation for DIModule!" );
2276
+ // Emit a line number and a file only if line number is significant
2277
+ if (line > 0 )
2278
+ addUInt (MDDie, dwarf::DW_AT_decl_line, None, line);
2279
+ // Emit a file if not empty name
2280
+ if (!file.empty () || !directory.empty ())
2281
+ {
2282
+ StringRef fileRef (file);
2283
+ StringRef dirRef (directory);
2284
+ unsigned FileID = DD->getOrCreateSourceID (fileRef, dirRef, getUniqueID ());
2285
+ addUInt (MDDie, dwarf::DW_AT_decl_file, None, FileID);
2286
+ }
2287
+ #else // LLVM_VERSION_MAJOR >= 11
2288
+ #if LLVM_VERSION_MAJOR == 11
2289
+ addSourceLine (MDDie, MD, MD->getLineNo ());
2290
+ #elif LLVM_VERSION_MAJOR >= 12
2291
+ if (!MD->getIsDecl ())
2292
+ {
2293
+ addSourceLine (MDDie, MD, MD->getLineNo ());
2294
+ }
2295
+ else
2296
+ {
2297
+ addFlag (MDDie, dwarf::DW_AT_declaration);
2298
+ }
2299
+ #endif // LLVM_VERSION_MAJOR >= 12
2300
+ #endif // LLVM_VERSION_MAJOR >= 11.
2181
2301
2182
- return nullptr ;
2302
+ return MDDie ;
2183
2303
}
2184
2304
2185
2305
// / constructSubrangeDIE - Construct subrange DIE from DISubrange.
0 commit comments