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

Division by non-multiple of 2 causes compiler to crash #426

Closed
mateoconlechuga opened this issue Feb 2, 2023 · 2 comments
Closed

Division by non-multiple of 2 causes compiler to crash #426

mateoconlechuga opened this issue Feb 2, 2023 · 2 comments
Assignees

Comments

@mateoconlechuga
Copy link
Member

mateoconlechuga commented Feb 2, 2023

int a(int b) { return b / sizeof(int); }

This causes a compiler crash when compiled with any -O flag except -Oz:

fatal error: error in backend: unable to legalize instruction: %21:_(s48) = G_LSHR %19:_, %22:_(s8) (in function: a)
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: /home/hew/CEdev/bin/ez80-clang -S -Os src/main.c
1.	<eof> parser at end of file
2.	Code generation
3.	Running pass 'Function Pass Manager' on module 'src/main.c'.
4.	Running pass 'Legalizer' on function '@a'
 #0 0x000000000201d1e3 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/hew/CEdev/bin/ez80-clang+0x201d1e3)
 #1 0x000000000201b10e llvm::sys::RunSignalHandlers() (/home/hew/CEdev/bin/ez80-clang+0x201b10e)
 #2 0x000000000201c70d llvm::sys::CleanupOnSignal(unsigned long) (/home/hew/CEdev/bin/ez80-clang+0x201c70d)
 #3 0x0000000001f94e2a (/home/hew/CEdev/bin/ez80-clang+0x1f94e2a)
 #4 0x0000000001f94dcb (/home/hew/CEdev/bin/ez80-clang+0x1f94dcb)
 #5 0x00000000020174c7 llvm::sys::Process::Exit(int, bool) (/home/hew/CEdev/bin/ez80-clang+0x20174c7)
 #6 0x00000000011d0dc2 (/home/hew/CEdev/bin/ez80-clang+0x11d0dc2)
 #7 0x0000000001f98a38 llvm::report_fatal_error(llvm::Twine const&, bool) (/home/hew/CEdev/bin/ez80-clang+0x1f98a38)
 #8 0x0000000002de4fa4 (/home/hew/CEdev/bin/ez80-clang+0x2de4fa4)
 #9 0x0000000002de52cd llvm::reportGISelFailure(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, char const*, llvm::StringRef, llvm::MachineInstr const&) (/home/hew/CEdev/bin/ez80-clang+0x2de52cd)
