[llvm] [LLVM-Reduce] - Distinct Metadata Reduction (PR #96072)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 25 23:34:55 PDT 2024
================
@@ -0,0 +1,150 @@
+//===- 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 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 (!NodesToTraverse.empty()) {
+ MDNode *CurrentNode = NodesToTraverse.front();
+ NodesToTraverse.pop();
+
+ // Mark the nodes for removal
+ 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())
----------------
arsenm wrote:
Should never use static_cast, and use the llvm cast operation. Also, avoid repeating the cast so many times.
https://github.com/llvm/llvm-project/pull/96072
More information about the llvm-commits
mailing list