Skip to content

[flang][cuda] Relax the verifier for cuf.register_kernel op #112585

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
Oct 17, 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
27 changes: 18 additions & 9 deletions flang/lib/Optimizer/Dialect/CUF/CUFOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "flang/Optimizer/Dialect/FIRAttr.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
Expand Down Expand Up @@ -276,18 +277,26 @@ mlir::LogicalResult cuf::RegisterKernelOp::verify() {

mlir::SymbolTable symTab(mod);
auto gpuMod = symTab.lookup<mlir::gpu::GPUModuleOp>(getKernelModuleName());
if (!gpuMod)
if (!gpuMod) {
// If already a gpu.binary then stop the check here.
if (symTab.lookup<mlir::gpu::BinaryOp>(getKernelModuleName()))
return mlir::success();
return emitOpError("gpu module not found");
}

mlir::SymbolTable gpuSymTab(gpuMod);
auto func = gpuSymTab.lookup<mlir::gpu::GPUFuncOp>(getKernelName());
if (!func)
return emitOpError("device function not found");

if (!func.isKernel())
return emitOpError("only kernel gpu.func can be registered");

return mlir::success();
if (auto func = gpuSymTab.lookup<mlir::gpu::GPUFuncOp>(getKernelName())) {
if (!func.isKernel())
return emitOpError("only kernel gpu.func can be registered");
return mlir::success();
} else if (auto func =
gpuSymTab.lookup<mlir::LLVM::LLVMFuncOp>(getKernelName())) {
if (!func->getAttrOfType<mlir::UnitAttr>(
mlir::gpu::GPUDialect::getKernelFuncAttrName()))
return emitOpError("only gpu.kernel llvm.func can be registered");
return mlir::success();
}
return emitOpError("device function not found");
}

// Tablegen operators
Expand Down
15 changes: 15 additions & 0 deletions flang/test/Fir/cuf-invalid.fir
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,18 @@ module attributes {gpu.container_module} {
llvm.return
}
}

// -----

module attributes {gpu.container_module} {
gpu.module @cuda_device_mod {
llvm.func @_QPsub_device1() {
llvm.return
}
}
llvm.func internal @__cudaFortranConstructor() {
// expected-error@+1{{'cuf.register_kernel' op only gpu.kernel llvm.func can be registered}}
cuf.register_kernel @cuda_device_mod::@_QPsub_device1
llvm.return
}
}
Loading