diff --git a/llvm/include/llvm/IR/ValueSymbolTable.h b/llvm/include/llvm/IR/ValueSymbolTable.h --- a/llvm/include/llvm/IR/ValueSymbolTable.h +++ b/llvm/include/llvm/IR/ValueSymbolTable.h @@ -60,18 +60,24 @@ /// @name Constructors /// @{ - ValueSymbolTable() : vmap(0) {} + ValueSymbolTable(unsigned MaxNameSize = 0) + : 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 > 0 && Name.size() > maxNameSize) + Name = Name.substr(0, std::max(1u, maxNameSize)); + + return vmap.lookup(Name); + } /// @returns true iff the symbol table is empty /// Determine if the symbol table is empty @@ -128,6 +134,8 @@ /// @{ ValueMap vmap; ///< The map that holds the symbol table. + unsigned 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 --- 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 @@ -76,6 +77,10 @@ // are not in the public header file... template class llvm::SymbolTableListTraits; +static cl::opt 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 //===----------------------------------------------------------------------===// @@ -385,7 +390,7 @@ // We only need a symbol table for a function if the context keeps value names if (!getContext().shouldDiscardValueNames()) - SymTab = std::make_unique(); + SymTab = std::make_unique(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 --- a/llvm/lib/IR/Module.cpp +++ b/llvm/lib/IR/Module.cpp @@ -72,7 +72,7 @@ // Module::Module(StringRef MID, LLVMContext &C) - : Context(C), ValSymTab(std::make_unique()), + : Context(C), ValSymTab(std::make_unique(0)), 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 --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -42,11 +42,6 @@ "use-dereferenceable-at-point-semantics", cl::Hidden, cl::init(false), cl::desc("Deref attributes and metadata infer facts at definition only")); - -static cl::opt 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 @@ if (getName() == NameRef) return; - // Cap the size of non-GlobalValue names. - if (NameRef.size() > NonGlobalValueMaxNameSize && !isa(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 --- a/llvm/lib/IR/ValueSymbolTable.cpp +++ b/llvm/lib/IR/ValueSymbolTable.cpp @@ -100,6 +100,9 @@ /// 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 > 0 && Name.size() > maxNameSize) + Name = Name.substr(0, std::max(1u, 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 --- /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 +}