diff options
| author | Yong He <yonghe@outlook.com> | 2023-08-15 20:55:21 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-15 20:55:21 -0700 |
| commit | c16f711d83df90f6826a65d61fc56cdbb932c92c (patch) | |
| tree | 3680ff05b91aa4521fd5c5e9caa9de171cbac558 /source/slang/slang-lower-to-ir.cpp | |
| parent | 0c366bc0a4332ee14d08f2555396a18cb64229fa (diff) | |
SPIRV: debug source and debug line. (#3109)
Diffstat (limited to 'source/slang/slang-lower-to-ir.cpp')
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 58 |
1 files changed, 53 insertions, 5 deletions
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index f89f92711..74ece5bc9 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -494,6 +494,8 @@ struct SharedIRGenContext Dictionary<Stmt*, IRBlock*> breakLabels; Dictionary<Stmt*, IRBlock*> continueLabels; + Dictionary<SourceFile*, IRInst*> mapSourceFileToDebugSourceInst; + void setGlobalValue(Decl* decl, LoweredValInfo value) { globalEnv.mapDeclToValue[decl] = value; @@ -503,17 +505,20 @@ struct SharedIRGenContext Session* session, DiagnosticSink* sink, bool obfuscateCode, - ModuleDecl* mainModuleDecl = nullptr) + ModuleDecl* mainModuleDecl, + Linkage* linkage) : m_session(session) , m_sink(sink) , m_obfuscateCode(obfuscateCode) , m_mainModuleDecl(mainModuleDecl) + , m_linkage(linkage) {} Session* m_session = nullptr; DiagnosticSink* m_sink = nullptr; bool m_obfuscateCode = false; ModuleDecl* m_mainModuleDecl = nullptr; + Linkage* m_linkage = nullptr; // List of all string literals used in user code, regardless // of how they were used (i.e., whether or not they were hashed). @@ -556,6 +561,8 @@ struct IRGenContext // The IR witness value to use for `ThisType` IRInst* thisTypeWitness = nullptr; + bool includeDebugInfo = false; + explicit IRGenContext(SharedIRGenContext* inShared, ASTBuilder* inAstBuilder) : shared(inShared) , astBuilder(inAstBuilder) @@ -588,6 +595,11 @@ struct IRGenContext return shared->m_mainModuleDecl; } + Linkage* getLinkage() + { + return shared->m_linkage; + } + LoweredValInfo* findLoweredDecl(Decl* decl) { IRGenEnv* envToFindIn = env; @@ -5712,6 +5724,24 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor> } }; +void maybeEmitDebugLine(IRGenContext* context, Stmt* stmt) +{ + if (!context->includeDebugInfo) + return; + if (as<EmptyStmt>(stmt)) + return; + auto sourceView = context->getLinkage()->getSourceManager()->findSourceView(stmt->loc); + if (!sourceView) + return; + auto source = sourceView->getSourceFile(); + IRInst* debugSourceInst = nullptr; + if (context->shared->mapSourceFileToDebugSourceInst.tryGetValue(source, debugSourceInst)) + { + auto humaneLoc = context->getLinkage()->getSourceManager()->getHumaneLoc(stmt->loc, SourceLocType::Emit); + context->irBuilder->emitDebugLine(debugSourceInst, humaneLoc.line, humaneLoc.line, humaneLoc.column, humaneLoc.column + 1); + } +} + void lowerStmt( IRGenContext* context, Stmt* stmt) @@ -5723,6 +5753,7 @@ void lowerStmt( try { + maybeEmitDebugLine(context, stmt); visitor.dispatch(stmt); } // Don't emit any context message for an explicit `AbortCompilationException` @@ -9542,7 +9573,8 @@ RefPtr<IRModule> generateIRForTranslationUnit( session, translationUnit->compileRequest->getSink(), translationUnit->compileRequest->getLinkage()->m_obfuscateCode, - translationUnit->getModuleDecl()); + translationUnit->getModuleDecl(), + translationUnit->compileRequest->getLinkage()); SharedIRGenContext* sharedContext = &sharedContextStorage; IRGenContext contextStorage(sharedContext, astBuilder); @@ -9554,10 +9586,22 @@ RefPtr<IRModule> generateIRForTranslationUnit( IRBuilder* builder = &builderStorage; context->irBuilder = builder; + context->includeDebugInfo = compileRequest->getLinkage()->debugInfoLevel != DebugInfoLevel::None; // We need to emit IR for all public/exported symbols // in the translation unit. // + // If debug info is enabled, we emit the DebugSource insts for each source file into IR. + if (context->includeDebugInfo) + { + builder->setInsertInto(module->getModuleInst()); + for (auto source : translationUnit->getSourceFiles()) + { + auto debugSource = builder->emitDebugSource(source->getPathInfo().getMostUniqueIdentity().getUnownedSlice(), source->getContent()); + context->shared->mapSourceFileToDebugSourceInst.add(source, debugSource); + } + } + // For now, we will assume that *all* global-scope declarations // represent public/exported symbols. @@ -9781,7 +9825,9 @@ struct SpecializedComponentTypeIRGenContext : ComponentTypeVisitor SharedIRGenContext sharedContextStorage( session, sink, - linkage->m_obfuscateCode + linkage->m_obfuscateCode, + nullptr, + linkage ); SharedIRGenContext* sharedContext = &sharedContextStorage; @@ -9918,7 +9964,7 @@ struct TypeConformanceIRGenContext linkage = typeConformance->getLinkage(); session = linkage->getSessionImpl(); - SharedIRGenContext sharedContextStorage(session, sink, linkage->m_obfuscateCode); + SharedIRGenContext sharedContextStorage(session, sink, linkage->m_obfuscateCode, nullptr, linkage); SharedIRGenContext* sharedContext = &sharedContextStorage; IRGenContext contextStorage(sharedContext, linkage->getASTBuilder()); @@ -10268,7 +10314,9 @@ RefPtr<IRModule> TargetProgram::createIRModuleForLayout(DiagnosticSink* sink) SharedIRGenContext sharedContextStorage( session, sink, - linkage->m_obfuscateCode); + linkage->m_obfuscateCode, + nullptr, + linkage); auto sharedContext = &sharedContextStorage; ASTBuilder* astBuilder = linkage->getASTBuilder(); |
