Skip to content

Commit 6d9eb31

Browse files
authored
[mlir][spirv] Fix spirv.Select min version requirement (#72173)
Per the spec, "Before version 1.4, results are only computed per component." So using scalar condition to select composite needs SPIR-V v1.4 at least.
1 parent 8b5eacb commit 6d9eb31

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

mlir/include/mlir/Dialect/SPIRV/IR/SPIRVLogicalOps.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,10 @@ def SPIRV_SelectOp : SPIRV_Op<"Select",
10021002
let assemblyFormat = [{
10031003
operands attr-dict `:` type($condition) `,` type($result)
10041004
}];
1005+
1006+
// These ops require dynamic availability specification based on operand and
1007+
// result types.
1008+
bit autogenAvailability = 0;
10051009
}
10061010

10071011
// -----

mlir/lib/Dialect/SPIRV/IR/ControlFlowOps.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
1314
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
15+
#include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
1416
#include "mlir/Interfaces/CallInterfaces.h"
1517

1618
#include "SPIRVOpUtils.h"
@@ -429,6 +431,27 @@ LogicalResult SelectOp::verify() {
429431
return success();
430432
}
431433

434+
// Custom availability implementation is needed for spirv.Select given the
435+
// syntax changes starting v1.4.
436+
SmallVector<ArrayRef<spirv::Extension>, 1> SelectOp::getExtensions() {
437+
return {};
438+
}
439+
SmallVector<ArrayRef<spirv::Capability>, 1> SelectOp::getCapabilities() {
440+
return {};
441+
}
442+
std::optional<spirv::Version> SelectOp::getMinVersion() {
443+
// Per the spec, "Before version 1.4, results are only computed per
444+
// component."
445+
if (isa<spirv::ScalarType>(getCondition().getType()) &&
446+
isa<spirv::CompositeType>(getType()))
447+
return Version::V_1_4;
448+
449+
return Version::V_1_0;
450+
}
451+
std::optional<spirv::Version> SelectOp::getMaxVersion() {
452+
return Version::V_1_6;
453+
}
454+
432455
//===----------------------------------------------------------------------===//
433456
// spirv.mlir.selection
434457
//===----------------------------------------------------------------------===//

mlir/test/Dialect/SPIRV/Transforms/vce-deduction.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ spirv.module Logical GLSL450 attributes {
3232
}
3333
}
3434

35+
// CHECK: requires #spirv.vce<v1.4, [Shader], []>
36+
spirv.module Logical GLSL450 attributes {
37+
spirv.target_env = #spirv.target_env<#spirv.vce<v1.6, [Shader], []>, #spirv.resource_limits<>>
38+
} {
39+
spirv.func @select_with_scalar_condition(%predicate : i1, %a: vector<2xf32>, %b: vector<2xf32>) -> vector<2xf32> "None" {
40+
%0 = spirv.Select %predicate, %a, %b : i1, vector<2xf32>
41+
spirv.ReturnValue %0: vector<2xf32>
42+
}
43+
}
44+
3545
//===----------------------------------------------------------------------===//
3646
// Capability
3747
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)