Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3a92171
[DTLTO][ELF][COFF][MachO] Add archive support for DTLTO.
kbelochapka Aug 25, 2025
127471f
Merge branch 'llvm:main' into kbelochapka/main/dtlto-archives-support
kbelochapka Sep 5, 2025
9afd973
[DTLTO][ELF][COFF][MachO] Add archive support for DTLTO - Fixed compi…
kbelochapka Sep 6, 2025
ee4ec95
[DTLTO][ELF][COFF][MachO] Add archive support for DTLTO - Fixed forma…
kbelochapka Sep 6, 2025
09c2b99
Merge branch 'main' into kbelochapka/main/dtlto-archives-support
kbelochapka Sep 6, 2025
4171a33
[DTLTO][ELF][COFF][MachO] Add archive support for DTLTO - Addressed r…
kbelochapka Sep 8, 2025
fa7376c
[DTLTO][ELF][COFF][MachO] Add archive support for DTLTO - Compilation…
kbelochapka Sep 8, 2025
ccc8ae3
[DTLTO][ELF][COFF] Archive support for DTLTO - Removed thin archives …
kbelochapka Sep 9, 2025
70b51bd
[DTLTO][ELF][COFF] Archive support for DTLTO - Removed archives suppo…
kbelochapka Sep 9, 2025
d61b606
[DTLTO][ELF][COFF] Archive support for DTLTO - Fixed crash in lld/COF…
kbelochapka Sep 9, 2025
fb43b8f
[DTLTO][ELF][COFF] Archive support for DTLTO - Fixed formatting in ll…
kbelochapka Sep 9, 2025
c127f0a
Merge branch 'main' into kbelochapka/main/dtlto-archives-support
kbelochapka Oct 3, 2025
7ea9a04
[DTLTO][Archives] Fixed formatting issues after the merge.
kbelochapka Oct 6, 2025
1582a50
[DTLTO][Archives] - Addressed review comment from Teresa - Removed 'i…
kbelochapka Nov 10, 2025
2e9421c
[DTLTO][Archives] - Addressed review comment from Teresa - Replaced '…
kbelochapka Nov 10, 2025
cf91406
[DTLTO][Archives] - Addressed review comments from Teresa - Made add…
kbelochapka Dec 4, 2025
85d2344
[DTLTO][Archives] - Addressed review comments from Teresa - Fixed mem…
kbelochapka Dec 5, 2025
4136170
[DTLTO][Archives] - Simplification - Made TempFilesRemover class a me…
kbelochapka Dec 5, 2025
f89dc85
[DTLTO][Archives] - Addressed review comments from Teresa - Introduce…
kbelochapka Dec 7, 2025
9571a3d
[DTLTO][Archives] - Fixed code formatting.
kbelochapka Dec 7, 2025
e7aebd4
[DTLTO][Archives] - Fixed code formatting.
kbelochapka Dec 8, 2025
ea80ce2
[DTLTO][Archives] - Addressed review comments from Teresa - Removed D…
kbelochapka Dec 10, 2025
ce11095
[DTLTO][Archives] - Addressed review comments from Teresa - Made some…
kbelochapka Dec 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions cross-project-tests/dtlto/archive.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
REQUIRES: x86-registered-target,ld.lld,llvm-ar

# Test that a DTLTO link succeeds and outputs the expected set of files
# correctly when archives are present.

RUN: rm -rf %t && split-file %s %t && cd %t
# Compile sources into bitcode. -O2 is required for cross-module importing.
RUN: %clang -O2 --target=x86_64-linux-gnu -flto=thin -c foo.c boo.c moo.c loo.c voo.c main.c

RUN: llvm-ar rcs archive.a foo.o boo.o moo.o
RUN: llvm-ar rcsT archive.thin.a loo.o voo.o

# Build with DTLTO.
RUN: %clang -O2 --target=x86_64-linux-gnu -Werror -flto=thin \
RUN: -fuse-ld=lld -nostdlib -e main \
RUN: main.o archive.a archive.thin.a -o main.elf \
RUN: -Wl,--thinlto-distributor=%python \
RUN: -Wl,--thinlto-distributor-arg=%llvm_src_root/utils/dtlto/local.py \
RUN: -Wl,--thinlto-remote-compiler=%clang \
RUN: -Wl,--save-temps

# Check that the required output files have been created.
RUN: ls | FileCheck %s --check-prefix=OUTPUTS

# JSON jobs description.
OUTPUTS-DAG: {{^}}main.[[PID:[0-9]+]].dist-file.json

