Skip to content

Mutex lock (implementation base on ASTContext::getAllocator::identifyObject) #704

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Vipul-Cariappa
Copy link
Collaborator

Same as #698
But using ASTContext::getAllocator::identifyObject.

This implementation is terribly slow.

We need to identify which interpreter a Decl belongs to, when using multiple interpreter.
We do it by checking which `clang::ASTContext` the `clang::Decl` belongs
We maintain a map: `clang::ASTContext -> Cpp::InterpreterInfo`. Using this map, be identify the correct interpreter.

There are 2 usecases for this:
1. We can now lock the correct interpreter making it thread safe.
2. User of `libCppInterOp` need not set the correct active interpreter using `Cpp::ActivateInterpreter`, this information can be retrived using the map.
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 21. Check the log or trigger a new build to see more.

@@ -126,15 +134,60 @@ struct InterpreterInfo {
};

// std::deque avoids relocations and calling the dtor of InterpreterInfo.
static llvm::ManagedStatic<std::deque<InterpreterInfo>> sInterpreters;
static llvm::ManagedStatic<std::deque<std::shared_ptr<InterpreterInfo>>>
sInterpreters;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'sInterpreters' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    sInterpreters;
    ^

static llvm::ManagedStatic<std::deque<std::shared_ptr<InterpreterInfo>>>
sInterpreters;
static llvm::ManagedStatic<
std::unordered_map<clang::ASTContext*, std::weak_ptr<InterpreterInfo>>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::unordered_map" is directly included [misc-include-cleaner]

lib/CppInterOp/CppInterOp.cpp:40:

- #if CLANG_VERSION_MAJOR >= 19
+ #include <unordered_map>
+ #if CLANG_VERSION_MAJOR >= 19

sInterpreters;
static llvm::ManagedStatic<
std::unordered_map<clang::ASTContext*, std::weak_ptr<InterpreterInfo>>>
sInterpreterASTMap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'sInterpreterASTMap' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    sInterpreterASTMap;
    ^

static llvm::ManagedStatic<
std::unordered_map<clang::ASTContext*, std::weak_ptr<InterpreterInfo>>>
sInterpreterASTMap;
static std::mutex InterpreterStackLock;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'InterpreterStackLock' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

static std::mutex InterpreterStackLock;
                  ^

if (item.first->getAllocator().identifyObject(D))
return *item.second.lock();
}
llvm_unreachable(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "llvm_unreachable" is directly included [misc-include-cleaner]

lib/CppInterOp/CppInterOp.cpp:40:

- #if CLANG_VERSION_MAJOR >= 19
+ #include <llvm/Support/ErrorHandling.h>
+ #if CLANG_VERSION_MAJOR >= 19

@@ -684,13 +757,14 @@
TCppScope_t GetNamed(const std::string& name,
TCppScope_t parent /*= nullptr*/) {
clang::DeclContext* Within = 0;
auto* D = (clang::Decl*)parent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

  auto* D = (clang::Decl*)parent;
            ^

D = GetUnderlyingScope(D);
Within = llvm::dyn_cast<clang::DeclContext>(D);
}

auto* ND = Cpp_utils::Lookup::Named(&getSema(), name, Within);
auto* ND = Cpp_utils::Lookup::Named(&getSema(D), name, Within);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::utils::Lookup::Named" is directly included [misc-include-cleaner]

lib/CppInterOp/CppInterOp.cpp:12:

+ #include "CppInterOpInterpreter.h"

@@ -734,7 +811,11 @@
TCppScope_t GetBaseClass(TCppScope_t klass, TCppIndex_t ibase) {
auto* D = (Decl*)klass;
auto* CXXRD = llvm::dyn_cast_or_null<CXXRecordDecl>(D);
if (!CXXRD || CXXRD->getNumBases() <= ibase)
if (!CXXRD)
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use nullptr [modernize-use-nullptr]

Suggested change
return 0;
return nullptr;

return interp.getCI()->getSema().LookupDefaultConstructor(CXXRD);
}

TCppFunction_t GetDefaultConstructor(TCppScope_t scope) {
return GetDefaultConstructor(getInterp(), scope);
auto* CXXRD = (clang::CXXRecordDecl*)scope;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

  auto* CXXRD = (clang::CXXRecordDecl*)scope;
                ^


auto* D = (clang::Decl*)func;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

  auto* D = (clang::Decl*)func;
            ^

@vgvassilev
Copy link
Contributor

Same as #698 But using ASTContext::getAllocator::identifyObject.

This implementation is terribly slow.

Probably no need to pursue if we have a superior approach.

@Vipul-Cariappa Vipul-Cariappa marked this pull request as draft August 13, 2025 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants