[llvm] [LLVM-Reduce] - Distinct Metadata Reduction (PR #96072)
Robert Barinov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 21 05:58:14 PDT 2024
https://github.com/rbintel updated https://github.com/llvm/llvm-project/pull/96072
>From 211101936383973e0895f8ca1570c4b52acab335 Mon Sep 17 00:00:00 2001
From: "Barinov, Robert" <robert.barinov at intel.com>
Date: Wed, 19 Jun 2024 15:48:15 +0200
Subject: [PATCH 1/2] [LLVM-Reduce] - Distinct Metadata Reduction
* Add a distinct metadata reduction pass, which traverses the whole
unnamed metadata tree and applies reduction where possible. Previous
version could do this only partially, either removing named metadata,
metadata attached to instructions or debug information.
* Modify current named node operand reduction, make it more aggressive
by generalizing the algorithm instead of reducing hard-coded
instructions, I see no issue in trying a more aggressive reduction
and rolling it back in case it doesn't go through.
* Refactor some of the tests to suit new functionality:
- Remove the --abort-on-invalid-reduction flag from remove-dp-values.ll
tests, if it is included, the new named metadata reduction algorithm
will fail at some point, if not, the test passes, valid IR is generated
and the module is reduced, which costs a few more iterations.
- Refactor remove-metadata.ll, the new functionality will not only
erase the top level nodes but also their children nodes, so one can't
expect them to be present after the run.
- Refactor remove-named-metadata.ll, the expected behaviour now is to
also remove the operands of !some.unknown.named.
- Add a test for the new functionality, expected behaviour is to leave
no boring nodes and all interesting nodes.
---
.../Inputs/reduce-distinct-metadata.py | 19 +++
.../llvm-reduce/Inputs/remove-metadata.py | 2 +-
.../llvm-reduce/reduce-distinct-metadata.ll | 28 ++++
.../llvm-reduce/reduce-named-metadata.ll | 3 -
.../tools/llvm-reduce/remove-dp-values.ll | 4 +-
.../test/tools/llvm-reduce/remove-metadata.ll | 5 +-
llvm/tools/llvm-reduce/CMakeLists.txt | 1 +
llvm/tools/llvm-reduce/DeltaManager.cpp | 4 +-
.../deltas/ReduceDistinctMetadata.cpp | 146 ++++++++++++++++++
.../deltas/ReduceDistinctMetadata.h | 24 +++
.../llvm-reduce/deltas/ReduceMetadata.cpp | 28 +---
11 files changed, 233 insertions(+), 31 deletions(-)
create mode 100644 llvm/test/tools/llvm-reduce/Inputs/reduce-distinct-metadata.py
mode change 100755 => 100644 llvm/test/tools/llvm-reduce/Inputs/remove-metadata.py
create mode 100644 llvm/test/tools/llvm-reduce/reduce-distinct-metadata.ll
create mode 100644 llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp
create mode 100644 llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.h
diff --git a/llvm/test/tools/llvm-reduce/Inputs/reduce-distinct-metadata.py b/llvm/test/tools/llvm-reduce/Inputs/reduce-distinct-metadata.py
new file mode 100644
index 0000000000000..e80ff6ea33a7a
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/Inputs/reduce-distinct-metadata.py
@@ -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)
\ No newline at end of file
diff --git a/llvm/test/tools/llvm-reduce/Inputs/remove-metadata.py b/llvm/test/tools/llvm-reduce/Inputs/remove-metadata.py
old mode 100755
new mode 100644
index 37af33314853c..56117715e8c64
--- a/llvm/test/tools/llvm-reduce/Inputs/remove-metadata.py
+++ b/llvm/test/tools/llvm-reduce/Inputs/remove-metadata.py
@@ -5,4 +5,4 @@
if "!interesting" in line:
sys.exit(0)
-sys.exit(1)
+sys.exit(1)
\ No newline at end of file
diff --git a/llvm/test/tools/llvm-reduce/reduce-distinct-metadata.ll b/llvm/test/tools/llvm-reduce/reduce-distinct-metadata.ll
new file mode 100644
index 0000000000000..cef42e213b4ab
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/reduce-distinct-metadata.ll
@@ -0,0 +1,28 @@
+; Test that every boring node is removed and all interesting distinct nodes remain.
+
+; RUN: llvm-reduce --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"}
\ No newline at end of file
diff --git a/llvm/test/tools/llvm-reduce/reduce-named-metadata.ll b/llvm/test/tools/llvm-reduce/reduce-named-metadata.ll
index 88b60560507b4..9cc8ad4f64511 100644
--- a/llvm/test/tools/llvm-reduce/reduce-named-metadata.ll
+++ b/llvm/test/tools/llvm-reduce/reduce-named-metadata.ll
@@ -23,7 +23,6 @@
; RESULT: !opencl.used.extensions = !{![[OCL_EXTENSION:[0-9]+]]}
; RESULT: !opencl.used.optional.core.features = !{![[OCL_OPTIONAL_CORE_FEATURE:[0-9]+]]}
; RESULT: !opencl.compiler.options = !{![[OCL_COMPILER_OPTION:[0-9]+]]}
-; RESULT: !some.unknown.named = !{![[UNKNOWN_0:[0-9]+]], ![[UNKNOWN_1:[0-9]+]]}
; RESULT: ![[LLVM_IDENT]] = !{!"some llvm version 0"}
@@ -32,8 +31,6 @@
; RESULT: ![[OCL_EXTENSION]] = !{!"some ocl extension 1"}
; RESULT: ![[OCL_OPTIONAL_CORE_FEATURE]] = !{!"some ocl optional core feature 1"}
; RESULT: ![[OCL_COMPILER_OPTION]] = !{!"some ocl compiler option 1"}
-; RESULT: ![[UNKNOWN_0]] = !{!"some unknown option 0"}
-; RESULT: ![[UNKNOWN_1]] = !{!"some unknown option 1"}
!llvm.ident = !{!0, !1, !0}
!opencl.spir.version = !{!2, !3}
diff --git a/llvm/test/tools/llvm-reduce/remove-dp-values.ll b/llvm/test/tools/llvm-reduce/remove-dp-values.ll
index ab9cff4ae7686..56733ef6206ce 100644
--- a/llvm/test/tools/llvm-reduce/remove-dp-values.ll
+++ b/llvm/test/tools/llvm-reduce/remove-dp-values.ll
@@ -1,8 +1,8 @@
-; RUN: llvm-reduce --abort-on-invalid-reduction --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck --check-prefixes=CHECK-FINAL --input-file=%t %s --implicit-check-not=#dbg_value
; RUN: opt < %s -S --write-experimental-debuginfo=false > %t.intrinsics.ll
-; RUN: llvm-reduce --abort-on-invalid-reduction --test FileCheck --test-arg --check-prefixes=INTRINSIC-INTERESTINGNESS --test-arg %s --test-arg --input-file %t.intrinsics.ll -o %t
+; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=INTRINSIC-INTERESTINGNESS --test-arg %s --test-arg --input-file %t.intrinsics.ll -o %t
; RUN: FileCheck --check-prefixes=INTRINSIC-FINAL --input-file=%t %s --implicit-check-not=#dbg_value
; Test that we can, in RemoveDIs mode / DbgVariableRecords mode (where variable location
diff --git a/llvm/test/tools/llvm-reduce/remove-metadata.ll b/llvm/test/tools/llvm-reduce/remove-metadata.ll
index 51a50ca20a985..500465a883929 100644
--- a/llvm/test/tools/llvm-reduce/remove-metadata.ll
+++ b/llvm/test/tools/llvm-reduce/remove-metadata.ll
@@ -2,7 +2,7 @@
; 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: FileCheck -implicit-check-not=! %s < %t
@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"}
diff --git a/llvm/tools/llvm-reduce/CMakeLists.txt b/llvm/tools/llvm-reduce/CMakeLists.txt
index a4c605fcd2443..b8ad6f71b41e5 100644
--- a/llvm/tools/llvm-reduce/CMakeLists.txt
+++ b/llvm/tools/llvm-reduce/CMakeLists.txt
@@ -31,6 +31,7 @@ add_llvm_tool(llvm-reduce
deltas/ReduceAttributes.cpp
deltas/ReduceBasicBlocks.cpp
deltas/ReduceDIMetadata.cpp
+ deltas/ReduceDistinctMetadata.cpp
deltas/ReduceDbgRecords.cpp
deltas/ReduceFunctionBodies.cpp
deltas/ReduceFunctions.cpp
diff --git a/llvm/tools/llvm-reduce/DeltaManager.cpp b/llvm/tools/llvm-reduce/DeltaManager.cpp
index 67fbc2fdc7ad4..624b5306bc71b 100644
--- a/llvm/tools/llvm-reduce/DeltaManager.cpp
+++ b/llvm/tools/llvm-reduce/DeltaManager.cpp
@@ -21,6 +21,7 @@
#include "deltas/ReduceBasicBlocks.h"
#include "deltas/ReduceDIMetadata.h"
#include "deltas/ReduceDbgRecords.h"
+#include "deltas/ReduceDistinctMetadata.h"
#include "deltas/ReduceFunctionBodies.h"
#include "deltas/ReduceFunctions.h"
#include "deltas/ReduceGlobalObjects.h"
@@ -93,6 +94,7 @@ static cl::list<std::string>
DELTA_PASS("global-variables", reduceGlobalsDeltaPass) \
DELTA_PASS("di-metadata", reduceDIMetadataDeltaPass) \
DELTA_PASS("dbg-records", reduceDbgRecordDeltaPass) \
+ DELTA_PASS("distinct-metadata", reduceDistinctMetadataDeltaPass) \
DELTA_PASS("metadata", reduceMetadataDeltaPass) \
DELTA_PASS("named-metadata", reduceNamedMetadataDeltaPass) \
DELTA_PASS("arguments", reduceArgumentsDeltaPass) \
@@ -112,7 +114,7 @@ static cl::list<std::string>
DELTA_PASS("atomic-ordering", reduceAtomicOrderingDeltaPass) \
DELTA_PASS("syncscopes", reduceAtomicSyncScopesDeltaPass) \
DELTA_PASS("instruction-flags", reduceInstructionFlagsDeltaPass) \
-} while (false)
+ } while (false)
#define DELTA_PASSES_MIR \
do { \
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp b/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp
new file mode 100644
index 0000000000000..7e0f79204b358
--- /dev/null
+++ b/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp
@@ -0,0 +1,146 @@
+//===- ReduceDistinctMetadata.cpp - 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 unnamed distinct metadata nodes.
+//
+//===------------------------------------------------------------------------------===//
+
+#include "ReduceDistinctMetadata.h"
+#include "Delta.h"
+#include "llvm/ADT/Sequence.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/IR/InstIterator.h"
+#include <algorithm>
+#include <queue>
+#include <set>
+
+using namespace llvm;
+
+// Traverse the graph breadth-first and try to remove unnamed metadata nodes
+void BFS_removal(MDNode *root,
+ std::set<std::pair<unsigned int, MDNode *>> &nodesToDelete,
+ MDNode *tmp, Oracle &O, Module &Program) {
+ std::queue<MDNode *> q{};
+ std::set<MDNode *>
+ visited{}; // Keep track of visited nodes not to get into loops
+ q.push(root);
+
+ while (!q.empty()) {
+ MDNode *current = q.front();
+ q.pop();
+
+ // Mark the nodes for removal
+ for (unsigned int i = 0; i < current->getNumOperands(); ++i) {
+ Metadata *operand = current->getOperand(i).get();
+ if (isa_and_nonnull<MDNode>(operand)) {
+ if (std::find(visited.begin(), visited.end(), operand) ==
+ visited.end()) { // Check whether node has been visited
+ q.push(static_cast<MDNode *>(operand));
+ visited.insert(static_cast<MDNode *>(operand));
+ }
+ // Delete the node only if it is distinct
+ if (static_cast<MDNode *>(operand)->isDistinct())
+ nodesToDelete.insert(
+ std::make_pair(i, current)); // Add to removal list
+ }
+ }
+
+ // Remove the nodes
+ for (std::pair<unsigned int, MDNode *> node : nodesToDelete) {
+ if (!O.shouldKeep())
+ node.second->replaceOperandWith(node.first, tmp);
+ }
+ nodesToDelete.clear();
+ }
+}
+
+// After reducing metadata, we need to remove references to the temporary node,
+// this is also done with BFS
+void cleanup(NamedMDNode &namedNode, MDTuple *tmp, Module &Program) {
+ std::queue<MDTuple *> q{};
+ std::set<MDTuple *> visited{};
+
+ // 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 (std::find(visited.begin(), visited.end(), *i) == visited.end()) {
+ q.push(static_cast<MDTuple *>(*i));
+ visited.insert(static_cast<MDTuple *>(*i));
+ }
+ }
+
+ while (!q.empty()) {
+ MDTuple *current = q.front();
+ q.pop();
+
+ // Shift all of the interesting elements to the left, pop remaining
+ // afterwards
+ if (current->isDistinct()) { // Do resizing and cleaning operations only if
+ // the node is distinct, as resizing is not
+ // supported for unique nodes and is redundant.
+ unsigned int notToRemove = 0;
+ for (unsigned int i = 0; i < current->getNumOperands(); ++i) {
+ Metadata *operand = current->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 != tmp) {
+ Metadata *temporary = current->getOperand(notToRemove).get();
+ current->replaceOperandWith(notToRemove, operand);
+ current->replaceOperandWith(i, temporary);
+ ++notToRemove;
+ }
+ }
+
+ // Remove all the uninteresting elements
+ unsigned int originalOperands = current->getNumOperands();
+ for (unsigned int i = 0; i < originalOperands - notToRemove; ++i)
+ current->pop_back();
+ }
+
+ // Push the remaining nodes into the queue
+ for (unsigned int i = 0; i < current->getNumOperands(); ++i) {
+ Metadata *operand = current->getOperand(i).get();
+ if (isa_and_nonnull<MDTuple>(operand) &&
+ std::find(visited.begin(), visited.end(), operand) == visited.end()) {
+ q.push(static_cast<MDTuple *>(operand));
+ visited.insert(static_cast<MDTuple *>(
+ operand)); // If the node hasn't been traversed yet, add it to the
+ // queue of nodes to traverse.
+ }
+ }
+ }
+}
+
+static void extractDistinctMetadataFromModule(Oracle &O,
+ ReducerWorkItem &WorkItem) {
+ Module &Program = WorkItem.getModule();
+ MDTuple *tmp = MDTuple::getDistinct(
+ Program.getContext(), SmallVector<Metadata *, 1>{llvm::MDString::get(
+ Program.getContext(), "temporary_node")});
+ std::set<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..
+ Metadata *operand = namedNode.getOperand(i);
+ if (isa_and_nonnull<MDTuple>(operand))
+ BFS_removal(static_cast<MDTuple *>(operand), nodesToDelete, tmp, O,
+ Program);
+ }
+ }
+ for (NamedMDNode &namedNode : Program.named_metadata())
+ cleanup(namedNode, tmp, Program);
+}
+
+void llvm::reduceDistinctMetadataDeltaPass(TestRunner &Test) {
+ runDeltaPass(Test, extractDistinctMetadataFromModule,
+ "Reducing Distinct Metadata");
+}
\ No newline at end of file
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.h b/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.h
new file mode 100644
index 0000000000000..1d72d60ea5964
--- /dev/null
+++ b/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.h
@@ -0,0 +1,24 @@
+//===- 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
\ No newline at end of file
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp b/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
index 30bf612fe9694..8212d33f23227 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
@@ -28,40 +28,26 @@ static bool shouldKeepDebugNamedMetadata(NamedMDNode &MD) {
return MD.getName() == "llvm.dbg.cu" && MD.getNumOperands() != 0;
}
-// Named metadata with simple list-like behavior, so that it's valid to remove
-// operands individually.
-static constexpr StringLiteral ListNamedMetadata[] = {
- "llvm.module.flags",
- "llvm.ident",
- "opencl.spir.version",
- "opencl.ocl.version",
- "opencl.used.extensions",
- "opencl.used.optional.core.features",
- "opencl.compiler.options"
-};
-
/// Remove unneeded arguments to named metadata.
static void reduceNamedMetadataOperands(Oracle &O, ReducerWorkItem &WorkItem) {
Module &M = WorkItem.getModule();
- for (StringRef MDName : ListNamedMetadata) {
- NamedMDNode *NamedNode = M.getNamedMetadata(MDName);
- if (!NamedNode)
- continue;
+ for (NamedMDNode &i : M.named_metadata()) {
+ NamedMDNode &NamedNode = i;
bool MadeChange = false;
- SmallVector<MDNode *, 16> KeptOperands;
- for (auto I : seq<unsigned>(0, NamedNode->getNumOperands())) {
+ SmallVector<MDNode *> KeptOperands;
+ for (auto I : seq<unsigned>(0, NamedNode.getNumOperands())) {
if (O.shouldKeep())
- KeptOperands.push_back(NamedNode->getOperand(I));
+ KeptOperands.push_back(NamedNode.getOperand(I));
else
MadeChange = true;
}
if (MadeChange) {
- NamedNode->clearOperands();
+ NamedNode.clearOperands();
for (MDNode *KeptOperand : KeptOperands)
- NamedNode->addOperand(KeptOperand);
+ NamedNode.addOperand(KeptOperand);
}
}
}
>From 6057cf60fcf9558d325f3e07260fcac42ea2c1ad Mon Sep 17 00:00:00 2001
From: "Barinov, Robert" <robert.barinov at intel.com>
Date: Fri, 21 Jun 2024 14:57:46 +0200
Subject: [PATCH 2/2] Code Review adjustments
---
.../Inputs/reduce-distinct-metadata.py | 16 +-
.../deltas/ReduceDistinctMetadata.cpp | 144 +++++++++---------
.../deltas/ReduceDistinctMetadata.h | 5 +-
.../llvm-reduce/deltas/ReduceMetadata.cpp | 12 +-
4 files changed, 89 insertions(+), 88 deletions(-)
diff --git a/llvm/test/tools/llvm-reduce/Inputs/reduce-distinct-metadata.py b/llvm/test/tools/llvm-reduce/Inputs/reduce-distinct-metadata.py
index e80ff6ea33a7a..605b4f825c3e6 100644
--- a/llvm/test/tools/llvm-reduce/Inputs/reduce-distinct-metadata.py
+++ b/llvm/test/tools/llvm-reduce/Inputs/reduce-distinct-metadata.py
@@ -5,15 +5,15 @@
input = open(sys.argv[1], "r").read().splitlines()
-depth_map = {"0": 1, "1": 3, "2": 3, "3": 2, "4" : 1}
+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)
+ 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)
\ No newline at end of file
+sys.exit(0)
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp b/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp
index 7e0f79204b358..b3db5ff064272 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.cpp
@@ -1,5 +1,4 @@
-//===- ReduceDistinctMetadata.cpp - Specialized Delta Pass
-//------------------------===//
+//===- ReduceDistinctMetadata.cpp - 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.
@@ -24,96 +23,101 @@
using namespace llvm;
// Traverse the graph breadth-first and try to remove unnamed metadata nodes
-void BFS_removal(MDNode *root,
- std::set<std::pair<unsigned int, MDNode *>> &nodesToDelete,
- MDNode *tmp, Oracle &O, Module &Program) {
- std::queue<MDNode *> q{};
- std::set<MDNode *>
- visited{}; // Keep track of visited nodes not to get into loops
- q.push(root);
+void reduceNodes(MDNode *Root,
+ std::set<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
+ std::set<MDNode *> VisitedNodes{};
+ NodesToTraverse.push(Root);
- while (!q.empty()) {
- MDNode *current = q.front();
- q.pop();
+ while (!NodesToTraverse.empty()) {
+ MDNode *CurrentNode = NodesToTraverse.front();
+ NodesToTraverse.pop();
// Mark the nodes for removal
- for (unsigned int i = 0; i < current->getNumOperands(); ++i) {
- Metadata *operand = current->getOperand(i).get();
- if (isa_and_nonnull<MDNode>(operand)) {
- if (std::find(visited.begin(), visited.end(), operand) ==
- visited.end()) { // Check whether node has been visited
- q.push(static_cast<MDNode *>(operand));
- visited.insert(static_cast<MDNode *>(operand));
+ for (unsigned int I = 0; I < CurrentNode->getNumOperands(); ++I) {
+ Metadata *Operand = CurrentNode->getOperand(I).get();
+ if (isa_and_nonnull<MDNode>(Operand)) {
+ // Check whether node has been visited
+ if (VisitedNodes.find(static_cast<MDNode *>(Operand)) ==
+ VisitedNodes.end()) {
+ NodesToTraverse.push(static_cast<MDNode *>(Operand));
+ VisitedNodes.insert(static_cast<MDNode *>(Operand));
}
// Delete the node only if it is distinct
- if (static_cast<MDNode *>(operand)->isDistinct())
- nodesToDelete.insert(
- std::make_pair(i, current)); // Add to removal list
+ if (static_cast<MDNode *>(Operand)->isDistinct())
+ // Add to removal list
+ NodesToDelete.insert(std::make_pair(I, CurrentNode));
}
}
// Remove the nodes
- for (std::pair<unsigned int, MDNode *> node : nodesToDelete) {
+ for (std::pair<unsigned int, MDNode *> Node : NodesToDelete) {
if (!O.shouldKeep())
- node.second->replaceOperandWith(node.first, tmp);
+ Node.second->replaceOperandWith(Node.first, TemporaryNode);
}
- nodesToDelete.clear();
+ NodesToDelete.clear();
}
}
// After reducing metadata, we need to remove references to the temporary node,
// this is also done with BFS
-void cleanup(NamedMDNode &namedNode, MDTuple *tmp, Module &Program) {
- std::queue<MDTuple *> q{};
- std::set<MDTuple *> visited{};
+void cleanUpTemporaries(NamedMDNode &NamedNode, MDTuple *TemporaryTuple,
+ Module &Program) {
+ std::queue<MDTuple *> NodesToTraverse{};
+ std::set<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) {
+ 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 (std::find(visited.begin(), visited.end(), *i) == visited.end()) {
- q.push(static_cast<MDTuple *>(*i));
- visited.insert(static_cast<MDTuple *>(*i));
+ if (VisitedNodes.find(static_cast<MDTuple *>(*I)) == VisitedNodes.end()) {
+ NodesToTraverse.push(static_cast<MDTuple *>(*I));
+ VisitedNodes.insert(static_cast<MDTuple *>(*I));
}
}
- while (!q.empty()) {
- MDTuple *current = q.front();
- q.pop();
+ while (!NodesToTraverse.empty()) {
+ MDTuple *CurrentTuple = NodesToTraverse.front();
+ NodesToTraverse.pop();
// Shift all of the interesting elements to the left, pop remaining
// afterwards
- if (current->isDistinct()) { // Do resizing and cleaning operations only if
- // the node is distinct, as resizing is not
- // supported for unique nodes and is redundant.
- unsigned int notToRemove = 0;
- for (unsigned int i = 0; i < current->getNumOperands(); ++i) {
- Metadata *operand = current->getOperand(i).get();
+ 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.
+ 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 != tmp) {
- Metadata *temporary = current->getOperand(notToRemove).get();
- current->replaceOperandWith(notToRemove, operand);
- current->replaceOperandWith(i, temporary);
- ++notToRemove;
+ 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 = current->getNumOperands();
- for (unsigned int i = 0; i < originalOperands - notToRemove; ++i)
- current->pop_back();
+ 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 < current->getNumOperands(); ++i) {
- Metadata *operand = current->getOperand(i).get();
- if (isa_and_nonnull<MDTuple>(operand) &&
- std::find(visited.begin(), visited.end(), operand) == visited.end()) {
- q.push(static_cast<MDTuple *>(operand));
- visited.insert(static_cast<MDTuple *>(
- operand)); // If the node hasn't been traversed yet, add it to the
- // queue of nodes to traverse.
+ for (unsigned int I = 0; I < CurrentTuple->getNumOperands(); ++I) {
+ Metadata *Operand = CurrentTuple->getOperand(I).get();
+ if (isa_and_nonnull<MDTuple>(Operand) &&
+ VisitedNodes.find(static_cast<MDTuple *>(Operand)) ==
+ VisitedNodes.end()) {
+ NodesToTraverse.push(static_cast<MDTuple *>(Operand));
+ // If the node hasn't been traversed yet, add it to the queue of nodes
+ // to traverse.
+ VisitedNodes.insert(static_cast<MDTuple *>(Operand));
}
}
}
@@ -122,25 +126,25 @@ void cleanup(NamedMDNode &namedNode, MDTuple *tmp, Module &Program) {
static void extractDistinctMetadataFromModule(Oracle &O,
ReducerWorkItem &WorkItem) {
Module &Program = WorkItem.getModule();
- MDTuple *tmp = MDTuple::getDistinct(
+ MDTuple *TemporaryTuple = MDTuple::getDistinct(
Program.getContext(), SmallVector<Metadata *, 1>{llvm::MDString::get(
- Program.getContext(), "temporary_node")});
- std::set<std::pair<unsigned int, MDNode *>> nodesToDelete{};
- for (NamedMDNode &namedNode :
+ Program.getContext(), "temporary_tuple")});
+ std::set<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..
- Metadata *operand = namedNode.getOperand(i);
- if (isa_and_nonnull<MDTuple>(operand))
- BFS_removal(static_cast<MDTuple *>(operand), nodesToDelete, tmp, O,
- Program);
+ for (unsigned int I = 0; I < NamedNode.getNumOperands();
+ ++I) { // Iterate over first level unnamed nodes..
+ Metadata *Operand = NamedNode.getOperand(I);
+ if (isa_and_nonnull<MDTuple>(Operand))
+ reduceNodes(static_cast<MDTuple *>(Operand), NodesToDelete,
+ TemporaryTuple, O, Program);
}
}
- for (NamedMDNode &namedNode : Program.named_metadata())
- cleanup(namedNode, tmp, Program);
+ for (NamedMDNode &NamedNode : Program.named_metadata())
+ cleanUpTemporaries(NamedNode, TemporaryTuple, Program);
}
void llvm::reduceDistinctMetadataDeltaPass(TestRunner &Test) {
runDeltaPass(Test, extractDistinctMetadataFromModule,
"Reducing Distinct Metadata");
-}
\ No newline at end of file
+}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.h b/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.h
index 1d72d60ea5964..d02e8e6107b75 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.h
+++ b/llvm/tools/llvm-reduce/deltas/ReduceDistinctMetadata.h
@@ -1,5 +1,4 @@
-//===- ReduceDistinctMetadata.h - Specialized Delta Pass
-//------------------------===//
+//===- 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.
@@ -21,4 +20,4 @@ namespace llvm {
void reduceDistinctMetadataDeltaPass(TestRunner &Test);
} // namespace llvm
-#endif
\ No newline at end of file
+#endif
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp b/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
index 8212d33f23227..b7e98442650be 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
@@ -32,22 +32,20 @@ static bool shouldKeepDebugNamedMetadata(NamedMDNode &MD) {
static void reduceNamedMetadataOperands(Oracle &O, ReducerWorkItem &WorkItem) {
Module &M = WorkItem.getModule();
- for (NamedMDNode &i : M.named_metadata()) {
- NamedMDNode &NamedNode = i;
-
+ for (NamedMDNode &I : M.named_metadata()) {
bool MadeChange = false;
SmallVector<MDNode *> KeptOperands;
- for (auto I : seq<unsigned>(0, NamedNode.getNumOperands())) {
+ for (auto J : seq<unsigned>(0, I.getNumOperands())) {
if (O.shouldKeep())
- KeptOperands.push_back(NamedNode.getOperand(I));
+ KeptOperands.push_back(I.getOperand(J));
else
MadeChange = true;
}
if (MadeChange) {
- NamedNode.clearOperands();
+ I.clearOperands();
for (MDNode *KeptOperand : KeptOperands)
- NamedNode.addOperand(KeptOperand);
+ I.addOperand(KeptOperand);
}
}
}
More information about the llvm-commits
mailing list