# Main source.
OUTPUTS-DAG: {{^}}main.{{[0-9]+}}.[[PID]].native.o{{$}}
OUTPUTS-DAG: {{^}}main.{{[0-9]+}}.[[PID]].native.o.thinlto.bc{{$}}

# Regular archive members.
# Filename composition: <archive>(<member> at <offset>).<task>.<pid>.<task>.<pid>.native.o[.thinlto.bc].
OUTPUTS-DAG: {{^}}archive.a(boo.o at {{[0-9]+}}).2.[[HEXPID:[a-fA-F0-9]+]].2.[[PID]].native.o{{$}}
OUTPUTS-DAG: {{^}}archive.a(boo.o at {{[0-9]+}}).2.[[HEXPID]].2.[[PID]].native.o.thinlto.bc{{$}}

OUTPUTS-DAG: {{^}}archive.a(foo.o at {{[0-9]+}}).3.[[HEXPID]].3.[[PID]].native.o{{$}}
OUTPUTS-DAG: {{^}}archive.a(foo.o at {{[0-9]+}}).3.[[HEXPID]].3.[[PID]].native.o.thinlto.bc{{$}}

OUTPUTS-DAG: {{^}}archive.a(moo.o at {{[0-9]+}}).4.[[HEXPID]].4.[[PID]].native.o{{$}}
OUTPUTS-DAG: {{^}}archive.a(moo.o at {{[0-9]+}}).4.[[HEXPID]].4.[[PID]].native.o.thinlto.bc{{$}}

# Thin archive members.
OUTPUTS-DAG: {{^}}voo.{{[0-9]+}}.[[PID]].native.o{{$}}
OUTPUTS-DAG: {{^}}voo.{{[0-9]+}}.[[PID]].native.o.thinlto.bc{{$}}

OUTPUTS-DAG: {{^}}loo.{{[0-9]+}}.[[PID]].native.o{{$}}
OUTPUTS-DAG: {{^}}loo.{{[0-9]+}}.[[PID]].native.o.thinlto.bc{{$}}

# Executable file.
OUTPUTS-DAG: {{^}}main.elf{{$}}

#--- foo.c
volatile int foo_int;
__attribute__((retain)) int foo(int x) { return x + foo_int; }

#--- boo.c
extern int foo(int x);
__attribute__((retain)) int boo(int x) { return foo(x); }

#--- moo.c
__attribute__((retain)) int moo() { return 3; }

#--- loo.c
extern int moo(int x);
__attribute__((retain)) int loo(int x) { return moo(x); }

#--- voo.c
extern int foo(int x);
extern int loo(int x);
__attribute__((retain)) int voo(int x) { return foo(x) + loo(x + 1) + 7; }

#--- main.c
extern int boo(int x);
extern int moo();
extern int voo(int x);
__attribute__((retain)) int main(int argc, char** argv) {
return boo(argc) + moo() + voo(argc + 3);
}

35 changes: 35 additions & 0 deletions cross-project-tests/dtlto/archives-mixed-lto-modes-test.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
REQUIRES: x86-registered-target,ld.lld,llvm-ar

# Test that DTLTO works with a mixture of FullLTO and ThinLTO bitcode archive members
# where there is more than one LTO partition.

RUN: rm -rf %t && split-file %s %t && cd %t

RUN: %clang --target=x86_64-linux-gnu -flto -c one.c two.c
RUN: %clang --target=x86_64-linux-gnu -flto=thin -c three.c

RUN: llvm-ar rc archive.a one.o two.o three.o

# Build with DTLTO.
RUN: %clang --target=x86_64-linux-gnu -Werror -flto -fuse-ld=lld -nostdlib \
RUN: -Wl,--whole-archive archive.a \
RUN: -Wl,--thinlto-distributor=%python \
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know in some earlier instances tests that where referring to %python needed to be quoted when running on windows. I still see this in some of the tests but not all of them - was there a rule when this was needed @bd1976bris ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I know in some earlier instances tests that where referring to %python needed to be quoted when running on windows. I still see this in some of the tests but not all of them - was there a rule when this was needed @bd1976bris ?

Good thought! The quoting was only needed in the response-file case when %python expanded to a path containing spaces. In normal runs, lit expands %python and takes care of Windows quoting, so extra quotes aren’t required.

RUN: -Wl,--thinlto-distributor-arg=%llvm_src_root/utils/dtlto/local.py \
RUN: -Wl,--thinlto-remote-compiler=%clang \
RUN: -Wl,--save-temps,--lto-partitions=2

