[llvm] 8d25762 - Fix non-global-value-max-name-size not considered by LLParser

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Wed May 26 21:20:46 PDT 2021


Author: Hasyimi Bahrudin
Date: 2021-05-27T04:20:03Z
New Revision: 8d25762720660aba3344752e577ae7017e6125c2

URL: https://github.com/llvm/llvm-project/commit/8d25762720660aba3344752e577ae7017e6125c2
DIFF: https://github.com/llvm/llvm-project/commit/8d25762720660aba3344752e577ae7017e6125c2.diff

LOG: Fix non-global-value-max-name-size not considered by LLParser

`non-global-value-max-name-size` is used by `Value` to cap the length of local value name. However, this flag is not considered by `LLParser`, which leads to unexpected `use of undefined value error`. The fix is to move the responsibility of capping the length to `ValueSymbolTable`.

The test is the one provided by [[ https://bugs.llvm.org/show_bug.cgi?id=45899 | Mikael in the bug report ]].

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D102707

Added: 
    llvm/test/Assembler/non-global-value-max-name-size.ll

Modified: 
    llvm/include/llvm/IR/ValueSymbolTable.h
    llvm/lib/IR/Function.cpp
    llvm/lib/IR/Module.cpp
    llvm/lib/IR/Value.cpp
    llvm/lib/IR/ValueSymbolTable.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/ValueSymbolTable.h b/llvm/include/llvm/IR/ValueSymbolTable.h
index 105ea73857afe..43d00268f4b22 100644
--- a/llvm/include/llvm/IR/ValueSymbolTable.h
+++ b/llvm/include/llvm/IR/ValueSymbolTable.h
@@ -60,18 +60,23 @@ class ValueSymbolTable {
 /// @name Constructors
 /// @{
 
-  ValueSymbolTable() : vmap(0) {}
+  ValueSymbolTable(int MaxNameSize = -1) : vmap(0), MaxNameSize(MaxNameSize) {}
   ~ValueSymbolTable();
 
-/// @}
-/// @name Accessors
-/// @{
+  /// @}
+  /// @name Accessors
+  /// @{
 
   /// This method finds the value with the given \p Name in the
   /// the symbol table.
   /// @returns the value associated with the \p Name
   /// Lookup a named Value.
-  Value *lookup(StringRef Name) const { return vmap.lookup(Name); }
+  Value *lookup(StringRef Name) const {
+    if (MaxNameSize > -1 && Name.size() > (unsigned)MaxNameSize)
+      Name = Name.substr(0, std::max(1u, (unsigned)MaxNameSize));
+
+    return vmap.lookup(Name);
+  }
 
   /// @returns true iff the symbol table is empty
   /// Determine if the symbol table is empty
@@ -128,6 +133,8 @@ class ValueSymbolTable {
   /// @{
 
   ValueMap vmap;                    ///< The map that holds the symbol table.
+  int MaxNameSize; ///< The maximum size for each name. If the limit is
+                   ///< exceeded, the name is capped.
   mutable uint32_t LastUnique = 0;  ///< Counter for tracking unique names
 
 /// @}

diff  --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index a5c47563c47f5..29e0c64c4ea2a 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -60,6 +60,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/IR/ValueSymbolTable.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include <algorithm>
@@ -76,6 +77,10 @@ using ProfileCount = Function::ProfileCount;
 // are not in the public header file...
 template class llvm::SymbolTableListTraits<BasicBlock>;
 
+static cl::opt<unsigned> NonGlobalValueMaxNameSize(
+    "non-global-value-max-name-size", cl::Hidden, cl::init(1024),
+    cl::desc("Maximum size for the name of non-global values."));
+
 //===----------------------------------------------------------------------===//
 // Argument Implementation
 //===----------------------------------------------------------------------===//
@@ -391,7 +396,7 @@ Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace,
 
   // We only need a symbol table for a function if the context keeps value names
   if (!getContext().shouldDiscardValueNames())
-    SymTab = std::make_unique<ValueSymbolTable>();
+    SymTab = std::make_unique<ValueSymbolTable>(NonGlobalValueMaxNameSize);
 
   // If the function has arguments, mark them as lazily built.
   if (Ty->getNumParams())

diff  --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index d38b2d1de2464..eae4e69e76f70 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -72,7 +72,7 @@ template class llvm::SymbolTableListTraits<GlobalIFunc>;
 //
 
 Module::Module(StringRef MID, LLVMContext &C)
-    : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>()),
+    : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>(-1)),
       Materializer(), ModuleID(std::string(MID)),
       SourceFileName(std::string(MID)), DL("") {
   Context.addModule(this);

diff  --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index d5587b42e6c0f..33b61c900e70f 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -42,11 +42,6 @@ static cl::opt<unsigned> UseDerefAtPointSemantics(
     "use-dereferenceable-at-point-semantics", cl::Hidden, cl::init(false),
     cl::desc("Deref attributes and metadata infer facts at definition only"));
 
-
-static cl::opt<unsigned> NonGlobalValueMaxNameSize(
-    "non-global-value-max-name-size", cl::Hidden, cl::init(1024),
-    cl::desc("Maximum size for the name of non-global values."));
-
 //===----------------------------------------------------------------------===//
 //                                Value Class
 //===----------------------------------------------------------------------===//
@@ -323,11 +318,6 @@ void Value::setNameImpl(const Twine &NewName) {
   if (getName() == NameRef)
     return;
 
-  // Cap the size of non-GlobalValue names.
-  if (NameRef.size() > NonGlobalValueMaxNameSize && !isa<GlobalValue>(this))
-    NameRef =
-        NameRef.substr(0, std::max(1u, (unsigned)NonGlobalValueMaxNameSize));
-
   assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
 
   // Get the symbol table to update for this object.

diff  --git a/llvm/lib/IR/ValueSymbolTable.cpp b/llvm/lib/IR/ValueSymbolTable.cpp
index b49842315f36f..6e5330ecc5f89 100644
--- a/llvm/lib/IR/ValueSymbolTable.cpp
+++ b/llvm/lib/IR/ValueSymbolTable.cpp
@@ -100,6 +100,9 @@ void ValueSymbolTable::removeValueName(ValueName *V) {
 /// it into the symbol table with the specified name.  If it conflicts, it
 /// auto-renames the name and returns that instead.
 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
+  if (MaxNameSize > -1 && Name.size() > (unsigned)MaxNameSize)
+    Name = Name.substr(0, std::max(1u, (unsigned)MaxNameSize));
+
   // In the common case, the name is not already in the symbol table.
   auto IterBool = vmap.insert(std::make_pair(Name, V));
   if (IterBool.second) {

diff  --git a/llvm/test/Assembler/non-global-value-max-name-size.ll b/llvm/test/Assembler/non-global-value-max-name-size.ll
new file mode 100644
index 0000000000000..4536ce2056862
--- /dev/null
+++ b/llvm/test/Assembler/non-global-value-max-name-size.ll
@@ -0,0 +1,10 @@
+; RUN: opt < %s -S -non-global-value-max-name-size=4
+; Test that local value name lookup works if the name is capped
+
+define void @f() {
+bb0:
+  br label %testz
+
+testz:
+  br label %testz
+}


        


More information about the llvm-commits mailing list