Skip to content

[flang] Allow user to define free via BIND(C) #78428

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

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 24 additions & 17 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,20 +1156,23 @@ struct EmboxCharOpConversion : public FIROpConversion<fir::EmboxCharOp> {
} // namespace

/// Return the LLVMFuncOp corresponding to the standard malloc call.
static mlir::LLVM::LLVMFuncOp
static mlir::SymbolRefAttr
getMalloc(fir::AllocMemOp op, mlir::ConversionPatternRewriter &rewriter) {
static constexpr char mallocName[] = "malloc";
auto module = op->getParentOfType<mlir::ModuleOp>();
if (mlir::LLVM::LLVMFuncOp mallocFunc =
module.lookupSymbol<mlir::LLVM::LLVMFuncOp>("malloc"))
return mallocFunc;
if (auto mallocFunc = module.lookupSymbol<mlir::LLVM::LLVMFuncOp>(mallocName))
return mlir::SymbolRefAttr::get(mallocFunc);
if (auto userMalloc = module.lookupSymbol<mlir::func::FuncOp>(mallocName))
return mlir::SymbolRefAttr::get(userMalloc);
mlir::OpBuilder moduleBuilder(
op->getParentOfType<mlir::ModuleOp>().getBodyRegion());
auto indexType = mlir::IntegerType::get(op.getContext(), 64);
return moduleBuilder.create<mlir::LLVM::LLVMFuncOp>(
rewriter.getUnknownLoc(), "malloc",
auto mallocDecl = moduleBuilder.create<mlir::LLVM::LLVMFuncOp>(
op.getLoc(), mallocName,
mlir::LLVM::LLVMFunctionType::get(getLlvmPtrType(op.getContext()),
indexType,
/*isVarArg=*/false));
return mlir::SymbolRefAttr::get(mallocDecl);
}

/// Helper function for generating the LLVM IR that computes the distance
Expand Down Expand Up @@ -1217,7 +1220,6 @@ struct AllocMemOpConversion : public FIROpConversion<fir::AllocMemOp> {
matchAndRewrite(fir::AllocMemOp heap, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
mlir::Type heapTy = heap.getType();
mlir::LLVM::LLVMFuncOp mallocFunc = getMalloc(heap, rewriter);
mlir::Location loc = heap.getLoc();
auto ity = lowerTy().indexType();
mlir::Type dataTy = fir::unwrapRefType(heapTy);
Expand All @@ -1230,7 +1232,7 @@ struct AllocMemOpConversion : public FIROpConversion<fir::AllocMemOp> {
for (mlir::Value opnd : adaptor.getOperands())
size = rewriter.create<mlir::LLVM::MulOp>(
loc, ity, size, integerCast(loc, rewriter, ity, opnd));
heap->setAttr("callee", mlir::SymbolRefAttr::get(mallocFunc));
heap->setAttr("callee", getMalloc(heap, rewriter));
rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
heap, ::getLlvmPtrType(heap.getContext()), size, heap->getAttrs());
return mlir::success();
Expand All @@ -1248,19 +1250,25 @@ struct AllocMemOpConversion : public FIROpConversion<fir::AllocMemOp> {
} // namespace

/// Return the LLVMFuncOp corresponding to the standard free call.
static mlir::LLVM::LLVMFuncOp
getFree(fir::FreeMemOp op, mlir::ConversionPatternRewriter &rewriter) {
static mlir::SymbolRefAttr getFree(fir::FreeMemOp op,
mlir::ConversionPatternRewriter &rewriter) {
static constexpr char freeName[] = "free";
auto module = op->getParentOfType<mlir::ModuleOp>();
if (mlir::LLVM::LLVMFuncOp freeFunc =
module.lookupSymbol<mlir::LLVM::LLVMFuncOp>("free"))
return freeFunc;
// Check if free already defined in the module.
if (auto freeFunc = module.lookupSymbol<mlir::LLVM::LLVMFuncOp>(freeName))
return mlir::SymbolRefAttr::get(freeFunc);
if (auto freeDefinedByUser =
module.lookupSymbol<mlir::func::FuncOp>(freeName))
return mlir::SymbolRefAttr::get(freeDefinedByUser);
// Create llvm declaration for free.
mlir::OpBuilder moduleBuilder(module.getBodyRegion());
auto voidType = mlir::LLVM::LLVMVoidType::get(op.getContext());
return moduleBuilder.create<mlir::LLVM::LLVMFuncOp>(
rewriter.getUnknownLoc(), "free",
auto freeDecl = moduleBuilder.create<mlir::LLVM::LLVMFuncOp>(
rewriter.getUnknownLoc(), freeName,
mlir::LLVM::LLVMFunctionType::get(voidType,
getLlvmPtrType(op.getContext()),
/*isVarArg=*/false));
return mlir::SymbolRefAttr::get(freeDecl);
}

static unsigned getDimension(mlir::LLVM::LLVMArrayType ty) {
Expand All @@ -1280,9 +1288,8 @@ struct FreeMemOpConversion : public FIROpConversion<fir::FreeMemOp> {
mlir::LogicalResult
matchAndRewrite(fir::FreeMemOp freemem, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
mlir::LLVM::LLVMFuncOp freeFunc = getFree(freemem, rewriter);
mlir::Location loc = freemem.getLoc();
freemem->setAttr("callee", mlir::SymbolRefAttr::get(freeFunc));
freemem->setAttr("callee", getFree(freemem, rewriter));
rewriter.create<mlir::LLVM::CallOp>(loc, mlir::TypeRange{},
mlir::ValueRange{adaptor.getHeapref()},
freemem->getAttrs());
Expand Down
22 changes: 22 additions & 0 deletions flang/test/Fir/already-defined-free.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Test that FIR codegen handles cases when free and malloc have
// already been defined in FIR (either by the user in Fortran via
// BIND(C) or by some FIR pass in between).
// RUN: fir-opt --fir-to-llvm-ir %s | FileCheck %s


func.func @already_declared_free_malloc() {
%c4 = arith.constant 4 : index
%0 = fir.call @malloc(%c4) : (index) -> !fir.heap<i32>
fir.call @free(%0) : (!fir.heap<i32>) -> ()
%1 = fir.allocmem i32
fir.freemem %1 : !fir.heap<i32>
return
}

// CHECK: llvm.call @malloc(%{{.*}})
// CHECK: llvm.call @free(%{{.*}})
// CHECK: llvm.call @malloc(%{{.*}})
// CHECK: llvm.call @free(%{{.*}})

func.func private @free(!fir.heap<i32>)
func.func private @malloc(index) -> !fir.heap<i32>