Skip to content

Commit daae43b

Browse files
committed
Add error for invalid strings
1 parent 754ca1f commit daae43b

File tree

8 files changed

+285
-9
lines changed

8 files changed

+285
-9
lines changed

lib/API/VK/Device.cpp

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ class VKDevice : public offloadtest::Device {
12851285
return llvm::Error::success();
12861286
}
12871287

1288-
static void
1288+
static llvm::Error
12891289
parseSpecializationConstant(const SpecializationConstant &SpecConst,
12901290
VkSpecializationMapEntry &Entry,
12911291
llvm::SmallVector<char> &SpecData) {
@@ -1295,7 +1295,11 @@ class VKDevice : public offloadtest::Device {
12951295
case DataFormat::Float32: {
12961296
float Value = 0.0f;
12971297
double Tmp = 0.0;
1298-
llvm::StringRef(SpecConst.Value).getAsDouble(Tmp);
1298+
if (llvm::StringRef(SpecConst.Value).getAsDouble(Tmp))
1299+
return llvm::createStringError(
1300+
std::errc::invalid_argument,
1301+
"Invalid float value for specialization constant '%s'",
1302+
SpecConst.Value.c_str());
12991303
Value = static_cast<float>(Tmp);
13001304
Entry.size = sizeof(float);
13011305
SpecData.resize(SpecData.size() + sizeof(float));
@@ -1304,47 +1308,71 @@ class VKDevice : public offloadtest::Device {
13041308
}
13051309
case DataFormat::Float64: {
13061310
double Value = 0.0;
1307-
llvm::StringRef(SpecConst.Value).getAsDouble(Value);
1311+
if (llvm::StringRef(SpecConst.Value).getAsDouble(Value))
1312+
return llvm::createStringError(
1313+
std::errc::invalid_argument,
1314+
"Invalid double value for specialization constant '%s'",
1315+
SpecConst.Value.c_str());
13081316
Entry.size = sizeof(double);
13091317
SpecData.resize(SpecData.size() + sizeof(double));
13101318
memcpy(SpecData.data() + Entry.offset, &Value, sizeof(double));
13111319
break;
13121320
}
13131321
case DataFormat::Int16: {
13141322
int16_t Value = 0;
1315-
llvm::StringRef(SpecConst.Value).getAsInteger(0, Value);
1323+
if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value))
1324+
return llvm::createStringError(
1325+
std::errc::invalid_argument,
1326+
"Invalid int16 value for specialization constant '%s'",
1327+
SpecConst.Value.c_str());
13161328
Entry.size = sizeof(int16_t);
13171329
SpecData.resize(SpecData.size() + sizeof(int16_t));
13181330
memcpy(SpecData.data() + Entry.offset, &Value, sizeof(int16_t));
13191331
break;
13201332
}
13211333
case DataFormat::UInt16: {
13221334
uint16_t Value = 0;
1323-
llvm::StringRef(SpecConst.Value).getAsInteger(0, Value);
1335+
if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value))
1336+
return llvm::createStringError(
1337+
std::errc::invalid_argument,
1338+
"Invalid uint16 value for specialization constant '%s'",
1339+
SpecConst.Value.c_str());
13241340
Entry.size = sizeof(uint16_t);
13251341
SpecData.resize(SpecData.size() + sizeof(uint16_t));
13261342
memcpy(SpecData.data() + Entry.offset, &Value, sizeof(uint16_t));
13271343
break;
13281344
}
13291345
case DataFormat::Int32: {
13301346
int32_t Value = 0;
1331-
llvm::StringRef(SpecConst.Value).getAsInteger(0, Value);
1347+
if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value))
1348+
return llvm::createStringError(
1349+
std::errc::invalid_argument,
1350+
"Invalid int32 value for specialization constant '%s'",
1351+
SpecConst.Value.c_str());
13321352
Entry.size = sizeof(int32_t);
13331353
SpecData.resize(SpecData.size() + sizeof(int32_t));
13341354
memcpy(SpecData.data() + Entry.offset, &Value, sizeof(int32_t));
13351355
break;
13361356
}
13371357
case DataFormat::UInt32: {
13381358
uint32_t Value = 0;
1339-
llvm::StringRef(SpecConst.Value).getAsInteger(0, Value);
1359+
if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value))
1360+
return llvm::createStringError(
1361+
std::errc::invalid_argument,
1362+
"Invalid uint32 value for specialization constant '%s'",
1363+
SpecConst.Value.c_str());
13401364
Entry.size = sizeof(uint32_t);
13411365
SpecData.resize(SpecData.size() + sizeof(uint32_t));
13421366
memcpy(SpecData.data() + Entry.offset, &Value, sizeof(uint32_t));
13431367
break;
13441368
}
13451369
case DataFormat::Bool: {
13461370
bool Value = false;
1347-
llvm::StringRef(SpecConst.Value).getAsInteger(0, Value);
1371+
if (llvm::StringRef(SpecConst.Value).getAsInteger(0, Value))
1372+
return llvm::createStringError(
1373+
std::errc::invalid_argument,
1374+
"Invalid bool value for specialization constant '%s'",
1375+
SpecConst.Value.c_str());
13481376
Entry.size = sizeof(bool);
13491377
SpecData.resize(SpecData.size() + sizeof(bool));
13501378
memcpy(SpecData.data() + Entry.offset, &Value, sizeof(bool));
@@ -1353,6 +1381,7 @@ class VKDevice : public offloadtest::Device {
13531381
default:
13541382
llvm_unreachable("Unsupported specialization constant type");
13551383
}
1384+
return llvm::Error::success();
13561385
}
13571386

