[mlir] Do not print back 0 alignment in LLVM dialect 'alloca' op
authorAlex Zinenko <zinenko@google.com>
Mon, 26 Oct 2020 18:29:28 +0000 (19:29 +0100)
committerAlex Zinenko <zinenko@google.com>
Mon, 26 Oct 2020 22:19:20 +0000 (23:19 +0100)
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

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

index c745c1dedea3bd6c2abc520098f29c41b125e0a0..f2ee83d03f64843d8c7f0082e88ede474324113c 100644 (file)
@@ -257,6 +257,12 @@ public:
   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 @@ private:
   /// 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.
index 8da2e0b363003198b116b4996c479977901c8fdd..e67f09f9c167d6810ce67e50f1f2727f86524f8c 100644 (file)
@@ -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 ||
index 1ba50f6839d516d466113bbbdeb2f6cc4d13d503..0d72ea5f0ea99e848788786d7ae27fdfc8b8a7a5 100644 (file)
@@ -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());
index 322d5397a4176edcca372852f10bafd51d3019f1..5a6dfd869a05bebe3c7b4efb9d223a7f2d617160 100644 (file)
@@ -62,6 +62,13 @@ func @alloca_non_function_type() {
 
 // -----
 
+func @alloca_non_integer_alignment() {
+  // expected-error@+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@+1 {{2 operands present, but expected 0}}
   llvm.getelementptr %base[%pos] : () -> ()