Skip to content

[mlir][ods] Verify access to operands in inferReturnTypes #112574

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 4 commits into from
Oct 23, 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
7 changes: 7 additions & 0 deletions mlir/test/mlir-tblgen/op-result.td
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def OpL1 : NS_Op<"op_with_all_types_constraint",

// CHECK-LABEL: LogicalResult OpL1::inferReturnTypes
// CHECK-NOT: }
// CHECK: if (operands.size() <= 0)
// CHECK-NEXT: return ::mlir::failure();
// CHECK: ::mlir::Type odsInferredType0 = operands[0].getType();
// CHECK: inferredReturnTypes[0] = odsInferredType0;

Expand All @@ -141,6 +143,9 @@ def OpL2 : NS_Op<"op_with_all_types_constraint",

// CHECK-LABEL: LogicalResult OpL2::inferReturnTypes
// CHECK-NOT: }
// CHECK: if (operands.size() <= 2)
// CHECK-NEXT: return ::mlir::failure();
// CHECK-NOT: if (operands.size() <= 0)
// CHECK: ::mlir::Type odsInferredType0 = operands[2].getType();
// CHECK: ::mlir::Type odsInferredType1 = operands[0].getType();
// CHECK: inferredReturnTypes[0] = odsInferredType0;
Expand All @@ -166,6 +171,8 @@ def OpL4 : NS_Op<"two_inference_edges", [
}

// CHECK-LABEL: LogicalResult OpL4::inferReturnTypes
// CHECK: if (operands.size() <= 0)
// CHECK-NEXT: return ::mlir::failure();
// CHECK: odsInferredType0 = fromInput(operands[0].getType())
// CHECK: odsInferredType1 = infer0(odsInferredType0)
// CHECK: odsInferredType2 = infer1(odsInferredType1)
Expand Down
21 changes: 20 additions & 1 deletion mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3584,6 +3584,24 @@ void OpEmitter::genTypeInterfaceMethods() {
fctx.addSubst("_ctxt", "context");
body << " ::mlir::Builder odsBuilder(context);\n";

// Preprocessing stage to verify all accesses to operands are valid.
int maxAccessedIndex = -1;
for (int i = 0, e = op.getNumResults(); i != e; ++i) {
const InferredResultType &infer = op.getInferredResultType(i);
if (!infer.isArg())
continue;
Operator::OperandOrAttribute arg =
op.getArgToOperandOrAttribute(infer.getIndex());
if (arg.kind() == Operator::OperandOrAttribute::Kind::Operand) {
maxAccessedIndex =
std::max(maxAccessedIndex, arg.operandOrAttributeIndex());
}
}
if (maxAccessedIndex != -1) {
body << " if (operands.size() <= " << Twine(maxAccessedIndex) << ")\n";
body << " return ::mlir::failure();\n";
}

// Process the type inference graph in topological order, starting from types
// that are always fully-inferred: operands and results with constructible
// types. The type inference graph here will always be a DAG, so this gives
Expand All @@ -3600,7 +3618,8 @@ void OpEmitter::genTypeInterfaceMethods() {
if (infer.isArg()) {
// If this is an operand, just index into operand list to access the
// type.
auto arg = op.getArgToOperandOrAttribute(infer.getIndex());
Operator::OperandOrAttribute arg =
op.getArgToOperandOrAttribute(infer.getIndex());
if (arg.kind() == Operator::OperandOrAttribute::Kind::Operand) {
typeStr = ("operands[" + Twine(arg.operandOrAttributeIndex()) +
"].getType()")
Expand Down
Loading