Skip to content

Commit

Permalink
[v0.0.3] Compiler environment cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
DronCode committed Mar 21, 2024
1 parent a35a482 commit f1da3b6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
4 changes: 4 additions & 0 deletions LLVM/include/RG3/LLVM/CodeAnalyzer.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include <RG3/LLVM/CompilerConfigDetector.h>
#include <RG3/LLVM/CompilerConfig.h>
#include <RG3/Cpp/TypeBase.h>
#include <filesystem>
#include <variant>
#include <cstdint>
#include <optional>
#include <span>


Expand Down Expand Up @@ -40,12 +42,14 @@ namespace rg3::llvm

void setSourceCode(const std::string& sourceCode);
void setSourceFile(const std::filesystem::path& sourceFile);
void setCompilerEnvironment(const CompilerEnvironment& env);
CompilerConfig& getCompilerConfig();

AnalyzerResult analyze();

private:
std::variant<std::filesystem::path, std::string> m_source;
std::optional<CompilerEnvironment> m_env;
CompilerConfig m_compilerConfig;
};
}
25 changes: 18 additions & 7 deletions LLVM/source/CodeAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ namespace rg3::llvm
m_source.emplace<std::filesystem::path>(sourceFile);
}

void CodeAnalyzer::setCompilerEnvironment(const CompilerEnvironment& env)
{
m_env = env;
}

CompilerConfig& CodeAnalyzer::getCompilerConfig()
{
return m_compilerConfig;
Expand Down Expand Up @@ -140,18 +145,24 @@ namespace rg3::llvm
AnalyzerResult CodeAnalyzer::analyze()
{
AnalyzerResult result;

const CompilerEnvironment* pCompilerEnv = nullptr;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Run platform env detector
const auto compilerEnvironment = CompilerConfigDetector::detectSystemCompilerEnvironment();
if (auto pEnvFailure = std::get_if<CompilerEnvError>(&compilerEnvironment))
if (!m_env.has_value())
{
// Fatal error
result.vIssues.emplace_back(AnalyzerResult::CompilerIssue { AnalyzerResult::CompilerIssue::IssueKind::IK_ERROR, sourceToString(m_source), 0, 0, pEnvFailure->message });
return result;
const auto compilerEnvironment = CompilerConfigDetector::detectSystemCompilerEnvironment();
if (auto pEnvFailure = std::get_if<CompilerEnvError>(&compilerEnvironment))
{
// Fatal error
result.vIssues.emplace_back(AnalyzerResult::CompilerIssue { AnalyzerResult::CompilerIssue::IssueKind::IK_ERROR, sourceToString(m_source), 0, 0, pEnvFailure->message });
return result;
}

// Override env
m_env = *std::get_if<CompilerEnvironment>(&compilerEnvironment);
}

const auto* pCompilerEnv = std::get_if<CompilerEnvironment>(&compilerEnvironment);
pCompilerEnv = &m_env.value();

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
36 changes: 33 additions & 3 deletions PyBind/source/PyAnalyzerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <RG3/PyBind/PyTypeEnum.h>
#include <RG3/PyBind/PyTypeAlias.h>
#include <RG3/LLVM/CodeAnalyzer.h>
#include <RG3/LLVM/CompilerConfigDetector.h>
#include <RG3/Cpp/TransactionGuard.h>
#include <RG3/Cpp/TypeClass.h>
#include <RG3/Cpp/TypeEnum.h>
Expand Down Expand Up @@ -91,6 +92,7 @@ namespace rg3::pybind
std::mutex lockMtx;
Storage tasks;
std::vector<std::thread> workers;
std::optional<rg3::llvm::CompilerEnvironment> m_compilerEnv {};

PyFoundSubjects* pAnalyzerStorage{ nullptr };

Expand Down Expand Up @@ -161,6 +163,11 @@ namespace rg3::pybind
return Transaction(lockMtx, tasks);
}

void setCompilerEnvironment(const rg3::llvm::CompilerEnvironment& compilerEnv)
{
m_compilerEnv = compilerEnv;
}

bool runWorkers(int workersAmount)
{
if (workersAmount <= 1)
Expand All @@ -173,7 +180,7 @@ namespace rg3::pybind
{
std::thread worker {
[this, iWorkerIndex = static_cast<size_t>(i)]() {
workerEntryPoint(iWorkerIndex);
workerEntryPoint(iWorkerIndex, m_compilerEnv);
}
};

Expand All @@ -193,12 +200,13 @@ namespace rg3::pybind
}

private:
void workerEntryPoint(size_t iWorkerId)
void workerEntryPoint(size_t iWorkerId, const std::optional<rg3::llvm::CompilerEnvironment>& sCompilerEnvironment)
{
struct Visitor
{
bool* stopFlag;
PyFoundSubjects* pAnalyzerStorage { nullptr };
std::optional<rg3::llvm::CompilerEnvironment> sCompilerEnv { std::nullopt };

void operator()(NullTask)
{
Expand All @@ -218,6 +226,12 @@ namespace rg3::pybind
{
// Do analyze stub
rg3::llvm::CodeAnalyzer codeAnalyzer { analyzeHeader.headerPath, analyzeHeader.compilerConfig };
if (sCompilerEnv.has_value())
{
// set environment from cache
codeAnalyzer.setCompilerEnvironment(sCompilerEnv.value());
}

rg3::llvm::AnalyzerResult analyzeResult = codeAnalyzer.analyze();

{
Expand Down Expand Up @@ -292,7 +306,7 @@ namespace rg3::pybind


bool bShouldStop = false;
Visitor v { &bShouldStop, pAnalyzerStorage };
Visitor v { &bShouldStop, pAnalyzerStorage, sCompilerEnvironment };

while (!bShouldStop)
{
Expand Down Expand Up @@ -515,6 +529,22 @@ namespace rg3::pybind
m_pySubjects.vFoundTypeInstances.clear();
bool bResult = false;

// Collect compiler environment
auto environmentExtractResult = rg3::llvm::CompilerConfigDetector::detectSystemCompilerEnvironment();
if (rg3::llvm::CompilerEnvError* pError = std::get_if<rg3::llvm::CompilerEnvError>(&environmentExtractResult))
{
rg3::llvm::AnalyzerResult::CompilerIssue issue;
issue.kind = rg3::llvm::AnalyzerResult::CompilerIssue::IssueKind::IK_ERROR;
issue.sSourceFile = "RG3_GLOBAL_SCOPE";
issue.sMessage = fmt::format("RG3|Detect compiler environment failed: {}", pError->message);

m_pySubjects.pyFoundIssues.append(issue);
return false;
}

// Set environment to minimize future clang invocations
m_pContext->setCompilerEnvironment(*std::get_if<rg3::llvm::CompilerEnvironment>(&environmentExtractResult));

// Create tasks
{
PyGuard pyGuard {};
Expand Down

0 comments on commit f1da3b6

Please sign in to comment.