From: Sven van Haastregt Date: Wed, 18 Mar 2026 14:18:14 +0100 Subject: [09/13] [Backport to 22] Avoid creating ConstantComposites with spec constants (#3662) Origin: upstream, https://github.com/KhronosGroup/SPIRV-LLVM-Translator/commit/e9d0bd15b22ae2db5ce532c71bf2266b9d5821b5 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. --- lib/SPIRV/libSPIRV/SPIRVModule.cpp | 8 ++++++++ test/transcoding/constant-vars.ll | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/SPIRV/libSPIRV/SPIRVModule.cpp b/lib/SPIRV/libSPIRV/SPIRVModule.cpp index 18eb938f..270ea451 100644 --- a/lib/SPIRV/libSPIRV/SPIRVModule.cpp +++ b/lib/SPIRV/libSPIRV/SPIRVModule.cpp @@ -53,6 +53,7 @@ #include "SPIRVValue.h" #include "llvm/ADT/APInt.h" +#include "llvm/ADT/STLExtras.h" #include #include @@ -1453,6 +1454,13 @@ SPIRVValue *SPIRVModuleImpl::addNullConstant(SPIRVType *Ty) { SPIRVValue *SPIRVModuleImpl::addCompositeConstant( SPIRVType *Ty, const std::vector &Elements) { + // Add an OpSpecConstantComposite instead if any of the elements is a + // SpecConstant. + if (llvm::any_of(Elements, [](SPIRVValue *V) { + return isSpecConstantOpCode(V->getOpCode()); + })) + return addSpecConstantComposite(Ty, Elements); + constexpr int MaxNumElements = MaxWordCount - SPIRVConstantComposite::FixedWC; const int NumElements = Elements.size(); diff --git a/test/transcoding/constant-vars.ll b/test/transcoding/constant-vars.ll index a680d602..a84bb094 100644 --- a/test/transcoding/constant-vars.ll +++ b/test/transcoding/constant-vars.ll @@ -39,7 +39,7 @@ target triple = "spir-unknown-unknown" ; CHECK-SPIRV-TYPED-PTR: 7 SpecConstantOp [[AS2]] [[ASTRC:[0-9]+]] 70 [[ASTR]] [[I320]] [[I320]] ; CHECK-SPIRV-UNTYPED-PTR: 8 SpecConstantOp [[AS2]] [[ASTRC:[0-9]+]] 4424 [[#]] [[ASTR]] [[I320]] [[I320]] ; CHECK-SPIRV: 5 SpecConstantOp [[AS1]] [[I64ARRC:[0-9]+]] 124 [[I64ARR]] -; CHECK-SPIRV: 5 ConstantComposite [[STRUCTTY]] [[STRUCT_INIT:[0-9]+]] [[ASTRC]] [[I64ARRC]] +; CHECK-SPIRV: 5 SpecConstantComposite [[STRUCTTY]] [[STRUCT_INIT:[0-9]+]] [[ASTRC]] [[I64ARRC]] ; CHECK-SPIRV-TYPED-PTR: 5 Variable {{[0-9]+}} [[STRUCT:[0-9]+]] 5 [[STRUCT_INIT]] ; CHECK-SPIRV-UNTYPED-PTR: 6 UntypedVariableKHR {{[0-9]+}} [[STRUCT:[0-9]+]] 5 [[STRUCTTY]] [[STRUCT_INIT]] @@ -49,8 +49,8 @@ target triple = "spir-unknown-unknown" ; CHECK-SPIRV-TYPED-PTR: 5 SpecConstantOp [[AS1]] [[STRUCTC:[0-9]+]] 124 [[STRUCT]] ; CHECK-SPIRV-TYPED-PTR: 7 SpecConstantOp {{[0-9]+}} [[GEP:[0-9]+]] 67 [[I64ARR]] ; CHECK-SPIRV-UNTYPED-PTR: 8 SpecConstantOp {{[0-9]+}} [[GEP:[0-9]+]] 4423 [[#]] [[I64ARR]] -; CHECK-SPIRV-TYPED-PTR: 6 ConstantComposite [[ARRAYTY]] [[ARRAY_INIT:[0-9]+]] [[I64ARRC2]] [[STRUCTC]] [[GEP]] -; CHECK-SPIRV-UNTYPED-PTR: 6 ConstantComposite [[ARRAYTY]] [[ARRAY_INIT:[0-9]+]] [[I64ARRC2]] [[STRUCT]] [[GEP]] +; CHECK-SPIRV-TYPED-PTR: 6 SpecConstantComposite [[ARRAYTY]] [[ARRAY_INIT:[0-9]+]] [[I64ARRC2]] [[STRUCTC]] [[GEP]] +; CHECK-SPIRV-UNTYPED-PTR: 6 SpecConstantComposite [[ARRAYTY]] [[ARRAY_INIT:[0-9]+]] [[I64ARRC2]] [[STRUCT]] [[GEP]] ; CHECK-SPIRV: 5 Variable {{[0-9]+}} [[ARRAY:[0-9]+]] 5 [[ARRAY_INIT]] ; CHECK-LLVM: %structtype = type { ptr addrspace(2), ptr addrspace(1) } -- 2.55.0