Skip to content

Commit a9c73f6

Browse files
authored
[flang][cuda] Add fir.cuda_alloc/fir.cuda_free operations (llvm#90525)
This patch introduces fir.cuda_alloc/fir.cuda_free. These operations will be used instead of fir.alloca for local CUDA device, managed and unified variables.
1 parent f815d1f commit a9c73f6

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

flang/include/flang/Optimizer/Dialect/FIROps.td

+54
Original file line numberDiff line numberDiff line change
@@ -3247,4 +3247,58 @@ def fir_CUDADeallocateOp : fir_Op<"cuda_deallocate",
32473247
let hasVerifier = 1;
32483248
}
32493249

3250+
def fir_CUDAAllocOp : fir_Op<"cuda_alloc", [AttrSizedOperandSegments,
3251+
MemoryEffects<[MemAlloc]>]> {
3252+
let summary = "Allocate an object on device";
3253+
3254+
let description = [{
3255+
This is a drop in replacement for fir.alloca and fir.allocmem for device
3256+
object. Any device, managed or unified object declared in an host
3257+
subprogram needs to be allocated in the device memory through runtime calls.
3258+
The fir.cuda_alloc is an abstraction to the runtime calls and works together
3259+
with fir.cuda_free.
3260+
}];
3261+
3262+
let arguments = (ins
3263+
TypeAttr:$in_type,
3264+
OptionalAttr<StrAttr>:$uniq_name,
3265+
OptionalAttr<StrAttr>:$bindc_name,
3266+
Variadic<AnyIntegerType>:$typeparams,
3267+
Variadic<AnyIntegerType>:$shape,
3268+
fir_CUDADataAttributeAttr:$cuda_attr
3269+
);
3270+
3271+
let results = (outs fir_ReferenceType:$ptr);
3272+
3273+
let assemblyFormat = [{
3274+
$in_type (`(` $typeparams^ `:` type($typeparams) `)`)?
3275+
(`,` $shape^ `:` type($shape) )? attr-dict `->` qualified(type($ptr))
3276+
}];
3277+
3278+
let builders = [
3279+
OpBuilder<(ins "mlir::Type":$inType, "llvm::StringRef":$uniqName,
3280+
"llvm::StringRef":$bindcName,
3281+
"fir::CUDADataAttributeAttr":$cudaAttr,
3282+
CArg<"mlir::ValueRange", "{}">:$typeparams,
3283+
CArg<"mlir::ValueRange", "{}">:$shape,
3284+
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes)>];
3285+
}
3286+
3287+
def fir_CUDAFreeOp : fir_Op<"cuda_free", [MemoryEffects<[MemFree]>]> {
3288+
let summary = "Free a device allocated object";
3289+
3290+
let description = [{
3291+
The fir.cuda_free operation frees the memory allocated by fir.cuda_alloc.
3292+
This is used for non-allocatable device, managed and unified device
3293+
variables declare in host subprogram.
3294+
}];
3295+
3296+
let arguments = (ins
3297+
Arg<AnyReferenceLike, "", [MemFree]>:$devptr,
3298+
fir_CUDADataAttributeAttr:$cuda_attr
3299+
);
3300+
3301+
let assemblyFormat = "$devptr `:` qualified(type($devptr)) attr-dict";
3302+
}
3303+
32503304
#endif

flang/test/Fir/cuf.mlir

+12
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,15 @@ func.func @_QPsub1() {
7474

7575
// CHECK: fir.cuda_allocate %{{.*}} : !fir.ref<!fir.box<none>> errmsg(%{{.*}} : !fir.box<none>) {cuda_attr = #fir.cuda<device>, hasStat} -> i32
7676
// CHECK: fir.cuda_deallocate %{{.*}} : !fir.ref<!fir.box<none>> errmsg(%{{.*}} : !fir.box<none>) {cuda_attr = #fir.cuda<device>, hasStat} -> i32
77+
78+
// -----
79+
80+
func.func @_QPsub1() {
81+
%0 = fir.cuda_alloc f32 {bindc_name = "r", cuda_attr = #fir.cuda<device>, uniq_name = "_QFsub1Er"} -> !fir.ref<f32>
82+
fir.cuda_free %0 : !fir.ref<f32> {cuda_attr = #fir.cuda<device>}
83+
return
84+
}
85+
86+
// CHECK: fir.cuda_alloc
87+
// CHECK: fir.cuda_free
88+

0 commit comments

Comments
 (0)