Skip to content

Commit 50be455

Browse files
authored
[TableGen] Add check for number of intrinsic return values (#107326)
Fail if we see an intrinsic that returns more than the supported number of return values. Intrinsics can return only upto a certain nyumber of values, as defined by the `IIT_RetNumbers` list in `Intrinsics.td`. Currently, if we define an intrinsic that exceeds the limit, llvm-tblgen crashes. Instead, read this limit and fail if it's exceeded with a proper error message.
1 parent 64498c5 commit 50be455

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
// RUN: llvm-tblgen -gen-intrinsic-enums -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS | FileCheck %s
1+
// RUN: llvm-tblgen -gen-intrinsic-enums -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS | FileCheck %s --check-prefix=CHECK-ENUM
2+
// RUN: llvm-tblgen -gen-intrinsic-impl -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS > /dev/null 2>&1
3+
// RUN: not llvm-tblgen -gen-intrinsic-impl -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS -DENABLE_ERROR 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
4+
25
// XFAIL: vg_leak
36

47
include "llvm/IR/Intrinsics.td"
58

6-
// Make sure we can return up to 8 values
7-
// CHECK: returns_8_results = {{[0-9]+}}, // llvm.returns.8.results
8-
def int_returns_8_results : Intrinsic<
9-
[llvm_anyint_ty, llvm_anyint_ty, llvm_anyint_ty, llvm_anyint_ty,
10-
llvm_anyint_ty, llvm_anyint_ty, llvm_anyint_ty, llvm_anyint_ty],
11-
[], [], "llvm.returns.8.results">;
9+
// Make sure we can return up to 9 values.
10+
// CHECK-ENUM: returns_9_results = {{[0-9]+}}, // llvm.returns.9.results
11+
def int_returns_9_results : Intrinsic<
12+
!listsplat(llvm_anyint_ty, 9),
13+
[], [], "llvm.returns.9.results">;
14+
15+
#ifdef ENABLE_ERROR
16+
// CHECK-ERROR: error: intrinsics can only return upto 9 values, 'int_returns_10_results' returns 10 values
17+
// CHECK-ERROR-NEXT: def int_returns_10_results : Intrinsic<
18+
def int_returns_10_results : Intrinsic<
19+
!listsplat(llvm_anyint_ty, 10),
20+
[], [], "llvm.returns.10.results">;
21+
22+
#endif

llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ CodeGenIntrinsicContext::CodeGenIntrinsicContext(const RecordKeeper &RC) {
2929
for (const Record *Rec : RC.getAllDerivedDefinitions("IntrinsicProperty"))
3030
if (Rec->getValueAsBit("IsDefault"))
3131
DefaultProperties.push_back(Rec);
32+
33+
// The maximum number of values that an intrinsic can return is the size of
34+
// of `IIT_RetNumbers` list - 1 (since we index into this list using the
35+
// number of return values as the index).
36+
const auto *IIT_RetNumbers =
37+
dyn_cast_or_null<ListInit>(RC.getGlobal("IIT_RetNumbers"));
38+
if (!IIT_RetNumbers)
39+
PrintFatalError("unable to find 'IIT_RetNumbers' list");
40+
MaxNumReturn = IIT_RetNumbers->size() - 1;
3241
}
3342

3443
CodeGenIntrinsicTable::CodeGenIntrinsicTable(const RecordKeeper &RC) {
@@ -106,6 +115,13 @@ CodeGenIntrinsic::CodeGenIntrinsic(const Record *R,
106115
TargetPrefix + ".'!");
107116
}
108117

118+
unsigned NumRet = R->getValueAsListInit("RetTypes")->size();
119+
if (NumRet > Ctx.MaxNumReturn)
120+
PrintFatalError(DefLoc, "intrinsics can only return upto " +
121+
Twine(Ctx.MaxNumReturn) + " values, '" +
122+
DefName + "' returns " + Twine(NumRet) +
123+
" values");
124+
109125
const Record *TypeInfo = R->getValueAsDef("TypeInfo");
110126
if (!TypeInfo->isSubClassOf("TypeInfoGen"))
111127
PrintFatalError(DefLoc, "TypeInfo field in " + DefName +
@@ -116,7 +132,6 @@ CodeGenIntrinsic::CodeGenIntrinsic(const Record *R,
116132

117133
// Types field is a concatenation of Return types followed by Param types.
118134
unsigned Idx = 0;
119-
unsigned NumRet = R->getValueAsListInit("RetTypes")->size();
120135
for (; Idx < NumRet; ++Idx)
121136
IS.RetTys.push_back(TypeList->getElementAsRecord(Idx));
122137

llvm/utils/TableGen/Basic/CodeGenIntrinsics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class RecordKeeper;
3030
struct CodeGenIntrinsicContext {
3131
explicit CodeGenIntrinsicContext(const RecordKeeper &RC);
3232
std::vector<const Record *> DefaultProperties;
33+
34+
// Maximum number of values an intrinsic can return.
35+
unsigned MaxNumReturn;
3336
};
3437

3538
struct CodeGenIntrinsic {

0 commit comments

Comments
 (0)