[PATCH] D102707: Fix non-global-value-max-name-size not considered by LLParser

Hasyimi Bahrudin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 19 03:20:03 PDT 2021


hasyimibhar updated this revision to Diff 346381.
hasyimibhar added a comment.

Fix clang-tidy error


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102707/new/

https://reviews.llvm.org/D102707

Files:
  llvm/include/llvm/IR/Value.h
  llvm/lib/AsmParser/LLParser.cpp
  llvm/lib/IR/Value.cpp
  llvm/test/Assembler/non-global-value-max-name-size.ll


Index: llvm/test/Assembler/non-global-value-max-name-size.ll
===================================================================
--- /dev/null
+++ 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
+}
Index: llvm/lib/IR/Value.cpp
===================================================================
--- llvm/lib/IR/Value.cpp
+++ 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<GlobalValue>(this))
-    NameRef =
-        NameRef.substr(0, std::max(1u, (unsigned)NonGlobalValueMaxNameSize));
+  if (!isa<GlobalValue>(this))
+    NameRef = capLocalValueName(NameRef);
 
   assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
 
Index: llvm/lib/AsmParser/LLParser.cpp
===================================================================
--- llvm/lib/AsmParser/LLParser.cpp
+++ 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.
Index: llvm/include/llvm/IR/Value.h
===================================================================
--- llvm/include/llvm/IR/Value.h
+++ 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<Value> or std::unique_ptr<Instruction>.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102707.346381.patch
Type: text/x-patch
Size: 2811 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210519/dfd2ebb1/attachment.bin>


More information about the llvm-commits mailing list