Skip to content

Commit dc9cca4

Browse files
committed
[𝘀𝗽𝗿] changes introduced through rebase
Created using spr 1.3.6-beta.1 [skip ci]
1 parent 58417bc commit dc9cca4

File tree

7 files changed

+170
-18
lines changed

7 files changed

+170
-18
lines changed

llvm/docs/CalleeTypeMetadata.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Introduction
77
This ``!callee_type`` metadata is introduced to support the generation of a call graph
88
section in the object file. The ``!callee_type`` metadata is used
99
to identify the types of the intended callees of indirect call instructions. The ``!callee_type`` metadata is a
10-
list of one or more ``!type`` metadata objects (See :doc:`TypeMetadata`) with each ``!type`` metadata
11-
pointing to a callee's :ref:`type identifier <calleetype-type-identifier>`.
10+
list of one or more generalized ``!type`` metadata objects (See :doc:`TypeMetadata`) with each ``!type``
11+
metadata pointing to a callee's :ref:`type identifier <calleetype-type-identifier>`.
1212
LLVM's `Control Flow Integrity (CFI)`_ also uses the ``!type`` metadata in its implementation.
1313

1414
.. _Control Flow Integrity (CFI): https://clang.llvm.org/docs/ControlFlowIntegrity.html

llvm/lib/IR/Metadata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ MDNode *MDNode::getMergedCalleeTypeMetadata(LLVMContext &Ctx, MDNode *A,
13061306
MDNode *B) {
13071307
SmallVector<Metadata *, 8> AB;
13081308
SmallSet<Metadata *, 8> MergedCallees;
1309-
auto AddUniqueCallees = [&AB, &MergedCallees](llvm::MDNode *N) {
1309+
auto AddUniqueCallees = [&AB, &MergedCallees](MDNode *N) {
13101310
if (!N)
13111311
return;
13121312
for (const MDOperand &Op : N->operands()) {
@@ -1317,7 +1317,7 @@ MDNode *MDNode::getMergedCalleeTypeMetadata(LLVMContext &Ctx, MDNode *A,
13171317
};
13181318
AddUniqueCallees(A);
13191319
AddUniqueCallees(B);
1320-
return llvm::MDNode::get(Ctx, AB);
1320+
return MDNode::get(Ctx, AB);
13211321
}
13221322

13231323
MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4163,8 +4163,10 @@ Instruction *InstCombinerImpl::visitCallBase(CallBase &Call) {
41634163

41644164
// Drop unnecessary callee_type metadata from calls that were converted
41654165
// into direct calls.
4166-
if (Call.getMetadata(LLVMContext::MD_callee_type) && !Call.isIndirectCall())
4166+
if (Call.getMetadata(LLVMContext::MD_callee_type) && !Call.isIndirectCall()) {
41674167
Call.setMetadata(LLVMContext::MD_callee_type, nullptr);
4168+
Changed = true;
4169+
}
41684170

41694171
// Drop unnecessary kcfi operand bundles from calls that were converted
41704172
// into direct calls.

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,6 +3365,7 @@ static void combineMetadata(Instruction *K, const Instruction *J,
33653365
case LLVMContext::MD_mmra:
33663366
case LLVMContext::MD_memprof:
33673367
case LLVMContext::MD_callsite:
3368+
case LLVMContext::MD_callee_type:
33683369
break;
33693370
case LLVMContext::MD_align:
33703371
if (!AAOnly && (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef)))
@@ -3377,11 +3378,6 @@ static void combineMetadata(Instruction *K, const Instruction *J,
33773378
K->setMetadata(Kind,
33783379
MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
33793380
break;
3380-
case LLVMContext::MD_callee_type:
3381-
if (!AAOnly)
3382-
K->setMetadata(Kind, MDNode::getMergedCalleeTypeMetadata(
3383-
K->getContext(), KMD, JMD));
3384-
break;
33853381
case LLVMContext::MD_preserve_access_index:
33863382
// Preserve !preserve.access.index in K.
33873383
break;
@@ -3442,6 +3438,17 @@ static void combineMetadata(Instruction *K, const Instruction *J,
34423438
MDNode::getMergedCallsiteMetadata(KCallSite, JCallSite));
34433439
}
34443440

3441+
// Merge callee_type metadata.
3442+
// Handle separately to support cases where only one instruction has the
3443+
// metadata.
3444+
auto *JCalleeType = J->getMetadata(LLVMContext::MD_callee_type);
3445+
auto *KCalleeType = K->getMetadata(LLVMContext::MD_callee_type);
3446+
if (!AAOnly && (JCalleeType || KCalleeType)) {
3447+
K->setMetadata(LLVMContext::MD_callee_type,
3448+
MDNode::getMergedCalleeTypeMetadata(
3449+
K->getContext(), KCalleeType, JCalleeType));
3450+
}
3451+
34453452
// Merge prof metadata.
34463453
// Handle separately to support cases where only one instruction has the
34473454
// metadata.

llvm/test/Assembler/callee-type-metadata.ll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,3 @@ declare !type !2 i32 @_Z3barc(i8 signext)
1919
!0 = !{i64 0, !"_ZTSFiPvcE.generalized"}
2020
!1 = !{!2}
2121
!2 = !{i64 0, !"_ZTSFicE.generalized"}
22-
!3 = !{i64 0, !"_ZTSFicE"}
23-
!4 = !{!3}
24-
!8 = !{i64 0, !"_ZTSFicE.generalized"}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
21
;; Test if the callee_type metadata is dropped when it is attached
32
;; to a direct function call during instcombine.
43

54
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
65

7-
define i32 @_Z3barv() !type !3 {
6+
define i32 @_Z3barv() !type !0 {
87
; CHECK-LABEL: define i32 @_Z3barv(
98
; CHECK-SAME: ) !type [[META0:![0-9]+]] {
109
; CHECK-NEXT: [[ENTRY:.*:]]
11-
; CHECK-NEXT: [[CALL:%.*]] = call i32 @_Z3fooc(i8 97)
10+
; CHECK-NEXT: [[CALL:%.*]] = call i32 @_Z3fooc(i8 97){{$}}
1211
; CHECK-NEXT: ret i32 [[CALL]]
1312
;
1413
entry:
@@ -18,10 +17,9 @@ entry:
1817

1918
declare !type !2 i32 @_Z3fooc(i8 signext)
2019

21-
!0 = !{i64 0, !"_ZTSFiPvcE.generalized"}
20+
!0 = !{i64 0, !"_ZTSFivE.generalized"}
2221
!1 = !{!2}
2322
!2 = !{i64 0, !"_ZTSFicE.generalized"}
24-
!3 = !{i64 0, !"_ZTSFivE.generalized"}
2523
;.
2624
; CHECK: [[META0]] = !{i64 0, !"_ZTSFivE.generalized"}
2725
;.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 5
2+
;; Test if the callee_type metadata is merged correctly.
3+
4+
; RUN: opt -passes=simplifycfg -S < %s | FileCheck %s
5+
6+
;; Test if the callee_type metadata is merged correctly when
7+
;; the instructions carry differring callee_type metadata.
8+
define ptr @_Z10test_diffb(i1 zeroext %b) {
9+
; CHECK-LABEL: define ptr @_Z10test_diffb(
10+
; CHECK-SAME: i1 zeroext [[B:%.*]]) {
11+
; CHECK-NEXT: [[ENTRY:.*:]]
12+
; CHECK-NEXT: [[CALL:%.*]] = call ptr @_Znwm(i64 4), !callee_type [[META0:![0-9]+]]
13+
; CHECK-NEXT: ret ptr [[CALL]]
14+
;
15+
entry:
16+
br i1 %b, label %if.then, label %if.else
17+
18+
if.then: ; preds = %entry
19+
%call = call ptr @_Znwm(i64 4), !callee_type !4
20+
br label %if.end
21+
22+
if.else: ; preds = %entry
23+
%call1 = call ptr @_Znwm(i64 4), !callee_type !3
24+
br label %if.end
25+
26+
if.end: ; preds = %if.else, %if.then
27+
%x.0 = phi ptr [ %call, %if.then ], [ %call1, %if.else ]
28+
ret ptr %x.0
29+
}
30+
31+
;; Test if the callee_type metadata is merged correctly when
32+
;; the instructions carry same callee_type metadata.
33+
define ptr @_Z10test_sameb(i1 zeroext %b) {
34+
; CHECK-LABEL: define ptr @_Z10test_sameb(
35+
; CHECK-SAME: i1 zeroext [[B:%.*]]) {
36+
; CHECK-NEXT: [[ENTRY:.*:]]
37+
; CHECK-NEXT: [[CALL:%.*]] = call ptr @_Znwm(i64 4), !callee_type [[META3:![0-9]+]]
38+
; CHECK-NEXT: ret ptr [[CALL]]
39+
;
40+
entry:
41+
br i1 %b, label %if.then, label %if.else
42+
43+
if.then: ; preds = %entry
44+
%call = call ptr @_Znwm(i64 4), !callee_type !3
45+
br label %if.end
46+
47+
if.else: ; preds = %entry
48+
%call1 = call ptr @_Znwm(i64 4), !callee_type !3
49+
br label %if.end
50+
51+
if.end: ; preds = %if.else, %if.then
52+
%x.0 = phi ptr [ %call, %if.then ], [ %call1, %if.else ]
53+
ret ptr %x.0
54+
}
55+
56+
;; Test if the callee_type metadata is merged correctly when
57+
;; only the right instruction has callee_type metadata.
58+
define ptr @_Z10test_leftb(i1 zeroext %b) {
59+
; CHECK-LABEL: define ptr @_Z10test_leftb(
60+
; CHECK-SAME: i1 zeroext [[B:%.*]]) {
61+
; CHECK-NEXT: [[ENTRY:.*:]]
62+
; CHECK-NEXT: [[CALL:%.*]] = call ptr @_Znwm(i64 4), !callee_type [[META4:![0-9]+]]
63+
; CHECK-NEXT: ret ptr [[CALL]]
64+
;
65+
entry:
66+
br i1 %b, label %if.then, label %if.else
67+
68+
if.then: ; preds = %entry
69+
%call = call ptr @_Znwm(i64 4), !callee_type !4
70+
br label %if.end
71+
72+
if.else: ; preds = %entry
73+
%call1 = call ptr @_Znwm(i64 4)
74+
br label %if.end
75+
76+
if.end: ; preds = %if.else, %if.then
77+
%x.0 = phi ptr [ %call, %if.then ], [ %call1, %if.else ]
78+
ret ptr %x.0
79+
}
80+
81+
;; Test if the callee_type metadata is merged correctly when
82+
;; each of the callee_type metadata are lists.
83+
define ptr @_Z10test_rightb(i1 zeroext %b) {
84+
; CHECK-LABEL: define ptr @_Z10test_rightb(
85+
; CHECK-SAME: i1 zeroext [[B:%.*]]) {
86+
; CHECK-NEXT: [[ENTRY:.*:]]
87+
; CHECK-NEXT: [[CALL:%.*]] = call ptr @_Znwm(i64 4), !callee_type [[META3]]
88+
; CHECK-NEXT: ret ptr [[CALL]]
89+
;
90+
entry:
91+
br i1 %b, label %if.then, label %if.else
92+
93+
if.then: ; preds = %entry
94+
%call = call ptr @_Znwm(i64 4)
95+
br label %if.end
96+
97+
if.else: ; preds = %entry
98+
%call1 = call ptr @_Znwm(i64 4), !callee_type !3
99+
br label %if.end
100+
101+
if.end: ; preds = %if.else, %if.then
102+
%x.0 = phi ptr [ %call, %if.then ], [ %call1, %if.else ]
103+
ret ptr %x.0
104+
}
105+
106+
;; Test if the callee_type metadata is merged correctly when
107+
;; only the left instruction has callee_type metadata.
108+
define ptr @_Z10test_listb(i1 zeroext %b) {
109+
; CHECK-LABEL: define ptr @_Z10test_listb(
110+
; CHECK-SAME: i1 zeroext [[B:%.*]]) {
111+
; CHECK-NEXT: [[ENTRY:.*:]]
112+
; CHECK-NEXT: [[CALL:%.*]] = call ptr @_Znwm(i64 4), !callee_type [[META5:![0-9]+]]
113+
; CHECK-NEXT: ret ptr [[CALL]]
114+
;
115+
entry:
116+
br i1 %b, label %if.then, label %if.else
117+
118+
if.then: ; preds = %entry
119+
%call = call ptr @_Znwm(i64 4), !callee_type !6
120+
br label %if.end
121+
122+
if.else: ; preds = %entry
123+
%call1 = call ptr @_Znwm(i64 4), !callee_type !5
124+
br label %if.end
125+
126+
if.end: ; preds = %if.else, %if.then
127+
%x.0 = phi ptr [ %call, %if.then ], [ %call1, %if.else ]
128+
ret ptr %x.0
129+
}
130+
131+
declare ptr @_Znwm(i64)
132+
133+
!0 = !{i64 0, !"callee_type0.generalized"}
134+
!1 = !{i64 0, !"callee_type1.generalized"}
135+
!2 = !{i64 0, !"callee_type2.generalized"}
136+
!3 = !{!0}
137+
!4 = !{!2}
138+
!5 = !{!1, !2}
139+
!6 = !{!0, !2}
140+
;.
141+
; CHECK: [[META0]] = !{[[META1:![0-9]+]], [[META2:![0-9]+]]}
142+
; CHECK: [[META1]] = !{i64 0, !"callee_type2.generalized"}
143+
; CHECK: [[META2]] = !{i64 0, !"callee_type0.generalized"}
144+
; CHECK: [[META3]] = !{[[META2]]}
145+
; CHECK: [[META4]] = !{[[META1]]}
146+
; CHECK: [[META5]] = !{[[META2]], [[META1]], [[META6:![0-9]+]]}
147+
; CHECK: [[META6]] = !{i64 0, !"callee_type1.generalized"}
148+
;.

0 commit comments

Comments
 (0)