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

Use compiler optimization for c68kexec.c #444

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

Conversation

piggynl
Copy link

@piggynl piggynl commented Nov 22, 2022

In riscv64, gcc generates unlinkable ELF for c68kexec.c, causing yabause fails to build.

This is a GCC bug. For long branches over 1MiB, RISC-V requires an indirect branch sequence (auipc + jalr) instead a direct branch (j). But GCC can only generate a j with an overflowed operand now. The jump if (!C68k_Initialised) goto C68k_Init; crosses almost the entire function body, which is over 1MiB in riscv64, so we will encounter this bug.

if (!C68k_Initialised) goto C68k_Init;

C68k_Init:
{
u32 i, j;
#include "c68k_ini.inc"
C68k_Initialised = 1;
}
return 0;

Enabling compiler optimization (specifically -freorder-blocks) allows GCC to reorder the C68k_Init block to the front of the function, so there won't be a long branch, and yabause will build successfully.

I noticed compiler optimization is disabled for c68kexec.c on purpose. Does any code depend on disabling compiler optimizations? Maybe there is something I can help with?

@piggynl piggynl force-pushed the c68kexec-compiler-optimization branch from 9b52763 to 8582a6a Compare November 22, 2022 03:33
@piggynl
Copy link
Author

piggynl commented Nov 22, 2022

Remove /Od for MSVC and replace -O0 with -fno-strict-aliasing for other compilers (MSVC does not implement strict aliasing optimizations at all).

With this change, GCC doesn't complain about code may be misoptimized unless ‘-fno-strict-aliasing’ is used anymore. I think the program should behave properly now.

@CoelacanthusHex
Copy link

This is a GCC bug. For long branches over 1MiB, RISC-V requires an indirect branch sequence (auipc + jal) instead a direct branch (j).

Should be auipc + jalr

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

Successfully merging this pull request may close these issues.

2 participants