CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
176 lines
7.0 KiB
C++
176 lines
7.0 KiB
C++
//===-- Utils.cpp ---------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "flang/Optimizer/Support/Utils.h"
|
|
#include "flang/Optimizer/Dialect/FIROps.h"
|
|
#include "flang/Optimizer/Dialect/FIRType.h"
|
|
#include "flang/Optimizer/Support/InternalNames.h"
|
|
|
|
fir::TypeInfoOp fir::lookupTypeInfoOp(fir::RecordType recordType,
|
|
mlir::ModuleOp module,
|
|
const mlir::SymbolTable *symbolTable) {
|
|
// fir.type_info was created with the mangled name of the derived type.
|
|
// It is the same as the name in the related fir.type, except when a pass
|
|
// lowered the fir.type (e.g., when lowering fir.boxproc type if the type has
|
|
// pointer procedure components), in which case suffix may have been added to
|
|
// the fir.type name. Get rid of them when looking up for the fir.type_info.
|
|
llvm::StringRef originalMangledTypeName =
|
|
fir::NameUniquer::dropTypeConversionMarkers(recordType.getName());
|
|
return fir::lookupTypeInfoOp(originalMangledTypeName, module, symbolTable);
|
|
}
|
|
|
|
fir::TypeInfoOp fir::lookupTypeInfoOp(llvm::StringRef name,
|
|
mlir::ModuleOp module,
|
|
const mlir::SymbolTable *symbolTable) {
|
|
if (symbolTable)
|
|
if (auto typeInfo = symbolTable->lookup<fir::TypeInfoOp>(name))
|
|
return typeInfo;
|
|
return module.lookupSymbol<fir::TypeInfoOp>(name);
|
|
}
|
|
|
|
std::optional<llvm::ArrayRef<int64_t>> fir::getComponentLowerBoundsIfNonDefault(
|
|
fir::RecordType recordType, llvm::StringRef component,
|
|
mlir::ModuleOp module, const mlir::SymbolTable *symbolTable) {
|
|
fir::TypeInfoOp typeInfo =
|
|
fir::lookupTypeInfoOp(recordType, module, symbolTable);
|
|
if (!typeInfo || typeInfo.getComponentInfo().empty())
|
|
return std::nullopt;
|
|
for (auto componentInfo :
|
|
typeInfo.getComponentInfo().getOps<fir::DTComponentOp>())
|
|
if (componentInfo.getName() == component)
|
|
return componentInfo.getLowerBounds();
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<bool>
|
|
fir::isRecordWithFinalRoutine(fir::RecordType recordType, mlir::ModuleOp module,
|
|
const mlir::SymbolTable *symbolTable) {
|
|
fir::TypeInfoOp typeInfo =
|
|
fir::lookupTypeInfoOp(recordType, module, symbolTable);
|
|
if (!typeInfo)
|
|
return std::nullopt;
|
|
return !typeInfo.getNoFinal();
|
|
}
|
|
|
|
mlir::LLVM::ConstantOp
|
|
fir::genConstantIndex(mlir::Location loc, mlir::Type ity,
|
|
mlir::ConversionPatternRewriter &rewriter,
|
|
std::int64_t offset) {
|
|
auto cattr = rewriter.getIntegerAttr(ity, offset);
|
|
return mlir::LLVM::ConstantOp::create(rewriter, loc, ity, cattr);
|
|
}
|
|
|
|
mlir::Value
|
|
fir::computeElementDistance(mlir::Location loc, mlir::Type llvmObjectType,
|
|
mlir::Type idxTy,
|
|
mlir::ConversionPatternRewriter &rewriter,
|
|
const mlir::DataLayout &dataLayout) {
|
|
llvm::TypeSize size = dataLayout.getTypeSize(llvmObjectType);
|
|
unsigned short alignment = dataLayout.getTypeABIAlignment(llvmObjectType);
|
|
std::int64_t distance = llvm::alignTo(size, alignment);
|
|
return fir::genConstantIndex(loc, idxTy, rewriter, distance);
|
|
}
|
|
|
|
mlir::Value
|
|
fir::genAllocationScaleSize(mlir::Location loc, mlir::Type dataTy,
|
|
mlir::Type ity,
|
|
mlir::ConversionPatternRewriter &rewriter) {
|
|
auto seqTy = mlir::dyn_cast<fir::SequenceType>(dataTy);
|
|
fir::SequenceType::Extent constSize = 1;
|
|
if (seqTy) {
|
|
int constRows = seqTy.getConstantRows();
|
|
const fir::SequenceType::ShapeRef &shape = seqTy.getShape();
|
|
if (constRows != static_cast<int>(shape.size())) {
|
|
for (auto extent : shape) {
|
|
if (constRows-- > 0)
|
|
continue;
|
|
if (extent != fir::SequenceType::getUnknownExtent())
|
|
constSize *= extent;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (constSize != 1) {
|
|
mlir::Value constVal{
|
|
fir::genConstantIndex(loc, ity, rewriter, constSize).getResult()};
|
|
return constVal;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
mlir::Value fir::integerCast(const fir::LLVMTypeConverter &converter,
|
|
mlir::Location loc,
|
|
mlir::ConversionPatternRewriter &rewriter,
|
|
mlir::Type ty, mlir::Value val, bool fold) {
|
|
auto valTy = val.getType();
|
|
// If the value was not yet lowered, lower its type so that it can
|
|
// be used in getPrimitiveTypeSizeInBits.
|
|
if (!mlir::isa<mlir::IntegerType>(valTy))
|
|
valTy = converter.convertType(valTy);
|
|
auto toSize = mlir::LLVM::getPrimitiveTypeSizeInBits(ty);
|
|
auto fromSize = mlir::LLVM::getPrimitiveTypeSizeInBits(valTy);
|
|
if (fold) {
|
|
if (toSize < fromSize)
|
|
return rewriter.createOrFold<mlir::LLVM::TruncOp>(loc, ty, val);
|
|
if (toSize > fromSize)
|
|
return rewriter.createOrFold<mlir::LLVM::SExtOp>(loc, ty, val);
|
|
} else {
|
|
if (toSize < fromSize)
|
|
return mlir::LLVM::TruncOp::create(rewriter, loc, ty, val);
|
|
if (toSize > fromSize)
|
|
return mlir::LLVM::SExtOp::create(rewriter, loc, ty, val);
|
|
}
|
|
return val;
|
|
}
|
|
|
|
std::optional<bool> fir::isNewAllocationResult(mlir::OpResult result) {
|
|
if (!result)
|
|
return std::nullopt;
|
|
auto interface =
|
|
llvm::dyn_cast<mlir::MemoryEffectOpInterface>(result.getOwner());
|
|
if (!interface)
|
|
return std::nullopt;
|
|
llvm::SmallVector<mlir::MemoryEffects::EffectInstance, 4> effects;
|
|
interface.getEffects(effects);
|
|
for (mlir::MemoryEffects::EffectInstance &e : effects) {
|
|
if (mlir::isa<mlir::MemoryEffects::Allocate>(e.getEffect()) &&
|
|
e.getValue() && e.getValue() == result)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::string fir::getPresentableFunctionName(mlir::FunctionOpInterface func) {
|
|
if (func.getName() == fir::NameUniquer::doProgramEntry()) {
|
|
// Main program entry is all uppercase - to avoid name conflicts. But
|
|
// from a reporting perspective, keep it lowercase for consistency with
|
|
// every other symbol.
|
|
if (auto bindcName =
|
|
func->getAttrOfType<mlir::StringAttr>(fir::getSymbolAttrName())) {
|
|
llvm::StringRef name = bindcName.getValue();
|
|
if (!name.empty())
|
|
return name.lower();
|
|
}
|
|
}
|
|
std::string result;
|
|
// The internal name is the saved name after ExternalNameConversion prepares
|
|
// the names for external visibility. Without doing this, for many names we
|
|
// would get the underscore version instead of the name as user wrote it.
|
|
if (auto internalName = func->getAttrOfType<mlir::StringAttr>(
|
|
fir::getInternalFuncNameAttrName());
|
|
internalName && !internalName.getValue().empty())
|
|
result = internalName.getValue().str();
|
|
else
|
|
result = func.getName().str();
|
|
return fir::NameUniquer::deconstruct(result).second.name;
|
|
}
|