[mlir][spirv] Check GlobalVariableOp result to be of pointer types

Querying the storage class in the verifier will assume the result
type is of pointer types. We need to check that's true first to
make sure it won't crash.

Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D139053
This commit is contained in:
Lei Zhang 2022-11-30 19:07:19 -05:00
parent 1ee3bb17c3
commit b948a9f40f
2 changed files with 10 additions and 0 deletions

View File

@ -2672,6 +2672,9 @@ void spirv::GlobalVariableOp::print(OpAsmPrinter &printer) {
} }
LogicalResult spirv::GlobalVariableOp::verify() { LogicalResult spirv::GlobalVariableOp::verify() {
if (!getType().isa<spirv::PointerType>())
return emitOpError("result must be of a !spv.ptr type");
// SPIR-V spec: "Storage Class is the Storage Class of the memory holding the // SPIR-V spec: "Storage Class is the Storage Class of the memory holding the
// object. It cannot be Generic. It must be the same as the Storage Class // object. It cannot be Generic. It must be the same as the Storage Class
// operand of the Result Type." // operand of the Result Type."

View File

@ -369,6 +369,13 @@ spirv.module Logical GLSL450 {
// ----- // -----
spirv.module Logical GLSL450 {
// expected-error @+1 {{result must be of a !spv.ptr type}}
"spirv.GlobalVariable"() {sym_name = "var0", type = none} : () -> ()
}
// -----
spirv.module Logical GLSL450 { spirv.module Logical GLSL450 {
// expected-error @+1 {{op initializer must be result of a spirv.SpecConstant or spirv.GlobalVariable op}} // expected-error @+1 {{op initializer must be result of a spirv.SpecConstant or spirv.GlobalVariable op}}
spirv.GlobalVariable @var0 initializer(@var1) : !spirv.ptr<f32, Private> spirv.GlobalVariable @var0 initializer(@var1) : !spirv.ptr<f32, Private>