# Show that the FullLTO modules have been prepared for distribution, this is
# not optimal but has no functional impact.
RUN: FileCheck %s --input-file=a.out.resolution.txt
CHECK: archive.a(one.o at {{.*}}).1.[[PID:[a-zA-Z0-9_]+]].o
CHECK: archive.a(two.o at {{.*}}).2.[[PID]].o
CHECK: archive.a(three.o at {{.*}}).3.[[PID]].o

#--- one.c
__attribute__((retain)) void one() {}

#--- two.c
__attribute__((retain)) void two() {}

#--- three.c
__attribute__((retain)) void three() {}
55 changes: 55 additions & 0 deletions cross-project-tests/dtlto/archives-same-module-id.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
REQUIRES: x86-registered-target,ld.lld,llvm-ar

# Test that a DTLTO link succeeds when there are two archive member files with
# the same filename path component.

# Split this file into several sources.
RUN: rm -rf %t && split-file %s %t && cd %t

RUN: %clang -O2 --target=x86_64-linux-gnu -flto=thin -c start.c

# Create first archive.
RUN: mkdir archive1 && cd archive1
RUN: %clang -O2 --target=x86_64-linux-gnu -flto=thin -c ../t1.c ../t3.c
RUN: llvm-ar rc archive.a t3.o t1.o
RUN: cd ..

# Create second archive.
RUN: mkdir archive2 && cd archive2
RUN: %clang -O2 --target=x86_64-linux-gnu -flto=thin -c ../t1.c ../t3.c
RUN: llvm-ar rc archive.a t3.o t1.o
RUN: cd ..

RUN: %clang -O2 --target=x86_64-linux-gnu -Werror -flto=thin -fuse-ld=lld \
RUN: -nostdlib -Wl,--undefined=t1,--undefined=t3 \
RUN: start.o archive1/archive.a archive2/archive.a -o main.elf \
RUN: -Wl,--save-temps \
RUN: -Wl,--thinlto-distributor=%python \
RUN: -Wl,--thinlto-distributor-arg=%llvm_src_root/utils/dtlto/local.py \
RUN: -Wl,--thinlto-remote-compiler=%clang

# Check that the required output files have been created.
RUN: ls | FileCheck %s --check-prefix=OUTPUTS

# JSON jobs description.
OUTPUTS-DAG: {{^}}main.[[PID:[0-9]+]].dist-file.json

# Sources.
OUTPUTS-DAG: {{^}}start.{{[0-9]+}}.[[PID]].native.o{{$}}
OUTPUTS-DAG: {{^}}start.{{[0-9]+}}.[[PID]].native.o.thinlto.bc{{$}}

# Archive members.
# Filename composition: <archive>(<member> at <offset>).<task>.<pid>.<task>.<pid>.native.o[.thinlto.bc].
OUTPUTS-DAG: {{^}}archive.a(t3.o at {{[0-9]+}}).2.[[HEXPID:[a-fA-F0-9]+]].2.[[PID]].native.o{{$}}
OUTPUTS-DAG: {{^}}archive.a(t3.o at {{[0-9]+}}).2.[[HEXPID]].2.[[PID]].native.o.thinlto.bc{{$}}
OUTPUTS-DAG: {{^}}archive.a(t1.o at {{[0-9]+}}).3.[[HEXPID]].3.[[PID]].native.o{{$}}
OUTPUTS-DAG: {{^}}archive.a(t1.o at {{[0-9]+}}).3.[[HEXPID]].3.[[PID]].native.o.thinlto.bc{{$}}

#--- t1.c
__attribute__((retain)) void t1() { }

#--- start.c
__attribute__((retain)) void _start() { }

#--- t3.c
__attribute__((retain)) void t3() { }
1 change: 1 addition & 0 deletions lld/COFF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ BitcodeFile *BitcodeFile::create(COFFLinkerContext &ctx, MemoryBufferRef mb,
utostr(offsetInArchive)));

std::unique_ptr<lto::InputFile> obj = check(lto::InputFile::create(mbref));
obj->setArchivePathAndName(archiveName, mb.getBufferIdentifier());
return make<BitcodeFile>(ctx.getSymtab(getMachineType(obj.get())), mb, obj,
lazy);
}
Expand Down
3 changes: 3 additions & 0 deletions lld/COFF/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) {

ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
ctx.config.ltoPartitions);

if (!ctx.config.dtltoDistributor.empty())
ltoObj->Dtlto = true;
}

BitcodeCompiler::~BitcodeCompiler() = default;
Expand Down
59 changes: 11 additions & 48 deletions lld/ELF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "llvm/ADT/CachedHashString.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/LTO/LTO.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Support/AArch64AttributeParser.h"
#include "llvm/Support/ARMAttributeParser.h"
Expand Down Expand Up @@ -1812,39 +1811,6 @@ static uint8_t getOsAbi(const Triple &t) {
}
}

