LLVM 23.0.0git
DTLTODistributionDriver.cpp
Go to the documentation of this file.
1//===- DTLTODistributionDriver.cpp - DTLTO Distribution Driver ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// \file
10// This file implements the Integrated Distributed ThinLTO driver that prepares
11// the compilation job descriptions and invokes the external distributor.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/DTLTO/DTLTO.h"
16
17#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/JSON.h"
21#include "llvm/Support/Path.h"
25
26#include <string>
27
28using namespace llvm;
29
30// Generates a JSON file describing the backend compilations, for the
31// distributor.
32Error lto::DistributionDriver::emitJson() {
33 using json::Array;
34 std::error_code EC;
35 raw_fd_ostream OS(DistributorJsonFile, EC);
36 if (EC)
37 return createStringError(EC, "Error while creating Json file");
38
39 json::OStream JOS(OS);
40 JOS.object([&]() {
41 // Information common to all jobs.
42 JOS.attributeObject("common", [&]() {
43 JOS.attribute("linker_output", Params.LinkerOutputFile);
44
45 JOS.attributeArray("args", [&]() {
46 JOS.value(Params.RemoteCompiler);
47
48 // Forward any supplied prepend options.
49 if (!Params.RemoteCompilerPrependArgs.empty())
50 for (auto &A : Params.RemoteCompilerPrependArgs)
51 JOS.value(A);
52
53 JOS.value("-c");
54
55 JOS.value(std::string("--target=") + Params.TargetTriple.str());
56
57 for (const auto &A : Params.CodegenOptions)
58 JOS.value(A);
59 });
60
61 JOS.attribute("inputs", Array(Params.CommonInputs));
62 });
63
64 // Per-compilation-job information.
65 JOS.attributeArray("jobs", [&]() {
66 for (const auto &J : Jobs) {
67 assert(J.Task != 0);
68 if (J.Cached) {
69 continue;
70 }
71
73 SmallVector<StringRef, 1> Outputs;
74
75 JOS.object([&]() {
76 JOS.attributeArray("args", [&]() {
77 JOS.value(J.ModuleID);
78 Inputs.push_back(J.ModuleID);
79
80 JOS.value(
81 std::string("-fthinlto-index=" + J.SummaryIndexPath.str()));
82 Inputs.push_back(J.SummaryIndexPath);
83
84 JOS.value("-o");
85 JOS.value(J.NativeObjectPath);
86 Outputs.push_back(J.NativeObjectPath);
87 });
88
89 // Add the bitcode files from which imports will be made. These do
90 // not explicitly appear on the backend compilation command lines
91 // but are recorded in the summary index shards.
92 append_range(Inputs, J.ImportsFilesList);
93 JOS.attribute("inputs", Array(Inputs));
94
95 JOS.attribute("outputs", Array(Outputs));
96 });
97 }
98 });
99 });
100
101 return Error::success();
102}
103
104// Saves JSON file on a filesystem.
105Error lto::DistributionDriver::saveJson() {
106 DistributorJsonFile = sys::path::parent_path(Params.LinkerOutputFile);
107 TimeTraceScope TimeScope("Emit DTLTO JSON");
108 sys::path::append(DistributorJsonFile,
109 sys::path::stem(Params.LinkerOutputFile) + "." +
111 ".dist-file.json");
112 if (Error E = emitJson())
114 BCError + "failed to generate distributor JSON script: " +
115 DistributorJsonFile,
116 errorToErrorCode(std::move(E)));
117
118 // Add JSON file to the cleanup files list.
119 if (!SaveTemps)
120 AddToCleanup(DistributorJsonFile);
121 return Error::success();
122}
123
124// Invokes the distributor to compile uncached ThinLTO modules remotely.
126 if (Error E = saveJson())
127 return E;
128
129 TimeTraceScope TimeScope("Execute DTLTO distributor", Params.DistributorPath);
130 SmallVector<StringRef, 3> Args = {Params.DistributorPath};
131 append_range(Args, Params.DistributorArgs);
132 Args.push_back(DistributorJsonFile);
133 std::string ErrMsg;
134 if (sys::ExecuteAndWait(Args[0], Args,
135 /*Env=*/std::nullopt, /*Redirects=*/{},
136 /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg)) {
138 BCError + "distributor execution failed" +
139 (!ErrMsg.empty() ? ": " + ErrMsg + Twine(".") : Twine(".")),
141 }
142 return Error::success();
143}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file supports working with JSON data.
Provides a library for accessing information about this process and other processes on the operating ...
This file contains some templates that are useful if you are working with the STL at all.
This file contains some functions that are useful when dealing with strings.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Error operator()()
Invokes the distributor to compile bitcode modules remotely.
static LLVM_ABI Pid getProcessId()
Get the process's identifier.
LLVM_ABI StringRef stem(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get stem.
Definition Path.cpp:596
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:478
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:467
LLVM_ABI int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env=std::nullopt, ArrayRef< std::optional< StringRef > > Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, std::optional< ProcessStatistics > *ProcStat=nullptr, BitVector *AffinityMask=nullptr)
This function executes the program using the arguments provided.
Definition Program.cpp:32
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2207
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
LLVM_ABI std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition Error.cpp:113
std::string itostr(int64_t X)