diff --git a/flang/include/flang/Optimizer/Builder/Character.h b/flang/include/flang/Optimizer/Builder/Character.h --- a/flang/include/flang/Optimizer/Builder/Character.h +++ b/flang/include/flang/Optimizer/Builder/Character.h @@ -15,6 +15,7 @@ #include "flang/Optimizer/Builder/BoxValue.h" #include "flang/Optimizer/Builder/LowLevelIntrinsics.h" +#include "flang/Optimizer/Builder/Runtime/Character.h" namespace fir { class FirOpBuilder; @@ -66,6 +67,11 @@ fir::CharBoxValue createConcatenate(const fir::CharBoxValue &lhs, const fir::CharBoxValue &rhs); + /// Create {max,min}(lhs,rhs) in temp obtained with fir.alloca + fir::CharBoxValue + createCharExtremum(bool predIsMin, + const llvm::SmallVector &opCBVs); + /// LEN_TRIM intrinsic. mlir::Value createLenTrim(const fir::CharBoxValue &str); diff --git a/flang/include/flang/Optimizer/HLFIR/CMakeLists.txt b/flang/include/flang/Optimizer/HLFIR/CMakeLists.txt --- a/flang/include/flang/Optimizer/HLFIR/CMakeLists.txt +++ b/flang/include/flang/Optimizer/HLFIR/CMakeLists.txt @@ -5,6 +5,8 @@ mlir_tablegen(HLFIRDialect.cpp.inc -gen-dialect-defs -dialect=hlfir) mlir_tablegen(HLFIRAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=hlfir) mlir_tablegen(HLFIRAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=hlfir) +mlir_tablegen(HLFIREnums.h.inc -gen-enum-decls) +mlir_tablegen(HLFIREnums.cpp.inc -gen-enum-defs) set(LLVM_TARGET_DEFINITIONS HLFIROps.td) mlir_tablegen(HLFIROpInterfaces.h.inc -gen-op-interface-decls) diff --git a/flang/include/flang/Optimizer/HLFIR/HLFIRDialect.h b/flang/include/flang/Optimizer/HLFIR/HLFIRDialect.h --- a/flang/include/flang/Optimizer/HLFIR/HLFIRDialect.h +++ b/flang/include/flang/Optimizer/HLFIR/HLFIRDialect.h @@ -26,6 +26,8 @@ #include "flang/Optimizer/HLFIR/HLFIRDialect.h.inc" +#include "flang/Optimizer/HLFIR/HLFIREnums.h.inc" + #define GET_TYPEDEF_CLASSES #include "flang/Optimizer/HLFIR/HLFIRTypes.h.inc" diff --git a/flang/include/flang/Optimizer/HLFIR/HLFIROpBase.td b/flang/include/flang/Optimizer/HLFIR/HLFIROpBase.td --- a/flang/include/flang/Optimizer/HLFIR/HLFIROpBase.td +++ b/flang/include/flang/Optimizer/HLFIR/HLFIROpBase.td @@ -15,6 +15,7 @@ #define FORTRAN_DIALECT_HLFIR_OP_BASE include "mlir/IR/AttrTypeBase.td" +include "mlir/IR/EnumAttr.td" include "mlir/IR/OpBase.td" include "flang/Optimizer/Dialect/FIRTypes.td" @@ -138,4 +139,12 @@ def AnyFortranLogicalArrayObject : Type; +def hlfir_CharExtremumPredicateAttr : I32EnumAttr< + "CharExtremumPredicate", "", + [ + I32EnumAttrCase<"min", 0>, + I32EnumAttrCase<"max", 1> + ]> { + let cppNamespace = "hlfir"; +} #endif // FORTRAN_DIALECT_HLFIR_OP_BASE diff --git a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td --- a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td +++ b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td @@ -1355,4 +1355,29 @@ let hasCanonicalizeMethod = 1; } +def hlfir_CharExtremumOp : hlfir_Op<"char_extremum", []> { + let summary = "Find max/min from given character strings"; + let description = [{ + Find the lexicographical minimum or maximum of two or more character + strings of the same character kind and return the string with the lexicographical + minimum or maximum number of characters. Example: + + %0 = hlfir.char_extremum min, %arg0, %arg1 : (!fir.ref>, !fir.ref>) -> !hlfir.expr> + }]; + + let arguments = (ins hlfir_CharExtremumPredicateAttr:$predicate, + Variadic:$strings + ); + + let results = (outs AnyScalarCharacterExpr); + + let assemblyFormat = [{ + $predicate `,` $strings attr-dict `:` functional-type(operands, results) + }]; + + let builders = [OpBuilder<(ins "hlfir::CharExtremumPredicate":$predicate, "mlir::ValueRange":$strings)>]; + + let hasVerifier = 1; +} + #endif // FORTRAN_DIALECT_HLFIR_OPS diff --git a/flang/lib/Lower/ConvertArrayConstructor.cpp b/flang/lib/Lower/ConvertArrayConstructor.cpp --- a/flang/lib/Lower/ConvertArrayConstructor.cpp +++ b/flang/lib/Lower/ConvertArrayConstructor.cpp @@ -194,8 +194,8 @@ fir::SequenceType declaredType, mlir::Value extent, llvm::ArrayRef lengths) : StrategyBase{stmtCtx, symMap}, shape{builder.genShape(loc, {extent})}, - lengthParams{lengths.begin(), lengths.end()}, - exprType{getExprType(declaredType)} {} + lengthParams{lengths.begin(), lengths.end()}, exprType{getExprType( + declaredType)} {} static hlfir::ExprType getExprType(fir::SequenceType declaredType) { // Note: 7.8 point 4: the dynamic type of an array constructor is its static @@ -284,8 +284,8 @@ std::optional extent, llvm::ArrayRef lengths, bool missingLengthParameters) - : StrategyBase{stmtCtx, symMap}, - arrayConstructorElementType{declaredType.getEleTy()} { + : StrategyBase{stmtCtx, symMap}, arrayConstructorElementType{ + declaredType.getEleTy()} { mlir::Type heapType = fir::HeapType::get(declaredType); mlir::Type boxType = fir::BoxType::get(heapType); allocatableTemp = builder.createTemporary(loc, boxType, tempName); diff --git a/flang/lib/Lower/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp --- a/flang/lib/Lower/ConvertCall.cpp +++ b/flang/lib/Lower/ConvertCall.cpp @@ -1364,13 +1364,18 @@ hlfir::Entity actual = arg->getOriginalActual(); mlir::Value valArg; - fir::ArgLoweringRule argRules = - fir::lowerIntrinsicArgumentAs(*argLowering, i); - if (!argRules.handleDynamicOptional && - argRules.lowerAs != fir::LowerIntrinsicArgAs::Inquired) - valArg = hlfir::derefPointersAndAllocatables(loc, builder, actual); - else - valArg = actual.getBase(); + // if intrinsic handler has no lowering rules + if (!argLowering) { + valArg = hlfir::loadTrivialScalar(loc, builder, actual); + } else { + fir::ArgLoweringRule argRules = + fir::lowerIntrinsicArgumentAs(*argLowering, i); + if (!argRules.handleDynamicOptional && + argRules.lowerAs != fir::LowerIntrinsicArgAs::Inquired) + valArg = hlfir::derefPointersAndAllocatables(loc, builder, actual); + else + valArg = actual.getBase(); + } operands.emplace_back(valArg); } @@ -1478,6 +1483,21 @@ return {hlfir::EntityWithAttributes{anyOp.getResult()}}; } + if ((intrinsicName == "min" || intrinsicName == "max") && + hlfir::getFortranElementType(callContext.resultType.value()) + .isa()) { + llvm::SmallVector operands = getOperandVector(loweredActuals); + assert(operands.size() >= 2); + + hlfir::CharExtremumPredicate pred = (intrinsicName == "min") + ? hlfir::CharExtremumPredicate::min + : hlfir::CharExtremumPredicate::max; + hlfir::CharExtremumOp charExtremumOp = + builder.create(loc, pred, + mlir::ValueRange{operands}); + return {hlfir::EntityWithAttributes{charExtremumOp.getResult()}}; + } + // TODO add hlfir operations for other transformational intrinsics here // fallback to calling the intrinsic via fir.call diff --git a/flang/lib/Optimizer/Builder/Character.cpp b/flang/lib/Optimizer/Builder/Character.cpp --- a/flang/lib/Optimizer/Builder/Character.cpp +++ b/flang/lib/Optimizer/Builder/Character.cpp @@ -784,3 +784,75 @@ mlir::Type lenType = mlir::IntegerType::get(context, 64); return mlir::TupleType::get(context, {funcPointerType, lenType}); } + +fir::CharBoxValue fir::factory::CharacterExprHelper::createCharExtremum( + bool predIsMin, const llvm::SmallVector &opCBVs) { + // inputs: we are given a vector of all of the charboxes of the arguments + // passed to hlfir.char_extremum, as well as the predicate for whether we + // want llt or lgt + // + // note: we know that, regardless of whether we're looking at smallest or + // largest char, the size of the output buffer will be the same size as the + // largest character out of all of the operands. so, we find the biggest + // length first. It's okay if these char lengths are undefined. + + fir::CharBoxValue firstCBV = opCBVs[0]; + mlir::Value firstBuf = getCharBoxBuffer(firstCBV); + auto firstLen = builder.createConvert(loc, builder.getCharacterLengthType(), + firstCBV.getLen()); + + mlir::Value resultBuf = firstBuf; + mlir::Value resultLen = firstLen; + mlir::Value biggestLen = firstLen; + + // values for casting buf type and len type + auto typeLen = fir::CharacterType::unknownLen(); + auto kind = recoverCharacterType(firstBuf.getType()).getFKind(); + auto charTy = fir::CharacterType::get(builder.getContext(), kind, typeLen); + auto type = fir::ReferenceType::get(charTy); + + size_t numOperands = opCBVs.size(); + for (size_t cbv_idx = 1; cbv_idx < numOperands; ++cbv_idx) { + auto currChar = opCBVs[cbv_idx]; + auto currBuf = getCharBoxBuffer(currChar); + auto currLen = builder.createConvert(loc, builder.getCharacterLengthType(), + currChar.getLen()); + // biggest len result + mlir::Value lhsBigger = builder.create( + loc, mlir::arith::CmpIPredicate::uge, biggestLen, currLen); + biggestLen = builder.create(loc, lhsBigger, + biggestLen, currLen); + + auto cmp = predIsMin ? mlir::arith::CmpIPredicate::slt + : mlir::arith::CmpIPredicate::sgt; + + // lexical compare result + mlir::Value resultCmp = fir::runtime::genCharCompare( + builder, loc, cmp, currBuf, currLen, resultBuf, resultLen); + + // it's casting (to unknown size) time! + resultBuf = builder.createConvert(loc, type, resultBuf); + currBuf = builder.createConvert(loc, type, currBuf); + + resultBuf = builder.create(loc, resultCmp, currBuf, + resultBuf); + resultLen = builder.create(loc, resultCmp, currLen, + resultLen); + } + + // if the length is known at compile time, let's recover it + if (auto cstLen = getIntIfConstant(resultLen)) { + typeLen = *cstLen; + charTy = fir::CharacterType::get(builder.getContext(), kind, typeLen); + type = fir::ReferenceType::get(charTy); + resultBuf = builder.createConvert(loc, type, resultBuf); + } + + // now that we know the lexicographically biggest/smallest char and which char + // had the biggest len, we can populate a temp CBV and return it + fir::CharBoxValue temp = createCharacterTemp(resultBuf.getType(), biggestLen); + auto toBuf = temp; + fir::CharBoxValue fromBuf{resultBuf, resultLen}; + createAssign(toBuf, fromBuf); + return temp; +} diff --git a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp --- a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp +++ b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp @@ -1324,6 +1324,44 @@ return mlir::failure(); } +//===----------------------------------------------------------------------===// +// CharExtremumOp +//===----------------------------------------------------------------------===// + +mlir::LogicalResult hlfir::CharExtremumOp::verify() { + if (getStrings().size() < 2) + return emitOpError("must be provided at least two string operands"); + unsigned kind = getCharacterKind(getResult().getType()); + for (auto string : getStrings()) + if (kind != getCharacterKind(string.getType())) + return emitOpError("strings must have the same KIND as the result type"); + return mlir::success(); +} + +void hlfir::CharExtremumOp::build(mlir::OpBuilder &builder, + mlir::OperationState &result, + hlfir::CharExtremumPredicate predicate, + mlir::ValueRange strings) { + + fir::CharacterType::LenType resultTypeLen = 0; + assert(!strings.empty() && "must contain operands"); + unsigned kind = getCharacterKind(strings[0].getType()); + for (auto string : strings) + if (auto cstLen = getCharacterLengthIfStatic(string.getType())) { + resultTypeLen = std::max(resultTypeLen, *cstLen); + } else { + resultTypeLen = fir::CharacterType::unknownLen(); + break; + } + auto resultType = hlfir::ExprType::get( + builder.getContext(), hlfir::ExprType::Shape{}, + fir::CharacterType::get(builder.getContext(), kind, resultTypeLen), + false); + + build(builder, result, resultType, predicate, strings); +} + #include "flang/Optimizer/HLFIR/HLFIROpInterfaces.cpp.inc" #define GET_OP_CLASSES -#include "flang/Optimizer/HLFIR/HLFIROps.cpp.inc" +#include "flang/Optimizer/HLFIR/HLFIREnums.cpp.inc" +#include "flang/Optimizer/HLFIR/HLFIROps.cpp.inc" \ No newline at end of file diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp --- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp +++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp @@ -591,6 +591,54 @@ return mlir::success(); } }; +struct CharExtremumOpConversion + : public mlir::OpConversionPattern { + using mlir::OpConversionPattern::OpConversionPattern; + explicit CharExtremumOpConversion(mlir::MLIRContext *ctx) + : mlir::OpConversionPattern{ctx} {} + mlir::LogicalResult + matchAndRewrite(hlfir::CharExtremumOp char_extremum, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + mlir::Location loc = char_extremum->getLoc(); + auto module = char_extremum->getParentOfType(); + auto predicate = char_extremum.getPredicate(); + bool predIsMin = + predicate == hlfir::CharExtremumPredicate::min ? true : false; + fir::FirOpBuilder builder(rewriter, fir::getKindMapping(module)); + assert(adaptor.getStrings().size() >= 2 && + "must have at least two strings operands"); + auto numOperands = adaptor.getStrings().size(); + + std::vector chars; + std::vector< + std::pair>> + pairs; + llvm::SmallVector opCBVs; + for (size_t i = 0; i < numOperands; ++i) { + chars.emplace_back(getBufferizedExprStorage(adaptor.getStrings()[i])); + pairs.emplace_back( + hlfir::translateToExtendedValue(loc, builder, chars[i])); + assert(!pairs[i].second && "expected variables"); + opCBVs.emplace_back(*pairs[i].first.getCharBox()); + } + + fir::ExtendedValue res = + fir::factory::CharacterExprHelper{builder, loc}.createCharExtremum( + predIsMin, opCBVs); + mlir::Type addrType = fir::ReferenceType::get( + hlfir::getFortranElementType(char_extremum.getResult().getType())); + mlir::Value cast = builder.createConvert(loc, addrType, fir::getBase(res)); + res = fir::substBase(res, cast); + hlfir::Entity hlfirTempRes = + hlfir::Entity{hlfir::genDeclare(loc, builder, res, "tmp", + fir::FortranVariableFlagsAttr{}) + .getBase()}; + mlir::Value bufferizedExpr = + packageBufferizedExpr(loc, builder, hlfirTempRes, false); + rewriter.replaceOp(char_extremum, bufferizedExpr); + return mlir::success(); + } +}; class BufferizeHLFIR : public hlfir::impl::BufferizeHLFIRBase { public: @@ -605,11 +653,12 @@ auto module = this->getOperation(); auto *context = &getContext(); mlir::RewritePatternSet patterns(context); - patterns.insert(context); + patterns + .insert(context); mlir::ConversionTarget target(*context); // Note that YieldElementOp is not marked as an illegal operation. // It must be erased by its parent converter and there is no explicit diff --git a/flang/test/HLFIR/char_extremum-bufferization.fir b/flang/test/HLFIR/char_extremum-bufferization.fir new file mode 100644 --- /dev/null +++ b/flang/test/HLFIR/char_extremum-bufferization.fir @@ -0,0 +1,473 @@ +// Test hlfir.concat operation lowering to operations operating on memory. + +// RUN: fir-opt %s -bufferize-hlfir | FileCheck %s +// XFAIL: * + +func.func @_QPmax1(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}) { + %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFmax1Ec1"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %2:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %3:2 = hlfir.declare %2#0 typeparams %2#1 {uniq_name = "_QFmax1Ec2"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %4:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %5:2 = hlfir.declare %4#0 typeparams %4#1 {uniq_name = "_QFmax1Ec3"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %6 = hlfir.char_extremum max, %3#0, %5#0 : (!fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + hlfir.assign %6 to %1#0 : !hlfir.expr>, !fir.boxchar<1> + hlfir.destroy %6 : !hlfir.expr> + return +} + +// func.func @_QPmax1(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}) { +// %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFmax1Ec1"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %2:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %3:2 = hlfir.declare %2#0 typeparams %2#1 {uniq_name = "_QFmax1Ec2"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %4:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %5:2 = hlfir.declare %4#0 typeparams %4#1 {uniq_name = "_QFmax1Ec3"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %6 = arith.cmpi uge, %2#1, %4#1 : index +// %7 = arith.select %6, %2#1, %4#1 : index +// %8 = fir.convert %5#1 : (!fir.ref>) -> !fir.ref +// %9 = fir.convert %3#1 : (!fir.ref>) -> !fir.ref +// %10 = fir.convert %4#1 : (index) -> i64 +// %11 = fir.convert %2#1 : (index) -> i64 +// %12 = fir.call @_FortranACharacterCompareScalar1(%8, %9, %10, %11) : (!fir.ref, !fir.ref, i64, i64) -> i32 +// %c0_i32 = arith.constant 0 : i32 +// %13 = arith.cmpi sgt, %12, %c0_i32 : i32 +// %14 = arith.select %13, %5#1, %3#1 : !fir.ref> +// %15 = arith.select %13, %4#1, %2#1 : index +// %16 = fir.alloca !fir.char<1,?>(%7 : index) {bindc_name = ".chrtmp"} +// %17 = arith.cmpi slt, %7, %15 : index +// %18 = arith.select %17, %7, %15 : index +// %c1_i64 = arith.constant 1 : i64 +// %19 = fir.convert %18 : (index) -> i64 +// %20 = arith.muli %c1_i64, %19 : i64 +// %false = arith.constant false +// %21 = fir.convert %16 : (!fir.ref>) -> !fir.ref +// %22 = fir.convert %14 : (!fir.ref>) -> !fir.ref +// fir.call @llvm.memmove.p0.p0.i64(%21, %22, %20, %false) : (!fir.ref, !fir.ref, i64, i1) -> () +// %c1 = arith.constant 1 : index +// %23 = arith.subi %7, %c1 : index +// %c32_i8 = arith.constant 32 : i8 +// %24 = fir.undefined !fir.char<1> +// %25 = fir.insert_value %24, %c32_i8, [0 : index] : (!fir.char<1>, i8) -> !fir.char<1> +// %c1_0 = arith.constant 1 : index +// fir.do_loop %arg3 = %18 to %23 step %c1_0 { +// %30 = fir.convert %16 : (!fir.ref>) -> !fir.ref>> +// %31 = fir.coordinate_of %30, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %25 to %31 : !fir.ref> +// } +// %26:2 = hlfir.declare %16 typeparams %7 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %false_1 = arith.constant false +// %27 = fir.undefined tuple, i1> +// %28 = fir.insert_value %27, %false_1, [1 : index] : (tuple, i1>, i1) -> tuple, i1> +// %29 = fir.insert_value %28, %26#0, [0 : index] : (tuple, i1>, !fir.boxchar<1>) -> tuple, i1> +// hlfir.assign %26#0 to %1#0 : !fir.boxchar<1>, !fir.boxchar<1> +// return +// } + +func.func @_QPmin1(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}) { + %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFmin1Ec1"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %2:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %3:2 = hlfir.declare %2#0 typeparams %2#1 {uniq_name = "_QFmin1Ec2"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %4:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %5:2 = hlfir.declare %4#0 typeparams %4#1 {uniq_name = "_QFmin1Ec3"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %6 = hlfir.char_extremum min, %3#0, %5#0 : (!fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + hlfir.assign %6 to %1#0 : !hlfir.expr>, !fir.boxchar<1> + hlfir.destroy %6 : !hlfir.expr> + return +} + +// func.func @_QPmin1(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}) { +// %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFmin1Ec1"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %2:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %3:2 = hlfir.declare %2#0 typeparams %2#1 {uniq_name = "_QFmin1Ec2"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %4:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %5:2 = hlfir.declare %4#0 typeparams %4#1 {uniq_name = "_QFmin1Ec3"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %6 = arith.cmpi uge, %2#1, %4#1 : index +// %7 = arith.select %6, %2#1, %4#1 : index +// %8 = fir.convert %5#1 : (!fir.ref>) -> !fir.ref +// %9 = fir.convert %3#1 : (!fir.ref>) -> !fir.ref +// %10 = fir.convert %4#1 : (index) -> i64 +// %11 = fir.convert %2#1 : (index) -> i64 +// %12 = fir.call @_FortranACharacterCompareScalar1(%8, %9, %10, %11) : (!fir.ref, !fir.ref, i64, i64) -> i32 +// %c0_i32 = arith.constant 0 : i32 +// %13 = arith.cmpi slt, %12, %c0_i32 : i32 +// %14 = arith.select %13, %5#1, %3#1 : !fir.ref> +// %15 = arith.select %13, %4#1, %2#1 : index +// %16 = fir.alloca !fir.char<1,?>(%7 : index) {bindc_name = ".chrtmp"} +// %17 = arith.cmpi slt, %7, %15 : index +// %18 = arith.select %17, %7, %15 : index +// %c1_i64 = arith.constant 1 : i64 +// %19 = fir.convert %18 : (index) -> i64 +// %20 = arith.muli %c1_i64, %19 : i64 +// %false = arith.constant false +// %21 = fir.convert %16 : (!fir.ref>) -> !fir.ref +// %22 = fir.convert %14 : (!fir.ref>) -> !fir.ref +// fir.call @llvm.memmove.p0.p0.i64(%21, %22, %20, %false) : (!fir.ref, !fir.ref, i64, i1) -> () +// %c1 = arith.constant 1 : index +// %23 = arith.subi %7, %c1 : index +// %c32_i8 = arith.constant 32 : i8 +// %24 = fir.undefined !fir.char<1> +// %25 = fir.insert_value %24, %c32_i8, [0 : index] : (!fir.char<1>, i8) -> !fir.char<1> +// %c1_0 = arith.constant 1 : index +// fir.do_loop %arg3 = %18 to %23 step %c1_0 { +// %30 = fir.convert %16 : (!fir.ref>) -> !fir.ref>> +// %31 = fir.coordinate_of %30, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %25 to %31 : !fir.ref> +// } +// %26:2 = hlfir.declare %16 typeparams %7 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %false_1 = arith.constant false +// %27 = fir.undefined tuple, i1> +// %28 = fir.insert_value %27, %false_1, [1 : index] : (tuple, i1>, i1) -> tuple, i1> +// %29 = fir.insert_value %28, %26#0, [0 : index] : (tuple, i1>, !fir.boxchar<1>) -> tuple, i1> +// hlfir.assign %26#0 to %1#0 : !fir.boxchar<1>, !fir.boxchar<1> +// return +// } + +func.func @_QPmax2(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}) { + %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %1 = fir.convert %0#0 : (!fir.ref>) -> !fir.ref>> + %c100 = arith.constant 100 : index + %2 = fir.shape %c100 : (index) -> !fir.shape<1> + %3:2 = hlfir.declare %1(%2) typeparams %0#1 {uniq_name = "_QFmax2Ec1"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.box>>, !fir.ref>>) + %4:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %c10 = arith.constant 10 : index + %5 = fir.convert %4#0 : (!fir.ref>) -> !fir.ref>> + %c100_0 = arith.constant 100 : index + %6 = fir.shape %c100_0 : (index) -> !fir.shape<1> + %7:2 = hlfir.declare %5(%6) typeparams %c10 {uniq_name = "_QFmax2Ec2"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) + %8:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %c20 = arith.constant 20 : index + %9 = fir.convert %8#0 : (!fir.ref>) -> !fir.ref>> + %c100_1 = arith.constant 100 : index + %10 = fir.shape %c100_1 : (index) -> !fir.shape<1> + %11:2 = hlfir.declare %9(%10) typeparams %c20 {uniq_name = "_QFmax2Ec3"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) + %c1 = arith.constant 1 : index + %12 = hlfir.designate %7#0 (%c1) typeparams %c10 : (!fir.ref>>, index, index) -> !fir.ref> + %c1_2 = arith.constant 1 : index + %13 = hlfir.designate %11#0 (%c1_2) typeparams %c20 : (!fir.ref>>, index, index) -> !fir.ref> + %14 = hlfir.char_extremum max, %12, %13 : (!fir.ref>, !fir.ref>) -> !hlfir.expr> + %c1_3 = arith.constant 1 : index + %15 = hlfir.designate %3#0 (%c1_3) typeparams %0#1 : (!fir.box>>, index, index) -> !fir.boxchar<1> + hlfir.assign %14 to %15 : !hlfir.expr>, !fir.boxchar<1> + hlfir.destroy %14 : !hlfir.expr> + return +} +// func.func @_QPmax2(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}) { +// %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %1 = fir.convert %0#0 : (!fir.ref>) -> !fir.ref>> +// %c100 = arith.constant 100 : index +// %2 = fir.shape %c100 : (index) -> !fir.shape<1> +// %3:2 = hlfir.declare %1(%2) typeparams %0#1 {uniq_name = "_QFmax2Ec1"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.box>>, !fir.ref>>) +// %4:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %c10 = arith.constant 10 : index +// %5 = fir.convert %4#0 : (!fir.ref>) -> !fir.ref>> +// %c100_0 = arith.constant 100 : index +// %6 = fir.shape %c100_0 : (index) -> !fir.shape<1> +// %7:2 = hlfir.declare %5(%6) typeparams %c10 {uniq_name = "_QFmax2Ec2"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) +// %8:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %c20 = arith.constant 20 : index +// %9 = fir.convert %8#0 : (!fir.ref>) -> !fir.ref>> +// %c100_1 = arith.constant 100 : index +// %10 = fir.shape %c100_1 : (index) -> !fir.shape<1> +// %11:2 = hlfir.declare %9(%10) typeparams %c20 {uniq_name = "_QFmax2Ec3"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) +// %c1 = arith.constant 1 : index +// %12 = hlfir.designate %7#0 (%c1) typeparams %c10 : (!fir.ref>>, index, index) -> !fir.ref> +// %c1_2 = arith.constant 1 : index +// %13 = hlfir.designate %11#0 (%c1_2) typeparams %c20 : (!fir.ref>>, index, index) -> !fir.ref> +// %14 = arith.cmpi uge, %c10, %c20 : index +// %15 = arith.select %14, %c10, %c20 : index +// %16 = fir.convert %13 : (!fir.ref>) -> !fir.ref +// %17 = fir.convert %12 : (!fir.ref>) -> !fir.ref +// %18 = fir.convert %c20 : (index) -> i64 +// %19 = fir.convert %c10 : (index) -> i64 +// %20 = fir.call @_FortranACharacterCompareScalar1(%16, %17, %18, %19) : (!fir.ref, !fir.ref, i64, i64) -> i32 +// %c0_i32 = arith.constant 0 : i32 +// %21 = arith.cmpi sgt, %20, %c0_i32 : i32 +// %22 = fir.convert %12 : (!fir.ref>) -> !fir.ref> +// %23 = fir.convert %13 : (!fir.ref>) -> !fir.ref> +// %24 = arith.select %21, %23, %22 : !fir.ref> +// %25 = arith.select %21, %c20, %c10 : index +// %26 = fir.alloca !fir.char<1,?>(%15 : index) {bindc_name = ".chrtmp"} +// %27 = arith.cmpi slt, %15, %25 : index +// %28 = arith.select %27, %15, %25 : index +// %c1_i64 = arith.constant 1 : i64 +// %29 = fir.convert %28 : (index) -> i64 +// %30 = arith.muli %c1_i64, %29 : i64 +// %false = arith.constant false +// %31 = fir.convert %26 : (!fir.ref>) -> !fir.ref +// %32 = fir.convert %24 : (!fir.ref>) -> !fir.ref +// fir.call @llvm.memmove.p0.p0.i64(%31, %32, %30, %false) : (!fir.ref, !fir.ref, i64, i1) -> () +// %c1_3 = arith.constant 1 : index +// %33 = arith.subi %15, %c1_3 : index +// %c32_i8 = arith.constant 32 : i8 +// %34 = fir.undefined !fir.char<1> +// %35 = fir.insert_value %34, %c32_i8, [0 : index] : (!fir.char<1>, i8) -> !fir.char<1> +// %c1_4 = arith.constant 1 : index +// fir.do_loop %arg3 = %28 to %33 step %c1_4 { +// %42 = fir.convert %26 : (!fir.ref>) -> !fir.ref>> +// %43 = fir.coordinate_of %42, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %35 to %43 : !fir.ref> +// } +// %36 = fir.convert %26 : (!fir.ref>) -> !fir.ref> +// %37:2 = hlfir.declare %36 typeparams %15 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.ref>, !fir.ref>) +// %false_5 = arith.constant false +// %38 = fir.undefined tuple>, i1> +// %39 = fir.insert_value %38, %false_5, [1 : index] : (tuple>, i1>, i1) -> tuple>, i1> +// %40 = fir.insert_value %39, %37#0, [0 : index] : (tuple>, i1>, !fir.ref>) -> tuple>, i1> +// %c1_6 = arith.constant 1 : index +// %41 = hlfir.designate %3#0 (%c1_6) typeparams %0#1 : (!fir.box>>, index, index) -> !fir.boxchar<1> +// hlfir.assign %37#0 to %41 : !fir.ref>, !fir.boxchar<1> +// return +// } + +func.func @_QPmin2(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}) { + %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %1 = fir.convert %0#0 : (!fir.ref>) -> !fir.ref>> + %c100 = arith.constant 100 : index + %2 = fir.shape %c100 : (index) -> !fir.shape<1> + %3:2 = hlfir.declare %1(%2) typeparams %0#1 {uniq_name = "_QFmin2Ec1"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.box>>, !fir.ref>>) + %4:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %c10 = arith.constant 10 : index + %5 = fir.convert %4#0 : (!fir.ref>) -> !fir.ref>> + %c100_0 = arith.constant 100 : index + %6 = fir.shape %c100_0 : (index) -> !fir.shape<1> + %7:2 = hlfir.declare %5(%6) typeparams %c10 {uniq_name = "_QFmin2Ec2"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) + %8:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %c20 = arith.constant 20 : index + %9 = fir.convert %8#0 : (!fir.ref>) -> !fir.ref>> + %c100_1 = arith.constant 100 : index + %10 = fir.shape %c100_1 : (index) -> !fir.shape<1> + %11:2 = hlfir.declare %9(%10) typeparams %c20 {uniq_name = "_QFmin2Ec3"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) + %c1 = arith.constant 1 : index + %12 = hlfir.designate %7#0 (%c1) typeparams %c10 : (!fir.ref>>, index, index) -> !fir.ref> + %c1_2 = arith.constant 1 : index + %13 = hlfir.designate %11#0 (%c1_2) typeparams %c20 : (!fir.ref>>, index, index) -> !fir.ref> + %14 = hlfir.char_extremum min, %12, %13 : (!fir.ref>, !fir.ref>) -> !hlfir.expr> + %c1_3 = arith.constant 1 : index + %15 = hlfir.designate %3#0 (%c1_3) typeparams %0#1 : (!fir.box>>, index, index) -> !fir.boxchar<1> + hlfir.assign %14 to %15 : !hlfir.expr>, !fir.boxchar<1> + hlfir.destroy %14 : !hlfir.expr> + return +} + +// func.func @_QPmin2(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}) { +// %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %1 = fir.convert %0#0 : (!fir.ref>) -> !fir.ref>> +// %c100 = arith.constant 100 : index +// %2 = fir.shape %c100 : (index) -> !fir.shape<1> +// %3:2 = hlfir.declare %1(%2) typeparams %0#1 {uniq_name = "_QFmin2Ec1"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.box>>, !fir.ref>>) +// %4:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %c10 = arith.constant 10 : index +// %5 = fir.convert %4#0 : (!fir.ref>) -> !fir.ref>> +// %c100_0 = arith.constant 100 : index +// %6 = fir.shape %c100_0 : (index) -> !fir.shape<1> +// %7:2 = hlfir.declare %5(%6) typeparams %c10 {uniq_name = "_QFmin2Ec2"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) +// %8:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %c20 = arith.constant 20 : index +// %9 = fir.convert %8#0 : (!fir.ref>) -> !fir.ref>> +// %c100_1 = arith.constant 100 : index +// %10 = fir.shape %c100_1 : (index) -> !fir.shape<1> +// %11:2 = hlfir.declare %9(%10) typeparams %c20 {uniq_name = "_QFmin2Ec3"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) +// %c1 = arith.constant 1 : index +// %12 = hlfir.designate %7#0 (%c1) typeparams %c10 : (!fir.ref>>, index, index) -> !fir.ref> +// %c1_2 = arith.constant 1 : index +// %13 = hlfir.designate %11#0 (%c1_2) typeparams %c20 : (!fir.ref>>, index, index) -> !fir.ref> +// %14 = arith.cmpi uge, %c10, %c20 : index +// %15 = arith.select %14, %c10, %c20 : index +// %16 = fir.convert %13 : (!fir.ref>) -> !fir.ref +// %17 = fir.convert %12 : (!fir.ref>) -> !fir.ref +// %18 = fir.convert %c20 : (index) -> i64 +// %19 = fir.convert %c10 : (index) -> i64 +// %20 = fir.call @_FortranACharacterCompareScalar1(%16, %17, %18, %19) : (!fir.ref, !fir.ref, i64, i64) -> i32 +// %c0_i32 = arith.constant 0 : i32 +// %21 = arith.cmpi slt, %20, %c0_i32 : i32 +// %22 = fir.convert %12 : (!fir.ref>) -> !fir.ref> +// %23 = fir.convert %13 : (!fir.ref>) -> !fir.ref> +// %24 = arith.select %21, %23, %22 : !fir.ref> +// %25 = arith.select %21, %c20, %c10 : index +// %26 = fir.alloca !fir.char<1,?>(%15 : index) {bindc_name = ".chrtmp"} +// %27 = arith.cmpi slt, %15, %25 : index +// %28 = arith.select %27, %15, %25 : index +// %c1_i64 = arith.constant 1 : i64 +// %29 = fir.convert %28 : (index) -> i64 +// %30 = arith.muli %c1_i64, %29 : i64 +// %false = arith.constant false +// %31 = fir.convert %26 : (!fir.ref>) -> !fir.ref +// %32 = fir.convert %24 : (!fir.ref>) -> !fir.ref +// fir.call @llvm.memmove.p0.p0.i64(%31, %32, %30, %false) : (!fir.ref, !fir.ref, i64, i1) -> () +// %c1_3 = arith.constant 1 : index +// %33 = arith.subi %15, %c1_3 : index +// %c32_i8 = arith.constant 32 : i8 +// %34 = fir.undefined !fir.char<1> +// %35 = fir.insert_value %34, %c32_i8, [0 : index] : (!fir.char<1>, i8) -> !fir.char<1> +// %c1_4 = arith.constant 1 : index +// fir.do_loop %arg3 = %28 to %33 step %c1_4 { +// %42 = fir.convert %26 : (!fir.ref>) -> !fir.ref>> +// %43 = fir.coordinate_of %42, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %35 to %43 : !fir.ref> +// } +// %36 = fir.convert %26 : (!fir.ref>) -> !fir.ref> +// %37:2 = hlfir.declare %36 typeparams %15 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.ref>, !fir.ref>) +// %false_5 = arith.constant false +// %38 = fir.undefined tuple>, i1> +// %39 = fir.insert_value %38, %false_5, [1 : index] : (tuple>, i1>, i1) -> tuple>, i1> +// %40 = fir.insert_value %39, %37#0, [0 : index] : (tuple>, i1>, !fir.ref>) -> tuple>, i1> +// %c1_6 = arith.constant 1 : index +// %41 = hlfir.designate %3#0 (%c1_6) typeparams %0#1 : (!fir.box>>, index, index) -> !fir.boxchar<1> +// hlfir.assign %37#0 to %41 : !fir.ref>, !fir.boxchar<1> +// return +// } + +func.func @_QPmax3(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}, %arg3: !fir.boxchar<1> {fir.bindc_name = "c4"}) { + %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFmax3Ec1"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %2:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %3:2 = hlfir.declare %2#0 typeparams %2#1 {uniq_name = "_QFmax3Ec2"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %4:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %5:2 = hlfir.declare %4#0 typeparams %4#1 {uniq_name = "_QFmax3Ec3"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %6:2 = fir.unboxchar %arg3 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %7:2 = hlfir.declare %6#0 typeparams %6#1 {uniq_name = "_QFmax3Ec4"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %8 = hlfir.char_extremum max, %3#0, %5#0, %7#0 : (!fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + hlfir.assign %8 to %1#0 : !hlfir.expr>, !fir.boxchar<1> + hlfir.destroy %8 : !hlfir.expr> + return +} + +// func.func @_QPmax3(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}, %arg3: !fir.boxchar<1> {fir.bindc_name = "c4"}) { +// %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFmax3Ec1"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %2:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %3:2 = hlfir.declare %2#0 typeparams %2#1 {uniq_name = "_QFmax3Ec2"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %4:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %5:2 = hlfir.declare %4#0 typeparams %4#1 {uniq_name = "_QFmax3Ec3"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %6:2 = fir.unboxchar %arg3 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %7:2 = hlfir.declare %6#0 typeparams %6#1 {uniq_name = "_QFmax3Ec4"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %8 = arith.cmpi uge, %2#1, %4#1 : index +// %9 = arith.select %8, %2#1, %4#1 : index +// %10 = fir.convert %5#1 : (!fir.ref>) -> !fir.ref +// %11 = fir.convert %3#1 : (!fir.ref>) -> !fir.ref +// %12 = fir.convert %4#1 : (index) -> i64 +// %13 = fir.convert %2#1 : (index) -> i64 +// %14 = fir.call @_FortranACharacterCompareScalar1(%10, %11, %12, %13) : (!fir.ref, !fir.ref, i64, i64) -> i32 +// %c0_i32 = arith.constant 0 : i32 +// %15 = arith.cmpi sgt, %14, %c0_i32 : i32 +// %16 = arith.select %15, %5#1, %3#1 : !fir.ref> +// %17 = arith.select %15, %4#1, %2#1 : index +// %18 = arith.cmpi uge, %9, %6#1 : index +// %19 = arith.select %18, %9, %6#1 : index +// %20 = fir.convert %7#1 : (!fir.ref>) -> !fir.ref +// %21 = fir.convert %16 : (!fir.ref>) -> !fir.ref +// %22 = fir.convert %6#1 : (index) -> i64 +// %23 = fir.convert %17 : (index) -> i64 +// %24 = fir.call @_FortranACharacterCompareScalar1(%20, %21, %22, %23) : (!fir.ref, !fir.ref, i64, i64) -> i32 +// %c0_i32_0 = arith.constant 0 : i32 +// %25 = arith.cmpi sgt, %24, %c0_i32_0 : i32 +// %26 = arith.select %25, %7#1, %16 : !fir.ref> +// %27 = arith.select %25, %6#1, %17 : index +// %28 = fir.alloca !fir.char<1,?>(%19 : index) {bindc_name = ".chrtmp"} +// %29 = arith.cmpi slt, %19, %27 : index +// %30 = arith.select %29, %19, %27 : index +// %c1_i64 = arith.constant 1 : i64 +// %31 = fir.convert %30 : (index) -> i64 +// %32 = arith.muli %c1_i64, %31 : i64 +// %false = arith.constant false +// %33 = fir.convert %28 : (!fir.ref>) -> !fir.ref +// %34 = fir.convert %26 : (!fir.ref>) -> !fir.ref +// fir.call @llvm.memmove.p0.p0.i64(%33, %34, %32, %false) : (!fir.ref, !fir.ref, i64, i1) -> () +// %c1 = arith.constant 1 : index +// %35 = arith.subi %19, %c1 : index +// %c32_i8 = arith.constant 32 : i8 +// %36 = fir.undefined !fir.char<1> +// %37 = fir.insert_value %36, %c32_i8, [0 : index] : (!fir.char<1>, i8) -> !fir.char<1> +// %c1_1 = arith.constant 1 : index +// fir.do_loop %arg4 = %30 to %35 step %c1_1 { +// %42 = fir.convert %28 : (!fir.ref>) -> !fir.ref>> +// %43 = fir.coordinate_of %42, %arg4 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %37 to %43 : !fir.ref> +// } +// %38:2 = hlfir.declare %28 typeparams %19 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %false_2 = arith.constant false +// %39 = fir.undefined tuple, i1> +// %40 = fir.insert_value %39, %false_2, [1 : index] : (tuple, i1>, i1) -> tuple, i1> +// %41 = fir.insert_value %40, %38#0, [0 : index] : (tuple, i1>, !fir.boxchar<1>) -> tuple, i1> +// hlfir.assign %38#0 to %1#0 : !fir.boxchar<1>, !fir.boxchar<1> +// return +// } + +func.func @_QPmin3(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}, %arg3: !fir.boxchar<1> {fir.bindc_name = "c4"}) { + %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFmin3Ec1"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %2:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %3:2 = hlfir.declare %2#0 typeparams %2#1 {uniq_name = "_QFmin3Ec2"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %4:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %5:2 = hlfir.declare %4#0 typeparams %4#1 {uniq_name = "_QFmin3Ec3"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %6:2 = fir.unboxchar %arg3 : (!fir.boxchar<1>) -> (!fir.ref>, index) + %7:2 = hlfir.declare %6#0 typeparams %6#1 {uniq_name = "_QFmin3Ec4"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) + %8 = hlfir.char_extremum min, %3#0, %5#0, %7#0 : (!fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + hlfir.assign %8 to %1#0 : !hlfir.expr>, !fir.boxchar<1> + hlfir.destroy %8 : !hlfir.expr> + return +} + +// func.func @_QPmin3(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}, %arg3: !fir.boxchar<1> {fir.bindc_name = "c4"}) { +// %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFmin3Ec1"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %2:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %3:2 = hlfir.declare %2#0 typeparams %2#1 {uniq_name = "_QFmin3Ec2"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %4:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %5:2 = hlfir.declare %4#0 typeparams %4#1 {uniq_name = "_QFmin3Ec3"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %6:2 = fir.unboxchar %arg3 : (!fir.boxchar<1>) -> (!fir.ref>, index) +// %7:2 = hlfir.declare %6#0 typeparams %6#1 {uniq_name = "_QFmin3Ec4"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %8 = arith.cmpi uge, %2#1, %4#1 : index +// %9 = arith.select %8, %2#1, %4#1 : index +// %10 = fir.convert %5#1 : (!fir.ref>) -> !fir.ref +// %11 = fir.convert %3#1 : (!fir.ref>) -> !fir.ref +// %12 = fir.convert %4#1 : (index) -> i64 +// %13 = fir.convert %2#1 : (index) -> i64 +// %14 = fir.call @_FortranACharacterCompareScalar1(%10, %11, %12, %13) : (!fir.ref, !fir.ref, i64, i64) -> i32 +// %c0_i32 = arith.constant 0 : i32 +// %15 = arith.cmpi slt, %14, %c0_i32 : i32 +// %16 = arith.select %15, %5#1, %3#1 : !fir.ref> +// %17 = arith.select %15, %4#1, %2#1 : index +// %18 = arith.cmpi uge, %9, %6#1 : index +// %19 = arith.select %18, %9, %6#1 : index +// %20 = fir.convert %7#1 : (!fir.ref>) -> !fir.ref +// %21 = fir.convert %16 : (!fir.ref>) -> !fir.ref +// %22 = fir.convert %6#1 : (index) -> i64 +// %23 = fir.convert %17 : (index) -> i64 +// %24 = fir.call @_FortranACharacterCompareScalar1(%20, %21, %22, %23) : (!fir.ref, !fir.ref, i64, i64) -> i32 +// %c0_i32_0 = arith.constant 0 : i32 +// %25 = arith.cmpi slt, %24, %c0_i32_0 : i32 +// %26 = arith.select %25, %7#1, %16 : !fir.ref> +// %27 = arith.select %25, %6#1, %17 : index +// %28 = fir.alloca !fir.char<1,?>(%19 : index) {bindc_name = ".chrtmp"} +// %29 = arith.cmpi slt, %19, %27 : index +// %30 = arith.select %29, %19, %27 : index +// %c1_i64 = arith.constant 1 : i64 +// %31 = fir.convert %30 : (index) -> i64 +// %32 = arith.muli %c1_i64, %31 : i64 +// %false = arith.constant false +// %33 = fir.convert %28 : (!fir.ref>) -> !fir.ref +// %34 = fir.convert %26 : (!fir.ref>) -> !fir.ref +// fir.call @llvm.memmove.p0.p0.i64(%33, %34, %32, %false) : (!fir.ref, !fir.ref, i64, i1) -> () +// %c1 = arith.constant 1 : index +// %35 = arith.subi %19, %c1 : index +// %c32_i8 = arith.constant 32 : i8 +// %36 = fir.undefined !fir.char<1> +// %37 = fir.insert_value %36, %c32_i8, [0 : index] : (!fir.char<1>, i8) -> !fir.char<1> +// %c1_1 = arith.constant 1 : index +// fir.do_loop %arg4 = %30 to %35 step %c1_1 { +// %42 = fir.convert %28 : (!fir.ref>) -> !fir.ref>> +// %43 = fir.coordinate_of %42, %arg4 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %37 to %43 : !fir.ref> +// } +// %38:2 = hlfir.declare %28 typeparams %19 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %false_2 = arith.constant false +// %39 = fir.undefined tuple, i1> +// %40 = fir.insert_value %39, %false_2, [1 : index] : (tuple, i1>, i1) -> tuple, i1> +// %41 = fir.insert_value %40, %38#0, [0 : index] : (tuple, i1>, !fir.boxchar<1>) -> tuple, i1> +// hlfir.assign %38#0 to %1#0 : !fir.boxchar<1>, !fir.boxchar<1> +// return +// } diff --git a/flang/test/HLFIR/char_extremum.fir b/flang/test/HLFIR/char_extremum.fir new file mode 100644 --- /dev/null +++ b/flang/test/HLFIR/char_extremum.fir @@ -0,0 +1,130 @@ +// Test hlfir.char_extremum operation parse, verify (no errors), and unparse. + +// RUN: fir-opt %s | fir-opt | FileCheck %s + + +// variable check +func.func @char_extremum_min_var(%arg0: !fir.ref>, %arg1: !fir.ref>) { + %0 = hlfir.char_extremum min, %arg0, %arg1 : (!fir.ref>, !fir.ref>) -> !hlfir.expr> + return +} + +// CHECK-LABEL: func.func @char_extremum_min_var( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !fir.ref>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !fir.ref>) { +// CHECK: %{{.*}} = hlfir.char_extremum min, %[[VAL_0]], %[[VAL_1]] : (!fir.ref>, !fir.ref>) -> !hlfir.expr> + +func.func @char_extremum_max_var(%arg0: !fir.ref>, %arg1: !fir.ref>) { +%0 = hlfir.char_extremum min, %arg0, %arg1 : (!fir.ref>, !fir.ref>) -> !hlfir.expr> +return +} + +// CHECK-LABEL: func.func @char_extremum_max_var( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !fir.ref>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !fir.ref>) { +// CHECK: %{{.*}} = hlfir.char_extremum min, %[[VAL_0]], %[[VAL_1]] : (!fir.ref>, !fir.ref>) -> !hlfir.expr> + +func.func @char_extremum_min_var_variadic(%arg0: !fir.ref>, %arg1: !fir.ref>, %arg2: !fir.ref>) { + %0 = hlfir.char_extremum min, %arg0, %arg1, %arg2 : (!fir.ref>, !fir.ref>, !fir.ref>) -> !hlfir.expr> + return +} + +// CHECK-LABEL: func.func @char_extremum_min_var_variadic( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !fir.ref>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !fir.ref>, +// CHECK-SAME: %[[VAL_2:.*]]: !fir.ref>) { +// CHECK: %{{.*}} = hlfir.char_extremum min, %[[VAL_0]], %[[VAL_1]], %[[VAL_2]] : (!fir.ref>, !fir.ref>, !fir.ref>) -> !hlfir.expr> + +func.func @char_extremum_max_var_variadic(%arg0: !fir.ref>, %arg1: !fir.ref>, %arg2: !fir.ref>) { + %0 = hlfir.char_extremum max, %arg0, %arg1, %arg2 : (!fir.ref>, !fir.ref>, !fir.ref>) -> !hlfir.expr> + return +} + +// CHECK-LABEL: func.func @char_extremum_max_var_variadic( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !fir.ref>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !fir.ref>, +// CHECK-SAME: %[[VAL_2:.*]]: !fir.ref>) { +// CHECK: %{{.*}} = hlfir.char_extremum max, %[[VAL_0]], %[[VAL_1]], %[[VAL_2]] : (!fir.ref>, !fir.ref>, !fir.ref>) -> !hlfir.expr> + + +// boxchar check +func.func @char_extremum_min_boxchar(%arg0: !fir.boxchar<1>, %arg1: !fir.boxchar<1>) { + %0 = hlfir.char_extremum min, %arg0, %arg1 : (!fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + return +} +// CHECK-LABEL: func.func @char_extremum_min_boxchar( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !fir.boxchar<1>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !fir.boxchar<1>) { +// CHECK: %{{.*}} = hlfir.char_extremum min, %[[VAL_0]], %[[VAL_1]] : (!fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + +func.func @char_extremum_max_boxchar(%arg0: !fir.boxchar<1>, %arg1: !fir.boxchar<1>) { + %0 = hlfir.char_extremum max, %arg0, %arg1 : (!fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + return +} +// CHECK-LABEL: func.func @char_extremum_max_boxchar( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !fir.boxchar<1>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !fir.boxchar<1>) { +// CHECK: %{{.*}} = hlfir.char_extremum max, %[[VAL_0]], %[[VAL_1]] : (!fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + +func.func @char_extremum_min_boxchar_variadic(%arg0: !fir.boxchar<1>, %arg1: !fir.boxchar<1>, %arg2: !fir.boxchar<1>, %arg3: !fir.boxchar<1>) { + %0 = hlfir.char_extremum min, %arg0, %arg1, %arg2, %arg3 : (!fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + return +} +// CHECK-LABEL: func.func @char_extremum_min_boxchar_variadic( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !fir.boxchar<1>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !fir.boxchar<1>, +// CHECK-SAME: %[[VAL_2:[a-zA-Z0-9_]*]]: !fir.boxchar<1>, +// CHECK-SAME: %[[VAL_3:[a-zA-Z0-9_]*]]: !fir.boxchar<1>) { +// CHECK: %{{.*}} = hlfir.char_extremum min, %[[VAL_0]], %[[VAL_1]], %[[VAL_2]], %[[VAL_3]] : (!fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + +func.func @char_extremum_max_boxchar_variadic(%arg0: !fir.boxchar<1>, %arg1: !fir.boxchar<1>, %arg2: !fir.boxchar<1>, %arg3: !fir.boxchar<1>) { + %0 = hlfir.char_extremum max, %arg0, %arg1, %arg2, %arg3 : (!fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + return +} +// CHECK-LABEL: func.func @char_extremum_max_boxchar_variadic( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !fir.boxchar<1>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !fir.boxchar<1>, +// CHECK-SAME: %[[VAL_2:[a-zA-Z0-9_]*]]: !fir.boxchar<1>, +// CHECK-SAME: %[[VAL_3:[a-zA-Z0-9_]*]]: !fir.boxchar<1>) { +// CHECK: %{{.*}} = hlfir.char_extremum max, %[[VAL_0]], %[[VAL_1]], %[[VAL_2]], %[[VAL_3]] : (!fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> + + +//expr check +func.func @char_extremum_min_expr(%arg0: !hlfir.expr>, %arg1: !hlfir.expr>) { + %0 = hlfir.char_extremum min, %arg0, %arg1 : (!hlfir.expr>, !hlfir.expr>) -> (!hlfir.expr>) + return +} +// CHECK-LABEL: func.func @char_extremum_min_expr( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !hlfir.expr>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !hlfir.expr>) { +// CHECK: %{{.*}} = hlfir.char_extremum min, %[[VAL_0]], %[[VAL_1]] : (!hlfir.expr>, !hlfir.expr>) -> !hlfir.expr> + +func.func @char_extremum_max_expr(%arg0: !hlfir.expr>, %arg1: !hlfir.expr>) { + %0 = hlfir.char_extremum max, %arg0, %arg1 : (!hlfir.expr>, !hlfir.expr>) -> (!hlfir.expr>) + return +} +// CHECK-LABEL: func.func @char_extremum_max_expr( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !hlfir.expr>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !hlfir.expr>) { +// CHECK: %{{.*}} = hlfir.char_extremum max, %[[VAL_0]], %[[VAL_1]] : (!hlfir.expr>, !hlfir.expr>) -> !hlfir.expr> + +func.func @char_extremum_min_expr_variadic(%arg0: !hlfir.expr>, %arg1: !hlfir.expr>, %arg2: !hlfir.expr>) { + %0 = hlfir.char_extremum min, %arg0, %arg1, %arg2 : (!hlfir.expr>, !hlfir.expr>, !hlfir.expr>) -> (!hlfir.expr>) + return +} +// CHECK-LABEL: func.func @char_extremum_min_expr_variadic( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !hlfir.expr>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !hlfir.expr>, +// CHECK-SAME: %[[VAL_2:[a-zA-Z0-9_]*]]: !hlfir.expr>) { +// CHECK: %{{.*}} = hlfir.char_extremum min, %[[VAL_0]], %[[VAL_1]], %[[VAL_2]] : (!hlfir.expr>, !hlfir.expr>, !hlfir.expr>) -> !hlfir.expr> + +func.func @char_extremum_max_expr_variadic(%arg0: !hlfir.expr>, %arg1: !hlfir.expr>, %arg2: !hlfir.expr>) { + %0 = hlfir.char_extremum max, %arg0, %arg1, %arg2 : (!hlfir.expr>, !hlfir.expr>, !hlfir.expr>) -> (!hlfir.expr>) + return +} +// CHECK-LABEL: func.func @char_extremum_max_expr_variadic( +// CHECK-SAME: %[[VAL_0:[a-zA-Z0-9_]*]]: !hlfir.expr>, +// CHECK-SAME: %[[VAL_1:[a-zA-Z0-9_]*]]: !hlfir.expr>, +// CHECK-SAME: %[[VAL_2:[a-zA-Z0-9_]*]]: !hlfir.expr>) { +// CHECK: %{{.*}} = hlfir.char_extremum max, %[[VAL_0]], %[[VAL_1]], %[[VAL_2]] : (!hlfir.expr>, !hlfir.expr>, !hlfir.expr>) -> !hlfir.expr> + diff --git a/flang/test/Lower/HLFIR/char_extremum.f03 b/flang/test/Lower/HLFIR/char_extremum.f03 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/HLFIR/char_extremum.f03 @@ -0,0 +1,134 @@ +! Test lowering of the max and min intrinsics on character types to HLFIR +! RUN: bbc -emit-fir -hlfir -o - %s 2>&1 | FileCheck %s + +subroutine max1(c1, c2, c3) + character(*) :: c1, c2, c3 + c1 = max(c2,c3) +end subroutine + +! CHECK-LABEL: func.func @_QPmax1 +! CHECK: %[[VAL_0:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_1:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_0]]#0 typeparams %[[VAL_0]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_2:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_3:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_2]]#0 typeparams %[[VAL_2]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_4:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_5:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_4]]#0 typeparams %[[VAL_4]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_6:[a-zA-Z0-9_]*]] = hlfir.char_extremum max, %[[VAL_3]]#0, %[[VAL_5]]#0 : (!fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> +! CHECK: hlfir.assign %[[VAL_6]] to %[[VAL_1]]#0 : !hlfir.expr>, !fir.boxchar<1> +! CHECK: hlfir.destroy %[[VAL_6]] : !hlfir.expr> + +subroutine min1(c1, c2, c3) + character(*) :: c1, c2, c3 + c1 = min(c2,c3) +end subroutine +! CHECK-LABEL: func.func @_QPmin1 +! CHECK: %[[VAL_0:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_1:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_0]]#0 typeparams %[[VAL_0]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_2:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_3:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_2]]#0 typeparams %[[VAL_2]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_4:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_5:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_4]]#0 typeparams %[[VAL_4]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_6:[a-zA-Z0-9_]*]] = hlfir.char_extremum min, %[[VAL_3]]#0, %[[VAL_5]]#0 : (!fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> +! CHECK: hlfir.assign %[[VAL_6]] to %[[VAL_1]]#0 : !hlfir.expr>, !fir.boxchar<1> +! CHECK: hlfir.destroy %[[VAL_6]] : !hlfir.expr> +! CHECK: return + +subroutine max2(c1, c2, c3) + character(*) :: c1(100) + character :: c2(100)*10, c3(100)*20 + c1(1) = max(c2(1),c3(1)) +end subroutine +! CHECK-LABEL: func.func @_QPmax2 +! CHECK: %[[VAL_0:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_1:[a-zA-Z0-9_]*]] = fir.convert %[[VAL_0]]#0 : (!fir.ref>) -> !fir.ref>> +! CHECK: %[[VAL_C100:[a-zA-Z0-9_]*]] = arith.constant 100 : index +! CHECK: %[[VAL_2:[a-zA-Z0-9_]*]] = fir.shape %[[VAL_C100]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_3:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_1]](%[[VAL_2]]) typeparams %[[VAL_0]]#1 {{.*}} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.box>>, !fir.ref>>) +! CHECK: %[[VAL_4:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_C10:[a-zA-Z0-9_]*]] = arith.constant 10 : index +! CHECK: %[[VAL_5:[a-zA-Z0-9_]*]] = fir.convert %[[VAL_4]]#0 : (!fir.ref>) -> !fir.ref>> +! CHECK: %[[VAL_C100_0:[a-zA-Z0-9_]*]] = arith.constant 100 : index +! CHECK: %[[VAL_6:[a-zA-Z0-9_]*]] = fir.shape %[[VAL_C100_0]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_7:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_5]](%[[VAL_6]]) typeparams %[[VAL_C10]] {{.*}} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) +! CHECK: %[[VAL_8:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_C20:[a-zA-Z0-9_]*]] = arith.constant 20 : index +! CHECK: %[[VAL_9:[a-zA-Z0-9_]*]] = fir.convert %[[VAL_8]]#0 : (!fir.ref>) -> !fir.ref>> +! CHECK: %[[VAL_C100_1:[a-zA-Z0-9_]*]] = arith.constant 100 : index +! CHECK: %[[VAL_10:[a-zA-Z0-9_]*]] = fir.shape %[[VAL_C100_1]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_11:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_9]](%[[VAL_10]]) typeparams %[[VAL_C20]] {{.*}} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) +! CHECK: %[[VAL_C1:[a-zA-Z0-9_]*]] = arith.constant 1 : index +! CHECK: %[[VAL_12:[a-zA-Z0-9_]*]] = hlfir.designate %[[VAL_7]]#0 (%[[VAL_C1]]) typeparams %[[VAL_C10]] : (!fir.ref>>, index, index) -> !fir.ref> +! CHECK: %[[VAL_C1_2:[a-zA-Z0-9_]*]] = arith.constant 1 : index +! CHECK: %[[VAL_13:[a-zA-Z0-9_]*]] = hlfir.designate %[[VAL_11]]#0 (%[[VAL_C1_2]]) typeparams %[[VAL_C20]] : (!fir.ref>>, index, index) -> !fir.ref> +! CHECK: %[[VAL_14:[a-zA-Z0-9_]*]] = hlfir.char_extremum max, %[[VAL_12]], %[[VAL_13]] : (!fir.ref>, !fir.ref>) -> !hlfir.expr> +! CHECK: %c1_3 = arith.constant 1 : index +! CHECK: %[[VAL_15:[a-zA-Z0-9_]*]] = hlfir.designate %[[VAL_3]]#0 (%c1_3) typeparams %[[VAL_0]]#1 : (!fir.box>>, index, index) -> !fir.boxchar<1> +! CHECK: hlfir.assign %[[VAL_14]] to %[[VAL_15]] : !hlfir.expr>, !fir.boxchar<1> +! CHECK: hlfir.destroy %[[VAL_14]] : !hlfir.expr> + +subroutine min2(c1, c2, c3) + character(*) :: c1(100) + character :: c2(100)*10, c3(100)*20 + c1(1) = min(c2(1),c3(1)) +end subroutine +! CHECK-LABEL: func.func @_QPmin2 +! CHECK: %[[VAL_0:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_1:[a-zA-Z0-9_]*]] = fir.convert %[[VAL_0]]#0 : (!fir.ref>) -> !fir.ref>> +! CHECK: %[[VAL_C100:[a-zA-Z0-9_]*]] = arith.constant 100 : index +! CHECK: %[[VAL_2:[a-zA-Z0-9_]*]] = fir.shape %[[VAL_C100]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_3:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_1]](%[[VAL_2]]) typeparams %[[VAL_0]]#1 {{.*}} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.box>>, !fir.ref>>) +! CHECK: %[[VAL_4:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_C10:[a-zA-Z0-9_]*]] = arith.constant 10 : index +! CHECK: %[[VAL_5:[a-zA-Z0-9_]*]] = fir.convert %[[VAL_4]]#0 : (!fir.ref>) -> !fir.ref>> +! CHECK: %[[VAL_C100_0:[a-zA-Z0-9_]*]] = arith.constant 100 : index +! CHECK: %[[VAL_6:[a-zA-Z0-9_]*]] = fir.shape %[[VAL_C100_0]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_7:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_5]](%[[VAL_6]]) typeparams %[[VAL_C10]] {{.*}} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) +! CHECK: %[[VAL_8:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_C20:[a-zA-Z0-9_]*]] = arith.constant 20 : index +! CHECK: %[[VAL_C9:[a-zA-Z0-9_]*]] = fir.convert %[[VAL_8]]#0 : (!fir.ref>) -> !fir.ref>> +! CHECK: %[[VAL_C100_1:[a-zA-Z0-9_]*]] = arith.constant 100 : index +! CHECK: %[[VAL_10:[a-zA-Z0-9_]*]] = fir.shape %[[VAL_C100_1]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_11:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_C9]](%[[VAL_10]]) typeparams %[[VAL_C20]] {{.*}} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) +! CHECK: %[[VAL_C1:[a-zA-Z0-9_]*]] = arith.constant 1 : index +! CHECK: %[[VAL_12:[a-zA-Z0-9_]*]] = hlfir.designate %[[VAL_7]]#0 (%[[VAL_C1]]) typeparams %[[VAL_C10]] : (!fir.ref>>, index, index) -> !fir.ref> +! CHECK: %[[VAL_C1_2:[a-zA-Z0-9_]*]] = arith.constant 1 : index +! CHECK: %[[VAL_13:[a-zA-Z0-9_]*]] = hlfir.designate %[[VAL_11]]#0 (%[[VAL_C1_2]]) typeparams %[[VAL_C20]] : (!fir.ref>>, index, index) -> !fir.ref> +! CHECK: %[[VAL_14:[a-zA-Z0-9_]*]] = hlfir.char_extremum min, %[[VAL_12]], %[[VAL_13]] : (!fir.ref>, !fir.ref>) -> !hlfir.expr> +! CHECK: %[[VAL_C1_3:[a-zA-Z0-9_]*]] = arith.constant 1 : index +! CHECK: %[[VAL_15:[a-zA-Z0-9_]*]] = hlfir.designate %[[VAL_3]]#0 (%[[VAL_C1_3]]) typeparams %[[VAL_0]]#1 : (!fir.box>>, index, index) -> !fir.boxchar<1> +! CHECK: hlfir.assign %[[VAL_14]] to %[[VAL_15]] : !hlfir.expr>, !fir.boxchar<1> +! CHECK: hlfir.destroy %[[VAL_14]] : !hlfir.expr> + +subroutine max3(c1, c2, c3, c4) + character(*) :: c1, c2, c3, c4 + c1 = max(c2, c3, c4) +end subroutine +! CHECK-LABEL: func.func @_QPmax3 +! CHECK: %[[VAL_0:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_1:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_0]]#0 typeparams %[[VAL_0]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_2:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_3:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_2]]#0 typeparams %[[VAL_2]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_4:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_5:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_4]]#0 typeparams %[[VAL_4]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_6:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg3 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_7:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_6]]#0 typeparams %[[VAL_6]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_8:[a-zA-Z0-9_]*]] = hlfir.char_extremum max, %[[VAL_3]]#0, %[[VAL_5]]#0, %[[VAL_7]]#0 : (!fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> +! CHECK: hlfir.assign %[[VAL_8]] to %[[VAL_1]]#0 : !hlfir.expr>, !fir.boxchar<1> +! CHECK: hlfir.destroy %[[VAL_8]] : !hlfir.expr> + +subroutine min3(c1, c2, c3, c4) + character(*) :: c1, c2, c3, c4 + c1 = min(c2, c3, c4) +end subroutine +! CHECK-LABEL: func.func @_QPmin3 +! CHECK: %[[VAL_0:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_1:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_0]]#0 typeparams %[[VAL_0]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_2:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_3:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_2]]#0 typeparams %[[VAL_2]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_4:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_5:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_4]]#0 typeparams %[[VAL_4]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_6:[a-zA-Z0-9_]*]]:2 = fir.unboxchar %arg3 : (!fir.boxchar<1>) -> (!fir.ref>, index) +! CHECK: %[[VAL_7:[a-zA-Z0-9_]*]]:2 = hlfir.declare %[[VAL_6]]#0 typeparams %[[VAL_6]]#1 {{.*}} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_8:[a-zA-Z0-9_]*]] = hlfir.char_extremum min, %[[VAL_3]]#0, %[[VAL_5]]#0, %[[VAL_7]]#0 : (!fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr> +! CHECK: hlfir.assign %[[VAL_8]] to %[[VAL_1]]#0 : !hlfir.expr>, !fir.boxchar<1> +! CHECK: hlfir.destroy %[[VAL_8]] : !hlfir.expr>