Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/include/llvm/IR/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,8 @@ class MDNode : public Metadata {
static MDNode *getMergedProfMetadata(MDNode *A, MDNode *B,
const Instruction *AInstr,
const Instruction *BInstr);
static MDNode *getMergedMemProfMetadata(MDNode *A, MDNode *B);
static MDNode *getMergedCallsiteMetadata(MDNode *A, MDNode *B);
};

/// Tuple of metadata.
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/Transforms/Utils/Local.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ Instruction *removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU = nullptr);
bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU = nullptr,
MemorySSAUpdater *MSSAU = nullptr);

/// DO NOT CALL EXTERNALLY.
/// FIXME: https://github.com/llvm/llvm-project/issues/121495
/// Once external callers of this function are removed, either inline into
/// combineMetadataForCSE, or internalize and remove KnownIDs parameter.
///
/// Combine the metadata of two instructions so that K can replace J. Some
/// metadata kinds can only be kept if K does not move, meaning it dominated
/// J in the original IR.
Expand Down
21 changes: 21 additions & 0 deletions llvm/lib/Analysis/MemoryProfileInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,24 @@ template <> uint64_t CallStack<MDNode, MDNode::op_iterator>::back() const {
return mdconst::dyn_extract<ConstantInt>(N->operands().back())
->getZExtValue();
}

MDNode *MDNode::getMergedMemProfMetadata(MDNode *A, MDNode *B) {
if (!(A && B)) {
return A ? A : B;
}

// TODO: Support more sophisticated merging, such as selecting the one with
// more bytes allocated, or implement support for carrying multiple allocation
// leaf contexts. For now, keep the first one.
return A;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is a little hard to read. Can this be replaced with --

if(A) return A;
return B;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

MDNode *MDNode::getMergedCallsiteMetadata(MDNode *A, MDNode *B) {
if (!(A && B)) {
return A ? A : B;
}

// TODO: Support more sophisticated merging, which will require support for
// carrying multiple contexts. For now, keep the first one.
return A;
}
3 changes: 3 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,9 @@ Instruction *InstCombinerImpl::foldPHIArgLoadIntoPHI(PHINode &PN) {
BasicBlock *BB = std::get<0>(Incoming);
Value *V = std::get<1>(Incoming);
LoadInst *LI = cast<LoadInst>(V);
// FIXME: https://github.com/llvm/llvm-project/issues/121495
// Call combineMetadataForCSE instead, so that an explicit set of KnownIDs
// doesn't need to be maintained here.
combineMetadata(NewLI, LI, KnownIDs, true);
Value *NewInVal = LI->getOperand(0);
if (NewInVal != InVal)
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ static void combineAAMetadata(Instruction *ReplInst, Instruction *I) {
LLVMContext::MD_noalias, LLVMContext::MD_invariant_group,
LLVMContext::MD_access_group, LLVMContext::MD_prof,
LLVMContext::MD_memprof, LLVMContext::MD_callsite};
// FIXME: https://github.com/llvm/llvm-project/issues/121495
// Use custom AA metadata combining handling instead of combineMetadata, which
// is meant for CSE and will drop any metadata not in the KnownIDs list.
combineMetadata(ReplInst, I, KnownIDs, true);
}

Expand Down
11 changes: 10 additions & 1 deletion llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3308,6 +3308,9 @@ bool llvm::removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU,
return Changed;
}

// FIXME: https://github.com/llvm/llvm-project/issues/121495
// Once external callers of this function are removed, either inline into
// combineMetadataForCSE, or internalize and remove KnownIDs parameter.
void llvm::combineMetadata(Instruction *K, const Instruction *J,
ArrayRef<unsigned> KnownIDs, bool DoesKMove) {
SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
Expand All @@ -3320,6 +3323,10 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J,

switch (Kind) {
default:
// FIXME: https://github.com/llvm/llvm-project/issues/121495
// Change to removing only explicitly listed other metadata, and assert
// on unknown metadata, to avoid inadvertently dropping newly added
// metadata types.
K->setMetadata(Kind, nullptr); // Remove unknown metadata
break;
case LLVMContext::MD_dbg:
Expand Down Expand Up @@ -3380,8 +3387,10 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J,
MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
break;
case LLVMContext::MD_memprof:
K->setMetadata(Kind, MDNode::getMergedMemProfMetadata(KMD, JMD));
break;
case LLVMContext::MD_callsite:
// Preserve !memprof and !callsite metadata on K.
K->setMetadata(Kind, MDNode::getMergedCallsiteMetadata(KMD, JMD));
break;
case LLVMContext::MD_preserve_access_index:
// Preserve !preserve.access.index in K.
Expand Down
Loading