Skip to content

[clang] WIP: Improved Context Declaration tracking #107942

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 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions benchmark.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
stage1-O3:

Benchmark Old New
kimwitu++ 42984M 43445M (+1.07%)
sqlite3 38976M 39006M (+0.08%)
consumer-typeset 35207M 35272M (+0.18%)
Bullet 104230M 105079M (+0.82%)
tramp3d-v4 87692M 87953M (+0.30%)
mafft 36675M 36746M (+0.19%)
ClamAV 55998M 56096M (+0.17%)
lencod 67627M 67727M (+0.15%)
SPASS 47456M 47609M (+0.32%)
7zip 214160M 217285M (+1.46%)
geomean 61396M 61686M (+0.47%)
stage1-ReleaseThinLTO:

Benchmark Old New
kimwitu++ 53856M 54341M (+0.90%)
sqlite3 51098M 51104M (+0.01%)
consumer-typeset 49397M 49473M (+0.15%)
Bullet 98328M 99182M (+0.87%)
tramp3d-v4 142518M 142862M (+0.24%)
mafft 31630M 31701M (+0.22%)
ClamAV 74595M 74691M (+0.13%)
lencod 116718M 116741M (+0.02%)
SPASS 66986M 67120M (+0.20%)
7zip 266444M 269535M (+1.16%)
geomean 78912M 79220M (+0.39%)
stage1-ReleaseLTO-g:

Benchmark Old New
kimwitu++ 58953M 59413M (+0.78%)
sqlite3 62117M 62104M (-0.02%)
consumer-typeset 55231M 55311M (+0.15%)
Bullet 111542M 112397M (+0.77%)
tramp3d-v4 171205M 171520M (+0.18%)
mafft 39383M 39452M (+0.17%)
ClamAV 86874M 86962M (+0.10%)
lencod 134904M 135012M (+0.08%)
SPASS 78556M 78680M (+0.16%)
7zip 273629M 276683M (+1.12%)
geomean 90854M 91170M (+0.35%)
stage1-O0-g:

Benchmark Old New
kimwitu++ 24880M 25346M (+1.87%)
sqlite3 4819M 4831M (+0.25%)
consumer-typeset 12581M 12648M (+0.54%)
Bullet 64850M 65658M (+1.25%)
tramp3d-v4 21204M 21503M (+1.41%)
mafft 6744M 6812M (+1.02%)
ClamAV 13666M 13766M (+0.73%)
lencod 12768M 12861M (+0.72%)
SPASS 14417M 14560M (+0.99%)
7zip 142702M 145480M (+1.95%)
geomean 18624M 18824M (+1.07%)
stage2-O3:

Benchmark Old New
kimwitu++ 38237M 38648M (+1.07%)
sqlite3 34510M 34521M (+0.03%)
consumer-typeset 31500M 31555M (+0.17%)
Bullet 91970M 92608M (+0.69%)
tramp3d-v4 77162M 77411M (+0.32%)
mafft 32367M 32426M (+0.18%)
ClamAV 49676M 49761M (+0.17%)
lencod 60029M 60110M (+0.14%)
SPASS 42203M 42319M (+0.27%)
7zip 189874M 192365M (+1.31%)
geomean 54428M 54665M (+0.44%)
stage2-O0-g:

Benchmark Old New
kimwitu++ 21908M 22281M (+1.71%)
sqlite3 4153M 4161M (+0.19%)
consumer-typeset 11226M 11286M (+0.53%)
Bullet 56719M 57407M (+1.21%)
tramp3d-v4 18519M 18754M (+1.27%)
mafft 5971M 6031M (+1.00%)
ClamAV 12197M 12284M (+0.71%)
lencod 11286M 11366M (+0.71%)
SPASS 12839M 12969M (+1.01%)
7zip 126866M 129246M (+1.88%)
geomean 16433M 16601M (+1.02%)
clang build:

Metric Old New
instructions:u 32641635M 33228307M (+1.80%)
wall-time 558.38s 568.45s (+1.80%)
size-file 130961KiB 130988KiB (+0.02%)
size-file (stage1) 131043KiB 131064KiB (+0.02%)
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ namespace nb {
void f(function<void(int)> func, int param) { func(param); }
void g() { f([](int x) {}, 1); }

// x::X in function type parameter list will have translation unit context, so
// we simply replace it with fully-qualified name.
using TX = function<x::X(x::X)>;
// CHECK: using TX = function<X(x::X)>;
// CHECK: using TX = function<X(X)>;

class A {};
using TA = function<A(A)>;
Expand Down
11 changes: 0 additions & 11 deletions clang/include/clang/AST/ASTLambda.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,6 @@ inline bool isGenericLambdaCallOperatorSpecialization(DeclContext *DC) {
dyn_cast<CXXMethodDecl>(DC));
}

inline bool isGenericLambdaCallOperatorOrStaticInvokerSpecialization(
const DeclContext *DC) {
const auto *MD = dyn_cast<CXXMethodDecl>(DC);
if (!MD) return false;
const CXXRecordDecl *LambdaClass = MD->getParent();
if (LambdaClass && LambdaClass->isGenericLambda())
return (isLambdaCallOperator(MD) || MD->isLambdaStaticInvoker()) &&
MD->isFunctionTemplateSpecialization();
return false;
}

