Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions flang/include/flang/Optimizer/HLFIR/HLFIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,26 @@ def hlfir_ConcatOp : hlfir_Op<"concat",
let hasVerifier = 1;
}

def hlfir_CmpCharOp : hlfir_Op<"cmpchar",
[DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
let summary = "compare two characters";
let description = [{
Compare two character strings of a same character kind.
}];

let arguments = (ins Arith_CmpIPredicateAttr:$predicate,
AnyScalarCharacterEntity:$lchr,
AnyScalarCharacterEntity:$rchr);

let results = (outs I1);

let assemblyFormat = [{
$predicate $lchr $rchr attr-dict `:` functional-type(operands, results)
}];

let hasVerifier = 1;
}

def hlfir_AllOp : hlfir_Op<"all", [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
let summary = "ALL transformational intrinsic";
let description = [{
Expand Down
34 changes: 34 additions & 0 deletions flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,40 @@ void hlfir::ConcatOp::getEffects(
getIntrinsicEffects(getOperation(), effects);
}

//===----------------------------------------------------------------------===//
// CmpCharOp
//===----------------------------------------------------------------------===//

llvm::LogicalResult hlfir::CmpCharOp::verify() {
mlir::Value lchr = getLchr();
mlir::Value rchr = getRchr();

unsigned kind = getCharacterKind(lchr.getType());
if (kind != getCharacterKind(rchr.getType()))
return emitOpError("character arguments must have the same KIND");

switch (getPredicate()) {
case mlir::arith::CmpIPredicate::slt:
case mlir::arith::CmpIPredicate::sle:
case mlir::arith::CmpIPredicate::eq:
case mlir::arith::CmpIPredicate::ne:
case mlir::arith::CmpIPredicate::sgt:
case mlir::arith::CmpIPredicate::sge:
Comment on lines +836 to +841
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please could you add fallthrkugh annotations here so that compilers don't generate warnings. e.g.

  case mlir::arith::CmpIPredicate::slt:
  [[fallthrough]];
  case mlir::arith::CmpIPredicate::sle:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, Tom. I added it.

Copy link
Contributor

@vzakhari vzakhari Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe [[fallthrough]] is not needed here, because it complicates a very simple set of cases. I believe it is only needed, when there is code between two cases. Look at llvm/lib sources to find that it is not used for simple lists of cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay my mistake. Thanks for the clarification Slava. @valerydmit please feel free to remove these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :-)

break;
default:
return emitOpError("expected signed predicate");
}

return mlir::success();
}

void hlfir::CmpCharOp::getEffects(
llvm::SmallVectorImpl<
mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
&effects) {
getIntrinsicEffects(getOperation(), effects);
}

//===----------------------------------------------------------------------===//
// NumericalReductionOp
//===----------------------------------------------------------------------===//
Expand Down
11 changes: 11 additions & 0 deletions flang/test/HLFIR/invalid.fir
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ func.func @bad_concat_4(%arg0: !fir.ref<!fir.char<1,30>>) {
return
}

// -----
func.func @bad_cmpchar_1(%arg0: !fir.ref<!fir.char<1,10>>, %arg1: !fir.ref<!fir.char<2,10>>) {
// expected-error@+1 {{'hlfir.cmpchar' op character arguments must have the same KIND}}
%0 = hlfir.cmpchar ne %arg0 %arg1 : (!fir.ref<!fir.char<1,10>>, !fir.ref<!fir.char<2,10>>) -> i1
}

func.func @bad_cmpchar_2(%arg0: !fir.ref<!fir.char<1,10>>, %arg1: !fir.ref<!fir.char<1,10>>) {
// expected-error@+1 {{'hlfir.cmpchar' op expected signed predicate}}
%0 = hlfir.cmpchar ugt %arg0 %arg1 : (!fir.ref<!fir.char<1,10>>, !fir.ref<!fir.char<1,10>>) -> i1
}

// -----
func.func @bad_any1(%arg0: !hlfir.expr<?x!fir.logical<4>>) {
// expected-error@+1 {{'hlfir.any' op result must have the same element type as MASK argument}}
Expand Down