Skip to content

[flang][cuda] Add cuf.shared_memory operation #131392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions flang/include/flang/Optimizer/Dialect/CUF/CUFOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,33 @@ def cuf_DeviceAddressOp : cuf_Op<"device_address", []> {
let results = (outs fir_ReferenceType:$addr);
}

def cuf_SharedMemoryOp
: cuf_Op<"shared_memory", [AttrSizedOperandSegments, Pure]> {
let summary = "Get the pointer to the kernel shared memory";

let description = [{
Return the pointer in the shared memory relative to the specified offset.
}];

let arguments = (ins TypeAttr:$in_type, OptionalAttr<StrAttr>:$uniq_name,
OptionalAttr<StrAttr>:$bindc_name, Variadic<AnyIntegerType>:$typeparams,
Variadic<AnyIntegerType>:$shape,
OptionalAttr<I32Attr>:$offset // offset in bytes from the shared memory
// base address.
);

let results = (outs fir_ReferenceType:$ptr);

let assemblyFormat = [{
$in_type (`(` $typeparams^ `:` type($typeparams) `)`)?
(`,` $shape^ `:` type($shape) )? attr-dict `->` qualified(type($ptr))
}];

let builders = [OpBuilder<(ins "mlir::Type":$inType,
"llvm::StringRef":$uniqName, "llvm::StringRef":$bindcName,
CArg<"mlir::ValueRange", "{}">:$typeparams,
CArg<"mlir::ValueRange", "{}">:$shape,
CArg<"llvm::ArrayRef<mlir::NamedAttribute>", "{}">:$attributes)>];
}

#endif // FORTRAN_DIALECT_CUF_CUF_OPS
19 changes: 19 additions & 0 deletions flang/lib/Optimizer/Dialect/CUF/CUFOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,25 @@ mlir::LogicalResult cuf::RegisterKernelOp::verify() {
return emitOpError("device function not found");
}

//===----------------------------------------------------------------------===//
// SharedMemoryOp
//===----------------------------------------------------------------------===//

void cuf::SharedMemoryOp::build(
mlir::OpBuilder &builder, mlir::OperationState &result, mlir::Type inType,
llvm::StringRef uniqName, llvm::StringRef bindcName,
mlir::ValueRange typeparams, mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
mlir::StringAttr nameAttr =
uniqName.empty() ? mlir::StringAttr{} : builder.getStringAttr(uniqName);
mlir::StringAttr bindcAttr =
bindcName.empty() ? mlir::StringAttr{} : builder.getStringAttr(bindcName);
build(builder, result, wrapAllocaResultType(inType),
mlir::TypeAttr::get(inType), nameAttr, bindcAttr, typeparams, shape,
mlir::IntegerAttr{});
result.addAttributes(attributes);
}

// Tablegen operators

#define GET_OP_CLASSES
Expand Down
27 changes: 27 additions & 0 deletions flang/test/Fir/cuf.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,30 @@ func.func @_QPsub1() {
// CHECK: cuf.alloc
// CHECK: cuf.free

// -----

gpu.module @cuda_device_mod {
gpu.func @_QPdynshared() kernel {
%c-1 = arith.constant -1 : index
%6 = cuf.shared_memory !fir.array<?xf32>, %c-1 : index {bindc_name = "r", uniq_name = "_QFdynsharedEr"} -> !fir.ref<!fir.array<?xf32>>
%7 = fir.shape %c-1 : (index) -> !fir.shape<1>
%8 = fir.declare %6(%7) {data_attr = #cuf.cuda<shared>, uniq_name = "_QFdynsharedEr"} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>) -> !fir.ref<!fir.array<?xf32>>
gpu.return
}
}

// CHECK: cuf.shared_memory

// -----

gpu.module @cuda_device_mod {
gpu.func @_QPshared_static() attributes {cuf.proc_attr = #cuf.cuda_proc<global>} {
%0 = cuf.shared_memory i32 {bindc_name = "a", uniq_name = "_QFshared_staticEa"} -> !fir.ref<i32>
%1 = fir.declare %0 {data_attr = #cuf.cuda<shared>, uniq_name = "_QFshared_staticEa"} : (!fir.ref<i32>) -> !fir.ref<i32>
%2 = cuf.shared_memory i32 {bindc_name = "b", uniq_name = "_QFshared_staticEb"} -> !fir.ref<i32>
%3 = fir.declare %2 {data_attr = #cuf.cuda<shared>, uniq_name = "_QFshared_staticEb"} : (!fir.ref<i32>) -> !fir.ref<i32>
gpu.return
}
}

// CHECK-COUNT-2: cuf.shared_memory
Loading