Skip to content

Instantly share code, notes, and snippets.

@bgogul
Last active September 17, 2018 18:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bgogul/bf9f00e2d2d58622216dde3bb8a98b73 to your computer and use it in GitHub Desktop.
POC for simulating merge modules pass & serialized SIL in lldb
diff --git a/include/lldb/Symbol/SwiftASTContext.h b/include/lldb/Symbol/SwiftASTContext.h
index 2e2ae08c0..d768f0a1b 100644
--- a/include/lldb/Symbol/SwiftASTContext.h
+++ b/include/lldb/Symbol/SwiftASTContext.h
@@ -210,6 +210,20 @@ public:
m_resource_dir.clear();
}
+ const char *GetReplExprModulesDir() const {
+ if (m_repl_expr_modules_dir.empty())
+ return NULL;
+ return m_repl_expr_modules_dir.c_str();
+ }
+
+ void SetReplExprModulesDir(const char *path) {
+ if (path)
+ m_repl_expr_modules_dir = path;
+ else
+ m_repl_expr_modules_dir.clear();
+ }
+
+
size_t GetNumModuleSearchPaths() const;
const char *GetModuleSearchPathAtIndex(size_t idx) const;
@@ -844,6 +858,7 @@ protected:
// target's process pointer be filled in
std::string m_platform_sdk_path;
std::string m_resource_dir;
+ std::string m_repl_expr_modules_dir;
typedef std::map<Module *, std::vector<lldb::DataBufferSP>> ASTFileDataMap;
ASTFileDataMap m_ast_file_data_map;
// FIXME: this vector is needed because the LLDBNameLookup debugger clients
diff --git a/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp b/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp
index 93e4b7e7b..9e8ab583c 100644
--- a/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp
+++ b/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp
@@ -71,6 +71,7 @@
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
+#include "swift/Serialization/SerializationOptions.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Subsystems.h"
@@ -90,7 +91,6 @@ SwiftExpressionParser::SwiftExpressionParser(
// TODO This code is copied from ClangExpressionParser.cpp.
// Factor this out into common code.
-
lldb::TargetSP target_sp;
if (exe_scope) {
target_sp = exe_scope->CalculateTarget();
@@ -292,8 +292,7 @@ static bool PerformAutoImport(SwiftASTContext &swift_ast_context,
} else {
llvm::SmallVector<swift::ModuleDecl::ImportedModule, 2> parsed_imports;
- source_file.getImportedModules(parsed_imports,
- swift::ModuleDecl::ImportFilter::All);
+ source_file.getImportedModules(parsed_imports, swift::ModuleDecl::ImportFilter::All);
auto *persistent_expression_state =
sc.target_sp->GetSwiftPersistentExpressionState(exe_scope);
@@ -1841,7 +1840,7 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
variable_info.m_metadata.reset(
new VariableMetadataPersistent(persistent_variable));
- persistent_state->RegisterSwiftPersistentDecl(decl);
+ //persistent_state->RegisterSwiftPersistentDecl(decl);
}
}
@@ -1851,7 +1850,7 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
non_variables);
for (swift::ValueDecl *decl : non_variables) {
- persistent_state->RegisterSwiftPersistentDecl(decl);
+ //persistent_state->RegisterSwiftPersistentDecl(decl);
}
}
}
@@ -1949,6 +1948,45 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
runSILTFPartitionPass(*sil_module);
// SWIFT_ENABLE_TENSORFLOW
+ // Serialize the file and add it to the list of hand loaded modules!
+ if (auto expr_module_dir = m_swift_ast_context->GetReplExprModulesDir()) {
+ llvm::SmallString<256> filename(expr_module_dir);
+ std::string module_name;
+ GetNameFromModule(&parsed_expr->module, module_name);
+ // module_name += "ser";
+ llvm::sys::path::append(filename, module_name);
+ llvm::sys::path::replace_extension(filename, ".swiftmodule");
+ // // TODO: Check language is swift
+ // llvm::StringRef file_prefix;
+ // if (playground)
+ // file_prefix = "ser_playground";
+ // else if (repl)
+ // file_prefix = "ser_repl";
+ // else
+ // file_prefix = "ser_expr";
+ // llvm::Twine prefix =
+ // llvm::Twine(file_prefix)
+ // .concat(llvm::Twine(m_options.GetExpressionNumber()));
+ // int temp_fd;
+ // std::error_code err = llvm::sys::fs::createTemporaryFile(
+ // prefix, "swiftmodule", temp_fd, buffer);
+ // if (err) {
+ // diagnostic_manager.PutString(
+ // eDiagnosticSeverityError,
+ // "Unable to serialize SIL module, no additional error");
+ // return 1;
+ // }
+ // if (!err) {
+ swift::SerializationOptions serializationOpts;
+ //std::string output_path = buffer.str().str();
+ serializationOpts.OutputPath = filename.c_str();
+ serializationOpts.SerializeAllSIL = true;
+ serializationOpts.IsSIB = true;
+ llvm::outs() << "Serializing module to " << serializationOpts.OutputPath
+ << "\n";
+ swift::serialize(sil_module->getSwiftModule(), serializationOpts,
+ sil_module.get());
+ }
if (log) {
std::string s;
@@ -2015,14 +2053,22 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
// as part of the parse from the staging area in the external
// lookup object into the SwiftPersistentExpressionState.
swift::ModuleDecl *module = &parsed_expr->module;
- parsed_expr->ast_context.LoadedModules.insert({module->getName(), module});
- if (m_swift_ast_context)
- m_swift_ast_context->CacheModule(module);
+ // parsed_expr->ast_context.LoadedModules.insert({module->getName(), module});
+ // if (m_swift_ast_context)
+ // m_swift_ast_context->CacheModule(module);
if (m_sc.target_sp) {
auto *persistent_state =
m_sc.target_sp->GetSwiftPersistentExpressionState(*m_exe_scope);
- persistent_state->CopyInSwiftPersistentDecls(
- parsed_expr->external_lookup.GetStagedDecls());
+ // persistent_state->CopyInSwiftPersistentDecls(
+ // parsed_expr->external_lookup.GetStagedDecls());
+ // Add currently parsed module as a hand-loaded module for subsequent use.
+ {
+ std::string module_name = module->getName().str();
+ if (!module_name.empty()) {
+ ConstString module_const_str(module_name);
+ persistent_state->AddHandLoadedModule(module_const_str);
+ }
+ }
}
return 0;
}
diff --git a/source/Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h b/source/Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h
index f7b3e3e43..ec534854f 100644
--- a/source/Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h
+++ b/source/Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h
@@ -88,6 +88,19 @@ public:
return is_error ? "$E" : "$R";
}
+ const char *GetReplExprModulesDir() const {
+ if (m_repl_expr_modules_dir.empty())
+ return NULL;
+ return m_repl_expr_modules_dir.c_str();
+ }
+
+ void SetReplExprModulesDir(const char *path) {
+ if (path)
+ m_repl_expr_modules_dir = path;
+ else
+ m_repl_expr_modules_dir.clear();
+ }
+
void RemovePersistentVariable(lldb::ExpressionVariableSP variable) override;
void RegisterSwiftPersistentDecl(swift::ValueDecl *value_decl);
@@ -131,6 +144,8 @@ private:
SwiftDeclMap m_swift_persistent_decls; ///< The persistent functions declared
///by the user.
+ std::string m_repl_expr_modules_dir;
+
typedef std::set<lldb_private::ConstString> HandLoadedModuleSet;
HandLoadedModuleSet m_hand_loaded_modules; ///< These are the names of modules
///that we have loaded by
diff --git a/source/Symbol/SwiftASTContext.cpp b/source/Symbol/SwiftASTContext.cpp
index a36d1bb33..28775f4e9 100644
--- a/source/Symbol/SwiftASTContext.cpp
+++ b/source/Symbol/SwiftASTContext.cpp
@@ -1199,6 +1199,23 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
log->Printf("No resource dir available for module's SwiftASTContext.");
}
+ // Create a unique directory where we put serialized modules from REPL.
+ if (!swift_ast_sp->GetReplExprModulesDir()) {
+ llvm::SmallString<256> module_dir;
+ std::error_code err =
+ llvm::sys::fs::createUniqueDirectory("repl-swift-modules", module_dir);
+ if (err) {
+ if (log)
+ log->Printf("Unable to create serialized modules directory.");
+ } else {
+ if (log) {
+ log->Printf("Setting serialized module directory to %s",
+ module_dir.c_str());
+ }
+ swift_ast_sp->SetReplExprModulesDir(module_dir.c_str());
+ }
+ }
+
if (!got_serialized_options) {
std::vector<std::string> framework_search_paths;
@@ -1351,6 +1368,27 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
pool.wait();
}
+
+ // Create a unique directory where we put serialized modules from REPL.
+ if (!swift_ast_sp->GetReplExprModulesDir()) {
+ llvm::SmallString<256> module_dir;
+ std::error_code err =
+ llvm::sys::fs::createUniqueDirectory("repl-swift-modules", module_dir);
+ if (err) {
+ llvm::dbgs() << "Unable to create serialized modules directory.";
+ if (log)
+ log->Printf("Unable to create serialized modules directory.");
+ } else {
+ if (log) {
+ log->Printf("Setting serialized module directory to %s",
+ module_dir.c_str());
+ }
+ llvm::dbgs() << "Setting serialized module directory to " << module_dir;
+ swift_ast_sp->SetReplExprModulesDir(module_dir.c_str());
+ }
+ }
+
+
Status module_error;
for (size_t mi = 0; mi != num_images; ++mi) {
ModuleSP module_sp = target.GetImages().GetModuleAtIndex(mi);
@@ -2560,6 +2598,11 @@ swift::SearchPathOptions &SwiftASTContext::GetSearchPathOptions() {
if (resource_dir.Exists())
ConfigureResourceDirs(GetCompilerInvocation(), resource_dir, triple);
}
+
+ if (auto repl_modules_dir = GetReplExprModulesDir()) {
+ search_path_opts.ImportSearchPaths.emplace_back(repl_modules_dir);
+ }
+
}
return search_path_opts;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment