
This follows the RISC-V work done in 4b40ced4e5ba10b841516b3970e7699ba8ded572. This uses AArch64's target parser instead. We just list the names, without the "+" on them, which matches RISC-V's format. ``` $ ./bin/clang -target aarch64-linux-gnu --print-supported-extensions clang version 18.0.0 (https://github.com/llvm/llvm-project.git 154da8aec20719c82235a6957aa6e461f5a5e030) Target: aarch64-unknown-linux-gnu Thread model: posix InstalledDir: <...> All available -march extensions for AArch64 aes b16b16 bf16 brbe crc crypto cssc <...> ``` Since our extensions don't have versions in the same way there's just one column with the name in. Any extension without a feature name (including the special "none") is not listed as those cannot be passed to -march, they're just for the backend. For example the MTE extension can be added with "+memtag" but MTE2 and MTE3 do not have feature names so they cannot be added to -march. This does not attempt to tackle the fact that clang allows invalid combinations of AArch64 extensions, it simply lists the possible options. It's still up to the user to ask for something sensible. Equally, this has no context of what CPU is being selected. Neither does the RISC-V option, the user has to be aware of that. I've added a target parser test, and a high level clang test that checks RISC-V and AArch64 work and that Intel, that doesn't support this, shows the correct error.
328 lines
12 KiB
C++
328 lines
12 KiB
C++
//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This is the entry point to the clang -cc1 functionality, which implements the
|
|
// core compiler functionality along with a number of additional tools for
|
|
// demonstration and testing purposes.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Basic/Stack.h"
|
|
#include "clang/Basic/TargetOptions.h"
|
|
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
|
|
#include "clang/Config/config.h"
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
|
#include "clang/Driver/Options.h"
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
#include "clang/Frontend/TextDiagnosticBuffer.h"
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
#include "clang/Frontend/Utils.h"
|
|
#include "clang/FrontendTool/Utils.h"
|
|
#include "llvm/ADT/Statistic.h"
|
|
#include "llvm/Config/llvm-config.h"
|
|
#include "llvm/LinkAllPasses.h"
|
|
#include "llvm/MC/TargetRegistry.h"
|
|
#include "llvm/Option/Arg.h"
|
|
#include "llvm/Option/ArgList.h"
|
|
#include "llvm/Option/OptTable.h"
|
|
#include "llvm/Support/BuryPointer.h"
|
|
#include "llvm/Support/Compiler.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
#include "llvm/Support/Path.h"
|
|
#include "llvm/Support/Process.h"
|
|
#include "llvm/Support/RISCVISAInfo.h"
|
|
#include "llvm/Support/Signals.h"
|
|
#include "llvm/Support/TargetSelect.h"
|
|
#include "llvm/Support/TimeProfiler.h"
|
|
#include "llvm/Support/Timer.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
#include "llvm/TargetParser/AArch64TargetParser.h"
|
|
#include <cstdio>
|
|
|
|
#ifdef CLANG_HAVE_RLIMITS
|
|
#include <sys/resource.h>
|
|
#endif
|
|
|
|
using namespace clang;
|
|
using namespace llvm::opt;
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Main driver
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
static void LLVMErrorHandler(void *UserData, const char *Message,
|
|
bool GenCrashDiag) {
|
|
DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
|
|
|
|
Diags.Report(diag::err_fe_error_backend) << Message;
|
|
|
|
// Run the interrupt handlers to make sure any special cleanups get done, in
|
|
// particular that we remove files registered with RemoveFileOnSignal.
|
|
llvm::sys::RunInterruptHandlers();
|
|
|
|
// We cannot recover from llvm errors. When reporting a fatal error, exit
|
|
// with status 70 to generate crash diagnostics. For BSD systems this is
|
|
// defined as an internal software error. Otherwise, exit with status 1.
|
|
llvm::sys::Process::Exit(GenCrashDiag ? 70 : 1);
|
|
}
|
|
|
|
#ifdef CLANG_HAVE_RLIMITS
|
|
#if defined(__linux__) && defined(__PIE__)
|
|
static size_t getCurrentStackAllocation() {
|
|
// If we can't compute the current stack usage, allow for 512K of command
|
|
// line arguments and environment.
|
|
size_t Usage = 512 * 1024;
|
|
if (FILE *StatFile = fopen("/proc/self/stat", "r")) {
|
|
// We assume that the stack extends from its current address to the end of
|
|
// the environment space. In reality, there is another string literal (the
|
|
// program name) after the environment, but this is close enough (we only
|
|
// need to be within 100K or so).
|
|
unsigned long StackPtr, EnvEnd;
|
|
// Disable silly GCC -Wformat warning that complains about length
|
|
// modifiers on ignored format specifiers. We want to retain these
|
|
// for documentation purposes even though they have no effect.
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Wformat"
|
|
#endif
|
|
if (fscanf(StatFile,
|
|
"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %*lu "
|
|
"%*lu %*ld %*ld %*ld %*ld %*ld %*ld %*llu %*lu %*ld %*lu %*lu "
|
|
"%*lu %*lu %lu %*lu %*lu %*lu %*lu %*lu %*llu %*lu %*lu %*d %*d "
|
|
"%*u %*u %*llu %*lu %*ld %*lu %*lu %*lu %*lu %*lu %*lu %lu %*d",
|
|
&StackPtr, &EnvEnd) == 2) {
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
|
#pragma GCC diagnostic pop
|
|
#endif
|
|
Usage = StackPtr < EnvEnd ? EnvEnd - StackPtr : StackPtr - EnvEnd;
|
|
}
|
|
fclose(StatFile);
|
|
}
|
|
return Usage;
|
|
}
|
|
|
|
#include <alloca.h>
|
|
|
|
LLVM_ATTRIBUTE_NOINLINE
|
|
static void ensureStackAddressSpace() {
|
|
// Linux kernels prior to 4.1 will sometimes locate the heap of a PIE binary
|
|
// relatively close to the stack (they are only guaranteed to be 128MiB
|
|
// apart). This results in crashes if we happen to heap-allocate more than
|
|
// 128MiB before we reach our stack high-water mark.
|
|
//
|
|
// To avoid these crashes, ensure that we have sufficient virtual memory
|
|
// pages allocated before we start running.
|
|
size_t Curr = getCurrentStackAllocation();
|
|
const int kTargetStack = DesiredStackSize - 256 * 1024;
|
|
if (Curr < kTargetStack) {
|
|
volatile char *volatile Alloc =
|
|
static_cast<volatile char *>(alloca(kTargetStack - Curr));
|
|
Alloc[0] = 0;
|
|
Alloc[kTargetStack - Curr - 1] = 0;
|
|
}
|
|
}
|
|
#else
|
|
static void ensureStackAddressSpace() {}
|
|
#endif
|
|
|
|
/// Attempt to ensure that we have at least 8MiB of usable stack space.
|
|
static void ensureSufficientStack() {
|
|
struct rlimit rlim;
|
|
if (getrlimit(RLIMIT_STACK, &rlim) != 0)
|
|
return;
|
|
|
|
// Increase the soft stack limit to our desired level, if necessary and
|
|
// possible.
|
|
if (rlim.rlim_cur != RLIM_INFINITY &&
|
|
rlim.rlim_cur < rlim_t(DesiredStackSize)) {
|
|
// Try to allocate sufficient stack.
|
|
if (rlim.rlim_max == RLIM_INFINITY ||
|
|
rlim.rlim_max >= rlim_t(DesiredStackSize))
|
|
rlim.rlim_cur = DesiredStackSize;
|
|
else if (rlim.rlim_cur == rlim.rlim_max)
|
|
return;
|
|
else
|
|
rlim.rlim_cur = rlim.rlim_max;
|
|
|
|
if (setrlimit(RLIMIT_STACK, &rlim) != 0 ||
|
|
rlim.rlim_cur != DesiredStackSize)
|
|
return;
|
|
}
|
|
|
|
// We should now have a stack of size at least DesiredStackSize. Ensure
|
|
// that we can actually use that much, if necessary.
|
|
ensureStackAddressSpace();
|
|
}
|
|
#else
|
|
static void ensureSufficientStack() {}
|
|
#endif
|
|
|
|
/// Print supported cpus of the given target.
|
|
static int PrintSupportedCPUs(std::string TargetStr) {
|
|
std::string Error;
|
|
const llvm::Target *TheTarget =
|
|
llvm::TargetRegistry::lookupTarget(TargetStr, Error);
|
|
if (!TheTarget) {
|
|
llvm::errs() << Error;
|
|
return 1;
|
|
}
|
|
|
|
// the target machine will handle the mcpu printing
|
|
llvm::TargetOptions Options;
|
|
std::unique_ptr<llvm::TargetMachine> TheTargetMachine(
|
|
TheTarget->createTargetMachine(TargetStr, "", "+cpuhelp", Options,
|
|
std::nullopt));
|
|
return 0;
|
|
}
|
|
|
|
static int PrintSupportedExtensions(std::string TargetStr) {
|
|
std::string Error;
|
|
const llvm::Target *TheTarget =
|
|
llvm::TargetRegistry::lookupTarget(TargetStr, Error);
|
|
if (!TheTarget) {
|
|
llvm::errs() << Error;
|
|
return 1;
|
|
}
|
|
|
|
llvm::TargetOptions Options;
|
|
std::unique_ptr<llvm::TargetMachine> TheTargetMachine(
|
|
TheTarget->createTargetMachine(TargetStr, "", "", Options, std::nullopt));
|
|
const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple();
|
|
|
|
if (MachineTriple.isRISCV())
|
|
llvm::riscvExtensionsHelp();
|
|
else if (MachineTriple.isAArch64())
|
|
llvm::AArch64::PrintSupportedExtensions();
|
|
else {
|
|
// The option was already checked in Driver::HandleImmediateArgs,
|
|
// so we do not expect to get here if we are not a supported architecture.
|
|
assert(0 && "Unhandled triple for --print-supported-extensions option.");
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
|
|
ensureSufficientStack();
|
|
|
|
std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
// Register the support for object-file-wrapped Clang modules.
|
|
auto PCHOps = Clang->getPCHContainerOperations();
|
|
PCHOps->registerWriter(std::make_unique<ObjectFilePCHContainerWriter>());
|
|
PCHOps->registerReader(std::make_unique<ObjectFilePCHContainerReader>());
|
|
|
|
// Initialize targets first, so that --version shows registered targets.
|
|
llvm::InitializeAllTargets();
|
|
llvm::InitializeAllTargetMCs();
|
|
llvm::InitializeAllAsmPrinters();
|
|
llvm::InitializeAllAsmParsers();
|
|
|
|
// Buffer diagnostics from argument parsing so that we can output them using a
|
|
// well formed diagnostic object.
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
|
|
|
|
// Setup round-trip remarks for the DiagnosticsEngine used in CreateFromArgs.
|
|
if (find(Argv, StringRef("-Rround-trip-cc1-args")) != Argv.end())
|
|
Diags.setSeverity(diag::remark_cc1_round_trip_generated,
|
|
diag::Severity::Remark, {});
|
|
|
|
bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
|
|
Argv, Diags, Argv0);
|
|
|
|
if (!Clang->getFrontendOpts().TimeTracePath.empty()) {
|
|
llvm::timeTraceProfilerInitialize(
|
|
Clang->getFrontendOpts().TimeTraceGranularity, Argv0);
|
|
}
|
|
// --print-supported-cpus takes priority over the actual compilation.
|
|
if (Clang->getFrontendOpts().PrintSupportedCPUs)
|
|
return PrintSupportedCPUs(Clang->getTargetOpts().Triple);
|
|
|
|
// --print-supported-extensions takes priority over the actual compilation.
|
|
if (Clang->getFrontendOpts().PrintSupportedExtensions)
|
|
return PrintSupportedExtensions(Clang->getTargetOpts().Triple);
|
|
|
|
// Infer the builtin include path if unspecified.
|
|
if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
|
|
Clang->getHeaderSearchOpts().ResourceDir.empty())
|
|
Clang->getHeaderSearchOpts().ResourceDir =
|
|
CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
|
|
|
|
// Create the actual diagnostics engine.
|
|
Clang->createDiagnostics();
|
|
if (!Clang->hasDiagnostics())
|
|
return 1;
|
|
|
|
// Set an error handler, so that any LLVM backend diagnostics go through our
|
|
// error handler.
|
|
llvm::install_fatal_error_handler(LLVMErrorHandler,
|
|
static_cast<void*>(&Clang->getDiagnostics()));
|
|
|
|
DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
|
|
if (!Success) {
|
|
Clang->getDiagnosticClient().finish();
|
|
return 1;
|
|
}
|
|
|
|
// Execute the frontend actions.
|
|
{
|
|
llvm::TimeTraceScope TimeScope("ExecuteCompiler");
|
|
Success = ExecuteCompilerInvocation(Clang.get());
|
|
}
|
|
|
|
// If any timers were active but haven't been destroyed yet, print their
|
|
// results now. This happens in -disable-free mode.
|
|
llvm::TimerGroup::printAll(llvm::errs());
|
|
llvm::TimerGroup::clearAll();
|
|
|
|
if (llvm::timeTraceProfilerEnabled()) {
|
|
// It is possible that the compiler instance doesn't own a file manager here
|
|
// if we're compiling a module unit. Since the file manager are owned by AST
|
|
// when we're compiling a module unit. So the file manager may be invalid
|
|
// here.
|
|
//
|
|
// It should be fine to create file manager here since the file system
|
|
// options are stored in the compiler invocation and we can recreate the VFS
|
|
// from the compiler invocation.
|
|
if (!Clang->hasFileManager())
|
|
Clang->createFileManager(createVFSFromCompilerInvocation(
|
|
Clang->getInvocation(), Clang->getDiagnostics()));
|
|
|
|
if (auto profilerOutput = Clang->createOutputFile(
|
|
Clang->getFrontendOpts().TimeTracePath, /*Binary=*/false,
|
|
/*RemoveFileOnSignal=*/false,
|
|
/*useTemporary=*/false)) {
|
|
llvm::timeTraceProfilerWrite(*profilerOutput);
|
|
profilerOutput.reset();
|
|
llvm::timeTraceProfilerCleanup();
|
|
Clang->clearOutputFiles(false);
|
|
}
|
|
}
|
|
|
|
// Our error handler depends on the Diagnostics object, which we're
|
|
// potentially about to delete. Uninstall the handler now so that any
|
|
// later errors use the default handling behavior instead.
|
|
llvm::remove_fatal_error_handler();
|
|
|
|
// When running with -disable-free, don't do any destruction or shutdown.
|
|
if (Clang->getFrontendOpts().DisableFree) {
|
|
llvm::BuryPointer(std::move(Clang));
|
|
return !Success;
|
|
}
|
|
|
|
return !Success;
|
|
}
|