Skip to content

Commit

Permalink
[Utils] Use NoAliasScopeDeclInst in a few more places (NFC)
Browse files Browse the repository at this point in the history
In the cloning infrastructure, only track an MDNode mapping,
without explicitly storing the Metadata mapping, same as is done
during inlining. This makes things slightly simpler.
  • Loading branch information
nikic committed Jan 24, 2021
1 parent 4cc94b7 commit 8b9df70
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 67 deletions.
5 changes: 5 additions & 0 deletions llvm/include/llvm/IR/IntrinsicInst.h
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,11 @@ class NoAliasScopeDeclInst : public IntrinsicInst {
cast<MetadataAsValue>(getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
return cast<MDNode>(MV->getMetadata());
}

void setScopeList(MDNode *ScopeList) {
setOperand(Intrinsic::NoAliasScopeDeclScopeArg,
MetadataAsValue::get(getContext(), ScopeList));
}
};

} // end namespace llvm
Expand Down
17 changes: 6 additions & 11 deletions llvm/include/llvm/Transforms/Utils/Cloning.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,41 +272,36 @@ void updateProfileCallee(
/// basic blocks and extract their scope. These are candidates for duplication
/// when cloning.
void identifyNoAliasScopesToClone(
ArrayRef<BasicBlock *> BBs,
SmallVectorImpl<MetadataAsValue *> &NoAliasDeclScopes);
ArrayRef<BasicBlock *> BBs, SmallVectorImpl<MDNode *> &NoAliasDeclScopes);

/// Duplicate the specified list of noalias decl scopes.
/// The 'Ext' string is added as an extension to the name.
/// Afterwards, the ClonedMVScopes contains a mapping of the original MV onto
/// the cloned version.
/// The ClonedScopes contains the mapping of the original scope MDNode onto the
/// cloned scope.
/// Afterwards, the ClonedScopes contains the mapping of the original scope
/// MDNode onto the cloned scope.
/// Be aware that the cloned scopes are still part of the original scope domain.
void cloneNoAliasScopes(
ArrayRef<MetadataAsValue *> NoAliasDeclScopes,
ArrayRef<MDNode *> NoAliasDeclScopes,
DenseMap<MDNode *, MDNode *> &ClonedScopes,
DenseMap<MetadataAsValue *, MetadataAsValue *> &ClonedMVScopes,
StringRef Ext, LLVMContext &Context);

/// Adapt the metadata for the specified instruction according to the
/// provided mapping. This is normally used after cloning an instruction, when
/// some noalias scopes needed to be cloned.
void adaptNoAliasScopes(
llvm::Instruction *I, const DenseMap<MDNode *, MDNode *> &ClonedScopes,
const DenseMap<MetadataAsValue *, MetadataAsValue *> &ClonedMVScopes,
LLVMContext &Context);

/// Clone the specified noalias decl scopes. Then adapt all instructions in the
/// NewBlocks basicblocks to the cloned versions.
/// 'Ext' will be added to the duplicate scope names.
void cloneAndAdaptNoAliasScopes(ArrayRef<MetadataAsValue *> NoAliasDeclScopes,
void cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
ArrayRef<BasicBlock *> NewBlocks,
LLVMContext &Context, StringRef Ext);

/// Clone the specified noalias decl scopes. Then adapt all instructions in the
/// [IStart, IEnd] (IEnd included !) range to the cloned versions. 'Ext' will be
/// added to the duplicate scope names.
void cloneAndAdaptNoAliasScopes(ArrayRef<MetadataAsValue *> NoAliasDeclScopes,
void cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
Instruction *IStart, Instruction *IEnd,
LLVMContext &Context, StringRef Ext);
} // end namespace llvm
Expand Down
78 changes: 34 additions & 44 deletions llvm/lib/Transforms/Utils/CloneFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,15 +886,13 @@ BasicBlock *llvm::DuplicateInstructionsInSplitBetween(
}

