Skip to content

Commit 1860331

Browse files
committed
[MemCpyOpt] Correctly merge alias scopes during call slot optimization
When MemCpyOpt performs call slot optimization it will concatenate the `alias.scope` metadata between the function call and the memcpy. However, scoped AA relies on the domains in metadata to be maintained in a caller-callee relationship. Naive concatenation breaks this assumption leading to bad AA results. The fix is to take the intersection of domains then union the scopes within those domains. The original bug came from a case of rust bad codegen which uses this bad aliasing to perform additional memcpy optimizations. As show in the added test case `%src` got forwarded past its lifetime leading to a dereference of garbage data. Testing ninja check-llvm Reviewed By: jeroen.dobbelaere Differential Revision: https://reviews.llvm.org/D91576
1 parent c8d406c commit 1860331

8 files changed

Lines changed: 156 additions & 56 deletions

File tree

‎llvm/include/llvm/Analysis/ScopedNoAliasAA.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ class Function;
2525
class MDNode;
2626
class MemoryLocation;
2727

28+
/// This is a simple wrapper around an MDNode which provides a higher-level
29+
/// interface by hiding the details of how alias analysis information is encoded
30+
/// in its operands.
31+
class AliasScopeNode {
32+
const MDNode *Node = nullptr;
33+
34+
public:
35+
AliasScopeNode() = default;
36+
explicit AliasScopeNode(const MDNode *N) : Node(N) {}
37+
38+
/// Get the MDNode for this AliasScopeNode.
39+
const MDNode *getNode() const { return Node; }
40+
41+
/// Get the MDNode for this AliasScopeNode's domain.
42+
const MDNode *getDomain() const {
43+
if (Node->getNumOperands() < 2)
44+
return nullptr;
45+
return dyn_cast_or_null<MDNode>(Node->getOperand(1));
46+
}
47+
};
48+
2849
/// A simple AA result which uses scoped-noalias metadata to answer queries.
2950
class ScopedNoAliasAAResult : public AAResultBase<ScopedNoAliasAAResult> {
3051
friend AAResultBase<ScopedNoAliasAAResult>;

‎llvm/lib/Analysis/ScopedNoAliasAA.cpp‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,6 @@ using namespace llvm;
5050
static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
5151
cl::init(true), cl::Hidden);
5252

