Skip to content

[flang][cuda] Lower launch_bounds values #81537

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 2 commits into from
Feb 13, 2024
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
12 changes: 12 additions & 0 deletions flang/include/flang/Optimizer/Dialect/FIRAttr.td
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,16 @@ def fir_CUDAProcAttributeAttr :
let assemblyFormat = [{ ```<` $value `>` }];
}

def fir_CUDALaunchBoundsAttr : fir_Attr<"CUDALaunchBounds"> {
let mnemonic = "launch_bounds";

let parameters = (ins
"mlir::IntegerAttr":$maxTPB,
"mlir::IntegerAttr":$minBPM,
OptionalParameter<"mlir::IntegerAttr">:$upperBoundClusterSize
);

let assemblyFormat = "`<` struct(params) `>`";
}

#endif // FIR_DIALECT_FIR_ATTRS
5 changes: 5 additions & 0 deletions flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ static constexpr llvm::StringRef getTargetAttrName() { return "fir.target"; }
/// Attribute to mark Fortran entities with the CUDA attribute.
static constexpr llvm::StringRef getCUDAAttrName() { return "fir.cuda_attr"; }

/// Attribute to carry CUDA launch_bounds values.
static constexpr llvm::StringRef getCUDALaunchBoundsAttrName() {
return "fir.cuda_launch_bounds";
}

/// Attribute to mark that a function argument is a character dummy procedure.
/// Character dummy procedure have special ABI constraints.
static constexpr llvm::StringRef getCharacterProcedureDummyAttrName() {
Expand Down
45 changes: 39 additions & 6 deletions flang/lib/Lower/CallInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,43 @@ static void addSymbolAttribute(mlir::func::FuncOp func,
mlir::StringAttr::get(&mlirContext, name));
}

static void
setCUDAAttributes(mlir::func::FuncOp func,
const Fortran::semantics::Symbol *sym,
std::optional<Fortran::evaluate::characteristics::Procedure>
characteristic) {
if (characteristic && characteristic->cudaSubprogramAttrs) {
func.getOperation()->setAttr(
fir::getCUDAAttrName(),
fir::getCUDAProcAttribute(func.getContext(),
*characteristic->cudaSubprogramAttrs));
}

if (sym) {
if (auto details =
sym->GetUltimate()
.detailsIf<Fortran::semantics::SubprogramDetails>()) {
if (!details->cudaLaunchBounds().empty()) {
assert(details->cudaLaunchBounds().size() >= 2 &&
"expect at least 2 values");
mlir::Type i64Ty = mlir::IntegerType::get(func.getContext(), 64);
auto maxTPBAttr =
mlir::IntegerAttr::get(i64Ty, details->cudaLaunchBounds()[0]);
auto minBPMAttr =
mlir::IntegerAttr::get(i64Ty, details->cudaLaunchBounds()[1]);
mlir::IntegerAttr ubAttr;
if (details->cudaLaunchBounds().size() > 2)
ubAttr =
mlir::IntegerAttr::get(i64Ty, details->cudaLaunchBounds()[2]);
func.getOperation()->setAttr(
fir::getCUDALaunchBoundsAttrName(),
fir::CUDALaunchBoundsAttr::get(func.getContext(), maxTPBAttr,
minBPMAttr, ubAttr));
}
}
}
}

/// Declare drives the different actions to be performed while analyzing the
/// signature and building/finding the mlir::func::FuncOp.
template <typename T>
Expand Down Expand Up @@ -559,12 +596,8 @@ void Fortran::lower::CallInterface<T>::declare() {
if (!placeHolder.value().attributes.empty())
func.setArgAttrs(placeHolder.index(), placeHolder.value().attributes);
side().setFuncAttrs(func);
}
if (characteristic && characteristic->cudaSubprogramAttrs) {
func.getOperation()->setAttr(
fir::getCUDAAttrName(),
fir::getCUDAProcAttribute(func.getContext(),
*characteristic->cudaSubprogramAttrs));

setCUDAAttributes(func, side().getProcedureSymbol(), characteristic);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion flang/lib/Optimizer/Dialect/FIRAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,6 @@ void fir::printFirAttribute(FIROpsDialect *dialect, mlir::Attribute attr,
void FIROpsDialect::registerAttributes() {
addAttributes<ClosedIntervalAttr, ExactTypeAttr, FortranVariableFlagsAttr,
LowerBoundAttr, PointIntervalAttr, RealAttr, SubclassAttr,
UpperBoundAttr, CUDADataAttributeAttr, CUDAProcAttributeAttr>();
UpperBoundAttr, CUDADataAttributeAttr, CUDAProcAttributeAttr,
CUDALaunchBoundsAttr>();
}
6 changes: 6 additions & 0 deletions flang/test/Lower/CUDA/cuda-proc-attribute.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ attributes(host) attributes(device) integer function fct_host_device; end

attributes(device) attributes(host) integer function fct_device_host; end
! CHECK: func.func @_QPfct_device_host() -> i32 attributes {fir.cuda_attr = #fir.cuda_proc<host_device>}

attributes(global) launch_bounds(1, 2) subroutine sub_lbounds1(); end
! CHECK: func.func @_QPsub_lbounds1() attributes {fir.cuda_attr = #fir.cuda_proc<global>, fir.cuda_launch_bounds = #fir.launch_bounds<maxTPB = 1 : i64, minBPM = 2 : i64>}

attributes(global) launch_bounds(1, 2, 3) subroutine sub_lbounds2(); end
! CHECK: func.func @_QPsub_lbounds2() attributes {fir.cuda_attr = #fir.cuda_proc<global>, fir.cuda_launch_bounds = #fir.launch_bounds<maxTPB = 1 : i64, minBPM = 2 : i64, upperBoundClusterSize = 3 : i64>}