Skip to content

Commit

Permalink
Merged main:a906e3eccd1e into amd-gfx:a41f8084775a
Browse files Browse the repository at this point in the history
Local branch amd-gfx a41f808 Merged main:fce9dd6b2521 into amd-gfx:d697cf024dfc
Remote branch main a906e3e [NFC][SampleFDO] Preparation to support multiple sections with the same type in ExtBinary format.
  • Loading branch information
Sw authored and Sw committed Dec 17, 2020
2 parents a41f808 + a906e3e commit 7af1748
Show file tree
Hide file tree
Showing 31 changed files with 602 additions and 174 deletions.
28 changes: 22 additions & 6 deletions llvm/docs/HowToUpdateDebugInfo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ A variant of the ``debugify`` utility described in
:ref:`Mutation testing for IR-level transformations<IRDebugify>` can be used
for MIR-level transformations as well: much like the IR-level pass,
``mir-debugify`` inserts sequentially increasing line locations to each
``MachineInstr`` in a ``Module`` (although there is no equivalent MIR-level
``check-debugify`` pass).
``MachineInstr`` in a ``Module``. And the MIR-level ``mir-check-debugify`` is
similar to IR-level ``check-debugify`` pass.

For example, here is a snippet before:

Expand Down Expand Up @@ -403,16 +403,32 @@ and ``-start-after``. For example:
$ llc -debugify-and-strip-all-safe -run-pass=... <other llc args>
$ llc -debugify-and-strip-all-safe -O1 <other llc args>
If you want to check it after each pass in a pipeline, use
``-debugify-check-and-strip-all-safe``. This can also be combined with
``-start-before`` and ``-start-after``. For example:

.. code-block:: bash
$ llc -debugify-check-and-strip-all-safe -run-pass=... <other llc args>
$ llc -debugify-check-and-strip-all-safe -O1 <other llc args>
To check all debug info from a test, use ``mir-check-debugify``, like:

.. code-block:: bash
$ llc -run-pass=mir-debugify,other-pass,mir-check-debugify
To strip out all debug info from a test, use ``mir-strip-debug``, like:

.. code-block:: bash
$ llc -run-pass=mir-debugify,other-pass,mir-strip-debug
It can be useful to combine ``mir-debugify`` and ``mir-strip-debug`` to
identify backend transformations which break in the presence of debug info.
For example, to run the AArch64 backend tests with all normal passes
"sandwiched" in between MIRDebugify and MIRStripDebugify mutation passes, run:
It can be useful to combine ``mir-debugify``, ``mir-check-debugify`` and/or
``mir-strip-debug`` to identify backend transformations which break in
the presence of debug info. For example, to run the AArch64 backend tests
with all normal passes "sandwiched" in between MIRDebugify and
MIRStripDebugify mutation passes, run:

