Skip to content

Commit e56ae96

Browse files
authored
[CIR][NFC] Add Symbol Table to CIRGenFunction (#153625)
This patchs adds a symbol table to CIRGenFunction plus scopes and insertions to the table where we were missing them previously.
1 parent 1e9fc8e commit e56ae96

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ struct MissingFeatures {
2727
// Address space related
2828
static bool addressSpace() { return false; }
2929

30-
// CIRGenFunction implementation details
31-
static bool cgfSymbolTable() { return false; }
32-
3330
// Unhandled global/linkage information.
3431
static bool opGlobalThreadLocal() { return false; }
3532
static bool opGlobalConstant() { return false; }

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ LValue CIRGenFunction::emitDeclRefLValue(const DeclRefExpr *e) {
591591
? emitLoadOfReferenceLValue(addr, getLoc(e->getSourceRange()),
592592
vd->getType(), AlignmentSource::Decl)
593593
: makeAddrLValue(addr, ty, AlignmentSource::Decl);
594+
595+
// Statics are defined as globals, so they are not include in the function's
596+
// symbol table.
597+
assert((vd->isStaticLocal() || symbolTable.count(vd)) &&
598+
"non-static locals should be already mapped");
599+
594600
return lv;
595601
}
596602

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void CIRGenFunction::declare(mlir::Value addrVal, const Decl *var, QualType ty,
215215
mlir::Location loc, CharUnits alignment,
216216
bool isParam) {
217217
assert(isa<NamedDecl>(var) && "Needs a named decl");
218-
assert(!cir::MissingFeatures::cgfSymbolTable());
218+
assert(!symbolTable.count(var) && "not supposed to be available just yet");
219219

220220
auto allocaOp = addrVal.getDefiningOp<cir::AllocaOp>();
221221
assert(allocaOp && "expected cir::AllocaOp");
@@ -224,6 +224,8 @@ void CIRGenFunction::declare(mlir::Value addrVal, const Decl *var, QualType ty,
224224
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
225225
if (ty->isReferenceType() || ty.isConstQualified())
226226
allocaOp.setConstantAttr(mlir::UnitAttr::get(&getMLIRContext()));
227+
228+
symbolTable.insert(var, allocaOp);
227229
}
228230

229231
void CIRGenFunction::LexicalScope::cleanup() {
@@ -485,6 +487,9 @@ void CIRGenFunction::finishFunction(SourceLocation endLoc) {
485487
}
486488

487489
mlir::LogicalResult CIRGenFunction::emitFunctionBody(const clang::Stmt *body) {
490+
// We start with function level scope for variables.
491+
SymTableScopeTy varScope(symbolTable);
492+
488493
auto result = mlir::LogicalResult::success();
489494
if (const CompoundStmt *block = dyn_cast<CompoundStmt>(body))
490495
emitCompoundStmtWithoutScope(*block);
@@ -531,6 +536,8 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,
531536
FunctionArgList args;
532537
QualType retTy = buildFunctionArgList(gd, args);
533538

539+
// Create a scope in the symbol table to hold variable declarations.
540+
SymTableScopeTy varScope(symbolTable);
534541
{
535542
LexicalScope lexScope(*this, fusedLoc, entryBB);
536543

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "clang/CIR/Dialect/IR/CIRDialect.h"
3232
#include "clang/CIR/MissingFeatures.h"
3333
#include "clang/CIR/TypeEvaluationKind.h"
34+
#include "llvm/ADT/ScopedHashTable.h"
3435

3536
namespace {
3637
class ScalarExprEmitter;
@@ -103,6 +104,14 @@ class CIRGenFunction : public CIRGenTypeCache {
103104
/// Sanitizers enabled for this function.
104105
clang::SanitizerSet sanOpts;
105106

107+
/// The symbol table maps a variable name to a value in the current scope.
108+
/// Entering a function creates a new scope, and the function arguments are
109+
/// added to the mapping. When the processing of a function is terminated,
110+
/// the scope is destroyed and the mappings created in this scope are
111+
/// dropped.
112+
using SymTableTy = llvm::ScopedHashTable<const clang::Decl *, mlir::Value>;
113+
SymTableTy symbolTable;
114+
106115
/// Whether or not a Microsoft-style asm block has been processed within
107116
/// this fuction. These can potentially set the return value.
108117
bool sawAsmBlock = false;
@@ -325,6 +334,9 @@ class CIRGenFunction : public CIRGenTypeCache {
325334
~SourceLocRAIIObject() { restore(); }
326335
};
327336

337+
using SymTableScopeTy =
338+
llvm::ScopedHashTableScope<const clang::Decl *, mlir::Value>;
339+
328340
/// Hold counters for incrementally naming temporaries
329341
unsigned counterRefTmp = 0;
330342
unsigned counterAggTmp = 0;
@@ -499,7 +511,11 @@ class CIRGenFunction : public CIRGenTypeCache {
499511
void setAddrOfLocalVar(const clang::VarDecl *vd, Address addr) {
500512
assert(!localDeclMap.count(vd) && "Decl already exists in LocalDeclMap!");
501513
localDeclMap.insert({vd, addr});
502-
// TODO: Add symbol table support
514+
515+
// Add to the symbol table if not there already.
516+
if (symbolTable.count(vd))
517+
return;
518+
symbolTable.insert(vd, addr.getPointer());
503519
}
504520

505521
bool shouldNullCheckClassCastValue(const CastExpr *ce);

clang/lib/CIR/CodeGen/CIRGenStmt.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ void CIRGenFunction::emitCompoundStmtWithoutScope(const CompoundStmt &s) {
3333
}
3434

3535
void CIRGenFunction::emitCompoundStmt(const CompoundStmt &s) {
36+
// Add local scope to track new declared variables.
37+
SymTableScopeTy varScope(symbolTable);
3638
mlir::Location scopeLoc = getLoc(s.getSourceRange());
3739
mlir::OpBuilder::InsertPoint scopeInsPt;
3840
builder.create<cir::ScopeOp>(

0 commit comments

Comments
 (0)