Skip to content

Add macOS build and runtime compatibility fixes#785

Merged
jensen-yan merged 3 commits intoxs-devfrom
support-macos
Mar 16, 2026
Merged

Add macOS build and runtime compatibility fixes#785
jensen-yan merged 3 commits intoxs-devfrom
support-macos

Conversation

@jensen-yan
Copy link
Copy Markdown
Collaborator

@jensen-yan jensen-yan commented Mar 12, 2026

Make the XiangShan gem5 tree build and run on macOS/Apple Silicon.

This updates the Darwin toolchain setup, fixes runtime library lookup, and addresses a set of clang/libc++ and Python 3.14 compatibility issues that blocked local builds.

It also adjusts DRAMsim3 and shared-memory handling for macOS, documents the degraded difftest loading semantics on Darwin, and fixes a handful of format-string and case-sensitivity issues exposed by the platform.

Summary by CodeRabbit

  • New Features

    • Improved macOS/Homebrew discovery and RPATH handling for more reliable dependency lookup.
    • Platform-aware library loading to maintain expected behavior across OSes.
  • Bug Fixes

    • Fixed 64-bit integer formatting in logs and traces.
    • Improved platform-specific shared-memory handling and reopen/cleanup robustness.
  • Chores

    • Replaced deprecated utilities and updated quoting; standardized debug log categories and minor portability fixes.

Make the XiangShan gem5 tree build and run on macOS/Apple Silicon.

This updates the Darwin toolchain setup, fixes runtime library lookup, and addresses a set of clang/libc++ and Python 3.14 compatibility issues that blocked local builds.

It also adjusts DRAMsim3 and shared-memory handling for macOS, documents the degraded difftest loading semantics on Darwin, and fixes a handful of format-string and case-sensitivity issues exposed by the platform.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 12, 2026

📝 Walkthrough

Walkthrough

Adds macOS-aware build and runtime handling, fixes C++17/template issues, corrects 64-bit format specifiers, centralizes platform-aware difftest library loading, introduces platform-specific shared-memory code and state, replaces deprecated Python API, and renames a debug flag. (50 words)

Changes

Cohort / File(s) Summary
Build & macOS toolchain
SConstruct, ext/dramsim3/SConscript, site_scons/gem5_scons/builders/add_local_rpath.py, src/SConscript
Add RPATH variables and Homebrew include/lib/pkgconfig search on macOS, use platform-aware origin token in RPATH, conditionally link gomp on non-Darwin, and replace distutils.spawn.find_executable with shutil.which.
C++17 / template fixes
ext/drampower/src/CmdScheduler.h, src/arch/riscv/insts/vector.hh, src/arch/riscv/isa/formats/fp.isa, src/arch/riscv/isa/.../vector_arith.isa
Remove deprecated std::binary_function inheritance, add .template for dependent-member calls, and remove std:: qualifier from feclearexcept.
64-bit formatting & prints
src/cpu/pred/general_arch_db.cc, src/mem/cache/prefetch/cmc.cc, src/sim/arch_db.cc, src/mem/physical.cc
Change printf/SQL format specifiers to use %llx/%llu/%lld with explicit casts and add std::max<uint64_t> to ensure type consistency.
Platform-aware runtime loading
src/cpu/difftest.cc
Centralize difftest library loading into openDiffTestHandle() with macOS fallback using dlopen (RTLD_LOCAL/RTLD_FIRST) and non-macOS using dlmopen(..., RTLD_DEEPBIND).
Shared memory & mem utilities
src/mem/mem_util.cc, src/mem/mem_util.hh
Introduce platform-specific shared memory creation (Linux: memfd_create + huge pages; non-Linux: shm_open with /gem5- prefix), add namedShmObject flag, refine reopen/cleanup logic, and add APPLE/libc++ compatibility guards.
Logging / debug flag rename
src/cpu/pred/SConscript, src/cpu/pred/tage.cc, src/cpu/pred/tage_base.cc
Rename debug category from Tage to TAGE, update includes and all DPRINTF usages accordingly.
Python modernization
src/python/m5/main.py
Replace deprecated pipes.quote with shlex.quote.
Miscellaneous small edits
ext/drampower/src/CmdScheduler.h, src/cpu/pred/SConscript (small include/add/remove)
Minor include additions, comparator struct simplification, and removal of a debug header entry.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • Yakkhini
  • jueshiwenli

Poem

🐰 I nibble paths and tweak the RPATH trail,
Templates hop through code without fail,
Shared pages stitched for Linux and macOS,
Prints fixed, flags renamed — small chaos,
A rabbit applauds: build and run prevail!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 15.56% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add macOS build and runtime compatibility fixes' accurately summarizes the main objective of the pull request, which is to add macOS support and compatibility across multiple areas (build, runtime, library lookup, Python, DRAMsim3, etc.).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch support-macos
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0f35a9495e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
SConstruct (1)

