[llvm-branch-commits] [llvm] [MIR] Round-trip the "func" kind of inline asm operand flags (PR #237461)
Bill Wendling via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Nov 20 02:42:56 PST 2026
https://github.com/isanbard updated https://github.com/llvm/llvm-project/pull/237461
>From 9e830e1dc805ee043a496012b9dc87129215b13c Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Fri, 30 Oct 2026 18:58:28 -0700
Subject: [PATCH 1/3] [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 at 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 at 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 at 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
+}
>From 46798723a6349ed5e2c51482835858bb4eb923c0 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Fri, 30 Oct 2026 22:11:09 -0700
Subject: [PATCH 2/3] Output null filetype instead of piping to /dev/null
Co-authored-by: Matt Arsenault <Matthew.Arsenault at amd.com>
---
llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll b/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll
index de6c3f183c16ca..853109ca53f477 100644
--- a/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll
+++ b/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll
@@ -3,7 +3,7 @@
; 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
+; RUN: llc -mtriple=x86_64-linux-gnu -global-isel=0 -print-after=finalize-isel -filetype=null %s 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.
>From 089619833dba0325a5c4509092a04e86d74b8dee Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Fri, 30 Oct 2026 22:12:14 -0700
Subject: [PATCH 3/3] Remove unneeded -global-isel=0
---
llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll b/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll
index 853109ca53f477..c4d2b05bbcc490 100644
--- a/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll
+++ b/llvm/test/CodeGen/MIR/X86/inline-asm-func-kind.ll
@@ -1,9 +1,9 @@
-; RUN: llc -mtriple=x86_64-linux-gnu -global-isel=0 -stop-after=finalize-isel %s -o %t.mir
+; RUN: llc -mtriple=x86_64-linux-gnu -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 -filetype=null %s 2>&1 | FileCheck %s --check-prefix=PRINT
+; RUN: llc -mtriple=x86_64-linux-gnu %s -o - | FileCheck %s
+; RUN: llc -mtriple=x86_64-linux-gnu -print-after=finalize-isel -filetype=null %s 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.
More information about the llvm-branch-commits
mailing list