Skip to content

Rebase llvm19 patches #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion clang/lib/Basic/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ OptionalFileEntryRef FileManager::getBypassFile(FileEntryRef VF) {

// If we've already bypassed just use the existing one.
auto Insertion = SeenBypassFileEntries->insert(
{VF.getNameAsRequested(), std::errc::no_such_file_or_directory});
{VF.getName(), std::errc::no_such_file_or_directory});
if (!Insertion.second)
return FileEntryRef(*Insertion.first);

Expand Down
9 changes: 1 addition & 8 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2076,16 +2076,9 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(

// Check whether M refers to the file in the prebuilt module path.
if (M && M->getASTFile())
if (auto ModuleFile = FileMgr->getOptionalFileRef(ModuleFilename)) {
if (auto ModuleFile = FileMgr->getFile(ModuleFilename))
if (*ModuleFile == M->getASTFile())
return M;
#if !defined(__APPLE__)
// Workaround for ext4 file system. Also check bypass file if exists.
if (auto Bypass = FileMgr->getBypassFile(*ModuleFile))
if (*Bypass == M->getASTFile())
return M;
#endif
}

getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt)
<< ModuleName;
Expand Down
8 changes: 1 addition & 7 deletions clang/lib/Frontend/Rewrite/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,9 @@ class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {

void visitModuleFile(StringRef Filename,
serialization::ModuleKind Kind) override {
auto File = CI.getFileManager().getOptionalFileRef(Filename);
auto File = CI.getFileManager().getFile(Filename);
assert(File && "missing file for loaded module?");

#if !defined(__APPLE__)
// Workaround for ext4 file system.
if (auto Bypass = CI.getFileManager().getBypassFile(*File))
File = *Bypass;
#endif

// Only rewrite each module file once.
if (!Rewritten.insert(*File).second)
return;
Expand Down
26 changes: 2 additions & 24 deletions clang/lib/Serialization/ModuleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,8 @@ using namespace clang;
using namespace serialization;

ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const {
auto Entry = FileMgr.getOptionalFileRef(Name, /*OpenFile=*/false,
/*CacheFailure=*/false);
#if !defined(__APPLE__)
if (Entry) {
// On Linux ext4 FileManager's inode caching system does not
// provide us correct behaviour for ModuleCache directories.
// inode can be reused after PCM delete resulting in cache misleading.
if (auto BypassFile = FileMgr.getBypassFile(*Entry))
Entry = *BypassFile;
}
#endif

auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
/*CacheFailure=*/false);
if (Entry)
return lookup(*Entry);

Expand Down Expand Up @@ -455,18 +445,6 @@ bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,
File = FileMgr.getOptionalFileRef(FileName, /*OpenFile=*/true,
/*CacheFailure=*/false);

#if !defined(__APPLE__)
if (File) {
// On Linux ext4 FileManager's inode caching system does not
// provide us correct behaviour for ModuleCache directories.
// inode can be reused after PCM delete resulting in cache misleading.
// Only use the bypass file if bypass succeed in case the underlying file
// system doesn't support bypass (thus there is no need for the workaround).
if (auto Bypass = FileMgr.getBypassFile(*File))
File = *Bypass;
}
#endif

// If the file is known to the module cache but not in the filesystem, it is
// a memory buffer. Create a virtual file for it.
if (!File) {
Expand Down
3 changes: 0 additions & 3 deletions clang/test/Modules/explicit-build-relpath.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/// Due to module bypass workaround in https://reviews.llvm.org/D97850 for ext4 FS, relative and absolute path do not match after bypass.
// REQUIRES: system-darwin

// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: cd %t
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm-c/Transforms/PassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ void LLVMPassBuilderOptionsSetMergeFunctions(LLVMPassBuilderOptionsRef Options,
void LLVMPassBuilderOptionsSetInlinerThreshold(
LLVMPassBuilderOptionsRef Options, int Threshold);

void LLVMPassBuilderOptionsSetMaxDevirtIterations(
LLVMPassBuilderOptionsRef Options, int Iterations);

/**
* Dispose of a heap-allocated PassBuilderOptions instance
*/
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Passes/PassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class PipelineTuningOptions {
// analyses after various module->function or cgscc->function adaptors in the
// default pipelines.
bool EagerlyInvalidateAnalyses;

/// Tuning option to override default devirtualization iterations.
int MaxDevirtIterations;
};

/// This class provides access to building LLVM's passes.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/LTO/ThinLTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ void ThinLTOCodeGenerator::run() {
};
struct ModuleInfo {
std::unique_ptr<AsyncModuleCacheEntry> Entry;
std::atomic<unsigned> State = ModuleState::MS_Empty;
std::atomic<unsigned> State = 0b0000;
std::unique_ptr<MemoryBuffer> ComputedBuffer;
std::unique_ptr<MemoryBuffer> CachedBuffer;
};
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Passes/PassBuilderBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ void LLVMPassBuilderOptionsSetInlinerThreshold(
unwrap(Options)->PTO.InlinerThreshold = Threshold;
}

void LLVMPassBuilderOptionsSetMaxDevirtIterations(
LLVMPassBuilderOptionsRef Options, int Iterations) {
unwrap(Options)->PTO.MaxDevirtIterations = Iterations;
}

void LLVMDisposePassBuilderOptions(LLVMPassBuilderOptionsRef Options) {
delete unwrap(Options);
}
4 changes: 3 additions & 1 deletion llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ PipelineTuningOptions::PipelineTuningOptions() {
UnifiedLTO = false;
MergeFunctions = EnableMergeFunctions;
InlinerThreshold = -1;
MaxDevirtIterations = -1;
EagerlyInvalidateAnalyses = EnableEagerlyInvalidateAnalyses;
}

Expand Down Expand Up @@ -912,9 +913,10 @@ PassBuilder::buildInlinerPipeline(OptimizationLevel Level,
if (PGOOpt)
IP.EnableDeferral = EnablePGOInlineDeferral;

int MDI = PTO.MaxDevirtIterations == -1 ? MaxDevirtIterations : PTO.MaxDevirtIterations;
ModuleInlinerWrapperPass MIWP(IP, PerformMandatoryInliningsFirst,
InlineContext{Phase, InlinePass::CGSCCInliner},
UseInlineAdvisor, MaxDevirtIterations);
UseInlineAdvisor, MDI);

// Require the GlobalsAA analysis for the module so we can query it within
// the CGSCC pipeline.
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Support/CrashRecoveryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,12 @@ static void uninstallExceptionOrSignalHandlers() {

#include <signal.h>

/// KOTLIN/NATIVE SPECIFIC HACK
///
/// We need to disable handling of signals that can originate from the JVM GC,
/// since they should not trigger the LLVM signal handler.
static const int Signals[] =
{ SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
{ SIGABRT, /* SIGBUS, SIGFPE, SIGILL, SIGSEGV, */ SIGTRAP };
static const unsigned NumSignals = std::size(Signals);
static struct sigaction PrevActions[NumSignals];

Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/Support/Unix/Signals.inc
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,17 @@ static void RegisterHandlers() { // Not signal-safe.

enum class SignalKind { IsKill, IsInfo };
auto registerHandler = [&](int Signal, SignalKind Kind) {
/// KOTLIN/NATIVE SPECIFIC HACK
///
/// We need to disable handling of signals that can originate from the JVM GC,
/// since they should not trigger the LLVM signal handler.

static const int JvmSigs[] = {
SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGUSR1, SIGUSR2
};

for (auto S : JvmSigs) if (Signal == S) return;

unsigned Index = NumRegisteredSignals.load();
assert(Index < std::size(RegisteredSignalInfo) &&
"Out of space for signal handlers!");
Expand Down
9 changes: 9 additions & 0 deletions llvm/unittests/Support/CrashRecoveryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ static void incrementGlobal() { ++GlobalInt; }
static void llvmTrap() { LLVM_BUILTIN_TRAP; }
static void incrementGlobalWithParam(void *) { ++GlobalInt; }

#define SKIP_WITH_KOTLIN_PATCH() \
GTEST_SKIP() << "Crash recovery from SEGFAULT disabled by Kotlin patch"

TEST(CrashRecoveryTest, Basic) {
SKIP_WITH_KOTLIN_PATCH();

llvm::CrashRecoveryContext::Enable();
GlobalInt = 0;
EXPECT_TRUE(CrashRecoveryContext().RunSafely(incrementGlobal));
Expand All @@ -57,6 +62,8 @@ struct IncrementGlobalCleanup : CrashRecoveryContextCleanup {
static void noop() {}

TEST(CrashRecoveryTest, Cleanup) {
SKIP_WITH_KOTLIN_PATCH();

llvm::CrashRecoveryContext::Enable();
GlobalInt = 0;
{
Expand All @@ -77,6 +84,8 @@ TEST(CrashRecoveryTest, Cleanup) {
}

TEST(CrashRecoveryTest, DumpStackCleanup) {
SKIP_WITH_KOTLIN_PATCH();

SmallString<128> Filename;
std::error_code EC = sys::fs::createTemporaryFile("crash", "test", Filename);
EXPECT_FALSE(EC);
Expand Down