Skip to content

Commit

Permalink
Limit size of non-GlobalValue name
Browse files Browse the repository at this point in the history
Otherwise, in some extreme test case, very long names are created and the
compiler consumes large amount of memory. Size limit is set to a relatively
high value not to disturb debugging.

Compiler flag -non-global-value-max-name-size=<value> can be used to customize
the size.

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

llvm-svn: 321886
  • Loading branch information
serge-sans-paille-qb committed Jan 5, 2018
1 parent 9e52e50 commit 4c97557
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions llvm/lib/IR/Value.cpp
Expand Up @@ -39,6 +39,10 @@

using namespace llvm;

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
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -244,6 +248,11 @@ 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.
Expand Down
18 changes: 18 additions & 0 deletions llvm/test/Bitcode/value-with-long-name.ll
@@ -0,0 +1,18 @@
; Check the size of generated variable when no option is set
; RUN: opt -S %s -O2 -o - | FileCheck -check-prefix=CHECK-LONG %s
; CHECK-LONG: %{{[a-z]{4}[a-z]+}}

; Then check we correctly cap the size of newly generated non-global values name
; Force the size to be small so that the check works on release and debug build
; RUN: opt -S %s -O2 -o - -non-global-value-max-name-size=0 | FileCheck -check-prefix=CHECK-SHORT %s
; RUN: opt -S %s -O2 -o - -non-global-value-max-name-size=1 | FileCheck -check-prefix=CHECK-SHORT %s
; CHECK-SHORT-NOT: %{{[a-z][a-z]+}}

define i32 @f(i32 %a, i32 %b) {
%c = add i32 %a, %b
%d = add i32 %c, %a
%e = add i32 %d, %b
ret i32 %e
}


0 comments on commit 4c97557

Please sign in to comment.