[Mlir-commits] [mlir] 2416e28 - [mlir] Add support for alignment annotations to the LLVM dialect to LLVM translation.
Stephan Herhut
llvmlistbot at llvm.org
Fri Jun 19 07:37:21 PDT 2020
Author: Stephan Herhut
Date: 2020-06-19T16:36:06+02:00
New Revision: 2416e28c2585e4264bc108f051ea2f1ffc2c2035
URL: https://github.com/llvm/llvm-project/commit/2416e28c2585e4264bc108f051ea2f1ffc2c2035
DIFF: https://github.com/llvm/llvm-project/commit/2416e28c2585e4264bc108f051ea2f1ffc2c2035.diff
LOG: [mlir] Add support for alignment annotations to the LLVM dialect to LLVM translation.
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
Added:
Modified:
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
Removed:
################################################################################
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 9fd8bfe6d26e..af9b1ca08b47 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -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();
}
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index fea50ce3f42e..32880a9d2374 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -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++;
}
diff --git a/mlir/test/Dialect/LLVMIR/invalid.mlir b/mlir/test/Dialect/LLVMIR/invalid.mlir
index 0562cd4701e2..c97a4acdf6b4 100644
--- a/mlir/test/Dialect/LLVMIR/invalid.mlir
+++ b/mlir/test/Dialect/LLVMIR/invalid.mlir
@@ -5,6 +5,13 @@ func @invalid_noalias(%arg0: !llvm.i32 {llvm.noalias = 3}) {
"llvm.return"() : () -> ()
}
+// -----
+
+// expected-error at +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.
diff --git a/mlir/test/Target/llvmir-invalid.mlir b/mlir/test/Target/llvmir-invalid.mlir
index 692975b2cf08..5effcdc60d37 100644
--- a/mlir/test/Target/llvmir-invalid.mlir
+++ b/mlir/test/Target/llvmir-invalid.mlir
@@ -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}]]]">
diff --git a/mlir/test/Target/llvmir.mlir b/mlir/test/Target/llvmir.mlir
index a052203c8ba8..e1bea72cf147 100644
--- a/mlir/test/Target/llvmir.mlir
+++ b/mlir/test/Target/llvmir.mlir
@@ -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(...)
More information about the Mlir-commits
mailing list