Skip to content

Commit 2a0073f

Browse files
authored
[LLVM][TableGen] Check overloaded intrinsic mangling suffix conflicts (llvm#110324)
Check name conflicts between intrinsics caused by mangling suffix. If the base name of an overloaded intrinsic is a proper prefix of another intrinsic, check if the other intrinsic name suffix after the proper prefix can match a mangled type and issue an error if it can.
1 parent 9b7491e commit 2a0073f

File tree

5 files changed

+175
-4
lines changed

5 files changed

+175
-4
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: llvm-tblgen -gen-intrinsic-enums -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS | FileCheck %s
2+
// RUN: not llvm-tblgen -gen-intrinsic-enums -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS -DCONFLICT 2>&1 | FileCheck %s -DFILE=%s --check-prefix=CHECK-CONFLICT
3+
4+
5+
include "llvm/IR/Intrinsics.td"
6+
// CHECK: foo = 1,
7+
def int_foo : Intrinsic<[llvm_any_ty]>;
8+
9+
// No conflicts, since .bar is not a vaid mangled type.
10+
// CHECK: foo_bar,
11+
def int_foo_bar : Intrinsic<[llvm_i32_ty]>;
12+
13+
// CHECK: foo_bar_f32,
14+
def int_foo_bar_f32 : Intrinsic<[llvm_i32_ty]>;
15+
16+
#ifdef CONFLICT
17+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.a3` cannot share prefix `llvm.foo.a3` with another overloaded intrinsic `llvm.foo`
18+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.bf16` cannot share prefix `llvm.foo.bf16` with another overloaded intrinsic `llvm.foo`
19+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.f32` cannot share prefix `llvm.foo.f32` with another overloaded intrinsic `llvm.foo`
20+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.f64` cannot share prefix `llvm.foo.f64` with another overloaded intrinsic `llvm.foo`
21+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.f_3` cannot share prefix `llvm.foo.f_3` with another overloaded intrinsic `llvm.foo`
22+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.i65` cannot share prefix `llvm.foo.i65` with another overloaded intrinsic `llvm.foo`
23+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.i65.bar` cannot share prefix `llvm.foo.i65` with another overloaded intrinsic `llvm.foo`
24+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.p3` cannot share prefix `llvm.foo.p3` with another overloaded intrinsic `llvm.foo`
25+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.s_3` cannot share prefix `llvm.foo.s_3` with another overloaded intrinsic `llvm.foo`
26+
// CHECK-CONFLICT: error: intrinsic `llvm.foo.v4` cannot share prefix `llvm.foo.v4` with another overloaded intrinsic `llvm.foo`
27+
// CHECK-CONFLICT: 10 errors
28+
// CHECK-CONFLICT-NOT: error
29+
30+
// Conflicts due to suffix being a possible mangled type.
31+
def int_foo_f32 : Intrinsic<[llvm_i32_ty]>;
32+
def int_foo_f64 : Intrinsic<[llvm_i32_ty]>;
33+
def int_foo_bf16: Intrinsic<[llvm_i32_ty]>;
34+
def int_foo_p3 : Intrinsic<[llvm_i32_ty]>;
35+
def int_foo_a3 : Intrinsic<[llvm_i32_ty]>;
36+
def int_foo_v4 : Intrinsic<[llvm_i32_ty]>;
37+
def int_foo_func : Intrinsic<[llvm_i32_ty], [], [], "llvm.foo.f_3">;
38+
def int_foo_struct : Intrinsic<[llvm_i32_ty], [], [], "llvm.foo.s_3">;
39+
def int_foo_t3 : Intrinsic<[llvm_i32_ty]>;
40+
def int_foo_i65 : Intrinsic<[llvm_i32_ty]>;
41+
def int_foo_i65_bar : Intrinsic<[llvm_i32_ty]>;
42+
43+
#endif

llvm/unittests/IR/IntrinsicsTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class IntrinsicsTest : public ::testing::Test {
6363
};
6464

6565
TEST(IntrinsicNameLookup, Basic) {
66-
static constexpr const char *const NameTable1[] = {
66+
static constexpr const char *const NameTable[] = {
6767
"llvm.foo", "llvm.foo.a", "llvm.foo.b", "llvm.foo.b.a", "llvm.foo.c",
6868
};
6969

@@ -74,11 +74,11 @@ TEST(IntrinsicNameLookup, Basic) {
7474
};
7575

7676
for (const auto &[Name, ExpectedIdx] : Tests) {
77-
int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable1, Name);
77+
int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
7878
EXPECT_EQ(ExpectedIdx, Idx);
7979
if (!StringRef(Name).starts_with("llvm.foo"))
8080
continue;
81-
Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable1, Name, "foo");
81+
Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name, "foo");
8282
EXPECT_EQ(ExpectedIdx, Idx);
8383
}
8484
}

llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ CodeGenIntrinsicTable::CodeGenIntrinsicTable(const RecordKeeper &RC) {
7676

7777
CheckDuplicateIntrinsics();
7878
CheckTargetIndependentIntrinsics();
79+
CheckOverloadSuffixConflicts();
7980
}
8081

8182
// Check for duplicate intrinsic names.
@@ -124,6 +125,129 @@ void CodeGenIntrinsicTable::CheckTargetIndependentIntrinsics() const {
124125
}
125126
}
126127

128+
// Return true if the given Suffix looks like a mangled type. Note that this
129+
// check is conservative, but allows all existing LLVM intrinsic suffixes to be
130+
// considered as not looking like a mangling suffix.
131+
static bool doesSuffixLookLikeMangledType(StringRef Suffix) {
132+
// Try to match against possible mangling suffixes for various types.
133+
// See getMangledTypeStr() for the mangling suffixes possible. It includes
134+
// pointer : p[0-9]+
135+
// array : a[0-9]+.+
136+
// struct: : s_/sl_.+
137+
// function : f_.+
138+
// vector : v/nxv[0-9]+.+
139+
// target type : t.+
140+
// integer : i[0-9]+
141+
// named types : See `NamedTypes` below.
142+
143+
// Match anything with an _, so match function and struct types.
144+
if (Suffix.contains('_'))
145+
return true;
146+
147+
// [av][0-9]+.+, simplified to [av][0-9].+
148+
if (Suffix.size() >= 2 && is_contained("av", Suffix[0]) && isDigit(Suffix[1]))
149+
return true;
150+
151+
// nxv[0-9]+.+, simplified to nxv[0-9].+
152+
if (Suffix.size() >= 4 && Suffix.starts_with("nxv") && isDigit(Suffix[3]))
153+
return true;
154+
155+
// t.+
156+
if (Suffix.size() > 1 && Suffix.starts_with('t'))
157+
return false;
158+
159+
// [pi][0-9]+
160+
if (is_contained("pi", Suffix[0]) && all_of(Suffix.drop_front(), isDigit))
161+
return true;
162+
163+
// Match one of the named types.
164+
static constexpr StringLiteral NamedTypes[] = {
165+
"isVoid", "Metadata", "f16", "f32", "f64",
166+
"f80", "f128", "bf16", "ppcf128", "x86amx"};
167+
return is_contained(NamedTypes, Suffix);
168+
}
169+
170+
// Check for conflicts with overloaded intrinsics. If there exists an overloaded
171+
// intrinsic with base name `llvm.target.foo`, LLVM will add a mangling suffix
172+
// to it to encode the overload types. This mangling suffix is 1 or more .
173+
// prefixed mangled type string as defined in `getMangledTypeStr`. If there
174+
// exists another intrinsic `llvm.target.foo[.<suffixN>]+`, which has the same
175+
// prefix as the overloaded intrinsic, its possible that there may be a name
176+
// conflict with the overloaded intrinsic and either one may interfere with name
177+
// lookup for the other, leading to wrong intrinsic ID being assigned.
178+
//
179+
// The actual name lookup in the intrinsic name table is done by a search
180+
// on each successive '.' separted component of the intrinsic name (see
181+
// `lookupLLVMIntrinsicByName`). Consider first the case where there exists a
182+
// non-overloaded intrinsic `llvm.target.foo[.suffix]+`. For the non-overloaded
183+
// intrinsics, the name lookup is an exact match, so the presence of the
184+
// overloaded intrinsic with the same prefix will not interfere with the
185+
// search. However, a lookup intended to match the overloaded intrinsic might be
186+
// affected by the presence of another entry in the name table with the same
187+
// prefix.
188+
//
189+
// Since LLVM's name lookup first selects the target specific (or target
190+
// independent) slice of the name table to look into, intrinsics in 2 different
191+
// targets cannot conflict with each other. Within a specific target,
192+
// if we have an overloaded intrinsic with name `llvm.target.foo` and another
193+
// one with same prefix and one or more suffixes `llvm.target.foo[.<suffixN>]+`,
194+
// then the name search will try to first match against suffix0, then suffix1
195+
// etc. If suffix0 can match a mangled type, then the search for an
196+
// `llvm.target.foo` with a mangling suffix can match against suffix0,
197+
// preventing a match with `llvm.target.foo`. If suffix0 cannot match a mangled
198+
// type, then that cannot happen, so we do not need to check for later suffixes.
199+
//
200+
// Generalizing, the `llvm.target.foo[.suffixN]+` will cause a conflict if the
201+
// first suffix (.suffix0) can match a mangled type (and then we do not need to
202+
// check later suffixes) and will not cause a conflict if it cannot (and then
203+
// again, we do not need to check for later suffixes).
204+
void CodeGenIntrinsicTable::CheckOverloadSuffixConflicts() const {
205+
for (const TargetSet &Set : Targets) {
206+
const CodeGenIntrinsic *Overloaded = nullptr;
207+
for (const CodeGenIntrinsic &Int : (*this)[Set]) {
208+
// If we do not have an overloaded intrinsic to check against, nothing
209+
// to do except potentially identifying this as a candidate for checking
210+
// against in future iteration.
211+
if (!Overloaded) {
212+
if (Int.isOverloaded)
213+
Overloaded = &Int;
214+
continue;
215+
}
216+
217+
StringRef Name = Int.Name;
218+
StringRef OverloadName = Overloaded->Name;
219+
// If we have an overloaded intrinsic to check again, check if its name is
220+
// a proper prefix of this intrinsic.
221+
if (Name.starts_with(OverloadName) && Name[OverloadName.size()] == '.') {
222+
// If yes, verify suffixes and flag an error.
223+
StringRef Suffixes = Name.drop_front(OverloadName.size() + 1);
224+
225+
// Only need to look at the first suffix.
226+
StringRef Suffix0 = Suffixes.split('.').first;
227+
228+
if (!doesSuffixLookLikeMangledType(Suffix0))
229+
continue;
230+
231+
unsigned SuffixSize = OverloadName.size() + 1 + Suffix0.size();
232+
// If suffix looks like mangling suffix, flag it as an error.
233+
PrintError(Int.TheDef->getLoc(),
234+
"intrinsic `" + Name + "` cannot share prefix `" +
235+
Name.take_front(SuffixSize) +
236+
"` with another overloaded intrinsic `" + OverloadName +
237+
"`");
238+
PrintNote(Overloaded->TheDef->getLoc(),
239+
"Overloaded intrinsic `" + OverloadName + "` defined here");
240+
continue;
241+
}
242+
243+
// If we find an intrinsic that is not a proper prefix, any later
244+
// intrinsic is also not going to be a proper prefix, so invalidate the
245+
// overloaded to check against.
246+
Overloaded = nullptr;
247+
}
248+
}
249+
}
250+
127251
const CodeGenIntrinsic &CodeGenIntrinsicMap::operator[](const Record *Record) {
128252
if (!Record->isSubClassOf("Intrinsic"))
129253
PrintFatalError("Intrinsic defs should be subclass of 'Intrinsic' class");

llvm/utils/TableGen/Basic/CodeGenIntrinsics.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,15 @@ class CodeGenIntrinsicTable {
185185
const CodeGenIntrinsic &operator[](size_t Pos) const {
186186
return Intrinsics[Pos];
187187
}
188+
ArrayRef<CodeGenIntrinsic> operator[](const TargetSet &Set) const {
189+
return ArrayRef(&Intrinsics[Set.Offset], Set.Count);
190+
}
188191
ArrayRef<TargetSet> getTargets() const { return Targets; }
189192

190193
private:
191194
void CheckDuplicateIntrinsics() const;
192195
void CheckTargetIndependentIntrinsics() const;
196+
void CheckOverloadSuffixConflicts() const;
193197

194198
std::vector<CodeGenIntrinsic> Intrinsics;
195199
std::vector<TargetSet> Targets;

llvm/utils/TableGen/IntrinsicEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
155155

156156
OS << "// Enum values for intrinsics.\n";
157157
bool First = true;
158-
for (const auto &Int : ArrayRef(&Ints[Set->Offset], Set->Count)) {
158+
for (const auto &Int : Ints[*Set]) {
159159
OS << " " << Int.EnumName;
160160

161161
// Assign a value to the first intrinsic in this target set so that all

0 commit comments

Comments
 (0)