-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[mlir][spirv] Fix lookup logic spirv.target_env
for gpu.module
#147262
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
Changes from 12 commits
5d05b68
71d3d92
ae6ba9c
fa3b44e
7d43c59
40b6f07
0b68019
0833b2a
a688ec2
1f38539
9344c20
3f8a379
3703a1e
b8b3c18
1a7d496
b1e810b
6a56521
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,19 +48,45 @@ struct GPUToSPIRVPass final : impl::ConvertGPUToSPIRVBase<GPUToSPIRVPass> { | |
void runOnOperation() override; | ||
|
||
private: | ||
/// Queries the target environment from 'targets' attribute of the given | ||
/// `moduleOp`. | ||
spirv::TargetEnvAttr lookupTargetEnvInTargets(gpu::GPUModuleOp moduleOp); | ||
|
||
/// Queries the target environment from 'targets' attribute of the given | ||
/// `moduleOp` or returns target environment as returned by | ||
/// `spirv::lookupTargetEnvOrDefault` if not provided by 'targets'. | ||
spirv::TargetEnvAttr lookupTargetEnvOrDefault(gpu::GPUModuleOp moduleOp); | ||
bool mapMemorySpace; | ||
}; | ||
|
||
spirv::TargetEnvAttr | ||
oojahooo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GPUToSPIRVPass::lookupTargetEnvInTargets(gpu::GPUModuleOp moduleOp) { | ||
if (const ArrayAttr &targets = moduleOp.getTargetsAttr()) { | ||
oojahooo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
for (const Attribute &targetAttr : targets) | ||
if (auto spirvTargetEnvAttr = dyn_cast<spirv::TargetEnvAttr>(targetAttr)) | ||
return spirvTargetEnvAttr; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
spirv::TargetEnvAttr | ||
GPUToSPIRVPass::lookupTargetEnvOrDefault(gpu::GPUModuleOp moduleOp) { | ||
if (spirv::TargetEnvAttr targetEnvAttr = lookupTargetEnvInTargets(moduleOp)) | ||
return targetEnvAttr; | ||
|
||
return spirv::lookupTargetEnvOrDefault(moduleOp); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the tests, it looks like they both test the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understood correctly, we should test the behavior when the I believe this case might already be covered by existing tests. For instance, gpu-to-spirv.mlir tests the behavior when no target env is present at all, and load-store.mlir covers the case where the target env is not attached to the However, if a more specific test is needed, such as one where the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Correct.
Sounds good, I think we are good. |
||
} | ||
|
||
void GPUToSPIRVPass::runOnOperation() { | ||
MLIRContext *context = &getContext(); | ||
ModuleOp module = getOperation(); | ||
|
||
SmallVector<Operation *, 1> gpuModules; | ||
OpBuilder builder(context); | ||
|
||
auto targetEnvSupportsKernelCapability = [](gpu::GPUModuleOp moduleOp) { | ||
Operation *gpuModule = moduleOp.getOperation(); | ||
auto targetAttr = spirv::lookupTargetEnvOrDefault(gpuModule); | ||
auto targetEnvSupportsKernelCapability = [this](gpu::GPUModuleOp moduleOp) { | ||
auto targetAttr = lookupTargetEnvOrDefault(moduleOp); | ||
spirv::TargetEnv targetEnv(targetAttr); | ||
return targetEnv.allows(spirv::Capability::Kernel); | ||
}; | ||
|
@@ -86,7 +112,7 @@ void GPUToSPIRVPass::runOnOperation() { | |
// TargetEnv attributes. | ||
for (Operation *gpuModule : gpuModules) { | ||
spirv::TargetEnvAttr targetAttr = | ||
spirv::lookupTargetEnvOrDefault(gpuModule); | ||
lookupTargetEnvOrDefault(cast<gpu::GPUModuleOp>(gpuModule)); | ||
|
||
// Map MemRef memory space to SPIR-V storage class first if requested. | ||
if (mapMemorySpace) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// RUN: mlir-opt --convert-gpu-to-spirv %s | FileCheck %s | ||
|
||
module attributes {gpu.container_module} { | ||
// CHECK-LABEL: spirv.module @{{.*}} GLSL450 | ||
gpu.module @kernels [#spirv.target_env<#spirv.vce<v1.0, [Shader], [SPV_KHR_storage_buffer_storage_class]>, #spirv.resource_limits<>>] { | ||
kuhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// CHECK: spirv.func @load_kernel | ||
// CHECK-SAME: %[[ARG:.*]]: !spirv.ptr<!spirv.struct<(!spirv.array<48 x f32, stride=4> [0])>, StorageBuffer> {spirv.interface_var_abi = #spirv.interface_var_abi<(0, 0)>}) | ||
gpu.func @load_kernel(%arg0: memref<12x4xf32>) kernel attributes {spirv.entry_point_abi = #spirv.entry_point_abi<workgroup_size = [16, 1, 1]>} { | ||
%c0 = arith.constant 0 : index | ||
// CHECK: %[[PTR:.*]] = spirv.AccessChain %[[ARG]]{{\[}}{{%.*}}, {{%.*}}{{\]}} | ||
// CHECK-NEXT: {{%.*}} = spirv.Load "StorageBuffer" %[[PTR]] : f32 | ||
%0 = memref.load %arg0[%c0, %c0] : memref<12x4xf32> | ||
// CHECK: spirv.Return | ||
gpu.return | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.