Skip to content

Commit ef51514

Browse files
authored
[FunctionAttrs] Don't bail out on unknown calls (#150958)
When inferring attributes, we should not bail out early on unknown calls (such as virtual calls), as we may still have call-site attributes that can be used for inference. Fixes #150817.
1 parent 11a959b commit ef51514

File tree

4 files changed

+11
-32
lines changed

4 files changed

+11
-32
lines changed

llvm/lib/Transforms/IPO/FunctionAttrs.cpp

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,6 @@ void AttributeInferer::run(const SCCNodeSet &SCCNodes,
18631863

18641864
struct SCCNodesResult {
18651865
SCCNodeSet SCCNodes;
1866-
bool HasUnknownCall;
18671866
};
18681867

18691868
} // end anonymous namespace
@@ -2227,29 +2226,13 @@ static void addWillReturn(const SCCNodeSet &SCCNodes,
22272226

22282227
static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {
22292228
SCCNodesResult Res;
2230-
Res.HasUnknownCall = false;
22312229
for (Function *F : Functions) {
22322230
if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked) ||
22332231
F->isPresplitCoroutine()) {
2234-
// Treat any function we're trying not to optimize as if it were an
2235-
// indirect call and omit it from the node set used below.
2236-
Res.HasUnknownCall = true;
2232+
// Omit any functions we're trying not to optimize from the set.
22372233
continue;
22382234
}
2239-
// Track whether any functions in this SCC have an unknown call edge.
2240-
// Note: if this is ever a performance hit, we can common it with
2241-
// subsequent routines which also do scans over the instructions of the
2242-
// function.
2243-
if (!Res.HasUnknownCall) {
2244-
for (Instruction &I : instructions(*F)) {
2245-
if (auto *CB = dyn_cast<CallBase>(&I)) {
2246-
if (!CB->getCalledFunction()) {
2247-
Res.HasUnknownCall = true;
2248-
break;
2249-
}
2250-
}
2251-
}
2252-
}
2235+
22532236
Res.SCCNodes.insert(F);
22542237
}
22552238
return Res;
@@ -2282,15 +2265,10 @@ deriveAttrsInPostOrder(ArrayRef<Function *> Functions, AARGetterT &&AARGetter,
22822265
addColdAttrs(Nodes.SCCNodes, Changed);
22832266
addWillReturn(Nodes.SCCNodes, Changed);
22842267
addNoUndefAttrs(Nodes.SCCNodes, Changed);
2285-
2286-
// If we have no external nodes participating in the SCC, we can deduce some
2287-
// more precise attributes as well.
2288-
if (!Nodes.HasUnknownCall) {
2289-
addNoAliasAttrs(Nodes.SCCNodes, Changed);
2290-
addNonNullAttrs(Nodes.SCCNodes, Changed);
2291-
inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);
2292-
addNoRecurseAttrs(Nodes.SCCNodes, Changed);
2293-
}
2268+
addNoAliasAttrs(Nodes.SCCNodes, Changed);
2269+
addNonNullAttrs(Nodes.SCCNodes, Changed);
2270+
inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);
2271+
addNoRecurseAttrs(Nodes.SCCNodes, Changed);
22942272

22952273
// Finally, infer the maximal set of attributes from the ones we've inferred
22962274
// above. This is handling the cases where one attribute on a signature

llvm/test/Transforms/FunctionAttrs/noalias.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ define ptr @return_unknown_call(ptr %fn) {
235235
}
236236

237237
define ptr @return_unknown_noalias_call(ptr %fn) {
238-
; CHECK-LABEL: define ptr @return_unknown_noalias_call(
238+
; CHECK-LABEL: define noalias ptr @return_unknown_noalias_call(
239239
; CHECK-SAME: ptr readonly captures(none) [[FN:%.*]]) {
240240
; CHECK-NEXT: [[A:%.*]] = call noalias ptr [[FN]]()
241241
; CHECK-NEXT: ret ptr [[A]]

llvm/test/Transforms/FunctionAttrs/nonnull.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ define ptr @unknown_func(ptr %fn) {
14121412
}
14131413

14141414
define ptr @unknown_nonnull_func(ptr %fn) {
1415-
; FNATTRS-LABEL: define ptr @unknown_nonnull_func(
1415+
; FNATTRS-LABEL: define nonnull ptr @unknown_nonnull_func(
14161416
; FNATTRS-SAME: ptr readonly captures(none) [[FN:%.*]]) {
14171417
; FNATTRS-NEXT: [[RES:%.*]] = call nonnull ptr [[FN]]()
14181418
; FNATTRS-NEXT: ret ptr [[RES]]

llvm/test/Transforms/FunctionAttrs/nounwind.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,10 @@ define void @unknown_call(ptr %fn) {
418418
}
419419

420420
define void @unknown_nounwind_call(ptr %fn) {
421+
; FNATTRS: Function Attrs: nounwind
421422
; FNATTRS-LABEL: define {{[^@]+}}@unknown_nounwind_call
422-
; FNATTRS-SAME: (ptr readonly captures(none) [[FN:%.*]]) {
423-
; FNATTRS-NEXT: call void [[FN]]() #[[ATTR2:[0-9]+]]
423+
; FNATTRS-SAME: (ptr readonly captures(none) [[FN:%.*]]) #[[ATTR2:[0-9]+]] {
424+
; FNATTRS-NEXT: call void [[FN]]() #[[ATTR2]]
424425
; FNATTRS-NEXT: ret void
425426
;
426427
; ATTRIBUTOR: Function Attrs: nounwind

0 commit comments

Comments
 (0)