Skip to content

Commit 285dbed

Browse files
committed
[Inliner] Propagate callee argument memory access attributes before inlining
To avoid losing information, we can propagate some access attribute from the to-be-inlined callee to its callsites. We can propagate argument memory access attributes to callsite parameters if they are from the same underlying object. Closes #89024
1 parent f8ff51e commit 285dbed

File tree

4 files changed

+108
-31
lines changed

4 files changed

+108
-31
lines changed

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,79 @@ static bool MayContainThrowingOrExitingCallAfterCB(CallBase *Begin,
13441344
++BeginIt, End->getIterator(), InlinerAttributeWindow + 1);
13451345
}
13461346

1347+
// Add attributes from CB params and Fn attributes that can always be propagated
1348+
// to the corresponding argument / inner callbases.
1349+
static void AddParamAndFnBasicAttributes(const CallBase &CB,
1350+
ValueToValueMapTy &VMap) {
1351+
auto *CalledFunction = CB.getCalledFunction();
1352+
auto &Context = CalledFunction->getContext();
1353+
1354+
// Collect valid attributes for all params.
1355+
SmallVector<AttrBuilder> ValidParamAttrs;
1356+
bool HasAttrToPropagate = false;
1357+
1358+
for (unsigned I = 0, E = CB.arg_size(); I < E; ++I) {
1359+
ValidParamAttrs.emplace_back(AttrBuilder{CB.getContext()});
1360+
// Access attributes can be propagated to any param with the same underlying
1361+
// object as the argument.
1362+
if (CB.paramHasAttr(I, Attribute::ReadNone))
1363+
ValidParamAttrs.back().addAttribute(Attribute::ReadNone);
1364+
if (CB.paramHasAttr(I, Attribute::ReadOnly))
1365+
ValidParamAttrs.back().addAttribute(Attribute::ReadOnly);
1366+
if (CB.paramHasAttr(I, Attribute::WriteOnly))
1367+
ValidParamAttrs.back().addAttribute(Attribute::WriteOnly);
1368+
HasAttrToPropagate |= ValidParamAttrs.back().hasAttributes();
1369+
}
1370+
1371+
// Won't be able to propagate anything.
1372+
if (!HasAttrToPropagate)
1373+
return;
1374+
1375+
for (BasicBlock &BB : *CalledFunction) {
1376+
for (Instruction &Ins : BB) {
1377+
const auto *InnerCB = dyn_cast<CallBase>(&Ins);
1378+
if (!InnerCB)
1379+
continue;
1380+
auto *NewInnerCB = dyn_cast_or_null<CallBase>(VMap.lookup(InnerCB));
1381+
if (!NewInnerCB)
1382+
continue;
1383+
AttributeList AL = NewInnerCB->getAttributes();
1384+
for (unsigned I = 0, E = InnerCB->arg_size(); I < E; ++I) {
1385+
// Check if the underlying value for the parameter is an argument.
1386+
const Value *UnderlyingV =
1387+
getUnderlyingObject(InnerCB->getArgOperand(I));
1388+
const Argument *Arg = dyn_cast<Argument>(UnderlyingV);
1389+
if (!Arg)
1390+
continue;
1391+
1392+
unsigned ArgNo = Arg->getArgNo();
1393+
// If so, propagate its access attributes.
1394+
AL = AL.addParamAttributes(Context, I, ValidParamAttrs[ArgNo]);
1395+
// We can have conflicting attributes from the inner callsite and
1396+
// to-be-inlined callsite. In that case, choose the most
1397+
// restrictive.
1398+
1399+
// readonly + writeonly means we can never deref so make readnone.
1400+
if (AL.hasParamAttr(I, Attribute::ReadOnly) &&
1401+
AL.hasParamAttr(I, Attribute::WriteOnly))
1402+
AL = AL.addParamAttribute(Context, I, Attribute::ReadNone);
1403+
1404+
// If have readnone, need to clear readonly/writeonly
1405+
if (AL.hasParamAttr(I, Attribute::ReadNone)) {
1406+
AL = AL.removeParamAttribute(Context, I, Attribute::ReadOnly);
1407+
AL = AL.removeParamAttribute(Context, I, Attribute::WriteOnly);
1408+
}
1409+
1410+
// Writable cannot exist in conjunction w/ readonly/readnone
1411+
if (AL.hasParamAttr(I, Attribute::ReadOnly) ||
1412+
AL.hasParamAttr(I, Attribute::ReadNone))
1413+
AL = AL.removeParamAttribute(Context, I, Attribute::Writable);
1414+
}
1415+
NewInnerCB->setAttributes(AL);
1416+
}
1417+
}
1418+
}
1419+
13471420
// Only allow these white listed attributes to be propagated back to the
13481421
// callee. This is because other attributes may only be valid on the call
13491422
// itself, i.e. attributes such as signext and zeroext.
@@ -2363,6 +2436,10 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
23632436
// function which feed into its return value.
23642437
AddReturnAttributes(CB, VMap);
23652438

