diff --git a/llvm/include/llvm/IR/Value.h b/llvm/include/llvm/IR/Value.h --- a/llvm/include/llvm/IR/Value.h +++ b/llvm/include/llvm/IR/Value.h @@ -854,6 +854,10 @@ void setValueSubclassData(unsigned short D) { SubclassData = D; } }; +// Use this to cap local value name according to the value of +// non-global-value-max-name-size. +StringRef capLocalValueName(StringRef NameRef); + struct ValueDeleter { void operator()(Value *V) { V->deleteValue(); } }; /// Use this instead of std::unique_ptr or std::unique_ptr. diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -3116,7 +3116,10 @@ Value *LLParser::PerFunctionState::getVal(const std::string &Name, Type *Ty, LocTy Loc, bool IsCall) { // Look this name up in the normal function symbol table. - Value *Val = F.getValueSymbolTable()->lookup(Name); + // Ensure that the name is capped according to the value of + // non-global-value-max-name-size, otherwise symbol table + // lookup will fail if Name exceeds the size cap. + Value *Val = F.getValueSymbolTable()->lookup(capLocalValueName(Name)); // If this is a forward reference for the value, see if we already created a // forward ref record. 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 @@ -55,6 +55,12 @@ return Ty; } +StringRef llvm::capLocalValueName(StringRef NameRef) { + if (NameRef.size() > NonGlobalValueMaxNameSize) + return NameRef.substr(0, std::max(1u, (unsigned)NonGlobalValueMaxNameSize)); + return NameRef; +} + Value::Value(Type *ty, unsigned scid) : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), NumUserOperands(0), @@ -324,9 +330,8 @@ return; // Cap the size of non-GlobalValue names. - if (NameRef.size() > NonGlobalValueMaxNameSize && !isa(this)) - NameRef = - NameRef.substr(0, std::max(1u, (unsigned)NonGlobalValueMaxNameSize)); + if (!isa(this)) + NameRef = capLocalValueName(NameRef); assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 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 +}