Skip to content

Commit 9992668

Browse files
authored
[flang][cuda] Add runtime check for passing device arrays (#144003)
1 parent 07dad4e commit 9992668

File tree

8 files changed

+73
-0
lines changed

8 files changed

+73
-0
lines changed

flang-rt/lib/cuda/descriptor.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ void RTDEF(CUFSyncGlobalDescriptor)(
5454
((Descriptor *)devAddr, (Descriptor *)hostPtr, sourceFile, sourceLine);
5555
}
5656

57+
void RTDEF(CUFDescriptorCheckSection)(
58+
const Descriptor *desc, const char *sourceFile, int sourceLine) {
59+
if (desc && !desc->IsContiguous()) {
60+
Terminator terminator{sourceFile, sourceLine};
61+
terminator.Crash("device array section argument is not contiguous");
62+
}
63+
}
64+
5765
RT_EXT_API_GROUP_END
5866
}
5967
} // namespace Fortran::runtime::cuda

flang/include/flang/Lower/LoweringOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,8 @@ ENUM_LOWERINGOPT(StackRepackArrays, unsigned, 1, 0)
6363
/// in the leading dimension.
6464
ENUM_LOWERINGOPT(RepackArraysWhole, unsigned, 1, 0)
6565

66+
/// If true, CUDA Fortran runtime check is inserted.
67+
ENUM_LOWERINGOPT(CUDARuntimeCheck, unsigned, 1, 0)
68+
6669
#undef LOWERINGOPT
6770
#undef ENUM_LOWERINGOPT

flang/include/flang/Optimizer/Builder/Runtime/CUDA/Descriptor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ namespace fir::runtime::cuda {
2626
void genSyncGlobalDescriptor(fir::FirOpBuilder &builder, mlir::Location loc,
2727
mlir::Value hostPtr);
2828

29+
/// Generate runtime call to check the section of a descriptor and raise an
30+
/// error if it is not contiguous.
31+
void genDescriptorCheckSection(fir::FirOpBuilder &builder, mlir::Location loc,
32+
mlir::Value desc);
33+
2934
} // namespace fir::runtime::cuda
3035

3136
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_CUDA_DESCRIPTOR_H_

flang/include/flang/Runtime/CUDA/descriptor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ void RTDECL(CUFDescriptorSync)(Descriptor *dst, const Descriptor *src,
3737
void RTDECL(CUFSyncGlobalDescriptor)(
3838
void *hostPtr, const char *sourceFile = nullptr, int sourceLine = 0);
3939

40+
/// Check descriptor passed to a kernel.
41+
void RTDECL(CUFDescriptorCheckSection)(
42+
const Descriptor *, const char *sourceFile = nullptr, int sourceLine = 0);
43+
4044
} // extern "C"
4145

4246
} // namespace Fortran::runtime::cuda

flang/lib/Lower/ConvertCall.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "flang/Optimizer/Builder/IntrinsicCall.h"
2727
#include "flang/Optimizer/Builder/LowLevelIntrinsics.h"
2828
#include "flang/Optimizer/Builder/MutableBox.h"
29+
#include "flang/Optimizer/Builder/Runtime/CUDA/Descriptor.h"
2930
#include "flang/Optimizer/Builder/Runtime/Derived.h"
3031
#include "flang/Optimizer/Builder/Todo.h"
3132
#include "flang/Optimizer/Dialect/CUF/CUFOps.h"
@@ -543,6 +544,19 @@ Fortran::lower::genCallOpAndResult(
543544
fir::FortranProcedureFlagsEnumAttr procAttrs =
544545
caller.getProcedureAttrs(builder.getContext());
545546

547+
if (converter.getLoweringOptions().getCUDARuntimeCheck()) {
548+
if (caller.getCallDescription().chevrons().empty()) {
549+
for (auto [oper, arg] :
550+
llvm::zip(operands, caller.getPassedArguments())) {
551+
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(oper.getType())) {
552+
const Fortran::semantics::Symbol *sym = caller.getDummySymbol(arg);
553+
if (sym && Fortran::evaluate::IsCUDADeviceSymbol(*sym))
554+
fir::runtime::cuda::genDescriptorCheckSection(builder, loc, oper);
555+
}
556+
}
557+
}
558+
}
559+
546560
if (!caller.getCallDescription().chevrons().empty()) {
547561
// A call to a CUDA kernel with the chevron syntax.
548562

flang/lib/Optimizer/Builder/Runtime/CUDA/Descriptor.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,18 @@ void fir::runtime::cuda::genSyncGlobalDescriptor(fir::FirOpBuilder &builder,
3232
builder, loc, fTy, hostPtr, sourceFile, sourceLine)};
3333
builder.create<fir::CallOp>(loc, callee, args);
3434
}
35+
36+
void fir::runtime::cuda::genDescriptorCheckSection(fir::FirOpBuilder &builder,
37+
mlir::Location loc,
38+
mlir::Value desc) {
39+
mlir::func::FuncOp func =
40+
fir::runtime::getRuntimeFunc<mkRTKey(CUFDescriptorCheckSection)>(loc,
41+
builder);
42+
auto fTy = func.getFunctionType();
43+
mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
44+
mlir::Value sourceLine =
45+
fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
46+
llvm::SmallVector<mlir::Value> args{fir::runtime::createArguments(
47+
builder, loc, fTy, desc, sourceFile, sourceLine)};
48+
builder.create<fir::CallOp>(loc, func, args);
49+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
2+
3+
! Check insertion of runtime checks
4+
5+
interface
6+
subroutine foo(a)
7+
real, device, dimension(:,:) :: a
8+
end subroutine
9+
end interface
10+
11+
real, device, allocatable, dimension(:,:) :: a
12+
allocate(a(10,10))
13+
call foo(a(1:10,1:10:2))
14+
end
15+
16+
subroutine foo(a)
17+
real, device, dimension(:,:) :: a
18+
end subroutine
19+
20+
! CHECK-LABEL: func.func @_QQmain()
21+
! CHECK: fir.call @_FortranACUFDescriptorCheckSection
22+
! CHECK: fir.call @_QPfoo

flang/tools/bbc/bbc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
434434
loweringOptions.setStackRepackArrays(stackRepackArrays);
435435
loweringOptions.setRepackArrays(repackArrays);
436436
loweringOptions.setRepackArraysWhole(repackArraysWhole);
437+
if (enableCUDA)
438+
loweringOptions.setCUDARuntimeCheck(true);
437439
std::vector<Fortran::lower::EnvironmentDefault> envDefaults = {};
438440
Fortran::frontend::TargetOptions targetOpts;
439441
Fortran::frontend::CodeGenOptions cgOpts;

0 commit comments

Comments
 (0)