[llvm] [MIParser] - Add support for MMRAs (PR #180320)

Ryan Mitchell via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 9 07:41:37 PST 2026


https://github.com/RyanRio updated https://github.com/llvm/llvm-project/pull/180320

>From 953c8848e90b02dbc197a5a857b6f381ea646bb4 Mon Sep 17 00:00:00 2001
From: Ryan Mitchell <Ryan.Mitchell at amd.com>
Date: Fri, 6 Feb 2026 21:01:26 -0800
Subject: [PATCH 1/5] [MIParser] - Add support for MMRAs

---
 llvm/lib/CodeGen/MIRParser/MILexer.cpp      |  1 +
 llvm/lib/CodeGen/MIRParser/MILexer.h        |  1 +
 llvm/lib/CodeGen/MIRParser/MIParser.cpp     | 25 +++++++++++++++++++--
 llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir | 25 +++++++++++++++++++++
 4 files changed, 50 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir

diff --git a/llvm/lib/CodeGen/MIRParser/MILexer.cpp b/llvm/lib/CodeGen/MIRParser/MILexer.cpp
index f9852c6c889b8..a8f656820e306 100644
--- a/llvm/lib/CodeGen/MIRParser/MILexer.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MILexer.cpp
@@ -294,6 +294,7 @@ static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
             MIToken::kw_machine_block_address_taken)
       .Case("call-frame-size", MIToken::kw_call_frame_size)
       .Case("noconvergent", MIToken::kw_noconvergent)
+      .Case("mmra", MIToken::kw_mmra)
       .Default(MIToken::Identifier);
 }
 
diff --git a/llvm/lib/CodeGen/MIRParser/MILexer.h b/llvm/lib/CodeGen/MIRParser/MILexer.h
index a9dc9af6858da..1284973df5dd2 100644
--- a/llvm/lib/CodeGen/MIRParser/MILexer.h
+++ b/llvm/lib/CodeGen/MIRParser/MILexer.h
@@ -147,6 +147,7 @@ struct MIToken {
     kw_machine_block_address_taken,
     kw_call_frame_size,
     kw_noconvergent,
+    kw_mmra,
 
     // Metadata types.
     kw_distinct,
diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.cpp b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
index 1071cf00aa843..a3b27061dcb18 100644
--- a/llvm/lib/CodeGen/MIRParser/MIParser.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
@@ -523,6 +523,7 @@ class MIParser {
   bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol);
   bool parseHeapAllocMarker(MDNode *&Node);
   bool parsePCSections(MDNode *&Node);
+  bool parseMMRA(MDNode *&Node);
 
   bool parseTargetImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
                               MachineOperand &Dest, const MIRFormatter &MF);
@@ -1077,7 +1078,7 @@ bool MIParser::parse(MachineInstr *&MI) {
   while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_pre_instr_symbol) &&
          Token.isNot(MIToken::kw_post_instr_symbol) &&
          Token.isNot(MIToken::kw_heap_alloc_marker) &&
-         Token.isNot(MIToken::kw_pcsections) &&
+         Token.isNot(MIToken::kw_pcsections) && Token.isNot(MIToken::kw_mmra) &&
          Token.isNot(MIToken::kw_cfi_type) &&
          Token.isNot(MIToken::kw_deactivation_symbol) &&
          Token.isNot(MIToken::kw_debug_location) &&
