Skip to content
Open
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
31 changes: 24 additions & 7 deletions mlir/lib/Dialect/GPU/Transforms/KernelOutlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,11 @@ class GpuKernelOutliningPass
// Create nested module and insert outlinedFunc. The module will
// originally get the same name as the function, but may be renamed on
// insertion into the parent module.
auto kernelModule = createKernelModule(op, outlinedFunc, symbolTable);
auto kernelModuleOrFailure =
createKernelModule(op, outlinedFunc, symbolTable);
if (failed(kernelModuleOrFailure))
return WalkResult::interrupt();
auto kernelModule = *kernelModuleOrFailure;
symbolTable.insert(kernelModule, insertPt);

// Potentially changes signature, pulling in constants.
Expand All @@ -392,9 +396,9 @@ class GpuKernelOutliningPass

private:
/// Returns a gpu.module containing kernelFunc and all callees (recursive).
gpu::GPUModuleOp createKernelModule(gpu::LaunchOp gpuLaunchOp,
gpu::GPUFuncOp kernelFunc,
const SymbolTable &parentSymbolTable) {
FailureOr<gpu::GPUModuleOp>
createKernelModule(gpu::LaunchOp gpuLaunchOp, gpu::GPUFuncOp kernelFunc,
const SymbolTable &parentSymbolTable) {
// TODO: This code cannot use an OpBuilder because it must be inserted into
// a SymbolTable by the caller. SymbolTable needs to be refactored to
// prevent manual building of Ops with symbols in code using SymbolTables
Expand Down Expand Up @@ -431,12 +435,25 @@ class GpuKernelOutliningPass
if (std::optional<SymbolTable::UseRange> symbolUses =
SymbolTable::getSymbolUses(symbolDefWorklist.pop_back_val())) {
for (SymbolTable::SymbolUse symbolUse : *symbolUses) {
StringAttr symbolName = symbolUse.getSymbolRef().getLeafReference();
SymbolRefAttr symbolRef = symbolUse.getSymbolRef();
StringAttr symbolName = symbolRef.getLeafReference();
if (symbolTable.lookup(symbolName))
continue;

Operation *symbolDefClone =
parentSymbolTable.lookup(symbolName)->clone();
Operation *symbolDef =
SymbolTable::lookupSymbolIn(parentSymbolTable.getOp(), symbolRef);
if (!symbolDef) {
if (isa<FlatSymbolRefAttr>(symbolRef)) {
return symbolUse.getUser()->emitOpError(
"failed to outline gpu kernel: symbol '" +
symbolName.getValue() + "' not found");
}
return symbolUse.getUser()->emitOpError(
"failed to outline gpu kernel: "
"found invalid symbol reference: ")
<< symbolRef;
}
Operation *symbolDefClone = symbolDef->clone();
symbolDefWorklist.push_back(symbolDefClone);
symbolTable.insert(symbolDefClone);
}
Expand Down
29 changes: 29 additions & 0 deletions mlir/test/Dialect/GPU/outlining-invalid-symbol.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: mlir-opt -gpu-kernel-outlining -verify-diagnostics -split-input-file %s

module attributes {gpu.container_module} {
func.func @kernel_crash() {
%c1 = arith.constant 1 : index
gpu.launch blocks(%bx, %by, %bz) in (%grid_x = %c1, %grid_y = %c1, %grid_z = %c1)
threads(%tx, %ty, %tz) in (%block_x = %c1, %block_y = %c1, %block_z = %c1) {
// expected-error@+1 {{failed to outline gpu kernel: symbol 'unknown_func' not found}}
"test.op"() {symbol = @unknown_func} : () -> ()
gpu.terminator
}
return
}
}

// -----

module attributes {gpu.container_module} {
func.func @kernel_invalid_ref() {
%c1 = arith.constant 1 : index
gpu.launch blocks(%bx, %by, %bz) in (%grid_x = %c1, %grid_y = %c1, %grid_z = %c1)
threads(%tx, %ty, %tz) in (%block_x = %c1, %block_y = %c1, %block_z = %c1) {
// expected-error@+1 {{failed to outline gpu kernel: found invalid symbol reference: @nested::@ref}}
"test.op"() {symbol = @nested::@ref} : () -> ()
gpu.terminator
}
return
}
}