Skip to content

Restoring OpenCL support in Mesa #62933

Closed

Description

@athas

Nixpkgs packages Mesa (maintained by @vcunat), which has a reasonable operational OpenCL implementation that works at least on AMD GPUs (and whose OpenGL implementation is probably better than AMDs proprietary one). Unfortunately, OpenCL support was explicitly removed from the package in d21b670 by @edolstra, apparently to remove dependency bloat (I guess the reason is that OpenCL needs an entire C compiler frontend at runtime).

I'm a Nix novice, but I've poked around a bit to see how to fix this. First, libclc must be updated to work at all (already made a pull request). Then, reverting (in spirit) d21b670 makes the derivation at least compile the OpenCL bits again, most importantly libMesaOpenCL.so (a whopping 53MiB file!). The mesa package already contains a separate output "drivers" that depends on LLVM, so perhaps the OpenCL bits could be moved there? Alternatively, a new "opencl" output could be made.

There is one more problem. OpenCL specifies a directory /etc/OpenCL/vendors, which should contain text files containing names of ICDs (for example libMesaOpenCL.so). In Nix, this is instead /run/opengl-driver/etc/OpenCL/vendors. I don't understand how to make stuff available there.

Activity

  1. abbradar commented on Jun 10, 2019

    @abbradar
    Member

    We could try making a separate opencl output, this sounds good. To add something to /run/opengl-driver add a package to hardware.opengl.extraPackages.

  2. matthewbauer commented on Jun 10, 2019

    @matthewbauer
    Member

    Does opencl even need clang to work? This seems surprising if it did. On the other hand, lots of drivers need llvm to work.

  3. athas commented on Jun 10, 2019

    @athas
    ContributorAuthor

    I don't know if it needs the CLI program clang, but it certainly needs a C frontend, which I assume is built with libclang.

  4. added this to the 19.09 milestone on Jun 10, 2019
  5. athas commented on Jun 10, 2019

    @athas
    ContributorAuthor

    It looks like just reverting d21b670 and patching up a few omissions makes something that at least builds libMesaOpenCL.so (in mesa-drivers) and even /run/opengl-driver/etc/OpenCL/vendors/mesa.icd seems to show up correctly. I guess mesa-drivers is already in hardware.opengl.extraPackages. Before I create an opencl output, I'd like to understand whether it's still worthwhile. mesa-drivers already looks pretty large, so is the overhead of carrying libclang all that significant?

    Also, I can't yet claim that it actually works. The Mesa OpenCL ICD is picked up just fine, but my device (Vega 64 GPU) is not found. Haven't investigated closely yet. It should be supported, as it works fine in the Mesa that Fedora ships.

    In case anyone is curious, these are the changes I made:

    diff --git a/pkgs/development/libraries/mesa/default.nix b/pkgs/development/libraries/mesa/default.nix
    index 5bc1dfaf878..4826722c956 100644
    --- a/pkgs/development/libraries/mesa/default.nix
    +++ b/pkgs/development/libraries/mesa/default.nix
    @@ -10,6 +10,7 @@
     , vulkanDrivers ? null
     , eglPlatforms ? [ "x11" ] ++ lib.optionals stdenv.isLinux [ "wayland" "drm" ]
     , OpenGL, Xplugin
    +, libclc
     }:
     
     /** Packaging design:
    @@ -69,6 +70,7 @@ in
     let
       version = "18.3.4";
       branch  = head (splitString "." version);
    +  clang = if llvmPackages ? clang-unwrapped then llvmPackages.clang-unwrapped else llvmPackages.clang;
     in
     
     let self = stdenv.mkDerivation {
    @@ -131,7 +133,9 @@ let self = stdenv.mkDerivation {
         "--enable-llvm-shared-libs"
         (enableFeature stdenv.isLinux "omx-bellagio")
         (enableFeature stdenv.isLinux "va")
    -    "--disable-opencl"
    +   "--enable-opencl"
    +   "--enable-opencl-icd"
    +   "--with-clang-libdir=${clang}/lib"
       ];
     
       nativeBuildInputs = [
    @@ -147,7 +151,7 @@ let self = stdenv.mkDerivation {
       buildInputs = with xorg; [
         expat llvmPackages.llvm libglvnd xorgproto
         libX11 libXext libxcb libXt libXfixes libxshmfence libXrandr
    -    libffi libvdpau libelf libXvMC
    +    libffi libvdpau libelf libXvMC libclc clang
         libpthreadstubs openssl /*or another sha1 provider*/
       ] ++ lib.optionals (elem "wayland" eglPlatforms) [ wayland wayland-protocols ]
         ++ lib.optionals stdenv.isLinux [ valgrind-light libomxil-bellagio libva-minimal ];
    @@ -173,7 +177,9 @@ let self = stdenv.mkDerivation {
           $out/lib/vdpau         \
           $out/lib/bellagio      \
           $out/lib/libxatracker* \
    -      $out/lib/libvulkan_*
    +      $out/lib/libvulkan_*   \
    +      $out/lib/gallium-pipe  \
    +      $out/lib/libMesaOpenCL*
     
         # Move other drivers to a separate output
         mv $out/lib/dri/* $drivers/lib/dri # */
    
  6. matthewbauer commented on Jun 10, 2019

    @matthewbauer
    Member

    Looks good! d21b670 actually predates multiple outputs, so I suspect it is not a problem anymore?

  7. abbradar commented on Jun 10, 2019

    @abbradar
    Member
  8. vcunat commented on Jun 10, 2019

    @vcunat
    Member

    It does not predate multi-output mesa derivation (I suppose you meant libclang and I understood it wrong); you can have a look at the whole file around the diff. Mesa drivers will be present on any graphical NixOS installation that uses free drivers (basically all except those using binary nvidia), so I can understand this default would better not be too bloated.

    This new lib + libclang output might be roughly 100 MB of new stuff in NixOS closures (even though C-like developers will often have some libclang already; *2 with 32-bit included), so from this quick estimate I'd expect it should better be a separate output.

  9. athas commented on Jun 10, 2019

    @athas
    ContributorAuthor

    What I'm currently stuck on is libMesaOpenCL.so, which as built contains a reference to /nix/store/i9xsrfn3isrpzdw1xn2rxdfiwg3n230d-mesa-noglu-18.3.4/lib/gallium-pipe.so, but this file has to be located in -drivers (or -opencl) to avoid circular references. The common solution that I find in other packages is to perform a text substitution in the corresponding .la file, but libMesaOpenCL.so is not linked like a normal library, but instead dlopen()ed by the ICD loader (the actual libOpenCL.so, the ocl-icd package in Nix).

    I'll see if Mesa has some compilation options I can tweak, but how is this kind of stuff otherwise handled normally?

  10. vcunat commented on Jun 10, 2019

    @vcunat
    Member

    IIRC we now don't really need to separate .out and .drivers, as package closures should only refer to the libglvnd stubs. We indirect some paths through ${libglvnd.driverLink} as well, and dlopen() cases would be likely suspects for that. (I haven't really thought about these ATM.)

  11. athas commented on Jun 11, 2019

    @athas
    ContributorAuthor

    So you're suggesting that we remove .drivers altogether?

  12. vcunat commented on Jun 11, 2019

    @vcunat
    Member

    I expect it's a possibility, in case it helps dealing with the cycles.

  13. athas commented on Jun 11, 2019

    @athas
    ContributorAuthor

    It's fine with me, as it'll certainly simplify my task here (rebuilding now to see whether that's actually the case). I'm too much of a novice to be able to evaluate the impact, however. I assume the split was made for a reason, and as I've been using NixOS for all of two days, I don't really know if those reasons have changed since then.

  14. 9 remaining items

  15. athas commented on Jul 6, 2019

    @athas
    ContributorAuthor

    I managed to move the OpenCL parts into a separate .opencl output, which means the .drivers output size is essentially unchanged compared to without OpenCL. I can make a pull request with my changes to discuss further, but they still depend on first or concurrently bumping libclc (pull request here).

  16. modified the milestones: 19.09, 20.03 on Sep 4, 2019
  17. modified the milestones: 20.03, 20.09 on Feb 5, 2020
  18. stale commented on Aug 3, 2020

    @stale

    Hello, I'm a bot and I thank you in the name of the community for opening this issue.

    To help our human contributors focus on the most-relevant reports, I check up on old issues to see if they're still relevant. This issue has had no activity for 180 days, and so I marked it as stale, but you can rest assured it will never be closed by a non-human.

    The community would appreciate your effort in checking if the issue is still valid. If it isn't, please close it.

    If the issue persists, and you'd like to remove the stale label, you simply need to leave a comment. Your comment can be as simple as "still important to me". If you'd like it to get more attention, you can ask for help by searching for maintainers and people that previously touched related code and @ mention them in a comment. You can use Git blame or GitHub's web interface on the relevant files to find them.

    Lastly, you can always ask for help at our Discourse Forum or at #nixos' IRC channel.

  19. added
    2.status: stale
    on Aug 3, 2020
  20. athas commented on Aug 3, 2020

    @athas
    ContributorAuthor

    Still important to me, but not high priority. I'll look at it again eventually.

  21. removed
    2.status: stale
    on Aug 3, 2020
  22. modified the milestones: 20.09, 21.03 on Dec 20, 2020
  23. stale commented on Jun 18, 2021

    @stale

    I marked this as stale due to inactivity. → More info

  24. added
    2.status: stale
    on Jun 18, 2021
  25. sbruder commented on Sep 19, 2021

    @sbruder
    Contributor

    OpenCL support in Mesa was restored in #136402.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.status: stale

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions