[IR] Introduce an appendTags() idiom to set MMRA metadata [NFC] (#199621)

This is a simple set-union of new tags and existing tags. This is safer
than directly setting metadata, which can over-write existing MMRAs.

Assisted-By: Claude Opus 4.6

---------

Co-authored-by: Pierre van Houtryve <pierre.vanhoutryve@amd.com>
This commit is contained in:
Sameer Sahasrabuddhe 2026-05-27 10:57:19 +05:30 committed by GitHub
parent 4ee7e900f3
commit 4f54064a6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 20 additions and 4 deletions

View file

@ -440,7 +440,6 @@ void CodeGenFunction::AddAMDGPUFenceAddressSpaceMMRA(llvm::Instruction *Inst,
const CallExpr *E) {
constexpr const char *Tag = "amdgpu-synchronize-as";
LLVMContext &Ctx = Inst->getContext();
SmallVector<MMRAMetadata::TagT, 3> MMRAs;
for (unsigned K = 2; K < E->getNumArgs(); ++K) {
llvm::Value *V = EmitScalarExpr(E->getArg(K));
@ -454,9 +453,7 @@ void CodeGenFunction::AddAMDGPUFenceAddressSpaceMMRA(llvm::Instruction *Inst,
"expected an address space name as a string literal");
}
llvm::sort(MMRAs);
MMRAs.erase(llvm::unique(MMRAs), MMRAs.end());
Inst->setMetadata(LLVMContext::MD_mmra, MMRAMetadata::getMD(Ctx, MMRAs));
MMRAMetadata::appendTags(*Inst, MMRAs);
}
static Value *GetAMDGPUPredicate(CodeGenFunction &CGF, Twine Name) {

View file

@ -83,6 +83,10 @@ public:
/// \returns true if \p MD is a well-formed MMRA tag.
LLVM_ABI static bool isTagMD(const Metadata *MD);
/// Appends \p Tags to the !mmra metadata on \p I,
/// merging with any existing MMRA metadata.
LLVM_ABI static void appendTags(Instruction &I, ArrayRef<TagT> Tags);
/// @}
/// \name Compatibility Helpers

View file

@ -98,6 +98,21 @@ MDNode *MMRAMetadata::combine(LLVMContext &Ctx, const MMRAMetadata &A,
return MDTuple::get(Ctx, Result);
}
void MMRAMetadata::appendTags(Instruction &I, ArrayRef<TagT> Tags) {
if (Tags.empty())
return;
SmallVector<MMRAMetadata::TagT> MMRAs(Tags);
LLVMContext &Ctx = I.getContext();
if (MDNode *Existing = I.getMetadata(LLVMContext::MD_mmra)) {
// Merge with existing MMRA tags.
MMRAMetadata Parsed(Existing);
MMRAs.append(Parsed.begin(), Parsed.end());
}
llvm::sort(MMRAs);
MMRAs.erase(llvm::unique(MMRAs), MMRAs.end());
I.setMetadata(LLVMContext::MD_mmra, MMRAMetadata::getMD(Ctx, MMRAs));
}
bool MMRAMetadata::hasTag(StringRef Prefix, StringRef Suffix) const {
return Tags.count({Prefix, Suffix});
}