[mlir][FuncToLLVM] Fix arg attr memref interaction
authorChristian Ulmann <christian.ulmann@nextsilicon.com>
Tue, 24 Jan 2023 08:44:29 +0000 (09:44 +0100)
committerChristian Ulmann <christian.ulmann@nextsilicon.com>
Tue, 24 Jan 2023 08:47:59 +0000 (09:47 +0100)
This commit ensures that argument attributes are dropped from types
that are expanded to multiple function arguments. Previously, they
were attached to all arguments belonging to the memref, which
resulted in illegal LLVMIR, e.g., noalias on integers.

Reviewed By: gysit

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

mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
mlir/test/Conversion/FuncToLLVM/convert-argattrs.mlir

index b18bf56..6ff5256 100644 (file)
@@ -354,9 +354,19 @@ protected:
         }
         auto mapping = result.getInputMapping(i);
         assert(mapping && "unexpected deletion of function argument");
+        // Only attach the new argument attributes if there is a one-to-one
+        // mapping from old to new types. Otherwise, attributes might be
+        // attached to types that they do not support.
+        if (mapping->size == 1) {
+          newArgAttrs[mapping->inputNo] =
+              DictionaryAttr::get(rewriter.getContext(), convertedAttrs);
+          continue;
+        }
+        // TODO: Implement custom handling for types that expand to multiple
+        // function arguments.
         for (size_t j = 0; j < mapping->size; ++j)
           newArgAttrs[mapping->inputNo + j] =
-              DictionaryAttr::get(rewriter.getContext(), convertedAttrs);
+              DictionaryAttr::get(rewriter.getContext(), {});
       }
       attributes.push_back(rewriter.getNamedAttr(
           funcOp.getArgAttrsAttrName(), rewriter.getArrayAttr(newArgAttrs)));
index 0e63e62..85c7cbd 100644 (file)
@@ -1,17 +1,20 @@
 // RUN: mlir-opt -convert-func-to-llvm %s | FileCheck %s
 
 // CHECK-LABEL: func @check_attributes
-// When expanding the memref to multiple arguments, argument attributes are replicated.
-// CHECK-COUNT-7: {dialect.a = true, dialect.b = 4 : i64}
-func.func @check_attributes(%static: memref<10x20xf32> {dialect.a = true, dialect.b = 4 : i64 }) {
+// CHECK-SAME: {dialect.a = true, dialect.b = 4 : i64}
+func.func @check_attributes(%int: i64 {dialect.a = true, dialect.b = 4 : i64 }) {
+  return
+}
+
+// CHECK-LABEL: func @check_memref
+// When expanding the memref to multiple arguments, argument attributes should be dropped entirely.
+// CHECK-NOT: {llvm.noalias}
+func.func @check_memref(%static: memref<10x20xf32> {llvm.noalias}) {
   return
 }
 
 // CHECK-LABEL: func @check_multiple
-// Make sure arguments attributes are attached to the right argument. We match
-// commas in the argument list for this purpose.
-// CHECK: %{{.*}}: !llvm{{.*}} {first.arg = true}, %{{.*}}: !llvm{{.*}} {first.arg = true}, %{{.*}}: i{{.*}} {first.arg = true},
-// CHECK-SAME: %{{.*}}: !llvm{{.*}} {second.arg = 42 : i32}, %{{.*}}: !llvm{{.*}} {second.arg = 42 : i32}, %{{.*}}: i{{.*}} {second.arg = 42 : i32})
-func.func @check_multiple(%first: memref<f32> {first.arg = true}, %second: memref<f32> {second.arg = 42 : i32}) {
+// CHECK-SAME: %{{.*}}: f32 {first.arg = true}, %{{.*}}: i64 {second.arg = 42 : i32}
+func.func @check_multiple(%first: f32 {first.arg = true}, %second: i64 {second.arg = 42 : i32}) {
   return
 }