Skip to content

Commit 98947ff

Browse files
authored
[flang] Add definition of hlfir.cmpchar operation. (#155457)
Fortran character comparison now lowered early into a runtime call. It is going to be lowered into the operation, so that later it could be optimized as inline code or end up into a runtime call.
1 parent c649d31 commit 98947ff

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

flang/include/flang/Optimizer/HLFIR/HLFIROps.td

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,26 @@ def hlfir_ConcatOp : hlfir_Op<"concat",
348348
let hasVerifier = 1;
349349
}
350350

351+
def hlfir_CmpCharOp : hlfir_Op<"cmpchar",
352+
[DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
353+
let summary = "compare two characters";
354+
let description = [{
355+
Compare two character strings of a same character kind.
356+
}];
357+
358+
let arguments = (ins Arith_CmpIPredicateAttr:$predicate,
359+
AnyScalarCharacterEntity:$lchr,
360+
AnyScalarCharacterEntity:$rchr);
361+
362+
let results = (outs I1);
363+
364+
let assemblyFormat = [{
365+
$predicate $lchr $rchr attr-dict `:` functional-type(operands, results)
366+
}];
367+
368+
let hasVerifier = 1;
369+
}
370+
351371
def hlfir_AllOp : hlfir_Op<"all", [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
352372
let summary = "ALL transformational intrinsic";
353373
let description = [{

flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,40 @@ void hlfir::ConcatOp::getEffects(
820820
getIntrinsicEffects(getOperation(), effects);
821821
}
822822

823+
//===----------------------------------------------------------------------===//
824+
// CmpCharOp
825+
//===----------------------------------------------------------------------===//
826+
827+
llvm::LogicalResult hlfir::CmpCharOp::verify() {
828+
mlir::Value lchr = getLchr();
829+
mlir::Value rchr = getRchr();
830+
831+
unsigned kind = getCharacterKind(lchr.getType());
832+
if (kind != getCharacterKind(rchr.getType()))
833+
return emitOpError("character arguments must have the same KIND");
834+
835+
switch (getPredicate()) {
836+
case mlir::arith::CmpIPredicate::slt:
837+
case mlir::arith::CmpIPredicate::sle:
838+
case mlir::arith::CmpIPredicate::eq:
839+
case mlir::arith::CmpIPredicate::ne:
840+
case mlir::arith::CmpIPredicate::sgt:
841+
case mlir::arith::CmpIPredicate::sge:
842+
break;
843+
default:
844+
return emitOpError("expected signed predicate");
845+
}
846+
847+
return mlir::success();
848+
}
849+
850+
void hlfir::CmpCharOp::getEffects(
851+
llvm::SmallVectorImpl<
852+
mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
853+
&effects) {
854+
getIntrinsicEffects(getOperation(), effects);
855+
}
856+
823857
//===----------------------------------------------------------------------===//
824858
// NumericalReductionOp
825859
//===----------------------------------------------------------------------===//

flang/test/HLFIR/invalid.fir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ func.func @bad_concat_4(%arg0: !fir.ref<!fir.char<1,30>>) {
296296
return
297297
}
298298

299+
// -----
300+
func.func @bad_cmpchar_1(%arg0: !fir.ref<!fir.char<1,10>>, %arg1: !fir.ref<!fir.char<2,10>>) {
301+
// expected-error@+1 {{'hlfir.cmpchar' op character arguments must have the same KIND}}
302+
%0 = hlfir.cmpchar ne %arg0 %arg1 : (!fir.ref<!fir.char<1,10>>, !fir.ref<!fir.char<2,10>>) -> i1
303+
}
304+
305+
func.func @bad_cmpchar_2(%arg0: !fir.ref<!fir.char<1,10>>, %arg1: !fir.ref<!fir.char<1,10>>) {
306+
// expected-error@+1 {{'hlfir.cmpchar' op expected signed predicate}}
307+
%0 = hlfir.cmpchar ugt %arg0 %arg1 : (!fir.ref<!fir.char<1,10>>, !fir.ref<!fir.char<1,10>>) -> i1
308+
}
309+
299310
// -----
300311
func.func @bad_any1(%arg0: !hlfir.expr<?x!fir.logical<4>>) {
301312
// expected-error@+1 {{'hlfir.any' op result must have the same element type as MASK argument}}

0 commit comments

Comments
 (0)