[Mlir-commits] [mlir] [mlir][llvm] Use zeroinitializer for TargetExtType (PR #66510)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Sep 15 06:27:38 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-llvm
            
<details>
<summary>Changes</summary>
Use the recently introduced llvm.mlir.zero operation for values with LLVM target extension type. Replaces the previous workaround that uses a single zero-valued integer attribute constant operation.
--
Full diff: https://github.com/llvm/llvm-project/pull/66510.diff

7 Files Affected:

- (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td (+1) 
- (modified) mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp (+18-17) 
- (modified) mlir/lib/Target/LLVMIR/ModuleImport.cpp (+3-10) 
- (modified) mlir/test/Dialect/LLVMIR/global.mlir (+10-4) 
- (modified) mlir/test/Dialect/LLVMIR/invalid.mlir (+6-4) 
- (modified) mlir/test/Target/LLVMIR/Import/target-ext-type.ll (+2-2) 
- (modified) mlir/test/Target/LLVMIR/target-ext-type.mlir (+2-2) 


<pre>
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 57dce72e102e7a4..726349597aa2556 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -1530,6 +1530,7 @@ def LLVM_ZeroOp
   let results = (outs LLVM_Type:$res);
   let builders = [LLVM_OneResultOpBuilder];
   let assemblyFormat = &quot;attr-dict `:` type($res)&quot;;
+  let hasVerifier = 1;
 }
 
 def LLVM_ConstantOp
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index c3575d299b3888a..9b7d17ef77ea272 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -2051,13 +2051,8 @@ LogicalResult GlobalOp::verify() {
              &lt;&lt; &quot;this target extension type cannot be used in a global&quot;;
 
     if (Attribute value = getValueOrNull()) {
-      // Only a single, zero integer attribute (=zeroinitializer) is allowed for
-      // a global value with TargetExtType.
-      // TODO: Replace with &#x27;zeroinitializer&#x27; once there is a dedicated
-      // zeroinitializer operation in the LLVM dialect.
-      if (!isa&lt;IntegerAttr&gt;(value) || !isZeroAttribute(value))
-        return emitOpError()
-               &lt;&lt; &quot;expected zero value for global with target extension type&quot;;
+      return emitOpError() &lt;&lt; &quot;global with target extension type can only be &quot;
+                              &quot;initialized with zero-initializer&quot;;
     }
   }
 
@@ -2510,6 +2505,21 @@ Region *LLVMFuncOp::getCallableRegion() {
   return &amp;getBody();
 }
 
+//===----------------------------------------------------------------------===//
+// ZeroOp.
+//===----------------------------------------------------------------------===//
+
+LogicalResult LLVM::ZeroOp::verify() {
+  if (auto targetExtType = dyn_cast&lt;LLVMTargetExtType&gt;(getType())) {
+    if (!targetExtType.hasProperty(LLVM::LLVMTargetExtType::HasZeroInit))
+      return emitOpError()
+             &lt;&lt; &quot;target extension type does not support zero-initializer&quot;;
+
+    return success();
+  }
+  return success();
+}
+
 //===----------------------------------------------------------------------===//
 // ConstantOp.
 //===----------------------------------------------------------------------===//
@@ -2554,16 +2564,7 @@ LogicalResult LLVM::ConstantOp::verify() {
     return success();
   }
   if (auto targetExtType = dyn_cast&lt;LLVMTargetExtType&gt;(getType())) {
-    if (!targetExtType.hasProperty(LLVM::LLVMTargetExtType::HasZeroInit))
-      return emitOpError()
-             &lt;&lt; &quot;target extension type does not support zero-initializer&quot;;
-    // Only a single, zero integer attribute (=zeroinitializer) is allowed for a
-    // global value with TargetExtType.
-    if (!isa&lt;IntegerAttr&gt;(getValue()) || !isZeroAttribute(getValue()))
-      return emitOpError()
-             &lt;&lt; &quot;only zero-initializer allowed for target extension types&quot;;
-
-    return success();
+    return emitOpError() &lt;&lt; &quot;target extension type does not support constants.&quot;;
   }
   if (!llvm::isa&lt;IntegerAttr, ArrayAttr, FloatAttr, ElementsAttr&gt;(getValue()))
     return emitOpError()
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 35b2fcd3d3abe4e..0096a1dfee519e9 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -1077,16 +1077,9 @@ FailureOr&lt;Value&gt; ModuleImport::convertConstant(llvm::Constant *constant) {
         cast&lt;LLVMTargetExtType&gt;(convertType(constTargetNone-&gt;getType()));
     assert(targetExtType.hasProperty(LLVMTargetExtType::HasZeroInit) &amp;&amp;
            &quot;target extension type does not support zero-initialization&quot;);
-    // As the number of values needed for initialization is target-specific and
-    // opaque to the compiler, use a single i64 zero-valued attribute to
-    // represent the &#x27;zeroinitializer&#x27;, which is the only constant value allowed
-    // for target extension types (besides poison and undef).
-    // TODO: Replace with &#x27;zeroinitializer&#x27; once there is a dedicated
-    // zeroinitializer operation in the LLVM dialect.
-    return builder
-        .create&lt;LLVM::ConstantOp&gt;(loc, targetExtType,
-                                  builder.getI64IntegerAttr(0))
-        .getRes();
+    // Create llvm.mlir.zero operation to represent zero-initialization of
+    // target extension type.
+    return builder.create&lt;LLVM::ZeroOp&gt;(loc, targetExtType).getRes();
   }
 
   StringRef error = &quot;&quot;;
diff --git a/mlir/test/Dialect/LLVMIR/global.mlir b/mlir/test/Dialect/LLVMIR/global.mlir
index e653ec48d5679bf..daa53228995fcb8 100644
--- a/mlir/test/Dialect/LLVMIR/global.mlir
+++ b/mlir/test/Dialect/LLVMIR/global.mlir
@@ -246,10 +246,16 @@ llvm.mlir.global_dtors { dtors = [@dtor], priorities = [0 : i32]}
 // CHECK: llvm.mlir.global external @target_ext() {addr_space = 0 : i32} : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
 llvm.mlir.global @target_ext() : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
 
-// CHECK: llvm.mlir.global external @target_ext_init(0 : i64) {addr_space = 0 : i32} : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
-llvm.mlir.global @target_ext_init(0 : i64) : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
+// CHECK:       llvm.mlir.global external @target_ext_init() {addr_space = 0 : i32} : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
+// CHECK-NEXT:    %0 = llvm.mlir.zero : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
+// CHECK-NEXT:    llvm.return %0 : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
+// CHECK-NEXT:  } 
+llvm.mlir.global @target_ext_init() : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt; {
+  %0 = llvm.mlir.zero : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
+  llvm.return %0 : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
+}
 
 // -----
 
-// expected-error @+1 {{expected zero value for global with target extension type}}
-llvm.mlir.global @target_fail(1 : i64) : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
+// expected-error @+1 {{global with target extension type can only be initialized with zero-initializer}}
+llvm.mlir.global @target_fail(0 : i64) : !llvm.target&lt;&quot;spirv.Image&quot;, i32, 0&gt;
diff --git a/mlir/test/Dialect/LLVMIR/invalid.mlir b/mlir/test/Dialect/LLVMIR/invalid.mlir
index cf4697b17aa468a..02bbb019542632c 100644
--- a/mlir/test/Dialect/LLVMIR/invalid.mlir
+++ b/mlir/test/Dialect/LLVMIR/invalid.mlir
@@ -1336,16 +1336,18 @@ func.func @invalid_target_ext_atomic(%arg0 : !llvm.ptr) {
 
 // -----
 
-func.func @invalid_target_ext_constant() {
+func.func @invalid_target_ext_constant_unsupported() {
   // expected-error at +1 {{target extension type does not support zero-initializer}}
-  %0 = llvm.mlir.constant(0 : index) : !llvm.target&lt;&quot;invalid_constant&quot;&gt;
+  %0 = llvm.mlir.zero : !llvm.target&lt;&quot;invalid_constant&quot;&gt;
+  llvm.return
 }
 
 // -----
 
 func.func @invalid_target_ext_constant() {
-  // expected-error at +1 {{only zero-initializer allowed for target extension types}}
-  %0 = llvm.mlir.constant(42 : index) : !llvm.target&lt;&quot;spirv.Event&quot;&gt;
+  // expected-error at +1 {{target extension type does not support constants.}}
+  %0 = llvm.mlir.constant(0 : index) : !llvm.target&lt;&quot;spirv.Event&quot;&gt;
+  llvm.return
 }
 
 // -----
diff --git a/mlir/test/Target/LLVMIR/Import/target-ext-type.ll b/mlir/test/Target/LLVMIR/Import/target-ext-type.ll
index 62194cad9152c75..3c575b71038bf0d 100644
--- a/mlir/test/Target/LLVMIR/Import/target-ext-type.ll
+++ b/mlir/test/Target/LLVMIR/Import/target-ext-type.ll
@@ -2,7 +2,7 @@
 
 ; CHECK-LABEL: llvm.mlir.global external @global() {addr_space = 0 : i32}
 ; CHECK-SAME:    !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
-; CHECK-NEXT:      %0 = llvm.mlir.constant(0 : i64) : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
+; CHECK-NEXT:      %0 = llvm.mlir.zero : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
 ; CHECK-NEXT:      llvm.return %0 : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
 @global = global target(&quot;spirv.DeviceEvent&quot;) zeroinitializer
 
@@ -45,7 +45,7 @@ define target(&quot;spirv.Event&quot;) @func2() {
 
 ; CHECK-LABEL: llvm.func @func3()
 define void @func3() {
-  ; CHECK-NEXT:    %0 = llvm.mlir.constant(0 : i64) : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
+  ; CHECK-NEXT:    %0 = llvm.mlir.zero : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
   ; CHECK-NEXT:    %1 = llvm.freeze %0 : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
   %val = freeze target(&quot;spirv.DeviceEvent&quot;) zeroinitializer
   ; CHECK-NEXT:    llvm.return
diff --git a/mlir/test/Target/LLVMIR/target-ext-type.mlir b/mlir/test/Target/LLVMIR/target-ext-type.mlir
index e7004b2699dc6b1..6b2d2ea3d4c2318 100644
--- a/mlir/test/Target/LLVMIR/target-ext-type.mlir
+++ b/mlir/test/Target/LLVMIR/target-ext-type.mlir
@@ -2,7 +2,7 @@
 
 // CHECK: @global = global target(&quot;spirv.DeviceEvent&quot;) zeroinitializer
 llvm.mlir.global external @global() {addr_space = 0 : i32} : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt; {
-  %0 = llvm.mlir.constant(0 : i64) : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
+  %0 = llvm.mlir.zero : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
   llvm.return %0 : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
 }
 
@@ -22,7 +22,7 @@ llvm.func @func2() -&gt; !llvm.target&lt;&quot;spirv.Event&quot;&gt; {
 // CHECK-NEXT:    %1 = freeze target(&quot;spirv.DeviceEvent&quot;) zeroinitializer
 // CHECK-NEXT:    ret void
 llvm.func @func3() {
-  %0 = llvm.mlir.constant(0 : i64) : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
+  %0 = llvm.mlir.zero : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
   %1 = llvm.freeze %0 : !llvm.target&lt;&quot;spirv.DeviceEvent&quot;&gt;
   llvm.return
 }
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66510


More information about the Mlir-commits mailing list