Skip to content

mesa: restore OpenCL support. - #136402

Merged
primeos merged 1 commit into
NixOS:stagingfrom
sbruder:mesa-opencl
Sep 18, 2021
Merged

primeos merged 1 commit into
NixOS:stagingfrom
sbruder:mesa-opencl

Conversation

@sbruder

@sbruder sbruder commented Sep 1, 2021

Copy link
Copy Markdown
Contributor

Continuation of #82729.

Works with a Radeon RX 480 on NixOS 21.05, see #82729 (comment) for details.

Motivation for this change
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • 21.11 Release Notes (or backporting 21.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@ofborg ofborg Bot added the 8.has: package (new) This PR adds a new package label Sep 1, 2021
@ofborg
ofborg Bot requested review from primeos and vcunat September 1, 2021 17:20
@ofborg ofborg Bot added 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 1001-2500 This PR causes many rebuilds on Darwin and should most likely target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 1001-2500 This PR causes many rebuilds on Linux and should target the staging branches. labels Sep 1, 2021
Comment thread pkgs/top-level/all-packages.nix Resolved

@primeos primeos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall this looks mergeable to me with a few changes. Thanks!

@vobloeb

vobloeb commented Sep 1, 2021

Copy link
Copy Markdown
Contributor

Works on Nvidia P2000 (not exactly this PR, but my own rebase of #82729 to the latest mesa which is basically the same as this PR)

@sbruder

sbruder commented Sep 1, 2021

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews, I think I’ve addressed them.

@sbruder

sbruder commented Sep 1, 2021

Copy link
Copy Markdown
Contributor Author

Force pushed to change the commit message (there shouldn’t be a . at the end).

@ofborg
ofborg Bot requested a review from primeos September 1, 2021 21:19
@vobloeb

vobloeb commented Sep 2, 2021

Copy link
Copy Markdown
Contributor

This way to enable OpenCL (taken from @sbruder #82729 (comment))

{ pkgs, ... }:
{
  hardware.opengl =
    let
      pkgsMesaClover = import
        (pkgs.fetchFromGitHub {
          owner = "sbruder";
          repo = "nixpkgs";
          rev = "41d33334ec2d07da8f5bd0f3749949241c42266b";
          sha256 = "sha256-sA/kwZ8kXuRC/sLjja2ISpyTnlEwZjifyYeiz9hFWRE=";
        })
        { inherit (pkgs) system; };
    in
    {
      package = pkgsMesaClover.mesa-opencl.drivers;
      extraPackages = [
        pkgsMesaClover.mesa-opencl
      ];
    };
}

does not required mass-rebuild because of mesa compiled with different flags, and open a space to abandon the enableOpelCL flag at all.
Just to compile mesa always as though enableOpelCL=true, but copy the drivers which bring huge clang closure to a separate output.
And then just hardware.opengl.package = pkgs.mesa.opencl-drivers; to enable OpenCL (not the dot is after mesa, not mesa-opencl)
Will it work ?

@sbruder

sbruder commented Sep 2, 2021

Copy link
Copy Markdown
Contributor Author

I’m not sure if I understand you correctly, but according to #82729 (comment) using a separate output for OpenCL is not easily possible.

Concerning mass rebuilds, that will only affect nixpkgs once (when this PR is merged), but after that it only requires building one additional package (mesa_opencl), which will be done on Hydra, so there are no mass-rebuilds necessary for users. That is because mesa is not necessarily a dependency of all packages using it (though there are exceptions, but they will still use mesa and not mesa_opencl, which should be fine). Instead, the version present at /run/opengl-driver is loaded dynamically because otherwise it would be impossible to use e.g. the proprietary nvidia driver without every package using hardware acceleration depending on it.

That is the case independent of what method is used to make the OpenCL support optional (separate output or different package/guards).

@vobloeb

vobloeb commented Sep 2, 2021

Copy link
Copy Markdown
Contributor

But mesa_opencl contains all the files from mesa + open cl drivers?
And the files duplicated from mesa are not really needed

@vobloeb

vobloeb commented Sep 2, 2021 •

Copy link
Copy Markdown
Contributor

As "mass-rebuild way" I meant the way to enable OpenCL which I used:

(super: self: {
  mesa = super.mesa.override { enableOpenCL = true; }
})

Here is a single mesa, but mass-rebuild.
In the variant in #136402 (comment) there is no mass-rebuild, but many duplicated files in mesa and mesa_opencl, because system closure includes them both
It seems possible to end up with a solution without mass-rebuild and duplicated files (which I thought adding an output could be)

@sbruder

sbruder commented Sep 2, 2021

Copy link
Copy Markdown
Contributor Author

But mesa_opencl contains all the files from mesa + open cl drivers?

Yes it does. However, duplicated files are not that uncommon on a NixOS system and are introduced with every big mass-rebuild (e.g. on an openssl (patch) update, most packages depending on it do not change, but are still stored multiple times). To only store the files once there is nix-store --optimise and the auto-optimise-store Nix option which hard-link the files so it is not that much of a problem.

While it might be cleaner to move the OpenCL support it into a separate output, it seems to be not that easily possible. Though if you manage to get it working with an extra output, I’m more than happy to use that instead.

(super: self: {
  mesa = super.mesa.override { enableOpenCL = true; }
})

Overriding a widely-used package globally will always cause mass-rebuilds. For that reason, hardware.opengl.packages and hardware.opengl.extraPackages should be used in this case.

@vobloeb

vobloeb commented Sep 2, 2021

Copy link
Copy Markdown
Contributor

So I suggested that the additional output of mesa.opencl-driver could contain files which are present in mesa_opencl and are not present in mesa (because mesa_opencl is supposed to be used where mesa is already present).
Never mind, this is just a small optimization that can be done later.
It's more important to somehow repair OpenCL, which has been broken for months.

@sbruder

sbruder commented Sep 2, 2021

Copy link
Copy Markdown
Contributor Author

On further testing it turned out that installing OpenCL support into its own output is not that complicated. I force-pushed my attempt at doing it. Enabling OpenCL support now works by setting hardware.opengl.extraPackages = [ pkgs.mesa.opencl ];, the internal package option does not need to be modified.

@sbruder
sbruder force-pushed the mesa-opencl branch 2 times, most recently from 15fc39c to c3e7ab0 Compare September 5, 2021 19:44

@primeos primeos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I didn't have time to test it yet / investigate if this affects the other outputs and their closure sizes but the diff LGTM now and enabling it by default on Linux seems reasonable (due to the separate output).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note to self: We probably want to package this as llvmPackages*.libclc to keep it up-to-date, etc. (outside the scope of this PR but in case anyone has an opinion on that).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i suggested it before, but it seem not to be done yet

#89187 (comment)
#111065 (comment)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did you test if this works? I guess this gets linked as /run/opengl-driver/etc/OpenCL/vendors/mesa.icd but I'm not sure if that path is searched and I haven't tested it yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, this worked when I tested it. The NixOS manual mentions hardware.opengl.extraPackages (which get linked to /run/opengl-driver) as a way for installing OpenCL ICDs, so that seems to be the intended path for them.

@rnhmjoj rnhmjoj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I haven't tested the driver but the changes look reasonable to me.
The closure size for the other outputs didn't change:

  • before:

    • out: 33.2M
    • dev: 38.4M
    • drivers: 864.5M
  • after:

    • out: 33.2M
    • dev: 38.4M
    • drivers: 864.5M
    • opencl: 1.0G

so I think it's safe to be merged. Even if the drivers have some issue it's still an improvement over the current situation.

Co-authored-by: Sandro <[email protected]>
Co-authored-by: Simon Bruder <[email protected]>
@primeos

primeos commented Sep 18, 2021

Copy link
Copy Markdown
Member

@ofborg test sway

@primeos primeos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@sbruder I'm really sorry about the long delay. It was a very busy month for me and I lost track of this PR / didn't get around to merge it. I catched a last mistake and will merge this PR once CI (hopefully) passes. Thanks a lot for this PR and your fast responses!

Also huge thanks to everyone who reviewed this!
(Especially @rnhmjoj for verifying the closure sizes.)

@primeos primeos mentioned this pull request Sep 18, 2021
2 of 10 tasks
@ofborg ofborg Bot added the ofborg-internal-error Ofborg encountered an error label Sep 18, 2021
@primeos

primeos commented Sep 18, 2021

Copy link
Copy Markdown
Member

Let's ignore CI then ¯\_(ツ)_/¯ (previous results were fine; apart from timeouts).

I already ran the build and Sway test locally anyway:

$ nix-build -A nixosTests.sway
/nix/store/i6c1ybq9jlbha0h6dzzac83agm4vdcwq-vm-test-run-sway

@primeos
primeos merged commit aa596a9 into NixOS:staging Sep 18, 2021
@cole-h cole-h removed the ofborg-internal-error Ofborg encountered an error label Sep 20, 2021
@kurnevsky

kurnevsky commented Dec 21, 2021 •

Copy link
Copy Markdown
Member

I get the following error when trying to use mesa opencl:

<built-in>:1:10: fatal error: 'clc/clc.h' file not found

Tried to add libclc to the system environment and to opengl.extraPackages - neither worked. Any idea how to fix it?

@kotatsuyaki

kotatsuyaki commented Dec 25, 2021 •

Copy link
Copy Markdown
Contributor

I am getting the same error

<built-in>:1:10: fatal error: 'clc/clc.h' file not found

When trying to run a hello-world program for OpenCL. It seems that it tried to find that file, but the path is ill-formatted. See the repeated path /nix/store/d902ifqzwfld7mjw3vxgy08l87055whr-libclc-11.0.1//nix/store/d902ifqzwfld7mjw3vxgy08l87055whr-libclc-11.0.1/include in the strace below.

strace output
$ strace ./hello 2>&1 | rg clc
newfstatat(AT_FDCWD, "/nix/store/d902ifqzwfld7mjw3vxgy08l87055whr-libclc-11.0.1//nix/store/d902ifqzwfld7mjw3vxgy08l87055whr-libclc-11.0.1/include", 0x7ffe2a5cf060, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/nix/store/d902ifqzwfld7mjw3vxgy08l87055whr-libclc-11.0.1/share/clc/polaris10-amdgcn-mesa-mesa3d.bc", O_RDONLY|O_CLOEXEC) = 8
newfstatat(AT_FDCWD, "./clc", 0x7ffe2a5cde50, 0) = -1 ENOENT (No such file or directory)
write(2, "<built-in>:1:10: fatal error: 'c"..., 57<built-in>:1:10: fatal error: 'clc/clc.h' file not found

The path is hardcoded in libMesaOpenCL.so.1.

xxd output
$ xxd -d /run/opengl-driver/lib/libMesaOpenCL.so.1 | rg libclc-11 -C 8
31535136: 6c69 622f 6c69 622f 636c 616e 672f 3133  lib/lib/clang/13
31535152: 2e30 2e30 2f69 6e63 6c75 6465 0000 0000  .0.0/include....
31535168: 2f6e 6978 2f73 746f 7265 2f64 3930 3269  /nix/store/d902i
31535184: 6671 7a77 666c 6437 6d6a 7733 7678 6779  fqzwfld7mjw3vxgy
31535200: 3038 6c38 3730 3535 7768 722d 6c69 6263  08l87055whr-libc
31535216: 6c63 2d31 312e 302e 312f 2f6e 6978 2f73  lc-11.0.1//nix/s
31535232: 746f 7265 2f64 3930 3269 6671 7a77 666c  tore/d902ifqzwfl
31535248: 6437 6d6a 7733 7678 6779 3038 6c38 3730  d7mjw3vxgy08l870
31535264: 3535 7768 722d 6c69 6263 6c63 2d31 312e  55whr-libclc-11.
31535280: 302e 312f 696e 636c 7564 652f 0000 0000  0.1/include/....
31535296: 636c 5f63 6c61 6e67 5f73 746f 7261 6765  cl_clang_storage
31535312: 5f63 6c61 7373 5f73 7065 6369 6669 6572  _class_specifier
31535328: 7300 0000 0000 0000 2f6e 6978 2f73 746f  s......./nix/sto
31535344: 7265 2f64 3930 3269 6671 7a77 666c 6437  re/d902ifqzwfld7
31535360: 6d6a 7733 7678 6779 3038 6c38 3730 3535  mjw3vxgy08l87055
31535376: 7768 722d 6c69 6263 6c63 2d31 312e 302e  whr-libclc-11.0.
31535392: 312f 7368 6172 652f 636c 632f 006b 6572  1/share/clc/.ker
31535408: 6e65 6c5f 6172 675f 7479 7065 002e 6c6c  nel_arg_type..ll
31535424: 002e 6173 6d00 0000 0000 0000 0000 0000  ..asm...........
31535440: 5374 3139 5f53 705f 6d61 6b65 5f73 6861  St19_Sp_make_sha
31535456: 7265 645f 7461 6700 0000 0000 0000 0000  red_tag.........
31535472: 0000 0000 0000 0000 0000 0000 0000 0000  ................
31535488: 2a5a 4e31 325f 474c 4f42 414c 5f5f 4e5f  *ZN12_GLOBAL__N_
31535504: 3138 6f70 7469 6d69 7a65 4552 4e34 6c6c  18optimizeERN4ll

The wrong include directory is added by clover here, which is generated by this meson script. I'm not knowledgeable enough to fix it, but I think this is close to the root of the problem.

@shpinog

shpinog commented Jun 3, 2022

Copy link
Copy Markdown

Are there any plans to add this in the stable branch?

@vcunat

vcunat commented Jun 3, 2022

Copy link
Copy Markdown
Member

This PR is quite old, contained in both 21.11 and 22.05 releases.

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

Labels

8.has: package (new) This PR adds a new package 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 1001-2500 This PR causes many rebuilds on Darwin and should most likely target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 1001-2500 This PR causes many rebuilds on Linux and should target the staging branches.

Projects

None yet

Development

Successfully merging this pull request may close these issues.