[llvm] 103cc91 - [x86/asm] Make variants work when converting at&t inline asm input to intel asm output

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 17 10:23:53 PST 2021


Author: Nico Weber
Date: 2021-11-17T13:23:18-05:00
New Revision: 103cc914d63371212fdc81e450572ee096952211

URL: https://github.com/llvm/llvm-project/commit/103cc914d63371212fdc81e450572ee096952211
DIFF: https://github.com/llvm/llvm-project/commit/103cc914d63371212fdc81e450572ee096952211.diff

LOG: [x86/asm] Make variants work when converting at&t inline asm input to intel asm output

`asm` always has AT&T-style input (`asm inteldialect` has Intel-style asm
input), so EmitGCCInlineAsmStr() always has to pick the same variant since it
cares about the input asm string, not the output asm string.

For PowerPC, that default variant is 1. For other targets, it's 0.

Without this, the included test case errors out with

    error: unknown use of instruction mnemonic without a size suffix
             mov rax, rbx

since it picks the intel branch and then tries to interpret it as AT&T
when selecting intel-style output with `-x86-asm-syntax=intel`.

Differential Revision: https://reviews.llvm.org/D113894

Added: 
    llvm/test/CodeGen/X86/asm-dialect.ll

Modified: 
    llvm/include/llvm/Target/TargetMachine.h
    llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
    llvm/lib/Target/PowerPC/PPCTargetMachine.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
index cae30ab3a7f3a..acfb265a9ff93 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -485,6 +485,10 @@ class LLVMTargetMachine : public TargetMachine {
   virtual bool useIPRA() const {
     return false;
   }
+
+  /// The default variant to use in unqualified `asm` instructions.
+  /// If this returns 0, `asm "$(foo$|bar$)"` will evaluate to `asm "foo"`.
+  virtual int unqualifiedInlineAsmVariant() const { return 0; }
 };
 
 /// Helper method for getting the code model, returning Default if

diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
index 7ca38d3ad8ad7..0c5f81bbebfcd 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
@@ -276,7 +276,7 @@ static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
   int CurVariant = -1;            // The number of the {.|.|.} region we are in.
   const char *LastEmitted = AsmStr; // One past the last character emitted.
   unsigned NumOperands = MI->getNumOperands();
-  int AsmPrinterVariant = MAI->getAssemblerDialect();
+  int AsmPrinterVariant = MMI->getTarget().unqualifiedInlineAsmVariant();
 
   if (MAI->getEmitGNUAsmStartIndentationMarker())
     OS << '\t';

diff  --git a/llvm/lib/Target/PowerPC/PPCTargetMachine.h b/llvm/lib/Target/PowerPC/PPCTargetMachine.h
index ed9e74b72d1e4..d3fe5362ccdc0 100644
--- a/llvm/lib/Target/PowerPC/PPCTargetMachine.h
+++ b/llvm/lib/Target/PowerPC/PPCTargetMachine.h
@@ -68,6 +68,8 @@ class PPCTargetMachine final : public LLVMTargetMachine {
   }
 
   bool isLittleEndian() const;
+
+  int unqualifiedInlineAsmVariant() const override { return 1; }
 };
 } // end namespace llvm
 

diff  --git a/llvm/test/CodeGen/X86/asm-dialect.ll b/llvm/test/CodeGen/X86/asm-dialect.ll
new file mode 100644
index 0000000000000..62d7fdad3d2e6
--- /dev/null
+++ b/llvm/test/CodeGen/X86/asm-dialect.ll
@@ -0,0 +1,24 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=x86_64-unknown-linux-gnu %s -o - \
+; RUN:     | FileCheck --check-prefix=OUTPUT_ATT %s
+; RUN: llc -mtriple=x86_64-unknown-linux-gnu %s -x86-asm-syntax=intel -o - \
+; RUN:     | FileCheck --check-prefix=OUTPUT_INTEL %s
+
+define void @f() {
+; OUTPUT_ATT-LABEL: f:
+; OUTPUT_ATT:       # %bb.0:
+; OUTPUT_ATT-NEXT:    #APP
+; OUTPUT_ATT-NEXT:    movq %rbx, %rax
+; OUTPUT_ATT-NEXT:    #NO_APP
+; OUTPUT_ATT-NEXT:    retq
+;
+; OUTPUT_INTEL-LABEL: f:
+; OUTPUT_INTEL:       # %bb.0:
+; OUTPUT_INTEL-NEXT:    #APP
+; OUTPUT_INTEL-NEXT:    mov rax, rbx
+; OUTPUT_INTEL-NEXT:    #NO_APP
+; OUTPUT_INTEL-NEXT:    ret
+  call void asm sideeffect "$(movq %rbx, %rax $|mov rax, rbx$)", "~{dirflag},~{fpsr},~{flags}"()
+
+  ret void
+}


        


More information about the llvm-commits mailing list