[Mlir-commits] [mlir] [mlir][SPIR-V] Reject initializer on Import-linkage GlobalVariable (PR #192302)

Arseniy Obolenskiy llvmlistbot at llvm.org
Wed Apr 15 11:05:23 PDT 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/192302

Per the SPIR-V spec, a module-scope OpVariable with Import linkage must not have an initializer

>From 1b93960e09d7255bd98fa7e124d27da6b96f4418 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 15 Apr 2026 20:04:26 +0200
Subject: [PATCH] [mlir][SPIR-V] Reject initializer on Import-linkage
 GlobalVariable

Per the SPIR-V spec, a module-scope OpVariable with Import linkage must not have an initializer
---
 mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp        | 10 ++++++++++
 mlir/test/Dialect/SPIRV/IR/structure-ops.mlir | 13 +++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
index cecc8c2194237..74686ad72114b 100644
--- a/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
@@ -1339,6 +1339,16 @@ LogicalResult spirv::GlobalVariableOp::verify() {
            << stringifyStorageClass(storageClass) << "'";
   }
 
+  // SPIR-V spec: "If Linkage Type is Import, no further operands are
+  // permitted." — i.e. an Import-linkage variable must not have an initializer.
+  if (auto linkage = getLinkageAttributes()) {
+    if (linkage->getLinkageType().getValue() == spirv::LinkageType::Import &&
+        getInitializer()) {
+      return emitOpError(
+          "with Import linkage type must not have an initializer");
+    }
+  }
+
   if (auto init = (*this)->getAttrOfType<FlatSymbolRefAttr>(
           this->getInitializerAttrName())) {
     Operation *initOp = SymbolTable::lookupNearestSymbolFrom(
diff --git a/mlir/test/Dialect/SPIRV/IR/structure-ops.mlir b/mlir/test/Dialect/SPIRV/IR/structure-ops.mlir
index e12b70cc5c139..991530458bd26 100644
--- a/mlir/test/Dialect/SPIRV/IR/structure-ops.mlir
+++ b/mlir/test/Dialect/SPIRV/IR/structure-ops.mlir
@@ -589,6 +589,19 @@ spirv.module Logical GLSL450 {
 
 // -----
 
+spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, Linkage], []> {
+  spirv.SpecConstant @sc = 1.0 : f32
+  // expected-error @+1 {{op with Import linkage type must not have an initializer}}
+  spirv.GlobalVariable @var0 initializer(@sc) {
+    linkage_attributes = #spirv.linkage_attributes<
+      linkage_name = "importedVar",
+      linkage_type = <Import>
+    >
+  } : !spirv.ptr<f32, Private>
+}
+
+// -----
+
 spirv.module Logical GLSL450 {
   spirv.func @foo() "None" {
     // expected-error @+1 {{op must appear in a module-like op's block}}



More information about the Mlir-commits mailing list