Skip to content

Prototype: Replace DILocations with function-local source locations #133949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

SLTozer
Copy link
Contributor

@SLTozer SLTozer commented Apr 1, 2025

See the README, split-debugloc-model-readme.md, for more details. This is not meant to be a "working" prototype, just a demonstration of the code changes required to perform this rewrite; despite the size of the PR, many of these changes are either boilerplate, temporary scaffolding, or rough edges that will be smoothed over to a small or non-existent diff down the line.

@llvmbot
Copy link
Member

llvmbot commented Apr 15, 2025

@llvm/pr-subscribers-debuginfo

Author: Stephen Tozer (SLTozer)

Changes

See the README, split-debugloc-model-readme.md, for more details. This is not meant to be a "working" prototype, just a demonstration of the code changes required to perform this rewrite; despite the size of the PR, many of these changes are either boilerplate, temporary scaffolding, or rough edges that will be smoothed over to a small or non-existent diff down the line.


Patch is 574.40 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/133949.diff

211 Files Affected:

  • (modified) clang/lib/CodeGen/BackendUtil.cpp (+1)
  • (modified) clang/lib/CodeGen/CGBuiltin.cpp (+1-1)
  • (modified) clang/lib/CodeGen/CGClass.cpp (+1-1)
  • (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+35-30)
  • (modified) clang/lib/CodeGen/CGDebugInfo.h (+15-6)
  • (modified) clang/lib/CodeGen/CGLoopInfo.cpp (+5-4)
  • (modified) clang/lib/CodeGen/CGLoopInfo.h (+1)
  • (modified) llvm/include/llvm/Analysis/InlineAdvisor.h (+6-5)
  • (modified) llvm/include/llvm/Analysis/LoopInfo.h (+1)
  • (modified) llvm/include/llvm/Analysis/ReplayInlineAdvisor.h (+2-1)
  • (modified) llvm/include/llvm/AsmParser/LLParser.h (+3)
  • (modified) llvm/include/llvm/CodeGen/BasicTTIImpl.h (+1-1)
  • (modified) llvm/include/llvm/CodeGen/DbgEntityHistoryCalculator.h (+2-3)
  • (modified) llvm/include/llvm/CodeGen/DebugHandlerBase.h (+1-1)
  • (modified) llvm/include/llvm/CodeGen/DroppedVariableStatsMIR.h (+2-2)
  • (modified) llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h (+1-2)
  • (modified) llvm/include/llvm/CodeGen/GlobalISel/Utils.h (+1)
  • (modified) llvm/include/llvm/CodeGen/LexicalScopes.h (+38-17)
  • (modified) llvm/include/llvm/CodeGen/MachineBasicBlock.h (+4)
  • (modified) llvm/include/llvm/CodeGen/MachineFunction.h (+5-5)
  • (modified) llvm/include/llvm/CodeGen/MachineInstr.h (+2-1)
  • (modified) llvm/include/llvm/CodeGen/MachineInstrBuilder.h (+5-8)
  • (modified) llvm/include/llvm/CodeGen/MachineLoopInfo.h (+1)
  • (modified) llvm/include/llvm/CodeGen/MachineOptimizationRemarkEmitter.h (+1-1)
  • (modified) llvm/include/llvm/CodeGen/SelectionDAG.h (+7-7)
  • (modified) llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h (+1-1)
  • (modified) llvm/include/llvm/IR/DIBuilder.h (+6-6)
  • (modified) llvm/include/llvm/IR/DebugInfo.h (+10-11)
  • (modified) llvm/include/llvm/IR/DebugInfoMetadata.h (+574-9)
  • (modified) llvm/include/llvm/IR/DebugLoc.h (+124-77)
  • (modified) llvm/include/llvm/IR/DebugProgramInstruction.h (+15-12)
  • (modified) llvm/include/llvm/IR/DiagnosticInfo.h (+7-2)
  • (modified) llvm/include/llvm/IR/DroppedVariableStats.h (+10-9)
  • (modified) llvm/include/llvm/IR/DroppedVariableStatsIR.h (+2-2)
  • (modified) llvm/include/llvm/IR/IRBuilder.h (+11-4)
  • (modified) llvm/include/llvm/IR/Instruction.h (+4-3)
  • (modified) llvm/include/llvm/IR/LLVMContext.h (+6)
  • (modified) llvm/include/llvm/IR/Metadata.h (+1)
  • (modified) llvm/include/llvm/ProfileData/SampleProf.h (+4-4)
  • (modified) llvm/include/llvm/Transforms/IPO/SampleContextTracker.h (+5-6)
  • (modified) llvm/include/llvm/Transforms/Utils/Instrumentation.h (+2-2)
  • (modified) llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h (+11-12)
  • (modified) llvm/lib/Analysis/InlineAdvisor.cpp (+20-18)
  • (modified) llvm/lib/Analysis/KernelInfo.cpp (+3-3)
  • (modified) llvm/lib/Analysis/LoopAccessAnalysis.cpp (+5-4)
  • (modified) llvm/lib/Analysis/LoopInfo.cpp (+8-5)
  • (modified) llvm/lib/Analysis/MLInlineAdvisor.cpp (+3-1)
  • (modified) llvm/lib/Analysis/ReplayInlineAdvisor.cpp (+2-1)
  • (modified) llvm/lib/AsmParser/LLParser.cpp (+84-6)
  • (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+27-11)
  • (modified) llvm/lib/Bitcode/Writer/BitcodeWriter.cpp (+21-6)
  • (modified) llvm/lib/Bitcode/Writer/ValueEnumerator.cpp (+16-3)
  • (modified) llvm/lib/Bitcode/Writer/ValueEnumerator.h (+2)
  • (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+2-3)
  • (modified) llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp (+20-19)
  • (modified) llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h (+12-6)
  • (modified) llvm/lib/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp (+7-6)
  • (modified) llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp (+15-15)
  • (modified) llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h (+6-6)
  • (modified) llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp (+6-4)
  • (modified) llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.h (+2-1)
  • (modified) llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp (+22-16)
  • (modified) llvm/lib/CodeGen/BranchFolding.cpp (+4-1)
  • (modified) llvm/lib/CodeGen/DroppedVariableStatsMIR.cpp (+4-4)
  • (modified) llvm/lib/CodeGen/DwarfEHPrepare.cpp (+3-2)
  • (modified) llvm/lib/CodeGen/EarlyIfConversion.cpp (+4-3)
  • (modified) llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp (+4-3)
  • (modified) llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp (+8-8)
  • (modified) llvm/lib/CodeGen/GlobalISel/LoadStoreOpt.cpp (+5-3)
  • (modified) llvm/lib/CodeGen/GlobalISel/Localizer.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/GlobalISel/LostDebugLocObserver.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp (+2-2)
  • (modified) llvm/lib/CodeGen/GlobalISel/Utils.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/HardwareLoops.cpp (+2-1)
  • (modified) llvm/lib/CodeGen/LexicalScopes.cpp (+19-12)
  • (modified) llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp (+22-20)
  • (modified) llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h (+13-13)
  • (modified) llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp (+11-11)
  • (modified) llvm/lib/CodeGen/LiveDebugVariables.cpp (+24-24)
  • (modified) llvm/lib/CodeGen/MIRFSDiscriminator.cpp (+4-4)
  • (modified) llvm/lib/CodeGen/MIRParser/MIParser.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/MIRParser/MIRParser.cpp (+2-2)
  • (modified) llvm/lib/CodeGen/MIRPrinter.cpp (+6-6)
  • (modified) llvm/lib/CodeGen/MIRSampleProfile.cpp (+4-3)
  • (modified) llvm/lib/CodeGen/MachineBasicBlock.cpp (+12-2)
  • (modified) llvm/lib/CodeGen/MachineCheckDebugify.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/MachineDebugify.cpp (+9-8)
  • (modified) llvm/lib/CodeGen/MachineInstr.cpp (+26-8)
  • (modified) llvm/lib/CodeGen/MachineLoopInfo.cpp (+17)
  • (modified) llvm/lib/CodeGen/MachineOutliner.cpp (+4-4)
  • (modified) llvm/lib/CodeGen/MachinePipeliner.cpp (+13-13)
  • (modified) llvm/lib/CodeGen/MachineSink.cpp (+3-2)
  • (modified) llvm/lib/CodeGen/PHIElimination.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/PseudoProbeInserter.cpp (+3-3)
  • (modified) llvm/lib/CodeGen/RegAllocBase.cpp (+2-2)
  • (modified) llvm/lib/CodeGen/RegAllocFast.cpp (+2-2)
  • (modified) llvm/lib/CodeGen/RegAllocGreedy.cpp (+3-3)
  • (modified) llvm/lib/CodeGen/RemoveRedundantDebugValues.cpp (+2-2)
  • (modified) llvm/lib/CodeGen/SafeStack.cpp (+3-1)
  • (modified) llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp (+2-2)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SDNodeDbgValue.h (+1)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (+9-9)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (+24-24)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h (+13-13)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp (+6-6)
  • (modified) llvm/lib/CodeGen/ShrinkWrap.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/StackProtector.cpp (+1-2)
  • (modified) llvm/lib/CodeGen/WinEHPrepare.cpp (+1)
  • (modified) llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp (+19-19)
  • (modified) llvm/lib/IR/AsmWriter.cpp (+160-6)
  • (modified) llvm/lib/IR/Core.cpp (+16-15)
  • (modified) llvm/lib/IR/DIBuilder.cpp (+17-13)
  • (modified) llvm/lib/IR/DebugInfo.cpp (+58-181)
  • (modified) llvm/lib/IR/DebugInfoMetadata.cpp (+760-3)
  • (modified) llvm/lib/IR/DebugLoc.cpp (+54-108)
  • (modified) llvm/lib/IR/DebugProgramInstruction.cpp (+27-26)
  • (modified) llvm/lib/IR/DiagnosticInfo.cpp (+41-12)
  • (modified) llvm/lib/IR/DroppedVariableStats.cpp (+9-9)
  • (modified) llvm/lib/IR/DroppedVariableStatsIR.cpp (+4-4)
  • (modified) llvm/lib/IR/IRBuilder.cpp (+3-10)
  • (modified) llvm/lib/IR/IRPrintingPasses.cpp (+1)
  • (modified) llvm/lib/IR/LLVMContext.cpp (+9)
  • (modified) llvm/lib/IR/Metadata.cpp (+3-17)
  • (modified) llvm/lib/IR/PseudoProbe.cpp (+12-12)
  • (modified) llvm/lib/IR/Verifier.cpp (+74-74)
  • (modified) llvm/lib/IRPrinter/IRPrintingPasses.cpp (+1)
  • (modified) llvm/lib/ProfileData/SampleProf.cpp (+9-10)
  • (modified) llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp (+3-3)
  • (modified) llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp (+3-2)
  • (modified) llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp (+1-1)
  • (modified) llvm/lib/Target/ARM/ARMISelLowering.cpp (+5-5)
  • (modified) llvm/lib/Target/ARM/ARMInstructionSelector.cpp (+2-2)
  • (modified) llvm/lib/Target/ARM/ARMLoadStoreOptimizer.cpp (+1-2)
  • (modified) llvm/lib/Target/X86/X86DiscriminateMemOps.cpp (+5-6)
  • (modified) llvm/lib/Target/X86/X86ISelLoweringCall.cpp (+1-1)
  • (modified) llvm/lib/Target/X86/X86InsertPrefetch.cpp (+1-1)
  • (modified) llvm/lib/Transforms/Coroutines/CoroFrame.cpp (+6-7)
  • (modified) llvm/lib/Transforms/Coroutines/CoroSplit.cpp (+3-3)
  • (modified) llvm/lib/Transforms/HipStdPar/HipStdPar.cpp (+2-2)
  • (modified) llvm/lib/Transforms/IPO/AlwaysInliner.cpp (+2-1)
  • (modified) llvm/lib/Transforms/IPO/IROutliner.cpp (+8-12)
  • (modified) llvm/lib/Transforms/IPO/MergeFunctions.cpp (+4-6)
  • (modified) llvm/lib/Transforms/IPO/OpenMPOpt.cpp (+1-1)
  • (modified) llvm/lib/Transforms/IPO/PartialInlining.cpp (+2-1)
  • (modified) llvm/lib/Transforms/IPO/SampleContextTracker.cpp (+28-27)
  • (modified) llvm/lib/Transforms/IPO/SampleProfile.cpp (+13-13)
  • (modified) llvm/lib/Transforms/IPO/SampleProfileMatcher.cpp (+8-8)
  • (modified) llvm/lib/Transforms/IPO/SampleProfileProbe.cpp (+7-8)
  • (modified) llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp (+3-3)
  • (modified) llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp (+4-2)
  • (modified) llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp (+2-1)
  • (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+4-13)
  • (modified) llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp (+9-7)
  • (modified) llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp (+4-2)
  • (modified) llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp (+12-7)
  • (modified) llvm/lib/Transforms/Instrumentation/MemProfiler.cpp (+5-6)
  • (modified) llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp (+1-1)
  • (modified) llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp (+4-2)
  • (modified) llvm/lib/Transforms/Scalar/ADCE.cpp (+15-15)
  • (modified) llvm/lib/Transforms/Scalar/AnnotationRemarks.cpp (+2-2)
  • (modified) llvm/lib/Transforms/Scalar/ConstantHoisting.cpp (+1-1)
  • (modified) llvm/lib/Transforms/Scalar/JumpThreading.cpp (+1)
  • (modified) llvm/lib/Transforms/Scalar/LICM.cpp (+4-3)
  • (modified) llvm/lib/Transforms/Scalar/LoopDeletion.cpp (+2-2)
  • (modified) llvm/lib/Transforms/Scalar/LoopDistribute.cpp (+4-4)
  • (modified) llvm/lib/Transforms/Scalar/LoopFlatten.cpp (+1-1)
  • (modified) llvm/lib/Transforms/Scalar/LoopFuse.cpp (+2-2)
  • (modified) llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp (+3-3)
  • (modified) llvm/lib/Transforms/Scalar/LoopInterchange.cpp (+16-16)
  • (modified) llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp (+5-5)
  • (modified) llvm/lib/Transforms/Scalar/LoopVersioningLICM.cpp (+5-5)
  • (modified) llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp (+8-9)
  • (modified) llvm/lib/Transforms/Scalar/SROA.cpp (+4-4)
  • (modified) llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp (+3-2)
  • (modified) llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp (+5-5)
  • (modified) llvm/lib/Transforms/Utils/AddDiscriminators.cpp (+5-4)
  • (modified) llvm/lib/Transforms/Utils/BasicBlockUtils.cpp (+6-6)
  • (modified) llvm/lib/Transforms/Utils/CloneFunction.cpp (+16)
  • (modified) llvm/lib/Transforms/Utils/CodeExtractor.cpp (+21-18)
  • (modified) llvm/lib/Transforms/Utils/Debugify.cpp (+22-16)
  • (modified) llvm/lib/Transforms/Utils/EntryExitInstrumenter.cpp (+5-3)
  • (modified) llvm/lib/Transforms/Utils/InlineFunction.cpp (+43-36)
  • (modified) llvm/lib/Transforms/Utils/Local.cpp (+7-5)
  • (modified) llvm/lib/Transforms/Utils/LoopUnroll.cpp (+3-3)
  • (modified) llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp (+3-3)
  • (modified) llvm/lib/Transforms/Utils/LoopUtils.cpp (+6-2)
  • (modified) llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp (+2-2)
  • (modified) llvm/lib/Transforms/Utils/SimplifyCFG.cpp (+8-5)
  • (modified) llvm/lib/Transforms/Utils/ValueMapper.cpp (+2-4)
  • (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp (+3-3)
  • (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+19-19)
  • (modified) llvm/lib/Transforms/Vectorize/VPlan.cpp (+6-3)
  • (modified) llvm/lib/Transforms/Vectorize/VPlanHelpers.h (+4-1)
  • (modified) llvm/test/Transforms/ADCE/preserve-memoryssa-if-only-remove-debug.ll (+1-1)
  • (modified) llvm/test/Transforms/AddDiscriminators/dbg-declare-discriminator.ll (+3-2)
  • (modified) llvm/test/Transforms/Inline/debug-invoke.ll (+8-6)
  • (modified) llvm/test/Transforms/Inline/delete-function-with-metadata-use.ll (+1-2)
  • (modified) llvm/test/Transforms/Inline/ignore-debug-info.ll (+6-5)
  • (modified) llvm/test/Transforms/Inline/local-as-metadata-undominated-use.ll (+8)
  • (modified) llvm/test/Transforms/LICM/opt-remarks.ll (+5-3)
  • (modified) llvm/test/Transforms/LoopVectorize/X86/reg-usage-debug.ll (+3)
  • (modified) llvm/test/Transforms/SampleProfile/summary.ll (+3-3)
  • (modified) llvm/test/Transforms/SimplifyCFG/X86/sink-common-code.ll (+1-1)
  • (modified) llvm/test/Transforms/SimplifyCFG/tail-merge-noreturn.ll (+5-3)
  • (modified) llvm/tools/llvm-dis/llvm-dis.cpp (+3-3)
  • (modified) llvm/tools/llvm-profgen/ProfileGenerator.h (+2-2)
  • (modified) llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp (+1-5)
  • (modified) llvm/utils/llvm-original-di-preservation.py (+1-1)
  • (added) split-debugloc-model-readme.md (+186)
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 7557cb8408921..e70b02f1a2e2f 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -29,6 +29,7 @@
 #include "llvm/Frontend/Driver/CodeGenOptions.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/DebugInfo.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/ModuleSummaryIndex.h"
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 91ac7c5847b02..d8e9ee516fa9a 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3472,7 +3472,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
     EmitTrapCall(Intrinsic::trap);
     return RValue::get(nullptr);
   case Builtin::BI__builtin_verbose_trap: {
-    llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation();
+    DebugLoc TrapLocation = Builder.getCurrentDebugLocation();
     if (getDebugInfo()) {
       TrapLocation = getDebugInfo()->CreateTrapFailureMessageFor(
           TrapLocation, *E->getArg(0)->tryEvaluateString(getContext()),
diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp
index 98c93b5bb4883..1455f0a020281 100644
--- a/clang/lib/CodeGen/CGClass.cpp
+++ b/clang/lib/CodeGen/CGClass.cpp
@@ -1653,7 +1653,7 @@ namespace {
 
   class DeclAsInlineDebugLocation {
     CGDebugInfo *DI;
-    llvm::MDNode *InlinedAt;
+    llvm::DebugLoc InlinedAt;
     std::optional<ApplyDebugLocation> Location;
 
   public:
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 54025b767dc81..e4aadc664a095 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -42,6 +42,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DataLayout.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Intrinsics.h"
@@ -157,9 +158,12 @@ void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
 
   // Construct a location that has a valid scope, but no line info.
   assert(!DI->LexicalBlockStack.empty());
+  llvm::DIScope *Scope = DI->LexicalBlockStack.back();
+  while (!isa<llvm::DISubprogram>(Scope))
+    Scope = Scope->getScope();
   CGF->Builder.SetCurrentDebugLocation(
-      llvm::DILocation::get(DI->LexicalBlockStack.back()->getContext(), 0, 0,
-                            DI->LexicalBlockStack.back(), DI->getInlinedAt()));
+      llvm::DebugLoc(0, cast<llvm::DISubprogram>(Scope)->getLocScopeIndex(
+        llvm::DILocScopeData(cast<llvm::DILocalScope>(DI->LexicalBlockStack.back()), DI->getInlinedAt()), true)));
 }
 
 ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
@@ -194,8 +198,9 @@ ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
   }
   auto &DI = *CGF.getDebugInfo();
   SavedLocation = DI.getLocation();
+  llvm::DISubprogram *SP = CGF.Builder.GetInsertBlock()->getParent()->getSubprogram();
   assert((DI.getInlinedAt() ==
-          CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
+          CGF.Builder.getCurrentDebugLocation().getInlinedAt(SP)) &&
          "CGDebugInfo and IRBuilder are out of sync");
 
   DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
@@ -537,6 +542,12 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
   return PLoc.isValid() ? PLoc.getColumn() : 0;
 }
 
+llvm::DISrcLocData CGDebugInfo::getDISrcLoc(SourceLocation Loc, bool ForceColumn) {
+  unsigned Line = getLineNumber(Loc);
+  unsigned Column = getColumnNumber(Loc, ForceColumn);
+  return llvm::DISrcLocData(Line, Column);
+}
+
 StringRef CGDebugInfo::getCurrentDirname() {
   if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
     return CGM.getCodeGenOpts().DebugCompilationDir;
@@ -3614,7 +3625,7 @@ llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
   return DBuilder.createTempMacroFile(Parent, Line, FName);
 }
 
-llvm::DILocation *CGDebugInfo::CreateTrapFailureMessageFor(
+llvm::DebugLoc CGDebugInfo::CreateTrapFailureMessageFor(
     llvm::DebugLoc TrapLocation, StringRef Category, StringRef FailureMsg) {
   // Create a debug location from `TrapLocation` that adds an artificial inline
   // frame.
@@ -3626,9 +3637,9 @@ llvm::DILocation *CGDebugInfo::CreateTrapFailureMessageFor(
   FuncName += FailureMsg;
 
   llvm::DISubprogram *TrapSP =
-      createInlinedTrapSubprogram(FuncName, TrapLocation->getFile());
-  return llvm::DILocation::get(CGM.getLLVMContext(), /*Line=*/0, /*Column=*/0,
-                               /*Scope=*/TrapSP, /*InlinedAt=*/TrapLocation);
+      createInlinedTrapSubprogram(FuncName, llvm::DILocRef(CurSP, TrapLocation)->getFile());
+  CurSP->FnLocScopes.push_back(llvm::DILocScopeData(TrapSP, TrapLocation));
+  return llvm::DebugLoc(0, CurSP->FnLocScopes.size() - 1);
 }
 
 static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
@@ -4455,6 +4466,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
       if (SP && SP->isDefinition()) {
         LexicalBlockStack.emplace_back(SP);
         RegionMap[D].reset(SP);
+        CurSP = SP;
         return;
       }
     }
@@ -4531,6 +4543,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
     DeclCache[D->getCanonicalDecl()].reset(SP);
 
   // Push the function onto the lexical block stack.
+  CurSP = SP;
   LexicalBlockStack.emplace_back(SP);
 
   if (HasDecl)
@@ -4652,7 +4665,7 @@ void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
 void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
   assert(CurInlinedAt && "unbalanced inline scope stack");
   EmitFunctionEnd(Builder, nullptr);
-  setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
+  setInlinedAt(CurInlinedAt.getInlinedAt(CurSP));
 }
 
 void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
@@ -4664,8 +4677,7 @@ void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
 
   llvm::MDNode *Scope = LexicalBlockStack.back();
   Builder.SetCurrentDebugLocation(
-      llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(CurLoc),
-                            getColumnNumber(CurLoc), Scope, CurInlinedAt));
+      CurSP->getDebugLoc(getDISrcLoc(CurLoc), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true));
 }
 
 void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
