Skip to content

Conversation

@ZuseZ4
Copy link
Member

@ZuseZ4 ZuseZ4 commented Nov 22, 2025

This automates steps 2+3 (the clang invocations) of the rust offload usage pipeline.
Needs a full rewrite before review, but I managed to skip everything except the clang-linker-invocation with this!

r? ghost

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-rustc-dev-guide Area: rustc-dev-guide S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 22, 2025
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 added the F-gpu_offload `#![feature(gpu_offload)]` label Nov 22, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 mentioned this pull request Nov 23, 2025
5 tasks
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 5992ea5 to 0588363 Compare November 23, 2025 09:01
@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Nov 23, 2025
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 0588363 to 99348e0 Compare November 23, 2025 09:05
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 99348e0 to b117ff4 Compare November 23, 2025 15:01
@ZuseZ4 ZuseZ4 changed the title Automate offload clangs automate offload, part 2 - clang calls Nov 23, 2025
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 mentioned this pull request Nov 24, 2025
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from b117ff4 to 3a9d453 Compare November 25, 2025 23:47
@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Nov 25, 2025
@ZuseZ4 ZuseZ4 force-pushed the automate-offload-clangs branch from 89a17be to ebbe811 Compare November 26, 2025 18:35
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
fmt: checked 6571 files
tidy check
tidy [rustdoc_json (src)]: `rustdoc-json-types` modified, checking format version
tidy: Skipping binary file check, read-only filesystem
tidy [style (compiler)]: /checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1543: line longer than 100 chars
tidy [style (compiler)]: /checkout/compiler/rustc_codegen_llvm/src/back/write.rs:778: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
tidy [style (compiler)]: /checkout/compiler/rustc_codegen_llvm/src/back/write.rs:780: `dbg!` macro is intended as a debugging tool. It should not be in version control.
tidy [style (compiler)]: /checkout/compiler/rustc_codegen_llvm/src/back/write.rs:785: `dbg!` macro is intended as a debugging tool. It should not be in version control.
tidy [style (compiler)]: /checkout/compiler/rustc_codegen_llvm/src/back/write.rs:786: `dbg!` macro is intended as a debugging tool. It should not be in version control.
tidy [style (compiler)]: /checkout/compiler/rustc_codegen_llvm/src/back/write.rs:801: `dbg!` macro is intended as a debugging tool. It should not be in version control.
tidy [style (compiler)]: FAIL
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'venv'
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'virtualenv'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (25.3)
linting python files
All checks passed!
checking python file formatting
28 files already formatted
checking C++ file formatting
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:6:28: error: code should be clang-formatted [-Wclang-format-violations]
#include "llvm-c/Support.h"          // LLVMMemoryBufferRef, LLVMGetBufferStart, ...
                           ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:47:1: error: code should be clang-formatted [-Wclang-format-violations]
#include "llvm/Bitcode/BitcodeReader.h"
^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:167:2: error: code should be clang-formatted [-Wclang-format-violations]
}
 ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1480:34: error: code should be clang-formatted [-Wclang-format-violations]
