Skip to content

Commit 42f7498

Browse files
committed
Don't transform calls unless some aspect is unneeded
1 parent 9dff0b3 commit 42f7498

File tree

2 files changed

+69
-58
lines changed

2 files changed

+69
-58
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,8 +4016,28 @@ static Value *optimizeModularFormat(CallInst *CI, IRBuilderBase &B) {
40164016
--FirstArgIdx;
40174017
StringRef FnName = Args[3];
40184018
StringRef ImplName = Args[4];
4019-
DenseSet<StringRef> Aspects(llvm::from_range,
4020-
ArrayRef<StringRef>(Args).drop_front(5));
4019+
ArrayRef<StringRef> AllAspects = ArrayRef<StringRef>(Args).drop_front(5);
4020+
4021+
if (AllAspects.empty())
4022+
return nullptr;
4023+
4024+
SmallVector<StringRef> NeededAspects;
4025+
for (StringRef Aspect : AllAspects) {
4026+
if (Aspect == "float") {
4027+
if (llvm::any_of(
4028+
llvm::make_range(std::next(CI->arg_begin(), FirstArgIdx),
4029+
CI->arg_end()),
4030+
[](Value *V) { return V->getType()->isFloatingPointTy(); }))
4031+
NeededAspects.push_back("float");
4032+
} else {
4033+
// Unknown aspects are always considered to be needed.
4034+
NeededAspects.push_back(Aspect);
4035+
}
4036+
}
4037+
4038+
if (NeededAspects.size() == AllAspects.size())
4039+
return nullptr;
4040+
40214041
Module *M = CI->getModule();
40224042
Function *Callee = CI->getCalledFunction();
40234043
FunctionCallee ModularFn =
@@ -4040,18 +4060,8 @@ static Value *optimizeModularFormat(CallInst *CI, IRBuilderBase &B) {
40404060
B.CreateCall(RelocNoneFn, {Sym});
40414061
};
40424062

4043-
if (Aspects.contains("float")) {
4044-
Aspects.erase("float");
4045-
if (llvm::any_of(
4046-
llvm::make_range(std::next(CI->arg_begin(), FirstArgIdx),
4047-
CI->arg_end()),
4048-
[](Value *V) { return V->getType()->isFloatingPointTy(); }))
4049-
ReferenceAspect("float");
4050-
}
4051-
4052-
SmallVector<StringRef> UnknownAspects(Aspects.begin(), Aspects.end());
4053-
llvm::sort(UnknownAspects);
4054-
for (StringRef Request : UnknownAspects)
4063+
llvm::sort(NeededAspects);
4064+
for (StringRef Request : NeededAspects)
40554065
ReferenceAspect(Request);
40564066

40574067
return New;

llvm/test/Transforms/InstCombine/modular-format.ll

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,58 @@ target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f3
88
@.str.int = constant [3 x i8] c"%d\00"
99
@.str.float = constant [3 x i8] c"%f\00"
1010
@.str.multi = constant [6 x i8] c"%f %d\00"
11-
@.str.multifp = constant [6 x i8] c"%f %f\00"
1211
@.str.noargs = constant [1 x i8] c"\00"
1312

14-
; Basic Transformation
13+
; No aspects are specified, so no transformation occurs.
1514
define void @test_basic(i32 %arg) {
1615
; CHECK-LABEL: @test_basic(
17-
; CHECK-NEXT: call void (ptr, ...) @basic_mod(ptr nonnull @.str.int, i32 [[ARG:%.*]])
16+
; CHECK-NEXT: call void (ptr, ...) @basic(ptr nonnull @.str.int, i32 [[ARG:%.*]])
1817
; CHECK-NEXT: ret void
1918
;
2019
call void (ptr, ...) @basic(ptr @.str.int, i32 %arg)
2120
ret void
2221
}
2322

24-
declare void @basic(ptr, ...) "modular-format"="printf,1,2,basic_mod,basic_impl"
25-
; "float" Aspect - Present
23+
declare void @basic(ptr, ...) #0
24+
25+
; The "float" aspect is present and needed, so no transformation occurs.
2626
define void @test_float_present(double %arg) {
2727
; CHECK-LABEL: @test_float_present(
28-
; CHECK-NEXT: call void (ptr, ...) @float_present_mod(ptr nonnull @.str.float, double [[ARG:%.*]])
29-
; CHECK-NEXT: call void @llvm.reloc.none(ptr nonnull @basic_impl_float)
28+
; CHECK-NEXT: call void (ptr, ...) @float_present(ptr nonnull @.str.float, double [[ARG:%.*]])
3029
; CHECK-NEXT: ret void
3130
;
3231
call void (ptr, ...) @float_present(ptr @.str.float, double %arg)
3332
ret void
3433
}
3534

36-
declare void @float_present(ptr, ...) #0
35+
declare void @float_present(ptr, ...) #1
3736

38-
; Unknown Aspects
39-
define void @test_unknown_aspects(i32 %arg) {
40-
; CHECK-LABEL: @test_unknown_aspects(
41-
; CHECK-NEXT: call void (ptr, ...) @unknown_aspects_mod(ptr nonnull @.str.int, i32 [[ARG:%.*]])
42-
; CHECK-NEXT: call void @llvm.reloc.none(ptr nonnull @basic_impl_unknown1)
43-
; CHECK-NEXT: call void @llvm.reloc.none(ptr nonnull @basic_impl_unknown2)
44-
; CHECK-NEXT: ret void
45-
;
46-
call void (ptr, ...) @unknown_aspects(ptr @.str.int, i32 %arg)
47-
ret void
48-
}
49-
50-
declare void @unknown_aspects(ptr, ...) "modular-format"="printf,1,2,unknown_aspects_mod,basic_impl,unknown1,unknown2"
51-
52-
; Multiple Aspects
53-
define void @test_multiple_aspects(double %arg1, i32 %arg2) {
54-
; CHECK-LABEL: @test_multiple_aspects(
55-
; CHECK-NEXT: call void (ptr, ...) @multiple_aspects_mod(ptr nonnull @.str.multi, double [[ARG1:%.*]], i32 [[ARG2:%.*]])
56-
; CHECK-NEXT: call void @llvm.reloc.none(ptr nonnull @basic_impl_float)
57-
; CHECK-NEXT: call void @llvm.reloc.none(ptr nonnull @basic_impl_unknown)
37+
; The "float" aspect is present but not needed, so the call is transformed.
38+
define void @test_float_absent(i32 %arg) {
39+
; CHECK-LABEL: @test_float_absent(
40+
; CHECK-NEXT: call void (ptr, ...) @float_present_mod(ptr nonnull @.str.int, i32 [[ARG:%.*]])
5841
; CHECK-NEXT: ret void
5942
;
60-
call void (ptr, ...) @multiple_aspects(ptr @.str.multi, double %arg1, i32 %arg2)
43+
call void (ptr, ...) @float_absent(ptr @.str.int, i32 %arg)
6144
ret void
6245
}
6346

64-
declare void @multiple_aspects(ptr, ...) "modular-format"="printf,1,2,multiple_aspects_mod,basic_impl,float,unknown"
47+
declare void @float_absent(ptr, ...) #1
6548

66-
; Multiple Floating-Point Arguments
67-
define void @test_multiple_fp_args(double %arg1, float %arg2) {
68-
; CHECK-LABEL: @test_multiple_fp_args(
69-
; CHECK-NEXT: call void (ptr, ...) @float_present_mod(ptr nonnull @.str.multifp, double [[ARG1:%.*]], float [[ARG2:%.*]])
70-
; CHECK-NEXT: call void @llvm.reloc.none(ptr nonnull @basic_impl_float)
49+
; Unknown aspects are always considered needed, so no transformation occurs.
50+
define void @test_unknown_aspects(i32 %arg) {
51+
; CHECK-LABEL: @test_unknown_aspects(
52+
; CHECK-NEXT: call void (ptr, ...) @unknown_aspects(ptr nonnull @.str.int, i32 [[ARG:%.*]])
7153
; CHECK-NEXT: ret void
7254
;
73-
call void (ptr, ...) @multiple_fp_args(ptr @.str.multifp, double %arg1, float %arg2)
55+
call void (ptr, ...) @unknown_aspects(ptr @.str.int, i32 %arg)
7456
ret void
7557
}
7658

77-
declare void @multiple_fp_args(ptr, ...) #0
59+
declare void @unknown_aspects(ptr, ...) #2
7860

79-
; No Arguments to Check
61+
; The call has no arguments to check, so the "float" aspect is not needed and
62+
; the call is transformed.
8063
define void @test_no_args_to_check() {
8164
; CHECK-LABEL: @test_no_args_to_check(
8265
; CHECK-NEXT: call void (ptr, ...) @float_present_mod(ptr nonnull @.str.noargs)
@@ -86,19 +69,37 @@ define void @test_no_args_to_check() {
8669
ret void
8770
}
8871

89-
declare void @no_args_to_check(ptr, ...) #0
72+
declare void @no_args_to_check(ptr, ...) #1
9073

91-
; First argument index != 2
74+
; The first argument index is not 2. The "float" aspect is needed, so no
75+
; transformation occurs.
9276
define void @test_first_arg_idx(i32 %ignored, double %arg) {
9377
; CHECK-LABEL: @test_first_arg_idx(
94-
; CHECK-NEXT: call void (i32, ptr, ...) @first_arg_idx_mod(i32 [[IGNORED:%.*]], ptr nonnull @.str.float, double [[ARG:%.*]])
95-
; CHECK-NEXT: call void @llvm.reloc.none(ptr nonnull @basic_impl_float)
78+
; CHECK-NEXT: call void (i32, ptr, ...) @first_arg_idx(i32 [[IGNORED:%.*]], ptr nonnull @.str.float, double [[ARG:%.*]])
9679
; CHECK-NEXT: ret void
9780
;
9881
call void (i32, ptr, ...) @first_arg_idx(i32 %ignored, ptr @.str.float, double %arg)
9982
ret void
10083
}
10184

102-
declare void @first_arg_idx(i32, ptr, ...) "modular-format"="printf,2,3,first_arg_idx_mod,basic_impl,float"
85+
declare void @first_arg_idx(i32, ptr, ...) #3
86+
87+
; One aspect ("unknown") is needed, but one ("float") is not. The call is
88+
; transformed, and a reference to the needed aspect is emitted.
89+
define void @test_partial_aspects(i32 %arg) {
90+
; CHECK-LABEL: @test_partial_aspects(
91+
; CHECK-NEXT: call void (ptr, ...) @multiple_aspects_mod(ptr nonnull @.str.int, i32 [[ARG:%.*]])
92+
; CHECK-NEXT: call void @llvm.reloc.none(ptr nonnull @basic_impl_unknown)
93+
; CHECK-NEXT: ret void
94+
;
95+
call void (ptr, ...) @partial_aspects(ptr @.str.int, i32 %arg)
96+
ret void
97+
}
98+
99+
declare void @partial_aspects(ptr, ...) #4
103100

104-
attributes #0 = { "modular-format"="printf,1,2,float_present_mod,basic_impl,float" }
101+
attributes #0 = { "modular-format"="printf,1,2,basic_mod,basic_impl" }
102+
attributes #1 = { "modular-format"="printf,1,2,float_present_mod,basic_impl,float" }
103+
attributes #2 = { "modular-format"="printf,1,2,unknown_aspects_mod,basic_impl,unknown1,unknown2" }
104+
attributes #3 = { "modular-format"="printf,2,3,first_arg_idx_mod,basic_impl,float" }
105+
attributes #4 = { "modular-format"="printf,1,2,multiple_aspects_mod,basic_impl,float,unknown" }

0 commit comments

Comments
 (0)