53-
namespace {
54-
55-
/// This is a simple wrapper around an MDNode which provides a higher-level
56-
/// interface by hiding the details of how alias analysis information is encoded
57-
/// in its operands.
58-
class AliasScopeNode {
59-
const MDNode *Node = nullptr;
60-
61-
public:
62-
AliasScopeNode() = default;
63-
explicit AliasScopeNode(const MDNode *N) : Node(N) {}
64-
65-
/// Get the MDNode for this AliasScopeNode.
66-
const MDNode *getNode() const { return Node; }
67-
68-
/// Get the MDNode for this AliasScopeNode's domain.
69-
const MDNode *getDomain() const {
70-
if (Node->getNumOperands() < 2)
71-
return nullptr;
72-
return dyn_cast_or_null<MDNode>(Node->getOperand(1));
73-
}
74-
};
75-
76-
} // end anonymous namespace
77-
7853
AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
7954
const MemoryLocation &LocB,
8055
AAQueryInfo &AAQI) {

‎llvm/lib/IR/Metadata.cpp‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/ADT/StringMap.h"
2828
#include "llvm/ADT/StringRef.h"
2929
#include "llvm/ADT/Twine.h"
30+
#include "llvm/Analysis/ScopedNoAliasAA.h"
3031
#include "llvm/IR/Argument.h"
3132
#include "llvm/IR/BasicBlock.h"
3233
#include "llvm/IR/Constant.h"
@@ -926,7 +927,32 @@ MDNode *MDNode::getMostGenericAliasScope(MDNode *A, MDNode *B) {
926927
if (!A || !B)
927928
return nullptr;
928929

929-
return concatenate(A, B);
930+
// Take the intersection of domains then union the scopes
931+
// within those domains
932+
SmallPtrSet<const MDNode *, 16> ADomains;
933+
SmallPtrSet<const MDNode *, 16> IntersectDomains;
934+
SmallSetVector<Metadata *, 4> MDs;
935+
for (const MDOperand &MDOp : A->operands())
936+
if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
937+
if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
938+
ADomains.insert(Domain);
939+
940+
for (const MDOperand &MDOp : B->operands())
941+
if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
942+
if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
943+
if (ADomains.contains(Domain)) {
944+
IntersectDomains.insert(Domain);
945+
MDs.insert(MDOp);
946+
}
947+
948+
for (const MDOperand &MDOp : A->operands())
949+
if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
950+
if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
951+
if (IntersectDomains.contains(Domain))
952+
MDs.insert(MDOp);
953+
954+
return MDs.empty() ? nullptr
955+
: getOrSelfReference(A->getContext(), MDs.getArrayRef());
930956
}
931957

932958
MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; RUN: opt < %s -S -memcpyopt | FileCheck --match-full-lines %s
2+
3+
; Alias scopes are merged by taking the intersection of domains, then the union of the scopes within those domains
4+
define i8 @test(i8 %input) {
5+
%tmp = alloca i8
6+
%dst = alloca i8
7+
%src = alloca i8
8+
; CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %dst, i8* align 8 %src, i64 1, i1 false), !alias.scope ![[SCOPE:[0-9]+]]
9+
call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %src), !noalias !4
10+
store i8 %input, i8* %src
11+
call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %tmp, i8* align 8 %src, i64 1, i1 false), !alias.scope !0
12+
call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %src), !noalias !4
13+
call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %dst, i8* align 8 %tmp, i64 1, i1 false), !alias.scope !4
14+
%ret_value = load i8, i8* %dst
15+
ret i8 %ret_value
16+
}
17+
18+
; Merged scope contains "callee0: %a" and "callee0 : %b"
19+
; CHECK-DAG: ![[CALLEE0_A:[0-9]+]] = distinct !{!{{[0-9]+}}, !{{[0-9]+}}, !"callee0: %a"}
20+
; CHECK-DAG: ![[CALLEE0_B:[0-9]+]] = distinct !{!{{[0-9]+}}, !{{[0-9]+}}, !"callee0: %b"}
21+
; CHECK-DAG: ![[SCOPE]] = !{![[CALLEE0_A]], ![[CALLEE0_B]]}
22+
23+
declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)
24+
declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
25+
declare void @llvm.memcpy.p0i8.p0i8.i64(i8*, i8*, i64, i1)
26+
27+
!0 = !{!1, !7}
28+
!1 = distinct !{!1, !3, !"callee0: %a"}
29+
!2 = distinct !{!2, !3, !"callee0: %b"}
30+
!3 = distinct !{!3, !"callee0"}
31+
32+
!4 = !{!2, !5}
33+
!5 = distinct !{!5, !6, !"callee1: %a"}
34+
!6 = distinct !{!6, !"callee1"}
35+
36+
!7 = distinct !{!7, !8, !"callee2: %a"}
37+
!8 = distinct !{!8, !"callee2"}

‎llvm/test/Transforms/GVN/noalias.ll‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,40 @@ define i32 @test1(i32* %p, i32* %q) {
55
; CHECK: load i32, i32* %p
66
; CHECK-NOT: noalias
77
; CHECK: %c = add i32 %a, %a
8-
%a = load i32, i32* %p, !noalias !0
8+
%a = load i32, i32* %p, !noalias !3
99
%b = load i32, i32* %p
1010
%c = add i32 %a, %b
1111
ret i32 %c
1212
}
1313

1414
define i32 @test2(i32* %p, i32* %q) {
1515
; CHECK-LABEL: @test2(i32* %p, i32* %q)
16-
; CHECK: load i32, i32* %p, align 4, !alias.scope !0
16+
; CHECK: load i32, i32* %p, align 4, !alias.scope ![[SCOPE1:[0-9]+]]
1717
; CHECK: %c = add i32 %a, %a
18-
%a = load i32, i32* %p, !alias.scope !0
19-
%b = load i32, i32* %p, !alias.scope !0
18+
%a = load i32, i32* %p, !alias.scope !3
19+
%b = load i32, i32* %p, !alias.scope !3
2020
%c = add i32 %a, %b
2121
ret i32 %c
2222
}
2323