@@ -4696,9 +4708,8 @@ void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
   setLocation(Loc);
 
   // Emit a line table change for the current location inside the new scope.
-  Builder.SetCurrentDebugLocation(llvm::DILocation::get(
-      CGM.getLLVMContext(), getLineNumber(Loc), getColumnNumber(Loc),
-      LexicalBlockStack.back(), CurInlinedAt));
+  Builder.SetCurrentDebugLocation(
+    CurSP->getDebugLoc(getDISrcLoc(CurLoc), llvm::DILocScopeData(cast<llvm::DILocalScope>(LexicalBlockStack.back()), CurInlinedAt), true));
 
   if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
     return;
@@ -4733,8 +4744,10 @@ void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
   }
   FnBeginRegionCount.pop_back();
 
-  if (Fn && Fn->getSubprogram())
+  if (Fn && Fn->getSubprogram()) {
     DBuilder.finalizeSubprogram(Fn->getSubprogram());
+    Fn->getSubprogram()->normalizeDebugLocs(Fn);
+  }
 }
 
 CGDebugInfo::BlockByRefType
@@ -4914,9 +4927,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
 
         // Insert an llvm.dbg.declare into the current block.
         DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
-                               llvm::DILocation::get(CGM.getLLVMContext(), Line,
-                                                     Column, Scope,
-                                                     CurInlinedAt),
+          CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true),
                                Builder.GetInsertBlock());
       }
     }
