Skip to content

Commit 13f1c79

Browse files
authored
Avoid creating ConstantComposites with spec constants (#3662)
A recent version of spirv-val is now reporting "OpConstantComposite must not have spec constant operands" for some tests. Fix by letting `addCompositeConstant` defer to `addSpecConstantComposite` when attempting to add a constant that has one or more specialization constant operands.
1 parent 12645e3 commit 13f1c79

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "SPIRVValue.h"
5454

5555
#include "llvm/ADT/APInt.h"
56+
#include "llvm/ADT/STLExtras.h"
5657

5758
#include <set>
5859
#include <unordered_map>
@@ -1444,6 +1445,13 @@ SPIRVValue *SPIRVModuleImpl::addNullConstant(SPIRVType *Ty) {
14441445

14451446
SPIRVValue *SPIRVModuleImpl::addCompositeConstant(
14461447
SPIRVType *Ty, const std::vector<SPIRVValue *> &Elements) {
1448+
// Add an OpSpecConstantComposite instead if any of the elements is a
1449+
// SpecConstant.
1450+
if (llvm::any_of(Elements, [](SPIRVValue *V) {
1451+
return isSpecConstantOpCode(V->getOpCode());
1452+
}))
1453+
return addSpecConstantComposite(Ty, Elements);
1454+
14471455
constexpr int MaxNumElements = MaxWordCount - SPIRVConstantComposite::FixedWC;
14481456
const int NumElements = Elements.size();
14491457

test/transcoding/constant-vars.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ target triple = "spir-unknown-unknown"
3939
; CHECK-SPIRV-TYPED-PTR: 7 SpecConstantOp [[AS2]] [[ASTRC:[0-9]+]] 70 [[ASTR]] [[I320]] [[I320]]
4040
; CHECK-SPIRV-UNTYPED-PTR: 8 SpecConstantOp [[AS2]] [[ASTRC:[0-9]+]] 4424 [[#]] [[ASTR]] [[I320]] [[I320]]
4141
; CHECK-SPIRV: 5 SpecConstantOp [[AS1]] [[I64ARRC:[0-9]+]] 124 [[I64ARR]]
42-
; CHECK-SPIRV: 5 ConstantComposite [[STRUCTTY]] [[STRUCT_INIT:[0-9]+]] [[ASTRC]] [[I64ARRC]]
42+
; CHECK-SPIRV: 5 SpecConstantComposite [[STRUCTTY]] [[STRUCT_INIT:[0-9]+]] [[ASTRC]] [[I64ARRC]]
4343
; CHECK-SPIRV-TYPED-PTR: 5 Variable {{[0-9]+}} [[STRUCT:[0-9]+]] 5 [[STRUCT_INIT]]
4444
; CHECK-SPIRV-UNTYPED-PTR: 6 UntypedVariableKHR {{[0-9]+}} [[STRUCT:[0-9]+]] 5 [[STRUCTTY]] [[STRUCT_INIT]]
4545

@@ -49,8 +49,8 @@ target triple = "spir-unknown-unknown"
4949
; CHECK-SPIRV-TYPED-PTR: 5 SpecConstantOp [[AS1]] [[STRUCTC:[0-9]+]] 124 [[STRUCT]]
5050
; CHECK-SPIRV-TYPED-PTR: 7 SpecConstantOp {{[0-9]+}} [[GEP:[0-9]+]] 67 [[I64ARR]]
5151
; CHECK-SPIRV-UNTYPED-PTR: 8 SpecConstantOp {{[0-9]+}} [[GEP:[0-9]+]] 4423 [[#]] [[I64ARR]]
52-
; CHECK-SPIRV-TYPED-PTR: 6 ConstantComposite [[ARRAYTY]] [[ARRAY_INIT:[0-9]+]] [[I64ARRC2]] [[STRUCTC]] [[GEP]]
53-
; CHECK-SPIRV-UNTYPED-PTR: 6 ConstantComposite [[ARRAYTY]] [[ARRAY_INIT:[0-9]+]] [[I64ARRC2]] [[STRUCT]] [[GEP]]
52+
; CHECK-SPIRV-TYPED-PTR: 6 SpecConstantComposite [[ARRAYTY]] [[ARRAY_INIT:[0-9]+]] [[I64ARRC2]] [[STRUCTC]] [[GEP]]
53+
; CHECK-SPIRV-UNTYPED-PTR: 6 SpecConstantComposite [[ARRAYTY]] [[ARRAY_INIT:[0-9]+]] [[I64ARRC2]] [[STRUCT]] [[GEP]]
5454
; CHECK-SPIRV: 5 Variable {{[0-9]+}} [[ARRAY:[0-9]+]] 5 [[ARRAY_INIT]]
5555

5656
; CHECK-LLVM: %structtype = type { ptr addrspace(2), ptr addrspace(1) }

0 commit comments

Comments
 (0)