Skip to content

Commit 3864bfd

Browse files
authored
[IR] Fix ignoring non-global-value-max-name-size in ValueSymbolTable::makeUniqueName(). (#89057)
E.g. during inlining new symbol name can be duplicated and then `ValueSymbolTable::makeUniqueName()` will add unique suffix, exceeding the `non-global-value-max-name-size` restriction. Also fixed `unsigned` type of the option to `int` since `ValueSymbolTable`' constructor can use `-1` value that means unrestricted name size.
1 parent 46a30df commit 3864bfd

File tree

5 files changed

+58
-15
lines changed

5 files changed

+58
-15
lines changed

llvm/lib/IR/Function.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ using ProfileCount = Function::ProfileCount;
7979
// are not in the public header file...
8080
template class llvm::SymbolTableListTraits<BasicBlock>;
8181

82-
static cl::opt<unsigned> NonGlobalValueMaxNameSize(
82+
static cl::opt<int> NonGlobalValueMaxNameSize(
8383
"non-global-value-max-name-size", cl::Hidden, cl::init(1024),
8484
cl::desc("Maximum size for the name of non-global values."));
8585

llvm/lib/IR/ValueSymbolTable.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,34 @@ ValueSymbolTable::~ValueSymbolTable() {
4343
ValueName *ValueSymbolTable::makeUniqueName(Value *V,
4444
SmallString<256> &UniqueName) {
4545
unsigned BaseSize = UniqueName.size();
46+
bool AppenDot = false;
47+
if (auto *GV = dyn_cast<GlobalValue>(V)) {
48+
// A dot is appended to mark it as clone during ABI demangling so that
49+
// for example "_Z1fv" and "_Z1fv.1" both demangle to "f()", the second
50+
// one being a clone.
51+
// On NVPTX we cannot use a dot because PTX only allows [A-Za-z0-9_$] for
52+
// identifiers. This breaks ABI demangling but at least ptxas accepts and
53+
// compiles the program.
54+
const Module *M = GV->getParent();
55+
if (!(M && Triple(M->getTargetTriple()).isNVPTX()))
56+
AppenDot = true;
57+
}
58+
4659
while (true) {
4760
// Trim any suffix off and append the next number.
4861
UniqueName.resize(BaseSize);
4962
raw_svector_ostream S(UniqueName);
50-
if (auto *GV = dyn_cast<GlobalValue>(V)) {
51-
// A dot is appended to mark it as clone during ABI demangling so that
52-
// for example "_Z1fv" and "_Z1fv.1" both demangle to "f()", the second
53-
// one being a clone.
54-
// On NVPTX we cannot use a dot because PTX only allows [A-Za-z0-9_$] for
55-
// identifiers. This breaks ABI demangling but at least ptxas accepts and
56-
// compiles the program.
57-
const Module *M = GV->getParent();
58-
if (!(M && Triple(M->getTargetTriple()).isNVPTX()))
59-
S << ".";
60-
}
63+
if (AppenDot)
64+
S << ".";
6165
S << ++LastUnique;
6266

67+
// Retry if MaxNameSize has been exceeded.
68+
if (MaxNameSize > -1 && UniqueName.size() > (size_t)MaxNameSize) {
69+
assert(BaseSize >= UniqueName.size() - (size_t)MaxNameSize &&
70+
"Can't generate unique name: MaxNameSize is too small.");
71+
BaseSize -= UniqueName.size() - (size_t)MaxNameSize;
72+
continue;
73+
}
6374
// Try insert the vmap entry with this suffix.
6475
auto IterBool = vmap.insert(std::make_pair(UniqueName.str(), V));
6576
if (IterBool.second)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: opt < %s -S -passes='always-inline' -non-global-value-max-name-size=5 | opt -non-global-value-max-name-size=5 -passes=verify -disable-output
2+
3+
; Opt should not generate too long name for labels during inlining.
4+
5+
define internal i32 @inner(i32 %flag) alwaysinline {
6+
entry:
7+
%icmp = icmp slt i32 %flag, 0
8+
br i1 %icmp, label %one, label %two
9+
10+
one:
11+
ret i32 42
12+
13+
two:
14+
ret i32 44
15+
}
16+
17+
define i32 @outer(i32 %x) {
18+
entry:
19+
%call1 = call i32 @inner(i32 %x)
20+
%call2 = call i32 @inner(i32 %x)
21+
%ret = add i32 %call1, %call2
22+
ret i32 %ret
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; REQUIRES: asserts
2+
; Force the size to be small to check assertion message.
3+
; RUN: not --crash opt -S %s -O2 -o - -non-global-value-max-name-size=0 2>&1 | FileCheck %s
4+
; CHECK: Can't generate unique name: MaxNameSize is too small.
5+
6+
define i32 @f(i32 %a, i32 %b) {
7+
%c = add i32 %a, %b
8+
%d = add i32 %c, %a
9+
%e = add i32 %d, %b
10+
ret i32 %e
11+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
; Check the size of generated variable when no option is set
22
; RUN: opt -S %s -O2 -o - | FileCheck -check-prefix=CHECK-LONG %s
3+
; RUN: opt -S %s -O2 -o - -non-global-value-max-name-size=-1 | FileCheck -check-prefix=CHECK-LONG %s
34
; CHECK-LONG: %{{[a-z]{4}[a-z]+}}
45

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

@@ -14,5 +14,3 @@ define i32 @f(i32 %a, i32 %b) {
1414
%e = add i32 %d, %b
1515
ret i32 %e
1616
}
17-
18-

0 commit comments

Comments
 (0)