Skip to content

[LLVM][TableGen] Add error check for duplicate intrinsic names #109226

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
merged 1 commit into from
Sep 19, 2024
Merged
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
9 changes: 9 additions & 0 deletions llvm/test/TableGen/intrinsic-duplicate-name.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: not llvm-tblgen -gen-intrinsic-impl -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS 2>&1 | FileCheck %s -DFILE=%s

include "llvm/IR/Intrinsics.td"

def int_foo0 : Intrinsic<[llvm_anyint_ty], [], [], "llvm.foo">;

// CHECK: [[FILE]]:[[@LINE+2]]:5: error: Intrinsic `llvm.foo` is already defined
// CHECK: [[FILE]]:[[@LINE-3]]:5: note: Previous definition here
def int_foo1 : Intrinsic<[llvm_anyint_ty], [], [], "llvm.foo">;
37 changes: 34 additions & 3 deletions llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,51 @@ CodeGenIntrinsicTable::CodeGenIntrinsicTable(const RecordKeeper &RC) {
Intrinsics.reserve(Defs.size());

for (const Record *Def : Defs)
Intrinsics.push_back(CodeGenIntrinsic(Def, Ctx));
Intrinsics.emplace_back(CodeGenIntrinsic(Def, Ctx));

// To ensure deterministic sorted order when duplicates are present, use
// record ID as a tie-breaker similar to sortAndReportDuplicates in Utils.cpp.
llvm::sort(Intrinsics,
[](const CodeGenIntrinsic &LHS, const CodeGenIntrinsic &RHS) {
return std::tie(LHS.TargetPrefix, LHS.Name) <
std::tie(RHS.TargetPrefix, RHS.Name);
unsigned LhsID = LHS.TheDef->getID();
unsigned RhsID = RHS.TheDef->getID();
return std::tie(LHS.TargetPrefix, LHS.Name, LhsID) <
std::tie(RHS.TargetPrefix, RHS.Name, RhsID);
});

Targets.push_back({"", 0, 0});
for (size_t I = 0, E = Intrinsics.size(); I < E; ++I)
if (Intrinsics[I].TargetPrefix != Targets.back().Name) {
Targets.back().Count = I - Targets.back().Offset;
Targets.push_back({Intrinsics[I].TargetPrefix, I, 0});
}
Targets.back().Count = Intrinsics.size() - Targets.back().Offset;

CheckDuplicateIntrinsics();
}

// Check for duplicate intrinsic names.
void CodeGenIntrinsicTable::CheckDuplicateIntrinsics() const {
// Since the Intrinsics vector is already sorted by name, if there are 2 or
// more intrinsics with duplicate names, they will appear adjacent in sorted
// order. Note that if the intrinsic name was derived from the record name
// there cannot be be duplicate as TableGen parser would have flagged that.
// However, if the name was specified in the intrinsic definition, then its
// possible to have duplicate names.
auto I = std::adjacent_find(
Intrinsics.begin(), Intrinsics.end(),
[](const CodeGenIntrinsic &Int1, const CodeGenIntrinsic &Int2) {
return Int1.Name == Int2.Name;
});
if (I == Intrinsics.end())
return;

// Found a duplicate intrinsics.
const CodeGenIntrinsic &First = *I;
const CodeGenIntrinsic &Second = *(I + 1);
PrintError(Second.TheDef,
Twine("Intrinsic `") + First.Name + "` is already defined");
PrintFatalNote(First.TheDef, "Previous definition here");
}

CodeGenIntrinsic &CodeGenIntrinsicMap::operator[](const Record *Record) {
Expand Down
3 changes: 3 additions & 0 deletions llvm/utils/TableGen/Basic/CodeGenIntrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ class CodeGenIntrinsicTable {
const CodeGenIntrinsic &operator[](size_t Pos) const {
return Intrinsics[Pos];
}

private:
void CheckDuplicateIntrinsics() const;
};

// This class builds `CodeGenIntrinsic` on demand for a given Def.
Expand Down
Loading