Skip to content

Commit 0b24739

Browse files
committed
[PGO][indirect-call-promotion] Add extra parameter check for musttail callsite
Indirect-call-promtion for musttail callsite needs stricter type check with parameter. This patch adds the needed extra parameter type check. Differential Revision: https://reviews.llvm.org/D139051
1 parent 50fd660 commit 0b24739

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

llvm/lib/Transforms/Utils/CallPromotionUtils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,18 @@ bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee,
439439
*FailureReason = "Argument type mismatch";
440440
return false;
441441
}
442+
443+
// MustTail call needs stricter type match. See
444+
// Verifier::verifyMustTailCall().
445+
if (CB.isMustTailCall()) {
446+
PointerType *PF = dyn_cast<PointerType>(FormalTy);
447+
PointerType *PA = dyn_cast<PointerType>(ActualTy);
448+
if (!PF || !PA || PF->getAddressSpace() != PA->getAddressSpace()) {
449+
if (FailureReason)
450+
*FailureReason = "Musttail call Argument type mismatch";
451+
return false;
452+
}
453+
}
442454
}
443455
for (; I < NumArgs; I++) {
444456
// Vararg functions can have more arguments than parameters.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; Mustcall needs stricter parameter type checks otherwise it will fail in verifier.
3+
4+
; RUN: opt < %s -passes=pgo-icall-prom -S | FileCheck %s
5+
6+
; Here we check there is no ICP due to parameter mismatch.
7+
define ptr @func(ptr %msg, ptr %ptr, ptr %ctx, i64 %data.coerce, ptr %table, i64 %hasbits) {
8+
; CHECK-LABEL: @func(
9+
; CHECK-NEXT: entry:
10+
; CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr null, align 8
11+
; CHECK-NEXT: ret ptr null
12+
; CHECK: 1:
13+
; CHECK-NEXT: [[CALL11_I:%.*]] = musttail call ptr [[TMP0]](ptr null, ptr null, ptr null, i64 0, ptr null, i64 0), !prof [[PROF0:![0-9]+]]
14+
; CHECK-NEXT: ret ptr [[CALL11_I]]
15+
;
16+
entry:
17+
%0 = load ptr, ptr null, align 8
18+
ret ptr null
19+
20+
1:
21+
%call11.i = musttail call ptr %0(ptr null, ptr null, ptr null, i64 0, ptr null, i64 0), !prof !0
22+
ret ptr %call11.i
23+
}
24+
25+
; Here we check that ICP succeeds since parameters match. Also check the direct-call has a MustCall attribute.
26+
define ptr @func2(ptr %msg, i64 %tag, ptr %ctx, ptr %type, ptr %table, ptr %ptr) {
27+
; CHECK-LABEL: @func2(
28+
; CHECK-NEXT: entry:
29+
; CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr null, align 8
30+
; CHECK-NEXT: ret ptr null
31+
; CHECK: 1:
32+
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq ptr [[TMP1]], @_ZN6proto28internal12ExtensionSet10ParseFieldEmPKcPKNS_7MessageEPNS0_16InternalMetadataEPNS0_12ParseContextE
33+
; CHECK-NEXT: br i1 [[TMP2]], label [[IF_TRUE_DIRECT_TARG:%.*]], label [[TMP4:%.*]], !prof [[PROF1:![0-9]+]]
34+
; CHECK: if.true.direct_targ:
35+
; CHECK-NEXT: [[TMP3:%.*]] = musttail call ptr @_ZN6proto28internal12ExtensionSet10ParseFieldEmPKcPKNS_7MessageEPNS0_16InternalMetadataEPNS0_12ParseContextE(ptr null, i64 0, ptr null, ptr null, ptr null, ptr null)
36+
; CHECK-NEXT: ret ptr [[TMP3]]
37+
; CHECK: 4:
38+
; CHECK-NEXT: [[CALL11_I:%.*]] = musttail call ptr [[TMP1]](ptr null, i64 0, ptr null, ptr null, ptr null, ptr null), !prof [[PROF2:![0-9]+]]
39+
; CHECK-NEXT: ret ptr [[CALL11_I]]
40+
;
41+
entry:
42+
%0 = load ptr, ptr null, align 8
43+
ret ptr null
44+
45+
1:
46+
%call11.i = musttail call ptr %0(ptr null, i64 0, ptr null, ptr null, ptr null, ptr null), !prof !0
47+
ret ptr %call11.i
48+
}
49+
50+
define available_externally ptr @_ZN6proto28internal12ExtensionSet10ParseFieldEmPKcPKNS_7MessageEPNS0_16InternalMetadataEPNS0_12ParseContextE(ptr %this, i64 %tag, ptr %ptr, ptr %containing_type, ptr %metadata, ptr %ctx) {
51+
entry:
52+
ret ptr null
53+
}
54+
55+
!0 = !{!"VP", i32 0, i64 2024, i64 -4843250054591211088, i64 -1, i64 1456131869974120143, i64 947, i64 -4941069334091589447, i64 18}

0 commit comments

Comments
 (0)