.. code-block:: bash
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/CodeGen/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ namespace llvm {
/// info was generated by another source such as clang.
ModulePass *createStripDebugMachineModulePass(bool OnlyDebugified);

/// Creates MIR Check Debug pass. \see MachineCheckDebugify.cpp
ModulePass *createCheckDebugMachineModulePass();

/// The pass fixups statepoint machine instruction to replace usage of
/// caller saved registers with stack slots.
extern char &FixupStatepointCallerSavedID;
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/CodeGen/TargetPassConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ class TargetPassConfig : public ImmutablePass {
/// Add a pass to remove debug info from the MIR.
void addStripDebugPass();

/// Add a pass to check synthesized debug info for MIR.
void addCheckDebugPass();

/// Add standard passes before a pass that's about to be added. For example,
/// the DebugifyMachineModulePass if it is enabled.
void addMachinePrePasses(bool AllowDebugify = true);
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/DataLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class DataLayout {

bool isNonIntegralAddressSpace(unsigned AddrSpace) const {
ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces();
return find(NonIntegralSpaces, AddrSpace) != NonIntegralSpaces.end();
return is_contained(NonIntegralSpaces, AddrSpace);
}

bool isNonIntegralPointerType(PointerType *PT) const {
Expand Down
6 changes: 4 additions & 2 deletions llvm/include/llvm/IR/IntrinsicsRISCV.td
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ let TargetPrefix = "riscv" in {
: Intrinsic<[llvm_anyvector_ty ],
[LLVMMatchType<0>,
LLVMPointerType<LLVMMatchType<0>>,
llvm_anyvector_ty, llvm_anyint_ty],
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>,
llvm_anyint_ty],
[NoCapture<ArgIndex<1>>, IntrReadMem]>, RISCVVIntrinsic;
// For unit stride store
// Input: (vector_in, pointer, vl)
Expand All @@ -108,7 +109,8 @@ let TargetPrefix = "riscv" in {
: Intrinsic<[],
[llvm_anyvector_ty,
LLVMPointerType<LLVMMatchType<0>>,
llvm_anyvector_ty, llvm_anyint_ty],
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>,
llvm_anyint_ty],
[NoCapture<ArgIndex<1>>, IntrWriteMem]>, RISCVVIntrinsic;
// For destination vector type is the same as first source vector.
// Input: (vector_in, vector_in/scalar_in, vl)
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void initializeCallGraphViewerPass(PassRegistry&);
void initializeCallGraphWrapperPassPass(PassRegistry&);
void initializeCallSiteSplittingLegacyPassPass(PassRegistry&);
void initializeCalledValuePropagationLegacyPassPass(PassRegistry &);
void initializeCheckDebugMachineModulePass(PassRegistry &);
void initializeCodeGenPreparePass(PassRegistry&);
void initializeConstantHoistingLegacyPassPass(PassRegistry&);
void initializeConstantMergeLegacyPassPass(PassRegistry&);
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/ProfileData/SampleProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ struct SecHdrTableEntry {
uint64_t Flags;
uint64_t Offset;
uint64_t Size;
// The index indicating the location of the current entry in
// SectionHdrLayout table.
uint32_t LayoutIndex;
};

// Flags common for all sections are defined here. In SecHdrTableEntry::Flags,
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/ProfileData/SampleProfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {

protected:
std::vector<SecHdrTableEntry> SecHdrTable;
std::error_code readSecHdrTableEntry();
std::error_code readSecHdrTableEntry(uint32_t Idx);
std::error_code readSecHdrTable();

std::error_code readFuncMetadata();
Expand Down
19 changes: 12 additions & 7 deletions llvm/include/llvm/ProfileData/SampleProfWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
};

protected:
uint64_t markSectionStart(SecType Type);
std::error_code addNewSection(SecType Sec, uint64_t SectionStart);
uint64_t markSectionStart(SecType Type, uint32_t LayoutIdx);
std::error_code addNewSection(SecType Sec, uint32_t LayoutIdx,
uint64_t SectionStart);
template <class SecFlagType>
void addSectionFlag(SecType Type, SecFlagType Flag) {
for (auto &Entry : SectionHdrLayout) {
Expand All @@ -193,9 +194,11 @@ class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
virtual std::error_code
writeSections(const StringMap<FunctionSamples> &ProfileMap) = 0;

// Dispatch section writer for each section.
// Dispatch section writer for each section. \p LayoutIdx is the sequence
// number indicating where the section is located in SectionHdrLayout.
virtual std::error_code
writeOneSection(SecType Type, const StringMap<FunctionSamples> &ProfileMap);
writeOneSection(SecType Type, uint32_t LayoutIdx,
const StringMap<FunctionSamples> &ProfileMap);

// Helper function to write name table.
virtual std::error_code writeNameTable() override;
Expand All @@ -209,7 +212,7 @@ class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
std::error_code writeProfileSymbolListSection();

// Specifiy the order of sections in section header table. Note
// the order of sections in the profile may be different that the
// the order of sections in SecHdrTable may be different that the
// order in SectionHdrLayout. sample Reader will follow the order
// in SectionHdrLayout to read each section.
SmallVector<SecHdrTableEntry, 8> SectionHdrLayout;
Expand All @@ -224,7 +227,6 @@ class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
std::error_code writeSecHdrTable();
virtual std::error_code
writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
SecHdrTableEntry &getEntryInLayout(SecType Type);
std::error_code compressAndOutput();

// We will swap the raw_ostream held by LocalBufStream and that
Expand All @@ -241,7 +243,10 @@ class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
// The location in the output stream where the SecHdrTable should be
// written to.
uint64_t SecHdrTableOffset;
// Initial Section Flags setting.
// The table contains SecHdrTableEntry entries in order of how they are
// populated in the writer. It may be different from the order in
// SectionHdrLayout which specifies the sequence in which sections will
// be read.
std::vector<SecHdrTableEntry> SecHdrTable;

// FuncOffsetTable maps function name to its profile offset in SecLBRProfile
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ add_llvm_component_library(LLVMCodeGen
MachineCombiner.cpp
MachineCopyPropagation.cpp
MachineCSE.cpp
MachineCheckDebugify.cpp
MachineDebugify.cpp
MachineDominanceFrontier.cpp
MachineDominators.cpp
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeBranchRelaxationPass(Registry);
initializeCFGuardLongjmpPass(Registry);
initializeCFIInstrInserterPass(Registry);
initializeCheckDebugMachineModulePass(Registry);
initializeCodeGenPreparePass(Registry);
initializeDeadMachineInstructionElimPass(Registry);
initializeDebugifyMachineModulePass(Registry);
Expand Down
126 changes: 126 additions & 0 deletions llvm/lib/CodeGen/MachineCheckDebugify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//===- MachineCheckDebugify.cpp - Check 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 checks debug info after mir-debugify (+ pass-to-test). Currently
/// it simply checks the integrity of line info in DILocation and
/// DILocalVariable which mir-debugifiy generated before.
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Utils/Debugify.h"

#define DEBUG_TYPE "mir-check-debugify"

using namespace llvm;

namespace {

struct CheckDebugMachineModule : public ModulePass {
bool runOnModule(Module &M) override {
MachineModuleInfo &MMI =
getAnalysis<MachineModuleInfoWrapperPass>().getMMI();

NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
if (!NMD) {
errs() << "WARNING: Please run mir-debugify to generate "
"llvm.mir.debugify metadata first.\n";
return false;
}

auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
->getZExtValue();
};
assert(NMD->getNumOperands() == 2 &&
"llvm.mir.debugify should have exactly 2 operands!");
unsigned NumLines = getDebugifyOperand(0);
unsigned NumVars = getDebugifyOperand(1);
BitVector MissingLines{NumLines, true};
BitVector MissingVars{NumVars, true};

for (Function &F : M.functions()) {
MachineFunction *MF = MMI.getMachineFunction(F);
if (!MF)
continue;
for (MachineBasicBlock &MBB : *MF) {
// Find missing lines.
// TODO: Avoid meta instructions other than dbg_val.
for (MachineInstr &MI : MBB) {
if (MI.isDebugValue())
continue;
const DebugLoc DL = MI.getDebugLoc();
if (DL && DL.getLine() != 0) {
MissingLines.reset(DL.getLine() - 1);
continue;
}

if (!DL) {
errs() << "WARNING: Instruction with empty DebugLoc in function ";
errs() << F.getName() << " --";
MI.print(errs());
}
}

// Find missing variables.
// TODO: Handle DBG_INSTR_REF which is under an experimental option now.
for (MachineInstr &MI : MBB) {
if (!MI.isDebugValue())
continue;
const DILocalVariable *LocalVar = MI.getDebugVariable();
unsigned Var = ~0U;

(void)to_integer(LocalVar->getName(), Var, 10);
assert(Var <= NumVars && "Unexpected name for DILocalVariable");
MissingVars.reset(Var - 1);
}
}
}

bool Fail = false;
for (unsigned Idx : MissingLines.set_bits()) {
errs() << "WARNING: Missing line " << Idx + 1 << "\n";
Fail = true;
}

for (unsigned Idx : MissingVars.set_bits()) {
errs() << "WARNING: Missing variable " << Idx + 1 << "\n";
Fail = true;
}
errs() << "Machine IR debug info check: ";
errs() << (Fail ? "FAIL" : "PASS") << "\n";

return false;
}

CheckDebugMachineModule() : ModulePass(ID) {}

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<MachineModuleInfoWrapperPass>();
AU.addPreserved<MachineModuleInfoWrapperPass>();
AU.setPreservesCFG();
}

static char ID; // Pass identification.
};
char CheckDebugMachineModule::ID = 0;

} // end anonymous namespace

INITIALIZE_PASS_BEGIN(CheckDebugMachineModule, DEBUG_TYPE,
"Machine Check Debug Module", false, false)
INITIALIZE_PASS_END(CheckDebugMachineModule, DEBUG_TYPE,
"Machine Check Debug Module", false, false)

ModulePass *llvm::createCheckDebugMachineModulePass() {
return new CheckDebugMachineModule();
}
32 changes: 31 additions & 1 deletion llvm/lib/CodeGen/MachineDebugify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
Expand Down Expand Up @@ -89,10 +90,11 @@ bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
// Do this by introducing debug uses of each register definition. If that is
// not possible (e.g. we have a phi or a meta instruction), emit a constant.
uint64_t NextImm = 0;
SmallSet<DILocalVariable *, 16> VarSet;
const MCInstrDesc &DbgValDesc = TII.get(TargetOpcode::DBG_VALUE);
for (MachineBasicBlock &MBB : MF) {
MachineBasicBlock::iterator FirstNonPHIIt = MBB.getFirstNonPHI();
for (auto I = MBB.begin(), E = MBB.end(); I != E; ) {
for (auto I = MBB.begin(), E = MBB.end(); I != E;) {
MachineInstr &MI = *I;
++I;

Expand All @@ -113,6 +115,7 @@ bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
Line = EarliestDVI->getDebugLoc().getLine();
DILocalVariable *LocalVar = Line2Var[Line];
assert(LocalVar && "No variable for current line?");
VarSet.insert(LocalVar);

// Emit DBG_VALUEs for register definitions.
SmallVector<MachineOperand *, 4> RegDefs;
Expand All @@ -132,6 +135,33 @@ bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
}
}

// Here we save the number of lines and variables into "llvm.mir.debugify".
// It is useful for mir-check-debugify.
NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
IntegerType *Int32Ty = Type::getInt32Ty(Ctx);
if (!NMD) {
NMD = M.getOrInsertNamedMetadata("llvm.mir.debugify");
auto addDebugifyOperand = [&](unsigned N) {
NMD->addOperand(MDNode::get(
Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
};
// Add number of lines.
addDebugifyOperand(NextLine - 1);
// Add number of variables.
addDebugifyOperand(VarSet.size());
} else {
assert(NMD->getNumOperands() == 2 &&
"llvm.mir.debugify should have exactly 2 operands!");
auto setDebugifyOperand = [&](unsigned Idx, unsigned N) {
NMD->setOperand(Idx, MDNode::get(Ctx, ValueAsMetadata::getConstant(
ConstantInt::get(Int32Ty, N))));
};
// Set number of lines.
setDebugifyOperand(0, NextLine - 1);
// Set number of variables.
setDebugifyOperand(1, VarSet.size());
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,7 @@ void SelectionDAGISel::UpdateChains(

// If the node became dead and we haven't already seen it, delete it.
if (ChainNode != NodeToMatch && ChainNode->use_empty() &&
!std::count(NowDeadNodes.begin(), NowDeadNodes.end(), ChainNode))
!llvm::is_contained(NowDeadNodes, ChainNode))
NowDeadNodes.push_back(ChainNode);
}
}
Expand Down
Loading

0 comments on commit 7af1748

Please sign in to comment.