Skip to content

[flang][cuda] Update stream type for cuf kernel op #136627

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 1 commit into from
Apr 22, 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
19 changes: 7 additions & 12 deletions flang/include/flang/Optimizer/Dialect/CUF/CUFOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,19 @@ def cuf_KernelOp : cuf_Op<"kernel", [AttrSizedOperandSegments,
represented by a 0 constant value.
}];

let arguments = (ins
Variadic<I32>:$grid, // empty means `*`
Variadic<I32>:$block, // empty means `*`
Optional<I32>:$stream,
Variadic<Index>:$lowerbound,
Variadic<Index>:$upperbound,
Variadic<Index>:$step,
OptionalAttr<I64Attr>:$n,
Variadic<AnyType>:$reduceOperands,
OptionalAttr<ArrayAttr>:$reduceAttrs
);
let arguments = (ins Variadic<I32>:$grid, // empty means `*`
Variadic<I32>:$block, // empty means `*`
Optional<fir_ReferenceType>:$stream, Variadic<Index>:$lowerbound,
Variadic<Index>:$upperbound, Variadic<Index>:$step,
OptionalAttr<I64Attr>:$n, Variadic<AnyType>:$reduceOperands,
OptionalAttr<ArrayAttr>:$reduceAttrs);

let regions = (region AnyRegion:$region);

let assemblyFormat = [{
`<` `<` `<` custom<CUFKernelValues>($grid, type($grid)) `,`
custom<CUFKernelValues>($block, type($block))
( `,` `stream` `=` $stream^ )? `>` `>` `>`
( `,` `stream` `=` $stream^ `:` qualified(type($stream)))? `>` `>` `>`
( `reduce` `(` $reduceOperands^ `:` type($reduceOperands) `:` $reduceAttrs `)` )?
custom<CUFKernelLoopControl>($region, $lowerbound, type($lowerbound),
$upperbound, type($upperbound), $step, type($step))
Expand Down
10 changes: 4 additions & 6 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3097,7 +3097,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {

llvm::SmallVector<mlir::Value> gridValues;
llvm::SmallVector<mlir::Value> blockValues;
mlir::Value streamValue;
mlir::Value streamAddr;

if (launchConfig) {
const std::list<Fortran::parser::CUFKernelDoConstruct::StarOrExpr> &grid =
Expand Down Expand Up @@ -3130,10 +3130,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
}

if (stream)
streamValue = builder->createConvert(
loc, builder->getI32Type(),
fir::getBase(
genExprValue(*Fortran::semantics::GetExpr(*stream), stmtCtx)));
streamAddr = fir::getBase(
genExprAddr(*Fortran::semantics::GetExpr(*stream), stmtCtx));
}

const auto &outerDoConstruct =
Expand Down Expand Up @@ -3267,7 +3265,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
}

auto op = builder->create<cuf::KernelOp>(
loc, gridValues, blockValues, streamValue, lbs, ubs, steps, n,
loc, gridValues, blockValues, streamAddr, lbs, ubs, steps, n,
mlir::ValueRange(reduceOperands), builder->getArrayAttr(reduceAttrs));
builder->createBlock(&op.getRegion(), op.getRegion().end(), ivTypes,
ivLocs);
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/Dialect/CUF/CUFOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ llvm::LogicalResult cuf::KernelOp::verify() {
return emitOpError("expect reduce attributes to be ReduceAttr");
}
}
return mlir::success();
return checkStreamType(*this);
}

//===----------------------------------------------------------------------===//
Expand Down
4 changes: 1 addition & 3 deletions flang/test/Lower/CUDA/cuda-kernel-loop-directive.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ subroutine sub1()
end do
end

! CHECK: %[[STREAM_LOAD:.*]] = fir.load %[[STREAM]]#0 : !fir.ref<i64>
! CHECK: %[[STREAM_I32:.*]] = fir.convert %[[STREAM_LOAD]] : (i64) -> i32
! CHECK: cuf.kernel<<<*, *, stream = %[[STREAM_I32]]>>>
! CHECK: cuf.kernel<<<*, *, stream = %[[STREAM]]#0 : !fir.ref<i64>>>>


! Test lowering with unstructured construct inside.
Expand Down
Loading