-
Notifications
You must be signed in to change notification settings - Fork 15.4k
TableGen: Split RuntimeLibcallsEmitter into separate utility header #166583
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
Merged
arsenm
merged 2 commits into
main
from
users/arsenm/tablegen-split-runtime-libcalls-emitter
Nov 5, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "RuntimeLibcalls.h" | ||
| #include "llvm/TableGen/Error.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| RuntimeLibcalls::RuntimeLibcalls(const RecordKeeper &Records) { | ||
| ArrayRef<const Record *> AllRuntimeLibcalls = | ||
| Records.getAllDerivedDefinitions("RuntimeLibcall"); | ||
|
|
||
| RuntimeLibcallDefList.reserve(AllRuntimeLibcalls.size()); | ||
|
|
||
| size_t CallTypeEnumVal = 0; | ||
| for (const Record *RuntimeLibcallDef : AllRuntimeLibcalls) { | ||
| RuntimeLibcallDefList.emplace_back(RuntimeLibcallDef, CallTypeEnumVal++); | ||
| Def2RuntimeLibcall[RuntimeLibcallDef] = &RuntimeLibcallDefList.back(); | ||
| } | ||
|
|
||
| for (RuntimeLibcall &LibCall : RuntimeLibcallDefList) | ||
| Def2RuntimeLibcall[LibCall.getDef()] = &LibCall; | ||
|
|
||
| ArrayRef<const Record *> AllRuntimeLibcallImplsRaw = | ||
| Records.getAllDerivedDefinitions("RuntimeLibcallImpl"); | ||
|
|
||
| SmallVector<const Record *, 1024> AllRuntimeLibcallImpls( | ||
| AllRuntimeLibcallImplsRaw); | ||
|
|
||
| // Sort by libcall impl name and secondarily by the enum name. | ||
| sort(AllRuntimeLibcallImpls, [](const Record *A, const Record *B) { | ||
| return std::pair(A->getValueAsString("LibCallFuncName"), A->getName()) < | ||
| std::pair(B->getValueAsString("LibCallFuncName"), B->getName()); | ||
| }); | ||
|
|
||
| RuntimeLibcallImplDefList.reserve(AllRuntimeLibcallImpls.size()); | ||
|
|
||
| size_t LibCallImplEnumVal = 1; | ||
| for (const Record *LibCallImplDef : AllRuntimeLibcallImpls) { | ||
jurahul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| RuntimeLibcallImplDefList.emplace_back(LibCallImplDef, Def2RuntimeLibcall, | ||
| LibCallImplEnumVal++); | ||
|
|
||
| const RuntimeLibcallImpl &LibCallImpl = RuntimeLibcallImplDefList.back(); | ||
| Def2RuntimeLibcallImpl[LibCallImplDef] = &LibCallImpl; | ||
|
|
||
| if (LibCallImpl.isDefault()) { | ||
| const RuntimeLibcall *Provides = LibCallImpl.getProvides(); | ||
| if (!Provides) | ||
| PrintFatalError(LibCallImplDef->getLoc(), | ||
| "default implementations must provide a libcall"); | ||
| LibCallToDefaultImpl[Provides] = &LibCallImpl; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void LibcallPredicateExpander::expand(SetTheory &ST, const Record *Def, | ||
| SetTheory::RecSet &Elts) { | ||
| assert(Def->isSubClassOf("LibcallImpls")); | ||
|
|
||
| SetTheory::RecSet TmpElts; | ||
|
|
||
| ST.evaluate(Def->getValueInit("MemberList"), TmpElts, Def->getLoc()); | ||
|
|
||
| Elts.insert(TmpElts.begin(), TmpElts.end()); | ||
|
|
||
| AvailabilityPredicate AP(Def->getValueAsDef("AvailabilityPredicate")); | ||
| const Record *CCClass = Def->getValueAsOptionalDef("CallingConv"); | ||
|
|
||
| // This is assuming we aren't conditionally applying a calling convention to | ||
| // some subsets, and not another, but this doesn't appear to be used. | ||
|
|
||
| for (const Record *LibcallImplDef : TmpElts) { | ||
| const RuntimeLibcallImpl *LibcallImpl = | ||
| Libcalls.getRuntimeLibcallImpl(LibcallImplDef); | ||
| if (!AP.isAlwaysAvailable() || CCClass) { | ||
| auto [It, Inserted] = Func2Preds.insert({LibcallImpl, {{}, CCClass}}); | ||
| if (!Inserted) { | ||
| PrintError( | ||
| Def, | ||
| "combining nested libcall set predicates currently unhandled: '" + | ||
| LibcallImpl->getLibcallFuncName() + "'"); | ||
| } | ||
|
|
||
| It->second.first.push_back(AP.getDef()); | ||
| It->second.second = CCClass; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| //===------------------------------------------------------------*- 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_UTILS_TABLEGEN_COMMON_RUNTIMELIBCALLS_H | ||
| #define LLVM_UTILS_TABLEGEN_COMMON_RUNTIMELIBCALLS_H | ||
|
|
||
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
| #include "llvm/TableGen/Record.h" | ||
| #include "llvm/TableGen/SetTheory.h" | ||
|
|
||
| namespace llvm { | ||
|
|
||
| class AvailabilityPredicate { | ||
| const Record *TheDef; | ||
| StringRef PredicateString; | ||
|
|
||
| public: | ||
| AvailabilityPredicate(const Record *Def) : TheDef(Def) { | ||
| if (TheDef) | ||
| PredicateString = TheDef->getValueAsString("Cond"); | ||
| } | ||
|
|
||
| const Record *getDef() const { return TheDef; } | ||
|
|
||
| bool isAlwaysAvailable() const { return PredicateString.empty(); } | ||
|
|
||
| void emitIf(raw_ostream &OS) const { | ||
| OS << "if (" << PredicateString << ") {\n"; | ||
| } | ||
|
|
||
| void emitEndIf(raw_ostream &OS) const { OS << "}\n"; } | ||
|
|
||
| void emitTableVariableNameSuffix(raw_ostream &OS) const { | ||
| if (TheDef) | ||
| OS << '_' << TheDef->getName(); | ||
| } | ||
| }; | ||
|
|
||
| class RuntimeLibcalls; | ||
| class RuntimeLibcallImpl; | ||
|
|
||
| /// Used to apply predicates to nested sets of libcalls. | ||
| struct LibcallPredicateExpander : SetTheory::Expander { | ||
| const RuntimeLibcalls &Libcalls; | ||
| DenseMap<const RuntimeLibcallImpl *, | ||
| std::pair<std::vector<const Record *>, const Record *>> &Func2Preds; | ||
|
|
||
| LibcallPredicateExpander( | ||
| const RuntimeLibcalls &Libcalls, | ||
| DenseMap<const RuntimeLibcallImpl *, | ||
| std::pair<std::vector<const Record *>, const Record *>> | ||
| &Func2Preds) | ||
| : Libcalls(Libcalls), Func2Preds(Func2Preds) {} | ||
|
|
||
| void expand(SetTheory &ST, const Record *Def, | ||
| SetTheory::RecSet &Elts) override; | ||
| }; | ||
|
|
||
| class RuntimeLibcall { | ||
| const Record *TheDef = nullptr; | ||
| const size_t EnumVal; | ||
|
|
||
| public: | ||
| RuntimeLibcall() = delete; | ||
| RuntimeLibcall(const Record *Def, size_t EnumVal) | ||
| : TheDef(Def), EnumVal(EnumVal) { | ||
| assert(Def); | ||
| } | ||
|
|
||
| ~RuntimeLibcall() { assert(TheDef); } | ||
|
|
||
| const Record *getDef() const { return TheDef; } | ||
|
|
||
| StringRef getName() const { return TheDef->getName(); } | ||
|
|
||
| size_t getEnumVal() const { return EnumVal; } | ||
|
|
||
| void emitEnumEntry(raw_ostream &OS) const { | ||
| OS << "RTLIB::" << TheDef->getValueAsString("Name"); | ||
| } | ||
| }; | ||
|
|
||
| class RuntimeLibcallImpl { | ||
| const Record *TheDef; | ||
| const RuntimeLibcall *Provides = nullptr; | ||
| const size_t EnumVal; | ||
|
|
||
| public: | ||
| RuntimeLibcallImpl( | ||
| const Record *Def, | ||
| const DenseMap<const Record *, const RuntimeLibcall *> &ProvideMap, | ||
| size_t EnumVal) | ||
| : TheDef(Def), EnumVal(EnumVal) { | ||
| if (const Record *ProvidesDef = Def->getValueAsDef("Provides")) | ||
| Provides = ProvideMap.lookup(ProvidesDef); | ||
| } | ||
|
|
||
| ~RuntimeLibcallImpl() = default; | ||
|
|
||
| const Record *getDef() const { return TheDef; } | ||
|
|
||
| StringRef getName() const { return TheDef->getName(); } | ||
|
|
||
| size_t getEnumVal() const { return EnumVal; } | ||
|
|
||
| const RuntimeLibcall *getProvides() const { return Provides; } | ||
|
|
||
| StringRef getLibcallFuncName() const { | ||
| return TheDef->getValueAsString("LibCallFuncName"); | ||
| } | ||
|
|
||
| const Record *getCallingConv() const { | ||
| return TheDef->getValueAsOptionalDef("CallingConv"); | ||
| } | ||
|
|
||
| void emitQuotedLibcallFuncName(raw_ostream &OS) const { | ||
| OS << '\"' << getLibcallFuncName() << '\"'; | ||
| } | ||
|
|
||
| bool isDefault() const { return TheDef->getValueAsBit("IsDefault"); } | ||
|
|
||
| void emitEnumEntry(raw_ostream &OS) const { | ||
| OS << "RTLIB::impl_" << this->getName(); | ||
| } | ||
|
|
||
| void emitSetImplCall(raw_ostream &OS) const { | ||
| OS << "setLibcallImpl("; | ||
| Provides->emitEnumEntry(OS); | ||
| OS << ", "; | ||
| emitEnumEntry(OS); | ||
| OS << "); // " << getLibcallFuncName() << '\n'; | ||
| } | ||
|
|
||
| void emitTableEntry(raw_ostream &OS) const { | ||
| OS << '{'; | ||
| Provides->emitEnumEntry(OS); | ||
| OS << ", "; | ||
| emitEnumEntry(OS); | ||
| OS << "}, // " << getLibcallFuncName() << '\n'; | ||
| } | ||
|
|
||
| void emitSetCallingConv(raw_ostream &OS) const {} | ||
| }; | ||
|
|
||
| struct LibcallsWithCC { | ||
| std::vector<const RuntimeLibcallImpl *> LibcallImpls; | ||
| const Record *CallingConv = nullptr; | ||
| }; | ||
|
|
||
| class RuntimeLibcalls { | ||
| private: | ||
| DenseMap<const Record *, const RuntimeLibcall *> Def2RuntimeLibcall; | ||
| DenseMap<const Record *, const RuntimeLibcallImpl *> Def2RuntimeLibcallImpl; | ||
|
|
||
| std::vector<RuntimeLibcall> RuntimeLibcallDefList; | ||
| std::vector<RuntimeLibcallImpl> RuntimeLibcallImplDefList; | ||
|
|
||
| DenseMap<const RuntimeLibcall *, const RuntimeLibcallImpl *> | ||
| LibCallToDefaultImpl; | ||
|
|
||
| public: | ||
| RuntimeLibcalls(const RecordKeeper &Records); | ||
|
|
||
| ArrayRef<RuntimeLibcall> getRuntimeLibcallDefList() const { | ||
| return RuntimeLibcallDefList; | ||
| } | ||
|
|
||
| ArrayRef<RuntimeLibcallImpl> getRuntimeLibcallImplDefList() const { | ||
| return RuntimeLibcallImplDefList; | ||
| } | ||
|
|
||
| const RuntimeLibcall *getRuntimeLibcall(const Record *Def) const { | ||
| return Def2RuntimeLibcall.lookup(Def); | ||
| } | ||
|
|
||
| const RuntimeLibcallImpl *getRuntimeLibcallImpl(const Record *Def) const { | ||
| return Def2RuntimeLibcallImpl.lookup(Def); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace llvm | ||
|
|
||
| #endif // LLVM_UTILS_TABLEGEN_COMMON_RUNTIMELIBCALLS_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.