void llvm::cloneNoAliasScopes(
ArrayRef<MetadataAsValue *> NoAliasDeclScopes,
ArrayRef<MDNode *> NoAliasDeclScopes,
DenseMap<MDNode *, MDNode *> &ClonedScopes,
DenseMap<MetadataAsValue *, MetadataAsValue *> &ClonedMVScopes,
StringRef Ext, LLVMContext &Context) {
MDBuilder MDB(Context);

for (auto *MV : NoAliasDeclScopes) {
SmallVector<Metadata *, 4> ScopeList;
for (auto &MDOperand : cast<MDNode>(MV->getMetadata())->operands()) {
for (auto *ScopeList : NoAliasDeclScopes) {
for (auto &MDOperand : ScopeList->operands()) {
if (MDNode *MD = dyn_cast<MDNode>(MDOperand)) {
AliasScopeNode SNANode(MD);

Expand All @@ -908,94 +906,86 @@ void llvm::cloneNoAliasScopes(
MDNode *NewScope = MDB.createAnonymousAliasScope(
const_cast<MDNode *>(SNANode.getDomain()), Name);
ClonedScopes.insert(std::make_pair(MD, NewScope));
ScopeList.push_back(NewScope);
}
}
MDNode *NewScopeList = MDNode::get(Context, ScopeList);
ClonedMVScopes.insert(
std::make_pair(MV, MetadataAsValue::get(Context, NewScopeList)));
}
}

void llvm::adaptNoAliasScopes(
Instruction *I, const DenseMap<MDNode *, MDNode *> &ClonedScopes,
const DenseMap<MetadataAsValue *, MetadataAsValue *> &ClonedMVScopes,
LLVMContext &Context) {
// MetadataAsValue will always be replaced !
for (Use &U : I->operands())
if (MetadataAsValue *MV = dyn_cast<MetadataAsValue>(U))
if (auto *NewMV = ClonedMVScopes.lookup(MV))
U.set(NewMV);

auto replaceWhenNeeded = [&](unsigned MD_ID) {
if (const MDNode *CSNoAlias = I->getMetadata(MD_ID)) {
bool NeedsReplacement = false;
SmallVector<Metadata *, 8> NewScopeList;
for (auto &MDOp : CSNoAlias->operands()) {
if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
if (auto *NewMD = ClonedScopes.lookup(MD)) {
NewScopeList.push_back(NewMD);
NeedsReplacement = true;
continue;
}
NewScopeList.push_back(MD);
auto CloneScopeList = [&](const MDNode *ScopeList) -> MDNode * {
bool NeedsReplacement = false;
SmallVector<Metadata *, 8> NewScopeList;
for (auto &MDOp : ScopeList->operands()) {
if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
if (auto *NewMD = ClonedScopes.lookup(MD)) {
NewScopeList.push_back(NewMD);
NeedsReplacement = true;
continue;
}
NewScopeList.push_back(MD);
}
if (NeedsReplacement)
I->setMetadata(MD_ID, MDNode::get(Context, NewScopeList));
}
if (NeedsReplacement)
return MDNode::get(Context, NewScopeList);
return nullptr;
};

if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I))
if (auto *NewScopeList = CloneScopeList(Decl->getScopeList()))
Decl->setScopeList(NewScopeList);

auto replaceWhenNeeded = [&](unsigned MD_ID) {
if (const MDNode *CSNoAlias = I->getMetadata(MD_ID))
if (auto *NewScopeList = CloneScopeList(CSNoAlias))
I->setMetadata(MD_ID, NewScopeList);
};
replaceWhenNeeded(LLVMContext::MD_noalias);
replaceWhenNeeded(LLVMContext::MD_alias_scope);
}

void llvm::cloneAndAdaptNoAliasScopes(
ArrayRef<MetadataAsValue *> NoAliasDeclScopes,
ArrayRef<MDNode *> NoAliasDeclScopes,
ArrayRef<BasicBlock *> NewBlocks, LLVMContext &Context, StringRef Ext) {
if (NoAliasDeclScopes.empty())
return;

DenseMap<MDNode *, MDNode *> ClonedScopes;
DenseMap<MetadataAsValue *, MetadataAsValue *> ClonedMVScopes;
LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
<< NoAliasDeclScopes.size() << " node(s)\n");

cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, ClonedMVScopes, Ext,
Context);
cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
// Identify instructions using metadata that needs adaptation
for (BasicBlock *NewBlock : NewBlocks)
for (Instruction &I : *NewBlock)
adaptNoAliasScopes(&I, ClonedScopes, ClonedMVScopes, Context);
adaptNoAliasScopes(&I, ClonedScopes, Context);
}

void llvm::cloneAndAdaptNoAliasScopes(
ArrayRef<MetadataAsValue *> NoAliasDeclScopes, Instruction *IStart,
ArrayRef<MDNode *> NoAliasDeclScopes, Instruction *IStart,
Instruction *IEnd, LLVMContext &Context, StringRef Ext) {
if (NoAliasDeclScopes.empty())
return;

DenseMap<MDNode *, MDNode *> ClonedScopes;
DenseMap<MetadataAsValue *, MetadataAsValue *> ClonedMVScopes;
LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
<< NoAliasDeclScopes.size() << " node(s)\n");

cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, ClonedMVScopes, Ext,
Context);
cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
// Identify instructions using metadata that needs adaptation
assert(IStart->getParent() == IEnd->getParent() && "different basic block ?");
auto ItStart = IStart->getIterator();
auto ItEnd = IEnd->getIterator();
++ItEnd; // IEnd is included, increment ItEnd to get the end of the range
for (auto &I : llvm::make_range(ItStart, ItEnd))
adaptNoAliasScopes(&I, ClonedScopes, ClonedMVScopes, Context);
adaptNoAliasScopes(&I, ClonedScopes, Context);
}

