Skip to content

Commit dbf5f41

Browse files
authored
Improvements around review of debug serialization info (shader-slang#769)
* * Make SourceView and SourceFile no longer derive from RefObject * Both have life time now managed by SourceManager * Tidied up a little around the serialization test code - just create the IRModule once * Simplified code around deleting SourceView/File. * Looked into generateIRForTranslationUnit - seems reasonable to just call it once, because it has side effects.
1 parent eb33144 commit dbf5f41

7 files changed

+59
-53
lines changed

source/slang/compiler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ namespace Slang
183183
// The source file(s) that will be compiled to form this translation unit
184184
//
185185
// Usually, for HLSL or GLSL there will be only one file.
186-
List<RefPtr<SourceFile> > sourceFiles;
186+
List<SourceFile*> sourceFiles;
187187

188188
// The entry points associated with this translation unit
189189
List<RefPtr<EntryPointRequest> > entryPoints;

source/slang/ir-serialize.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ static int _calcFixSourceLoc(const IRSerialData::DebugSourceInfo& info, SourceVi
18001800
pathInfo.type = PathInfo::Type::FoundPath;
18011801
pathInfo.foundPath = debugStringSlices[UInt(srcSourceInfo.m_pathIndex)];
18021802

1803-
RefPtr<SourceFile> sourceFile = sourceManager->createSourceFileWithSize(pathInfo, srcSourceInfo.m_endSourceLoc - srcSourceInfo.m_startSourceLoc);
1803+
SourceFile* sourceFile = sourceManager->createSourceFileWithSize(pathInfo, srcSourceInfo.m_endSourceLoc - srcSourceInfo.m_startSourceLoc);
18041804
SourceView* sourceView = sourceManager->createSourceView(sourceFile);
18051805

18061806
// We need to accumulate all line numbers, for this source file, both adjusted and unadjusted

source/slang/ir-serialize.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ struct IRSerialWriter
473473

474474
SourceLoc::RawValue m_baseSourceLoc; ///< The base source location
475475

476-
RefPtr<SourceFile> m_sourceFile; ///< The source file
476+
SourceFile* m_sourceFile; ///< The source file
477477
List<uint8_t> m_lineIndexUsed; ///< Has 1 if the line is used
478478
List<uint32_t> m_usedLineIndices; ///< Holds the lines that have been hit
479479

source/slang/preprocessor.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,7 @@ static Token AdvanceToken(Preprocessor* preprocessor)
842842
// We create a dummy file to represent the token-paste operation
843843
PathInfo pathInfo = PathInfo::makeTokenPaste();
844844

845-
RefPtr<SourceFile> sourceFile = sourceManager->createSourceFileWithString(pathInfo, sb.ProduceString());
846-
845+
SourceFile* sourceFile = sourceManager->createSourceFileWithString(pathInfo, sb.ProduceString());
847846
SourceView* sourceView = sourceManager->createSourceView(sourceFile);
848847

849848
Lexer lexer;
@@ -1634,7 +1633,7 @@ static void HandleIncludeDirective(PreprocessorDirectiveContext* context)
16341633
auto sourceManager = context->preprocessor->getCompileRequest()->getSourceManager();
16351634

16361635
// See if this an already loaded source file
1637-
RefPtr<SourceFile> sourceFile = sourceManager->findSourceFileRecursively(filePathInfo.canonicalPath);
1636+
SourceFile* sourceFile = sourceManager->findSourceFileRecursively(filePathInfo.canonicalPath);
16381637
// If not create a new one, and add to the list of known source files
16391638
if (!sourceFile)
16401639
{
@@ -2268,8 +2267,8 @@ static void DefineMacro(
22682267

22692268
auto sourceManager = preprocessor->translationUnit->compileRequest->getSourceManager();
22702269

2271-
RefPtr<SourceFile> keyFile = sourceManager->createSourceFileWithString(pathInfo, key);
2272-
RefPtr<SourceFile> valueFile = sourceManager->createSourceFileWithString(pathInfo, value);
2270+
SourceFile* keyFile = sourceManager->createSourceFileWithString(pathInfo, key);
2271+
SourceFile* valueFile = sourceManager->createSourceFileWithString(pathInfo, value);
22732272

22742273
SourceView* keyView = sourceManager->createSourceView(keyFile);
22752274
SourceView* valueView = sourceManager->createSourceView(valueFile);

source/slang/slang.cpp

+15-28
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ RefPtr<Expr> CompileRequest::parseTypeString(TranslationUnitRequest * translatio
420420
SourceManager localSourceManager;
421421
localSourceManager.initialize(sourceManager);
422422

423-
Slang::RefPtr<Slang::SourceFile> srcFile(localSourceManager.createSourceFileWithString(PathInfo::makeTypeParse(), typeStr));
423+
Slang::SourceFile* srcFile = localSourceManager.createSourceFileWithString(PathInfo::makeTypeParse(), typeStr);
424424

425425
// We'll use a temporary diagnostic sink
426426
DiagnosticSink sink;
@@ -561,17 +561,15 @@ void CompileRequest::generateIR()
561561
// in isolation.
562562
for( auto& translationUnit : translationUnits )
563563
{
564-
// TODO JS:
565-
// This is a bit of HACK. Apparently if we call generateIRForTranslationUnit(translationUnit) twice
566-
// we get a different result (!).
567-
// So here, we only create once even if we run verification.
568-
RefPtr<IRModule> irModule;
564+
// We want to only run generateIRForTranslationUnit once here. This is for two side effects:
565+
// * it can dump ir
566+
// * it can generate diagnostics
567+
568+
/// Generate IR for translation unit
569+
RefPtr<IRModule> irModule(generateIRForTranslationUnit(translationUnit));
569570

570571
if (verifyDebugSerialization)
571572
{
572-
/// Generate IR for translation unit
573-
irModule = generateIRForTranslationUnit(translationUnit);
574-
575573
// Verify debug information
576574
if (SLANG_FAILED(IRSerialUtil::verifySerialize(irModule, mSession, sourceManager, IRSerialBinary::CompressionType::None, IRSerialWriter::OptionFlag::DebugInfo)))
577575
{
@@ -583,16 +581,11 @@ void CompileRequest::generateIR()
583581
{
584582
IRSerialData serialData;
585583
{
586-
/// Generate IR for translation unit
587-
if (!irModule)
588-
{
589-
irModule = generateIRForTranslationUnit(translationUnit);
590-
}
591-
592584
// Write IR out to serialData - copying over SourceLoc information directly
593585
IRSerialWriter writer;
594586
writer.write(irModule, sourceManager, IRSerialWriter::OptionFlag::RawSourceLocation, &serialData);
595587

588+
// Destroy irModule such that memory can be used for newly constructed read irReadModule
596589
irModule = nullptr;
597590
}
598591
RefPtr<IRModule> irReadModule;
@@ -602,18 +595,12 @@ void CompileRequest::generateIR()
602595
reader.read(serialData, mSession, nullptr, irReadModule);
603596
}
604597

605-
// Use the serialized irModule
606-
translationUnit->irModule = irReadModule;
598+
// Set irModule to the read module
599+
irModule = irReadModule;
607600
}
608-
else
609-
{
610-
if (!irModule)
611-
{
612-
irModule = generateIRForTranslationUnit(translationUnit);
613-
}
614601

615-
translationUnit->irModule = irModule;
616-
}
602+
// Set the module on the translation unit
603+
translationUnit->irModule = irModule;
617604
}
618605
}
619606

@@ -779,7 +766,7 @@ void CompileRequest::addTranslationUnitSourceBlob(
779766
ISlangBlob* sourceBlob)
780767
{
781768
PathInfo pathInfo = PathInfo::makePath(path);
782-
RefPtr<SourceFile> sourceFile = getSourceManager()->createSourceFileWithBlob(pathInfo, sourceBlob);
769+
SourceFile* sourceFile = getSourceManager()->createSourceFileWithBlob(pathInfo, sourceBlob);
783770

784771
addTranslationUnitSourceFile(translationUnitIndex, sourceFile);
785772
}
@@ -790,7 +777,7 @@ void CompileRequest::addTranslationUnitSourceString(
790777
String const& source)
791778
{
792779
PathInfo pathInfo = PathInfo::makePath(path);
793-
RefPtr<SourceFile> sourceFile = getSourceManager()->createSourceFileWithString(pathInfo, source);
780+
SourceFile* sourceFile = getSourceManager()->createSourceFileWithString(pathInfo, source);
794781

795782
addTranslationUnitSourceFile(translationUnitIndex, sourceFile);
796783
}
@@ -915,7 +902,7 @@ RefPtr<ModuleDecl> CompileRequest::loadModule(
915902
translationUnit->compileFlags = 0;
916903

917904
// Create with the 'friendly' name
918-
RefPtr<SourceFile> sourceFile = getSourceManager()->createSourceFileWithBlob(filePathInfo, sourceBlob);
905+
SourceFile* sourceFile = getSourceManager()->createSourceFileWithBlob(filePathInfo, sourceBlob);
919906

920907
translationUnit->sourceFiles.Add(sourceFile);
921908

source/slang/source-loc.cpp

+23-7
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ void SourceManager::initialize(
332332
m_nextLoc = m_startLoc;
333333
}
334334

335+
SourceManager::~SourceManager()
336+
{
337+
for (auto item : m_sourceViews)
338+
{
339+
delete item;
340+
}
341+
342+
for (auto item : m_sourceFiles)
343+
{
344+
delete item;
345+
}
346+
}
347+
335348
UnownedStringSlice SourceManager::allocateStringSlice(const UnownedStringSlice& slice)
336349
{
337350
const UInt numChars = slice.size();
@@ -359,22 +372,25 @@ SourceRange SourceManager::allocateSourceRange(UInt size)
359372
return SourceRange(beginLoc, endLoc);
360373
}
361374

362-
RefPtr<SourceFile> SourceManager::createSourceFileWithSize(const PathInfo& pathInfo, size_t contentSize)
375+
SourceFile* SourceManager::createSourceFileWithSize(const PathInfo& pathInfo, size_t contentSize)
363376
{
364377
SourceFile* sourceFile = new SourceFile(pathInfo, contentSize);
378+
m_sourceFiles.Add(sourceFile);
365379
return sourceFile;
366380
}
367381

368-
RefPtr<SourceFile> SourceManager::createSourceFileWithString(const PathInfo& pathInfo, const String& contents)
382+
SourceFile* SourceManager::createSourceFileWithString(const PathInfo& pathInfo, const String& contents)
369383
{
370384
SourceFile* sourceFile = new SourceFile(pathInfo, contents.Length());
385+
m_sourceFiles.Add(sourceFile);
371386
sourceFile->setContents(contents);
372387
return sourceFile;
373388
}
374389

375-
RefPtr<SourceFile> SourceManager::createSourceFileWithBlob(const PathInfo& pathInfo, ISlangBlob* blob)
390+
SourceFile* SourceManager::createSourceFileWithBlob(const PathInfo& pathInfo, ISlangBlob* blob)
376391
{
377-
RefPtr<SourceFile> sourceFile(new SourceFile(pathInfo, blob->getBufferSize()));
392+
SourceFile* sourceFile = new SourceFile(pathInfo, blob->getBufferSize());
393+
m_sourceFiles.Add(sourceFile);
378394
sourceFile->setContents(blob);
379395
return sourceFile;
380396
}
@@ -465,8 +481,8 @@ SourceView* SourceManager::findSourceViewRecursively(SourceLoc loc) const
465481

466482
SourceFile* SourceManager::findSourceFile(const String& canonicalPath) const
467483
{
468-
RefPtr<SourceFile>* filePtr = m_sourceFiles.TryGetValue(canonicalPath);
469-
return (filePtr) ? filePtr->Ptr() : nullptr;
484+
SourceFile*const* filePtr = m_sourceFileMap.TryGetValue(canonicalPath);
485+
return (filePtr) ? *filePtr : nullptr;
470486
}
471487

472488
SourceFile* SourceManager::findSourceFileRecursively(const String& canonicalPath) const
@@ -487,7 +503,7 @@ SourceFile* SourceManager::findSourceFileRecursively(const String& canonicalPath
487503
void SourceManager::addSourceFile(const String& canonicalPath, SourceFile* sourceFile)
488504
{
489505
SLANG_ASSERT(!findSourceFileRecursively(canonicalPath));
490-
m_sourceFiles.Add(canonicalPath, sourceFile);
506+
m_sourceFileMap.Add(canonicalPath, sourceFile);
491507
}
492508

493509
HumaneSourceLoc SourceManager::getHumaneLoc(SourceLoc loc, SourceLocType type)

source/slang/source-loc.h

+14-10
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ struct SourceRange
141141

142142
// A logical or physical storage object for a range of input code
143143
// that has logically contiguous source locations.
144-
class SourceFile : public RefObject
144+
class SourceFile
145145
{
146146
public:
147147

@@ -217,7 +217,7 @@ struct SourceManager;
217217
It is distinct from a SourceFile - because a SourceFile may be included multiple times, with different interpretations (depending
218218
on #defines for example).
219219
*/
220-
class SourceView: public RefObject
220+
class SourceView
221221
{
222222
public:
223223

@@ -285,7 +285,7 @@ class SourceView: public RefObject
285285

286286
SourceManager* m_sourceManager; /// Get the manager this belongs to
287287
SourceRange m_range; ///< The range that this SourceView applies to
288-
RefPtr<SourceFile> m_sourceFile; ///< The source file can hold the line breaks
288+
SourceFile* m_sourceFile; ///< The source file. Can hold the line breaks
289289
List<Entry> m_entries; ///< An array entries describing how we should interpret a range, starting from the start location.
290290
};
291291

@@ -298,9 +298,9 @@ struct SourceManager
298298
SourceRange allocateSourceRange(UInt size);
299299

300300
/// Create a SourceFile defined with the specified path, and content held within a blob
301-
RefPtr<SourceFile> createSourceFileWithSize(const PathInfo& pathInfo, size_t contentSize);
302-
RefPtr<SourceFile> createSourceFileWithString(const PathInfo& pathInfo, const String& contents);
303-
RefPtr<SourceFile> createSourceFileWithBlob(const PathInfo& pathInfo, ISlangBlob* blob);
301+
SourceFile* createSourceFileWithSize(const PathInfo& pathInfo, size_t contentSize);
302+
SourceFile* createSourceFileWithString(const PathInfo& pathInfo, const String& contents);
303+
SourceFile* createSourceFileWithBlob(const PathInfo& pathInfo, ISlangBlob* blob);
304304

305305
/// Get the humane source location
306306
HumaneSourceLoc getHumaneLoc(SourceLoc loc, SourceLocType type = SourceLocType::Nominal);
@@ -348,6 +348,7 @@ struct SourceManager
348348
SourceManager() :
349349
m_memoryArena(2048)
350350
{}
351+
~SourceManager();
351352

352353
protected:
353354

@@ -362,16 +363,19 @@ struct SourceManager
362363
// The location to be used by the next source file to be loaded
363364
SourceLoc m_nextLoc;
364365

365-
// All of the SourceViews. These are held in increasing order of range, so can find by doing a binary chop.
366-
List<RefPtr<SourceView> > m_sourceViews;
366+
// All of the SourceViews constructed on this SourceManager. These are held in increasing order of range, so can find by doing a binary chop.
367+
List<SourceView*> m_sourceViews;
368+
// All of the SourceFiles constructed on this SourceManager. This owns the SourceFile.
369+
List<SourceFile*> m_sourceFiles;
370+
367371
StringSlicePool m_slicePool;
368372

369373
// Memory arena that can be used for holding data to held in scope as long as the Source is
370-
// Can be used for storing the decoded contents of Token.Content for exampel
374+
// Can be used for storing the decoded contents of Token. Content for example.
371375
MemoryArena m_memoryArena;
372376

373377
// Maps canonical paths to source files
374-
Dictionary<String, RefPtr<SourceFile> > m_sourceFiles;
378+
Dictionary<String, SourceFile*> m_sourceFileMap;
375379
};
376380

377381
} // namespace Slang

0 commit comments

Comments
 (0)