2439+
// Clone attributes on the params of the callsite to calls within the
2440+
// inlined function which use the same param.
2441+
AddParamAndFnBasicAttributes(CB, VMap);
2442+
23662443
propagateMemProfMetadata(CalledFunc, CB,
23672444
InlinedFunctionInfo.ContainsMemProfMetadata, VMap);
23682445

llvm/test/Transforms/Inline/access-attributes-prop.ll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ define dso_local void @foo2_through_obj(ptr %p, ptr %p2) {
189189
define void @prop_param_func_decl(ptr %p) {
190190
; CHECK-LABEL: define {{[^@]+}}@prop_param_func_decl
191191
; CHECK-SAME: (ptr [[P:%.*]]) {
192-
; CHECK-NEXT: call void @bar1(ptr [[P]])
192+
; CHECK-NEXT: call void @bar1(ptr readonly [[P]])
193193
; CHECK-NEXT: ret void
194194
;
195195
call void @foo1_rdonly(ptr %p)
@@ -199,7 +199,7 @@ define void @prop_param_func_decl(ptr %p) {
199199
define void @prop_param_callbase_def(ptr %p) {
200200
; CHECK-LABEL: define {{[^@]+}}@prop_param_callbase_def
201201
; CHECK-SAME: (ptr [[P:%.*]]) {
202-
; CHECK-NEXT: call void @bar1(ptr [[P]])
202+
; CHECK-NEXT: call void @bar1(ptr readonly [[P]])
203203
; CHECK-NEXT: call void @bar1(ptr [[P]])
204204
; CHECK-NEXT: ret void
205205
;
@@ -211,7 +211,7 @@ define void @prop_param_callbase_def(ptr %p) {
211211
define void @prop_param_callbase_def_2x(ptr %p, ptr %p2) {
212212
; CHECK-LABEL: define {{[^@]+}}@prop_param_callbase_def_2x
213213
; CHECK-SAME: (ptr [[P:%.*]], ptr [[P2:%.*]]) {
214-
; CHECK-NEXT: call void @bar2(ptr [[P]], ptr [[P]])
214+
; CHECK-NEXT: call void @bar2(ptr readonly [[P]], ptr readonly [[P]])
215215
; CHECK-NEXT: ret void
216216
;
217217
call void @foo2(ptr readonly %p, ptr %p)
@@ -223,7 +223,7 @@ define void @prop_param_callbase_def_2x_2(ptr %p, ptr %p2) {
223223
; CHECK-SAME: (ptr [[P:%.*]], ptr [[P2:%.*]]) {
224224
; CHECK-NEXT: [[PP_I:%.*]] = getelementptr i8, ptr [[P]], i64 9
225225
; CHECK-NEXT: [[P2P_I:%.*]] = getelementptr i8, ptr [[P2]], i64 123
226-
; CHECK-NEXT: call void @bar2(ptr [[P2P_I]], ptr [[PP_I]])
226+
; CHECK-NEXT: call void @bar2(ptr writeonly [[P2P_I]], ptr readonly [[PP_I]])
227227
; CHECK-NEXT: ret void
228228
;
229229
call void @foo2_through_obj(ptr readonly %p, ptr writeonly %p2)
@@ -235,7 +235,7 @@ define void @prop_param_callbase_def_2x_incompat(ptr %p, ptr %p2) {
235235
; CHECK-SAME: (ptr [[P:%.*]], ptr [[P2:%.*]]) {
236236
; CHECK-NEXT: [[PP_I:%.*]] = getelementptr i8, ptr [[P]], i64 9
237237
; CHECK-NEXT: [[P2P_I:%.*]] = getelementptr i8, ptr [[P]], i64 123
238-
; CHECK-NEXT: call void @bar2(ptr [[P2P_I]], ptr [[PP_I]])
238+
; CHECK-NEXT: call void @bar2(ptr readonly [[P2P_I]], ptr readnone [[PP_I]])
239239
; CHECK-NEXT: ret void
240240
;
241241
call void @foo2_through_obj(ptr readnone %p, ptr readonly %p)
@@ -245,7 +245,7 @@ define void @prop_param_callbase_def_2x_incompat(ptr %p, ptr %p2) {
245245
define void @prop_param_callbase_def_2x_incompat_2(ptr %p, ptr %p2) {
246246
; CHECK-LABEL: define {{[^@]+}}@prop_param_callbase_def_2x_incompat_2
247247
; CHECK-SAME: (ptr [[P:%.*]], ptr [[P2:%.*]]) {
248-
; CHECK-NEXT: call void @bar2(ptr [[P]], ptr [[P]])
248+
; CHECK-NEXT: call void @bar2(ptr readonly [[P]], ptr readonly [[P]])
249249
; CHECK-NEXT: ret void
250250
;
251251
call void @foo2(ptr readonly %p, ptr readnone %p)
@@ -255,7 +255,7 @@ define void @prop_param_callbase_def_2x_incompat_2(ptr %p, ptr %p2) {
255255
define void @prop_param_callbase_def_2x_incompat_3(ptr %p, ptr %p2) {
256256
; CHECK-LABEL: define {{[^@]+}}@prop_param_callbase_def_2x_incompat_3
257257
; CHECK-SAME: (ptr [[P:%.*]], ptr [[P2:%.*]]) {
258-
; CHECK-NEXT: call void @bar2(ptr [[P]], ptr [[P]])
258+
; CHECK-NEXT: call void @bar2(ptr readnone [[P]], ptr readnone [[P]])
259259
; CHECK-NEXT: ret void
260260
;
261261
call void @foo2_2(ptr readonly %p, ptr readnone %p)
@@ -265,7 +265,7 @@ define void @prop_param_callbase_def_2x_incompat_3(ptr %p, ptr %p2) {
265265
define void @prop_param_callbase_def_1x_partial(ptr %p, ptr %p2) {
266266
; CHECK-LABEL: define {{[^@]+}}@prop_param_callbase_def_1x_partial
267267
; CHECK-SAME: (ptr [[P:%.*]], ptr [[P2:%.*]]) {
268-
; CHECK-NEXT: call void @bar2(ptr [[P]], ptr [[P]])
268+
; CHECK-NEXT: call void @bar2(ptr readonly [[P]], ptr readonly [[P]])
269269
; CHECK-NEXT: ret void
270270
;
271271
call void @foo2(ptr readonly %p, ptr %p)
@@ -285,7 +285,7 @@ define void @prop_param_callbase_def_1x_partial_2(ptr %p, ptr %p2) {
285285
define void @prop_param_callbase_def_1x_partial_3(ptr %p, ptr %p2) {
286286
; CHECK-LABEL: define {{[^@]+}}@prop_param_callbase_def_1x_partial_3
287287
; CHECK-SAME: (ptr [[P:%.*]], ptr [[P2:%.*]]) {
288-
; CHECK-NEXT: call void @bar2(ptr [[P]], ptr [[P]])
288+
; CHECK-NEXT: call void @bar2(ptr readonly [[P]], ptr readnone [[P]])
289289
; CHECK-NEXT: ret void
290290
;
291291
call void @foo2_3(ptr readonly %p, ptr %p)
@@ -521,7 +521,7 @@ define void @prop_cb_def_mustprogress(ptr %p) {
521521
define void @prop_no_conflict_writable(ptr %p) {
522522
; CHECK-LABEL: define {{[^@]+}}@prop_no_conflict_writable
523523
; CHECK-SAME: (ptr [[P:%.*]]) {
524-
; CHECK-NEXT: call void @bar1(ptr writable [[P]])
524+
; CHECK-NEXT: call void @bar1(ptr readonly [[P]])
525525
; CHECK-NEXT: ret void
526526
;
527527
call void @foo1_writable(ptr readonly %p)
@@ -532,7 +532,7 @@ define void @prop_no_conflict_writable(ptr %p) {
532532
define void @prop_no_conflict_writable2(ptr %p) {
533533
; CHECK-LABEL: define {{[^@]+}}@prop_no_conflict_writable2
534534
; CHECK-SAME: (ptr [[P:%.*]]) {
535-
; CHECK-NEXT: call void @bar3(ptr [[P]])
535+
; CHECK-NEXT: call void @bar3(ptr readnone [[P]])
536536
; CHECK-NEXT: ret void
537537
;
538538
call void @foo3_writable(ptr readnone %p)

llvm/test/Transforms/Inline/noalias-calls-always.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ define void @foo(ptr nocapture %a, ptr nocapture readonly %c, ptr nocapture %b)
3434
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META0:![0-9]+]])
3535
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META3:![0-9]+]])
3636
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 512, ptr [[L_I]])
37-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A:%.*]], ptr align 16 [[B:%.*]], i64 16, i1 false), !noalias !3
38-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[B]], ptr align 16 [[C:%.*]], i64 16, i1 false), !noalias !0
39-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr align 16 [[C]], i64 16, i1 false), !alias.scope !5
40-
; CHECK-NEXT: call void @hey(), !noalias !5
41-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[L_I]], ptr align 16 [[C]], i64 16, i1 false), !noalias !0
37+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A:%.*]], ptr align 16 [[B:%.*]], i64 16, i1 false), !noalias [[META3]]
38+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[B]], ptr readonly align 16 [[C:%.*]], i64 16, i1 false), !noalias [[META0]]
39+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr readonly align 16 [[C]], i64 16, i1 false), !alias.scope [[META5:![0-9]+]]
40+
; CHECK-NEXT: call void @hey(), !noalias [[META5]]
41+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[L_I]], ptr readonly align 16 [[C]], i64 16, i1 false), !noalias [[META0]]
4242
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 512, ptr [[L_I]])
4343
; CHECK-NEXT: ret void
4444
;
@@ -75,11 +75,11 @@ define void @foo_cs(ptr nocapture %a, ptr nocapture readonly %c, ptr nocapture %
7575
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META6:![0-9]+]])
7676
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META9:![0-9]+]])
7777
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 512, ptr [[L_I]])
78-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A:%.*]], ptr align 16 [[B:%.*]], i64 16, i1 false), !noalias !9
79-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[B]], ptr align 16 [[C:%.*]], i64 16, i1 false), !noalias !6
80-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr align 16 [[C]], i64 16, i1 false), !alias.scope !11
81-
; CHECK-NEXT: call void @hey(), !noalias !11
82-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[L_I]], ptr align 16 [[C]], i64 16, i1 false), !noalias !6
78+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A:%.*]], ptr align 16 [[B:%.*]], i64 16, i1 false), !noalias [[META9]]
79+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[B]], ptr readonly align 16 [[C:%.*]], i64 16, i1 false), !noalias [[META6]]
80+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr readonly align 16 [[C]], i64 16, i1 false), !alias.scope [[META11:![0-9]+]]
81+
; CHECK-NEXT: call void @hey(), !noalias [[META11]]
82+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[L_I]], ptr readonly align 16 [[C]], i64 16, i1 false), !noalias [[META6]]
8383
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 512, ptr [[L_I]])
8484
; CHECK-NEXT: ret void
8585
;

