Skip to content
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

[llvm] Fix the MCSubtargetInfo used for module-level assembly. #97685

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chrisnc
Copy link
Contributor

@chrisnc chrisnc commented Jul 4, 2024

Provide both the default target CPU and default target features from the
module's context, rather than empty strings.

Fixes #61991.

Provide both the default target CPU and default target features from the
module's context, rather than empty strings.

Fixes llvm#61991.
@llvmbot
Copy link
Member

llvmbot commented Jul 4, 2024

@llvm/pr-subscribers-llvm-binary-utilities

Author: Chris Copeland (chrisnc)

Changes

Provide both the default target CPU and default target features from the
module's context, rather than empty strings.

Fixes #61991.


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

1 Files Affected:

  • (modified) llvm/lib/Object/ModuleSymbolTable.cpp (+5-1)
diff --git a/llvm/lib/Object/ModuleSymbolTable.cpp b/llvm/lib/Object/ModuleSymbolTable.cpp
index d8f520ad02c2f..079c33c801f6e 100644
--- a/llvm/lib/Object/ModuleSymbolTable.cpp
+++ b/llvm/lib/Object/ModuleSymbolTable.cpp
@@ -22,6 +22,7 @@
 #include "llvm/IR/GlobalValue.h"
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/IR/InlineAsm.h"
+#include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCContext.h"
@@ -92,8 +93,11 @@ initializeRecordStreamer(const Module &M,
   if (!MAI)
     return;
 
+  LLVMContext &Context = M.getContext();
+
   std::unique_ptr<MCSubtargetInfo> STI(
-      T->createMCSubtargetInfo(TT.str(), "", ""));
+      T->createMCSubtargetInfo(TT.str(), Context.getDefaultTargetCPU(),
+                               Context.getDefaultTargetFeatures()));
   if (!STI)
     return;
 

@chrisnc
Copy link
Contributor Author

chrisnc commented Jul 4, 2024

test.c

asm("vmsr fpexc, r0");

Without fix:

$ bin/clang -target arm-none-eabi test.c -c -flto=thin -mcpu=cortex-r4
<inline asm>:1:1: error: instruction requires: VFP2
    1 | vmsr fpexc, r0
      | ^
1 error generated.

$ bin/clang -target arm-none-eabi test.c -c -flto=thin -mcpu=cortex-r5
<inline asm>:1:1: error: instruction requires: VFP2
    1 | vmsr fpexc, r0
      | ^
1 error generated.

With fix, using -mcpu=cortex-r5 (which implies FPU support, while cortex-r4 does not), does not emit an error:

$ bin/clang -target arm-none-eabi test.c -c -flto=thin -mcpu=cortex-r4
<inline asm>:1:1: error: instruction requires: VFP2
    1 | vmsr fpexc, r0
      | ^
1 error generated.

$ bin/clang -target arm-none-eabi test.c -c -flto=thin -mcpu=cortex-r5

@chrisnc
Copy link
Contributor Author

chrisnc commented Jul 4, 2024

Credit to #61991 (comment) @Dirbaio for finding where the problem was.

Hopefully this is not a breaking change for anything and the fix is this simple...

@topperc topperc requested review from pcc, efriedma-quic and topperc July 4, 2024 09:07
@efriedma-quic
Copy link
Collaborator

The idea here makes sense, but the API in question is likely to change, so I'd rather not add new uses of that API until we've sorted that out. See https://discourse.llvm.org/t/functions-generated-by-function-createwithdefaultattr-should-respect-target-features/79838 .

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

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

Request changes since the ABI will change. My summary:

#96721 implemented a strategy by storing the target features in LLVMContext, which is not serialized.
Function::createWithDefaultAttr will load the target features from LLVMContext.

While 96721 works well, there is a drawback that the generate module from the LTO pre-link compilation does not contain sufficient information.
Running opt without the correct target features would not mimick Clang .
To address this drawback, we can use module flags metadata that is dropped during merging.

@chrisnc
Copy link
Contributor Author

chrisnc commented Jul 5, 2024

Oh! I did not notice that the interfaces I'm using only appeared a week ago. Thanks for the additional context.

use module flags metadata

Is this something I can do now at this call-site, or something that needs more iteration on what the correct way to store/retrieve this information from various phases should be?

@chrisnc
Copy link
Contributor Author

chrisnc commented Jul 30, 2024

Hi @MaskRay, is #98673 the confirmed direction for how this info will be handled? If so, I'll watch for that to merge and then re-visit this.

@MaskRay
Copy link
Member

MaskRay commented Jul 31, 2024

Hi @MaskRay, is #98673 the confirmed direction for how this info will be handled? If so, I'll watch for that to merge and then re-visit this.

I think so. Thanks for waiting :)

trdthg added a commit to trdthg/WebKit that referenced this pull request Nov 10, 2024
https://bugs.webkit.org/show_bug.cgi?id=282900