@@ -1113,7 +1114,9 @@ bool MIParser::parse(MachineInstr *&MI) {
   if (Token.is(MIToken::kw_pcsections))
     if (parsePCSections(PCSections))
       return true;
-
+  MDNode *MMRA = nullptr;
+  if (Token.is(MIToken::kw_mmra) && parseMMRA(MMRA))
+    return true;
   unsigned CFIType = 0;
   if (Token.is(MIToken::kw_cfi_type)) {
     lex();
@@ -1210,6 +1213,8 @@ bool MIParser::parse(MachineInstr *&MI) {
     MI->setHeapAllocMarker(MF, HeapAllocMarker);
   if (PCSections)
     MI->setPCSections(MF, PCSections);
+  if (MMRA)
+    MI->setMMRAMetadata(MF, MMRA);
   if (CFIType)
     MI->setCFIType(MF, CFIType);
   if (DS)
@@ -3614,6 +3619,22 @@ bool MIParser::parsePCSections(MDNode *&Node) {
   return false;
 }
 
+bool MIParser::parseMMRA(MDNode *&Node) {
+  assert(Token.is(MIToken::kw_mmra) && "Invalid token for MMRA!");
+  lex();
+  if (parseMDNode(Node))
+    return true;
+  if (!Node)
+    return error("expected a MDNode after 'mmra'");
+  if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
+      Token.is(MIToken::lbrace))
+    return false;
+  if (Token.isNot(MIToken::comma))
+    return error("expected ',' before the next machine operand");
+  lex();
+  return false;
+}
+
 static void initSlots2BasicBlocks(
     const Function &F,
     DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
diff --git a/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir
new file mode 100644
index 0000000000000..1b72a297d6d8f
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir
@@ -0,0 +1,25 @@
+# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -o - %s | FileCheck %s
+# This test ensures that the MIR parser parses mmra metadata correctly.
+
+--- |
+
+  define void @test_mmra(ptr addrspace(1) %ptr) {
+  entry:
+    fence syncscope("workgroup") release, mmra !0
+    ret void
+  }
+
+  !0 = !{!"amdgpu-synchronize-as", !"local"}
+
+...
+---
+name:            test_mmra
+body:             |
+  bb.0.entry:
+    liveins: $vgpr0, $vgpr1
+
+    ; CHECK-LABEL: name: test_mmra
+    ; CHECK: ATOMIC_FENCE 5, 2, mmra !0
+    ATOMIC_FENCE 5, 2, mmra !0
+    S_ENDPGM 0
+...

>From cb080288276bf370a42549f2a1f43906add9ed2e Mon Sep 17 00:00:00 2001
From: Ryan Mitchell <Ryan.Mitchell at amd.com>
Date: Sat, 7 Feb 2026 15:27:23 -0800
Subject: [PATCH 2/5] add error tests

---
 llvm/lib/CodeGen/MIRParser/MIParser.cpp       |  2 -
 .../CodeGen/MIR/AMDGPU/instr-mmra-error.mir   | 49 +++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir

diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.cpp b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
index a3b27061dcb18..17bd15fc64c52 100644
--- a/llvm/lib/CodeGen/MIRParser/MIParser.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
@@ -3624,8 +3624,6 @@ bool MIParser::parseMMRA(MDNode *&Node) {
   lex();
   if (parseMDNode(Node))
     return true;
-  if (!Node)
-    return error("expected a MDNode after 'mmra'");
   if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
       Token.is(MIToken::lbrace))
     return false;
diff --git a/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir
new file mode 100644
index 0000000000000..0ec9fb5d6a134
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir
@@ -0,0 +1,49 @@
+# RUN: split-file %s %t
+# RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -o /dev/null %t/expected-id.mir 2>&1 | FileCheck -check-prefix=ERR0 %s
+# RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -o /dev/null %t/undefined-id.mir 2>&1 | FileCheck -check-prefix=ERR1 %s
+# RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -o /dev/null %t/missing-comma.mir 2>&1 | FileCheck -check-prefix=ERR2 %s
+
+;--- expected-id.mir
+# ERR0: expected metadata id after '!'
+--- |
+  define void @test_expected_id() { unreachable }
+  !0 = !{!"amdgpu-synchronize-as", !"local"}
+...
+---
+name:            test_expected_id
+body:             |
+  bb.0:
+    liveins: $vgpr0, $vgpr1
+    ATOMIC_FENCE 5, 2, mmra ! _
+    S_ENDPGM 0
+...
+
+;--- undefined-id.mir
+# ERR1: use of undefined metadata '!1'
+--- |
+  define void @test_undefined_id() { unreachable }
+  !0 = !{!"amdgpu-synchronize-as", !"local"}
+...
+---
+name:            test_undefined_id
+body:             |
+  bb.0:
+    liveins: $vgpr0, $vgpr1
+    ATOMIC_FENCE 5, 2, mmra !1
+    S_ENDPGM 0
+...
+
+;--- missing-comma.mir
+# ERR2: expected ',' before the next machine operand
+--- |
+  define void @test_missing_comma() { unreachable }
+  !0 = !{!"amdgpu-synchronize-as", !"local"}
+...
+---
+name:            test_missing_comma
+body:             |
+  bb.0:
+    liveins: $vgpr0, $vgpr1
+    ATOMIC_FENCE 5, 2, mmra !0 implicit $exec
+    S_ENDPGM 0
+...

>From f1dc9f3c15b2b52d77379464603882c63d213cda Mon Sep 17 00:00:00 2001
From: Ryan Mitchell <Ryan.Mitchell at amd.com>
Date: Sat, 7 Feb 2026 16:12:18 -0800
Subject: [PATCH 3/5] Missing ! in ll

---
 llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir
index 1b72a297d6d8f..433d43b69a817 100644
--- a/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir
+++ b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra.mir
@@ -5,7 +5,7 @@
 
   define void @test_mmra(ptr addrspace(1) %ptr) {
   entry:
-    fence syncscope("workgroup") release, mmra !0
+    fence syncscope("workgroup") release, !mmra !0
     ret void
   }
 

>From e79f820722358550a645415d19985911624f68f1 Mon Sep 17 00:00:00 2001
From: Ryan Mitchell <Ryan.Mitchell at amd.com>
Date: Mon, 9 Feb 2026 06:56:48 -0800
Subject: [PATCH 4/5] Remove extra line

Co-authored-by: Matt Arsenault <arsenm2 at gmail.com>
---
 llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir
index 0ec9fb5d6a134..43cb93fe37305 100644
--- a/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir
+++ b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir
@@ -1,7 +1,9 @@
 # RUN: split-file %s %t
 # RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -o /dev/null %t/expected-id.mir 2>&1 | FileCheck -check-prefix=ERR0 %s
 # RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -o /dev/null %t/undefined-id.mir 2>&1 | FileCheck -check-prefix=ERR1 %s
-# RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -o /dev/null %t/missing-comma.mir 2>&1 | FileCheck -check-prefix=ERR2 %s
+# RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -filetype=null %t/expected-id.mir 2>&1 | FileCheck -check-prefix=ERR0 %s
+# RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -filetype=null %t/undefined-id.mir 2>&1 | FileCheck -check-prefix=ERR1 %s
+# RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -filetype=null  %t/missing-comma.mir 2>&1 | FileCheck -check-prefix=ERR2 %s
 
 ;--- expected-id.mir
 # ERR0: expected metadata id after '!'

>From afdb999f6146923cfdc8ac324eee33332ac4dc59 Mon Sep 17 00:00:00 2001
From: Ryan Mitchell <Ryan.Mitchell at amd.com>
Date: Mon, 9 Feb 2026 07:41:24 -0800
Subject: [PATCH 5/5] Update instr-mmra-error.mir

---
 llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir | 2 --
 1 file changed, 2 deletions(-)

diff --git a/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir
index 43cb93fe37305..8d2d75136383c 100644
--- a/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir
+++ b/llvm/test/CodeGen/MIR/AMDGPU/instr-mmra-error.mir
@@ -1,6 +1,4 @@
 # RUN: split-file %s %t
-# RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -o /dev/null %t/expected-id.mir 2>&1 | FileCheck -check-prefix=ERR0 %s
-# RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -o /dev/null %t/undefined-id.mir 2>&1 | FileCheck -check-prefix=ERR1 %s
 # RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -filetype=null %t/expected-id.mir 2>&1 | FileCheck -check-prefix=ERR0 %s
 # RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -filetype=null %t/undefined-id.mir 2>&1 | FileCheck -check-prefix=ERR1 %s
 # RUN: not llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1010 -run-pass=none -filetype=null  %t/missing-comma.mir 2>&1 | FileCheck -check-prefix=ERR2 %s



More information about the llvm-commits mailing list