540-552: Consider breaking after finding the first valid Homebrew prefix.

The current implementation adds both /opt/homebrew (Apple Silicon) and /usr/local (Intel Mac) to the paths if both directories exist. While rare, this could lead to library version conflicts if both prefixes contain different versions of the same dependency. Typically, only one Homebrew prefix is active on a given system.

♻️ Suggested fix to use only the first valid Homebrew prefix
             # Homebrew installs headers and libraries under a non-system
             # prefix on macOS (e.g., /opt/homebrew on Apple Silicon). Add the
             # common prefixes here so Configure checks can find dependencies
             # such as zstd without requiring per-shell environment setup.
             for brew_prefix in ('/opt/homebrew', '/usr/local'):
                 if os.path.isdir(brew_prefix):
                     env.Prepend(CPPPATH=[os.path.join(brew_prefix, 'include')])
                     env.Prepend(LIBPATH=[os.path.join(brew_prefix, 'lib')])
                     env['ENV']['PKG_CONFIG_PATH'] = \
                         os.path.join(brew_prefix, 'lib', 'pkgconfig') + \
                         (':' + env['ENV']['PKG_CONFIG_PATH']
                          if 'PKG_CONFIG_PATH' in env['ENV'] and
                          env['ENV']['PKG_CONFIG_PATH'] else '')
