Skip to content

Commit ce2a65a

Browse files
committed
[flang][cuda] Lower match_any_sync functions to nvvm intrinsics
1 parent 496fec5 commit ce2a65a

File tree

7 files changed

+96
-1
lines changed

7 files changed

+96
-1
lines changed

flang/include/flang/Optimizer/Builder/IntrinsicCall.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ struct IntrinsicLibrary {
335335
mlir::Value genMalloc(mlir::Type, llvm::ArrayRef<mlir::Value>);
336336
template <typename Shift>
337337
mlir::Value genMask(mlir::Type, llvm::ArrayRef<mlir::Value>);
338+
mlir::Value genMatchAnySync(mlir::Type, llvm::ArrayRef<mlir::Value>);
338339
fir::ExtendedValue genMatmul(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
339340
fir::ExtendedValue genMatmulTranspose(mlir::Type,
340341
llvm::ArrayRef<fir::ExtendedValue>);

flang/include/flang/Semantics/tools.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ inline bool NeedCUDAAlloc(const Symbol &sym) {
231231
(*details->cudaDataAttr() == common::CUDADataAttr::Device ||
232232
*details->cudaDataAttr() == common::CUDADataAttr::Managed ||
233233
*details->cudaDataAttr() == common::CUDADataAttr::Unified ||
234+
*details->cudaDataAttr() == common::CUDADataAttr::Shared ||
234235
*details->cudaDataAttr() == common::CUDADataAttr::Pinned)) {
235236
return true;
236237
}

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,22 @@ static constexpr IntrinsicHandler handlers[]{
469469
{"malloc", &I::genMalloc},
470470
{"maskl", &I::genMask<mlir::arith::ShLIOp>},
471471
{"maskr", &I::genMask<mlir::arith::ShRUIOp>},
472+
{"match_any_syncjd",
473+
&I::genMatchAnySync,
474+
{{{"mask", asValue}, {"value", asValue}}},
475+
/*isElemental=*/false},
476+
{"match_any_syncjf",
477+
&I::genMatchAnySync,
478+
{{{"mask", asValue}, {"value", asValue}}},
479+
/*isElemental=*/false},
480+
{"match_any_syncjj",
481+
&I::genMatchAnySync,
482+
{{{"mask", asValue}, {"value", asValue}}},
483+
/*isElemental=*/false},
484+
{"match_any_syncjx",
485+
&I::genMatchAnySync,
486+
{{{"mask", asValue}, {"value", asValue}}},
487+
/*isElemental=*/false},
472488
{"matmul",
473489
&I::genMatmul,
474490
{{{"matrix_a", asAddr}, {"matrix_b", asAddr}}},
@@ -6044,6 +6060,31 @@ mlir::Value IntrinsicLibrary::genMask(mlir::Type resultType,
60446060
return result;
60456061
}
60466062

6063+
mlir::Value
6064+
IntrinsicLibrary::genMatchAnySync(mlir::Type resultType,
6065+
llvm::ArrayRef<mlir::Value> args) {
6066+
assert(args.size() == 2);
6067+
bool is32 = args[1].getType().isInteger(32) || args[1].getType().isF32();
6068+
6069+
llvm::StringRef funcName =
6070+
is32 ? "llvm.nvvm.match.any.sync.i32p" : "llvm.nvvm.match.any.sync.i64p";
6071+
mlir::MLIRContext *context = builder.getContext();
6072+
mlir::Type i32Ty = builder.getI32Type();
6073+
mlir::Type i64Ty = builder.getI64Type();
6074+
mlir::Type valTy = is32 ? i32Ty : i64Ty;
6075+
6076+
mlir::FunctionType ftype =
6077+
mlir::FunctionType::get(context, {i32Ty, valTy}, {i32Ty});
6078+
auto funcOp = builder.createFunction(loc, funcName, ftype);
6079+
llvm::SmallVector<mlir::Value> filteredArgs;
6080+
filteredArgs.push_back(args[0]);
6081+
if (args[1].getType().isF32() || args[1].getType().isF64())
6082+
filteredArgs.push_back(builder.create<fir::ConvertOp>(loc, valTy, args[1]));
6083+
else
6084+
filteredArgs.push_back(args[1]);
6085+
return builder.create<fir::CallOp>(loc, funcOp, filteredArgs).getResult(0);
6086+
}
6087+
60476088
// MATMUL
60486089
fir::ExtendedValue
60496090
IntrinsicLibrary::genMatmul(mlir::Type resultType,

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ struct AllocaOpConversion : public fir::FIROpConversion<fir::AllocaOp> {
292292
rewriter.setInsertionPointAfter(size.getDefiningOp());
293293
}
294294

295+
if (auto dataAttr = alloc->getAttrOfType<cuf::DataAttributeAttr>(
296+
cuf::getDataAttrName())) {
297+
if (dataAttr.getValue() == cuf::DataAttribute::Shared)
298+
allocaAs = 3;
299+
}
300+
295301
// NOTE: we used to pass alloc->getAttrs() in the builder for non opaque
296302
// pointers! Only propagate pinned and bindc_name to help debugging, but
297303
// this should have no functional purpose (and passing the operand segment
@@ -316,6 +322,7 @@ struct AllocaOpConversion : public fir::FIROpConversion<fir::AllocaOp> {
316322
rewriter.replaceOpWithNewOp<mlir::LLVM::AddrSpaceCastOp>(
317323
alloc, ::getLlvmPtrType(alloc.getContext(), programAs), llvmAlloc);
318324
}
325+
319326
return mlir::success();
320327
}
321328
};

flang/lib/Optimizer/Dialect/CUF/CUFOps.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ static llvm::LogicalResult checkCudaAttr(Op op) {
5757
if (op.getDataAttr() == cuf::DataAttribute::Device ||
5858
op.getDataAttr() == cuf::DataAttribute::Managed ||
5959
op.getDataAttr() == cuf::DataAttribute::Unified ||
60-
op.getDataAttr() == cuf::DataAttribute::Pinned)
60+
op.getDataAttr() == cuf::DataAttribute::Pinned ||
61+
op.getDataAttr() == cuf::DataAttribute::Shared)
6162
return mlir::success();
6263
return op.emitOpError()
6364
<< "expect device, managed, pinned or unified cuda attribute";

flang/module/cudadevice.f90

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,27 @@ attributes(device) integer(8) function clock64()
562562
end function
563563
end interface
564564

565+
interface match_any_sync
566+
attributes(device) integer function match_any_syncjj(mask, val)
567+
!dir$ ignore_tkr(d) mask, (d) val
568+
integer(4), value :: mask
569+
integer(4), value :: val
570+
end function
571+
attributes(device) integer function match_any_syncjx(mask, val)
572+
!dir$ ignore_tkr(d) mask, (d) val
573+
integer(4), value :: mask
574+
integer(8), value :: val
575+
end function
576+
attributes(device) integer function match_any_syncjf(mask, val)
577+
!dir$ ignore_tkr(d) mask, (d) val
578+
integer(4), value :: mask
579+
real(4), value :: val
580+
end function
581+
attributes(device) integer function match_any_syncjd(mask, val)
582+
!dir$ ignore_tkr(d) mask, (d) val
583+
integer(4), value :: mask
584+
real(8), value :: val
585+
end function
586+
end interface
587+
565588
end module

flang/test/Lower/CUDA/cuda-device-proc.cuf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ end
112112
! CHECK: fir.call @llvm.nvvm.barrier0.popc(%c1{{.*}}) fastmath<contract> : (i32) -> i32
113113
! CHECK: fir.call @llvm.nvvm.barrier0.or(%c1{{.*}}) fastmath<contract> : (i32) -> i32
114114

115+
attributes(device) subroutine testMatchAny()
116+
integer :: a, mask, v32
117+
integer(8) :: v64
118+
real(4) :: r4
119+
real(8) :: r8
120+
a = match_any_sync(mask, v32)
121+
a = match_any_sync(mask, v64)
122+
a = match_any_sync(mask, r4)
123+
a = match_any_sync(mask, r8)
124+
end subroutine
125+
126+
! CHECK-LABEL: func.func @_QPtestmatchany()
127+
! CHECK: fir.call @llvm.nvvm.match.any.sync.i32p
128+
! CHECK: fir.call @llvm.nvvm.match.any.sync.i64p
129+
! CHECK: fir.convert %{{.*}} : (f32) -> i32
130+
! CHECK: fir.call @llvm.nvvm.match.any.sync.i32p
131+
! CHECK: fir.convert %{{.*}} : (f64) -> i64
132+
! CHECK: fir.call @llvm.nvvm.match.any.sync.i64p
133+
115134
! CHECK: func.func private @llvm.nvvm.barrier0()
116135
! CHECK: func.func private @llvm.nvvm.bar.warp.sync(i32)
117136
! CHECK: func.func private @llvm.nvvm.membar.gl()
@@ -120,3 +139,5 @@ end
120139
! CHECK: func.func private @llvm.nvvm.barrier0.and(i32) -> i32
121140
! CHECK: func.func private @llvm.nvvm.barrier0.popc(i32) -> i32
122141
! CHECK: func.func private @llvm.nvvm.barrier0.or(i32) -> i32
142+
! CHECK: func.func private @llvm.nvvm.match.any.sync.i32p(i32, i32) -> i32
143+
! CHECK: func.func private @llvm.nvvm.match.any.sync.i64p(i32, i64) -> i32

0 commit comments

Comments
 (0)