Index: flang/lib/Lower/IntrinsicCall.cpp =================================================================== --- flang/lib/Lower/IntrinsicCall.cpp +++ flang/lib/Lower/IntrinsicCall.cpp @@ -506,6 +506,8 @@ fir::ExtendedValue genLbound(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genLen(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genLenTrim(mlir::Type, llvm::ArrayRef); + template + mlir::Value genMask(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genMatmul(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genMaxloc(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genMaxval(mlir::Type, llvm::ArrayRef); @@ -791,6 +793,8 @@ {"lgt", &I::genCharacterCompare}, {"lle", &I::genCharacterCompare}, {"llt", &I::genCharacterCompare}, + {"maskl", &I::genMask}, + {"maskr", &I::genMask}, {"matmul", &I::genMatmul, {{{"matrix_a", asAddr}, {"matrix_b", asAddr}}}, @@ -3175,6 +3179,37 @@ fir::getBase(args[1]), fir::getLen(args[1])); } +// MASKL, MASKR +template +mlir::Value IntrinsicLibrary::genMask(mlir::Type resultType, + llvm::ArrayRef args) { + assert(args.size() == 2); + + mlir::Value zero = builder.createIntegerConstant(loc, resultType, 0); + mlir::Value ones = builder.createIntegerConstant(loc, resultType, -1); + mlir::Value bitsToSet = builder.createConvert(loc, resultType, args[0]); + mlir::Value bitSize = builder.createIntegerConstant( + loc, resultType, resultType.getIntOrFloatBitWidth()); + if (args[1]) { + mlir::Value eight = builder.createIntegerConstant(loc, resultType, 8); + mlir::Value kind = builder.createConvert(loc, resultType, args[1]); + bitSize = builder.create(loc, kind, eight); + } + + mlir::Value tooSmall = builder.create( + loc, mlir::arith::CmpIPredicate::sle, bitsToSet, zero); + mlir::Value tooLarge = builder.create( + loc, mlir::arith::CmpIPredicate::sgt, bitsToSet, bitSize); + mlir::Value outOfBounds = + builder.create(loc, tooSmall, tooLarge); + + mlir::Value shift = + builder.create(loc, bitSize, bitsToSet); + mlir::Value result = builder.create(loc, ones, shift); + + return builder.create(loc, outOfBounds, zero, result); +} + // MATMUL fir::ExtendedValue IntrinsicLibrary::genMatmul(mlir::Type resultType, Index: flang/test/Lower/Intrinsics/maskl.f90 =================================================================== --- /dev/null +++ flang/test/Lower/Intrinsics/maskl.f90 @@ -0,0 +1,110 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! CHECK-LABEL: maskl_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskl_test(a, b) + integer :: a + integer :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskl(a) + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i32 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i32 + ! CHECK: %[[C_32:.*]] = arith.constant 32 : i32 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_VAL]], %[[C_0]] : i32 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_VAL]], %[[C_32]] : i32 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[C_32]], %[[A_VAL]] : i32 + ! CHECK: %[[SHIFT:.*]] = arith.shli %[[C__1]], %[[LEN]] : i32 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i32 +end subroutine maskl_test + +! CHECK-LABEL: maskl1_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskl1_test(a, b) + integer :: a + integer(kind=1) :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskl(a, 1) + ! CHECK: %[[C_K:.*]] = arith.constant 1 : i32 + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i8 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i8 + ! CHECK: %[[A_CONV:.*]] = fir.convert %[[A_VAL]] : (i32) -> i8 + ! CHECK: %[[C_8:.*]] = arith.constant 8 : i8 + ! CHECK: %[[C_K_CONV:.*]] = fir.convert %[[C_K]] : (i32) -> i8 + ! CHECK: %[[BITS:.*]] = arith.muli %[[C_K_CONV]], %[[C_8]] : i8 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_CONV]], %[[C_0]] : i8 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_CONV]], %[[BITS]] : i8 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[BITS]], %[[A_CONV]] : i8 + ! CHECK: %[[SHIFT:.*]] = arith.shli %[[C__1]], %[[LEN]] : i8 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i8 +end subroutine maskl1_test + +! CHECK-LABEL: maskl2_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskl2_test(a, b) + integer :: a + integer(kind=2) :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskl(a, 2) + ! CHECK: %[[C_K:.*]] = arith.constant 2 : i32 + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i16 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i16 + ! CHECK: %[[A_CONV:.*]] = fir.convert %[[A_VAL]] : (i32) -> i16 + ! CHECK: %[[C_8:.*]] = arith.constant 8 : i16 + ! CHECK: %[[C_K_CONV:.*]] = fir.convert %[[C_K]] : (i32) -> i16 + ! CHECK: %[[BITS:.*]] = arith.muli %[[C_K_CONV]], %[[C_8]] : i16 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_CONV]], %[[C_0]] : i16 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_CONV]], %[[BITS]] : i16 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[BITS]], %[[A_CONV]] : i16 + ! CHECK: %[[SHIFT:.*]] = arith.shli %[[C__1]], %[[LEN]] : i16 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i16 +end subroutine maskl2_test + +! CHECK-LABEL: maskl4_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskl4_test(a, b) + integer :: a + integer(kind=4) :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskl(a, 4) + ! CHECK: %[[C_K:.*]] = arith.constant 4 : i32 + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i32 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i32 + ! CHECK: %[[C_8:.*]] = arith.constant 8 : i32 + ! CHECK: %[[BITS:.*]] = arith.muli %[[C_K]], %[[C_8]] : i32 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_VAL]], %[[C_0]] : i32 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_VAL]], %[[BITS]] : i32 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[BITS]], %[[A_VAL]] : i32 + ! CHECK: %[[SHIFT:.*]] = arith.shli %[[C__1]], %[[LEN]] : i32 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i32 +end subroutine maskl4_test + +! CHECK-LABEL: maskl8_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskl8_test(a, b) + integer :: a + integer(kind=8) :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskl(a, 8) + ! CHECK: %[[C_K:.*]] = arith.constant 8 : i32 + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i64 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i64 + ! CHECK: %[[A_CONV:.*]] = fir.convert %[[A_VAL]] : (i32) -> i64 + ! CHECK: %[[C_8:.*]] = arith.constant 8 : i64 + ! CHECK: %[[C_K_CONV:.*]] = fir.convert %[[C_K]] : (i32) -> i64 + ! CHECK: %[[BITS:.*]] = arith.muli %[[C_K_CONV]], %[[C_8]] : i64 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_CONV]], %[[C_0]] : i64 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_CONV]], %[[BITS]] : i64 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[BITS]], %[[A_CONV]] : i64 + ! CHECK: %[[SHIFT:.*]] = arith.shli %[[C__1]], %[[LEN]] : i64 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i64 +end subroutine maskl8_test Index: flang/test/Lower/Intrinsics/maskr.f90 =================================================================== --- /dev/null +++ flang/test/Lower/Intrinsics/maskr.f90 @@ -0,0 +1,110 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! CHECK-LABEL: maskr_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskr_test(a, b) + integer :: a + integer :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskr(a) + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i32 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i32 + ! CHECK: %[[C_32:.*]] = arith.constant 32 : i32 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_VAL]], %[[C_0]] : i32 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_VAL]], %[[C_32]] : i32 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[C_32]], %[[A_VAL]] : i32 + ! CHECK: %[[SHIFT:.*]] = arith.shrui %[[C__1]], %[[LEN]] : i32 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i32 +end subroutine maskr_test + +! CHECK-LABEL: maskr1_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskr1_test(a, b) + integer :: a + integer(kind=1) :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskr(a, 1) + ! CHECK: %[[C_K:.*]] = arith.constant 1 : i32 + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i8 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i8 + ! CHECK: %[[A_CONV:.*]] = fir.convert %[[A_VAL]] : (i32) -> i8 + ! CHECK: %[[C_8:.*]] = arith.constant 8 : i8 + ! CHECK: %[[C_K_CONV:.*]] = fir.convert %[[C_K]] : (i32) -> i8 + ! CHECK: %[[BITS:.*]] = arith.muli %[[C_K_CONV]], %[[C_8]] : i8 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_CONV]], %[[C_0]] : i8 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_CONV]], %[[BITS]] : i8 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[BITS]], %[[A_CONV]] : i8 + ! CHECK: %[[SHIFT:.*]] = arith.shrui %[[C__1]], %[[LEN]] : i8 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i8 +end subroutine maskr1_test + +! CHECK-LABEL: maskr2_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskr2_test(a, b) + integer :: a + integer(kind=2) :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskr(a, 2) + ! CHECK: %[[C_K:.*]] = arith.constant 2 : i32 + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i16 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i16 + ! CHECK: %[[A_CONV:.*]] = fir.convert %[[A_VAL]] : (i32) -> i16 + ! CHECK: %[[C_8:.*]] = arith.constant 8 : i16 + ! CHECK: %[[C_K_CONV:.*]] = fir.convert %[[C_K]] : (i32) -> i16 + ! CHECK: %[[BITS:.*]] = arith.muli %[[C_K_CONV]], %[[C_8]] : i16 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_CONV]], %[[C_0]] : i16 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_CONV]], %[[BITS]] : i16 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[BITS]], %[[A_CONV]] : i16 + ! CHECK: %[[SHIFT:.*]] = arith.shrui %[[C__1]], %[[LEN]] : i16 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i16 +end subroutine maskr2_test + +! CHECK-LABEL: maskr4_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskr4_test(a, b) + integer :: a + integer(kind=4) :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskr(a, 4) + ! CHECK: %[[C_K:.*]] = arith.constant 4 : i32 + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i32 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i32 + ! CHECK: %[[C_8:.*]] = arith.constant 8 : i32 + ! CHECK: %[[BITS:.*]] = arith.muli %[[C_K]], %[[C_8]] : i32 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_VAL]], %[[C_0]] : i32 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_VAL]], %[[BITS]] : i32 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[BITS]], %[[A_VAL]] : i32 + ! CHECK: %[[SHIFT:.*]] = arith.shrui %[[C__1]], %[[LEN]] : i32 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i32 +end subroutine maskr4_test + +! CHECK-LABEL: maskr8_test +! CHECK-SAME: %[[A:.*]]: !fir.ref{{.*}}, %[[B:.*]]: !fir.ref{{.*}} +subroutine maskr8_test(a, b) + integer :: a + integer(kind=8) :: b + + ! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref + b = maskr(a, 8) + ! CHECK: %[[C_K:.*]] = arith.constant 8 : i32 + ! CHECK: %[[C_0:.*]] = arith.constant 0 : i64 + ! CHECK: %[[C__1:.*]] = arith.constant -1 : i64 + ! CHECK: %[[A_CONV:.*]] = fir.convert %[[A_VAL]] : (i32) -> i64 + ! CHECK: %[[C_8:.*]] = arith.constant 8 : i64 + ! CHECK: %[[C_K_CONV:.*]] = fir.convert %[[C_K]] : (i32) -> i64 + ! CHECK: %[[BITS:.*]] = arith.muli %[[C_K_CONV]], %[[C_8]] : i64 + ! CHECK: %[[UNDER:.*]] = arith.cmpi sle, %[[A_CONV]], %[[C_0]] : i64 + ! CHECK: %[[OVER:.*]] = arith.cmpi sgt, %[[A_CONV]], %[[BITS]] : i64 + ! CHECK: %[[INVALID:.*]] = arith.ori %[[UNDER]], %[[OVER]] : i1 + ! CHECK: %[[LEN:.*]] = arith.subi %[[BITS]], %[[A_CONV]] : i64 + ! CHECK: %[[SHIFT:.*]] = arith.shrui %[[C__1]], %[[LEN]] : i64 + ! CHECK: %[[RES:.*]] = arith.select %[[INVALID]], %[[C_0]], %[[SHIFT]] : i64 +end subroutine maskr8_test