Files
scriptc/native/llvm-codegen/src/main.cpp
T
Chris Tate 3ea231d70d Optimize and generate native code in parallel partitions
Large executables were simplified and inlined as one module before the
optimizer split them, so most of a release build ran on a single core,
and any edit recompiled the whole program.

The LLVM helper now divides the program before optimization. It splits
the emitter's assembly text directly, so partitions also parse
concurrently, and falls back to LLVM's module splitter for any input
outside the emitter's grammar. Internal functions referenced by a single
function stay with that function, and globals stay with their first user.
Optimized partitions follow the ThinLTO model: each is simplified and
summarized on its own thread, a thin link imports small functions across
partitions for inlining and returns unexported hidden definitions to
internal linkage, then each partition is optimized and compiled on its
own thread. Unoptimized partitions compile independently, so dev builds
of large programs are parallel too.

A private per-helper cache directory stores simplified partitions under
the hash of their source and objects under the hash of the module after
importing, so a rebuild recompiles only the partitions an edit can
affect. To keep unchanged partitions identical across edits, each
partition numbers its metadata in first-use order, lists only the debug
compile units it uses, and orders declarations by name, and interned
string constants are named by their content rather than first-use order.

Partition counts are powers of two that depend only on the module size,
up to 64, so output stays the same on every host and functions keep
their partition across ordinary edits. LLVM text that arrives in parts is
written in large batches and handed to the helper in place instead of
being written a second time.
2026-10-09 19:51:47 -05:00

93 lines
3.2 KiB
C++

#include "diagnostics.h"
#include "emit.h"
#include "runtime-unit.h"
#include "target.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/Host.h"
#include <optional>
#include <cstdlib>
#include <string>
using namespace llvm;
#ifndef SCRIPTC_PACKAGE_VERSION
#define SCRIPTC_PACKAGE_VERSION "0.0.0-dev"
#endif
namespace {
int version(int Argc, char **Argv) {
if (Argc != 3 || StringRef(Argv[2]) != "--format=json")
return scriptc::reportError("usage", "version requires --format=json");
std::string Error;
std::unique_ptr<TargetMachine> Machine = scriptc::createTargetMachine(
scriptc::DefaultTarget, "2", Error);
if (!Machine)
return scriptc::reportError("target_machine_failed", Error);
json::Array Targets;
StringRef Backends = scriptc::TargetBackends;
while (!Backends.empty()) {
auto Split = Backends.split(',');
Targets.push_back(Split.first);
Backends = Split.second;
}
json::Array SupportedTargets;
StringRef TargetsText = scriptc::AllowedTargets;
while (!TargetsText.empty()) {
auto Split = TargetsText.split(',');
SupportedTargets.push_back(Split.first);
TargetsText = Split.second;
}
json::Object Response{
{"ok", true},
{"protocol_version", scriptc::ProtocolVersion},
{"scriptc_package_version", SCRIPTC_PACKAGE_VERSION},
{"llvm_version", LLVM_VERSION_STRING},
{"host_triple", sys::getDefaultTargetTriple()},
{"targets", std::move(Targets)},
{"supported_targets", std::move(SupportedTargets)},
{"default_target", scriptc::DefaultTarget},
{"data_layout", Machine->createDataLayout().getStringRepresentation()},
};
outs() << formatv("{0}\n", json::Value(std::move(Response)));
return 0;
}
} // namespace
int main(int Argc, char **Argv) {
scriptc::installFatalDiagnosticHandler();
// Process-isolated test seam for the fatal handler itself. It is inert in
// every ordinary invocation and ensures a future LLVM fatal never becomes
// an unstructured abort/stack trace in the Node caller.
if (std::getenv("SCRIPTC_LLVM_TEST_FATAL") != nullptr)
report_fatal_error("scriptc LLVM fatal diagnostic self-test");
if (Argc >= 2 && StringRef(Argv[1]) == "version")
return version(Argc, Argv);
if (Argc >= 2 && StringRef(Argv[1]) == "runtime-unit")
return scriptc::runtimeUnit(Argc, Argv);
if (Argc >= 2 && StringRef(Argv[1]) == "emit") {
std::optional<scriptc::EmitOptions> Options =
scriptc::parseEmitOptions(Argc, Argv);
if (!Options)
return scriptc::reportError(
"usage",
"emit requires --input <file> --output <file> and accepts "
"--filetype <obj|asm> --target <triple> --opt-level <0|1|2|3|s|z> "
"--relocation-model pic --diagnostic-format json "
"--source-path <path> --import-bitcode <file> --cache-dir <dir>");
return scriptc::emit(*Options);
}
return scriptc::reportError("usage",
"expected 'version --format=json', 'emit', or 'runtime-unit'");
}