Skip to content

[TableGen] Add check for number of intrinsic return values #107326

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 5, 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
25 changes: 18 additions & 7 deletions llvm/test/TableGen/intrinsic-struct.td
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
// RUN: llvm-tblgen -gen-intrinsic-enums -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS | FileCheck %s
// RUN: llvm-tblgen -gen-intrinsic-enums -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS | FileCheck %s --check-prefix=CHECK-ENUM
// RUN: llvm-tblgen -gen-intrinsic-impl -I %p/../../include %s -DTEST_INTRINSICS_SUPPRESS_DEFS > /dev/null 2>&1
// 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

// XFAIL: vg_leak

include "llvm/IR/Intrinsics.td"

// Make sure we can return up to 8 values
// CHECK: returns_8_results = {{[0-9]+}}, // llvm.returns.8.results
def int_returns_8_results : Intrinsic<
[llvm_anyint_ty, llvm_anyint_ty, llvm_anyint_ty, llvm_anyint_ty,
llvm_anyint_ty, llvm_anyint_ty, llvm_anyint_ty, llvm_anyint_ty],
[], [], "llvm.returns.8.results">;
// Make sure we can return up to 9 values.
// CHECK-ENUM: returns_9_results = {{[0-9]+}}, // llvm.returns.9.results
def int_returns_9_results : Intrinsic<
!listsplat(llvm_anyint_ty, 9),
[], [], "llvm.returns.9.results">;

#ifdef ENABLE_ERROR
// CHECK-ERROR: error: intrinsics can only return upto 9 values, 'int_returns_10_results' returns 10 values
// CHECK-ERROR-NEXT: def int_returns_10_results : Intrinsic<
def int_returns_10_results : Intrinsic<
!listsplat(llvm_anyint_ty, 10),
[], [], "llvm.returns.10.results">;

#endif
17 changes: 16 additions & 1 deletion llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ CodeGenIntrinsicContext::CodeGenIntrinsicContext(const RecordKeeper &RC) {
for (const Record *Rec : RC.getAllDerivedDefinitions("IntrinsicProperty"))
if (Rec->getValueAsBit("IsDefault"))
DefaultProperties.push_back(Rec);

// The maximum number of values that an intrinsic can return is the size of
// of `IIT_RetNumbers` list - 1 (since we index into this list using the
// number of return values as the index).
const auto *IIT_RetNumbers =
dyn_cast_or_null<ListInit>(RC.getGlobal("IIT_RetNumbers"));
if (!IIT_RetNumbers)
PrintFatalError("unable to find 'IIT_RetNumbers' list");
MaxNumReturn = IIT_RetNumbers->size() - 1;
}

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

unsigned NumRet = R->getValueAsListInit("RetTypes")->size();
if (NumRet > Ctx.MaxNumReturn)
PrintFatalError(DefLoc, "intrinsics can only return upto " +
Twine(Ctx.MaxNumReturn) + " values, '" +
DefName + "' returns " + Twine(NumRet) +
" values");

const Record *TypeInfo = R->getValueAsDef("TypeInfo");
if (!TypeInfo->isSubClassOf("TypeInfoGen"))
PrintFatalError(DefLoc, "TypeInfo field in " + DefName +
Expand All @@ -116,7 +132,6 @@ CodeGenIntrinsic::CodeGenIntrinsic(const Record *R,

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

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 @@ -30,6 +30,9 @@ class RecordKeeper;
struct CodeGenIntrinsicContext {
explicit CodeGenIntrinsicContext(const RecordKeeper &RC);
std::vector<const Record *> DefaultProperties;

// Maximum number of values an intrinsic can return.
unsigned MaxNumReturn;
};

struct CodeGenIntrinsic {
Expand Down
Loading