extern "C" LLVMRustModuleBuffer *LLVMRustModuleBufferFromFile(const char *path) {
                                 ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1543:71: error: code should be clang-formatted [-Wclang-format-violations]
extern "C" bool LLVMRustBundleImages(const LLVMRustModuleBuffer *MBuf, TargetMachine &TM, const char *HostOutPath) {
                                                                      ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1543:90: error: code should be clang-formatted [-Wclang-format-violations]
extern "C" bool LLVMRustBundleImages(const LLVMRustModuleBuffer *MBuf, TargetMachine &TM, const char *HostOutPath) {
                                                                                         ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1555:51: error: code should be clang-formatted [-Wclang-format-violations]
  ImageBinary.TheOffloadKind = object::OFK_OpenMP;
                                                  ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1617:1: error: code should be clang-formatted [-Wclang-format-violations]
#include "llvm/Support/TargetSelect.h"
^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1621:3: error: code should be clang-formatted [-Wclang-format-violations]
---
  ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1623:1: error: code should be clang-formatted [-Wclang-format-violations]
#include "llvm/MC/TargetRegistry.h"
^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1628:42: error: code should be clang-formatted [-Wclang-format-violations]
Error emitHostObjectWithTM(Module &HostM,
                                         ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1632:5: error: code should be clang-formatted [-Wclang-format-violations]
  //HostM.setDataLayout(TM.createDataLayout());
    ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1633:5: error: code should be clang-formatted [-Wclang-format-violations]
  //HostM.setTargetTriple(TM.getTargetTriple().str());
    ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1641:46: error: code should be clang-formatted [-Wclang-format-violations]
  if (TM.addPassesToEmitFile(PM, OS, nullptr, llvm::CodeGenFileType::ObjectFile))
                                             ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1683:18: error: code should be clang-formatted [-Wclang-format-violations]
  std::string CPU       = "x86-64"; // OK for X86
                 ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1696:58: error: code should be clang-formatted [-Wclang-format-violations]
      T->createTargetMachine(TripleStr, CPU, /*Features*/"", Opts, RM));
                                                         ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1702:71: error: code should be clang-formatted [-Wclang-format-violations]
// lib.bc (from first rustc) + host.out (from LLVMRustBundleImages) => host.offload.o
                                                                      ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1705:66: error: code should be clang-formatted [-Wclang-format-violations]
                                        const char *OutObjPath) {
                                                                 ^
/checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp:1706:20: error: code should be clang-formatted [-Wclang-format-violations]
   LLVMContext Ctx;
                   ^

clang-format linting failed! Printing diff suggestions:
--- /checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp (actual)
+++ /checkout/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp (formatted)
@@ -2,9 +2,9 @@
 
 #include "llvm-c/Analysis.h"
 #include "llvm-c/Core.h"
 #include "llvm-c/DebugInfo.h"
-#include "llvm-c/Support.h"          // LLVMMemoryBufferRef, LLVMGetBufferStart, ...
+#include "llvm-c/Support.h" // LLVMMemoryBufferRef, LLVMGetBufferStart, ...
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringRef.h"
@@ -43,23 +43,23 @@
 // Some of the functions below rely on LLVM modules that may not always be
 // available. As such, we only try to build it in the first place, if
 // llvm.offload is enabled.
 #ifdef OFFLOAD
---
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
 #endif
 
 // for raw `write` in the bad-alloc handler
 #ifdef _MSC_VER
@@ -164,9 +164,8 @@
 extern "C" void LLVMRustPrintStatistics(RustStringRef OutBuf) {
   auto OS = RawRustStringOstream(OutBuf);
   llvm::PrintStatistics(OS);
 }
-
 
 extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name,
                                               size_t NameLen) {
   return wrap(unwrap(M)->getNamedValue(StringRef(Name, NameLen)));
@@ -1476,9 +1475,10 @@
   }
   return Ret.release();
 }
 
-extern "C" LLVMRustModuleBuffer *LLVMRustModuleBufferFromFile(const char *path) {
+extern "C" LLVMRustModuleBuffer *
+LLVMRustModuleBufferFromFile(const char *path) {
   auto Ret = std::make_unique<LLVMRustModuleBuffer>();
 
   LLVMMemoryBufferRef Buf = nullptr;
   char *Message = nullptr;
@@ -1539,9 +1539,11 @@
 // clang-offload-packager -o host.out
 //  --image=file=device.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp
 // The input module is the rust code compiled for a gpu target like amdgpu.
 // Based on clang/tools/clang-offload-packager/ClangOffloadPackager.cpp
-extern "C" bool LLVMRustBundleImages(const LLVMRustModuleBuffer *MBuf, TargetMachine &TM, const char *HostOutPath) {
+extern "C" bool LLVMRustBundleImages(const LLVMRustModuleBuffer *MBuf,
+                                     TargetMachine &TM,
+                                     const char *HostOutPath) {
 
   // Recreate a MemoryBuffer from the bytes in LLVMRustModuleBuffer
   StringRef Storage(MBuf->data.data(), MBuf->data.size());
   auto MB = MemoryBuffer::getMemBufferCopy(Storage, "device.bc");
@@ -1553,9 +1555,8 @@
   ImageBinary.TheImageKind = object::IMG_Bitcode;
   ImageBinary.Image = std::move(MB);
   ImageBinary.TheOffloadKind = object::OFK_OpenMP;
 
-
   std::string TripleStr = TM.getTargetTriple().str();
   llvm::StringRef CPURef = TM.getTargetCPU();
   ImageBinary.StringData["triple"] = TripleStr;
   ImageBinary.StringData["arch"] = CPURef;
@@ -1613,33 +1614,33 @@
   embedBufferInModule(HostM, Buf);
   return Error::success();
 }
 
+#include "llvm/IR/LegacyPassManager.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
-#include "llvm/IR/LegacyPassManager.h"
-//#include "llvm/Support/Host.h"
-//#include "llvm/Support/TargetRegistry.h"
+// #include "llvm/Support/Host.h"
+// #include "llvm/Support/TargetRegistry.h"
 #include "llvm/MC/TargetRegistry.h"
-#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/CodeGen.h" // <-- new
 #include "llvm/Support/FileSystem.h"
-#include "llvm/Support/CodeGen.h"        // <-- new
+#include "llvm/Support/raw_ostream.h"
 
-Error emitHostObjectWithTM(Module &HostM,
-                           TargetMachine &TM,
+Error emitHostObjectWithTM(Module &HostM, TargetMachine &TM,
                            StringRef OutObjPath) {
   // Make sure module matches the TM
-  //HostM.setDataLayout(TM.createDataLayout());
-  //HostM.setTargetTriple(TM.getTargetTriple().str());
+  // HostM.setDataLayout(TM.createDataLayout());
+  // HostM.setTargetTriple(TM.getTargetTriple().str());
 
   legacy::PassManager PM;
   std::error_code EC;
   raw_fd_ostream OS(OutObjPath, EC, sys::fs::OF_None);
   if (EC)
     return errorCodeToError(EC);
 
-  if (TM.addPassesToEmitFile(PM, OS, nullptr, llvm::CodeGenFileType::ObjectFile))
+  if (TM.addPassesToEmitFile(PM, OS, nullptr,
+                             llvm::CodeGenFileType::ObjectFile))
     return createStringError(inconvertibleErrorCode(),
                              "TargetMachine can't emit a file of this type");
 
   PM.run(HostM);
@@ -1679,9 +1680,9 @@
   }
 
   // Hardcoded host triple + CPU (adapt if your CI/host differs)
   std::string TripleStr = "x86_64-unknown-linux-gnu";
-  std::string CPU       = "x86-64"; // OK for X86
+  std::string CPU = "x86-64"; // OK for X86
 
   std::string Err;
   const Target *T = TargetRegistry::lookupTarget(TripleStr, Err);
   if (!T) {
@@ -1692,20 +1693,21 @@
   TargetOptions Opts;
   auto RM = std::optional<Reloc::Model>(Reloc::PIC_);
 
   std::unique_ptr<TargetMachine> TM(
-      T->createTargetMachine(TripleStr, CPU, /*Features*/"", Opts, RM));
+      T->createTargetMachine(TripleStr, CPU, /*Features*/ "", Opts, RM));
 
   return TM;
 }
 
 // Top-level entry: host finalize in second rustc invocation
-// lib.bc (from first rustc) + host.out (from LLVMRustBundleImages) => host.offload.o
+// lib.bc (from first rustc) + host.out (from LLVMRustBundleImages) =>
+// host.offload.o
 extern "C" bool LLVMRustFinalizeOffload(const LLVMRustModuleBuffer *MBuf,
                                         const char *HostOutPath,
                                         const char *OutObjPath) {
-   LLVMContext Ctx;
-   llvm::errs() << "Before A\n";
+  LLVMContext Ctx;
+  llvm::errs() << "Before A\n";
 
   // new:
   StringRef Storage(MBuf->data.data(), MBuf->data.size());
   MemoryBufferRef Ref(Storage, "host.bc");

rerun tidy with `--extra-checks=cpp:fmt --bless` to reformat C++ code
tidy [extra_checks]: checks with external tool 'clang-format' failed
tidy [extra_checks]: FAIL
tidy: The following checks failed: extra_checks, style (compiler)
Command `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools-bin/rust-tidy /checkout /checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo /checkout/obj/build 4 /node/bin/yarn --extra-checks=py,cpp,js,spellcheck` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:1599:23
Executed at: src/bootstrap/src/core/build_steps/test.rs:1285:29

Command has failed. Rerun with -v to see more details.
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`
Build completed unsuccessfully in 0:01:19
  local time: Wed Nov 26 23:49:45 UTC 2025
  network time: Wed, 26 Nov 2025 23:49:45 GMT
##[error]Process completed with exit code 1.

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-rustc-dev-guide Area: rustc-dev-guide F-gpu_offload `#![feature(gpu_offload)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants