Skip to content

Commit 5e1f87e

Browse files
authored
[flang][cuda] Correctly allocate memory for descriptor load (#120164)
CodeGen will allocate memory for a new descriptor on descriptor loads. CUDA Fortran local descriptor are allocated in managed memory by the runtime. The newly allocated storage for cuda descriptor must also be allocated through the runtime.
1 parent fba3e06 commit 5e1f87e

File tree

2 files changed

+133
-2
lines changed

2 files changed

+133
-2
lines changed

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "flang/Optimizer/Support/InternalNames.h"
2424
#include "flang/Optimizer/Support/TypeCode.h"
2525
#include "flang/Optimizer/Support/Utils.h"
26+
#include "flang/Runtime/CUDA/descriptor.h"
2627
#include "flang/Runtime/allocator-registry-consts.h"
2728
#include "flang/Runtime/descriptor-consts.h"
2829
#include "flang/Semantics/runtime-type-info.h"
@@ -2970,6 +2971,93 @@ struct GlobalOpConversion : public fir::FIROpConversion<fir::GlobalOp> {
29702971
}
29712972
};
29722973

2974+
static mlir::Value genSourceFile(mlir::Location loc, mlir::ModuleOp mod,
2975+
mlir::ConversionPatternRewriter &rewriter) {
2976+
auto ptrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
2977+
if (auto flc = mlir::dyn_cast<mlir::FileLineColLoc>(loc)) {
2978+
auto fn = flc.getFilename().str() + '\0';
2979+
std::string globalName = fir::factory::uniqueCGIdent("cl", fn);
2980+
2981+
if (auto g = mod.lookupSymbol<fir::GlobalOp>(globalName)) {
2982+
return rewriter.create<mlir::LLVM::AddressOfOp>(loc, ptrTy, g.getName());
2983+
} else if (auto g = mod.lookupSymbol<mlir::LLVM::GlobalOp>(globalName)) {
2984+
return rewriter.create<mlir::LLVM::AddressOfOp>(loc, ptrTy, g.getName());
2985+
}
2986+
2987+
auto crtInsPt = rewriter.saveInsertionPoint();
2988+
rewriter.setInsertionPoint(mod.getBody(), mod.getBody()->end());
2989+
auto arrayTy = mlir::LLVM::LLVMArrayType::get(
2990+
mlir::IntegerType::get(rewriter.getContext(), 8), fn.size());
2991+
mlir::LLVM::GlobalOp globalOp = rewriter.create<mlir::LLVM::GlobalOp>(
2992+
loc, arrayTy, /*constant=*/true, mlir::LLVM::Linkage::Linkonce,
2993+
globalName, mlir::Attribute());
2994+
2995+
mlir::Region &region = globalOp.getInitializerRegion();
2996+
mlir::Block *block = rewriter.createBlock(&region);
2997+
rewriter.setInsertionPoint(block, block->begin());
2998+
mlir::Value constValue = rewriter.create<mlir::LLVM::ConstantOp>(
2999+
loc, arrayTy, rewriter.getStringAttr(fn));
3000+
rewriter.create<mlir::LLVM::ReturnOp>(loc, constValue);
3001+
rewriter.restoreInsertionPoint(crtInsPt);
3002+
return rewriter.create<mlir::LLVM::AddressOfOp>(loc, ptrTy,
3003+
globalOp.getName());
3004+
}
3005+
return rewriter.create<mlir::LLVM::ZeroOp>(loc, ptrTy);
3006+
}
3007+
3008+
static mlir::Value genSourceLine(mlir::Location loc,
3009+
mlir::ConversionPatternRewriter &rewriter) {
3010+
if (auto flc = mlir::dyn_cast<mlir::FileLineColLoc>(loc))
3011+
return rewriter.create<mlir::LLVM::ConstantOp>(loc, rewriter.getI32Type(),
3012+
flc.getLine());
3013+
return rewriter.create<mlir::LLVM::ConstantOp>(loc, rewriter.getI32Type(), 0);
3014+
}
3015+
3016+
static mlir::Value
3017+
genCUFAllocDescriptor(mlir::Location loc,
3018+
mlir::ConversionPatternRewriter &rewriter,
3019+
mlir::ModuleOp mod, fir::BaseBoxType boxTy,
3020+
const fir::LLVMTypeConverter &typeConverter) {
3021+
std::optional<mlir::DataLayout> dl =
3022+
fir::support::getOrSetDataLayout(mod, /*allowDefaultLayout=*/true);
3023+
if (!dl)
3024+
mlir::emitError(mod.getLoc(),
3025+
"module operation must carry a data layout attribute "
3026+
"to generate llvm IR from FIR");
3027+
3028+
mlir::Value sourceFile = genSourceFile(loc, mod, rewriter);
3029+
mlir::Value sourceLine = genSourceLine(loc, rewriter);
3030+
3031+
mlir::MLIRContext *ctx = mod.getContext();
3032+
3033+
mlir::LLVM::LLVMPointerType llvmPointerType =
3034+
mlir::LLVM::LLVMPointerType::get(ctx);
3035+
mlir::Type llvmInt32Type = mlir::IntegerType::get(ctx, 32);
3036+
mlir::Type llvmIntPtrType =
3037+
mlir::IntegerType::get(ctx, typeConverter.getPointerBitwidth(0));
3038+
auto fctTy = mlir::LLVM::LLVMFunctionType::get(
3039+
llvmPointerType, {llvmIntPtrType, llvmPointerType, llvmInt32Type});
3040+
3041+
auto llvmFunc = mod.lookupSymbol<mlir::LLVM::LLVMFuncOp>(
3042+
RTNAME_STRING(CUFAllocDesciptor));
3043+
auto funcFunc =
3044+
mod.lookupSymbol<mlir::func::FuncOp>(RTNAME_STRING(CUFAllocDesciptor));
3045+
if (!llvmFunc && !funcFunc)
3046+
mlir::OpBuilder::atBlockEnd(mod.getBody())
3047+
.create<mlir::LLVM::LLVMFuncOp>(loc, RTNAME_STRING(CUFAllocDesciptor),
3048+
fctTy);
3049+
3050+
mlir::Type structTy = typeConverter.convertBoxTypeAsStruct(boxTy);
3051+
std::size_t boxSize = dl->getTypeSizeInBits(structTy) / 8;
3052+
mlir::Value sizeInBytes =
3053+
genConstantIndex(loc, llvmIntPtrType, rewriter, boxSize);
3054+
llvm::SmallVector args = {sizeInBytes, sourceFile, sourceLine};
3055+
return rewriter
3056+
.create<mlir::LLVM::CallOp>(loc, fctTy, RTNAME_STRING(CUFAllocDesciptor),
3057+
args)
3058+
.getResult();
3059+
}
3060+
29733061
/// `fir.load` --> `llvm.load`
29743062
struct LoadOpConversion : public fir::FIROpConversion<fir::LoadOp> {
29753063
using FIROpConversion::FIROpConversion;
@@ -2986,9 +3074,23 @@ struct LoadOpConversion : public fir::FIROpConversion<fir::LoadOp> {
29863074
// loading a fir.ref<fir.box> is implemented as taking a snapshot of the
29873075
// descriptor value into a new descriptor temp.
29883076
auto inputBoxStorage = adaptor.getOperands()[0];
3077+
mlir::Value newBoxStorage;
29893078
mlir::Location loc = load.getLoc();
2990-
auto newBoxStorage =
2991-
genAllocaAndAddrCastWithType(loc, llvmLoadTy, defaultAlign, rewriter);
3079+
if (auto callOp = mlir::dyn_cast_or_null<mlir::LLVM::CallOp>(
3080+
inputBoxStorage.getDefiningOp())) {
3081+
if (callOp.getCallee() &&
3082+
(*callOp.getCallee())
3083+
.starts_with(RTNAME_STRING(CUFAllocDesciptor))) {
3084+
// CUDA Fortran local descriptor are allocated in managed memory. So
3085+
// new storage must be allocated the same way.
3086+
auto mod = load->getParentOfType<mlir::ModuleOp>();
3087+
newBoxStorage =
3088+
genCUFAllocDescriptor(loc, rewriter, mod, boxTy, lowerTy());
3089+
}
3090+
}
3091+
if (!newBoxStorage)
3092+
newBoxStorage = genAllocaAndAddrCastWithType(loc, llvmLoadTy,
3093+
defaultAlign, rewriter);
29923094

29933095
TypePair boxTypePair{boxTy, llvmLoadTy};
29943096
mlir::Value boxSize =
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: fir-opt --split-input-file --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" %s | FileCheck %s
2+
3+
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<f80, dense<128> : vector<2xi64>>, #dlti.dl_entry<i128, dense<128> : vector<2xi64>>, #dlti.dl_entry<i64, dense<64> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr<272>, dense<64> : vector<4xi64>>, #dlti.dl_entry<!llvm.ptr<271>, dense<32> : vector<4xi64>>, #dlti.dl_entry<!llvm.ptr<270>, dense<32> : vector<4xi64>>, #dlti.dl_entry<f128, dense<128> : vector<2xi64>>, #dlti.dl_entry<f64, dense<64> : vector<2xi64>>, #dlti.dl_entry<f16, dense<16> : vector<2xi64>>, #dlti.dl_entry<i32, dense<32> : vector<2xi64>>, #dlti.dl_entry<i16, dense<16> : vector<2xi64>>, #dlti.dl_entry<i8, dense<8> : vector<2xi64>>, #dlti.dl_entry<i1, dense<8> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr, dense<64> : vector<4xi64>>, #dlti.dl_entry<"dlti.endianness", "little">, #dlti.dl_entry<"dlti.stack_alignment", 128 : i64>>} {
4+
5+
func.func @_QQmain() attributes {fir.bindc_name = "cufkernel_global"} {
6+
%c0 = arith.constant 0 : index
7+
%0 = fir.address_of(@_QQclX3C737464696E3E00) : !fir.ref<!fir.char<1,8>>
8+
%c4_i32 = arith.constant 4 : i32
9+
%c48 = arith.constant 48 : index
10+
%1 = fir.convert %c48 : (index) -> i64
11+
%2 = fir.convert %0 : (!fir.ref<!fir.char<1,8>>) -> !fir.ref<i8>
12+
%3 = fir.call @_FortranACUFAllocDesciptor(%1, %2, %c4_i32) : (i64, !fir.ref<i8>, i32) -> !fir.ref<!fir.box<none>>
13+
%4 = fir.convert %3 : (!fir.ref<!fir.box<none>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
14+
%5 = fir.zero_bits !fir.heap<!fir.array<?xi32>>
15+
%6 = fircg.ext_embox %5(%c0) {allocator_idx = 2 : i32} : (!fir.heap<!fir.array<?xi32>>, index) -> !fir.box<!fir.heap<!fir.array<?xi32>>>
16+
fir.store %6 to %4 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
17+
%8 = fir.load %3 : !fir.ref<!fir.box<none>>
18+
return
19+
}
20+
21+
// CHECK-LABEL: llvm.func @_QQmain()
22+
// CHECK-COUNT-2: llvm.call @_FortranACUFAllocDesciptor
23+
24+
fir.global linkonce @_QQclX3C737464696E3E00 constant : !fir.char<1,8> {
25+
%0 = fir.string_lit "<stdin>\00"(8) : !fir.char<1,8>
26+
fir.has_value %0 : !fir.char<1,8>
27+
}
28+
func.func private @_FortranACUFAllocDesciptor(i64, !fir.ref<i8>, i32) -> !fir.ref<!fir.box<none>> attributes {fir.runtime}
29+
}

0 commit comments

Comments
 (0)