-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[LLVM-Reduce] - Distinct Metadata Reduction #104624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
llvm/test/tools/llvm-reduce/Inputs/reduce-distinct-metadata.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Helper script for distinct metadata reduction test | ||
|
||
import sys | ||
import re | ||
|
||
input = open(sys.argv[1], "r").read().splitlines() | ||
|
||
depth_map = {"0": 1, "1": 3, "2": 3, "3": 2, "4": 1} | ||
|
||
|
||
for i in range(len(depth_map)): | ||
counter = 0 | ||
for line in input: | ||
if re.match(rf".*interesting_{i}.*", line) != None: | ||
counter += 1 | ||
if counter != depth_map[str(i)]: | ||
sys.exit(1) | ||
|
||
sys.exit(0) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
; Test that every boring node is removed and all interesting distinct nodes remain after aggressive distinct metadata reduction. | ||
|
||
; RUN: llvm-reduce --aggressive-md --test %python --test-arg %p/Inputs/reduce-distinct-metadata.py %s -o %t | ||
; RUN: FileCheck %s < %t | ||
|
||
; CHECK-NOT: {{.*}}boring{{.*}} | ||
|
||
define void @main() { | ||
ret void | ||
} | ||
|
||
!named.metadata = !{!0, !2} | ||
!llvm.test.other.metadata = !{} | ||
|
||
!0 = distinct !{!"interesting_0", !1, !3, !4, !10, !11} | ||
!1 = distinct !{!"interesting_1", !5, !7, !"something"} | ||
!2 = distinct !{!"boring_0", !3, !4, i32 5} | ||
!3 = distinct !{!"interesting_1", !3, !4} | ||
!4 = distinct !{!"interesting_1", !6, i2 1} | ||
!5 = distinct !{!"interesting_2", !8} | ||
!6 = distinct !{!"interesting_2", !10} | ||
!7 = distinct !{!"interesting_2", !12} | ||
!8 = distinct !{!"interesting_3", !10, !9} | ||
!9 = distinct !{!"interesting_3", !11, !13} | ||
!10 = distinct !{!"boring_1", i32 50} | ||
!11 = distinct !{!"boring_1", i32 2} | ||
!12 = distinct !{!"boring_3", i2 1} | ||
!13 = distinct !{!"interesting_4"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
; Test that llvm-reduce can remove uninteresting metadata from an IR file. | ||
; The Metadata pass erases named & unnamed metadata nodes. | ||
; | ||
; RUN: llvm-reduce --test %python --test-arg %p/Inputs/remove-metadata.py %s -o %t | ||
; RUN: cat %t | FileCheck -implicit-check-not=! %s | ||
; RUN: llvm-reduce --aggressive-md --test %python --test-arg %p/Inputs/remove-metadata.py %s -o %t | ||
; RUN: FileCheck -implicit-check-not=! %s < %t | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check with and without the aggressive flag |
||
|
||
@global = global i32 0, !dbg !0 | ||
|
||
|
@@ -11,9 +11,8 @@ define void @main() !dbg !0 { | |
} | ||
|
||
!uninteresting = !{!0} | ||
; CHECK: !interesting = !{!0} | ||
; CHECK: !interesting = !{} | ||
!interesting = !{!1} | ||
|
||
!0 = !{!"uninteresting"} | ||
; CHECK: !0 = !{!"interesting"} | ||
!1 = !{!"interesting"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
//===- ReduceDistinctMetadata.cpp - Specialized Delta Pass ------------------------===// | ||
arsenm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// 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 | ||
// | ||
//===------------------------------------------------------------------------------===// | ||
// | ||
// This file implements two functions used by the Generic Delta Debugging | ||
// Algorithm, which are used to reduce unnamed distinct metadata nodes. | ||
// | ||
//===------------------------------------------------------------------------------===// | ||
|
||
#include "ReduceDistinctMetadata.h" | ||
#include "Delta.h" | ||
#include "llvm/ADT/Sequence.h" | ||
#include "llvm/ADT/SetVector.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/IR/InstIterator.h" | ||
#include <algorithm> | ||
#include <queue> | ||
|
||
using namespace llvm; | ||
|
||
// Traverse the graph breadth-first and try to remove unnamed metadata nodes | ||
void reduceNodes(MDNode *Root, | ||
rbintel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SetVector<std::pair<unsigned int, MDNode *>> &NodesToDelete, | ||
MDNode *TemporaryNode, Oracle &O, Module &Program) { | ||
std::queue<MDNode *> NodesToTraverse{}; | ||
// Keep track of visited nodes not to get into loops | ||
SetVector<MDNode *> VisitedNodes{}; | ||
NodesToTraverse.push(Root); | ||
|
||
while (!NodesToTraverse.empty()) { | ||
MDNode *CurrentNode = NodesToTraverse.front(); | ||
NodesToTraverse.pop(); | ||
|
||
// Mark the nodes for removal | ||
for (unsigned int I = 0; I < CurrentNode->getNumOperands(); ++I) { | ||
if (MDNode *Operand = | ||
dyn_cast<MDNode>(CurrentNode->getOperand(I).get())) { | ||
// Check whether node has been visited | ||
if (!VisitedNodes.contains(Operand)) { | ||
NodesToTraverse.push(Operand); | ||
VisitedNodes.insert(Operand); | ||
} | ||
// Delete the node only if it is distinct | ||
if (Operand->isDistinct()) | ||
rbintel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Add to removal list | ||
NodesToDelete.insert(std::make_pair(I, CurrentNode)); | ||
} | ||
} | ||
|
||
// Remove the nodes | ||
for (auto [PositionToReplace, Node] : NodesToDelete) { | ||
if (!O.shouldKeep()) | ||
Node->replaceOperandWith(PositionToReplace, TemporaryNode); | ||
} | ||
NodesToDelete.clear(); | ||
} | ||
} | ||
|
||
// After reducing metadata, we need to remove references to the temporary node, | ||
// this is also done with BFS | ||
void cleanUpTemporaries(NamedMDNode &NamedNode, MDTuple *TemporaryTuple, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static |
||
Module &Program) { | ||
std::queue<MDTuple *> NodesToTraverse{}; | ||
SetVector<MDTuple *> VisitedNodes{}; | ||
|
||
// Push all first level operands of the named node to the queue | ||
for (auto I = NamedNode.op_begin(); I != NamedNode.op_end(); ++I) { | ||
// If the node hasn't been traversed yet, add it to the queue of nodes to | ||
// traverse. | ||
if (MDTuple *TupleI = dyn_cast<MDTuple>((*I))) { | ||
if (!VisitedNodes.contains(TupleI)) { | ||
NodesToTraverse.push(TupleI); | ||
VisitedNodes.insert(TupleI); | ||
} | ||
} | ||
} | ||
|
||
while (!NodesToTraverse.empty()) { | ||
MDTuple *CurrentTuple = NodesToTraverse.front(); | ||
NodesToTraverse.pop(); | ||
|
||
// Shift all of the interesting elements to the left, pop remaining | ||
// afterwards | ||
if (CurrentTuple | ||
->isDistinct()) { // Do resizing and cleaning operations only if | ||
// the node is distinct, as resizing is not | ||
// supported for unique nodes and is redundant. | ||
rbintel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsigned int NotToRemove = 0; | ||
for (unsigned int I = 0; I < CurrentTuple->getNumOperands(); ++I) { | ||
Metadata *Operand = CurrentTuple->getOperand(I).get(); | ||
// If current operand is not the temporary node, move it to the front | ||
// and increase notToRemove so that it will be saved | ||
if (Operand != TemporaryTuple) { | ||
Metadata *TemporaryMetadata = | ||
CurrentTuple->getOperand(NotToRemove).get(); | ||
CurrentTuple->replaceOperandWith(NotToRemove, Operand); | ||
CurrentTuple->replaceOperandWith(I, TemporaryMetadata); | ||
++NotToRemove; | ||
} | ||
} | ||
|
||
// Remove all the uninteresting elements | ||
unsigned int OriginalOperands = CurrentTuple->getNumOperands(); | ||
for (unsigned int I = 0; I < OriginalOperands - NotToRemove; ++I) | ||
CurrentTuple->pop_back(); | ||
} | ||
|
||
// Push the remaining nodes into the queue | ||
for (unsigned int I = 0; I < CurrentTuple->getNumOperands(); ++I) { | ||
MDTuple *Operand = dyn_cast<MDTuple>(CurrentTuple->getOperand(I).get()); | ||
if (Operand && !VisitedNodes.contains(Operand)) { | ||
NodesToTraverse.push(Operand); | ||
// If the node hasn't been traversed yet, add it to the queue of nodes | ||
// to traverse. | ||
VisitedNodes.insert(Operand); | ||
} | ||
} | ||
} | ||
} | ||
|
||
static void extractDistinctMetadataFromModule(Oracle &O, | ||
ReducerWorkItem &WorkItem) { | ||
Module &Program = WorkItem.getModule(); | ||
MDTuple *TemporaryTuple = MDTuple::getDistinct( | ||
Program.getContext(), SmallVector<Metadata *, 1>{llvm::MDString::get( | ||
Program.getContext(), "temporary_tuple")}); | ||
rbintel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SetVector<std::pair<unsigned int, MDNode *>> NodesToDelete{}; | ||
for (NamedMDNode &NamedNode : | ||
Program.named_metadata()) { // Iterate over the named nodes | ||
for (unsigned int I = 0; I < NamedNode.getNumOperands(); | ||
++I) { // Iterate over first level unnamed nodes.. | ||
if (MDTuple *Operand = dyn_cast<MDTuple>(NamedNode.getOperand(I))) | ||
reduceNodes(Operand, NodesToDelete, TemporaryTuple, O, Program); | ||
} | ||
} | ||
for (NamedMDNode &NamedNode : Program.named_metadata()) | ||
cleanUpTemporaries(NamedNode, TemporaryTuple, Program); | ||
} | ||
|
||
void llvm::reduceDistinctMetadataDeltaPass(TestRunner &Test) { | ||
runDeltaPass(Test, extractDistinctMetadataFromModule, | ||
"Reducing Distinct Metadata"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//===- ReduceDistinctMetadata.h - Specialized Delta Pass ------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------------===// | ||
// | ||
// This file implements two functions used by the Generic Delta Debugging | ||
// Algorithm, which are used to reduce Metadata nodes. | ||
// | ||
//===----------------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEDISTINCTMETADATA_H | ||
#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEDISTINCTMETADATA_H | ||
|
||
#include "TestRunner.h" | ||
|
||
namespace llvm { | ||
void reduceDistinctMetadataDeltaPass(TestRunner &Test); | ||
} // namespace llvm | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.