Skip to content

Commit 5035455

Browse files
clementvalvdonaldsonjeanPerier
committed
[flang] Lower mvbits intrinsic
This patch adds the lowering for the `mvbits` intrinsic. This patch is part of the upstreaming effort from fir-dev branch. Reviewed By: PeteSteinfeld Differential Revision: https://reviews.llvm.org/D122412 Co-authored-by: V Donaldson <vdonaldson@nvidia.com> Co-authored-by: Jean Perier <jperier@nvidia.com>
1 parent 56a5491 commit 5035455

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

flang/lib/Lower/IntrinsicCall.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ struct IntrinsicLibrary {
499499
fir::ExtendedValue genMinval(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
500500
mlir::Value genMod(mlir::Type, llvm::ArrayRef<mlir::Value>);
501501
mlir::Value genModulo(mlir::Type, llvm::ArrayRef<mlir::Value>);
502+
void genMvbits(llvm::ArrayRef<fir::ExtendedValue>);
502503
mlir::Value genNearest(mlir::Type, llvm::ArrayRef<mlir::Value>);
503504
mlir::Value genNint(mlir::Type, llvm::ArrayRef<mlir::Value>);
504505
mlir::Value genNot(mlir::Type, llvm::ArrayRef<mlir::Value>);
@@ -805,6 +806,13 @@ static constexpr IntrinsicHandler handlers[]{
805806
/*isElemental=*/false},
806807
{"mod", &I::genMod},
807808
{"modulo", &I::genModulo},
809+
{"mvbits",
810+
&I::genMvbits,
811+
{{{"from", asValue},
812+
{"frompos", asValue},
813+
{"len", asValue},
814+
{"to", asAddr},
815+
{"topos", asValue}}}},
808816
{"nearest", &I::genNearest},
809817
{"nint", &I::genNint},
810818
{"not", &I::genNot},
@@ -2854,6 +2862,53 @@ mlir::Value IntrinsicLibrary::genModulo(mlir::Type resultType,
28542862
remainder);
28552863
}
28562864

2865+
// MVBITS
2866+
void IntrinsicLibrary::genMvbits(llvm::ArrayRef<fir::ExtendedValue> args) {
2867+
// A conformant MVBITS(FROM,FROMPOS,LEN,TO,TOPOS) call satisfies:
2868+
// FROMPOS >= 0
2869+
// LEN >= 0
2870+
// TOPOS >= 0
2871+
// FROMPOS + LEN <= BIT_SIZE(FROM)
2872+
// TOPOS + LEN <= BIT_SIZE(TO)
2873+
// MASK = -1 >> (BIT_SIZE(FROM) - LEN)
2874+
// TO = LEN == 0 ? TO : ((!(MASK << TOPOS)) & TO) |
2875+
// (((FROM >> FROMPOS) & MASK) << TOPOS)
2876+
assert(args.size() == 5);
2877+
auto unbox = [&](fir::ExtendedValue exv) {
2878+
const mlir::Value *arg = exv.getUnboxed();
2879+
assert(arg && "nonscalar mvbits argument");
2880+
return *arg;
2881+
};
2882+
mlir::Value from = unbox(args[0]);
2883+
mlir::Type resultType = from.getType();
2884+
mlir::Value frompos = builder.createConvert(loc, resultType, unbox(args[1]));
2885+
mlir::Value len = builder.createConvert(loc, resultType, unbox(args[2]));
2886+
mlir::Value toAddr = unbox(args[3]);
2887+
assert(fir::dyn_cast_ptrEleTy(toAddr.getType()) == resultType &&
2888+
"mismatched mvbits types");
2889+
auto to = builder.create<fir::LoadOp>(loc, resultType, toAddr);
2890+
mlir::Value topos = builder.createConvert(loc, resultType, unbox(args[4]));
2891+
mlir::Value zero = builder.createIntegerConstant(loc, resultType, 0);
2892+
mlir::Value ones = builder.createIntegerConstant(loc, resultType, -1);
2893+
mlir::Value bitSize = builder.createIntegerConstant(
2894+
loc, resultType, resultType.cast<mlir::IntegerType>().getWidth());
2895+
auto shiftCount = builder.create<mlir::arith::SubIOp>(loc, bitSize, len);
2896+
auto mask = builder.create<mlir::arith::ShRUIOp>(loc, ones, shiftCount);
2897+
auto unchangedTmp1 = builder.create<mlir::arith::ShLIOp>(loc, mask, topos);
2898+
auto unchangedTmp2 =
2899+
builder.create<mlir::arith::XOrIOp>(loc, unchangedTmp1, ones);
2900+
auto unchanged = builder.create<mlir::arith::AndIOp>(loc, unchangedTmp2, to);
2901+
auto frombitsTmp1 = builder.create<mlir::arith::ShRUIOp>(loc, from, frompos);
2902+
auto frombitsTmp2 =
2903+
builder.create<mlir::arith::AndIOp>(loc, frombitsTmp1, mask);
2904+
auto frombits = builder.create<mlir::arith::ShLIOp>(loc, frombitsTmp2, topos);
2905+
auto resTmp = builder.create<mlir::arith::OrIOp>(loc, unchanged, frombits);
2906+
auto lenIsZero = builder.create<mlir::arith::CmpIOp>(
2907+
loc, mlir::arith::CmpIPredicate::eq, len, zero);
2908+
auto res = builder.create<mlir::arith::SelectOp>(loc, lenIsZero, to, resTmp);
2909+
builder.create<fir::StoreOp>(loc, res, toAddr);
2910+
}
2911+
28572912
// NEAREST
28582913
mlir::Value IntrinsicLibrary::genNearest(mlir::Type resultType,
28592914
llvm::ArrayRef<mlir::Value> args) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QPmvbits_test(
4+
function mvbits_test(from, frompos, len, to, topos)
5+
! CHECK: %[[result:.*]] = fir.alloca i32 {bindc_name = "mvbits_test"
6+
! CHECK-DAG: %[[from:.*]] = fir.load %arg0 : !fir.ref<i32>
7+
! CHECK-DAG: %[[frompos:.*]] = fir.load %arg1 : !fir.ref<i32>
8+
! CHECK-DAG: %[[len:.*]] = fir.load %arg2 : !fir.ref<i32>
9+
! CHECK-DAG: %[[to:.*]] = fir.load %arg3 : !fir.ref<i32>
10+
! CHECK-DAG: %[[topos:.*]] = fir.load %arg4 : !fir.ref<i32>
11+
integer :: from, frompos, len, to, topos
12+
integer :: mvbits_test
13+
! CHECK: %[[VAL_11:.*]] = arith.constant 0 : i32
14+
! CHECK: %[[VAL_12:.*]] = arith.constant -1 : i32
15+
! CHECK: %[[VAL_13:.*]] = arith.constant 32 : i32
16+
! CHECK: %[[VAL_14:.*]] = arith.subi %[[VAL_13]], %[[len]] : i32
17+
! CHECK: %[[VAL_15:.*]] = arith.shrui %[[VAL_12]], %[[VAL_14]] : i32
18+
! CHECK: %[[VAL_16:.*]] = arith.shli %[[VAL_15]], %[[topos]] : i32
19+
! CHECK: %[[VAL_17:.*]] = arith.xori %[[VAL_16]], %[[VAL_12]] : i32
20+
! CHECK: %[[VAL_18:.*]] = arith.andi %[[VAL_17]], %[[to]] : i32
21+
! CHECK: %[[VAL_19:.*]] = arith.shrui %[[from]], %[[frompos]] : i32
22+
! CHECK: %[[VAL_20:.*]] = arith.andi %[[VAL_19]], %[[VAL_15]] : i32
23+
! CHECK: %[[VAL_21:.*]] = arith.shli %[[VAL_20]], %[[topos]] : i32
24+
! CHECK: %[[VAL_22:.*]] = arith.ori %[[VAL_18]], %[[VAL_21]] : i32
25+
! CHECK: %[[VAL_23:.*]] = arith.cmpi eq, %[[len]], %[[VAL_11]] : i32
26+
! CHECK: %[[VAL_24:.*]] = arith.select %[[VAL_23]], %[[to]], %[[VAL_22]] : i32
27+
! CHECK: fir.store %[[VAL_24]] to %arg3 : !fir.ref<i32>
28+
! CHECK: %[[VAL_25:.*]] = fir.load %arg3 : !fir.ref<i32>
29+
! CHECK: fir.store %[[VAL_25]] to %[[result]] : !fir.ref<i32>
30+
call mvbits(from, frompos, len, to, topos)
31+
! CHECK: %[[VAL_26:.*]] = fir.load %[[result]] : !fir.ref<i32>
32+
! CHECK: return %[[VAL_26]] : i32
33+
mvbits_test = to
34+
end
35+
36+
! CHECK-LABEL: func @_QPmvbits_array_test(
37+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xi32>>{{.*}}, %[[VAL_1:.*]]: !fir.ref<i32>{{.*}}, %[[VAL_2:.*]]: !fir.ref<i32>{{.*}}, %[[VAL_3:.*]]: !fir.box<!fir.array<?xi32>>{{.*}}, %[[VAL_4:.*]]: !fir.ref<i32>{{.*}}) {
38+
! CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
39+
! CHECK: %[[VAL_6:.*]]:3 = fir.box_dims %[[VAL_0]], %[[VAL_5]] : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
40+
! CHECK: %[[VAL_7:.*]] = fir.array_load %[[VAL_0]] : (!fir.box<!fir.array<?xi32>>) -> !fir.array<?xi32>
41+
! CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_1]] : !fir.ref<i32>
42+
! CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_2]] : !fir.ref<i32>
43+
! CHECK: %[[VAL_10:.*]] = fir.load %[[VAL_4]] : !fir.ref<i32>
44+
! CHECK: %[[VAL_11:.*]] = arith.constant 1 : index
45+
! CHECK: %[[VAL_12:.*]] = arith.constant 0 : index
46+
! CHECK: %[[VAL_13:.*]] = arith.subi %[[VAL_6]]#1, %[[VAL_11]] : index
47+
! CHECK: fir.do_loop %[[VAL_14:.*]] = %[[VAL_12]] to %[[VAL_13]] step %[[VAL_11]] {
48+
! CHECK: %[[VAL_15:.*]] = fir.array_fetch %[[VAL_7]], %[[VAL_14]] : (!fir.array<?xi32>, index) -> i32
49+
! CHECK: %[[VAL_16:.*]] = arith.constant 1 : index
50+
! CHECK: %[[VAL_17:.*]] = arith.addi %[[VAL_14]], %[[VAL_16]] : index
51+
! CHECK: %[[VAL_18:.*]] = fir.array_coor %[[VAL_3]] %[[VAL_17]] : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
52+
! CHECK: %[[VAL_19:.*]] = fir.load %[[VAL_18]] : !fir.ref<i32>
53+
! CHECK: %[[VAL_20:.*]] = arith.constant 0 : i32
54+
! CHECK: %[[VAL_21:.*]] = arith.constant -1 : i32
55+
! CHECK: %[[VAL_22:.*]] = arith.constant 32 : i32
56+
! CHECK: %[[VAL_23:.*]] = arith.subi %[[VAL_22]], %[[VAL_9]] : i32
57+
! CHECK: %[[VAL_24:.*]] = arith.shrui %[[VAL_21]], %[[VAL_23]] : i32
58+
! CHECK: %[[VAL_25:.*]] = arith.shli %[[VAL_24]], %[[VAL_10]] : i32
59+
! CHECK: %[[VAL_26:.*]] = arith.xori %[[VAL_25]], %[[VAL_21]] : i32
60+
! CHECK: %[[VAL_27:.*]] = arith.andi %[[VAL_26]], %[[VAL_19]] : i32
61+
! CHECK: %[[VAL_28:.*]] = arith.shrui %[[VAL_15]], %[[VAL_8]] : i32
62+
! CHECK: %[[VAL_29:.*]] = arith.andi %[[VAL_28]], %[[VAL_24]] : i32
63+
! CHECK: %[[VAL_30:.*]] = arith.shli %[[VAL_29]], %[[VAL_10]] : i32
64+
! CHECK: %[[VAL_31:.*]] = arith.ori %[[VAL_27]], %[[VAL_30]] : i32
65+
! CHECK: %[[VAL_32:.*]] = arith.cmpi eq, %[[VAL_9]], %[[VAL_20]] : i32
66+
! CHECK: %[[VAL_33:.*]] = arith.select %[[VAL_32]], %[[VAL_19]], %[[VAL_31]] : i32
67+
! CHECK: fir.store %[[VAL_33]] to %[[VAL_18]] : !fir.ref<i32>
68+
! CHECK: }
69+
! CHECK: return
70+
! CHECK: }
71+
72+
subroutine mvbits_array_test(from, frompos, len, to, topos)
73+
integer :: from(:), frompos, len, to(:), topos
74+
75+
call mvbits(from, frompos, len, to, topos)
76+
end subroutine

0 commit comments

Comments
 (0)