From d50c3f34a2eda5bf5e278c78d32cc9923fd83b82 Mon Sep 17 00:00:00 2001 From: aidanfnv Date: Tue, 1 Jul 2025 00:41:52 -0700 Subject: Add arguments for controlling floating point denormal mode (#7461) * Implement -fp-denorm-mode slangc arg * Split fp-denorm-mode into 3 args for fp16/32/64 * Remove redundant option categories * Use emitInst for multiple of the same OpExecutionMode * Fix formatting * Remove -denorm any * Re-add option categories * emitinst for ftz * Use enums for type text * Remove extra categories again * Add tests for denorm mode * Move denorm mode to post linking * format code (#8) Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> * regenerate command line reference (#9) Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> * Clean up tests * Fix option text * format code (#10) Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> * Add tests for "any" mode * Return "any" enum if option not set * Simplify emission logic * Add support for generic entrypoints * Move denorm modes to end of CompilerOptionName enum * format code (#11) Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> * Move new enum members to before CountOf * Add not checks to tests, fix generic test, add functionality tests * Rename denorm to fpDenormal * Clean up functional test * Rename denorm test dir * Fix formatting, regenerate cmdline ref * Fold simple tests into functional tests, add more dxil checks * Remove no-op DX tests, make tests more consistent * Disable VK functionality tests that will fail on the CI configs * Fix formatting * Add comments to disabled tests explaining why --------- Co-authored-by: slangbot Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- source/slang/slang-emit.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) (limited to 'source/slang/slang-emit.cpp') diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index f4d535466..f92eedaa4 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -617,6 +617,85 @@ static void unexportNonEmbeddableIR(CodeGenTarget target, IRModule* irModule) } } +// Add DenormPreserve and DenormFlushToZero decorations to all entry point functions +static void addDenormalModeDecorations(IRModule* irModule, CodeGenContext* codeGenContext) +{ + auto optionSet = codeGenContext->getTargetProgram()->getOptionSet(); + + // Only add decorations if we have floating point denormal handling mode options set + auto denormalModeFp16 = optionSet.getDenormalModeFp16(); + auto denormalModeFp32 = optionSet.getDenormalModeFp32(); + auto denormalModeFp64 = optionSet.getDenormalModeFp64(); + + if (denormalModeFp16 == FloatingPointDenormalMode::Any && + denormalModeFp32 == FloatingPointDenormalMode::Any && + denormalModeFp64 == FloatingPointDenormalMode::Any) + return; + + IRBuilder builder(irModule); + + // Apply floating point denormal handling mode decorations to all entry point functions + for (auto inst : irModule->getGlobalInsts()) + { + IRFunc* func = nullptr; + + // Check if this is a direct function + if (auto directFunc = as(inst)) + { + func = directFunc; + } + // Check if this is a generic that contains an entry point function + else if (auto generic = as(inst)) + { + if (auto innerFunc = as(findGenericReturnVal(generic))) + { + func = innerFunc; + } + } + + if (!func) + continue; + + // Check if this is an entry point function + auto entryPoint = func->findDecoration(); + if (!entryPoint) + continue; + + // Handle FP16 denormal handling mode + auto width16 = builder.getIntValue(builder.getUIntType(), 16); + if (denormalModeFp16 == FloatingPointDenormalMode::Preserve) + { + builder.addFpDenormalPreserveDecoration(func, width16); + } + else if (denormalModeFp16 == FloatingPointDenormalMode::FlushToZero) + { + builder.addFpDenormalFlushToZeroDecoration(func, width16); + } + + // Handle FP32 denormal handling mode + auto width32 = builder.getIntValue(builder.getUIntType(), 32); + if (denormalModeFp32 == FloatingPointDenormalMode::Preserve) + { + builder.addFpDenormalPreserveDecoration(func, width32); + } + else if (denormalModeFp32 == FloatingPointDenormalMode::FlushToZero) + { + builder.addFpDenormalFlushToZeroDecoration(func, width32); + } + + // Handle FP64 denormal handling mode + auto width64 = builder.getIntValue(builder.getUIntType(), 64); + if (denormalModeFp64 == FloatingPointDenormalMode::Preserve) + { + builder.addFpDenormalPreserveDecoration(func, width64); + } + else if (denormalModeFp64 == FloatingPointDenormalMode::FlushToZero) + { + builder.addFpDenormalFlushToZeroDecoration(func, width64); + } + } +} + // Helper function to convert a 20 byte SHA1 to a hexadecimal string, // needed for the build identifier instruction. String getBuildIdentifierString(ComponentType* component) @@ -755,6 +834,15 @@ Result linkAndOptimizeIR( checkEntryPointDecorations(irModule, target, sink); + // Add floating point denormal handling mode decorations to entry point functions based on + // compiler options. This is done post-linking to ensure all entry points from linked modules + // are processed. + addDenormalModeDecorations(irModule, codeGenContext); +#if 0 + dumpIRIfEnabled(codeGenContext, irModule, "FP DENORMAL MODE DECORATIONS ADDED"); +#endif + validateIRModuleIfEnabled(codeGenContext, irModule); + // Another transformation that needed to wait until we // had layout information on parameters is to take uniform // parameters of a shader entry point and move them into -- cgit v1.2.3