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

Switch to XCode's Metal compiler? #7

Open
maleadt opened this issue Apr 7, 2022 · 1 comment
Open

Switch to XCode's Metal compiler? #7

maleadt opened this issue Apr 7, 2022 · 1 comment
Labels
kernels Things about kernels and how they are compiled. speculative Note sure if we want this.

Comments

@maleadt
Copy link
Member

maleadt commented Apr 7, 2022

We're currently using a custom metallib-as to downgrade LLVM IR and generate a metallib containing LLVM 5 bitcode, however, it's possible to just re-use XCode's Clang-based metal compiler for this:

$ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/metal/macos/bin/metal kernel.(ll|bc)
warning: overriding the module target triple with air64-apple-macosx12.0.0 [-Woverride-module]
1 warning generated

The problem is that we still need to downgrade the LLVM IR (this is more obvious when using textual bitcode inputs, in which case metal will complain about unsupported deferencerable arguments, spFlags MDNode contents, etc), but a tool for that could be more valuable, e.g., to support NVIDIA's NVVM compiler.

Another disadvantage is that we'd then require users to have Xcode installed, and the full thing at that (the metal compiler isn't part of the command line xcode tools). That's a multi-gigabyte download and installation, since presumably Apple doesn't allow us to redistribute those parts of the SDK.

@maleadt maleadt added the speculative Note sure if we want this. label Apr 7, 2022
@maleadt
Copy link
Member Author

maleadt commented Apr 7, 2022

diff --git a/src/metal.jl b/src/metal.jl
index b314899..922e9be 100644
--- a/src/metal.jl
+++ b/src/metal.jl
@@ -708,18 +708,34 @@ end
                      If you think this is a bug, please file an issue and attach $(input).""")
         end
     end
+    cp(translated, "/tmp/ours.metallib"; force=true)
+
+    input_alternative = tempname(cleanup=false) * ".ll"
+    translated_alternative = tempname(cleanup=false) * ".reference.metallib"
+    strip_debuginfo!(mod)   # quick hack to support some more IR without downgrading
+    write(input_alternative, string(mod))
+    let
+        proc = run(ignorestatus(`/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/metal/macos/bin/metal -o $translated_alternative $input_alternative`))
+        if !success(proc)
+            error("""Failed to translate LLVM code to MetalLib.
+                     If you think this is a bug, please file an issue and attach $(input_alternative).""")
+        end
+    end
+    cp(translated_alternative, "/tmp/theirs.metallib"; force=true)

     output = if format == LLVM.API.LLVMObjectFile
-        read(translated)
+        read(translated_alternative)
     else
         # disassemble
         Metal_LLVM_Tools_jll.metallib_dis() do disassembler
-            read(`$disassembler -o - $translated`, String)
+            read(`$disassembler -o - $translated_alternative`, String)
         end
     end

     rm(input)
     rm(translated)
+    rm(input_alternative)
+    rm(translated_alternative)

     return output
 end

@maleadt maleadt added the kernels Things about kernels and how they are compiled. label Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernels Things about kernels and how they are compiled. speculative Note sure if we want this.
Projects
None yet
Development

No branches or pull requests

1 participant