void llvm::identifyNoAliasScopesToClone(
ArrayRef<BasicBlock *> BBs,
SmallVectorImpl<MetadataAsValue *> &NoAliasDeclScopes) {
ArrayRef<BasicBlock *> BBs, SmallVectorImpl<MDNode *> &NoAliasDeclScopes) {
for (BasicBlock *BB : BBs)
for (Instruction &I : *BB)
if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
NoAliasDeclScopes.push_back(cast<MetadataAsValue>(
Decl->getOperand(Intrinsic::NoAliasScopeDeclScopeArg)));
NoAliasDeclScopes.push_back(Decl->getScopeList());
}
7 changes: 2 additions & 5 deletions llvm/lib/Transforms/Utils/InlineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,11 +927,8 @@ void ScopedAliasMetadataDeepCloner::remap(ValueToValueMapTy &VMap) {
if (MDNode *M = I->getMetadata(LLVMContext::MD_noalias))
I->setMetadata(LLVMContext::MD_noalias, MDMap[M]);

if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I)) {
auto *NewMV =
MetadataAsValue::get(Decl->getContext(), MDMap[Decl->getScopeList()]);
Decl->setOperand(Intrinsic::NoAliasScopeDeclScopeArg, NewMV);
}
if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I))
Decl->setScopeList(MDMap[Decl->getScopeList()]);
}
}

Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/Transforms/Utils/LoopRotationUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
// Remember the local noalias scope declarations in the header. After the
// rotation, they must be duplicated and the scope must be cloned. This
// avoids unwanted interaction across iterations.
SmallVector<Instruction *, 6> NoAliasDeclInstructions;
SmallVector<NoAliasScopeDeclInst *, 6> NoAliasDeclInstructions;
for (Instruction &I : *OrigHeader)
if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
NoAliasDeclInstructions.push_back(Decl);
Expand Down Expand Up @@ -493,7 +493,7 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {

// Clone the llvm.experimental.noalias.decl again for the NewHeader.
Instruction *NewHeaderInsertionPoint = &(*NewHeader->getFirstNonPHI());
for (Instruction *NAD : NoAliasDeclInstructions) {
for (NoAliasScopeDeclInst *NAD : NoAliasDeclInstructions) {
LLVM_DEBUG(dbgs() << " Cloning llvm.experimental.noalias.scope.decl:"
<< *NAD << "\n");
Instruction *NewNAD = NAD->clone();
Expand All @@ -505,10 +505,9 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
{
auto &Context = NewHeader->getContext();

SmallVector<MetadataAsValue *, 8> NoAliasDeclScopes;
for (Instruction *NAD : NoAliasDeclInstructions)
NoAliasDeclScopes.push_back(cast<MetadataAsValue>(
NAD->getOperand(Intrinsic::NoAliasScopeDeclScopeArg)));
SmallVector<MDNode *, 8> NoAliasDeclScopes;
for (NoAliasScopeDeclInst *NAD : NoAliasDeclInstructions)
NoAliasDeclScopes.push_back(NAD->getScopeList());

LLVM_DEBUG(dbgs() << " Updating OrigHeader scopes\n");
cloneAndAdaptNoAliasScopes(NoAliasDeclScopes, {OrigHeader}, Context,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Utils/LoopUnroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,

// Identify what noalias metadata is inside the loop: if it is inside the
// loop, the associated metadata must be cloned for each iteration.
SmallVector<MetadataAsValue *, 6> LoopLocalNoAliasDeclScopes;
SmallVector<MDNode *, 6> LoopLocalNoAliasDeclScopes;
identifyNoAliasScopesToClone(L->getBlocks(), LoopLocalNoAliasDeclScopes);

for (unsigned It = 1; It != ULO.Count; ++It) {
Expand Down

0 comments on commit 8b9df70

Please sign in to comment.