1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
//===- MemoryModelRelaxationAnnotations.cpp ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/MemoryModelRelaxationAnnotations.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Metadata.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
//===- MMRAMetadata -------------------------------------------------------===//
MMRAMetadata::MMRAMetadata(const Instruction &I)
: MMRAMetadata(I.getMetadata(LLVMContext::MD_mmra)) {}
MMRAMetadata::MMRAMetadata(MDNode *MD) {
if (!MD)
return;
// TODO: Split this into a "tryParse" function that can return an err.
// CTor can use the tryParse & just fatal on err.
MDTuple *Tuple = dyn_cast<MDTuple>(MD);
assert(Tuple && "Invalid MMRA structure");
const auto HandleTagMD = [this](MDNode *TagMD) {
Tags.insert({cast<MDString>(TagMD->getOperand(0))->getString(),
cast<MDString>(TagMD->getOperand(1))->getString()});
};
if (isTagMD(Tuple)) {
HandleTagMD(Tuple);
return;
}
for (const MDOperand &Op : Tuple->operands()) {
MDNode *MDOp = cast<MDNode>(Op.get());
assert(isTagMD(MDOp));
HandleTagMD(MDOp);
}
}
bool MMRAMetadata::isTagMD(const Metadata *MD) {
if (auto *Tuple = dyn_cast<MDTuple>(MD)) {
return Tuple->getNumOperands() == 2 &&
isa<MDString>(Tuple->getOperand(0)) &&
isa<MDString>(Tuple->getOperand(1));
}
return false;
}
MDTuple *MMRAMetadata::getTagMD(LLVMContext &Ctx, StringRef Prefix,
StringRef Suffix) {
return MDTuple::get(Ctx,
{MDString::get(Ctx, Prefix), MDString::get(Ctx, Suffix)});
}
MDTuple *MMRAMetadata::getMD(LLVMContext &Ctx,
ArrayRef<MMRAMetadata::TagT> Tags) {
if (Tags.empty())
return nullptr;
if (Tags.size() == 1)
return getTagMD(Ctx, Tags.front());
SmallVector<Metadata *> MMRAs;
for (const auto &Tag : Tags)
MMRAs.push_back(getTagMD(Ctx, Tag));
return MDTuple::get(Ctx, MMRAs);
}
MDNode *MMRAMetadata::combine(LLVMContext &Ctx, const MMRAMetadata &A,
const MMRAMetadata &B) {
// Let A and B be two tags set, and U be the prefix-wise union of A and B.
// For every unique tag prefix P present in A or B:
// * If either A or B has no tags with prefix P, no tags with prefix
// P are added to U.
// * If both A and B have at least one tag with prefix P, all tags with prefix
// P from both sets are added to U.
SmallVector<Metadata *> Result;
for (const auto &[P, S] : A) {
if (B.hasTagWithPrefix(P))
Result.push_back(getTagMD(Ctx, P, S));
}
for (const auto &[P, S] : B) {
if (A.hasTagWithPrefix(P))
Result.push_back(getTagMD(Ctx, P, S));
}
return MDTuple::get(Ctx, Result);
}
bool MMRAMetadata::hasTag(StringRef Prefix, StringRef Suffix) const {
return Tags.count({Prefix, Suffix});
}
bool MMRAMetadata::isCompatibleWith(const MMRAMetadata &Other) const {
// Two sets of tags are compatible iff, for every unique tag prefix P
// present in at least one set:
// - the other set contains no tag with prefix P, or
// - at least one tag with prefix P is common to both sets.
StringMap<bool> PrefixStatuses;
for (const auto &[P, S] : Tags)
PrefixStatuses[P] |= (Other.hasTag(P, S) || !Other.hasTagWithPrefix(P));
for (const auto &[P, S] : Other)
PrefixStatuses[P] |= (hasTag(P, S) || !hasTagWithPrefix(P));
for (auto &[Prefix, Status] : PrefixStatuses) {
if (!Status)
return false;
}
return true;
}
bool MMRAMetadata::hasTagWithPrefix(StringRef Prefix) const {
for (const auto &[P, S] : Tags)
if (P == Prefix)
return true;
return false;
}
MMRAMetadata::const_iterator MMRAMetadata::begin() const {
return Tags.begin();
}
MMRAMetadata::const_iterator MMRAMetadata::end() const { return Tags.end(); }
bool MMRAMetadata::empty() const { return Tags.empty(); }
unsigned MMRAMetadata::size() const { return Tags.size(); }
void MMRAMetadata::print(raw_ostream &OS) const {
bool IsFirst = true;
// TODO: use map_iter + join
for (const auto &[P, S] : Tags) {
if (IsFirst)
IsFirst = false;
else
OS << ", ";
OS << P << ":" << S;
}
}
LLVM_DUMP_METHOD
void MMRAMetadata::dump() const { print(dbgs()); }
//===- Helpers ------------------------------------------------------------===//
static bool isReadWriteMemCall(const Instruction &I) {
if (const auto *C = dyn_cast<CallBase>(&I))
return C->mayReadOrWriteMemory() ||
!C->getMemoryEffects().doesNotAccessMemory();
return false;
}
bool llvm::canInstructionHaveMMRAs(const Instruction &I) {
return isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) ||
isa<AtomicRMWInst>(I) || isa<FenceInst>(I) || isReadWriteMemCall(I);
}
|