Skip to content

Commit b7093a9

Browse files
committed
Reapply "[flang] In AllocMemOp lowering, convert types for calling malloc on 32-bit (llvm#129308)"
Changes: * The alloc-32.fir test is now marked as requiring the X86 target. * Drive-by fixes uncovered by fixing tests involving malloc
1 parent 3121da5 commit b7093a9

File tree

8 files changed

+77
-16
lines changed

8 files changed

+77
-16
lines changed

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,8 @@ struct EmboxCharOpConversion : public fir::FIROpConversion<fir::EmboxCharOp> {
982982
template <typename ModuleOp>
983983
static mlir::SymbolRefAttr
984984
getMallocInModule(ModuleOp mod, fir::AllocMemOp op,
985-
mlir::ConversionPatternRewriter &rewriter) {
985+
mlir::ConversionPatternRewriter &rewriter,
986+
mlir::Type indexType) {
986987
static constexpr char mallocName[] = "malloc";
987988
if (auto mallocFunc =
988989
mod.template lookupSymbol<mlir::LLVM::LLVMFuncOp>(mallocName))
@@ -992,7 +993,6 @@ getMallocInModule(ModuleOp mod, fir::AllocMemOp op,
992993
return mlir::SymbolRefAttr::get(userMalloc);
993994

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

10041004
/// Return the LLVMFuncOp corresponding to the standard malloc call.
1005-
static mlir::SymbolRefAttr
1006-
getMalloc(fir::AllocMemOp op, mlir::ConversionPatternRewriter &rewriter) {
1005+
static mlir::SymbolRefAttr getMalloc(fir::AllocMemOp op,
1006+
mlir::ConversionPatternRewriter &rewriter,
1007+
mlir::Type indexType) {
10071008
if (auto mod = op->getParentOfType<mlir::gpu::GPUModuleOp>())
1008-
return getMallocInModule(mod, op, rewriter);
1009+
return getMallocInModule(mod, op, rewriter, indexType);
10091010
auto mod = op->getParentOfType<mlir::ModuleOp>();
1010-
return getMallocInModule(mod, op, rewriter);
1011+
return getMallocInModule(mod, op, rewriter, indexType);
10111012
}
10121013

10131014
/// Helper function for generating the LLVM IR that computes the distance
@@ -1067,7 +1068,12 @@ struct AllocMemOpConversion : public fir::FIROpConversion<fir::AllocMemOp> {
10671068
for (mlir::Value opnd : adaptor.getOperands())
10681069
size = rewriter.create<mlir::LLVM::MulOp>(
10691070
loc, ity, size, integerCast(loc, rewriter, ity, opnd));
1070-
heap->setAttr("callee", getMalloc(heap, rewriter));
1071+
auto mallocTyWidth = lowerTy().getIndexTypeBitwidth();
1072+
auto mallocTy =
1073+
mlir::IntegerType::get(rewriter.getContext(), mallocTyWidth);
1074+
if (mallocTyWidth != ity.getIntOrFloatBitWidth())
1075+
size = integerCast(loc, rewriter, mallocTy, size);
1076+
heap->setAttr("callee", getMalloc(heap, rewriter, mallocTy));
10711077
rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
10721078
heap, ::getLlvmPtrType(heap.getContext()), size,
10731079
addLLVMOpBundleAttrs(rewriter, heap->getAttrs(), 1));
@@ -2116,7 +2122,7 @@ struct XReboxOpConversion : public EmboxCommonConversion<fir::cg::XReboxOp> {
21162122
unsigned dim = iter.index();
21172123
mlir::Value lb = one;
21182124
if (!lbounds.empty()) {
2119-
lb = lbounds[dim];
2125+
lb = integerCast(loc, rewriter, lowerTy().indexType(), lbounds[dim]);
21202126
auto extentIsEmpty = rewriter.create<mlir::LLVM::ICmpOp>(
21212127
loc, mlir::LLVM::ICmpPredicate::eq, extent, zero);
21222128
lb = rewriter.create<mlir::LLVM::SelectOp>(loc, extentIsEmpty, one, lb);

flang/lib/Optimizer/CodeGen/TypeConverter.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,27 @@
2828

2929
namespace fir {
3030

31+
static mlir::LowerToLLVMOptions MakeLowerOptions(mlir::ModuleOp module) {
32+
llvm::StringRef dataLayoutString;
33+
auto dataLayoutAttr = module->template getAttrOfType<mlir::StringAttr>(
34+
mlir::LLVM::LLVMDialect::getDataLayoutAttrName());
35+
if (dataLayoutAttr)
36+
dataLayoutString = dataLayoutAttr.getValue();
37+
38+
auto options = mlir::LowerToLLVMOptions(module.getContext());
39+
auto llvmDL = llvm::DataLayout(dataLayoutString);
40+
if (llvmDL.getPointerSizeInBits(0) == 32) {
41+
// FIXME: Should translateDataLayout in the MLIR layer be doing this?
42+
options.overrideIndexBitwidth(32);
43+
}
44+
options.dataLayout = llvmDL;
45+
return options;
46+
}
47+
3148
LLVMTypeConverter::LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA,
3249
bool forceUnifiedTBAATree,
3350
const mlir::DataLayout &dl)
34-
: mlir::LLVMTypeConverter(module.getContext()),
51+
: mlir::LLVMTypeConverter(module.getContext(), MakeLowerOptions(module)),
3552
kindMapping(getKindMapping(module)),
3653
specifics(CodeGenSpecifics::get(
3754
module.getContext(), getTargetTriple(module), getKindMapping(module),

flang/test/Fir/alloc-32.fir

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %flang_fc1 -triple i686 -emit-llvm %s -o - | FileCheck %s
2+
// REQUIRES: x86-registered-target
3+
4+
// This is a check for calling malloc using i32 when on a 32-bit target (only).
5+
// It doesn't contain the comprehensive tests that alloc.fir has, and
6+
// that file should be used to exercise most code paths.
7+
8+
module attributes {
9+
fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.target_triple = "i686"
10+
} {
11+
12+
// CHECK-LABEL: define ptr @allocmem_scalar_nonchar(
13+
// CHECK: call ptr @malloc(i32 4)
14+
func.func @allocmem_scalar_nonchar() -> !fir.heap<i32> {
15+
%1 = fir.allocmem i32
16+
return %1 : !fir.heap<i32>
17+
}
18+
19+
// CHECK-LABEL: define ptr @allocmem_scalar_dynchar(
20+
// CHECK-SAME: i32 %[[len:.*]])
21+
// CHECK: %[[mul1:.*]] = sext i32 %[[len]] to i64
22+
// CHECK: %[[mul2:.*]] = mul i64 1, %[[mul1]]
23+
// CHECK: %[[trunc:.*]] = trunc i64 %[[mul2]] to i32
24+
// CHECK: call ptr @malloc(i32 %[[trunc]])
25+
func.func @allocmem_scalar_dynchar(%l : i32) -> !fir.heap<!fir.char<1,?>> {
26+
%1 = fir.allocmem !fir.char<1,?>(%l : i32)
27+
return %1 : !fir.heap<!fir.char<1,?>>
28+
}
29+
30+
}

flang/test/Fir/alloc.fir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// RUN: %flang_fc1 -emit-llvm %s -o - | FileCheck %s
33

44
// UNSUPPORTED: system-windows
5+
// UNSUPPORTED: target-x86
6+
// UNSUPPORTED: target=sparc-{{.*}}
7+
// UNSUPPORTED: target=sparcel-{{.*}}
58

69
// CHECK-LABEL: define ptr @alloca_scalar_nonchar()
710
// CHECK: alloca i32, i64 1

flang/test/Integration/OpenMP/private-global.f90

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ program bug
3131
! CHECK: %[[FIFTY:.*]] = alloca i32, i64 1, align 4
3232
! CHECK: %[[INTERMEDIATE:.*]] = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
3333
! CHECK: %[[TABLE_BOX_ADDR2:.*]] = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, i64 1, align 8
34-
! CHECK: call void @llvm.memcpy.p0.p0.i32(ptr %[[INTERMEDIATE]], ptr %[[PRIV_BOX_ALLOC]], i32 48, i1 false)
34+
! CHECK: call void @llvm.memcpy.p0.p0.i32(ptr %[[INTERMEDIATE]], ptr %[[PRIV_BOX_ALLOC]], i32 {{4[48]}}, i1 false)
3535
! CHECK: store i32 50, ptr %[[FIFTY]], align 4
3636
! 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
37-
! CHECK: store { ptr, i64, i32, i8, i8, i8, i8 } %[[FIFTY_BOX_VAL]], ptr %[[BOXED_FIFTY]], align 8
38-
! CHECK: call void @llvm.memcpy.p0.p0.i32(ptr %[[TABLE_BOX_ADDR2]], ptr %[[INTERMEDIATE]], i32 48, i1 false)
37+
! CHECK: store { ptr, i64, i32, i8, i8, i8, i8 } %[[FIFTY_BOX_VAL]], ptr %[[BOXED_FIFTY]], align {{[48]}}
38+
! CHECK: call void @llvm.memcpy.p0.p0.i32(ptr %[[TABLE_BOX_ADDR2]], ptr %[[INTERMEDIATE]], i32 {{4[48]}}, i1 false)
3939
! CHECK: call void @_FortranAAssign(ptr %[[TABLE_BOX_ADDR2]], ptr %[[BOXED_FIFTY]], ptr @{{.*}}, i32 9)
40-
! CHECK: call void @llvm.memcpy.p0.p0.i32(ptr %[[TABLE_BOX_ADDR]], ptr %[[PRIV_BOX_ALLOC]], i32 48, i1 false)
41-
! CHECK: %[[PRIV_TABLE:.*]] = call ptr @malloc(i64 40)
40+
! CHECK: call void @llvm.memcpy.p0.p0.i32(ptr %[[TABLE_BOX_ADDR]], ptr %[[PRIV_BOX_ALLOC]], i32 {{4[48]}}, i1 false)
41+
! CHECK: %[[PRIV_TABLE:.*]] = call ptr @malloc(i{{(32)|(64)}} 40)
4242
! ...
4343
! check that we use the private copy of table for table/=50
4444
! CHECK: omp.par.region3:

flang/test/Lower/OpenMP/parallel-reduction-mixed.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ end subroutine proc
3636

3737
!CHECK: [[MALLOC_BB]]:
3838
!CHECK-NOT: omp.par.{{.*}}:
39-
!CHECK: call ptr @malloc(i64 80)
39+
!CHECK: call ptr @malloc(i{{(32)|(64)}} 80)
4040

4141
!CHECK: %[[RED_ARR_0:.*]] = getelementptr inbounds [2 x ptr], ptr %red.array, i64 0, i64 0
4242
!CHECK: store ptr %[[F_priv]], ptr %[[RED_ARR_0:.*]]

flang/test/Lower/forall/character-1.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
! RUN: %flang -emit-llvm -flang-deprecated-no-hlfir -S -mmlir -disable-external-name-interop %s -o - | FileCheck %s
33
! Test from Fortran source through to LLVM IR.
44
! UNSUPPORTED: system-windows
5+
! UNSUPPORTED: target-x86
6+
! UNSUPPORTED: target=sparc-{{.*}}
7+
! UNSUPPORTED: target=sparcel-{{.*}}
58

69
! Assumed size array of assumed length character.
710
program test

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3771,6 +3771,8 @@ OpenMPIRBuilder::createReductions(const LocationDescription &Loc,
37713771

37723772
// Emit a call to the runtime function that orchestrates the reduction.
37733773
// Declare the reduction function in the process.
3774+
Type *IndexTy = Builder.getIndexTy(
3775+
M.getDataLayout(), M.getDataLayout().getDefaultGlobalsAddressSpace());
37743776
Function *Func = Builder.GetInsertBlock()->getParent();
37753777
Module *Module = Func->getParent();
37763778
uint32_t SrcLocStrSize;
@@ -3786,7 +3788,7 @@ OpenMPIRBuilder::createReductions(const LocationDescription &Loc,
37863788
Constant *NumVariables = Builder.getInt32(NumReductions);
37873789
const DataLayout &DL = Module->getDataLayout();
37883790
unsigned RedArrayByteSize = DL.getTypeStoreSize(RedArrayTy);
3789-
Constant *RedArraySize = Builder.getInt64(RedArrayByteSize);
3791+
Constant *RedArraySize = ConstantInt::get(IndexTy, RedArrayByteSize);
37903792
Function *ReductionFunc = getFreshReductionFunc(*Module);
37913793
Value *Lock = getOMPCriticalRegionLock(".reduction");
37923794
Function *ReduceFunc = getOrCreateRuntimeFunctionPtr(

0 commit comments

Comments
 (0)