Skip to content

Commit 12da7fa

Browse files
committed
Make sure we don't increase pre-existing captures attributes
1 parent 519e5da commit 12da7fa

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

llvm/lib/Transforms/IPO/FunctionAttrs.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,8 +1320,9 @@ static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
13201320
if (ArgumentSCC[0]->Uses.size() == 1 &&
13211321
ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
13221322
Argument *A = ArgumentSCC[0]->Definition;
1323-
CaptureInfo NewCI = ArgumentSCC[0]->CC;
1324-
if (NewCI != A->getAttributes().getCaptureInfo()) {
1323+
CaptureInfo OrigCI = A->getAttributes().getCaptureInfo();
1324+
CaptureInfo NewCI = CaptureInfo(ArgumentSCC[0]->CC) & OrigCI;
1325+
if (NewCI != OrigCI) {
13251326
A->addAttr(Attribute::getWithCaptureInfo(A->getContext(), NewCI));
13261327
addCapturesStat(NewCI);
13271328
Changed.insert(A->getParent());
@@ -1361,10 +1362,13 @@ static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
13611362
if (!capturesAll(CC)) {
13621363
for (ArgumentGraphNode *N : ArgumentSCC) {
13631364
Argument *A = N->Definition;
1364-
CaptureInfo CI = N->CC | CC;
1365-
A->addAttr(Attribute::getWithCaptureInfo(A->getContext(), CI));
1366-
addCapturesStat(CI);
1367-
Changed.insert(A->getParent());
1365+
CaptureInfo OrigCI = A->getAttributes().getCaptureInfo();
1366+
CaptureInfo NewCI = CaptureInfo(N->CC | CC) & OrigCI;
1367+
if (NewCI != OrigCI) {
1368+
A->addAttr(Attribute::getWithCaptureInfo(A->getContext(), NewCI));
1369+
addCapturesStat(NewCI);
1370+
Changed.insert(A->getParent());
1371+
}
13681372
}
13691373
}
13701374

llvm/test/Transforms/FunctionAttrs/nocapture.ll

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,5 +1018,72 @@ else:
10181018
ret ptr %p
10191019
}
10201020

1021+
define i1 @improve_existing_captures(ptr captures(address) %p) {
1022+
; FNATTRS: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
1023+
; FNATTRS-LABEL: define i1 @improve_existing_captures
1024+
; FNATTRS-SAME: (ptr readnone captures(address_is_null) [[P:%.*]]) #[[ATTR0]] {
1025+
; FNATTRS-NEXT: [[CMP:%.*]] = icmp eq ptr [[P]], null
1026+
; FNATTRS-NEXT: ret i1 [[CMP]]
1027+
;
1028+
; ATTRIBUTOR: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
1029+
; ATTRIBUTOR-LABEL: define i1 @improve_existing_captures
1030+
; ATTRIBUTOR-SAME: (ptr nofree readnone captures(address) [[P:%.*]]) #[[ATTR0]] {
1031+
; ATTRIBUTOR-NEXT: [[CMP:%.*]] = icmp eq ptr [[P]], null
1032+
; ATTRIBUTOR-NEXT: ret i1 [[CMP]]
1033+
;
1034+
%cmp = icmp eq ptr %p, null
1035+
ret i1 %cmp
1036+
}
1037+
1038+
define void @dont_increase_existing_captures(ptr captures(address) %p) {
1039+
; COMMON-LABEL: define void @dont_increase_existing_captures
1040+
; COMMON-SAME: (ptr captures(address) [[P:%.*]]) {
1041+
; COMMON-NEXT: call void @capture(ptr [[P]])
1042+
; COMMON-NEXT: ret void
1043+
;
1044+
call void @capture(ptr %p)
1045+
ret void
1046+
}
1047+
1048+
define void @dont_increase_existing_captures_trivial_scc(ptr captures(address) %p) {
1049+
; COMMON-LABEL: define void @dont_increase_existing_captures_trivial_scc
1050+
; COMMON-SAME: (ptr captures(address) [[P:%.*]]) {
1051+
; COMMON-NEXT: call void @capture(ptr captures(address, read_provenance) [[P]])
1052+
; COMMON-NEXT: call void @dont_increase_existing_captures_trivial_scc(ptr [[P]])
1053+
; COMMON-NEXT: ret void
1054+
;
1055+
call void @capture(ptr captures(address, read_provenance) %p)
1056+
call void @dont_increase_existing_captures_trivial_scc(ptr %p)
1057+
ret void
1058+
}
1059+
1060+
define void @dont_increase_existing_captures_scc1(ptr captures(address) %p) {
1061+
; COMMON-LABEL: define void @dont_increase_existing_captures_scc1
1062+
; COMMON-SAME: (ptr captures(address) [[P:%.*]]) {
1063+
; COMMON-NEXT: call void @dont_increase_existing_captures_scc2(ptr [[P]])
1064+
; COMMON-NEXT: ret void
1065+
;
1066+
call void @dont_increase_existing_captures_scc2(ptr %p)
1067+
ret void
1068+
}
1069+
1070+
define void @dont_increase_existing_captures_scc2(ptr %p) {
1071+
; FNATTRS-LABEL: define void @dont_increase_existing_captures_scc2
1072+
; FNATTRS-SAME: (ptr captures(address, read_provenance) [[P:%.*]]) {
1073+
; FNATTRS-NEXT: call void @capture(ptr captures(address, read_provenance) [[P]])
1074+
; FNATTRS-NEXT: call void @dont_increase_existing_captures_scc1(ptr [[P]])
1075+
; FNATTRS-NEXT: ret void
1076+
;
1077+
; ATTRIBUTOR-LABEL: define void @dont_increase_existing_captures_scc2
1078+
; ATTRIBUTOR-SAME: (ptr [[P:%.*]]) {
1079+
; ATTRIBUTOR-NEXT: call void @capture(ptr captures(address, read_provenance) [[P]])
1080+
; ATTRIBUTOR-NEXT: call void @dont_increase_existing_captures_scc1(ptr [[P]])
1081+
; ATTRIBUTOR-NEXT: ret void
1082+
;
1083+
call void @capture(ptr captures(address, read_provenance) %p)
1084+
call void @dont_increase_existing_captures_scc1(ptr %p)
1085+
ret void
1086+
}
1087+
10211088
declare ptr @llvm.launder.invariant.group.p0(ptr)
10221089
declare ptr @llvm.strip.invariant.group.p0(ptr)

0 commit comments

Comments
 (0)