diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2022-03-20 11:40:34 +0000 | 
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2022-05-14 11:43:05 +0000 | 
| commit | 349cc55c9796c4596a5b9904cd3281af295f878f (patch) | |
| tree | 410c5a785075730a35f1272ca6a7adf72222ad03 /contrib/llvm-project/clang/lib/Interpreter | |
| parent | cb2ae6163174b90e999326ecec3699ee093a5d43 (diff) | |
| parent | c0981da47d5696fe36474fcf86b4ce03ae3ff818 (diff) | |
Diffstat (limited to 'contrib/llvm-project/clang/lib/Interpreter')
5 files changed, 73 insertions, 15 deletions
diff --git a/contrib/llvm-project/clang/lib/Interpreter/IncrementalExecutor.cpp b/contrib/llvm-project/clang/lib/Interpreter/IncrementalExecutor.cpp index 9a368d9122bc..705235aafa07 100644 --- a/contrib/llvm-project/clang/lib/Interpreter/IncrementalExecutor.cpp +++ b/contrib/llvm-project/clang/lib/Interpreter/IncrementalExecutor.cpp @@ -60,4 +60,15 @@ llvm::Error IncrementalExecutor::runCtors() const {    return Jit->initialize(Jit->getMainJITDylib());  } +llvm::Expected<llvm::JITTargetAddress> +IncrementalExecutor::getSymbolAddress(llvm::StringRef Name, +                                      SymbolNameKind NameKind) const { +  auto Sym = (NameKind == LinkerName) ? Jit->lookupLinkerMangled(Name) +                                      : Jit->lookup(Name); + +  if (!Sym) +    return Sym.takeError(); +  return Sym->getAddress(); +} +  } // end namespace clang diff --git a/contrib/llvm-project/clang/lib/Interpreter/IncrementalExecutor.h b/contrib/llvm-project/clang/lib/Interpreter/IncrementalExecutor.h index b4c6ddec1047..24447994d5f1 100644 --- a/contrib/llvm-project/clang/lib/Interpreter/IncrementalExecutor.h +++ b/contrib/llvm-project/clang/lib/Interpreter/IncrementalExecutor.h @@ -35,12 +35,16 @@ class IncrementalExecutor {    llvm::orc::ThreadSafeContext &TSCtx;  public: +  enum SymbolNameKind { IRName, LinkerName }; +    IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC, llvm::Error &Err,                        const llvm::Triple &Triple);    ~IncrementalExecutor();    llvm::Error addModule(std::unique_ptr<llvm::Module> M);    llvm::Error runCtors() const; +  llvm::Expected<llvm::JITTargetAddress> +  getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;  };  } // end namespace clang diff --git a/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.cpp b/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.cpp index 897e2cd1aaed..84eabc3a210f 100644 --- a/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.cpp +++ b/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.cpp @@ -65,6 +65,8 @@ public:            case frontend::ParseSyntaxOnly:              Act = CreateFrontendAction(CI);              break; +          case frontend::PluginAction: +            LLVM_FALLTHROUGH;            case frontend::EmitAssembly:              LLVM_FALLTHROUGH;            case frontend::EmitObj: @@ -289,4 +291,11 @@ IncrementalParser::Parse(llvm::StringRef input) {    return PTU;  } + +llvm::StringRef IncrementalParser::GetMangledName(GlobalDecl GD) const { +  CodeGenerator *CG = getCodeGen(Act.get()); +  assert(CG); +  return CG->GetMangledName(GD); +} +  } // end namespace clang diff --git a/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.h b/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.h index aa8142cbe493..e5ce798025d9 100644 --- a/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.h +++ b/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.h @@ -15,6 +15,8 @@  #include "clang/Interpreter/PartialTranslationUnit.h" +#include "clang/AST/GlobalDecl.h" +  #include "llvm/ADT/ArrayRef.h"  #include "llvm/ADT/StringRef.h"  #include "llvm/Support/Error.h" @@ -69,6 +71,10 @@ public:    /// \c TranslationUnitDecl and \c llvm::Module corresponding to the input.    llvm::Expected<PartialTranslationUnit &> Parse(llvm::StringRef Input); +  /// Uses the CodeGenModule mangled name cache and avoids recomputing. +  ///\returns the mangled name of a \c GD. +  llvm::StringRef GetMangledName(GlobalDecl GD) const; +  private:    llvm::Expected<PartialTranslationUnit &> ParseOrWrapTopLevelDecl();  }; diff --git a/contrib/llvm-project/clang/lib/Interpreter/Interpreter.cpp b/contrib/llvm-project/clang/lib/Interpreter/Interpreter.cpp index 937504f34739..b2e7727be39a 100644 --- a/contrib/llvm-project/clang/lib/Interpreter/Interpreter.cpp +++ b/contrib/llvm-project/clang/lib/Interpreter/Interpreter.cpp @@ -30,13 +30,13 @@  #include "clang/Lex/PreprocessorOptions.h"  #include "llvm/IR/Module.h" +#include "llvm/Support/Errc.h"  #include "llvm/Support/Host.h"  using namespace clang;  // FIXME: Figure out how to unify with namespace init_convenience from -//        tools/clang-import-test/clang-import-test.cpp and -//        examples/clang-interpreter/main.cpp +//        tools/clang-import-test/clang-import-test.cpp  namespace {  /// Retrieves the clang CC1 specific flags out of the compilation's jobs.  /// \returns NULL on error. @@ -47,14 +47,14 @@ GetCC1Arguments(DiagnosticsEngine *Diagnostics,    // failed. Extract that job from the Compilation.    const driver::JobList &Jobs = Compilation->getJobs();    if (!Jobs.size() || !isa<driver::Command>(*Jobs.begin())) -    return llvm::createStringError(std::errc::state_not_recoverable, +    return llvm::createStringError(llvm::errc::not_supported,                                     "Driver initialization failed. "                                     "Unable to create a driver job");    // The one job we find should be to invoke clang again.    const driver::Command *Cmd = cast<driver::Command>(&(*Jobs.begin()));    if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") -    return llvm::createStringError(std::errc::state_not_recoverable, +    return llvm::createStringError(llvm::errc::not_supported,                                     "Driver initialization failed");    return &Cmd->getArguments(); @@ -89,13 +89,13 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {    // Create the actual diagnostics engine.    Clang->createDiagnostics();    if (!Clang->hasDiagnostics()) -    return llvm::createStringError(std::errc::state_not_recoverable, +    return llvm::createStringError(llvm::errc::not_supported,                                     "Initialization failed. "                                     "Unable to create diagnostics engine");    DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());    if (!Success) -    return llvm::createStringError(std::errc::state_not_recoverable, +    return llvm::createStringError(llvm::errc::not_supported,                                     "Initialization failed. "                                     "Unable to flush diagnostics"); @@ -106,12 +106,16 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {    Clang->setTarget(TargetInfo::CreateTargetInfo(        Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));    if (!Clang->hasTarget()) -    return llvm::createStringError(std::errc::state_not_recoverable, +    return llvm::createStringError(llvm::errc::not_supported,                                     "Initialization failed. "                                     "Target is missing");    Clang->getTarget().adjust(Clang->getDiagnostics(), Clang->getLangOpts()); +  // Don't clear the AST before backend codegen since we do codegen multiple +  // times, reusing the same AST. +  Clang->getCodeGenOpts().ClearASTBeforeBackend = false; +    return std::move(Clang);  } @@ -143,19 +147,13 @@ IncrementalCompilerBuilder::create(std::vector<const char *> &ClangArgv) {    // driver to construct.    ClangArgv.push_back("<<< inputs >>>"); -  CompilerInvocation Invocation;    // Buffer diagnostics from argument parsing so that we can output them using a    // well formed diagnostic object.    IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); -  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); +  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = +      CreateAndPopulateDiagOpts(ClangArgv);    TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;    DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer); -  unsigned MissingArgIndex, MissingArgCount; -  const llvm::opt::OptTable &Opts = driver::getDriverOptTable(); -  llvm::opt::InputArgList ParsedArgs = -      Opts.ParseArgs(ArrayRef<const char *>(ClangArgv).slice(1), -                     MissingArgIndex, MissingArgCount); -  ParseDiagnosticArgs(*DiagOpts, ParsedArgs, &Diags);    driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0],                          llvm::sys::getProcessTriple(), Diags); @@ -223,3 +221,33 @@ llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {    return llvm::Error::success();  } + +llvm::Expected<llvm::JITTargetAddress> +Interpreter::getSymbolAddress(GlobalDecl GD) const { +  if (!IncrExecutor) +    return llvm::make_error<llvm::StringError>("Operation failed. " +                                               "No execution engine", +                                               std::error_code()); +  llvm::StringRef MangledName = IncrParser->GetMangledName(GD); +  return getSymbolAddress(MangledName); +} + +llvm::Expected<llvm::JITTargetAddress> +Interpreter::getSymbolAddress(llvm::StringRef IRName) const { +  if (!IncrExecutor) +    return llvm::make_error<llvm::StringError>("Operation failed. " +                                               "No execution engine", +                                               std::error_code()); + +  return IncrExecutor->getSymbolAddress(IRName, IncrementalExecutor::IRName); +} + +llvm::Expected<llvm::JITTargetAddress> +Interpreter::getSymbolAddressFromLinkerName(llvm::StringRef Name) const { +  if (!IncrExecutor) +    return llvm::make_error<llvm::StringError>("Operation failed. " +                                               "No execution engine", +                                               std::error_code()); + +  return IncrExecutor->getSymbolAddress(Name, IncrementalExecutor::LinkerName); +}  | 