// This returns the parent DeclContext ensuring that the correct
// parent DeclContext is returned for Lambdas
inline DeclContext *getLambdaAwareParentOfDeclContext(DeclContext *DC) {
Expand Down
31 changes: 28 additions & 3 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,11 @@ class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
LLVM_PREFERRED_TYPE(bool)
unsigned IsObjCMethodParam : 1;

/// Whether this parameter was initially created in a context with a
/// different template depth.
LLVM_PREFERRED_TYPE(bool)
unsigned HasLazyTemplateDepth : 1;

/// If IsObjCMethodParam, a Decl::ObjCDeclQualifier.
/// Otherwise, the number of function parameter scopes enclosing
/// the function parameter scope in which this parameter was
Expand Down Expand Up @@ -1719,29 +1724,38 @@ class ImplicitParamDecl : public VarDecl {
};

/// Represents a parameter to a function.
class ParmVarDecl : public VarDecl {
class ParmVarDecl final : public VarDecl,
private llvm::TrailingObjects<ParmVarDecl, uint16_t> {
public:
enum { MaxFunctionScopeDepth = 255 };
enum { MaxFunctionScopeIndex = 255 };

protected:
ParmVarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg)
TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg,
std::optional<unsigned> TemplateDepth)
: VarDecl(DK, C, DC, StartLoc, IdLoc, Id, T, TInfo, S) {
assert(ParmVarDeclBits.HasInheritedDefaultArg == false);
assert(ParmVarDeclBits.DefaultArgKind == DAK_None);
assert(ParmVarDeclBits.IsKNRPromoted == false);
assert(ParmVarDeclBits.IsObjCMethodParam == false);
setDefaultArg(DefArg);
ParmVarDeclBits.HasLazyTemplateDepth = bool(TemplateDepth);
if (TemplateDepth) {
assert(*TemplateDepth !=
Decl::castFromDeclContext(DC)->getTemplateDepth());
*getTrailingObjects<uint16_t>() = *TemplateDepth;
}
}

public:
static ParmVarDecl *Create(ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, StorageClass S,
Expr *DefArg);
Expr *DefArg,
std::optional<unsigned> TemplateDepth);

static ParmVarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);

Expand Down Expand Up @@ -1872,6 +1886,12 @@ class ParmVarDecl : public VarDecl {
ParmVarDeclBits.HasInheritedDefaultArg = I;
}

unsigned getTemplateDepth() const {
if (numTrailingObjects(OverloadToken<uint16_t>()))
return *getTrailingObjects<uint16_t>();
return Decl::castFromDeclContext(getDeclContext())->getTemplateDepth();
}

QualType getOriginalType() const;

/// Sets the function declaration that owns this
Expand All @@ -1885,11 +1905,16 @@ class ParmVarDecl : public VarDecl {
static bool classofKind(Kind K) { return K == ParmVar; }

private:
friend TrailingObjects;
friend class ASTDeclReader;

enum { ParameterIndexSentinel = (1 << NumParameterIndexBits) - 1 };
SourceLocation ExplicitObjectParameterIntroducerLoc;

size_t numTrailingObjects(OverloadToken<uint16_t>) const {
return ParmVarDeclBits.HasLazyTemplateDepth;
}

void setParameterIndex(unsigned parameterIndex) {
if (parameterIndex >= ParameterIndexSentinel) {
setParameterIndexLarge(parameterIndex);
Expand Down
16 changes: 15 additions & 1 deletion clang/include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,8 @@ class alignas(8) Decl {

/// Determine the number of levels of template parameter surrounding this
/// declaration.
unsigned getTemplateDepth() const;
unsigned
getTemplateDepth(ArrayRef<TemplateArgument> SpecArgs = std::nullopt) const;

/// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
/// scoped decl is defined outside the current function or method. This is
Expand Down Expand Up @@ -1995,6 +1996,16 @@ class DeclContext {
uint64_t CanAvoidCopyToHeap : 1;
};

class RequiresExprBodyDeclBitfields {
friend class RequiresExprBodyDecl;
/// For the bits in DeclContextBitfields.
LLVM_PREFERRED_TYPE(DeclContextBitfields)
uint64_t : NumDeclContextBits;

LLVM_PREFERRED_TYPE(unsigned)
uint64_t NumContextArgsOrNoContext : 20;
};

/// Number of inherited and non-inherited bits in BlockDeclBitfields.
enum { NumBlockDeclBits = NumDeclContextBits + 5 };

Expand Down Expand Up @@ -2028,6 +2039,7 @@ class DeclContext {
ObjCContainerDeclBitfields ObjCContainerDeclBits;
LinkageSpecDeclBitfields LinkageSpecDeclBits;
BlockDeclBitfields BlockDeclBits;
RequiresExprBodyDeclBitfields RequiresExprBodyDeclBits;

static_assert(sizeof(DeclContextBitfields) <= 8,
"DeclContextBitfields is larger than 8 bytes!");
Expand All @@ -2053,6 +2065,8 @@ class DeclContext {
"LinkageSpecDeclBitfields is larger than 8 bytes!");
static_assert(sizeof(BlockDeclBitfields) <= 8,
"BlockDeclBitfields is larger than 8 bytes!");
static_assert(sizeof(RequiresExprBodyDeclBitfields) <= 8,
"RequiresExprBodyDeclBitfields is larger than 8 bytes!");
};

/// FirstDecl - The first declaration stored within this declaration
Expand Down
Loading