24-
; FIXME: In this case we can do better than intersecting the scopes, and can
25-
; concatenate them instead. Both loads are in the same basic block, the first
26-
; makes the second safe to speculatively execute, and there are no calls that may
27-
; throw in between.
2824
define i32 @test3(i32* %p, i32* %q) {
2925
; CHECK-LABEL: @test3(i32* %p, i32* %q)
30-
; CHECK: load i32, i32* %p, align 4, !alias.scope !1
26+
; CHECK: load i32, i32* %p, align 4, !alias.scope ![[SCOPE2:[0-9]+]]
3127
; CHECK: %c = add i32 %a, %a
32-
%a = load i32, i32* %p, !alias.scope !1
33-
%b = load i32, i32* %p, !alias.scope !2
28+
%a = load i32, i32* %p, !alias.scope !4
29+
%b = load i32, i32* %p, !alias.scope !5
3430
%c = add i32 %a, %b
3531
ret i32 %c
3632
}
3733

34+
; CHECK: ![[SCOPE1]] = !{!{{[0-9]+}}}
35+
; CHECK: ![[SCOPE2]] = !{!{{[0-9]+}}, !{{[0-9]+}}}
3836
declare i32 @foo(i32*) readonly
3937

40-
!0 = !{!0}
41-
!1 = !{!1}
42-
!2 = !{!0, !1}
38+
!0 = distinct !{!0, !2, !"callee0: %a"}
39+
!1 = distinct !{!1, !2, !"callee0: %b"}
40+
!2 = distinct !{!2, !"callee0"}
4341

42+
!3 = !{!0}
43+
!4 = !{!1}
44+
!5 = !{!0, !1}

‎llvm/test/Transforms/InstCombine/fold-phi-load-metadata.ll‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ return: ; preds = %if.end, %if.then
4040
; CHECK: ![[TBAA]] = !{![[TAG1:[0-9]+]], ![[TAG1]], i64 0}
4141
; CHECK: ![[TAG1]] = !{!"int", !{{[0-9]+}}, i64 0}
4242
; CHECK: ![[RANGE]] = !{i32 10, i32 25}
43-
; CHECK: ![[ALIAS_SCOPE]] = !{![[SCOPE0:[0-9]+]], ![[SCOPE2:[0-9]+]], ![[SCOPE1:[0-9]+]]}
43+
; CHECK: ![[ALIAS_SCOPE]] = !{![[SCOPE0:[0-9]+]], ![[SCOPE1:[0-9]+]], ![[SCOPE2:[0-9]+]]}
4444
; CHECK: ![[SCOPE0]] = distinct !{![[SCOPE0]], !{{[0-9]+}}, !"scope0"}
45-
; CHECK: ![[SCOPE2]] = distinct !{![[SCOPE2]], !{{[0-9]+}}, !"scope2"}
4645
; CHECK: ![[SCOPE1]] = distinct !{![[SCOPE1]], !{{[0-9]+}}, !"scope1"}
46+
; CHECK: ![[SCOPE2]] = distinct !{![[SCOPE2]], !{{[0-9]+}}, !"scope2"}
4747
; CHECK: ![[NOALIAS]] = !{![[SCOPE3:[0-9]+]]}
4848
; CHECK: ![[SCOPE3]] = distinct !{![[SCOPE3]], !{{[0-9]+}}, !"scope3"}
4949

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: opt < %s -S -memcpyopt | FileCheck --match-full-lines %s
2+
3+
; Make sure callslot optimization merges alias.scope metadata correctly when it merges instructions.
4+
; Merging here naively generates:
5+
; call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %dst, i8* align 8 %src, i64 1, i1 false), !alias.scope !3
6+
; call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %src), !noalias !0
7+
; ...
8+
; !0 = !{!1}
9+
; !1 = distinct !{!1, !2, !"callee1: %a"}
10+
; !2 = distinct !{!2, !"callee1"}
11+
; !3 = !{!1, !4}
12+
; !4 = distinct !{!4, !5, !"callee0: %a"}
13+
; !5 = distinct !{!5, !"callee0"}
14+
; Which is incorrect because the lifetime.end of %src will now "noalias" the above memcpy.
15+
define i8 @test(i8 %input) {
16+
%tmp = alloca i8
17+
%dst = alloca i8
18+
%src = alloca i8
19+
; NOTE: we're matching the full line and looking for the lack of !alias.scope here
20+
; CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %dst, i8* align 8 %src, i64 1, i1 false)
21+
call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %src), !noalias !3
22+
store i8 %input, i8* %src
23+
call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %tmp, i8* align 8 %src, i64 1, i1 false), !alias.scope !0
24+
call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %src), !noalias !3
25+
call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %dst, i8* align 8 %tmp, i64 1, i1 false), !alias.scope !3
26+
%ret_value = load i8, i8* %dst
27+
ret i8 %ret_value
28+
}
29+
30+
declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)
31+
declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
32+
declare void @llvm.memcpy.p0i8.p0i8.i64(i8*, i8*, i64, i1)
33+
34+
!0 = !{!1}
35+
!1 = distinct !{!1, !2, !"callee0: %a"}
36+
!2 = distinct !{!2, !"callee0"}
37+
!3 = !{!4}
38+
!4 = distinct !{!4, !5, !"callee1: %a"}
39+
!5 = distinct !{!5, !"callee1"}

