Skip to content

[flang] Introduce omp.target_allocmem and omp.target_freemem omp dialect ops. #145464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,69 @@ def fir_ZeroOp : fir_OneResultOp<"zero_bits", [NoMemoryEffect]> {
let assemblyFormat = "type($intype) attr-dict";
}

def fir_OmpTargetAllocMemOp : fir_Op<"omp_target_allocmem",
[MemoryEffects<[MemAlloc<DefaultResource>]>, AttrSizedOperandSegments]> {
let summary = "allocate storage on an openmp device for an object of a given type";

let description = [{
Creates a heap memory reference suitable for storing a value of the
given type, T. The heap refernce returned has type `!fir.heap<T>`.
The memory object is in an undefined state. `omp_target_allocmem` operations must
be paired with `omp_target_freemem` operations to avoid memory leaks.

```
%device = arith.constant 0 : i32
%1 = fir.omp_target_allocmem %device : i32, !fir.array<3x3xi32>
```
}];

let arguments = (ins
Arg<AnyIntegerType>:$device,
TypeAttr:$in_type,
OptionalAttr<StrAttr>:$uniq_name,
OptionalAttr<StrAttr>:$bindc_name,
Variadic<AnyIntegerType>:$typeparams,
Variadic<AnyIntegerType>:$shape
);
let results = (outs fir_HeapType);

let hasCustomAssemblyFormat = 1;
let hasVerifier = 1;

let extraClassDeclaration = [{
mlir::Type getAllocatedType();
bool hasLenParams() { return !getTypeparams().empty(); }
bool hasShapeOperands() { return !getShape().empty(); }
unsigned numLenParams() { return getTypeparams().size(); }
operand_range getLenParams() { return getTypeparams(); }
unsigned numShapeOperands() { return getShape().size(); }
operand_range getShapeOperands() { return getShape(); }
static mlir::Type getRefTy(mlir::Type ty);
}];
}

def fir_OmpTargetFreeMemOp : fir_Op<"omp_target_freemem",
[MemoryEffects<[MemFree]>]> {
let summary = "free a heap object on an openmp device";

let description = [{
Deallocates a heap memory reference that was allocated by an `omp_target_allocmem`.
The memory object that is deallocated is placed in an undefined state
after `fir.omp_target_freemem`.
```
%device = arith.constant 0 : i32
%1 = fir.omp_target_allocmem %device : i32, !fir.array<3x3xi32>
fir.omp_target_freemem %device, %1 : i32, !fir.heap<!fir.array<3x3xi32>>
```
}];

let arguments = (ins
Arg<AnyIntegerType, "", [MemFree]>:$device,
Arg<fir_HeapType, "", [MemFree]>:$heapref
);
let assemblyFormat = "$device `,` $heapref attr-dict `:` type($device) `,` qualified(type($heapref))";
}

//===----------------------------------------------------------------------===//
// Terminator operations
//===----------------------------------------------------------------------===//
Expand Down
102 changes: 101 additions & 1 deletion flang/lib/Optimizer/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,105 @@ struct FreeMemOpConversion : public fir::FIROpConversion<fir::FreeMemOp> {
};
} // namespace

static mlir::LLVM::LLVMFuncOp getOmpTargetAlloc(mlir::Operation *op) {
auto module = op->getParentOfType<mlir::ModuleOp>();
if (mlir::LLVM::LLVMFuncOp mallocFunc =
module.lookupSymbol<mlir::LLVM::LLVMFuncOp>("omp_target_alloc"))
return mallocFunc;
mlir::OpBuilder moduleBuilder(module.getBodyRegion());
auto i64Ty = mlir::IntegerType::get(module->getContext(), 64);
auto i32Ty = mlir::IntegerType::get(module->getContext(), 32);
return moduleBuilder.create<mlir::LLVM::LLVMFuncOp>(
moduleBuilder.getUnknownLoc(), "omp_target_alloc",
mlir::LLVM::LLVMFunctionType::get(
mlir::LLVM::LLVMPointerType::get(module->getContext()),
{i64Ty, i32Ty},
/*isVarArg=*/false));
}

