[flang-commits] [flang] 63bbac1 - [flang][lowering] Add support for lowering of the `ibset` intrinsic

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Wed Mar 16 03:33:40 PDT 2022


Author: Andrzej Warzynski
Date: 2022-03-16T10:33:19Z
New Revision: 63bbac1065594a6151ed2772e707b278c36e47ba

URL: https://github.com/llvm/llvm-project/commit/63bbac1065594a6151ed2772e707b278c36e47ba
DIFF: https://github.com/llvm/llvm-project/commit/63bbac1065594a6151ed2772e707b278c36e47ba.diff

LOG: [flang][lowering] Add support for lowering of the `ibset` intrinsic

This patch adds support for lowering of the `ibset` intrinsic from Fortran
to the FIR dialect of MLIR.

This is part of the upstreaming effort from the `fir-dev` branch in [1].

[1] https://github.com/flang-compiler/f18-llvm-project

Co-authored-by: Jean Perier <jperier at nvidia.com>
Co-authored-by: Valentin Clement <clementval at gmail.com>
Co-authored-by: V Donaldson <vdonaldson at nvidia.com>

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

Added: 
    flang/test/Lower/Intrinsics/ibset.f90

Modified: 
    flang/lib/Lower/IntrinsicCall.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp
index 3a5a6148bc288..d884f9b16e344 100644
--- a/flang/lib/Lower/IntrinsicCall.cpp
+++ b/flang/lib/Lower/IntrinsicCall.cpp
@@ -449,6 +449,7 @@ struct IntrinsicLibrary {
   /// in the llvm::ArrayRef.
   mlir::Value genIand(mlir::Type, llvm::ArrayRef<mlir::Value>);
   mlir::Value genIbits(mlir::Type, llvm::ArrayRef<mlir::Value>);
+  mlir::Value genIbset(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue genLbound(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genNull(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genLen(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
@@ -595,6 +596,7 @@ static constexpr IntrinsicHandler handlers[]{
      /*isElemental=*/false},
     {"iand", &I::genIand},
     {"ibits", &I::genIbits},
+    {"ibset", &I::genIbset},
     {"len",
      &I::genLen,
      {{{"string", asInquired}, {"kind", asValue}}},
@@ -1682,6 +1684,20 @@ mlir::Value IntrinsicLibrary::genIbits(mlir::Type resultType,
   return builder.create<mlir::arith::SelectOp>(loc, lenIsZero, zero, res2);
 }
 
+// IBSET
+mlir::Value IntrinsicLibrary::genIbset(mlir::Type resultType,
+                                       llvm::ArrayRef<mlir::Value> args) {
+  // A conformant IBSET(I,POS) call satisfies:
+  //     POS >= 0
+  //     POS < BIT_SIZE(I)
+  // Return:  I | (1 << POS)
+  assert(args.size() == 2);
+  mlir::Value pos = builder.createConvert(loc, resultType, args[1]);
+  mlir::Value one = builder.createIntegerConstant(loc, resultType, 1);
+  auto mask = builder.create<mlir::arith::ShLIOp>(loc, one, pos);
+  return builder.create<mlir::arith::OrIOp>(loc, args[0], mask);
+}
+
 // LEN
 // Note that this is only used for an unrestricted intrinsic LEN call.
 // Other uses of LEN are rewritten as descriptor inquiries by the front-end.

diff  --git a/flang/test/Lower/Intrinsics/ibset.f90 b/flang/test/Lower/Intrinsics/ibset.f90
new file mode 100644
index 0000000000000..7dcaf9f475ea0
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/ibset.f90
@@ -0,0 +1,17 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s
+
+! CHECK-LABEL: ibset_test
+function ibset_test(i, j)
+  ! CHECK-DAG: %[[result:.*]] = fir.alloca i32 {bindc_name = "ibset_test"
+  ! CHECK-DAG: %[[i:.*]] = fir.load %arg0 : !fir.ref<i32>
+  ! CHECK-DAG: %[[j:.*]] = fir.load %arg1 : !fir.ref<i32>
+  ! CHECK-DAG: %[[VAL_5:.*]] = arith.constant 1 : i32
+  ! CHECK: %[[VAL_6:.*]] = arith.shli %[[VAL_5]], %[[j]] : i32
+  ! CHECK: %[[VAL_7:.*]] = arith.ori %[[i]], %[[VAL_6]] : i32
+  ! CHECK: fir.store %[[VAL_7]] to %[[result]] : !fir.ref<i32>
+  ! CHECK: %[[VAL_8:.*]] = fir.load %[[result]] : !fir.ref<i32>
+  ! CHECK: return %[[VAL_8]] : i32
+  ibset_test = ibset(i, j)
+end
+


        


More information about the flang-commits mailing list