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

Lightning+Lightrec updates and other optimizations #862

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

ZachCook
Copy link
Collaborator

@ZachCook ZachCook commented Sep 25, 2022

Split out cpu_lightrec.cpp from cpu.cpp instead of being mashed together.

Added hugepage, codebuffer, mmap at 0x0, and SP GP hit RAM optimizations and fallbacks, see commits for more details.

Use lightrec's GTE registers directly when not using beetle interpreter (while maintaining backwards compatibility with savestates).

Removed the Cycle Timing Check dynarec option.

@lgtm-com
Copy link

lgtm-com bot commented Sep 25, 2022

This pull request introduces 1 alert when merging 8ae372a into bd6b9ef - view on LGTM.com

new alerts:

  • 1 for Missing return statement

@@ -375,7 +376,8 @@ ifeq ($(HAVE_LIGHTREC), 1)
$(DEPS_DIR)/lightrec/memmanager.c \
$(DEPS_DIR)/lightrec/optimizer.c \
$(DEPS_DIR)/lightrec/reaper.c \
$(DEPS_DIR)/lightrec/regcache.c
$(DEPS_DIR)/lightrec/regcache.c \
$(DEPS_DIR)/lightrec/tlsf/tlsf.c
Copy link
Contributor

Choose a reason for hiding this comment

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

You shouldn't have to compile TLSF since you disabled the code buffer support (ENABLE_CODE_BUFFER=0)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ENABLE_CODE_BUFFER=1 was set in 3012b97 of this PR, there were many changes squashed together, and it could maybe use even more to make this more clear maybe?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I saw it after my message. I guess this change should be squashed into 3012b97 then.

@LibretroAdmin LibretroAdmin marked this pull request as ready for review September 25, 2022 18:29
@Ryunam
Copy link

Ryunam commented Sep 29, 2022

After testing this with a few games on my Windows 11 system, I have noticed a few Runahead-related issues:

  • As reported on Discord, enabling Runahead in RetroArch with either single instance or secondary instance mode and having the dynarec active seems to be triggering unpredictable crashes with this PR, at several spots. This is with the same values/settings used with current master. Also, the amount of dynarec cycles do not appear to affect the likelihood of the crashes.
  • Based on my testings so far, the crashes can occur either at startup, especially if one fastforwards right after launching the content, or when loading a save. The latter in particular is being triggered here consistently whenever I try to load my "Akumajou Dracula X: Gekka no Yasoukyoku" save from the memory card screen with Runahead active, which worked normally before this PR. Again, fastforwarding seems to greatly increase the chance of RetroArch and the core suddenly crashing with this setup.
  • If Dynarec SPU Samples is set to any value above 1 and Runahead is enabled with Single Instance mode, the audio will get noticeably choppy and distorted. Disabling Runahead or setting it to Second Instance Mode makes it sound normal again.

@lgtm-com
Copy link

lgtm-com bot commented Oct 1, 2022

This pull request introduces 1 alert when merging 1e71531 into bd6b9ef - view on LGTM.com

new alerts:

  • 1 for Missing return statement

@LibretroAdmin
Copy link
Contributor

@Ryunam Hi, are your issues addressed now?

@Ryunam
Copy link

Ryunam commented Oct 1, 2022

Okay, I have performed a few more tests after the last commit:

  • The audio issue caused by the combination of SPU Samples set to a value higher than 1 and Runhead Single Instance mode seems to be fixed. I tried switching the values back and forth during gameplay and everything appears to sound normally.
  • However, the Dynarec SPU Samples by itself (with or without Runahead) seems not to play nicely with a few games. "Final Fantasy" (the PS1 remake of FF1) stood out during my testing, as any value higher than 1 triggers missing and sustained notes in the music of that game. A clear example can be seen by starting the game, then immediately entering the first city, Corneria. Set the samples to 16 and the music will start having missing parts or oddly prolonged notes. Setting the Samples value back to 1 makes the music play normally again. I assume this is partly expected behavior, as the feature in question might not work well with certain games depending on how the music files are streamed, but i figure it's worth pointing it out.
  • Interestingly, the crashes do not seem to happen anymore either! I have tried to trigger them multiple times and so far so good.

@ZachCook
Copy link
Collaborator Author

ZachCook commented Oct 1, 2022

@Ryunam
I'm surprised the crashes disappeared, though I couldn't reproduce so just let me know if they come back.

The SPU Samples option was expected to cause that, I'll add a note in the description about it, it's basically just running the sampling at a lower frequency and then running multiple times to catch up, it's a performance hack, I'm surprised it works as well as it does. Though now that you found a game I can test I might be able to improve it further by doing some run time detection or tweaking the timing code, but that may be a bigger project for the future.

@Ryunam
Copy link

Ryunam commented Oct 1, 2022

Sorry @ZachCook, I stand corrected. The crashes appeared once again while I was testing other games, so they seem to be unrelated to the SPU changes.
They are also very unpredictable: sometimes the affected games will launch normally and then crash when loading a save, sometimes the crash will occur almost immediately at startup, some other times they won't happen at all.

