Skip to content

Commit 4b7993c

Browse files
authored
Add option to set the max id (microsoft#6654)
Vulkan implementation can have different limits on the maximum value used as an id in a SPIR-V binary. SPIRV-Tools generall assumes this limit is 0x3FFFFF because all implementations must support at least that value for an id. Since many implementations can support larger values, the tools allows an option that will set a different limit. This commit add an option to DXC to do the same. Fixes microsoft#6636
1 parent 1d19665 commit 4b7993c

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

include/dxc/Support/HLSLOptions.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ def fspv_preserve_interface : Flag<["-"], "fspv-preserve-interface">, Group<spir
412412
HelpText<"Preserves all interface variables in the entry point, even when those variables are unused">;
413413
def fvk_allow_rwstructuredbuffer_arrays: Flag<["-"], "fvk-allow-rwstructuredbuffer-arrays">, Group<spirv_Group>, Flags<[CoreOption, DriverOption, HelpHidden]>,
414414
HelpText<"Allow arrays of RWStructuredBuffers, AppendStructuredBuffers, and ConsumeStructuredBuffers. This is in development, and the option will be removed when the feature is complete.">;
415+
def fspv_max_id : MultiArg<["-"], "fspv-max-id", 1>, MetaVarName<"<shift> <space>">, Group<spirv_Group>, Flags<[CoreOption, DriverOption]>,
416+
HelpText<"Set the maximum value for an id in the SPIR-V binary. Default is 0x3FFFFF, which is the largest value all drivers must support.">;
415417
// SPIRV Change Ends
416418

417419
//////////////////////////////////////////////////////////////////////////////

include/dxc/Support/SPIRVOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct SpirvCodeGenOptions {
8383
SpirvLayoutRule ampPayloadLayoutRule;
8484
llvm::StringRef stageIoOrder;
8585
llvm::StringRef targetEnv;
86+
uint32_t maxId;
8687
llvm::SmallVector<int32_t, 4> bShift;
8788
llvm::SmallVector<int32_t, 4> sShift;
8889
llvm::SmallVector<int32_t, 4> tShift;

lib/DxcSupport/HLSLOptions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,15 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
11791179
opts.SpirvOptions.targetEnv =
11801180
Args.getLastArgValue(OPT_fspv_target_env_EQ, "vulkan1.0");
11811181

1182+
llvm::APInt maxId;
1183+
1184+
// 0X3FFFFF is the default value for -fspv-max-id because it is the largest
1185+
// value that is guaranteed to be allowed in all Vulkan implementations.
1186+
if (Args.getLastArgValue(OPT_fspv_max_id, "3FFFFF").getAsInteger(16, maxId)) {
1187+
errors << "-fspv-max-id must be an integer in hexadecimal format";
1188+
}
1189+
opts.SpirvOptions.maxId = maxId.getLimitedValue(0xFFFFFFFF);
1190+
11821191
// Handle -Oconfig=<comma-separated-list> option.
11831192
uint32_t numOconfigs = 0;
11841193
for (const Arg *A : Args.filtered(OPT_Oconfig)) {

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14415,6 +14415,8 @@ bool SpirvEmitter::spirvToolsValidate(std::vector<uint32_t> *mod,
1441514415
} else {
1441614416
options.SetRelaxBlockLayout(true);
1441714417
}
14418+
options.SetUniversalLimit(spv_validator_limit_max_id_bound,
14419+
spirvOptions.maxId);
1441814420

1441914421
return tools.Validate(mod->data(), mod->size(), options);
1442014422
}
@@ -14487,6 +14489,7 @@ bool SpirvEmitter::spirvToolsTrimCapabilities(std::vector<uint32_t> *mod,
1448714489
spvtools::OptimizerOptions options;
1448814490
options.set_run_validator(false);
1448914491
options.set_preserve_bindings(spirvOptions.preserveBindings);
14492+
options.set_max_id_bound(spirvOptions.maxId);
1449014493

1449114494
optimizer.RegisterPass(spvtools::CreateTrimCapabilitiesPass());
1449214495

@@ -14509,6 +14512,7 @@ bool SpirvEmitter::spirvToolsOptimize(std::vector<uint32_t> *mod,
1450914512
spvtools::OptimizerOptions options;
1451014513
options.set_run_validator(false);
1451114514
options.set_preserve_bindings(spirvOptions.preserveBindings);
14515+
options.set_max_id_bound(spirvOptions.maxId);
1451214516

1451314517
if (spirvOptions.optConfig.empty()) {
1451414518
// Add performance passes.
@@ -14550,6 +14554,8 @@ bool SpirvEmitter::spirvToolsLegalize(std::vector<uint32_t> *mod,
1455014554
spvtools::OptimizerOptions options;
1455114555
options.set_run_validator(false);
1455214556
options.set_preserve_bindings(spirvOptions.preserveBindings);
14557+
options.set_max_id_bound(spirvOptions.maxId);
14558+
1455314559
// Add interface variable SROA if the signature packing is enabled.
1455414560
if (spirvOptions.signaturePacking) {
1455514561
optimizer.RegisterPass(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: not %dxc -T cs_6_0 -E main %s -spirv -fspv-max-id 30 2>&1 | FileCheck %s --check-prefix=CHECK-30
2+
// RUN: %dxc -T cs_6_0 -E main %s -spirv -fspv-max-id 400 2>&1 | FileCheck %s --check-prefix=CHECK-400
3+
4+
// With a lower limit, there will be an ID overflow in the optimizer. Note that
5+
// the error message can vary depending on where the optimizer fails. This test
6+
// is low enough to fail as early as possible leading to a consistent error
7+
// message.
8+
// CHECK-30: fatal error: failed to optimize SPIR-V: ID overflow. Try running compact-ids.
9+
10+
// With a larger limit, the test case can compile successfully.
11+
// CHECK-400: Bound: 204
12+
13+
14+
RWStructuredBuffer<int> data;
15+
16+
[numthreads(1,1,1)]
17+
void main(uint3 id : SV_DispatchThreadID)
18+
{
19+
[[unroll]]
20+
for( int i = 0; i < 64; i++ ) {
21+
data[i] = i;
22+
}
23+
}

0 commit comments

Comments
 (0)