Skip to content

Commit

Permalink
[libc][TableGen] Migrate libc-hdrgen backend to use const RecordKeeper (
Browse files Browse the repository at this point in the history
llvm#107542)

Migrate libc-hdrgen backend to use const RecordKeeper
  • Loading branch information
jurahul authored Sep 7, 2024
1 parent b60c6cb commit 98563b1
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 73 deletions.
2 changes: 1 addition & 1 deletion libc/utils/HdrGen/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Command {
virtual ~Command();

virtual void run(llvm::raw_ostream &OS, const ArgVector &Args,
llvm::StringRef StdHeader, llvm::RecordKeeper &Records,
llvm::StringRef StdHeader, const llvm::RecordKeeper &Records,
const ErrorReporter &Reporter) const = 0;
};

Expand Down
15 changes: 8 additions & 7 deletions libc/utils/HdrGen/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ void Generator::parseCommandArgs(llvm::StringRef ArgStr, ArgVector &Args) {
}
}

void Generator::generate(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
void Generator::generate(llvm::raw_ostream &OS,
const llvm::RecordKeeper &Records) {
auto DefFileBuffer = llvm::MemoryBuffer::getFile(HeaderDefFile);
if (!DefFileBuffer) {
llvm::errs() << "Unable to open " << HeaderDefFile << ".\n";
Expand Down Expand Up @@ -126,7 +127,7 @@ void Generator::generate(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
}

void Generator::generateDecls(llvm::raw_ostream &OS,
llvm::RecordKeeper &Records) {
const llvm::RecordKeeper &Records) {

OS << "//===-- C standard declarations for " << StdHeader << " "
<< std::string(80 - (42 + StdHeader.size()), '-') << "===//\n"
Expand Down Expand Up @@ -161,15 +162,15 @@ void Generator::generateDecls(llvm::raw_ostream &OS,
if (G.FunctionSpecMap.find(Name) == G.FunctionSpecMap.end())
continue;

llvm::Record *FunctionSpec = G.FunctionSpecMap[Name];
llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
const llvm::Record *FunctionSpec = G.FunctionSpecMap[Name];
const llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
const llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");

OS << G.getTypeAsString(ReturnType) << " " << Name << "(";

auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args");
for (size_t i = 0; i < ArgsList.size(); ++i) {
llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
const llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
OS << G.getTypeAsString(ArgType);
if (i < ArgsList.size() - 1)
OS << ", ";
Expand All @@ -182,7 +183,7 @@ void Generator::generateDecls(llvm::raw_ostream &OS,
for (const auto &Name : EntrypointNameList) {
if (G.ObjectSpecMap.find(Name) == G.ObjectSpecMap.end())
continue;
llvm::Record *ObjectSpec = G.ObjectSpecMap[Name];
const llvm::Record *ObjectSpec = G.ObjectSpecMap[Name];
auto Type = ObjectSpec->getValueAsString("Type");
OS << "extern " << Type << " " << Name << " __LIBC_ATTRS;\n";
}
Expand Down
4 changes: 2 additions & 2 deletions libc/utils/HdrGen/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class Generator {
: HeaderDefFile(DefFile), EntrypointNameList(EN), StdHeader(Header),
ArgMap(Map) {}

void generate(llvm::raw_ostream &OS, llvm::RecordKeeper &Records);
void generateDecls(llvm::raw_ostream &OS, llvm::RecordKeeper &Records);
void generate(llvm::raw_ostream &OS, const llvm::RecordKeeper &Records);
void generateDecls(llvm::raw_ostream &OS, const llvm::RecordKeeper &Records);
};

} // namespace llvm_libc
Expand Down
2 changes: 1 addition & 1 deletion libc/utils/HdrGen/IncludeFileCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const char IncludeFileCommand::Name[] = "include_file";

void IncludeFileCommand::run(llvm::raw_ostream &OS, const ArgVector &Args,
llvm::StringRef StdHeader,
llvm::RecordKeeper &Records,
const llvm::RecordKeeper &Records,
const Command::ErrorReporter &Reporter) const {
if (Args.size() != 1) {
Reporter.printFatalError(
Expand Down
2 changes: 1 addition & 1 deletion libc/utils/HdrGen/IncludeFileCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class IncludeFileCommand : public Command {
static const char Name[];

void run(llvm::raw_ostream &OS, const ArgVector &Args,
llvm::StringRef StdHeader, llvm::RecordKeeper &Records,
llvm::StringRef StdHeader, const llvm::RecordKeeper &Records,
const Command::ErrorReporter &Reporter) const override;
};

Expand Down
3 changes: 2 additions & 1 deletion libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ llvm::cl::list<std::string>

} // anonymous namespace

bool TestGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &records) {
bool TestGeneratorMain(llvm::raw_ostream &OS,
const llvm::RecordKeeper &records) {
OS << "#include \"src/__support/CPP/type_traits.h\"\n";
llvm_libc::APIIndexer G(records);
std::unordered_set<std::string> headerFileSet;
Expand Down
41 changes: 20 additions & 21 deletions libc/utils/HdrGen/PublicAPICommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static bool isAsciiIdentifier(llvm::StringRef S) {
return true;
}

static AttributeStyle getAttributeStyle(llvm::Record *Instance) {
static AttributeStyle getAttributeStyle(const llvm::Record *Instance) {
llvm::StringRef Style = Instance->getValueAsString("Style");
return llvm::StringSwitch<AttributeStyle>(Style)
.Case("cxx11", AttributeStyle::Cxx11)
Expand All @@ -79,29 +79,28 @@ static AttributeStyle getAttributeStyle(llvm::Record *Instance) {
.Default(AttributeStyle::Gnu);
}

static AttributeNamespace getAttributeNamespace(llvm::Record *Instance) {
static AttributeNamespace getAttributeNamespace(const llvm::Record *Instance) {
llvm::StringRef Namespace = Instance->getValueAsString("Namespace");
return llvm::StringSwitch<AttributeNamespace>(Namespace)
.Case("clang", AttributeNamespace::Clang)
.Case("gnu", AttributeNamespace::Gnu)
.Default(AttributeNamespace::None);
}

using AttributeMap = llvm::DenseMap<llvm::StringRef, llvm::Record *>;
using AttributeMap = llvm::DenseMap<llvm::StringRef, const llvm::Record *>;

template <class SpecMap, class FuncList>
static AttributeMap collectAttributeMacros(const SpecMap &Spec,
const FuncList &Funcs) {
llvm::DenseMap<llvm::StringRef, llvm::Record *> MacroAttr;
llvm::DenseMap<llvm::StringRef, const llvm::Record *> MacroAttr;
for (const auto &Name : Funcs) {
auto Iter = Spec.find(Name);
if (Iter == Spec.end())
continue;

llvm::Record *FunctionSpec = Iter->second;
std::vector<llvm::Record *> Attributes =
FunctionSpec->getValueAsListOfDefs("Attributes");
for (llvm::Record *Attr : Attributes)
const llvm::Record *FunctionSpec = Iter->second;
for (const llvm::Record *Attr :
FunctionSpec->getValueAsListOfDefs("Attributes"))
MacroAttr[Attr->getValueAsString("Macro")] = Attr;
}
return MacroAttr;
Expand All @@ -112,11 +111,11 @@ static void emitAttributeMacroDecls(const AttributeMap &MacroAttr,
for (auto &[Macro, Attr] : MacroAttr) {
std::vector<llvm::Record *> Instances =
Attr->getValueAsListOfDefs("Instances");
llvm::SmallVector<std::pair<AttributeStyle, llvm::Record *>> Styles;
llvm::SmallVector<std::pair<AttributeStyle, const llvm::Record *>> Styles;
std::transform(Instances.begin(), Instances.end(),
std::back_inserter(Styles),
[&](llvm::Record *Instance)
-> std::pair<AttributeStyle, llvm::Record *> {
[&](const llvm::Record *Instance)
-> std::pair<AttributeStyle, const llvm::Record *> {
auto Style = getAttributeStyle(Instance);
return {Style, Instance};
});
Expand Down Expand Up @@ -195,7 +194,7 @@ static void emitAttributeMacroForFunction(const llvm::Record *FunctionSpec,
FunctionSpec->getValueAsListOfDefs("Attributes");
llvm::interleave(
Attributes.begin(), Attributes.end(),
[&](llvm::Record *Attr) { OS << Attr->getValueAsString("Macro"); },
[&](const llvm::Record *Attr) { OS << Attr->getValueAsString("Macro"); },
[&]() { OS << ' '; });
if (!Attributes.empty())
OS << ' ';
Expand All @@ -217,7 +216,7 @@ static void writeAPIFromIndex(APIIndexer &G,
if (!G.MacroSpecMap.count(Name))
llvm::PrintFatalError(Name + " not found in any standard spec.\n");

llvm::Record *MacroDef = Pair.second;
const llvm::Record *MacroDef = Pair.second;
dedentAndWrite(MacroDef->getValueAsString("Defn"), OS);

OS << '\n';
Expand All @@ -237,7 +236,7 @@ static void writeAPIFromIndex(APIIndexer &G,
llvm::PrintFatalError(
Name + " is not listed as an enumeration in any standard spec.\n");

llvm::Record *EnumerationSpec = G.EnumerationSpecMap[Name];
const llvm::Record *EnumerationSpec = G.EnumerationSpecMap[Name];
OS << " " << EnumerationSpec->getValueAsString("Name");
auto Value = EnumerationSpec->getValueAsString("Value");
if (Value == "__default__") {
Expand Down Expand Up @@ -267,9 +266,9 @@ static void writeAPIFromIndex(APIIndexer &G,
if (Iter == G.FunctionSpecMap.end())
continue;

llvm::Record *FunctionSpec = Iter->second;
llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
const llvm::Record *FunctionSpec = Iter->second;
const llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
const llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");

// TODO: https://github.com/llvm/llvm-project/issues/81208
// Ideally, we should group functions based on their guarding macros.
Expand All @@ -285,7 +284,7 @@ static void writeAPIFromIndex(APIIndexer &G,

auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args");
for (size_t i = 0; i < ArgsList.size(); ++i) {
llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
const llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
OS << G.getTypeAsString(ArgType);
if (i < ArgsList.size() - 1)
OS << ", ";
Expand All @@ -304,7 +303,7 @@ static void writeAPIFromIndex(APIIndexer &G,
auto Iter = G.ObjectSpecMap.find(Name);
if (Iter == G.ObjectSpecMap.end())
continue;
llvm::Record *ObjectSpec = Iter->second;
const llvm::Record *ObjectSpec = Iter->second;
auto Type = ObjectSpec->getValueAsString("Type");
OS << "extern " << Type << " " << Name << ";\n";
}
Expand All @@ -314,13 +313,13 @@ static void writeAPIFromIndex(APIIndexer &G,
emitUndefsForAttributeMacros(MacroAttr, OS);
}

void writePublicAPI(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {}
void writePublicAPI(llvm::raw_ostream &OS, const llvm::RecordKeeper &Records) {}

const char PublicAPICommand::Name[] = "public_api";

void PublicAPICommand::run(llvm::raw_ostream &OS, const ArgVector &Args,
llvm::StringRef StdHeader,
llvm::RecordKeeper &Records,
const llvm::RecordKeeper &Records,
const Command::ErrorReporter &Reporter) const {
if (Args.size() != 0)
Reporter.printFatalError("public_api command does not take any arguments.");
Expand Down
2 changes: 1 addition & 1 deletion libc/utils/HdrGen/PublicAPICommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PublicAPICommand : public Command {
: EntrypointNameList(EntrypointNames) {}

void run(llvm::raw_ostream &OS, const ArgVector &Args,
llvm::StringRef StdHeader, llvm::RecordKeeper &Records,
llvm::StringRef StdHeader, const llvm::RecordKeeper &Records,
const Command::ErrorReporter &Reporter) const override;
};

Expand Down
26 changes: 13 additions & 13 deletions libc/utils/LibcTableGenUtil/APIIndexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static const char StructTypeClassName[] = "Struct";
static const char StandardSpecClassName[] = "StandardSpec";
static const char PublicAPIClassName[] = "PublicAPI";

static bool isa(llvm::Record *Def, llvm::Record *TypeClass) {
static bool isa(const llvm::Record *Def, const llvm::Record *TypeClass) {
llvm::RecordRecTy *RecordType = Def->getType();
llvm::ArrayRef<llvm::Record *> Classes = RecordType->getClasses();
// We want exact types. That is, we don't want the classes listed in
Expand All @@ -35,35 +35,35 @@ static bool isa(llvm::Record *Def, llvm::Record *TypeClass) {
return Classes[0] == TypeClass;
}

bool APIIndexer::isaNamedType(llvm::Record *Def) {
bool APIIndexer::isaNamedType(const llvm::Record *Def) {
return isa(Def, NamedTypeClass);
}

bool APIIndexer::isaStructType(llvm::Record *Def) {
bool APIIndexer::isaStructType(const llvm::Record *Def) {
return isa(Def, StructClass);
}

bool APIIndexer::isaPtrType(llvm::Record *Def) {
bool APIIndexer::isaPtrType(const llvm::Record *Def) {
return isa(Def, PtrTypeClass);
}

bool APIIndexer::isaConstType(llvm::Record *Def) {
bool APIIndexer::isaConstType(const llvm::Record *Def) {
return isa(Def, ConstTypeClass);
}

bool APIIndexer::isaRestrictedPtrType(llvm::Record *Def) {
bool APIIndexer::isaRestrictedPtrType(const llvm::Record *Def) {
return isa(Def, RestrictedPtrTypeClass);
}

bool APIIndexer::isaStandardSpec(llvm::Record *Def) {
bool APIIndexer::isaStandardSpec(const llvm::Record *Def) {
return isa(Def, StandardSpecClass);
}

bool APIIndexer::isaPublicAPI(llvm::Record *Def) {
bool APIIndexer::isaPublicAPI(const llvm::Record *Def) {
return isa(Def, PublicAPIClass);
}

std::string APIIndexer::getTypeAsString(llvm::Record *TypeRecord) {
std::string APIIndexer::getTypeAsString(const llvm::Record *TypeRecord) {
if (isaNamedType(TypeRecord) || isaStructType(TypeRecord)) {
return std::string(TypeRecord->getValueAsString("Name"));
} else if (isaPtrType(TypeRecord)) {
Expand All @@ -79,7 +79,7 @@ std::string APIIndexer::getTypeAsString(llvm::Record *TypeRecord) {
}
}

void APIIndexer::indexStandardSpecDef(llvm::Record *StandardSpec) {
void APIIndexer::indexStandardSpecDef(const llvm::Record *StandardSpec) {
auto HeaderSpecList = StandardSpec->getValueAsListOfDefs("Headers");
for (llvm::Record *HeaderSpec : HeaderSpecList) {
llvm::StringRef Header = HeaderSpec->getValueAsString("Name");
Expand Down Expand Up @@ -119,7 +119,7 @@ void APIIndexer::indexStandardSpecDef(llvm::Record *StandardSpec) {
}
}

void APIIndexer::indexPublicAPIDef(llvm::Record *PublicAPI) {
void APIIndexer::indexPublicAPIDef(const llvm::Record *PublicAPI) {
// While indexing the public API, we do not check if any of the entities
// requested is from an included standard. Such a check is done while
// generating the API.
Expand Down Expand Up @@ -148,7 +148,7 @@ void APIIndexer::indexPublicAPIDef(llvm::Record *PublicAPI) {
Objects.insert(std::string(ObjectName));
}

void APIIndexer::index(llvm::RecordKeeper &Records) {
void APIIndexer::index(const llvm::RecordKeeper &Records) {
NamedTypeClass = Records.getClass(NamedTypeClassName);
PtrTypeClass = Records.getClass(PtrTypeClassName);
RestrictedPtrTypeClass = Records.getClass(RestrictedPtrTypeClassName);
Expand All @@ -159,7 +159,7 @@ void APIIndexer::index(llvm::RecordKeeper &Records) {

const auto &DefsMap = Records.getDefs();
for (auto &Pair : DefsMap) {
llvm::Record *Def = Pair.second.get();
const llvm::Record *Def = Pair.second.get();
if (isaStandardSpec(Def))
indexStandardSpecDef(Def);
if (isaPublicAPI(Def)) {
Expand Down
48 changes: 25 additions & 23 deletions libc/utils/LibcTableGenUtil/APIIndexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,38 @@ class APIIndexer {
std::optional<llvm::StringRef> StdHeader;

// TableGen classes in spec.td.
llvm::Record *NamedTypeClass;
llvm::Record *PtrTypeClass;
llvm::Record *RestrictedPtrTypeClass;
llvm::Record *ConstTypeClass;
llvm::Record *StructClass;
llvm::Record *StandardSpecClass;
llvm::Record *PublicAPIClass;

bool isaNamedType(llvm::Record *Def);
bool isaStructType(llvm::Record *Def);
bool isaPtrType(llvm::Record *Def);
bool isaConstType(llvm::Record *Def);
bool isaRestrictedPtrType(llvm::Record *Def);
bool isaStandardSpec(llvm::Record *Def);
bool isaPublicAPI(llvm::Record *Def);

void indexStandardSpecDef(llvm::Record *StandardSpec);
void indexPublicAPIDef(llvm::Record *PublicAPI);
void index(llvm::RecordKeeper &Records);
const llvm::Record *NamedTypeClass;
const llvm::Record *PtrTypeClass;
const llvm::Record *RestrictedPtrTypeClass;
const llvm::Record *ConstTypeClass;
const llvm::Record *StructClass;
const llvm::Record *StandardSpecClass;
const llvm::Record *PublicAPIClass;

bool isaNamedType(const llvm::Record *Def);
bool isaStructType(const llvm::Record *Def);
bool isaPtrType(const llvm::Record *Def);
bool isaConstType(const llvm::Record *Def);
bool isaRestrictedPtrType(const llvm::Record *Def);
bool isaStandardSpec(const llvm::Record *Def);
bool isaPublicAPI(const llvm::Record *Def);

void indexStandardSpecDef(const llvm::Record *StandardSpec);
void indexPublicAPIDef(const llvm::Record *PublicAPI);
void index(const llvm::RecordKeeper &Records);

public:
using NameToRecordMapping = std::unordered_map<std::string, llvm::Record *>;
using NameToRecordMapping =
std::unordered_map<std::string, const llvm::Record *>;
using NameSet = std::unordered_set<std::string>;

// This indexes all headers, not just a specified one.
explicit APIIndexer(llvm::RecordKeeper &Records) : StdHeader(std::nullopt) {
explicit APIIndexer(const llvm::RecordKeeper &Records)
: StdHeader(std::nullopt) {
index(Records);
}

APIIndexer(llvm::StringRef Header, llvm::RecordKeeper &Records)
APIIndexer(llvm::StringRef Header, const llvm::RecordKeeper &Records)
: StdHeader(Header) {
index(Records);
}
Expand All @@ -76,7 +78,7 @@ class APIIndexer {
NameSet Objects;
NameSet PublicHeaders;

std::string getTypeAsString(llvm::Record *TypeRecord);
std::string getTypeAsString(const llvm::Record *TypeRecord);
};

} // namespace llvm_libc
Expand Down
Loading

0 comments on commit 98563b1

Please sign in to comment.