Skip to content

Commit 96d60c0

Browse files
authored
[mlir][spirv] Verify matching of entry block arguments and function signature (llvm#133167)
Fixes: llvm#132894
1 parent 16603d8 commit 96d60c0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,24 @@ 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+
for (auto [index, fnArgType, blockArgType] :
1033+
llvm::enumerate(getArgumentTypes(), entryBlock.getArgumentTypes())) {
1034+
if (blockArgType != fnArgType) {
1035+
return emitOpError("type of entry block argument #")
1036+
<< index << '(' << blockArgType
1037+
<< ") must match the type of the corresponding argument in "
1038+
<< "function signature(" << fnArgType << ')';
1039+
}
1040+
}
1041+
}
10241042

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

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,20 @@ 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 @+1 {{'spirv.func' op entry block must have 1 arguments to match function signature}}
80+
spirv.func @f(f32) "None" {
81+
%c0 = arith.constant 0 : index
82+
spirv.Return
83+
}
84+
85+
// -----
86+
87+
// expected-error @+1 {{'spirv.func' op type of entry block argument #0('f64') must match the type of the corresponding argument in function signature('f32')}}
88+
spirv.func @f(f32) "None" {
89+
^bb0(%arg0: f64):
90+
%c0 = arith.constant 0 : index
91+
spirv.Return
92+
}

0 commit comments

Comments
 (0)