diff --git a/clang-tools-extra/clangd/CMakeLists.txt b/clang-tools-extra/clangd/CMakeLists.txt --- a/clang-tools-extra/clangd/CMakeLists.txt +++ b/clang-tools-extra/clangd/CMakeLists.txt @@ -18,6 +18,7 @@ unset(CLANGD_BUILD_XPC_DEFAULT) endif () +option(CLANGD_DECISION_FOREST "Enable decision forest model for ranking code completion items" ON) option(CLANGD_MALLOC_TRIM "Call malloc_trim(3) periodically in Clangd. (only takes effect when using glibc)" ON) # -DCLANG_TIDY_CHECKS=Off avoids a dependency on clang-tidy, reducing rebuilds. option(CLANGD_TIDY_CHECKS "Link all clang-tidy checks into clangd" ON) @@ -29,6 +30,7 @@ CLANGD_MALLOC_TRIM CLANGD_TIDY_CHECKS LLVM_ENABLE_ZLIB + CLANGD_DECISION_FOREST ) configure_file( @@ -43,8 +45,16 @@ Option ) -include(${CMAKE_CURRENT_SOURCE_DIR}/quality/CompletionModel.cmake) -gen_decision_forest(${CMAKE_CURRENT_SOURCE_DIR}/quality/model CompletionModel clang::clangd::Example) +set(COMPLETIONMODEL_SOURCES) +if(CLANGD_DECISION_FOREST) + include(${CMAKE_CURRENT_SOURCE_DIR}/quality/CompletionModel.cmake) + gen_decision_forest(${CMAKE_CURRENT_SOURCE_DIR}/quality/model CompletionModel clang::clangd::Example) + + list(APPEND COMPLETIONMODEL_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/CompletionModel.cpp) + list(APPEND COMPLETIONMODEL_SOURCES decision-forest/DecisionForest.cpp) +else() + list(APPEND COMPLETIONMODEL_SOURCES decision-forest/DecisionForest_disabled.cpp) +endif() if(MSVC AND NOT CLANG_CL) set_source_files_properties(CompileCommands.cpp PROPERTIES COMPILE_FLAGS -wd4130) # disables C4130: logical operation on address of string constant @@ -102,7 +112,7 @@ TUScheduler.cpp URI.cpp XRefs.cpp - ${CMAKE_CURRENT_BINARY_DIR}/CompletionModel.cpp + ${COMPLETIONMODEL_SOURCES} index/Background.cpp index/BackgroundIndexLoader.cpp diff --git a/clang-tools-extra/clangd/Features.inc.in b/clang-tools-extra/clangd/Features.inc.in --- a/clang-tools-extra/clangd/Features.inc.in +++ b/clang-tools-extra/clangd/Features.inc.in @@ -4,3 +4,4 @@ #define ENABLE_GRPC_REFLECTION @ENABLE_GRPC_REFLECTION@ #define CLANGD_MALLOC_TRIM @CLANGD_MALLOC_TRIM@ #define CLANGD_TIDY_CHECKS @CLANGD_TIDY_CHECKS@ +#define CLANGD_DECISION_FOREST @CLANGD_DECISION_FOREST@ diff --git a/clang-tools-extra/clangd/Quality.cpp b/clang-tools-extra/clangd/Quality.cpp --- a/clang-tools-extra/clangd/Quality.cpp +++ b/clang-tools-extra/clangd/Quality.cpp @@ -9,7 +9,6 @@ #include "Quality.h" #include "AST.h" #include "ASTSignals.h" -#include "CompletionModel.h" #include "FileDistance.h" #include "SourceCode.h" #include "index/Symbol.h" @@ -529,65 +528,6 @@ return SymbolQuality * SymbolRelevance; } -DecisionForestScores -evaluateDecisionForest(const SymbolQualitySignals &Quality, - const SymbolRelevanceSignals &Relevance, float Base) { - Example E; - E.setIsDeprecated(Quality.Deprecated); - E.setIsReservedName(Quality.ReservedName); - E.setIsImplementationDetail(Quality.ImplementationDetail); - E.setNumReferences(Quality.References); - E.setSymbolCategory(Quality.Category); - - SymbolRelevanceSignals::DerivedSignals Derived = - Relevance.calculateDerivedSignals(); - int NumMatch = 0; - if (Relevance.ContextWords) { - for (const auto &Word : Relevance.ContextWords->keys()) { - if (Relevance.Name.contains_insensitive(Word)) { - ++NumMatch; - } - } - } - E.setIsNameInContext(NumMatch > 0); - E.setNumNameInContext(NumMatch); - E.setFractionNameInContext( - Relevance.ContextWords && !Relevance.ContextWords->empty() - ? NumMatch * 1.0 / Relevance.ContextWords->size() - : 0); - E.setIsInBaseClass(Relevance.InBaseClass); - E.setFileProximityDistanceCost(Derived.FileProximityDistance); - E.setSemaFileProximityScore(Relevance.SemaFileProximityScore); - E.setSymbolScopeDistanceCost(Derived.ScopeProximityDistance); - E.setSemaSaysInScope(Relevance.SemaSaysInScope); - E.setScope(Relevance.Scope); - E.setContextKind(Relevance.Context); - E.setIsInstanceMember(Relevance.IsInstanceMember); - E.setHadContextType(Relevance.HadContextType); - E.setHadSymbolType(Relevance.HadSymbolType); - E.setTypeMatchesPreferred(Relevance.TypeMatchesPreferred); - - DecisionForestScores Scores; - // Exponentiating DecisionForest prediction makes the score of each tree a - // multiplciative boost (like NameMatch). This allows us to weigh the - // prediction score and NameMatch appropriately. - Scores.ExcludingName = pow(Base, Evaluate(E)); - // Following cases are not part of the generated training dataset: - // - Symbols with `NeedsFixIts`. - // - Forbidden symbols. - // - Keywords: Dataset contains only macros and decls. - if (Relevance.NeedsFixIts) - Scores.ExcludingName *= 0.5; - if (Relevance.Forbidden) - Scores.ExcludingName *= 0; - if (Quality.Category == SymbolQualitySignals::Keyword) - Scores.ExcludingName *= 4; - - // NameMatch should be a multiplier on total score to support rescoring. - Scores.Total = Relevance.NameMatch * Scores.ExcludingName; - return Scores; -} - // Produces an integer that sorts in the same order as F. // That is: a < b <==> encodeFloat(a) < encodeFloat(b). static uint32_t encodeFloat(float F) { diff --git a/clang-tools-extra/clangd/benchmarks/CMakeLists.txt b/clang-tools-extra/clangd/benchmarks/CMakeLists.txt --- a/clang-tools-extra/clangd/benchmarks/CMakeLists.txt +++ b/clang-tools-extra/clangd/benchmarks/CMakeLists.txt @@ -1,4 +1,6 @@ -add_subdirectory(CompletionModel) +if(CLANGD_DECISION_FOREST) + add_subdirectory(CompletionModel) +endif() add_benchmark(IndexBenchmark IndexBenchmark.cpp) diff --git a/clang-tools-extra/clangd/decision-forest/DecisionForest.cpp b/clang-tools-extra/clangd/decision-forest/DecisionForest.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clangd/decision-forest/DecisionForest.cpp @@ -0,0 +1,76 @@ +//===--- DecisionForest.cpp --------------------------------------*- C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// Implementation of evaluateDecisionForest with completion model enabled. +//===----------------------------------------------------------------------===// + +#include "CompletionModel.h" +#include "Quality.h" +#include + +namespace clang::clangd { + +DecisionForestScores +evaluateDecisionForest(const SymbolQualitySignals &Quality, + const SymbolRelevanceSignals &Relevance, float Base) { + Example E; + E.setIsDeprecated(Quality.Deprecated); + E.setIsReservedName(Quality.ReservedName); + E.setIsImplementationDetail(Quality.ImplementationDetail); + E.setNumReferences(Quality.References); + E.setSymbolCategory(Quality.Category); + + SymbolRelevanceSignals::DerivedSignals Derived = + Relevance.calculateDerivedSignals(); + int NumMatch = 0; + if (Relevance.ContextWords) { + for (const auto &Word : Relevance.ContextWords->keys()) { + if (Relevance.Name.contains_insensitive(Word)) { + ++NumMatch; + } + } + } + E.setIsNameInContext(NumMatch > 0); + E.setNumNameInContext(NumMatch); + E.setFractionNameInContext( + Relevance.ContextWords && !Relevance.ContextWords->empty() + ? NumMatch * 1.0 / Relevance.ContextWords->size() + : 0); + E.setIsInBaseClass(Relevance.InBaseClass); + E.setFileProximityDistanceCost(Derived.FileProximityDistance); + E.setSemaFileProximityScore(Relevance.SemaFileProximityScore); + E.setSymbolScopeDistanceCost(Derived.ScopeProximityDistance); + E.setSemaSaysInScope(Relevance.SemaSaysInScope); + E.setScope(Relevance.Scope); + E.setContextKind(Relevance.Context); + E.setIsInstanceMember(Relevance.IsInstanceMember); + E.setHadContextType(Relevance.HadContextType); + E.setHadSymbolType(Relevance.HadSymbolType); + E.setTypeMatchesPreferred(Relevance.TypeMatchesPreferred); + + DecisionForestScores Scores; + // Exponentiating DecisionForest prediction makes the score of each tree a + // multiplciative boost (like NameMatch). This allows us to weigh the + // prediction score and NameMatch appropriately. + Scores.ExcludingName = pow(Base, Evaluate(E)); + // Following cases are not part of the generated training dataset: + // - Symbols with `NeedsFixIts`. + // - Forbidden symbols. + // - Keywords: Dataset contains only macros and decls. + if (Relevance.NeedsFixIts) + Scores.ExcludingName *= 0.5; + if (Relevance.Forbidden) + Scores.ExcludingName *= 0; + if (Quality.Category == SymbolQualitySignals::Keyword) + Scores.ExcludingName *= 4; + + // NameMatch should be a multiplier on total score to support rescoring. + Scores.Total = Relevance.NameMatch * Scores.ExcludingName; + return Scores; +} + +} // namespace clang::clangd diff --git a/clang-tools-extra/clangd/decision-forest/DecisionForest_disabled.cpp b/clang-tools-extra/clangd/decision-forest/DecisionForest_disabled.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clangd/decision-forest/DecisionForest_disabled.cpp @@ -0,0 +1,26 @@ +//===--- DecisionForest_disabled.cpp -----------------------------*- C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// Implementation which calls std::abort. Used when completion model is +// disabled at build time. +//===----------------------------------------------------------------------===// + +#include "Quality.h" +#include "llvm/Support/raw_ostream.h" +#include + +namespace clang { +namespace clangd { + +DecisionForestScores +evaluateDecisionForest(const SymbolQualitySignals &Quality, + const SymbolRelevanceSignals &Relevance, float Base) { + llvm::errs() << "Clangd was compiled without decision forest support.\n"; + std::abort(); +} +} // namespace clangd +} // namespace clang diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -209,11 +209,15 @@ "ranking-model", cat(Features), desc("Model to use to rank code-completion items"), - values(clEnumValN(CodeCompleteOptions::Heuristics, "heuristics", - "Use hueristics to rank code completion items"), - clEnumValN(CodeCompleteOptions::DecisionForest, "decision_forest", - "Use Decision Forest model to rank completion items")), + values(clEnumValN(CodeCompleteOptions::DecisionForest, "decision_forest", + "Use Decision Forest model to rank completion items"), + clEnumValN(CodeCompleteOptions::Heuristics, "heuristics", + "Use hueristics to rank code completion items")), +#ifdef CLANGD_DECISION_FOREST init(CodeCompleteOptions().RankingModel), +#else + init(CodeCompleteOptions::Heuristics), +#endif Hidden, }; @@ -797,6 +801,13 @@ } } +#ifndef CLANGD_DECISION_FOREST + if (RankingModel == clangd::CodeCompleteOptions::DecisionForest) { + llvm::errs() << "Clangd was compiled without decision forest support.\n"; + return 1; + } +#endif + // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a // trace flag in your editor's config is annoying, launching with // `CLANGD_TRACE=trace.json vim` is easier.