[mlir][LLVM] Fix empty res attr import
authorChristian Ulmann <christian.ulmann@nextsilicon.com>
Thu, 22 Jun 2023 14:53:21 +0000 (14:53 +0000)
committerChristian Ulmann <christian.ulmann@nextsilicon.com>
Thu, 22 Jun 2023 16:07:23 +0000 (16:07 +0000)
This commit ensures that an empty list of result attributes is not
imported as an empty `ArrayAttr`. Instead, the attribute is just not
added to the `LLVMFuncOp`.

Reviewed By: gysit

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

mlir/lib/Target/LLVMIR/ModuleImport.cpp
mlir/test/Target/LLVMIR/Import/function-attributes-generic.ll [new file with mode: 0644]

index e8ffee3..889555e 100644 (file)
@@ -761,8 +761,8 @@ Attribute ModuleImport::getConstantAsAttr(llvm::Constant *constant) {
 
   // Returns the static shape of the provided type if possible.
   auto getConstantShape = [&](llvm::Type *type) {
-    return llvm::dyn_cast_if_present<ShapedType>(getBuiltinTypeForAttr(convertType(type))
-        );
+    return llvm::dyn_cast_if_present<ShapedType>(
+        getBuiltinTypeForAttr(convertType(type)));
   };
 
   // Convert one-dimensional constant arrays or vectors that store 1/2/4/8-byte
@@ -829,8 +829,8 @@ Attribute ModuleImport::getConstantAsAttr(llvm::Constant *constant) {
 
   // Convert zero aggregates.
   if (auto *constZero = dyn_cast<llvm::ConstantAggregateZero>(constant)) {
-    auto shape = llvm::dyn_cast_if_present<ShapedType>(getBuiltinTypeForAttr(convertType(constZero->getType()))
-                     );
+    auto shape = llvm::dyn_cast_if_present<ShapedType>(
+        getBuiltinTypeForAttr(convertType(constZero->getType())));
     if (!shape)
       return {};
     // Convert zero aggregates with a static shape to splat elements attributes.
@@ -1683,6 +1683,8 @@ void ModuleImport::convertParameterAttributes(llvm::Function *func,
   // Convert the result attributes and attach them wrapped in an ArrayAttribute
   // to the funcOp.
   llvm::AttributeSet llvmResAttr = llvmAttrs.getRetAttrs();
+  if (!llvmResAttr.hasAttributes())
+    return;
   funcOp.setResAttrsAttr(
       builder.getArrayAttr(convertParameterAttribute(llvmResAttr, builder)));
 }
diff --git a/mlir/test/Target/LLVMIR/Import/function-attributes-generic.ll b/mlir/test/Target/LLVMIR/Import/function-attributes-generic.ll
new file mode 100644 (file)
index 0000000..d7f8400
--- /dev/null
@@ -0,0 +1,9 @@
+; RUN: mlir-translate -import-llvm -split-input-file %s --mlir-print-op-generic | FileCheck %s
+
+; Ensure that no empty parameter attribute lists are created.
+; CHECK: "llvm.func"
+; CHECK-SAME:  <{
+; CHECK-NOT:  arg_attr
+; CHECK-NOT:  res_attrs
+; CHECK-SAME:  }>
+declare ptr @func_no_param_attrs()