diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -2217,7 +2217,7 @@ if (fir::isa_complex(objectTy)) { mlir::LLVM::ConstantOp c0 = genConstantIndex(loc, lowerTy().indexType(), rewriter, 0); - SmallVector offs = {c0, operands[1]}; + llvm::SmallVector offs = {c0, operands[1]}; mlir::Value gep = genGEP(loc, ty, rewriter, base, offs); rewriter.replaceOp(coor, gep); return success(); @@ -2227,15 +2227,16 @@ if (baseObjectTy.dyn_cast()) return doRewriteBox(coor, ty, operands, loc, rewriter); - // Reference or pointer type - if (baseObjectTy.isa()) + // Reference, pointer or a heap type + if (baseObjectTy + .isa()) return doRewriteRefOrPtr(coor, ty, operands, loc, rewriter); return rewriter.notifyMatchFailure( coor, "fir.coordinate_of base operand has unsupported type"); } - unsigned getFieldNumber(fir::RecordType ty, mlir::Value op) const { + static unsigned getFieldNumber(fir::RecordType ty, mlir::Value op) { return fir::hasDynamicSize(ty) ? op.getDefiningOp() ->getAttrOfType("field") @@ -2243,7 +2244,7 @@ : getIntValue(op); } - int64_t getIntValue(mlir::Value val) const { + static int64_t getIntValue(mlir::Value val) { assert(val && val.dyn_cast() && "must not be null value"); mlir::Operation *defop = val.getDefiningOp(); @@ -2255,7 +2256,7 @@ fir::emitFatalError(val.getLoc(), "must be a constant"); } - bool hasSubDimensions(mlir::Type type) const { + static bool hasSubDimensions(mlir::Type type) { return type.isa(); } @@ -2264,7 +2265,7 @@ /// all valid forms of `!fir.coordinate_of`. /// TODO: Either implement the unsupported cases or extend the verifier /// in FIROps.cpp instead. - bool supportedCoordinate(mlir::Type type, mlir::ValueRange coors) const { + static bool supportedCoordinate(mlir::Type type, mlir::ValueRange coors) { const std::size_t numOfCoors = coors.size(); std::size_t i = 0; bool subEle = false; @@ -2293,10 +2294,9 @@ /// Walk the abstract memory layout and determine if the path traverses any /// array types with unknown shape. Return true iff all the array types have a /// constant shape along the path. - bool arraysHaveKnownShape(mlir::Type type, mlir::ValueRange coors) const { + static bool arraysHaveKnownShape(mlir::Type type, mlir::ValueRange coors) { const std::size_t sz = coors.size(); - std::size_t i = 0; - for (; i < sz; ++i) { + for (std::size_t i = 0; i < sz; ++i) { mlir::Value nxtOpnd = coors[i]; if (auto arrTy = type.dyn_cast()) { if (fir::sequenceWithNonConstantShape(arrTy)) @@ -2329,12 +2329,10 @@ // %lenp = fir.len_param_index len1, !fir.type // %addr = coordinate_of %box, %lenp if (coor.getNumOperands() == 2) { - mlir::Operation *coordinateDef = - (*coor.getCoor().begin()).getDefiningOp(); - if (isa_and_nonnull(coordinateDef)) { + mlir::Operation *coordinateDef = (*coor.getCoor().begin()).getDefiningOp(); + if (isa_and_nonnull(coordinateDef)) TODO(loc, "fir.coordinate_of - fir.len_param_index is not supported yet"); - } } // 2. GENERAL CASE: @@ -2347,9 +2345,10 @@ // %idx = ... : i32 // %resultAddr = coordinate_of %box, %idx : !fir.ref // 2.3 (`fir.derived` inside `fir.array`) - // %box = ... : !fir.box>> %idx1 = ... : index %idx2 = ... : i32 %resultAddr = - // coordinate_of %box, %idx1, %idx2 : !fir.ref + // %box = ... : !fir.box>> + // %idx1 = ... : index + // %idx2 = ... : i32 + // %resultAddr = coordinate_of %box, %idx1, %idx2 : !fir.ref // 2.4. TODO: Either document or disable any other case that the following // implementation might convert. mlir::LLVM::ConstantOp c0 = @@ -2357,11 +2356,12 @@ mlir::Value resultAddr = loadBaseAddrFromBox(loc, getBaseAddrTypeFromBox(boxBaseAddr.getType()), boxBaseAddr, rewriter); - auto currentObjTy = fir::dyn_cast_ptrOrBoxEleTy(boxObjTy); + // Component Type + auto cpnTy = fir::dyn_cast_ptrOrBoxEleTy(boxObjTy); mlir::Type voidPtrTy = ::getVoidPtrType(coor.getContext()); for (unsigned i = 1, last = operands.size(); i < last; ++i) { - if (auto arrTy = currentObjTy.dyn_cast()) { + if (auto arrTy = cpnTy.dyn_cast()) { if (i != 1) TODO(loc, "fir.array nested inside other array and/or derived type"); // Applies byte strides from the box. Ignore lower bound from box @@ -2384,16 +2384,16 @@ resultAddr = rewriter.create(loc, voidPtrTy, voidPtrBase, args); i += arrTy.getDimension() - 1; - currentObjTy = arrTy.getEleTy(); - } else if (auto recTy = currentObjTy.dyn_cast()) { + cpnTy = arrTy.getEleTy(); + } else if (auto recTy = cpnTy.dyn_cast()) { auto recRefTy = mlir::LLVM::LLVMPointerType::get(lowerTy().convertType(recTy)); mlir::Value nxtOpnd = operands[i]; auto memObj = rewriter.create(loc, recRefTy, resultAddr); llvm::SmallVector args = {c0, nxtOpnd}; - currentObjTy = recTy.getType(getFieldNumber(recTy, nxtOpnd)); - auto llvmCurrentObjTy = lowerTy().convertType(currentObjTy); + cpnTy = recTy.getType(getFieldNumber(recTy, nxtOpnd)); + auto llvmCurrentObjTy = lowerTy().convertType(cpnTy); auto gep = rewriter.create( loc, mlir::LLVM::LLVMPointerType::get(llvmCurrentObjTy), memObj, args); @@ -2414,25 +2414,25 @@ mlir::ConversionPatternRewriter &rewriter) const { mlir::Type baseObjectTy = coor.getBaseType(); - mlir::Type currentObjTy = fir::dyn_cast_ptrOrBoxEleTy(baseObjectTy); - bool hasSubdimension = hasSubDimensions(currentObjTy); + // Component Type + mlir::Type cpnTy = fir::dyn_cast_ptrOrBoxEleTy(baseObjectTy); + bool hasSubdimension = hasSubDimensions(cpnTy); bool columnIsDeferred = !hasSubdimension; - if (!supportedCoordinate(currentObjTy, operands.drop_front(1))) { + if (!supportedCoordinate(cpnTy, operands.drop_front(1))) TODO(loc, "unsupported combination of coordinate operands"); - } const bool hasKnownShape = - arraysHaveKnownShape(currentObjTy, operands.drop_front(1)); + arraysHaveKnownShape(cpnTy, operands.drop_front(1)); // If only the column is `?`, then we can simply place the column value in // the 0-th GEP position. - if (auto arrTy = currentObjTy.dyn_cast()) { + if (auto arrTy = cpnTy.dyn_cast()) { if (!hasKnownShape) { const unsigned sz = arrTy.getDimension(); if (arraysHaveKnownShape(arrTy.getEleTy(), operands.drop_front(1 + sz))) { - llvm::ArrayRef shape = arrTy.getShape(); + fir::SequenceType::ShapeRef shape = arrTy.getShape(); bool allConst = true; for (unsigned i = 0; i < sz - 1; ++i) { if (shape[i] < 0) { @@ -2446,11 +2446,9 @@ } } - if (fir::hasDynamicSize(fir::unwrapSequenceType(currentObjTy))) { - mlir::emitError( + if (fir::hasDynamicSize(fir::unwrapSequenceType(cpnTy))) + return mlir::emitError( loc, "fir.coordinate_of with a dynamic element size is unsupported"); - return failure(); - } if (hasKnownShape || columnIsDeferred) { SmallVector offs; @@ -2459,16 +2457,13 @@ genConstantIndex(loc, lowerTy().indexType(), rewriter, 0); offs.push_back(c0); } - const std::size_t sz = operands.size(); Optional dims; SmallVector arrIdx; - for (std::size_t i = 1; i < sz; ++i) { + for (std::size_t i = 1, sz = operands.size(); i < sz; ++i) { mlir::Value nxtOpnd = operands[i]; - if (!currentObjTy) { - mlir::emitError(loc, "invalid coordinate/check failed"); - return failure(); - } + if (!cpnTy) + return mlir::emitError(loc, "invalid coordinate/check failed"); // check if the i-th coordinate relates to an array if (dims.hasValue()) { @@ -2478,32 +2473,32 @@ dims = dimsLeft - 1; continue; } - currentObjTy = currentObjTy.cast().getEleTy(); + cpnTy = cpnTy.cast().getEleTy(); // append array range in reverse (FIR arrays are column-major) offs.append(arrIdx.rbegin(), arrIdx.rend()); arrIdx.clear(); dims.reset(); continue; } - if (auto arrTy = currentObjTy.dyn_cast()) { + if (auto arrTy = cpnTy.dyn_cast()) { int d = arrTy.getDimension() - 1; if (d > 0) { dims = d; arrIdx.push_back(nxtOpnd); continue; } - currentObjTy = currentObjTy.cast().getEleTy(); + cpnTy = cpnTy.cast().getEleTy(); offs.push_back(nxtOpnd); continue; } // check if the i-th coordinate relates to a field - if (auto recTy = currentObjTy.dyn_cast()) - currentObjTy = recTy.getType(getFieldNumber(recTy, nxtOpnd)); - else if (auto tupTy = currentObjTy.dyn_cast()) - currentObjTy = tupTy.getType(getIntValue(nxtOpnd)); + if (auto recTy = cpnTy.dyn_cast()) + cpnTy = recTy.getType(getFieldNumber(recTy, nxtOpnd)); + else if (auto tupTy = cpnTy.dyn_cast()) + cpnTy = tupTy.getType(getIntValue(nxtOpnd)); else - currentObjTy = nullptr; + cpnTy = nullptr; offs.push_back(nxtOpnd); } @@ -2515,8 +2510,8 @@ return success(); } - mlir::emitError(loc, "fir.coordinate_of base operand has unsupported type"); - return failure(); + return mlir::emitError( + loc, "fir.coordinate_of base operand has unsupported type"); } };