LLVM 17.0.0git
Macros | Functions | Variables
AddDiscriminators.cpp File Reference
#include "llvm/Transforms/Utils/AddDiscriminators.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PassManager.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils.h"
#include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h"
#include <utility>

Go to the source code of this file.


#define DEBUG_TYPE   "add-discriminators"


 INITIALIZE_PASS_BEGIN (AddDiscriminatorsLegacyPass, "add-discriminators", "Add DWARF path discriminators", false, false) INITIALIZE_PASS_END(AddDiscriminatorsLegacyPass
static bool shouldHaveDiscriminator (const Instruction *I)
static bool addDiscriminators (Function &F)
 Assign DWARF discriminators.


static cl::opt< boolNoDiscriminators ("no-discriminators", cl::init(false), cl::desc("Disable generation of discriminator information."))
add discriminators
add Add DWARF path false

Macro Definition Documentation


#define DEBUG_TYPE   "add-discriminators"

Definition at line 79 of file AddDiscriminators.cpp.

Function Documentation

◆ addDiscriminators()

static bool addDiscriminators ( Function F)

Assign DWARF discriminators.

To assign discriminators, we examine the boundaries of every basic block and its successors. Suppose there is a basic block B1 with successor B2. The last instruction I1 in B1 and the first instruction I2 in B2 are located at the same file and line number. This situation is illustrated in the following code snippet:

  if (i < 10) x = i;

  br i1 %cmp, label %if.then, label %if.end, !dbg !10
  %1 = load i32* %i.addr, align 4, !dbg !10
  store i32 %1, i32* %x, align 4, !dbg !10
  br label %if.end, !dbg !10
  ret void, !dbg !12

Notice how the branch instruction in block 'entry' and all the instructions in block 'if.then' have the exact same debug location information (!dbg !10).

To distinguish instructions in block 'entry' from instructions in block 'if.then', we generate a new lexical block for all the instruction in block 'if.then' that share the same file and line location with the last instruction of block 'entry'.

This new lexical block will have the same location information as the previous one, but with a new DWARF discriminator value.

One of the main uses of this discriminator value is in runtime sample profilers. It allows the profiler to distinguish instructions at location !dbg !10 that execute on different basic blocks. This is important because while the predicate 'if (x < 10)' may have been executed millions of times, the assignment 'x = i' may have only executed a handful of times (meaning that the entry->if.then edge is seldom taken).

If we did not have discriminator information, the profiler would assign the same weight to both blocks 'entry' and 'if.then', which in turn will make it conclude that the entry->if.then edge is very hot.

To decide where to create new discriminator values, this function traverses the CFG and examines instruction at basic block boundaries. If the last instruction I1 of a block B1 is at the same file and line location as instruction I2 of successor B2, then it creates a new lexical block for I2 and all the instruction in B2 that share the same file and line location as I2. This new lexical block will have a different discriminator number than I1.

Definition at line 170 of file AddDiscriminators.cpp.

References B, llvm::DILocation::cloneWithBaseDiscriminator(), llvm::sampleprofutil::createFSDiscriminatorVariable(), llvm::dbgs(), llvm::EnableFSDiscriminator, F, I, LLVM_DEBUG, NoDiscriminators, and shouldHaveDiscriminator().

Referenced by llvm::AddDiscriminatorsPass::run().


INITIALIZE_PASS_BEGIN ( AddDiscriminatorsLegacyPass  ,
"add-discriminators"  ,
"Add DWARF path discriminators"  ,
false  ,

◆ shouldHaveDiscriminator()

static bool shouldHaveDiscriminator ( const Instruction I)

Definition at line 115 of file AddDiscriminators.cpp.

References I.

Referenced by addDiscriminators().

Variable Documentation

◆ discriminators

add Add DWARF path discriminators

Definition at line 107 of file AddDiscriminators.cpp.

◆ false

add Add DWARF path false

Definition at line 108 of file AddDiscriminators.cpp.

◆ NoDiscriminators

cl::opt< bool > NoDiscriminators("no-discriminators", cl::init(false), cl::desc("Disable generation of discriminator information.")) ( "no-discriminators"  ,
cl::init(false)  ,
cl::desc("Disable generation of discriminator information.")   

Referenced by addDiscriminators().