Skip to content

[M68k] Backend error "Unsupported asm expression. Only absolute address can be placed here." when using TLS #63162

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

Closed
glaubitz opened this issue Jun 7, 2023 · 5 comments
Assignees

Comments

@glaubitz
Copy link
Contributor

glaubitz commented Jun 7, 2023

I have been testing the new TLS code and was able to trigger a bug when compiling the following sample code:

#include <thread>
#include <future>
#include <iostream>

int main(){
  auto future = std::async(std::launch::async, [](){
    std::cout << "I'm a thread" << std::endl;
  });

  future.get();

  return 0;
}

In order to be able to build the code with the 68020 baseline, I used the following workaround:

diff --git a/llvm/lib/Target/M68k/M68kExpandPseudo.cpp b/llvm/lib/Target/M68k/M68kExpandPseudo.cpp
index 2f60fc834a18..c6e1a146f6a2 100644
--- a/llvm/lib/Target/M68k/M68kExpandPseudo.cpp
+++ b/llvm/lib/Target/M68k/M68kExpandPseudo.cpp
@@ -260,7 +260,7 @@ bool M68kExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
       MIB = BuildMI(MBB, MBBI, DL, TII->get(M68k::RTS));
     } else if (isUInt<16>(StackAdj)) {
 
-      if (STI->atLeastM68020()) {
+      if (STI->atLeastM68020() && false) {
         llvm_unreachable("RTD is not implemented");
       } else {
         // Copy PC from stack to a free address(A0 or A1) register

Then I get:

glaubitz@node54:/data/home/glaubitz> /data/home/glaubitz/llvm-project/stage1.install/bin/clang -target m68k-linux-gnu future.cc -o future.o -m68020 -I /data/home/glaubitz/sid-m68k-sbuild/usr/m68k-linux-gnu/include/c++/12/m68k-linux-gnu/ -I /data/home/glaubitz/sid-m68k-sbuild/usr/m68k-linux-gnu/include/ -I /data/home/glaubitz/sid-m68k-sbuild/usr/include/c++/12/ -I /data/home/glaubitz/sid-m68k-sbuild/usr/include/m68k-linux-gnu/c++/12/ -c
fatal error: error in backend: Unsupported asm expression. Only absolute address can be placed here.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /data/home/glaubitz/llvm-project/stage1.install/bin/clang -target m68k-linux-gnu future.cc -o future.o -m68020 -I /data/home/glaubitz/sid-m68k-sbuild/usr/m68k-linux-gnu/include/c++/12/m68k-linux-gnu/ -I /data/home/glaubitz/sid-m68k-sbuild/usr/m68k-linux-gnu/include/ -I /data/home/glaubitz/sid-m68k-sbuild/usr/include/c++/12/ -I /data/home/glaubitz/sid-m68k-sbuild/usr/include/m68k-linux-gnu/c++/12/ -c
1.      <eof> parser at end of file
2.      Code generation
3.      Running pass 'Function Pass Manager' on module 'future.cc'.
4.      Running pass 'M68k Assembly Printer' on function '@_ZNSt9once_flag18_Prepare_executionC2IZSt9call_onceIMNSt13__future_base13_State_baseV2EFvPSt8functionIFSt10unique_ptrINS3_12_Result_baseENS7_8_DeleterEEvEEPbEJPS4_SC_SD_EEvRS_OT_DpOT0_EUlvE_EERSI_'
 #0 0x0000000001e508d7 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1e508d7)
 #1 0x0000000001e4e5bc llvm::sys::CleanupOnSignal(unsigned long) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1e4e5bc)
 #2 0x0000000001dadc66 llvm::CrashRecoveryContext::HandleExit(int) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1dadc66)
 #3 0x0000000001e4555e llvm::sys::Process::Exit(int, bool) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1e4555e)
 #4 0x0000000000bdeb76 LLVMErrorHandler(void*, char const*, bool) cc1_main.cpp:0:0
 #5 0x0000000001db681c llvm::report_fatal_error(llvm::Twine const&, bool) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1db681c)
 #6 0x0000000001db6998 (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1db6998)
 #7 0x0000000000feb2b9 (anonymous namespace)::M68kMCCodeEmitter::getMachineOpValue(llvm::MCInst const&, llvm::MCOperand const&, unsigned int, llvm::APInt&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::MCSubtargetInfo const&) const (.isra.25) M68kMCCodeEmitter.cpp:0:0
 #8 0x0000000000fed931 (anonymous namespace)::M68kMCCodeEmitter::getBinaryCodeForInstr(llvm::MCInst const&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::APInt&, llvm::APInt&, llvm::MCSubtargetInfo const&) const (.isra.65) M68kMCCodeEmitter.cpp:0:0
 #9 0x0000000000ffe377 (anonymous namespace)::M68kMCCodeEmitter::encodeInstruction(llvm::MCInst const&, llvm::SmallVectorImpl<char>&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::MCSubtargetInfo const&) const M68kMCCodeEmitter.cpp:0:0
#10 0x0000000001b5428f llvm::MCELFStreamer::emitInstToData(llvm::MCInst const&, llvm::MCSubtargetInfo const&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1b5428f)
#11 0x0000000001b74a00 llvm::MCObjectStreamer::emitInstructionImpl(llvm::MCInst const&, llvm::MCSubtargetInfo const&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1b74a00)
#12 0x0000000001b74c72 llvm::MCObjectStreamer::emitInstruction(llvm::MCInst const&, llvm::MCSubtargetInfo const&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1b74c72)
#13 0x0000000000f97c42 llvm::M68kAsmPrinter::emitInstruction(llvm::MachineInstr const*) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0xf97c42)
#14 0x00000000029c7d0f llvm::AsmPrinter::emitFunctionBody() (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x29c7d0f)
#15 0x0000000000f97a74 llvm::M68kAsmPrinter::runOnMachineFunction(llvm::MachineFunction&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0xf97a74)
#16 0x00000000013a6080 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.72) MachineFunctionPass.cpp:0:0
#17 0x000000000195c373 llvm::FPPassManager::runOnFunction(llvm::Function&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x195c373)
#18 0x000000000195c689 llvm::FPPassManager::runOnModule(llvm::Module&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x195c689)
#19 0x000000000195d458 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x195d458)
#20 0x0000000002074015 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x2074015)
#21 0x0000000002e0e3bf clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x2e0e3bf)
#22 0x0000000003de2239 clang::ParseAST(clang::Sema&, bool, bool) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x3de2239)
#23 0x0000000002e0cca8 clang::CodeGenAction::ExecuteAction() (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x2e0cca8)
#24 0x000000000279fa59 clang::FrontendAction::Execute() (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x279fa59)
#25 0x000000000273665a clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x273665a)
#26 0x000000000286e9ff clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x286e9ff)
#27 0x0000000000bdfef2 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0xbdfef2)
#28 0x0000000000bdb5ef ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#29 0x00000000025bbff9 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#30 0x0000000001dadb15 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x1dadb15)
#31 0x00000000025beb26 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x25beb26)
#32 0x000000000258cb73 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x258cb73)
#33 0x000000000258d713 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x258d713)
#34 0x0000000002595fb6 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0x2595fb6)
#35 0x0000000000bde2f2 clang_main(int, char**, llvm::ToolContext const&) (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0xbde2f2)
#36 0x0000000000b51341 main (/data/home/glaubitz/llvm-project/stage1.install/bin/clang+0xb51341)
#37 0x00007fb11476029d __libc_start_main (/lib64/libc.so.6+0x3529d)
#38 0x0000000000bd5aca _start /home/abuild/rpmbuild/BUILD/glibc-2.31/csu/../sysdeps/x86_64/start.S:122:0
clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
clang version 17.0.0 (https://github.com/llvm/llvm-project.git abe6ecd7e5f5bc4e57fb6b530e3ea4811d06e4b9)
Target: m68k-unknown-linux-gnu
Thread model: posix
InstalledDir: /data/home/glaubitz/llvm-project/stage1.install/bin
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/future-7a398f.cpp
clang: note: diagnostic msg: /tmp/future-7a398f.sh
clang: note: diagnostic msg: 

********************
glaubitz@node54:/data/home/glaubitz>

This can be easily worked around by disabling the corresponding check in the backend code:

diff --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kMCCodeEmitter.cpp b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCCodeEmitter.cpp
index d4fc0510f944..425015206e16 100644
--- a/llvm/lib/Target/M68k/MCTargetDesc/M68kMCCodeEmitter.cpp
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCCodeEmitter.cpp
@@ -190,9 +190,9 @@ void M68kMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &Op,
   } else if (Op.isExpr()) {
     // Absolute address
     int64_t Addr;
-    if (!Op.getExpr()->evaluateAsAbsolute(Addr))
-      report_fatal_error("Unsupported asm expression. Only absolute address "
-                         "can be placed here.");
+    // if (!Op.getExpr()->evaluateAsAbsolute(Addr))
+    //   report_fatal_error("Unsupported asm expression. Only absolute address "
+    //                      "can be placed here.");
     Value |= static_cast<uint64_t>(Addr);
   } else {
     llvm_unreachable("Unsupported operand type");

After that, I can successfully compile the sample code from future.cc. Maybe we can just drop the check?

CC @mshockwave @0x59616e

@llvmbot
Copy link
Member

llvmbot commented Jun 7, 2023

@llvm/issue-subscribers-backend-m68k

@mshockwave
Copy link
Member

I think this is an issue where SelectionDAGISel selects the incorrect addressing mode for operands generated for TLS. Which actually appears in existing test cases as well.
For instance, this line:

add.l (0,myvar@GOTTPOFF,%a0), %d0

The (0,myvar@GOTTPOFF,%a0) is ill-formed since it is supposed to be ARII (address register indirect with index register) but the second segment is not base (address) register as it should have been.

The culprit is likely to be M68kDAGToDAGISel::matchAddress and friends. We copied only the basic logics for this part from X86 backend, but not those for advanced features like TLS.

@glaubitz
Copy link
Contributor Author

glaubitz commented Jun 9, 2023

I think this is an issue where SelectionDAGISel selects the incorrect addressing mode for operands generated for TLS. Which actually appears in existing test cases as well.

Ah, I remember you mentioning issues with the instruction selection when dealing with multiplication.

So, I guess this is just another occurrence of this issue then?

@0x59616e 0x59616e self-assigned this Jun 13, 2023
@0x59616e
Copy link
Contributor

Hi @glaubitz

It would be great if you can provide the LLVM IR of this C++ snippet.

@glaubitz
Copy link
Contributor Author

It would be great if you can provide the LLVM IR of this C++ snippet.

No problem. See attached file.

future.ll.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants