Skip to content

[flang] In AllocMemOp lowering, convert types for calling malloc on 32-bit #129308

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 2 commits into from
Mar 8, 2025
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
20 changes: 13 additions & 7 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,8 @@ struct EmboxCharOpConversion : public fir::FIROpConversion<fir::EmboxCharOp> {
template <typename ModuleOp>
static mlir::SymbolRefAttr
getMallocInModule(ModuleOp mod, fir::AllocMemOp op,
mlir::ConversionPatternRewriter &rewriter) {
mlir::ConversionPatternRewriter &rewriter,
mlir::Type indexType) {
static constexpr char mallocName[] = "malloc";
if (auto mallocFunc =
mod.template lookupSymbol<mlir::LLVM::LLVMFuncOp>(mallocName))
Expand All @@ -992,7 +993,6 @@ getMallocInModule(ModuleOp mod, fir::AllocMemOp op,
return mlir::SymbolRefAttr::get(userMalloc);

mlir::OpBuilder moduleBuilder(mod.getBodyRegion());
auto indexType = mlir::IntegerType::get(op.getContext(), 64);
auto mallocDecl = moduleBuilder.create<mlir::LLVM::LLVMFuncOp>(
op.getLoc(), mallocName,
mlir::LLVM::LLVMFunctionType::get(getLlvmPtrType(op.getContext()),
Expand All @@ -1002,12 +1002,13 @@ getMallocInModule(ModuleOp mod, fir::AllocMemOp op,
}

/// Return the LLVMFuncOp corresponding to the standard malloc call.
static mlir::SymbolRefAttr
getMalloc(fir::AllocMemOp op, mlir::ConversionPatternRewriter &rewriter) {
static mlir::SymbolRefAttr getMalloc(fir::AllocMemOp op,
mlir::ConversionPatternRewriter &rewriter,
mlir::Type indexType) {
if (auto mod = op->getParentOfType<mlir::gpu::GPUModuleOp>())
return getMallocInModule(mod, op, rewriter);
return getMallocInModule(mod, op, rewriter, indexType);
auto mod = op->getParentOfType<mlir::ModuleOp>();
return getMallocInModule(mod, op, rewriter);
return getMallocInModule(mod, op, rewriter, indexType);
}

/// Helper function for generating the LLVM IR that computes the distance
Expand Down Expand Up @@ -1067,7 +1068,12 @@ struct AllocMemOpConversion : public fir::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", getMalloc(heap, rewriter));
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", getMalloc(heap, rewriter, mallocTy));
rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
heap, ::getLlvmPtrType(heap.getContext()), size,
addLLVMOpBundleAttrs(rewriter, heap->getAttrs(), 1));
Expand Down
19 changes: 18 additions & 1 deletion flang/lib/Optimizer/CodeGen/TypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,27 @@

namespace fir {

static mlir::LowerToLLVMOptions MakeLowerOptions(mlir::ModuleOp module) {
llvm::StringRef dataLayoutString;
auto dataLayoutAttr = module->template getAttrOfType<mlir::StringAttr>(
mlir::LLVM::LLVMDialect::getDataLayoutAttrName());
if (dataLayoutAttr)
dataLayoutString = dataLayoutAttr.getValue();

auto options = mlir::LowerToLLVMOptions(module.getContext());
auto llvmDL = llvm::DataLayout(dataLayoutString);
if (llvmDL.getPointerSizeInBits(0) == 32) {
// FIXME: Should translateDataLayout in the MLIR layer be doing this?
options.overrideIndexBitwidth(32);
}
options.dataLayout = llvmDL;
return options;
}

LLVMTypeConverter::LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA,
bool forceUnifiedTBAATree,
const mlir::DataLayout &dl)
: mlir::LLVMTypeConverter(module.getContext()),
: mlir::LLVMTypeConverter(module.getContext(), MakeLowerOptions(module)),
kindMapping(getKindMapping(module)),
specifics(CodeGenSpecifics::get(
module.getContext(), getTargetTriple(module), getKindMapping(module),
Expand Down
29 changes: 29 additions & 0 deletions flang/test/Fir/alloc-32.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %flang_fc1 -triple i686 -emit-llvm %s -o - | FileCheck %s

// This is a check for calling malloc using i32 when on a 32-bit target (only).
// It doesn't contain the comprehensive tests that alloc.fir has, and
// that file should be used to exercise most code paths.

module attributes {
fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.target_triple = "i686"
} {

// CHECK-LABEL: define ptr @allocmem_scalar_nonchar(
// CHECK: call ptr @malloc(i32 4)
func.func @allocmem_scalar_nonchar() -> !fir.heap<i32> {
%1 = fir.allocmem i32
return %1 : !fir.heap<i32>
}

// CHECK-LABEL: define ptr @allocmem_scalar_dynchar(
// CHECK-SAME: i32 %[[len:.*]])
// CHECK: %[[mul1:.*]] = sext i32 %[[len]] to i64
// CHECK: %[[mul2:.*]] = mul i64 1, %[[mul1]]
// CHECK: %[[trunc:.*]] = trunc i64 %[[mul2]] to i32
// CHECK: call ptr @malloc(i32 %[[trunc]])
func.func @allocmem_scalar_dynchar(%l : i32) -> !fir.heap<!fir.char<1,?>> {
%1 = fir.allocmem !fir.char<1,?>(%l : i32)
return %1 : !fir.heap<!fir.char<1,?>>
}

}
3 changes: 3 additions & 0 deletions flang/test/Fir/alloc.fir
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// RUN: %flang_fc1 -emit-llvm %s -o - | FileCheck %s

// UNSUPPORTED: system-windows
// UNSUPPORTED: target-x86
// UNSUPPORTED: target=sparc-{{.*}}
// UNSUPPORTED: target=sparcel-{{.*}}

// CHECK-LABEL: define ptr @alloca_scalar_nonchar()
// CHECK: alloca i32, i64 1
Expand Down
5 changes: 4 additions & 1 deletion flang/test/Integration/OpenMP/private-global.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
!RUN: %flang_fc1 -emit-llvm -fopenmp %s -o - | FileCheck %s
!UNSUPPORTED: target-x86
!UNSUPPORTED: target=sparc-{{.*}}
!UNSUPPORTED: target=sparcel-{{.*}}

! Regression test for https://github.com/llvm/llvm-project/issues/106297

Expand Down Expand Up @@ -36,7 +39,7 @@ program bug
! CHECK: %[[FIFTY_BOX_VAL:.*]] = insertvalue { ptr, i64, i32, i8, i8, i8, i8 } { ptr undef, i64 4, i32 20240719, i8 0, i8 9, i8 0, i8 0 }, ptr %[[FIFTY]], 0
! CHECK: store { ptr, i64, i32, i8, i8, i8, i8 } %[[FIFTY_BOX_VAL]], ptr %[[BOXED_FIFTY]], align 8
! CHECK: call void @llvm.memcpy.p0.p0.i32(ptr %[[TABLE_BOX_ADDR2]], ptr %[[INTERMEDIATE]], i32 48, i1 false)
! CHECK: call void @_FortranAAssign(ptr %[[TABLE_BOX_ADDR2]], ptr %[[BOXED_FIFTY]], ptr @{{.*}}, i32 9)
! CHECK: call void @_FortranAAssign(ptr %[[TABLE_BOX_ADDR2]], ptr %[[BOXED_FIFTY]], ptr @{{.*}}, i32 12)
! CHECK: call void @llvm.memcpy.p0.p0.i32(ptr %[[TABLE_BOX_ADDR]], ptr %[[PRIV_BOX_ALLOC]], i32 48, i1 false)
! CHECK: %[[PRIV_TABLE:.*]] = call ptr @malloc(i64 40)
! ...
Expand Down
4 changes: 4 additions & 0 deletions flang/test/Lower/OpenMP/parallel-reduction-mixed.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
! RUN: %flang_fc1 -emit-llvm -fopenmp -o - %s 2>&1 \
! RUN: | FileCheck %s

! UNSUPPORTED: target-x86
! UNSUPPORTED: target=sparc-{{.*}}
! UNSUPPORTED: target=sparcel-{{.*}}

subroutine proc
implicit none
real(8),allocatable :: F(:)
Expand Down
3 changes: 3 additions & 0 deletions flang/test/Lower/forall/character-1.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
! RUN: %flang -emit-llvm -flang-deprecated-no-hlfir -S -mmlir -disable-external-name-interop %s -o - | FileCheck %s
! Test from Fortran source through to LLVM IR.
! UNSUPPORTED: system-windows
! UNSUPPORTED: target-x86
! UNSUPPORTED: target=sparc-{{.*}}
! UNSUPPORTED: target=sparcel-{{.*}}

! Assumed size array of assumed length character.
program test
Expand Down