Skip to content

Commit 51bd42e

Browse files
committed
[Attributor] Enable heap-to-stack of any size
Enable Attributor's heap-to-stack to lower unbounded allocations given a max size of -1 Differential Revision: https://reviews.llvm.org/D97873
1 parent 1c2e7d2 commit 51bd42e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5166,13 +5166,25 @@ ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
51665166
}
51675167

51685168
if (IsMalloc) {
5169+
if (MaxHeapToStackSize == -1) {
5170+
if (UsesCheck(I) || FreeCheck(I)) {
5171+
MallocCalls.insert(&I);
5172+
return true;
5173+
}
5174+
}
51695175
if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
51705176
if (Size->getValue().ule(MaxHeapToStackSize))
51715177
if (UsesCheck(I) || FreeCheck(I)) {
51725178
MallocCalls.insert(&I);
51735179
return true;
51745180
}
51755181
} else if (IsAlignedAllocLike && isa<ConstantInt>(I.getOperand(0))) {
5182+
if (MaxHeapToStackSize == -1) {
5183+
if (UsesCheck(I) || FreeCheck(I)) {
5184+
MallocCalls.insert(&I);
5185+
return true;
5186+
}
5187+
}
51765188
// Only if the alignment and sizes are constant.
51775189
if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
51785190
if (Size->getValue().ule(MaxHeapToStackSize))
@@ -5181,6 +5193,12 @@ ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
51815193
return true;
51825194
}
51835195
} else if (IsCalloc) {
5196+
if (MaxHeapToStackSize == -1) {
5197+
if (UsesCheck(I) || FreeCheck(I)) {
5198+
MallocCalls.insert(&I);
5199+
return true;
5200+
}
5201+
}
51845202
bool Overflow = false;
51855203
if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
51865204
if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes
2+
; RUN: opt -max-heap-to-stack-size=-1 -attributor -enable-new-pm=0 -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=1 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM
3+
; RUN: opt -max-heap-to-stack-size=-1 -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=1 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM
4+
; RUN: opt -max-heap-to-stack-size=-1 -attributor-cgscc -enable-new-pm=0 -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_NPM,IS__CGSCC____,IS________OPM,IS__CGSCC_OPM
5+
; RUN: opt -max-heap-to-stack-size=-1 -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_OPM,IS__CGSCC____,IS________NPM,IS__CGSCC_NPM
6+
7+
declare i64 @subfn(i8*) #0
8+
9+
declare noalias i8* @malloc(i64)
10+
declare void @free(i8*)
11+
12+
define i64 @f(i64 %len) {
13+
; IS________OPM-LABEL: define {{[^@]+}}@f
14+
; IS________OPM-SAME: (i64 [[LEN:%.*]]) {
15+
; IS________OPM-NEXT: entry:
16+
; IS________OPM-NEXT: [[MEM:%.*]] = call noalias i8* @malloc(i64 [[LEN]])
17+
; IS________OPM-NEXT: [[RES:%.*]] = call i64 @subfn(i8* [[MEM]]) [[ATTR1:#.*]]
18+
; IS________OPM-NEXT: call void @free(i8* [[MEM]])
19+
; IS________OPM-NEXT: ret i64 [[RES]]
20+
;
21+
; IS________NPM-LABEL: define {{[^@]+}}@f
22+
; IS________NPM-SAME: (i64 [[LEN:%.*]]) {
23+
; IS________NPM-NEXT: entry:
24+
; IS________NPM-NEXT: [[TMP0:%.*]] = alloca i8, i64 [[LEN]], align 1
25+
; IS________NPM-NEXT: [[RES:%.*]] = call i64 @subfn(i8* [[TMP0]]) [[ATTR1:#.*]]
26+
; IS________NPM-NEXT: ret i64 [[RES]]
27+
;
28+
entry:
29+
%mem = call i8* @malloc(i64 %len)
30+
%res = call i64 @subfn(i8* %mem)
31+
call void @free(i8* %mem)
32+
ret i64 %res
33+
}
34+
35+
attributes #0 = { nounwind willreturn }

0 commit comments

Comments
 (0)