Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit 7812f51

Browse files
[fir] Add substring to fir.slice operation
This patch adds the substriing information to the fir.slice operation. This will be used by character operations in later upstreaming patches. This patch is part of the upstreaming effort from fir-dev branch. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D112441 Co-authored-by: Eric Schweitz <[email protected]>
1 parent a458ef4 commit 7812f51

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

flang/include/flang/Optimizer/Dialect/FIROps.td

+21-4
Original file line numberDiff line numberDiff line change
@@ -1907,21 +1907,38 @@ def fir_SliceOp : fir_Op<"slice", [NoSideEffect, AttrSizedOperandSegments]> {
19071907

19081908
```mlir
19091909
%fld = fir.field_index component, !fir.type<t{...component:ct...}>
1910-
%d = fir.slice %lo, %hi, %step path %fld : (index, index, index, !fir.field) -> !fir.slice<1>
1910+
%d = fir.slice %lo, %hi, %step path %fld :
1911+
(index, index, index, !fir.field) -> !fir.slice<1>
1912+
```
1913+
1914+
Projections of `!fir.char` type can be further narrowed to invariant
1915+
substrings.
1916+
1917+
```mlir
1918+
%d = fir.slice %lo, %hi, %step substr %offset, %width :
1919+
(index, index, index, index, index) -> !fir.slice<1>
19111920
```
19121921
}];
19131922

19141923
let arguments = (ins
1915-
Variadic<AnyCoordinateType>:$triples,
1916-
Variadic<AnyComponentType>:$fields
1924+
Variadic<AnyIntegerType>:$triples,
1925+
Variadic<AnyComponentType>:$fields,
1926+
Variadic<AnyIntegerType>:$substr
19171927
);
19181928

19191929
let results = (outs fir_SliceType);
19201930

19211931
let assemblyFormat = [{
1922-
$triples (`path` $fields^)? attr-dict `:` functional-type(operands, results)
1932+
$triples (`path` $fields^)? (`substr` $substr^)? attr-dict `:`
1933+
functional-type(operands, results)
19231934
}];
19241935

1936+
let builders = [
1937+
OpBuilder<(ins "mlir::ValueRange":$triples,
1938+
CArg<"mlir::ValueRange", "llvm::None">:$fields,
1939+
CArg<"mlir::ValueRange", "llvm::None">:$substr)>
1940+
];
1941+
19251942
let verifier = "return ::verify(*this);";
19261943

19271944
let extraClassDeclaration = [{

flang/lib/Optimizer/Dialect/FIROps.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -2811,13 +2811,21 @@ static mlir::LogicalResult verify(fir::ShiftOp &op) {
28112811
// SliceOp
28122812
//===----------------------------------------------------------------------===//
28132813

2814+
void fir::SliceOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
2815+
mlir::ValueRange trips, mlir::ValueRange path,
2816+
mlir::ValueRange substr) {
2817+
const auto rank = trips.size() / 3;
2818+
auto sliceTy = fir::SliceType::get(builder.getContext(), rank);
2819+
build(builder, result, sliceTy, trips, path, substr);
2820+
}
2821+
28142822
/// Return the output rank of a slice op. The output rank must be between 1 and
28152823
/// the rank of the array being sliced (inclusive).
28162824
unsigned fir::SliceOp::getOutputRank(mlir::ValueRange triples) {
28172825
unsigned rank = 0;
28182826
if (!triples.empty()) {
28192827
for (unsigned i = 1, end = triples.size(); i < end; i += 3) {
2820-
auto op = triples[i].getDefiningOp();
2828+
auto *op = triples[i].getDefiningOp();
28212829
if (!mlir::isa_and_nonnull<fir::UndefOp>(op))
28222830
++rank;
28232831
}

flang/test/Fir/fir-ops.fir

+10
Original file line numberDiff line numberDiff line change
@@ -699,3 +699,13 @@ func @char_convert() {
699699
fir.char_convert %2 for %1 to %3 : !fir.ref<!fir.char<1>>, i32, !fir.ref<!fir.array<?x!fir.char<2,?>>>
700700
return
701701
}
702+
703+
func @slice_substr() {
704+
%lb = arith.constant 0 : index
705+
%ub = arith.constant 42 : index
706+
%c1 = arith.constant 1 : index
707+
%offset = arith.constant 10 : index
708+
%0 = fir.slice %lb, %ub, %c1 substr %offset, %c1 : (index, index, index, index, index) -> !fir.slice<1>
709+
// CHECK: fir.slice %{{.*}}, %{{.*}}, %{{.*}} substr %{{.*}}, %{{.*}} : (index, index, index, index, index) -> !fir.slice<1>
710+
return
711+
}

flang/test/Fir/invalid.fir

+10
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,13 @@ func @bad_array_modify(%arr1 : !fir.ref<!fir.array<?x?xf32>>, %m : index, %n : i
606606
fir.array_merge_store %av1, %av2 to %arr1 : !fir.array<?x?xf32>, !fir.array<?x?xf32>, !fir.ref<!fir.array<?x?xf32>>
607607
return
608608
}
609+
610+
// -----
611+
612+
func @slice_must_be_integral() {
613+
%0 = arith.constant 42 : i32
614+
%1 = fir.field_index field, !fir.type<t(param:i32){field:i32}> (%0 : i32)
615+
// expected-error@+1 {{'fir.slice' op operand #0 must be any integer, but got '!fir.field'}}
616+
%2 = fir.slice %1, %1, %1 : (!fir.field, !fir.field, !fir.field) -> !fir.slice<1>
617+
return
618+
}

0 commit comments

Comments
 (0)