Skip to content

[X86,AsmPrinter] Set assembler dialect for module inline asm #85367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

MaskRay
Copy link
Member

@MaskRay MaskRay commented Mar 15, 2024

clang -c -masm=intel compiling a source file with file scope basic asm
incorrectly uses the AT&T dialect.

% cat a.c
asm("mov rax, rax");
% clang a.c -c -masm=intel
<inline asm>:1:1: error: unknown use of instruction mnemonic without a size suffix
mov rax, rax
^

Fix this by setting the assembler dialect from the MCAsmInfo object.

Note: clang -c -flto -masm=intel a.c still fails because of
https://reviews.llvm.org/D82862 for #34830: it tried to support AT&T
syntax for clang-cl, but the forced AT&T syntax is not compatible with
intended Intel syntax.

Created using spr 1.3.5-bogner
@llvmbot
Copy link
Member

llvmbot commented Mar 15, 2024

@llvm/pr-subscribers-backend-x86

Author: Fangrui Song (MaskRay)

Changes

clang -c -masm=intel compiling a source file with file scope basic asm
incorrectly uses the AT&T dialect.

% cat a.c
asm("mov rax, rax");
% clang a.c -c -masm=intel
&lt;inline asm&gt;:1:1: error: unknown use of instruction mnemonic without a size suffix
mov rax, rax
^

Fix this by setting the assembler dialect from the MCAsmInfo object.

Note: clang -c -flto -masm=intel a.c still fails due to a known LTO
problem of ModuleSymbolTable::CollectAsmSymbols not using sufficient
target information.


Full diff: https://github.com/llvm/llvm-project/pull/85367.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+4-2)
  • (added) llvm/test/CodeGen/X86/asm-dialect-module.ll (+10)
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 0efe7a0e733672..a15538755d73b3 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -538,8 +538,10 @@ bool AsmPrinter::doInitialization(Module &M) {
   if (!M.getModuleInlineAsm().empty()) {
     OutStreamer->AddComment("Start of file scope inline assembly");
     OutStreamer->addBlankLine();
-    emitInlineAsm(M.getModuleInlineAsm() + "\n", *TM.getMCSubtargetInfo(),
-                  TM.Options.MCOptions);
+    emitInlineAsm(
+        M.getModuleInlineAsm() + "\n", *TM.getMCSubtargetInfo(),
+        TM.Options.MCOptions, nullptr,
+        InlineAsm::AsmDialect(TM.getMCAsmInfo()->getAssemblerDialect()));
     OutStreamer->AddComment("End of file scope inline assembly");
     OutStreamer->addBlankLine();
   }
diff --git a/llvm/test/CodeGen/X86/asm-dialect-module.ll b/llvm/test/CodeGen/X86/asm-dialect-module.ll
new file mode 100644
index 00000000000000..2c00a44424c2c3
--- /dev/null
+++ b/llvm/test/CodeGen/X86/asm-dialect-module.ll
@@ -0,0 +1,10 @@
+;; Test that we respect the assembler dialect when parsing module-level inline asm.
+; RUN: not llc < %s -mtriple=x86_64 2>&1 | FileCheck %s --check-prefix=ERR
+; RUN: llc < %s -mtriple=x86_64 -x86-asm-syntax=intel | FileCheck %s
+
+; ERR: <inline asm>:1:1: error: unknown use of instruction mnemonic without a size suffix
+
+; CHECK: .intel_syntax noprefix
+; CHECK: mov eax, eax
+
+module asm "mov eax, eax"

Copy link
Contributor

@KanRobert KanRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MaskRay MaskRay merged commit f4335f0 into main Mar 15, 2024
@MaskRay MaskRay deleted the users/MaskRay/spr/x86asmprinter-set-assembler-dialect-for-module-inline-asm branch March 15, 2024 16:38
hubot pushed a commit to v8/v8 that referenced this pull request Mar 29, 2024
…emory

With llvm/llvm-project#85367, clang-cl uses
-x86-asm-syntax=intel by default on Windows, so we need to add
.att_syntax to tell clang to use AT&T instead of Intel syntax.

Bug: chromium:330163686
Change-Id: Ief192022802a1c22e5756123ef12e09a7ac05d81
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5405986
Reviewed-by: Nico Weber <[email protected]>
Commit-Queue: Adam Klein <[email protected]>
Auto-Submit: Alan Zhao <[email protected]>
Reviewed-by: Adam Klein <[email protected]>
Cr-Commit-Position: refs/heads/main@{#93094}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants