Skip to content

wasm: Remove PIC guards from Wasm.S#142

Open
angerman wants to merge 2 commits intostable-ghc-9.14from
fix/wasm-remove-pic-guards
Open

wasm: Remove PIC guards from Wasm.S#142
angerman wants to merge 2 commits intostable-ghc-9.14from
fix/wasm-remove-pic-guards

Conversation

@angerman
Copy link

@angerman angerman commented Feb 18, 2026

Removes #if !defined(__PIC__) guards from rts/wasm/Wasm.S.

These guards were added to prevent building predefined GlobalRegs for WASM PIC mode (due to dylink limitations with relocatable wasm globals). After disabling tables-next-to-code for stage3 WASM builds, the guards are no longer needed.

Related: commit 7210be4 "Fix build of WASM"

@angerman
Copy link
Author

This reverts ghc@98a32ec.

@angerman
Copy link
Author

This seems to originate from #134

@angerman
Copy link
Author

This change is somehow part of #140, where it most certainly does not belong to.

This commit teaches GHC's internal package representation (UnitInfo)
about package data directories, enabling the compiler to properly find
files installed via Cabal's data-files mechanism.

Background:
-----------
Previously, while Cabal's InstalledPackageInfo includes dataDir, and
this field appears in package .conf files, it was intentionally filtered
out during conversion to GHC's internal DbUnitInfo format. The compiler's
UnitInfo type didn't expose data directories because compilers typically
don't need them.

However, certain backends may need to access data files during compilation
or linking. This commit adds proper support for exposing data directories
to the compiler.

Changes:
--------
1. libraries/ghc-boot/GHC/Unit/Database.hs:
   - Add unitDataDir :: Maybe FilePathST field to GenericUnitInfo
   - Update Binary instance (put/get) to serialize the field
   - Update mungeUnitInfoPaths to handle ${pkgroot} variable substitution
   - Add documentation explaining the field's purpose

2. utils/ghc-pkg/Main.hs:
   - Stop filtering out dataDir in convertPackageInfoToCacheFormat
   - Convert Cabal's dataDir to GHC's unitDataDir (Nothing if empty)

Benefits:
---------
- Uses Cabal's data-files mechanism as intended
- Discoverable through package metadata (not hardcoded paths)
- Future-proof: enables any package's compiler code to access data files
- Standard path munging for relocatable installs via ${pkgroot}

Note: This breaks binary package cache format - caches will be rebuilt
on next GHC build.
This commit implements support for WASM's GlobalRegs pattern where global
registers are defined in a separate assembly file that's compiled on-demand
during executable linking, rather than being statically linked into the RTS.

Changes:
--------

1. rts/wasm/Wasm.S:
   - Remove PIC (#ifdef PIC) guards that were preventing WASM builds
   - WASM doesn't use position-independent code in the traditional sense
   - Clean up assembly to work correctly for WASM architecture

2. rts/wasm/WasmGlobalRegs.S (NEW):
   - Extract GlobalRegs definitions into a separate file
   - Contains WASM-specific global register declarations
   - Will be compiled on-demand during executable linking
   - Installed via rts.cabal data-files mechanism

3. rts/rts.cabal:
   - Add wasm/WasmGlobalRegs.S to data-files
   - Makes the file available for compiler access during linking
   - See Note [WASM GlobalRegs Linking] for rationale

4. compiler/GHC/Linker/Executable.hs:
   - Add mkWasmGlobalRegsObj function for on-demand compilation
   - Searches for WasmGlobalRegs.S via unitDataDir (primary path)
   - Falls back to library-relative and source tree paths
   - Compiles to temporary object file during executable linking
   - Includes comprehensive error reporting if file not found
   - Add import for GHC.Data.ShortText and noSrcSpan

5. compiler/GHC/Platform/Ways.hs:
   - Update WASM way definition to handle GlobalRegs differently
   - Fix module import cycle by adjusting imports
   - Add reference to mkWasmGlobalRegsObj for documentation

Architecture:
-------------
The WASM backend requires GlobalRegs to be compiled separately per
executable rather than being part of the RTS library. This is because:
- WASM has specific requirements for global register handling
- Each executable needs its own GlobalRegs object
- Cannot be statically linked into the RTS

The linker now:
1. Detects WASM architecture during executable linking
2. Locates WasmGlobalRegs.S via package metadata (unitDataDir)
3. Compiles it on-demand to a temporary object file
4. Includes it in the final link

This uses the new unitDataDir support to properly find the file via
Cabal's data-files mechanism rather than hardcoded path assumptions.
@angerman angerman force-pushed the fix/wasm-remove-pic-guards branch from 6cd43f7 to e156b65 Compare February 18, 2026 11:28
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