@@ -4983,8 +4994,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
   }
   // Insert an llvm.dbg.declare into the current block.
   DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
-                         llvm::DILocation::get(CGM.getLLVMContext(), Line,
-                                               Column, Scope, CurInlinedAt),
+    CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true),
                          Builder.GetInsertBlock());
 
   return D;
@@ -5089,8 +5099,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,
 
   // Insert an llvm.dbg.declare into the current block.
   DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
-                         llvm::DILocation::get(CGM.getLLVMContext(), Line,
-                                               Column, Scope, CurInlinedAt),
+    CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true),
                          Builder.GetInsertBlock());
 
   return D;
@@ -5136,8 +5145,7 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {
 
   // Insert an llvm.dbg.label into the current block.
   DBuilder.insertLabel(L,
-                       llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
-                                             Scope, CurInlinedAt),
+                      CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true),
                        Builder.GetInsertBlock()->end());
 }
 
@@ -5212,8 +5220,7 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
       Line, Ty, false, llvm::DINode::FlagZero, Align);
 
   // Insert an llvm.dbg.declare into the current block.
-  auto DL = llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
-                                  LexicalBlockStack.back(), CurInlinedAt);
+  auto DL = CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(LexicalBlockStack.back()), CurInlinedAt), true);
   auto *Expr = DBuilder.createExpression(addr);
   if (InsertPoint)
     DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint->getIterator());
