Skip to content

Commit 6f04011

Browse files
committed
[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 llvm#58579 Differential Revision: https://reviews.llvm.org/D137048
1 parent 0eb2f66 commit 6f04011

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def LLVM_Dialect : Dialect {
4949
static StringRef getStructRetAttrName() { return "llvm.sret"; }
5050
static StringRef getInAllocaAttrName() { return "llvm.inalloca"; }
5151
static StringRef getNoUndefAttrName() { return "llvm.noundef"; }
52+
static StringRef getSExtAttrName() { return "llvm.signext"; }
53+
static StringRef getZExtAttrName() { return "llvm.zeroext"; }
5254

5355
/// Verifies if the attribute is a well-formed value for "llvm.struct_attrs"
5456
static LogicalResult verifyStructAttr(

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,25 @@ LogicalResult ModuleTranslation::convertFunctionSignatures() {
984984
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext())
985985
.addAttribute(llvm::Attribute::NoUndef));
986986
}
987+
if (auto attr = function.getArgAttrOfType<UnitAttr>(
988+
argIdx, LLVMDialect::getSExtAttrName())) {
989+
// llvm.signext can be added to any integer argument type.
990+
if (!mlirArgTy.isa<mlir::IntegerType>())
991+
return function.emitError(
992+
"llvm.signext attribute attached to LLVM non-integer argument");
993+
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext())
994+
.addAttribute(llvm::Attribute::SExt));
995+
}
996+
if (auto attr = function.getArgAttrOfType<UnitAttr>(
997+
argIdx, LLVMDialect::getZExtAttrName())) {
998+
// llvm.zeroext can be added to any integer argument type.
999+
if (!mlirArgTy.isa<mlir::IntegerType>())
1000+
return function.emitError(
1001+
"llvm.zeroext attribute attached to LLVM non-integer argument");
1002+
llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext())
1003+
.addAttribute(llvm::Attribute::ZExt));
1004+
}
1005+
9871006
++argIdx;
9881007
}
9891008

mlir/test/Target/LLVMIR/llvmir-invalid.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ llvm.func @invalid_align(%arg0 : f32 {llvm.align = 4}) -> f32 {
8383

8484
// -----
8585

86+
// expected-error @below{{llvm.signext attribute attached to LLVM non-integer argument}}
87+
llvm.func @invalid_signext(%arg0: f32 {llvm.signext}) {
88+
"llvm.return"() : () -> ()
89+
}
90+
91+
// -----
92+
93+
// expected-error @below{{llvm.zeroext attribute attached to LLVM non-integer argument}}
94+
llvm.func @invalid_zeroext(%arg0: f32 {llvm.zeroext}) {
95+
"llvm.return"() : () -> ()
96+
}
97+
98+
// -----
99+
86100
llvm.func @no_non_complex_struct() -> !llvm.array<2 x array<2 x array<2 x struct<(i32)>>>> {
87101
// expected-error @below{{expected struct type to be a complex number}}
88102
%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)>>>>

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,22 @@ llvm.func @inallocaattr(%arg0: !llvm.ptr<i32> {llvm.inalloca = i32}) {
11081108
// CHECK-LABEL: declare void @inallocaattr_decl(ptr inalloca(i32))
11091109
llvm.func @inallocaattr_decl(!llvm.ptr<i32> {llvm.inalloca = i32})
11101110

1111+
// CHECK-LABEL: define void @signextattr(i1 signext %
1112+
llvm.func @signextattr(%arg0: i1 {llvm.signext}) {
1113+
llvm.return
1114+
}
1115+
1116+
// CHECK-LABEL: declare void @signextattr_decl(i1 signext)
1117+
llvm.func @signextattr_decl(i1 {llvm.signext})
1118+
1119+
// CHECK-LABEL: define void @zeroextattr(i1 zeroext %
1120+
llvm.func @zeroextattr(%arg0: i1 {llvm.zeroext}) {
1121+
llvm.return
1122+
}
1123+
1124+
// CHECK-LABEL: declare void @zeroextattr_decl(i1 zeroext)
1125+
llvm.func @zeroextattr_decl(i1 {llvm.zeroext})
1126+
11111127
// CHECK-LABEL: @llvm_varargs(...)
11121128
llvm.func @llvm_varargs(...)
11131129

0 commit comments

Comments
 (0)