swiftlang / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. This fork is used to manage Swift’s stable releases of Clang as well as support the Swift project.
https://llvm.org
Other
1.11k stars 328 forks source link

Swift MCCAS object files are 0 bytes on cache miss #9164

Closed rastogishubham closed 2 weeks ago

rastogishubham commented 3 weeks ago

When clang caching and MCCAS are enabled, on a cache miss, the object file is not written to in MachOCASWriter, instead the replay code is rerun and the object file is serialized and written out. However, in swift, the replay code isn't run on a cache miss at all, and it expects the object writer to write to the object file.

This patch makes it so that on a cache miss, the MachOCASWriter always writes to the raw_ostream buffer for the object file and doesn't serialize the object file in the replay code.

rdar://134425492 ([MCCAS] Fix bug with swift caching where object files are 0 bytes in size)

rastogishubham commented 3 weeks ago

@swift-ci please test

rastogishubham commented 3 weeks ago

@swift-ci please test llvm

rastogishubham commented 2 weeks ago

@cachemeifyoucan I added a test for the -fcasid-output option.

The code is now

if (JustComputedResult && !WriteOutputAsCASID)

In file-based caching, if -fcasid-output is not used and if there is a cache miss, JustComputedResult will be true and WriteOutputAsCASID will be false, so the code will just exit this function and not replay anything.

In file-based caching, if -fcasid-output is not used and if there is a cache hit, JustComputedResult will be false and WriteOutputAsCASID will be false, so the code will replay the object file.

In MCCAS, if -fcasid-output is not used and if there is a cache miss, JustComputedResult will be true and WriteOutputAsCASID will be false, so the code will just exit this function and not replay anything.

In MCCAS, if -fcasid-output is not used and if there is a cache hit, JustComputedResult will be false and WriteOutputAsCASID will be false, so the code will replay the object file.

if -fcasid-output is used, then WriteOutputAsCASID is true, so we always go into the replay code whether we used MCCAS or file based caching, whether there is a cache hit or miss. However, the raw_ostream is filtered out.

CompileJobCache.cpp:449

IntrusiveRefCntPtr<llvm::vfs::OutputBackend> OnDiskOutputs =
      llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>();
  if (WriteOutputAsCASID) {
    OnDiskOutputs = llvm::vfs::makeFilteringOutputBackend(
        OnDiskOutputs,
        [this](StringRef ResolvedPath, std::optional<llvm::vfs::OutputConfig>) {
          return ResolvedPath != this->OutputFile;
        });
  }

So we always write the casid to the object file stream when -fcasid-output is used.

Now this patch also ensures that whether swift caching or clang caching is used with MCCAS. The MachOCASWriter always writes to the raw_ostream it gets, which also solves the bug with 0 byte object files in swift MCCAS.

rastogishubham commented 2 weeks ago

@swift-ci please test

rastogishubham commented 2 weeks ago

@swift-ci please test

rastogishubham commented 2 weeks ago

@swift-ci please test macOS