namespace {
struct OmpTargetAllocMemOpConversion
: public fir::FIROpConversion<fir::OmpTargetAllocMemOp> {
using FIROpConversion::FIROpConversion;

mlir::LogicalResult
matchAndRewrite(fir::OmpTargetAllocMemOp heap, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
mlir::Type heapTy = heap.getType();
mlir::LLVM::LLVMFuncOp mallocFunc = getOmpTargetAlloc(heap);
mlir::Location loc = heap.getLoc();
auto ity = lowerTy().indexType();
mlir::Type dataTy = fir::unwrapRefType(heapTy);
mlir::Type llvmObjectTy = convertObjectType(dataTy);
if (fir::isRecordWithTypeParameters(fir::unwrapSequenceType(dataTy)))
TODO(loc, "fir.omp_target_allocmem codegen of derived type with length "
"parameters");
mlir::Value size = genTypeSizeInBytes(loc, ity, rewriter, llvmObjectTy);
if (auto scaleSize = genAllocationScaleSize(heap, ity, rewriter))
size = rewriter.create<mlir::LLVM::MulOp>(loc, ity, size, scaleSize);
for (mlir::Value opnd : adaptor.getOperands().drop_front())
size = rewriter.create<mlir::LLVM::MulOp>(
loc, ity, size, integerCast(loc, rewriter, ity, opnd));
auto mallocTyWidth = lowerTy().getIndexTypeBitwidth();
auto mallocTy =
mlir::IntegerType::get(rewriter.getContext(), mallocTyWidth);
if (mallocTyWidth != ity.getIntOrFloatBitWidth())
size = integerCast(loc, rewriter, mallocTy, size);
heap->setAttr("callee", mlir::SymbolRefAttr::get(mallocFunc));
rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
heap, ::getLlvmPtrType(heap.getContext()),
mlir::SmallVector<mlir::Value, 2>({size, heap.getDevice()}),
addLLVMOpBundleAttrs(rewriter, heap->getAttrs(), 2));
return mlir::success();
}

/// Compute the allocation size in bytes of the element type of
/// \p llTy pointer type. The result is returned as a value of \p idxTy
/// integer type.
mlir::Value genTypeSizeInBytes(mlir::Location loc, mlir::Type idxTy,
mlir::ConversionPatternRewriter &rewriter,
mlir::Type llTy) const {
return computeElementDistance(loc, llTy, idxTy, rewriter, getDataLayout());
}
};
} // namespace

static mlir::LLVM::LLVMFuncOp getOmpTargetFree(mlir::Operation *op) {
auto module = op->getParentOfType<mlir::ModuleOp>();
if (mlir::LLVM::LLVMFuncOp freeFunc =
module.lookupSymbol<mlir::LLVM::LLVMFuncOp>("omp_target_free"))
return freeFunc;
mlir::OpBuilder moduleBuilder(module.getBodyRegion());
auto i32Ty = mlir::IntegerType::get(module->getContext(), 32);
return moduleBuilder.create<mlir::LLVM::LLVMFuncOp>(
moduleBuilder.getUnknownLoc(), "omp_target_free",
mlir::LLVM::LLVMFunctionType::get(
mlir::LLVM::LLVMVoidType::get(module->getContext()),
{getLlvmPtrType(module->getContext()), i32Ty},
/*isVarArg=*/false));
}

namespace {
struct OmpTargetFreeMemOpConversion
: public fir::FIROpConversion<fir::OmpTargetFreeMemOp> {
using FIROpConversion::FIROpConversion;

mlir::LogicalResult
matchAndRewrite(fir::OmpTargetFreeMemOp freemem, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
mlir::LLVM::LLVMFuncOp freeFunc = getOmpTargetFree(freemem);
mlir::Location loc = freemem.getLoc();
freemem->setAttr("callee", mlir::SymbolRefAttr::get(freeFunc));
rewriter.create<mlir::LLVM::CallOp>(
loc, mlir::TypeRange{},
mlir::ValueRange{adaptor.getHeapref(), freemem.getDevice()},
addLLVMOpBundleAttrs(rewriter, freemem->getAttrs(), 2));
rewriter.eraseOp(freemem);
return mlir::success();
}
};
} // namespace

// Convert subcomponent array indices from column-major to row-major ordering.
static llvm::SmallVector<mlir::Value>
convertSubcomponentIndices(mlir::Location loc, mlir::Type eleTy,
Expand Down Expand Up @@ -4274,7 +4373,8 @@ void fir::populateFIRToLLVMConversionPatterns(
GlobalLenOpConversion, GlobalOpConversion, InsertOnRangeOpConversion,
IsPresentOpConversion, LenParamIndexOpConversion, LoadOpConversion,
LocalitySpecifierOpConversion, MulcOpConversion, NegcOpConversion,
NoReassocOpConversion, SelectCaseOpConversion, SelectOpConversion,
NoReassocOpConversion, OmpTargetAllocMemOpConversion,
OmpTargetFreeMemOpConversion, SelectCaseOpConversion, SelectOpConversion,
SelectRankOpConversion, SelectTypeOpConversion, ShapeOpConversion,
ShapeShiftOpConversion, ShiftOpConversion, SliceOpConversion,
StoreOpConversion, StringLitOpConversion, SubcOpConversion,
Expand Down
90 changes: 80 additions & 10 deletions flang/lib/Optimizer/Dialect/FIROps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,38 @@ static bool verifyTypeParamCount(mlir::Type inType, unsigned numParams) {
return false;
}

/// Parser shared by Alloca and Allocmem
///
/// Parser shared by Alloca, Allocmem and OmpTargetAllocmem
/// boolean flag isTargetOp is used to identify omp_target_allocmem
/// operation ::= %res = (`fir.alloca` | `fir.allocmem`) $in_type
/// ( `(` $typeparams `)` )? ( `,` $shape )?
/// attr-dict-without-keyword
/// operation ::= %res = (`fir.omp_target_alloca`) $device : devicetype,
/// $in_type ( `(` $typeparams `)` )? ( `,` $shape )?
/// attr-dict-without-keyword
template <typename FN>
static mlir::ParseResult parseAllocatableOp(FN wrapResultType,
mlir::OpAsmParser &parser,
mlir::OperationState &result) {
static mlir::ParseResult
parseAllocatableOp(FN wrapResultType, mlir::OpAsmParser &parser,
mlir::OperationState &result, bool isTargetOp = false) {
auto &builder = parser.getBuilder();
bool hasOperands = false;
std::int32_t typeparamsSize = 0;
// Parse device number as a new operand
if (isTargetOp) {
mlir::OpAsmParser::UnresolvedOperand deviceOperand;
mlir::Type deviceType;
if (parser.parseOperand(deviceOperand) || parser.parseColonType(deviceType))
return mlir::failure();
if (parser.resolveOperand(deviceOperand, deviceType, result.operands))
return mlir::failure();
if (parser.parseComma())
return mlir::failure();
}
mlir::Type intype;
if (parser.parseType(intype))
return mlir::failure();
auto &builder = parser.getBuilder();
result.addAttribute("in_type", mlir::TypeAttr::get(intype));
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands;
llvm::SmallVector<mlir::Type> typeVec;
bool hasOperands = false;
std::int32_t typeparamsSize = 0;
if (!parser.parseOptionalLParen()) {
// parse the LEN params of the derived type. (<params> : <types>)
if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None) ||
Expand All @@ -147,13 +161,19 @@ static mlir::ParseResult parseAllocatableOp(FN wrapResultType,
parser.resolveOperands(operands, typeVec, parser.getNameLoc(),
result.operands))
return mlir::failure();

mlir::Type restype = wrapResultType(intype);
if (!restype) {
parser.emitError(parser.getNameLoc(), "invalid allocate type: ") << intype;
return mlir::failure();
}
result.addAttribute("operandSegmentSizes", builder.getDenseI32ArrayAttr(
{typeparamsSize, shapeSize}));
llvm::SmallVector<std::int32_t> segmentSizes;
if (isTargetOp)
segmentSizes.push_back(1);
segmentSizes.push_back(typeparamsSize);
segmentSizes.push_back(shapeSize);
result.addAttribute("operandSegmentSizes",
builder.getDenseI32ArrayAttr(segmentSizes));
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.addTypeToList(restype, result.types))
return mlir::failure();
Expand Down Expand Up @@ -385,6 +405,56 @@ llvm::LogicalResult fir::AllocMemOp::verify() {
return mlir::success();
}

//===----------------------------------------------------------------------===//
// OmpTargetAllocMemOp
//===----------------------------------------------------------------------===//

mlir::Type fir::OmpTargetAllocMemOp::getAllocatedType() {
return mlir::cast<fir::HeapType>(getType()).getEleTy();
}

mlir::Type fir::OmpTargetAllocMemOp::getRefTy(mlir::Type ty) {
return fir::HeapType::get(ty);
}

mlir::ParseResult
fir::OmpTargetAllocMemOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
return parseAllocatableOp(wrapAllocMemResultType, parser, result, true);
}

void fir::OmpTargetAllocMemOp::print(mlir::OpAsmPrinter &p) {
p << " ";
p.printOperand(getDevice());
p << " : ";
p << getDevice().getType();
p << ", ";
p << getInType();
if (!getTypeparams().empty()) {
p << '(' << getTypeparams() << " : " << getTypeparams().getTypes() << ')';
}
for (auto sh : getShape()) {
p << ", ";
p.printOperand(sh);
}
p.printOptionalAttrDict((*this)->getAttrs(),
{"in_type", "operandSegmentSizes"});
}

llvm::LogicalResult fir::OmpTargetAllocMemOp::verify() {
llvm::SmallVector<llvm::StringRef> visited;
if (verifyInType(getInType(), visited, numShapeOperands()))
return emitOpError("invalid type for allocation");
if (verifyTypeParamCount(getInType(), numLenParams()))
return emitOpError("LEN params do not correspond to type");
mlir::Type outType = getType();
if (!mlir::dyn_cast<fir::HeapType>(outType))
return emitOpError("must be a !fir.heap type");
if (fir::isa_unknown_size_box(fir::dyn_cast_ptrEleTy(outType)))
return emitOpError("cannot allocate !fir.box of unknown rank or type");
return mlir::success();
}

//===----------------------------------------------------------------------===//
// ArrayCoorOp
//===----------------------------------------------------------------------===//
Expand Down
28 changes: 28 additions & 0 deletions flang/test/Fir/omp_target_allocmem.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %flang_fc1 -emit-llvm %s -o - | FileCheck %s

// CHECK-LABEL: define ptr @omp_target_allocmem_array_of_nonchar(
// CHECK: call ptr @omp_target_alloc(i64 36, i32 0)
func.func @omp_target_allocmem_array_of_nonchar() -> !fir.heap<!fir.array<3x3xi32>> {
%device = arith.constant 0 : i32
%1 = fir.omp_target_allocmem %device : i32, !fir.array<3x3xi32>
return %1 : !fir.heap<!fir.array<3x3xi32>>
}

// CHECK-LABEL: define ptr @omp_target_allocmem_array_of_char(
// CHECK: call ptr @omp_target_alloc(i64 90, i32 0)
func.func @omp_target_allocmem_array_of_char() -> !fir.heap<!fir.array<3x3x!fir.char<1,10>>> {
%device = arith.constant 0 : i32
%1 = fir.omp_target_allocmem %device : i32, !fir.array<3x3x!fir.char<1,10>>
return %1 : !fir.heap<!fir.array<3x3x!fir.char<1,10>>>
}

// CHECK-LABEL: define ptr @omp_target_allocmem_array_of_dynchar(
// CHECK-SAME: i32 %[[len:.*]])
// CHECK: %[[mul1:.*]] = sext i32 %[[len]] to i64
// CHECK: %[[mul2:.*]] = mul i64 9, %[[mul1]]
// CHECK: call ptr @omp_target_alloc(i64 %[[mul2]], i32 0)
func.func @omp_target_allocmem_array_of_dynchar(%l: i32) -> !fir.heap<!fir.array<3x3x!fir.char<1,?>>> {
%device = arith.constant 0 : i32
%1 = fir.omp_target_allocmem %device : i32, !fir.array<3x3x!fir.char<1,?>>(%l : i32)
return %1 : !fir.heap<!fir.array<3x3x!fir.char<1,?>>>
}
28 changes: 28 additions & 0 deletions flang/test/Fir/omp_target_freemem.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %flang_fc1 -emit-llvm %s -o - | FileCheck %s

// CHECK-LABEL: define void @omp_target_allocmem_array_of_nonchar(
// CHECK: call void @omp_target_free(ptr {{.*}}, i32 0)
func.func @omp_target_allocmem_array_of_nonchar() -> () {
%device = arith.constant 0 : i32
%1 = fir.omp_target_allocmem %device : i32, !fir.array<3x3xi32>
fir.omp_target_freemem %device, %1 : i32, !fir.heap<!fir.array<3x3xi32>>
return
}

// CHECK-LABEL: define void @omp_target_allocmem_array_of_char(
// CHECK: call void @omp_target_free(ptr {{.*}}, i32 0)
func.func @omp_target_allocmem_array_of_char() -> () {
%device = arith.constant 0 : i32
%1 = fir.omp_target_allocmem %device : i32, !fir.array<3x3x!fir.char<1,10>>
fir.omp_target_freemem %device, %1 : i32, !fir.heap<!fir.array<3x3x!fir.char<1,10>>>
return
}

// CHECK-LABEL: define void @omp_target_allocmem_array_of_dynchar(
// CHECK: call void @omp_target_free(ptr {{.*}}, i32 0)
func.func @omp_target_allocmem_array_of_dynchar(%l: i32) -> () {
%device = arith.constant 0 : i32
%1 = fir.omp_target_allocmem %device : i32, !fir.array<3x3x!fir.char<1,?>>(%l : i32)
fir.omp_target_freemem %device, %1 : i32, !fir.heap<!fir.array<3x3x!fir.char<1,?>>>
return
}