Reviewed by NOBODY (OOPS!).

This is a llvm bug, passing `-march=riscv64gc -cpu=lp64d` from clang
will lost after it got llvm side. So we have to set arch here.

More related infomations are here:

- rust-lang/rust#80608
- llvm/llvm-project#61991
- llvm/llvm-project#97685

* Source/JavaScriptCore/assembler/MacroAssemblerRISCV64.cpp:
* Source/JavaScriptCore/llint/LowLevelInterpreter.cpp:
trdthg added a commit to trdthg/WebKit that referenced this pull request Nov 10, 2024
https://bugs.webkit.org/show_bug.cgi?id=282900

Reviewed by NOBODY (OOPS!).

This is a llvm bug, passing `-march=riscv64gc -cpu=lp64d` from clang
will lost after it got llvm side. So we have to set arch here.

More related infomations are here:

- rust-lang/rust#80608
- llvm/llvm-project#61991
- llvm/llvm-project#97685

* Source/JavaScriptCore/assembler/MacroAssemblerRISCV64.cpp:
* Source/JavaScriptCore/llint/LowLevelInterpreter.cpp:
trdthg added a commit to trdthg/WebKit that referenced this pull request Nov 10, 2024
https://bugs.webkit.org/show_bug.cgi?id=282900

Reviewed by NOBODY (OOPS!).

This is a llvm bug, passing `-march=riscv64gc -cpu=lp64d` from clang
will lost after it got llvm side. So we have to set arch here.

More related infomations are here:

- rust-lang/rust#80608
- llvm/llvm-project#61991
- llvm/llvm-project#97685

* Source/JavaScriptCore/assembler/MacroAssemblerRISCV64.cpp:
* Source/JavaScriptCore/llint/LowLevelInterpreter.cpp:
trdthg added a commit to trdthg/WebKit that referenced this pull request Nov 11, 2024
https://bugs.webkit.org/show_bug.cgi?id=282900

Reviewed by NOBODY (OOPS!).

This is a llvm bug, passing `-march=riscv64gc -cpu=lp64d` from clang
will lost after it got llvm side. So we have to set arch here.

More related infomations are here:

- rust-lang/rust#80608
- llvm/llvm-project#61991
- llvm/llvm-project#97685

* Source/JavaScriptCore/assembler/MacroAssemblerRISCV64.cpp:
* Source/JavaScriptCore/llint/LowLevelInterpreter.cpp:
webkit-commit-queue pushed a commit to trdthg/WebKit that referenced this pull request Nov 19, 2024
https://bugs.webkit.org/show_bug.cgi?id=282900

Reviewed by Yusuke Suzuki.

This is a llvm bug, passing `-march=riscv64gc -cpu=lp64d` from clang
will lost after it got llvm side. So we have to set arch here.

More related infomations are here:

- rust-lang/rust#80608
- llvm/llvm-project#61991
- llvm/llvm-project#97685

* Source/JavaScriptCore/assembler/MacroAssemblerRISCV64.cpp:
* Source/JavaScriptCore/llint/LowLevelInterpreter.cpp:

Canonical link: https://commits.webkit.org/286815@main
aperezdc pushed a commit to WebKit/WebKit that referenced this pull request Dec 15, 2024
…gi?id=282900

    [RISCV] Fix instruction requires the following: 'D'/'F'/'M'
    https://bugs.webkit.org/show_bug.cgi?id=282900

    Reviewed by Yusuke Suzuki.

    This is a llvm bug, passing `-march=riscv64gc -cpu=lp64d` from clang
    will lost after it got llvm side. So we have to set arch here.

    More related infomations are here:

    - rust-lang/rust#80608
    - llvm/llvm-project#61991
    - llvm/llvm-project#97685

    * Source/JavaScriptCore/assembler/MacroAssemblerRISCV64.cpp:
    * Source/JavaScriptCore/llint/LowLevelInterpreter.cpp:

    Canonical link: https://commits.webkit.org/286815@main

Canonical link: https://commits.webkit.org/282416.360@webkitglib/2.46
mnutt pushed a commit to movableink/webkit that referenced this pull request Dec 23, 2024
https://bugs.webkit.org/show_bug.cgi?id=282900

Reviewed by Yusuke Suzuki.

This is a llvm bug, passing `-march=riscv64gc -cpu=lp64d` from clang
will lost after it got llvm side. So we have to set arch here.

More related infomations are here:

- rust-lang/rust#80608
- llvm/llvm-project#61991
- llvm/llvm-project#97685

* Source/JavaScriptCore/assembler/MacroAssemblerRISCV64.cpp:
* Source/JavaScriptCore/llint/LowLevelInterpreter.cpp:

Canonical link: https://commits.webkit.org/286815@main
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.

Spurious errors emitted for global-scope asm with ThinLTO
4 participants