Skip to content

Commit

Permalink
Implement first effective use in SymbolExternalizer
Browse files Browse the repository at this point in the history
Instead of externalizing to the first use, externalize now to the
first effective use, that means the first decl that won't be discarded
by the closure.

Signed-off-by: Giuliano Belinassi <[email protected]>
  • Loading branch information
giulianobelinassi committed Jul 12, 2024
1 parent 1357aa4 commit ec3894f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
8 changes: 4 additions & 4 deletions libcextract/Closure.hh
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ class ClosureSet
class DeclClosureVisitor : public RecursiveASTVisitor<DeclClosureVisitor>
{
public:
DeclClosureVisitor(ASTUnit *ast, EnumConstantTable &table)
DeclClosureVisitor(ASTUnit *ast)
: RecursiveASTVisitor(),
AST(ast),
EnumTable(table)
EnumTable(ast)
{
}

Expand Down Expand Up @@ -520,7 +520,7 @@ class DeclClosureVisitor : public RecursiveASTVisitor<DeclClosureVisitor>
std::unordered_set<std::string> *matched_names = nullptr);

private:

/** The ASTUnit object. */
ASTUnit *AST;

Expand All @@ -533,7 +533,7 @@ class DeclClosureVisitor : public RecursiveASTVisitor<DeclClosureVisitor>

/** The table maping EnumConstantDecl to its original EnumDecl, used to find
out where a certain EnumConstantDecl was defined. */
EnumConstantTable &EnumTable;
EnumConstantTable EnumTable;

/** The set of all analyzed Decls. */
std::unordered_set<Decl *> AnalyzedDecls;
Expand Down
3 changes: 1 addition & 2 deletions libcextract/FunctionDepsFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
/** FunctionDependencyFinder class methods implementation. */
FunctionDependencyFinder::FunctionDependencyFinder(PassManager::Context *ctx)
: AST(ctx->AST.get()),
EnumTable(AST),
IT(AST, ctx->IncExpansionPolicy, ctx->HeadersToExpand),
KeepIncludes(ctx->KeepIncludes),
Visitor(AST, EnumTable)
Visitor(AST)
{
}

Expand Down
4 changes: 0 additions & 4 deletions libcextract/FunctionDepsFinder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ class FunctionDependencyFinder
/** The AST that are being used in our analysis. */
ASTUnit* AST;

/** Object holding information about constant enum symbols and a mapping to
they original enum object. */
EnumConstantTable EnumTable;

/* Tree of #includes. */
IncludeTree IT;

Expand Down
4 changes: 3 additions & 1 deletion libcextract/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,9 @@ class FunctionExternalizerPass : public Pass
/* Issue externalization. */
SymbolExternalizer externalizer(ctx->AST.get(), ctx->IA, ctx->Ibt,
ctx->AllowLateExternalizations,
ctx->PatchObject, ctx->DumpPasses);
ctx->PatchObject,
ctx->FuncExtractNames,
ctx->DumpPasses);
if (ctx->RenameSymbols)
/* The FuncExtractNames will be modified, as the function will be renamed. */
externalizer.Externalize_Symbols(ctx->Externalize, ctx->FuncExtractNames);
Expand Down
22 changes: 20 additions & 2 deletions libcextract/SymbolExternalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ClangCompat.hh"
#include "LLVMMisc.hh"
#include "IntervalTree.hh"
#include "Closure.hh"

#include <unordered_set>
#include <iostream>
Expand Down Expand Up @@ -205,9 +206,20 @@ class ExternalizerVisitor: public RecursiveASTVisitor<ExternalizerVisitor>
if (sym == nullptr || !sym->Done)
return VISITOR_CONTINUE;

/* If this is the first use of the symbol, then remember it. */
/* Get the first effective use, which means an DeclRefExpr of a decl that
will not be removed by the Closure. */
if (sym->FirstUse == nullptr) {
sym->FirstUse = expr;
ClosureSet &closure = SE.ClosureVisitor.Get_Closure();
ASTUnit *ast = SE.AST;
SourceManager &sm = ast->getSourceManager();
SourceLocation loc = sm.getExpansionLoc(expr->getLocation());
Decl *topdecl = Get_Toplevel_Decl_At_Location(ast, loc);

/* If the declaration is reachable from the functions we want to extract,
then we mark it as FirstUse. */
if (closure.Is_Decl_Marked(topdecl)) {
sym->FirstUse = expr;
}
}

/* We must be careful to ensure that the reference we got is actually
Expand Down Expand Up @@ -787,6 +799,12 @@ void SymbolExternalizer::Compute_SymbolsMap_Late_Insert_Locations(std::vector<Sy
for (auto array_it = array.begin(); array_it != array.end(); ++array_it) {
SymbolUpdateStatus *sym = *array_it;
DeclRefExpr *first_use = sym->FirstUse;

/* In case FirstUse is nullptr, then there is nothing to externalize
because it will be removed by the ClosurePass. */
if (first_use == nullptr)
continue;

SourceLocation loc_1stuse = SM.getExpansionLoc(first_use->getLocation());

Decl *topdecl = Get_Toplevel_Decl_At_Location(AST, loc_1stuse);
Expand Down
11 changes: 9 additions & 2 deletions libcextract/SymbolExternalizer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "MacroWalker.hh"
#include "InlineAnalysis.hh"
#include "Closure.hh"

#include <clang/Tooling/Tooling.h>
#include <clang/Rewrite/Core/Rewriter.h>
Expand Down Expand Up @@ -96,7 +97,7 @@ struct SymbolUpdateStatus
attempt of externalize an symbol which was removed by the ClosurePass. */
inline bool Is_Used(void)
{
return OldDecl && NewDecl;
return OldDecl && NewDecl && FirstUse;
}

void Dump(SourceManager &SM)
Expand Down Expand Up @@ -284,6 +285,7 @@ class SymbolExternalizer
public:
SymbolExternalizer(ASTUnit *ast, InlineAnalysis &ia, bool ibt,
bool allow_late_externalize, std::string patch_object,
const std::vector<std::string> &functions_to_extract,
bool dump = false)
: AST(ast),
MW(ast->getPreprocessor()),
Expand All @@ -292,8 +294,10 @@ class SymbolExternalizer
Ibt(ibt),
AllowLateExternalization(allow_late_externalize),
PatchObject(patch_object),
SymbolsMap({})
SymbolsMap({}),
ClosureVisitor(ast)
{
ClosureVisitor.Compute_Closure_Of_Symbols(functions_to_extract);
}

friend class ExternalizerVisitor;
Expand Down Expand Up @@ -387,4 +391,7 @@ class SymbolExternalizer

/** Symbols and its externalization type */
llvm::StringMap<SymbolUpdateStatus> SymbolsMap;

/* ClosureVisitor to compute the closure. */
DeclClosureVisitor ClosureVisitor;
};

0 comments on commit ec3894f

Please sign in to comment.