https://github.com/isanbard created 
https://github.com/llvm/llvm-project/pull/237461

An x86 inline asm call or jmp whose target is a function, given as a "p" 
operand or as a "*m" operand that ISel turns into an address, gets a flag of 
kind Func. Like a Mem flag, it carries a memory constraint. But getKindName() 
returned "mem" for it, and the MIR printer printed the constraint only for a 
Mem flag. So this:

    call void asm sideeffect "call ${0:P}", "p,~{dirflag},~{fpsr},~{flags}"(ptr 
@g)

was printed as:

    INLINEASM &"call ${0:P}", sideeffect attdialect, mem ops:5, 
target-flags(x86-plt) @g, 1, $noreg, 0, $noreg, ...

which parses back as a Mem flag with no constraint. Printing that again hits 
llvm_unreachable, and the asm printer asserts in PrintMemReference() instead of 
emitting "callq g@PLT". MachineInstr::print() also printed the constraint as a 
register class (e.g. "mem:GR8_NOREX").

Name the kind "func", and print and parse its constraint as for a Mem flag. No 
in-tree test had a Func flag, so no existing tests change.

Assisted-by: Claude Opus 5.5


>From 9e830e1dc805ee043a496012b9dc87129215b13c Mon Sep 17 00:00:00 2001
From: Bill Wendling <[email protected]>
Date: Fri, 30 Oct 2026 18:58:28 -0700
Subject: [PATCH] [MIR] Round-trip the "func" kind of inline asm operand flags

An x86 inline asm call or jmp whose target is a function, given as a "p"
operand or as a "*m" operand that ISel turns into an address, gets a flag
of kind Func. Like a Mem flag, it carries a memory constraint. But
getKindName() returned "mem" for it, and the MIR printer printed the
constraint only for a Mem flag. So this:

    call void asm sideeffect "call ${0:P}", "p,~{dirflag},~{fpsr},~{flags}"(ptr 
@g)

was printed as:

    INLINEASM &"call ${0:P}", sideeffect attdialect, mem ops:5, 
target-flags(x86-plt) @g, 1, $noreg, 0, $noreg, ...

which parses back as a Mem flag with no constraint. Printing that again
hits llvm_unreachable, and the asm printer asserts in PrintMemReference()
instead of emitting "callq g@PLT". MachineInstr::print() also printed the
constraint as a register class (e.g. "mem:GR8_NOREX").

Name the kind "func", and print and parse its constraint as for a Mem
flag. No in-tree test had a Func flag, so no existing tests change.

Assisted-by: Claude Opus 5.5
---
 llvm/include/llvm/IR/InlineAsm.h              |  3 +-
 llvm/lib/CodeGen/MIRParser/MIParser.cpp       |  3 +-
 llvm/lib/CodeGen/MIRPrinter.cpp               |  2 +-
 llvm/lib/CodeGen/MachineInstr.cpp             |  5 ++-
 .../CodeGen/MIR/X86/inline-asm-func-kind.ll   | 38 +++++++++++++++++++
 5 files changed, 46 insertions(+), 5 deletions(-)
 create mode 100644 llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll

diff --git a/llvm/include/llvm/IR/InlineAsm.h b/llvm/include/llvm/IR/InlineAsm.h
index 4bcbdeab6a1ef7..f3bd1b10baf86d 100644
--- a/llvm/include/llvm/IR/InlineAsm.h
+++ b/llvm/include/llvm/IR/InlineAsm.h
@@ -350,8 +350,9 @@ class InlineAsm final : public Value {
       case Kind::Imm:
         return "imm";
       case Kind::Mem:
-      case Kind::Func:
         return "mem";
+      case Kind::Func:
+        return "func";
       }
       llvm_unreachable("impossible kind");
     }
diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.cpp 
b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
index 2bfa3afe12dc39..236f4d8aecd12d 100644
--- a/llvm/lib/CodeGen/MIRParser/MIParser.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
@@ -1879,6 +1879,7 @@ bool MIParser::parseSymbolicInlineAsmOperand(unsigned 
OpIdx,
           .Case("clobber", InlineAsm::Kind::Clobber)
           .Case("imm", InlineAsm::Kind::Imm)
           .Case("mem", InlineAsm::Kind::Mem)
+          .Case("func", InlineAsm::Kind::Func)
           .Default(InvalidKind);
   if (K == InvalidKind)
     return error("unknown inline asm operand kind '" + KindStr + "'");
@@ -1918,7 +1919,7 @@ bool MIParser::parseSymbolicInlineAsmOperand(unsigned 
OpIdx,
           "expected register class or memory constraint name after ':'");
 
     StringRef ConstraintStr = Token.stringValue();
-    if (F.isMemKind()) {
+    if (F.isMemKind() || F.isFuncKind()) {
       InlineAsm::ConstraintCode CC =
           StringSwitch<InlineAsm::ConstraintCode>(ConstraintStr)
               .Case("es", InlineAsm::ConstraintCode::es)
diff --git a/llvm/lib/CodeGen/MIRPrinter.cpp b/llvm/lib/CodeGen/MIRPrinter.cpp
index 7a4958c6073649..2be8aaf26632e8 100644
--- a/llvm/lib/CodeGen/MIRPrinter.cpp
+++ b/llvm/lib/CodeGen/MIRPrinter.cpp
@@ -1019,7 +1019,7 @@ static void printMIOperand(raw_ostream &OS, MFPrintState 
&State,
         if (IsReg && F.hasRegClassConstraint(RCID))
           OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
 
-        if (F.isMemKind()) {
+        if (F.isMemKind() || F.isFuncKind()) {
           InlineAsm::ConstraintCode MCID = F.getMemoryConstraintID();
           OS << ':' << InlineAsm::getMemConstraintName(MCID);
         }
diff --git a/llvm/lib/CodeGen/MachineInstr.cpp 
b/llvm/lib/CodeGen/MachineInstr.cpp
index 4a05542ed5d8f0..c8b0ff2e991eb9 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -2006,14 +2006,15 @@ void MachineInstr::print(raw_ostream &OS, 
ModuleSlotTracker &MST,
       OS << F.getKindName();
 
       unsigned RCID;
-      if (!F.isImmKind() && !F.isMemKind() && F.hasRegClassConstraint(RCID)) {
+      if (!F.isImmKind() && !F.isMemKind() && !F.isFuncKind() &&
+          F.hasRegClassConstraint(RCID)) {
         if (TRI) {
           OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
         } else
           OS << ":RC" << RCID;
       }
 
-      if (F.isMemKind()) {
+      if (F.isMemKind() || F.isFuncKind()) {
         const InlineAsm::ConstraintCode MCID = F.getMemoryConstraintID();
         OS << ":" << InlineAsm::getMemConstraintName(MCID);
       }
diff --git a/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll 
b/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll
new file mode 100644
index 00000000000000..de6c3f183c16ca
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll
@@ -0,0 +1,38 @@
+; RUN: llc -mtriple=x86_64-linux-gnu -global-isel=0 -stop-after=finalize-isel 
%s -o %t.mir
+; RUN: FileCheck %s --check-prefix=MIR < %t.mir
+; RUN: llc -mtriple=x86_64-linux-gnu -run-pass=none %t.mir -o - | FileCheck %s 
--check-prefix=MIR
+; RUN: llc -mtriple=x86_64-linux-gnu -start-after=finalize-isel %t.mir -o - | 
FileCheck %s
+; RUN: llc -mtriple=x86_64-linux-gnu -global-isel=0 %s -o - | FileCheck %s
+; RUN: llc -mtriple=x86_64-linux-gnu -global-isel=0 -print-after=finalize-isel 
%s -o /dev/null 2>&1 | FileCheck %s --check-prefix=PRINT
+
+; Test that the "func" flag of a function operand of an inline asm call, and
+; its constraint, survive a MIR round trip and are printed.
+
+declare void @g()
+
+define void @test_p() {
+; MIR-LABEL: name: test_p
+; MIR: INLINEASM &"call ${0:P}", sideeffect attdialect, func:p ops:5, 
target-flags(x86-plt) @g, 1, $noreg, 0, $noreg,
+;
+; CHECK-LABEL: test_p:
+; CHECK: callq g@PLT
+;
+; PRINT-LABEL: # Machine code for function test_p:
+; PRINT: INLINEASM &"call ${0:P}" [sideeffect] [attdialect], $0:[func:p], 
target-flags(x86-plt) @g, 1, $noreg, 0, $noreg,
+  call void asm sideeffect "call ${0:P}", "p,~{dirflag},~{fpsr},~{flags}"(ptr 
@g)
+  ret void
+}
+
+; A "*m" call target is turned into an address, like "p".
+define void @test_indirect_m() {
+; MIR-LABEL: name: test_indirect_m
+; MIR: INLINEASM &"call ${0:P}", sideeffect mayload attdialect, func:m ops:5, 
target-flags(x86-plt) @g, 1, $noreg, 0, $noreg,
+;
+; CHECK-LABEL: test_indirect_m:
+; CHECK: callq g@PLT
+;
+; PRINT-LABEL: # Machine code for function test_indirect_m:
+; PRINT: INLINEASM &"call ${0:P}" [sideeffect] [mayload] [attdialect], 
$0:[func:m], target-flags(x86-plt) @g, 1, $noreg, 0, $noreg,
+  call void asm sideeffect "call ${0:P}", "*m,~{dirflag},~{fpsr},~{flags}"(ptr 
elementtype(void ()) @g)
+  ret void
+}

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to