@@ -5399,8 +5406,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
 
   // Insert an llvm.dbg.declare into the current block.
   DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
-                         llvm::DILocation::get(CGM.getLLVMContext(), line,
-                                               column, scope, CurInlinedAt),
+    CurSP->getDebugLoc(llvm::DISrcLocData(line, column), llvm::DILocScopeData(scope, CurInlinedAt), true),
                          Builder.GetInsertBlock());
 }
 
@@ -5851,7 +5857,7 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
       llvm::codegenoptions::DebugLineTablesOnly)
     return;
 
-  llvm::DILocation *DIL = Value->getDebugLoc().get();
+  llvm::DILocRef DIL(*Value);
   if (!DIL)
     return;
 
@@ -6163,8 +6169,7 @@ llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
     return llvm::DebugLoc();
 
   llvm::MDNode *Scope = LexicalBlockStack.back();
-  return llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(Loc),
-                               getColumnNumber(Loc), Scope);
+  return CurSP->getDebugLoc(getDISrcLoc(Loc), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true);
 }
 
 llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const {
diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h
index 9db5113fe5d8e..c3c864e42b7e7 100644
--- a/clang/lib/CodeGen/CGDebugInfo.h
+++ b/clang/lib/CodeGen/CGDebugInfo.h
@@ -25,6 +25,7 @@
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseSet.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/DIBuilder.h"
 #include "llvm/IR/DebugInfo.h"
 #include "llvm/IR/ValueHandle.h"
@@ -66,7 +67,10 @@ class CGDebugInfo {
   ModuleMap *ClangModuleMap = nullptr;
   ASTSourceDescriptor PCHDescriptor;
   SourceLocation CurLoc;
-  llvm::MDNode *CurInlinedAt = nullptr;
+  /// The DISubprogram of the current function, if any, ignoring any inlined
+  /// functions.
+  llvm::DISubprogram *CurSP = nullptr;
+  llvm::DebugLoc CurInlinedAt;
   llvm::DIType *VTablePtrType = nullptr;
   llvm::DIType *ClassTy = nullptr;
   llvm::DICompositeType *ObjTy = nullptr;
@@ -454,10 +458,10 @@ class CGDebugInfo {
 
   /// Update the current inline scope. All subsequent calls to \p EmitLocation
   /// will create a location with this inlinedAt field.
-  void setInlinedAt(llvm::MDNode *InlinedAt) { CurInlinedAt = InlinedAt; }
+  void setInlinedAt(llvm::DebugLoc InlinedAt) { CurInlinedAt = InlinedAt; }
 
   /// \return the current inline scope.
-  llvm::MDNode *getInlinedAt() const { return CurInlinedAt; }
+  llvm::DebugLoc getInlinedAt() const { return CurInlinedAt; }
 
   // Converts a SourceLocation to a DebugLoc
   llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Loc);
@@ -632,9 +636,9 @@ class CGDebugInfo {
   /// `<Prefix>` is "__clang_trap_msg".
   ///
   /// This is used to store failure reasons for traps.
-  llvm::DILocation *CreateTrapFailureMessageFor(llvm::DebugLoc TrapLocation,
-                                                StringRef Category,
-                                                StringRef FailureMsg);
+  llvm::DebugLoc CreateTrapFailureMessageFor(llvm::DebugLoc TrapLocation,
+                                             StringRef Category,
+                                             StringRef FailureMsg);
 
 private:
   /// Emit call to llvm.dbg.declare for a variable declaration.
@@ -819,6 +823,11 @@ class CGDebugInfo {
   /// invalid then use current location.
   /// \param Force  Assume DebugColumnInfo option is true.
   unsigned getColumnNumber(SourceLocation Loc, bool Force = false);
+  
+  /// Get the LLVM SrcLoc for the location. If location is invalid then use
+  /// current location.
+  /// \param ForceColumn  Assume DebugColumnInfo option is true.
+  llvm::DISrcLocData getDISrcLoc(SourceLocation Loc, bool ForceColumn = false);
 
   /// Collect various properties of a FunctionDecl.
   /// \param GD  A GlobalDecl whose getDecl() must return a FunctionDecl.
diff --git a/clang/lib/CodeGen/CGLoopInfo.cpp b/clang/lib/CodeGen/CGLoopInfo.cpp
index 2b7d7881ab990..dfcb376773368 100644
--- a/clang/lib/CodeGen/CGLoopInfo.cpp
+++ b/clang/lib/CodeGen/CGLoopInfo.cpp
@@ -14,6 +14,7 @@
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Metadata.h"
@@ -395,16 +396,16 @@ SmallVector<Metadata *, 4> LoopInfo::createMetadata(
     bool &HasUserTransforms) {
   SmallVector<Metadata *, 3> LoopProperties;
 
+  LLVMContext &Ctx = Header->getContext();
   // If we have a valid start debug location for the loop, add it.
   if (StartLoc) {
-    LoopProperties.push_back(StartLoc.getAsMDNode());
-
+    LoopProperties.push_back(StartLoc.toMetadata(Ctx));
+    
     // If we also have a valid end debug location for the loop, add it.
     if (EndLoc)
-      LoopProperties.push_back(EndLoc.getAsMDNode());
+      LoopProperties.push_back(EndLoc.toMetadata(Ctx));
   }
 
-  LLVMContext &Ctx = Header->getContext();
   if (Attrs.MustProgress)
     LoopProperties.push_back(
         MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.mustprogress")));
diff --git a/clang/lib/CodeGen/CGLoopInfo.h b/clang/lib/CodeGen/CGLoopInfo.h
index 3c57124f4137c..064ce8735abf4 100644
--- a/clang/lib/CodeGen/CGLoopInfo.h
+++ b/clang/lib/CodeGen/CGLoopInfo.h
@@ -17,6 +17,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/IR/DebugLoc.h"
+#include "llvm/IR/Metadata.h"
 #include "llvm/IR/Value.h"
 #include "llvm/Support/Compiler.h"
 
diff --git a/llvm/include/llvm/Analysis/InlineAdvisor.h b/llvm/include/llvm/Analysis/InlineAdvisor.h
index 18fb7377ff667..d5e65f484a97a 100644
--- a/llvm/include/llvm/Analysis/InlineAdvisor.h
+++ b/llvm/include/llvm/Analysis/InlineAdvisor.h
@@ -12,6 +12,7 @@
 #include "llvm/Analysis/CGSCCPassManager.h"
 #include "llvm/Analysis/InlineCost.h"
 #include "llvm/Analysis/LazyCallGraph.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/PassManager.h"
 #include <memory>
 
@@ -108,7 +109,7 @@ class InlineAdvice {
 
   /// Get the inlining recommendation.
   bool isInliningRecommended() const { return IsInliningRecommended; }
-  const DebugLoc &getOriginalCallSiteDebugLoc() const { return DLoc; }
+  DILocRef getOriginalCallSiteDebugLoc() const { return DLoc; }
   const BasicBlock *getOriginalCallSiteBasicBlock() const { return Block; }
 
 protected:
@@ -125,7 +126,7 @@ class InlineAdvice {
   // Capture the context of CB before inlining, as a successful inlining may
   // change that context, and we want to report success or failure in the
   // original context.
-  const DebugLoc DLoc;
+  DILocRef DLoc;
   const BasicBlock *const Block;
   OptimizationRemarkEmitter &ORE;
   const bool IsInliningRecommended;
@@ -367,21 +368,21 @@ shouldInline(CallBase &CB, TargetTransformInfo &CalleeTTI,
              OptimizationRemarkEmitter &ORE, bool EnableDeferral = true);
 
 /// Emit ORE message.
-void emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
+void emitInlinedInto(OptimizationRemarkEmitter &ORE, DILocRef DLoc,
                      const BasicBlock *Block, const Function &Callee,
                      const Function &Caller, bool IsMandatory,
                      function_ref<void(OptimizationRemark &)> ExtraContext = {},
                      const char *PassName = nullptr);
 
 /// Emit ORE message based in cost (default heuristic).
-void emitInlinedIntoBasedOnCost(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
+void emitInlinedIntoBasedOnCost(OptimizationRemarkEmitter &ORE, DILocRef DLoc,
                                 const BasicBlock *Block, const Function &Callee,
                                 const Function &Caller, const InlineCost &IC,
                                 bool ForProfileContext = false,
                                 const char *PassName = nullptr);
 
 /// Add location info to ORE message.
-void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc);
+void addLocationToRemarks(OptimizationRemark &Remark, DILocRef DLoc);
 
 /// Set the inline-remark attribute.
 void setInlineRemark(CallBase &CB, StringRef Message);
diff --git a/llvm/include/llvm/Analysis/LoopInfo.h b/llvm/include/llvm/Analysis/LoopInfo.h
index abc0bb8588fa8..cc6091665f806 100644
--- a/llvm/include/llvm/Analysis/LoopInfo.h
+++ b/llvm/include/llvm/Analysis/LoopInfo.h
@@ -376,6 +376,7 @@ class LLVM_ABI Loop : public LoopBase<BasicBlock, Loop> {
   /// cannot find a terminating instruction with location information,
   /// it returns an unknown location.
   DebugLoc getStartLoc() const;
+  DILocRefWrapper getStartLocRef() const;
 
   /// Return the source code span of the loop.
   LocRange getLocRange() const;
diff --git a/llvm/include/llvm/Analysis/ReplayInlineAdvisor.h b/llvm/include/llvm/Analysis/ReplayInlineAdvisor.h
index 6f6ce763746a1..dd0cc1c496684 100644
--- a/llvm/include/llvm/Analysis/ReplayInlineAdvisor.h
+++ b/llvm/include/llvm/Analysis/ReplayInlineAdvisor.h
@@ -11,6 +11,7 @@
 
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Analysis/InlineAdvisor.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 
 namespace llvm {
 class CallBase;
@@ -50,7 +51,7 @@ struct ReplayInlinerSettings {
 };
 
 /// Get call site location as a string with the given format
-std::string formatCallSiteLocation(DebugLoc DLoc, const CallSiteFormat &Format);
+std::string formatCallSiteLocation(DILocRef DLoc, const CallSiteFormat &Format);
 
 std::unique_ptr<InlineAdvisor>
 getReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
diff --git a/llvm/include/llvm/AsmParser/LLParser.h b/llvm/include/llvm/AsmParser/LLParser.h
index c01de4a289a69..5497a0c5...
[truncated]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants