[Mlir-commits] [mlir] 5c73db2 - [mlir] disallow side-effecting ops in llvm.mlir.global

Alex Zinenko llvmlistbot at llvm.org
Tue Mar 1 05:16:16 PST 2022


Author: Alex Zinenko
Date: 2022-03-01T14:16:09+01:00
New Revision: 5c73db24df14501e1a76ef2e31111562a3a3287d

URL: https://github.com/llvm/llvm-project/commit/5c73db24df14501e1a76ef2e31111562a3a3287d
DIFF: https://github.com/llvm/llvm-project/commit/5c73db24df14501e1a76ef2e31111562a3a3287d.diff

LOG: [mlir] disallow side-effecting ops in llvm.mlir.global

The llvm.mlir.global operation accepts a region as initializer. This region
corresponds to an LLVM IR constant expression and therefore should not accept
operations with side effects. Add a corresponding verifier.

Reviewed By: wsmoses, bondhugula

Differential Revision: https://reviews.llvm.org/D120632

Added: 
    

Modified: 
    mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
    mlir/test/Dialect/LLVMIR/invalid.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 91a3ff92dc0bc..3e855f5c01dc6 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -1798,6 +1798,13 @@ LogicalResult GlobalOp::verify() {
              << *ret.operand_type_begin() << " does not match global type "
              << getType();
 
+    for (Operation &op : *b) {
+      auto iface = dyn_cast<MemoryEffectOpInterface>(op);
+      if (!iface || !iface.hasNoEffect())
+        return op.emitError()
+               << "ops with side effects not allowed in global initializers";
+    }
+
     if (getValueOrNull())
       return emitOpError("cannot have both initializer value and region");
   }

diff  --git a/mlir/test/Dialect/LLVMIR/invalid.mlir b/mlir/test/Dialect/LLVMIR/invalid.mlir
index bbaef30097f08..c64efeededf59 100644
--- a/mlir/test/Dialect/LLVMIR/invalid.mlir
+++ b/mlir/test/Dialect/LLVMIR/invalid.mlir
@@ -1254,7 +1254,17 @@ func @gep_out_of_bounds(%ptr: !llvm.ptr<struct<(i32, struct<(i32, f32)>)>>, %idx
 // -----
 
 func @non_splat_shuffle_on_scalable_vector(%arg0: vector<[4]xf32>) {
-  // expected-error at +1 {{expected a splat operation for scalable vectors}}
+  // expected-error at below {{expected a splat operation for scalable vectors}}
   %0 = llvm.shufflevector %arg0, %arg0 [0 : i32, 0 : i32, 0 : i32, 1 : i32] : vector<[4]xf32>, vector<[4]xf32>
   return
-}
\ No newline at end of file
+}
+
+// -----
+
+llvm.mlir.global internal @side_effecting_global() : !llvm.struct<(i8)> {
+  %0 = llvm.mlir.constant(1 : i64) : i64
+  // expected-error at below {{ops with side effects not allowed in global initializers}}
+  %1 = llvm.alloca %0 x !llvm.struct<(i8)> : (i64) -> !llvm.ptr<struct<(i8)>>
+  %2 = llvm.load %1 : !llvm.ptr<struct<(i8)>>
+  llvm.return %2 : !llvm.struct<(i8)>
+}


        


More information about the Mlir-commits mailing list