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,21 @@ fir::CharBoxValue createConcatenate(const fir::CharBoxValue &lhs, const fir::CharBoxValue &rhs); + /// Create {max,min}(lhs,rhs) in temp obtained with fir.alloca + /// TODO: delete this. `createCharExtremum2` is the better solution + fir::CharBoxValue createCharExtremum(bool predIsMin, + const fir::CharBoxValue &lhs, + const fir::CharBoxValue &rhs); + /// Create {max,min}(lhs,rhs) in temp obtained with fir.alloca + /*fir::CharBoxValue createCharExtremum2( + bool predIsMin, + const llvm::SmallVector &opExvs);*/ + + /// Create {max,min}(lhs,rhs) in temp obtained with fir.alloca + fir::CharBoxValue + createCharExtremum3(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 @@ -1351,4 +1351,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/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp --- a/flang/lib/Lower/ConvertCall.cpp +++ b/flang/lib/Lower/ConvertCall.cpp @@ -1334,13 +1334,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); } @@ -1448,6 +1453,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,175 @@ mlir::Type lenType = mlir::IntegerType::get(context, 64); return mlir::TupleType::get(context, {funcPointerType, lenType}); } + +// TODO: remove this. `createCharExtremum2` is the better solution. +fir::CharBoxValue fir::factory::CharacterExprHelper::createCharExtremum( + bool predIsMin, const fir::CharBoxValue &lhs, + const fir::CharBoxValue &rhs) { + auto lhsLen = builder.createConvert(loc, builder.getCharacterLengthType(), + lhs.getLen()); + auto rhsLen = builder.createConvert(loc, builder.getCharacterLengthType(), + rhs.getLen()); + mlir::Value lhsBigger = builder.create( + loc, mlir::arith::CmpIPredicate::ugt, lhsLen, rhsLen); + mlir::Value lhsCBB = getCharBoxBuffer(lhs); + mlir::Value rhsCBB = getCharBoxBuffer(rhs); + mlir::Value biggestChar = + builder.create(loc, lhsBigger, lhsCBB, rhsCBB); + mlir::Value biggestLen = + builder.create(loc, lhsBigger, lhsLen, rhsLen); + auto temp = createCharacterTemp(biggestChar.getType(), biggestLen); + auto lowerBound = + builder.createIntegerConstant(loc, builder.getIndexType(), 0); + auto one = builder.createIntegerConstant(loc, biggestLen.getType(), 1); + auto upperBound = builder.create(loc, biggestLen, one); + mlir::Value fromBuff = predIsMin ? builder.create( + loc, lhsBigger, rhsCBB, lhsCBB) + : biggestChar; + auto toBuff = getCharBoxBuffer(temp); + fir::factory::DoLoopHelper{builder, loc}.createLoop( + lowerBound, upperBound, one, + [&](fir::FirOpBuilder &bldr, mlir::Value index) { + auto charVal = createLoadCharAt(fromBuff, index); + createStoreCharAt(toBuff, index, charVal); + }); + return temp; +} + +/*fir::CharBoxValue fir::factory::CharacterExprHelper::createCharExtremum2( + bool predIsMin, + const llvm::SmallVector &opExvs) { + + auto resultChar = *opExvs[0]; + + auto typeLen = fir::CharacterType::unknownLen(); + auto kind = recoverCharacterType(resultChar.getBuffer().getType()).getFKind(); + auto charTy = fir::CharacterType::get(builder.getContext(), kind, typeLen); + auto type = fir::ReferenceType::get(charTy); + auto buffer = builder.createConvert(loc, type, resultChar.getBuffer()); + fir::CharBoxValue castedResultCBV{buffer, resultChar.getLen()}; + + mlir::Value resultCharCBB = getCharBoxBuffer(castedResultCBV); + mlir::Value biggestLen = builder.createConvert( + loc, builder.getCharacterLengthType(), (*opExvs[0]).getLen()); + mlir::Value smallestLen = biggestLen; + + for (size_t i = 1; i < opExvs.size(); ++i) { + auto compareChar = *opExvs[i]; + auto castedBuffer = + builder.createConvert(loc, type, compareChar.getBuffer()); + fir::CharBoxValue castedCompareCBV{castedBuffer, compareChar.getLen()}; + auto compareCharCBB = getCharBoxBuffer(castedCompareCBV); + auto compareCharLen = builder.createConvert( + loc, builder.getCharacterLengthType(), compareChar.getLen()); + + mlir::Value lhsBigger = builder.create( + loc, mlir::arith::CmpIPredicate::uge, biggestLen, compareCharLen); + biggestLen = builder.create( + loc, lhsBigger, biggestLen, compareCharLen); + + if (predIsMin) { + auto lhsSmaller = builder.create( + loc, mlir::arith::CmpIPredicate::ult, smallestLen, compareCharLen); + resultCharCBB = builder.create( + loc, lhsSmaller, resultCharCBB, compareCharCBB); + } else + resultCharCBB = builder.create( + loc, lhsBigger, resultCharCBB, compareCharCBB); + } + auto resultLen = predIsMin ? smallestLen : biggestLen; + auto temp = createCharacterTemp(resultCharCBB.getType(), biggestLen); + auto lowerBound = + builder.createIntegerConstant(loc, builder.getIndexType(), 0); + auto one = builder.createIntegerConstant(loc, biggestLen.getType(), 1); + auto upperBound = builder.create(loc, resultLen, one); + auto toBuff = getCharBoxBuffer(temp); + fir::factory::DoLoopHelper{builder, loc}.createLoop( + lowerBound, upperBound, one, + [&](fir::FirOpBuilder &bldr, mlir::Value index) { + auto charVal = createLoadCharAt(resultCharCBB, index); + createStoreCharAt(toBuff, index, charVal); + }); + return temp; +}*/ + +fir::CharBoxValue fir::factory::CharacterExprHelper::createCharExtremum3( + // bool predIsMin, llvm::ArrayRef &opCBVs) { + bool predIsMin, const llvm::SmallVector &opCBVs) { + // inputs: we are given a vector of all of the charboxes of the arguments + // passed to the char_extremum op, as well as the predicate for whether we + // want llt, lle, lgt, lle + // + // 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 are undefined. + + // now we will need to loop over all of the opearnds in the vector. + // before loop, set first operand as biggest/smallest char. + // TODO: look at how concat does it. we will probably do something similar + // with temp. So, don't create temp until after the for loop is done + + 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; + + 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); + + // the cmp you pass should reflect whether predIsMin is true or false + 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 biggest size) time! + auto typeLen = fir::CharacterType::unknownLen(); + if (auto cstLen = getIntIfConstant(biggestLen)) + typeLen = *cstLen; + auto kind = recoverCharacterType(firstBuf.getType()).getFKind(); + auto charTy = fir::CharacterType::get(builder.getContext(), kind, typeLen); + auto type = fir::ReferenceType::get(charTy); + 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); + // at this point, I think the correct things are selected and casted! + } + + // now that we know the biggest/smallest char and len, we can populate a temp + // CBV and return it + fir::CharBoxValue temp = createCharacterTemp(resultBuf.getType(), biggestLen); + auto lowerBound = + builder.createIntegerConstant(loc, builder.getIndexType(), 0); + auto one = builder.createIntegerConstant(loc, resultLen.getType(), 1); + auto upperBound = builder.create(loc, resultLen, one); + auto toBuf = getCharBoxBuffer(temp); + auto fromBuf = resultBuf; + fir::factory::DoLoopHelper{builder, loc}.createLoop( + lowerBound, upperBound, one, + [&](fir::FirOpBuilder &bldr, mlir::Value index) { + auto charVal = createLoadCharAt(fromBuf, index); + createStoreCharAt(toBuf, index, charVal); + }); + return temp; +} \ No newline at end of file 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 @@ -592,6 +592,95 @@ return mlir::success(); } }; +struct CharExtremumOpConversion + : public mlir::OpConversionPattern { + using mlir::OpConversionPattern::OpConversionPattern; + explicit CharExtremumOpConversion(mlir::MLIRContext *ctx) + : mlir::OpConversionPattern{ctx} {} + // TODO: remove this. this was for use with `createCharExtremum` + /*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"); + // It seems like we should be able to handle variadic args with a little + // more effort. For feedback though, see what folks think for the case where + // it's just 2 args. + if (adaptor.getStrings().size() > 2) + TODO(loc, "codegen of optimized chained char_extremum of more than two " + "strings"); + hlfir::Entity lhs = getBufferizedExprStorage(adaptor.getStrings()[0]); + hlfir::Entity rhs = getBufferizedExprStorage(adaptor.getStrings()[1]); + auto [lhsExv, c1] = hlfir::translateToExtendedValue(loc, builder, lhs); + auto [rhsExv, c2] = hlfir::translateToExtendedValue(loc, builder, rhs); + assert(!c1 && !c2 && "expected variables"); + // things are different here! `createConcatenate` drives the bus + fir::ExtendedValue res = + fir::factory::CharacterExprHelper{builder, loc}.createCharExtremum( + predIsMin, *lhsExv.getCharBox(), *rhsExv.getCharBox()); + // Ensure the memory type is the same as the result type. + 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(); + }*/ + 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}.createCharExtremum3( + predIsMin, opCBVs); //, resultType); + 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: @@ -606,11 +695,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,421 @@ +// 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"} +// %c0 = arith.constant 0 : index +// %c1 = arith.constant 1 : index +// %17 = arith.subi %15, %c1 : index +// fir.do_loop %arg3 = %c0 to %17 step %c1 { +// %22 = fir.convert %14 : (!fir.ref>) -> !fir.ref>> +// %23 = fir.coordinate_of %22, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// %24 = fir.load %23 : !fir.ref> +// %25 = fir.convert %16 : (!fir.ref>) -> !fir.ref>> +// %26 = fir.coordinate_of %25, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %24 to %26 : !fir.ref> +// } +// %18:2 = hlfir.declare %16 typeparams %7 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %false = arith.constant false +// %19 = fir.undefined tuple, i1> +// %20 = fir.insert_value %19, %false, [1 : index] : (tuple, i1>, i1) -> tuple, i1> +// %21 = fir.insert_value %20, %18#0, [0 : index] : (tuple, i1>, !fir.boxchar<1>) -> tuple, i1> +// hlfir.assign %18#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"} +// %c0 = arith.constant 0 : index +// %c1 = arith.constant 1 : index +// %17 = arith.subi %15, %c1 : index +// fir.do_loop %arg3 = %c0 to %17 step %c1 { +// %22 = fir.convert %14 : (!fir.ref>) -> !fir.ref>> +// %23 = fir.coordinate_of %22, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// %24 = fir.load %23 : !fir.ref> +// %25 = fir.convert %16 : (!fir.ref>) -> !fir.ref>> +// %26 = fir.coordinate_of %25, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %24 to %26 : !fir.ref> +// } +// %18:2 = hlfir.declare %16 typeparams %7 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %false = arith.constant false +// %19 = fir.undefined tuple, i1> +// %20 = fir.insert_value %19, %false, [1 : index] : (tuple, i1>, i1) -> tuple, i1> +// %21 = fir.insert_value %20, %18#0, [0 : index] : (tuple, i1>, !fir.boxchar<1>) -> tuple, i1> +// hlfir.assign %18#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"} +// %c0 = arith.constant 0 : index +// %c1_3 = arith.constant 1 : index +// %27 = arith.subi %25, %c1_3 : index +// fir.do_loop %arg3 = %c0 to %27 step %c1_3 { +// %34 = fir.convert %24 : (!fir.ref>) -> !fir.ref>> +// %35 = fir.coordinate_of %34, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// %36 = fir.load %35 : !fir.ref> +// %37 = fir.convert %26 : (!fir.ref>) -> !fir.ref>> +// %38 = fir.coordinate_of %37, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %36 to %38 : !fir.ref> +// } +// %28 = fir.convert %26 : (!fir.ref>) -> !fir.ref> +// %29:2 = hlfir.declare %28 typeparams %15 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.ref>, !fir.ref>) +// %false = arith.constant false +// %30 = fir.undefined tuple>, i1> +// %31 = fir.insert_value %30, %false, [1 : index] : (tuple>, i1>, i1) -> tuple>, i1> +// %32 = fir.insert_value %31, %29#0, [0 : index] : (tuple>, i1>, !fir.ref>) -> tuple>, i1> +// %c1_4 = arith.constant 1 : index +// %33 = hlfir.designate %3#0 (%c1_4) typeparams %0#1 : (!fir.box>>, index, index) -> !fir.boxchar<1> +// hlfir.assign %29#0 to %33 : !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"} +// %c0 = arith.constant 0 : index +// %c1_3 = arith.constant 1 : index +// %27 = arith.subi %25, %c1_3 : index +// fir.do_loop %arg3 = %c0 to %27 step %c1_3 { +// %34 = fir.convert %24 : (!fir.ref>) -> !fir.ref>> +// %35 = fir.coordinate_of %34, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// %36 = fir.load %35 : !fir.ref> +// %37 = fir.convert %26 : (!fir.ref>) -> !fir.ref>> +// %38 = fir.coordinate_of %37, %arg3 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %36 to %38 : !fir.ref> +// } +// %28 = fir.convert %26 : (!fir.ref>) -> !fir.ref> +// %29:2 = hlfir.declare %28 typeparams %15 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.ref>, !fir.ref>) +// %false = arith.constant false +// %30 = fir.undefined tuple>, i1> +// %31 = fir.insert_value %30, %false, [1 : index] : (tuple>, i1>, i1) -> tuple>, i1> +// %32 = fir.insert_value %31, %29#0, [0 : index] : (tuple>, i1>, !fir.ref>) -> tuple>, i1> +// %c1_4 = arith.constant 1 : index +// %33 = hlfir.designate %3#0 (%c1_4) typeparams %0#1 : (!fir.box>>, index, index) -> !fir.boxchar<1> +// hlfir.assign %29#0 to %33 : !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"} +// %c0 = arith.constant 0 : index +// %c1 = arith.constant 1 : index +// %29 = arith.subi %27, %c1 : index +// fir.do_loop %arg4 = %c0 to %29 step %c1 { +// %34 = fir.convert %26 : (!fir.ref>) -> !fir.ref>> +// %35 = fir.coordinate_of %34, %arg4 : (!fir.ref>>, index) -> !fir.ref> +// %36 = fir.load %35 : !fir.ref> +// %37 = fir.convert %28 : (!fir.ref>) -> !fir.ref>> +// %38 = fir.coordinate_of %37, %arg4 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %36 to %38 : !fir.ref> +// } +// %30:2 = hlfir.declare %28 typeparams %19 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %false = arith.constant false +// %31 = fir.undefined tuple, i1> +// %32 = fir.insert_value %31, %false, [1 : index] : (tuple, i1>, i1) -> tuple, i1> +// %33 = fir.insert_value %32, %30#0, [0 : index] : (tuple, i1>, !fir.boxchar<1>) -> tuple, i1> +// hlfir.assign %30#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"} +// %c0 = arith.constant 0 : index +// %c1 = arith.constant 1 : index +// %29 = arith.subi %27, %c1 : index +// fir.do_loop %arg4 = %c0 to %29 step %c1 { +// %34 = fir.convert %26 : (!fir.ref>) -> !fir.ref>> +// %35 = fir.coordinate_of %34, %arg4 : (!fir.ref>>, index) -> !fir.ref> +// %36 = fir.load %35 : !fir.ref> +// %37 = fir.convert %28 : (!fir.ref>) -> !fir.ref>> +// %38 = fir.coordinate_of %37, %arg4 : (!fir.ref>>, index) -> !fir.ref> +// fir.store %36 to %38 : !fir.ref> +// } +// %30:2 = hlfir.declare %28 typeparams %19 {uniq_name = "tmp"} : (!fir.ref>, index) -> (!fir.boxchar<1>, !fir.ref>) +// %false = arith.constant false +// %31 = fir.undefined tuple, i1> +// %32 = fir.insert_value %31, %false, [1 : index] : (tuple, i1>, i1) -> tuple, i1> +// %33 = fir.insert_value %32, %30#0, [0 : index] : (tuple, i1>, !fir.boxchar<1>) -> tuple, i1> +// hlfir.assign %30#0 to %1#0 : !fir.boxchar<1>, !fir.boxchar<1> +// return +// } +// func.func private @_FortranACharacterCompareScalar1(!fir.ref, !fir.ref, i64, i64) -> i32 attributes {fir.runtime} \ No newline at end of file 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>