llvm/test/Transforms/Inline/noalias-calls.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ define void @foo(ptr nocapture %a, ptr nocapture readonly %c, ptr nocapture %b)
3737
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META0:![0-9]+]])
3838
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META3:![0-9]+]])
3939
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 512, ptr [[L_I]])
40-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr align 16 [[B]], i64 16, i1 false), !noalias !3
41-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[B]], ptr align 16 [[C]], i64 16, i1 false), !noalias !0
42-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr align 16 [[C]], i64 16, i1 false), !alias.scope !5
43-
; CHECK-NEXT: call void @hey(), !noalias !5
44-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[L_I]], ptr align 16 [[C]], i64 16, i1 false), !noalias !0
40+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr align 16 [[B]], i64 16, i1 false), !noalias [[META3]]
41+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[B]], ptr readonly align 16 [[C]], i64 16, i1 false), !noalias [[META0]]
42+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr readonly align 16 [[C]], i64 16, i1 false), !alias.scope [[META5:![0-9]+]]
43+
; CHECK-NEXT: call void @hey(), !noalias [[META5]]
44+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[L_I]], ptr readonly align 16 [[C]], i64 16, i1 false), !noalias [[META0]]
4545
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 512, ptr [[L_I]])
4646
; CHECK-NEXT: ret void
4747
;
@@ -80,11 +80,11 @@ define void @foo_cs(ptr nocapture %a, ptr nocapture readonly %c, ptr nocapture %
8080
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META6:![0-9]+]])
8181
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META9:![0-9]+]])
8282
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 512, ptr [[L_I]])
83-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr align 16 [[B]], i64 16, i1 false), !noalias !9
84-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[B]], ptr align 16 [[C]], i64 16, i1 false), !noalias !6
85-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr align 16 [[C]], i64 16, i1 false), !alias.scope !11
86-
; CHECK-NEXT: call void @hey(), !noalias !11
87-
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[L_I]], ptr align 16 [[C]], i64 16, i1 false), !noalias !6
83+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr align 16 [[B]], i64 16, i1 false), !noalias [[META9]]
84+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[B]], ptr readonly align 16 [[C]], i64 16, i1 false), !noalias [[META6]]
85+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[A]], ptr readonly align 16 [[C]], i64 16, i1 false), !alias.scope [[META11:![0-9]+]]
86+
; CHECK-NEXT: call void @hey(), !noalias [[META11]]
87+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[L_I]], ptr readonly align 16 [[C]], i64 16, i1 false), !noalias [[META6]]
8888
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 512, ptr [[L_I]])
8989
; CHECK-NEXT: ret void
9090
;

0 commit comments

Comments
 (0)