‎llvm/test/Transforms/NewGVN/noalias.ll‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,40 @@ define i32 @test1(i32* %p, i32* %q) {
55
; CHECK: load i32, i32* %p
66
; CHECK-NOT: noalias
77
; CHECK: %c = add i32 %a, %a
8-
%a = load i32, i32* %p, !noalias !0
8+
%a = load i32, i32* %p, !noalias !3
99
%b = load i32, i32* %p
1010
%c = add i32 %a, %b
1111
ret i32 %c
1212
}
1313

1414
define i32 @test2(i32* %p, i32* %q) {
1515
; CHECK-LABEL: @test2(i32* %p, i32* %q)
16-
; CHECK: load i32, i32* %p, align 4, !alias.scope !0
16+
; CHECK: load i32, i32* %p, align 4, !alias.scope ![[SCOPE1:[0-9]+]]
1717
; CHECK: %c = add i32 %a, %a
18-
%a = load i32, i32* %p, !alias.scope !0
19-
%b = load i32, i32* %p, !alias.scope !0
18+
%a = load i32, i32* %p, !alias.scope !3
19+
%b = load i32, i32* %p, !alias.scope !3
2020
%c = add i32 %a, %b
2121
ret i32 %c
2222
}
2323

24-
; FIXME: In this case we can do better than intersecting the scopes, and can
25-
; concatenate them instead. Both loads are in the same basic block, the first
26-
; makes the second safe to speculatively execute, and there are no calls that may
27-
; throw in between.
2824
define i32 @test3(i32* %p, i32* %q) {
2925
; CHECK-LABEL: @test3(i32* %p, i32* %q)
30-
; CHECK: load i32, i32* %p, align 4, !alias.scope !1
26+
; CHECK: load i32, i32* %p, align 4, !alias.scope ![[SCOPE2:[0-9]+]]
3127
; CHECK: %c = add i32 %a, %a
32-
%a = load i32, i32* %p, !alias.scope !1
33-
%b = load i32, i32* %p, !alias.scope !2
28+
%a = load i32, i32* %p, !alias.scope !4
29+
%b = load i32, i32* %p, !alias.scope !5
3430
%c = add i32 %a, %b
3531
ret i32 %c
3632
}
3733

34+
; CHECK: ![[SCOPE1]] = !{!{{[0-9]+}}}
35+
; CHECK: ![[SCOPE2]] = !{!{{[0-9]+}}, !{{[0-9]+}}}
3836
declare i32 @foo(i32*) readonly
3937

40-
!0 = !{!0}
41-
!1 = !{!1}
42-
!2 = !{!0, !1}
38+
!0 = distinct !{!0, !2, !"callee0: %a"}
39+
!1 = distinct !{!1, !2, !"callee0: %b"}
40+
!2 = distinct !{!2, !"callee0"}
4341

42+
!3 = !{!0}
43+
!4 = !{!1}
44+
!5 = !{!0, !1}

0 commit comments

Comments
 (0)