mirror of
https://github.com/llvm/llvm-project.git
synced 2025-05-10 23:06:05 +00:00

Summary: Re-used the IR-level debugify for the most part. The MIR-level code then adds locations to the MachineInstrs afterwards based on the LLVM-IR debug info. It's worth mentioning that the resulting locations make little sense as the range of line numbers used in a Function at the MIR level exceeds that of the equivelent IR level function. As such, MachineInstrs can appear to originate from outside the subprogram scope (and from other subprogram scopes). However, it doesn't seem worth worrying about as the source is imaginary anyway. There's a few high level goals this pass works towards: * We should be able to debugify our .ll/.mir in the lit tests without changing the checks and still pass them. I.e. Debug info should not change codegen. Combining this with a strip-debug pass should enable this. The main issue I ran into without the strip-debug pass was instructions with MMO's and checks on both the instruction and the MMO as the debug-location is between them. I currently have a simple hack in the MIRPrinter to resolve that but the more general solution is a proper strip-debug pass. * We should be able to test that GlobalISel does not lose debug info. I recently found that the legalizer can be unexpectedly lossy in seemingly simple cases (e.g. expanding one instr into many). I have a verifier (will be posted separately) that can be integrated with passes that use the observer interface and will catch location loss (it does not verify correctness, just that there's zero lossage). It is a little conservative as the line-0 locations that arise from conflicts do not track the conflicting locations but it can still catch a fair bit. Depends on D77439, D77438 Reviewers: aprantl, bogner, vsk Subscribers: mgorny, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D77446
85 lines
2.9 KiB
C++
85 lines
2.9 KiB
C++
//===- MachineDebugify.cpp - Attach synthetic debug info to everything ----===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
///
|
|
/// \file This pass attaches synthetic debug info to everything. It can be used
|
|
/// to create targeted tests for debug info preservation.
|
|
///
|
|
/// This isn't intended to have feature parity with Debugify.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
#include "llvm/CodeGen/Passes.h"
|
|
#include "llvm/IR/DIBuilder.h"
|
|
#include "llvm/IR/DebugInfo.h"
|
|
#include "llvm/InitializePasses.h"
|
|
#include "llvm/Transforms/Utils/Debugify.h"
|
|
|
|
#define DEBUG_TYPE "mir-debugify"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
|
|
DIBuilder &DIB, Function &F) {
|
|
MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
|
|
|
|
DISubprogram *SP = F.getSubprogram();
|
|
assert(SP && "IR Debugify just created it?");
|
|
|
|
LLVMContext &Ctx = F.getParent()->getContext();
|
|
unsigned NextLine = SP->getLine();
|
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
for (MachineInstr &MI : MBB) {
|
|
// This will likely emit line numbers beyond the end of the imagined
|
|
// source function and into subsequent ones. We don't do anything about
|
|
// that as it doesn't really matter to the compiler where the line is in
|
|
// the imaginary source code.
|
|
MI.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// ModulePass for attaching synthetic debug info to everything, used with the
|
|
/// legacy module pass manager.
|
|
struct DebugifyMachineModule : public ModulePass {
|
|
bool runOnModule(Module &M) override {
|
|
MachineModuleInfo &MMI =
|
|
getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
|
|
return applyDebugifyMetadata(
|
|
M, M.functions(),
|
|
"ModuleDebugify: ", [&](DIBuilder &DIB, Function &F) -> bool {
|
|
return applyDebugifyMetadataToMachineFunction(MMI, DIB, F);
|
|
});
|
|
}
|
|
|
|
DebugifyMachineModule() : ModulePass(ID) {}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
AU.addRequired<MachineModuleInfoWrapperPass>();
|
|
AU.addPreserved<MachineModuleInfoWrapperPass>();
|
|
}
|
|
|
|
static char ID; // Pass identification.
|
|
};
|
|
char DebugifyMachineModule::ID = 0;
|
|
|
|
} // end anonymous namespace
|
|
|
|
INITIALIZE_PASS_BEGIN(DebugifyMachineModule, DEBUG_TYPE,
|
|
"Machine Debugify Module", false, false)
|
|
INITIALIZE_PASS_END(DebugifyMachineModule, DEBUG_TYPE,
|
|
"Machine Debugify Module", false, false)
|
|
|
|
ModulePass *createDebugifyMachineModulePass() {
|
|
return new DebugifyMachineModule();
|
|
}
|