Skip to content

Commit 3c5403e

Browse files
committed
llvm-reduce: Fix losing callsite attributes when removing arguments
The attribute APIs make this cumbersome. There seem to be missing overloads using AttrBuilder for the function attrs. Plus there doesn't seem to be a direct way to set the function attrs on the call.
1 parent 6526cda commit 3c5403e

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

llvm/test/tools/llvm-reduce/reduce-arguments-x86_intrcc.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ declare x86_intrcc void @extern_decl(ptr byval(i32), i32, i32)
2020

2121
; INTERESTING-LABEL: void @callsite(
2222
; INTERESTING: call
23-
; REDUCED: call x86_intrcc void @func(ptr %k, i32 %other.keep)
23+
; REDUCED: call x86_intrcc void @func(ptr byval(i32) %k, i32 %other.keep)
2424
define void @callsite(ptr %k, i32 %other.keep, i32 %other.drop) {
2525
call x86_intrcc void @func(ptr byval(i32) %k, i32 %other.keep, i32 %other.drop)
2626
ret void
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
; Check that when removing arguments, existing callsite attributes are preserved
2+
3+
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=arguments --test FileCheck --test-arg --check-prefixes=INTERESTING --test-arg %s --test-arg --input-file %s -o %t
4+
; RUN: FileCheck --check-prefixes=RESULT %s < %t
5+
6+
; INTERESTING-LABEL: define void @callee0(
7+
define void @callee0(ptr %interesting0, ptr %interesting1, i32 %uninteresting2) {
8+
ret void
9+
}
10+
11+
; INTERESTING-LABEL: define void @callee1(
12+
define void @callee1(ptr byval(i64) %interesting0, ptr %interesting1, i32 %uninteresting2) {
13+
ret void
14+
}
15+
16+
; INTERESTING-LABEL: define void @caller0(
17+
18+
; INTERESTING: byval
19+
; INTERESTING-SAME: "some-attr"
20+
21+
; INTERESTING: byval
22+
; INTERESTING-SAME: "more-attr"
23+
24+
; RESULT-LABEL: define void @caller0(ptr %val0) {
25+
; RESULT: call void @callee0(ptr byval(i32) %val0, ptr "some-attr" %alloca0) #0
26+
; RESULT: call void @callee1(ptr byval(i64) %alloca1, ptr "more-attr" %alloca1) #1
27+
define void @caller0(ptr %val0, i32 %val1) {
28+
%alloca0 = alloca i32
29+
%alloca1 = alloca i64
30+
call void @callee0(ptr byval(i32) %val0, ptr "some-attr" %alloca0, i32 %val1) nounwind memory(none) "a-func-attr"
31+
call void @callee1(ptr byval(i64) %alloca1, ptr "more-attr" %alloca1, i32 9) "val-func-attr="="something"
32+
ret void
33+
}
34+
35+
; RESULT-LABEL: define ptr @callee2() {
36+
; RESULT-NEXT: ret ptr null
37+
define ptr @callee2(ptr %val0, i32 %b) {
38+
store i32 %b, ptr %val0
39+
ret ptr %val0
40+
}
41+
42+
; Make sure ret attributes are preserved
43+
; INTERESTING: define ptr @caller1(
44+
; INTERESTING: call
45+
46+
; RESULT-LABEL: define ptr @caller1() {
47+
; RESULT: %ret = call align 4 "ret-attr" ptr @callee2()
48+
49+
define ptr @caller1(ptr %val0, i32 %val1) {
50+
%ret = call align 4 "ret-attr" ptr @callee2(ptr %val0, i32 %val1)
51+
ret ptr %ret
52+
}
53+
54+
; RESULT: attributes #0 = { nounwind memory(none) "a-func-attr" }
55+
; RESULT: attributes #1 = { "val-func-attr="="something" }

llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static bool callingConvRequiresArgument(const Function &F,
3939
/// Goes over OldF calls and replaces them with a call to NewF
4040
static void replaceFunctionCalls(Function &OldF, Function &NewF,
4141
const std::set<int> &ArgIndexesToKeep) {
42+
LLVMContext &Ctx = OldF.getContext();
43+
4244
const auto &Users = OldF.users();
4345
for (auto I = Users.begin(), E = Users.end(); I != E; )
4446
if (auto *CI = dyn_cast<CallInst>(*I++)) {
@@ -47,12 +49,30 @@ static void replaceFunctionCalls(Function &OldF, Function &NewF,
4749
if (CI->getCalledFunction() != &OldF)
4850
continue;
4951
SmallVector<Value *, 8> Args;
50-
for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI)
51-
if (ArgIndexesToKeep.count(ArgI - CI->arg_begin()))
52+
SmallVector<AttrBuilder, 8> ArgAttrs;
53+
54+
for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI) {
55+
unsigned ArgIdx = ArgI - CI->arg_begin();
56+
if (ArgIndexesToKeep.count(ArgIdx)) {
5257
Args.push_back(*ArgI);
58+
ArgAttrs.emplace_back(Ctx, CI->getParamAttributes(ArgIdx));
59+
}
60+
}
5361

62+
// FIXME: Losing bundles, fast math flags and metadata
5463
CallInst *NewCI = CallInst::Create(&NewF, Args);
5564
NewCI->setCallingConv(NewF.getCallingConv());
65+
66+
AttrBuilder CallSiteAttrs(Ctx, CI->getAttributes().getFnAttrs());
67+
NewCI->setAttributes(
68+
AttributeList::get(Ctx, AttributeList::FunctionIndex, CallSiteAttrs));
69+
NewCI->addRetAttrs(AttrBuilder(Ctx, CI->getRetAttributes()));
70+
71+
unsigned AttrIdx = 0;
72+
for (auto ArgI = NewCI->arg_begin(), E = NewCI->arg_end(); ArgI != E;
73+
++ArgI, ++AttrIdx)
74+
NewCI->addParamAttrs(AttrIdx, ArgAttrs[AttrIdx]);
75+
5676
if (!CI->use_empty())
5777
CI->replaceAllUsesWith(NewCI);
5878
ReplaceInstWithInst(CI, NewCI);

0 commit comments

Comments
 (0)