Skip to content

Commit 797594a

Browse files
committed
[mlir][spirv] Fix nullptr dereference in UnifyAliasedResource
Fixes: llvm#62368 Reviewed By: antiagainst Differential Revision: https://reviews.llvm.org/D149376
1 parent d636bcb commit 797594a

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

mlir/lib/Dialect/SPIRV/Transforms/UnifyAliasedResourcePass.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ ResourceAliasAnalysis::ResourceAliasAnalysis(Operation *root) {
220220
}
221221

222222
bool ResourceAliasAnalysis::shouldUnify(Operation *op) const {
223+
if (!op)
224+
return false;
225+
223226
if (auto varOp = dyn_cast<spirv::GlobalVariableOp>(op)) {
224227
auto canonicalOp = getCanonicalResource(varOp);
225228
return canonicalOp && varOp != canonicalOp;
@@ -566,16 +569,15 @@ class UnifyAliasedResourcePass final
566569
private:
567570
spirv::GetTargetEnvFn getTargetEnvFn;
568571
};
569-
} // namespace
570572

571573
void UnifyAliasedResourcePass::runOnOperation() {
572574
spirv::ModuleOp moduleOp = getOperation();
573575
MLIRContext *context = &getContext();
574576

575577
if (getTargetEnvFn) {
576-
// This pass is only needed for targeting WebGPU, Metal, or layering Vulkan
577-
// on Metal via MoltenVK, where we need to translate SPIR-V into WGSL or
578-
// MSL. The translation has limitations.
578+
// This pass is only needed for targeting WebGPU, Metal, or layering
579+
// Vulkan on Metal via MoltenVK, where we need to translate SPIR-V into
580+
// WGSL or MSL. The translation has limitations.
579581
spirv::TargetEnvAttr targetEnv = getTargetEnvFn(moduleOp);
580582
spirv::ClientAPI clientAPI = targetEnv.getClientAPI();
581583
bool isVulkanOnAppleDevices =
@@ -614,6 +616,7 @@ void UnifyAliasedResourcePass::runOnOperation() {
614616
resources.front()->removeAttr("aliased");
615617
}
616618
}
619+
} // namespace
617620

618621
std::unique_ptr<mlir::OperationPass<spirv::ModuleOp>>
619622
spirv::createUnifyAliasedResourcePass(spirv::GetTargetEnvFn getTargetEnv) {

mlir/test/Dialect/SPIRV/Transforms/unify-aliased-resource.mlir

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,19 @@ spirv.module Logical GLSL450 {
506506
// CHECK: %[[CC:.+]] = spirv.CompositeConstruct %[[BC0]], %[[BC1]] : (vector<2xf32>, vector<2xf32>) -> vector<4xf32>
507507
// CHECK: spirv.ReturnValue %[[CC]]
508508

509+
// -----
510+
511+
// Make sure we do not crash on function arguments.
512+
513+
spirv.module Logical GLSL450 {
514+
spirv.func @main(%arg0: !spirv.ptr<!spirv.struct<(!spirv.rtarray<f32, stride=4> [0])>, StorageBuffer>) "None" {
515+
%cst0_i32 = spirv.Constant 0 : i32
516+
%0 = spirv.AccessChain %arg0[%cst0_i32, %cst0_i32] : !spirv.ptr<!spirv.struct<(!spirv.rtarray<f32, stride=4> [0])>, StorageBuffer>, i32, i32
517+
spirv.Return
518+
}
519+
}
520+
521+
// CHECK-LABEL: spirv.module
522+
// CHECK-LABEL: spirv.func @main
523+
// CHECK-SAME: (%{{.+}}: !spirv.ptr<!spirv.struct<(!spirv.rtarray<f32, stride=4> [0])>, StorageBuffer>) "None"
524+
// CHECK: spirv.Return

0 commit comments

Comments
 (0)