Skip to content

Commit 43d76ba

Browse files
committed
Add getMD API
It's useful for clang codegen.
1 parent 5a4e1b4 commit 43d76ba

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

llvm/include/llvm/IR/MemoryModelRelaxationAnnotations.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
namespace llvm {
2626

27+
template<typename T>
28+
class ArrayRef;
29+
2730
class MDNode;
2831
class MDTuple;
2932
class Metadata;
@@ -72,6 +75,10 @@ class MMRAMetadata {
7275
return getTagMD(Ctx, T.first, T.second);
7376
}
7477

78+
/// Creates !mmra metadata from \p Tags.
79+
/// \returns nullptr or a MDTuple* from \p Tags.
80+
static MDTuple *getMD(LLVMContext &Ctx, ArrayRef<TagT> Tags);
81+
7582
/// \returns true if \p MD is a well-formed MMRA tag.
7683
static bool isTagMD(const Metadata *MD);
7784

llvm/lib/IR/MemoryModelRelaxationAnnotations.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ MDTuple *MMRAMetadata::getTagMD(LLVMContext &Ctx, StringRef Prefix,
6262
{MDString::get(Ctx, Prefix), MDString::get(Ctx, Suffix)});
6363
}
6464

65+
MDTuple *MMRAMetadata::getMD(LLVMContext &Ctx, ArrayRef<MMRAMetadata::TagT> Tags) {
66+
if (Tags.empty())
67+
return nullptr;
68+
69+
if (Tags.size() == 1)
70+
return getTagMD(Ctx, Tags.front());
71+
72+
SmallVector<Metadata*> MMRAs;
73+
for(const auto &Tag: Tags)
74+
MMRAs.push_back(getTagMD(Ctx, Tag));
75+
return MDTuple::get(Ctx, MMRAs);
76+
}
77+
6578
MDNode *MMRAMetadata::combine(LLVMContext &Ctx, const MMRAMetadata &A,
6679
const MMRAMetadata &B) {
6780
// Let A and B be two tags set, and U be the prefix-wise union of A and B.

llvm/unittests/IR/MemoryModelRelaxationAnnotationsTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ TEST(MMRATest, MDParse) {
5252
checkMMRA(NestedMMRA, {{"foo", "bar"}, {"bux", "qux"}});
5353
}
5454

55+
TEST(MMRATest, GetMD) {
56+
LLVMContext Ctx;
57+
58+
EXPECT_EQ(MMRAMetadata::getMD(Ctx, {}), nullptr);
59+
60+
MDTuple* SingleMD = MMRAMetadata::getMD(Ctx, {{"foo", "bar"}});
61+
EXPECT_EQ(SingleMD->getNumOperands(), 2);
62+
EXPECT_EQ(cast<MDString>(SingleMD->getOperand(0))->getString(), "foo");
63+
EXPECT_EQ(cast<MDString>(SingleMD->getOperand(1))->getString(), "bar");
64+
65+
MDTuple* MultiMD = MMRAMetadata::getMD(Ctx, {{"foo", "bar"}, {"bux", "qux"}});
66+
EXPECT_EQ(MultiMD->getNumOperands(), 2);
67+
68+
MDTuple* FooBar = cast<MDTuple>(MultiMD->getOperand(0));
69+
EXPECT_EQ(cast<MDString>(FooBar->getOperand(0))->getString(), "foo");
70+
EXPECT_EQ(cast<MDString>(FooBar->getOperand(1))->getString(), "bar");
71+
MDTuple* BuxQux = cast<MDTuple>(MultiMD->getOperand(1));
72+
EXPECT_EQ(cast<MDString>(BuxQux->getOperand(0))->getString(), "bux");
73+
EXPECT_EQ(cast<MDString>(BuxQux->getOperand(1))->getString(), "qux");
74+
}
75+
5576
TEST(MMRATest, Utility) {
5677
LLVMContext Ctx;
5778
MMRAMetadata MMRA =

0 commit comments

Comments
 (0)