// For DTLTO, bitcode member names must be valid paths to files on disk.
// For thin archives, resolve `memberPath` relative to the archive's location.
// Returns true if adjusted; false otherwise. Non-thin archives are unsupported.
static bool dtltoAdjustMemberPathIfThinArchive(Ctx &ctx, StringRef archivePath,
std::string &memberPath) {
assert(!archivePath.empty());

if (ctx.arg.dtltoDistributor.empty())
return false;

// Read the archive header to determine if it's a thin archive.
auto bufferOrErr =
MemoryBuffer::getFileSlice(archivePath, sizeof(ThinArchiveMagic) - 1, 0);
if (std::error_code ec = bufferOrErr.getError()) {
ErrAlways(ctx) << "cannot open " << archivePath << ": " << ec.message();
return false;
}

if (!bufferOrErr->get()->getBuffer().starts_with(ThinArchiveMagic))
return false;

SmallString<128> resolvedPath;
if (path::is_relative(memberPath)) {
resolvedPath = path::parent_path(archivePath);
path::append(resolvedPath, memberPath);
} else
resolvedPath = memberPath;

path::remove_dots(resolvedPath, /*remove_dot_dot=*/true);
memberPath = resolvedPath.str();
return true;
}

BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
uint64_t offsetInArchive, bool lazy)
: InputFile(ctx, BitcodeKind, mb) {
Expand All @@ -1855,25 +1821,22 @@ BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
if (ctx.arg.thinLTOIndexOnly)
path = replaceThinLTOSuffix(ctx, mb.getBufferIdentifier());

// ThinLTO assumes that all MemoryBufferRefs given to it have a unique
// name. If two archives define two members with the same name, this
// causes a collision which result in only one of the objects being taken
// into consideration at LTO time (which very likely causes undefined
// symbols later in the link stage). So we append file offset to make
// filename unique.
StringSaver &ss = ctx.saver;
StringRef name;
if (archiveName.empty() ||
dtltoAdjustMemberPathIfThinArchive(ctx, archiveName, path)) {
name = ss.save(path);
} else {
// ThinLTO assumes that all MemoryBufferRefs given to it have a unique
// name. If two archives define two members with the same name, this
// causes a collision which result in only one of the objects being taken
// into consideration at LTO time (which very likely causes undefined
// symbols later in the link stage). So we append file offset to make
// filename unique.
name = ss.save(archiveName + "(" + path::filename(path) + " at " +
utostr(offsetInArchive) + ")");
}
StringRef name = archiveName.empty()
? ss.save(path)
: ss.save(archiveName + "(" + path::filename(path) +
" at " + utostr(offsetInArchive) + ")");

MemoryBufferRef mbref(mb.getBuffer(), name);

obj = CHECK2(lto::InputFile::create(mbref), this);
obj->setArchivePathAndName(archiveName, mb.getBufferIdentifier());

Triple t(obj->getTargetTriple());
ekind = getBitcodeELFKind(t);
Expand Down
3 changes: 3 additions & 0 deletions lld/ELF/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
ctx.arg.ltoPartitions,
ltoModes[ctx.arg.ltoKind]);

if (!ctx.arg.dtltoDistributor.empty())
ltoObj->Dtlto = true;

// Initialize usedStartStop.
if (ctx.bitcodeFiles.empty())
return;
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/Bitcode/BitcodeReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ struct ParserCallbacks {

StringRef getModuleIdentifier() const { return ModuleIdentifier; }

// Assign a new module identifier to this bitcode module.
void setModuleIdentifier(llvm::StringRef ModuleId) {
ModuleIdentifier = ModuleId;
}

/// Read the bitcode module and prepare for lazy deserialization of function
/// bodies. If ShouldLazyLoadMetadata is true, lazily load metadata as well.
/// If IsImporting is true, this module is being parsed for ThinLTO
Expand Down
23 changes: 23 additions & 0 deletions llvm/include/llvm/DTLTO/DTLTO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===- DTLTO.h - Distributed ThinLTO functions and classes ----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//

#ifndef LLVM_DTLTO_H
#define LLVM_DTLTO_H

#include "llvm/LTO/LTO.h"
#include "llvm/Support/MemoryBuffer.h"

namespace dtlto {

llvm::Expected<llvm::lto::InputFile *>
addInput(llvm::lto::LTO *LtoObj, std::unique_ptr<llvm::lto::InputFile> Input);

llvm::Error process(llvm::lto::LTO &LtoObj);
} // namespace dtlto

#endif // LLVM_DTLTO_H
Loading