From e0c77d80585428818c32886eb8c26755ea8ed159 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sat, 1 Aug 2026 18:17:33 +0800 Subject: [CodeGen] Track debug instruction identities Add monotonic MIR debug instruction numbers and structured value substitutions. Verify live number uniqueness, meta-instruction exclusion, substitution graph integrity, and target subregister references while preserving debug identities during cloning. --- backend/include/rcc/CodeGen/MachineFunction.h | 4 ++++ .../include/rcc/CodeGen/MachineFunctionRecords.h | 28 ++++++++++++++++++++++ backend/include/rcc/CodeGen/MachineInstr.h | 13 ++++++++++ 3 files changed, 45 insertions(+) (limited to 'backend/include') diff --git a/backend/include/rcc/CodeGen/MachineFunction.h b/backend/include/rcc/CodeGen/MachineFunction.h index c067ee1..8addd4e 100644 --- a/backend/include/rcc/CodeGen/MachineFunction.h +++ b/backend/include/rcc/CodeGen/MachineFunction.h @@ -16,6 +16,7 @@ #include "rcc/Support/Error.h" #include +#include #include #include #include @@ -176,6 +177,8 @@ public: private: MachineFunction(std::string Name, MachineFunctionStableID StableID); + [[nodiscard]] Expected allocateDebugInstrNumber(); + void observeDebugInstrNumber(std::uint32_t Number) noexcept; std::string Name; MachineFunctionStableID StableID; @@ -195,6 +198,7 @@ private: std::vector AllMemOperands; StableIDGenerator BlockIDGenerator; StableIDGenerator InstrIDGenerator; + std::uint32_t DebugInstrNumberingCount = 0; std::unique_ptr Arena; friend class MachineBasicBlock; diff --git a/backend/include/rcc/CodeGen/MachineFunctionRecords.h b/backend/include/rcc/CodeGen/MachineFunctionRecords.h index 7050072..7929ed3 100644 --- a/backend/include/rcc/CodeGen/MachineFunctionRecords.h +++ b/backend/include/rcc/CodeGen/MachineFunctionRecords.h @@ -18,6 +18,25 @@ namespace rcc { class MachineFunctionCloneContext; +inline constexpr std::uint32_t MachineDebugMemoryOperandNumber = 1'000'000; + +struct MachineDebugValueReference final { + std::uint32_t InstructionNumber = 0; + std::uint32_t OperandNumber = 0; + + friend constexpr bool operator==(MachineDebugValueReference Left, + MachineDebugValueReference Right) noexcept = default; +}; + +struct MachineDebugValueSubstitution final { + MachineDebugValueReference Source; + MachineDebugValueReference Destination; + SubRegIndex Subregister; + + friend constexpr bool operator==(MachineDebugValueSubstitution Left, + MachineDebugValueSubstitution Right) noexcept = default; +}; + struct MachineCallSiteArgument final { PhysicalRegister Reg; std::uint16_t ArgumentNumber = 0; @@ -95,6 +114,14 @@ public: [[nodiscard]] Expected addPrefetchTarget(MachinePrefetchTarget Target); + [[nodiscard]] std::span + debugValueSubstitutions() const noexcept { + return DebugValueSubstitutions; + } + + [[nodiscard]] Expected + addDebugValueSubstitution(MachineDebugValueSubstitution Substitution); + [[nodiscard]] Expected verify() const; [[nodiscard]] Expected clone(const MachineFunctionCloneContext &CloneContext) const; @@ -107,6 +134,7 @@ private: std::vector CallSites; std::vector CalledGlobals; std::vector PrefetchTargets; + std::vector DebugValueSubstitutions; }; } // namespace rcc diff --git a/backend/include/rcc/CodeGen/MachineInstr.h b/backend/include/rcc/CodeGen/MachineInstr.h index bcae306..78d48cb 100644 --- a/backend/include/rcc/CodeGen/MachineInstr.h +++ b/backend/include/rcc/CodeGen/MachineInstr.h @@ -9,6 +9,7 @@ #include "rcc/Support/Error.h" #include +#include #include #include #include @@ -132,6 +133,17 @@ public: Origin.reset(); } + [[nodiscard]] std::uint32_t debugInstrNumber() const noexcept { + return DebugInstrNumber; + } + + [[nodiscard]] Expected setDebugInstrNumber(std::uint64_t Number); + [[nodiscard]] Expected assignDebugInstrNumber(); + + void clearDebugInstrNumber() noexcept { + DebugInstrNumber = 0; + } + private: MachineInstr(MachineFunction &OwnerFunction, Opcode InstructionOpcode, MachineInstrStableID StableID); @@ -153,6 +165,7 @@ private: MachineInstrFlags Flags; std::optional Location; std::optional Origin; + std::uint32_t DebugInstrNumber = 0; friend class MachineArena; friend class MachineBasicBlock; -- cgit v1.2.3