LLVM 20.0.0git
MemoryModelRelaxationAnnotations.cpp
Go to the documentation of this file.
1//===- MemoryModelRelaxationAnnotations.cpp ---------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/StringSet.h"
12#include "llvm/IR/Metadata.h"
13#include "llvm/Support/Debug.h"
15
16using namespace llvm;
17
18//===- MMRAMetadata -------------------------------------------------------===//
19
21 : MMRAMetadata(I.getMetadata(LLVMContext::MD_mmra)) {}
22
24 if (!MD)
25 return;
26
27 // TODO: Split this into a "tryParse" function that can return an err.
28 // CTor can use the tryParse & just fatal on err.
29
30 MDTuple *Tuple = dyn_cast<MDTuple>(MD);
31 assert(Tuple && "Invalid MMRA structure");
32
33 const auto HandleTagMD = [this](MDNode *TagMD) {
34 Tags.insert({cast<MDString>(TagMD->getOperand(0))->getString(),
35 cast<MDString>(TagMD->getOperand(1))->getString()});
36 };
37
38 if (isTagMD(Tuple)) {
39 HandleTagMD(Tuple);
40 return;
41 }
42
43 for (const MDOperand &Op : Tuple->operands()) {
44 MDNode *MDOp = cast<MDNode>(Op.get());
45 assert(isTagMD(MDOp));
46 HandleTagMD(MDOp);
47 }
48}
49
51 if (auto *Tuple = dyn_cast<MDTuple>(MD)) {
52 return Tuple->getNumOperands() == 2 &&
53 isa<MDString>(Tuple->getOperand(0)) &&
54 isa<MDString>(Tuple->getOperand(1));
55 }
56 return false;
57}
58
60 StringRef Suffix) {
61 return MDTuple::get(Ctx,
62 {MDString::get(Ctx, Prefix), MDString::get(Ctx, Suffix)});
63}
64
67 if (Tags.empty())
68 return nullptr;
69
70 if (Tags.size() == 1)
71 return getTagMD(Ctx, Tags.front());
72
74 for (const auto &Tag : Tags)
75 MMRAs.push_back(getTagMD(Ctx, Tag));
76 return MDTuple::get(Ctx, MMRAs);
77}
78
80 const MMRAMetadata &B) {
81 // Let A and B be two tags set, and U be the prefix-wise union of A and B.
82 // For every unique tag prefix P present in A or B:
83 // * If either A or B has no tags with prefix P, no tags with prefix
84 // P are added to U.
85 // * If both A and B have at least one tag with prefix P, all tags with prefix
86 // P from both sets are added to U.
87
89
90 for (const auto &[P, S] : A) {
91 if (B.hasTagWithPrefix(P))
92 Result.push_back(getTagMD(Ctx, P, S));
93 }
94 for (const auto &[P, S] : B) {
95 if (A.hasTagWithPrefix(P))
96 Result.push_back(getTagMD(Ctx, P, S));
97 }
98
99 return MDTuple::get(Ctx, Result);
100}
101
102bool MMRAMetadata::hasTag(StringRef Prefix, StringRef Suffix) const {
103 return Tags.count({Prefix, Suffix});
104}
105
107 // Two sets of tags are compatible iff, for every unique tag prefix P
108 // present in at least one set:
109 // - the other set contains no tag with prefix P, or
110 // - at least one tag with prefix P is common to both sets.
111
112 StringMap<bool> PrefixStatuses;
113 for (const auto &[P, S] : Tags)
114 PrefixStatuses[P] |= (Other.hasTag(P, S) || !Other.hasTagWithPrefix(P));
115 for (const auto &[P, S] : Other)
116 PrefixStatuses[P] |= (hasTag(P, S) || !hasTagWithPrefix(P));
117
118 for (auto &[Prefix, Status] : PrefixStatuses) {
119 if (!Status)
120 return false;
121 }
122
123 return true;
124}
125
127 for (const auto &[P, S] : Tags)
128 if (P == Prefix)
129 return true;
130 return false;
131}
132
134 return Tags.begin();
135}
136
138
139bool MMRAMetadata::empty() const { return Tags.empty(); }
140
141unsigned MMRAMetadata::size() const { return Tags.size(); }
142
144 bool IsFirst = true;
145 // TODO: use map_iter + join
146 for (const auto &[P, S] : Tags) {
147 if (IsFirst)
148 IsFirst = false;
149 else
150 OS << ", ";
151 OS << P << ":" << S;
152 }
153}
154
156void MMRAMetadata::dump() const { print(dbgs()); }
157
158//===- Helpers ------------------------------------------------------------===//
159
160static bool isReadWriteMemCall(const Instruction &I) {
161 if (const auto *C = dyn_cast<CallBase>(&I))
162 return C->mayReadOrWriteMemory() ||
163 !C->getMemoryEffects().doesNotAccessMemory();
164 return false;
165}
166
168 return isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicCmpXchgInst>(I) ||
169 isa<AtomicRMWInst>(I) || isa<FenceInst>(I) || isReadWriteMemCall(I);
170}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:533
#define I(x, y, z)
Definition: MD5.cpp:58
static bool isReadWriteMemCall(const Instruction &I)
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
This file contains the declarations for metadata subclasses.
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
StringSet - A set-like wrapper for the StringMap.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:168
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
This class represents an Operation in the Expression.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Metadata node.
Definition: Metadata.h:1069
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1428
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:891
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:606
Tuple of metadata.
Definition: Metadata.h:1472
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1499
Helper class to manipulate !mmra metadata nodes.
static MDTuple * getTagMD(LLVMContext &Ctx, StringRef Prefix, StringRef Suffix)
Creates !mmra metadata for a single tag.
void print(raw_ostream &OS) const
static MDNode * combine(LLVMContext &Ctx, const MMRAMetadata &A, const MMRAMetadata &B)
Combines A and B according to MMRA semantics.
static MDTuple * getMD(LLVMContext &Ctx, ArrayRef< TagT > Tags)
Creates !mmra metadata from Tags.
bool hasTag(StringRef Prefix, StringRef Suffix) const
MMRAMetadata()=default
bool isCompatibleWith(const MMRAMetadata &Other) const
bool hasTagWithPrefix(StringRef Prefix) const
static bool isTagMD(const Metadata *MD)
Root of the metadata hierarchy.
Definition: Metadata.h:62
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
size_type size() const
Definition: DenseSet.h:81
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool canInstructionHaveMMRAs(const Instruction &I)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ Other
Any other memory.