Skip to content

Commit 3040c8c

Browse files
committed
add cuf allc/allocate mem alloc effect
1 parent e5e74e9 commit 3040c8c

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

flang/include/flang/Optimizer/Dialect/CUF/CUFOps.td

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ include "mlir/IR/BuiltinAttributes.td"
2626
class cuf_Op<string mnemonic, list<Trait> traits>
2727
: Op<CUFDialect, mnemonic, traits>;
2828

29-
def cuf_AllocOp : cuf_Op<"alloc", [AttrSizedOperandSegments,
30-
MemoryEffects<[MemAlloc]>]> {
29+
def cuf_AllocOp : cuf_Op<"alloc", [AttrSizedOperandSegments]> {
3130
let summary = "Allocate an object on device";
3231

3332
let description = [{
@@ -47,7 +46,9 @@ def cuf_AllocOp : cuf_Op<"alloc", [AttrSizedOperandSegments,
4746
cuf_DataAttributeAttr:$data_attr
4847
);
4948

50-
let results = (outs fir_ReferenceType:$ptr);
49+
// Value-scoped Allocate on the returned reference
50+
let results = (outs Res<fir_ReferenceType, "",
51+
[MemAlloc<DefaultResource>]>:$ptr);
5152

5253
let assemblyFormat = [{
5354
$in_type (`(` $typeparams^ `:` type($typeparams) `)`)?
@@ -84,8 +85,7 @@ def cuf_FreeOp : cuf_Op<"free", [MemoryEffects<[MemFree]>]> {
8485
let hasVerifier = 1;
8586
}
8687

87-
def cuf_AllocateOp : cuf_Op<"allocate", [AttrSizedOperandSegments,
88-
MemoryEffects<[MemAlloc<DefaultResource>]>]> {
88+
def cuf_AllocateOp : cuf_Op<"allocate", [AttrSizedOperandSegments]> {
8989
let summary = "Perform the device allocation of data of an allocatable";
9090

9191
let description = [{
@@ -94,7 +94,9 @@ def cuf_AllocateOp : cuf_Op<"allocate", [AttrSizedOperandSegments,
9494
is initialized before with the standard flang runtime calls.
9595
}];
9696

97-
let arguments = (ins Arg<fir_ReferenceType, "", [MemRead, MemWrite]>:$box,
97+
// Value-scoped Allocate on the descriptor being allocated
98+
let arguments = (ins Arg<fir_ReferenceType, "",
99+
[MemAlloc<DefaultResource>, MemRead, MemWrite]>:$box,
98100
Arg<Optional<AnyRefOrBoxType>, "", [MemWrite]>:$errmsg,
99101
Optional<fir_ReferenceType>:$stream,
100102
Arg<Optional<AnyRefOrBoxType>, "", [MemWrite]>:$pinned,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// REQUIRES: asserts
2+
// RUN: fir-opt %s -pass-pipeline='builtin.module(func.func(test-fir-alias-analysis))' -debug-only=fir-alias-analysis --mlir-disable-threading 2>&1 | FileCheck %s
3+
4+
// Verify that a CUF allocation is recognized as SourceKind::Allocate by
5+
// fir::AliasAnalysis::getSource.
6+
7+
module {
8+
func.func @_QQmain() attributes {fir.bindc_name = "TEST"} {
9+
// Allocate two independent device arrays and tag the results; with
10+
// value-scoped MemAlloc handling in AA, these should be classified as
11+
// Allocate and not alias.
12+
%a = cuf.alloc !fir.box<!fir.heap<!fir.array<?xf32>>> {bindc_name = "a1", data_attr = #cuf.cuda<device>, uniq_name = "_QFEa1", test.ptr = "cuf_alloc_a"} -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
13+
%b = cuf.alloc !fir.box<!fir.heap<!fir.array<?xf32>>> {bindc_name = "a2", data_attr = #cuf.cuda<device>, uniq_name = "_QFEa2", test.ptr = "cuf_alloc_b"} -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
14+
return
15+
}
16+
}
17+
18+
// CHECK-LABEL: Testing : "_QQmain"
19+
// Distinct allocations should not alias.
20+
// CHECK: cuf_alloc_a#0 <-> cuf_alloc_b#0: NoAlias
21+
22+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// REQUIRES: asserts
2+
// RUN: fir-opt %s -pass-pipeline='builtin.module(func.func(test-fir-alias-analysis))' -debug-only=fir-alias-analysis --mlir-disable-threading 2>&1 | FileCheck %s
3+
4+
// Verify that CUF allocation via cuf.allocate is recognized as
5+
// SourceKind::Allocate by fir::AliasAnalysis::getSource on the box value.
6+
7+
module {
8+
func.func @_QQmain() attributes {fir.bindc_name = "TEST"} {
9+
// Create two independent device boxes and tag their refs.
10+
%a = cuf.alloc !fir.box<!fir.heap<!fir.array<?xf32>>> {bindc_name = "a1", data_attr = #cuf.cuda<device>, uniq_name = "_QFEa1", test.ptr = "cuf_allocate_a"} -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
11+
%b = cuf.alloc !fir.box<!fir.heap<!fir.array<?xf32>>> {bindc_name = "a2", data_attr = #cuf.cuda<device>, uniq_name = "_QFEa2", test.ptr = "cuf_allocate_b"} -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
12+
// Allocate device data for each descriptor; AA should classify the box
13+
// values (tagged above) as Allocate sources and not alias.
14+
%sa = cuf.allocate %a : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>> {data_attr = #cuf.cuda<device>} -> i32
15+
%sb = cuf.allocate %b : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>> {data_attr = #cuf.cuda<device>} -> i32
16+
return
17+
}
18+
}
19+
20+
// CHECK-LABEL: Testing : "_QQmain"
21+
// Distinct allocations via cuf.allocate should not alias.
22+
// CHECK: cuf_allocate_a#0 <-> cuf_allocate_b#0: NoAlias
23+
24+

0 commit comments

Comments
 (0)