2020-04-09 01:27:17 +08:00
|
|
|
//===- MachineStripDebug.cpp - Strip debug info ---------------------------===//
|
|
|
|
//
|
|
|
|
// 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 removes debug info from everything. It can be used to ensure
|
|
|
|
/// tests can be debugified without affecting the output MIR.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-03-15 17:54:19 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2020-04-09 01:27:17 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/InitializePasses.h"
|
2020-04-10 01:36:50 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2020-04-11 05:58:13 +08:00
|
|
|
#include "llvm/Transforms/Utils/Debugify.h"
|
2020-04-09 01:27:17 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "mir-strip-debug"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2020-04-10 01:36:50 +08:00
|
|
|
cl::opt<bool>
|
|
|
|
OnlyDebugifiedDefault("mir-strip-debugify-only",
|
|
|
|
cl::desc("Should mir-strip-debug only strip debug "
|
|
|
|
"info from debugified modules by default"),
|
|
|
|
cl::init(true));
|
2020-04-09 01:27:17 +08:00
|
|
|
|
|
|
|
struct StripDebugMachineModule : public ModulePass {
|
|
|
|
bool runOnModule(Module &M) override {
|
2020-04-10 01:36:50 +08:00
|
|
|
if (OnlyDebugified) {
|
|
|
|
NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
|
|
|
|
if (!DebugifyMD) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Not stripping debug info"
|
|
|
|
" (debugify metadata not found)?\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 01:27:17 +08:00
|
|
|
MachineModuleInfo &MMI =
|
|
|
|
getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
for (Function &F : M.functions()) {
|
2020-04-18 01:24:35 +08:00
|
|
|
MachineFunction *MaybeMF = MMI.getMachineFunction(F);
|
|
|
|
if (!MaybeMF)
|
|
|
|
continue;
|
|
|
|
MachineFunction &MF = *MaybeMF;
|
2020-04-09 01:27:17 +08:00
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
2021-11-16 13:28:46 +08:00
|
|
|
for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
|
|
|
|
if (MI.isDebugInstr()) {
|
2020-04-09 01:27:17 +08:00
|
|
|
// FIXME: We should remove all of them. However, AArch64 emits an
|
|
|
|
// invalid `DBG_VALUE $lr` with only one operand instead of
|
|
|
|
// the usual three and has a test that depends on it's
|
|
|
|
// preservation. Preserve it for now.
|
2021-11-16 13:28:46 +08:00
|
|
|
if (MI.getNumOperands() > 1) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Removing debug instruction " << MI);
|
|
|
|
MBB.erase(&MI);
|
2020-04-09 01:27:17 +08:00
|
|
|
Changed |= true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 13:28:46 +08:00
|
|
|
if (MI.getDebugLoc()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Removing location " << MI);
|
|
|
|
MI.setDebugLoc(DebugLoc());
|
2020-04-09 01:27:17 +08:00
|
|
|
Changed |= true;
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-16 13:28:46 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Keeping " << MI);
|
2020-04-09 01:27:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 05:58:13 +08:00
|
|
|
Changed |= stripDebugifyMetadata(M);
|
2020-04-09 01:27:17 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2020-04-10 01:36:50 +08:00
|
|
|
StripDebugMachineModule() : StripDebugMachineModule(OnlyDebugifiedDefault) {}
|
|
|
|
StripDebugMachineModule(bool OnlyDebugified)
|
|
|
|
: ModulePass(ID), OnlyDebugified(OnlyDebugified) {}
|
2020-04-09 01:27:17 +08:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<MachineModuleInfoWrapperPass>();
|
|
|
|
AU.addPreserved<MachineModuleInfoWrapperPass>();
|
Add -debugify-and-strip-all to add debug info before a pass and remove it after
Summary:
This allows us to test each backend pass under the presence
of debug info using pre-existing tests. The tests should not
fail as a result of this so long as it's true that debug info
does not affect CodeGen.
In practice, a few tests are sensitive to this:
* Tests that check the pass structure (e.g. O0-pipeline.ll)
* Tests that check --debug output. Specifically instruction
dumps containing MMO's (e.g. prelegalizercombiner-extends.ll)
* Tests that contain debugify metadata as mir-strip-debug will
remove it (e.g. fastisel-debugvalue-undef.ll)
* Tests with partial debug info (e.g.
patchable-function-entry-empty.mir had debug info but no
!llvm.dbg.cu)
* Tests that check optimization remarks overly strictly (e.g.
prologue-epilogue-remarks.mir)
* Tests that would inject the pass in an unsafe region (e.g.
seqpairspill.mir would inject between register alloc and
virt reg rewriter)
In all cases, the checks can either be updated or
--debugify-and-strip-all-safe=0 can be used to avoid being
affected by something like llvm-lit -Dllc='llc --debugify-and-strip-all-safe'
I tested this without the lost debug locations verifier to
confirm that AArch64 behaviour is unaffected (with the fixes
in this patch) and with it to confirm it finds the problems
without the additional RUN lines we had before.
Depends on D77886, D77887, D77747
Reviewers: aprantl, vsk, bogner
Subscribers: qcolombet, kristof.beyls, hiraditya, danielkiss, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77888
2020-04-09 04:48:40 +08:00
|
|
|
AU.setPreservesCFG();
|
2020-04-09 01:27:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static char ID; // Pass identification.
|
2020-04-10 01:36:50 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool OnlyDebugified;
|
2020-04-09 01:27:17 +08:00
|
|
|
};
|
|
|
|
char StripDebugMachineModule::ID = 0;
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
|
|
|
|
"Machine Strip Debug Module", false, false)
|
|
|
|
INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
|
|
|
|
"Machine Strip Debug Module", false, false)
|
|
|
|
|
Add -debugify-and-strip-all to add debug info before a pass and remove it after
Summary:
This allows us to test each backend pass under the presence
of debug info using pre-existing tests. The tests should not
fail as a result of this so long as it's true that debug info
does not affect CodeGen.
In practice, a few tests are sensitive to this:
* Tests that check the pass structure (e.g. O0-pipeline.ll)
* Tests that check --debug output. Specifically instruction
dumps containing MMO's (e.g. prelegalizercombiner-extends.ll)
* Tests that contain debugify metadata as mir-strip-debug will
remove it (e.g. fastisel-debugvalue-undef.ll)
* Tests with partial debug info (e.g.
patchable-function-entry-empty.mir had debug info but no
!llvm.dbg.cu)
* Tests that check optimization remarks overly strictly (e.g.
prologue-epilogue-remarks.mir)
* Tests that would inject the pass in an unsafe region (e.g.
seqpairspill.mir would inject between register alloc and
virt reg rewriter)
In all cases, the checks can either be updated or
--debugify-and-strip-all-safe=0 can be used to avoid being
affected by something like llvm-lit -Dllc='llc --debugify-and-strip-all-safe'
I tested this without the lost debug locations verifier to
confirm that AArch64 behaviour is unaffected (with the fixes
in this patch) and with it to confirm it finds the problems
without the additional RUN lines we had before.
Depends on D77886, D77887, D77747
Reviewers: aprantl, vsk, bogner
Subscribers: qcolombet, kristof.beyls, hiraditya, danielkiss, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77888
2020-04-09 04:48:40 +08:00
|
|
|
ModulePass *llvm::createStripDebugMachineModulePass(bool OnlyDebugified) {
|
2020-04-10 01:36:50 +08:00
|
|
|
return new StripDebugMachineModule(OnlyDebugified);
|
2020-04-09 01:27:17 +08:00
|
|
|
}
|