[Mlir-commits] [mlir] 03e6f40 - [mlir] Do not print back 0 alignment in LLVM dialect 'alloca' op

Alex Zinenko llvmlistbot at llvm.org
Mon Oct 26 15:19:27 PDT 2020


Author: Alex Zinenko
Date: 2020-10-26T23:19:20+01:00
New Revision: 03e6f40cdba61e6fbe0fef00e92ae1ebaf8431a7

URL: https://github.com/llvm/llvm-project/commit/03e6f40cdba61e6fbe0fef00e92ae1ebaf8431a7
DIFF: https://github.com/llvm/llvm-project/commit/03e6f40cdba61e6fbe0fef00e92ae1ebaf8431a7.diff

LOG: [mlir] Do not print back 0 alignment in LLVM dialect 'alloca' op

The alignment attribute in the 'alloca' op treats the '0' value as 'unset'.
When parsing the custom form of the 'alloca' op, ignore the alignment attribute
with if its value is '0' instead of actually creating it and producing a
slightly different textually yet equivalent semantically form in the output.

Reviewed By: rriddle

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h
index c745c1dedea3..f2ee83d03f64 100644
--- a/mlir/include/mlir/IR/OperationSupport.h
+++ b/mlir/include/mlir/IR/OperationSupport.h
@@ -257,6 +257,12 @@ class NamedAttrList {
   void set(Identifier name, Attribute value);
   void set(StringRef name, Attribute value);
 
+  /// Erase the attribute with the given name from the list. Return the
+  /// attribute that was erased, or nullptr if there was no attribute with such
+  /// name.
+  Attribute erase(Identifier name);
+  Attribute erase(StringRef name);
+
   const_iterator begin() const { return attrs.begin(); }
   const_iterator end() const { return attrs.end(); }
 
@@ -268,6 +274,9 @@ class NamedAttrList {
   /// Return whether the attributes are sorted.
   bool isSorted() const { return dictionarySorted.getInt(); }
 
+  /// Erase the attribute at the given iterator position.
+  Attribute eraseImpl(SmallVectorImpl<NamedAttribute>::iterator it);
+
   // These are marked mutable as they may be modified (e.g., sorted)
   mutable SmallVector<NamedAttribute, 4> attrs;
   // Pair with cached DictionaryAttr and status of whether attrs is sorted.

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 8da2e0b36300..e67f09f9c167 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -143,6 +143,17 @@ static ParseResult parseAllocaOp(OpAsmParser &parser, OperationState &result) {
       parser.getCurrentLocation(&trailingTypeLoc) || parser.parseType(type))
     return failure();
 
+  Optional<NamedAttribute> alignmentAttr =
+      result.attributes.getNamed("alignment");
+  if (alignmentAttr.hasValue()) {
+    auto alignmentInt = alignmentAttr.getValue().second.dyn_cast<IntegerAttr>();
+    if (!alignmentInt)
+      return parser.emitError(parser.getNameLoc(),
+                              "expected integer alignment");
+    if (alignmentInt.getValue().isNullValue())
+      result.attributes.erase("alignment");
+  }
+
   // Extract the result type from the trailing function type.
   auto funcType = type.dyn_cast<FunctionType>();
   if (!funcType || funcType.getNumInputs() != 1 ||

diff  --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp
index 1ba50f6839d5..0d72ea5f0ea9 100644
--- a/mlir/lib/IR/OperationSupport.cpp
+++ b/mlir/lib/IR/OperationSupport.cpp
@@ -150,6 +150,26 @@ void NamedAttrList::set(StringRef name, Attribute value) {
   return set(mlir::Identifier::get(name, value.getContext()), value);
 }
 
+Attribute
+NamedAttrList::eraseImpl(SmallVectorImpl<NamedAttribute>::iterator it) {
+  if (it == attrs.end())
+    return nullptr;
+
+  // Erasing does not affect the sorted property.
+  Attribute attr = it->second;
+  attrs.erase(it);
+  dictionarySorted.setPointer(nullptr);
+  return attr;
+}
+
+Attribute NamedAttrList::erase(Identifier name) {
+  return eraseImpl(findAttr(attrs, name, isSorted()));
+}
+
+Attribute NamedAttrList::erase(StringRef name) {
+  return eraseImpl(findAttr(attrs, name, isSorted()));
+}
+
 NamedAttrList &
 NamedAttrList::operator=(const SmallVectorImpl<NamedAttribute> &rhs) {
   assign(rhs.begin(), rhs.end());

diff  --git a/mlir/test/Dialect/LLVMIR/invalid.mlir b/mlir/test/Dialect/LLVMIR/invalid.mlir
index 322d5397a417..5a6dfd869a05 100644
--- a/mlir/test/Dialect/LLVMIR/invalid.mlir
+++ b/mlir/test/Dialect/LLVMIR/invalid.mlir
@@ -62,6 +62,13 @@ func @alloca_non_function_type() {
 
 // -----
 
+func @alloca_non_integer_alignment() {
+  // expected-error at +1 {{expected integer alignment}}
+  llvm.alloca %size x !llvm.i32 {alignment = 3.0} : !llvm.ptr<i32>
+}
+
+// -----
+
 func @gep_missing_input_result_type(%pos : !llvm.i64, %base : !llvm.ptr<float>) {
   // expected-error at +1 {{2 operands present, but expected 0}}
   llvm.getelementptr %base[%pos] : () -> ()


        


More information about the Mlir-commits mailing list