[mlir] Add support for alignment annotations to the LLVM dialect to LLVM translation.
authorStephan Herhut <herhut@google.com>
Fri, 19 Jun 2020 08:59:07 +0000 (10:59 +0200)
committerStephan Herhut <herhut@google.com>
Fri, 19 Jun 2020 14:36:06 +0000 (16:36 +0200)
Summary:
With this change, a function argument attribute of the form
"llvm.align" = <int> will be translated to the corresponding align
attribute in LLVM by the ModuleConversion.

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

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
mlir/test/Dialect/LLVMIR/invalid.mlir
mlir/test/Target/llvmir-invalid.mlir
mlir/test/Target/llvmir.mlir

index 9fd8bfe..af9b1ca 100644 (file)
@@ -1719,6 +1719,10 @@ LogicalResult LLVMDialect::verifyRegionArgAttribute(Operation *op,
   if (argAttr.first == "llvm.noalias" && !argAttr.second.isa<BoolAttr>())
     return op->emitError()
            << "llvm.noalias argument attribute of non boolean type";
+  // Check that llvm.align is an integer attribute.
+  if (argAttr.first == "llvm.align" && !argAttr.second.isa<IntegerAttr>())
+    return op->emitError()
+           << "llvm.align argument attribute of non integer type";
   return success();
 }
 
index fea50ce..32880a9 100644 (file)
@@ -724,6 +724,18 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
       if (attr.getValue())
         llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias);
     }
+
+    if (auto attr = func.getArgAttrOfType<IntegerAttr>(argIdx, "llvm.align")) {
+      // NB: Attribute already verified to be int, so check if we can indeed
+      // attach the attribute to this argument, based on its type.
+      auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>();
+      if (!argTy.getUnderlyingType()->isPointerTy())
+        return func.emitError(
+            "llvm.align attribute attached to LLVM non-pointer argument");
+      llvmArg.addAttrs(
+          llvm::AttrBuilder().addAlignmentAttr(llvm::Align(attr.getInt())));
+    }
+
     valueMapping[mlirArg] = &llvmArg;
     argIdx++;
   }
index 0562cd4..c97a4ac 100644 (file)
@@ -5,6 +5,13 @@ func @invalid_noalias(%arg0: !llvm.i32 {llvm.noalias = 3}) {
   "llvm.return"() : () -> ()
 }
 
+// -----
+
+// expected-error@+1{{llvm.align argument attribute of non integer type}}
+func @invalid_align(%arg0: !llvm.i32 {llvm.align = "foo"}) {
+  "llvm.return"() : () -> ()
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 
 // Check that parser errors are properly produced and do not crash the compiler.
index 692975b..5effcdc 100644 (file)
@@ -7,6 +7,20 @@ func @foo() {
 
 // -----
 
+// expected-error @+1 {{llvm.noalias attribute attached to LLVM non-pointer argument}}
+llvm.func @invalid_noalias(%arg0 : !llvm.float {llvm.noalias = true}) -> !llvm.float {
+  llvm.return %arg0 : !llvm.float
+}
+
+// -----
+
+// expected-error @+1 {{llvm.align attribute attached to LLVM non-pointer argument}}
+llvm.func @invalid_align(%arg0 : !llvm.float {llvm.align = 4}) -> !llvm.float {
+  llvm.return %arg0 : !llvm.float
+}
+
+// -----
+
 llvm.func @no_nested_struct() -> !llvm<"[2 x [2 x [2 x {i32}]]]"> {
   // expected-error @+1 {{struct types are not supported in constants}}
   %0 = llvm.mlir.constant(dense<[[[1, 2], [3, 4]], [[42, 43], [44, 45]]]> : tensor<2x2x2xi32>) : !llvm<"[2 x [2 x [2 x {i32}]]]">
index a052203..e1bea72 100644 (file)
@@ -927,6 +927,11 @@ llvm.func @llvm_noalias(%arg0: !llvm<"float*"> {llvm.noalias = true}) {
   llvm.return
 }
 
+// CHECK-LABEL: define void @llvm_align(float* align 4 {{%*.}})
+llvm.func @llvm_align(%arg0: !llvm<"float*"> {llvm.align = 4}) {
+  llvm.return
+}
+
 // CHECK-LABEL: @llvm_varargs(...)
 llvm.func @llvm_varargs(...)