diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-22 18:59:22 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-22 18:59:22 +0800 |
| commit | 36b0be1ec1558f88d4f031a1817c3a4c2cba07a6 (patch) | |
| tree | 3cf29b19ec7e450bd04dca8d416d68350aa20de2 /backend | |
| parent | 8a4bb6e3b0bffa0ed79b51347b12ee4293b622a7 (diff) | |
| download | rcc-project-36b0be1ec1558f88d4f031a1817c3a4c2cba07a6.tar.gz rcc-project-36b0be1ec1558f88d4f031a1817c3a4c2cba07a6.tar.xz | |
[CodeGen] Match LLVM machine debug stripping
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/include/rcc/CodeGen/MachineInstr.h | 2 | ||||
| -rw-r--r-- | backend/include/rcc/CodeGen/MachineStripDebug.h | 16 | ||||
| -rw-r--r-- | backend/include/rcc/Tooling/PipelineDriver.h | 1 | ||||
| -rw-r--r-- | backend/lib/CodeGen/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | backend/lib/CodeGen/MIRPrinter.cpp | 3 | ||||
| -rw-r--r-- | backend/lib/CodeGen/MachineInstr.cpp | 16 | ||||
| -rw-r--r-- | backend/lib/CodeGen/MachineStripDebug.cpp | 34 | ||||
| -rw-r--r-- | backend/lib/Tooling/PipelineDriver.cpp | 8 | ||||
| -rw-r--r-- | backend/tools/MIRTool/MIRToolMain.cpp | 5 |
9 files changed, 85 insertions, 2 deletions
diff --git a/backend/include/rcc/CodeGen/MachineInstr.h b/backend/include/rcc/CodeGen/MachineInstr.h index 60f8014..6de4a4e 100644 --- a/backend/include/rcc/CodeGen/MachineInstr.h +++ b/backend/include/rcc/CodeGen/MachineInstr.h @@ -79,6 +79,8 @@ public: return ParentBlock != nullptr; } + [[nodiscard]] bool isDebugInstr() const noexcept; + [[nodiscard]] std::size_t operandCount() const noexcept { return Operands.size(); } diff --git a/backend/include/rcc/CodeGen/MachineStripDebug.h b/backend/include/rcc/CodeGen/MachineStripDebug.h new file mode 100644 index 0000000..eb89155 --- /dev/null +++ b/backend/include/rcc/CodeGen/MachineStripDebug.h @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_MACHINESTRIPDEBUG_H +#define RCC_CODEGEN_MACHINESTRIPDEBUG_H + +#include "rcc/Support/Error.h" + +namespace rcc { + +class MachineFunction; + +[[nodiscard]] Expected<bool> stripDebugMachineFunction(MachineFunction &Function); + +} // namespace rcc + +#endif // RCC_CODEGEN_MACHINESTRIPDEBUG_H diff --git a/backend/include/rcc/Tooling/PipelineDriver.h b/backend/include/rcc/Tooling/PipelineDriver.h index d10fe4d..c01650c 100644 --- a/backend/include/rcc/Tooling/PipelineDriver.h +++ b/backend/include/rcc/Tooling/PipelineDriver.h @@ -47,6 +47,7 @@ struct PipelineControls final { bool VerifyEach = true; bool DetectUnreportedChanges = true; bool SimplifyMIR = false; + bool DebugifyAndStripAllSafe = false; }; struct PipelineSnapshot final { diff --git a/backend/lib/CodeGen/CMakeLists.txt b/backend/lib/CodeGen/CMakeLists.txt index 2fc7254..b95fe42 100644 --- a/backend/lib/CodeGen/CMakeLists.txt +++ b/backend/lib/CodeGen/CMakeLists.txt @@ -23,6 +23,7 @@ set( MachineCopyPropagation.cpp MachineCSE.cpp MachineDebugInfo.cpp + MachineStripDebug.cpp MachineDCE.cpp MachineFrameInfo.cpp MachineFinalization.cpp @@ -111,6 +112,7 @@ set( "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineCopyPropagation.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineCSE.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineDebugInfo.h" + "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineStripDebug.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineDCE.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineFrameInfo.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineFinalization.h" diff --git a/backend/lib/CodeGen/MIRPrinter.cpp b/backend/lib/CodeGen/MIRPrinter.cpp index 5eb244a..c684368 100644 --- a/backend/lib/CodeGen/MIRPrinter.cpp +++ b/backend/lib/CodeGen/MIRPrinter.cpp @@ -1935,7 +1935,8 @@ Expected<void> BodyPrinter::printRegisterOperand(const MachineOperand &Operand, Output.append("internal "); if (Flags.has(RegisterOperandFlag::EarlyClobber)) Output.append("early-clobber "); - if (Flags.has(RegisterOperandFlag::Debug)) + if (Flags.has(RegisterOperandFlag::Debug) && + (Operand.parent() == nullptr || !Operand.parent()->isDebugInstr())) Output.append("debug-use "); if (Flags.has(RegisterOperandFlag::Renamable)) Output.append("renamable "); diff --git a/backend/lib/CodeGen/MachineInstr.cpp b/backend/lib/CodeGen/MachineInstr.cpp index f4602e0..55b4ce9 100644 --- a/backend/lib/CodeGen/MachineInstr.cpp +++ b/backend/lib/CodeGen/MachineInstr.cpp @@ -2,6 +2,7 @@ #include "rcc/CodeGen/MachineInstr.h" +#include "rcc/CodeGen/GenericOpcodes.h" #include "rcc/CodeGen/MachineBasicBlock.h" #include "rcc/CodeGen/MachineFunction.h" #include "rcc/CodeGen/MachineMemOperand.h" @@ -18,6 +19,14 @@ namespace rcc { +bool MachineInstr::isDebugInstr() const noexcept { + return InstructionOpcode == rcc::opcode(GenericOpcode::DBG_VALUE) || + InstructionOpcode == rcc::opcode(GenericOpcode::DBG_VALUE_LIST) || + InstructionOpcode == rcc::opcode(GenericOpcode::DBG_LABEL) || + InstructionOpcode == rcc::opcode(GenericOpcode::DBG_INSTR_REF) || + InstructionOpcode == rcc::opcode(GenericOpcode::DBG_PHI); +} + MachineInstr::MachineInstr(MachineFunction &OwnerFunction, Opcode InstructionOpcode, MachineInstrStableID StableID) : OwnerFunction(&OwnerFunction), InstructionOpcode(InstructionOpcode), StableID(StableID) {} @@ -97,6 +106,13 @@ Expected<void> MachineInstr::insertOperand(std::size_t Index, MachineOperand &&O return makeUnexpected(ErrorCode::InvalidArgument, "register operand references an unknown virtual register"); } + if (Operand.isUse() && isDebugInstr()) { + RegisterOperandFlags RegisterFlags = Operand.registerFlags(); + RegisterFlags.set(RegisterOperandFlag::Debug); + auto Updated = Operand.setRegisterFlags(RegisterFlags); + if (!Updated) + return std::unexpected(Updated.error()); + } Operands.insert(Operands.begin() + static_cast<std::ptrdiff_t>(Index), std::move(Operand)); adjustTiesForInsertion(Index); diff --git a/backend/lib/CodeGen/MachineStripDebug.cpp b/backend/lib/CodeGen/MachineStripDebug.cpp new file mode 100644 index 0000000..4699ef0 --- /dev/null +++ b/backend/lib/CodeGen/MachineStripDebug.cpp @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "rcc/CodeGen/MachineStripDebug.h" + +#include "rcc/CodeGen/MachineBasicBlock.h" +#include "rcc/CodeGen/MachineFunction.h" +#include "rcc/CodeGen/MachineInstr.h" + +namespace rcc { + +Expected<bool> stripDebugMachineFunction(MachineFunction &Function) { + bool Changed = false; + for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr; Block = Block->next()) { + for (MachineInstr *Instruction = Block->firstInstruction(); Instruction != nullptr;) { + MachineInstr *Next = Instruction->next(); + if (Instruction->isDebugInstr() && Instruction->operandCount() > 1) { + auto Removed = Block->removeInstruction(*Instruction); + if (!Removed) + return std::unexpected(Removed.error()); + Changed = true; + Instruction = Next; + continue; + } + if (Instruction->debugLocation()) { + Instruction->clearDebugLocation(); + Changed = true; + } + Instruction = Next; + } + } + return Changed; +} + +} // namespace rcc diff --git a/backend/lib/Tooling/PipelineDriver.cpp b/backend/lib/Tooling/PipelineDriver.cpp index 0ba0c54..959ff4c 100644 --- a/backend/lib/Tooling/PipelineDriver.cpp +++ b/backend/lib/Tooling/PipelineDriver.cpp @@ -6,6 +6,7 @@ #include "rcc/CodeGen/MIRPrinter.h" #include "rcc/CodeGen/MachineFunction.h" #include "rcc/CodeGen/MachineOptimizationPipeline.h" +#include "rcc/CodeGen/MachineStripDebug.h" #include "rcc/CodeGen/MachineVerifier.h" #include "rcc/Target/RISCV/RISCVPipeline.h" @@ -322,6 +323,13 @@ runFunction(MachineFunction &Function, mir::MIRDocument &Document, const TargetD auto Run = SinglePass.run(Function, Target, Analyses, Instrumentation); if (!Run) return std::unexpected(Run.error()); + if (Controls.DebugifyAndStripAllSafe) { + auto Stripped = stripDebugMachineFunction(Function); + if (!Stripped) + return std::unexpected(Stripped.error()); + if (*Stripped) + Analyses.invalidate(Function, PreservedAnalyses::none(), MachinePassChanges::all()); + } Result.ExecutedPasses.push_back({ .Function = std::string(Function.name()), .Pass = Name, diff --git a/backend/tools/MIRTool/MIRToolMain.cpp b/backend/tools/MIRTool/MIRToolMain.cpp index 2885b0f..b635c27 100644 --- a/backend/tools/MIRTool/MIRToolMain.cpp +++ b/backend/tools/MIRTool/MIRToolMain.cpp @@ -330,6 +330,10 @@ void setBackendOption(std::vector<rcc::tooling::BackendOption> &Options, std::st Command.Controls.DetectUnreportedChanges = false; continue; } + if (Argument == "-debugify-and-strip-all-safe" || Argument == "--debugify-and-strip-all-safe") { + Command.Controls.DebugifyAndStripAllSafe = true; + continue; + } if (Argument == "--print-actions") { Command.PrintActions = true; continue; @@ -346,7 +350,6 @@ void setBackendOption(std::vector<rcc::tooling::BackendOption> &Options, std::st bool ParsedCompatibilityOption = false; for (const std::string_view Name : { std::string_view{"-disable-gisel-legality-check"}, - std::string_view{"-debugify-and-strip-all-safe"}, std::string_view{"-mcp-use-is-copy-instr"}, std::string_view{"-riscv-enable-cfi-instr-inserter"}, std::string_view{"-riscv-outliner-regsave"}, |