+                    break  # Use only the first valid Homebrew prefix
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@SConstruct` around lines 540 - 552, Loop through brew_prefixes but stop after
the first existing prefix to avoid adding both /opt/homebrew and /usr/local; in
the SConstruct loop that iterates over ('/opt/homebrew', '/usr/local') (variable
brew_prefix) check os.path.isdir(brew_prefix), apply env.Prepend(CPPPATH=...),
env.Prepend(LIBPATH=...), and update env['ENV']['PKG_CONFIG_PATH'] as currently
done, then break out of the loop (use a break) so only the first valid Homebrew
prefix is used.
src/sim/arch_db.cc (1)

195-198: Minor: Unnecessary casts for int parameters.

The way and is_write parameters are int types, so casting them to unsigned long long and using %llu is overkill. Using %d for these integer parameters would be more appropriate and cleaner.

♻️ Suggested improvement
     sprintf(sql,
             "INSERT INTO dcacheWayPreTrace(PC,VADDR, WAY, Tick, IsWrite,SITE)"
-            "VALUES(%llu,%llu,%llu,%llu,%llu,'%s');",
-            static_cast<unsigned long long>(pc), static_cast<unsigned long long>(vaddr),
-            static_cast<unsigned long long>(way), static_cast<unsigned long long>(tick),
-            static_cast<unsigned long long>(is_write), "dacheWayPre");
+            "VALUES(%llu,%llu,%d,%llu,%d,'%s');",
+            static_cast<unsigned long long>(pc), static_cast<unsigned long long>(vaddr),
+            way, static_cast<unsigned long long>(tick),
+            is_write, "dacheWayPre");
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/sim/arch_db.cc` around lines 195 - 198, The SQL value formatting
currently casts way and is_write to unsigned long long and uses "%llu"; update
the format string and arguments so that way and is_write use "%d" and pass them
as ints (remove the static_cast<unsigned long long> for way and is_write) while
keeping the existing casts for pc, vaddr, and tick; locate the VALUES(...) call
in arch_db.cc that builds the SQL (the line containing
"VALUES(%llu,%llu,%llu,%llu,%llu,'%s');") and change the two %llu placeholders
for way and is_write to %d and stop casting those two variables to unsigned long
long.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/cpu/difftest.cc`:
- Around line 43-57: openDiffTestHandle uses dlopen on macOS which will return
the same loaded image on repeated calls, causing ref-side globals to be shared
and corrupt multi-core difftest; modify openDiffTestHandle to add a static guard
that tracks whether the ref_so has already been loaded on macOS and, if a second
load is attempted, fail early (return nullptr or abort with a clear error)
instead of calling dlopen again; reference openDiffTestHandle and the multi-core
initialization path (e.g., NemuProxy::initState and its use of
difftest_set_mhartid/multiCore) so the guard prevents subsequent loads when
multiCore is enabled and documents the failure path for callers.

---

Nitpick comments:
In `@SConstruct`:
- Around line 540-552: Loop through brew_prefixes but stop after the first
existing prefix to avoid adding both /opt/homebrew and /usr/local; in the
SConstruct loop that iterates over ('/opt/homebrew', '/usr/local') (variable
brew_prefix) check os.path.isdir(brew_prefix), apply env.Prepend(CPPPATH=...),
env.Prepend(LIBPATH=...), and update env['ENV']['PKG_CONFIG_PATH'] as currently
done, then break out of the loop (use a break) so only the first valid Homebrew
prefix is used.

In `@src/sim/arch_db.cc`:
- Around line 195-198: The SQL value formatting currently casts way and is_write
to unsigned long long and uses "%llu"; update the format string and arguments so
that way and is_write use "%d" and pass them as ints (remove the
static_cast<unsigned long long> for way and is_write) while keeping the existing
casts for pc, vaddr, and tick; locate the VALUES(...) call in arch_db.cc that
builds the SQL (the line containing "VALUES(%llu,%llu,%llu,%llu,%llu,'%s');")
and change the two %llu placeholders for way and is_write to %d and stop casting
those two variables to unsigned long long.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ec51d356-91d0-4537-8068-517e89a4ab00

📥 Commits

Reviewing files that changed from the base of the PR and between ceed8e7 and 0f35a94.

📒 Files selected for processing (20)
  • SConstruct
  • ext/drampower/src/CmdScheduler.h
  • ext/dramsim3/SConscript
  • site_scons/gem5_scons/builders/add_local_rpath.py
  • src/SConscript
  • src/arch/riscv/insts/vector.hh
  • src/arch/riscv/isa/formats/fp.isa
  • src/arch/riscv/isa/vector/base/vector_arith.isa
  • src/arch/riscv/isa/vector/simple/vector_arith.isa
  • src/cpu/difftest.cc
  • src/cpu/pred/SConscript
  • src/cpu/pred/general_arch_db.cc
  • src/cpu/pred/tage.cc
  • src/cpu/pred/tage_base.cc
  • src/mem/cache/prefetch/cmc.cc
  • src/mem/mem_util.cc
  • src/mem/mem_util.hh
  • src/mem/physical.cc
  • src/python/m5/main.py
  • src/sim/arch_db.cc
💤 Files with no reviewable changes (1)
  • src/cpu/pred/SConscript

Change-Id: I21d87980fb91b9760f87f226d6859441e916b150
@github-actions
Copy link
Copy Markdown

🚀 Coremark Smoke Test Results

Branch IPC Change
Base (xs-dev) 2.2607 -
This PR 2.2607 ➡️ 0.0000 (0.00%)

✅ Difftest smoke test passed!

@jensen-yan
Copy link
Copy Markdown
Collaborator Author

当前还没完成实现macos 上的difftest, 可以先关闭difftest 来做测试。

Change-Id: I27736c6fd4ca8132fcd6934d05457d9f7cd0d05b
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/sim/arch_db.cc`:
- Around line 11-15: The function sqliteSignedInt currently casts uint64_t
values above INT64_MAX into negative numbers; add a runtime guard in
sqliteSignedInt that checks if value <= static_cast<uint64_t>(INT64_MAX) and
only then returns static_cast<long long>(static_cast<int64_t>(value)); otherwise
signal an error (e.g., throw std::overflow_error or std::out_of_range with a
clear message) to prevent silent wraparound; ensure any headers needed for the
exception ( <stdexcept> ) are included and callers handle or propagate the
exception appropriately.
- Around line 186-189: The code builds an SQL string into a fixed char sql[512]
with sprintf using site (from this->name().c_str()) which can overflow; replace
the sprintf call with snprintf(sql, sizeof(sql), "VALUES(%lld, %lld, %lld, %lld,
%lld, '%s');", sqliteSignedInt(pc), sqliteSignedInt(source),
sqliteSignedInt(paddr), sqliteSignedInt(vaddr), sqliteSignedInt(stamp), site)
and then validate the returned length (int n) is >= 0 and < (int)sizeof(sql); if
it’s >= sizeof(sql) handle truncation safely (e.g., log error and skip insert or
truncate site) and ensure sql is NUL-terminated before using it in the insert
routine in arch_db.cc where sql, sprintf, site, this->name().c_str(), and
sqliteSignedInt(...) are referenced.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 998e0d93-fa52-4159-9ebc-f39646ca5fe9

📥 Commits

Reviewing files that changed from the base of the PR and between c3195ea and 18e70bf.

📒 Files selected for processing (3)
  • SConstruct
  • src/cpu/pred/general_arch_db.cc
  • src/sim/arch_db.cc
🚧 Files skipped from review as they are similar to previous changes (2)
  • SConstruct
  • src/cpu/pred/general_arch_db.cc

@github-actions
Copy link
Copy Markdown

🚀 Coremark Smoke Test Results

Branch IPC Change
Base (xs-dev) 2.2665 -
This PR 2.2665 ➡️ 0.0000 (0.00%)

✅ Difftest smoke test passed!

@jensen-yan jensen-yan merged commit dbc3782 into xs-dev Mar 16, 2026
2 checks passed
@jensen-yan jensen-yan deleted the support-macos branch March 16, 2026 03:11
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.

1 participant