[Mlir-commits] [mlir] 6f04011 - [mlir][llvmir] Add support for llvm.signext/zeroext function attributes.
Slava Zakharin
llvmlistbot at llvm.org
Thu Nov 3 09:20:09 PDT 2022
Author: Slava Zakharin
Date: 2022-11-03T09:19:02-07:00
New Revision: 6f04011f15aca2581571989f349b7a3f332bb4f6
URL: https://github.com/llvm/llvm-project/commit/6f04011f15aca2581571989f349b7a3f332bb4f6
DIFF: https://github.com/llvm/llvm-project/commit/6f04011f15aca2581571989f349b7a3f332bb4f6.diff
LOG: [mlir][llvmir] Add support for llvm.signext/zeroext function attributes.
This change-set adds basic support for llvm.signext and llvm.zeroext
attributes, and makes sure that the attributes are translated to LLVM IR
when attached to arguments.
This is needed for https://github.com/llvm/llvm-project/issues/58579
Differential Revision: https://reviews.llvm.org/D137048
Added:
Modified:
mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
mlir/test/Target/LLVMIR/llvmir-invalid.mlir
mlir/test/Target/LLVMIR/llvmir.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
index ec6d565b67b00..cf39f2cb2d49c 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
@@ -49,6 +49,8 @@ def LLVM_Dialect : Dialect {
static StringRef getStructRetAttrName() { return "llvm.sret"; }
static StringRef getInAllocaAttrName() { return "llvm.inalloca"; }
static StringRef getNoUndefAttrName() { return "llvm.noundef"; }
+ static StringRef getSExtAttrName() { return "llvm.signext"; }
+ static StringRef getZExtAttrName() { return "llvm.zeroext"; }
/// Verifies if the attribute is a well-formed value for "llvm.struct_attrs"
static LogicalResult verifyStructAttr(
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 3f808a5d05f7d..7c0e3efe8e0b6 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -984,6 +984,25 @@ LogicalResult ModuleTranslation::convertFunctionSignatures() {
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext())
.addAttribute(llvm::Attribute::NoUndef));
}
+ if (auto attr = function.getArgAttrOfType<UnitAttr>(
+ argIdx, LLVMDialect::getSExtAttrName())) {
+ // llvm.signext can be added to any integer argument type.
+ if (!mlirArgTy.isa<mlir::IntegerType>())
+ return function.emitError(
+ "llvm.signext attribute attached to LLVM non-integer argument");
+ llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext())
+ .addAttribute(llvm::Attribute::SExt));
+ }
+ if (auto attr = function.getArgAttrOfType<UnitAttr>(
+ argIdx, LLVMDialect::getZExtAttrName())) {
+ // llvm.zeroext can be added to any integer argument type.
+ if (!mlirArgTy.isa<mlir::IntegerType>())
+ return function.emitError(
+ "llvm.zeroext attribute attached to LLVM non-integer argument");
+ llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext())
+ .addAttribute(llvm::Attribute::ZExt));
+ }
+
++argIdx;
}
diff --git a/mlir/test/Target/LLVMIR/llvmir-invalid.mlir b/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
index 7217979dbf005..4627df83eb425 100644
--- a/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
@@ -83,6 +83,20 @@ llvm.func @invalid_align(%arg0 : f32 {llvm.align = 4}) -> f32 {
// -----
+// expected-error @below{{llvm.signext attribute attached to LLVM non-integer argument}}
+llvm.func @invalid_signext(%arg0: f32 {llvm.signext}) {
+ "llvm.return"() : () -> ()
+}
+
+// -----
+
+// expected-error @below{{llvm.zeroext attribute attached to LLVM non-integer argument}}
+llvm.func @invalid_zeroext(%arg0: f32 {llvm.zeroext}) {
+ "llvm.return"() : () -> ()
+}
+
+// -----
+
llvm.func @no_non_complex_struct() -> !llvm.array<2 x array<2 x array<2 x struct<(i32)>>>> {
// expected-error @below{{expected struct type to be a complex number}}
%0 = llvm.mlir.constant(dense<[[[1, 2], [3, 4]], [[42, 43], [44, 45]]]> : tensor<2x2x2xi32>) : !llvm.array<2 x array<2 x array<2 x struct<(i32)>>>>
diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir
index 84c750abcfd73..2c4f54c765352 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -1108,6 +1108,22 @@ llvm.func @inallocaattr(%arg0: !llvm.ptr<i32> {llvm.inalloca = i32}) {
// CHECK-LABEL: declare void @inallocaattr_decl(ptr inalloca(i32))
llvm.func @inallocaattr_decl(!llvm.ptr<i32> {llvm.inalloca = i32})
+// CHECK-LABEL: define void @signextattr(i1 signext %
+llvm.func @signextattr(%arg0: i1 {llvm.signext}) {
+ llvm.return
+}
+
+// CHECK-LABEL: declare void @signextattr_decl(i1 signext)
+llvm.func @signextattr_decl(i1 {llvm.signext})
+
+// CHECK-LABEL: define void @zeroextattr(i1 zeroext %
+llvm.func @zeroextattr(%arg0: i1 {llvm.zeroext}) {
+ llvm.return
+}
+
+// CHECK-LABEL: declare void @zeroextattr_decl(i1 zeroext)
+llvm.func @zeroextattr_decl(i1 {llvm.zeroext})
+
// CHECK-LABEL: @llvm_varargs(...)
llvm.func @llvm_varargs(...)
More information about the Mlir-commits
mailing list