Skip to content

[llvm-ml] Fix RIP-relative addressing for ptr operands #107618

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

Merged
merged 1 commit into from
Sep 9, 2024

Conversation

nga888
Copy link
Collaborator

@nga888 nga888 commented Sep 6, 2024

Fixes #54773

@llvmbot
Copy link
Member

llvmbot commented Sep 6, 2024

@llvm/pr-subscribers-backend-x86

Author: Andrew Ng (nga888)

Changes

Fixes #54773


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

2 Files Affected:

  • (modified) llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp (+1-1)
  • (modified) llvm/test/tools/llvm-ml/rip_relative_addressing.asm (+7-1)
diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
index 03f49306c2b7b5..e88dc9cfbf4877 100644
--- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -2707,7 +2707,7 @@ bool X86AsmParser::parseIntelOperand(OperandVector &Operands, StringRef Name) {
   bool MaybeDirectBranchDest = true;
 
   if (Parser.isParsingMasm()) {
-    if (is64BitMode() && SM.getElementSize() > 0) {
+    if (is64BitMode() && (PtrInOperand || SM.getElementSize() > 0)) {
       DefaultBaseReg = X86::RIP;
     }
     if (IsUnconditionalBranch) {
diff --git a/llvm/test/tools/llvm-ml/rip_relative_addressing.asm b/llvm/test/tools/llvm-ml/rip_relative_addressing.asm
index d237e84435b7d6..cdd984ee6a8522 100644
--- a/llvm/test/tools/llvm-ml/rip_relative_addressing.asm
+++ b/llvm/test/tools/llvm-ml/rip_relative_addressing.asm
@@ -53,4 +53,10 @@ mov eax, [t8]
 ; CHECK-LABEL: t8:
 ; CHECK: mov eax, dword ptr [t8]
 
-END
\ No newline at end of file
+t9:
+mov eax, dword ptr [bar]
+; CHECK-LABEL: t9:
+; CHECK-32: mov eax, dword ptr [bar]
+; CHECK-64: mov eax, dword ptr [rip + bar]
+
+END

@mstorsjo
Copy link
Member

mstorsjo commented Sep 9, 2024

Thanks for working on this! Unfortunately, I'm not quite familiar enough with x86 assembly to comment reasonably on this... Hopefully someone else can ack it.

@ericastor
Copy link
Contributor

ericastor commented Sep 9, 2024

Have we confirmed that this fully fixes #54773 ?

I'm prepared to approve this if so - it seems to match ml64's behavior - but I don't want us closing the other bug if this doesn't fix it!

@nga888
Copy link
Collaborator Author

nga888 commented Sep 9, 2024

Have we confirmed that this fully fixes #54773 ?

I'm prepared to approve this if so - it seems to match ml64's behavior - but I don't want us closing the other bug if this doesn't fix it!

I have checked that using llvm-ml.exe with this patch applied to the LLVM 18 toolchain can build a Windows x64 LLVM 19 toolchain that runs and doesn't crash because of the mis-assembly of the Blake3 MASM assembly source.

It was my own experiment of trying to replace MS ml64.exe with llvm-ml.exe -m64 in my Windows build that led me to this issue...

@ericastor
Copy link
Contributor

Fantastic! LGTM.

@ericastor ericastor merged commit 7543d09 into llvm:main Sep 9, 2024
8 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 9, 2024

LLVM Buildbot has detected a new failure on builder llvm-clang-aarch64-darwin running on doug-worker-5 while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/190/builds/5376

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'Clang :: CodeGen/mips-varargs.c' FAILED ********************
Exit Code: 134

Command Output (stderr):
--
RUN: at line 1: /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips-unknown-linux -o - -emit-llvm /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c | /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,O32 -enable-var-scope
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips-unknown-linux -o - -emit-llvm /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,O32 -enable-var-scope
RUN: at line 2: /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mipsel-unknown-linux -o - -emit-llvm /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c | /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,O32 -enable-var-scope
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mipsel-unknown-linux -o - -emit-llvm /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,O32 -enable-var-scope
RUN: at line 3: /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips64-unknown-linux -o - -emit-llvm  -target-abi n32 /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c | /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N32,NEW -enable-var-scope
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips64-unknown-linux -o - -emit-llvm -target-abi n32 /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N32,NEW -enable-var-scope
RUN: at line 4: /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips64-unknown-linux -o - -emit-llvm  -target-abi n32 /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c | /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N32,NEW -enable-var-scope
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips64-unknown-linux -o - -emit-llvm -target-abi n32 /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N32,NEW -enable-var-scope
RUN: at line 5: /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips64-unknown-linux -o - -emit-llvm /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c | /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N64,NEW -enable-var-scope
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips64-unknown-linux -o - -emit-llvm /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N64,NEW -enable-var-scope
RUN: at line 6: /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips64el-unknown-linux -o - -emit-llvm /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c | /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N64,NEW -enable-var-scope
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips64el-unknown-linux -o - -emit-llvm /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c
+ /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N64,NEW -enable-var-scope
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N64,NEW -enable-var-scope
 #0 0x00000001005ac1c4 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck+0x1000701c4)
 #1 0x00000001005aa74c llvm::sys::RunSignalHandlers() (/Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck+0x10006e74c)
 #2 0x00000001005ac86c SignalHandler(int) (/Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck+0x10007086c)
 #3 0x00000001941a2584 (/usr/lib/system/libsystem_platform.dylib+0x18047a584)
 #4 0x0000000194171c20 (/usr/lib/system/libsystem_pthread.dylib+0x180449c20)
 #5 0x000000019407ea30 (/usr/lib/system/libsystem_c.dylib+0x180356a30)
 #6 0x0000000193f8edc4 (/usr/lib/system/libsystem_malloc.dylib+0x180266dc4)
 #7 0x0000000193f92430 (/usr/lib/system/libsystem_malloc.dylib+0x18026a430)
 #8 0x0000000193fac494 (/usr/lib/system/libsystem_malloc.dylib+0x180284494)
 #9 0x0000000100563da8 llvm::FileCheckPatternContext::~FileCheckPatternContext() (/Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck+0x100027da8)
#10 0x0000000100555fa8 llvm::FileCheck::~FileCheck() (/Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck+0x100019fa8)
#11 0x0000000100542fbc main (/Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck+0x100006fbc)
#12 0x0000000193de7154 
/Users/buildbot/buildbot-root/aarch64-darwin/build/tools/clang/test/CodeGen/Output/mips-varargs.c.script: line 6: 90557 Done                    /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/clang -cc1 -internal-isystem /Users/buildbot/buildbot-root/aarch64-darwin/build/lib/clang/20/include -nostdsysteminc -triple mips64el-unknown-linux -o - -emit-llvm /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c
     90558 Abort trap: 6           | /Users/buildbot/buildbot-root/aarch64-darwin/build/bin/FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/clang/test/CodeGen/mips-varargs.c -check-prefixes=ALL,N64,NEW -enable-var-scope

--

********************


@nga888 nga888 deleted the fix-llvm-ml-rip-relative-addressing branch September 9, 2024 17:35
@mstorsjo
Copy link
Member

mstorsjo commented Sep 9, 2024

I think it'd be great to have this fix widely deployed in 19.x as well, so we probably should file a backport request. But as it isn't entirely critical, and the bar for including things in 19.1.0 now is pretty high, we probably should file a backport request only after 19.1.0 is cut (early next week).

mstorsjo added a commit that referenced this pull request Sep 10, 2024
…)"

This reverts commit 7543d09.

This change caused failed asserts when building the openmp assembly
sources, reproducible with:

    $ llvm-ml -m64 -D_M_AMD64 -c -Fo out.obj openmp/runtime/src/z_Windows_NT-586_asm.asm
    llvm-ml: ../lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp:624: void {anonymous}::X86MCCodeEmitter::emitMemModRMByte(const llvm::MCInst&, unsigned int, unsigned int, uint64_t, {anonymous}::PrefixKind, uint64_t, llvm::SmallVectorImpl<char>&, llvm::SmallVectorImpl<llvm::MCFixup>&, const llvm::MCSubtargetInfo&, bool) const: Assertion `IndexReg.getReg() == 0 && !ForceSIB && "Invalid rip-relative address"' failed.

The assert can also be triggered with one lone instruction:

    lea rdx, QWORD PTR [rax*8+16]
@mstorsjo
Copy link
Member

Unfortunately, I had to revert this, as this triggered failed asserts when building the openmp runtime assembly sources. See 1581183 for details on how to build that, and for the single instruction that triggers the assert (lea rdx, QWORD PTR [rax*8+16]).

nga888 added a commit to nga888/llvm-project that referenced this pull request Sep 10, 2024
Relands llvm#107618 with fix for assertion triggered by OpenMP runtime MASM
assembly source.
nga888 added a commit to nga888/llvm-project that referenced this pull request Sep 13, 2024
Relands llvm#107618 with fix for assertion triggered by OpenMP runtime MASM
assembly source.
nga888 added a commit that referenced this pull request Sep 13, 2024
Relands #107618 with fix for assertion triggered by OpenMP runtime MASM
assembly source.
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Sep 18, 2024
…08061)

Relands llvm#107618 with fix for assertion triggered by OpenMP runtime MASM
assembly source.

(cherry picked from commit 7574e1d)
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Sep 24, 2024
…08061)

Relands llvm#107618 with fix for assertion triggered by OpenMP runtime MASM
assembly source.

(cherry picked from commit 7574e1d)
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.

llvm-ml cannot assemble LLVM's own MASM-syntax source code
6 participants