One consistent situation where I can reproduce the crash right now is with the Japanese PS1 port of Street Fighter Zero 2:

  • Under Core Options -> Emulation Hacks, set Skip BIOS to ON.
  • Have Runahead enabled with Second Instance ON in a per-core override, to make it activate automatically upon launching the game.
  • The amount of Runahead frames does not seem to matter. I'm using the default value of 1 in this case.
  • This was tested with both the glcore and Vulkan video drivers in RA.
  • Launch Street Fighter Zero 2 and hold the Fastforward button right after starting the game. One thing that may also contribute to the crash being triggered is pressing/mashing the Circle button while it's fastforwarding, but this was not verified.
  • On my system the game and RA will crash either after the Capcom logo animation, or later on when selecting an option from the main menu (for example, "Versus Mode").

The same core as compiled from current master, before this PR, never triggers any crash with these same steps and/or settings.

@LibretroAdmin
Copy link
Contributor

Hi there, what is the state of this PR as it stands? Are there any roadblocks?

@ZachCook
Copy link
Collaborator Author

Hi there, what is the state of this PR as it stands? Are there any roadblocks?

I'll update it again to latest lightrec+lightning, and see if I can reproduce the above mentioned crash, or at least narrow down what triggers it, I wouldn't really want to merge this until that is dealt with.

@ZachCook
Copy link
Collaborator Author

Updated to recently released Lightning 2.2.0 and Lightrec 0.6

Still working on crashes with runahead and loading, the hack I had there (restart entire dynarec and switch to interpreter for a while) looks awfully suspect and I'm working on a proper fix to at least rule that out as a cause if it doesn't just fix it.

@lgtm-com
Copy link

lgtm-com bot commented Nov 17, 2022

This pull request introduces 1 alert and fixes 1 when merging c41b8a7 into f8a904e - view on LGTM.com

new alerts:

  • 1 for FIXME comment

fixed alerts:

  • 1 for FIXME comment

Heads-up: LGTM.com's PR analysis will be disabled on the 5th of December, and LGTM.com will be shut down ⏻ completely on the 16th of December 2022. Please enable GitHub code scanning, which uses the same CodeQL engine ⚙️ that powers LGTM.com. For more information, please check out our post on the GitHub blog.

@LibretroAdmin
Copy link
Contributor

@ZachCook Is this ready for merging now? Will this have any known regressions vs. the current state of things?

@ZachCook
Copy link
Collaborator Author

@ZachCook Is this ready for merging now? Will this have any known regressions vs. the current state of things?

@LibretroAdmin It is not ready yet, I am seeing lots of crashing now than can be triggered by any kind of run-ahead or loading a save state.

You could apply just 8814bcb for now, it is quite a useful improvement by itself.

@LibretroAdmin
Copy link
Contributor

OK, I cherry-picked that commit for now and pushed it to master

@ZachCook ZachCook marked this pull request as draft September 16, 2024 00:16
@ZachCook
Copy link
Collaborator Author

ZachCook commented Sep 16, 2024

Still have to re-fix the runahead/savestate issue right after bios since I ripped out the previous next_interpreter hack, but otherwise I have tried to update and clean up the code a bit.

I think some of the older commits are a bit messy/WIP that are fixed later in the series since there was such a long period of time since I last was working on this, so it might be easier to view the changes as a whole for some parts in cpu/cpu_lightrec.

Fix bios freeze with lightrec, AssertIRQ must change lightrec's CAUSE too

cleanup unused cop0 functions
Codebuffer is much faster due to lightrec's 32-bit LUT optimization

Moved lightning.h to lightning-actual.h and made lightning.h include lightning-actual.h and define HAVE_MMAP for lightning, instead of defining in the Makefile which applied it to other parts of beetle, causing issues on Windows in libretro-common/vfs
This allows less code to be emitted by lightrec, but will only
be used if root user enables it with sysctl -w vm.mmap_min_addr=0
…htrec api

This was only useful for debugging, users don't benefit from it, the config
will fallback to using the beetle interpreter if it was selected

The lightrec execute_one function this option called was removed, for
equivalent debugging pass current timestamp instead of next_event_ts to
lightrec_execute

Use lightrec cop2_notify for PGXP, update interpreter to new api
subrepo:
  subdir:   "deps/lightning"
  merged:   "edc57df2"
upstream:
  origin:   "https://git.savannah.gnu.org/git/lightning.git"
  branch:   "master"
  commit:   "bfd695a9"
git-subrepo:
  version:  "0.4.9"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "ea10886"
subrepo:
  subdir:   "deps/lightrec"
  merged:   "ea20362c"
upstream:
  origin:   "https://github.com/pcercuei/lightrec.git"
  branch:   "master"
  commit:   "ea20362c"
git-subrepo:
  version:  "0.4.9"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "ea10886"
tweak lightrec startup and syncing stuff
remove next_interpreter hack
tweak setoptions, should be safer
Disabled by default for max compatibility
Enabling should generate faster code for most normal games that
do not have sp and gp registers pointing outside of RAM
This was broken by cpu.cpp and cpu_lightrec.cpp being split apart
Add breakpoint handling to match interpreter
Fix setting of options to lightrec
@ZachCook ZachCook force-pushed the libretro-dynarec-backup branch 2 times, most recently from ea881ad to c3a4eaa Compare September 18, 2024 01:12
Replaces supported_io_bases array with min/inc/max_io_base
Makes it easier to avoid ranges of address for different platforms
Fix unmap of lightrec codebuffer
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.

4 participants