#10 0x0000000002d8d175 llvm::Legalizer::runOnMachineFunction(llvm::MachineFunction&) (/home/hew/CEdev/bin/ez80-clang+0x2d8d175)
#11 0x00000000015957ee llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/home/hew/CEdev/bin/ez80-clang+0x15957ee)
#12 0x0000000001959a8d llvm::FPPassManager::runOnFunction(llvm::Function&) (/home/hew/CEdev/bin/ez80-clang+0x1959a8d)
#13 0x000000000195f703 llvm::FPPassManager::runOnModule(llvm::Module&) (/home/hew/CEdev/bin/ez80-clang+0x195f703)
#14 0x000000000195a0cf llvm::legacy::PassManagerImpl::run(llvm::Module&) (/home/hew/CEdev/bin/ez80-clang+0x195a0cf)
#15 0x000000000229d914 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) (/home/hew/CEdev/bin/ez80-clang+0x229d914)
#16 0x0000000002b066f8 (/home/hew/CEdev/bin/ez80-clang+0x2b066f8)
#17 0x00000000039c3b03 clang::ParseAST(clang::Sema&, bool, bool) (/home/hew/CEdev/bin/ez80-clang+0x39c3b03)
#18 0x0000000002a592a7 clang::FrontendAction::Execute() (/home/hew/CEdev/bin/ez80-clang+0x2a592a7)
#19 0x00000000029d0aa6 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/hew/CEdev/bin/ez80-clang+0x29d0aa6)
#20 0x0000000002b00c6b clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/hew/CEdev/bin/ez80-clang+0x2b00c6b)
#21 0x00000000011d0a78 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/home/hew/CEdev/bin/ez80-clang+0x11d0a78)
#22 0x00000000011cebab (/home/hew/CEdev/bin/ez80-clang+0x11cebab)
#23 0x0000000002858a42 (/home/hew/CEdev/bin/ez80-clang+0x2858a42)
#24 0x0000000001f94dad llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/home/hew/CEdev/bin/ez80-clang+0x1f94dad)
#25 0x0000000002858574 clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, bool*) const (/home/hew/CEdev/bin/ez80-clang+0x2858574)
#26 0x000000000281bae4 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&) const (/home/hew/CEdev/bin/ez80-clang+0x281bae4)
#27 0x000000000281bf77 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) const (/home/hew/CEdev/bin/ez80-clang+0x281bf77)
#28 0x00000000028386f8 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) (/home/hew/CEdev/bin/ez80-clang+0x28386f8)
#29 0x00000000011ce2b7 main (/home/hew/CEdev/bin/ez80-clang+0x11ce2b7)
#30 0x00007fa6b7c97083 __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:342:3
#31 0x00000000011cb8ae _start (/home/hew/CEdev/bin/ez80-clang+0x11cb8ae)
ez80-clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
clang version 15.0.0 (https://github.com/jacobly0/llvm-project 6d9aa7a16db70f3d02ae304b08d4a03011281fef)
Target: ez80
Thread model: posix
InstalledDir: /home/hew/CEdev/bin
ez80-clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
ez80-clang: note: diagnostic msg: /tmp/main-b2e0d4.c
ez80-clang: note: diagnostic msg: /tmp/main-b2e0d4.sh
ez80-clang: note: diagnostic msg: 

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

=> See here jacobly0/llvm-project#12

@mateoconlechuga mateoconlechuga changed the title Structure causes compiler to crash Division by 3 causes compiler to crash Feb 2, 2023
@mateoconlechuga mateoconlechuga changed the title Division by 3 causes compiler to crash Division by multiple of 3 causes compiler to crash Feb 2, 2023
@mateoconlechuga mateoconlechuga changed the title Division by multiple of 3 causes compiler to crash Division by non-multiple of 2 causes compiler to crash Feb 2, 2023
@runer112
Copy link
Member

runer112 commented Sep 19, 2023

This seems really important to fix ASAP. Other optimization levels produce faster code, often significantly. Additionally, I've found that they may even produce a smaller binary because inlining, as long as it's not excessive, actually saves space due to omitting all the value juggling necessary when every function is emitted as an actual function.

Even if the fix is just to make these non-multiple-of-two divisions call the general division routine instead of using some optimized multiply trick, slow code is better than no code.

@adriweb
Copy link
Member

adriweb commented Nov 17, 2023

Fixed in latest compiler version.

in -Oz:

_a:
	call	__frameset0
	ld	hl, (ix + 6)
	ld	bc, 3
	call	__idivu
	pop	ix
	ret

in -O1/-O2/-O3:

_a:
	ld	iy, 0
	add	iy, sp
	ld	hl, (iy + 3)
	ld	iy, -5592405
	ld	de, 0
	ld.sis	bc, 0
	push	de
	push	de
	push	iy
	call	__llmulu
	ld	iy, 9
	add	iy, sp
	ld	sp, iy
	ld	bc, -1
	ld	iy, 0
	push	iy
	push	bc
	push	bc
	call	__lland
	ld	iy, 9
	add	iy, sp
	ld	sp, iy
	ld	iy, 24
	push	iy
	call	__llshru
	pop	de
	ld	c, 1
	call	__ishru
	ret

in -O0:

_a:
	push	ix
	ld	ix, 0
	add	ix, sp
	ld	hl, -3
	add	hl, sp
	ld	sp, hl
	ld	hl, (ix + 6)
	ld	(ix - 3), hl
	ld	hl, (ix - 3)
	ld	iy, -5592405
	ld	de, 0
	ld.sis	bc, 0
	push	de
	push	de
	push	iy
	call	__llmulu
	ld	iy, 9
	add	iy, sp
	ld	sp, iy
	ld	bc, -1
	ld	iy, 0
	push	iy
	push	bc
	push	bc
	call	__lland
	ld	iy, 9
	add	iy, sp
	ld	sp, iy
	ld	iy, 24
	push	iy
	call	__llshru
	ld	iy, 3
	add	iy, sp
	ld	sp, iy
	ld	c, 1
	call	__ishru
	ld	iy, 3
	add	iy, sp
	ld	sp, iy
	pop	ix
	ret

@adriweb adriweb closed this as completed Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants