Skip to content

Commit 6076904

Browse files
committed
Verify entry block in SPIRV dialect
1 parent a415b7f commit 6076904

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,25 @@ LogicalResult spirv::FuncOp::verifyType() {
10211021

10221022
LogicalResult spirv::FuncOp::verifyBody() {
10231023
FunctionType fnType = getFunctionType();
1024+
if (!isExternal()) {
1025+
Block &entryBlock = front();
1026+
1027+
unsigned numArguments = this->getNumArguments();
1028+
if (entryBlock.getNumArguments() != numArguments)
1029+
return emitOpError("entry block must have ")
1030+
<< numArguments << " arguments to match function signature";
1031+
1032+
ArrayRef<Type> fnInputTypes = getArgumentTypes();
1033+
for (unsigned i = 0, e = numArguments; i != e; ++i) {
1034+
Type argType = entryBlock.getArgument(i).getType();
1035+
if (fnInputTypes[i] != argType) {
1036+
return emitOpError("type of entry block argument #")
1037+
<< i << '(' << argType
1038+
<< ") must match the type of the corresponding argument in "
1039+
<< "function signature(" << fnInputTypes[i] << ')';
1040+
}
1041+
}
1042+
}
10241043

10251044
auto walkResult = walk([fnType](Operation *op) -> WalkResult {
10261045
if (auto retOp = dyn_cast<spirv::ReturnOp>(op)) {

mlir/test/Dialect/SPIRV/IR/function-decorations.mlir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,24 @@ spirv.func @no_decoration_name_attr(%arg0 : !spirv.ptr<i32, PhysicalStorageBuffe
7373
spirv.func @no_decoration_name_attr(%arg0 : !spirv.ptr<i32, PhysicalStorageBuffer> { spirv.decoration = #spirv.decoration<Restrict>, random_attr = #spirv.decoration<Aliased> }) "None" {
7474
spirv.Return
7575
}
76+
77+
// -----
78+
79+
// expected-error @+2 {{'spirv.func' op entry block must have 1 arguments to match function signature}}
80+
module {
81+
spirv.func @f(f32) "None" {
82+
%c0 = arith.constant 0 : index
83+
spirv.Return
84+
}
85+
}
86+
87+
// -----
88+
89+
// expected-error @+2 {{'spirv.func' op type of entry block argument #0('f64') must match the type of the corresponding argument in function signature('f32')}}
90+
module {
91+
spirv.func @f(f32) "None" {
92+
^bb0(%arg0: f64):
93+
%c0 = arith.constant 0 : index
94+
spirv.Return
95+
}
96+
}

0 commit comments

Comments
 (0)