13581387
llvm::Error createPipeline(Pipeline &P, InvocationState &IS) {
@@ -1380,7 +1409,9 @@ class VKDevice : public offloadtest::Device {
13801409
if (!Shader.SpecializationConstants.empty()) {
13811410
for (const auto &SpecConst : Shader.SpecializationConstants) {
13821411
VkSpecializationMapEntry Entry;
1383-
parseSpecializationConstant(SpecConst, Entry, SpecData);
1412+
if (auto Err =
1413+
parseSpecializationConstant(SpecConst, Entry, SpecData))
1414+
return Err;
13841415
SpecEntries.push_back(Entry);
13851416
}
13861417

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#--- invalid_bool.hlsl
2+
[[vk::constant_id(0)]]
3+
const bool spec_bool = false;
4+
RWStructuredBuffer<uint> OutBool : register(u0, space0);
5+
6+
[numthreads(1,1,1)]
7+
void main(uint GI : SV_GroupIndex) {
8+
OutBool[GI] = (uint)spec_bool;
9+
}
10+
#--- invalid_bool.yaml
11+
---
12+
Shaders:
13+
- Stage: Compute
14+
Entry: main
15+
DispatchSize: [1, 1, 1]
16+
SpecializationConstants:
17+
- { ConstantID: 0, Value: "not a number", Type: Bool }
18+
Buffers:
19+
- { Name: OutBool, Format: Int32, Stride: 4, FillSize: 4 }
20+
DescriptorSets:
21+
- Resources:
22+
- Name: OutBool
23+
Kind: RWStructuredBuffer
24+
DirectXBinding: { Register: 0, Space: 0 }
25+
VulkanBinding: { Binding: 0 }
26+
...
27+
#--- end
28+
29+
# REQUIRES: Vulkan
30+
31+
# RUN: split-file %s %t
32+
# RUN: %dxc_target -T cs_6_2 -Fo %t.o %t/invalid_bool.hlsl
33+
# RUN: not %offloader %t/invalid_bool.yaml %t.o 2>&1 | FileCheck %s
34+
35+
# CHECK: Invalid bool value for specialization constant 'not a number'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#--- invalid_double.hlsl
2+
[[vk::constant_id(0)]]
3+
const double spec_double = 0.0;
4+
RWStructuredBuffer<double> OutDouble : register(u0, space0);
5+
6+
[numthreads(1,1,1)]
7+
void main(uint GI : SV_GroupIndex) {
8+
OutDouble[GI] = spec_double;
9+
}
10+
#--- invalid_double.yaml
11+
---
12+
Shaders:
13+
- Stage: Compute
14+
Entry: main
15+
DispatchSize: [1, 1, 1]
16+
SpecializationConstants:
17+
- { ConstantID: 0, Value: "not a number", Type: Float64 }
18+
Buffers:
19+
- { Name: OutDouble, Format: Float64, Stride: 8, FillSize: 8 }
20+
DescriptorSets:
21+
- Resources:
22+
- Name: OutDouble
23+
Kind: RWStructuredBuffer
24+
DirectXBinding: { Register: 0, Space: 0 }
25+
VulkanBinding: { Binding: 0 }
26+
...
27+
#--- end
28+
29+
# REQUIRES: Vulkan
30+
31+
# RUN: split-file %s %t
32+
# RUN: %dxc_target -T cs_6_2 -Fo %t.o %t/invalid_double.hlsl
33+
# RUN: not %offloader %t/invalid_double.yaml %t.o 2>&1 | FileCheck %s
34+
35+
# CHECK: Invalid double value for specialization constant 'not a number'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#--- invalid_float.hlsl
2+
[[vk::constant_id(0)]]
3+
const float spec_float = 0.0;
4+
RWStructuredBuffer<float> OutFloat : register(u0, space0);
5+
6+
[numthreads(1,1,1)]
7+
void main(uint GI : SV_GroupIndex) {
8+
OutFloat[GI] = spec_float;
9+
}
10+
#--- invalid_float.yaml
11+
---
12+
Shaders:
13+
- Stage: Compute
14+
Entry: main
15+
DispatchSize: [1, 1, 1]
16+
SpecializationConstants:
17+
- { ConstantID: 0, Value: "not a number", Type: Float32 }
18+
Buffers:
19+
- { Name: OutFloat, Format: Float32, Stride: 4, FillSize: 4 }
20+
DescriptorSets:
21+
- Resources:
22+
- Name: OutFloat
23+
Kind: RWStructuredBuffer
24+
DirectXBinding: { Register: 0, Space: 0 }
25+
VulkanBinding: { Binding: 0 }
26+
...
27+
#--- end
28+
29+
# REQUIRES: Vulkan
30+
31+
# RUN: split-file %s %t
32+
# RUN: %dxc_target -T cs_6_2 -Fo %t.o %t/invalid_float.hlsl
33+
# RUN: not %offloader %t/invalid_float.yaml %t.o 2>&1 | FileCheck %s
34+
35+
# CHECK: Invalid float value for specialization constant 'not a number'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#--- invalid_int16.hlsl
2+
[[vk::constant_id(0)]]
3+
const int16_t spec_short = 0;
4+
RWStructuredBuffer<int16_t> OutShort : register(u0, space0);
5+
6+
[numthreads(1,1,1)]
7+
void main(uint GI : SV_GroupIndex) {
8+
OutShort[GI] = spec_short;
9+
}
10+
#--- invalid_int16.yaml
11+
---
12+
Shaders:
13+
- Stage: Compute
14+
Entry: main
15+
DispatchSize: [1, 1, 1]
16+
SpecializationConstants:
17+
- { ConstantID: 0, Value: "not a number", Type: Int16 }
18+
Buffers:
19+
- { Name: OutShort, Format: Int16, Stride: 2, FillSize: 2 }
20+
DescriptorSets:
21+
- Resources:
22+
- Name: OutShort
23+
Kind: RWStructuredBuffer
24+
DirectXBinding: { Register: 0, Space: 0 }
25+
VulkanBinding: { Binding: 0 }
26+
...
27+
#--- end
28+
29+
# REQUIRES: Vulkan
30+
31+
# RUN: split-file %s %t
32+
# RUN: %dxc_target -T cs_6_2 -enable-16bit-types -Fo %t.o %t/invalid_int16.hlsl
33+
# RUN: not %offloader %t/invalid_int16.yaml %t.o 2>&1 | FileCheck %s
34+
35+
# CHECK: Invalid int16 value for specialization constant 'not a number'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#--- invalid_int32.hlsl
2+
[[vk::constant_id(0)]]
3+
const int spec_int = 0;
4+
RWStructuredBuffer<int> OutInt : register(u0, space0);
5+
6+
[numthreads(1,1,1)]
7+
void main(uint GI : SV_GroupIndex) {
8+
OutInt[GI] = spec_int;
9+
}
10+
#--- invalid_int32.yaml
11+
---
12+
Shaders:
13+
- Stage: Compute
14+
Entry: main
15+
DispatchSize: [1, 1, 1]
16+
SpecializationConstants:
17+
- { ConstantID: 0, Value: "not a number", Type: Int32 }
18+
Buffers:
19+
- { Name: OutInt, Format: Int32, Stride: 4, FillSize: 4 }
20+
DescriptorSets:
21+
- Resources:
22+
- Name: OutInt
23+
Kind: RWStructuredBuffer
24+
DirectXBinding: { Register: 0, Space: 0 }
25+
VulkanBinding: { Binding: 0 }
26+
...
27+
#--- end
28+
29+
# REQUIRES: Vulkan
30+
31+
# RUN: split-file %s %t
32+
# RUN: %dxc_target -T cs_6_2 -Fo %t.o %t/invalid_int32.hlsl
33+
# RUN: not %offloader %t/invalid_int32.yaml %t.o 2>&1 | FileCheck %s
34+
35+
# CHECK: Invalid int32 value for specialization constant 'not a number'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#--- invalid_uint16.hlsl
2+
[[vk::constant_id(0)]]
3+
const uint16_t spec_ushort = 0;
4+
RWStructuredBuffer<uint16_t> OutUShort : register(u0, space0);
5+
6+
[numthreads(1,1,1)]
7+
void main(uint GI : SV_GroupIndex) {
8+
OutUShort[GI] = spec_ushort;
9+
}
10+
#--- invalid_uint16.yaml
11+
---
12+
Shaders:
13+
- Stage: Compute
14+
Entry: main
15+
DispatchSize: [1, 1, 1]
16+
SpecializationConstants:
17+
- { ConstantID: 0, Value: "not a number", Type: UInt16 }
18+
Buffers:
19+
- { Name: OutUShort, Format: UInt16, Stride: 2, FillSize: 2 }
20+
DescriptorSets:
21+
- Resources:
22+
- Name: OutUShort
23+
Kind: RWStructuredBuffer
24+
DirectXBinding: { Register: 0, Space: 0 }
25+
VulkanBinding: { Binding: 0 }
26+
...
27+
#--- end
28+
29+
# REQUIRES: Vulkan
30+
31+
# RUN: split-file %s %t
32+
# RUN: %dxc_target -T cs_6_2 -enable-16bit-types -Fo %t.o %t/invalid_uint16.hlsl
33+
# RUN: not %offloader %t/invalid_uint16.yaml %t.o 2>&1 | FileCheck %s
34+
35+
# CHECK: Invalid uint16 value for specialization constant 'not a number'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#--- invalid_uint32.hlsl
2+
[[vk::constant_id(0)]]
3+
const uint spec_uint = 0;
4+
RWStructuredBuffer<uint> OutUInt : register(u0, space0);
5+
6+
[numthreads(1,1,1)]
7+
void main(uint GI : SV_GroupIndex) {
8+
OutUInt[GI] = spec_uint;
9+
}
10+
#--- invalid_uint32.yaml
11+
---
12+
Shaders:
13+
- Stage: Compute
14+
Entry: main
15+
DispatchSize: [1, 1, 1]
16+
SpecializationConstants:
17+
- { ConstantID: 0, Value: "not a number", Type: UInt32 }
18+
Buffers:
19+
- { Name: OutUInt, Format: UInt32, Stride: 4, FillSize: 4 }
20+
DescriptorSets:
21+
- Resources:
22+
- Name: OutUInt
23+
Kind: RWStructuredBuffer
24+
DirectXBinding: { Register: 0, Space: 0 }
25+
VulkanBinding: { Binding: 0 }
26+
...
27+
#--- end
28+
29+
# REQUIRES: Vulkan
30+
31+
# RUN: split-file %s %t
32+
# RUN: %dxc_target -T cs_6_2 -Fo %t.o %t/invalid_uint32.hlsl
33+
# RUN: not %offloader %t/invalid_uint32.yaml %t.o 2>&1 | FileCheck %s
34+
35+
# CHECK: Invalid uint32 value for specialization constant 'not a number'

0 commit comments

Comments
 (0)